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 #ifndef __always_inline
29 #define __always_inline inline
32 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
33 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
36 # define stringify_in_c(...) __VA_ARGS__
37 # define ASM_CONST(x) x
39 /* This version of stringify will deal with commas... */
40 # define __stringify_in_c(...) #__VA_ARGS__
41 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
42 # define __ASM_CONST(x) x##UL
43 # define ASM_CONST(x) __ASM_CONST(x)
48 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
50 * This program is free software; you can redistribute it and/or modify
51 * it under the terms of the GNU General Public License as published by
52 * the Free Software Foundation, but version 2 only due to inclusion
53 * of Linux-kernel code.
55 * This program is distributed in the hope that it will be useful,
56 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 * GNU General Public License for more details.
60 * You should have received a copy of the GNU General Public License
61 * along with this program; if not, write to the Free Software
62 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
64 * Copyright (c) 2006 Paul E. McKenney, IBM.
66 * Much code taken from the Linux kernel. For such code, the option
67 * to redistribute under later versions of GPL might not be available.
74 #define CACHE_LINE_SIZE 64
75 #define ____cacheline_internodealigned_in_smp \
76 __attribute__((__aligned__(1 << 6)))
78 #define LOCK_PREFIX "lock ; "
80 #if 0 /* duplicate with arch_atomic.h */
83 * Atomic data structure, initialization, and access.
86 typedef struct { volatile int counter
; } atomic_t
;
88 #define ATOMIC_INIT(i) { (i) }
90 #define atomic_read(v) ((v)->counter)
91 #define atomic_set(v, i) (((v)->counter) = (i))
98 * atomic_add - add integer to atomic variable
99 * @i: integer value to add
100 * @v: pointer of type atomic_t
102 * Atomically adds @i to @v.
104 static __inline__
void atomic_add(int i
, atomic_t
*v
)
106 __asm__
__volatile__(
107 LOCK_PREFIX
"addl %1,%0"
113 * atomic_sub - subtract the atomic variable
114 * @i: integer value to subtract
115 * @v: pointer of type atomic_t
117 * Atomically subtracts @i from @v.
119 static __inline__
void atomic_sub(int i
, atomic_t
*v
)
121 __asm__
__volatile__(
122 LOCK_PREFIX
"subl %1,%0"
128 * atomic_sub_and_test - subtract value from variable and test result
129 * @i: integer value to subtract
130 * @v: pointer of type atomic_t
132 * Atomically subtracts @i from @v and returns
133 * true if the result is zero, or false for all
136 static __inline__
int atomic_sub_and_test(int i
, atomic_t
*v
)
140 __asm__
__volatile__(
141 LOCK_PREFIX
"subl %2,%0; sete %1"
142 :"+m" (v
->counter
), "=qm" (c
)
143 :"ir" (i
) : "memory");
148 * atomic_inc - increment atomic variable
149 * @v: pointer of type atomic_t
151 * Atomically increments @v by 1.
153 static __inline__
void atomic_inc(atomic_t
*v
)
155 __asm__
__volatile__(
156 LOCK_PREFIX
"incl %0"
161 * atomic_dec - decrement atomic variable
162 * @v: pointer of type atomic_t
164 * Atomically decrements @v by 1.
166 static __inline__
void atomic_dec(atomic_t
*v
)
168 __asm__
__volatile__(
169 LOCK_PREFIX
"decl %0"
174 * atomic_dec_and_test - decrement and test
175 * @v: pointer of type atomic_t
177 * Atomically decrements @v by 1 and
178 * returns true if the result is 0, or false for all other
181 static __inline__
int atomic_dec_and_test(atomic_t
*v
)
185 __asm__
__volatile__(
186 LOCK_PREFIX
"decl %0; sete %1"
187 :"+m" (v
->counter
), "=qm" (c
)
193 * atomic_inc_and_test - increment and test
194 * @v: pointer of type atomic_t
196 * Atomically increments @v by 1
197 * and returns true if the result is zero, or false for all
200 static __inline__
int atomic_inc_and_test(atomic_t
*v
)
204 __asm__
__volatile__(
205 LOCK_PREFIX
"incl %0; sete %1"
206 :"+m" (v
->counter
), "=qm" (c
)
212 * atomic_add_negative - add and test if negative
213 * @v: pointer of type atomic_t
214 * @i: integer value to add
216 * Atomically adds @i to @v and returns true
217 * if the result is negative, or false when
218 * result is greater than or equal to zero.
220 static __inline__
int atomic_add_negative(int i
, atomic_t
*v
)
224 __asm__
__volatile__(
225 LOCK_PREFIX
"addl %2,%0; sets %1"
226 :"+m" (v
->counter
), "=qm" (c
)
227 :"ir" (i
) : "memory");
232 * atomic_add_return - add and return
233 * @v: pointer of type atomic_t
234 * @i: integer value to add
236 * Atomically adds @i to @v and returns @i + @v
238 static __inline__
int atomic_add_return(int i
, atomic_t
*v
)
243 __asm__
__volatile__(
244 LOCK_PREFIX
"xaddl %0, %1;"
246 :"m"(v
->counter
), "0"(i
));
250 static __inline__
int atomic_sub_return(int i
, atomic_t
*v
)
252 return atomic_add_return(-i
,v
);
255 static inline unsigned int
256 cmpxchg(volatile long *ptr
, long oldval
, long newval
)
258 unsigned long retval
;
261 "lock; cmpxchgl %4,(%2)\n"
262 "# end atomic_cmpxchg4"
263 : "=a" (retval
), "=m" (*ptr
)
264 : "r" (ptr
), "0" (oldval
), "r" (newval
), "m" (*ptr
)
269 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
270 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
273 * atomic_add_unless - add unless the number is a given value
274 * @v: pointer of type atomic_t
275 * @a: the amount to add to v...
276 * @u: ...unless v is equal to u.
278 * Atomically adds @a to @v, so long as it was not @u.
279 * Returns non-zero if @v was not @u, and zero otherwise.
281 #define atomic_add_unless(v, a, u) \
284 c = atomic_read(v); \
286 if (unlikely(c == (u))) \
288 old = atomic_cmpxchg((v), c, c + (a)); \
289 if (likely(old == c)) \
295 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
297 #define atomic_inc_return(v) (atomic_add_return(1,v))
298 #define atomic_dec_return(v) (atomic_sub_return(1,v))
300 /* These are x86-specific, used by some header files */
301 #define atomic_clear_mask(mask, addr) \
302 __asm__ __volatile__(LOCK_PREFIX "andl %0,%1" \
303 : : "r" (~(mask)),"m" (*addr) : "memory")
305 #define atomic_set_mask(mask, addr) \
306 __asm__ __volatile__(LOCK_PREFIX "orl %0,%1" \
307 : : "r" (mask),"m" (*(addr)) : "memory")
309 /* Atomic operations are already serializing on x86 */
310 #define smp_mb__before_atomic_dec() barrier()
311 #define smp_mb__after_atomic_dec() barrier()
312 #define smp_mb__before_atomic_inc() barrier()
313 #define smp_mb__after_atomic_inc() barrier()
318 * api_pthreads.h: API mapping to pthreads environment.
320 * This program is free software; you can redistribute it and/or modify
321 * it under the terms of the GNU General Public License as published by
322 * the Free Software Foundation; either version 2 of the License, or
323 * (at your option) any later version. However, please note that much
324 * of the code in this file derives from the Linux kernel, and that such
325 * code may not be available except under GPLv2.
327 * This program is distributed in the hope that it will be useful,
328 * but WITHOUT ANY WARRANTY; without even the implied warranty of
329 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
330 * GNU General Public License for more details.
332 * You should have received a copy of the GNU General Public License
333 * along with this program; if not, write to the Free Software
334 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
336 * Copyright (c) 2006 Paul E. McKenney, IBM.
343 #include <sys/types.h>
347 #include <sys/param.h>
348 /* #include "atomic.h" */
353 #define container_of(ptr, type, member) ({ \
354 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
355 (type *)( (char *)__mptr - offsetof(type,member) );})
358 * Default machine parameters.
361 #ifndef CACHE_LINE_SIZE
362 #define CACHE_LINE_SIZE 128
363 #endif /* #ifndef CACHE_LINE_SIZE */
366 * Exclusive locking primitives.
369 typedef pthread_mutex_t spinlock_t
;
371 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
372 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
374 static void spin_lock_init(spinlock_t
*sp
)
376 if (pthread_mutex_init(sp
, NULL
) != 0) {
377 perror("spin_lock_init:pthread_mutex_init");
382 static void spin_lock(spinlock_t
*sp
)
384 if (pthread_mutex_lock(sp
) != 0) {
385 perror("spin_lock:pthread_mutex_lock");
390 static void spin_unlock(spinlock_t
*sp
)
392 if (pthread_mutex_unlock(sp
) != 0) {
393 perror("spin_unlock:pthread_mutex_unlock");
398 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
399 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
402 * Thread creation/destruction primitives.
405 typedef pthread_t thread_id_t
;
407 #define NR_THREADS 128
409 #define __THREAD_ID_MAP_EMPTY 0
410 #define __THREAD_ID_MAP_WAITING 1
411 thread_id_t __thread_id_map
[NR_THREADS
];
412 spinlock_t __thread_id_map_mutex
;
414 #define for_each_thread(t) \
415 for (t = 0; t < NR_THREADS; t++)
417 #define for_each_running_thread(t) \
418 for (t = 0; t < NR_THREADS; t++) \
419 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
420 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
422 pthread_key_t thread_id_key
;
424 static int __smp_thread_id(void)
427 thread_id_t tid
= pthread_self();
429 for (i
= 0; i
< NR_THREADS
; i
++) {
430 if (__thread_id_map
[i
] == tid
) {
431 long v
= i
+ 1; /* must be non-NULL. */
433 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
434 perror("pthread_setspecific");
440 spin_lock(&__thread_id_map_mutex
);
441 for (i
= 0; i
< NR_THREADS
; i
++) {
442 if (__thread_id_map
[i
] == tid
)
443 spin_unlock(&__thread_id_map_mutex
);
446 spin_unlock(&__thread_id_map_mutex
);
447 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
452 static int smp_thread_id(void)
456 id
= pthread_getspecific(thread_id_key
);
458 return __smp_thread_id();
459 return (long)(id
- 1);
462 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
467 spin_lock(&__thread_id_map_mutex
);
468 for (i
= 0; i
< NR_THREADS
; i
++) {
469 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
472 if (i
>= NR_THREADS
) {
473 spin_unlock(&__thread_id_map_mutex
);
474 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
477 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
478 spin_unlock(&__thread_id_map_mutex
);
479 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
480 perror("create_thread:pthread_create");
483 __thread_id_map
[i
] = tid
;
487 static void *wait_thread(thread_id_t tid
)
492 for (i
= 0; i
< NR_THREADS
; i
++) {
493 if (__thread_id_map
[i
] == tid
)
496 if (i
>= NR_THREADS
){
497 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
501 if (pthread_join(tid
, &vp
) != 0) {
502 perror("wait_thread:pthread_join");
505 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
509 static void wait_all_threads(void)
514 for (i
= 1; i
< NR_THREADS
; i
++) {
515 tid
= __thread_id_map
[i
];
516 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
517 tid
!= __THREAD_ID_MAP_WAITING
)
518 (void)wait_thread(tid
);
522 static void run_on(int cpu
)
528 sched_setaffinity(0, sizeof(mask
), &mask
);
532 * timekeeping -- very crude -- should use MONOTONIC...
535 long long get_microseconds(void)
539 if (gettimeofday(&tv
, NULL
) != 0)
541 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
545 * Per-thread variables.
548 #define DEFINE_PER_THREAD(type, name) \
551 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
552 } __per_thread_##name[NR_THREADS];
553 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
555 #define per_thread(name, thread) __per_thread_##name[thread].v
556 #define __get_thread_var(name) per_thread(name, smp_thread_id())
558 #define init_per_thread(name, v) \
561 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
562 per_thread(name, __i_p_t_i) = v; \
566 * CPU traversal primitives.
571 #endif /* #ifndef NR_CPUS */
573 #define for_each_possible_cpu(cpu) \
574 for (cpu = 0; cpu < NR_CPUS; cpu++)
575 #define for_each_online_cpu(cpu) \
576 for (cpu = 0; cpu < NR_CPUS; cpu++)
582 #define DEFINE_PER_CPU(type, name) \
585 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
586 } __per_cpu_##name[NR_CPUS]
587 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
589 DEFINE_PER_THREAD(int, smp_processor_id
);
591 #define per_cpu(name, thread) __per_cpu_##name[thread].v
592 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
594 #define init_per_cpu(name, v) \
597 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
598 per_cpu(name, __i_p_c_i) = v; \
602 * CPU state checking (crowbarred).
605 #define idle_cpu(cpu) 0
606 #define in_softirq() 1
607 #define hardirq_count() 0
608 #define PREEMPT_SHIFT 0
609 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
610 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
611 #define PREEMPT_BITS 8
612 #define SOFTIRQ_BITS 8
618 struct notifier_block
{
619 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
620 struct notifier_block
*next
;
624 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
625 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
626 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
627 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
628 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
629 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
630 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
631 * not handling interrupts, soon dead */
632 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
635 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
636 * operation in progress
638 #define CPU_TASKS_FROZEN 0x0010
640 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
641 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
642 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
643 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
644 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
645 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
646 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
648 /* Hibernation and suspend events */
649 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
650 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
651 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
652 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
653 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
654 #define PM_POST_RESTORE 0x0006 /* Restore failed */
656 #define NOTIFY_DONE 0x0000 /* Don't care */
657 #define NOTIFY_OK 0x0001 /* Suits me */
658 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
659 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
660 /* Bad/Veto action */
662 * Clean way to return from the notifier and stop further calls.
664 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
670 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
673 * Initialization -- Must be called before calling any primitives.
676 static void smp_init(void)
680 spin_lock_init(&__thread_id_map_mutex
);
681 __thread_id_map
[0] = pthread_self();
682 for (i
= 1; i
< NR_THREADS
; i
++)
683 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
684 init_per_thread(smp_processor_id
, 0);
685 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
686 perror("pthread_key_create");
691 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
693 #ifndef _LINUX_LIST_H
694 #define _LINUX_LIST_H
696 #define LIST_POISON1 ((void *) 0x00100100)
697 #define LIST_POISON2 ((void *) 0x00200200)
699 #define container_of(ptr, type, member) ({ \
700 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
701 (type *)( (char *)__mptr - offsetof(type,member) );})
704 * Simple doubly linked list implementation.
706 * Some of the internal functions ("__xxx") are useful when
707 * manipulating whole lists rather than single entries, as
708 * sometimes we already know the next/prev entries and we can
709 * generate better code by using them directly rather than
710 * using the generic single-entry routines.
714 struct list_head
*next
, *prev
;
717 #define LIST_HEAD_INIT(name) { &(name), &(name) }
719 #define LIST_HEAD(name) \
720 struct list_head name = LIST_HEAD_INIT(name)
722 static inline void INIT_LIST_HEAD(struct list_head
*list
)
729 * Insert a new entry between two known consecutive entries.
731 * This is only for internal list manipulation where we know
732 * the prev/next entries already!
734 #ifndef CONFIG_DEBUG_LIST
735 static inline void __list_add(struct list_head
*new,
736 struct list_head
*prev
,
737 struct list_head
*next
)
745 extern void __list_add(struct list_head
*new,
746 struct list_head
*prev
,
747 struct list_head
*next
);
751 * list_add - add a new entry
752 * @new: new entry to be added
753 * @head: list head to add it after
755 * Insert a new entry after the specified head.
756 * This is good for implementing stacks.
758 static inline void list_add(struct list_head
*new, struct list_head
*head
)
760 __list_add(new, head
, head
->next
);
765 * list_add_tail - add a new entry
766 * @new: new entry to be added
767 * @head: list head to add it before
769 * Insert a new entry before the specified head.
770 * This is useful for implementing queues.
772 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
774 __list_add(new, head
->prev
, head
);
778 * Delete a list entry by making the prev/next entries
779 * point to each other.
781 * This is only for internal list manipulation where we know
782 * the prev/next entries already!
784 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
791 * list_del - deletes entry from list.
792 * @entry: the element to delete from the list.
793 * Note: list_empty() on entry does not return true after this, the entry is
794 * in an undefined state.
796 #ifndef CONFIG_DEBUG_LIST
797 static inline void list_del(struct list_head
*entry
)
799 __list_del(entry
->prev
, entry
->next
);
800 entry
->next
= LIST_POISON1
;
801 entry
->prev
= LIST_POISON2
;
804 extern void list_del(struct list_head
*entry
);
808 * list_replace - replace old entry by new one
809 * @old : the element to be replaced
810 * @new : the new element to insert
812 * If @old was empty, it will be overwritten.
814 static inline void list_replace(struct list_head
*old
,
815 struct list_head
*new)
817 new->next
= old
->next
;
818 new->next
->prev
= new;
819 new->prev
= old
->prev
;
820 new->prev
->next
= new;
823 static inline void list_replace_init(struct list_head
*old
,
824 struct list_head
*new)
826 list_replace(old
, new);
831 * list_del_init - deletes entry from list and reinitialize it.
832 * @entry: the element to delete from the list.
834 static inline void list_del_init(struct list_head
*entry
)
836 __list_del(entry
->prev
, entry
->next
);
837 INIT_LIST_HEAD(entry
);
841 * list_move - delete from one list and add as another's head
842 * @list: the entry to move
843 * @head: the head that will precede our entry
845 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
847 __list_del(list
->prev
, list
->next
);
848 list_add(list
, head
);
852 * list_move_tail - delete from one list and add as another's tail
853 * @list: the entry to move
854 * @head: the head that will follow our entry
856 static inline void list_move_tail(struct list_head
*list
,
857 struct list_head
*head
)
859 __list_del(list
->prev
, list
->next
);
860 list_add_tail(list
, head
);
864 * list_is_last - tests whether @list is the last entry in list @head
865 * @list: the entry to test
866 * @head: the head of the list
868 static inline int list_is_last(const struct list_head
*list
,
869 const struct list_head
*head
)
871 return list
->next
== head
;
875 * list_empty - tests whether a list is empty
876 * @head: the list to test.
878 static inline int list_empty(const struct list_head
*head
)
880 return head
->next
== head
;
884 * list_empty_careful - tests whether a list is empty and not being modified
885 * @head: the list to test
888 * tests whether a list is empty _and_ checks that no other CPU might be
889 * in the process of modifying either member (next or prev)
891 * NOTE: using list_empty_careful() without synchronization
892 * can only be safe if the only activity that can happen
893 * to the list entry is list_del_init(). Eg. it cannot be used
894 * if another CPU could re-list_add() it.
896 static inline int list_empty_careful(const struct list_head
*head
)
898 struct list_head
*next
= head
->next
;
899 return (next
== head
) && (next
== head
->prev
);
903 * list_is_singular - tests whether a list has just one entry.
904 * @head: the list to test.
906 static inline int list_is_singular(const struct list_head
*head
)
908 return !list_empty(head
) && (head
->next
== head
->prev
);
911 static inline void __list_cut_position(struct list_head
*list
,
912 struct list_head
*head
, struct list_head
*entry
)
914 struct list_head
*new_first
= entry
->next
;
915 list
->next
= head
->next
;
916 list
->next
->prev
= list
;
919 head
->next
= new_first
;
920 new_first
->prev
= head
;
924 * list_cut_position - cut a list into two
925 * @list: a new list to add all removed entries
926 * @head: a list with entries
927 * @entry: an entry within head, could be the head itself
928 * and if so we won't cut the list
930 * This helper moves the initial part of @head, up to and
931 * including @entry, from @head to @list. You should
932 * pass on @entry an element you know is on @head. @list
933 * should be an empty list or a list you do not care about
937 static inline void list_cut_position(struct list_head
*list
,
938 struct list_head
*head
, struct list_head
*entry
)
940 if (list_empty(head
))
942 if (list_is_singular(head
) &&
943 (head
->next
!= entry
&& head
!= entry
))
946 INIT_LIST_HEAD(list
);
948 __list_cut_position(list
, head
, entry
);
951 static inline void __list_splice(const struct list_head
*list
,
952 struct list_head
*prev
,
953 struct list_head
*next
)
955 struct list_head
*first
= list
->next
;
956 struct list_head
*last
= list
->prev
;
966 * list_splice - join two lists, this is designed for stacks
967 * @list: the new list to add.
968 * @head: the place to add it in the first list.
970 static inline void list_splice(const struct list_head
*list
,
971 struct list_head
*head
)
973 if (!list_empty(list
))
974 __list_splice(list
, head
, head
->next
);
978 * list_splice_tail - join two lists, each list being a queue
979 * @list: the new list to add.
980 * @head: the place to add it in the first list.
982 static inline void list_splice_tail(struct list_head
*list
,
983 struct list_head
*head
)
985 if (!list_empty(list
))
986 __list_splice(list
, head
->prev
, head
);
990 * list_splice_init - join two lists and reinitialise the emptied list.
991 * @list: the new list to add.
992 * @head: the place to add it in the first list.
994 * The list at @list is reinitialised
996 static inline void list_splice_init(struct list_head
*list
,
997 struct list_head
*head
)
999 if (!list_empty(list
)) {
1000 __list_splice(list
, head
, head
->next
);
1001 INIT_LIST_HEAD(list
);
1006 * list_splice_tail_init - join two lists and reinitialise the emptied list
1007 * @list: the new list to add.
1008 * @head: the place to add it in the first list.
1010 * Each of the lists is a queue.
1011 * The list at @list is reinitialised
1013 static inline void list_splice_tail_init(struct list_head
*list
,
1014 struct list_head
*head
)
1016 if (!list_empty(list
)) {
1017 __list_splice(list
, head
->prev
, head
);
1018 INIT_LIST_HEAD(list
);
1023 * list_entry - get the struct for this entry
1024 * @ptr: the &struct list_head pointer.
1025 * @type: the type of the struct this is embedded in.
1026 * @member: the name of the list_struct within the struct.
1028 #define list_entry(ptr, type, member) \
1029 container_of(ptr, type, member)
1032 * list_first_entry - get the first element from a list
1033 * @ptr: the list head to take the element from.
1034 * @type: the type of the struct this is embedded in.
1035 * @member: the name of the list_struct within the struct.
1037 * Note, that list is expected to be not empty.
1039 #define list_first_entry(ptr, type, member) \
1040 list_entry((ptr)->next, type, member)
1043 * list_for_each - iterate over a list
1044 * @pos: the &struct list_head to use as a loop cursor.
1045 * @head: the head for your list.
1047 #define list_for_each(pos, head) \
1048 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
1052 * __list_for_each - iterate over a list
1053 * @pos: the &struct list_head to use as a loop cursor.
1054 * @head: the head for your list.
1056 * This variant differs from list_for_each() in that it's the
1057 * simplest possible list iteration code, no prefetching is done.
1058 * Use this for code that knows the list to be very short (empty
1059 * or 1 entry) most of the time.
1061 #define __list_for_each(pos, head) \
1062 for (pos = (head)->next; pos != (head); pos = pos->next)
1065 * list_for_each_prev - iterate over a list backwards
1066 * @pos: the &struct list_head to use as a loop cursor.
1067 * @head: the head for your list.
1069 #define list_for_each_prev(pos, head) \
1070 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1074 * list_for_each_safe - iterate over a list safe against removal of list entry
1075 * @pos: the &struct list_head to use as a loop cursor.
1076 * @n: another &struct list_head to use as temporary storage
1077 * @head: the head for your list.
1079 #define list_for_each_safe(pos, n, head) \
1080 for (pos = (head)->next, n = pos->next; pos != (head); \
1081 pos = n, n = pos->next)
1084 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1085 * @pos: the &struct list_head to use as a loop cursor.
1086 * @n: another &struct list_head to use as temporary storage
1087 * @head: the head for your list.
1089 #define list_for_each_prev_safe(pos, n, head) \
1090 for (pos = (head)->prev, n = pos->prev; \
1091 prefetch(pos->prev), pos != (head); \
1092 pos = n, n = pos->prev)
1095 * list_for_each_entry - iterate over list of given type
1096 * @pos: the type * to use as a loop cursor.
1097 * @head: the head for your list.
1098 * @member: the name of the list_struct within the struct.
1100 #define list_for_each_entry(pos, head, member) \
1101 for (pos = list_entry((head)->next, typeof(*pos), member); \
1102 prefetch(pos->member.next), &pos->member != (head); \
1103 pos = list_entry(pos->member.next, typeof(*pos), member))
1106 * list_for_each_entry_reverse - iterate backwards over list of given type.
1107 * @pos: the type * to use as a loop cursor.
1108 * @head: the head for your list.
1109 * @member: the name of the list_struct within the struct.
1111 #define list_for_each_entry_reverse(pos, head, member) \
1112 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1113 prefetch(pos->member.prev), &pos->member != (head); \
1114 pos = list_entry(pos->member.prev, typeof(*pos), member))
1117 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1118 * @pos: the type * to use as a start point
1119 * @head: the head of the list
1120 * @member: the name of the list_struct within the struct.
1122 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1124 #define list_prepare_entry(pos, head, member) \
1125 ((pos) ? : list_entry(head, typeof(*pos), member))
1128 * list_for_each_entry_continue - continue iteration over list of given type
1129 * @pos: the type * to use as a loop cursor.
1130 * @head: the head for your list.
1131 * @member: the name of the list_struct within the struct.
1133 * Continue to iterate over list of given type, continuing after
1134 * the current position.
1136 #define list_for_each_entry_continue(pos, head, member) \
1137 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1138 prefetch(pos->member.next), &pos->member != (head); \
1139 pos = list_entry(pos->member.next, typeof(*pos), member))
1142 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1143 * @pos: the type * to use as a loop cursor.
1144 * @head: the head for your list.
1145 * @member: the name of the list_struct within the struct.
1147 * Start to iterate over list of given type backwards, continuing after
1148 * the current position.
1150 #define list_for_each_entry_continue_reverse(pos, head, member) \
1151 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1152 prefetch(pos->member.prev), &pos->member != (head); \
1153 pos = list_entry(pos->member.prev, typeof(*pos), member))
1156 * list_for_each_entry_from - iterate over list of given type from the current point
1157 * @pos: the type * to use as a loop cursor.
1158 * @head: the head for your list.
1159 * @member: the name of the list_struct within the struct.
1161 * Iterate over list of given type, continuing from current position.
1163 #define list_for_each_entry_from(pos, head, member) \
1164 for (; prefetch(pos->member.next), &pos->member != (head); \
1165 pos = list_entry(pos->member.next, typeof(*pos), member))
1168 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1169 * @pos: the type * to use as a loop cursor.
1170 * @n: another type * to use as temporary storage
1171 * @head: the head for your list.
1172 * @member: the name of the list_struct within the struct.
1174 #define list_for_each_entry_safe(pos, n, head, member) \
1175 for (pos = list_entry((head)->next, typeof(*pos), member), \
1176 n = list_entry(pos->member.next, typeof(*pos), member); \
1177 &pos->member != (head); \
1178 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1181 * list_for_each_entry_safe_continue
1182 * @pos: the type * to use as a loop cursor.
1183 * @n: another type * to use as temporary storage
1184 * @head: the head for your list.
1185 * @member: the name of the list_struct within the struct.
1187 * Iterate over list of given type, continuing after current point,
1188 * safe against removal of list entry.
1190 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1191 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1192 n = list_entry(pos->member.next, typeof(*pos), member); \
1193 &pos->member != (head); \
1194 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1197 * list_for_each_entry_safe_from
1198 * @pos: the type * to use as a loop cursor.
1199 * @n: another type * to use as temporary storage
1200 * @head: the head for your list.
1201 * @member: the name of the list_struct within the struct.
1203 * Iterate over list of given type from current point, safe against
1204 * removal of list entry.
1206 #define list_for_each_entry_safe_from(pos, n, head, member) \
1207 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1208 &pos->member != (head); \
1209 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1212 * list_for_each_entry_safe_reverse
1213 * @pos: the type * to use as a loop cursor.
1214 * @n: another type * to use as temporary storage
1215 * @head: the head for your list.
1216 * @member: the name of the list_struct within the struct.
1218 * Iterate backwards over list of given type, safe against removal
1221 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1222 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1223 n = list_entry(pos->member.prev, typeof(*pos), member); \
1224 &pos->member != (head); \
1225 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1228 * Double linked lists with a single pointer list head.
1229 * Mostly useful for hash tables where the two pointer list head is
1231 * You lose the ability to access the tail in O(1).
1235 struct hlist_node
*first
;
1239 struct hlist_node
*next
, **pprev
;
1242 #define HLIST_HEAD_INIT { .first = NULL }
1243 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1244 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1245 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
1251 static inline int hlist_unhashed(const struct hlist_node
*h
)
1256 static inline int hlist_empty(const struct hlist_head
*h
)
1261 static inline void __hlist_del(struct hlist_node
*n
)
1263 struct hlist_node
*next
= n
->next
;
1264 struct hlist_node
**pprev
= n
->pprev
;
1267 next
->pprev
= pprev
;
1270 static inline void hlist_del(struct hlist_node
*n
)
1273 n
->next
= LIST_POISON1
;
1274 n
->pprev
= LIST_POISON2
;
1277 static inline void hlist_del_init(struct hlist_node
*n
)
1279 if (!hlist_unhashed(n
)) {
1285 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
1287 struct hlist_node
*first
= h
->first
;
1290 first
->pprev
= &n
->next
;
1292 n
->pprev
= &h
->first
;
1295 /* next must be != NULL */
1296 static inline void hlist_add_before(struct hlist_node
*n
,
1297 struct hlist_node
*next
)
1299 n
->pprev
= next
->pprev
;
1301 next
->pprev
= &n
->next
;
1305 static inline void hlist_add_after(struct hlist_node
*n
,
1306 struct hlist_node
*next
)
1308 next
->next
= n
->next
;
1310 next
->pprev
= &n
->next
;
1313 next
->next
->pprev
= &next
->next
;
1317 * Move a list from one list head to another. Fixup the pprev
1318 * reference of the first entry if it exists.
1320 static inline void hlist_move_list(struct hlist_head
*old
,
1321 struct hlist_head
*new)
1323 new->first
= old
->first
;
1325 new->first
->pprev
= &new->first
;
1329 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1331 #define hlist_for_each(pos, head) \
1332 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1335 #define hlist_for_each_safe(pos, n, head) \
1336 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1340 * hlist_for_each_entry - iterate over list of given type
1341 * @tpos: the type * to use as a loop cursor.
1342 * @pos: the &struct hlist_node to use as a loop cursor.
1343 * @head: the head for your list.
1344 * @member: the name of the hlist_node within the struct.
1346 #define hlist_for_each_entry(tpos, pos, head, member) \
1347 for (pos = (head)->first; \
1348 pos && ({ prefetch(pos->next); 1;}) && \
1349 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1353 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1354 * @tpos: the type * to use as a loop cursor.
1355 * @pos: the &struct hlist_node to use as a loop cursor.
1356 * @member: the name of the hlist_node within the struct.
1358 #define hlist_for_each_entry_continue(tpos, pos, member) \
1359 for (pos = (pos)->next; \
1360 pos && ({ prefetch(pos->next); 1;}) && \
1361 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1365 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1366 * @tpos: the type * to use as a loop cursor.
1367 * @pos: the &struct hlist_node to use as a loop cursor.
1368 * @member: the name of the hlist_node within the struct.
1370 #define hlist_for_each_entry_from(tpos, pos, member) \
1371 for (; pos && ({ prefetch(pos->next); 1;}) && \
1372 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1376 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1377 * @tpos: the type * to use as a loop cursor.
1378 * @pos: the &struct hlist_node to use as a loop cursor.
1379 * @n: another &struct hlist_node to use as temporary storage
1380 * @head: the head for your list.
1381 * @member: the name of the hlist_node within the struct.
1383 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1384 for (pos = (head)->first; \
1385 pos && ({ n = pos->next; 1; }) && \
1386 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \