Remove glibc < 2.4 compat code for sched_setaffinity
[urcu.git] / tests / common / api.h
CommitLineData
1a43bbd8 1#ifndef _INCLUDE_API_H
0578089f
PM
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
3282a76b 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0578089f
PM
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
78bec10c 27#include <urcu/compiler.h>
3c570c9c
PB
28#include <urcu/arch.h>
29
0578089f
PM
30/*
31 * Machine parameters.
32 */
33
0578089f 34#define ____cacheline_internodealigned_in_smp \
901bbb44 35 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
6ee91d83 36
0578089f
PM
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
3282a76b 54 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0578089f
PM
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>
0578089f 64#include <pthread.h>
0578089f
PM
65#include <sys/param.h>
66/* #include "atomic.h" */
67
0578089f
PM
68/*
69 * Exclusive locking primitives.
70 */
71
72typedef 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
77static 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
85static 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
93static 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
108typedef pthread_t thread_id_t;
109
af9987a9 110#define NR_THREADS 4096
0578089f 111
3c570c9c
PB
112#define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0)
113#define __THREAD_ID_MAP_WAITING ((thread_id_t) 1)
0578089f
PM
114thread_id_t __thread_id_map[NR_THREADS];
115spinlock_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
3c570c9c
PB
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
0578089f
PM
130pthread_key_t thread_id_key;
131
132static 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++) {
8ff97cc3 150 if (__thread_id_map[i] == tid) {
0578089f
PM
151 spin_unlock(&__thread_id_map_mutex);
152 return i;
8ff97cc3 153 }
0578089f
PM
154 }
155 spin_unlock(&__thread_id_map_mutex);
665eb3ef
MD
156 fprintf(stderr, "smp_thread_id: Rogue thread, id: %lu(%#lx)\n",
157 (unsigned long) tid, (unsigned long) tid);
0578089f
PM
158 exit(-1);
159}
160
161static 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
171static 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
196static 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){
665eb3ef
MD
206 fprintf(stderr, "wait_thread: bad tid = %lu(%#lx)\n",
207 (unsigned long)tid, (unsigned long)tid);
0578089f
PM
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
218static 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
231static void run_on(int cpu)
232{
d8540fc5 233#if HAVE_SCHED_SETAFFINITY
0578089f
PM
234 cpu_set_t mask;
235
236 CPU_ZERO(&mask);
237 CPU_SET(cpu, &mask);
238 sched_setaffinity(0, sizeof(mask), &mask);
d8540fc5 239#endif /* HAVE_SCHED_SETAFFINITY */
0578089f
PM
240}
241
242/*
243 * timekeeping -- very crude -- should use MONOTONIC...
244 */
245
246long long get_microseconds(void)
247{
248 struct timeval tv;
249
250 if (gettimeofday(&tv, NULL) != 0)
251 abort();
252 return ((long long)tv.tv_sec) * 1000000LL + (long long)tv.tv_usec;
253}
254
255/*
256 * Per-thread variables.
257 */
258
259#define DEFINE_PER_THREAD(type, name) \
260 struct { \
261 __typeof__(type) v \
06f22bdb 262 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
e1789ce2 263 } __per_thread_##name[NR_THREADS]
0578089f
PM
264#define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
265
266#define per_thread(name, thread) __per_thread_##name[thread].v
267#define __get_thread_var(name) per_thread(name, smp_thread_id())
268
269#define init_per_thread(name, v) \
270 do { \
271 int __i_p_t_i; \
272 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
273 per_thread(name, __i_p_t_i) = v; \
274 } while (0)
275
0578089f
PM
276DEFINE_PER_THREAD(int, smp_processor_id);
277
0578089f
PM
278/*
279 * Bug checks.
280 */
281
282#define BUG_ON(c) do { if (!(c)) abort(); } while (0)
283
284/*
285 * Initialization -- Must be called before calling any primitives.
286 */
287
288static void smp_init(void)
289{
290 int i;
291
292 spin_lock_init(&__thread_id_map_mutex);
293 __thread_id_map[0] = pthread_self();
294 for (i = 1; i < NR_THREADS; i++)
295 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
296 init_per_thread(smp_processor_id, 0);
297 if (pthread_key_create(&thread_id_key, NULL) != 0) {
298 perror("pthread_key_create");
299 exit(-1);
300 }
301}
302
1a43bbd8 303#endif
This page took 0.052409 seconds and 4 git commands to generate.