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 CAA_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() cmm_barrier()
255 #define smp_mb__after_atomic_dec() cmm_barrier()
256 #define smp_mb__before_atomic_inc() cmm_barrier()
257 #define smp_mb__after_atomic_inc() cmm_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" */
295 * Default machine parameters.
298 #ifndef CAA_CACHE_LINE_SIZE
299 /* #define CAA_CACHE_LINE_SIZE 128 */
300 #endif /* #ifndef CAA_CACHE_LINE_SIZE */
303 * Exclusive locking primitives.
306 typedef pthread_mutex_t spinlock_t
;
308 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
309 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
311 static void spin_lock_init(spinlock_t
*sp
)
313 if (pthread_mutex_init(sp
, NULL
) != 0) {
314 perror("spin_lock_init:pthread_mutex_init");
319 static void spin_lock(spinlock_t
*sp
)
321 if (pthread_mutex_lock(sp
) != 0) {
322 perror("spin_lock:pthread_mutex_lock");
327 static void spin_unlock(spinlock_t
*sp
)
329 if (pthread_mutex_unlock(sp
) != 0) {
330 perror("spin_unlock:pthread_mutex_unlock");
335 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
336 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
339 * Thread creation/destruction primitives.
342 typedef pthread_t thread_id_t
;
344 #define NR_THREADS 128
346 #define __THREAD_ID_MAP_EMPTY 0
347 #define __THREAD_ID_MAP_WAITING 1
348 thread_id_t __thread_id_map
[NR_THREADS
];
349 spinlock_t __thread_id_map_mutex
;
351 #define for_each_thread(t) \
352 for (t = 0; t < NR_THREADS; t++)
354 #define for_each_running_thread(t) \
355 for (t = 0; t < NR_THREADS; t++) \
356 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
357 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
359 pthread_key_t thread_id_key
;
361 static int __smp_thread_id(void)
364 thread_id_t tid
= pthread_self();
366 for (i
= 0; i
< NR_THREADS
; i
++) {
367 if (__thread_id_map
[i
] == tid
) {
368 long v
= i
+ 1; /* must be non-NULL. */
370 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
371 perror("pthread_setspecific");
377 spin_lock(&__thread_id_map_mutex
);
378 for (i
= 0; i
< NR_THREADS
; i
++) {
379 if (__thread_id_map
[i
] == tid
)
380 spin_unlock(&__thread_id_map_mutex
);
383 spin_unlock(&__thread_id_map_mutex
);
384 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
389 static int smp_thread_id(void)
393 id
= pthread_getspecific(thread_id_key
);
395 return __smp_thread_id();
396 return (long)(id
- 1);
399 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
404 spin_lock(&__thread_id_map_mutex
);
405 for (i
= 0; i
< NR_THREADS
; i
++) {
406 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
409 if (i
>= NR_THREADS
) {
410 spin_unlock(&__thread_id_map_mutex
);
411 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
414 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
415 spin_unlock(&__thread_id_map_mutex
);
416 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
417 perror("create_thread:pthread_create");
420 __thread_id_map
[i
] = tid
;
424 static void *wait_thread(thread_id_t tid
)
429 for (i
= 0; i
< NR_THREADS
; i
++) {
430 if (__thread_id_map
[i
] == tid
)
433 if (i
>= NR_THREADS
){
434 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
438 if (pthread_join(tid
, &vp
) != 0) {
439 perror("wait_thread:pthread_join");
442 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
446 static void wait_all_threads(void)
451 for (i
= 1; i
< NR_THREADS
; i
++) {
452 tid
= __thread_id_map
[i
];
453 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
454 tid
!= __THREAD_ID_MAP_WAITING
)
455 (void)wait_thread(tid
);
459 #ifndef HAVE_CPU_SET_T
460 typedef unsigned long cpu_set_t
;
461 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
462 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
465 static void run_on(int cpu
)
467 #if HAVE_SCHED_SETAFFINITY
472 #if SCHED_SETAFFINITY_ARGS == 2
473 sched_setaffinity(0, &mask
);
475 sched_setaffinity(0, sizeof(mask
), &mask
);
477 #endif /* HAVE_SCHED_SETAFFINITY */
481 * timekeeping -- very crude -- should use MONOTONIC...
484 long long get_microseconds(void)
488 if (gettimeofday(&tv
, NULL
) != 0)
490 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
494 * Per-thread variables.
497 #define DEFINE_PER_THREAD(type, name) \
500 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
501 } __per_thread_##name[NR_THREADS];
502 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
504 #define per_thread(name, thread) __per_thread_##name[thread].v
505 #define __get_thread_var(name) per_thread(name, smp_thread_id())
507 #define init_per_thread(name, v) \
510 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
511 per_thread(name, __i_p_t_i) = v; \
515 * CPU traversal primitives.
520 #endif /* #ifndef NR_CPUS */
522 #define for_each_possible_cpu(cpu) \
523 for (cpu = 0; cpu < NR_CPUS; cpu++)
524 #define for_each_online_cpu(cpu) \
525 for (cpu = 0; cpu < NR_CPUS; cpu++)
531 #define DEFINE_PER_CPU(type, name) \
534 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
535 } __per_cpu_##name[NR_CPUS]
536 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
538 DEFINE_PER_THREAD(int, smp_processor_id
);
540 #define per_cpu(name, thread) __per_cpu_##name[thread].v
541 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
543 #define init_per_cpu(name, v) \
546 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
547 per_cpu(name, __i_p_c_i) = v; \
551 * CPU state checking (crowbarred).
554 #define idle_cpu(cpu) 0
555 #define in_softirq() 1
556 #define hardirq_count() 0
557 #define PREEMPT_SHIFT 0
558 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
559 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
560 #define PREEMPT_BITS 8
561 #define SOFTIRQ_BITS 8
567 struct notifier_block
{
568 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
569 struct notifier_block
*next
;
573 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
574 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
575 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
576 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
577 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
578 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
579 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
580 * not handling interrupts, soon dead */
581 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
584 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
585 * operation in progress
587 #define CPU_TASKS_FROZEN 0x0010
589 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
590 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
591 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
592 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
593 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
594 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
595 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
597 /* Hibernation and suspend events */
598 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
599 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
600 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
601 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
602 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
603 #define PM_POST_RESTORE 0x0006 /* Restore failed */
605 #define NOTIFY_DONE 0x0000 /* Don't care */
606 #define NOTIFY_OK 0x0001 /* Suits me */
607 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
608 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
609 /* Bad/Veto action */
611 * Clean way to return from the notifier and stop further calls.
613 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
619 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
622 * Initialization -- Must be called before calling any primitives.
625 static void smp_init(void)
629 spin_lock_init(&__thread_id_map_mutex
);
630 __thread_id_map
[0] = pthread_self();
631 for (i
= 1; i
< NR_THREADS
; i
++)
632 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
633 init_per_thread(smp_processor_id
, 0);
634 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
635 perror("pthread_key_create");
640 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
642 #ifndef _LINUX_LIST_H
643 #define _LINUX_LIST_H
645 #define LIST_POISON1 ((void *) 0x00100100)
646 #define LIST_POISON2 ((void *) 0x00200200)
650 * Simple doubly linked list implementation.
652 * Some of the internal functions ("__xxx") are useful when
653 * manipulating whole lists rather than single entries, as
654 * sometimes we already know the next/prev entries and we can
655 * generate better code by using them directly rather than
656 * using the generic single-entry routines.
659 struct cds_list_head
{
660 struct cds_list_head
*next
, *prev
;
663 #define CDS_LIST_HEAD_INIT(name) { &(name), &(name) }
665 #define CDS_LIST_HEAD(name) \
666 struct cds_list_head name = CDS_LIST_HEAD_INIT(name)
668 static inline void CDS_INIT_LIST_HEAD(struct cds_list_head
*list
)
675 * Insert a new entry between two known consecutive entries.
677 * This is only for internal list manipulation where we know
678 * the prev/next entries already!
680 #ifndef CONFIG_DEBUG_LIST
681 static inline void __cds_list_add(struct cds_list_head
*new,
682 struct cds_list_head
*prev
,
683 struct cds_list_head
*next
)
691 extern void __cds_list_add(struct cds_list_head
*new,
692 struct cds_list_head
*prev
,
693 struct cds_list_head
*next
);
697 * cds_list_add - add a new entry
698 * @new: new entry to be added
699 * @head: list head to add it after
701 * Insert a new entry after the specified head.
702 * This is good for implementing stacks.
704 static inline void cds_list_add(struct cds_list_head
*new, struct cds_list_head
*head
)
706 __cds_list_add(new, head
, head
->next
);
711 * cds_list_add_tail - add a new entry
712 * @new: new entry to be added
713 * @head: list head to add it before
715 * Insert a new entry before the specified head.
716 * This is useful for implementing queues.
718 static inline void cds_list_add_tail(struct cds_list_head
*new, struct cds_list_head
*head
)
720 __cds_list_add(new, head
->prev
, head
);
724 * Delete a list entry by making the prev/next entries
725 * point to each other.
727 * This is only for internal list manipulation where we know
728 * the prev/next entries already!
730 static inline void __cds_list_del(struct cds_list_head
* prev
, struct cds_list_head
* next
)
737 * cds_list_del - deletes entry from list.
738 * @entry: the element to delete from the list.
739 * Note: cds_list_empty() on entry does not return true after this, the entry is
740 * in an undefined state.
742 #ifndef CONFIG_DEBUG_LIST
743 static inline void cds_list_del(struct cds_list_head
*entry
)
745 __cds_list_del(entry
->prev
, entry
->next
);
746 entry
->next
= LIST_POISON1
;
747 entry
->prev
= LIST_POISON2
;
750 extern void cds_list_del(struct cds_list_head
*entry
);
754 * cds_list_replace - replace old entry by new one
755 * @old : the element to be replaced
756 * @new : the new element to insert
758 * If @old was empty, it will be overwritten.
760 static inline void cds_list_replace(struct cds_list_head
*old
,
761 struct cds_list_head
*new)
763 new->next
= old
->next
;
764 new->next
->prev
= new;
765 new->prev
= old
->prev
;
766 new->prev
->next
= new;
769 static inline void cds_list_replace_init(struct cds_list_head
*old
,
770 struct cds_list_head
*new)
772 cds_list_replace(old
, new);
773 CDS_INIT_LIST_HEAD(old
);
777 * cds_list_del_init - deletes entry from list and reinitialize it.
778 * @entry: the element to delete from the list.
780 static inline void cds_list_del_init(struct cds_list_head
*entry
)
782 __cds_list_del(entry
->prev
, entry
->next
);
783 CDS_INIT_LIST_HEAD(entry
);
787 * cds_list_move - delete from one list and add as another's head
788 * @list: the entry to move
789 * @head: the head that will precede our entry
791 static inline void cds_list_move(struct cds_list_head
*list
, struct cds_list_head
*head
)
793 __cds_list_del(list
->prev
, list
->next
);
794 cds_list_add(list
, head
);
798 * cds_list_move_tail - delete from one list and add as another's tail
799 * @list: the entry to move
800 * @head: the head that will follow our entry
802 static inline void cds_list_move_tail(struct cds_list_head
*list
,
803 struct cds_list_head
*head
)
805 __cds_list_del(list
->prev
, list
->next
);
806 cds_list_add_tail(list
, head
);
810 * list_is_last - tests whether @list is the last entry in list @head
811 * @list: the entry to test
812 * @head: the head of the list
814 static inline int list_is_last(const struct cds_list_head
*list
,
815 const struct cds_list_head
*head
)
817 return list
->next
== head
;
821 * cds_list_empty - tests whether a list is empty
822 * @head: the list to test.
824 static inline int cds_list_empty(const struct cds_list_head
*head
)
826 return head
->next
== head
;
830 * cds_list_empty_careful - tests whether a list is empty and not being modified
831 * @head: the list to test
834 * tests whether a list is empty _and_ checks that no other CPU might be
835 * in the process of modifying either member (next or prev)
837 * NOTE: using cds_list_empty_careful() without synchronization
838 * can only be safe if the only activity that can happen
839 * to the list entry is cds_list_del_init(). Eg. it cannot be used
840 * if another CPU could re-list_add() it.
842 static inline int cds_list_empty_careful(const struct cds_list_head
*head
)
844 struct cds_list_head
*next
= head
->next
;
845 return (next
== head
) && (next
== head
->prev
);
849 * list_is_singular - tests whether a list has just one entry.
850 * @head: the list to test.
852 static inline int list_is_singular(const struct cds_list_head
*head
)
854 return !list_empty(head
) && (head
->next
== head
->prev
);
857 static inline void __list_cut_position(struct cds_list_head
*list
,
858 struct cds_list_head
*head
, struct cds_list_head
*entry
)
860 struct cds_list_head
*new_first
= entry
->next
;
861 list
->next
= head
->next
;
862 list
->next
->prev
= list
;
865 head
->next
= new_first
;
866 new_first
->prev
= head
;
870 * list_cut_position - cut a list into two
871 * @list: a new list to add all removed entries
872 * @head: a list with entries
873 * @entry: an entry within head, could be the head itself
874 * and if so we won't cut the list
876 * This helper moves the initial part of @head, up to and
877 * including @entry, from @head to @list. You should
878 * pass on @entry an element you know is on @head. @list
879 * should be an empty list or a list you do not care about
883 static inline void list_cut_position(struct cds_list_head
*list
,
884 struct cds_list_head
*head
, struct cds_list_head
*entry
)
886 if (cds_list_empty(head
))
888 if (list_is_singular(head
) &&
889 (head
->next
!= entry
&& head
!= entry
))
892 CDS_INIT_LIST_HEAD(list
);
894 __list_cut_position(list
, head
, entry
);
897 static inline void __cds_list_splice(const struct cds_list_head
*list
,
898 struct cds_list_head
*prev
,
899 struct cds_list_head
*next
)
901 struct cds_list_head
*first
= list
->next
;
902 struct cds_list_head
*last
= list
->prev
;
912 * cds_list_splice - join two lists, this is designed for stacks
913 * @list: the new list to add.
914 * @head: the place to add it in the first list.
916 static inline void cds_list_splice(const struct cds_list_head
*list
,
917 struct cds_list_head
*head
)
919 if (!cds_list_empty(list
))
920 __cds_list_splice(list
, head
, head
->next
);
924 * cds_list_splice_tail - join two lists, each list being a queue
925 * @list: the new list to add.
926 * @head: the place to add it in the first list.
928 static inline void cds_list_splice_tail(struct cds_list_head
*list
,
929 struct cds_list_head
*head
)
931 if (!cds_list_empty(list
))
932 __cds_list_splice(list
, head
->prev
, head
);
936 * cds_list_splice_init - join two lists and reinitialise the emptied list.
937 * @list: the new list to add.
938 * @head: the place to add it in the first list.
940 * The list at @list is reinitialised
942 static inline void cds_list_splice_init(struct cds_list_head
*list
,
943 struct cds_list_head
*head
)
945 if (!cds_list_empty(list
)) {
946 __cds_list_splice(list
, head
, head
->next
);
947 CDS_INIT_LIST_HEAD(list
);
952 * cds_list_splice_tail_init - join two lists and reinitialise the emptied list
953 * @list: the new list to add.
954 * @head: the place to add it in the first list.
956 * Each of the lists is a queue.
957 * The list at @list is reinitialised
959 static inline void cds_list_splice_tail_init(struct cds_list_head
*list
,
960 struct cds_list_head
*head
)
962 if (!cds_list_empty(list
)) {
963 __cds_list_splice(list
, head
->prev
, head
);
964 CDS_INIT_LIST_HEAD(list
);
969 * cds_list_entry - get the struct for this entry
970 * @ptr: the &struct cds_list_head pointer.
971 * @type: the type of the struct this is embedded in.
972 * @member: the name of the list_struct within the struct.
974 #define cds_list_entry(ptr, type, member) \
975 caa_container_of(ptr, type, member)
978 * list_first_entry - get the first element from a list
979 * @ptr: the list head to take the element from.
980 * @type: the type of the struct this is embedded in.
981 * @member: the name of the list_struct within the struct.
983 * Note, that list is expected to be not empty.
985 #define list_first_entry(ptr, type, member) \
986 cds_list_entry((ptr)->next, type, member)
989 * cds_list_for_each - iterate over a list
990 * @pos: the &struct cds_list_head to use as a loop cursor.
991 * @head: the head for your list.
993 #define cds_list_for_each(pos, head) \
994 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
998 * __cds_list_for_each - iterate over a list
999 * @pos: the &struct cds_list_head to use as a loop cursor.
1000 * @head: the head for your list.
1002 * This variant differs from cds_list_for_each() in that it's the
1003 * simplest possible list iteration code, no prefetching is done.
1004 * Use this for code that knows the list to be very short (empty
1005 * or 1 entry) most of the time.
1007 #define __cds_list_for_each(pos, head) \
1008 for (pos = (head)->next; pos != (head); pos = pos->next)
1011 * cds_list_for_each_prev - iterate over a list backwards
1012 * @pos: the &struct cds_list_head to use as a loop cursor.
1013 * @head: the head for your list.
1015 #define cds_list_for_each_prev(pos, head) \
1016 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1020 * cds_list_for_each_safe - iterate over a list safe against removal of list entry
1021 * @pos: the &struct cds_list_head to use as a loop cursor.
1022 * @n: another &struct cds_list_head to use as temporary storage
1023 * @head: the head for your list.
1025 #define cds_list_for_each_safe(pos, n, head) \
1026 for (pos = (head)->next, n = pos->next; pos != (head); \
1027 pos = n, n = pos->next)
1030 * cds_list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1031 * @pos: the &struct cds_list_head to use as a loop cursor.
1032 * @n: another &struct cds_list_head to use as temporary storage
1033 * @head: the head for your list.
1035 #define cds_list_for_each_prev_safe(pos, n, head) \
1036 for (pos = (head)->prev, n = pos->prev; \
1037 prefetch(pos->prev), pos != (head); \
1038 pos = n, n = pos->prev)
1041 * cds_list_for_each_entry - iterate over list of given type
1042 * @pos: the type * to use as a loop cursor.
1043 * @head: the head for your list.
1044 * @member: the name of the list_struct within the struct.
1046 #define cds_list_for_each_entry(pos, head, member) \
1047 for (pos = cds_list_entry((head)->next, typeof(*pos), member); \
1048 prefetch(pos->member.next), &pos->member != (head); \
1049 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
1052 * cds_list_for_each_entry_reverse - iterate backwards 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 cds_list_for_each_entry_reverse(pos, head, member) \
1058 for (pos = cds_list_entry((head)->prev, typeof(*pos), member); \
1059 prefetch(pos->member.prev), &pos->member != (head); \
1060 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
1063 * list_prepare_entry - prepare a pos entry for use in cds_list_for_each_entry_continue()
1064 * @pos: the type * to use as a start point
1065 * @head: the head of the list
1066 * @member: the name of the list_struct within the struct.
1068 * Prepares a pos entry for use as a start point in cds_list_for_each_entry_continue().
1070 #define list_prepare_entry(pos, head, member) \
1071 ((pos) ? : cds_list_entry(head, typeof(*pos), member))
1074 * cds_list_for_each_entry_continue - continue iteration over list of given type
1075 * @pos: the type * to use as a loop cursor.
1076 * @head: the head for your list.
1077 * @member: the name of the list_struct within the struct.
1079 * Continue to iterate over list of given type, continuing after
1080 * the current position.
1082 #define cds_list_for_each_entry_continue(pos, head, member) \
1083 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member); \
1084 prefetch(pos->member.next), &pos->member != (head); \
1085 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
1088 * cds_list_for_each_entry_continue_reverse - iterate backwards from the given point
1089 * @pos: the type * to use as a loop cursor.
1090 * @head: the head for your list.
1091 * @member: the name of the list_struct within the struct.
1093 * Start to iterate over list of given type backwards, continuing after
1094 * the current position.
1096 #define cds_list_for_each_entry_continue_reverse(pos, head, member) \
1097 for (pos = cds_list_entry(pos->member.prev, typeof(*pos), member); \
1098 prefetch(pos->member.prev), &pos->member != (head); \
1099 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
1102 * cds_list_for_each_entry_from - iterate over list of given type from the current point
1103 * @pos: the type * to use as a loop cursor.
1104 * @head: the head for your list.
1105 * @member: the name of the list_struct within the struct.
1107 * Iterate over list of given type, continuing from current position.
1109 #define cds_list_for_each_entry_from(pos, head, member) \
1110 for (; prefetch(pos->member.next), &pos->member != (head); \
1111 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
1114 * cds_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1115 * @pos: the type * to use as a loop cursor.
1116 * @n: another type * to use as temporary storage
1117 * @head: the head for your list.
1118 * @member: the name of the list_struct within the struct.
1120 #define cds_list_for_each_entry_safe(pos, n, head, member) \
1121 for (pos = cds_list_entry((head)->next, typeof(*pos), member), \
1122 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
1123 &pos->member != (head); \
1124 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
1127 * cds_list_for_each_entry_safe_continue
1128 * @pos: the type * to use as a loop cursor.
1129 * @n: another type * to use as temporary storage
1130 * @head: the head for your list.
1131 * @member: the name of the list_struct within the struct.
1133 * Iterate over list of given type, continuing after current point,
1134 * safe against removal of list entry.
1136 #define cds_list_for_each_entry_safe_continue(pos, n, head, member) \
1137 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member), \
1138 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
1139 &pos->member != (head); \
1140 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
1143 * cds_list_for_each_entry_safe_from
1144 * @pos: the type * to use as a loop cursor.
1145 * @n: another type * to use as temporary storage
1146 * @head: the head for your list.
1147 * @member: the name of the list_struct within the struct.
1149 * Iterate over list of given type from current point, safe against
1150 * removal of list entry.
1152 #define cds_list_for_each_entry_safe_from(pos, n, head, member) \
1153 for (n = cds_list_entry(pos->member.next, typeof(*pos), member); \
1154 &pos->member != (head); \
1155 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
1158 * cds_list_for_each_entry_safe_reverse
1159 * @pos: the type * to use as a loop cursor.
1160 * @n: another type * to use as temporary storage
1161 * @head: the head for your list.
1162 * @member: the name of the list_struct within the struct.
1164 * Iterate backwards over list of given type, safe against removal
1167 #define cds_list_for_each_entry_safe_reverse(pos, n, head, member) \
1168 for (pos = cds_list_entry((head)->prev, typeof(*pos), member), \
1169 n = cds_list_entry(pos->member.prev, typeof(*pos), member); \
1170 &pos->member != (head); \
1171 pos = n, n = cds_list_entry(n->member.prev, typeof(*n), member))
1176 * Double linked lists with a single pointer list head.
1177 * Mostly useful for hash tables where the two pointer list head is
1179 * You lose the ability to access the tail in O(1).
1182 struct cds_hlist_head
{
1183 struct cds_hlist_node
*first
;
1186 struct cds_hlist_node
{
1187 struct cds_hlist_node
*next
, **pprev
;
1190 #define HLIST_HEAD_INIT { .first = NULL }
1191 #define HLIST_HEAD(name) struct cds_hlist_head name = { .first = NULL }
1192 #define CDS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1193 static inline void INIT_HLIST_NODE(struct cds_hlist_node
*h
)
1199 static inline int hlist_unhashed(const struct cds_hlist_node
*h
)
1204 static inline int hlist_empty(const struct cds_hlist_head
*h
)
1209 static inline void __cds_hlist_del(struct cds_hlist_node
*n
)
1211 struct cds_hlist_node
*next
= n
->next
;
1212 struct cds_hlist_node
**pprev
= n
->pprev
;
1215 next
->pprev
= pprev
;
1218 static inline void cds_hlist_del(struct cds_hlist_node
*n
)
1221 n
->next
= LIST_POISON1
;
1222 n
->pprev
= LIST_POISON2
;
1225 static inline void cds_hlist_del_init(struct cds_hlist_node
*n
)
1227 if (!hlist_unhashed(n
)) {
1233 static inline void cds_hlist_add_head(struct cds_hlist_node
*n
, struct cds_hlist_head
*h
)
1235 struct cds_hlist_node
*first
= h
->first
;
1238 first
->pprev
= &n
->next
;
1240 n
->pprev
= &h
->first
;
1243 /* next must be != NULL */
1244 static inline void hlist_add_before(struct cds_hlist_node
*n
,
1245 struct cds_hlist_node
*next
)
1247 n
->pprev
= next
->pprev
;
1249 next
->pprev
= &n
->next
;
1253 static inline void hlist_add_after(struct cds_hlist_node
*n
,
1254 struct cds_hlist_node
*next
)
1256 next
->next
= n
->next
;
1258 next
->pprev
= &n
->next
;
1261 next
->next
->pprev
= &next
->next
;
1265 * Move a list from one list head to another. Fixup the pprev
1266 * reference of the first entry if it exists.
1268 static inline void hlist_move_list(struct cds_hlist_head
*old
,
1269 struct cds_hlist_head
*new)
1271 new->first
= old
->first
;
1273 new->first
->pprev
= &new->first
;
1277 #define cds_hlist_entry(ptr, type, member) caa_container_of(ptr,type,member)
1279 #define cds_hlist_for_each(pos, head) \
1280 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1283 #define cds_hlist_for_each_safe(pos, n, head) \
1284 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1288 * cds_hlist_for_each_entry - iterate over list of given type
1289 * @tpos: the type * to use as a loop cursor.
1290 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1291 * @head: the head for your list.
1292 * @member: the name of the cds_hlist_node within the struct.
1294 #define cds_hlist_for_each_entry(tpos, pos, head, member) \
1295 for (pos = (head)->first; \
1296 pos && ({ prefetch(pos->next); 1;}) && \
1297 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1301 * cds_hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1302 * @tpos: the type * to use as a loop cursor.
1303 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1304 * @member: the name of the cds_hlist_node within the struct.
1306 #define cds_hlist_for_each_entry_continue(tpos, pos, member) \
1307 for (pos = (pos)->next; \
1308 pos && ({ prefetch(pos->next); 1;}) && \
1309 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1313 * cds_hlist_for_each_entry_from - iterate over a hlist continuing from current point
1314 * @tpos: the type * to use as a loop cursor.
1315 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1316 * @member: the name of the cds_hlist_node within the struct.
1318 #define cds_hlist_for_each_entry_from(tpos, pos, member) \
1319 for (; pos && ({ prefetch(pos->next); 1;}) && \
1320 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1324 * cds_hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1325 * @tpos: the type * to use as a loop cursor.
1326 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1327 * @n: another &struct cds_hlist_node to use as temporary storage
1328 * @head: the head for your list.
1329 * @member: the name of the cds_hlist_node within the struct.
1331 #define cds_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1332 for (pos = (head)->first; \
1333 pos && ({ n = pos->next; 1; }) && \
1334 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \