tests: use SPDX identifiers
[urcu.git] / tests / common / api.h
1 // SPDX-FileCopyrightText: 2006 Paul E. McKenney, IBM.
2 //
3 // SPDX-License-Identifier: GPL-2.0-only
4
5 #ifndef _INCLUDE_API_H
6 #define _INCLUDE_API_H
7
8 /*
9 * common.h: Common Linux kernel-isms.
10 *
11 * Much code taken from the Linux kernel. For such code, the option
12 * to redistribute under later versions of GPL might not be available.
13 */
14
15 #include <urcu/compiler.h>
16 #include <urcu/arch.h>
17
18 /*
19 * Machine parameters.
20 */
21
22 #define ____cacheline_internodealigned_in_smp \
23 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
24
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <sys/types.h>
31 #include <pthread.h>
32 #include <sys/param.h>
33 /* #include "atomic.h" */
34
35 /*
36 * Exclusive locking primitives.
37 */
38
39 typedef pthread_mutex_t spinlock_t;
40
41 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
42 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
43
44 static void spin_lock_init(spinlock_t *sp)
45 {
46 if (pthread_mutex_init(sp, NULL) != 0) {
47 perror("spin_lock_init:pthread_mutex_init");
48 exit(-1);
49 }
50 }
51
52 static void spin_lock(spinlock_t *sp)
53 {
54 if (pthread_mutex_lock(sp) != 0) {
55 perror("spin_lock:pthread_mutex_lock");
56 exit(-1);
57 }
58 }
59
60 static void spin_unlock(spinlock_t *sp)
61 {
62 if (pthread_mutex_unlock(sp) != 0) {
63 perror("spin_unlock:pthread_mutex_unlock");
64 exit(-1);
65 }
66 }
67
68 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
69 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
70
71 /*
72 * Thread creation/destruction primitives.
73 */
74
75 typedef pthread_t thread_id_t;
76
77 #define NR_THREADS 4096
78
79 #define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0)
80 #define __THREAD_ID_MAP_WAITING ((thread_id_t) 1)
81 thread_id_t __thread_id_map[NR_THREADS];
82 spinlock_t __thread_id_map_mutex;
83
84 #define for_each_thread(t) \
85 for (t = 0; t < NR_THREADS; t++)
86
87 #define for_each_running_thread(t) \
88 for (t = 0; t < NR_THREADS; t++) \
89 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
90 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
91
92 #define for_each_tid(t, tid) \
93 for (t = 0; t < NR_THREADS; t++) \
94 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
95 ((tid) != __THREAD_ID_MAP_WAITING))
96
97 pthread_key_t thread_id_key;
98
99 static int __smp_thread_id(void)
100 {
101 int i;
102 thread_id_t tid = pthread_self();
103
104 for (i = 0; i < NR_THREADS; i++) {
105 if (__thread_id_map[i] == tid) {
106 long v = i + 1; /* must be non-NULL. */
107
108 if (pthread_setspecific(thread_id_key, (void *)v) != 0) {
109 perror("pthread_setspecific");
110 exit(-1);
111 }
112 return i;
113 }
114 }
115 spin_lock(&__thread_id_map_mutex);
116 for (i = 0; i < NR_THREADS; i++) {
117 if (__thread_id_map[i] == tid) {
118 spin_unlock(&__thread_id_map_mutex);
119 return i;
120 }
121 }
122 spin_unlock(&__thread_id_map_mutex);
123 fprintf(stderr, "smp_thread_id: Rogue thread, id: %lu(%#lx)\n",
124 (unsigned long) tid, (unsigned long) tid);
125 exit(-1);
126 }
127
128 static int smp_thread_id(void)
129 {
130 void *id;
131
132 id = pthread_getspecific(thread_id_key);
133 if (id == NULL)
134 return __smp_thread_id();
135 return ((long) id - 1);
136 }
137
138 static thread_id_t create_thread(void *(*func)(void *), void *arg)
139 {
140 thread_id_t tid;
141 int i;
142
143 spin_lock(&__thread_id_map_mutex);
144 for (i = 0; i < NR_THREADS; i++) {
145 if (__thread_id_map[i] == __THREAD_ID_MAP_EMPTY)
146 break;
147 }
148 if (i >= NR_THREADS) {
149 spin_unlock(&__thread_id_map_mutex);
150 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
151 exit(-1);
152 }
153 __thread_id_map[i] = __THREAD_ID_MAP_WAITING;
154 spin_unlock(&__thread_id_map_mutex);
155 if (pthread_create(&tid, NULL, func, arg) != 0) {
156 perror("create_thread:pthread_create");
157 exit(-1);
158 }
159 __thread_id_map[i] = tid;
160 return tid;
161 }
162
163 static void *wait_thread(thread_id_t tid)
164 {
165 int i;
166 void *vp;
167
168 for (i = 0; i < NR_THREADS; i++) {
169 if (__thread_id_map[i] == tid)
170 break;
171 }
172 if (i >= NR_THREADS){
173 fprintf(stderr, "wait_thread: bad tid = %lu(%#lx)\n",
174 (unsigned long)tid, (unsigned long)tid);
175 exit(-1);
176 }
177 if (pthread_join(tid, &vp) != 0) {
178 perror("wait_thread:pthread_join");
179 exit(-1);
180 }
181 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
182 return vp;
183 }
184
185 static void wait_all_threads(void)
186 {
187 int i;
188 thread_id_t tid;
189
190 for (i = 1; i < NR_THREADS; i++) {
191 tid = __thread_id_map[i];
192 if (tid != __THREAD_ID_MAP_EMPTY &&
193 tid != __THREAD_ID_MAP_WAITING)
194 (void)wait_thread(tid);
195 }
196 }
197
198 #ifdef HAVE_SCHED_SETAFFINITY
199 static void run_on(int cpu)
200 {
201 cpu_set_t mask;
202
203 CPU_ZERO(&mask);
204 CPU_SET(cpu, &mask);
205 sched_setaffinity(0, sizeof(mask), &mask);
206 }
207 #else
208
209 static void run_on(int cpu __attribute__((unused)))
210 {}
211 #endif /* HAVE_SCHED_SETAFFINITY */
212
213 /*
214 * timekeeping -- very crude -- should use MONOTONIC...
215 */
216
217 static inline
218 long long get_microseconds(void)
219 {
220 struct timeval tv;
221
222 if (gettimeofday(&tv, NULL) != 0)
223 abort();
224 return ((long long)tv.tv_sec) * 1000000LL + (long long)tv.tv_usec;
225 }
226
227 /*
228 * Per-thread variables.
229 */
230
231 #define DEFINE_PER_THREAD(type, name) \
232 struct { \
233 __typeof__(type) v \
234 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
235 } __per_thread_##name[NR_THREADS]
236 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
237
238 #define per_thread(name, thread) __per_thread_##name[thread].v
239 #define __get_thread_var(name) per_thread(name, smp_thread_id())
240
241 #define init_per_thread(name, v) \
242 do { \
243 int __i_p_t_i; \
244 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
245 per_thread(name, __i_p_t_i) = v; \
246 } while (0)
247
248 DEFINE_PER_THREAD(int, smp_processor_id);
249
250 /*
251 * Bug checks.
252 */
253
254 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
255
256 /*
257 * Initialization -- Must be called before calling any primitives.
258 */
259
260 static void smp_init(void)
261 {
262 int i;
263
264 spin_lock_init(&__thread_id_map_mutex);
265 __thread_id_map[0] = pthread_self();
266 for (i = 1; i < NR_THREADS; i++)
267 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
268 init_per_thread(smp_processor_id, 0);
269 if (pthread_key_create(&thread_id_key, NULL) != 0) {
270 perror("pthread_key_create");
271 exit(-1);
272 }
273 }
274
275 #endif
This page took 0.034921 seconds and 5 git commands to generate.