6 * common.h: Common Linux kernel-isms.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; but version 2 of the License only due
11 * to code included from the Linux kernel.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Copyright (c) 2006 Paul E. McKenney, IBM.
24 * Much code taken from the Linux kernel. For such code, the option
25 * to redistribute under later versions of GPL might not be available.
28 #ifndef __always_inline
29 #define __always_inline inline
32 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
33 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
36 # define stringify_in_c(...) __VA_ARGS__
37 # define ASM_CONST(x) x
39 /* This version of stringify will deal with commas... */
40 # define __stringify_in_c(...) #__VA_ARGS__
41 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
42 # define __ASM_CONST(x) x##UL
43 # define ASM_CONST(x) __ASM_CONST(x)
48 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
50 * This program is free software; you can redistribute it and/or modify
51 * it under the terms of the GNU General Public License as published by
52 * the Free Software Foundation, but version 2 only due to inclusion
53 * of Linux-kernel code.
55 * This program is distributed in the hope that it will be useful,
56 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 * GNU General Public License for more details.
60 * You should have received a copy of the GNU General Public License
61 * along with this program; if not, write to the Free Software
62 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
64 * Copyright (c) 2006 Paul E. McKenney, IBM.
66 * Much code taken from the Linux kernel. For such code, the option
67 * to redistribute under later versions of GPL might not be available.
74 /* #define CACHE_LINE_SIZE 64 */
75 #define ____cacheline_internodealigned_in_smp \
76 __attribute__((__aligned__(1 << 6)))
78 #define LOCK_PREFIX "lock ; "
80 #if 0 /* duplicate with arch_atomic.h */
82 * Atomic data structure, initialization, and access.
85 typedef struct { volatile int counter
; } atomic_t
;
87 #define ATOMIC_INIT(i) { (i) }
89 #define atomic_read(v) ((v)->counter)
90 #define atomic_set(v, i) (((v)->counter) = (i))
97 * atomic_add - add integer to atomic variable
98 * @i: integer value to add
99 * @v: pointer of type atomic_t
101 * Atomically adds @i to @v.
104 static __inline__
void atomic_add(int i
, atomic_t
*v
)
106 (void)__sync_fetch_and_add(&v
->counter
, i
);
110 * atomic_sub - subtract the atomic variable
111 * @i: integer value to subtract
112 * @v: pointer of type atomic_t
114 * Atomically subtracts @i from @v.
116 static __inline__
void atomic_sub(int i
, atomic_t
*v
)
118 (void)__sync_fetch_and_add(&v
->counter
, -i
);
122 * atomic_sub_and_test - subtract value from variable and test result
123 * @i: integer value to subtract
124 * @v: pointer of type atomic_t
126 * Atomically subtracts @i from @v and returns
127 * true if the result is zero, or false for all
130 static __inline__
int atomic_sub_and_test(int i
, atomic_t
*v
)
132 return __sync_add_and_fetch(&v
->counter
, -i
) == 0;
136 * atomic_inc - increment atomic variable
137 * @v: pointer of type atomic_t
139 * Atomically increments @v by 1.
141 static __inline__
void atomic_inc(atomic_t
*v
)
143 (void)__sync_fetch_and_add(&v
->counter
, 1);
147 * atomic_dec - decrement atomic variable
148 * @v: pointer of type atomic_t
150 * Atomically decrements @v by 1.
152 static __inline__
void atomic_dec(atomic_t
*v
)
154 (void)__sync_fetch_and_add(&v
->counter
, -1);
158 * atomic_dec_and_test - decrement and test
159 * @v: pointer of type atomic_t
161 * Atomically decrements @v by 1 and
162 * returns true if the result is 0, or false for all other
165 static __inline__
int atomic_dec_and_test(atomic_t
*v
)
167 return __sync_add_and_fetch(&v
->counter
, -1) == 0;
171 * atomic_inc_and_test - increment and test
172 * @v: pointer of type atomic_t
174 * Atomically increments @v by 1
175 * and returns true if the result is zero, or false for all
178 static __inline__
int atomic_inc_and_test(atomic_t
*v
)
180 return __sync_add_and_fetch(&v
->counter
, 1) == 0;
184 * atomic_add_negative - add and test if negative
185 * @v: pointer of type atomic_t
186 * @i: integer value to add
188 * Atomically adds @i to @v and returns true
189 * if the result is negative, or false when
190 * result is greater than or equal to zero.
192 static __inline__
int atomic_add_negative(int i
, atomic_t
*v
)
194 return __sync_add_and_fetch(&v
->counter
, i
) < 0;
198 * atomic_add_return - add and return
199 * @v: pointer of type atomic_t
200 * @i: integer value to add
202 * Atomically adds @i to @v and returns @i + @v
204 static __inline__
int atomic_add_return(int i
, atomic_t
*v
)
206 return __sync_add_and_fetch(&v
->counter
, i
);
209 static __inline__
int atomic_sub_return(int i
, atomic_t
*v
)
211 return atomic_add_return(-i
,v
);
214 static inline unsigned int
215 cmpxchg(volatile long *ptr
, long oldval
, long newval
)
217 return __sync_val_compare_and_swap(ptr
, oldval
, newval
);
220 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
221 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
224 * atomic_add_unless - add unless the number is a given value
225 * @v: pointer of type atomic_t
226 * @a: the amount to add to v...
227 * @u: ...unless v is equal to u.
229 * Atomically adds @a to @v, so long as it was not @u.
230 * Returns non-zero if @v was not @u, and zero otherwise.
232 #define atomic_add_unless(v, a, u) \
235 c = atomic_read(v); \
237 if (unlikely(c == (u))) \
239 old = atomic_cmpxchg((v), c, c + (a)); \
240 if (likely(old == c)) \
246 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
248 #define atomic_inc_return(v) (atomic_add_return(1,v))
249 #define atomic_dec_return(v) (atomic_sub_return(1,v))
251 /* Atomic operations are already serializing on x86 */
252 #define smp_mb__before_atomic_dec() barrier()
253 #define smp_mb__after_atomic_dec() barrier()
254 #define smp_mb__before_atomic_inc() barrier()
255 #define smp_mb__after_atomic_inc() barrier()
257 #endif //0 /* duplicate with arch_atomic.h */
260 * api_pthreads.h: API mapping to pthreads environment.
262 * This program is free software; you can redistribute it and/or modify
263 * it under the terms of the GNU General Public License as published by
264 * the Free Software Foundation; either version 2 of the License, or
265 * (at your option) any later version. However, please note that much
266 * of the code in this file derives from the Linux kernel, and that such
267 * code may not be available except under GPLv2.
269 * This program is distributed in the hope that it will be useful,
270 * but WITHOUT ANY WARRANTY; without even the implied warranty of
271 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
272 * GNU General Public License for more details.
274 * You should have received a copy of the GNU General Public License
275 * along with this program; if not, write to the Free Software
276 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
278 * Copyright (c) 2006 Paul E. McKenney, IBM.
285 #include <sys/types.h>
289 #include <sys/param.h>
290 /* #include "atomic.h" */
295 #define container_of(ptr, type, member) ({ \
296 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
297 (type *)( (char *)__mptr - offsetof(type,member) );})
300 * Default machine parameters.
303 #ifndef CACHE_LINE_SIZE
304 /* #define CACHE_LINE_SIZE 128 */
305 #endif /* #ifndef CACHE_LINE_SIZE */
308 * Exclusive locking primitives.
311 typedef pthread_mutex_t spinlock_t
;
313 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
314 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
316 static void spin_lock_init(spinlock_t
*sp
)
318 if (pthread_mutex_init(sp
, NULL
) != 0) {
319 perror("spin_lock_init:pthread_mutex_init");
324 static void spin_lock(spinlock_t
*sp
)
326 if (pthread_mutex_lock(sp
) != 0) {
327 perror("spin_lock:pthread_mutex_lock");
332 static void spin_unlock(spinlock_t
*sp
)
334 if (pthread_mutex_unlock(sp
) != 0) {
335 perror("spin_unlock:pthread_mutex_unlock");
340 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
341 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
344 * Thread creation/destruction primitives.
347 typedef pthread_t thread_id_t
;
349 #define NR_THREADS 128
351 #define __THREAD_ID_MAP_EMPTY 0
352 #define __THREAD_ID_MAP_WAITING 1
353 thread_id_t __thread_id_map
[NR_THREADS
];
354 spinlock_t __thread_id_map_mutex
;
356 #define for_each_thread(t) \
357 for (t = 0; t < NR_THREADS; t++)
359 #define for_each_running_thread(t) \
360 for (t = 0; t < NR_THREADS; t++) \
361 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
362 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
364 pthread_key_t thread_id_key
;
366 static int __smp_thread_id(void)
369 thread_id_t tid
= pthread_self();
371 for (i
= 0; i
< NR_THREADS
; i
++) {
372 if (__thread_id_map
[i
] == tid
) {
373 long v
= i
+ 1; /* must be non-NULL. */
375 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
376 perror("pthread_setspecific");
382 spin_lock(&__thread_id_map_mutex
);
383 for (i
= 0; i
< NR_THREADS
; i
++) {
384 if (__thread_id_map
[i
] == tid
)
385 spin_unlock(&__thread_id_map_mutex
);
388 spin_unlock(&__thread_id_map_mutex
);
389 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
394 static int smp_thread_id(void)
398 id
= pthread_getspecific(thread_id_key
);
400 return __smp_thread_id();
401 return (long)(id
- 1);
404 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
409 spin_lock(&__thread_id_map_mutex
);
410 for (i
= 0; i
< NR_THREADS
; i
++) {
411 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
414 if (i
>= NR_THREADS
) {
415 spin_unlock(&__thread_id_map_mutex
);
416 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
419 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
420 spin_unlock(&__thread_id_map_mutex
);
421 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
422 perror("create_thread:pthread_create");
425 __thread_id_map
[i
] = tid
;
429 static void *wait_thread(thread_id_t tid
)
434 for (i
= 0; i
< NR_THREADS
; i
++) {
435 if (__thread_id_map
[i
] == tid
)
438 if (i
>= NR_THREADS
){
439 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
443 if (pthread_join(tid
, &vp
) != 0) {
444 perror("wait_thread:pthread_join");
447 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
451 static void wait_all_threads(void)
456 for (i
= 1; i
< NR_THREADS
; i
++) {
457 tid
= __thread_id_map
[i
];
458 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
459 tid
!= __THREAD_ID_MAP_WAITING
)
460 (void)wait_thread(tid
);
464 static void run_on(int cpu
)
470 sched_setaffinity(0, sizeof(mask
), &mask
);
474 * timekeeping -- very crude -- should use MONOTONIC...
477 long long get_microseconds(void)
481 if (gettimeofday(&tv
, NULL
) != 0)
483 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
487 * Per-thread variables.
490 #define DEFINE_PER_THREAD(type, name) \
493 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
494 } __per_thread_##name[NR_THREADS];
495 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
497 #define per_thread(name, thread) __per_thread_##name[thread].v
498 #define __get_thread_var(name) per_thread(name, smp_thread_id())
500 #define init_per_thread(name, v) \
503 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
504 per_thread(name, __i_p_t_i) = v; \
508 * CPU traversal primitives.
513 #endif /* #ifndef NR_CPUS */
515 #define for_each_possible_cpu(cpu) \
516 for (cpu = 0; cpu < NR_CPUS; cpu++)
517 #define for_each_online_cpu(cpu) \
518 for (cpu = 0; cpu < NR_CPUS; cpu++)
524 #define DEFINE_PER_CPU(type, name) \
527 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
528 } __per_cpu_##name[NR_CPUS]
529 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
531 DEFINE_PER_THREAD(int, smp_processor_id
);
533 #define per_cpu(name, thread) __per_cpu_##name[thread].v
534 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
536 #define init_per_cpu(name, v) \
539 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
540 per_cpu(name, __i_p_c_i) = v; \
544 * CPU state checking (crowbarred).
547 #define idle_cpu(cpu) 0
548 #define in_softirq() 1
549 #define hardirq_count() 0
550 #define PREEMPT_SHIFT 0
551 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
552 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
553 #define PREEMPT_BITS 8
554 #define SOFTIRQ_BITS 8
560 struct notifier_block
{
561 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
562 struct notifier_block
*next
;
566 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
567 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
568 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
569 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
570 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
571 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
572 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
573 * not handling interrupts, soon dead */
574 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
577 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
578 * operation in progress
580 #define CPU_TASKS_FROZEN 0x0010
582 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
583 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
584 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
585 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
586 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
587 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
588 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
590 /* Hibernation and suspend events */
591 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
592 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
593 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
594 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
595 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
596 #define PM_POST_RESTORE 0x0006 /* Restore failed */
598 #define NOTIFY_DONE 0x0000 /* Don't care */
599 #define NOTIFY_OK 0x0001 /* Suits me */
600 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
601 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
602 /* Bad/Veto action */
604 * Clean way to return from the notifier and stop further calls.
606 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
612 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
615 * Initialization -- Must be called before calling any primitives.
618 static void smp_init(void)
622 spin_lock_init(&__thread_id_map_mutex
);
623 __thread_id_map
[0] = pthread_self();
624 for (i
= 1; i
< NR_THREADS
; i
++)
625 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
626 init_per_thread(smp_processor_id
, 0);
627 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
628 perror("pthread_key_create");
633 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
635 #ifndef _LINUX_LIST_H
636 #define _LINUX_LIST_H
638 #define LIST_POISON1 ((void *) 0x00100100)
639 #define LIST_POISON2 ((void *) 0x00200200)
641 #define container_of(ptr, type, member) ({ \
642 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
643 (type *)( (char *)__mptr - offsetof(type,member) );})
647 * Simple doubly linked list implementation.
649 * Some of the internal functions ("__xxx") are useful when
650 * manipulating whole lists rather than single entries, as
651 * sometimes we already know the next/prev entries and we can
652 * generate better code by using them directly rather than
653 * using the generic single-entry routines.
657 struct list_head
*next
, *prev
;
660 #define LIST_HEAD_INIT(name) { &(name), &(name) }
662 #define LIST_HEAD(name) \
663 struct list_head name = LIST_HEAD_INIT(name)
665 static inline void INIT_LIST_HEAD(struct list_head
*list
)
672 * Insert a new entry between two known consecutive entries.
674 * This is only for internal list manipulation where we know
675 * the prev/next entries already!
677 #ifndef CONFIG_DEBUG_LIST
678 static inline void __list_add(struct list_head
*new,
679 struct list_head
*prev
,
680 struct list_head
*next
)
688 extern void __list_add(struct list_head
*new,
689 struct list_head
*prev
,
690 struct list_head
*next
);
694 * list_add - add a new entry
695 * @new: new entry to be added
696 * @head: list head to add it after
698 * Insert a new entry after the specified head.
699 * This is good for implementing stacks.
701 static inline void list_add(struct list_head
*new, struct list_head
*head
)
703 __list_add(new, head
, head
->next
);
708 * list_add_tail - add a new entry
709 * @new: new entry to be added
710 * @head: list head to add it before
712 * Insert a new entry before the specified head.
713 * This is useful for implementing queues.
715 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
717 __list_add(new, head
->prev
, head
);
721 * Delete a list entry by making the prev/next entries
722 * point to each other.
724 * This is only for internal list manipulation where we know
725 * the prev/next entries already!
727 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
734 * list_del - deletes entry from list.
735 * @entry: the element to delete from the list.
736 * Note: list_empty() on entry does not return true after this, the entry is
737 * in an undefined state.
739 #ifndef CONFIG_DEBUG_LIST
740 static inline void list_del(struct list_head
*entry
)
742 __list_del(entry
->prev
, entry
->next
);
743 entry
->next
= LIST_POISON1
;
744 entry
->prev
= LIST_POISON2
;
747 extern void list_del(struct list_head
*entry
);
751 * list_replace - replace old entry by new one
752 * @old : the element to be replaced
753 * @new : the new element to insert
755 * If @old was empty, it will be overwritten.
757 static inline void list_replace(struct list_head
*old
,
758 struct list_head
*new)
760 new->next
= old
->next
;
761 new->next
->prev
= new;
762 new->prev
= old
->prev
;
763 new->prev
->next
= new;
766 static inline void list_replace_init(struct list_head
*old
,
767 struct list_head
*new)
769 list_replace(old
, new);
774 * list_del_init - deletes entry from list and reinitialize it.
775 * @entry: the element to delete from the list.
777 static inline void list_del_init(struct list_head
*entry
)
779 __list_del(entry
->prev
, entry
->next
);
780 INIT_LIST_HEAD(entry
);
784 * list_move - delete from one list and add as another's head
785 * @list: the entry to move
786 * @head: the head that will precede our entry
788 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
790 __list_del(list
->prev
, list
->next
);
791 list_add(list
, head
);
795 * list_move_tail - delete from one list and add as another's tail
796 * @list: the entry to move
797 * @head: the head that will follow our entry
799 static inline void list_move_tail(struct list_head
*list
,
800 struct list_head
*head
)
802 __list_del(list
->prev
, list
->next
);
803 list_add_tail(list
, head
);
807 * list_is_last - tests whether @list is the last entry in list @head
808 * @list: the entry to test
809 * @head: the head of the list
811 static inline int list_is_last(const struct list_head
*list
,
812 const struct list_head
*head
)
814 return list
->next
== head
;
818 * list_empty - tests whether a list is empty
819 * @head: the list to test.
821 static inline int list_empty(const struct list_head
*head
)
823 return head
->next
== head
;
827 * list_empty_careful - tests whether a list is empty and not being modified
828 * @head: the list to test
831 * tests whether a list is empty _and_ checks that no other CPU might be
832 * in the process of modifying either member (next or prev)
834 * NOTE: using list_empty_careful() without synchronization
835 * can only be safe if the only activity that can happen
836 * to the list entry is list_del_init(). Eg. it cannot be used
837 * if another CPU could re-list_add() it.
839 static inline int list_empty_careful(const struct list_head
*head
)
841 struct list_head
*next
= head
->next
;
842 return (next
== head
) && (next
== head
->prev
);
846 * list_is_singular - tests whether a list has just one entry.
847 * @head: the list to test.
849 static inline int list_is_singular(const struct list_head
*head
)
851 return !list_empty(head
) && (head
->next
== head
->prev
);
854 static inline void __list_cut_position(struct list_head
*list
,
855 struct list_head
*head
, struct list_head
*entry
)
857 struct list_head
*new_first
= entry
->next
;
858 list
->next
= head
->next
;
859 list
->next
->prev
= list
;
862 head
->next
= new_first
;
863 new_first
->prev
= head
;
867 * list_cut_position - cut a list into two
868 * @list: a new list to add all removed entries
869 * @head: a list with entries
870 * @entry: an entry within head, could be the head itself
871 * and if so we won't cut the list
873 * This helper moves the initial part of @head, up to and
874 * including @entry, from @head to @list. You should
875 * pass on @entry an element you know is on @head. @list
876 * should be an empty list or a list you do not care about
880 static inline void list_cut_position(struct list_head
*list
,
881 struct list_head
*head
, struct list_head
*entry
)
883 if (list_empty(head
))
885 if (list_is_singular(head
) &&
886 (head
->next
!= entry
&& head
!= entry
))
889 INIT_LIST_HEAD(list
);
891 __list_cut_position(list
, head
, entry
);
894 static inline void __list_splice(const struct list_head
*list
,
895 struct list_head
*prev
,
896 struct list_head
*next
)
898 struct list_head
*first
= list
->next
;
899 struct list_head
*last
= list
->prev
;
909 * list_splice - join two lists, this is designed for stacks
910 * @list: the new list to add.
911 * @head: the place to add it in the first list.
913 static inline void list_splice(const struct list_head
*list
,
914 struct list_head
*head
)
916 if (!list_empty(list
))
917 __list_splice(list
, head
, head
->next
);
921 * list_splice_tail - join two lists, each list being a queue
922 * @list: the new list to add.
923 * @head: the place to add it in the first list.
925 static inline void list_splice_tail(struct list_head
*list
,
926 struct list_head
*head
)
928 if (!list_empty(list
))
929 __list_splice(list
, head
->prev
, head
);
933 * list_splice_init - join two lists and reinitialise the emptied list.
934 * @list: the new list to add.
935 * @head: the place to add it in the first list.
937 * The list at @list is reinitialised
939 static inline void list_splice_init(struct list_head
*list
,
940 struct list_head
*head
)
942 if (!list_empty(list
)) {
943 __list_splice(list
, head
, head
->next
);
944 INIT_LIST_HEAD(list
);
949 * list_splice_tail_init - join two lists and reinitialise the emptied list
950 * @list: the new list to add.
951 * @head: the place to add it in the first list.
953 * Each of the lists is a queue.
954 * The list at @list is reinitialised
956 static inline void list_splice_tail_init(struct list_head
*list
,
957 struct list_head
*head
)
959 if (!list_empty(list
)) {
960 __list_splice(list
, head
->prev
, head
);
961 INIT_LIST_HEAD(list
);
966 * list_entry - get the struct for this entry
967 * @ptr: the &struct list_head pointer.
968 * @type: the type of the struct this is embedded in.
969 * @member: the name of the list_struct within the struct.
971 #define list_entry(ptr, type, member) \
972 container_of(ptr, type, member)
975 * list_first_entry - get the first element from a list
976 * @ptr: the list head to take the element from.
977 * @type: the type of the struct this is embedded in.
978 * @member: the name of the list_struct within the struct.
980 * Note, that list is expected to be not empty.
982 #define list_first_entry(ptr, type, member) \
983 list_entry((ptr)->next, type, member)
986 * list_for_each - iterate over a list
987 * @pos: the &struct list_head to use as a loop cursor.
988 * @head: the head for your list.
990 #define list_for_each(pos, head) \
991 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
995 * __list_for_each - iterate over a list
996 * @pos: the &struct list_head to use as a loop cursor.
997 * @head: the head for your list.
999 * This variant differs from list_for_each() in that it's the
1000 * simplest possible list iteration code, no prefetching is done.
1001 * Use this for code that knows the list to be very short (empty
1002 * or 1 entry) most of the time.
1004 #define __list_for_each(pos, head) \
1005 for (pos = (head)->next; pos != (head); pos = pos->next)
1008 * list_for_each_prev - iterate over a list backwards
1009 * @pos: the &struct list_head to use as a loop cursor.
1010 * @head: the head for your list.
1012 #define list_for_each_prev(pos, head) \
1013 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1017 * list_for_each_safe - iterate over a list safe against removal of list entry
1018 * @pos: the &struct list_head to use as a loop cursor.
1019 * @n: another &struct list_head to use as temporary storage
1020 * @head: the head for your list.
1022 #define list_for_each_safe(pos, n, head) \
1023 for (pos = (head)->next, n = pos->next; pos != (head); \
1024 pos = n, n = pos->next)
1027 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1028 * @pos: the &struct list_head to use as a loop cursor.
1029 * @n: another &struct list_head to use as temporary storage
1030 * @head: the head for your list.
1032 #define list_for_each_prev_safe(pos, n, head) \
1033 for (pos = (head)->prev, n = pos->prev; \
1034 prefetch(pos->prev), pos != (head); \
1035 pos = n, n = pos->prev)
1038 * list_for_each_entry - iterate over list of given type
1039 * @pos: the type * to use as a loop cursor.
1040 * @head: the head for your list.
1041 * @member: the name of the list_struct within the struct.
1043 #define list_for_each_entry(pos, head, member) \
1044 for (pos = list_entry((head)->next, typeof(*pos), member); \
1045 prefetch(pos->member.next), &pos->member != (head); \
1046 pos = list_entry(pos->member.next, typeof(*pos), member))
1049 * list_for_each_entry_reverse - iterate backwards over list of given type.
1050 * @pos: the type * to use as a loop cursor.
1051 * @head: the head for your list.
1052 * @member: the name of the list_struct within the struct.
1054 #define list_for_each_entry_reverse(pos, head, member) \
1055 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1056 prefetch(pos->member.prev), &pos->member != (head); \
1057 pos = list_entry(pos->member.prev, typeof(*pos), member))
1060 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1061 * @pos: the type * to use as a start point
1062 * @head: the head of the list
1063 * @member: the name of the list_struct within the struct.
1065 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1067 #define list_prepare_entry(pos, head, member) \
1068 ((pos) ? : list_entry(head, typeof(*pos), member))
1071 * list_for_each_entry_continue - continue iteration over list of given type
1072 * @pos: the type * to use as a loop cursor.
1073 * @head: the head for your list.
1074 * @member: the name of the list_struct within the struct.
1076 * Continue to iterate over list of given type, continuing after
1077 * the current position.
1079 #define list_for_each_entry_continue(pos, head, member) \
1080 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1081 prefetch(pos->member.next), &pos->member != (head); \
1082 pos = list_entry(pos->member.next, typeof(*pos), member))
1085 * list_for_each_entry_continue_reverse - iterate backwards from the given point
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 * Start to iterate over list of given type backwards, continuing after
1091 * the current position.
1093 #define list_for_each_entry_continue_reverse(pos, head, member) \
1094 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1095 prefetch(pos->member.prev), &pos->member != (head); \
1096 pos = list_entry(pos->member.prev, typeof(*pos), member))
1099 * list_for_each_entry_from - iterate over list of given type from the current 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 * Iterate over list of given type, continuing from current position.
1106 #define list_for_each_entry_from(pos, head, member) \
1107 for (; prefetch(pos->member.next), &pos->member != (head); \
1108 pos = list_entry(pos->member.next, typeof(*pos), member))
1111 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1112 * @pos: the type * to use as a loop cursor.
1113 * @n: another type * to use as temporary storage
1114 * @head: the head for your list.
1115 * @member: the name of the list_struct within the struct.
1117 #define list_for_each_entry_safe(pos, n, head, member) \
1118 for (pos = list_entry((head)->next, typeof(*pos), member), \
1119 n = list_entry(pos->member.next, typeof(*pos), member); \
1120 &pos->member != (head); \
1121 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1124 * list_for_each_entry_safe_continue
1125 * @pos: the type * to use as a loop cursor.
1126 * @n: another type * to use as temporary storage
1127 * @head: the head for your list.
1128 * @member: the name of the list_struct within the struct.
1130 * Iterate over list of given type, continuing after current point,
1131 * safe against removal of list entry.
1133 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1134 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1135 n = list_entry(pos->member.next, typeof(*pos), member); \
1136 &pos->member != (head); \
1137 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1140 * list_for_each_entry_safe_from
1141 * @pos: the type * to use as a loop cursor.
1142 * @n: another type * to use as temporary storage
1143 * @head: the head for your list.
1144 * @member: the name of the list_struct within the struct.
1146 * Iterate over list of given type from current point, safe against
1147 * removal of list entry.
1149 #define list_for_each_entry_safe_from(pos, n, head, member) \
1150 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1151 &pos->member != (head); \
1152 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1155 * list_for_each_entry_safe_reverse
1156 * @pos: the type * to use as a loop cursor.
1157 * @n: another type * to use as temporary storage
1158 * @head: the head for your list.
1159 * @member: the name of the list_struct within the struct.
1161 * Iterate backwards over list of given type, safe against removal
1164 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1165 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1166 n = list_entry(pos->member.prev, typeof(*pos), member); \
1167 &pos->member != (head); \
1168 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1173 * Double linked lists with a single pointer list head.
1174 * Mostly useful for hash tables where the two pointer list head is
1176 * You lose the ability to access the tail in O(1).
1180 struct hlist_node
*first
;
1184 struct hlist_node
*next
, **pprev
;
1187 #define HLIST_HEAD_INIT { .first = NULL }
1188 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1189 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1190 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
1196 static inline int hlist_unhashed(const struct hlist_node
*h
)
1201 static inline int hlist_empty(const struct hlist_head
*h
)
1206 static inline void __hlist_del(struct hlist_node
*n
)
1208 struct hlist_node
*next
= n
->next
;
1209 struct hlist_node
**pprev
= n
->pprev
;
1212 next
->pprev
= pprev
;
1215 static inline void hlist_del(struct hlist_node
*n
)
1218 n
->next
= LIST_POISON1
;
1219 n
->pprev
= LIST_POISON2
;
1222 static inline void hlist_del_init(struct hlist_node
*n
)
1224 if (!hlist_unhashed(n
)) {
1230 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
1232 struct hlist_node
*first
= h
->first
;
1235 first
->pprev
= &n
->next
;
1237 n
->pprev
= &h
->first
;
1240 /* next must be != NULL */
1241 static inline void hlist_add_before(struct hlist_node
*n
,
1242 struct hlist_node
*next
)
1244 n
->pprev
= next
->pprev
;
1246 next
->pprev
= &n
->next
;
1250 static inline void hlist_add_after(struct hlist_node
*n
,
1251 struct hlist_node
*next
)
1253 next
->next
= n
->next
;
1255 next
->pprev
= &n
->next
;
1258 next
->next
->pprev
= &next
->next
;
1262 * Move a list from one list head to another. Fixup the pprev
1263 * reference of the first entry if it exists.
1265 static inline void hlist_move_list(struct hlist_head
*old
,
1266 struct hlist_head
*new)
1268 new->first
= old
->first
;
1270 new->first
->pprev
= &new->first
;
1274 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1276 #define hlist_for_each(pos, head) \
1277 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1280 #define hlist_for_each_safe(pos, n, head) \
1281 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1285 * hlist_for_each_entry - iterate over list of given type
1286 * @tpos: the type * to use as a loop cursor.
1287 * @pos: the &struct hlist_node to use as a loop cursor.
1288 * @head: the head for your list.
1289 * @member: the name of the hlist_node within the struct.
1291 #define hlist_for_each_entry(tpos, pos, head, member) \
1292 for (pos = (head)->first; \
1293 pos && ({ prefetch(pos->next); 1;}) && \
1294 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1298 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1299 * @tpos: the type * to use as a loop cursor.
1300 * @pos: the &struct hlist_node to use as a loop cursor.
1301 * @member: the name of the hlist_node within the struct.
1303 #define hlist_for_each_entry_continue(tpos, pos, member) \
1304 for (pos = (pos)->next; \
1305 pos && ({ prefetch(pos->next); 1;}) && \
1306 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1310 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1311 * @tpos: the type * to use as a loop cursor.
1312 * @pos: the &struct hlist_node to use as a loop cursor.
1313 * @member: the name of the hlist_node within the struct.
1315 #define hlist_for_each_entry_from(tpos, pos, member) \
1316 for (; pos && ({ prefetch(pos->next); 1;}) && \
1317 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1321 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1322 * @tpos: the type * to use as a loop cursor.
1323 * @pos: the &struct hlist_node to use as a loop cursor.
1324 * @n: another &struct hlist_node to use as temporary storage
1325 * @head: the head for your list.
1326 * @member: the name of the hlist_node within the struct.
1328 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1329 for (pos = (head)->first; \
1330 pos && ({ n = pos->next; 1; }) && \
1331 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \