1 /* MECHANICALLY GENERATED, DO NOT EDIT!!! */
9 * common.h: Common Linux kernel-isms.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; but version 2 of the License only due
14 * to code included from the Linux kernel.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Copyright (c) 2006 Paul E. McKenney, IBM.
27 * Much code taken from the Linux kernel. For such code, the option
28 * to redistribute under later versions of GPL might not be available.
31 #include <urcu/arch.h>
33 #ifndef __always_inline
34 #define __always_inline inline
37 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
38 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
41 # define stringify_in_c(...) __VA_ARGS__
42 # define ASM_CONST(x) x
44 /* This version of stringify will deal with commas... */
45 # define __stringify_in_c(...) #__VA_ARGS__
46 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
47 # define __ASM_CONST(x) x##UL
48 # define ASM_CONST(x) __ASM_CONST(x)
53 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
55 * This program is free software; you can redistribute it and/or modify
56 * it under the terms of the GNU General Public License as published by
57 * the Free Software Foundation, but version 2 only due to inclusion
58 * of Linux-kernel code.
60 * This program is distributed in the hope that it will be useful,
61 * but WITHOUT ANY WARRANTY; without even the implied warranty of
62 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 * GNU General Public License for more details.
65 * You should have received a copy of the GNU General Public License
66 * along with this program; if not, write to the Free Software
67 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
69 * Copyright (c) 2006 Paul E. McKenney, IBM.
71 * Much code taken from the Linux kernel. For such code, the option
72 * to redistribute under later versions of GPL might not be available.
79 /* #define CACHE_LINE_SIZE 64 */
80 #define ____cacheline_internodealigned_in_smp \
81 __attribute__((__aligned__(1 << 6)))
83 #define LOCK_PREFIX "lock ; "
85 #if 0 /* duplicate with arch_atomic.h */
88 * Atomic data structure, initialization, and access.
91 typedef struct { volatile int counter
; } atomic_t
;
93 #define ATOMIC_INIT(i) { (i) }
95 #define atomic_read(v) ((v)->counter)
96 #define atomic_set(v, i) (((v)->counter) = (i))
103 * atomic_add - add integer to atomic variable
104 * @i: integer value to add
105 * @v: pointer of type atomic_t
107 * Atomically adds @i to @v.
109 static __inline__
void atomic_add(int i
, atomic_t
*v
)
111 __asm__
__volatile__(
112 LOCK_PREFIX
"addl %1,%0"
118 * atomic_sub - subtract the atomic variable
119 * @i: integer value to subtract
120 * @v: pointer of type atomic_t
122 * Atomically subtracts @i from @v.
124 static __inline__
void atomic_sub(int i
, atomic_t
*v
)
126 __asm__
__volatile__(
127 LOCK_PREFIX
"subl %1,%0"
133 * atomic_sub_and_test - subtract value from variable and test result
134 * @i: integer value to subtract
135 * @v: pointer of type atomic_t
137 * Atomically subtracts @i from @v and returns
138 * true if the result is zero, or false for all
141 static __inline__
int atomic_sub_and_test(int i
, atomic_t
*v
)
145 __asm__
__volatile__(
146 LOCK_PREFIX
"subl %2,%0; sete %1"
147 :"+m" (v
->counter
), "=qm" (c
)
148 :"ir" (i
) : "memory");
153 * atomic_inc - increment atomic variable
154 * @v: pointer of type atomic_t
156 * Atomically increments @v by 1.
158 static __inline__
void atomic_inc(atomic_t
*v
)
160 __asm__
__volatile__(
161 LOCK_PREFIX
"incl %0"
166 * atomic_dec - decrement atomic variable
167 * @v: pointer of type atomic_t
169 * Atomically decrements @v by 1.
171 static __inline__
void atomic_dec(atomic_t
*v
)
173 __asm__
__volatile__(
174 LOCK_PREFIX
"decl %0"
179 * atomic_dec_and_test - decrement and test
180 * @v: pointer of type atomic_t
182 * Atomically decrements @v by 1 and
183 * returns true if the result is 0, or false for all other
186 static __inline__
int atomic_dec_and_test(atomic_t
*v
)
190 __asm__
__volatile__(
191 LOCK_PREFIX
"decl %0; sete %1"
192 :"+m" (v
->counter
), "=qm" (c
)
198 * atomic_inc_and_test - increment and test
199 * @v: pointer of type atomic_t
201 * Atomically increments @v by 1
202 * and returns true if the result is zero, or false for all
205 static __inline__
int atomic_inc_and_test(atomic_t
*v
)
209 __asm__
__volatile__(
210 LOCK_PREFIX
"incl %0; sete %1"
211 :"+m" (v
->counter
), "=qm" (c
)
217 * atomic_add_negative - add and test if negative
218 * @v: pointer of type atomic_t
219 * @i: integer value to add
221 * Atomically adds @i to @v and returns true
222 * if the result is negative, or false when
223 * result is greater than or equal to zero.
225 static __inline__
int atomic_add_negative(int i
, atomic_t
*v
)
229 __asm__
__volatile__(
230 LOCK_PREFIX
"addl %2,%0; sets %1"
231 :"+m" (v
->counter
), "=qm" (c
)
232 :"ir" (i
) : "memory");
237 * atomic_add_return - add and return
238 * @v: pointer of type atomic_t
239 * @i: integer value to add
241 * Atomically adds @i to @v and returns @i + @v
243 static __inline__
int atomic_add_return(int i
, atomic_t
*v
)
248 __asm__
__volatile__(
249 LOCK_PREFIX
"xaddl %0, %1;"
251 :"m"(v
->counter
), "0"(i
));
255 static __inline__
int atomic_sub_return(int i
, atomic_t
*v
)
257 return atomic_add_return(-i
,v
);
260 static inline unsigned int
261 cmpxchg(volatile long *ptr
, long oldval
, long newval
)
263 unsigned long retval
;
266 "lock; cmpxchgl %4,(%2)\n"
267 "# end atomic_cmpxchg4"
268 : "=a" (retval
), "=m" (*ptr
)
269 : "r" (ptr
), "0" (oldval
), "r" (newval
), "m" (*ptr
)
274 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
275 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
278 * atomic_add_unless - add unless the number is a given value
279 * @v: pointer of type atomic_t
280 * @a: the amount to add to v...
281 * @u: ...unless v is equal to u.
283 * Atomically adds @a to @v, so long as it was not @u.
284 * Returns non-zero if @v was not @u, and zero otherwise.
286 #define atomic_add_unless(v, a, u) \
289 c = atomic_read(v); \
291 if (unlikely(c == (u))) \
293 old = atomic_cmpxchg((v), c, c + (a)); \
294 if (likely(old == c)) \
300 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
302 #define atomic_inc_return(v) (atomic_add_return(1,v))
303 #define atomic_dec_return(v) (atomic_sub_return(1,v))
305 /* These are x86-specific, used by some header files */
306 #define atomic_clear_mask(mask, addr) \
307 __asm__ __volatile__(LOCK_PREFIX "andl %0,%1" \
308 : : "r" (~(mask)),"m" (*addr) : "memory")
310 #define atomic_set_mask(mask, addr) \
311 __asm__ __volatile__(LOCK_PREFIX "orl %0,%1" \
312 : : "r" (mask),"m" (*(addr)) : "memory")
314 /* Atomic operations are already serializing on x86 */
315 #define smp_mb__before_atomic_dec() barrier()
316 #define smp_mb__after_atomic_dec() barrier()
317 #define smp_mb__before_atomic_inc() barrier()
318 #define smp_mb__after_atomic_inc() barrier()
323 * api_pthreads.h: API mapping to pthreads environment.
325 * This program is free software; you can redistribute it and/or modify
326 * it under the terms of the GNU General Public License as published by
327 * the Free Software Foundation; either version 2 of the License, or
328 * (at your option) any later version. However, please note that much
329 * of the code in this file derives from the Linux kernel, and that such
330 * code may not be available except under GPLv2.
332 * This program is distributed in the hope that it will be useful,
333 * but WITHOUT ANY WARRANTY; without even the implied warranty of
334 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
335 * GNU General Public License for more details.
337 * You should have received a copy of the GNU General Public License
338 * along with this program; if not, write to the Free Software
339 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
341 * Copyright (c) 2006 Paul E. McKenney, IBM.
348 #include <sys/types.h>
352 #include <sys/param.h>
353 /* #include "atomic.h" */
358 #define container_of(ptr, type, member) ({ \
359 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
360 (type *)( (char *)__mptr - offsetof(type,member) );})
363 * Default machine parameters.
366 #ifndef CACHE_LINE_SIZE
367 /* #define CACHE_LINE_SIZE 128 */
368 #endif /* #ifndef CACHE_LINE_SIZE */
371 * Exclusive locking primitives.
374 typedef pthread_mutex_t spinlock_t
;
376 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
377 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
379 static void spin_lock_init(spinlock_t
*sp
)
381 if (pthread_mutex_init(sp
, NULL
) != 0) {
382 perror("spin_lock_init:pthread_mutex_init");
387 static void spin_lock(spinlock_t
*sp
)
389 if (pthread_mutex_lock(sp
) != 0) {
390 perror("spin_lock:pthread_mutex_lock");
395 static void spin_unlock(spinlock_t
*sp
)
397 if (pthread_mutex_unlock(sp
) != 0) {
398 perror("spin_unlock:pthread_mutex_unlock");
403 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
404 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
407 * Thread creation/destruction primitives.
410 typedef pthread_t thread_id_t
;
412 #define NR_THREADS 128
414 #define __THREAD_ID_MAP_EMPTY 0
415 #define __THREAD_ID_MAP_WAITING 1
416 thread_id_t __thread_id_map
[NR_THREADS
];
417 spinlock_t __thread_id_map_mutex
;
419 #define for_each_thread(t) \
420 for (t = 0; t < NR_THREADS; t++)
422 #define for_each_running_thread(t) \
423 for (t = 0; t < NR_THREADS; t++) \
424 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
425 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
427 pthread_key_t thread_id_key
;
429 static int __smp_thread_id(void)
432 thread_id_t tid
= pthread_self();
434 for (i
= 0; i
< NR_THREADS
; i
++) {
435 if (__thread_id_map
[i
] == tid
) {
436 long v
= i
+ 1; /* must be non-NULL. */
438 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
439 perror("pthread_setspecific");
445 spin_lock(&__thread_id_map_mutex
);
446 for (i
= 0; i
< NR_THREADS
; i
++) {
447 if (__thread_id_map
[i
] == tid
)
448 spin_unlock(&__thread_id_map_mutex
);
451 spin_unlock(&__thread_id_map_mutex
);
452 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
457 static int smp_thread_id(void)
461 id
= pthread_getspecific(thread_id_key
);
463 return __smp_thread_id();
464 return (long)(id
- 1);
467 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
472 spin_lock(&__thread_id_map_mutex
);
473 for (i
= 0; i
< NR_THREADS
; i
++) {
474 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
477 if (i
>= NR_THREADS
) {
478 spin_unlock(&__thread_id_map_mutex
);
479 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
482 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
483 spin_unlock(&__thread_id_map_mutex
);
484 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
485 perror("create_thread:pthread_create");
488 __thread_id_map
[i
] = tid
;
492 static void *wait_thread(thread_id_t tid
)
497 for (i
= 0; i
< NR_THREADS
; i
++) {
498 if (__thread_id_map
[i
] == tid
)
501 if (i
>= NR_THREADS
){
502 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
506 if (pthread_join(tid
, &vp
) != 0) {
507 perror("wait_thread:pthread_join");
510 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
514 static void wait_all_threads(void)
519 for (i
= 1; i
< NR_THREADS
; i
++) {
520 tid
= __thread_id_map
[i
];
521 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
522 tid
!= __THREAD_ID_MAP_WAITING
)
523 (void)wait_thread(tid
);
527 #ifndef HAVE_CPU_SET_T
528 typedef unsigned long cpu_set_t
;
529 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
530 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
533 static void run_on(int cpu
)
535 #if HAVE_SCHED_SETAFFINITY
540 #if SCHED_SETAFFINITY_ARGS == 2
541 sched_setaffinity(0, &mask
);
543 sched_setaffinity(0, sizeof(mask
), &mask
);
545 #endif /* HAVE_SCHED_SETAFFINITY */
549 * timekeeping -- very crude -- should use MONOTONIC...
552 long long get_microseconds(void)
556 if (gettimeofday(&tv
, NULL
) != 0)
558 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
562 * Per-thread variables.
565 #define DEFINE_PER_THREAD(type, name) \
568 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
569 } __per_thread_##name[NR_THREADS];
570 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
572 #define per_thread(name, thread) __per_thread_##name[thread].v
573 #define __get_thread_var(name) per_thread(name, smp_thread_id())
575 #define init_per_thread(name, v) \
578 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
579 per_thread(name, __i_p_t_i) = v; \
583 * CPU traversal primitives.
588 #endif /* #ifndef NR_CPUS */
590 #define for_each_possible_cpu(cpu) \
591 for (cpu = 0; cpu < NR_CPUS; cpu++)
592 #define for_each_online_cpu(cpu) \
593 for (cpu = 0; cpu < NR_CPUS; cpu++)
599 #define DEFINE_PER_CPU(type, name) \
602 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
603 } __per_cpu_##name[NR_CPUS]
604 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
606 DEFINE_PER_THREAD(int, smp_processor_id
);
608 #define per_cpu(name, thread) __per_cpu_##name[thread].v
609 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
611 #define init_per_cpu(name, v) \
614 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
615 per_cpu(name, __i_p_c_i) = v; \
619 * CPU state checking (crowbarred).
622 #define idle_cpu(cpu) 0
623 #define in_softirq() 1
624 #define hardirq_count() 0
625 #define PREEMPT_SHIFT 0
626 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
627 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
628 #define PREEMPT_BITS 8
629 #define SOFTIRQ_BITS 8
635 struct notifier_block
{
636 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
637 struct notifier_block
*next
;
641 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
642 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
643 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
644 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
645 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
646 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
647 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
648 * not handling interrupts, soon dead */
649 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
652 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
653 * operation in progress
655 #define CPU_TASKS_FROZEN 0x0010
657 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
658 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
659 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
660 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
661 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
662 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
663 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
665 /* Hibernation and suspend events */
666 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
667 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
668 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
669 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
670 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
671 #define PM_POST_RESTORE 0x0006 /* Restore failed */
673 #define NOTIFY_DONE 0x0000 /* Don't care */
674 #define NOTIFY_OK 0x0001 /* Suits me */
675 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
676 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
677 /* Bad/Veto action */
679 * Clean way to return from the notifier and stop further calls.
681 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
687 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
690 * Initialization -- Must be called before calling any primitives.
693 static void smp_init(void)
697 spin_lock_init(&__thread_id_map_mutex
);
698 __thread_id_map
[0] = pthread_self();
699 for (i
= 1; i
< NR_THREADS
; i
++)
700 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
701 init_per_thread(smp_processor_id
, 0);
702 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
703 perror("pthread_key_create");
708 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
710 #ifndef _LINUX_LIST_H
711 #define _LINUX_LIST_H
713 #define LIST_POISON1 ((void *) 0x00100100)
714 #define LIST_POISON2 ((void *) 0x00200200)
716 #define container_of(ptr, type, member) ({ \
717 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
718 (type *)( (char *)__mptr - offsetof(type,member) );})
723 * Simple doubly linked list implementation.
725 * Some of the internal functions ("__xxx") are useful when
726 * manipulating whole lists rather than single entries, as
727 * sometimes we already know the next/prev entries and we can
728 * generate better code by using them directly rather than
729 * using the generic single-entry routines.
733 struct list_head
*next
, *prev
;
736 #define LIST_HEAD_INIT(name) { &(name), &(name) }
738 #define LIST_HEAD(name) \
739 struct list_head name = LIST_HEAD_INIT(name)
741 static inline void INIT_LIST_HEAD(struct list_head
*list
)
748 * Insert a new entry between two known consecutive entries.
750 * This is only for internal list manipulation where we know
751 * the prev/next entries already!
753 #ifndef CONFIG_DEBUG_LIST
754 static inline void __list_add(struct list_head
*new,
755 struct list_head
*prev
,
756 struct list_head
*next
)
764 extern void __list_add(struct list_head
*new,
765 struct list_head
*prev
,
766 struct list_head
*next
);
770 * list_add - add a new entry
771 * @new: new entry to be added
772 * @head: list head to add it after
774 * Insert a new entry after the specified head.
775 * This is good for implementing stacks.
777 static inline void list_add(struct list_head
*new, struct list_head
*head
)
779 __list_add(new, head
, head
->next
);
784 * list_add_tail - add a new entry
785 * @new: new entry to be added
786 * @head: list head to add it before
788 * Insert a new entry before the specified head.
789 * This is useful for implementing queues.
791 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
793 __list_add(new, head
->prev
, head
);
797 * Delete a list entry by making the prev/next entries
798 * point to each other.
800 * This is only for internal list manipulation where we know
801 * the prev/next entries already!
803 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
810 * list_del - deletes entry from list.
811 * @entry: the element to delete from the list.
812 * Note: list_empty() on entry does not return true after this, the entry is
813 * in an undefined state.
815 #ifndef CONFIG_DEBUG_LIST
816 static inline void list_del(struct list_head
*entry
)
818 __list_del(entry
->prev
, entry
->next
);
819 entry
->next
= LIST_POISON1
;
820 entry
->prev
= LIST_POISON2
;
823 extern void list_del(struct list_head
*entry
);
827 * list_replace - replace old entry by new one
828 * @old : the element to be replaced
829 * @new : the new element to insert
831 * If @old was empty, it will be overwritten.
833 static inline void list_replace(struct list_head
*old
,
834 struct list_head
*new)
836 new->next
= old
->next
;
837 new->next
->prev
= new;
838 new->prev
= old
->prev
;
839 new->prev
->next
= new;
842 static inline void list_replace_init(struct list_head
*old
,
843 struct list_head
*new)
845 list_replace(old
, new);
850 * list_del_init - deletes entry from list and reinitialize it.
851 * @entry: the element to delete from the list.
853 static inline void list_del_init(struct list_head
*entry
)
855 __list_del(entry
->prev
, entry
->next
);
856 INIT_LIST_HEAD(entry
);
860 * list_move - delete from one list and add as another's head
861 * @list: the entry to move
862 * @head: the head that will precede our entry
864 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
866 __list_del(list
->prev
, list
->next
);
867 list_add(list
, head
);
871 * list_move_tail - delete from one list and add as another's tail
872 * @list: the entry to move
873 * @head: the head that will follow our entry
875 static inline void list_move_tail(struct list_head
*list
,
876 struct list_head
*head
)
878 __list_del(list
->prev
, list
->next
);
879 list_add_tail(list
, head
);
883 * list_is_last - tests whether @list is the last entry in list @head
884 * @list: the entry to test
885 * @head: the head of the list
887 static inline int list_is_last(const struct list_head
*list
,
888 const struct list_head
*head
)
890 return list
->next
== head
;
894 * list_empty - tests whether a list is empty
895 * @head: the list to test.
897 static inline int list_empty(const struct list_head
*head
)
899 return head
->next
== head
;
903 * list_empty_careful - tests whether a list is empty and not being modified
904 * @head: the list to test
907 * tests whether a list is empty _and_ checks that no other CPU might be
908 * in the process of modifying either member (next or prev)
910 * NOTE: using list_empty_careful() without synchronization
911 * can only be safe if the only activity that can happen
912 * to the list entry is list_del_init(). Eg. it cannot be used
913 * if another CPU could re-list_add() it.
915 static inline int list_empty_careful(const struct list_head
*head
)
917 struct list_head
*next
= head
->next
;
918 return (next
== head
) && (next
== head
->prev
);
922 * list_is_singular - tests whether a list has just one entry.
923 * @head: the list to test.
925 static inline int list_is_singular(const struct list_head
*head
)
927 return !list_empty(head
) && (head
->next
== head
->prev
);
930 static inline void __list_cut_position(struct list_head
*list
,
931 struct list_head
*head
, struct list_head
*entry
)
933 struct list_head
*new_first
= entry
->next
;
934 list
->next
= head
->next
;
935 list
->next
->prev
= list
;
938 head
->next
= new_first
;
939 new_first
->prev
= head
;
943 * list_cut_position - cut a list into two
944 * @list: a new list to add all removed entries
945 * @head: a list with entries
946 * @entry: an entry within head, could be the head itself
947 * and if so we won't cut the list
949 * This helper moves the initial part of @head, up to and
950 * including @entry, from @head to @list. You should
951 * pass on @entry an element you know is on @head. @list
952 * should be an empty list or a list you do not care about
956 static inline void list_cut_position(struct list_head
*list
,
957 struct list_head
*head
, struct list_head
*entry
)
959 if (list_empty(head
))
961 if (list_is_singular(head
) &&
962 (head
->next
!= entry
&& head
!= entry
))
965 INIT_LIST_HEAD(list
);
967 __list_cut_position(list
, head
, entry
);
970 static inline void __list_splice(const struct list_head
*list
,
971 struct list_head
*prev
,
972 struct list_head
*next
)
974 struct list_head
*first
= list
->next
;
975 struct list_head
*last
= list
->prev
;
985 * list_splice - join two lists, this is designed for stacks
986 * @list: the new list to add.
987 * @head: the place to add it in the first list.
989 static inline void list_splice(const struct list_head
*list
,
990 struct list_head
*head
)
992 if (!list_empty(list
))
993 __list_splice(list
, head
, head
->next
);
997 * list_splice_tail - join two lists, each list being a queue
998 * @list: the new list to add.
999 * @head: the place to add it in the first list.
1001 static inline void list_splice_tail(struct list_head
*list
,
1002 struct list_head
*head
)
1004 if (!list_empty(list
))
1005 __list_splice(list
, head
->prev
, head
);
1009 * list_splice_init - join two lists and reinitialise the emptied list.
1010 * @list: the new list to add.
1011 * @head: the place to add it in the first list.
1013 * The list at @list is reinitialised
1015 static inline void list_splice_init(struct list_head
*list
,
1016 struct list_head
*head
)
1018 if (!list_empty(list
)) {
1019 __list_splice(list
, head
, head
->next
);
1020 INIT_LIST_HEAD(list
);
1025 * list_splice_tail_init - join two lists and reinitialise the emptied list
1026 * @list: the new list to add.
1027 * @head: the place to add it in the first list.
1029 * Each of the lists is a queue.
1030 * The list at @list is reinitialised
1032 static inline void list_splice_tail_init(struct list_head
*list
,
1033 struct list_head
*head
)
1035 if (!list_empty(list
)) {
1036 __list_splice(list
, head
->prev
, head
);
1037 INIT_LIST_HEAD(list
);
1042 * list_entry - get the struct for this entry
1043 * @ptr: the &struct list_head pointer.
1044 * @type: the type of the struct this is embedded in.
1045 * @member: the name of the list_struct within the struct.
1047 #define list_entry(ptr, type, member) \
1048 container_of(ptr, type, member)
1051 * list_first_entry - get the first element from a list
1052 * @ptr: the list head to take the element from.
1053 * @type: the type of the struct this is embedded in.
1054 * @member: the name of the list_struct within the struct.
1056 * Note, that list is expected to be not empty.
1058 #define list_first_entry(ptr, type, member) \
1059 list_entry((ptr)->next, type, member)
1062 * list_for_each - iterate over a list
1063 * @pos: the &struct list_head to use as a loop cursor.
1064 * @head: the head for your list.
1066 #define list_for_each(pos, head) \
1067 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
1071 * __list_for_each - iterate over a list
1072 * @pos: the &struct list_head to use as a loop cursor.
1073 * @head: the head for your list.
1075 * This variant differs from list_for_each() in that it's the
1076 * simplest possible list iteration code, no prefetching is done.
1077 * Use this for code that knows the list to be very short (empty
1078 * or 1 entry) most of the time.
1080 #define __list_for_each(pos, head) \
1081 for (pos = (head)->next; pos != (head); pos = pos->next)
1084 * list_for_each_prev - iterate over a list backwards
1085 * @pos: the &struct list_head to use as a loop cursor.
1086 * @head: the head for your list.
1088 #define list_for_each_prev(pos, head) \
1089 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1093 * list_for_each_safe - iterate over a list safe against removal of list entry
1094 * @pos: the &struct list_head to use as a loop cursor.
1095 * @n: another &struct list_head to use as temporary storage
1096 * @head: the head for your list.
1098 #define list_for_each_safe(pos, n, head) \
1099 for (pos = (head)->next, n = pos->next; pos != (head); \
1100 pos = n, n = pos->next)
1103 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1104 * @pos: the &struct list_head to use as a loop cursor.
1105 * @n: another &struct list_head to use as temporary storage
1106 * @head: the head for your list.
1108 #define list_for_each_prev_safe(pos, n, head) \
1109 for (pos = (head)->prev, n = pos->prev; \
1110 prefetch(pos->prev), pos != (head); \
1111 pos = n, n = pos->prev)
1114 * list_for_each_entry - iterate over list of given type
1115 * @pos: the type * to use as a loop cursor.
1116 * @head: the head for your list.
1117 * @member: the name of the list_struct within the struct.
1119 #define list_for_each_entry(pos, head, member) \
1120 for (pos = list_entry((head)->next, typeof(*pos), member); \
1121 prefetch(pos->member.next), &pos->member != (head); \
1122 pos = list_entry(pos->member.next, typeof(*pos), member))
1125 * list_for_each_entry_reverse - iterate backwards over list of given type.
1126 * @pos: the type * to use as a loop cursor.
1127 * @head: the head for your list.
1128 * @member: the name of the list_struct within the struct.
1130 #define list_for_each_entry_reverse(pos, head, member) \
1131 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1132 prefetch(pos->member.prev), &pos->member != (head); \
1133 pos = list_entry(pos->member.prev, typeof(*pos), member))
1136 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1137 * @pos: the type * to use as a start point
1138 * @head: the head of the list
1139 * @member: the name of the list_struct within the struct.
1141 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1143 #define list_prepare_entry(pos, head, member) \
1144 ((pos) ? : list_entry(head, typeof(*pos), member))
1147 * list_for_each_entry_continue - continue iteration over list of given type
1148 * @pos: the type * to use as a loop cursor.
1149 * @head: the head for your list.
1150 * @member: the name of the list_struct within the struct.
1152 * Continue to iterate over list of given type, continuing after
1153 * the current position.
1155 #define list_for_each_entry_continue(pos, head, member) \
1156 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1157 prefetch(pos->member.next), &pos->member != (head); \
1158 pos = list_entry(pos->member.next, typeof(*pos), member))
1161 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1162 * @pos: the type * to use as a loop cursor.
1163 * @head: the head for your list.
1164 * @member: the name of the list_struct within the struct.
1166 * Start to iterate over list of given type backwards, continuing after
1167 * the current position.
1169 #define list_for_each_entry_continue_reverse(pos, head, member) \
1170 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1171 prefetch(pos->member.prev), &pos->member != (head); \
1172 pos = list_entry(pos->member.prev, typeof(*pos), member))
1175 * list_for_each_entry_from - iterate over list of given type from the current point
1176 * @pos: the type * to use as a loop cursor.
1177 * @head: the head for your list.
1178 * @member: the name of the list_struct within the struct.
1180 * Iterate over list of given type, continuing from current position.
1182 #define list_for_each_entry_from(pos, head, member) \
1183 for (; prefetch(pos->member.next), &pos->member != (head); \
1184 pos = list_entry(pos->member.next, typeof(*pos), member))
1187 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1188 * @pos: the type * to use as a loop cursor.
1189 * @n: another type * to use as temporary storage
1190 * @head: the head for your list.
1191 * @member: the name of the list_struct within the struct.
1193 #define list_for_each_entry_safe(pos, n, head, member) \
1194 for (pos = list_entry((head)->next, typeof(*pos), member), \
1195 n = list_entry(pos->member.next, typeof(*pos), member); \
1196 &pos->member != (head); \
1197 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1200 * list_for_each_entry_safe_continue
1201 * @pos: the type * to use as a loop cursor.
1202 * @n: another type * to use as temporary storage
1203 * @head: the head for your list.
1204 * @member: the name of the list_struct within the struct.
1206 * Iterate over list of given type, continuing after current point,
1207 * safe against removal of list entry.
1209 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1210 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1211 n = list_entry(pos->member.next, typeof(*pos), member); \
1212 &pos->member != (head); \
1213 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1216 * list_for_each_entry_safe_from
1217 * @pos: the type * to use as a loop cursor.
1218 * @n: another type * to use as temporary storage
1219 * @head: the head for your list.
1220 * @member: the name of the list_struct within the struct.
1222 * Iterate over list of given type from current point, safe against
1223 * removal of list entry.
1225 #define list_for_each_entry_safe_from(pos, n, head, member) \
1226 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1227 &pos->member != (head); \
1228 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1231 * list_for_each_entry_safe_reverse
1232 * @pos: the type * to use as a loop cursor.
1233 * @n: another type * to use as temporary storage
1234 * @head: the head for your list.
1235 * @member: the name of the list_struct within the struct.
1237 * Iterate backwards over list of given type, safe against removal
1240 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1241 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1242 n = list_entry(pos->member.prev, typeof(*pos), member); \
1243 &pos->member != (head); \
1244 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1249 * Double linked lists with a single pointer list head.
1250 * Mostly useful for hash tables where the two pointer list head is
1252 * You lose the ability to access the tail in O(1).
1256 struct hlist_node
*first
;
1260 struct hlist_node
*next
, **pprev
;
1263 #define HLIST_HEAD_INIT { .first = NULL }
1264 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1265 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1266 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
1272 static inline int hlist_unhashed(const struct hlist_node
*h
)
1277 static inline int hlist_empty(const struct hlist_head
*h
)
1282 static inline void __hlist_del(struct hlist_node
*n
)
1284 struct hlist_node
*next
= n
->next
;
1285 struct hlist_node
**pprev
= n
->pprev
;
1288 next
->pprev
= pprev
;
1291 static inline void hlist_del(struct hlist_node
*n
)
1294 n
->next
= LIST_POISON1
;
1295 n
->pprev
= LIST_POISON2
;
1298 static inline void hlist_del_init(struct hlist_node
*n
)
1300 if (!hlist_unhashed(n
)) {
1306 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
1308 struct hlist_node
*first
= h
->first
;
1311 first
->pprev
= &n
->next
;
1313 n
->pprev
= &h
->first
;
1316 /* next must be != NULL */
1317 static inline void hlist_add_before(struct hlist_node
*n
,
1318 struct hlist_node
*next
)
1320 n
->pprev
= next
->pprev
;
1322 next
->pprev
= &n
->next
;
1326 static inline void hlist_add_after(struct hlist_node
*n
,
1327 struct hlist_node
*next
)
1329 next
->next
= n
->next
;
1331 next
->pprev
= &n
->next
;
1334 next
->next
->pprev
= &next
->next
;
1338 * Move a list from one list head to another. Fixup the pprev
1339 * reference of the first entry if it exists.
1341 static inline void hlist_move_list(struct hlist_head
*old
,
1342 struct hlist_head
*new)
1344 new->first
= old
->first
;
1346 new->first
->pprev
= &new->first
;
1350 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1352 #define hlist_for_each(pos, head) \
1353 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1356 #define hlist_for_each_safe(pos, n, head) \
1357 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1361 * hlist_for_each_entry - iterate over list of given type
1362 * @tpos: the type * to use as a loop cursor.
1363 * @pos: the &struct hlist_node to use as a loop cursor.
1364 * @head: the head for your list.
1365 * @member: the name of the hlist_node within the struct.
1367 #define hlist_for_each_entry(tpos, pos, head, member) \
1368 for (pos = (head)->first; \
1369 pos && ({ prefetch(pos->next); 1;}) && \
1370 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1374 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1375 * @tpos: the type * to use as a loop cursor.
1376 * @pos: the &struct hlist_node to use as a loop cursor.
1377 * @member: the name of the hlist_node within the struct.
1379 #define hlist_for_each_entry_continue(tpos, pos, member) \
1380 for (pos = (pos)->next; \
1381 pos && ({ prefetch(pos->next); 1;}) && \
1382 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1386 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1387 * @tpos: the type * to use as a loop cursor.
1388 * @pos: the &struct hlist_node to use as a loop cursor.
1389 * @member: the name of the hlist_node within the struct.
1391 #define hlist_for_each_entry_from(tpos, pos, member) \
1392 for (; pos && ({ prefetch(pos->next); 1;}) && \
1393 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1397 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1398 * @tpos: the type * to use as a loop cursor.
1399 * @pos: the &struct hlist_node to use as a loop cursor.
1400 * @n: another &struct hlist_node to use as temporary storage
1401 * @head: the head for your list.
1402 * @member: the name of the hlist_node within the struct.
1404 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1405 for (pos = (head)->first; \
1406 pos && ({ n = pos->next; 1; }) && \
1407 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \