ovs-atomic-pthreads: Use global shared locks for atomic_flag also.
[sliver-openvswitch.git] / lib / ovs-atomic-pthreads.h
1 /*
2  * Copyright (c) 2013, 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* This header implements atomic operation primitives using pthreads. */
18 #ifndef IN_OVS_ATOMIC_H
19 #error "This header should only be included indirectly via ovs-atomic.h."
20 #endif
21
22 #include "ovs-atomic-locked.h"
23
24 #define OVS_ATOMIC_PTHREADS_IMPL 1
25
26 #define ATOMIC(TYPE) TYPE
27 #include "ovs-atomic-types.h"
28
29 #define ATOMIC_BOOL_LOCK_FREE 0
30 #define ATOMIC_CHAR_LOCK_FREE 0
31 #define ATOMIC_SHORT_LOCK_FREE 0
32 #define ATOMIC_INT_LOCK_FREE 0
33 #define ATOMIC_LONG_LOCK_FREE 0
34 #define ATOMIC_LLONG_LOCK_FREE 0
35 #define ATOMIC_POINTER_LOCK_FREE 0
36
37 typedef enum {
38     memory_order_relaxed,
39     memory_order_consume,
40     memory_order_acquire,
41     memory_order_release,
42     memory_order_acq_rel,
43     memory_order_seq_cst
44 } memory_order;
45
46 #define ATOMIC_VAR_INIT(VALUE) (VALUE)
47 #define atomic_init(OBJECT, VALUE) (*(OBJECT) = (VALUE), (void) 0)
48 #define atomic_destroy(OBJECT) ((void) (OBJECT))
49
50 static inline void
51 atomic_thread_fence(memory_order order OVS_UNUSED)
52 {
53     /* Nothing to do. */
54 }
55
56 static inline void
57 atomic_signal_fence(memory_order order OVS_UNUSED)
58 {
59     /* Nothing to do. */
60 }
61
62 #define atomic_is_lock_free(OBJ) false
63
64 #define atomic_store(DST, SRC) atomic_store_locked(DST, SRC)
65 #define atomic_store_explicit(DST, SRC, ORDER) \
66     ((void) (ORDER), atomic_store(DST, SRC))
67
68 #define atomic_read(SRC, DST) atomic_read_locked(SRC, DST)
69 #define atomic_read_explicit(SRC, DST, ORDER)   \
70     ((void) (ORDER), atomic_read(SRC, DST))
71
72 #define atomic_add(RMW, ARG, ORIG) atomic_op_locked(RMW, add, ARG, ORIG)
73 #define atomic_sub(RMW, ARG, ORIG) atomic_op_locked(RMW, sub, ARG, ORIG)
74 #define atomic_or( RMW, ARG, ORIG) atomic_op_locked(RMW, or, ARG, ORIG)
75 #define atomic_xor(RMW, ARG, ORIG) atomic_op_locked(RMW, xor, ARG, ORIG)
76 #define atomic_and(RMW, ARG, ORIG) atomic_op_locked(RMW, and, ARG, ORIG)
77
78 #define atomic_add_explicit(RMW, ARG, ORIG, ORDER)  \
79     ((void) (ORDER), atomic_add(RMW, ARG, ORIG))
80 #define atomic_sub_explicit(RMW, ARG, ORIG, ORDER)  \
81     ((void) (ORDER), atomic_sub(RMW, ARG, ORIG))
82 #define atomic_or_explicit(RMW, ARG, ORIG, ORDER)   \
83     ((void) (ORDER), atomic_or(RMW, ARG, ORIG))
84 #define atomic_xor_explicit(RMW, ARG, ORIG, ORDER)  \
85     ((void) (ORDER), atomic_xor(RMW, ARG, ORIG))
86 #define atomic_and_explicit(RMW, ARG, ORIG, ORDER)  \
87     ((void) (ORDER), atomic_and(RMW, ARG, ORIG))
88 \f
89 /* atomic_flag */
90
91 typedef struct {
92     bool b;
93 } atomic_flag;
94 #define ATOMIC_FLAG_INIT { false }
95
96 static inline void
97 atomic_flag_init(volatile atomic_flag *flag OVS_UNUSED)
98 {
99     /* Nothing to do. */
100 }
101
102 static inline void
103 atomic_flag_destroy(volatile atomic_flag *flag OVS_UNUSED)
104 {
105     /* Nothing to do. */
106 }
107
108 static inline bool
109 atomic_flag_test_and_set(volatile atomic_flag *flag_)
110 {
111     atomic_flag *flag = CONST_CAST(atomic_flag *, flag_);
112     bool old_value;
113
114     atomic_lock__(flag);
115     old_value = flag->b;
116     flag->b = true;
117     atomic_unlock__(flag);
118
119     return old_value;
120 }
121
122 static inline bool
123 atomic_flag_test_and_set_explicit(volatile atomic_flag *flag,
124                                   memory_order order OVS_UNUSED)
125 {
126     return atomic_flag_test_and_set(flag);
127 }
128
129 static inline void
130 atomic_flag_clear(volatile atomic_flag *flag_)
131 {
132     atomic_flag *flag = CONST_CAST(atomic_flag *, flag_);
133
134     atomic_lock__(flag);
135     flag->b = false;
136     atomic_unlock__(flag);
137 }
138
139 static inline void
140 atomic_flag_clear_explicit(volatile atomic_flag *flag,
141                            memory_order order OVS_UNUSED)
142 {
143     atomic_flag_clear(flag);
144 }