Commit | Line | Data |
---|---|---|
6d0ce021 PM |
1 | /* MECHANICALLY GENERATED, DO NOT EDIT!!! */ |
2 | ||
1a43bbd8 | 3 | #ifndef _INCLUDE_API_H |
6d0ce021 PM |
4 | #define _INCLUDE_API_H |
5 | ||
d8540fc5 PA |
6 | #include "../config.h" |
7 | ||
6d0ce021 PM |
8 | /* |
9 | * common.h: Common Linux kernel-isms. | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; but version 2 of the License only due | |
14 | * to code included from the Linux kernel. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
24 | * | |
25 | * Copyright (c) 2006 Paul E. McKenney, IBM. | |
26 | * | |
27 | * Much code taken from the Linux kernel. For such code, the option | |
28 | * to redistribute under later versions of GPL might not be available. | |
29 | */ | |
30 | ||
22b63ec4 MD |
31 | #include <urcu/arch.h> |
32 | ||
6d0ce021 PM |
33 | #ifndef __always_inline |
34 | #define __always_inline inline | |
35 | #endif | |
36 | ||
37 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) | |
38 | #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1) | |
39 | ||
40 | #ifdef __ASSEMBLY__ | |
41 | # define stringify_in_c(...) __VA_ARGS__ | |
42 | # define ASM_CONST(x) x | |
43 | #else | |
44 | /* This version of stringify will deal with commas... */ | |
45 | # define __stringify_in_c(...) #__VA_ARGS__ | |
46 | # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " " | |
47 | # define __ASM_CONST(x) x##UL | |
48 | # define ASM_CONST(x) __ASM_CONST(x) | |
49 | #endif | |
50 | ||
51 | ||
52 | /* | |
53 | * arch-i386.h: Expose x86 atomic instructions. 80486 and better only. | |
54 | * | |
55 | * This program is free software; you can redistribute it and/or modify | |
56 | * it under the terms of the GNU General Public License as published by | |
57 | * the Free Software Foundation, but version 2 only due to inclusion | |
58 | * of Linux-kernel code. | |
59 | * | |
60 | * This program is distributed in the hope that it will be useful, | |
61 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
62 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
63 | * GNU General Public License for more details. | |
64 | * | |
65 | * You should have received a copy of the GNU General Public License | |
66 | * along with this program; if not, write to the Free Software | |
67 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
68 | * | |
69 | * Copyright (c) 2006 Paul E. McKenney, IBM. | |
70 | * | |
71 | * Much code taken from the Linux kernel. For such code, the option | |
72 | * to redistribute under later versions of GPL might not be available. | |
73 | */ | |
74 | ||
75 | /* | |
76 | * Machine parameters. | |
77 | */ | |
78 | ||
06f22bdb | 79 | /* #define CAA_CACHE_LINE_SIZE 64 */ |
6d0ce021 PM |
80 | #define ____cacheline_internodealigned_in_smp \ |
81 | __attribute__((__aligned__(1 << 6))) | |
82 | ||
83 | #define LOCK_PREFIX "lock ; " | |
84 | ||
6ee91d83 MD |
85 | #if 0 /* duplicate with arch_atomic.h */ |
86 | ||
6d0ce021 PM |
87 | /* |
88 | * Atomic data structure, initialization, and access. | |
89 | */ | |
90 | ||
91 | typedef struct { volatile int counter; } atomic_t; | |
92 | ||
93 | #define ATOMIC_INIT(i) { (i) } | |
94 | ||
95 | #define atomic_read(v) ((v)->counter) | |
96 | #define atomic_set(v, i) (((v)->counter) = (i)) | |
97 | ||
98 | /* | |
99 | * Atomic operations. | |
100 | */ | |
101 | ||
102 | /** | |
103 | * atomic_add - add integer to atomic variable | |
104 | * @i: integer value to add | |
105 | * @v: pointer of type atomic_t | |
106 | * | |
107 | * Atomically adds @i to @v. | |
108 | */ | |
109 | static __inline__ void atomic_add(int i, atomic_t *v) | |
110 | { | |
111 | __asm__ __volatile__( | |
112 | LOCK_PREFIX "addl %1,%0" | |
113 | :"+m" (v->counter) | |
114 | :"ir" (i)); | |
115 | } | |
116 | ||
117 | /** | |
118 | * atomic_sub - subtract the atomic variable | |
119 | * @i: integer value to subtract | |
120 | * @v: pointer of type atomic_t | |
121 | * | |
122 | * Atomically subtracts @i from @v. | |
123 | */ | |
124 | static __inline__ void atomic_sub(int i, atomic_t *v) | |
125 | { | |
126 | __asm__ __volatile__( | |
127 | LOCK_PREFIX "subl %1,%0" | |
128 | :"+m" (v->counter) | |
129 | :"ir" (i)); | |
130 | } | |
131 | ||
132 | /** | |
133 | * atomic_sub_and_test - subtract value from variable and test result | |
134 | * @i: integer value to subtract | |
135 | * @v: pointer of type atomic_t | |
136 | * | |
137 | * Atomically subtracts @i from @v and returns | |
138 | * true if the result is zero, or false for all | |
139 | * other cases. | |
140 | */ | |
141 | static __inline__ int atomic_sub_and_test(int i, atomic_t *v) | |
142 | { | |
143 | unsigned char c; | |
144 | ||
145 | __asm__ __volatile__( | |
146 | LOCK_PREFIX "subl %2,%0; sete %1" | |
147 | :"+m" (v->counter), "=qm" (c) | |
148 | :"ir" (i) : "memory"); | |
149 | return c; | |
150 | } | |
151 | ||
152 | /** | |
153 | * atomic_inc - increment atomic variable | |
154 | * @v: pointer of type atomic_t | |
155 | * | |
156 | * Atomically increments @v by 1. | |
157 | */ | |
158 | static __inline__ void atomic_inc(atomic_t *v) | |
159 | { | |
160 | __asm__ __volatile__( | |
161 | LOCK_PREFIX "incl %0" | |
162 | :"+m" (v->counter)); | |
163 | } | |
164 | ||
165 | /** | |
166 | * atomic_dec - decrement atomic variable | |
167 | * @v: pointer of type atomic_t | |
168 | * | |
169 | * Atomically decrements @v by 1. | |
170 | */ | |
171 | static __inline__ void atomic_dec(atomic_t *v) | |
172 | { | |
173 | __asm__ __volatile__( | |
174 | LOCK_PREFIX "decl %0" | |
175 | :"+m" (v->counter)); | |
176 | } | |
177 | ||
178 | /** | |
179 | * atomic_dec_and_test - decrement and test | |
180 | * @v: pointer of type atomic_t | |
181 | * | |
182 | * Atomically decrements @v by 1 and | |
183 | * returns true if the result is 0, or false for all other | |
184 | * cases. | |
185 | */ | |
186 | static __inline__ int atomic_dec_and_test(atomic_t *v) | |
187 | { | |
188 | unsigned char c; | |
189 | ||
190 | __asm__ __volatile__( | |
191 | LOCK_PREFIX "decl %0; sete %1" | |
192 | :"+m" (v->counter), "=qm" (c) | |
193 | : : "memory"); | |
194 | return c != 0; | |
195 | } | |
196 | ||
197 | /** | |
198 | * atomic_inc_and_test - increment and test | |
199 | * @v: pointer of type atomic_t | |
200 | * | |
201 | * Atomically increments @v by 1 | |
202 | * and returns true if the result is zero, or false for all | |
203 | * other cases. | |
204 | */ | |
205 | static __inline__ int atomic_inc_and_test(atomic_t *v) | |
206 | { | |
207 | unsigned char c; | |
208 | ||
209 | __asm__ __volatile__( | |
210 | LOCK_PREFIX "incl %0; sete %1" | |
211 | :"+m" (v->counter), "=qm" (c) | |
212 | : : "memory"); | |
213 | return c != 0; | |
214 | } | |
215 | ||
216 | /** | |
217 | * atomic_add_negative - add and test if negative | |
218 | * @v: pointer of type atomic_t | |
219 | * @i: integer value to add | |
220 | * | |
221 | * Atomically adds @i to @v and returns true | |
222 | * if the result is negative, or false when | |
223 | * result is greater than or equal to zero. | |
224 | */ | |
225 | static __inline__ int atomic_add_negative(int i, atomic_t *v) | |
226 | { | |
227 | unsigned char c; | |
228 | ||
229 | __asm__ __volatile__( | |
230 | LOCK_PREFIX "addl %2,%0; sets %1" | |
231 | :"+m" (v->counter), "=qm" (c) | |
232 | :"ir" (i) : "memory"); | |
233 | return c; | |
234 | } | |
235 | ||
236 | /** | |
237 | * atomic_add_return - add and return | |
238 | * @v: pointer of type atomic_t | |
239 | * @i: integer value to add | |
240 | * | |
241 | * Atomically adds @i to @v and returns @i + @v | |
242 | */ | |
243 | static __inline__ int atomic_add_return(int i, atomic_t *v) | |
244 | { | |
245 | int __i; | |
246 | ||
247 | __i = i; | |
248 | __asm__ __volatile__( | |
249 | LOCK_PREFIX "xaddl %0, %1;" | |
250 | :"=r"(i) | |
251 | :"m"(v->counter), "0"(i)); | |
252 | return i + __i; | |
253 | } | |
254 | ||
255 | static __inline__ int atomic_sub_return(int i, atomic_t *v) | |
256 | { | |
257 | return atomic_add_return(-i,v); | |
258 | } | |
259 | ||
260 | static inline unsigned int | |
261 | cmpxchg(volatile long *ptr, long oldval, long newval) | |
262 | { | |
263 | unsigned long retval; | |
264 | ||
265 | asm("# cmpxchg\n" | |
266 | "lock; cmpxchgl %4,(%2)\n" | |
267 | "# end atomic_cmpxchg4" | |
268 | : "=a" (retval), "=m" (*ptr) | |
269 | : "r" (ptr), "0" (oldval), "r" (newval), "m" (*ptr) | |
270 | : "cc"); | |
271 | return (retval); | |
272 | } | |
273 | ||
274 | #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new)) | |
275 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) | |
276 | ||
277 | /** | |
278 | * atomic_add_unless - add unless the number is a given value | |
279 | * @v: pointer of type atomic_t | |
280 | * @a: the amount to add to v... | |
281 | * @u: ...unless v is equal to u. | |
282 | * | |
283 | * Atomically adds @a to @v, so long as it was not @u. | |
284 | * Returns non-zero if @v was not @u, and zero otherwise. | |
285 | */ | |
286 | #define atomic_add_unless(v, a, u) \ | |
287 | ({ \ | |
288 | int c, old; \ | |
289 | c = atomic_read(v); \ | |
290 | for (;;) { \ | |
291 | if (unlikely(c == (u))) \ | |
292 | break; \ | |
293 | old = atomic_cmpxchg((v), c, c + (a)); \ | |
294 | if (likely(old == c)) \ | |
295 | break; \ | |
296 | c = old; \ | |
297 | } \ | |
298 | c != (u); \ | |
299 | }) | |
300 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) | |
301 | ||
302 | #define atomic_inc_return(v) (atomic_add_return(1,v)) | |
303 | #define atomic_dec_return(v) (atomic_sub_return(1,v)) | |
304 | ||
305 | /* These are x86-specific, used by some header files */ | |
306 | #define atomic_clear_mask(mask, addr) \ | |
307 | __asm__ __volatile__(LOCK_PREFIX "andl %0,%1" \ | |
308 | : : "r" (~(mask)),"m" (*addr) : "memory") | |
309 | ||
310 | #define atomic_set_mask(mask, addr) \ | |
311 | __asm__ __volatile__(LOCK_PREFIX "orl %0,%1" \ | |
312 | : : "r" (mask),"m" (*(addr)) : "memory") | |
313 | ||
314 | /* Atomic operations are already serializing on x86 */ | |
5481ddb3 DG |
315 | #define smp_mb__before_atomic_dec() cmm_barrier() |
316 | #define smp_mb__after_atomic_dec() cmm_barrier() | |
317 | #define smp_mb__before_atomic_inc() cmm_barrier() | |
318 | #define smp_mb__after_atomic_inc() cmm_barrier() | |
6d0ce021 | 319 | |
6ee91d83 MD |
320 | #endif //0 |
321 | ||
6d0ce021 PM |
322 | /* |
323 | * api_pthreads.h: API mapping to pthreads environment. | |
324 | * | |
325 | * This program is free software; you can redistribute it and/or modify | |
326 | * it under the terms of the GNU General Public License as published by | |
327 | * the Free Software Foundation; either version 2 of the License, or | |
328 | * (at your option) any later version. However, please note that much | |
329 | * of the code in this file derives from the Linux kernel, and that such | |
330 | * code may not be available except under GPLv2. | |
331 | * | |
332 | * This program is distributed in the hope that it will be useful, | |
333 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
334 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
335 | * GNU General Public License for more details. | |
336 | * | |
337 | * You should have received a copy of the GNU General Public License | |
338 | * along with this program; if not, write to the Free Software | |
339 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
340 | * | |
341 | * Copyright (c) 2006 Paul E. McKenney, IBM. | |
342 | */ | |
343 | ||
344 | #include <stdio.h> | |
345 | #include <stdlib.h> | |
346 | #include <errno.h> | |
347 | #include <limits.h> | |
348 | #include <sys/types.h> | |
349 | #define __USE_GNU | |
350 | #include <pthread.h> | |
351 | #include <sched.h> | |
352 | #include <sys/param.h> | |
353 | /* #include "atomic.h" */ | |
354 | ||
6d0ce021 PM |
355 | /* |
356 | * Default machine parameters. | |
357 | */ | |
358 | ||
06f22bdb DG |
359 | #ifndef CAA_CACHE_LINE_SIZE |
360 | /* #define CAA_CACHE_LINE_SIZE 128 */ | |
361 | #endif /* #ifndef CAA_CACHE_LINE_SIZE */ | |
6d0ce021 PM |
362 | |
363 | /* | |
364 | * Exclusive locking primitives. | |
365 | */ | |
366 | ||
367 | typedef pthread_mutex_t spinlock_t; | |
368 | ||
369 | #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER; | |
370 | #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER | |
371 | ||
372 | static void spin_lock_init(spinlock_t *sp) | |
373 | { | |
374 | if (pthread_mutex_init(sp, NULL) != 0) { | |
375 | perror("spin_lock_init:pthread_mutex_init"); | |
376 | exit(-1); | |
377 | } | |
378 | } | |
379 | ||
380 | static void spin_lock(spinlock_t *sp) | |
381 | { | |
382 | if (pthread_mutex_lock(sp) != 0) { | |
383 | perror("spin_lock:pthread_mutex_lock"); | |
384 | exit(-1); | |
385 | } | |
386 | } | |
387 | ||
6d0ce021 PM |
388 | static void spin_unlock(spinlock_t *sp) |
389 | { | |
390 | if (pthread_mutex_unlock(sp) != 0) { | |
391 | perror("spin_unlock:pthread_mutex_unlock"); | |
392 | exit(-1); | |
393 | } | |
394 | } | |
395 | ||
396 | #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0) | |
397 | #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0) | |
398 | ||
6d0ce021 PM |
399 | /* |
400 | * Thread creation/destruction primitives. | |
401 | */ | |
402 | ||
403 | typedef pthread_t thread_id_t; | |
404 | ||
405 | #define NR_THREADS 128 | |
406 | ||
407 | #define __THREAD_ID_MAP_EMPTY 0 | |
408 | #define __THREAD_ID_MAP_WAITING 1 | |
409 | thread_id_t __thread_id_map[NR_THREADS]; | |
410 | spinlock_t __thread_id_map_mutex; | |
411 | ||
412 | #define for_each_thread(t) \ | |
413 | for (t = 0; t < NR_THREADS; t++) | |
414 | ||
415 | #define for_each_running_thread(t) \ | |
416 | for (t = 0; t < NR_THREADS; t++) \ | |
417 | if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \ | |
418 | (__thread_id_map[t] != __THREAD_ID_MAP_WAITING)) | |
419 | ||
420 | pthread_key_t thread_id_key; | |
421 | ||
422 | static int __smp_thread_id(void) | |
423 | { | |
424 | int i; | |
425 | thread_id_t tid = pthread_self(); | |
426 | ||
427 | for (i = 0; i < NR_THREADS; i++) { | |
428 | if (__thread_id_map[i] == tid) { | |
429 | long v = i + 1; /* must be non-NULL. */ | |
430 | ||
431 | if (pthread_setspecific(thread_id_key, (void *)v) != 0) { | |
432 | perror("pthread_setspecific"); | |
433 | exit(-1); | |
434 | } | |
435 | return i; | |
436 | } | |
437 | } | |
438 | spin_lock(&__thread_id_map_mutex); | |
439 | for (i = 0; i < NR_THREADS; i++) { | |
440 | if (__thread_id_map[i] == tid) | |
441 | spin_unlock(&__thread_id_map_mutex); | |
442 | return i; | |
443 | } | |
444 | spin_unlock(&__thread_id_map_mutex); | |
0578089f PM |
445 | fprintf(stderr, "smp_thread_id: Rogue thread, id: %d(%#x)\n", |
446 | (int)tid, (int)tid); | |
6d0ce021 PM |
447 | exit(-1); |
448 | } | |
449 | ||
450 | static int smp_thread_id(void) | |
451 | { | |
452 | void *id; | |
453 | ||
454 | id = pthread_getspecific(thread_id_key); | |
455 | if (id == NULL) | |
456 | return __smp_thread_id(); | |
457 | return (long)(id - 1); | |
458 | } | |
459 | ||
460 | static thread_id_t create_thread(void *(*func)(void *), void *arg) | |
461 | { | |
462 | thread_id_t tid; | |
463 | int i; | |
464 | ||
465 | spin_lock(&__thread_id_map_mutex); | |
466 | for (i = 0; i < NR_THREADS; i++) { | |
467 | if (__thread_id_map[i] == __THREAD_ID_MAP_EMPTY) | |
468 | break; | |
469 | } | |
470 | if (i >= NR_THREADS) { | |
471 | spin_unlock(&__thread_id_map_mutex); | |
472 | fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS); | |
473 | exit(-1); | |
474 | } | |
475 | __thread_id_map[i] = __THREAD_ID_MAP_WAITING; | |
476 | spin_unlock(&__thread_id_map_mutex); | |
477 | if (pthread_create(&tid, NULL, func, arg) != 0) { | |
478 | perror("create_thread:pthread_create"); | |
479 | exit(-1); | |
480 | } | |
481 | __thread_id_map[i] = tid; | |
482 | return tid; | |
483 | } | |
484 | ||
485 | static void *wait_thread(thread_id_t tid) | |
486 | { | |
487 | int i; | |
488 | void *vp; | |
489 | ||
490 | for (i = 0; i < NR_THREADS; i++) { | |
491 | if (__thread_id_map[i] == tid) | |
492 | break; | |
493 | } | |
494 | if (i >= NR_THREADS){ | |
0578089f PM |
495 | fprintf(stderr, "wait_thread: bad tid = %d(%#x)\n", |
496 | (int)tid, (int)tid); | |
6d0ce021 PM |
497 | exit(-1); |
498 | } | |
499 | if (pthread_join(tid, &vp) != 0) { | |
500 | perror("wait_thread:pthread_join"); | |
501 | exit(-1); | |
502 | } | |
503 | __thread_id_map[i] = __THREAD_ID_MAP_EMPTY; | |
504 | return vp; | |
505 | } | |
506 | ||
507 | static void wait_all_threads(void) | |
508 | { | |
509 | int i; | |
510 | thread_id_t tid; | |
511 | ||
512 | for (i = 1; i < NR_THREADS; i++) { | |
513 | tid = __thread_id_map[i]; | |
514 | if (tid != __THREAD_ID_MAP_EMPTY && | |
515 | tid != __THREAD_ID_MAP_WAITING) | |
516 | (void)wait_thread(tid); | |
517 | } | |
518 | } | |
519 | ||
d8540fc5 PA |
520 | #ifndef HAVE_CPU_SET_T |
521 | typedef unsigned long cpu_set_t; | |
522 | # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) | |
523 | # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) | |
524 | #endif | |
525 | ||
6d0ce021 PM |
526 | static void run_on(int cpu) |
527 | { | |
d8540fc5 | 528 | #if HAVE_SCHED_SETAFFINITY |
6d0ce021 PM |
529 | cpu_set_t mask; |
530 | ||
531 | CPU_ZERO(&mask); | |
532 | CPU_SET(cpu, &mask); | |
d8540fc5 PA |
533 | #if SCHED_SETAFFINITY_ARGS == 2 |
534 | sched_setaffinity(0, &mask); | |
535 | #else | |
6d0ce021 | 536 | sched_setaffinity(0, sizeof(mask), &mask); |
d8540fc5 PA |
537 | #endif |
538 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
6d0ce021 PM |
539 | } |
540 | ||
541 | /* | |
542 | * timekeeping -- very crude -- should use MONOTONIC... | |
543 | */ | |
544 | ||
545 | long long get_microseconds(void) | |
546 | { | |
547 | struct timeval tv; | |
548 | ||
549 | if (gettimeofday(&tv, NULL) != 0) | |
550 | abort(); | |
551 | return ((long long)tv.tv_sec) * 1000000LL + (long long)tv.tv_usec; | |
552 | } | |
553 | ||
554 | /* | |
555 | * Per-thread variables. | |
556 | */ | |
557 | ||
558 | #define DEFINE_PER_THREAD(type, name) \ | |
559 | struct { \ | |
560 | __typeof__(type) v \ | |
06f22bdb | 561 | __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \ |
6d0ce021 PM |
562 | } __per_thread_##name[NR_THREADS]; |
563 | #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name) | |
564 | ||
565 | #define per_thread(name, thread) __per_thread_##name[thread].v | |
566 | #define __get_thread_var(name) per_thread(name, smp_thread_id()) | |
567 | ||
568 | #define init_per_thread(name, v) \ | |
569 | do { \ | |
570 | int __i_p_t_i; \ | |
571 | for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \ | |
572 | per_thread(name, __i_p_t_i) = v; \ | |
573 | } while (0) | |
574 | ||
575 | /* | |
576 | * CPU traversal primitives. | |
577 | */ | |
578 | ||
579 | #ifndef NR_CPUS | |
580 | #define NR_CPUS 16 | |
581 | #endif /* #ifndef NR_CPUS */ | |
582 | ||
583 | #define for_each_possible_cpu(cpu) \ | |
584 | for (cpu = 0; cpu < NR_CPUS; cpu++) | |
585 | #define for_each_online_cpu(cpu) \ | |
586 | for (cpu = 0; cpu < NR_CPUS; cpu++) | |
587 | ||
588 | /* | |
589 | * Per-CPU variables. | |
590 | */ | |
591 | ||
592 | #define DEFINE_PER_CPU(type, name) \ | |
593 | struct { \ | |
594 | __typeof__(type) v \ | |
06f22bdb | 595 | __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \ |
6d0ce021 PM |
596 | } __per_cpu_##name[NR_CPUS] |
597 | #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name) | |
598 | ||
599 | DEFINE_PER_THREAD(int, smp_processor_id); | |
600 | ||
6d0ce021 PM |
601 | #define per_cpu(name, thread) __per_cpu_##name[thread].v |
602 | #define __get_cpu_var(name) per_cpu(name, smp_processor_id()) | |
603 | ||
604 | #define init_per_cpu(name, v) \ | |
605 | do { \ | |
606 | int __i_p_c_i; \ | |
607 | for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \ | |
608 | per_cpu(name, __i_p_c_i) = v; \ | |
609 | } while (0) | |
610 | ||
611 | /* | |
612 | * CPU state checking (crowbarred). | |
613 | */ | |
614 | ||
615 | #define idle_cpu(cpu) 0 | |
616 | #define in_softirq() 1 | |
617 | #define hardirq_count() 0 | |
618 | #define PREEMPT_SHIFT 0 | |
619 | #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS) | |
620 | #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS) | |
621 | #define PREEMPT_BITS 8 | |
622 | #define SOFTIRQ_BITS 8 | |
623 | ||
624 | /* | |
625 | * CPU hotplug. | |
626 | */ | |
627 | ||
628 | struct notifier_block { | |
629 | int (*notifier_call)(struct notifier_block *, unsigned long, void *); | |
630 | struct notifier_block *next; | |
631 | int priority; | |
632 | }; | |
633 | ||
634 | #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */ | |
635 | #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */ | |
636 | #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */ | |
637 | #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */ | |
638 | #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */ | |
639 | #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */ | |
640 | #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task, | |
641 | * not handling interrupts, soon dead */ | |
642 | #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug | |
643 | * lock is dropped */ | |
644 | ||
645 | /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend | |
646 | * operation in progress | |
647 | */ | |
648 | #define CPU_TASKS_FROZEN 0x0010 | |
649 | ||
650 | #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN) | |
651 | #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN) | |
652 | #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN) | |
653 | #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN) | |
654 | #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN) | |
655 | #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN) | |
656 | #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN) | |
657 | ||
658 | /* Hibernation and suspend events */ | |
659 | #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */ | |
660 | #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */ | |
661 | #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */ | |
662 | #define PM_POST_SUSPEND 0x0004 /* Suspend finished */ | |
663 | #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */ | |
664 | #define PM_POST_RESTORE 0x0006 /* Restore failed */ | |
665 | ||
666 | #define NOTIFY_DONE 0x0000 /* Don't care */ | |
667 | #define NOTIFY_OK 0x0001 /* Suits me */ | |
668 | #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */ | |
669 | #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002) | |
670 | /* Bad/Veto action */ | |
671 | /* | |
672 | * Clean way to return from the notifier and stop further calls. | |
673 | */ | |
674 | #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK) | |
675 | ||
676 | /* | |
677 | * Bug checks. | |
678 | */ | |
679 | ||
680 | #define BUG_ON(c) do { if (!(c)) abort(); } while (0) | |
681 | ||
682 | /* | |
683 | * Initialization -- Must be called before calling any primitives. | |
684 | */ | |
685 | ||
686 | static void smp_init(void) | |
687 | { | |
688 | int i; | |
689 | ||
690 | spin_lock_init(&__thread_id_map_mutex); | |
691 | __thread_id_map[0] = pthread_self(); | |
692 | for (i = 1; i < NR_THREADS; i++) | |
693 | __thread_id_map[i] = __THREAD_ID_MAP_EMPTY; | |
694 | init_per_thread(smp_processor_id, 0); | |
695 | if (pthread_key_create(&thread_id_key, NULL) != 0) { | |
696 | perror("pthread_key_create"); | |
697 | exit(-1); | |
698 | } | |
699 | } | |
700 | ||
701 | /* Taken from the Linux kernel source tree, so GPLv2-only!!! */ | |
702 | ||
703 | #ifndef _LINUX_LIST_H | |
704 | #define _LINUX_LIST_H | |
705 | ||
706 | #define LIST_POISON1 ((void *) 0x00100100) | |
707 | #define LIST_POISON2 ((void *) 0x00200200) | |
708 | ||
63ff4873 MD |
709 | #if 0 |
710 | ||
6d0ce021 PM |
711 | /* |
712 | * Simple doubly linked list implementation. | |
713 | * | |
714 | * Some of the internal functions ("__xxx") are useful when | |
715 | * manipulating whole lists rather than single entries, as | |
716 | * sometimes we already know the next/prev entries and we can | |
717 | * generate better code by using them directly rather than | |
718 | * using the generic single-entry routines. | |
719 | */ | |
720 | ||
16aa9ee8 DG |
721 | struct cds_list_head { |
722 | struct cds_list_head *next, *prev; | |
6d0ce021 PM |
723 | }; |
724 | ||
16aa9ee8 | 725 | #define CDS_LIST_HEAD_INIT(name) { &(name), &(name) } |
6d0ce021 | 726 | |
16aa9ee8 DG |
727 | #define CDS_LIST_HEAD(name) \ |
728 | struct cds_list_head name = CDS_LIST_HEAD_INIT(name) | |
6d0ce021 | 729 | |
16aa9ee8 | 730 | static inline void CDS_INIT_LIST_HEAD(struct cds_list_head *list) |
6d0ce021 PM |
731 | { |
732 | list->next = list; | |
733 | list->prev = list; | |
734 | } | |
735 | ||
736 | /* | |
737 | * Insert a new entry between two known consecutive entries. | |
738 | * | |
739 | * This is only for internal list manipulation where we know | |
740 | * the prev/next entries already! | |
741 | */ | |
742 | #ifndef CONFIG_DEBUG_LIST | |
16aa9ee8 DG |
743 | static inline void __cds_list_add(struct cds_list_head *new, |
744 | struct cds_list_head *prev, | |
745 | struct cds_list_head *next) | |
6d0ce021 PM |
746 | { |
747 | next->prev = new; | |
748 | new->next = next; | |
749 | new->prev = prev; | |
750 | prev->next = new; | |
751 | } | |
752 | #else | |
16aa9ee8 DG |
753 | extern void __cds_list_add(struct cds_list_head *new, |
754 | struct cds_list_head *prev, | |
755 | struct cds_list_head *next); | |
6d0ce021 PM |
756 | #endif |
757 | ||
758 | /** | |
16aa9ee8 | 759 | * cds_list_add - add a new entry |
6d0ce021 PM |
760 | * @new: new entry to be added |
761 | * @head: list head to add it after | |
762 | * | |
763 | * Insert a new entry after the specified head. | |
764 | * This is good for implementing stacks. | |
765 | */ | |
16aa9ee8 | 766 | static inline void cds_list_add(struct cds_list_head *new, struct cds_list_head *head) |
6d0ce021 | 767 | { |
16aa9ee8 | 768 | __cds_list_add(new, head, head->next); |
6d0ce021 PM |
769 | } |
770 | ||
771 | ||
772 | /** | |
16aa9ee8 | 773 | * cds_list_add_tail - add a new entry |
6d0ce021 PM |
774 | * @new: new entry to be added |
775 | * @head: list head to add it before | |
776 | * | |
777 | * Insert a new entry before the specified head. | |
778 | * This is useful for implementing queues. | |
779 | */ | |
16aa9ee8 | 780 | static inline void cds_list_add_tail(struct cds_list_head *new, struct cds_list_head *head) |
6d0ce021 | 781 | { |
16aa9ee8 | 782 | __cds_list_add(new, head->prev, head); |
6d0ce021 PM |
783 | } |
784 | ||
785 | /* | |
786 | * Delete a list entry by making the prev/next entries | |
787 | * point to each other. | |
788 | * | |
789 | * This is only for internal list manipulation where we know | |
790 | * the prev/next entries already! | |
791 | */ | |
16aa9ee8 | 792 | static inline void __cds_list_del(struct cds_list_head * prev, struct cds_list_head * next) |
6d0ce021 PM |
793 | { |
794 | next->prev = prev; | |
795 | prev->next = next; | |
796 | } | |
797 | ||
798 | /** | |
16aa9ee8 | 799 | * cds_list_del - deletes entry from list. |
6d0ce021 | 800 | * @entry: the element to delete from the list. |
16aa9ee8 | 801 | * Note: cds_list_empty() on entry does not return true after this, the entry is |
6d0ce021 PM |
802 | * in an undefined state. |
803 | */ | |
804 | #ifndef CONFIG_DEBUG_LIST | |
16aa9ee8 | 805 | static inline void cds_list_del(struct cds_list_head *entry) |
6d0ce021 | 806 | { |
16aa9ee8 | 807 | __cds_list_del(entry->prev, entry->next); |
6d0ce021 PM |
808 | entry->next = LIST_POISON1; |
809 | entry->prev = LIST_POISON2; | |
810 | } | |
811 | #else | |
16aa9ee8 | 812 | extern void cds_list_del(struct cds_list_head *entry); |
6d0ce021 PM |
813 | #endif |
814 | ||
815 | /** | |
16aa9ee8 | 816 | * cds_list_replace - replace old entry by new one |
6d0ce021 PM |
817 | * @old : the element to be replaced |
818 | * @new : the new element to insert | |
819 | * | |
820 | * If @old was empty, it will be overwritten. | |
821 | */ | |
16aa9ee8 DG |
822 | static inline void cds_list_replace(struct cds_list_head *old, |
823 | struct cds_list_head *new) | |
6d0ce021 PM |
824 | { |
825 | new->next = old->next; | |
826 | new->next->prev = new; | |
827 | new->prev = old->prev; | |
828 | new->prev->next = new; | |
829 | } | |
830 | ||
16aa9ee8 DG |
831 | static inline void cds_list_replace_init(struct cds_list_head *old, |
832 | struct cds_list_head *new) | |
6d0ce021 | 833 | { |
16aa9ee8 DG |
834 | cds_list_replace(old, new); |
835 | CDS_INIT_LIST_HEAD(old); | |
6d0ce021 PM |
836 | } |
837 | ||
838 | /** | |
16aa9ee8 | 839 | * cds_list_del_init - deletes entry from list and reinitialize it. |
6d0ce021 PM |
840 | * @entry: the element to delete from the list. |
841 | */ | |
16aa9ee8 | 842 | static inline void cds_list_del_init(struct cds_list_head *entry) |
6d0ce021 | 843 | { |
16aa9ee8 DG |
844 | __cds_list_del(entry->prev, entry->next); |
845 | CDS_INIT_LIST_HEAD(entry); | |
6d0ce021 PM |
846 | } |
847 | ||
848 | /** | |
16aa9ee8 | 849 | * cds_list_move - delete from one list and add as another's head |
6d0ce021 PM |
850 | * @list: the entry to move |
851 | * @head: the head that will precede our entry | |
852 | */ | |
16aa9ee8 | 853 | static inline void cds_list_move(struct cds_list_head *list, struct cds_list_head *head) |
6d0ce021 | 854 | { |
16aa9ee8 DG |
855 | __cds_list_del(list->prev, list->next); |
856 | cds_list_add(list, head); | |
6d0ce021 PM |
857 | } |
858 | ||
859 | /** | |
16aa9ee8 | 860 | * cds_list_move_tail - delete from one list and add as another's tail |
6d0ce021 PM |
861 | * @list: the entry to move |
862 | * @head: the head that will follow our entry | |
863 | */ | |
16aa9ee8 DG |
864 | static inline void cds_list_move_tail(struct cds_list_head *list, |
865 | struct cds_list_head *head) | |
6d0ce021 | 866 | { |
16aa9ee8 DG |
867 | __cds_list_del(list->prev, list->next); |
868 | cds_list_add_tail(list, head); | |
6d0ce021 PM |
869 | } |
870 | ||
871 | /** | |
872 | * list_is_last - tests whether @list is the last entry in list @head | |
873 | * @list: the entry to test | |
874 | * @head: the head of the list | |
875 | */ | |
16aa9ee8 DG |
876 | static inline int list_is_last(const struct cds_list_head *list, |
877 | const struct cds_list_head *head) | |
6d0ce021 PM |
878 | { |
879 | return list->next == head; | |
880 | } | |
881 | ||
882 | /** | |
16aa9ee8 | 883 | * cds_list_empty - tests whether a list is empty |
6d0ce021 PM |
884 | * @head: the list to test. |
885 | */ | |
16aa9ee8 | 886 | static inline int cds_list_empty(const struct cds_list_head *head) |
6d0ce021 PM |
887 | { |
888 | return head->next == head; | |
889 | } | |
890 | ||
891 | /** | |
16aa9ee8 | 892 | * cds_list_empty_careful - tests whether a list is empty and not being modified |
6d0ce021 PM |
893 | * @head: the list to test |
894 | * | |
895 | * Description: | |
896 | * tests whether a list is empty _and_ checks that no other CPU might be | |
897 | * in the process of modifying either member (next or prev) | |
898 | * | |
16aa9ee8 | 899 | * NOTE: using cds_list_empty_careful() without synchronization |
6d0ce021 | 900 | * can only be safe if the only activity that can happen |
16aa9ee8 | 901 | * to the list entry is cds_list_del_init(). Eg. it cannot be used |
6d0ce021 PM |
902 | * if another CPU could re-list_add() it. |
903 | */ | |
16aa9ee8 | 904 | static inline int cds_list_empty_careful(const struct cds_list_head *head) |
6d0ce021 | 905 | { |
16aa9ee8 | 906 | struct cds_list_head *next = head->next; |
6d0ce021 PM |
907 | return (next == head) && (next == head->prev); |
908 | } | |
909 | ||
910 | /** | |
911 | * list_is_singular - tests whether a list has just one entry. | |
912 | * @head: the list to test. | |
913 | */ | |
16aa9ee8 | 914 | static inline int list_is_singular(const struct cds_list_head *head) |
6d0ce021 PM |
915 | { |
916 | return !list_empty(head) && (head->next == head->prev); | |
917 | } | |
918 | ||
16aa9ee8 DG |
919 | static inline void __list_cut_position(struct cds_list_head *list, |
920 | struct cds_list_head *head, struct cds_list_head *entry) | |
6d0ce021 | 921 | { |
16aa9ee8 | 922 | struct cds_list_head *new_first = entry->next; |
6d0ce021 PM |
923 | list->next = head->next; |
924 | list->next->prev = list; | |
925 | list->prev = entry; | |
926 | entry->next = list; | |
927 | head->next = new_first; | |
928 | new_first->prev = head; | |
929 | } | |
930 | ||
931 | /** | |
932 | * list_cut_position - cut a list into two | |
933 | * @list: a new list to add all removed entries | |
934 | * @head: a list with entries | |
935 | * @entry: an entry within head, could be the head itself | |
936 | * and if so we won't cut the list | |
937 | * | |
938 | * This helper moves the initial part of @head, up to and | |
939 | * including @entry, from @head to @list. You should | |
940 | * pass on @entry an element you know is on @head. @list | |
941 | * should be an empty list or a list you do not care about | |
942 | * losing its data. | |
943 | * | |
944 | */ | |
16aa9ee8 DG |
945 | static inline void list_cut_position(struct cds_list_head *list, |
946 | struct cds_list_head *head, struct cds_list_head *entry) | |
6d0ce021 | 947 | { |
16aa9ee8 | 948 | if (cds_list_empty(head)) |
6d0ce021 PM |
949 | return; |
950 | if (list_is_singular(head) && | |
951 | (head->next != entry && head != entry)) | |
952 | return; | |
953 | if (entry == head) | |
16aa9ee8 | 954 | CDS_INIT_LIST_HEAD(list); |
6d0ce021 PM |
955 | else |
956 | __list_cut_position(list, head, entry); | |
957 | } | |
958 | ||
16aa9ee8 DG |
959 | static inline void __cds_list_splice(const struct cds_list_head *list, |
960 | struct cds_list_head *prev, | |
961 | struct cds_list_head *next) | |
6d0ce021 | 962 | { |
16aa9ee8 DG |
963 | struct cds_list_head *first = list->next; |
964 | struct cds_list_head *last = list->prev; | |
6d0ce021 PM |
965 | |
966 | first->prev = prev; | |
967 | prev->next = first; | |
968 | ||
969 | last->next = next; | |
970 | next->prev = last; | |
971 | } | |
972 | ||
973 | /** | |
16aa9ee8 | 974 | * cds_list_splice - join two lists, this is designed for stacks |
6d0ce021 PM |
975 | * @list: the new list to add. |
976 | * @head: the place to add it in the first list. | |
977 | */ | |
16aa9ee8 DG |
978 | static inline void cds_list_splice(const struct cds_list_head *list, |
979 | struct cds_list_head *head) | |
6d0ce021 | 980 | { |
16aa9ee8 DG |
981 | if (!cds_list_empty(list)) |
982 | __cds_list_splice(list, head, head->next); | |
6d0ce021 PM |
983 | } |
984 | ||
985 | /** | |
16aa9ee8 | 986 | * cds_list_splice_tail - join two lists, each list being a queue |
6d0ce021 PM |
987 | * @list: the new list to add. |
988 | * @head: the place to add it in the first list. | |
989 | */ | |
16aa9ee8 DG |
990 | static inline void cds_list_splice_tail(struct cds_list_head *list, |
991 | struct cds_list_head *head) | |
6d0ce021 | 992 | { |
16aa9ee8 DG |
993 | if (!cds_list_empty(list)) |
994 | __cds_list_splice(list, head->prev, head); | |
6d0ce021 PM |
995 | } |
996 | ||
997 | /** | |
16aa9ee8 | 998 | * cds_list_splice_init - join two lists and reinitialise the emptied list. |
6d0ce021 PM |
999 | * @list: the new list to add. |
1000 | * @head: the place to add it in the first list. | |
1001 | * | |
1002 | * The list at @list is reinitialised | |
1003 | */ | |
16aa9ee8 DG |
1004 | static inline void cds_list_splice_init(struct cds_list_head *list, |
1005 | struct cds_list_head *head) | |
6d0ce021 | 1006 | { |
16aa9ee8 DG |
1007 | if (!cds_list_empty(list)) { |
1008 | __cds_list_splice(list, head, head->next); | |
1009 | CDS_INIT_LIST_HEAD(list); | |
6d0ce021 PM |
1010 | } |
1011 | } | |
1012 | ||
1013 | /** | |
16aa9ee8 | 1014 | * cds_list_splice_tail_init - join two lists and reinitialise the emptied list |
6d0ce021 PM |
1015 | * @list: the new list to add. |
1016 | * @head: the place to add it in the first list. | |
1017 | * | |
1018 | * Each of the lists is a queue. | |
1019 | * The list at @list is reinitialised | |
1020 | */ | |
16aa9ee8 DG |
1021 | static inline void cds_list_splice_tail_init(struct cds_list_head *list, |
1022 | struct cds_list_head *head) | |
6d0ce021 | 1023 | { |
16aa9ee8 DG |
1024 | if (!cds_list_empty(list)) { |
1025 | __cds_list_splice(list, head->prev, head); | |
1026 | CDS_INIT_LIST_HEAD(list); | |
6d0ce021 PM |
1027 | } |
1028 | } | |
1029 | ||
1030 | /** | |
16aa9ee8 DG |
1031 | * cds_list_entry - get the struct for this entry |
1032 | * @ptr: the &struct cds_list_head pointer. | |
6d0ce021 PM |
1033 | * @type: the type of the struct this is embedded in. |
1034 | * @member: the name of the list_struct within the struct. | |
1035 | */ | |
16aa9ee8 | 1036 | #define cds_list_entry(ptr, type, member) \ |
06f22bdb | 1037 | caa_container_of(ptr, type, member) |
6d0ce021 PM |
1038 | |
1039 | /** | |
1040 | * list_first_entry - get the first element from a list | |
1041 | * @ptr: the list head to take the element from. | |
1042 | * @type: the type of the struct this is embedded in. | |
1043 | * @member: the name of the list_struct within the struct. | |
1044 | * | |
1045 | * Note, that list is expected to be not empty. | |
1046 | */ | |
1047 | #define list_first_entry(ptr, type, member) \ | |
16aa9ee8 | 1048 | cds_list_entry((ptr)->next, type, member) |
6d0ce021 PM |
1049 | |
1050 | /** | |
16aa9ee8 DG |
1051 | * cds_list_for_each - iterate over a list |
1052 | * @pos: the &struct cds_list_head to use as a loop cursor. | |
6d0ce021 PM |
1053 | * @head: the head for your list. |
1054 | */ | |
16aa9ee8 | 1055 | #define cds_list_for_each(pos, head) \ |
6d0ce021 PM |
1056 | for (pos = (head)->next; prefetch(pos->next), pos != (head); \ |
1057 | pos = pos->next) | |
1058 | ||
1059 | /** | |
16aa9ee8 DG |
1060 | * __cds_list_for_each - iterate over a list |
1061 | * @pos: the &struct cds_list_head to use as a loop cursor. | |
6d0ce021 PM |
1062 | * @head: the head for your list. |
1063 | * | |
16aa9ee8 | 1064 | * This variant differs from cds_list_for_each() in that it's the |
6d0ce021 PM |
1065 | * simplest possible list iteration code, no prefetching is done. |
1066 | * Use this for code that knows the list to be very short (empty | |
1067 | * or 1 entry) most of the time. | |
1068 | */ | |
16aa9ee8 | 1069 | #define __cds_list_for_each(pos, head) \ |
6d0ce021 PM |
1070 | for (pos = (head)->next; pos != (head); pos = pos->next) |
1071 | ||
1072 | /** | |
16aa9ee8 DG |
1073 | * cds_list_for_each_prev - iterate over a list backwards |
1074 | * @pos: the &struct cds_list_head to use as a loop cursor. | |
6d0ce021 PM |
1075 | * @head: the head for your list. |
1076 | */ | |
16aa9ee8 | 1077 | #define cds_list_for_each_prev(pos, head) \ |
6d0ce021 PM |
1078 | for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ |
1079 | pos = pos->prev) | |
1080 | ||
1081 | /** | |
16aa9ee8 DG |
1082 | * cds_list_for_each_safe - iterate over a list safe against removal of list entry |
1083 | * @pos: the &struct cds_list_head to use as a loop cursor. | |
1084 | * @n: another &struct cds_list_head to use as temporary storage | |
6d0ce021 PM |
1085 | * @head: the head for your list. |
1086 | */ | |
16aa9ee8 | 1087 | #define cds_list_for_each_safe(pos, n, head) \ |
6d0ce021 PM |
1088 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
1089 | pos = n, n = pos->next) | |
1090 | ||
1091 | /** | |
16aa9ee8 DG |
1092 | * cds_list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry |
1093 | * @pos: the &struct cds_list_head to use as a loop cursor. | |
1094 | * @n: another &struct cds_list_head to use as temporary storage | |
6d0ce021 PM |
1095 | * @head: the head for your list. |
1096 | */ | |
16aa9ee8 | 1097 | #define cds_list_for_each_prev_safe(pos, n, head) \ |
6d0ce021 PM |
1098 | for (pos = (head)->prev, n = pos->prev; \ |
1099 | prefetch(pos->prev), pos != (head); \ | |
1100 | pos = n, n = pos->prev) | |
1101 | ||
1102 | /** | |
16aa9ee8 | 1103 | * cds_list_for_each_entry - iterate over list of given type |
6d0ce021 PM |
1104 | * @pos: the type * to use as a loop cursor. |
1105 | * @head: the head for your list. | |
1106 | * @member: the name of the list_struct within the struct. | |
1107 | */ | |
16aa9ee8 DG |
1108 | #define cds_list_for_each_entry(pos, head, member) \ |
1109 | for (pos = cds_list_entry((head)->next, typeof(*pos), member); \ | |
6d0ce021 | 1110 | prefetch(pos->member.next), &pos->member != (head); \ |
16aa9ee8 | 1111 | pos = cds_list_entry(pos->member.next, typeof(*pos), member)) |
6d0ce021 PM |
1112 | |
1113 | /** | |
16aa9ee8 | 1114 | * cds_list_for_each_entry_reverse - iterate backwards over list of given type. |
6d0ce021 PM |
1115 | * @pos: the type * to use as a loop cursor. |
1116 | * @head: the head for your list. | |
1117 | * @member: the name of the list_struct within the struct. | |
1118 | */ | |
16aa9ee8 DG |
1119 | #define cds_list_for_each_entry_reverse(pos, head, member) \ |
1120 | for (pos = cds_list_entry((head)->prev, typeof(*pos), member); \ | |
6d0ce021 | 1121 | prefetch(pos->member.prev), &pos->member != (head); \ |
16aa9ee8 | 1122 | pos = cds_list_entry(pos->member.prev, typeof(*pos), member)) |
6d0ce021 PM |
1123 | |
1124 | /** | |
16aa9ee8 | 1125 | * list_prepare_entry - prepare a pos entry for use in cds_list_for_each_entry_continue() |
6d0ce021 PM |
1126 | * @pos: the type * to use as a start point |
1127 | * @head: the head of the list | |
1128 | * @member: the name of the list_struct within the struct. | |
1129 | * | |
16aa9ee8 | 1130 | * Prepares a pos entry for use as a start point in cds_list_for_each_entry_continue(). |
6d0ce021 PM |
1131 | */ |
1132 | #define list_prepare_entry(pos, head, member) \ | |
16aa9ee8 | 1133 | ((pos) ? : cds_list_entry(head, typeof(*pos), member)) |
6d0ce021 PM |
1134 | |
1135 | /** | |
16aa9ee8 | 1136 | * cds_list_for_each_entry_continue - continue iteration over list of given type |
6d0ce021 PM |
1137 | * @pos: the type * to use as a loop cursor. |
1138 | * @head: the head for your list. | |
1139 | * @member: the name of the list_struct within the struct. | |
1140 | * | |
1141 | * Continue to iterate over list of given type, continuing after | |
1142 | * the current position. | |
1143 | */ | |
16aa9ee8 DG |
1144 | #define cds_list_for_each_entry_continue(pos, head, member) \ |
1145 | for (pos = cds_list_entry(pos->member.next, typeof(*pos), member); \ | |
6d0ce021 | 1146 | prefetch(pos->member.next), &pos->member != (head); \ |
16aa9ee8 | 1147 | pos = cds_list_entry(pos->member.next, typeof(*pos), member)) |
6d0ce021 PM |
1148 | |
1149 | /** | |
16aa9ee8 | 1150 | * cds_list_for_each_entry_continue_reverse - iterate backwards from the given point |
6d0ce021 PM |
1151 | * @pos: the type * to use as a loop cursor. |
1152 | * @head: the head for your list. | |
1153 | * @member: the name of the list_struct within the struct. | |
1154 | * | |
1155 | * Start to iterate over list of given type backwards, continuing after | |
1156 | * the current position. | |
1157 | */ | |
16aa9ee8 DG |
1158 | #define cds_list_for_each_entry_continue_reverse(pos, head, member) \ |
1159 | for (pos = cds_list_entry(pos->member.prev, typeof(*pos), member); \ | |
6d0ce021 | 1160 | prefetch(pos->member.prev), &pos->member != (head); \ |
16aa9ee8 | 1161 | pos = cds_list_entry(pos->member.prev, typeof(*pos), member)) |
6d0ce021 PM |
1162 | |
1163 | /** | |
16aa9ee8 | 1164 | * cds_list_for_each_entry_from - iterate over list of given type from the current point |
6d0ce021 PM |
1165 | * @pos: the type * to use as a loop cursor. |
1166 | * @head: the head for your list. | |
1167 | * @member: the name of the list_struct within the struct. | |
1168 | * | |
1169 | * Iterate over list of given type, continuing from current position. | |
1170 | */ | |
16aa9ee8 | 1171 | #define cds_list_for_each_entry_from(pos, head, member) \ |
6d0ce021 | 1172 | for (; prefetch(pos->member.next), &pos->member != (head); \ |
16aa9ee8 | 1173 | pos = cds_list_entry(pos->member.next, typeof(*pos), member)) |
6d0ce021 PM |
1174 | |
1175 | /** | |
16aa9ee8 | 1176 | * cds_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
6d0ce021 PM |
1177 | * @pos: the type * to use as a loop cursor. |
1178 | * @n: another type * to use as temporary storage | |
1179 | * @head: the head for your list. | |
1180 | * @member: the name of the list_struct within the struct. | |
1181 | */ | |
16aa9ee8 DG |
1182 | #define cds_list_for_each_entry_safe(pos, n, head, member) \ |
1183 | for (pos = cds_list_entry((head)->next, typeof(*pos), member), \ | |
1184 | n = cds_list_entry(pos->member.next, typeof(*pos), member); \ | |
6d0ce021 | 1185 | &pos->member != (head); \ |
16aa9ee8 | 1186 | pos = n, n = cds_list_entry(n->member.next, typeof(*n), member)) |
6d0ce021 PM |
1187 | |
1188 | /** | |
16aa9ee8 | 1189 | * cds_list_for_each_entry_safe_continue |
6d0ce021 PM |
1190 | * @pos: the type * to use as a loop cursor. |
1191 | * @n: another type * to use as temporary storage | |
1192 | * @head: the head for your list. | |
1193 | * @member: the name of the list_struct within the struct. | |
1194 | * | |
1195 | * Iterate over list of given type, continuing after current point, | |
1196 | * safe against removal of list entry. | |
1197 | */ | |
16aa9ee8 DG |
1198 | #define cds_list_for_each_entry_safe_continue(pos, n, head, member) \ |
1199 | for (pos = cds_list_entry(pos->member.next, typeof(*pos), member), \ | |
1200 | n = cds_list_entry(pos->member.next, typeof(*pos), member); \ | |
6d0ce021 | 1201 | &pos->member != (head); \ |
16aa9ee8 | 1202 | pos = n, n = cds_list_entry(n->member.next, typeof(*n), member)) |
6d0ce021 PM |
1203 | |
1204 | /** | |
16aa9ee8 | 1205 | * cds_list_for_each_entry_safe_from |
6d0ce021 PM |
1206 | * @pos: the type * to use as a loop cursor. |
1207 | * @n: another type * to use as temporary storage | |
1208 | * @head: the head for your list. | |
1209 | * @member: the name of the list_struct within the struct. | |
1210 | * | |
1211 | * Iterate over list of given type from current point, safe against | |
1212 | * removal of list entry. | |
1213 | */ | |
16aa9ee8 DG |
1214 | #define cds_list_for_each_entry_safe_from(pos, n, head, member) \ |
1215 | for (n = cds_list_entry(pos->member.next, typeof(*pos), member); \ | |
6d0ce021 | 1216 | &pos->member != (head); \ |
16aa9ee8 | 1217 | pos = n, n = cds_list_entry(n->member.next, typeof(*n), member)) |
6d0ce021 PM |
1218 | |
1219 | /** | |
16aa9ee8 | 1220 | * cds_list_for_each_entry_safe_reverse |
6d0ce021 PM |
1221 | * @pos: the type * to use as a loop cursor. |
1222 | * @n: another type * to use as temporary storage | |
1223 | * @head: the head for your list. | |
1224 | * @member: the name of the list_struct within the struct. | |
1225 | * | |
1226 | * Iterate backwards over list of given type, safe against removal | |
1227 | * of list entry. | |
1228 | */ | |
16aa9ee8 DG |
1229 | #define cds_list_for_each_entry_safe_reverse(pos, n, head, member) \ |
1230 | for (pos = cds_list_entry((head)->prev, typeof(*pos), member), \ | |
1231 | n = cds_list_entry(pos->member.prev, typeof(*pos), member); \ | |
6d0ce021 | 1232 | &pos->member != (head); \ |
16aa9ee8 | 1233 | pos = n, n = cds_list_entry(n->member.prev, typeof(*n), member)) |
6d0ce021 | 1234 | |
63ff4873 MD |
1235 | #endif //0 |
1236 | ||
6d0ce021 PM |
1237 | /* |
1238 | * Double linked lists with a single pointer list head. | |
1239 | * Mostly useful for hash tables where the two pointer list head is | |
1240 | * too wasteful. | |
1241 | * You lose the ability to access the tail in O(1). | |
1242 | */ | |
1243 | ||
16aa9ee8 DG |
1244 | struct cds_hlist_head { |
1245 | struct cds_hlist_node *first; | |
6d0ce021 PM |
1246 | }; |
1247 | ||
16aa9ee8 DG |
1248 | struct cds_hlist_node { |
1249 | struct cds_hlist_node *next, **pprev; | |
6d0ce021 PM |
1250 | }; |
1251 | ||
1252 | #define HLIST_HEAD_INIT { .first = NULL } | |
16aa9ee8 DG |
1253 | #define HLIST_HEAD(name) struct cds_hlist_head name = { .first = NULL } |
1254 | #define CDS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) | |
1255 | static inline void INIT_HLIST_NODE(struct cds_hlist_node *h) | |
6d0ce021 PM |
1256 | { |
1257 | h->next = NULL; | |
1258 | h->pprev = NULL; | |
1259 | } | |
1260 | ||
16aa9ee8 | 1261 | static inline int hlist_unhashed(const struct cds_hlist_node *h) |
6d0ce021 PM |
1262 | { |
1263 | return !h->pprev; | |
1264 | } | |
1265 | ||
16aa9ee8 | 1266 | static inline int hlist_empty(const struct cds_hlist_head *h) |
6d0ce021 PM |
1267 | { |
1268 | return !h->first; | |
1269 | } | |
1270 | ||
16aa9ee8 | 1271 | static inline void __cds_hlist_del(struct cds_hlist_node *n) |
6d0ce021 | 1272 | { |
16aa9ee8 DG |
1273 | struct cds_hlist_node *next = n->next; |
1274 | struct cds_hlist_node **pprev = n->pprev; | |
6d0ce021 PM |
1275 | *pprev = next; |
1276 | if (next) | |
1277 | next->pprev = pprev; | |
1278 | } | |
1279 | ||
16aa9ee8 | 1280 | static inline void cds_hlist_del(struct cds_hlist_node *n) |
6d0ce021 | 1281 | { |
16aa9ee8 | 1282 | __cds_hlist_del(n); |
6d0ce021 PM |
1283 | n->next = LIST_POISON1; |
1284 | n->pprev = LIST_POISON2; | |
1285 | } | |
1286 | ||
16aa9ee8 | 1287 | static inline void cds_hlist_del_init(struct cds_hlist_node *n) |
6d0ce021 PM |
1288 | { |
1289 | if (!hlist_unhashed(n)) { | |
16aa9ee8 | 1290 | __cds_hlist_del(n); |
6d0ce021 PM |
1291 | INIT_HLIST_NODE(n); |
1292 | } | |
1293 | } | |
1294 | ||
16aa9ee8 | 1295 | static inline void cds_hlist_add_head(struct cds_hlist_node *n, struct cds_hlist_head *h) |
6d0ce021 | 1296 | { |
16aa9ee8 | 1297 | struct cds_hlist_node *first = h->first; |
6d0ce021 PM |
1298 | n->next = first; |
1299 | if (first) | |
1300 | first->pprev = &n->next; | |
1301 | h->first = n; | |
1302 | n->pprev = &h->first; | |
1303 | } | |
1304 | ||
1305 | /* next must be != NULL */ | |
16aa9ee8 DG |
1306 | static inline void hlist_add_before(struct cds_hlist_node *n, |
1307 | struct cds_hlist_node *next) | |
6d0ce021 PM |
1308 | { |
1309 | n->pprev = next->pprev; | |
1310 | n->next = next; | |
1311 | next->pprev = &n->next; | |
1312 | *(n->pprev) = n; | |
1313 | } | |
1314 | ||
16aa9ee8 DG |
1315 | static inline void hlist_add_after(struct cds_hlist_node *n, |
1316 | struct cds_hlist_node *next) | |
6d0ce021 PM |
1317 | { |
1318 | next->next = n->next; | |
1319 | n->next = next; | |
1320 | next->pprev = &n->next; | |
1321 | ||
1322 | if(next->next) | |
1323 | next->next->pprev = &next->next; | |
1324 | } | |
1325 | ||
1326 | /* | |
1327 | * Move a list from one list head to another. Fixup the pprev | |
1328 | * reference of the first entry if it exists. | |
1329 | */ | |
16aa9ee8 DG |
1330 | static inline void hlist_move_list(struct cds_hlist_head *old, |
1331 | struct cds_hlist_head *new) | |
6d0ce021 PM |
1332 | { |
1333 | new->first = old->first; | |
1334 | if (new->first) | |
1335 | new->first->pprev = &new->first; | |
1336 | old->first = NULL; | |
1337 | } | |
1338 | ||
16aa9ee8 | 1339 | #define cds_hlist_entry(ptr, type, member) caa_container_of(ptr,type,member) |
6d0ce021 | 1340 | |
16aa9ee8 | 1341 | #define cds_hlist_for_each(pos, head) \ |
6d0ce021 PM |
1342 | for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ |
1343 | pos = pos->next) | |
1344 | ||
16aa9ee8 | 1345 | #define cds_hlist_for_each_safe(pos, n, head) \ |
6d0ce021 PM |
1346 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ |
1347 | pos = n) | |
1348 | ||
1349 | /** | |
16aa9ee8 | 1350 | * cds_hlist_for_each_entry - iterate over list of given type |
6d0ce021 | 1351 | * @tpos: the type * to use as a loop cursor. |
16aa9ee8 | 1352 | * @pos: the &struct cds_hlist_node to use as a loop cursor. |
6d0ce021 | 1353 | * @head: the head for your list. |
16aa9ee8 | 1354 | * @member: the name of the cds_hlist_node within the struct. |
6d0ce021 | 1355 | */ |
16aa9ee8 | 1356 | #define cds_hlist_for_each_entry(tpos, pos, head, member) \ |
6d0ce021 PM |
1357 | for (pos = (head)->first; \ |
1358 | pos && ({ prefetch(pos->next); 1;}) && \ | |
16aa9ee8 | 1359 | ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
6d0ce021 PM |
1360 | pos = pos->next) |
1361 | ||
1362 | /** | |
16aa9ee8 | 1363 | * cds_hlist_for_each_entry_continue - iterate over a hlist continuing after current point |
6d0ce021 | 1364 | * @tpos: the type * to use as a loop cursor. |
16aa9ee8 DG |
1365 | * @pos: the &struct cds_hlist_node to use as a loop cursor. |
1366 | * @member: the name of the cds_hlist_node within the struct. | |
6d0ce021 | 1367 | */ |
16aa9ee8 | 1368 | #define cds_hlist_for_each_entry_continue(tpos, pos, member) \ |
6d0ce021 PM |
1369 | for (pos = (pos)->next; \ |
1370 | pos && ({ prefetch(pos->next); 1;}) && \ | |
16aa9ee8 | 1371 | ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
6d0ce021 PM |
1372 | pos = pos->next) |
1373 | ||
1374 | /** | |
16aa9ee8 | 1375 | * cds_hlist_for_each_entry_from - iterate over a hlist continuing from current point |
6d0ce021 | 1376 | * @tpos: the type * to use as a loop cursor. |
16aa9ee8 DG |
1377 | * @pos: the &struct cds_hlist_node to use as a loop cursor. |
1378 | * @member: the name of the cds_hlist_node within the struct. | |
6d0ce021 | 1379 | */ |
16aa9ee8 | 1380 | #define cds_hlist_for_each_entry_from(tpos, pos, member) \ |
6d0ce021 | 1381 | for (; pos && ({ prefetch(pos->next); 1;}) && \ |
16aa9ee8 | 1382 | ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
6d0ce021 PM |
1383 | pos = pos->next) |
1384 | ||
1385 | /** | |
16aa9ee8 | 1386 | * cds_hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
6d0ce021 | 1387 | * @tpos: the type * to use as a loop cursor. |
16aa9ee8 DG |
1388 | * @pos: the &struct cds_hlist_node to use as a loop cursor. |
1389 | * @n: another &struct cds_hlist_node to use as temporary storage | |
6d0ce021 | 1390 | * @head: the head for your list. |
16aa9ee8 | 1391 | * @member: the name of the cds_hlist_node within the struct. |
6d0ce021 | 1392 | */ |
16aa9ee8 | 1393 | #define cds_hlist_for_each_entry_safe(tpos, pos, n, head, member) \ |
6d0ce021 PM |
1394 | for (pos = (head)->first; \ |
1395 | pos && ({ n = pos->next; 1; }) && \ | |
16aa9ee8 | 1396 | ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
6d0ce021 PM |
1397 | pos = n) |
1398 | ||
1399 | #endif | |
1a43bbd8 MD |
1400 | |
1401 | #endif |