]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ovs-atomic-pthreads.h
netdev-offload-tc: Use single 'once' variable for probing tc features
[mirror_ovs.git] / lib / ovs-atomic-pthreads.h
CommitLineData
31a3fc6e 1/*
7d53f6b0 2 * Copyright (c) 2013, 2014 Nicira, Inc.
31a3fc6e
BP
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
1bd2c9ed
BP
22#include "ovs-atomic-locked.h"
23
31a3fc6e
BP
24#define OVS_ATOMIC_PTHREADS_IMPL 1
25
1bd2c9ed 26#define ATOMIC(TYPE) TYPE
31a3fc6e
BP
27
28#define ATOMIC_BOOL_LOCK_FREE 0
31a3fc6e 29#define ATOMIC_CHAR_LOCK_FREE 0
31a3fc6e 30#define ATOMIC_SHORT_LOCK_FREE 0
31a3fc6e 31#define ATOMIC_INT_LOCK_FREE 0
31a3fc6e 32#define ATOMIC_LONG_LOCK_FREE 0
31a3fc6e 33#define ATOMIC_LLONG_LOCK_FREE 0
31a3fc6e 34#define ATOMIC_POINTER_LOCK_FREE 0
31a3fc6e
BP
35
36typedef enum {
37 memory_order_relaxed,
38 memory_order_consume,
39 memory_order_acquire,
40 memory_order_release,
41 memory_order_acq_rel,
42 memory_order_seq_cst
43} memory_order;
44
1bd2c9ed
BP
45#define ATOMIC_VAR_INIT(VALUE) (VALUE)
46#define atomic_init(OBJECT, VALUE) (*(OBJECT) = (VALUE), (void) 0)
31a3fc6e
BP
47
48static inline void
49atomic_thread_fence(memory_order order OVS_UNUSED)
50{
51 /* Nothing to do. */
52}
53
54static inline void
55atomic_signal_fence(memory_order order OVS_UNUSED)
56{
57 /* Nothing to do. */
58}
59
60#define atomic_is_lock_free(OBJ) false
61
1bd2c9ed 62#define atomic_store(DST, SRC) atomic_store_locked(DST, SRC)
31a3fc6e
BP
63#define atomic_store_explicit(DST, SRC, ORDER) \
64 ((void) (ORDER), atomic_store(DST, SRC))
65
1bd2c9ed 66#define atomic_read(SRC, DST) atomic_read_locked(SRC, DST)
31a3fc6e
BP
67#define atomic_read_explicit(SRC, DST, ORDER) \
68 ((void) (ORDER), atomic_read(SRC, DST))
69
25045d75
JR
70#define atomic_compare_exchange_strong(DST, EXP, SRC) \
71 atomic_compare_exchange_locked(DST, EXP, SRC)
72#define atomic_compare_exchange_strong_explicit(DST, EXP, SRC, ORD1, ORD2) \
73 ((void) (ORD1), (void) (ORD2), \
74 atomic_compare_exchange_strong(DST, EXP, SRC))
75#define atomic_compare_exchange_weak \
76 atomic_compare_exchange_strong
77#define atomic_compare_exchange_weak_explicit \
78 atomic_compare_exchange_strong_explicit
79
1bd2c9ed
BP
80#define atomic_add(RMW, ARG, ORIG) atomic_op_locked(RMW, add, ARG, ORIG)
81#define atomic_sub(RMW, ARG, ORIG) atomic_op_locked(RMW, sub, ARG, ORIG)
82#define atomic_or( RMW, ARG, ORIG) atomic_op_locked(RMW, or, ARG, ORIG)
83#define atomic_xor(RMW, ARG, ORIG) atomic_op_locked(RMW, xor, ARG, ORIG)
84#define atomic_and(RMW, ARG, ORIG) atomic_op_locked(RMW, and, ARG, ORIG)
85
86#define atomic_add_explicit(RMW, ARG, ORIG, ORDER) \
87 ((void) (ORDER), atomic_add(RMW, ARG, ORIG))
88#define atomic_sub_explicit(RMW, ARG, ORIG, ORDER) \
89 ((void) (ORDER), atomic_sub(RMW, ARG, ORIG))
90#define atomic_or_explicit(RMW, ARG, ORIG, ORDER) \
91 ((void) (ORDER), atomic_or(RMW, ARG, ORIG))
92#define atomic_xor_explicit(RMW, ARG, ORIG, ORDER) \
93 ((void) (ORDER), atomic_xor(RMW, ARG, ORIG))
94#define atomic_and_explicit(RMW, ARG, ORIG, ORDER) \
95 ((void) (ORDER), atomic_and(RMW, ARG, ORIG))
31a3fc6e
BP
96\f
97/* atomic_flag */
98
99typedef struct {
100 bool b;
31a3fc6e 101} atomic_flag;
4dff0893 102#define ATOMIC_FLAG_INIT { false }
31a3fc6e 103
4dff0893
BP
104static inline bool
105atomic_flag_test_and_set(volatile atomic_flag *flag_)
106{
107 atomic_flag *flag = CONST_CAST(atomic_flag *, flag_);
108 bool old_value;
109
110 atomic_lock__(flag);
111 old_value = flag->b;
112 flag->b = true;
113 atomic_unlock__(flag);
4597d2a5 114
4dff0893
BP
115 return old_value;
116}
117
118static inline bool
119atomic_flag_test_and_set_explicit(volatile atomic_flag *flag,
120 memory_order order OVS_UNUSED)
121{
122 return atomic_flag_test_and_set(flag);
123}
124
125static inline void
126atomic_flag_clear(volatile atomic_flag *flag_)
127{
128 atomic_flag *flag = CONST_CAST(atomic_flag *, flag_);
129
130 atomic_lock__(flag);
131 flag->b = false;
132 atomic_unlock__(flag);
133}
31a3fc6e 134
4dff0893
BP
135static inline void
136atomic_flag_clear_explicit(volatile atomic_flag *flag,
137 memory_order order OVS_UNUSED)
138{
139 atomic_flag_clear(flag);
140}