ff399250971217a84d004998717f485dc0b56afb
[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 #define OVS_ATOMIC_PTHREADS_IMPL 1
23
24 #define ATOMIC(TYPE) struct { TYPE value; pthread_mutex_t mutex; }
25 #include "ovs-atomic-types.h"
26
27 #define ATOMIC_BOOL_LOCK_FREE 0
28 #define ATOMIC_CHAR_LOCK_FREE 0
29 #define ATOMIC_SHORT_LOCK_FREE 0
30 #define ATOMIC_INT_LOCK_FREE 0
31 #define ATOMIC_LONG_LOCK_FREE 0
32 #define ATOMIC_LLONG_LOCK_FREE 0
33 #define ATOMIC_POINTER_LOCK_FREE 0
34
35 typedef enum {
36     memory_order_relaxed,
37     memory_order_consume,
38     memory_order_acquire,
39     memory_order_release,
40     memory_order_acq_rel,
41     memory_order_seq_cst
42 } memory_order;
43
44 #define ATOMIC_VAR_INIT(VALUE) { VALUE, PTHREAD_MUTEX_INITIALIZER }
45 #define atomic_init(OBJECT, VALUE)                      \
46     ((OBJECT)->value = (VALUE),                         \
47      pthread_mutex_init(&(OBJECT)->mutex, NULL),        \
48      (void) 0)
49 #define atomic_destroy(OBJECT)                  \
50     (pthread_mutex_destroy(&(OBJECT)->mutex),   \
51      (void) 0)
52
53 static inline void
54 atomic_thread_fence(memory_order order OVS_UNUSED)
55 {
56     /* Nothing to do. */
57 }
58
59 static inline void
60 atomic_signal_fence(memory_order order OVS_UNUSED)
61 {
62     /* Nothing to do. */
63 }
64
65 #define atomic_is_lock_free(OBJ) false
66
67 #define atomic_store(DST, SRC)                  \
68     (pthread_mutex_lock(&(DST)->mutex),         \
69      (DST)->value = (SRC),                      \
70      pthread_mutex_unlock(&(DST)->mutex),       \
71      (void) 0)
72 #define atomic_store_explicit(DST, SRC, ORDER) \
73     ((void) (ORDER), atomic_store(DST, SRC))
74
75 #define atomic_read(SRC, DST)                                           \
76     (pthread_mutex_lock(CONST_CAST(pthread_mutex_t *, &(SRC)->mutex)),  \
77      *(DST) = (SRC)->value,                                             \
78      pthread_mutex_unlock(CONST_CAST(pthread_mutex_t *, &(SRC)->mutex)), \
79      (void) 0)
80 #define atomic_read_explicit(SRC, DST, ORDER)   \
81     ((void) (ORDER), atomic_read(SRC, DST))
82
83 #define atomic_op__(RMW, OPERATOR, OPERAND, ORIG)       \
84     (pthread_mutex_lock(&(RMW)->mutex),                 \
85      *(ORIG) = (RMW)->value,                            \
86      (RMW)->value OPERATOR (OPERAND),                   \
87      pthread_mutex_unlock(&(RMW)->mutex),               \
88      (void) 0)
89
90 #define atomic_add(RMW, OPERAND, ORIG) atomic_op__(RMW, +=, OPERAND, ORIG)
91 #define atomic_sub(RMW, OPERAND, ORIG) atomic_op__(RMW, -=, OPERAND, ORIG)
92 #define atomic_or( RMW, OPERAND, ORIG) atomic_op__(RMW, |=, OPERAND, ORIG)
93 #define atomic_xor(RMW, OPERAND, ORIG) atomic_op__(RMW, ^=, OPERAND, ORIG)
94 #define atomic_and(RMW, OPERAND, ORIG) atomic_op__(RMW, &=, OPERAND, ORIG)
95
96 #define atomic_add_explicit(RMW, OPERAND, ORIG, ORDER)  \
97     ((void) (ORDER), atomic_add(RMW, OPERAND, ORIG))
98 #define atomic_sub_explicit(RMW, OPERAND, ORIG, ORDER)  \
99     ((void) (ORDER), atomic_sub(RMW, OPERAND, ORIG))
100 #define atomic_or_explicit(RMW, OPERAND, ORIG, ORDER)   \
101     ((void) (ORDER), atomic_or(RMW, OPERAND, ORIG))
102 #define atomic_xor_explicit(RMW, OPERAND, ORIG, ORDER)  \
103     ((void) (ORDER), atomic_xor(RMW, OPERAND, ORIG))
104 #define atomic_and_explicit(RMW, OPERAND, ORIG, ORDER)  \
105     ((void) (ORDER), atomic_and(RMW, OPERAND, ORIG))
106 \f
107 /* atomic_flag */
108
109 typedef struct {
110     bool b;
111     pthread_mutex_t mutex;
112 } atomic_flag;
113 #define ATOMIC_FLAG_INIT { false, PTHREAD_MUTEX_INITIALIZER }
114
115 void atomic_flag_init(volatile atomic_flag *);
116 void atomic_flag_destroy(volatile atomic_flag *);
117
118 bool atomic_flag_test_and_set(volatile atomic_flag *);
119 bool atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order);
120
121 void atomic_flag_clear(volatile atomic_flag *);
122 void atomic_flag_clear_explicit(volatile atomic_flag *, memory_order);