1 /* MECHANICALLY GENERATED, DO NOT EDIT!!! */
6 * common.h: Common Linux kernel-isms.
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.
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.
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.
22 * Copyright (c) 2006 Paul E. McKenney, IBM.
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.
28 #include <urcu/arch.h>
30 #ifndef __always_inline
31 #define __always_inline inline
34 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
35 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
38 # define stringify_in_c(...) __VA_ARGS__
39 # define ASM_CONST(x) x
41 /* This version of stringify will deal with commas... */
42 # define __stringify_in_c(...) #__VA_ARGS__
43 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
44 # define __ASM_CONST(x) x##UL
45 # define ASM_CONST(x) __ASM_CONST(x)
50 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
52 * This program is free software; you can redistribute it and/or modify
53 * it under the terms of the GNU General Public License as published by
54 * the Free Software Foundation, but version 2 only due to inclusion
55 * of Linux-kernel code.
57 * This program is distributed in the hope that it will be useful,
58 * but WITHOUT ANY WARRANTY; without even the implied warranty of
59 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 * GNU General Public License for more details.
62 * You should have received a copy of the GNU General Public License
63 * along with this program; if not, write to the Free Software
64 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
66 * Copyright (c) 2006 Paul E. McKenney, IBM.
68 * Much code taken from the Linux kernel. For such code, the option
69 * to redistribute under later versions of GPL might not be available.
76 /* #define CACHE_LINE_SIZE 64 */
77 #define ____cacheline_internodealigned_in_smp \
78 __attribute__((__aligned__(1 << 6)))
80 #define LOCK_PREFIX "lock ; "
82 #if 0 /* duplicate with arch_atomic.h */
85 * Atomic data structure, initialization, and access.
88 typedef struct { volatile int counter
; } atomic_t
;
90 #define ATOMIC_INIT(i) { (i) }
92 #define atomic_read(v) ((v)->counter)
93 #define atomic_set(v, i) (((v)->counter) = (i))
100 * atomic_add - add integer to atomic variable
101 * @i: integer value to add
102 * @v: pointer of type atomic_t
104 * Atomically adds @i to @v.
106 static __inline__
void atomic_add(int i
, atomic_t
*v
)
108 __asm__
__volatile__(
109 LOCK_PREFIX
"addl %1,%0"
115 * atomic_sub - subtract the atomic variable
116 * @i: integer value to subtract
117 * @v: pointer of type atomic_t
119 * Atomically subtracts @i from @v.
121 static __inline__
void atomic_sub(int i
, atomic_t
*v
)
123 __asm__
__volatile__(
124 LOCK_PREFIX
"subl %1,%0"
130 * atomic_sub_and_test - subtract value from variable and test result
131 * @i: integer value to subtract
132 * @v: pointer of type atomic_t
134 * Atomically subtracts @i from @v and returns
135 * true if the result is zero, or false for all
138 static __inline__
int atomic_sub_and_test(int i
, atomic_t
*v
)
142 __asm__
__volatile__(
143 LOCK_PREFIX
"subl %2,%0; sete %1"
144 :"+m" (v
->counter
), "=qm" (c
)
145 :"ir" (i
) : "memory");
150 * atomic_inc - increment atomic variable
151 * @v: pointer of type atomic_t
153 * Atomically increments @v by 1.
155 static __inline__
void atomic_inc(atomic_t
*v
)
157 __asm__
__volatile__(
158 LOCK_PREFIX
"incl %0"
163 * atomic_dec - decrement atomic variable
164 * @v: pointer of type atomic_t
166 * Atomically decrements @v by 1.
168 static __inline__
void atomic_dec(atomic_t
*v
)
170 __asm__
__volatile__(
171 LOCK_PREFIX
"decl %0"
176 * atomic_dec_and_test - decrement and test
177 * @v: pointer of type atomic_t
179 * Atomically decrements @v by 1 and
180 * returns true if the result is 0, or false for all other
183 static __inline__
int atomic_dec_and_test(atomic_t
*v
)
187 __asm__
__volatile__(
188 LOCK_PREFIX
"decl %0; sete %1"
189 :"+m" (v
->counter
), "=qm" (c
)
195 * atomic_inc_and_test - increment and test
196 * @v: pointer of type atomic_t
198 * Atomically increments @v by 1
199 * and returns true if the result is zero, or false for all
202 static __inline__
int atomic_inc_and_test(atomic_t
*v
)
206 __asm__
__volatile__(
207 LOCK_PREFIX
"incl %0; sete %1"
208 :"+m" (v
->counter
), "=qm" (c
)
214 * atomic_add_negative - add and test if negative
215 * @v: pointer of type atomic_t
216 * @i: integer value to add
218 * Atomically adds @i to @v and returns true
219 * if the result is negative, or false when
220 * result is greater than or equal to zero.
222 static __inline__
int atomic_add_negative(int i
, atomic_t
*v
)
226 __asm__
__volatile__(
227 LOCK_PREFIX
"addl %2,%0; sets %1"
228 :"+m" (v
->counter
), "=qm" (c
)
229 :"ir" (i
) : "memory");
234 * atomic_add_return - add and return
235 * @v: pointer of type atomic_t
236 * @i: integer value to add
238 * Atomically adds @i to @v and returns @i + @v
240 static __inline__
int atomic_add_return(int i
, atomic_t
*v
)
245 __asm__
__volatile__(
246 LOCK_PREFIX
"xaddl %0, %1;"
248 :"m"(v
->counter
), "0"(i
));
252 static __inline__
int atomic_sub_return(int i
, atomic_t
*v
)
254 return atomic_add_return(-i
,v
);
257 static inline unsigned int
258 cmpxchg(volatile long *ptr
, long oldval
, long newval
)
260 unsigned long retval
;
263 "lock; cmpxchgl %4,(%2)\n"
264 "# end atomic_cmpxchg4"
265 : "=a" (retval
), "=m" (*ptr
)
266 : "r" (ptr
), "0" (oldval
), "r" (newval
), "m" (*ptr
)
271 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
272 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
275 * atomic_add_unless - add unless the number is a given value
276 * @v: pointer of type atomic_t
277 * @a: the amount to add to v...
278 * @u: ...unless v is equal to u.
280 * Atomically adds @a to @v, so long as it was not @u.
281 * Returns non-zero if @v was not @u, and zero otherwise.
283 #define atomic_add_unless(v, a, u) \
286 c = atomic_read(v); \
288 if (unlikely(c == (u))) \
290 old = atomic_cmpxchg((v), c, c + (a)); \
291 if (likely(old == c)) \
297 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
299 #define atomic_inc_return(v) (atomic_add_return(1,v))
300 #define atomic_dec_return(v) (atomic_sub_return(1,v))
302 /* These are x86-specific, used by some header files */
303 #define atomic_clear_mask(mask, addr) \
304 __asm__ __volatile__(LOCK_PREFIX "andl %0,%1" \
305 : : "r" (~(mask)),"m" (*addr) : "memory")
307 #define atomic_set_mask(mask, addr) \
308 __asm__ __volatile__(LOCK_PREFIX "orl %0,%1" \
309 : : "r" (mask),"m" (*(addr)) : "memory")
311 /* Atomic operations are already serializing on x86 */
312 #define smp_mb__before_atomic_dec() barrier()
313 #define smp_mb__after_atomic_dec() barrier()
314 #define smp_mb__before_atomic_inc() barrier()
315 #define smp_mb__after_atomic_inc() barrier()
320 * api_pthreads.h: API mapping to pthreads environment.
322 * This program is free software; you can redistribute it and/or modify
323 * it under the terms of the GNU General Public License as published by
324 * the Free Software Foundation; either version 2 of the License, or
325 * (at your option) any later version. However, please note that much
326 * of the code in this file derives from the Linux kernel, and that such
327 * code may not be available except under GPLv2.
329 * This program is distributed in the hope that it will be useful,
330 * but WITHOUT ANY WARRANTY; without even the implied warranty of
331 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
332 * GNU General Public License for more details.
334 * You should have received a copy of the GNU General Public License
335 * along with this program; if not, write to the Free Software
336 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
338 * Copyright (c) 2006 Paul E. McKenney, IBM.
345 #include <sys/types.h>
349 #include <sys/param.h>
350 /* #include "atomic.h" */
355 #define container_of(ptr, type, member) ({ \
356 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
357 (type *)( (char *)__mptr - offsetof(type,member) );})
360 * Default machine parameters.
363 #ifndef CACHE_LINE_SIZE
364 /* #define CACHE_LINE_SIZE 128 */
365 #endif /* #ifndef CACHE_LINE_SIZE */
368 * Exclusive locking primitives.
371 typedef pthread_mutex_t spinlock_t
;
373 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
374 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
376 static void spin_lock_init(spinlock_t
*sp
)
378 if (pthread_mutex_init(sp
, NULL
) != 0) {
379 perror("spin_lock_init:pthread_mutex_init");
384 static void spin_lock(spinlock_t
*sp
)
386 if (pthread_mutex_lock(sp
) != 0) {
387 perror("spin_lock:pthread_mutex_lock");
392 static void spin_unlock(spinlock_t
*sp
)
394 if (pthread_mutex_unlock(sp
) != 0) {
395 perror("spin_unlock:pthread_mutex_unlock");
400 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
401 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
404 * Thread creation/destruction primitives.
407 typedef pthread_t thread_id_t
;
409 #define NR_THREADS 128
411 #define __THREAD_ID_MAP_EMPTY 0
412 #define __THREAD_ID_MAP_WAITING 1
413 thread_id_t __thread_id_map
[NR_THREADS
];
414 spinlock_t __thread_id_map_mutex
;
416 #define for_each_thread(t) \
417 for (t = 0; t < NR_THREADS; t++)
419 #define for_each_running_thread(t) \
420 for (t = 0; t < NR_THREADS; t++) \
421 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
422 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
424 pthread_key_t thread_id_key
;
426 static int __smp_thread_id(void)
429 thread_id_t tid
= pthread_self();
431 for (i
= 0; i
< NR_THREADS
; i
++) {
432 if (__thread_id_map
[i
] == tid
) {
433 long v
= i
+ 1; /* must be non-NULL. */
435 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
436 perror("pthread_setspecific");
442 spin_lock(&__thread_id_map_mutex
);
443 for (i
= 0; i
< NR_THREADS
; i
++) {
444 if (__thread_id_map
[i
] == tid
)
445 spin_unlock(&__thread_id_map_mutex
);
448 spin_unlock(&__thread_id_map_mutex
);
449 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
454 static int smp_thread_id(void)
458 id
= pthread_getspecific(thread_id_key
);
460 return __smp_thread_id();
461 return (long)(id
- 1);
464 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
469 spin_lock(&__thread_id_map_mutex
);
470 for (i
= 0; i
< NR_THREADS
; i
++) {
471 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
474 if (i
>= NR_THREADS
) {
475 spin_unlock(&__thread_id_map_mutex
);
476 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
479 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
480 spin_unlock(&__thread_id_map_mutex
);
481 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
482 perror("create_thread:pthread_create");
485 __thread_id_map
[i
] = tid
;
489 static void *wait_thread(thread_id_t tid
)
494 for (i
= 0; i
< NR_THREADS
; i
++) {
495 if (__thread_id_map
[i
] == tid
)
498 if (i
>= NR_THREADS
){
499 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
503 if (pthread_join(tid
, &vp
) != 0) {
504 perror("wait_thread:pthread_join");
507 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
511 static void wait_all_threads(void)
516 for (i
= 1; i
< NR_THREADS
; i
++) {
517 tid
= __thread_id_map
[i
];
518 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
519 tid
!= __THREAD_ID_MAP_WAITING
)
520 (void)wait_thread(tid
);
524 static void run_on(int cpu
)
530 sched_setaffinity(0, sizeof(mask
), &mask
);
534 * timekeeping -- very crude -- should use MONOTONIC...
537 long long get_microseconds(void)
541 if (gettimeofday(&tv
, NULL
) != 0)
543 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
547 * Per-thread variables.
550 #define DEFINE_PER_THREAD(type, name) \
553 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
554 } __per_thread_##name[NR_THREADS];
555 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
557 #define per_thread(name, thread) __per_thread_##name[thread].v
558 #define __get_thread_var(name) per_thread(name, smp_thread_id())
560 #define init_per_thread(name, v) \
563 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
564 per_thread(name, __i_p_t_i) = v; \
568 * CPU traversal primitives.
573 #endif /* #ifndef NR_CPUS */
575 #define for_each_possible_cpu(cpu) \
576 for (cpu = 0; cpu < NR_CPUS; cpu++)
577 #define for_each_online_cpu(cpu) \
578 for (cpu = 0; cpu < NR_CPUS; cpu++)
584 #define DEFINE_PER_CPU(type, name) \
587 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
588 } __per_cpu_##name[NR_CPUS]
589 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
591 DEFINE_PER_THREAD(int, smp_processor_id
);
593 #define per_cpu(name, thread) __per_cpu_##name[thread].v
594 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
596 #define init_per_cpu(name, v) \
599 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
600 per_cpu(name, __i_p_c_i) = v; \
604 * CPU state checking (crowbarred).
607 #define idle_cpu(cpu) 0
608 #define in_softirq() 1
609 #define hardirq_count() 0
610 #define PREEMPT_SHIFT 0
611 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
612 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
613 #define PREEMPT_BITS 8
614 #define SOFTIRQ_BITS 8
620 struct notifier_block
{
621 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
622 struct notifier_block
*next
;
626 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
627 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
628 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
629 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
630 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
631 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
632 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
633 * not handling interrupts, soon dead */
634 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
637 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
638 * operation in progress
640 #define CPU_TASKS_FROZEN 0x0010
642 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
643 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
644 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
645 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
646 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
647 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
648 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
650 /* Hibernation and suspend events */
651 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
652 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
653 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
654 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
655 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
656 #define PM_POST_RESTORE 0x0006 /* Restore failed */
658 #define NOTIFY_DONE 0x0000 /* Don't care */
659 #define NOTIFY_OK 0x0001 /* Suits me */
660 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
661 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
662 /* Bad/Veto action */
664 * Clean way to return from the notifier and stop further calls.
666 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
672 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
675 * Initialization -- Must be called before calling any primitives.
678 static void smp_init(void)
682 spin_lock_init(&__thread_id_map_mutex
);
683 __thread_id_map
[0] = pthread_self();
684 for (i
= 1; i
< NR_THREADS
; i
++)
685 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
686 init_per_thread(smp_processor_id
, 0);
687 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
688 perror("pthread_key_create");
693 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
695 #ifndef _LINUX_LIST_H
696 #define _LINUX_LIST_H
698 #define LIST_POISON1 ((void *) 0x00100100)
699 #define LIST_POISON2 ((void *) 0x00200200)
701 #define container_of(ptr, type, member) ({ \
702 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
703 (type *)( (char *)__mptr - offsetof(type,member) );})
708 * Simple doubly linked list implementation.
710 * Some of the internal functions ("__xxx") are useful when
711 * manipulating whole lists rather than single entries, as
712 * sometimes we already know the next/prev entries and we can
713 * generate better code by using them directly rather than
714 * using the generic single-entry routines.
718 struct list_head
*next
, *prev
;
721 #define LIST_HEAD_INIT(name) { &(name), &(name) }
723 #define LIST_HEAD(name) \
724 struct list_head name = LIST_HEAD_INIT(name)
726 static inline void INIT_LIST_HEAD(struct list_head
*list
)
733 * Insert a new entry between two known consecutive entries.
735 * This is only for internal list manipulation where we know
736 * the prev/next entries already!
738 #ifndef CONFIG_DEBUG_LIST
739 static inline void __list_add(struct list_head
*new,
740 struct list_head
*prev
,
741 struct list_head
*next
)
749 extern void __list_add(struct list_head
*new,
750 struct list_head
*prev
,
751 struct list_head
*next
);
755 * list_add - add a new entry
756 * @new: new entry to be added
757 * @head: list head to add it after
759 * Insert a new entry after the specified head.
760 * This is good for implementing stacks.
762 static inline void list_add(struct list_head
*new, struct list_head
*head
)
764 __list_add(new, head
, head
->next
);
769 * list_add_tail - add a new entry
770 * @new: new entry to be added
771 * @head: list head to add it before
773 * Insert a new entry before the specified head.
774 * This is useful for implementing queues.
776 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
778 __list_add(new, head
->prev
, head
);
782 * Delete a list entry by making the prev/next entries
783 * point to each other.
785 * This is only for internal list manipulation where we know
786 * the prev/next entries already!
788 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
795 * list_del - deletes entry from list.
796 * @entry: the element to delete from the list.
797 * Note: list_empty() on entry does not return true after this, the entry is
798 * in an undefined state.
800 #ifndef CONFIG_DEBUG_LIST
801 static inline void list_del(struct list_head
*entry
)
803 __list_del(entry
->prev
, entry
->next
);
804 entry
->next
= LIST_POISON1
;
805 entry
->prev
= LIST_POISON2
;
808 extern void list_del(struct list_head
*entry
);
812 * list_replace - replace old entry by new one
813 * @old : the element to be replaced
814 * @new : the new element to insert
816 * If @old was empty, it will be overwritten.
818 static inline void list_replace(struct list_head
*old
,
819 struct list_head
*new)
821 new->next
= old
->next
;
822 new->next
->prev
= new;
823 new->prev
= old
->prev
;
824 new->prev
->next
= new;
827 static inline void list_replace_init(struct list_head
*old
,
828 struct list_head
*new)
830 list_replace(old
, new);
835 * list_del_init - deletes entry from list and reinitialize it.
836 * @entry: the element to delete from the list.
838 static inline void list_del_init(struct list_head
*entry
)
840 __list_del(entry
->prev
, entry
->next
);
841 INIT_LIST_HEAD(entry
);
845 * list_move - delete from one list and add as another's head
846 * @list: the entry to move
847 * @head: the head that will precede our entry
849 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
851 __list_del(list
->prev
, list
->next
);
852 list_add(list
, head
);
856 * list_move_tail - delete from one list and add as another's tail
857 * @list: the entry to move
858 * @head: the head that will follow our entry
860 static inline void list_move_tail(struct list_head
*list
,
861 struct list_head
*head
)
863 __list_del(list
->prev
, list
->next
);
864 list_add_tail(list
, head
);
868 * list_is_last - tests whether @list is the last entry in list @head
869 * @list: the entry to test
870 * @head: the head of the list
872 static inline int list_is_last(const struct list_head
*list
,
873 const struct list_head
*head
)
875 return list
->next
== head
;
879 * list_empty - tests whether a list is empty
880 * @head: the list to test.
882 static inline int list_empty(const struct list_head
*head
)
884 return head
->next
== head
;
888 * list_empty_careful - tests whether a list is empty and not being modified
889 * @head: the list to test
892 * tests whether a list is empty _and_ checks that no other CPU might be
893 * in the process of modifying either member (next or prev)
895 * NOTE: using list_empty_careful() without synchronization
896 * can only be safe if the only activity that can happen
897 * to the list entry is list_del_init(). Eg. it cannot be used
898 * if another CPU could re-list_add() it.
900 static inline int list_empty_careful(const struct list_head
*head
)
902 struct list_head
*next
= head
->next
;
903 return (next
== head
) && (next
== head
->prev
);
907 * list_is_singular - tests whether a list has just one entry.
908 * @head: the list to test.
910 static inline int list_is_singular(const struct list_head
*head
)
912 return !list_empty(head
) && (head
->next
== head
->prev
);
915 static inline void __list_cut_position(struct list_head
*list
,
916 struct list_head
*head
, struct list_head
*entry
)
918 struct list_head
*new_first
= entry
->next
;
919 list
->next
= head
->next
;
920 list
->next
->prev
= list
;
923 head
->next
= new_first
;
924 new_first
->prev
= head
;
928 * list_cut_position - cut a list into two
929 * @list: a new list to add all removed entries
930 * @head: a list with entries
931 * @entry: an entry within head, could be the head itself
932 * and if so we won't cut the list
934 * This helper moves the initial part of @head, up to and
935 * including @entry, from @head to @list. You should
936 * pass on @entry an element you know is on @head. @list
937 * should be an empty list or a list you do not care about
941 static inline void list_cut_position(struct list_head
*list
,
942 struct list_head
*head
, struct list_head
*entry
)
944 if (list_empty(head
))
946 if (list_is_singular(head
) &&
947 (head
->next
!= entry
&& head
!= entry
))
950 INIT_LIST_HEAD(list
);
952 __list_cut_position(list
, head
, entry
);
955 static inline void __list_splice(const struct list_head
*list
,
956 struct list_head
*prev
,
957 struct list_head
*next
)
959 struct list_head
*first
= list
->next
;
960 struct list_head
*last
= list
->prev
;
970 * list_splice - join two lists, this is designed for stacks
971 * @list: the new list to add.
972 * @head: the place to add it in the first list.
974 static inline void list_splice(const struct list_head
*list
,
975 struct list_head
*head
)
977 if (!list_empty(list
))
978 __list_splice(list
, head
, head
->next
);
982 * list_splice_tail - join two lists, each list being a queue
983 * @list: the new list to add.
984 * @head: the place to add it in the first list.
986 static inline void list_splice_tail(struct list_head
*list
,
987 struct list_head
*head
)
989 if (!list_empty(list
))
990 __list_splice(list
, head
->prev
, head
);
994 * list_splice_init - join two lists and reinitialise the emptied list.
995 * @list: the new list to add.
996 * @head: the place to add it in the first list.
998 * The list at @list is reinitialised
1000 static inline void list_splice_init(struct list_head
*list
,
1001 struct list_head
*head
)
1003 if (!list_empty(list
)) {
1004 __list_splice(list
, head
, head
->next
);
1005 INIT_LIST_HEAD(list
);
1010 * list_splice_tail_init - join two lists and reinitialise the emptied list
1011 * @list: the new list to add.
1012 * @head: the place to add it in the first list.
1014 * Each of the lists is a queue.
1015 * The list at @list is reinitialised
1017 static inline void list_splice_tail_init(struct list_head
*list
,
1018 struct list_head
*head
)
1020 if (!list_empty(list
)) {
1021 __list_splice(list
, head
->prev
, head
);
1022 INIT_LIST_HEAD(list
);
1027 * list_entry - get the struct for this entry
1028 * @ptr: the &struct list_head pointer.
1029 * @type: the type of the struct this is embedded in.
1030 * @member: the name of the list_struct within the struct.
1032 #define list_entry(ptr, type, member) \
1033 container_of(ptr, type, member)
1036 * list_first_entry - get the first element from a list
1037 * @ptr: the list head to take the element from.
1038 * @type: the type of the struct this is embedded in.
1039 * @member: the name of the list_struct within the struct.
1041 * Note, that list is expected to be not empty.
1043 #define list_first_entry(ptr, type, member) \
1044 list_entry((ptr)->next, type, member)
1047 * list_for_each - iterate over a list
1048 * @pos: the &struct list_head to use as a loop cursor.
1049 * @head: the head for your list.
1051 #define list_for_each(pos, head) \
1052 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
1056 * __list_for_each - iterate over a list
1057 * @pos: the &struct list_head to use as a loop cursor.
1058 * @head: the head for your list.
1060 * This variant differs from list_for_each() in that it's the
1061 * simplest possible list iteration code, no prefetching is done.
1062 * Use this for code that knows the list to be very short (empty
1063 * or 1 entry) most of the time.
1065 #define __list_for_each(pos, head) \
1066 for (pos = (head)->next; pos != (head); pos = pos->next)
1069 * list_for_each_prev - iterate over a list backwards
1070 * @pos: the &struct list_head to use as a loop cursor.
1071 * @head: the head for your list.
1073 #define list_for_each_prev(pos, head) \
1074 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1078 * list_for_each_safe - iterate over a list safe against removal of list entry
1079 * @pos: the &struct list_head to use as a loop cursor.
1080 * @n: another &struct list_head to use as temporary storage
1081 * @head: the head for your list.
1083 #define list_for_each_safe(pos, n, head) \
1084 for (pos = (head)->next, n = pos->next; pos != (head); \
1085 pos = n, n = pos->next)
1088 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1089 * @pos: the &struct list_head to use as a loop cursor.
1090 * @n: another &struct list_head to use as temporary storage
1091 * @head: the head for your list.
1093 #define list_for_each_prev_safe(pos, n, head) \
1094 for (pos = (head)->prev, n = pos->prev; \
1095 prefetch(pos->prev), pos != (head); \
1096 pos = n, n = pos->prev)
1099 * list_for_each_entry - iterate over list of given type
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.
1104 #define list_for_each_entry(pos, head, member) \
1105 for (pos = list_entry((head)->next, typeof(*pos), member); \
1106 prefetch(pos->member.next), &pos->member != (head); \
1107 pos = list_entry(pos->member.next, typeof(*pos), member))
1110 * list_for_each_entry_reverse - iterate backwards over list of given type.
1111 * @pos: the type * to use as a loop cursor.
1112 * @head: the head for your list.
1113 * @member: the name of the list_struct within the struct.
1115 #define list_for_each_entry_reverse(pos, head, member) \
1116 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1117 prefetch(pos->member.prev), &pos->member != (head); \
1118 pos = list_entry(pos->member.prev, typeof(*pos), member))
1121 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1122 * @pos: the type * to use as a start point
1123 * @head: the head of the list
1124 * @member: the name of the list_struct within the struct.
1126 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1128 #define list_prepare_entry(pos, head, member) \
1129 ((pos) ? : list_entry(head, typeof(*pos), member))
1132 * list_for_each_entry_continue - continue iteration over list of given type
1133 * @pos: the type * to use as a loop cursor.
1134 * @head: the head for your list.
1135 * @member: the name of the list_struct within the struct.
1137 * Continue to iterate over list of given type, continuing after
1138 * the current position.
1140 #define list_for_each_entry_continue(pos, head, member) \
1141 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1142 prefetch(pos->member.next), &pos->member != (head); \
1143 pos = list_entry(pos->member.next, typeof(*pos), member))
1146 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1147 * @pos: the type * to use as a loop cursor.
1148 * @head: the head for your list.
1149 * @member: the name of the list_struct within the struct.
1151 * Start to iterate over list of given type backwards, continuing after
1152 * the current position.
1154 #define list_for_each_entry_continue_reverse(pos, head, member) \
1155 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1156 prefetch(pos->member.prev), &pos->member != (head); \
1157 pos = list_entry(pos->member.prev, typeof(*pos), member))
1160 * list_for_each_entry_from - iterate over list of given type from the current point
1161 * @pos: the type * to use as a loop cursor.
1162 * @head: the head for your list.
1163 * @member: the name of the list_struct within the struct.
1165 * Iterate over list of given type, continuing from current position.
1167 #define list_for_each_entry_from(pos, head, member) \
1168 for (; prefetch(pos->member.next), &pos->member != (head); \
1169 pos = list_entry(pos->member.next, typeof(*pos), member))
1172 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1173 * @pos: the type * to use as a loop cursor.
1174 * @n: another type * to use as temporary storage
1175 * @head: the head for your list.
1176 * @member: the name of the list_struct within the struct.
1178 #define list_for_each_entry_safe(pos, n, head, member) \
1179 for (pos = list_entry((head)->next, typeof(*pos), member), \
1180 n = list_entry(pos->member.next, typeof(*pos), member); \
1181 &pos->member != (head); \
1182 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1185 * list_for_each_entry_safe_continue
1186 * @pos: the type * to use as a loop cursor.
1187 * @n: another type * to use as temporary storage
1188 * @head: the head for your list.
1189 * @member: the name of the list_struct within the struct.
1191 * Iterate over list of given type, continuing after current point,
1192 * safe against removal of list entry.
1194 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1195 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1196 n = list_entry(pos->member.next, typeof(*pos), member); \
1197 &pos->member != (head); \
1198 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1201 * list_for_each_entry_safe_from
1202 * @pos: the type * to use as a loop cursor.
1203 * @n: another type * to use as temporary storage
1204 * @head: the head for your list.
1205 * @member: the name of the list_struct within the struct.
1207 * Iterate over list of given type from current point, safe against
1208 * removal of list entry.
1210 #define list_for_each_entry_safe_from(pos, n, head, member) \
1211 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1212 &pos->member != (head); \
1213 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1216 * list_for_each_entry_safe_reverse
1217 * @pos: the type * to use as a loop cursor.
1218 * @n: another type * to use as temporary storage
1219 * @head: the head for your list.
1220 * @member: the name of the list_struct within the struct.
1222 * Iterate backwards over list of given type, safe against removal
1225 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1226 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1227 n = list_entry(pos->member.prev, typeof(*pos), member); \
1228 &pos->member != (head); \
1229 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1234 * Double linked lists with a single pointer list head.
1235 * Mostly useful for hash tables where the two pointer list head is
1237 * You lose the ability to access the tail in O(1).
1241 struct hlist_node
*first
;
1245 struct hlist_node
*next
, **pprev
;
1248 #define HLIST_HEAD_INIT { .first = NULL }
1249 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1250 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1251 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
1257 static inline int hlist_unhashed(const struct hlist_node
*h
)
1262 static inline int hlist_empty(const struct hlist_head
*h
)
1267 static inline void __hlist_del(struct hlist_node
*n
)
1269 struct hlist_node
*next
= n
->next
;
1270 struct hlist_node
**pprev
= n
->pprev
;
1273 next
->pprev
= pprev
;
1276 static inline void hlist_del(struct hlist_node
*n
)
1279 n
->next
= LIST_POISON1
;
1280 n
->pprev
= LIST_POISON2
;
1283 static inline void hlist_del_init(struct hlist_node
*n
)
1285 if (!hlist_unhashed(n
)) {
1291 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
1293 struct hlist_node
*first
= h
->first
;
1296 first
->pprev
= &n
->next
;
1298 n
->pprev
= &h
->first
;
1301 /* next must be != NULL */
1302 static inline void hlist_add_before(struct hlist_node
*n
,
1303 struct hlist_node
*next
)
1305 n
->pprev
= next
->pprev
;
1307 next
->pprev
= &n
->next
;
1311 static inline void hlist_add_after(struct hlist_node
*n
,
1312 struct hlist_node
*next
)
1314 next
->next
= n
->next
;
1316 next
->pprev
= &n
->next
;
1319 next
->next
->pprev
= &next
->next
;
1323 * Move a list from one list head to another. Fixup the pprev
1324 * reference of the first entry if it exists.
1326 static inline void hlist_move_list(struct hlist_head
*old
,
1327 struct hlist_head
*new)
1329 new->first
= old
->first
;
1331 new->first
->pprev
= &new->first
;
1335 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1337 #define hlist_for_each(pos, head) \
1338 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1341 #define hlist_for_each_safe(pos, n, head) \
1342 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1346 * hlist_for_each_entry - iterate over list of given type
1347 * @tpos: the type * to use as a loop cursor.
1348 * @pos: the &struct hlist_node to use as a loop cursor.
1349 * @head: the head for your list.
1350 * @member: the name of the hlist_node within the struct.
1352 #define hlist_for_each_entry(tpos, pos, head, member) \
1353 for (pos = (head)->first; \
1354 pos && ({ prefetch(pos->next); 1;}) && \
1355 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1359 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1360 * @tpos: the type * to use as a loop cursor.
1361 * @pos: the &struct hlist_node to use as a loop cursor.
1362 * @member: the name of the hlist_node within the struct.
1364 #define hlist_for_each_entry_continue(tpos, pos, member) \
1365 for (pos = (pos)->next; \
1366 pos && ({ prefetch(pos->next); 1;}) && \
1367 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1371 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1372 * @tpos: the type * to use as a loop cursor.
1373 * @pos: the &struct hlist_node to use as a loop cursor.
1374 * @member: the name of the hlist_node within the struct.
1376 #define hlist_for_each_entry_from(tpos, pos, member) \
1377 for (; pos && ({ prefetch(pos->next); 1;}) && \
1378 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1382 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1383 * @tpos: the type * to use as a loop cursor.
1384 * @pos: the &struct hlist_node to use as a loop cursor.
1385 * @n: another &struct hlist_node to use as temporary storage
1386 * @head: the head for your list.
1387 * @member: the name of the hlist_node within the struct.
1389 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1390 for (pos = (head)->first; \
1391 pos && ({ n = pos->next; 1; }) && \
1392 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \