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.
76 #define CACHE_LINE_SIZE 64
77 #define ____cacheline_internodealigned_in_smp \
78 __attribute__((__aligned__(1 << 6)))
80 #define LOCK_PREFIX "lock ; "
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()
316 __asm__ __volatile__("mfence" : : : "memory")
317 /* __asm__ __volatile__("lock; addl $0,0(%%esp)" : : : "memory") */
321 * Generate 64-bit timestamp.
324 static unsigned long long get_timestamp(void)
326 unsigned int __a
,__d
;
328 __asm__
__volatile__("rdtsc" : "=a" (__a
), "=d" (__d
));
329 return ((long long)__a
) | (((long long)__d
)<<32);
333 * api_pthreads.h: API mapping to pthreads environment.
335 * This program is free software; you can redistribute it and/or modify
336 * it under the terms of the GNU General Public License as published by
337 * the Free Software Foundation; either version 2 of the License, or
338 * (at your option) any later version. However, please note that much
339 * of the code in this file derives from the Linux kernel, and that such
340 * code may not be available except under GPLv2.
342 * This program is distributed in the hope that it will be useful,
343 * but WITHOUT ANY WARRANTY; without even the implied warranty of
344 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
345 * GNU General Public License for more details.
347 * You should have received a copy of the GNU General Public License
348 * along with this program; if not, write to the Free Software
349 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
351 * Copyright (c) 2006 Paul E. McKenney, IBM.
358 #include <sys/types.h>
362 #include <sys/param.h>
363 /* #include "atomic.h" */
368 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
369 #define container_of(ptr, type, member) ({ \
370 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
371 (type *)( (char *)__mptr - offsetof(type,member) );})
372 #define barrier() __asm__ __volatile__("": : :"memory")
375 * Default machine parameters.
378 #ifndef CACHE_LINE_SIZE
379 #define CACHE_LINE_SIZE 128
380 #endif /* #ifndef CACHE_LINE_SIZE */
383 * Exclusive locking primitives.
386 typedef pthread_mutex_t spinlock_t
;
388 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
389 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
391 static void spin_lock_init(spinlock_t
*sp
)
393 if (pthread_mutex_init(sp
, NULL
) != 0) {
394 perror("spin_lock_init:pthread_mutex_init");
399 static void spin_lock(spinlock_t
*sp
)
401 if (pthread_mutex_lock(sp
) != 0) {
402 perror("spin_lock:pthread_mutex_lock");
407 static int spin_trylock(spinlock_t
*sp
)
411 if ((retval
= pthread_mutex_trylock(sp
)) == 0)
415 perror("spin_trylock:pthread_mutex_trylock");
419 static void spin_unlock(spinlock_t
*sp
)
421 if (pthread_mutex_unlock(sp
) != 0) {
422 perror("spin_unlock:pthread_mutex_unlock");
427 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
428 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
430 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
431 #define unlikely(x) x
433 #define prefetch(x) x
436 * Thread creation/destruction primitives.
439 typedef pthread_t thread_id_t
;
441 #define NR_THREADS 128
443 #define __THREAD_ID_MAP_EMPTY 0
444 #define __THREAD_ID_MAP_WAITING 1
445 thread_id_t __thread_id_map
[NR_THREADS
];
446 spinlock_t __thread_id_map_mutex
;
448 #define for_each_thread(t) \
449 for (t = 0; t < NR_THREADS; t++)
451 #define for_each_running_thread(t) \
452 for (t = 0; t < NR_THREADS; t++) \
453 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
454 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
456 pthread_key_t thread_id_key
;
458 static int __smp_thread_id(void)
461 thread_id_t tid
= pthread_self();
463 for (i
= 0; i
< NR_THREADS
; i
++) {
464 if (__thread_id_map
[i
] == tid
) {
465 long v
= i
+ 1; /* must be non-NULL. */
467 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
468 perror("pthread_setspecific");
474 spin_lock(&__thread_id_map_mutex
);
475 for (i
= 0; i
< NR_THREADS
; i
++) {
476 if (__thread_id_map
[i
] == tid
)
477 spin_unlock(&__thread_id_map_mutex
);
480 spin_unlock(&__thread_id_map_mutex
);
481 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n", tid
, tid
);
485 static int smp_thread_id(void)
489 id
= pthread_getspecific(thread_id_key
);
491 return __smp_thread_id();
492 return (long)(id
- 1);
495 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
500 spin_lock(&__thread_id_map_mutex
);
501 for (i
= 0; i
< NR_THREADS
; i
++) {
502 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
505 if (i
>= NR_THREADS
) {
506 spin_unlock(&__thread_id_map_mutex
);
507 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
510 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
511 spin_unlock(&__thread_id_map_mutex
);
512 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
513 perror("create_thread:pthread_create");
516 __thread_id_map
[i
] = tid
;
520 static void *wait_thread(thread_id_t tid
)
525 for (i
= 0; i
< NR_THREADS
; i
++) {
526 if (__thread_id_map
[i
] == tid
)
529 if (i
>= NR_THREADS
){
530 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n", tid
, tid
);
533 if (pthread_join(tid
, &vp
) != 0) {
534 perror("wait_thread:pthread_join");
537 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
541 static void wait_all_threads(void)
546 for (i
= 1; i
< NR_THREADS
; i
++) {
547 tid
= __thread_id_map
[i
];
548 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
549 tid
!= __THREAD_ID_MAP_WAITING
)
550 (void)wait_thread(tid
);
554 static void run_on(int cpu
)
560 sched_setaffinity(0, sizeof(mask
), &mask
);
564 * timekeeping -- very crude -- should use MONOTONIC...
567 long long get_microseconds(void)
571 if (gettimeofday(&tv
, NULL
) != 0)
573 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
577 * Per-thread variables.
580 #define DEFINE_PER_THREAD(type, name) \
583 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
584 } __per_thread_##name[NR_THREADS];
585 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
587 #define per_thread(name, thread) __per_thread_##name[thread].v
588 #define __get_thread_var(name) per_thread(name, smp_thread_id())
590 #define init_per_thread(name, v) \
593 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
594 per_thread(name, __i_p_t_i) = v; \
598 * CPU traversal primitives.
603 #endif /* #ifndef NR_CPUS */
605 #define for_each_possible_cpu(cpu) \
606 for (cpu = 0; cpu < NR_CPUS; cpu++)
607 #define for_each_online_cpu(cpu) \
608 for (cpu = 0; cpu < NR_CPUS; cpu++)
614 #define DEFINE_PER_CPU(type, name) \
617 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
618 } __per_cpu_##name[NR_CPUS]
619 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
621 DEFINE_PER_THREAD(int, smp_processor_id
);
623 static int smp_processor_id(void)
625 return __get_thread_var(smp_processor_id
);
628 static void set_smp_processor_id(int cpu
)
630 __get_thread_var(smp_processor_id
) = cpu
;
633 #define per_cpu(name, thread) __per_cpu_##name[thread].v
634 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
636 #define init_per_cpu(name, v) \
639 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
640 per_cpu(name, __i_p_c_i) = v; \
644 * CPU state checking (crowbarred).
647 #define idle_cpu(cpu) 0
648 #define in_softirq() 1
649 #define hardirq_count() 0
650 #define PREEMPT_SHIFT 0
651 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
652 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
653 #define PREEMPT_BITS 8
654 #define SOFTIRQ_BITS 8
660 struct notifier_block
{
661 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
662 struct notifier_block
*next
;
666 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
667 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
668 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
669 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
670 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
671 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
672 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
673 * not handling interrupts, soon dead */
674 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
677 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
678 * operation in progress
680 #define CPU_TASKS_FROZEN 0x0010
682 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
683 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
684 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
685 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
686 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
687 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
688 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
690 /* Hibernation and suspend events */
691 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
692 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
693 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
694 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
695 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
696 #define PM_POST_RESTORE 0x0006 /* Restore failed */
698 #define NOTIFY_DONE 0x0000 /* Don't care */
699 #define NOTIFY_OK 0x0001 /* Suits me */
700 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
701 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
702 /* Bad/Veto action */
704 * Clean way to return from the notifier and stop further calls.
706 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
712 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
715 * Initialization -- Must be called before calling any primitives.
718 static void smp_init(void)
722 spin_lock_init(&__thread_id_map_mutex
);
723 __thread_id_map
[0] = pthread_self();
724 for (i
= 1; i
< NR_THREADS
; i
++)
725 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
726 init_per_thread(smp_processor_id
, 0);
727 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
728 perror("pthread_key_create");
733 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
735 #ifndef _LINUX_LIST_H
736 #define _LINUX_LIST_H
738 #define LIST_POISON1 ((void *) 0x00100100)
739 #define LIST_POISON2 ((void *) 0x00200200)
741 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
742 #define container_of(ptr, type, member) ({ \
743 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
744 (type *)( (char *)__mptr - offsetof(type,member) );})
747 * Simple doubly linked list implementation.
749 * Some of the internal functions ("__xxx") are useful when
750 * manipulating whole lists rather than single entries, as
751 * sometimes we already know the next/prev entries and we can
752 * generate better code by using them directly rather than
753 * using the generic single-entry routines.
757 struct list_head
*next
, *prev
;
760 #define LIST_HEAD_INIT(name) { &(name), &(name) }
762 #define LIST_HEAD(name) \
763 struct list_head name = LIST_HEAD_INIT(name)
765 static inline void INIT_LIST_HEAD(struct list_head
*list
)
772 * Insert a new entry between two known consecutive entries.
774 * This is only for internal list manipulation where we know
775 * the prev/next entries already!
777 #ifndef CONFIG_DEBUG_LIST
778 static inline void __list_add(struct list_head
*new,
779 struct list_head
*prev
,
780 struct list_head
*next
)
788 extern void __list_add(struct list_head
*new,
789 struct list_head
*prev
,
790 struct list_head
*next
);
794 * list_add - add a new entry
795 * @new: new entry to be added
796 * @head: list head to add it after
798 * Insert a new entry after the specified head.
799 * This is good for implementing stacks.
801 static inline void list_add(struct list_head
*new, struct list_head
*head
)
803 __list_add(new, head
, head
->next
);
808 * list_add_tail - add a new entry
809 * @new: new entry to be added
810 * @head: list head to add it before
812 * Insert a new entry before the specified head.
813 * This is useful for implementing queues.
815 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
817 __list_add(new, head
->prev
, head
);
821 * Delete a list entry by making the prev/next entries
822 * point to each other.
824 * This is only for internal list manipulation where we know
825 * the prev/next entries already!
827 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
834 * list_del - deletes entry from list.
835 * @entry: the element to delete from the list.
836 * Note: list_empty() on entry does not return true after this, the entry is
837 * in an undefined state.
839 #ifndef CONFIG_DEBUG_LIST
840 static inline void list_del(struct list_head
*entry
)
842 __list_del(entry
->prev
, entry
->next
);
843 entry
->next
= LIST_POISON1
;
844 entry
->prev
= LIST_POISON2
;
847 extern void list_del(struct list_head
*entry
);
851 * list_replace - replace old entry by new one
852 * @old : the element to be replaced
853 * @new : the new element to insert
855 * If @old was empty, it will be overwritten.
857 static inline void list_replace(struct list_head
*old
,
858 struct list_head
*new)
860 new->next
= old
->next
;
861 new->next
->prev
= new;
862 new->prev
= old
->prev
;
863 new->prev
->next
= new;
866 static inline void list_replace_init(struct list_head
*old
,
867 struct list_head
*new)
869 list_replace(old
, new);
874 * list_del_init - deletes entry from list and reinitialize it.
875 * @entry: the element to delete from the list.
877 static inline void list_del_init(struct list_head
*entry
)
879 __list_del(entry
->prev
, entry
->next
);
880 INIT_LIST_HEAD(entry
);
884 * list_move - delete from one list and add as another's head
885 * @list: the entry to move
886 * @head: the head that will precede our entry
888 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
890 __list_del(list
->prev
, list
->next
);
891 list_add(list
, head
);
895 * list_move_tail - delete from one list and add as another's tail
896 * @list: the entry to move
897 * @head: the head that will follow our entry
899 static inline void list_move_tail(struct list_head
*list
,
900 struct list_head
*head
)
902 __list_del(list
->prev
, list
->next
);
903 list_add_tail(list
, head
);
907 * list_is_last - tests whether @list is the last entry in list @head
908 * @list: the entry to test
909 * @head: the head of the list
911 static inline int list_is_last(const struct list_head
*list
,
912 const struct list_head
*head
)
914 return list
->next
== head
;
918 * list_empty - tests whether a list is empty
919 * @head: the list to test.
921 static inline int list_empty(const struct list_head
*head
)
923 return head
->next
== head
;
927 * list_empty_careful - tests whether a list is empty and not being modified
928 * @head: the list to test
931 * tests whether a list is empty _and_ checks that no other CPU might be
932 * in the process of modifying either member (next or prev)
934 * NOTE: using list_empty_careful() without synchronization
935 * can only be safe if the only activity that can happen
936 * to the list entry is list_del_init(). Eg. it cannot be used
937 * if another CPU could re-list_add() it.
939 static inline int list_empty_careful(const struct list_head
*head
)
941 struct list_head
*next
= head
->next
;
942 return (next
== head
) && (next
== head
->prev
);
946 * list_is_singular - tests whether a list has just one entry.
947 * @head: the list to test.
949 static inline int list_is_singular(const struct list_head
*head
)
951 return !list_empty(head
) && (head
->next
== head
->prev
);
954 static inline void __list_cut_position(struct list_head
*list
,
955 struct list_head
*head
, struct list_head
*entry
)
957 struct list_head
*new_first
= entry
->next
;
958 list
->next
= head
->next
;
959 list
->next
->prev
= list
;
962 head
->next
= new_first
;
963 new_first
->prev
= head
;
967 * list_cut_position - cut a list into two
968 * @list: a new list to add all removed entries
969 * @head: a list with entries
970 * @entry: an entry within head, could be the head itself
971 * and if so we won't cut the list
973 * This helper moves the initial part of @head, up to and
974 * including @entry, from @head to @list. You should
975 * pass on @entry an element you know is on @head. @list
976 * should be an empty list or a list you do not care about
980 static inline void list_cut_position(struct list_head
*list
,
981 struct list_head
*head
, struct list_head
*entry
)
983 if (list_empty(head
))
985 if (list_is_singular(head
) &&
986 (head
->next
!= entry
&& head
!= entry
))
989 INIT_LIST_HEAD(list
);
991 __list_cut_position(list
, head
, entry
);
994 static inline void __list_splice(const struct list_head
*list
,
995 struct list_head
*prev
,
996 struct list_head
*next
)
998 struct list_head
*first
= list
->next
;
999 struct list_head
*last
= list
->prev
;
1009 * list_splice - join two lists, this is designed for stacks
1010 * @list: the new list to add.
1011 * @head: the place to add it in the first list.
1013 static inline void list_splice(const struct list_head
*list
,
1014 struct list_head
*head
)
1016 if (!list_empty(list
))
1017 __list_splice(list
, head
, head
->next
);
1021 * list_splice_tail - join two lists, each list being a queue
1022 * @list: the new list to add.
1023 * @head: the place to add it in the first list.
1025 static inline void list_splice_tail(struct list_head
*list
,
1026 struct list_head
*head
)
1028 if (!list_empty(list
))
1029 __list_splice(list
, head
->prev
, head
);
1033 * list_splice_init - join two lists and reinitialise the emptied list.
1034 * @list: the new list to add.
1035 * @head: the place to add it in the first list.
1037 * The list at @list is reinitialised
1039 static inline void list_splice_init(struct list_head
*list
,
1040 struct list_head
*head
)
1042 if (!list_empty(list
)) {
1043 __list_splice(list
, head
, head
->next
);
1044 INIT_LIST_HEAD(list
);
1049 * list_splice_tail_init - join two lists and reinitialise the emptied list
1050 * @list: the new list to add.
1051 * @head: the place to add it in the first list.
1053 * Each of the lists is a queue.
1054 * The list at @list is reinitialised
1056 static inline void list_splice_tail_init(struct list_head
*list
,
1057 struct list_head
*head
)
1059 if (!list_empty(list
)) {
1060 __list_splice(list
, head
->prev
, head
);
1061 INIT_LIST_HEAD(list
);
1066 * list_entry - get the struct for this entry
1067 * @ptr: the &struct list_head pointer.
1068 * @type: the type of the struct this is embedded in.
1069 * @member: the name of the list_struct within the struct.
1071 #define list_entry(ptr, type, member) \
1072 container_of(ptr, type, member)
1075 * list_first_entry - get the first element from a list
1076 * @ptr: the list head to take the element from.
1077 * @type: the type of the struct this is embedded in.
1078 * @member: the name of the list_struct within the struct.
1080 * Note, that list is expected to be not empty.
1082 #define list_first_entry(ptr, type, member) \
1083 list_entry((ptr)->next, type, member)
1086 * list_for_each - iterate over a list
1087 * @pos: the &struct list_head to use as a loop cursor.
1088 * @head: the head for your list.
1090 #define list_for_each(pos, head) \
1091 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
1095 * __list_for_each - iterate over a list
1096 * @pos: the &struct list_head to use as a loop cursor.
1097 * @head: the head for your list.
1099 * This variant differs from list_for_each() in that it's the
1100 * simplest possible list iteration code, no prefetching is done.
1101 * Use this for code that knows the list to be very short (empty
1102 * or 1 entry) most of the time.
1104 #define __list_for_each(pos, head) \
1105 for (pos = (head)->next; pos != (head); pos = pos->next)
1108 * list_for_each_prev - iterate over a list backwards
1109 * @pos: the &struct list_head to use as a loop cursor.
1110 * @head: the head for your list.
1112 #define list_for_each_prev(pos, head) \
1113 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1117 * list_for_each_safe - iterate over a list safe against removal of list entry
1118 * @pos: the &struct list_head to use as a loop cursor.
1119 * @n: another &struct list_head to use as temporary storage
1120 * @head: the head for your list.
1122 #define list_for_each_safe(pos, n, head) \
1123 for (pos = (head)->next, n = pos->next; pos != (head); \
1124 pos = n, n = pos->next)
1127 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1128 * @pos: the &struct list_head to use as a loop cursor.
1129 * @n: another &struct list_head to use as temporary storage
1130 * @head: the head for your list.
1132 #define list_for_each_prev_safe(pos, n, head) \
1133 for (pos = (head)->prev, n = pos->prev; \
1134 prefetch(pos->prev), pos != (head); \
1135 pos = n, n = pos->prev)
1138 * list_for_each_entry - iterate over list of given type
1139 * @pos: the type * to use as a loop cursor.
1140 * @head: the head for your list.
1141 * @member: the name of the list_struct within the struct.
1143 #define list_for_each_entry(pos, head, member) \
1144 for (pos = list_entry((head)->next, typeof(*pos), member); \
1145 prefetch(pos->member.next), &pos->member != (head); \
1146 pos = list_entry(pos->member.next, typeof(*pos), member))
1149 * list_for_each_entry_reverse - iterate backwards over list of given type.
1150 * @pos: the type * to use as a loop cursor.
1151 * @head: the head for your list.
1152 * @member: the name of the list_struct within the struct.
1154 #define list_for_each_entry_reverse(pos, head, member) \
1155 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1156 prefetch(pos->member.prev), &pos->member != (head); \
1157 pos = list_entry(pos->member.prev, typeof(*pos), member))
1160 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1161 * @pos: the type * to use as a start point
1162 * @head: the head of the list
1163 * @member: the name of the list_struct within the struct.
1165 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1167 #define list_prepare_entry(pos, head, member) \
1168 ((pos) ? : list_entry(head, typeof(*pos), member))
1171 * list_for_each_entry_continue - continue iteration over list of given type
1172 * @pos: the type * to use as a loop cursor.
1173 * @head: the head for your list.
1174 * @member: the name of the list_struct within the struct.
1176 * Continue to iterate over list of given type, continuing after
1177 * the current position.
1179 #define list_for_each_entry_continue(pos, head, member) \
1180 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1181 prefetch(pos->member.next), &pos->member != (head); \
1182 pos = list_entry(pos->member.next, typeof(*pos), member))
1185 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1186 * @pos: the type * to use as a loop cursor.
1187 * @head: the head for your list.
1188 * @member: the name of the list_struct within the struct.
1190 * Start to iterate over list of given type backwards, continuing after
1191 * the current position.
1193 #define list_for_each_entry_continue_reverse(pos, head, member) \
1194 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1195 prefetch(pos->member.prev), &pos->member != (head); \
1196 pos = list_entry(pos->member.prev, typeof(*pos), member))
1199 * list_for_each_entry_from - iterate over list of given type from the current point
1200 * @pos: the type * to use as a loop cursor.
1201 * @head: the head for your list.
1202 * @member: the name of the list_struct within the struct.
1204 * Iterate over list of given type, continuing from current position.
1206 #define list_for_each_entry_from(pos, head, member) \
1207 for (; prefetch(pos->member.next), &pos->member != (head); \
1208 pos = list_entry(pos->member.next, typeof(*pos), member))
1211 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1212 * @pos: the type * to use as a loop cursor.
1213 * @n: another type * to use as temporary storage
1214 * @head: the head for your list.
1215 * @member: the name of the list_struct within the struct.
1217 #define list_for_each_entry_safe(pos, n, head, member) \
1218 for (pos = list_entry((head)->next, typeof(*pos), member), \
1219 n = list_entry(pos->member.next, typeof(*pos), member); \
1220 &pos->member != (head); \
1221 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1224 * list_for_each_entry_safe_continue
1225 * @pos: the type * to use as a loop cursor.
1226 * @n: another type * to use as temporary storage
1227 * @head: the head for your list.
1228 * @member: the name of the list_struct within the struct.
1230 * Iterate over list of given type, continuing after current point,
1231 * safe against removal of list entry.
1233 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1234 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1235 n = list_entry(pos->member.next, typeof(*pos), member); \
1236 &pos->member != (head); \
1237 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1240 * list_for_each_entry_safe_from
1241 * @pos: the type * to use as a loop cursor.
1242 * @n: another type * to use as temporary storage
1243 * @head: the head for your list.
1244 * @member: the name of the list_struct within the struct.
1246 * Iterate over list of given type from current point, safe against
1247 * removal of list entry.
1249 #define list_for_each_entry_safe_from(pos, n, head, member) \
1250 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1251 &pos->member != (head); \
1252 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1255 * list_for_each_entry_safe_reverse
1256 * @pos: the type * to use as a loop cursor.
1257 * @n: another type * to use as temporary storage
1258 * @head: the head for your list.
1259 * @member: the name of the list_struct within the struct.
1261 * Iterate backwards over list of given type, safe against removal
1264 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1265 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1266 n = list_entry(pos->member.prev, typeof(*pos), member); \
1267 &pos->member != (head); \
1268 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1271 * Double linked lists with a single pointer list head.
1272 * Mostly useful for hash tables where the two pointer list head is
1274 * You lose the ability to access the tail in O(1).
1278 struct hlist_node
*first
;
1282 struct hlist_node
*next
, **pprev
;
1285 #define HLIST_HEAD_INIT { .first = NULL }
1286 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1287 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1288 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
1294 static inline int hlist_unhashed(const struct hlist_node
*h
)
1299 static inline int hlist_empty(const struct hlist_head
*h
)
1304 static inline void __hlist_del(struct hlist_node
*n
)
1306 struct hlist_node
*next
= n
->next
;
1307 struct hlist_node
**pprev
= n
->pprev
;
1310 next
->pprev
= pprev
;
1313 static inline void hlist_del(struct hlist_node
*n
)
1316 n
->next
= LIST_POISON1
;
1317 n
->pprev
= LIST_POISON2
;
1320 static inline void hlist_del_init(struct hlist_node
*n
)
1322 if (!hlist_unhashed(n
)) {
1328 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
1330 struct hlist_node
*first
= h
->first
;
1333 first
->pprev
= &n
->next
;
1335 n
->pprev
= &h
->first
;
1338 /* next must be != NULL */
1339 static inline void hlist_add_before(struct hlist_node
*n
,
1340 struct hlist_node
*next
)
1342 n
->pprev
= next
->pprev
;
1344 next
->pprev
= &n
->next
;
1348 static inline void hlist_add_after(struct hlist_node
*n
,
1349 struct hlist_node
*next
)
1351 next
->next
= n
->next
;
1353 next
->pprev
= &n
->next
;
1356 next
->next
->pprev
= &next
->next
;
1360 * Move a list from one list head to another. Fixup the pprev
1361 * reference of the first entry if it exists.
1363 static inline void hlist_move_list(struct hlist_head
*old
,
1364 struct hlist_head
*new)
1366 new->first
= old
->first
;
1368 new->first
->pprev
= &new->first
;
1372 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1374 #define hlist_for_each(pos, head) \
1375 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1378 #define hlist_for_each_safe(pos, n, head) \
1379 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1383 * hlist_for_each_entry - iterate over list of given type
1384 * @tpos: the type * to use as a loop cursor.
1385 * @pos: the &struct hlist_node to use as a loop cursor.
1386 * @head: the head for your list.
1387 * @member: the name of the hlist_node within the struct.
1389 #define hlist_for_each_entry(tpos, pos, head, member) \
1390 for (pos = (head)->first; \
1391 pos && ({ prefetch(pos->next); 1;}) && \
1392 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1396 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1397 * @tpos: the type * to use as a loop cursor.
1398 * @pos: the &struct hlist_node to use as a loop cursor.
1399 * @member: the name of the hlist_node within the struct.
1401 #define hlist_for_each_entry_continue(tpos, pos, member) \
1402 for (pos = (pos)->next; \
1403 pos && ({ prefetch(pos->next); 1;}) && \
1404 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1408 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1409 * @tpos: the type * to use as a loop cursor.
1410 * @pos: the &struct hlist_node to use as a loop cursor.
1411 * @member: the name of the hlist_node within the struct.
1413 #define hlist_for_each_entry_from(tpos, pos, member) \
1414 for (; pos && ({ prefetch(pos->next); 1;}) && \
1415 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1419 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1420 * @tpos: the type * to use as a loop cursor.
1421 * @pos: the &struct hlist_node to use as a loop cursor.
1422 * @n: another &struct hlist_node to use as temporary storage
1423 * @head: the head for your list.
1424 * @member: the name of the hlist_node within the struct.
1426 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1427 for (pos = (head)->first; \
1428 pos && ({ n = pos->next; 1; }) && \
1429 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \