1 /* MECHANICALLY GENERATED, DO NOT EDIT!!! */
7 * common.h: Common Linux kernel-isms.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; but version 2 of the License only due
12 * to code included from the Linux kernel.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Copyright (c) 2006 Paul E. McKenney, IBM.
25 * Much code taken from the Linux kernel. For such code, the option
26 * to redistribute under later versions of GPL might not be available.
29 #include <urcu/arch.h>
31 #ifndef __always_inline
32 #define __always_inline inline
35 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
36 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
39 # define stringify_in_c(...) __VA_ARGS__
40 # define ASM_CONST(x) x
42 /* This version of stringify will deal with commas... */
43 # define __stringify_in_c(...) #__VA_ARGS__
44 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
45 # define __ASM_CONST(x) x##UL
46 # define ASM_CONST(x) __ASM_CONST(x)
51 * arch-ppc64.h: Expose PowerPC atomic instructions.
53 * This program is free software; you can redistribute it and/or modify
54 * it under the terms of the GNU General Public License as published by
55 * the Free Software Foundation; but version 2 of the License only due
56 * to code included from the Linux kernel.
58 * This program is distributed in the hope that it will be useful,
59 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 * GNU General Public License for more details.
63 * You should have received a copy of the GNU General Public License
64 * along with this program; if not, write to the Free Software
65 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
67 * Copyright (c) 2006 Paul E. McKenney, IBM.
69 * Much code taken from the Linux kernel. For such code, the option
70 * to redistribute under later versions of GPL might not be available.
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 0
168 #define __THREAD_ID_MAP_WAITING 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 static void run_on(int cpu
)
291 sched_setaffinity(0, sizeof(mask
), &mask
);
295 * timekeeping -- very crude -- should use MONOTONIC...
298 long long get_microseconds(void)
302 if (gettimeofday(&tv
, NULL
) != 0)
304 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
308 * Per-thread variables.
311 #define DEFINE_PER_THREAD(type, name) \
314 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
315 } __per_thread_##name[NR_THREADS];
316 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
318 #define per_thread(name, thread) __per_thread_##name[thread].v
319 #define __get_thread_var(name) per_thread(name, smp_thread_id())
321 #define init_per_thread(name, v) \
324 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
325 per_thread(name, __i_p_t_i) = v; \
329 * CPU traversal primitives.
334 #endif /* #ifndef NR_CPUS */
336 #define for_each_possible_cpu(cpu) \
337 for (cpu = 0; cpu < NR_CPUS; cpu++)
338 #define for_each_online_cpu(cpu) \
339 for (cpu = 0; cpu < NR_CPUS; cpu++)
345 #define DEFINE_PER_CPU(type, name) \
348 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
349 } __per_cpu_##name[NR_CPUS]
350 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
352 DEFINE_PER_THREAD(int, smp_processor_id
);
354 #define per_cpu(name, thread) __per_cpu_##name[thread].v
355 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
357 #define init_per_cpu(name, v) \
360 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
361 per_cpu(name, __i_p_c_i) = v; \
365 * CPU state checking (crowbarred).
368 #define idle_cpu(cpu) 0
369 #define in_softirq() 1
370 #define hardirq_count() 0
371 #define PREEMPT_SHIFT 0
372 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
373 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
374 #define PREEMPT_BITS 8
375 #define SOFTIRQ_BITS 8
381 struct notifier_block
{
382 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
383 struct notifier_block
*next
;
387 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
388 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
389 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
390 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
391 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
392 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
393 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
394 * not handling interrupts, soon dead */
395 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
398 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
399 * operation in progress
401 #define CPU_TASKS_FROZEN 0x0010
403 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
404 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
405 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
406 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
407 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
408 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
409 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
411 /* Hibernation and suspend events */
412 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
413 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
414 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
415 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
416 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
417 #define PM_POST_RESTORE 0x0006 /* Restore failed */
419 #define NOTIFY_DONE 0x0000 /* Don't care */
420 #define NOTIFY_OK 0x0001 /* Suits me */
421 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
422 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
423 /* Bad/Veto action */
425 * Clean way to return from the notifier and stop further calls.
427 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
433 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
436 * Initialization -- Must be called before calling any primitives.
439 static void smp_init(void)
443 spin_lock_init(&__thread_id_map_mutex
);
444 __thread_id_map
[0] = pthread_self();
445 for (i
= 1; i
< NR_THREADS
; i
++)
446 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
447 init_per_thread(smp_processor_id
, 0);
448 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
449 perror("pthread_key_create");
454 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
456 #ifndef _LINUX_LIST_H
457 #define _LINUX_LIST_H
459 #define LIST_POISON1 ((void *) 0x00100100)
460 #define LIST_POISON2 ((void *) 0x00200200)
465 * Simple doubly linked list implementation.
467 * Some of the internal functions ("__xxx") are useful when
468 * manipulating whole lists rather than single entries, as
469 * sometimes we already know the next/prev entries and we can
470 * generate better code by using them directly rather than
471 * using the generic single-entry routines.
474 struct cds_list_head
{
475 struct cds_list_head
*next
, *prev
;
478 #define CDS_LIST_HEAD_INIT(name) { &(name), &(name) }
480 #define CDS_LIST_HEAD(name) \
481 struct cds_list_head name = CDS_LIST_HEAD_INIT(name)
483 static inline void CDS_INIT_LIST_HEAD(struct cds_list_head
*list
)
490 * Insert a new entry between two known consecutive entries.
492 * This is only for internal list manipulation where we know
493 * the prev/next entries already!
495 #ifndef CONFIG_DEBUG_LIST
496 static inline void __cds_list_add(struct cds_list_head
*new,
497 struct cds_list_head
*prev
,
498 struct cds_list_head
*next
)
506 extern void __cds_list_add(struct cds_list_head
*new,
507 struct cds_list_head
*prev
,
508 struct cds_list_head
*next
);
512 * cds_list_add - add a new entry
513 * @new: new entry to be added
514 * @head: list head to add it after
516 * Insert a new entry after the specified head.
517 * This is good for implementing stacks.
519 static inline void cds_list_add(struct cds_list_head
*new, struct cds_list_head
*head
)
521 __cds_list_add(new, head
, head
->next
);
526 * cds_list_add_tail - add a new entry
527 * @new: new entry to be added
528 * @head: list head to add it before
530 * Insert a new entry before the specified head.
531 * This is useful for implementing queues.
533 static inline void cds_list_add_tail(struct cds_list_head
*new, struct cds_list_head
*head
)
535 __cds_list_add(new, head
->prev
, head
);
539 * Delete a list entry by making the prev/next entries
540 * point to each other.
542 * This is only for internal list manipulation where we know
543 * the prev/next entries already!
545 static inline void __cds_list_del(struct cds_list_head
* prev
, struct cds_list_head
* next
)
552 * cds_list_del - deletes entry from list.
553 * @entry: the element to delete from the list.
554 * Note: cds_list_empty() on entry does not return true after this, the entry is
555 * in an undefined state.
557 #ifndef CONFIG_DEBUG_LIST
558 static inline void cds_list_del(struct cds_list_head
*entry
)
560 __cds_list_del(entry
->prev
, entry
->next
);
561 entry
->next
= LIST_POISON1
;
562 entry
->prev
= LIST_POISON2
;
565 extern void cds_list_del(struct cds_list_head
*entry
);
569 * cds_list_replace - replace old entry by new one
570 * @old : the element to be replaced
571 * @new : the new element to insert
573 * If @old was empty, it will be overwritten.
575 static inline void cds_list_replace(struct cds_list_head
*old
,
576 struct cds_list_head
*new)
578 new->next
= old
->next
;
579 new->next
->prev
= new;
580 new->prev
= old
->prev
;
581 new->prev
->next
= new;
584 static inline void cds_list_replace_init(struct cds_list_head
*old
,
585 struct cds_list_head
*new)
587 cds_list_replace(old
, new);
588 CDS_INIT_LIST_HEAD(old
);
592 * cds_list_del_init - deletes entry from list and reinitialize it.
593 * @entry: the element to delete from the list.
595 static inline void cds_list_del_init(struct cds_list_head
*entry
)
597 __cds_list_del(entry
->prev
, entry
->next
);
598 CDS_INIT_LIST_HEAD(entry
);
602 * cds_list_move - delete from one list and add as another's head
603 * @list: the entry to move
604 * @head: the head that will precede our entry
606 static inline void cds_list_move(struct cds_list_head
*list
, struct cds_list_head
*head
)
608 __cds_list_del(list
->prev
, list
->next
);
609 cds_list_add(list
, head
);
613 * cds_list_move_tail - delete from one list and add as another's tail
614 * @list: the entry to move
615 * @head: the head that will follow our entry
617 static inline void cds_list_move_tail(struct cds_list_head
*list
,
618 struct cds_list_head
*head
)
620 __cds_list_del(list
->prev
, list
->next
);
621 cds_list_add_tail(list
, head
);
625 * list_is_last - tests whether @list is the last entry in list @head
626 * @list: the entry to test
627 * @head: the head of the list
629 static inline int list_is_last(const struct cds_list_head
*list
,
630 const struct cds_list_head
*head
)
632 return list
->next
== head
;
636 * cds_list_empty - tests whether a list is empty
637 * @head: the list to test.
639 static inline int cds_list_empty(const struct cds_list_head
*head
)
641 return head
->next
== head
;
645 * cds_list_empty_careful - tests whether a list is empty and not being modified
646 * @head: the list to test
649 * tests whether a list is empty _and_ checks that no other CPU might be
650 * in the process of modifying either member (next or prev)
652 * NOTE: using cds_list_empty_careful() without synchronization
653 * can only be safe if the only activity that can happen
654 * to the list entry is cds_list_del_init(). Eg. it cannot be used
655 * if another CPU could re-list_add() it.
657 static inline int cds_list_empty_careful(const struct cds_list_head
*head
)
659 struct cds_list_head
*next
= head
->next
;
660 return (next
== head
) && (next
== head
->prev
);
664 * list_is_singular - tests whether a list has just one entry.
665 * @head: the list to test.
667 static inline int list_is_singular(const struct cds_list_head
*head
)
669 return !list_empty(head
) && (head
->next
== head
->prev
);
672 static inline void __list_cut_position(struct cds_list_head
*list
,
673 struct cds_list_head
*head
, struct cds_list_head
*entry
)
675 struct cds_list_head
*new_first
= entry
->next
;
676 list
->next
= head
->next
;
677 list
->next
->prev
= list
;
680 head
->next
= new_first
;
681 new_first
->prev
= head
;
685 * list_cut_position - cut a list into two
686 * @list: a new list to add all removed entries
687 * @head: a list with entries
688 * @entry: an entry within head, could be the head itself
689 * and if so we won't cut the list
691 * This helper moves the initial part of @head, up to and
692 * including @entry, from @head to @list. You should
693 * pass on @entry an element you know is on @head. @list
694 * should be an empty list or a list you do not care about
698 static inline void list_cut_position(struct cds_list_head
*list
,
699 struct cds_list_head
*head
, struct cds_list_head
*entry
)
701 if (cds_list_empty(head
))
703 if (list_is_singular(head
) &&
704 (head
->next
!= entry
&& head
!= entry
))
707 CDS_INIT_LIST_HEAD(list
);
709 __list_cut_position(list
, head
, entry
);
712 static inline void __cds_list_splice(const struct cds_list_head
*list
,
713 struct cds_list_head
*prev
,
714 struct cds_list_head
*next
)
716 struct cds_list_head
*first
= list
->next
;
717 struct cds_list_head
*last
= list
->prev
;
727 * cds_list_splice - join two lists, this is designed for stacks
728 * @list: the new list to add.
729 * @head: the place to add it in the first list.
731 static inline void cds_list_splice(const struct cds_list_head
*list
,
732 struct cds_list_head
*head
)
734 if (!cds_list_empty(list
))
735 __cds_list_splice(list
, head
, head
->next
);
739 * cds_list_splice_tail - join two lists, each list being a queue
740 * @list: the new list to add.
741 * @head: the place to add it in the first list.
743 static inline void cds_list_splice_tail(struct cds_list_head
*list
,
744 struct cds_list_head
*head
)
746 if (!cds_list_empty(list
))
747 __cds_list_splice(list
, head
->prev
, head
);
751 * cds_list_splice_init - join two lists and reinitialise the emptied list.
752 * @list: the new list to add.
753 * @head: the place to add it in the first list.
755 * The list at @list is reinitialised
757 static inline void cds_list_splice_init(struct cds_list_head
*list
,
758 struct cds_list_head
*head
)
760 if (!cds_list_empty(list
)) {
761 __cds_list_splice(list
, head
, head
->next
);
762 CDS_INIT_LIST_HEAD(list
);
767 * cds_list_splice_tail_init - join two lists and reinitialise the emptied list
768 * @list: the new list to add.
769 * @head: the place to add it in the first list.
771 * Each of the lists is a queue.
772 * The list at @list is reinitialised
774 static inline void cds_list_splice_tail_init(struct cds_list_head
*list
,
775 struct cds_list_head
*head
)
777 if (!cds_list_empty(list
)) {
778 __cds_list_splice(list
, head
->prev
, head
);
779 CDS_INIT_LIST_HEAD(list
);
784 * cds_list_entry - get the struct for this entry
785 * @ptr: the &struct cds_list_head pointer.
786 * @type: the type of the struct this is embedded in.
787 * @member: the name of the list_struct within the struct.
789 #define cds_list_entry(ptr, type, member) \
790 caa_container_of(ptr, type, member)
793 * list_first_entry - get the first element from a list
794 * @ptr: the list head to take the element from.
795 * @type: the type of the struct this is embedded in.
796 * @member: the name of the list_struct within the struct.
798 * Note, that list is expected to be not empty.
800 #define list_first_entry(ptr, type, member) \
801 cds_list_entry((ptr)->next, type, member)
804 * cds_list_for_each - iterate over a list
805 * @pos: the &struct cds_list_head to use as a loop cursor.
806 * @head: the head for your list.
808 #define cds_list_for_each(pos, head) \
809 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
813 * __cds_list_for_each - iterate over a list
814 * @pos: the &struct cds_list_head to use as a loop cursor.
815 * @head: the head for your list.
817 * This variant differs from cds_list_for_each() in that it's the
818 * simplest possible list iteration code, no prefetching is done.
819 * Use this for code that knows the list to be very short (empty
820 * or 1 entry) most of the time.
822 #define __cds_list_for_each(pos, head) \
823 for (pos = (head)->next; pos != (head); pos = pos->next)
826 * cds_list_for_each_prev - iterate over a list backwards
827 * @pos: the &struct cds_list_head to use as a loop cursor.
828 * @head: the head for your list.
830 #define cds_list_for_each_prev(pos, head) \
831 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
835 * cds_list_for_each_safe - iterate over a list safe against removal of list entry
836 * @pos: the &struct cds_list_head to use as a loop cursor.
837 * @n: another &struct cds_list_head to use as temporary storage
838 * @head: the head for your list.
840 #define cds_list_for_each_safe(pos, n, head) \
841 for (pos = (head)->next, n = pos->next; pos != (head); \
842 pos = n, n = pos->next)
845 * cds_list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
846 * @pos: the &struct cds_list_head to use as a loop cursor.
847 * @n: another &struct cds_list_head to use as temporary storage
848 * @head: the head for your list.
850 #define cds_list_for_each_prev_safe(pos, n, head) \
851 for (pos = (head)->prev, n = pos->prev; \
852 prefetch(pos->prev), pos != (head); \
853 pos = n, n = pos->prev)
856 * cds_list_for_each_entry - iterate over list of given type
857 * @pos: the type * to use as a loop cursor.
858 * @head: the head for your list.
859 * @member: the name of the list_struct within the struct.
861 #define cds_list_for_each_entry(pos, head, member) \
862 for (pos = cds_list_entry((head)->next, typeof(*pos), member); \
863 prefetch(pos->member.next), &pos->member != (head); \
864 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
867 * cds_list_for_each_entry_reverse - iterate backwards 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_reverse(pos, head, member) \
873 for (pos = cds_list_entry((head)->prev, typeof(*pos), member); \
874 prefetch(pos->member.prev), &pos->member != (head); \
875 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
878 * list_prepare_entry - prepare a pos entry for use in cds_list_for_each_entry_continue()
879 * @pos: the type * to use as a start point
880 * @head: the head of the list
881 * @member: the name of the list_struct within the struct.
883 * Prepares a pos entry for use as a start point in cds_list_for_each_entry_continue().
885 #define list_prepare_entry(pos, head, member) \
886 ((pos) ? : cds_list_entry(head, typeof(*pos), member))
889 * cds_list_for_each_entry_continue - continue iteration over list of given type
890 * @pos: the type * to use as a loop cursor.
891 * @head: the head for your list.
892 * @member: the name of the list_struct within the struct.
894 * Continue to iterate over list of given type, continuing after
895 * the current position.
897 #define cds_list_for_each_entry_continue(pos, head, member) \
898 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member); \
899 prefetch(pos->member.next), &pos->member != (head); \
900 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
903 * cds_list_for_each_entry_continue_reverse - iterate backwards from the given point
904 * @pos: the type * to use as a loop cursor.
905 * @head: the head for your list.
906 * @member: the name of the list_struct within the struct.
908 * Start to iterate over list of given type backwards, continuing after
909 * the current position.
911 #define cds_list_for_each_entry_continue_reverse(pos, head, member) \
912 for (pos = cds_list_entry(pos->member.prev, typeof(*pos), member); \
913 prefetch(pos->member.prev), &pos->member != (head); \
914 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
917 * cds_list_for_each_entry_from - iterate over list of given type from the current point
918 * @pos: the type * to use as a loop cursor.
919 * @head: the head for your list.
920 * @member: the name of the list_struct within the struct.
922 * Iterate over list of given type, continuing from current position.
924 #define cds_list_for_each_entry_from(pos, head, member) \
925 for (; prefetch(pos->member.next), &pos->member != (head); \
926 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
929 * cds_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
930 * @pos: the type * to use as a loop cursor.
931 * @n: another type * to use as temporary storage
932 * @head: the head for your list.
933 * @member: the name of the list_struct within the struct.
935 #define cds_list_for_each_entry_safe(pos, n, head, member) \
936 for (pos = cds_list_entry((head)->next, typeof(*pos), member), \
937 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
938 &pos->member != (head); \
939 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
942 * cds_list_for_each_entry_safe_continue
943 * @pos: the type * to use as a loop cursor.
944 * @n: another type * to use as temporary storage
945 * @head: the head for your list.
946 * @member: the name of the list_struct within the struct.
948 * Iterate over list of given type, continuing after current point,
949 * safe against removal of list entry.
951 #define cds_list_for_each_entry_safe_continue(pos, n, head, member) \
952 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member), \
953 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
954 &pos->member != (head); \
955 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
958 * cds_list_for_each_entry_safe_from
959 * @pos: the type * to use as a loop cursor.
960 * @n: another type * to use as temporary storage
961 * @head: the head for your list.
962 * @member: the name of the list_struct within the struct.
964 * Iterate over list of given type from current point, safe against
965 * removal of list entry.
967 #define cds_list_for_each_entry_safe_from(pos, n, head, member) \
968 for (n = cds_list_entry(pos->member.next, typeof(*pos), member); \
969 &pos->member != (head); \
970 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
973 * cds_list_for_each_entry_safe_reverse
974 * @pos: the type * to use as a loop cursor.
975 * @n: another type * to use as temporary storage
976 * @head: the head for your list.
977 * @member: the name of the list_struct within the struct.
979 * Iterate backwards over list of given type, safe against removal
982 #define cds_list_for_each_entry_safe_reverse(pos, n, head, member) \
983 for (pos = cds_list_entry((head)->prev, typeof(*pos), member), \
984 n = cds_list_entry(pos->member.prev, typeof(*pos), member); \
985 &pos->member != (head); \
986 pos = n, n = cds_list_entry(n->member.prev, typeof(*n), member))
991 * Double linked lists with a single pointer list head.
992 * Mostly useful for hash tables where the two pointer list head is
994 * You lose the ability to access the tail in O(1).
997 struct cds_hlist_head
{
998 struct cds_hlist_node
*first
;
1001 struct cds_hlist_node
{
1002 struct cds_hlist_node
*next
, **pprev
;
1005 #define HLIST_HEAD_INIT { .first = NULL }
1006 #define HLIST_HEAD(name) struct cds_hlist_head name = { .first = NULL }
1007 #define CDS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1008 static inline void INIT_HLIST_NODE(struct cds_hlist_node
*h
)
1014 static inline int hlist_unhashed(const struct cds_hlist_node
*h
)
1019 static inline int hlist_empty(const struct cds_hlist_head
*h
)
1024 static inline void __cds_hlist_del(struct cds_hlist_node
*n
)
1026 struct cds_hlist_node
*next
= n
->next
;
1027 struct cds_hlist_node
**pprev
= n
->pprev
;
1030 next
->pprev
= pprev
;
1033 static inline void cds_hlist_del(struct cds_hlist_node
*n
)
1036 n
->next
= LIST_POISON1
;
1037 n
->pprev
= LIST_POISON2
;
1040 static inline void cds_hlist_del_init(struct cds_hlist_node
*n
)
1042 if (!hlist_unhashed(n
)) {
1048 static inline void cds_hlist_add_head(struct cds_hlist_node
*n
, struct cds_hlist_head
*h
)
1050 struct cds_hlist_node
*first
= h
->first
;
1053 first
->pprev
= &n
->next
;
1055 n
->pprev
= &h
->first
;
1058 /* next must be != NULL */
1059 static inline void hlist_add_before(struct cds_hlist_node
*n
,
1060 struct cds_hlist_node
*next
)
1062 n
->pprev
= next
->pprev
;
1064 next
->pprev
= &n
->next
;
1068 static inline void hlist_add_after(struct cds_hlist_node
*n
,
1069 struct cds_hlist_node
*next
)
1071 next
->next
= n
->next
;
1073 next
->pprev
= &n
->next
;
1076 next
->next
->pprev
= &next
->next
;
1080 * Move a list from one list head to another. Fixup the pprev
1081 * reference of the first entry if it exists.
1083 static inline void hlist_move_list(struct cds_hlist_head
*old
,
1084 struct cds_hlist_head
*new)
1086 new->first
= old
->first
;
1088 new->first
->pprev
= &new->first
;
1092 #define cds_hlist_entry(ptr, type, member) caa_container_of(ptr,type,member)
1094 #define cds_hlist_for_each(pos, head) \
1095 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1098 #define cds_hlist_for_each_safe(pos, n, head) \
1099 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1103 * cds_hlist_for_each_entry - iterate over list of given type
1104 * @tpos: the type * to use as a loop cursor.
1105 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1106 * @head: the head for your list.
1107 * @member: the name of the cds_hlist_node within the struct.
1109 #define cds_hlist_for_each_entry(tpos, pos, head, member) \
1110 for (pos = (head)->first; \
1111 pos && ({ prefetch(pos->next); 1;}) && \
1112 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1116 * cds_hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1117 * @tpos: the type * to use as a loop cursor.
1118 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1119 * @member: the name of the cds_hlist_node within the struct.
1121 #define cds_hlist_for_each_entry_continue(tpos, pos, member) \
1122 for (pos = (pos)->next; \
1123 pos && ({ prefetch(pos->next); 1;}) && \
1124 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1128 * cds_hlist_for_each_entry_from - iterate over a hlist continuing from current point
1129 * @tpos: the type * to use as a loop cursor.
1130 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1131 * @member: the name of the cds_hlist_node within the struct.
1133 #define cds_hlist_for_each_entry_from(tpos, pos, member) \
1134 for (; pos && ({ prefetch(pos->next); 1;}) && \
1135 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1139 * cds_hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1140 * @tpos: the type * to use as a loop cursor.
1141 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1142 * @n: another &struct cds_hlist_node to use as temporary storage
1143 * @head: the head for your list.
1144 * @member: the name of the cds_hlist_node within the struct.
1146 #define cds_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1147 for (pos = (head)->first; \
1148 pos && ({ n = pos->next; 1; }) && \
1149 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \