4 * common.h: Common Linux kernel-isms.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; but version 2 of the License only due
9 * to code included from the Linux kernel.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * Copyright (c) 2006 Paul E. McKenney, IBM.
22 * Much code taken from the Linux kernel. For such code, the option
23 * to redistribute under later versions of GPL might not be available.
26 #ifndef __always_inline
27 #define __always_inline inline
30 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
31 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
34 # define stringify_in_c(...) __VA_ARGS__
35 # define ASM_CONST(x) x
37 /* This version of stringify will deal with commas... */
38 # define __stringify_in_c(...) #__VA_ARGS__
39 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
40 # define __ASM_CONST(x) x##UL
41 # define ASM_CONST(x) __ASM_CONST(x)
46 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
48 * This program is free software; you can redistribute it and/or modify
49 * it under the terms of the GNU General Public License as published by
50 * the Free Software Foundation, but version 2 only due to inclusion
51 * of Linux-kernel code.
53 * This program is distributed in the hope that it will be useful,
54 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
56 * GNU General Public License for more details.
58 * You should have received a copy of the GNU General Public License
59 * along with this program; if not, write to the Free Software
60 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
62 * Copyright (c) 2006 Paul E. McKenney, IBM.
64 * Much code taken from the Linux kernel. For such code, the option
65 * to redistribute under later versions of GPL might not be available.
72 #define CACHE_LINE_SIZE 64
73 #define ____cacheline_internodealigned_in_smp \
74 __attribute__((__aligned__(1 << 6)))
76 #define LOCK_PREFIX "lock ; "
79 * Atomic data structure, initialization, and access.
82 typedef struct { volatile int counter
; } atomic_t
;
84 #define ATOMIC_INIT(i) { (i) }
86 #define atomic_read(v) ((v)->counter)
87 #define atomic_set(v, i) (((v)->counter) = (i))
94 * atomic_add - add integer to atomic variable
95 * @i: integer value to add
96 * @v: pointer of type atomic_t
98 * Atomically adds @i to @v.
101 static __inline__
void atomic_add(int i
, atomic_t
*v
)
103 (void)__sync_fetch_and_add(&v
->counter
, i
);
107 * atomic_sub - subtract the atomic variable
108 * @i: integer value to subtract
109 * @v: pointer of type atomic_t
111 * Atomically subtracts @i from @v.
113 static __inline__
void atomic_sub(int i
, atomic_t
*v
)
115 (void)__sync_fetch_and_add(&v
->counter
, -i
);
119 * atomic_sub_and_test - subtract value from variable and test result
120 * @i: integer value to subtract
121 * @v: pointer of type atomic_t
123 * Atomically subtracts @i from @v and returns
124 * true if the result is zero, or false for all
127 static __inline__
int atomic_sub_and_test(int i
, atomic_t
*v
)
129 return __sync_add_and_fetch(&v
->counter
, -i
) == 0;
133 * atomic_inc - increment atomic variable
134 * @v: pointer of type atomic_t
136 * Atomically increments @v by 1.
138 static __inline__
void atomic_inc(atomic_t
*v
)
140 (void)__sync_fetch_and_add(&v
->counter
, 1);
144 * atomic_dec - decrement atomic variable
145 * @v: pointer of type atomic_t
147 * Atomically decrements @v by 1.
149 static __inline__
void atomic_dec(atomic_t
*v
)
151 (void)__sync_fetch_and_add(&v
->counter
, -1);
155 * atomic_dec_and_test - decrement and test
156 * @v: pointer of type atomic_t
158 * Atomically decrements @v by 1 and
159 * returns true if the result is 0, or false for all other
162 static __inline__
int atomic_dec_and_test(atomic_t
*v
)
164 return __sync_add_and_fetch(&v
->counter
, -1) == 0;
168 * atomic_inc_and_test - increment and test
169 * @v: pointer of type atomic_t
171 * Atomically increments @v by 1
172 * and returns true if the result is zero, or false for all
175 static __inline__
int atomic_inc_and_test(atomic_t
*v
)
177 return __sync_add_and_fetch(&v
->counter
, 1) == 0;
181 * atomic_add_negative - add and test if negative
182 * @v: pointer of type atomic_t
183 * @i: integer value to add
185 * Atomically adds @i to @v and returns true
186 * if the result is negative, or false when
187 * result is greater than or equal to zero.
189 static __inline__
int atomic_add_negative(int i
, atomic_t
*v
)
191 return __sync_add_and_fetch(&v
->counter
, i
) < 0;
195 * atomic_add_return - add and return
196 * @v: pointer of type atomic_t
197 * @i: integer value to add
199 * Atomically adds @i to @v and returns @i + @v
201 static __inline__
int atomic_add_return(int i
, atomic_t
*v
)
203 return __sync_add_and_fetch(&v
->counter
, i
);
206 static __inline__
int atomic_sub_return(int i
, atomic_t
*v
)
208 return atomic_add_return(-i
,v
);
211 static inline unsigned int
212 cmpxchg(volatile long *ptr
, long oldval
, long newval
)
214 return __sync_val_compare_and_swap(ptr
, oldval
, newval
);
217 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
218 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
221 * atomic_add_unless - add unless the number is a given value
222 * @v: pointer of type atomic_t
223 * @a: the amount to add to v...
224 * @u: ...unless v is equal to u.
226 * Atomically adds @a to @v, so long as it was not @u.
227 * Returns non-zero if @v was not @u, and zero otherwise.
229 #define atomic_add_unless(v, a, u) \
232 c = atomic_read(v); \
234 if (unlikely(c == (u))) \
236 old = atomic_cmpxchg((v), c, c + (a)); \
237 if (likely(old == c)) \
243 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
245 #define atomic_inc_return(v) (atomic_add_return(1,v))
246 #define atomic_dec_return(v) (atomic_sub_return(1,v))
248 /* Atomic operations are already serializing on x86 */
249 #define smp_mb__before_atomic_dec() barrier()
250 #define smp_mb__after_atomic_dec() barrier()
251 #define smp_mb__before_atomic_inc() barrier()
252 #define smp_mb__after_atomic_inc() barrier()
255 * api_pthreads.h: API mapping to pthreads environment.
257 * This program is free software; you can redistribute it and/or modify
258 * it under the terms of the GNU General Public License as published by
259 * the Free Software Foundation; either version 2 of the License, or
260 * (at your option) any later version. However, please note that much
261 * of the code in this file derives from the Linux kernel, and that such
262 * code may not be available except under GPLv2.
264 * This program is distributed in the hope that it will be useful,
265 * but WITHOUT ANY WARRANTY; without even the implied warranty of
266 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
267 * GNU General Public License for more details.
269 * You should have received a copy of the GNU General Public License
270 * along with this program; if not, write to the Free Software
271 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
273 * Copyright (c) 2006 Paul E. McKenney, IBM.
280 #include <sys/types.h>
284 #include <sys/param.h>
285 /* #include "atomic.h" */
290 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
291 #define container_of(ptr, type, member) ({ \
292 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
293 (type *)( (char *)__mptr - offsetof(type,member) );})
296 * Default machine parameters.
299 #ifndef CACHE_LINE_SIZE
300 #define CACHE_LINE_SIZE 128
301 #endif /* #ifndef CACHE_LINE_SIZE */
304 * Exclusive locking primitives.
307 typedef pthread_mutex_t spinlock_t
;
309 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
310 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
312 static void spin_lock_init(spinlock_t
*sp
)
314 if (pthread_mutex_init(sp
, NULL
) != 0) {
315 perror("spin_lock_init:pthread_mutex_init");
320 static void spin_lock(spinlock_t
*sp
)
322 if (pthread_mutex_lock(sp
) != 0) {
323 perror("spin_lock:pthread_mutex_lock");
328 static void spin_unlock(spinlock_t
*sp
)
330 if (pthread_mutex_unlock(sp
) != 0) {
331 perror("spin_unlock:pthread_mutex_unlock");
336 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
337 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
340 * Thread creation/destruction primitives.
343 typedef pthread_t thread_id_t
;
345 #define NR_THREADS 128
347 #define __THREAD_ID_MAP_EMPTY 0
348 #define __THREAD_ID_MAP_WAITING 1
349 thread_id_t __thread_id_map
[NR_THREADS
];
350 spinlock_t __thread_id_map_mutex
;
352 #define for_each_thread(t) \
353 for (t = 0; t < NR_THREADS; t++)
355 #define for_each_running_thread(t) \
356 for (t = 0; t < NR_THREADS; t++) \
357 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
358 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
360 pthread_key_t thread_id_key
;
362 static int __smp_thread_id(void)
365 thread_id_t tid
= pthread_self();
367 for (i
= 0; i
< NR_THREADS
; i
++) {
368 if (__thread_id_map
[i
] == tid
) {
369 long v
= i
+ 1; /* must be non-NULL. */
371 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
372 perror("pthread_setspecific");
378 spin_lock(&__thread_id_map_mutex
);
379 for (i
= 0; i
< NR_THREADS
; i
++) {
380 if (__thread_id_map
[i
] == tid
)
381 spin_unlock(&__thread_id_map_mutex
);
384 spin_unlock(&__thread_id_map_mutex
);
385 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
390 static int smp_thread_id(void)
394 id
= pthread_getspecific(thread_id_key
);
396 return __smp_thread_id();
397 return (long)(id
- 1);
400 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
405 spin_lock(&__thread_id_map_mutex
);
406 for (i
= 0; i
< NR_THREADS
; i
++) {
407 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
410 if (i
>= NR_THREADS
) {
411 spin_unlock(&__thread_id_map_mutex
);
412 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
415 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
416 spin_unlock(&__thread_id_map_mutex
);
417 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
418 perror("create_thread:pthread_create");
421 __thread_id_map
[i
] = tid
;
425 static void *wait_thread(thread_id_t tid
)
430 for (i
= 0; i
< NR_THREADS
; i
++) {
431 if (__thread_id_map
[i
] == tid
)
434 if (i
>= NR_THREADS
){
435 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
439 if (pthread_join(tid
, &vp
) != 0) {
440 perror("wait_thread:pthread_join");
443 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
447 static void wait_all_threads(void)
452 for (i
= 1; i
< NR_THREADS
; i
++) {
453 tid
= __thread_id_map
[i
];
454 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
455 tid
!= __THREAD_ID_MAP_WAITING
)
456 (void)wait_thread(tid
);
460 static void run_on(int cpu
)
466 sched_setaffinity(0, sizeof(mask
), &mask
);
470 * timekeeping -- very crude -- should use MONOTONIC...
473 long long get_microseconds(void)
477 if (gettimeofday(&tv
, NULL
) != 0)
479 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
483 * Per-thread variables.
486 #define DEFINE_PER_THREAD(type, name) \
489 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
490 } __per_thread_##name[NR_THREADS];
491 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
493 #define per_thread(name, thread) __per_thread_##name[thread].v
494 #define __get_thread_var(name) per_thread(name, smp_thread_id())
496 #define init_per_thread(name, v) \
499 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
500 per_thread(name, __i_p_t_i) = v; \
504 * CPU traversal primitives.
509 #endif /* #ifndef NR_CPUS */
511 #define for_each_possible_cpu(cpu) \
512 for (cpu = 0; cpu < NR_CPUS; cpu++)
513 #define for_each_online_cpu(cpu) \
514 for (cpu = 0; cpu < NR_CPUS; cpu++)
520 #define DEFINE_PER_CPU(type, name) \
523 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
524 } __per_cpu_##name[NR_CPUS]
525 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
527 DEFINE_PER_THREAD(int, smp_processor_id
);
529 #define per_cpu(name, thread) __per_cpu_##name[thread].v
530 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
532 #define init_per_cpu(name, v) \
535 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
536 per_cpu(name, __i_p_c_i) = v; \
540 * CPU state checking (crowbarred).
543 #define idle_cpu(cpu) 0
544 #define in_softirq() 1
545 #define hardirq_count() 0
546 #define PREEMPT_SHIFT 0
547 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
548 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
549 #define PREEMPT_BITS 8
550 #define SOFTIRQ_BITS 8
556 struct notifier_block
{
557 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
558 struct notifier_block
*next
;
562 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
563 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
564 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
565 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
566 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
567 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
568 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
569 * not handling interrupts, soon dead */
570 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
573 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
574 * operation in progress
576 #define CPU_TASKS_FROZEN 0x0010
578 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
579 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
580 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
581 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
582 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
583 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
584 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
586 /* Hibernation and suspend events */
587 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
588 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
589 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
590 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
591 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
592 #define PM_POST_RESTORE 0x0006 /* Restore failed */
594 #define NOTIFY_DONE 0x0000 /* Don't care */
595 #define NOTIFY_OK 0x0001 /* Suits me */
596 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
597 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
598 /* Bad/Veto action */
600 * Clean way to return from the notifier and stop further calls.
602 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
608 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
611 * Initialization -- Must be called before calling any primitives.
614 static void smp_init(void)
618 spin_lock_init(&__thread_id_map_mutex
);
619 __thread_id_map
[0] = pthread_self();
620 for (i
= 1; i
< NR_THREADS
; i
++)
621 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
622 init_per_thread(smp_processor_id
, 0);
623 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
624 perror("pthread_key_create");
629 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
631 #ifndef _LINUX_LIST_H
632 #define _LINUX_LIST_H
634 #define LIST_POISON1 ((void *) 0x00100100)
635 #define LIST_POISON2 ((void *) 0x00200200)
637 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
638 #define container_of(ptr, type, member) ({ \
639 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
640 (type *)( (char *)__mptr - offsetof(type,member) );})
643 * Simple doubly linked list implementation.
645 * Some of the internal functions ("__xxx") are useful when
646 * manipulating whole lists rather than single entries, as
647 * sometimes we already know the next/prev entries and we can
648 * generate better code by using them directly rather than
649 * using the generic single-entry routines.
653 struct list_head
*next
, *prev
;
656 #define LIST_HEAD_INIT(name) { &(name), &(name) }
658 #define LIST_HEAD(name) \
659 struct list_head name = LIST_HEAD_INIT(name)
661 static inline void INIT_LIST_HEAD(struct list_head
*list
)
668 * Insert a new entry between two known consecutive entries.
670 * This is only for internal list manipulation where we know
671 * the prev/next entries already!
673 #ifndef CONFIG_DEBUG_LIST
674 static inline void __list_add(struct list_head
*new,
675 struct list_head
*prev
,
676 struct list_head
*next
)
684 extern void __list_add(struct list_head
*new,
685 struct list_head
*prev
,
686 struct list_head
*next
);
690 * list_add - add a new entry
691 * @new: new entry to be added
692 * @head: list head to add it after
694 * Insert a new entry after the specified head.
695 * This is good for implementing stacks.
697 static inline void list_add(struct list_head
*new, struct list_head
*head
)
699 __list_add(new, head
, head
->next
);
704 * list_add_tail - add a new entry
705 * @new: new entry to be added
706 * @head: list head to add it before
708 * Insert a new entry before the specified head.
709 * This is useful for implementing queues.
711 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
713 __list_add(new, head
->prev
, head
);
717 * Delete a list entry by making the prev/next entries
718 * point to each other.
720 * This is only for internal list manipulation where we know
721 * the prev/next entries already!
723 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
730 * list_del - deletes entry from list.
731 * @entry: the element to delete from the list.
732 * Note: list_empty() on entry does not return true after this, the entry is
733 * in an undefined state.
735 #ifndef CONFIG_DEBUG_LIST
736 static inline void list_del(struct list_head
*entry
)
738 __list_del(entry
->prev
, entry
->next
);
739 entry
->next
= LIST_POISON1
;
740 entry
->prev
= LIST_POISON2
;
743 extern void list_del(struct list_head
*entry
);
747 * list_replace - replace old entry by new one
748 * @old : the element to be replaced
749 * @new : the new element to insert
751 * If @old was empty, it will be overwritten.
753 static inline void list_replace(struct list_head
*old
,
754 struct list_head
*new)
756 new->next
= old
->next
;
757 new->next
->prev
= new;
758 new->prev
= old
->prev
;
759 new->prev
->next
= new;
762 static inline void list_replace_init(struct list_head
*old
,
763 struct list_head
*new)
765 list_replace(old
, new);
770 * list_del_init - deletes entry from list and reinitialize it.
771 * @entry: the element to delete from the list.
773 static inline void list_del_init(struct list_head
*entry
)
775 __list_del(entry
->prev
, entry
->next
);
776 INIT_LIST_HEAD(entry
);
780 * list_move - delete from one list and add as another's head
781 * @list: the entry to move
782 * @head: the head that will precede our entry
784 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
786 __list_del(list
->prev
, list
->next
);
787 list_add(list
, head
);
791 * list_move_tail - delete from one list and add as another's tail
792 * @list: the entry to move
793 * @head: the head that will follow our entry
795 static inline void list_move_tail(struct list_head
*list
,
796 struct list_head
*head
)
798 __list_del(list
->prev
, list
->next
);
799 list_add_tail(list
, head
);
803 * list_is_last - tests whether @list is the last entry in list @head
804 * @list: the entry to test
805 * @head: the head of the list
807 static inline int list_is_last(const struct list_head
*list
,
808 const struct list_head
*head
)
810 return list
->next
== head
;
814 * list_empty - tests whether a list is empty
815 * @head: the list to test.
817 static inline int list_empty(const struct list_head
*head
)
819 return head
->next
== head
;
823 * list_empty_careful - tests whether a list is empty and not being modified
824 * @head: the list to test
827 * tests whether a list is empty _and_ checks that no other CPU might be
828 * in the process of modifying either member (next or prev)
830 * NOTE: using list_empty_careful() without synchronization
831 * can only be safe if the only activity that can happen
832 * to the list entry is list_del_init(). Eg. it cannot be used
833 * if another CPU could re-list_add() it.
835 static inline int list_empty_careful(const struct list_head
*head
)
837 struct list_head
*next
= head
->next
;
838 return (next
== head
) && (next
== head
->prev
);
842 * list_is_singular - tests whether a list has just one entry.
843 * @head: the list to test.
845 static inline int list_is_singular(const struct list_head
*head
)
847 return !list_empty(head
) && (head
->next
== head
->prev
);
850 static inline void __list_cut_position(struct list_head
*list
,
851 struct list_head
*head
, struct list_head
*entry
)
853 struct list_head
*new_first
= entry
->next
;
854 list
->next
= head
->next
;
855 list
->next
->prev
= list
;
858 head
->next
= new_first
;
859 new_first
->prev
= head
;
863 * list_cut_position - cut a list into two
864 * @list: a new list to add all removed entries
865 * @head: a list with entries
866 * @entry: an entry within head, could be the head itself
867 * and if so we won't cut the list
869 * This helper moves the initial part of @head, up to and
870 * including @entry, from @head to @list. You should
871 * pass on @entry an element you know is on @head. @list
872 * should be an empty list or a list you do not care about
876 static inline void list_cut_position(struct list_head
*list
,
877 struct list_head
*head
, struct list_head
*entry
)
879 if (list_empty(head
))
881 if (list_is_singular(head
) &&
882 (head
->next
!= entry
&& head
!= entry
))
885 INIT_LIST_HEAD(list
);
887 __list_cut_position(list
, head
, entry
);
890 static inline void __list_splice(const struct list_head
*list
,
891 struct list_head
*prev
,
892 struct list_head
*next
)
894 struct list_head
*first
= list
->next
;
895 struct list_head
*last
= list
->prev
;
905 * list_splice - join two lists, this is designed for stacks
906 * @list: the new list to add.
907 * @head: the place to add it in the first list.
909 static inline void list_splice(const struct list_head
*list
,
910 struct list_head
*head
)
912 if (!list_empty(list
))
913 __list_splice(list
, head
, head
->next
);
917 * list_splice_tail - join two lists, each list being a queue
918 * @list: the new list to add.
919 * @head: the place to add it in the first list.
921 static inline void list_splice_tail(struct list_head
*list
,
922 struct list_head
*head
)
924 if (!list_empty(list
))
925 __list_splice(list
, head
->prev
, head
);
929 * list_splice_init - join two lists and reinitialise the emptied list.
930 * @list: the new list to add.
931 * @head: the place to add it in the first list.
933 * The list at @list is reinitialised
935 static inline void list_splice_init(struct list_head
*list
,
936 struct list_head
*head
)
938 if (!list_empty(list
)) {
939 __list_splice(list
, head
, head
->next
);
940 INIT_LIST_HEAD(list
);
945 * list_splice_tail_init - join two lists and reinitialise the emptied list
946 * @list: the new list to add.
947 * @head: the place to add it in the first list.
949 * Each of the lists is a queue.
950 * The list at @list is reinitialised
952 static inline void list_splice_tail_init(struct list_head
*list
,
953 struct list_head
*head
)
955 if (!list_empty(list
)) {
956 __list_splice(list
, head
->prev
, head
);
957 INIT_LIST_HEAD(list
);
962 * list_entry - get the struct for this entry
963 * @ptr: the &struct list_head pointer.
964 * @type: the type of the struct this is embedded in.
965 * @member: the name of the list_struct within the struct.
967 #define list_entry(ptr, type, member) \
968 container_of(ptr, type, member)
971 * list_first_entry - get the first element from a list
972 * @ptr: the list head to take the element from.
973 * @type: the type of the struct this is embedded in.
974 * @member: the name of the list_struct within the struct.
976 * Note, that list is expected to be not empty.
978 #define list_first_entry(ptr, type, member) \
979 list_entry((ptr)->next, type, member)
982 * list_for_each - iterate over a list
983 * @pos: the &struct list_head to use as a loop cursor.
984 * @head: the head for your list.
986 #define list_for_each(pos, head) \
987 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
991 * __list_for_each - iterate over a list
992 * @pos: the &struct list_head to use as a loop cursor.
993 * @head: the head for your list.
995 * This variant differs from list_for_each() in that it's the
996 * simplest possible list iteration code, no prefetching is done.
997 * Use this for code that knows the list to be very short (empty
998 * or 1 entry) most of the time.
1000 #define __list_for_each(pos, head) \
1001 for (pos = (head)->next; pos != (head); pos = pos->next)
1004 * list_for_each_prev - iterate over a list backwards
1005 * @pos: the &struct list_head to use as a loop cursor.
1006 * @head: the head for your list.
1008 #define list_for_each_prev(pos, head) \
1009 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1013 * list_for_each_safe - iterate over a list safe against removal of list entry
1014 * @pos: the &struct list_head to use as a loop cursor.
1015 * @n: another &struct list_head to use as temporary storage
1016 * @head: the head for your list.
1018 #define list_for_each_safe(pos, n, head) \
1019 for (pos = (head)->next, n = pos->next; pos != (head); \
1020 pos = n, n = pos->next)
1023 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1024 * @pos: the &struct list_head to use as a loop cursor.
1025 * @n: another &struct list_head to use as temporary storage
1026 * @head: the head for your list.
1028 #define list_for_each_prev_safe(pos, n, head) \
1029 for (pos = (head)->prev, n = pos->prev; \
1030 prefetch(pos->prev), pos != (head); \
1031 pos = n, n = pos->prev)
1034 * list_for_each_entry - iterate over list of given type
1035 * @pos: the type * to use as a loop cursor.
1036 * @head: the head for your list.
1037 * @member: the name of the list_struct within the struct.
1039 #define list_for_each_entry(pos, head, member) \
1040 for (pos = list_entry((head)->next, typeof(*pos), member); \
1041 prefetch(pos->member.next), &pos->member != (head); \
1042 pos = list_entry(pos->member.next, typeof(*pos), member))
1045 * list_for_each_entry_reverse - iterate backwards over list of given type.
1046 * @pos: the type * to use as a loop cursor.
1047 * @head: the head for your list.
1048 * @member: the name of the list_struct within the struct.
1050 #define list_for_each_entry_reverse(pos, head, member) \
1051 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1052 prefetch(pos->member.prev), &pos->member != (head); \
1053 pos = list_entry(pos->member.prev, typeof(*pos), member))
1056 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1057 * @pos: the type * to use as a start point
1058 * @head: the head of the list
1059 * @member: the name of the list_struct within the struct.
1061 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1063 #define list_prepare_entry(pos, head, member) \
1064 ((pos) ? : list_entry(head, typeof(*pos), member))
1067 * list_for_each_entry_continue - continue iteration over list of given type
1068 * @pos: the type * to use as a loop cursor.
1069 * @head: the head for your list.
1070 * @member: the name of the list_struct within the struct.
1072 * Continue to iterate over list of given type, continuing after
1073 * the current position.
1075 #define list_for_each_entry_continue(pos, head, member) \
1076 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1077 prefetch(pos->member.next), &pos->member != (head); \
1078 pos = list_entry(pos->member.next, typeof(*pos), member))
1081 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1082 * @pos: the type * to use as a loop cursor.
1083 * @head: the head for your list.
1084 * @member: the name of the list_struct within the struct.
1086 * Start to iterate over list of given type backwards, continuing after
1087 * the current position.
1089 #define list_for_each_entry_continue_reverse(pos, head, member) \
1090 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1091 prefetch(pos->member.prev), &pos->member != (head); \
1092 pos = list_entry(pos->member.prev, typeof(*pos), member))
1095 * list_for_each_entry_from - iterate over list of given type from the current point
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 * Iterate over list of given type, continuing from current position.
1102 #define list_for_each_entry_from(pos, head, member) \
1103 for (; prefetch(pos->member.next), &pos->member != (head); \
1104 pos = list_entry(pos->member.next, typeof(*pos), member))
1107 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1108 * @pos: the type * to use as a loop cursor.
1109 * @n: another type * to use as temporary storage
1110 * @head: the head for your list.
1111 * @member: the name of the list_struct within the struct.
1113 #define list_for_each_entry_safe(pos, n, head, member) \
1114 for (pos = list_entry((head)->next, typeof(*pos), member), \
1115 n = list_entry(pos->member.next, typeof(*pos), member); \
1116 &pos->member != (head); \
1117 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1120 * list_for_each_entry_safe_continue
1121 * @pos: the type * to use as a loop cursor.
1122 * @n: another type * to use as temporary storage
1123 * @head: the head for your list.
1124 * @member: the name of the list_struct within the struct.
1126 * Iterate over list of given type, continuing after current point,
1127 * safe against removal of list entry.
1129 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1130 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1131 n = list_entry(pos->member.next, typeof(*pos), member); \
1132 &pos->member != (head); \
1133 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1136 * list_for_each_entry_safe_from
1137 * @pos: the type * to use as a loop cursor.
1138 * @n: another type * to use as temporary storage
1139 * @head: the head for your list.
1140 * @member: the name of the list_struct within the struct.
1142 * Iterate over list of given type from current point, safe against
1143 * removal of list entry.
1145 #define list_for_each_entry_safe_from(pos, n, head, member) \
1146 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1147 &pos->member != (head); \
1148 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1151 * list_for_each_entry_safe_reverse
1152 * @pos: the type * to use as a loop cursor.
1153 * @n: another type * to use as temporary storage
1154 * @head: the head for your list.
1155 * @member: the name of the list_struct within the struct.
1157 * Iterate backwards over list of given type, safe against removal
1160 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1161 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1162 n = list_entry(pos->member.prev, typeof(*pos), member); \
1163 &pos->member != (head); \
1164 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1167 * Double linked lists with a single pointer list head.
1168 * Mostly useful for hash tables where the two pointer list head is
1170 * You lose the ability to access the tail in O(1).
1174 struct hlist_node
*first
;
1178 struct hlist_node
*next
, **pprev
;
1181 #define HLIST_HEAD_INIT { .first = NULL }
1182 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1183 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1184 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
1190 static inline int hlist_unhashed(const struct hlist_node
*h
)
1195 static inline int hlist_empty(const struct hlist_head
*h
)
1200 static inline void __hlist_del(struct hlist_node
*n
)
1202 struct hlist_node
*next
= n
->next
;
1203 struct hlist_node
**pprev
= n
->pprev
;
1206 next
->pprev
= pprev
;
1209 static inline void hlist_del(struct hlist_node
*n
)
1212 n
->next
= LIST_POISON1
;
1213 n
->pprev
= LIST_POISON2
;
1216 static inline void hlist_del_init(struct hlist_node
*n
)
1218 if (!hlist_unhashed(n
)) {
1224 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
1226 struct hlist_node
*first
= h
->first
;
1229 first
->pprev
= &n
->next
;
1231 n
->pprev
= &h
->first
;
1234 /* next must be != NULL */
1235 static inline void hlist_add_before(struct hlist_node
*n
,
1236 struct hlist_node
*next
)
1238 n
->pprev
= next
->pprev
;
1240 next
->pprev
= &n
->next
;
1244 static inline void hlist_add_after(struct hlist_node
*n
,
1245 struct hlist_node
*next
)
1247 next
->next
= n
->next
;
1249 next
->pprev
= &n
->next
;
1252 next
->next
->pprev
= &next
->next
;
1256 * Move a list from one list head to another. Fixup the pprev
1257 * reference of the first entry if it exists.
1259 static inline void hlist_move_list(struct hlist_head
*old
,
1260 struct hlist_head
*new)
1262 new->first
= old
->first
;
1264 new->first
->pprev
= &new->first
;
1268 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1270 #define hlist_for_each(pos, head) \
1271 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1274 #define hlist_for_each_safe(pos, n, head) \
1275 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1279 * hlist_for_each_entry - iterate over list of given type
1280 * @tpos: the type * to use as a loop cursor.
1281 * @pos: the &struct hlist_node to use as a loop cursor.
1282 * @head: the head for your list.
1283 * @member: the name of the hlist_node within the struct.
1285 #define hlist_for_each_entry(tpos, pos, head, member) \
1286 for (pos = (head)->first; \
1287 pos && ({ prefetch(pos->next); 1;}) && \
1288 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1292 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1293 * @tpos: the type * to use as a loop cursor.
1294 * @pos: the &struct hlist_node to use as a loop cursor.
1295 * @member: the name of the hlist_node within the struct.
1297 #define hlist_for_each_entry_continue(tpos, pos, member) \
1298 for (pos = (pos)->next; \
1299 pos && ({ prefetch(pos->next); 1;}) && \
1300 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1304 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1305 * @tpos: the type * to use as a loop cursor.
1306 * @pos: the &struct hlist_node to use as a loop cursor.
1307 * @member: the name of the hlist_node within the struct.
1309 #define hlist_for_each_entry_from(tpos, pos, member) \
1310 for (; pos && ({ prefetch(pos->next); 1;}) && \
1311 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1315 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1316 * @tpos: the type * to use as a loop cursor.
1317 * @pos: the &struct hlist_node to use as a loop cursor.
1318 * @n: another &struct hlist_node to use as temporary storage
1319 * @head: the head for your list.
1320 * @member: the name of the hlist_node within the struct.
1322 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1323 for (pos = (head)->first; \
1324 pos && ({ n = pos->next; 1; }) && \
1325 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \