2 * Copyright (c) 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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."
22 #define OVS_ATOMIC_PTHREADS_IMPL 1
24 #define ATOMIC(TYPE) struct { TYPE value; pthread_mutex_t mutex; }
25 #include "ovs-atomic-types.h"
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
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), \
49 #define atomic_destroy(OBJECT) \
50 (pthread_mutex_destroy(&(OBJECT)->mutex), \
54 atomic_thread_fence(memory_order order OVS_UNUSED)
60 atomic_signal_fence(memory_order order OVS_UNUSED)
65 #define atomic_is_lock_free(OBJ) false
67 #define atomic_store(DST, SRC) \
68 (pthread_mutex_lock(&(DST)->mutex), \
69 (DST)->value = (SRC), \
70 pthread_mutex_unlock(&(DST)->mutex), \
72 #define atomic_store_explicit(DST, SRC, ORDER) \
73 ((void) (ORDER), atomic_store(DST, SRC))
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)), \
80 #define atomic_read_explicit(SRC, DST, ORDER) \
81 ((void) (ORDER), atomic_read(SRC, DST))
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), \
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)
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))
111 pthread_mutex_t mutex;
113 #define ATOMIC_FLAG_INIT { false, PTHREAD_MUTEX_INITIALIZER }
115 void atomic_flag_init(volatile atomic_flag *);
116 void atomic_flag_destroy(volatile atomic_flag *);
118 bool atomic_flag_test_and_set(volatile atomic_flag *);
119 bool atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order);
121 void atomic_flag_clear(volatile atomic_flag *);
122 void atomic_flag_clear_explicit(volatile atomic_flag *, memory_order);