ofproto-provider: Update documentation.
[sliver-openvswitch.git] / lib / ovs-atomic-pthreads.h
1 /*
2  * Copyright (c) 2013 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 #define OVS_ATOMIC_PTHREADS_IMPL 1
23
24 #define DEFINE_PTHREAD_ATOMIC(TYPE, NAME)       \
25     typedef struct {                            \
26         TYPE value;                             \
27         pthread_mutex_t mutex;                  \
28     } NAME;
29
30 #define ATOMIC_BOOL_LOCK_FREE 0
31 DEFINE_PTHREAD_ATOMIC(bool, atomic_bool);
32
33 #define ATOMIC_CHAR_LOCK_FREE 0
34 DEFINE_PTHREAD_ATOMIC(char, atomic_char);
35 DEFINE_PTHREAD_ATOMIC(signed char, atomic_schar);
36 DEFINE_PTHREAD_ATOMIC(unsigned char, atomic_uchar);
37
38 #define ATOMIC_SHORT_LOCK_FREE 0
39 DEFINE_PTHREAD_ATOMIC(short, atomic_short);
40 DEFINE_PTHREAD_ATOMIC(unsigned short, atomic_ushort);
41
42 #define ATOMIC_INT_LOCK_FREE 0
43 DEFINE_PTHREAD_ATOMIC(int, atomic_int);
44 DEFINE_PTHREAD_ATOMIC(unsigned int, atomic_uint);
45
46 #define ATOMIC_LONG_LOCK_FREE 0
47 DEFINE_PTHREAD_ATOMIC(long, atomic_long);
48 DEFINE_PTHREAD_ATOMIC(unsigned long, atomic_ulong);
49
50 #define ATOMIC_LLONG_LOCK_FREE 0
51 DEFINE_PTHREAD_ATOMIC(long long, atomic_llong);
52 DEFINE_PTHREAD_ATOMIC(unsigned long long, atomic_ullong);
53
54 DEFINE_PTHREAD_ATOMIC(size_t, atomic_size_t);
55 DEFINE_PTHREAD_ATOMIC(ptrdiff_t, atomic_ptrdiff_t);
56
57 DEFINE_PTHREAD_ATOMIC(intmax_t, atomic_intmax_t);
58 DEFINE_PTHREAD_ATOMIC(uintmax_t, atomic_uintmax_t);
59
60 #define ATOMIC_POINTER_LOCK_FREE 0
61 DEFINE_PTHREAD_ATOMIC(intptr_t, atomic_intptr_t);
62 DEFINE_PTHREAD_ATOMIC(uintptr_t, atomic_uintptr_t);
63
64 /* Nonstandard atomic types. */
65 DEFINE_PTHREAD_ATOMIC(uint8_t,  atomic_uint8_t);
66 DEFINE_PTHREAD_ATOMIC(uint16_t, atomic_uint16_t);
67 DEFINE_PTHREAD_ATOMIC(uint32_t, atomic_uint32_t);
68 DEFINE_PTHREAD_ATOMIC(int8_t,   atomic_int8_t);
69 DEFINE_PTHREAD_ATOMIC(int16_t,  atomic_int16_t);
70 DEFINE_PTHREAD_ATOMIC(int32_t,  atomic_int32_t);
71 DEFINE_PTHREAD_ATOMIC(uint64_t, atomic_uint64_t);
72 DEFINE_PTHREAD_ATOMIC(int64_t,  atomic_int64_t);
73
74 typedef enum {
75     memory_order_relaxed,
76     memory_order_consume,
77     memory_order_acquire,
78     memory_order_release,
79     memory_order_acq_rel,
80     memory_order_seq_cst
81 } memory_order;
82
83 #define ATOMIC_VAR_INIT(VALUE) { VALUE, PTHREAD_MUTEX_INITIALIZER }
84 #define atomic_init(OBJECT, VALUE)                      \
85     ((OBJECT)->value = (VALUE),                         \
86      pthread_mutex_init(&(OBJECT)->mutex, NULL),        \
87      (void) 0)
88 #define atomic_destroy(OBJECT)                  \
89     (pthread_mutex_destroy(&(OBJECT)->mutex),   \
90      (void) 0)
91
92 static inline void
93 atomic_thread_fence(memory_order order OVS_UNUSED)
94 {
95     /* Nothing to do. */
96 }
97
98 static inline void
99 atomic_signal_fence(memory_order order OVS_UNUSED)
100 {
101     /* Nothing to do. */
102 }
103
104 #define atomic_is_lock_free(OBJ) false
105
106 #define atomic_store(DST, SRC)                  \
107     (pthread_mutex_lock(&(DST)->mutex),         \
108      (DST)->value = (SRC),                      \
109      pthread_mutex_unlock(&(DST)->mutex),       \
110      (void) 0)
111 #define atomic_store_explicit(DST, SRC, ORDER) \
112     ((void) (ORDER), atomic_store(DST, SRC))
113
114 #define atomic_read(SRC, DST)                                           \
115     (pthread_mutex_lock(CONST_CAST(pthread_mutex_t *, &(SRC)->mutex)),  \
116      *(DST) = (SRC)->value,                                             \
117      pthread_mutex_unlock(CONST_CAST(pthread_mutex_t *, &(SRC)->mutex)), \
118      (void) 0)
119 #define atomic_read_explicit(SRC, DST, ORDER)   \
120     ((void) (ORDER), atomic_read(SRC, DST))
121
122 #define atomic_op__(RMW, OPERATOR, OPERAND, ORIG)       \
123     (pthread_mutex_lock(&(RMW)->mutex),                 \
124      *(ORIG) = (RMW)->value,                            \
125      (RMW)->value OPERATOR (OPERAND),                   \
126      pthread_mutex_unlock(&(RMW)->mutex),               \
127      (void) 0)
128
129 #define atomic_add(RMW, OPERAND, ORIG) atomic_op__(RMW, +=, OPERAND, ORIG)
130 #define atomic_sub(RMW, OPERAND, ORIG) atomic_op__(RMW, -=, OPERAND, ORIG)
131 #define atomic_or( RMW, OPERAND, ORIG) atomic_op__(RMW, |=, OPERAND, ORIG)
132 #define atomic_xor(RMW, OPERAND, ORIG) atomic_op__(RMW, ^=, OPERAND, ORIG)
133 #define atomic_and(RMW, OPERAND, ORIG) atomic_op__(RMW, &=, OPERAND, ORIG)
134
135 #define atomic_add_explicit(RMW, OPERAND, ORIG, ORDER)  \
136     ((void) (ORDER), atomic_add(RMW, OPERAND, ORIG))
137 #define atomic_sub_explicit(RMW, OPERAND, ORIG, ORDER)  \
138     ((void) (ORDER), atomic_sub(RMW, OPERAND, ORIG))
139 #define atomic_or_explicit(RMW, OPERAND, ORIG, ORDER)   \
140     ((void) (ORDER), atomic_or(RMW, OPERAND, ORIG))
141 #define atomic_xor_explicit(RMW, OPERAND, ORIG, ORDER)  \
142     ((void) (ORDER), atomic_xor(RMW, OPERAND, ORIG))
143 #define atomic_and_explicit(RMW, OPERAND, ORIG, ORDER)  \
144     ((void) (ORDER), atomic_and(RMW, OPERAND, ORIG))
145 \f
146 /* atomic_flag */
147
148 typedef struct {
149     bool b;
150     pthread_mutex_t mutex;
151 } atomic_flag;
152 #define ATOMIC_FLAG_INIT { false, PTHREAD_MUTEX_INITIALIZER }
153
154 void atomic_flag_init(volatile atomic_flag *);
155 void atomic_flag_destroy(volatile atomic_flag *);
156
157 bool atomic_flag_test_and_set(volatile atomic_flag *);
158 bool atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order);
159
160 void atomic_flag_clear(volatile atomic_flag *);
161 void atomic_flag_clear_explicit(volatile atomic_flag *, memory_order);