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