Document rcu_register_thread in README
[userspace-rcu.git] / api_ppc.h
CommitLineData
6d0ce021
PM
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_SMP
75#define CONFIG_PPC64
76
77#define CACHE_LINE_SIZE 128
78#define ____cacheline_internodealigned_in_smp \
79 __attribute__((__aligned__(1 << 7)))
80
81/*
82 * Atomic data structure, initialization, and access.
83 */
84
85typedef struct { volatile int counter; } atomic_t;
86
87#define ATOMIC_INIT(i) { (i) }
88
89#define atomic_read(v) ((v)->counter)
90#define atomic_set(v, i) (((v)->counter) = (i))
91
92/*
93 * Atomic operations.
94 */
95
96#define LWSYNC lwsync
97#define PPC405_ERR77(ra,rb)
98#ifdef CONFIG_SMP
99# define LWSYNC_ON_SMP stringify_in_c(LWSYNC) "\n"
100# define ISYNC_ON_SMP "\n\tisync\n"
101#else
102# define LWSYNC_ON_SMP
103# define ISYNC_ON_SMP
104#endif
105
106
107/*
108 * Atomic exchange
109 *
110 * Changes the memory location '*ptr' to be val and returns
111 * the previous value stored there.
112 */
113static __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 */
138static __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
156static __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
175static __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 */
197extern void __xchg_called_with_bad_pointer(void);
198
199static __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
214static __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
247static __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\
2622:"
263 : "=&r" (prev), "+m" (*p)
264 : "r" (p), "r" (old), "r" (new)
265 : "cc", "memory");
266
267 return prev;
268}
269
270static __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\
2842:"
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
293static __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\
3072:"
308 : "=&r" (prev), "+m" (*p)
309 : "r" (p), "r" (old), "r" (new)
310 : "cc", "memory");
311
312 return prev;
313}
314
315static __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\
3282:"
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(). */
339extern void __cmpxchg_called_with_bad_pointer(void);
340
341static __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
357static __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 */
425static __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 */
446static __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
460static __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 */
487static __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 */
498static __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 */
509static __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 */
522static __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 */
535static __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 */
547static __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 */
574static __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 */
588static __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#define smp_mb() __asm__ __volatile__("sync" : : : "memory")
622
623
624/*
625 * Generate 64-bit timestamp.
626 */
627
628static unsigned long long get_timestamp(void)
629{
630 unsigned int __a,__d;
631
632 asm volatile("mftbl %0" : "=r" (__a));
633 asm volatile("mftbu %0" : "=r" (__d));
634 return ((long long)__a) | (((long long)__d)<<32);
635}
636
637/*
638 * api_pthreads.h: API mapping to pthreads environment.
639 *
640 * This program is free software; you can redistribute it and/or modify
641 * it under the terms of the GNU General Public License as published by
642 * the Free Software Foundation; either version 2 of the License, or
643 * (at your option) any later version. However, please note that much
644 * of the code in this file derives from the Linux kernel, and that such
645 * code may not be available except under GPLv2.
646 *
647 * This program is distributed in the hope that it will be useful,
648 * but WITHOUT ANY WARRANTY; without even the implied warranty of
649 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
650 * GNU General Public License for more details.
651 *
652 * You should have received a copy of the GNU General Public License
653 * along with this program; if not, write to the Free Software
654 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
655 *
656 * Copyright (c) 2006 Paul E. McKenney, IBM.
657 */
658
659#include <stdio.h>
660#include <stdlib.h>
661#include <errno.h>
662#include <limits.h>
663#include <sys/types.h>
664#define __USE_GNU
665#include <pthread.h>
666#include <sched.h>
667#include <sys/param.h>
668/* #include "atomic.h" */
669
670/*
671 * Compiler magic.
672 */
673#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
674#define container_of(ptr, type, member) ({ \
675 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
676 (type *)( (char *)__mptr - offsetof(type,member) );})
677#define barrier() __asm__ __volatile__("": : :"memory")
678
679/*
680 * Default machine parameters.
681 */
682
683#ifndef CACHE_LINE_SIZE
684#define CACHE_LINE_SIZE 128
685#endif /* #ifndef CACHE_LINE_SIZE */
686
687/*
688 * Exclusive locking primitives.
689 */
690
691typedef pthread_mutex_t spinlock_t;
692
693#define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
694#define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
695
696static void spin_lock_init(spinlock_t *sp)
697{
698 if (pthread_mutex_init(sp, NULL) != 0) {
699 perror("spin_lock_init:pthread_mutex_init");
700 exit(-1);
701 }
702}
703
704static void spin_lock(spinlock_t *sp)
705{
706 if (pthread_mutex_lock(sp) != 0) {
707 perror("spin_lock:pthread_mutex_lock");
708 exit(-1);
709 }
710}
711
712static int spin_trylock(spinlock_t *sp)
713{
714 int retval;
715
716 if ((retval = pthread_mutex_trylock(sp)) == 0)
717 return 1;
718 if (retval == EBUSY)
719 return 0;
720 perror("spin_trylock:pthread_mutex_trylock");
721 exit(-1);
722}
723
724static void spin_unlock(spinlock_t *sp)
725{
726 if (pthread_mutex_unlock(sp) != 0) {
727 perror("spin_unlock:pthread_mutex_unlock");
728 exit(-1);
729 }
730}
731
732#define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
733#define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
734
735#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
736#define unlikely(x) x
737#define likely(x) x
738#define prefetch(x) x
739
740/*
741 * Thread creation/destruction primitives.
742 */
743
744typedef pthread_t thread_id_t;
745
746#define NR_THREADS 128
747
748#define __THREAD_ID_MAP_EMPTY 0
749#define __THREAD_ID_MAP_WAITING 1
750thread_id_t __thread_id_map[NR_THREADS];
751spinlock_t __thread_id_map_mutex;
752
753#define for_each_thread(t) \
754 for (t = 0; t < NR_THREADS; t++)
755
756#define for_each_running_thread(t) \
757 for (t = 0; t < NR_THREADS; t++) \
758 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
759 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
760
761#define for_each_tid(t, tid) \
762 for (t = 0; t < NR_THREADS; t++) \
763 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
764 ((tid) != __THREAD_ID_MAP_WAITING))
765
766pthread_key_t thread_id_key;
767
768static int __smp_thread_id(void)
769{
770 int i;
771 thread_id_t tid = pthread_self();
772
773 for (i = 0; i < NR_THREADS; i++) {
774 if (__thread_id_map[i] == tid) {
775 long v = i + 1; /* must be non-NULL. */
776
777 if (pthread_setspecific(thread_id_key, (void *)v) != 0) {
778 perror("pthread_setspecific");
779 exit(-1);
780 }
781 return i;
782 }
783 }
784 spin_lock(&__thread_id_map_mutex);
785 for (i = 0; i < NR_THREADS; i++) {
786 if (__thread_id_map[i] == tid)
787 spin_unlock(&__thread_id_map_mutex);
788 return i;
789 }
790 spin_unlock(&__thread_id_map_mutex);
791 fprintf(stderr, "smp_thread_id: Rogue thread, id: %d(%#x)\n", tid, tid);
792 exit(-1);
793}
794
795static int smp_thread_id(void)
796{
797 void *id;
798
799 id = pthread_getspecific(thread_id_key);
800 if (id == NULL)
801 return __smp_thread_id();
802 return (long)(id - 1);
803}
804
805static thread_id_t create_thread(void *(*func)(void *), void *arg)
806{
807 thread_id_t tid;
808 int i;
809
810 spin_lock(&__thread_id_map_mutex);
811 for (i = 0; i < NR_THREADS; i++) {
812 if (__thread_id_map[i] == __THREAD_ID_MAP_EMPTY)
813 break;
814 }
815 if (i >= NR_THREADS) {
816 spin_unlock(&__thread_id_map_mutex);
817 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
818 exit(-1);
819 }
820 __thread_id_map[i] = __THREAD_ID_MAP_WAITING;
821 spin_unlock(&__thread_id_map_mutex);
822 if (pthread_create(&tid, NULL, func, arg) != 0) {
823 perror("create_thread:pthread_create");
824 exit(-1);
825 }
826 __thread_id_map[i] = tid;
827 return tid;
828}
829
830static void *wait_thread(thread_id_t tid)
831{
832 int i;
833 void *vp;
834
835 for (i = 0; i < NR_THREADS; i++) {
836 if (__thread_id_map[i] == tid)
837 break;
838 }
839 if (i >= NR_THREADS){
840 fprintf(stderr, "wait_thread: bad tid = %d(%#x)\n", tid, tid);
841 exit(-1);
842 }
843 if (pthread_join(tid, &vp) != 0) {
844 perror("wait_thread:pthread_join");
845 exit(-1);
846 }
847 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
848 return vp;
849}
850
851static void wait_all_threads(void)
852{
853 int i;
854 thread_id_t tid;
855
856 for (i = 1; i < NR_THREADS; i++) {
857 tid = __thread_id_map[i];
858 if (tid != __THREAD_ID_MAP_EMPTY &&
859 tid != __THREAD_ID_MAP_WAITING)
860 (void)wait_thread(tid);
861 }
862}
863
864static void run_on(int cpu)
865{
866 cpu_set_t mask;
867
868 CPU_ZERO(&mask);
869 CPU_SET(cpu, &mask);
870 sched_setaffinity(0, sizeof(mask), &mask);
871}
872
873/*
874 * timekeeping -- very crude -- should use MONOTONIC...
875 */
876
877long long get_microseconds(void)
878{
879 struct timeval tv;
880
881 if (gettimeofday(&tv, NULL) != 0)
882 abort();
883 return ((long long)tv.tv_sec) * 1000000LL + (long long)tv.tv_usec;
884}
885
886/*
887 * Per-thread variables.
888 */
889
890#define DEFINE_PER_THREAD(type, name) \
891 struct { \
892 __typeof__(type) v \
893 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
894 } __per_thread_##name[NR_THREADS];
895#define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
896
897#define per_thread(name, thread) __per_thread_##name[thread].v
898#define __get_thread_var(name) per_thread(name, smp_thread_id())
899
900#define init_per_thread(name, v) \
901 do { \
902 int __i_p_t_i; \
903 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
904 per_thread(name, __i_p_t_i) = v; \
905 } while (0)
906
907/*
908 * CPU traversal primitives.
909 */
910
911#ifndef NR_CPUS
912#define NR_CPUS 16
913#endif /* #ifndef NR_CPUS */
914
915#define for_each_possible_cpu(cpu) \
916 for (cpu = 0; cpu < NR_CPUS; cpu++)
917#define for_each_online_cpu(cpu) \
918 for (cpu = 0; cpu < NR_CPUS; cpu++)
919
920/*
921 * Per-CPU variables.
922 */
923
924#define DEFINE_PER_CPU(type, name) \
925 struct { \
926 __typeof__(type) v \
927 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
928 } __per_cpu_##name[NR_CPUS]
929#define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
930
931DEFINE_PER_THREAD(int, smp_processor_id);
932
933static int smp_processor_id(void)
934{
935 return __get_thread_var(smp_processor_id);
936}
937
938static void set_smp_processor_id(int cpu)
939{
940 __get_thread_var(smp_processor_id) = cpu;
941}
942
943#define per_cpu(name, thread) __per_cpu_##name[thread].v
944#define __get_cpu_var(name) per_cpu(name, smp_processor_id())
945
946#define init_per_cpu(name, v) \
947 do { \
948 int __i_p_c_i; \
949 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
950 per_cpu(name, __i_p_c_i) = v; \
951 } while (0)
952
953/*
954 * CPU state checking (crowbarred).
955 */
956
957#define idle_cpu(cpu) 0
958#define in_softirq() 1
959#define hardirq_count() 0
960#define PREEMPT_SHIFT 0
961#define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
962#define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
963#define PREEMPT_BITS 8
964#define SOFTIRQ_BITS 8
965
966/*
967 * CPU hotplug.
968 */
969
970struct notifier_block {
971 int (*notifier_call)(struct notifier_block *, unsigned long, void *);
972 struct notifier_block *next;
973 int priority;
974};
975
976#define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
977#define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
978#define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
979#define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
980#define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
981#define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
982#define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
983 * not handling interrupts, soon dead */
984#define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
985 * lock is dropped */
986
987/* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
988 * operation in progress
989 */
990#define CPU_TASKS_FROZEN 0x0010
991
992#define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
993#define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
994#define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
995#define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
996#define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
997#define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
998#define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
999
1000/* Hibernation and suspend events */
1001#define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
1002#define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
1003#define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
1004#define PM_POST_SUSPEND 0x0004 /* Suspend finished */
1005#define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
1006#define PM_POST_RESTORE 0x0006 /* Restore failed */
1007
1008#define NOTIFY_DONE 0x0000 /* Don't care */
1009#define NOTIFY_OK 0x0001 /* Suits me */
1010#define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
1011#define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
1012 /* Bad/Veto action */
1013/*
1014 * Clean way to return from the notifier and stop further calls.
1015 */
1016#define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
1017
1018/*
1019 * Bug checks.
1020 */
1021
1022#define BUG_ON(c) do { if (!(c)) abort(); } while (0)
1023
1024/*
1025 * Initialization -- Must be called before calling any primitives.
1026 */
1027
1028static void smp_init(void)
1029{
1030 int i;
1031
1032 spin_lock_init(&__thread_id_map_mutex);
1033 __thread_id_map[0] = pthread_self();
1034 for (i = 1; i < NR_THREADS; i++)
1035 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
1036 init_per_thread(smp_processor_id, 0);
1037 if (pthread_key_create(&thread_id_key, NULL) != 0) {
1038 perror("pthread_key_create");
1039 exit(-1);
1040 }
1041}
1042
1043/* Taken from the Linux kernel source tree, so GPLv2-only!!! */
1044
1045#ifndef _LINUX_LIST_H
1046#define _LINUX_LIST_H
1047
1048#define LIST_POISON1 ((void *) 0x00100100)
1049#define LIST_POISON2 ((void *) 0x00200200)
1050
1051#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
1052#define container_of(ptr, type, member) ({ \
1053 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
1054 (type *)( (char *)__mptr - offsetof(type,member) );})
1055
1056/*
1057 * Simple doubly linked list implementation.
1058 *
1059 * Some of the internal functions ("__xxx") are useful when
1060 * manipulating whole lists rather than single entries, as
1061 * sometimes we already know the next/prev entries and we can
1062 * generate better code by using them directly rather than
1063 * using the generic single-entry routines.
1064 */
1065
1066struct list_head {
1067 struct list_head *next, *prev;
1068};
1069
1070#define LIST_HEAD_INIT(name) { &(name), &(name) }
1071
1072#define LIST_HEAD(name) \
1073 struct list_head name = LIST_HEAD_INIT(name)
1074
1075static inline void INIT_LIST_HEAD(struct list_head *list)
1076{
1077 list->next = list;
1078 list->prev = list;
1079}
1080
1081/*
1082 * Insert a new entry between two known consecutive entries.
1083 *
1084 * This is only for internal list manipulation where we know
1085 * the prev/next entries already!
1086 */
1087#ifndef CONFIG_DEBUG_LIST
1088static inline void __list_add(struct list_head *new,
1089 struct list_head *prev,
1090 struct list_head *next)
1091{
1092 next->prev = new;
1093 new->next = next;
1094 new->prev = prev;
1095 prev->next = new;
1096}
1097#else
1098extern void __list_add(struct list_head *new,
1099 struct list_head *prev,
1100 struct list_head *next);
1101#endif
1102
1103/**
1104 * list_add - add a new entry
1105 * @new: new entry to be added
1106 * @head: list head to add it after
1107 *
1108 * Insert a new entry after the specified head.
1109 * This is good for implementing stacks.
1110 */
1111static inline void list_add(struct list_head *new, struct list_head *head)
1112{
1113 __list_add(new, head, head->next);
1114}
1115
1116
1117/**
1118 * list_add_tail - add a new entry
1119 * @new: new entry to be added
1120 * @head: list head to add it before
1121 *
1122 * Insert a new entry before the specified head.
1123 * This is useful for implementing queues.
1124 */
1125static inline void list_add_tail(struct list_head *new, struct list_head *head)
1126{
1127 __list_add(new, head->prev, head);
1128}
1129
1130/*
1131 * Delete a list entry by making the prev/next entries
1132 * point to each other.
1133 *
1134 * This is only for internal list manipulation where we know
1135 * the prev/next entries already!
1136 */
1137static inline void __list_del(struct list_head * prev, struct list_head * next)
1138{
1139 next->prev = prev;
1140 prev->next = next;
1141}
1142
1143/**
1144 * list_del - deletes entry from list.
1145 * @entry: the element to delete from the list.
1146 * Note: list_empty() on entry does not return true after this, the entry is
1147 * in an undefined state.
1148 */
1149#ifndef CONFIG_DEBUG_LIST
1150static inline void list_del(struct list_head *entry)
1151{
1152 __list_del(entry->prev, entry->next);
1153 entry->next = LIST_POISON1;
1154 entry->prev = LIST_POISON2;
1155}
1156#else
1157extern void list_del(struct list_head *entry);
1158#endif
1159
1160/**
1161 * list_replace - replace old entry by new one
1162 * @old : the element to be replaced
1163 * @new : the new element to insert
1164 *
1165 * If @old was empty, it will be overwritten.
1166 */
1167static inline void list_replace(struct list_head *old,
1168 struct list_head *new)
1169{
1170 new->next = old->next;
1171 new->next->prev = new;
1172 new->prev = old->prev;
1173 new->prev->next = new;
1174}
1175
1176static inline void list_replace_init(struct list_head *old,
1177 struct list_head *new)
1178{
1179 list_replace(old, new);
1180 INIT_LIST_HEAD(old);
1181}
1182
1183/**
1184 * list_del_init - deletes entry from list and reinitialize it.
1185 * @entry: the element to delete from the list.
1186 */
1187static inline void list_del_init(struct list_head *entry)
1188{
1189 __list_del(entry->prev, entry->next);
1190 INIT_LIST_HEAD(entry);
1191}
1192
1193/**
1194 * list_move - delete from one list and add as another's head
1195 * @list: the entry to move
1196 * @head: the head that will precede our entry
1197 */
1198static inline void list_move(struct list_head *list, struct list_head *head)
1199{
1200 __list_del(list->prev, list->next);
1201 list_add(list, head);
1202}
1203
1204/**
1205 * list_move_tail - delete from one list and add as another's tail
1206 * @list: the entry to move
1207 * @head: the head that will follow our entry
1208 */
1209static inline void list_move_tail(struct list_head *list,
1210 struct list_head *head)
1211{
1212 __list_del(list->prev, list->next);
1213 list_add_tail(list, head);
1214}
1215
1216/**
1217 * list_is_last - tests whether @list is the last entry in list @head
1218 * @list: the entry to test
1219 * @head: the head of the list
1220 */
1221static inline int list_is_last(const struct list_head *list,
1222 const struct list_head *head)
1223{
1224 return list->next == head;
1225}
1226
1227/**
1228 * list_empty - tests whether a list is empty
1229 * @head: the list to test.
1230 */
1231static inline int list_empty(const struct list_head *head)
1232{
1233 return head->next == head;
1234}
1235
1236/**
1237 * list_empty_careful - tests whether a list is empty and not being modified
1238 * @head: the list to test
1239 *
1240 * Description:
1241 * tests whether a list is empty _and_ checks that no other CPU might be
1242 * in the process of modifying either member (next or prev)
1243 *
1244 * NOTE: using list_empty_careful() without synchronization
1245 * can only be safe if the only activity that can happen
1246 * to the list entry is list_del_init(). Eg. it cannot be used
1247 * if another CPU could re-list_add() it.
1248 */
1249static inline int list_empty_careful(const struct list_head *head)
1250{
1251 struct list_head *next = head->next;
1252 return (next == head) && (next == head->prev);
1253}
1254
1255/**
1256 * list_is_singular - tests whether a list has just one entry.
1257 * @head: the list to test.
1258 */
1259static inline int list_is_singular(const struct list_head *head)
1260{
1261 return !list_empty(head) && (head->next == head->prev);
1262}
1263
1264static inline void __list_cut_position(struct list_head *list,
1265 struct list_head *head, struct list_head *entry)
1266{
1267 struct list_head *new_first = entry->next;
1268 list->next = head->next;
1269 list->next->prev = list;
1270 list->prev = entry;
1271 entry->next = list;
1272 head->next = new_first;
1273 new_first->prev = head;
1274}
1275
1276/**
1277 * list_cut_position - cut a list into two
1278 * @list: a new list to add all removed entries
1279 * @head: a list with entries
1280 * @entry: an entry within head, could be the head itself
1281 * and if so we won't cut the list
1282 *
1283 * This helper moves the initial part of @head, up to and
1284 * including @entry, from @head to @list. You should
1285 * pass on @entry an element you know is on @head. @list
1286 * should be an empty list or a list you do not care about
1287 * losing its data.
1288 *
1289 */
1290static inline void list_cut_position(struct list_head *list,
1291 struct list_head *head, struct list_head *entry)
1292{
1293 if (list_empty(head))
1294 return;
1295 if (list_is_singular(head) &&
1296 (head->next != entry && head != entry))
1297 return;
1298 if (entry == head)
1299 INIT_LIST_HEAD(list);
1300 else
1301 __list_cut_position(list, head, entry);
1302}
1303
1304static inline void __list_splice(const struct list_head *list,
1305 struct list_head *prev,
1306 struct list_head *next)
1307{
1308 struct list_head *first = list->next;
1309 struct list_head *last = list->prev;
1310
1311 first->prev = prev;
1312 prev->next = first;
1313
1314 last->next = next;
1315 next->prev = last;
1316}
1317
1318/**
1319 * list_splice - join two lists, this is designed for stacks
1320 * @list: the new list to add.
1321 * @head: the place to add it in the first list.
1322 */
1323static inline void list_splice(const struct list_head *list,
1324 struct list_head *head)
1325{
1326 if (!list_empty(list))
1327 __list_splice(list, head, head->next);
1328}
1329
1330/**
1331 * list_splice_tail - join two lists, each list being a queue
1332 * @list: the new list to add.
1333 * @head: the place to add it in the first list.
1334 */
1335static inline void list_splice_tail(struct list_head *list,
1336 struct list_head *head)
1337{
1338 if (!list_empty(list))
1339 __list_splice(list, head->prev, head);
1340}
1341
1342/**
1343 * list_splice_init - join two lists and reinitialise the emptied list.
1344 * @list: the new list to add.
1345 * @head: the place to add it in the first list.
1346 *
1347 * The list at @list is reinitialised
1348 */
1349static inline void list_splice_init(struct list_head *list,
1350 struct list_head *head)
1351{
1352 if (!list_empty(list)) {
1353 __list_splice(list, head, head->next);
1354 INIT_LIST_HEAD(list);
1355 }
1356}
1357
1358/**
1359 * list_splice_tail_init - join two lists and reinitialise the emptied list
1360 * @list: the new list to add.
1361 * @head: the place to add it in the first list.
1362 *
1363 * Each of the lists is a queue.
1364 * The list at @list is reinitialised
1365 */
1366static inline void list_splice_tail_init(struct list_head *list,
1367 struct list_head *head)
1368{
1369 if (!list_empty(list)) {
1370 __list_splice(list, head->prev, head);
1371 INIT_LIST_HEAD(list);
1372 }
1373}
1374
1375/**
1376 * list_entry - get the struct for this entry
1377 * @ptr: the &struct list_head pointer.
1378 * @type: the type of the struct this is embedded in.
1379 * @member: the name of the list_struct within the struct.
1380 */
1381#define list_entry(ptr, type, member) \
1382 container_of(ptr, type, member)
1383
1384/**
1385 * list_first_entry - get the first element from a list
1386 * @ptr: the list head to take the element from.
1387 * @type: the type of the struct this is embedded in.
1388 * @member: the name of the list_struct within the struct.
1389 *
1390 * Note, that list is expected to be not empty.
1391 */
1392#define list_first_entry(ptr, type, member) \
1393 list_entry((ptr)->next, type, member)
1394
1395/**
1396 * list_for_each - iterate over a list
1397 * @pos: the &struct list_head to use as a loop cursor.
1398 * @head: the head for your list.
1399 */
1400#define list_for_each(pos, head) \
1401 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
1402 pos = pos->next)
1403
1404/**
1405 * __list_for_each - iterate over a list
1406 * @pos: the &struct list_head to use as a loop cursor.
1407 * @head: the head for your list.
1408 *
1409 * This variant differs from list_for_each() in that it's the
1410 * simplest possible list iteration code, no prefetching is done.
1411 * Use this for code that knows the list to be very short (empty
1412 * or 1 entry) most of the time.
1413 */
1414#define __list_for_each(pos, head) \
1415 for (pos = (head)->next; pos != (head); pos = pos->next)
1416
1417/**
1418 * list_for_each_prev - iterate over a list backwards
1419 * @pos: the &struct list_head to use as a loop cursor.
1420 * @head: the head for your list.
1421 */
1422#define list_for_each_prev(pos, head) \
1423 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1424 pos = pos->prev)
1425
1426/**
1427 * list_for_each_safe - iterate over a list safe against removal of list entry
1428 * @pos: the &struct list_head to use as a loop cursor.
1429 * @n: another &struct list_head to use as temporary storage
1430 * @head: the head for your list.
1431 */
1432#define list_for_each_safe(pos, n, head) \
1433 for (pos = (head)->next, n = pos->next; pos != (head); \
1434 pos = n, n = pos->next)
1435
1436/**
1437 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1438 * @pos: the &struct list_head to use as a loop cursor.
1439 * @n: another &struct list_head to use as temporary storage
1440 * @head: the head for your list.
1441 */
1442#define list_for_each_prev_safe(pos, n, head) \
1443 for (pos = (head)->prev, n = pos->prev; \
1444 prefetch(pos->prev), pos != (head); \
1445 pos = n, n = pos->prev)
1446
1447/**
1448 * list_for_each_entry - iterate over list of given type
1449 * @pos: the type * to use as a loop cursor.
1450 * @head: the head for your list.
1451 * @member: the name of the list_struct within the struct.
1452 */
1453#define list_for_each_entry(pos, head, member) \
1454 for (pos = list_entry((head)->next, typeof(*pos), member); \
1455 prefetch(pos->member.next), &pos->member != (head); \
1456 pos = list_entry(pos->member.next, typeof(*pos), member))
1457
1458/**
1459 * list_for_each_entry_reverse - iterate backwards over list of given type.
1460 * @pos: the type * to use as a loop cursor.
1461 * @head: the head for your list.
1462 * @member: the name of the list_struct within the struct.
1463 */
1464#define list_for_each_entry_reverse(pos, head, member) \
1465 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1466 prefetch(pos->member.prev), &pos->member != (head); \
1467 pos = list_entry(pos->member.prev, typeof(*pos), member))
1468
1469/**
1470 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1471 * @pos: the type * to use as a start point
1472 * @head: the head of the list
1473 * @member: the name of the list_struct within the struct.
1474 *
1475 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1476 */
1477#define list_prepare_entry(pos, head, member) \
1478 ((pos) ? : list_entry(head, typeof(*pos), member))
1479
1480/**
1481 * list_for_each_entry_continue - continue iteration over list of given type
1482 * @pos: the type * to use as a loop cursor.
1483 * @head: the head for your list.
1484 * @member: the name of the list_struct within the struct.
1485 *
1486 * Continue to iterate over list of given type, continuing after
1487 * the current position.
1488 */
1489#define list_for_each_entry_continue(pos, head, member) \
1490 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1491 prefetch(pos->member.next), &pos->member != (head); \
1492 pos = list_entry(pos->member.next, typeof(*pos), member))
1493
1494/**
1495 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1496 * @pos: the type * to use as a loop cursor.
1497 * @head: the head for your list.
1498 * @member: the name of the list_struct within the struct.
1499 *
1500 * Start to iterate over list of given type backwards, continuing after
1501 * the current position.
1502 */
1503#define list_for_each_entry_continue_reverse(pos, head, member) \
1504 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1505 prefetch(pos->member.prev), &pos->member != (head); \
1506 pos = list_entry(pos->member.prev, typeof(*pos), member))
1507
1508/**
1509 * list_for_each_entry_from - iterate over list of given type from the current point
1510 * @pos: the type * to use as a loop cursor.
1511 * @head: the head for your list.
1512 * @member: the name of the list_struct within the struct.
1513 *
1514 * Iterate over list of given type, continuing from current position.
1515 */
1516#define list_for_each_entry_from(pos, head, member) \
1517 for (; prefetch(pos->member.next), &pos->member != (head); \
1518 pos = list_entry(pos->member.next, typeof(*pos), member))
1519
1520/**
1521 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1522 * @pos: the type * to use as a loop cursor.
1523 * @n: another type * to use as temporary storage
1524 * @head: the head for your list.
1525 * @member: the name of the list_struct within the struct.
1526 */
1527#define list_for_each_entry_safe(pos, n, head, member) \
1528 for (pos = list_entry((head)->next, typeof(*pos), member), \
1529 n = list_entry(pos->member.next, typeof(*pos), member); \
1530 &pos->member != (head); \
1531 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1532
1533/**
1534 * list_for_each_entry_safe_continue
1535 * @pos: the type * to use as a loop cursor.
1536 * @n: another type * to use as temporary storage
1537 * @head: the head for your list.
1538 * @member: the name of the list_struct within the struct.
1539 *
1540 * Iterate over list of given type, continuing after current point,
1541 * safe against removal of list entry.
1542 */
1543#define list_for_each_entry_safe_continue(pos, n, head, member) \
1544 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1545 n = list_entry(pos->member.next, typeof(*pos), member); \
1546 &pos->member != (head); \
1547 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1548
1549/**
1550 * list_for_each_entry_safe_from
1551 * @pos: the type * to use as a loop cursor.
1552 * @n: another type * to use as temporary storage
1553 * @head: the head for your list.
1554 * @member: the name of the list_struct within the struct.
1555 *
1556 * Iterate over list of given type from current point, safe against
1557 * removal of list entry.
1558 */
1559#define list_for_each_entry_safe_from(pos, n, head, member) \
1560 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1561 &pos->member != (head); \
1562 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1563
1564/**
1565 * list_for_each_entry_safe_reverse
1566 * @pos: the type * to use as a loop cursor.
1567 * @n: another type * to use as temporary storage
1568 * @head: the head for your list.
1569 * @member: the name of the list_struct within the struct.
1570 *
1571 * Iterate backwards over list of given type, safe against removal
1572 * of list entry.
1573 */
1574#define list_for_each_entry_safe_reverse(pos, n, head, member) \
1575 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1576 n = list_entry(pos->member.prev, typeof(*pos), member); \
1577 &pos->member != (head); \
1578 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1579
1580/*
1581 * Double linked lists with a single pointer list head.
1582 * Mostly useful for hash tables where the two pointer list head is
1583 * too wasteful.
1584 * You lose the ability to access the tail in O(1).
1585 */
1586
1587struct hlist_head {
1588 struct hlist_node *first;
1589};
1590
1591struct hlist_node {
1592 struct hlist_node *next, **pprev;
1593};
1594
1595#define HLIST_HEAD_INIT { .first = NULL }
1596#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1597#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1598static inline void INIT_HLIST_NODE(struct hlist_node *h)
1599{
1600 h->next = NULL;
1601 h->pprev = NULL;
1602}
1603
1604static inline int hlist_unhashed(const struct hlist_node *h)
1605{
1606 return !h->pprev;
1607}
1608
1609static inline int hlist_empty(const struct hlist_head *h)
1610{
1611 return !h->first;
1612}
1613
1614static inline void __hlist_del(struct hlist_node *n)
1615{
1616 struct hlist_node *next = n->next;
1617 struct hlist_node **pprev = n->pprev;
1618 *pprev = next;
1619 if (next)
1620 next->pprev = pprev;
1621}
1622
1623static inline void hlist_del(struct hlist_node *n)
1624{
1625 __hlist_del(n);
1626 n->next = LIST_POISON1;
1627 n->pprev = LIST_POISON2;
1628}
1629
1630static inline void hlist_del_init(struct hlist_node *n)
1631{
1632 if (!hlist_unhashed(n)) {
1633 __hlist_del(n);
1634 INIT_HLIST_NODE(n);
1635 }
1636}
1637
1638static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
1639{
1640 struct hlist_node *first = h->first;
1641 n->next = first;
1642 if (first)
1643 first->pprev = &n->next;
1644 h->first = n;
1645 n->pprev = &h->first;
1646}
1647
1648/* next must be != NULL */
1649static inline void hlist_add_before(struct hlist_node *n,
1650 struct hlist_node *next)
1651{
1652 n->pprev = next->pprev;
1653 n->next = next;
1654 next->pprev = &n->next;
1655 *(n->pprev) = n;
1656}
1657
1658static inline void hlist_add_after(struct hlist_node *n,
1659 struct hlist_node *next)
1660{
1661 next->next = n->next;
1662 n->next = next;
1663 next->pprev = &n->next;
1664
1665 if(next->next)
1666 next->next->pprev = &next->next;
1667}
1668
1669/*
1670 * Move a list from one list head to another. Fixup the pprev
1671 * reference of the first entry if it exists.
1672 */
1673static inline void hlist_move_list(struct hlist_head *old,
1674 struct hlist_head *new)
1675{
1676 new->first = old->first;
1677 if (new->first)
1678 new->first->pprev = &new->first;
1679 old->first = NULL;
1680}
1681
1682#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1683
1684#define hlist_for_each(pos, head) \
1685 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1686 pos = pos->next)
1687
1688#define hlist_for_each_safe(pos, n, head) \
1689 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1690 pos = n)
1691
1692/**
1693 * hlist_for_each_entry - iterate over list of given type
1694 * @tpos: the type * to use as a loop cursor.
1695 * @pos: the &struct hlist_node to use as a loop cursor.
1696 * @head: the head for your list.
1697 * @member: the name of the hlist_node within the struct.
1698 */
1699#define hlist_for_each_entry(tpos, pos, head, member) \
1700 for (pos = (head)->first; \
1701 pos && ({ prefetch(pos->next); 1;}) && \
1702 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1703 pos = pos->next)
1704
1705/**
1706 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1707 * @tpos: the type * to use as a loop cursor.
1708 * @pos: the &struct hlist_node to use as a loop cursor.
1709 * @member: the name of the hlist_node within the struct.
1710 */
1711#define hlist_for_each_entry_continue(tpos, pos, member) \
1712 for (pos = (pos)->next; \
1713 pos && ({ prefetch(pos->next); 1;}) && \
1714 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1715 pos = pos->next)
1716
1717/**
1718 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1719 * @tpos: the type * to use as a loop cursor.
1720 * @pos: the &struct hlist_node to use as a loop cursor.
1721 * @member: the name of the hlist_node within the struct.
1722 */
1723#define hlist_for_each_entry_from(tpos, pos, member) \
1724 for (; pos && ({ prefetch(pos->next); 1;}) && \
1725 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1726 pos = pos->next)
1727
1728/**
1729 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1730 * @tpos: the type * to use as a loop cursor.
1731 * @pos: the &struct hlist_node to use as a loop cursor.
1732 * @n: another &struct hlist_node to use as temporary storage
1733 * @head: the head for your list.
1734 * @member: the name of the hlist_node within the struct.
1735 */
1736#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1737 for (pos = (head)->first; \
1738 pos && ({ n = pos->next; 1; }) && \
1739 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1740 pos = n)
1741
1742#endif
This page took 0.081215 seconds and 4 git commands to generate.