Debian Codegraph @

orc/orconce.h

raw
1
2 #ifndef _ORC_ONCE_H_
3 #define _ORC_ONCE_H_
4
5 #include <orc/orcutils.h>
6 #include <orc/orcdebug.h>
7
8 #if (!defined(_MSC_VER) || defined(__clang__)) && !defined(__STDC_NO_ATOMICS__)
9 // For MSVC, we need the Win32-based version, as C11 atomics
10 // end up preferring the actually more unlikely jump -- therefore
11 // triplicating the number of memory accesses.
12 // C11 atomics are also a MSVC 17.5 preview 2 feature (_MSC_VER >= 1935)
13 # define ORC_ATOMICS_ALLOWED 1
14 #else
15 # define ORC_ATOMICS_ALLOWED 0
16 #endif
17
18 #if defined(_WIN32) && !ORC_ATOMICS_ALLOWED
19 #include <windows.h>
20 typedef INIT_ONCE orc_once_atomic_int;
21 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && ORC_ATOMICS_ALLOWED
22 #include <stdatomic.h>
23 typedef atomic_int orc_once_atomic_int;
24 #else
25 typedef int orc_once_atomic_int;
26 #endif
27
28 ORC_BEGIN_DECLS
29
30 typedef struct _OrcMutex OrcMutex;
31
32 typedef struct _OrcOnce OrcOnce;
33
34 struct _OrcOnce {
35 orc_once_atomic_int inited;
36 void *value;
37 };
38
39 # if defined(_WIN32) && !ORC_ATOMICS_ALLOWED
40 #define ORC_ONCE_INIT { INIT_ONCE_STATIC_INIT, NULL }
41 #else
42 #define ORC_ONCE_INIT { 0, NULL }
43 #endif
44
45 ORC_API void orc_once_mutex_lock (void);
46 ORC_API void orc_once_mutex_unlock (void);
47
48 #if defined(_WIN32) && !ORC_ATOMICS_ALLOWED
49 static inline orc_bool orc_once_enter(OrcOnce *once, void **value) {
50 BOOL pending = FALSE;
51
52 if (!InitOnceBeginInitialize (&once->inited, 0, &pending, NULL)) {
53 DWORD err = GetLastError ();
54 /* should not fail though, there's nothing we can do if it happens */
55 ORC_ERROR ("InitOnceBeginInitialize failed with 0x%x", (int) err);
56 return FALSE;
57 }
58
59 if (!pending) {
60 *value = once->value;
61 return TRUE;
62 }
63
64 return FALSE;
65 }
66
67 static inline void orc_once_leave(OrcOnce *once, void *value) {
68 once->value = value;
69 InitOnceComplete (&once->inited, 0, NULL);
70 }
71
72 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && ORC_ATOMICS_ALLOWED
73
74 static inline orc_bool orc_once_enter(OrcOnce *once, void **value) {
75 int inited;
76
77 inited = atomic_load_explicit (&once->inited, memory_order_acquire);
78 if (ORC_LIKELY(inited)) {
79 *value = once->value;
80 return TRUE;
81 }
82
83 orc_once_mutex_lock ();
84
85 inited = atomic_load_explicit (&once->inited, memory_order_acquire);
86 if (ORC_UNLIKELY(inited)) {
87 *value = once->value;
88 orc_once_mutex_unlock ();
89 return TRUE;
90 }
91
92 return FALSE;
93 }
94
95 static inline void orc_once_leave(OrcOnce *once, void *value) {
96 int inited = TRUE;
97 once->value = value;
98 atomic_store_explicit (&once->inited, inited, memory_order_release);
99 orc_once_mutex_unlock ();
100 }
101
102 #elif ORC_GNUC_PREREQ(4, 1)
103
104 static inline orc_bool orc_once_enter(OrcOnce *once, void **value) {
105 int inited;
106
107 /* we use 0 for not initialized, 1 for initialized and 3 for currently
108 * being initialized */
109 inited = __sync_val_compare_and_swap(&once->inited, 0, 3);
110 /* if the value was previously initialized then just return here */
111 if (inited == 1) {
112 *value = once->value;
113 return TRUE;
114 }
115
116 orc_once_mutex_lock ();
117 /* if the value was currently being initialized then check if we're the
118 * thread that is doing the initialization or not */
119 if (inited == 3) {
120 inited = __sync_val_compare_and_swap(&once->inited, 3, 3);
121
122 /* the other thread initialized the value in the meantime so
123 * we can just return here */
124 if (inited == 1) {
125 *value = once->value;
126 orc_once_mutex_unlock ();
127 return TRUE;
128 }
129 }
130
131 return FALSE;
132 }
133
134 static inline void orc_once_leave(OrcOnce *once, void *value) {
135 once->value = value;
136 /* this effectively sets the previous value of 3 to 1 */
137 __sync_and_and_fetch (&once->inited, 1);
138 orc_once_mutex_unlock ();
139 }
140
141 #else
142 #warning No atomic operations available
143
144 static inline orc_bool orc_once_enter(OrcOnce *once, void **value) {
145 orc_once_mutex_lock ();
146 if (once->inited) {
147 *value = once->value;
148 orc_once_mutex_unlock ();
149 return TRUE;
150 }
151
152 return FALSE;
153 }
154
155 static inline void orc_once_leave(OrcOnce *once, void *value) {
156 once->value = value;
157 once->inited = TRUE;
158 orc_once_mutex_unlock ();
159 }
160
161 #endif
162
163 ORC_END_DECLS
164
165 #endif
166