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 #include <urcu/arch.h>
32 #ifndef __always_inline
33 #define __always_inline inline
36 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
37 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
40 # define stringify_in_c(...) __VA_ARGS__
41 # define ASM_CONST(x) x
43 /* This version of stringify will deal with commas... */
44 # define __stringify_in_c(...) #__VA_ARGS__
45 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
46 # define __ASM_CONST(x) x##UL
47 # define ASM_CONST(x) __ASM_CONST(x)
52 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
54 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
56 * the Free Software Foundation, but version 2 only due to inclusion
57 * of Linux-kernel code.
59 * This program is distributed in the hope that it will be useful,
60 * but WITHOUT ANY WARRANTY; without even the implied warranty of
61 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62 * GNU General Public License for more details.
64 * You should have received a copy of the GNU General Public License
65 * along with this program; if not, write to the Free Software
66 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
68 * Copyright (c) 2006 Paul E. McKenney, IBM.
70 * Much code taken from the Linux kernel. For such code, the option
71 * to redistribute under later versions of GPL might not be available.
78 /* #define CAA_CACHE_LINE_SIZE 64 */
79 #define ____cacheline_internodealigned_in_smp \
80 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
83 * api_pthreads.h: API mapping to pthreads environment.
85 * This program is free software; you can redistribute it and/or modify
86 * it under the terms of the GNU General Public License as published by
87 * the Free Software Foundation; either version 2 of the License, or
88 * (at your option) any later version. However, please note that much
89 * of the code in this file derives from the Linux kernel, and that such
90 * code may not be available except under GPLv2.
92 * This program is distributed in the hope that it will be useful,
93 * but WITHOUT ANY WARRANTY; without even the implied warranty of
94 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95 * GNU General Public License for more details.
97 * You should have received a copy of the GNU General Public License
98 * along with this program; if not, write to the Free Software
99 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
101 * Copyright (c) 2006 Paul E. McKenney, IBM.
108 #include <sys/types.h>
112 #include <sys/param.h>
113 /* #include "atomic.h" */
116 * Default machine parameters.
119 #ifndef CAA_CACHE_LINE_SIZE
120 /* #define CAA_CACHE_LINE_SIZE 128 */
121 #endif /* #ifndef CAA_CACHE_LINE_SIZE */
124 * Exclusive locking primitives.
127 typedef pthread_mutex_t spinlock_t
;
129 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
130 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
132 static void spin_lock_init(spinlock_t
*sp
)
134 if (pthread_mutex_init(sp
, NULL
) != 0) {
135 perror("spin_lock_init:pthread_mutex_init");
140 static void spin_lock(spinlock_t
*sp
)
142 if (pthread_mutex_lock(sp
) != 0) {
143 perror("spin_lock:pthread_mutex_lock");
148 static void spin_unlock(spinlock_t
*sp
)
150 if (pthread_mutex_unlock(sp
) != 0) {
151 perror("spin_unlock:pthread_mutex_unlock");
156 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
157 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
160 * Thread creation/destruction primitives.
163 typedef pthread_t thread_id_t
;
165 #define NR_THREADS 128
167 #define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0)
168 #define __THREAD_ID_MAP_WAITING ((thread_id_t) 1)
169 thread_id_t __thread_id_map
[NR_THREADS
];
170 spinlock_t __thread_id_map_mutex
;
172 #define for_each_thread(t) \
173 for (t = 0; t < NR_THREADS; t++)
175 #define for_each_running_thread(t) \
176 for (t = 0; t < NR_THREADS; t++) \
177 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
178 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
180 #define for_each_tid(t, tid) \
181 for (t = 0; t < NR_THREADS; t++) \
182 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
183 ((tid) != __THREAD_ID_MAP_WAITING))
185 pthread_key_t thread_id_key
;
187 static int __smp_thread_id(void)
190 thread_id_t tid
= pthread_self();
192 for (i
= 0; i
< NR_THREADS
; i
++) {
193 if (__thread_id_map
[i
] == tid
) {
194 long v
= i
+ 1; /* must be non-NULL. */
196 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
197 perror("pthread_setspecific");
203 spin_lock(&__thread_id_map_mutex
);
204 for (i
= 0; i
< NR_THREADS
; i
++) {
205 if (__thread_id_map
[i
] == tid
)
206 spin_unlock(&__thread_id_map_mutex
);
209 spin_unlock(&__thread_id_map_mutex
);
210 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
215 static int smp_thread_id(void)
219 id
= pthread_getspecific(thread_id_key
);
221 return __smp_thread_id();
222 return (long)(id
- 1);
225 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
230 spin_lock(&__thread_id_map_mutex
);
231 for (i
= 0; i
< NR_THREADS
; i
++) {
232 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
235 if (i
>= NR_THREADS
) {
236 spin_unlock(&__thread_id_map_mutex
);
237 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
240 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
241 spin_unlock(&__thread_id_map_mutex
);
242 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
243 perror("create_thread:pthread_create");
246 __thread_id_map
[i
] = tid
;
250 static void *wait_thread(thread_id_t tid
)
255 for (i
= 0; i
< NR_THREADS
; i
++) {
256 if (__thread_id_map
[i
] == tid
)
259 if (i
>= NR_THREADS
){
260 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
264 if (pthread_join(tid
, &vp
) != 0) {
265 perror("wait_thread:pthread_join");
268 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
272 static void wait_all_threads(void)
277 for (i
= 1; i
< NR_THREADS
; i
++) {
278 tid
= __thread_id_map
[i
];
279 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
280 tid
!= __THREAD_ID_MAP_WAITING
)
281 (void)wait_thread(tid
);
285 #ifndef HAVE_CPU_SET_T
286 typedef unsigned long cpu_set_t
;
287 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
288 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
291 static void run_on(int cpu
)
293 #if HAVE_SCHED_SETAFFINITY
298 #if SCHED_SETAFFINITY_ARGS == 2
299 sched_setaffinity(0, &mask
);
301 sched_setaffinity(0, sizeof(mask
), &mask
);
303 #endif /* HAVE_SCHED_SETAFFINITY */
307 * timekeeping -- very crude -- should use MONOTONIC...
310 long long get_microseconds(void)
314 if (gettimeofday(&tv
, NULL
) != 0)
316 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
320 * Per-thread variables.
323 #define DEFINE_PER_THREAD(type, name) \
326 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
327 } __per_thread_##name[NR_THREADS];
328 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
330 #define per_thread(name, thread) __per_thread_##name[thread].v
331 #define __get_thread_var(name) per_thread(name, smp_thread_id())
333 #define init_per_thread(name, v) \
336 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
337 per_thread(name, __i_p_t_i) = v; \
341 * CPU traversal primitives.
346 #endif /* #ifndef NR_CPUS */
348 #define for_each_possible_cpu(cpu) \
349 for (cpu = 0; cpu < NR_CPUS; cpu++)
350 #define for_each_online_cpu(cpu) \
351 for (cpu = 0; cpu < NR_CPUS; cpu++)
357 #define DEFINE_PER_CPU(type, name) \
360 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
361 } __per_cpu_##name[NR_CPUS]
362 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
364 DEFINE_PER_THREAD(int, smp_processor_id
);
366 #define per_cpu(name, thread) __per_cpu_##name[thread].v
367 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
369 #define init_per_cpu(name, v) \
372 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
373 per_cpu(name, __i_p_c_i) = v; \
377 * CPU state checking (crowbarred).
380 #define idle_cpu(cpu) 0
381 #define in_softirq() 1
382 #define hardirq_count() 0
383 #define PREEMPT_SHIFT 0
384 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
385 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
386 #define PREEMPT_BITS 8
387 #define SOFTIRQ_BITS 8
393 struct notifier_block
{
394 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
395 struct notifier_block
*next
;
399 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
400 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
401 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
402 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
403 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
404 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
405 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
406 * not handling interrupts, soon dead */
407 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
410 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
411 * operation in progress
413 #define CPU_TASKS_FROZEN 0x0010
415 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
416 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
417 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
418 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
419 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
420 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
421 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
423 /* Hibernation and suspend events */
424 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
425 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
426 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
427 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
428 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
429 #define PM_POST_RESTORE 0x0006 /* Restore failed */
431 #define NOTIFY_DONE 0x0000 /* Don't care */
432 #define NOTIFY_OK 0x0001 /* Suits me */
433 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
434 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
435 /* Bad/Veto action */
437 * Clean way to return from the notifier and stop further calls.
439 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
445 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
448 * Initialization -- Must be called before calling any primitives.
451 static void smp_init(void)
455 spin_lock_init(&__thread_id_map_mutex
);
456 __thread_id_map
[0] = pthread_self();
457 for (i
= 1; i
< NR_THREADS
; i
++)
458 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
459 init_per_thread(smp_processor_id
, 0);
460 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
461 perror("pthread_key_create");
466 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
468 #ifndef _LINUX_LIST_H
469 #define _LINUX_LIST_H
471 #define LIST_POISON1 ((void *) 0x00100100)
472 #define LIST_POISON2 ((void *) 0x00200200)
476 * Simple doubly linked list implementation.
478 * Some of the internal functions ("__xxx") are useful when
479 * manipulating whole lists rather than single entries, as
480 * sometimes we already know the next/prev entries and we can
481 * generate better code by using them directly rather than
482 * using the generic single-entry routines.
485 struct cds_list_head
{
486 struct cds_list_head
*next
, *prev
;
489 #define CDS_LIST_HEAD_INIT(name) { &(name), &(name) }
491 #define CDS_LIST_HEAD(name) \
492 struct cds_list_head name = CDS_LIST_HEAD_INIT(name)
494 static inline void CDS_INIT_LIST_HEAD(struct cds_list_head
*list
)
501 * Insert a new entry between two known consecutive entries.
503 * This is only for internal list manipulation where we know
504 * the prev/next entries already!
506 #ifndef CONFIG_DEBUG_LIST
507 static inline void __cds_list_add(struct cds_list_head
*new,
508 struct cds_list_head
*prev
,
509 struct cds_list_head
*next
)
517 extern void __cds_list_add(struct cds_list_head
*new,
518 struct cds_list_head
*prev
,
519 struct cds_list_head
*next
);
523 * cds_list_add - add a new entry
524 * @new: new entry to be added
525 * @head: list head to add it after
527 * Insert a new entry after the specified head.
528 * This is good for implementing stacks.
530 static inline void cds_list_add(struct cds_list_head
*new, struct cds_list_head
*head
)
532 __cds_list_add(new, head
, head
->next
);
537 * cds_list_add_tail - add a new entry
538 * @new: new entry to be added
539 * @head: list head to add it before
541 * Insert a new entry before the specified head.
542 * This is useful for implementing queues.
544 static inline void cds_list_add_tail(struct cds_list_head
*new, struct cds_list_head
*head
)
546 __cds_list_add(new, head
->prev
, head
);
550 * Delete a list entry by making the prev/next entries
551 * point to each other.
553 * This is only for internal list manipulation where we know
554 * the prev/next entries already!
556 static inline void __cds_list_del(struct cds_list_head
* prev
, struct cds_list_head
* next
)
563 * cds_list_del - deletes entry from list.
564 * @entry: the element to delete from the list.
565 * Note: cds_list_empty() on entry does not return true after this, the entry is
566 * in an undefined state.
568 #ifndef CONFIG_DEBUG_LIST
569 static inline void cds_list_del(struct cds_list_head
*entry
)
571 __cds_list_del(entry
->prev
, entry
->next
);
572 entry
->next
= LIST_POISON1
;
573 entry
->prev
= LIST_POISON2
;
576 extern void cds_list_del(struct cds_list_head
*entry
);
580 * cds_list_replace - replace old entry by new one
581 * @old : the element to be replaced
582 * @new : the new element to insert
584 * If @old was empty, it will be overwritten.
586 static inline void cds_list_replace(struct cds_list_head
*old
,
587 struct cds_list_head
*new)
589 new->next
= old
->next
;
590 new->next
->prev
= new;
591 new->prev
= old
->prev
;
592 new->prev
->next
= new;
595 static inline void cds_list_replace_init(struct cds_list_head
*old
,
596 struct cds_list_head
*new)
598 cds_list_replace(old
, new);
599 CDS_INIT_LIST_HEAD(old
);
603 * cds_list_del_init - deletes entry from list and reinitialize it.
604 * @entry: the element to delete from the list.
606 static inline void cds_list_del_init(struct cds_list_head
*entry
)
608 __cds_list_del(entry
->prev
, entry
->next
);
609 CDS_INIT_LIST_HEAD(entry
);
613 * cds_list_move - delete from one list and add as another's head
614 * @list: the entry to move
615 * @head: the head that will precede our entry
617 static inline void cds_list_move(struct cds_list_head
*list
, struct cds_list_head
*head
)
619 __cds_list_del(list
->prev
, list
->next
);
620 cds_list_add(list
, head
);
624 * cds_list_move_tail - delete from one list and add as another's tail
625 * @list: the entry to move
626 * @head: the head that will follow our entry
628 static inline void cds_list_move_tail(struct cds_list_head
*list
,
629 struct cds_list_head
*head
)
631 __cds_list_del(list
->prev
, list
->next
);
632 cds_list_add_tail(list
, head
);
636 * list_is_last - tests whether @list is the last entry in list @head
637 * @list: the entry to test
638 * @head: the head of the list
640 static inline int list_is_last(const struct cds_list_head
*list
,
641 const struct cds_list_head
*head
)
643 return list
->next
== head
;
647 * cds_list_empty - tests whether a list is empty
648 * @head: the list to test.
650 static inline int cds_list_empty(const struct cds_list_head
*head
)
652 return head
->next
== head
;
656 * cds_list_empty_careful - tests whether a list is empty and not being modified
657 * @head: the list to test
660 * tests whether a list is empty _and_ checks that no other CPU might be
661 * in the process of modifying either member (next or prev)
663 * NOTE: using cds_list_empty_careful() without synchronization
664 * can only be safe if the only activity that can happen
665 * to the list entry is cds_list_del_init(). Eg. it cannot be used
666 * if another CPU could re-list_add() it.
668 static inline int cds_list_empty_careful(const struct cds_list_head
*head
)
670 struct cds_list_head
*next
= head
->next
;
671 return (next
== head
) && (next
== head
->prev
);
675 * list_is_singular - tests whether a list has just one entry.
676 * @head: the list to test.
678 static inline int list_is_singular(const struct cds_list_head
*head
)
680 return !list_empty(head
) && (head
->next
== head
->prev
);
683 static inline void __list_cut_position(struct cds_list_head
*list
,
684 struct cds_list_head
*head
, struct cds_list_head
*entry
)
686 struct cds_list_head
*new_first
= entry
->next
;
687 list
->next
= head
->next
;
688 list
->next
->prev
= list
;
691 head
->next
= new_first
;
692 new_first
->prev
= head
;
696 * list_cut_position - cut a list into two
697 * @list: a new list to add all removed entries
698 * @head: a list with entries
699 * @entry: an entry within head, could be the head itself
700 * and if so we won't cut the list
702 * This helper moves the initial part of @head, up to and
703 * including @entry, from @head to @list. You should
704 * pass on @entry an element you know is on @head. @list
705 * should be an empty list or a list you do not care about
709 static inline void list_cut_position(struct cds_list_head
*list
,
710 struct cds_list_head
*head
, struct cds_list_head
*entry
)
712 if (cds_list_empty(head
))
714 if (list_is_singular(head
) &&
715 (head
->next
!= entry
&& head
!= entry
))
718 CDS_INIT_LIST_HEAD(list
);
720 __list_cut_position(list
, head
, entry
);
723 static inline void __cds_list_splice(const struct cds_list_head
*list
,
724 struct cds_list_head
*prev
,
725 struct cds_list_head
*next
)
727 struct cds_list_head
*first
= list
->next
;
728 struct cds_list_head
*last
= list
->prev
;
738 * cds_list_splice - join two lists, this is designed for stacks
739 * @list: the new list to add.
740 * @head: the place to add it in the first list.
742 static inline void cds_list_splice(const struct cds_list_head
*list
,
743 struct cds_list_head
*head
)
745 if (!cds_list_empty(list
))
746 __cds_list_splice(list
, head
, head
->next
);
750 * cds_list_splice_tail - join two lists, each list being a queue
751 * @list: the new list to add.
752 * @head: the place to add it in the first list.
754 static inline void cds_list_splice_tail(struct cds_list_head
*list
,
755 struct cds_list_head
*head
)
757 if (!cds_list_empty(list
))
758 __cds_list_splice(list
, head
->prev
, head
);
762 * cds_list_splice_init - join two lists and reinitialise the emptied list.
763 * @list: the new list to add.
764 * @head: the place to add it in the first list.
766 * The list at @list is reinitialised
768 static inline void cds_list_splice_init(struct cds_list_head
*list
,
769 struct cds_list_head
*head
)
771 if (!cds_list_empty(list
)) {
772 __cds_list_splice(list
, head
, head
->next
);
773 CDS_INIT_LIST_HEAD(list
);
778 * cds_list_splice_tail_init - join two lists and reinitialise the emptied list
779 * @list: the new list to add.
780 * @head: the place to add it in the first list.
782 * Each of the lists is a queue.
783 * The list at @list is reinitialised
785 static inline void cds_list_splice_tail_init(struct cds_list_head
*list
,
786 struct cds_list_head
*head
)
788 if (!cds_list_empty(list
)) {
789 __cds_list_splice(list
, head
->prev
, head
);
790 CDS_INIT_LIST_HEAD(list
);
795 * cds_list_entry - get the struct for this entry
796 * @ptr: the &struct cds_list_head pointer.
797 * @type: the type of the struct this is embedded in.
798 * @member: the name of the list_struct within the struct.
800 #define cds_list_entry(ptr, type, member) \
801 caa_container_of(ptr, type, member)
804 * list_first_entry - get the first element from a list
805 * @ptr: the list head to take the element from.
806 * @type: the type of the struct this is embedded in.
807 * @member: the name of the list_struct within the struct.
809 * Note, that list is expected to be not empty.
811 #define list_first_entry(ptr, type, member) \
812 cds_list_entry((ptr)->next, type, member)
815 * cds_list_for_each - iterate over a list
816 * @pos: the &struct cds_list_head to use as a loop cursor.
817 * @head: the head for your list.
819 #define cds_list_for_each(pos, head) \
820 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
824 * __cds_list_for_each - iterate over a list
825 * @pos: the &struct cds_list_head to use as a loop cursor.
826 * @head: the head for your list.
828 * This variant differs from cds_list_for_each() in that it's the
829 * simplest possible list iteration code, no prefetching is done.
830 * Use this for code that knows the list to be very short (empty
831 * or 1 entry) most of the time.
833 #define __cds_list_for_each(pos, head) \
834 for (pos = (head)->next; pos != (head); pos = pos->next)
837 * cds_list_for_each_prev - iterate over a list backwards
838 * @pos: the &struct cds_list_head to use as a loop cursor.
839 * @head: the head for your list.
841 #define cds_list_for_each_prev(pos, head) \
842 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
846 * cds_list_for_each_safe - iterate over a list safe against removal of list entry
847 * @pos: the &struct cds_list_head to use as a loop cursor.
848 * @n: another &struct cds_list_head to use as temporary storage
849 * @head: the head for your list.
851 #define cds_list_for_each_safe(pos, n, head) \
852 for (pos = (head)->next, n = pos->next; pos != (head); \
853 pos = n, n = pos->next)
856 * cds_list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
857 * @pos: the &struct cds_list_head to use as a loop cursor.
858 * @n: another &struct cds_list_head to use as temporary storage
859 * @head: the head for your list.
861 #define cds_list_for_each_prev_safe(pos, n, head) \
862 for (pos = (head)->prev, n = pos->prev; \
863 prefetch(pos->prev), pos != (head); \
864 pos = n, n = pos->prev)
867 * cds_list_for_each_entry - iterate over list of given type
868 * @pos: the type * to use as a loop cursor.
869 * @head: the head for your list.
870 * @member: the name of the list_struct within the struct.
872 #define cds_list_for_each_entry(pos, head, member) \
873 for (pos = cds_list_entry((head)->next, typeof(*pos), member); \
874 prefetch(pos->member.next), &pos->member != (head); \
875 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
878 * cds_list_for_each_entry_reverse - iterate backwards over list of given type.
879 * @pos: the type * to use as a loop cursor.
880 * @head: the head for your list.
881 * @member: the name of the list_struct within the struct.
883 #define cds_list_for_each_entry_reverse(pos, head, member) \
884 for (pos = cds_list_entry((head)->prev, typeof(*pos), member); \
885 prefetch(pos->member.prev), &pos->member != (head); \
886 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
889 * list_prepare_entry - prepare a pos entry for use in cds_list_for_each_entry_continue()
890 * @pos: the type * to use as a start point
891 * @head: the head of the list
892 * @member: the name of the list_struct within the struct.
894 * Prepares a pos entry for use as a start point in cds_list_for_each_entry_continue().
896 #define list_prepare_entry(pos, head, member) \
897 ((pos) ? : cds_list_entry(head, typeof(*pos), member))
900 * cds_list_for_each_entry_continue - continue iteration over list of given type
901 * @pos: the type * to use as a loop cursor.
902 * @head: the head for your list.
903 * @member: the name of the list_struct within the struct.
905 * Continue to iterate over list of given type, continuing after
906 * the current position.
908 #define cds_list_for_each_entry_continue(pos, head, member) \
909 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member); \
910 prefetch(pos->member.next), &pos->member != (head); \
911 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
914 * cds_list_for_each_entry_continue_reverse - iterate backwards from the given point
915 * @pos: the type * to use as a loop cursor.
916 * @head: the head for your list.
917 * @member: the name of the list_struct within the struct.
919 * Start to iterate over list of given type backwards, continuing after
920 * the current position.
922 #define cds_list_for_each_entry_continue_reverse(pos, head, member) \
923 for (pos = cds_list_entry(pos->member.prev, typeof(*pos), member); \
924 prefetch(pos->member.prev), &pos->member != (head); \
925 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
928 * cds_list_for_each_entry_from - iterate over list of given type from the current point
929 * @pos: the type * to use as a loop cursor.
930 * @head: the head for your list.
931 * @member: the name of the list_struct within the struct.
933 * Iterate over list of given type, continuing from current position.
935 #define cds_list_for_each_entry_from(pos, head, member) \
936 for (; prefetch(pos->member.next), &pos->member != (head); \
937 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
940 * cds_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
941 * @pos: the type * to use as a loop cursor.
942 * @n: another type * to use as temporary storage
943 * @head: the head for your list.
944 * @member: the name of the list_struct within the struct.
946 #define cds_list_for_each_entry_safe(pos, n, head, member) \
947 for (pos = cds_list_entry((head)->next, typeof(*pos), member), \
948 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
949 &pos->member != (head); \
950 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
953 * cds_list_for_each_entry_safe_continue
954 * @pos: the type * to use as a loop cursor.
955 * @n: another type * to use as temporary storage
956 * @head: the head for your list.
957 * @member: the name of the list_struct within the struct.
959 * Iterate over list of given type, continuing after current point,
960 * safe against removal of list entry.
962 #define cds_list_for_each_entry_safe_continue(pos, n, head, member) \
963 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member), \
964 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
965 &pos->member != (head); \
966 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
969 * cds_list_for_each_entry_safe_from
970 * @pos: the type * to use as a loop cursor.
971 * @n: another type * to use as temporary storage
972 * @head: the head for your list.
973 * @member: the name of the list_struct within the struct.
975 * Iterate over list of given type from current point, safe against
976 * removal of list entry.
978 #define cds_list_for_each_entry_safe_from(pos, n, head, member) \
979 for (n = cds_list_entry(pos->member.next, typeof(*pos), member); \
980 &pos->member != (head); \
981 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
984 * cds_list_for_each_entry_safe_reverse
985 * @pos: the type * to use as a loop cursor.
986 * @n: another type * to use as temporary storage
987 * @head: the head for your list.
988 * @member: the name of the list_struct within the struct.
990 * Iterate backwards over list of given type, safe against removal
993 #define cds_list_for_each_entry_safe_reverse(pos, n, head, member) \
994 for (pos = cds_list_entry((head)->prev, typeof(*pos), member), \
995 n = cds_list_entry(pos->member.prev, typeof(*pos), member); \
996 &pos->member != (head); \
997 pos = n, n = cds_list_entry(n->member.prev, typeof(*n), member))
1002 * Double linked lists with a single pointer list head.
1003 * Mostly useful for hash tables where the two pointer list head is
1005 * You lose the ability to access the tail in O(1).
1008 struct cds_hlist_head
{
1009 struct cds_hlist_node
*first
;
1012 struct cds_hlist_node
{
1013 struct cds_hlist_node
*next
, **pprev
;
1016 #define HLIST_HEAD_INIT { .first = NULL }
1017 #define HLIST_HEAD(name) struct cds_hlist_head name = { .first = NULL }
1018 #define CDS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1019 static inline void INIT_HLIST_NODE(struct cds_hlist_node
*h
)
1025 static inline int hlist_unhashed(const struct cds_hlist_node
*h
)
1030 static inline int hlist_empty(const struct cds_hlist_head
*h
)
1035 static inline void __cds_hlist_del(struct cds_hlist_node
*n
)
1037 struct cds_hlist_node
*next
= n
->next
;
1038 struct cds_hlist_node
**pprev
= n
->pprev
;
1041 next
->pprev
= pprev
;
1044 static inline void cds_hlist_del(struct cds_hlist_node
*n
)
1047 n
->next
= LIST_POISON1
;
1048 n
->pprev
= LIST_POISON2
;
1051 static inline void cds_hlist_del_init(struct cds_hlist_node
*n
)
1053 if (!hlist_unhashed(n
)) {
1059 static inline void cds_hlist_add_head(struct cds_hlist_node
*n
, struct cds_hlist_head
*h
)
1061 struct cds_hlist_node
*first
= h
->first
;
1064 first
->pprev
= &n
->next
;
1066 n
->pprev
= &h
->first
;
1069 /* next must be != NULL */
1070 static inline void hlist_add_before(struct cds_hlist_node
*n
,
1071 struct cds_hlist_node
*next
)
1073 n
->pprev
= next
->pprev
;
1075 next
->pprev
= &n
->next
;
1079 static inline void hlist_add_after(struct cds_hlist_node
*n
,
1080 struct cds_hlist_node
*next
)
1082 next
->next
= n
->next
;
1084 next
->pprev
= &n
->next
;
1087 next
->next
->pprev
= &next
->next
;
1091 * Move a list from one list head to another. Fixup the pprev
1092 * reference of the first entry if it exists.
1094 static inline void hlist_move_list(struct cds_hlist_head
*old
,
1095 struct cds_hlist_head
*new)
1097 new->first
= old
->first
;
1099 new->first
->pprev
= &new->first
;
1103 #define cds_hlist_entry(ptr, type, member) caa_container_of(ptr,type,member)
1105 #define cds_hlist_for_each(pos, head) \
1106 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1109 #define cds_hlist_for_each_safe(pos, n, head) \
1110 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1114 * cds_hlist_for_each_entry - iterate over list of given type
1115 * @tpos: the type * to use as a loop cursor.
1116 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1117 * @head: the head for your list.
1118 * @member: the name of the cds_hlist_node within the struct.
1120 #define cds_hlist_for_each_entry(tpos, pos, head, member) \
1121 for (pos = (head)->first; \
1122 pos && ({ prefetch(pos->next); 1;}) && \
1123 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1127 * cds_hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1128 * @tpos: the type * to use as a loop cursor.
1129 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1130 * @member: the name of the cds_hlist_node within the struct.
1132 #define cds_hlist_for_each_entry_continue(tpos, pos, member) \
1133 for (pos = (pos)->next; \
1134 pos && ({ prefetch(pos->next); 1;}) && \
1135 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1139 * cds_hlist_for_each_entry_from - iterate over a hlist continuing from current point
1140 * @tpos: the type * to use as a loop cursor.
1141 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1142 * @member: the name of the cds_hlist_node within the struct.
1144 #define cds_hlist_for_each_entry_from(tpos, pos, member) \
1145 for (; pos && ({ prefetch(pos->next); 1;}) && \
1146 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1150 * cds_hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1151 * @tpos: the type * to use as a loop cursor.
1152 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1153 * @n: another &struct cds_hlist_node to use as temporary storage
1154 * @head: the head for your list.
1155 * @member: the name of the cds_hlist_node within the struct.
1157 #define cds_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1158 for (pos = (head)->first; \
1159 pos && ({ n = pos->next; 1; }) && \
1160 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \