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