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