Commit | Line | Data |
---|---|---|
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 | 28 | #include <urcu/arch.h> |
108a92e5 | 29 | #include "cpuset.h" |
3c570c9c | 30 | |
0578089f PM |
31 | /* |
32 | * Machine parameters. | |
33 | */ | |
34 | ||
0578089f | 35 | #define ____cacheline_internodealigned_in_smp \ |
901bbb44 | 36 | __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))) |
6ee91d83 | 37 | |
0578089f PM |
38 | /* |
39 | * api_pthreads.h: API mapping to pthreads environment. | |
40 | * | |
41 | * This program is free software; you can redistribute it and/or modify | |
42 | * it under the terms of the GNU General Public License as published by | |
43 | * the Free Software Foundation; either version 2 of the License, or | |
44 | * (at your option) any later version. However, please note that much | |
45 | * of the code in this file derives from the Linux kernel, and that such | |
46 | * code may not be available except under GPLv2. | |
47 | * | |
48 | * This program is distributed in the hope that it will be useful, | |
49 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
50 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
51 | * GNU General Public License for more details. | |
52 | * | |
53 | * You should have received a copy of the GNU General Public License | |
54 | * along with this program; if not, write to the Free Software | |
3282a76b | 55 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
0578089f PM |
56 | * |
57 | * Copyright (c) 2006 Paul E. McKenney, IBM. | |
58 | */ | |
59 | ||
60 | #include <stdio.h> | |
61 | #include <stdlib.h> | |
62 | #include <errno.h> | |
63 | #include <limits.h> | |
64 | #include <sys/types.h> | |
0578089f | 65 | #include <pthread.h> |
0578089f PM |
66 | #include <sys/param.h> |
67 | /* #include "atomic.h" */ | |
68 | ||
0578089f PM |
69 | /* |
70 | * Exclusive locking primitives. | |
71 | */ | |
72 | ||
73 | typedef pthread_mutex_t spinlock_t; | |
74 | ||
75 | #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER; | |
76 | #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER | |
77 | ||
78 | static void spin_lock_init(spinlock_t *sp) | |
79 | { | |
80 | if (pthread_mutex_init(sp, NULL) != 0) { | |
81 | perror("spin_lock_init:pthread_mutex_init"); | |
82 | exit(-1); | |
83 | } | |
84 | } | |
85 | ||
86 | static void spin_lock(spinlock_t *sp) | |
87 | { | |
88 | if (pthread_mutex_lock(sp) != 0) { | |
89 | perror("spin_lock:pthread_mutex_lock"); | |
90 | exit(-1); | |
91 | } | |
92 | } | |
93 | ||
94 | static void spin_unlock(spinlock_t *sp) | |
95 | { | |
96 | if (pthread_mutex_unlock(sp) != 0) { | |
97 | perror("spin_unlock:pthread_mutex_unlock"); | |
98 | exit(-1); | |
99 | } | |
100 | } | |
101 | ||
102 | #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0) | |
103 | #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0) | |
104 | ||
105 | /* | |
106 | * Thread creation/destruction primitives. | |
107 | */ | |
108 | ||
109 | typedef pthread_t thread_id_t; | |
110 | ||
111 | #define NR_THREADS 128 | |
112 | ||
3c570c9c PB |
113 | #define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0) |
114 | #define __THREAD_ID_MAP_WAITING ((thread_id_t) 1) | |
0578089f PM |
115 | thread_id_t __thread_id_map[NR_THREADS]; |
116 | spinlock_t __thread_id_map_mutex; | |
117 | ||
118 | #define for_each_thread(t) \ | |
119 | for (t = 0; t < NR_THREADS; t++) | |
120 | ||
121 | #define for_each_running_thread(t) \ | |
122 | for (t = 0; t < NR_THREADS; t++) \ | |
123 | if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \ | |
124 | (__thread_id_map[t] != __THREAD_ID_MAP_WAITING)) | |
125 | ||
3c570c9c PB |
126 | #define for_each_tid(t, tid) \ |
127 | for (t = 0; t < NR_THREADS; t++) \ | |
128 | if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \ | |
129 | ((tid) != __THREAD_ID_MAP_WAITING)) | |
130 | ||
0578089f PM |
131 | pthread_key_t thread_id_key; |
132 | ||
133 | static int __smp_thread_id(void) | |
134 | { | |
135 | int i; | |
136 | thread_id_t tid = pthread_self(); | |
137 | ||
138 | for (i = 0; i < NR_THREADS; i++) { | |
139 | if (__thread_id_map[i] == tid) { | |
140 | long v = i + 1; /* must be non-NULL. */ | |
141 | ||
142 | if (pthread_setspecific(thread_id_key, (void *)v) != 0) { | |
143 | perror("pthread_setspecific"); | |
144 | exit(-1); | |
145 | } | |
146 | return i; | |
147 | } | |
148 | } | |
149 | spin_lock(&__thread_id_map_mutex); | |
150 | for (i = 0; i < NR_THREADS; i++) { | |
8ff97cc3 | 151 | if (__thread_id_map[i] == tid) { |
0578089f PM |
152 | spin_unlock(&__thread_id_map_mutex); |
153 | return i; | |
8ff97cc3 | 154 | } |
0578089f PM |
155 | } |
156 | spin_unlock(&__thread_id_map_mutex); | |
665eb3ef MD |
157 | fprintf(stderr, "smp_thread_id: Rogue thread, id: %lu(%#lx)\n", |
158 | (unsigned long) tid, (unsigned long) tid); | |
0578089f PM |
159 | exit(-1); |
160 | } | |
161 | ||
162 | static int smp_thread_id(void) | |
163 | { | |
164 | void *id; | |
165 | ||
166 | id = pthread_getspecific(thread_id_key); | |
167 | if (id == NULL) | |
168 | return __smp_thread_id(); | |
169 | return (long)(id - 1); | |
170 | } | |
171 | ||
172 | static thread_id_t create_thread(void *(*func)(void *), void *arg) | |
173 | { | |
174 | thread_id_t tid; | |
175 | int i; | |
176 | ||
177 | spin_lock(&__thread_id_map_mutex); | |
178 | for (i = 0; i < NR_THREADS; i++) { | |
179 | if (__thread_id_map[i] == __THREAD_ID_MAP_EMPTY) | |
180 | break; | |
181 | } | |
182 | if (i >= NR_THREADS) { | |
183 | spin_unlock(&__thread_id_map_mutex); | |
184 | fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS); | |
185 | exit(-1); | |
186 | } | |
187 | __thread_id_map[i] = __THREAD_ID_MAP_WAITING; | |
188 | spin_unlock(&__thread_id_map_mutex); | |
189 | if (pthread_create(&tid, NULL, func, arg) != 0) { | |
190 | perror("create_thread:pthread_create"); | |
191 | exit(-1); | |
192 | } | |
193 | __thread_id_map[i] = tid; | |
194 | return tid; | |
195 | } | |
196 | ||
197 | static void *wait_thread(thread_id_t tid) | |
198 | { | |
199 | int i; | |
200 | void *vp; | |
201 | ||
202 | for (i = 0; i < NR_THREADS; i++) { | |
203 | if (__thread_id_map[i] == tid) | |
204 | break; | |
205 | } | |
206 | if (i >= NR_THREADS){ | |
665eb3ef MD |
207 | fprintf(stderr, "wait_thread: bad tid = %lu(%#lx)\n", |
208 | (unsigned long)tid, (unsigned long)tid); | |
0578089f PM |
209 | exit(-1); |
210 | } | |
211 | if (pthread_join(tid, &vp) != 0) { | |
212 | perror("wait_thread:pthread_join"); | |
213 | exit(-1); | |
214 | } | |
215 | __thread_id_map[i] = __THREAD_ID_MAP_EMPTY; | |
216 | return vp; | |
217 | } | |
218 | ||
219 | static void wait_all_threads(void) | |
220 | { | |
221 | int i; | |
222 | thread_id_t tid; | |
223 | ||
224 | for (i = 1; i < NR_THREADS; i++) { | |
225 | tid = __thread_id_map[i]; | |
226 | if (tid != __THREAD_ID_MAP_EMPTY && | |
227 | tid != __THREAD_ID_MAP_WAITING) | |
228 | (void)wait_thread(tid); | |
229 | } | |
230 | } | |
231 | ||
232 | static void run_on(int cpu) | |
233 | { | |
d8540fc5 | 234 | #if HAVE_SCHED_SETAFFINITY |
0578089f PM |
235 | cpu_set_t mask; |
236 | ||
237 | CPU_ZERO(&mask); | |
238 | CPU_SET(cpu, &mask); | |
d8540fc5 PA |
239 | #if SCHED_SETAFFINITY_ARGS == 2 |
240 | sched_setaffinity(0, &mask); | |
241 | #else | |
0578089f | 242 | sched_setaffinity(0, sizeof(mask), &mask); |
d8540fc5 PA |
243 | #endif |
244 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
0578089f PM |
245 | } |
246 | ||
247 | /* | |
248 | * timekeeping -- very crude -- should use MONOTONIC... | |
249 | */ | |
250 | ||
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 \ | |
06f22bdb | 267 | __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \ |
e1789ce2 | 268 | } __per_thread_##name[NR_THREADS] |
0578089f PM |
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 | ||
0578089f PM |
281 | DEFINE_PER_THREAD(int, smp_processor_id); |
282 | ||
0578089f PM |
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 | ||
1a43bbd8 | 308 | #endif |