1 /* MECHANICALLY GENERATED, DO NOT EDIT!!! */
7 * common.h: Common Linux kernel-isms.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; but version 2 of the License only due
12 * to code included from the Linux kernel.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Copyright (c) 2006 Paul E. McKenney, IBM.
25 * Much code taken from the Linux kernel. For such code, the option
26 * to redistribute under later versions of GPL might not be available.
29 #include <urcu/arch.h>
31 #ifndef __always_inline
32 #define __always_inline inline
35 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
36 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
39 # define stringify_in_c(...) __VA_ARGS__
40 # define ASM_CONST(x) x
42 /* This version of stringify will deal with commas... */
43 # define __stringify_in_c(...) #__VA_ARGS__
44 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
45 # define __ASM_CONST(x) x##UL
46 # define ASM_CONST(x) __ASM_CONST(x)
51 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
53 * This program is free software; you can redistribute it and/or modify
54 * it under the terms of the GNU General Public License as published by
55 * the Free Software Foundation, but version 2 only due to inclusion
56 * of Linux-kernel code.
58 * This program is distributed in the hope that it will be useful,
59 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 * GNU General Public License for more details.
63 * You should have received a copy of the GNU General Public License
64 * along with this program; if not, write to the Free Software
65 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
67 * Copyright (c) 2006 Paul E. McKenney, IBM.
69 * Much code taken from the Linux kernel. For such code, the option
70 * to redistribute under later versions of GPL might not be available.
77 /* #define CACHE_LINE_SIZE 64 */
78 #define ____cacheline_internodealigned_in_smp \
79 __attribute__((__aligned__(1 << 6)))
81 #define LOCK_PREFIX "lock ; "
83 #if 0 /* duplicate with arch_atomic.h */
86 * Atomic data structure, initialization, and access.
89 typedef struct { volatile int counter
; } atomic_t
;
91 #define ATOMIC_INIT(i) { (i) }
93 #define atomic_read(v) ((v)->counter)
94 #define atomic_set(v, i) (((v)->counter) = (i))
101 * atomic_add - add integer to atomic variable
102 * @i: integer value to add
103 * @v: pointer of type atomic_t
105 * Atomically adds @i to @v.
107 static __inline__
void atomic_add(int i
, atomic_t
*v
)
109 __asm__
__volatile__(
110 LOCK_PREFIX
"addl %1,%0"
116 * atomic_sub - subtract the atomic variable
117 * @i: integer value to subtract
118 * @v: pointer of type atomic_t
120 * Atomically subtracts @i from @v.
122 static __inline__
void atomic_sub(int i
, atomic_t
*v
)
124 __asm__
__volatile__(
125 LOCK_PREFIX
"subl %1,%0"
131 * atomic_sub_and_test - subtract value from variable and test result
132 * @i: integer value to subtract
133 * @v: pointer of type atomic_t
135 * Atomically subtracts @i from @v and returns
136 * true if the result is zero, or false for all
139 static __inline__
int atomic_sub_and_test(int i
, atomic_t
*v
)
143 __asm__
__volatile__(
144 LOCK_PREFIX
"subl %2,%0; sete %1"
145 :"+m" (v
->counter
), "=qm" (c
)
146 :"ir" (i
) : "memory");
151 * atomic_inc - increment atomic variable
152 * @v: pointer of type atomic_t
154 * Atomically increments @v by 1.
156 static __inline__
void atomic_inc(atomic_t
*v
)
158 __asm__
__volatile__(
159 LOCK_PREFIX
"incl %0"
164 * atomic_dec - decrement atomic variable
165 * @v: pointer of type atomic_t
167 * Atomically decrements @v by 1.
169 static __inline__
void atomic_dec(atomic_t
*v
)
171 __asm__
__volatile__(
172 LOCK_PREFIX
"decl %0"
177 * atomic_dec_and_test - decrement and test
178 * @v: pointer of type atomic_t
180 * Atomically decrements @v by 1 and
181 * returns true if the result is 0, or false for all other
184 static __inline__
int atomic_dec_and_test(atomic_t
*v
)
188 __asm__
__volatile__(
189 LOCK_PREFIX
"decl %0; sete %1"
190 :"+m" (v
->counter
), "=qm" (c
)
196 * atomic_inc_and_test - increment and test
197 * @v: pointer of type atomic_t
199 * Atomically increments @v by 1
200 * and returns true if the result is zero, or false for all
203 static __inline__
int atomic_inc_and_test(atomic_t
*v
)
207 __asm__
__volatile__(
208 LOCK_PREFIX
"incl %0; sete %1"
209 :"+m" (v
->counter
), "=qm" (c
)
215 * atomic_add_negative - add and test if negative
216 * @v: pointer of type atomic_t
217 * @i: integer value to add
219 * Atomically adds @i to @v and returns true
220 * if the result is negative, or false when
221 * result is greater than or equal to zero.
223 static __inline__
int atomic_add_negative(int i
, atomic_t
*v
)
227 __asm__
__volatile__(
228 LOCK_PREFIX
"addl %2,%0; sets %1"
229 :"+m" (v
->counter
), "=qm" (c
)
230 :"ir" (i
) : "memory");
235 * atomic_add_return - add and return
236 * @v: pointer of type atomic_t
237 * @i: integer value to add
239 * Atomically adds @i to @v and returns @i + @v
241 static __inline__
int atomic_add_return(int i
, atomic_t
*v
)
246 __asm__
__volatile__(
247 LOCK_PREFIX
"xaddl %0, %1;"
249 :"m"(v
->counter
), "0"(i
));
253 static __inline__
int atomic_sub_return(int i
, atomic_t
*v
)
255 return atomic_add_return(-i
,v
);
258 static inline unsigned int
259 cmpxchg(volatile long *ptr
, long oldval
, long newval
)
261 unsigned long retval
;
264 "lock; cmpxchgl %4,(%2)\n"
265 "# end atomic_cmpxchg4"
266 : "=a" (retval
), "=m" (*ptr
)
267 : "r" (ptr
), "0" (oldval
), "r" (newval
), "m" (*ptr
)
272 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
273 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
276 * atomic_add_unless - add unless the number is a given value
277 * @v: pointer of type atomic_t
278 * @a: the amount to add to v...
279 * @u: ...unless v is equal to u.
281 * Atomically adds @a to @v, so long as it was not @u.
282 * Returns non-zero if @v was not @u, and zero otherwise.
284 #define atomic_add_unless(v, a, u) \
287 c = atomic_read(v); \
289 if (unlikely(c == (u))) \
291 old = atomic_cmpxchg((v), c, c + (a)); \
292 if (likely(old == c)) \
298 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
300 #define atomic_inc_return(v) (atomic_add_return(1,v))
301 #define atomic_dec_return(v) (atomic_sub_return(1,v))
303 /* These are x86-specific, used by some header files */
304 #define atomic_clear_mask(mask, addr) \
305 __asm__ __volatile__(LOCK_PREFIX "andl %0,%1" \
306 : : "r" (~(mask)),"m" (*addr) : "memory")
308 #define atomic_set_mask(mask, addr) \
309 __asm__ __volatile__(LOCK_PREFIX "orl %0,%1" \
310 : : "r" (mask),"m" (*(addr)) : "memory")
312 /* Atomic operations are already serializing on x86 */
313 #define smp_mb__before_atomic_dec() barrier()
314 #define smp_mb__after_atomic_dec() barrier()
315 #define smp_mb__before_atomic_inc() barrier()
316 #define smp_mb__after_atomic_inc() barrier()
321 * api_pthreads.h: API mapping to pthreads environment.
323 * This program is free software; you can redistribute it and/or modify
324 * it under the terms of the GNU General Public License as published by
325 * the Free Software Foundation; either version 2 of the License, or
326 * (at your option) any later version. However, please note that much
327 * of the code in this file derives from the Linux kernel, and that such
328 * code may not be available except under GPLv2.
330 * This program is distributed in the hope that it will be useful,
331 * but WITHOUT ANY WARRANTY; without even the implied warranty of
332 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
333 * GNU General Public License for more details.
335 * You should have received a copy of the GNU General Public License
336 * along with this program; if not, write to the Free Software
337 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
339 * Copyright (c) 2006 Paul E. McKenney, IBM.
346 #include <sys/types.h>
350 #include <sys/param.h>
351 /* #include "atomic.h" */
356 #define container_of(ptr, type, member) ({ \
357 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
358 (type *)( (char *)__mptr - offsetof(type,member) );})
361 * Default machine parameters.
364 #ifndef CACHE_LINE_SIZE
365 /* #define CACHE_LINE_SIZE 128 */
366 #endif /* #ifndef CACHE_LINE_SIZE */
369 * Exclusive locking primitives.
372 typedef pthread_mutex_t spinlock_t
;
374 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
375 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
377 static void spin_lock_init(spinlock_t
*sp
)
379 if (pthread_mutex_init(sp
, NULL
) != 0) {
380 perror("spin_lock_init:pthread_mutex_init");
385 static void spin_lock(spinlock_t
*sp
)
387 if (pthread_mutex_lock(sp
) != 0) {
388 perror("spin_lock:pthread_mutex_lock");
393 static void spin_unlock(spinlock_t
*sp
)
395 if (pthread_mutex_unlock(sp
) != 0) {
396 perror("spin_unlock:pthread_mutex_unlock");
401 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
402 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
405 * Thread creation/destruction primitives.
408 typedef pthread_t thread_id_t
;
410 #define NR_THREADS 128
412 #define __THREAD_ID_MAP_EMPTY 0
413 #define __THREAD_ID_MAP_WAITING 1
414 thread_id_t __thread_id_map
[NR_THREADS
];
415 spinlock_t __thread_id_map_mutex
;
417 #define for_each_thread(t) \
418 for (t = 0; t < NR_THREADS; t++)
420 #define for_each_running_thread(t) \
421 for (t = 0; t < NR_THREADS; t++) \
422 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
423 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
425 pthread_key_t thread_id_key
;
427 static int __smp_thread_id(void)
430 thread_id_t tid
= pthread_self();
432 for (i
= 0; i
< NR_THREADS
; i
++) {
433 if (__thread_id_map
[i
] == tid
) {
434 long v
= i
+ 1; /* must be non-NULL. */
436 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
437 perror("pthread_setspecific");
443 spin_lock(&__thread_id_map_mutex
);
444 for (i
= 0; i
< NR_THREADS
; i
++) {
445 if (__thread_id_map
[i
] == tid
)
446 spin_unlock(&__thread_id_map_mutex
);
449 spin_unlock(&__thread_id_map_mutex
);
450 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
455 static int smp_thread_id(void)
459 id
= pthread_getspecific(thread_id_key
);
461 return __smp_thread_id();
462 return (long)(id
- 1);
465 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
470 spin_lock(&__thread_id_map_mutex
);
471 for (i
= 0; i
< NR_THREADS
; i
++) {
472 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
475 if (i
>= NR_THREADS
) {
476 spin_unlock(&__thread_id_map_mutex
);
477 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
480 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
481 spin_unlock(&__thread_id_map_mutex
);
482 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
483 perror("create_thread:pthread_create");
486 __thread_id_map
[i
] = tid
;
490 static void *wait_thread(thread_id_t tid
)
495 for (i
= 0; i
< NR_THREADS
; i
++) {
496 if (__thread_id_map
[i
] == tid
)
499 if (i
>= NR_THREADS
){
500 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
504 if (pthread_join(tid
, &vp
) != 0) {
505 perror("wait_thread:pthread_join");
508 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
512 static void wait_all_threads(void)
517 for (i
= 1; i
< NR_THREADS
; i
++) {
518 tid
= __thread_id_map
[i
];
519 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
520 tid
!= __THREAD_ID_MAP_WAITING
)
521 (void)wait_thread(tid
);
525 static void run_on(int cpu
)
531 sched_setaffinity(0, sizeof(mask
), &mask
);
535 * timekeeping -- very crude -- should use MONOTONIC...
538 long long get_microseconds(void)
542 if (gettimeofday(&tv
, NULL
) != 0)
544 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
548 * Per-thread variables.
551 #define DEFINE_PER_THREAD(type, name) \
554 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
555 } __per_thread_##name[NR_THREADS];
556 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
558 #define per_thread(name, thread) __per_thread_##name[thread].v
559 #define __get_thread_var(name) per_thread(name, smp_thread_id())
561 #define init_per_thread(name, v) \
564 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
565 per_thread(name, __i_p_t_i) = v; \
569 * CPU traversal primitives.
574 #endif /* #ifndef NR_CPUS */
576 #define for_each_possible_cpu(cpu) \
577 for (cpu = 0; cpu < NR_CPUS; cpu++)
578 #define for_each_online_cpu(cpu) \
579 for (cpu = 0; cpu < NR_CPUS; cpu++)
585 #define DEFINE_PER_CPU(type, name) \
588 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
589 } __per_cpu_##name[NR_CPUS]
590 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
592 DEFINE_PER_THREAD(int, smp_processor_id
);
594 #define per_cpu(name, thread) __per_cpu_##name[thread].v
595 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
597 #define init_per_cpu(name, v) \
600 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
601 per_cpu(name, __i_p_c_i) = v; \
605 * CPU state checking (crowbarred).
608 #define idle_cpu(cpu) 0
609 #define in_softirq() 1
610 #define hardirq_count() 0
611 #define PREEMPT_SHIFT 0
612 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
613 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
614 #define PREEMPT_BITS 8
615 #define SOFTIRQ_BITS 8
621 struct notifier_block
{
622 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
623 struct notifier_block
*next
;
627 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
628 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
629 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
630 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
631 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
632 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
633 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
634 * not handling interrupts, soon dead */
635 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
638 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
639 * operation in progress
641 #define CPU_TASKS_FROZEN 0x0010
643 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
644 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
645 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
646 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
647 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
648 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
649 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
651 /* Hibernation and suspend events */
652 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
653 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
654 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
655 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
656 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
657 #define PM_POST_RESTORE 0x0006 /* Restore failed */
659 #define NOTIFY_DONE 0x0000 /* Don't care */
660 #define NOTIFY_OK 0x0001 /* Suits me */
661 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
662 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
663 /* Bad/Veto action */
665 * Clean way to return from the notifier and stop further calls.
667 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
673 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
676 * Initialization -- Must be called before calling any primitives.
679 static void smp_init(void)
683 spin_lock_init(&__thread_id_map_mutex
);
684 __thread_id_map
[0] = pthread_self();
685 for (i
= 1; i
< NR_THREADS
; i
++)
686 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
687 init_per_thread(smp_processor_id
, 0);
688 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
689 perror("pthread_key_create");
694 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
696 #ifndef _LINUX_LIST_H
697 #define _LINUX_LIST_H
699 #define LIST_POISON1 ((void *) 0x00100100)
700 #define LIST_POISON2 ((void *) 0x00200200)
702 #define container_of(ptr, type, member) ({ \
703 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
704 (type *)( (char *)__mptr - offsetof(type,member) );})
709 * Simple doubly linked list implementation.
711 * Some of the internal functions ("__xxx") are useful when
712 * manipulating whole lists rather than single entries, as
713 * sometimes we already know the next/prev entries and we can
714 * generate better code by using them directly rather than
715 * using the generic single-entry routines.
719 struct list_head
*next
, *prev
;
722 #define LIST_HEAD_INIT(name) { &(name), &(name) }
724 #define LIST_HEAD(name) \
725 struct list_head name = LIST_HEAD_INIT(name)
727 static inline void INIT_LIST_HEAD(struct list_head
*list
)
734 * Insert a new entry between two known consecutive entries.
736 * This is only for internal list manipulation where we know
737 * the prev/next entries already!
739 #ifndef CONFIG_DEBUG_LIST
740 static inline void __list_add(struct list_head
*new,
741 struct list_head
*prev
,
742 struct list_head
*next
)
750 extern void __list_add(struct list_head
*new,
751 struct list_head
*prev
,
752 struct list_head
*next
);
756 * list_add - add a new entry
757 * @new: new entry to be added
758 * @head: list head to add it after
760 * Insert a new entry after the specified head.
761 * This is good for implementing stacks.
763 static inline void list_add(struct list_head
*new, struct list_head
*head
)
765 __list_add(new, head
, head
->next
);
770 * list_add_tail - add a new entry
771 * @new: new entry to be added
772 * @head: list head to add it before
774 * Insert a new entry before the specified head.
775 * This is useful for implementing queues.
777 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
779 __list_add(new, head
->prev
, head
);
783 * Delete a list entry by making the prev/next entries
784 * point to each other.
786 * This is only for internal list manipulation where we know
787 * the prev/next entries already!
789 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
796 * list_del - deletes entry from list.
797 * @entry: the element to delete from the list.
798 * Note: list_empty() on entry does not return true after this, the entry is
799 * in an undefined state.
801 #ifndef CONFIG_DEBUG_LIST
802 static inline void list_del(struct list_head
*entry
)
804 __list_del(entry
->prev
, entry
->next
);
805 entry
->next
= LIST_POISON1
;
806 entry
->prev
= LIST_POISON2
;
809 extern void list_del(struct list_head
*entry
);
813 * list_replace - replace old entry by new one
814 * @old : the element to be replaced
815 * @new : the new element to insert
817 * If @old was empty, it will be overwritten.
819 static inline void list_replace(struct list_head
*old
,
820 struct list_head
*new)
822 new->next
= old
->next
;
823 new->next
->prev
= new;
824 new->prev
= old
->prev
;
825 new->prev
->next
= new;
828 static inline void list_replace_init(struct list_head
*old
,
829 struct list_head
*new)
831 list_replace(old
, new);
836 * list_del_init - deletes entry from list and reinitialize it.
837 * @entry: the element to delete from the list.
839 static inline void list_del_init(struct list_head
*entry
)
841 __list_del(entry
->prev
, entry
->next
);
842 INIT_LIST_HEAD(entry
);
846 * list_move - delete from one list and add as another's head
847 * @list: the entry to move
848 * @head: the head that will precede our entry
850 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
852 __list_del(list
->prev
, list
->next
);
853 list_add(list
, head
);
857 * list_move_tail - delete from one list and add as another's tail
858 * @list: the entry to move
859 * @head: the head that will follow our entry
861 static inline void list_move_tail(struct list_head
*list
,
862 struct list_head
*head
)
864 __list_del(list
->prev
, list
->next
);
865 list_add_tail(list
, head
);
869 * list_is_last - tests whether @list is the last entry in list @head
870 * @list: the entry to test
871 * @head: the head of the list
873 static inline int list_is_last(const struct list_head
*list
,
874 const struct list_head
*head
)
876 return list
->next
== head
;
880 * list_empty - tests whether a list is empty
881 * @head: the list to test.
883 static inline int list_empty(const struct list_head
*head
)
885 return head
->next
== head
;
889 * list_empty_careful - tests whether a list is empty and not being modified
890 * @head: the list to test
893 * tests whether a list is empty _and_ checks that no other CPU might be
894 * in the process of modifying either member (next or prev)
896 * NOTE: using list_empty_careful() without synchronization
897 * can only be safe if the only activity that can happen
898 * to the list entry is list_del_init(). Eg. it cannot be used
899 * if another CPU could re-list_add() it.
901 static inline int list_empty_careful(const struct list_head
*head
)
903 struct list_head
*next
= head
->next
;
904 return (next
== head
) && (next
== head
->prev
);
908 * list_is_singular - tests whether a list has just one entry.
909 * @head: the list to test.
911 static inline int list_is_singular(const struct list_head
*head
)
913 return !list_empty(head
) && (head
->next
== head
->prev
);
916 static inline void __list_cut_position(struct list_head
*list
,
917 struct list_head
*head
, struct list_head
*entry
)
919 struct list_head
*new_first
= entry
->next
;
920 list
->next
= head
->next
;
921 list
->next
->prev
= list
;
924 head
->next
= new_first
;
925 new_first
->prev
= head
;
929 * list_cut_position - cut a list into two
930 * @list: a new list to add all removed entries
931 * @head: a list with entries
932 * @entry: an entry within head, could be the head itself
933 * and if so we won't cut the list
935 * This helper moves the initial part of @head, up to and
936 * including @entry, from @head to @list. You should
937 * pass on @entry an element you know is on @head. @list
938 * should be an empty list or a list you do not care about
942 static inline void list_cut_position(struct list_head
*list
,
943 struct list_head
*head
, struct list_head
*entry
)
945 if (list_empty(head
))
947 if (list_is_singular(head
) &&
948 (head
->next
!= entry
&& head
!= entry
))
951 INIT_LIST_HEAD(list
);
953 __list_cut_position(list
, head
, entry
);
956 static inline void __list_splice(const struct list_head
*list
,
957 struct list_head
*prev
,
958 struct list_head
*next
)
960 struct list_head
*first
= list
->next
;
961 struct list_head
*last
= list
->prev
;
971 * list_splice - join two lists, this is designed for stacks
972 * @list: the new list to add.
973 * @head: the place to add it in the first list.
975 static inline void list_splice(const struct list_head
*list
,
976 struct list_head
*head
)
978 if (!list_empty(list
))
979 __list_splice(list
, head
, head
->next
);
983 * list_splice_tail - join two lists, each list being a queue
984 * @list: the new list to add.
985 * @head: the place to add it in the first list.
987 static inline void list_splice_tail(struct list_head
*list
,
988 struct list_head
*head
)
990 if (!list_empty(list
))
991 __list_splice(list
, head
->prev
, head
);
995 * list_splice_init - join two lists and reinitialise the emptied list.
996 * @list: the new list to add.
997 * @head: the place to add it in the first list.
999 * The list at @list is reinitialised
1001 static inline void list_splice_init(struct list_head
*list
,
1002 struct list_head
*head
)
1004 if (!list_empty(list
)) {
1005 __list_splice(list
, head
, head
->next
);
1006 INIT_LIST_HEAD(list
);
1011 * list_splice_tail_init - join two lists and reinitialise the emptied list
1012 * @list: the new list to add.
1013 * @head: the place to add it in the first list.
1015 * Each of the lists is a queue.
1016 * The list at @list is reinitialised
1018 static inline void list_splice_tail_init(struct list_head
*list
,
1019 struct list_head
*head
)
1021 if (!list_empty(list
)) {
1022 __list_splice(list
, head
->prev
, head
);
1023 INIT_LIST_HEAD(list
);
1028 * list_entry - get the struct for this entry
1029 * @ptr: the &struct list_head pointer.
1030 * @type: the type of the struct this is embedded in.
1031 * @member: the name of the list_struct within the struct.
1033 #define list_entry(ptr, type, member) \
1034 container_of(ptr, type, member)
1037 * list_first_entry - get the first element from a list
1038 * @ptr: the list head to take the element from.
1039 * @type: the type of the struct this is embedded in.
1040 * @member: the name of the list_struct within the struct.
1042 * Note, that list is expected to be not empty.
1044 #define list_first_entry(ptr, type, member) \
1045 list_entry((ptr)->next, type, member)
1048 * list_for_each - iterate over a list
1049 * @pos: the &struct list_head to use as a loop cursor.
1050 * @head: the head for your list.
1052 #define list_for_each(pos, head) \
1053 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
1057 * __list_for_each - iterate over a list
1058 * @pos: the &struct list_head to use as a loop cursor.
1059 * @head: the head for your list.
1061 * This variant differs from list_for_each() in that it's the
1062 * simplest possible list iteration code, no prefetching is done.
1063 * Use this for code that knows the list to be very short (empty
1064 * or 1 entry) most of the time.
1066 #define __list_for_each(pos, head) \
1067 for (pos = (head)->next; pos != (head); pos = pos->next)
1070 * list_for_each_prev - iterate over a list backwards
1071 * @pos: the &struct list_head to use as a loop cursor.
1072 * @head: the head for your list.
1074 #define list_for_each_prev(pos, head) \
1075 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1079 * list_for_each_safe - iterate over a list safe against removal of list entry
1080 * @pos: the &struct list_head to use as a loop cursor.
1081 * @n: another &struct list_head to use as temporary storage
1082 * @head: the head for your list.
1084 #define list_for_each_safe(pos, n, head) \
1085 for (pos = (head)->next, n = pos->next; pos != (head); \
1086 pos = n, n = pos->next)
1089 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1090 * @pos: the &struct list_head to use as a loop cursor.
1091 * @n: another &struct list_head to use as temporary storage
1092 * @head: the head for your list.
1094 #define list_for_each_prev_safe(pos, n, head) \
1095 for (pos = (head)->prev, n = pos->prev; \
1096 prefetch(pos->prev), pos != (head); \
1097 pos = n, n = pos->prev)
1100 * list_for_each_entry - iterate over list of given type
1101 * @pos: the type * to use as a loop cursor.
1102 * @head: the head for your list.
1103 * @member: the name of the list_struct within the struct.
1105 #define list_for_each_entry(pos, head, member) \
1106 for (pos = list_entry((head)->next, typeof(*pos), member); \
1107 prefetch(pos->member.next), &pos->member != (head); \
1108 pos = list_entry(pos->member.next, typeof(*pos), member))
1111 * list_for_each_entry_reverse - iterate backwards over list of given type.
1112 * @pos: the type * to use as a loop cursor.
1113 * @head: the head for your list.
1114 * @member: the name of the list_struct within the struct.
1116 #define list_for_each_entry_reverse(pos, head, member) \
1117 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1118 prefetch(pos->member.prev), &pos->member != (head); \
1119 pos = list_entry(pos->member.prev, typeof(*pos), member))
1122 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1123 * @pos: the type * to use as a start point
1124 * @head: the head of the list
1125 * @member: the name of the list_struct within the struct.
1127 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1129 #define list_prepare_entry(pos, head, member) \
1130 ((pos) ? : list_entry(head, typeof(*pos), member))
1133 * list_for_each_entry_continue - continue iteration over list of given type
1134 * @pos: the type * to use as a loop cursor.
1135 * @head: the head for your list.
1136 * @member: the name of the list_struct within the struct.
1138 * Continue to iterate over list of given type, continuing after
1139 * the current position.
1141 #define list_for_each_entry_continue(pos, head, member) \
1142 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1143 prefetch(pos->member.next), &pos->member != (head); \
1144 pos = list_entry(pos->member.next, typeof(*pos), member))
1147 * list_for_each_entry_continue_reverse - iterate backwards from the given point
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.
1152 * Start to iterate over list of given type backwards, continuing after
1153 * the current position.
1155 #define list_for_each_entry_continue_reverse(pos, head, member) \
1156 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1157 prefetch(pos->member.prev), &pos->member != (head); \
1158 pos = list_entry(pos->member.prev, typeof(*pos), member))
1161 * list_for_each_entry_from - iterate over list of given type from the current 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.
1166 * Iterate over list of given type, continuing from current position.
1168 #define list_for_each_entry_from(pos, head, member) \
1169 for (; prefetch(pos->member.next), &pos->member != (head); \
1170 pos = list_entry(pos->member.next, typeof(*pos), member))
1173 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1174 * @pos: the type * to use as a loop cursor.
1175 * @n: another type * to use as temporary storage
1176 * @head: the head for your list.
1177 * @member: the name of the list_struct within the struct.
1179 #define list_for_each_entry_safe(pos, n, head, member) \
1180 for (pos = list_entry((head)->next, typeof(*pos), member), \
1181 n = list_entry(pos->member.next, typeof(*pos), member); \
1182 &pos->member != (head); \
1183 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1186 * list_for_each_entry_safe_continue
1187 * @pos: the type * to use as a loop cursor.
1188 * @n: another type * to use as temporary storage
1189 * @head: the head for your list.
1190 * @member: the name of the list_struct within the struct.
1192 * Iterate over list of given type, continuing after current point,
1193 * safe against removal of list entry.
1195 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1196 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1197 n = list_entry(pos->member.next, typeof(*pos), member); \
1198 &pos->member != (head); \
1199 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1202 * list_for_each_entry_safe_from
1203 * @pos: the type * to use as a loop cursor.
1204 * @n: another type * to use as temporary storage
1205 * @head: the head for your list.
1206 * @member: the name of the list_struct within the struct.
1208 * Iterate over list of given type from current point, safe against
1209 * removal of list entry.
1211 #define list_for_each_entry_safe_from(pos, n, head, member) \
1212 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1213 &pos->member != (head); \
1214 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1217 * list_for_each_entry_safe_reverse
1218 * @pos: the type * to use as a loop cursor.
1219 * @n: another type * to use as temporary storage
1220 * @head: the head for your list.
1221 * @member: the name of the list_struct within the struct.
1223 * Iterate backwards over list of given type, safe against removal
1226 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1227 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1228 n = list_entry(pos->member.prev, typeof(*pos), member); \
1229 &pos->member != (head); \
1230 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1235 * Double linked lists with a single pointer list head.
1236 * Mostly useful for hash tables where the two pointer list head is
1238 * You lose the ability to access the tail in O(1).
1242 struct hlist_node
*first
;
1246 struct hlist_node
*next
, **pprev
;
1249 #define HLIST_HEAD_INIT { .first = NULL }
1250 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1251 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1252 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
1258 static inline int hlist_unhashed(const struct hlist_node
*h
)
1263 static inline int hlist_empty(const struct hlist_head
*h
)
1268 static inline void __hlist_del(struct hlist_node
*n
)
1270 struct hlist_node
*next
= n
->next
;
1271 struct hlist_node
**pprev
= n
->pprev
;
1274 next
->pprev
= pprev
;
1277 static inline void hlist_del(struct hlist_node
*n
)
1280 n
->next
= LIST_POISON1
;
1281 n
->pprev
= LIST_POISON2
;
1284 static inline void hlist_del_init(struct hlist_node
*n
)
1286 if (!hlist_unhashed(n
)) {
1292 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
1294 struct hlist_node
*first
= h
->first
;
1297 first
->pprev
= &n
->next
;
1299 n
->pprev
= &h
->first
;
1302 /* next must be != NULL */
1303 static inline void hlist_add_before(struct hlist_node
*n
,
1304 struct hlist_node
*next
)
1306 n
->pprev
= next
->pprev
;
1308 next
->pprev
= &n
->next
;
1312 static inline void hlist_add_after(struct hlist_node
*n
,
1313 struct hlist_node
*next
)
1315 next
->next
= n
->next
;
1317 next
->pprev
= &n
->next
;
1320 next
->next
->pprev
= &next
->next
;
1324 * Move a list from one list head to another. Fixup the pprev
1325 * reference of the first entry if it exists.
1327 static inline void hlist_move_list(struct hlist_head
*old
,
1328 struct hlist_head
*new)
1330 new->first
= old
->first
;
1332 new->first
->pprev
= &new->first
;
1336 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1338 #define hlist_for_each(pos, head) \
1339 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1342 #define hlist_for_each_safe(pos, n, head) \
1343 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1347 * hlist_for_each_entry - iterate over list of given type
1348 * @tpos: the type * to use as a loop cursor.
1349 * @pos: the &struct hlist_node to use as a loop cursor.
1350 * @head: the head for your list.
1351 * @member: the name of the hlist_node within the struct.
1353 #define hlist_for_each_entry(tpos, pos, head, member) \
1354 for (pos = (head)->first; \
1355 pos && ({ prefetch(pos->next); 1;}) && \
1356 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1360 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1361 * @tpos: the type * to use as a loop cursor.
1362 * @pos: the &struct hlist_node to use as a loop cursor.
1363 * @member: the name of the hlist_node within the struct.
1365 #define hlist_for_each_entry_continue(tpos, pos, member) \
1366 for (pos = (pos)->next; \
1367 pos && ({ prefetch(pos->next); 1;}) && \
1368 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1372 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1373 * @tpos: the type * to use as a loop cursor.
1374 * @pos: the &struct hlist_node to use as a loop cursor.
1375 * @member: the name of the hlist_node within the struct.
1377 #define hlist_for_each_entry_from(tpos, pos, member) \
1378 for (; pos && ({ prefetch(pos->next); 1;}) && \
1379 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1383 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1384 * @tpos: the type * to use as a loop cursor.
1385 * @pos: the &struct hlist_node to use as a loop cursor.
1386 * @n: another &struct hlist_node to use as temporary storage
1387 * @head: the head for your list.
1388 * @member: the name of the hlist_node within the struct.
1390 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1391 for (pos = (head)->first; \
1392 pos && ({ n = pos->next; 1; }) && \
1393 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \