define CACHE_LINE_SIZE in arch_*.h
[urcu.git] / tests / api_ppc.h
1 /* MECHANICALLY GENERATED, DO NOT EDIT!!! */
2
3 #define _INCLUDE_API_H
4
5 /*
6 * common.h: Common Linux kernel-isms.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; but version 2 of the License only due
11 * to code included from the Linux kernel.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 * Copyright (c) 2006 Paul E. McKenney, IBM.
23 *
24 * Much code taken from the Linux kernel. For such code, the option
25 * to redistribute under later versions of GPL might not be available.
26 */
27
28 #ifndef __always_inline
29 #define __always_inline inline
30 #endif
31
32 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
33 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
34
35 #ifdef __ASSEMBLY__
36 # define stringify_in_c(...) __VA_ARGS__
37 # define ASM_CONST(x) x
38 #else
39 /* This version of stringify will deal with commas... */
40 # define __stringify_in_c(...) #__VA_ARGS__
41 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
42 # define __ASM_CONST(x) x##UL
43 # define ASM_CONST(x) __ASM_CONST(x)
44 #endif
45
46
47 /*
48 * arch-ppc64.h: Expose PowerPC atomic instructions.
49 *
50 * This program is free software; you can redistribute it and/or modify
51 * it under the terms of the GNU General Public License as published by
52 * the Free Software Foundation; but version 2 of the License only due
53 * to code included from the Linux kernel.
54 *
55 * This program is distributed in the hope that it will be useful,
56 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 * GNU General Public License for more details.
59 *
60 * You should have received a copy of the GNU General Public License
61 * along with this program; if not, write to the Free Software
62 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
63 *
64 * Copyright (c) 2006 Paul E. McKenney, IBM.
65 *
66 * Much code taken from the Linux kernel. For such code, the option
67 * to redistribute under later versions of GPL might not be available.
68 */
69
70 /*
71 * Machine parameters.
72 */
73
74 #define CONFIG_PPC64
75
76 /*#define CACHE_LINE_SIZE 128 */
77 #define ____cacheline_internodealigned_in_smp \
78 __attribute__((__aligned__(1 << 7)))
79
80 #if 0 /* duplicate with arch_atomic.h */
81
82 /*
83 * Atomic data structure, initialization, and access.
84 */
85
86 typedef struct { volatile int counter; } atomic_t;
87
88 #define ATOMIC_INIT(i) { (i) }
89
90 #define atomic_read(v) ((v)->counter)
91 #define atomic_set(v, i) (((v)->counter) = (i))
92
93 /*
94 * Atomic operations.
95 */
96
97 #define LWSYNC lwsync
98 #define PPC405_ERR77(ra,rb)
99 #ifdef CONFIG_SMP
100 # define LWSYNC_ON_SMP stringify_in_c(LWSYNC) "\n"
101 # define ISYNC_ON_SMP "\n\tisync\n"
102 #else
103 # define LWSYNC_ON_SMP
104 # define ISYNC_ON_SMP
105 #endif
106
107 /*
108 * Atomic exchange
109 *
110 * Changes the memory location '*ptr' to be val and returns
111 * the previous value stored there.
112 */
113 static __always_inline unsigned long
114 __xchg_u32(volatile void *p, unsigned long val)
115 {
116 unsigned long prev;
117
118 __asm__ __volatile__(
119 LWSYNC_ON_SMP
120 "1: lwarx %0,0,%2 \n"
121 PPC405_ERR77(0,%2)
122 " stwcx. %3,0,%2 \n\
123 bne- 1b"
124 ISYNC_ON_SMP
125 : "=&r" (prev), "+m" (*(volatile unsigned int *)p)
126 : "r" (p), "r" (val)
127 : "cc", "memory");
128
129 return prev;
130 }
131
132 /*
133 * Atomic exchange
134 *
135 * Changes the memory location '*ptr' to be val and returns
136 * the previous value stored there.
137 */
138 static __always_inline unsigned long
139 __xchg_u32_local(volatile void *p, unsigned long val)
140 {
141 unsigned long prev;
142
143 __asm__ __volatile__(
144 "1: lwarx %0,0,%2 \n"
145 PPC405_ERR77(0,%2)
146 " stwcx. %3,0,%2 \n\
147 bne- 1b"
148 : "=&r" (prev), "+m" (*(volatile unsigned int *)p)
149 : "r" (p), "r" (val)
150 : "cc", "memory");
151
152 return prev;
153 }
154
155 #ifdef CONFIG_PPC64
156 static __always_inline unsigned long
157 __xchg_u64(volatile void *p, unsigned long val)
158 {
159 unsigned long prev;
160
161 __asm__ __volatile__(
162 LWSYNC_ON_SMP
163 "1: ldarx %0,0,%2 \n"
164 PPC405_ERR77(0,%2)
165 " stdcx. %3,0,%2 \n\
166 bne- 1b"
167 ISYNC_ON_SMP
168 : "=&r" (prev), "+m" (*(volatile unsigned long *)p)
169 : "r" (p), "r" (val)
170 : "cc", "memory");
171
172 return prev;
173 }
174
175 static __always_inline unsigned long
176 __xchg_u64_local(volatile void *p, unsigned long val)
177 {
178 unsigned long prev;
179
180 __asm__ __volatile__(
181 "1: ldarx %0,0,%2 \n"
182 PPC405_ERR77(0,%2)
183 " stdcx. %3,0,%2 \n\
184 bne- 1b"
185 : "=&r" (prev), "+m" (*(volatile unsigned long *)p)
186 : "r" (p), "r" (val)
187 : "cc", "memory");
188
189 return prev;
190 }
191 #endif
192
193 /*
194 * This function doesn't exist, so you'll get a linker error
195 * if something tries to do an invalid xchg().
196 */
197 extern void __xchg_called_with_bad_pointer(void);
198
199 static __always_inline unsigned long
200 __xchg(volatile void *ptr, unsigned long x, unsigned int size)
201 {
202 switch (size) {
203 case 4:
204 return __xchg_u32(ptr, x);
205 #ifdef CONFIG_PPC64
206 case 8:
207 return __xchg_u64(ptr, x);
208 #endif
209 }
210 __xchg_called_with_bad_pointer();
211 return x;
212 }
213
214 static __always_inline unsigned long
215 __xchg_local(volatile void *ptr, unsigned long x, unsigned int size)
216 {
217 switch (size) {
218 case 4:
219 return __xchg_u32_local(ptr, x);
220 #ifdef CONFIG_PPC64
221 case 8:
222 return __xchg_u64_local(ptr, x);
223 #endif
224 }
225 __xchg_called_with_bad_pointer();
226 return x;
227 }
228 #define xchg(ptr,x) \
229 ({ \
230 __typeof__(*(ptr)) _x_ = (x); \
231 (__typeof__(*(ptr))) __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
232 })
233
234 #define xchg_local(ptr,x) \
235 ({ \
236 __typeof__(*(ptr)) _x_ = (x); \
237 (__typeof__(*(ptr))) __xchg_local((ptr), \
238 (unsigned long)_x_, sizeof(*(ptr))); \
239 })
240
241 /*
242 * Compare and exchange - if *p == old, set it to new,
243 * and return the old value of *p.
244 */
245 #define __HAVE_ARCH_CMPXCHG 1
246
247 static __always_inline unsigned long
248 __cmpxchg_u32(volatile unsigned int *p, unsigned long old, unsigned long new)
249 {
250 unsigned int prev;
251
252 __asm__ __volatile__ (
253 LWSYNC_ON_SMP
254 "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
255 cmpw 0,%0,%3\n\
256 bne- 2f\n"
257 PPC405_ERR77(0,%2)
258 " stwcx. %4,0,%2\n\
259 bne- 1b"
260 ISYNC_ON_SMP
261 "\n\
262 2:"
263 : "=&r" (prev), "+m" (*p)
264 : "r" (p), "r" (old), "r" (new)
265 : "cc", "memory");
266
267 return prev;
268 }
269
270 static __always_inline unsigned long
271 __cmpxchg_u32_local(volatile unsigned int *p, unsigned long old,
272 unsigned long new)
273 {
274 unsigned int prev;
275
276 __asm__ __volatile__ (
277 "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
278 cmpw 0,%0,%3\n\
279 bne- 2f\n"
280 PPC405_ERR77(0,%2)
281 " stwcx. %4,0,%2\n\
282 bne- 1b"
283 "\n\
284 2:"
285 : "=&r" (prev), "+m" (*p)
286 : "r" (p), "r" (old), "r" (new)
287 : "cc", "memory");
288
289 return prev;
290 }
291
292 #ifdef CONFIG_PPC64
293 static __always_inline unsigned long
294 __cmpxchg_u64(volatile unsigned long *p, unsigned long old, unsigned long new)
295 {
296 unsigned long prev;
297
298 __asm__ __volatile__ (
299 LWSYNC_ON_SMP
300 "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
301 cmpd 0,%0,%3\n\
302 bne- 2f\n\
303 stdcx. %4,0,%2\n\
304 bne- 1b"
305 ISYNC_ON_SMP
306 "\n\
307 2:"
308 : "=&r" (prev), "+m" (*p)
309 : "r" (p), "r" (old), "r" (new)
310 : "cc", "memory");
311
312 return prev;
313 }
314
315 static __always_inline unsigned long
316 __cmpxchg_u64_local(volatile unsigned long *p, unsigned long old,
317 unsigned long new)
318 {
319 unsigned long prev;
320
321 __asm__ __volatile__ (
322 "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
323 cmpd 0,%0,%3\n\
324 bne- 2f\n\
325 stdcx. %4,0,%2\n\
326 bne- 1b"
327 "\n\
328 2:"
329 : "=&r" (prev), "+m" (*p)
330 : "r" (p), "r" (old), "r" (new)
331 : "cc", "memory");
332
333 return prev;
334 }
335 #endif
336
337 /* This function doesn't exist, so you'll get a linker error
338 if something tries to do an invalid cmpxchg(). */
339 extern void __cmpxchg_called_with_bad_pointer(void);
340
341 static __always_inline unsigned long
342 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new,
343 unsigned int size)
344 {
345 switch (size) {
346 case 4:
347 return __cmpxchg_u32(ptr, old, new);
348 #ifdef CONFIG_PPC64
349 case 8:
350 return __cmpxchg_u64(ptr, old, new);
351 #endif
352 }
353 __cmpxchg_called_with_bad_pointer();
354 return old;
355 }
356
357 static __always_inline unsigned long
358 __cmpxchg_local(volatile void *ptr, unsigned long old, unsigned long new,
359 unsigned int size)
360 {
361 switch (size) {
362 case 4:
363 return __cmpxchg_u32_local(ptr, old, new);
364 #ifdef CONFIG_PPC64
365 case 8:
366 return __cmpxchg_u64_local(ptr, old, new);
367 #endif
368 }
369 __cmpxchg_called_with_bad_pointer();
370 return old;
371 }
372
373 #define cmpxchg(ptr, o, n) \
374 ({ \
375 __typeof__(*(ptr)) _o_ = (o); \
376 __typeof__(*(ptr)) _n_ = (n); \
377 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
378 (unsigned long)_n_, sizeof(*(ptr))); \
379 })
380
381
382 #define cmpxchg_local(ptr, o, n) \
383 ({ \
384 __typeof__(*(ptr)) _o_ = (o); \
385 __typeof__(*(ptr)) _n_ = (n); \
386 (__typeof__(*(ptr))) __cmpxchg_local((ptr), (unsigned long)_o_, \
387 (unsigned long)_n_, sizeof(*(ptr))); \
388 })
389
390 #ifdef CONFIG_PPC64
391 /*
392 * We handle most unaligned accesses in hardware. On the other hand
393 * unaligned DMA can be very expensive on some ppc64 IO chips (it does
394 * powers of 2 writes until it reaches sufficient alignment).
395 *
396 * Based on this we disable the IP header alignment in network drivers.
397 * We also modify NET_SKB_PAD to be a cacheline in size, thus maintaining
398 * cacheline alignment of buffers.
399 */
400 #define NET_IP_ALIGN 0
401 #define NET_SKB_PAD L1_CACHE_BYTES
402
403 #define cmpxchg64(ptr, o, n) \
404 ({ \
405 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
406 cmpxchg((ptr), (o), (n)); \
407 })
408 #define cmpxchg64_local(ptr, o, n) \
409 ({ \
410 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
411 cmpxchg_local((ptr), (o), (n)); \
412 })
413 #endif
414
415 #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
416 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
417
418 /**
419 * atomic_add - add integer to atomic variable
420 * @i: integer value to add
421 * @v: pointer of type atomic_t
422 *
423 * Atomically adds @a to @v.
424 */
425 static __inline__ void atomic_add(int a, atomic_t *v)
426 {
427 int t;
428
429 __asm__ __volatile__(
430 "1: lwarx %0,0,%3 # atomic_add\n\
431 add %0,%2,%0 \n\
432 stwcx. %0,0,%3 \n\
433 bne- 1b"
434 : "=&r" (t), "+m" (v->counter)
435 : "r" (a), "r" (&v->counter)
436 : "cc");
437 }
438
439 /**
440 * atomic_sub - subtract the atomic variable
441 * @i: integer value to subtract
442 * @v: pointer of type atomic_t
443 *
444 * Atomically subtracts @a from @v.
445 */
446 static __inline__ void atomic_sub(int a, atomic_t *v)
447 {
448 int t;
449
450 __asm__ __volatile__(
451 "1: lwarx %0,0,%3 # atomic_sub \n\
452 subf %0,%2,%0 \n\
453 stwcx. %0,0,%3 \n\
454 bne- 1b"
455 : "=&r" (t), "+m" (v->counter)
456 : "r" (a), "r" (&v->counter)
457 : "cc");
458 }
459
460 static __inline__ atomic_sub_return(int a, atomic_t *v)
461 {
462 int t;
463
464 __asm__ __volatile__(
465 "lwsync\n\
466 1: lwarx %0,0,%2 # atomic_sub_return\n\
467 subf %0,%1,%0\n\
468 stwcx. %0,0,%2 \n\
469 bne- 1b \n\
470 isync"
471 : "=&r" (t)
472 : "r" (a), "r" (&v->counter)
473 : "cc", "memory");
474
475 return t;
476 }
477
478 /**
479 * atomic_sub_and_test - subtract value from variable and test result
480 * @i: integer value to subtract
481 * @v: pointer of type atomic_t
482 *
483 * Atomically subtracts @i from @v and returns
484 * true if the result is zero, or false for all
485 * other cases.
486 */
487 static __inline__ int atomic_sub_and_test(int a, atomic_t *v)
488 {
489 return atomic_sub_return(a, v) == 0;
490 }
491
492 /**
493 * atomic_inc - increment atomic variable
494 * @v: pointer of type atomic_t
495 *
496 * Atomically increments @v by 1.
497 */
498 static __inline__ void atomic_inc(atomic_t *v)
499 {
500 atomic_add(1, v);
501 }
502
503 /**
504 * atomic_dec - decrement atomic variable
505 * @v: pointer of type atomic_t
506 *
507 * Atomically decrements @v by 1.
508 */
509 static __inline__ void atomic_dec(atomic_t *v)
510 {
511 atomic_sub(1, v);
512 }
513
514 /**
515 * atomic_dec_and_test - decrement and test
516 * @v: pointer of type atomic_t
517 *
518 * Atomically decrements @v by 1 and
519 * returns true if the result is 0, or false for all other
520 * cases.
521 */
522 static __inline__ int atomic_dec_and_test(atomic_t *v)
523 {
524 return atomic_sub_and_test(1, v);
525 }
526
527 /**
528 * atomic_inc_and_test - increment and test
529 * @v: pointer of type atomic_t
530 *
531 * Atomically increments @v by 1
532 * and returns true if the result is zero, or false for all
533 * other cases.
534 */
535 static __inline__ int atomic_inc_and_test(atomic_t *v)
536 {
537 return atomic_inc_return(v);
538 }
539
540 /**
541 * atomic_add_return - add and return
542 * @v: pointer of type atomic_t
543 * @i: integer value to add
544 *
545 * Atomically adds @i to @v and returns @i + @v
546 */
547 static __inline__ int atomic_add_return(int a, atomic_t *v)
548 {
549 int t;
550
551 __asm__ __volatile__(
552 "lwsync \n\
553 1: lwarx %0,0,%2 # atomic_add_return \n\
554 add %0,%1,%0 \n\
555 stwcx. %0,0,%2 \n\
556 bne- 1b \n\
557 isync"
558 : "=&r" (t)
559 : "r" (a), "r" (&v->counter)
560 : "cc", "memory");
561
562 return t;
563 }
564
565 /**
566 * atomic_add_negative - add and test if negative
567 * @v: pointer of type atomic_t
568 * @i: integer value to add
569 *
570 * Atomically adds @i to @v and returns true
571 * if the result is negative, or false when
572 * result is greater than or equal to zero.
573 */
574 static __inline__ int atomic_add_negative(int a, atomic_t *v)
575 {
576 return atomic_add_return(a, v) < 0;
577 }
578
579 /**
580 * atomic_add_unless - add unless the number is a given value
581 * @v: pointer of type atomic_t
582 * @a: the amount to add to v...
583 * @u: ...unless v is equal to u.
584 *
585 * Atomically adds @a to @v, so long as it was not @u.
586 * Returns non-zero if @v was not @u, and zero otherwise.
587 */
588 static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
589 {
590 int t;
591
592 __asm__ __volatile__(
593 "lwsync \n\
594 1: lwarx %0,0,%1 # atomic_add_unless\n\
595 cmpd 0,%0,%3 \n\
596 beq- 2f \n\
597 add %0,%2,%0 \n\
598 stwcx. %0,0,%1 \n\
599 bne- 1b \n\
600 isync \n\
601 subf %0,%2,%0 \n\
602 2:"
603 : "=&r" (t)
604 : "r" (&v->counter), "r" (a), "r" (u)
605 : "cc", "memory");
606
607 return t != u;
608 }
609
610 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
611
612 #define atomic_inc_return(v) (atomic_add_return(1,v))
613 #define atomic_dec_return(v) (atomic_sub_return(1,v))
614
615 /* Atomic operations are already serializing on x86 */
616 #define smp_mb__before_atomic_dec() smp_mb()
617 #define smp_mb__after_atomic_dec() smp_mb()
618 #define smp_mb__before_atomic_inc() smp_mb()
619 #define smp_mb__after_atomic_inc() smp_mb()
620
621 #endif //0 /* duplicate with arch_atomic.h */
622
623 /*
624 * api_pthreads.h: API mapping to pthreads environment.
625 *
626 * This program is free software; you can redistribute it and/or modify
627 * it under the terms of the GNU General Public License as published by
628 * the Free Software Foundation; either version 2 of the License, or
629 * (at your option) any later version. However, please note that much
630 * of the code in this file derives from the Linux kernel, and that such
631 * code may not be available except under GPLv2.
632 *
633 * This program is distributed in the hope that it will be useful,
634 * but WITHOUT ANY WARRANTY; without even the implied warranty of
635 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
636 * GNU General Public License for more details.
637 *
638 * You should have received a copy of the GNU General Public License
639 * along with this program; if not, write to the Free Software
640 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
641 *
642 * Copyright (c) 2006 Paul E. McKenney, IBM.
643 */
644
645 #include <stdio.h>
646 #include <stdlib.h>
647 #include <errno.h>
648 #include <limits.h>
649 #include <sys/types.h>
650 #define __USE_GNU
651 #include <pthread.h>
652 #include <sched.h>
653 #include <sys/param.h>
654 /* #include "atomic.h" */
655
656 /*
657 * Compiler magic.
658 */
659 #define container_of(ptr, type, member) ({ \
660 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
661 (type *)( (char *)__mptr - offsetof(type,member) );})
662
663 /*
664 * Default machine parameters.
665 */
666
667 #ifndef CACHE_LINE_SIZE
668 /* #define CACHE_LINE_SIZE 128 */
669 #endif /* #ifndef CACHE_LINE_SIZE */
670
671 /*
672 * Exclusive locking primitives.
673 */
674
675 typedef pthread_mutex_t spinlock_t;
676
677 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
678 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
679
680 static void spin_lock_init(spinlock_t *sp)
681 {
682 if (pthread_mutex_init(sp, NULL) != 0) {
683 perror("spin_lock_init:pthread_mutex_init");
684 exit(-1);
685 }
686 }
687
688 static void spin_lock(spinlock_t *sp)
689 {
690 if (pthread_mutex_lock(sp) != 0) {
691 perror("spin_lock:pthread_mutex_lock");
692 exit(-1);
693 }
694 }
695
696 static void spin_unlock(spinlock_t *sp)
697 {
698 if (pthread_mutex_unlock(sp) != 0) {
699 perror("spin_unlock:pthread_mutex_unlock");
700 exit(-1);
701 }
702 }
703
704 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
705 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
706
707 /*
708 * Thread creation/destruction primitives.
709 */
710
711 typedef pthread_t thread_id_t;
712
713 #define NR_THREADS 128
714
715 #define __THREAD_ID_MAP_EMPTY 0
716 #define __THREAD_ID_MAP_WAITING 1
717 thread_id_t __thread_id_map[NR_THREADS];
718 spinlock_t __thread_id_map_mutex;
719
720 #define for_each_thread(t) \
721 for (t = 0; t < NR_THREADS; t++)
722
723 #define for_each_running_thread(t) \
724 for (t = 0; t < NR_THREADS; t++) \
725 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
726 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
727
728 #define for_each_tid(t, tid) \
729 for (t = 0; t < NR_THREADS; t++) \
730 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
731 ((tid) != __THREAD_ID_MAP_WAITING))
732
733 pthread_key_t thread_id_key;
734
735 static int __smp_thread_id(void)
736 {
737 int i;
738 thread_id_t tid = pthread_self();
739
740 for (i = 0; i < NR_THREADS; i++) {
741 if (__thread_id_map[i] == tid) {
742 long v = i + 1; /* must be non-NULL. */
743
744 if (pthread_setspecific(thread_id_key, (void *)v) != 0) {
745 perror("pthread_setspecific");
746 exit(-1);
747 }
748 return i;
749 }
750 }
751 spin_lock(&__thread_id_map_mutex);
752 for (i = 0; i < NR_THREADS; i++) {
753 if (__thread_id_map[i] == tid)
754 spin_unlock(&__thread_id_map_mutex);
755 return i;
756 }
757 spin_unlock(&__thread_id_map_mutex);
758 fprintf(stderr, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
759 (int)tid, (int)tid);
760 exit(-1);
761 }
762
763 static int smp_thread_id(void)
764 {
765 void *id;
766
767 id = pthread_getspecific(thread_id_key);
768 if (id == NULL)
769 return __smp_thread_id();
770 return (long)(id - 1);
771 }
772
773 static thread_id_t create_thread(void *(*func)(void *), void *arg)
774 {
775 thread_id_t tid;
776 int i;
777
778 spin_lock(&__thread_id_map_mutex);
779 for (i = 0; i < NR_THREADS; i++) {
780 if (__thread_id_map[i] == __THREAD_ID_MAP_EMPTY)
781 break;
782 }
783 if (i >= NR_THREADS) {
784 spin_unlock(&__thread_id_map_mutex);
785 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
786 exit(-1);
787 }
788 __thread_id_map[i] = __THREAD_ID_MAP_WAITING;
789 spin_unlock(&__thread_id_map_mutex);
790 if (pthread_create(&tid, NULL, func, arg) != 0) {
791 perror("create_thread:pthread_create");
792 exit(-1);
793 }
794 __thread_id_map[i] = tid;
795 return tid;
796 }
797
798 static void *wait_thread(thread_id_t tid)
799 {
800 int i;
801 void *vp;
802
803 for (i = 0; i < NR_THREADS; i++) {
804 if (__thread_id_map[i] == tid)
805 break;
806 }
807 if (i >= NR_THREADS){
808 fprintf(stderr, "wait_thread: bad tid = %d(%#x)\n",
809 (int)tid, (int)tid);
810 exit(-1);
811 }
812 if (pthread_join(tid, &vp) != 0) {
813 perror("wait_thread:pthread_join");
814 exit(-1);
815 }
816 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
817 return vp;
818 }
819
820 static void wait_all_threads(void)
821 {
822 int i;
823 thread_id_t tid;
824
825 for (i = 1; i < NR_THREADS; i++) {
826 tid = __thread_id_map[i];
827 if (tid != __THREAD_ID_MAP_EMPTY &&
828 tid != __THREAD_ID_MAP_WAITING)
829 (void)wait_thread(tid);
830 }
831 }
832
833 static void run_on(int cpu)
834 {
835 cpu_set_t mask;
836
837 CPU_ZERO(&mask);
838 CPU_SET(cpu, &mask);
839 sched_setaffinity(0, sizeof(mask), &mask);
840 }
841
842 /*
843 * timekeeping -- very crude -- should use MONOTONIC...
844 */
845
846 long long get_microseconds(void)
847 {
848 struct timeval tv;
849
850 if (gettimeofday(&tv, NULL) != 0)
851 abort();
852 return ((long long)tv.tv_sec) * 1000000LL + (long long)tv.tv_usec;
853 }
854
855 /*
856 * Per-thread variables.
857 */
858
859 #define DEFINE_PER_THREAD(type, name) \
860 struct { \
861 __typeof__(type) v \
862 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
863 } __per_thread_##name[NR_THREADS];
864 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
865
866 #define per_thread(name, thread) __per_thread_##name[thread].v
867 #define __get_thread_var(name) per_thread(name, smp_thread_id())
868
869 #define init_per_thread(name, v) \
870 do { \
871 int __i_p_t_i; \
872 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
873 per_thread(name, __i_p_t_i) = v; \
874 } while (0)
875
876 /*
877 * CPU traversal primitives.
878 */
879
880 #ifndef NR_CPUS
881 #define NR_CPUS 16
882 #endif /* #ifndef NR_CPUS */
883
884 #define for_each_possible_cpu(cpu) \
885 for (cpu = 0; cpu < NR_CPUS; cpu++)
886 #define for_each_online_cpu(cpu) \
887 for (cpu = 0; cpu < NR_CPUS; cpu++)
888
889 /*
890 * Per-CPU variables.
891 */
892
893 #define DEFINE_PER_CPU(type, name) \
894 struct { \
895 __typeof__(type) v \
896 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
897 } __per_cpu_##name[NR_CPUS]
898 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
899
900 DEFINE_PER_THREAD(int, smp_processor_id);
901
902 #define per_cpu(name, thread) __per_cpu_##name[thread].v
903 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
904
905 #define init_per_cpu(name, v) \
906 do { \
907 int __i_p_c_i; \
908 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
909 per_cpu(name, __i_p_c_i) = v; \
910 } while (0)
911
912 /*
913 * CPU state checking (crowbarred).
914 */
915
916 #define idle_cpu(cpu) 0
917 #define in_softirq() 1
918 #define hardirq_count() 0
919 #define PREEMPT_SHIFT 0
920 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
921 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
922 #define PREEMPT_BITS 8
923 #define SOFTIRQ_BITS 8
924
925 /*
926 * CPU hotplug.
927 */
928
929 struct notifier_block {
930 int (*notifier_call)(struct notifier_block *, unsigned long, void *);
931 struct notifier_block *next;
932 int priority;
933 };
934
935 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
936 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
937 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
938 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
939 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
940 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
941 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
942 * not handling interrupts, soon dead */
943 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
944 * lock is dropped */
945
946 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
947 * operation in progress
948 */
949 #define CPU_TASKS_FROZEN 0x0010
950
951 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
952 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
953 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
954 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
955 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
956 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
957 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
958
959 /* Hibernation and suspend events */
960 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
961 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
962 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
963 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
964 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
965 #define PM_POST_RESTORE 0x0006 /* Restore failed */
966
967 #define NOTIFY_DONE 0x0000 /* Don't care */
968 #define NOTIFY_OK 0x0001 /* Suits me */
969 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
970 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
971 /* Bad/Veto action */
972 /*
973 * Clean way to return from the notifier and stop further calls.
974 */
975 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
976
977 /*
978 * Bug checks.
979 */
980
981 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
982
983 /*
984 * Initialization -- Must be called before calling any primitives.
985 */
986
987 static void smp_init(void)
988 {
989 int i;
990
991 spin_lock_init(&__thread_id_map_mutex);
992 __thread_id_map[0] = pthread_self();
993 for (i = 1; i < NR_THREADS; i++)
994 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
995 init_per_thread(smp_processor_id, 0);
996 if (pthread_key_create(&thread_id_key, NULL) != 0) {
997 perror("pthread_key_create");
998 exit(-1);
999 }
1000 }
1001
1002 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
1003
1004 #ifndef _LINUX_LIST_H
1005 #define _LINUX_LIST_H
1006
1007 #define LIST_POISON1 ((void *) 0x00100100)
1008 #define LIST_POISON2 ((void *) 0x00200200)
1009
1010 #define container_of(ptr, type, member) ({ \
1011 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
1012 (type *)( (char *)__mptr - offsetof(type,member) );})
1013
1014 #if 0
1015
1016 /*
1017 * Simple doubly linked list implementation.
1018 *
1019 * Some of the internal functions ("__xxx") are useful when
1020 * manipulating whole lists rather than single entries, as
1021 * sometimes we already know the next/prev entries and we can
1022 * generate better code by using them directly rather than
1023 * using the generic single-entry routines.
1024 */
1025
1026 struct list_head {
1027 struct list_head *next, *prev;
1028 };
1029
1030 #define LIST_HEAD_INIT(name) { &(name), &(name) }
1031
1032 #define LIST_HEAD(name) \
1033 struct list_head name = LIST_HEAD_INIT(name)
1034
1035 static inline void INIT_LIST_HEAD(struct list_head *list)
1036 {
1037 list->next = list;
1038 list->prev = list;
1039 }
1040
1041 /*
1042 * Insert a new entry between two known consecutive entries.
1043 *
1044 * This is only for internal list manipulation where we know
1045 * the prev/next entries already!
1046 */
1047 #ifndef CONFIG_DEBUG_LIST
1048 static inline void __list_add(struct list_head *new,
1049 struct list_head *prev,
1050 struct list_head *next)
1051 {
1052 next->prev = new;
1053 new->next = next;
1054 new->prev = prev;
1055 prev->next = new;
1056 }
1057 #else
1058 extern void __list_add(struct list_head *new,
1059 struct list_head *prev,
1060 struct list_head *next);
1061 #endif
1062
1063 /**
1064 * list_add - add a new entry
1065 * @new: new entry to be added
1066 * @head: list head to add it after
1067 *
1068 * Insert a new entry after the specified head.
1069 * This is good for implementing stacks.
1070 */
1071 static inline void list_add(struct list_head *new, struct list_head *head)
1072 {
1073 __list_add(new, head, head->next);
1074 }
1075
1076
1077 /**
1078 * list_add_tail - add a new entry
1079 * @new: new entry to be added
1080 * @head: list head to add it before
1081 *
1082 * Insert a new entry before the specified head.
1083 * This is useful for implementing queues.
1084 */
1085 static inline void list_add_tail(struct list_head *new, struct list_head *head)
1086 {
1087 __list_add(new, head->prev, head);
1088 }
1089
1090 /*
1091 * Delete a list entry by making the prev/next entries
1092 * point to each other.
1093 *
1094 * This is only for internal list manipulation where we know
1095 * the prev/next entries already!
1096 */
1097 static inline void __list_del(struct list_head * prev, struct list_head * next)
1098 {
1099 next->prev = prev;
1100 prev->next = next;
1101 }
1102
1103 /**
1104 * list_del - deletes entry from list.
1105 * @entry: the element to delete from the list.
1106 * Note: list_empty() on entry does not return true after this, the entry is
1107 * in an undefined state.
1108 */
1109 #ifndef CONFIG_DEBUG_LIST
1110 static inline void list_del(struct list_head *entry)
1111 {
1112 __list_del(entry->prev, entry->next);
1113 entry->next = LIST_POISON1;
1114 entry->prev = LIST_POISON2;
1115 }
1116 #else
1117 extern void list_del(struct list_head *entry);
1118 #endif
1119
1120 /**
1121 * list_replace - replace old entry by new one
1122 * @old : the element to be replaced
1123 * @new : the new element to insert
1124 *
1125 * If @old was empty, it will be overwritten.
1126 */
1127 static inline void list_replace(struct list_head *old,
1128 struct list_head *new)
1129 {
1130 new->next = old->next;
1131 new->next->prev = new;
1132 new->prev = old->prev;
1133 new->prev->next = new;
1134 }
1135
1136 static inline void list_replace_init(struct list_head *old,
1137 struct list_head *new)
1138 {
1139 list_replace(old, new);
1140 INIT_LIST_HEAD(old);
1141 }
1142
1143 /**
1144 * list_del_init - deletes entry from list and reinitialize it.
1145 * @entry: the element to delete from the list.
1146 */
1147 static inline void list_del_init(struct list_head *entry)
1148 {
1149 __list_del(entry->prev, entry->next);
1150 INIT_LIST_HEAD(entry);
1151 }
1152
1153 /**
1154 * list_move - delete from one list and add as another's head
1155 * @list: the entry to move
1156 * @head: the head that will precede our entry
1157 */
1158 static inline void list_move(struct list_head *list, struct list_head *head)
1159 {
1160 __list_del(list->prev, list->next);
1161 list_add(list, head);
1162 }
1163
1164 /**
1165 * list_move_tail - delete from one list and add as another's tail
1166 * @list: the entry to move
1167 * @head: the head that will follow our entry
1168 */
1169 static inline void list_move_tail(struct list_head *list,
1170 struct list_head *head)
1171 {
1172 __list_del(list->prev, list->next);
1173 list_add_tail(list, head);
1174 }
1175
1176 /**
1177 * list_is_last - tests whether @list is the last entry in list @head
1178 * @list: the entry to test
1179 * @head: the head of the list
1180 */
1181 static inline int list_is_last(const struct list_head *list,
1182 const struct list_head *head)
1183 {
1184 return list->next == head;
1185 }
1186
1187 /**
1188 * list_empty - tests whether a list is empty
1189 * @head: the list to test.
1190 */
1191 static inline int list_empty(const struct list_head *head)
1192 {
1193 return head->next == head;
1194 }
1195
1196 /**
1197 * list_empty_careful - tests whether a list is empty and not being modified
1198 * @head: the list to test
1199 *
1200 * Description:
1201 * tests whether a list is empty _and_ checks that no other CPU might be
1202 * in the process of modifying either member (next or prev)
1203 *
1204 * NOTE: using list_empty_careful() without synchronization
1205 * can only be safe if the only activity that can happen
1206 * to the list entry is list_del_init(). Eg. it cannot be used
1207 * if another CPU could re-list_add() it.
1208 */
1209 static inline int list_empty_careful(const struct list_head *head)
1210 {
1211 struct list_head *next = head->next;
1212 return (next == head) && (next == head->prev);
1213 }
1214
1215 /**
1216 * list_is_singular - tests whether a list has just one entry.
1217 * @head: the list to test.
1218 */
1219 static inline int list_is_singular(const struct list_head *head)
1220 {
1221 return !list_empty(head) && (head->next == head->prev);
1222 }
1223
1224 static inline void __list_cut_position(struct list_head *list,
1225 struct list_head *head, struct list_head *entry)
1226 {
1227 struct list_head *new_first = entry->next;
1228 list->next = head->next;
1229 list->next->prev = list;
1230 list->prev = entry;
1231 entry->next = list;
1232 head->next = new_first;
1233 new_first->prev = head;
1234 }
1235
1236 /**
1237 * list_cut_position - cut a list into two
1238 * @list: a new list to add all removed entries
1239 * @head: a list with entries
1240 * @entry: an entry within head, could be the head itself
1241 * and if so we won't cut the list
1242 *
1243 * This helper moves the initial part of @head, up to and
1244 * including @entry, from @head to @list. You should
1245 * pass on @entry an element you know is on @head. @list
1246 * should be an empty list or a list you do not care about
1247 * losing its data.
1248 *
1249 */
1250 static inline void list_cut_position(struct list_head *list,
1251 struct list_head *head, struct list_head *entry)
1252 {
1253 if (list_empty(head))
1254 return;
1255 if (list_is_singular(head) &&
1256 (head->next != entry && head != entry))
1257 return;
1258 if (entry == head)
1259 INIT_LIST_HEAD(list);
1260 else
1261 __list_cut_position(list, head, entry);
1262 }
1263
1264 static inline void __list_splice(const struct list_head *list,
1265 struct list_head *prev,
1266 struct list_head *next)
1267 {
1268 struct list_head *first = list->next;
1269 struct list_head *last = list->prev;
1270
1271 first->prev = prev;
1272 prev->next = first;
1273
1274 last->next = next;
1275 next->prev = last;
1276 }
1277
1278 /**
1279 * list_splice - join two lists, this is designed for stacks
1280 * @list: the new list to add.
1281 * @head: the place to add it in the first list.
1282 */
1283 static inline void list_splice(const struct list_head *list,
1284 struct list_head *head)
1285 {
1286 if (!list_empty(list))
1287 __list_splice(list, head, head->next);
1288 }
1289
1290 /**
1291 * list_splice_tail - join two lists, each list being a queue
1292 * @list: the new list to add.
1293 * @head: the place to add it in the first list.
1294 */
1295 static inline void list_splice_tail(struct list_head *list,
1296 struct list_head *head)
1297 {
1298 if (!list_empty(list))
1299 __list_splice(list, head->prev, head);
1300 }
1301
1302 /**
1303 * list_splice_init - join two lists and reinitialise the emptied list.
1304 * @list: the new list to add.
1305 * @head: the place to add it in the first list.
1306 *
1307 * The list at @list is reinitialised
1308 */
1309 static inline void list_splice_init(struct list_head *list,
1310 struct list_head *head)
1311 {
1312 if (!list_empty(list)) {
1313 __list_splice(list, head, head->next);
1314 INIT_LIST_HEAD(list);
1315 }
1316 }
1317
1318 /**
1319 * list_splice_tail_init - join two lists and reinitialise the emptied list
1320 * @list: the new list to add.
1321 * @head: the place to add it in the first list.
1322 *
1323 * Each of the lists is a queue.
1324 * The list at @list is reinitialised
1325 */
1326 static inline void list_splice_tail_init(struct list_head *list,
1327 struct list_head *head)
1328 {
1329 if (!list_empty(list)) {
1330 __list_splice(list, head->prev, head);
1331 INIT_LIST_HEAD(list);
1332 }
1333 }
1334
1335 /**
1336 * list_entry - get the struct for this entry
1337 * @ptr: the &struct list_head pointer.
1338 * @type: the type of the struct this is embedded in.
1339 * @member: the name of the list_struct within the struct.
1340 */
1341 #define list_entry(ptr, type, member) \
1342 container_of(ptr, type, member)
1343
1344 /**
1345 * list_first_entry - get the first element from a list
1346 * @ptr: the list head to take the element from.
1347 * @type: the type of the struct this is embedded in.
1348 * @member: the name of the list_struct within the struct.
1349 *
1350 * Note, that list is expected to be not empty.
1351 */
1352 #define list_first_entry(ptr, type, member) \
1353 list_entry((ptr)->next, type, member)
1354
1355 /**
1356 * list_for_each - iterate over a list
1357 * @pos: the &struct list_head to use as a loop cursor.
1358 * @head: the head for your list.
1359 */
1360 #define list_for_each(pos, head) \
1361 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
1362 pos = pos->next)
1363
1364 /**
1365 * __list_for_each - iterate over a list
1366 * @pos: the &struct list_head to use as a loop cursor.
1367 * @head: the head for your list.
1368 *
1369 * This variant differs from list_for_each() in that it's the
1370 * simplest possible list iteration code, no prefetching is done.
1371 * Use this for code that knows the list to be very short (empty
1372 * or 1 entry) most of the time.
1373 */
1374 #define __list_for_each(pos, head) \
1375 for (pos = (head)->next; pos != (head); pos = pos->next)
1376
1377 /**
1378 * list_for_each_prev - iterate over a list backwards
1379 * @pos: the &struct list_head to use as a loop cursor.
1380 * @head: the head for your list.
1381 */
1382 #define list_for_each_prev(pos, head) \
1383 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1384 pos = pos->prev)
1385
1386 /**
1387 * list_for_each_safe - iterate over a list safe against removal of list entry
1388 * @pos: the &struct list_head to use as a loop cursor.
1389 * @n: another &struct list_head to use as temporary storage
1390 * @head: the head for your list.
1391 */
1392 #define list_for_each_safe(pos, n, head) \
1393 for (pos = (head)->next, n = pos->next; pos != (head); \
1394 pos = n, n = pos->next)
1395
1396 /**
1397 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1398 * @pos: the &struct list_head to use as a loop cursor.
1399 * @n: another &struct list_head to use as temporary storage
1400 * @head: the head for your list.
1401 */
1402 #define list_for_each_prev_safe(pos, n, head) \
1403 for (pos = (head)->prev, n = pos->prev; \
1404 prefetch(pos->prev), pos != (head); \
1405 pos = n, n = pos->prev)
1406
1407 /**
1408 * list_for_each_entry - iterate over list of given type
1409 * @pos: the type * to use as a loop cursor.
1410 * @head: the head for your list.
1411 * @member: the name of the list_struct within the struct.
1412 */
1413 #define list_for_each_entry(pos, head, member) \
1414 for (pos = list_entry((head)->next, typeof(*pos), member); \
1415 prefetch(pos->member.next), &pos->member != (head); \
1416 pos = list_entry(pos->member.next, typeof(*pos), member))
1417
1418 /**
1419 * list_for_each_entry_reverse - iterate backwards over list of given type.
1420 * @pos: the type * to use as a loop cursor.
1421 * @head: the head for your list.
1422 * @member: the name of the list_struct within the struct.
1423 */
1424 #define list_for_each_entry_reverse(pos, head, member) \
1425 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1426 prefetch(pos->member.prev), &pos->member != (head); \
1427 pos = list_entry(pos->member.prev, typeof(*pos), member))
1428
1429 /**
1430 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1431 * @pos: the type * to use as a start point
1432 * @head: the head of the list
1433 * @member: the name of the list_struct within the struct.
1434 *
1435 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1436 */
1437 #define list_prepare_entry(pos, head, member) \
1438 ((pos) ? : list_entry(head, typeof(*pos), member))
1439
1440 /**
1441 * list_for_each_entry_continue - continue iteration over list of given type
1442 * @pos: the type * to use as a loop cursor.
1443 * @head: the head for your list.
1444 * @member: the name of the list_struct within the struct.
1445 *
1446 * Continue to iterate over list of given type, continuing after
1447 * the current position.
1448 */
1449 #define list_for_each_entry_continue(pos, head, member) \
1450 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1451 prefetch(pos->member.next), &pos->member != (head); \
1452 pos = list_entry(pos->member.next, typeof(*pos), member))
1453
1454 /**
1455 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1456 * @pos: the type * to use as a loop cursor.
1457 * @head: the head for your list.
1458 * @member: the name of the list_struct within the struct.
1459 *
1460 * Start to iterate over list of given type backwards, continuing after
1461 * the current position.
1462 */
1463 #define list_for_each_entry_continue_reverse(pos, head, member) \
1464 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1465 prefetch(pos->member.prev), &pos->member != (head); \
1466 pos = list_entry(pos->member.prev, typeof(*pos), member))
1467
1468 /**
1469 * list_for_each_entry_from - iterate over list of given type from the current point
1470 * @pos: the type * to use as a loop cursor.
1471 * @head: the head for your list.
1472 * @member: the name of the list_struct within the struct.
1473 *
1474 * Iterate over list of given type, continuing from current position.
1475 */
1476 #define list_for_each_entry_from(pos, head, member) \
1477 for (; prefetch(pos->member.next), &pos->member != (head); \
1478 pos = list_entry(pos->member.next, typeof(*pos), member))
1479
1480 /**
1481 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1482 * @pos: the type * to use as a loop cursor.
1483 * @n: another type * to use as temporary storage
1484 * @head: the head for your list.
1485 * @member: the name of the list_struct within the struct.
1486 */
1487 #define list_for_each_entry_safe(pos, n, head, member) \
1488 for (pos = list_entry((head)->next, typeof(*pos), member), \
1489 n = list_entry(pos->member.next, typeof(*pos), member); \
1490 &pos->member != (head); \
1491 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1492
1493 /**
1494 * list_for_each_entry_safe_continue
1495 * @pos: the type * to use as a loop cursor.
1496 * @n: another type * to use as temporary storage
1497 * @head: the head for your list.
1498 * @member: the name of the list_struct within the struct.
1499 *
1500 * Iterate over list of given type, continuing after current point,
1501 * safe against removal of list entry.
1502 */
1503 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1504 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1505 n = list_entry(pos->member.next, typeof(*pos), member); \
1506 &pos->member != (head); \
1507 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1508
1509 /**
1510 * list_for_each_entry_safe_from
1511 * @pos: the type * to use as a loop cursor.
1512 * @n: another type * to use as temporary storage
1513 * @head: the head for your list.
1514 * @member: the name of the list_struct within the struct.
1515 *
1516 * Iterate over list of given type from current point, safe against
1517 * removal of list entry.
1518 */
1519 #define list_for_each_entry_safe_from(pos, n, head, member) \
1520 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1521 &pos->member != (head); \
1522 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1523
1524 /**
1525 * list_for_each_entry_safe_reverse
1526 * @pos: the type * to use as a loop cursor.
1527 * @n: another type * to use as temporary storage
1528 * @head: the head for your list.
1529 * @member: the name of the list_struct within the struct.
1530 *
1531 * Iterate backwards over list of given type, safe against removal
1532 * of list entry.
1533 */
1534 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1535 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1536 n = list_entry(pos->member.prev, typeof(*pos), member); \
1537 &pos->member != (head); \
1538 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1539
1540 #endif //0
1541
1542 /*
1543 * Double linked lists with a single pointer list head.
1544 * Mostly useful for hash tables where the two pointer list head is
1545 * too wasteful.
1546 * You lose the ability to access the tail in O(1).
1547 */
1548
1549 struct hlist_head {
1550 struct hlist_node *first;
1551 };
1552
1553 struct hlist_node {
1554 struct hlist_node *next, **pprev;
1555 };
1556
1557 #define HLIST_HEAD_INIT { .first = NULL }
1558 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1559 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1560 static inline void INIT_HLIST_NODE(struct hlist_node *h)
1561 {
1562 h->next = NULL;
1563 h->pprev = NULL;
1564 }
1565
1566 static inline int hlist_unhashed(const struct hlist_node *h)
1567 {
1568 return !h->pprev;
1569 }
1570
1571 static inline int hlist_empty(const struct hlist_head *h)
1572 {
1573 return !h->first;
1574 }
1575
1576 static inline void __hlist_del(struct hlist_node *n)
1577 {
1578 struct hlist_node *next = n->next;
1579 struct hlist_node **pprev = n->pprev;
1580 *pprev = next;
1581 if (next)
1582 next->pprev = pprev;
1583 }
1584
1585 static inline void hlist_del(struct hlist_node *n)
1586 {
1587 __hlist_del(n);
1588 n->next = LIST_POISON1;
1589 n->pprev = LIST_POISON2;
1590 }
1591
1592 static inline void hlist_del_init(struct hlist_node *n)
1593 {
1594 if (!hlist_unhashed(n)) {
1595 __hlist_del(n);
1596 INIT_HLIST_NODE(n);
1597 }
1598 }
1599
1600 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
1601 {
1602 struct hlist_node *first = h->first;
1603 n->next = first;
1604 if (first)
1605 first->pprev = &n->next;
1606 h->first = n;
1607 n->pprev = &h->first;
1608 }
1609
1610 /* next must be != NULL */
1611 static inline void hlist_add_before(struct hlist_node *n,
1612 struct hlist_node *next)
1613 {
1614 n->pprev = next->pprev;
1615 n->next = next;
1616 next->pprev = &n->next;
1617 *(n->pprev) = n;
1618 }
1619
1620 static inline void hlist_add_after(struct hlist_node *n,
1621 struct hlist_node *next)
1622 {
1623 next->next = n->next;
1624 n->next = next;
1625 next->pprev = &n->next;
1626
1627 if(next->next)
1628 next->next->pprev = &next->next;
1629 }
1630
1631 /*
1632 * Move a list from one list head to another. Fixup the pprev
1633 * reference of the first entry if it exists.
1634 */
1635 static inline void hlist_move_list(struct hlist_head *old,
1636 struct hlist_head *new)
1637 {
1638 new->first = old->first;
1639 if (new->first)
1640 new->first->pprev = &new->first;
1641 old->first = NULL;
1642 }
1643
1644 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1645
1646 #define hlist_for_each(pos, head) \
1647 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1648 pos = pos->next)
1649
1650 #define hlist_for_each_safe(pos, n, head) \
1651 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1652 pos = n)
1653
1654 /**
1655 * hlist_for_each_entry - iterate over list of given type
1656 * @tpos: the type * to use as a loop cursor.
1657 * @pos: the &struct hlist_node to use as a loop cursor.
1658 * @head: the head for your list.
1659 * @member: the name of the hlist_node within the struct.
1660 */
1661 #define hlist_for_each_entry(tpos, pos, head, member) \
1662 for (pos = (head)->first; \
1663 pos && ({ prefetch(pos->next); 1;}) && \
1664 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1665 pos = pos->next)
1666
1667 /**
1668 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1669 * @tpos: the type * to use as a loop cursor.
1670 * @pos: the &struct hlist_node to use as a loop cursor.
1671 * @member: the name of the hlist_node within the struct.
1672 */
1673 #define hlist_for_each_entry_continue(tpos, pos, member) \
1674 for (pos = (pos)->next; \
1675 pos && ({ prefetch(pos->next); 1;}) && \
1676 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1677 pos = pos->next)
1678
1679 /**
1680 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1681 * @tpos: the type * to use as a loop cursor.
1682 * @pos: the &struct hlist_node to use as a loop cursor.
1683 * @member: the name of the hlist_node within the struct.
1684 */
1685 #define hlist_for_each_entry_from(tpos, pos, member) \
1686 for (; pos && ({ prefetch(pos->next); 1;}) && \
1687 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1688 pos = pos->next)
1689
1690 /**
1691 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1692 * @tpos: the type * to use as a loop cursor.
1693 * @pos: the &struct hlist_node to use as a loop cursor.
1694 * @n: another &struct hlist_node to use as temporary storage
1695 * @head: the head for your list.
1696 * @member: the name of the hlist_node within the struct.
1697 */
1698 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1699 for (pos = (head)->first; \
1700 pos && ({ n = pos->next; 1; }) && \
1701 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1702 pos = n)
1703
1704 #endif
This page took 0.10664 seconds and 4 git commands to generate.