8 * common.h: Common Linux kernel-isms.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; but version 2 of the License only due
13 * to code included from the Linux kernel.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Copyright (c) 2006 Paul E. McKenney, IBM.
26 * Much code taken from the Linux kernel. For such code, the option
27 * to redistribute under later versions of GPL might not be available.
30 #ifndef __always_inline
31 #define __always_inline inline
34 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
35 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
38 # define stringify_in_c(...) __VA_ARGS__
39 # define ASM_CONST(x) x
41 /* This version of stringify will deal with commas... */
42 # define __stringify_in_c(...) #__VA_ARGS__
43 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
44 # define __ASM_CONST(x) x##UL
45 # define ASM_CONST(x) __ASM_CONST(x)
50 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
52 * This program is free software; you can redistribute it and/or modify
53 * it under the terms of the GNU General Public License as published by
54 * the Free Software Foundation, but version 2 only due to inclusion
55 * of Linux-kernel code.
57 * This program is distributed in the hope that it will be useful,
58 * but WITHOUT ANY WARRANTY; without even the implied warranty of
59 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 * GNU General Public License for more details.
62 * You should have received a copy of the GNU General Public License
63 * along with this program; if not, write to the Free Software
64 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
66 * Copyright (c) 2006 Paul E. McKenney, IBM.
68 * Much code taken from the Linux kernel. For such code, the option
69 * to redistribute under later versions of GPL might not be available.
76 /* #define CACHE_LINE_SIZE 64 */
77 #define ____cacheline_internodealigned_in_smp \
78 __attribute__((__aligned__(1 << 6)))
80 #define LOCK_PREFIX "lock ; "
82 #if 0 /* duplicate with arch_atomic.h */
84 * Atomic data structure, initialization, and access.
87 typedef struct { volatile int counter
; } atomic_t
;
89 #define ATOMIC_INIT(i) { (i) }
91 #define atomic_read(v) ((v)->counter)
92 #define atomic_set(v, i) (((v)->counter) = (i))
99 * atomic_add - add integer to atomic variable
100 * @i: integer value to add
101 * @v: pointer of type atomic_t
103 * Atomically adds @i to @v.
106 static __inline__
void atomic_add(int i
, atomic_t
*v
)
108 (void)__sync_fetch_and_add(&v
->counter
, i
);
112 * atomic_sub - subtract the atomic variable
113 * @i: integer value to subtract
114 * @v: pointer of type atomic_t
116 * Atomically subtracts @i from @v.
118 static __inline__
void atomic_sub(int i
, atomic_t
*v
)
120 (void)__sync_fetch_and_add(&v
->counter
, -i
);
124 * atomic_sub_and_test - subtract value from variable and test result
125 * @i: integer value to subtract
126 * @v: pointer of type atomic_t
128 * Atomically subtracts @i from @v and returns
129 * true if the result is zero, or false for all
132 static __inline__
int atomic_sub_and_test(int i
, atomic_t
*v
)
134 return __sync_add_and_fetch(&v
->counter
, -i
) == 0;
138 * atomic_inc - increment atomic variable
139 * @v: pointer of type atomic_t
141 * Atomically increments @v by 1.
143 static __inline__
void atomic_inc(atomic_t
*v
)
145 (void)__sync_fetch_and_add(&v
->counter
, 1);
149 * atomic_dec - decrement atomic variable
150 * @v: pointer of type atomic_t
152 * Atomically decrements @v by 1.
154 static __inline__
void atomic_dec(atomic_t
*v
)
156 (void)__sync_fetch_and_add(&v
->counter
, -1);
160 * atomic_dec_and_test - decrement and test
161 * @v: pointer of type atomic_t
163 * Atomically decrements @v by 1 and
164 * returns true if the result is 0, or false for all other
167 static __inline__
int atomic_dec_and_test(atomic_t
*v
)
169 return __sync_add_and_fetch(&v
->counter
, -1) == 0;
173 * atomic_inc_and_test - increment and test
174 * @v: pointer of type atomic_t
176 * Atomically increments @v by 1
177 * and returns true if the result is zero, or false for all
180 static __inline__
int atomic_inc_and_test(atomic_t
*v
)
182 return __sync_add_and_fetch(&v
->counter
, 1) == 0;
186 * atomic_add_negative - add and test if negative
187 * @v: pointer of type atomic_t
188 * @i: integer value to add
190 * Atomically adds @i to @v and returns true
191 * if the result is negative, or false when
192 * result is greater than or equal to zero.
194 static __inline__
int atomic_add_negative(int i
, atomic_t
*v
)
196 return __sync_add_and_fetch(&v
->counter
, i
) < 0;
200 * atomic_add_return - add and return
201 * @v: pointer of type atomic_t
202 * @i: integer value to add
204 * Atomically adds @i to @v and returns @i + @v
206 static __inline__
int atomic_add_return(int i
, atomic_t
*v
)
208 return __sync_add_and_fetch(&v
->counter
, i
);
211 static __inline__
int atomic_sub_return(int i
, atomic_t
*v
)
213 return atomic_add_return(-i
,v
);
216 static inline unsigned int
217 cmpxchg(volatile long *ptr
, long oldval
, long newval
)
219 return __sync_val_compare_and_swap(ptr
, oldval
, newval
);
222 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
223 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
226 * atomic_add_unless - add unless the number is a given value
227 * @v: pointer of type atomic_t
228 * @a: the amount to add to v...
229 * @u: ...unless v is equal to u.
231 * Atomically adds @a to @v, so long as it was not @u.
232 * Returns non-zero if @v was not @u, and zero otherwise.
234 #define atomic_add_unless(v, a, u) \
237 c = atomic_read(v); \
239 if (unlikely(c == (u))) \
241 old = atomic_cmpxchg((v), c, c + (a)); \
242 if (likely(old == c)) \
248 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
250 #define atomic_inc_return(v) (atomic_add_return(1,v))
251 #define atomic_dec_return(v) (atomic_sub_return(1,v))
253 /* Atomic operations are already serializing on x86 */
254 #define smp_mb__before_atomic_dec() barrier()
255 #define smp_mb__after_atomic_dec() barrier()
256 #define smp_mb__before_atomic_inc() barrier()
257 #define smp_mb__after_atomic_inc() barrier()
259 #endif //0 /* duplicate with arch_atomic.h */
262 * api_pthreads.h: API mapping to pthreads environment.
264 * This program is free software; you can redistribute it and/or modify
265 * it under the terms of the GNU General Public License as published by
266 * the Free Software Foundation; either version 2 of the License, or
267 * (at your option) any later version. However, please note that much
268 * of the code in this file derives from the Linux kernel, and that such
269 * code may not be available except under GPLv2.
271 * This program is distributed in the hope that it will be useful,
272 * but WITHOUT ANY WARRANTY; without even the implied warranty of
273 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
274 * GNU General Public License for more details.
276 * You should have received a copy of the GNU General Public License
277 * along with this program; if not, write to the Free Software
278 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
280 * Copyright (c) 2006 Paul E. McKenney, IBM.
287 #include <sys/types.h>
291 #include <sys/param.h>
292 /* #include "atomic.h" */
297 #define container_of(ptr, type, member) ({ \
298 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
299 (type *)( (char *)__mptr - offsetof(type,member) );})
302 * Default machine parameters.
305 #ifndef CACHE_LINE_SIZE
306 /* #define CACHE_LINE_SIZE 128 */
307 #endif /* #ifndef CACHE_LINE_SIZE */
310 * Exclusive locking primitives.
313 typedef pthread_mutex_t spinlock_t
;
315 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
316 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
318 static void spin_lock_init(spinlock_t
*sp
)
320 if (pthread_mutex_init(sp
, NULL
) != 0) {
321 perror("spin_lock_init:pthread_mutex_init");
326 static void spin_lock(spinlock_t
*sp
)
328 if (pthread_mutex_lock(sp
) != 0) {
329 perror("spin_lock:pthread_mutex_lock");
334 static void spin_unlock(spinlock_t
*sp
)
336 if (pthread_mutex_unlock(sp
) != 0) {
337 perror("spin_unlock:pthread_mutex_unlock");
342 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
343 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
346 * Thread creation/destruction primitives.
349 typedef pthread_t thread_id_t
;
351 #define NR_THREADS 128
353 #define __THREAD_ID_MAP_EMPTY 0
354 #define __THREAD_ID_MAP_WAITING 1
355 thread_id_t __thread_id_map
[NR_THREADS
];
356 spinlock_t __thread_id_map_mutex
;
358 #define for_each_thread(t) \
359 for (t = 0; t < NR_THREADS; t++)
361 #define for_each_running_thread(t) \
362 for (t = 0; t < NR_THREADS; t++) \
363 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
364 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
366 pthread_key_t thread_id_key
;
368 static int __smp_thread_id(void)
371 thread_id_t tid
= pthread_self();
373 for (i
= 0; i
< NR_THREADS
; i
++) {
374 if (__thread_id_map
[i
] == tid
) {
375 long v
= i
+ 1; /* must be non-NULL. */
377 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
378 perror("pthread_setspecific");
384 spin_lock(&__thread_id_map_mutex
);
385 for (i
= 0; i
< NR_THREADS
; i
++) {
386 if (__thread_id_map
[i
] == tid
)
387 spin_unlock(&__thread_id_map_mutex
);
390 spin_unlock(&__thread_id_map_mutex
);
391 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
396 static int smp_thread_id(void)
400 id
= pthread_getspecific(thread_id_key
);
402 return __smp_thread_id();
403 return (long)(id
- 1);
406 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
411 spin_lock(&__thread_id_map_mutex
);
412 for (i
= 0; i
< NR_THREADS
; i
++) {
413 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
416 if (i
>= NR_THREADS
) {
417 spin_unlock(&__thread_id_map_mutex
);
418 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
421 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
422 spin_unlock(&__thread_id_map_mutex
);
423 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
424 perror("create_thread:pthread_create");
427 __thread_id_map
[i
] = tid
;
431 static void *wait_thread(thread_id_t tid
)
436 for (i
= 0; i
< NR_THREADS
; i
++) {
437 if (__thread_id_map
[i
] == tid
)
440 if (i
>= NR_THREADS
){
441 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
445 if (pthread_join(tid
, &vp
) != 0) {
446 perror("wait_thread:pthread_join");
449 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
453 static void wait_all_threads(void)
458 for (i
= 1; i
< NR_THREADS
; i
++) {
459 tid
= __thread_id_map
[i
];
460 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
461 tid
!= __THREAD_ID_MAP_WAITING
)
462 (void)wait_thread(tid
);
466 #ifndef HAVE_CPU_SET_T
467 typedef unsigned long cpu_set_t
;
468 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
469 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
472 static void run_on(int cpu
)
474 #if HAVE_SCHED_SETAFFINITY
479 #if SCHED_SETAFFINITY_ARGS == 2
480 sched_setaffinity(0, &mask
);
482 sched_setaffinity(0, sizeof(mask
), &mask
);
484 #endif /* HAVE_SCHED_SETAFFINITY */
488 * timekeeping -- very crude -- should use MONOTONIC...
491 long long get_microseconds(void)
495 if (gettimeofday(&tv
, NULL
) != 0)
497 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
501 * Per-thread variables.
504 #define DEFINE_PER_THREAD(type, name) \
507 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
508 } __per_thread_##name[NR_THREADS];
509 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
511 #define per_thread(name, thread) __per_thread_##name[thread].v
512 #define __get_thread_var(name) per_thread(name, smp_thread_id())
514 #define init_per_thread(name, v) \
517 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
518 per_thread(name, __i_p_t_i) = v; \
522 * CPU traversal primitives.
527 #endif /* #ifndef NR_CPUS */
529 #define for_each_possible_cpu(cpu) \
530 for (cpu = 0; cpu < NR_CPUS; cpu++)
531 #define for_each_online_cpu(cpu) \
532 for (cpu = 0; cpu < NR_CPUS; cpu++)
538 #define DEFINE_PER_CPU(type, name) \
541 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
542 } __per_cpu_##name[NR_CPUS]
543 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
545 DEFINE_PER_THREAD(int, smp_processor_id
);
547 #define per_cpu(name, thread) __per_cpu_##name[thread].v
548 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
550 #define init_per_cpu(name, v) \
553 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
554 per_cpu(name, __i_p_c_i) = v; \
558 * CPU state checking (crowbarred).
561 #define idle_cpu(cpu) 0
562 #define in_softirq() 1
563 #define hardirq_count() 0
564 #define PREEMPT_SHIFT 0
565 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
566 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
567 #define PREEMPT_BITS 8
568 #define SOFTIRQ_BITS 8
574 struct notifier_block
{
575 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
576 struct notifier_block
*next
;
580 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
581 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
582 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
583 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
584 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
585 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
586 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
587 * not handling interrupts, soon dead */
588 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
591 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
592 * operation in progress
594 #define CPU_TASKS_FROZEN 0x0010
596 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
597 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
598 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
599 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
600 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
601 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
602 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
604 /* Hibernation and suspend events */
605 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
606 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
607 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
608 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
609 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
610 #define PM_POST_RESTORE 0x0006 /* Restore failed */
612 #define NOTIFY_DONE 0x0000 /* Don't care */
613 #define NOTIFY_OK 0x0001 /* Suits me */
614 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
615 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
616 /* Bad/Veto action */
618 * Clean way to return from the notifier and stop further calls.
620 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
626 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
629 * Initialization -- Must be called before calling any primitives.
632 static void smp_init(void)
636 spin_lock_init(&__thread_id_map_mutex
);
637 __thread_id_map
[0] = pthread_self();
638 for (i
= 1; i
< NR_THREADS
; i
++)
639 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
640 init_per_thread(smp_processor_id
, 0);
641 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
642 perror("pthread_key_create");
647 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
649 #ifndef _LINUX_LIST_H
650 #define _LINUX_LIST_H
652 #define LIST_POISON1 ((void *) 0x00100100)
653 #define LIST_POISON2 ((void *) 0x00200200)
655 #define container_of(ptr, type, member) ({ \
656 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
657 (type *)( (char *)__mptr - offsetof(type,member) );})
661 * Simple doubly linked list implementation.
663 * Some of the internal functions ("__xxx") are useful when
664 * manipulating whole lists rather than single entries, as
665 * sometimes we already know the next/prev entries and we can
666 * generate better code by using them directly rather than
667 * using the generic single-entry routines.
671 struct list_head
*next
, *prev
;
674 #define LIST_HEAD_INIT(name) { &(name), &(name) }
676 #define LIST_HEAD(name) \
677 struct list_head name = LIST_HEAD_INIT(name)
679 static inline void INIT_LIST_HEAD(struct list_head
*list
)
686 * Insert a new entry between two known consecutive entries.
688 * This is only for internal list manipulation where we know
689 * the prev/next entries already!
691 #ifndef CONFIG_DEBUG_LIST
692 static inline void __list_add(struct list_head
*new,
693 struct list_head
*prev
,
694 struct list_head
*next
)
702 extern void __list_add(struct list_head
*new,
703 struct list_head
*prev
,
704 struct list_head
*next
);
708 * list_add - add a new entry
709 * @new: new entry to be added
710 * @head: list head to add it after
712 * Insert a new entry after the specified head.
713 * This is good for implementing stacks.
715 static inline void list_add(struct list_head
*new, struct list_head
*head
)
717 __list_add(new, head
, head
->next
);
722 * list_add_tail - add a new entry
723 * @new: new entry to be added
724 * @head: list head to add it before
726 * Insert a new entry before the specified head.
727 * This is useful for implementing queues.
729 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
731 __list_add(new, head
->prev
, head
);
735 * Delete a list entry by making the prev/next entries
736 * point to each other.
738 * This is only for internal list manipulation where we know
739 * the prev/next entries already!
741 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
748 * list_del - deletes entry from list.
749 * @entry: the element to delete from the list.
750 * Note: list_empty() on entry does not return true after this, the entry is
751 * in an undefined state.
753 #ifndef CONFIG_DEBUG_LIST
754 static inline void list_del(struct list_head
*entry
)
756 __list_del(entry
->prev
, entry
->next
);
757 entry
->next
= LIST_POISON1
;
758 entry
->prev
= LIST_POISON2
;
761 extern void list_del(struct list_head
*entry
);
765 * list_replace - replace old entry by new one
766 * @old : the element to be replaced
767 * @new : the new element to insert
769 * If @old was empty, it will be overwritten.
771 static inline void list_replace(struct list_head
*old
,
772 struct list_head
*new)
774 new->next
= old
->next
;
775 new->next
->prev
= new;
776 new->prev
= old
->prev
;
777 new->prev
->next
= new;
780 static inline void list_replace_init(struct list_head
*old
,
781 struct list_head
*new)
783 list_replace(old
, new);
788 * list_del_init - deletes entry from list and reinitialize it.
789 * @entry: the element to delete from the list.
791 static inline void list_del_init(struct list_head
*entry
)
793 __list_del(entry
->prev
, entry
->next
);
794 INIT_LIST_HEAD(entry
);
798 * list_move - delete from one list and add as another's head
799 * @list: the entry to move
800 * @head: the head that will precede our entry
802 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
804 __list_del(list
->prev
, list
->next
);
805 list_add(list
, head
);
809 * list_move_tail - delete from one list and add as another's tail
810 * @list: the entry to move
811 * @head: the head that will follow our entry
813 static inline void list_move_tail(struct list_head
*list
,
814 struct list_head
*head
)
816 __list_del(list
->prev
, list
->next
);
817 list_add_tail(list
, head
);
821 * list_is_last - tests whether @list is the last entry in list @head
822 * @list: the entry to test
823 * @head: the head of the list
825 static inline int list_is_last(const struct list_head
*list
,
826 const struct list_head
*head
)
828 return list
->next
== head
;
832 * list_empty - tests whether a list is empty
833 * @head: the list to test.
835 static inline int list_empty(const struct list_head
*head
)
837 return head
->next
== head
;
841 * list_empty_careful - tests whether a list is empty and not being modified
842 * @head: the list to test
845 * tests whether a list is empty _and_ checks that no other CPU might be
846 * in the process of modifying either member (next or prev)
848 * NOTE: using list_empty_careful() without synchronization
849 * can only be safe if the only activity that can happen
850 * to the list entry is list_del_init(). Eg. it cannot be used
851 * if another CPU could re-list_add() it.
853 static inline int list_empty_careful(const struct list_head
*head
)
855 struct list_head
*next
= head
->next
;
856 return (next
== head
) && (next
== head
->prev
);
860 * list_is_singular - tests whether a list has just one entry.
861 * @head: the list to test.
863 static inline int list_is_singular(const struct list_head
*head
)
865 return !list_empty(head
) && (head
->next
== head
->prev
);
868 static inline void __list_cut_position(struct list_head
*list
,
869 struct list_head
*head
, struct list_head
*entry
)
871 struct list_head
*new_first
= entry
->next
;
872 list
->next
= head
->next
;
873 list
->next
->prev
= list
;
876 head
->next
= new_first
;
877 new_first
->prev
= head
;
881 * list_cut_position - cut a list into two
882 * @list: a new list to add all removed entries
883 * @head: a list with entries
884 * @entry: an entry within head, could be the head itself
885 * and if so we won't cut the list
887 * This helper moves the initial part of @head, up to and
888 * including @entry, from @head to @list. You should
889 * pass on @entry an element you know is on @head. @list
890 * should be an empty list or a list you do not care about
894 static inline void list_cut_position(struct list_head
*list
,
895 struct list_head
*head
, struct list_head
*entry
)
897 if (list_empty(head
))
899 if (list_is_singular(head
) &&
900 (head
->next
!= entry
&& head
!= entry
))
903 INIT_LIST_HEAD(list
);
905 __list_cut_position(list
, head
, entry
);
908 static inline void __list_splice(const struct list_head
*list
,
909 struct list_head
*prev
,
910 struct list_head
*next
)
912 struct list_head
*first
= list
->next
;
913 struct list_head
*last
= list
->prev
;
923 * list_splice - join two lists, this is designed for stacks
924 * @list: the new list to add.
925 * @head: the place to add it in the first list.
927 static inline void list_splice(const struct list_head
*list
,
928 struct list_head
*head
)
930 if (!list_empty(list
))
931 __list_splice(list
, head
, head
->next
);
935 * list_splice_tail - join two lists, each list being a queue
936 * @list: the new list to add.
937 * @head: the place to add it in the first list.
939 static inline void list_splice_tail(struct list_head
*list
,
940 struct list_head
*head
)
942 if (!list_empty(list
))
943 __list_splice(list
, head
->prev
, head
);
947 * list_splice_init - join two lists and reinitialise the emptied list.
948 * @list: the new list to add.
949 * @head: the place to add it in the first list.
951 * The list at @list is reinitialised
953 static inline void list_splice_init(struct list_head
*list
,
954 struct list_head
*head
)
956 if (!list_empty(list
)) {
957 __list_splice(list
, head
, head
->next
);
958 INIT_LIST_HEAD(list
);
963 * list_splice_tail_init - join two lists and reinitialise the emptied list
964 * @list: the new list to add.
965 * @head: the place to add it in the first list.
967 * Each of the lists is a queue.
968 * The list at @list is reinitialised
970 static inline void list_splice_tail_init(struct list_head
*list
,
971 struct list_head
*head
)
973 if (!list_empty(list
)) {
974 __list_splice(list
, head
->prev
, head
);
975 INIT_LIST_HEAD(list
);
980 * list_entry - get the struct for this entry
981 * @ptr: the &struct list_head pointer.
982 * @type: the type of the struct this is embedded in.
983 * @member: the name of the list_struct within the struct.
985 #define list_entry(ptr, type, member) \
986 container_of(ptr, type, member)
989 * list_first_entry - get the first element from a list
990 * @ptr: the list head to take the element from.
991 * @type: the type of the struct this is embedded in.
992 * @member: the name of the list_struct within the struct.
994 * Note, that list is expected to be not empty.
996 #define list_first_entry(ptr, type, member) \
997 list_entry((ptr)->next, type, member)
1000 * list_for_each - iterate over a list
1001 * @pos: the &struct list_head to use as a loop cursor.
1002 * @head: the head for your list.
1004 #define list_for_each(pos, head) \
1005 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
1009 * __list_for_each - iterate over a list
1010 * @pos: the &struct list_head to use as a loop cursor.
1011 * @head: the head for your list.
1013 * This variant differs from list_for_each() in that it's the
1014 * simplest possible list iteration code, no prefetching is done.
1015 * Use this for code that knows the list to be very short (empty
1016 * or 1 entry) most of the time.
1018 #define __list_for_each(pos, head) \
1019 for (pos = (head)->next; pos != (head); pos = pos->next)
1022 * list_for_each_prev - iterate over a list backwards
1023 * @pos: the &struct list_head to use as a loop cursor.
1024 * @head: the head for your list.
1026 #define list_for_each_prev(pos, head) \
1027 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1031 * list_for_each_safe - iterate over a list safe against removal of list entry
1032 * @pos: the &struct list_head to use as a loop cursor.
1033 * @n: another &struct list_head to use as temporary storage
1034 * @head: the head for your list.
1036 #define list_for_each_safe(pos, n, head) \
1037 for (pos = (head)->next, n = pos->next; pos != (head); \
1038 pos = n, n = pos->next)
1041 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1042 * @pos: the &struct list_head to use as a loop cursor.
1043 * @n: another &struct list_head to use as temporary storage
1044 * @head: the head for your list.
1046 #define list_for_each_prev_safe(pos, n, head) \
1047 for (pos = (head)->prev, n = pos->prev; \
1048 prefetch(pos->prev), pos != (head); \
1049 pos = n, n = pos->prev)
1052 * list_for_each_entry - iterate over list of given type
1053 * @pos: the type * to use as a loop cursor.
1054 * @head: the head for your list.
1055 * @member: the name of the list_struct within the struct.
1057 #define list_for_each_entry(pos, head, member) \
1058 for (pos = list_entry((head)->next, typeof(*pos), member); \
1059 prefetch(pos->member.next), &pos->member != (head); \
1060 pos = list_entry(pos->member.next, typeof(*pos), member))
1063 * list_for_each_entry_reverse - iterate backwards over list of given type.
1064 * @pos: the type * to use as a loop cursor.
1065 * @head: the head for your list.
1066 * @member: the name of the list_struct within the struct.
1068 #define list_for_each_entry_reverse(pos, head, member) \
1069 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1070 prefetch(pos->member.prev), &pos->member != (head); \
1071 pos = list_entry(pos->member.prev, typeof(*pos), member))
1074 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1075 * @pos: the type * to use as a start point
1076 * @head: the head of the list
1077 * @member: the name of the list_struct within the struct.
1079 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1081 #define list_prepare_entry(pos, head, member) \
1082 ((pos) ? : list_entry(head, typeof(*pos), member))
1085 * list_for_each_entry_continue - continue iteration over list of given type
1086 * @pos: the type * to use as a loop cursor.
1087 * @head: the head for your list.
1088 * @member: the name of the list_struct within the struct.
1090 * Continue to iterate over list of given type, continuing after
1091 * the current position.
1093 #define list_for_each_entry_continue(pos, head, member) \
1094 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1095 prefetch(pos->member.next), &pos->member != (head); \
1096 pos = list_entry(pos->member.next, typeof(*pos), member))
1099 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1100 * @pos: the type * to use as a loop cursor.
1101 * @head: the head for your list.
1102 * @member: the name of the list_struct within the struct.
1104 * Start to iterate over list of given type backwards, continuing after
1105 * the current position.
1107 #define list_for_each_entry_continue_reverse(pos, head, member) \
1108 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1109 prefetch(pos->member.prev), &pos->member != (head); \
1110 pos = list_entry(pos->member.prev, typeof(*pos), member))
1113 * list_for_each_entry_from - iterate over list of given type from the current point
1114 * @pos: the type * to use as a loop cursor.
1115 * @head: the head for your list.
1116 * @member: the name of the list_struct within the struct.
1118 * Iterate over list of given type, continuing from current position.
1120 #define list_for_each_entry_from(pos, head, member) \
1121 for (; prefetch(pos->member.next), &pos->member != (head); \
1122 pos = list_entry(pos->member.next, typeof(*pos), member))
1125 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1126 * @pos: the type * to use as a loop cursor.
1127 * @n: another type * to use as temporary storage
1128 * @head: the head for your list.
1129 * @member: the name of the list_struct within the struct.
1131 #define list_for_each_entry_safe(pos, n, head, member) \
1132 for (pos = list_entry((head)->next, typeof(*pos), member), \
1133 n = list_entry(pos->member.next, typeof(*pos), member); \
1134 &pos->member != (head); \
1135 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1138 * list_for_each_entry_safe_continue
1139 * @pos: the type * to use as a loop cursor.
1140 * @n: another type * to use as temporary storage
1141 * @head: the head for your list.
1142 * @member: the name of the list_struct within the struct.
1144 * Iterate over list of given type, continuing after current point,
1145 * safe against removal of list entry.
1147 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1148 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1149 n = list_entry(pos->member.next, typeof(*pos), member); \
1150 &pos->member != (head); \
1151 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1154 * list_for_each_entry_safe_from
1155 * @pos: the type * to use as a loop cursor.
1156 * @n: another type * to use as temporary storage
1157 * @head: the head for your list.
1158 * @member: the name of the list_struct within the struct.
1160 * Iterate over list of given type from current point, safe against
1161 * removal of list entry.
1163 #define list_for_each_entry_safe_from(pos, n, head, member) \
1164 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1165 &pos->member != (head); \
1166 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1169 * list_for_each_entry_safe_reverse
1170 * @pos: the type * to use as a loop cursor.
1171 * @n: another type * to use as temporary storage
1172 * @head: the head for your list.
1173 * @member: the name of the list_struct within the struct.
1175 * Iterate backwards over list of given type, safe against removal
1178 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1179 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1180 n = list_entry(pos->member.prev, typeof(*pos), member); \
1181 &pos->member != (head); \
1182 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1187 * Double linked lists with a single pointer list head.
1188 * Mostly useful for hash tables where the two pointer list head is
1190 * You lose the ability to access the tail in O(1).
1194 struct hlist_node
*first
;
1198 struct hlist_node
*next
, **pprev
;
1201 #define HLIST_HEAD_INIT { .first = NULL }
1202 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1203 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1204 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
1210 static inline int hlist_unhashed(const struct hlist_node
*h
)
1215 static inline int hlist_empty(const struct hlist_head
*h
)
1220 static inline void __hlist_del(struct hlist_node
*n
)
1222 struct hlist_node
*next
= n
->next
;
1223 struct hlist_node
**pprev
= n
->pprev
;
1226 next
->pprev
= pprev
;
1229 static inline void hlist_del(struct hlist_node
*n
)
1232 n
->next
= LIST_POISON1
;
1233 n
->pprev
= LIST_POISON2
;
1236 static inline void hlist_del_init(struct hlist_node
*n
)
1238 if (!hlist_unhashed(n
)) {
1244 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
1246 struct hlist_node
*first
= h
->first
;
1249 first
->pprev
= &n
->next
;
1251 n
->pprev
= &h
->first
;
1254 /* next must be != NULL */
1255 static inline void hlist_add_before(struct hlist_node
*n
,
1256 struct hlist_node
*next
)
1258 n
->pprev
= next
->pprev
;
1260 next
->pprev
= &n
->next
;
1264 static inline void hlist_add_after(struct hlist_node
*n
,
1265 struct hlist_node
*next
)
1267 next
->next
= n
->next
;
1269 next
->pprev
= &n
->next
;
1272 next
->next
->pprev
= &next
->next
;
1276 * Move a list from one list head to another. Fixup the pprev
1277 * reference of the first entry if it exists.
1279 static inline void hlist_move_list(struct hlist_head
*old
,
1280 struct hlist_head
*new)
1282 new->first
= old
->first
;
1284 new->first
->pprev
= &new->first
;
1288 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1290 #define hlist_for_each(pos, head) \
1291 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1294 #define hlist_for_each_safe(pos, n, head) \
1295 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1299 * hlist_for_each_entry - iterate over list of given type
1300 * @tpos: the type * to use as a loop cursor.
1301 * @pos: the &struct hlist_node to use as a loop cursor.
1302 * @head: the head for your list.
1303 * @member: the name of the hlist_node within the struct.
1305 #define hlist_for_each_entry(tpos, pos, head, member) \
1306 for (pos = (head)->first; \
1307 pos && ({ prefetch(pos->next); 1;}) && \
1308 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1312 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1313 * @tpos: the type * to use as a loop cursor.
1314 * @pos: the &struct hlist_node to use as a loop cursor.
1315 * @member: the name of the hlist_node within the struct.
1317 #define hlist_for_each_entry_continue(tpos, pos, member) \
1318 for (pos = (pos)->next; \
1319 pos && ({ prefetch(pos->next); 1;}) && \
1320 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1324 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1325 * @tpos: the type * to use as a loop cursor.
1326 * @pos: the &struct hlist_node to use as a loop cursor.
1327 * @member: the name of the hlist_node within the struct.
1329 #define hlist_for_each_entry_from(tpos, pos, member) \
1330 for (; pos && ({ prefetch(pos->next); 1;}) && \
1331 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1335 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1336 * @tpos: the type * to use as a loop cursor.
1337 * @pos: the &struct hlist_node to use as a loop cursor.
1338 * @n: another &struct hlist_node to use as temporary storage
1339 * @head: the head for your list.
1340 * @member: the name of the hlist_node within the struct.
1342 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1343 for (pos = (head)->first; \
1344 pos && ({ n = pos->next; 1; }) && \
1345 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \