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