uatomic/x86: Remove redundant memory barriers
[urcu.git] / tests / common / api.h
1 #ifndef _INCLUDE_API_H
2 #define _INCLUDE_API_H
3
4 /*
5 * common.h: Common Linux kernel-isms.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; but version 2 of the License only due
10 * to code included from the Linux kernel.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Copyright (c) 2006 Paul E. McKenney, IBM.
22 *
23 * Much code taken from the Linux kernel. For such code, the option
24 * to redistribute under later versions of GPL might not be available.
25 */
26
27 #include <urcu/compiler.h>
28 #include <urcu/arch.h>
29
30 /*
31 * Machine parameters.
32 */
33
34 #define ____cacheline_internodealigned_in_smp \
35 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
36
37 /*
38 * api_pthreads.h: API mapping to pthreads environment.
39 *
40 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version. However, please note that much
44 * of the code in this file derives from the Linux kernel, and that such
45 * code may not be available except under GPLv2.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, write to the Free Software
54 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
55 *
56 * Copyright (c) 2006 Paul E. McKenney, IBM.
57 */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <errno.h>
62 #include <limits.h>
63 #include <sys/types.h>
64 #include <pthread.h>
65 #include <sys/param.h>
66 /* #include "atomic.h" */
67
68 /*
69 * Exclusive locking primitives.
70 */
71
72 typedef pthread_mutex_t spinlock_t;
73
74 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
75 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
76
77 static void spin_lock_init(spinlock_t *sp)
78 {
79 if (pthread_mutex_init(sp, NULL) != 0) {
80 perror("spin_lock_init:pthread_mutex_init");
81 exit(-1);
82 }
83 }
84
85 static void spin_lock(spinlock_t *sp)
86 {
87 if (pthread_mutex_lock(sp) != 0) {
88 perror("spin_lock:pthread_mutex_lock");
89 exit(-1);
90 }
91 }
92
93 static void spin_unlock(spinlock_t *sp)
94 {
95 if (pthread_mutex_unlock(sp) != 0) {
96 perror("spin_unlock:pthread_mutex_unlock");
97 exit(-1);
98 }
99 }
100
101 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
102 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
103
104 /*
105 * Thread creation/destruction primitives.
106 */
107
108 typedef pthread_t thread_id_t;
109
110 #define NR_THREADS 4096
111
112 #define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0)
113 #define __THREAD_ID_MAP_WAITING ((thread_id_t) 1)
114 thread_id_t __thread_id_map[NR_THREADS];
115 spinlock_t __thread_id_map_mutex;
116
117 #define for_each_thread(t) \
118 for (t = 0; t < NR_THREADS; t++)
119
120 #define for_each_running_thread(t) \
121 for (t = 0; t < NR_THREADS; t++) \
122 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
123 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
124
125 #define for_each_tid(t, tid) \
126 for (t = 0; t < NR_THREADS; t++) \
127 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
128 ((tid) != __THREAD_ID_MAP_WAITING))
129
130 pthread_key_t thread_id_key;
131
132 static int __smp_thread_id(void)
133 {
134 int i;
135 thread_id_t tid = pthread_self();
136
137 for (i = 0; i < NR_THREADS; i++) {
138 if (__thread_id_map[i] == tid) {
139 long v = i + 1; /* must be non-NULL. */
140
141 if (pthread_setspecific(thread_id_key, (void *)v) != 0) {
142 perror("pthread_setspecific");
143 exit(-1);
144 }
145 return i;
146 }
147 }
148 spin_lock(&__thread_id_map_mutex);
149 for (i = 0; i < NR_THREADS; i++) {
150 if (__thread_id_map[i] == tid) {
151 spin_unlock(&__thread_id_map_mutex);
152 return i;
153 }
154 }
155 spin_unlock(&__thread_id_map_mutex);
156 fprintf(stderr, "smp_thread_id: Rogue thread, id: %lu(%#lx)\n",
157 (unsigned long) tid, (unsigned long) tid);
158 exit(-1);
159 }
160
161 static int smp_thread_id(void)
162 {
163 void *id;
164
165 id = pthread_getspecific(thread_id_key);
166 if (id == NULL)
167 return __smp_thread_id();
168 return ((long) id - 1);
169 }
170
171 static thread_id_t create_thread(void *(*func)(void *), void *arg)
172 {
173 thread_id_t tid;
174 int i;
175
176 spin_lock(&__thread_id_map_mutex);
177 for (i = 0; i < NR_THREADS; i++) {
178 if (__thread_id_map[i] == __THREAD_ID_MAP_EMPTY)
179 break;
180 }
181 if (i >= NR_THREADS) {
182 spin_unlock(&__thread_id_map_mutex);
183 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
184 exit(-1);
185 }
186 __thread_id_map[i] = __THREAD_ID_MAP_WAITING;
187 spin_unlock(&__thread_id_map_mutex);
188 if (pthread_create(&tid, NULL, func, arg) != 0) {
189 perror("create_thread:pthread_create");
190 exit(-1);
191 }
192 __thread_id_map[i] = tid;
193 return tid;
194 }
195
196 static void *wait_thread(thread_id_t tid)
197 {
198 int i;
199 void *vp;
200
201 for (i = 0; i < NR_THREADS; i++) {
202 if (__thread_id_map[i] == tid)
203 break;
204 }
205 if (i >= NR_THREADS){
206 fprintf(stderr, "wait_thread: bad tid = %lu(%#lx)\n",
207 (unsigned long)tid, (unsigned long)tid);
208 exit(-1);
209 }
210 if (pthread_join(tid, &vp) != 0) {
211 perror("wait_thread:pthread_join");
212 exit(-1);
213 }
214 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
215 return vp;
216 }
217
218 static void wait_all_threads(void)
219 {
220 int i;
221 thread_id_t tid;
222
223 for (i = 1; i < NR_THREADS; i++) {
224 tid = __thread_id_map[i];
225 if (tid != __THREAD_ID_MAP_EMPTY &&
226 tid != __THREAD_ID_MAP_WAITING)
227 (void)wait_thread(tid);
228 }
229 }
230
231 #ifdef HAVE_SCHED_SETAFFINITY
232 static void run_on(int cpu)
233 {
234 cpu_set_t mask;
235
236 CPU_ZERO(&mask);
237 CPU_SET(cpu, &mask);
238 sched_setaffinity(0, sizeof(mask), &mask);
239 }
240 #else
241
242 static void run_on(int cpu __attribute__((unused)))
243 {}
244 #endif /* HAVE_SCHED_SETAFFINITY */
245
246 /*
247 * timekeeping -- very crude -- should use MONOTONIC...
248 */
249
250 static inline
251 long long get_microseconds(void)
252 {
253 struct timeval tv;
254
255 if (gettimeofday(&tv, NULL) != 0)
256 abort();
257 return ((long long)tv.tv_sec) * 1000000LL + (long long)tv.tv_usec;
258 }
259
260 /*
261 * Per-thread variables.
262 */
263
264 #define DEFINE_PER_THREAD(type, name) \
265 struct { \
266 __typeof__(type) v \
267 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
268 } __per_thread_##name[NR_THREADS]
269 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
270
271 #define per_thread(name, thread) __per_thread_##name[thread].v
272 #define __get_thread_var(name) per_thread(name, smp_thread_id())
273
274 #define init_per_thread(name, v) \
275 do { \
276 int __i_p_t_i; \
277 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
278 per_thread(name, __i_p_t_i) = v; \
279 } while (0)
280
281 DEFINE_PER_THREAD(int, smp_processor_id);
282
283 /*
284 * Bug checks.
285 */
286
287 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
288
289 /*
290 * Initialization -- Must be called before calling any primitives.
291 */
292
293 static void smp_init(void)
294 {
295 int i;
296
297 spin_lock_init(&__thread_id_map_mutex);
298 __thread_id_map[0] = pthread_self();
299 for (i = 1; i < NR_THREADS; i++)
300 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
301 init_per_thread(smp_processor_id, 0);
302 if (pthread_key_create(&thread_id_key, NULL) != 0) {
303 perror("pthread_key_create");
304 exit(-1);
305 }
306 }
307
308 #endif
This page took 0.037015 seconds and 4 git commands to generate.