1 /* MECHANICALLY GENERATED, DO NOT EDIT!!! */
9 * common.h: Common Linux kernel-isms.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; but version 2 of the License only due
14 * to code included from the Linux kernel.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Copyright (c) 2006 Paul E. McKenney, IBM.
27 * Much code taken from the Linux kernel. For such code, the option
28 * to redistribute under later versions of GPL might not be available.
31 #include <urcu/arch.h>
33 #ifndef __always_inline
34 #define __always_inline inline
37 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
38 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
41 # define stringify_in_c(...) __VA_ARGS__
42 # define ASM_CONST(x) x
44 /* This version of stringify will deal with commas... */
45 # define __stringify_in_c(...) #__VA_ARGS__
46 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
47 # define __ASM_CONST(x) x##UL
48 # define ASM_CONST(x) __ASM_CONST(x)
53 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
55 * This program is free software; you can redistribute it and/or modify
56 * it under the terms of the GNU General Public License as published by
57 * the Free Software Foundation, but version 2 only due to inclusion
58 * of Linux-kernel code.
60 * This program is distributed in the hope that it will be useful,
61 * but WITHOUT ANY WARRANTY; without even the implied warranty of
62 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 * GNU General Public License for more details.
65 * You should have received a copy of the GNU General Public License
66 * along with this program; if not, write to the Free Software
67 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
69 * Copyright (c) 2006 Paul E. McKenney, IBM.
71 * Much code taken from the Linux kernel. For such code, the option
72 * to redistribute under later versions of GPL might not be available.
79 /* #define CAA_CACHE_LINE_SIZE 64 */
80 #define ____cacheline_internodealigned_in_smp \
81 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
84 * api_pthreads.h: API mapping to pthreads environment.
86 * This program is free software; you can redistribute it and/or modify
87 * it under the terms of the GNU General Public License as published by
88 * the Free Software Foundation; either version 2 of the License, or
89 * (at your option) any later version. However, please note that much
90 * of the code in this file derives from the Linux kernel, and that such
91 * code may not be available except under GPLv2.
93 * This program is distributed in the hope that it will be useful,
94 * but WITHOUT ANY WARRANTY; without even the implied warranty of
95 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96 * GNU General Public License for more details.
98 * You should have received a copy of the GNU General Public License
99 * along with this program; if not, write to the Free Software
100 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
102 * Copyright (c) 2006 Paul E. McKenney, IBM.
109 #include <sys/types.h>
113 #include <sys/param.h>
114 /* #include "atomic.h" */
117 * Default machine parameters.
120 #ifndef CAA_CACHE_LINE_SIZE
121 /* #define CAA_CACHE_LINE_SIZE 128 */
122 #endif /* #ifndef CAA_CACHE_LINE_SIZE */
125 * Exclusive locking primitives.
128 typedef pthread_mutex_t spinlock_t
;
130 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
131 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
133 static void spin_lock_init(spinlock_t
*sp
)
135 if (pthread_mutex_init(sp
, NULL
) != 0) {
136 perror("spin_lock_init:pthread_mutex_init");
141 static void spin_lock(spinlock_t
*sp
)
143 if (pthread_mutex_lock(sp
) != 0) {
144 perror("spin_lock:pthread_mutex_lock");
149 static void spin_unlock(spinlock_t
*sp
)
151 if (pthread_mutex_unlock(sp
) != 0) {
152 perror("spin_unlock:pthread_mutex_unlock");
157 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
158 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
161 * Thread creation/destruction primitives.
164 typedef pthread_t thread_id_t
;
166 #define NR_THREADS 128
168 #define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0)
169 #define __THREAD_ID_MAP_WAITING ((thread_id_t) 1)
170 thread_id_t __thread_id_map
[NR_THREADS
];
171 spinlock_t __thread_id_map_mutex
;
173 #define for_each_thread(t) \
174 for (t = 0; t < NR_THREADS; t++)
176 #define for_each_running_thread(t) \
177 for (t = 0; t < NR_THREADS; t++) \
178 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
179 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
181 pthread_key_t thread_id_key
;
183 static int __smp_thread_id(void)
186 thread_id_t tid
= pthread_self();
188 for (i
= 0; i
< NR_THREADS
; i
++) {
189 if (__thread_id_map
[i
] == tid
) {
190 long v
= i
+ 1; /* must be non-NULL. */
192 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
193 perror("pthread_setspecific");
199 spin_lock(&__thread_id_map_mutex
);
200 for (i
= 0; i
< NR_THREADS
; i
++) {
201 if (__thread_id_map
[i
] == tid
)
202 spin_unlock(&__thread_id_map_mutex
);
205 spin_unlock(&__thread_id_map_mutex
);
206 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
211 static int smp_thread_id(void)
215 id
= pthread_getspecific(thread_id_key
);
217 return __smp_thread_id();
218 return (long)(id
- 1);
221 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
226 spin_lock(&__thread_id_map_mutex
);
227 for (i
= 0; i
< NR_THREADS
; i
++) {
228 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
231 if (i
>= NR_THREADS
) {
232 spin_unlock(&__thread_id_map_mutex
);
233 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
236 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
237 spin_unlock(&__thread_id_map_mutex
);
238 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
239 perror("create_thread:pthread_create");
242 __thread_id_map
[i
] = tid
;
246 static void *wait_thread(thread_id_t tid
)
251 for (i
= 0; i
< NR_THREADS
; i
++) {
252 if (__thread_id_map
[i
] == tid
)
255 if (i
>= NR_THREADS
){
256 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
260 if (pthread_join(tid
, &vp
) != 0) {
261 perror("wait_thread:pthread_join");
264 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
268 static void wait_all_threads(void)
273 for (i
= 1; i
< NR_THREADS
; i
++) {
274 tid
= __thread_id_map
[i
];
275 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
276 tid
!= __THREAD_ID_MAP_WAITING
)
277 (void)wait_thread(tid
);
281 #ifndef HAVE_CPU_SET_T
282 typedef unsigned long cpu_set_t
;
283 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
284 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
287 static void run_on(int cpu
)
289 #if HAVE_SCHED_SETAFFINITY
294 #if SCHED_SETAFFINITY_ARGS == 2
295 sched_setaffinity(0, &mask
);
297 sched_setaffinity(0, sizeof(mask
), &mask
);
299 #endif /* HAVE_SCHED_SETAFFINITY */
303 * timekeeping -- very crude -- should use MONOTONIC...
306 long long get_microseconds(void)
310 if (gettimeofday(&tv
, NULL
) != 0)
312 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
316 * Per-thread variables.
319 #define DEFINE_PER_THREAD(type, name) \
322 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
323 } __per_thread_##name[NR_THREADS];
324 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
326 #define per_thread(name, thread) __per_thread_##name[thread].v
327 #define __get_thread_var(name) per_thread(name, smp_thread_id())
329 #define init_per_thread(name, v) \
332 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
333 per_thread(name, __i_p_t_i) = v; \
337 * CPU traversal primitives.
342 #endif /* #ifndef NR_CPUS */
344 #define for_each_possible_cpu(cpu) \
345 for (cpu = 0; cpu < NR_CPUS; cpu++)
346 #define for_each_online_cpu(cpu) \
347 for (cpu = 0; cpu < NR_CPUS; cpu++)
353 #define DEFINE_PER_CPU(type, name) \
356 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
357 } __per_cpu_##name[NR_CPUS]
358 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
360 DEFINE_PER_THREAD(int, smp_processor_id
);
362 #define per_cpu(name, thread) __per_cpu_##name[thread].v
363 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
365 #define init_per_cpu(name, v) \
368 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
369 per_cpu(name, __i_p_c_i) = v; \
373 * CPU state checking (crowbarred).
376 #define idle_cpu(cpu) 0
377 #define in_softirq() 1
378 #define hardirq_count() 0
379 #define PREEMPT_SHIFT 0
380 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
381 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
382 #define PREEMPT_BITS 8
383 #define SOFTIRQ_BITS 8
389 struct notifier_block
{
390 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
391 struct notifier_block
*next
;
395 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
396 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
397 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
398 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
399 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
400 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
401 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
402 * not handling interrupts, soon dead */
403 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
406 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
407 * operation in progress
409 #define CPU_TASKS_FROZEN 0x0010
411 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
412 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
413 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
414 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
415 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
416 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
417 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
419 /* Hibernation and suspend events */
420 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
421 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
422 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
423 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
424 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
425 #define PM_POST_RESTORE 0x0006 /* Restore failed */
427 #define NOTIFY_DONE 0x0000 /* Don't care */
428 #define NOTIFY_OK 0x0001 /* Suits me */
429 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
430 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
431 /* Bad/Veto action */
433 * Clean way to return from the notifier and stop further calls.
435 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
441 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
444 * Initialization -- Must be called before calling any primitives.
447 static void smp_init(void)
451 spin_lock_init(&__thread_id_map_mutex
);
452 __thread_id_map
[0] = pthread_self();
453 for (i
= 1; i
< NR_THREADS
; i
++)
454 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
455 init_per_thread(smp_processor_id
, 0);
456 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
457 perror("pthread_key_create");
462 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
464 #ifndef _LINUX_LIST_H
465 #define _LINUX_LIST_H
467 #define LIST_POISON1 ((void *) 0x00100100)
468 #define LIST_POISON2 ((void *) 0x00200200)
473 * Simple doubly linked list implementation.
475 * Some of the internal functions ("__xxx") are useful when
476 * manipulating whole lists rather than single entries, as
477 * sometimes we already know the next/prev entries and we can
478 * generate better code by using them directly rather than
479 * using the generic single-entry routines.
482 struct cds_list_head
{
483 struct cds_list_head
*next
, *prev
;
486 #define CDS_LIST_HEAD_INIT(name) { &(name), &(name) }
488 #define CDS_LIST_HEAD(name) \
489 struct cds_list_head name = CDS_LIST_HEAD_INIT(name)
491 static inline void CDS_INIT_LIST_HEAD(struct cds_list_head
*list
)
498 * Insert a new entry between two known consecutive entries.
500 * This is only for internal list manipulation where we know
501 * the prev/next entries already!
503 #ifndef CONFIG_DEBUG_LIST
504 static inline void __cds_list_add(struct cds_list_head
*new,
505 struct cds_list_head
*prev
,
506 struct cds_list_head
*next
)
514 extern void __cds_list_add(struct cds_list_head
*new,
515 struct cds_list_head
*prev
,
516 struct cds_list_head
*next
);
520 * cds_list_add - add a new entry
521 * @new: new entry to be added
522 * @head: list head to add it after
524 * Insert a new entry after the specified head.
525 * This is good for implementing stacks.
527 static inline void cds_list_add(struct cds_list_head
*new, struct cds_list_head
*head
)
529 __cds_list_add(new, head
, head
->next
);
534 * cds_list_add_tail - add a new entry
535 * @new: new entry to be added
536 * @head: list head to add it before
538 * Insert a new entry before the specified head.
539 * This is useful for implementing queues.
541 static inline void cds_list_add_tail(struct cds_list_head
*new, struct cds_list_head
*head
)
543 __cds_list_add(new, head
->prev
, head
);
547 * Delete a list entry by making the prev/next entries
548 * point to each other.
550 * This is only for internal list manipulation where we know
551 * the prev/next entries already!
553 static inline void __cds_list_del(struct cds_list_head
* prev
, struct cds_list_head
* next
)
560 * cds_list_del - deletes entry from list.
561 * @entry: the element to delete from the list.
562 * Note: cds_list_empty() on entry does not return true after this, the entry is
563 * in an undefined state.
565 #ifndef CONFIG_DEBUG_LIST
566 static inline void cds_list_del(struct cds_list_head
*entry
)
568 __cds_list_del(entry
->prev
, entry
->next
);
569 entry
->next
= LIST_POISON1
;
570 entry
->prev
= LIST_POISON2
;
573 extern void cds_list_del(struct cds_list_head
*entry
);
577 * cds_list_replace - replace old entry by new one
578 * @old : the element to be replaced
579 * @new : the new element to insert
581 * If @old was empty, it will be overwritten.
583 static inline void cds_list_replace(struct cds_list_head
*old
,
584 struct cds_list_head
*new)
586 new->next
= old
->next
;
587 new->next
->prev
= new;
588 new->prev
= old
->prev
;
589 new->prev
->next
= new;
592 static inline void cds_list_replace_init(struct cds_list_head
*old
,
593 struct cds_list_head
*new)
595 cds_list_replace(old
, new);
596 CDS_INIT_LIST_HEAD(old
);
600 * cds_list_del_init - deletes entry from list and reinitialize it.
601 * @entry: the element to delete from the list.
603 static inline void cds_list_del_init(struct cds_list_head
*entry
)
605 __cds_list_del(entry
->prev
, entry
->next
);
606 CDS_INIT_LIST_HEAD(entry
);
610 * cds_list_move - delete from one list and add as another's head
611 * @list: the entry to move
612 * @head: the head that will precede our entry
614 static inline void cds_list_move(struct cds_list_head
*list
, struct cds_list_head
*head
)
616 __cds_list_del(list
->prev
, list
->next
);
617 cds_list_add(list
, head
);
621 * cds_list_move_tail - delete from one list and add as another's tail
622 * @list: the entry to move
623 * @head: the head that will follow our entry
625 static inline void cds_list_move_tail(struct cds_list_head
*list
,
626 struct cds_list_head
*head
)
628 __cds_list_del(list
->prev
, list
->next
);
629 cds_list_add_tail(list
, head
);
633 * list_is_last - tests whether @list is the last entry in list @head
634 * @list: the entry to test
635 * @head: the head of the list
637 static inline int list_is_last(const struct cds_list_head
*list
,
638 const struct cds_list_head
*head
)
640 return list
->next
== head
;
644 * cds_list_empty - tests whether a list is empty
645 * @head: the list to test.
647 static inline int cds_list_empty(const struct cds_list_head
*head
)
649 return head
->next
== head
;
653 * cds_list_empty_careful - tests whether a list is empty and not being modified
654 * @head: the list to test
657 * tests whether a list is empty _and_ checks that no other CPU might be
658 * in the process of modifying either member (next or prev)
660 * NOTE: using cds_list_empty_careful() without synchronization
661 * can only be safe if the only activity that can happen
662 * to the list entry is cds_list_del_init(). Eg. it cannot be used
663 * if another CPU could re-list_add() it.
665 static inline int cds_list_empty_careful(const struct cds_list_head
*head
)
667 struct cds_list_head
*next
= head
->next
;
668 return (next
== head
) && (next
== head
->prev
);
672 * list_is_singular - tests whether a list has just one entry.
673 * @head: the list to test.
675 static inline int list_is_singular(const struct cds_list_head
*head
)
677 return !list_empty(head
) && (head
->next
== head
->prev
);
680 static inline void __list_cut_position(struct cds_list_head
*list
,
681 struct cds_list_head
*head
, struct cds_list_head
*entry
)
683 struct cds_list_head
*new_first
= entry
->next
;
684 list
->next
= head
->next
;
685 list
->next
->prev
= list
;
688 head
->next
= new_first
;
689 new_first
->prev
= head
;
693 * list_cut_position - cut a list into two
694 * @list: a new list to add all removed entries
695 * @head: a list with entries
696 * @entry: an entry within head, could be the head itself
697 * and if so we won't cut the list
699 * This helper moves the initial part of @head, up to and
700 * including @entry, from @head to @list. You should
701 * pass on @entry an element you know is on @head. @list
702 * should be an empty list or a list you do not care about
706 static inline void list_cut_position(struct cds_list_head
*list
,
707 struct cds_list_head
*head
, struct cds_list_head
*entry
)
709 if (cds_list_empty(head
))
711 if (list_is_singular(head
) &&
712 (head
->next
!= entry
&& head
!= entry
))
715 CDS_INIT_LIST_HEAD(list
);
717 __list_cut_position(list
, head
, entry
);
720 static inline void __cds_list_splice(const struct cds_list_head
*list
,
721 struct cds_list_head
*prev
,
722 struct cds_list_head
*next
)
724 struct cds_list_head
*first
= list
->next
;
725 struct cds_list_head
*last
= list
->prev
;
735 * cds_list_splice - join two lists, this is designed for stacks
736 * @list: the new list to add.
737 * @head: the place to add it in the first list.
739 static inline void cds_list_splice(const struct cds_list_head
*list
,
740 struct cds_list_head
*head
)
742 if (!cds_list_empty(list
))
743 __cds_list_splice(list
, head
, head
->next
);
747 * cds_list_splice_tail - join two lists, each list being a queue
748 * @list: the new list to add.
749 * @head: the place to add it in the first list.
751 static inline void cds_list_splice_tail(struct cds_list_head
*list
,
752 struct cds_list_head
*head
)
754 if (!cds_list_empty(list
))
755 __cds_list_splice(list
, head
->prev
, head
);
759 * cds_list_splice_init - join two lists and reinitialise the emptied list.
760 * @list: the new list to add.
761 * @head: the place to add it in the first list.
763 * The list at @list is reinitialised
765 static inline void cds_list_splice_init(struct cds_list_head
*list
,
766 struct cds_list_head
*head
)
768 if (!cds_list_empty(list
)) {
769 __cds_list_splice(list
, head
, head
->next
);
770 CDS_INIT_LIST_HEAD(list
);
775 * cds_list_splice_tail_init - join two lists and reinitialise the emptied list
776 * @list: the new list to add.
777 * @head: the place to add it in the first list.
779 * Each of the lists is a queue.
780 * The list at @list is reinitialised
782 static inline void cds_list_splice_tail_init(struct cds_list_head
*list
,
783 struct cds_list_head
*head
)
785 if (!cds_list_empty(list
)) {
786 __cds_list_splice(list
, head
->prev
, head
);
787 CDS_INIT_LIST_HEAD(list
);
792 * cds_list_entry - get the struct for this entry
793 * @ptr: the &struct cds_list_head pointer.
794 * @type: the type of the struct this is embedded in.
795 * @member: the name of the list_struct within the struct.
797 #define cds_list_entry(ptr, type, member) \
798 caa_container_of(ptr, type, member)
801 * list_first_entry - get the first element from a list
802 * @ptr: the list head to take the element from.
803 * @type: the type of the struct this is embedded in.
804 * @member: the name of the list_struct within the struct.
806 * Note, that list is expected to be not empty.
808 #define list_first_entry(ptr, type, member) \
809 cds_list_entry((ptr)->next, type, member)
812 * cds_list_for_each - iterate over a list
813 * @pos: the &struct cds_list_head to use as a loop cursor.
814 * @head: the head for your list.
816 #define cds_list_for_each(pos, head) \
817 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
821 * __cds_list_for_each - iterate over a list
822 * @pos: the &struct cds_list_head to use as a loop cursor.
823 * @head: the head for your list.
825 * This variant differs from cds_list_for_each() in that it's the
826 * simplest possible list iteration code, no prefetching is done.
827 * Use this for code that knows the list to be very short (empty
828 * or 1 entry) most of the time.
830 #define __cds_list_for_each(pos, head) \
831 for (pos = (head)->next; pos != (head); pos = pos->next)
834 * cds_list_for_each_prev - iterate over a list backwards
835 * @pos: the &struct cds_list_head to use as a loop cursor.
836 * @head: the head for your list.
838 #define cds_list_for_each_prev(pos, head) \
839 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
843 * cds_list_for_each_safe - iterate over a list safe against removal of list entry
844 * @pos: the &struct cds_list_head to use as a loop cursor.
845 * @n: another &struct cds_list_head to use as temporary storage
846 * @head: the head for your list.
848 #define cds_list_for_each_safe(pos, n, head) \
849 for (pos = (head)->next, n = pos->next; pos != (head); \
850 pos = n, n = pos->next)
853 * cds_list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
854 * @pos: the &struct cds_list_head to use as a loop cursor.
855 * @n: another &struct cds_list_head to use as temporary storage
856 * @head: the head for your list.
858 #define cds_list_for_each_prev_safe(pos, n, head) \
859 for (pos = (head)->prev, n = pos->prev; \
860 prefetch(pos->prev), pos != (head); \
861 pos = n, n = pos->prev)
864 * cds_list_for_each_entry - iterate over list of given type
865 * @pos: the type * to use as a loop cursor.
866 * @head: the head for your list.
867 * @member: the name of the list_struct within the struct.
869 #define cds_list_for_each_entry(pos, head, member) \
870 for (pos = cds_list_entry((head)->next, typeof(*pos), member); \
871 prefetch(pos->member.next), &pos->member != (head); \
872 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
875 * cds_list_for_each_entry_reverse - iterate backwards over list of given type.
876 * @pos: the type * to use as a loop cursor.
877 * @head: the head for your list.
878 * @member: the name of the list_struct within the struct.
880 #define cds_list_for_each_entry_reverse(pos, head, member) \
881 for (pos = cds_list_entry((head)->prev, typeof(*pos), member); \
882 prefetch(pos->member.prev), &pos->member != (head); \
883 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
886 * list_prepare_entry - prepare a pos entry for use in cds_list_for_each_entry_continue()
887 * @pos: the type * to use as a start point
888 * @head: the head of the list
889 * @member: the name of the list_struct within the struct.
891 * Prepares a pos entry for use as a start point in cds_list_for_each_entry_continue().
893 #define list_prepare_entry(pos, head, member) \
894 ((pos) ? : cds_list_entry(head, typeof(*pos), member))
897 * cds_list_for_each_entry_continue - continue iteration over list of given type
898 * @pos: the type * to use as a loop cursor.
899 * @head: the head for your list.
900 * @member: the name of the list_struct within the struct.
902 * Continue to iterate over list of given type, continuing after
903 * the current position.
905 #define cds_list_for_each_entry_continue(pos, head, member) \
906 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member); \
907 prefetch(pos->member.next), &pos->member != (head); \
908 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
911 * cds_list_for_each_entry_continue_reverse - iterate backwards from the given point
912 * @pos: the type * to use as a loop cursor.
913 * @head: the head for your list.
914 * @member: the name of the list_struct within the struct.
916 * Start to iterate over list of given type backwards, continuing after
917 * the current position.
919 #define cds_list_for_each_entry_continue_reverse(pos, head, member) \
920 for (pos = cds_list_entry(pos->member.prev, typeof(*pos), member); \
921 prefetch(pos->member.prev), &pos->member != (head); \
922 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
925 * cds_list_for_each_entry_from - iterate over list of given type from the current point
926 * @pos: the type * to use as a loop cursor.
927 * @head: the head for your list.
928 * @member: the name of the list_struct within the struct.
930 * Iterate over list of given type, continuing from current position.
932 #define cds_list_for_each_entry_from(pos, head, member) \
933 for (; prefetch(pos->member.next), &pos->member != (head); \
934 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
937 * cds_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
938 * @pos: the type * to use as a loop cursor.
939 * @n: another type * to use as temporary storage
940 * @head: the head for your list.
941 * @member: the name of the list_struct within the struct.
943 #define cds_list_for_each_entry_safe(pos, n, head, member) \
944 for (pos = cds_list_entry((head)->next, typeof(*pos), member), \
945 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
946 &pos->member != (head); \
947 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
950 * cds_list_for_each_entry_safe_continue
951 * @pos: the type * to use as a loop cursor.
952 * @n: another type * to use as temporary storage
953 * @head: the head for your list.
954 * @member: the name of the list_struct within the struct.
956 * Iterate over list of given type, continuing after current point,
957 * safe against removal of list entry.
959 #define cds_list_for_each_entry_safe_continue(pos, n, head, member) \
960 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member), \
961 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
962 &pos->member != (head); \
963 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
966 * cds_list_for_each_entry_safe_from
967 * @pos: the type * to use as a loop cursor.
968 * @n: another type * to use as temporary storage
969 * @head: the head for your list.
970 * @member: the name of the list_struct within the struct.
972 * Iterate over list of given type from current point, safe against
973 * removal of list entry.
975 #define cds_list_for_each_entry_safe_from(pos, n, head, member) \
976 for (n = cds_list_entry(pos->member.next, typeof(*pos), member); \
977 &pos->member != (head); \
978 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
981 * cds_list_for_each_entry_safe_reverse
982 * @pos: the type * to use as a loop cursor.
983 * @n: another type * to use as temporary storage
984 * @head: the head for your list.
985 * @member: the name of the list_struct within the struct.
987 * Iterate backwards over list of given type, safe against removal
990 #define cds_list_for_each_entry_safe_reverse(pos, n, head, member) \
991 for (pos = cds_list_entry((head)->prev, typeof(*pos), member), \
992 n = cds_list_entry(pos->member.prev, typeof(*pos), member); \
993 &pos->member != (head); \
994 pos = n, n = cds_list_entry(n->member.prev, typeof(*n), member))
999 * Double linked lists with a single pointer list head.
1000 * Mostly useful for hash tables where the two pointer list head is
1002 * You lose the ability to access the tail in O(1).
1005 struct cds_hlist_head
{
1006 struct cds_hlist_node
*first
;
1009 struct cds_hlist_node
{
1010 struct cds_hlist_node
*next
, **pprev
;
1013 #define HLIST_HEAD_INIT { .first = NULL }
1014 #define HLIST_HEAD(name) struct cds_hlist_head name = { .first = NULL }
1015 #define CDS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1016 static inline void INIT_HLIST_NODE(struct cds_hlist_node
*h
)
1022 static inline int hlist_unhashed(const struct cds_hlist_node
*h
)
1027 static inline int hlist_empty(const struct cds_hlist_head
*h
)
1032 static inline void __cds_hlist_del(struct cds_hlist_node
*n
)
1034 struct cds_hlist_node
*next
= n
->next
;
1035 struct cds_hlist_node
**pprev
= n
->pprev
;
1038 next
->pprev
= pprev
;
1041 static inline void cds_hlist_del(struct cds_hlist_node
*n
)
1044 n
->next
= LIST_POISON1
;
1045 n
->pprev
= LIST_POISON2
;
1048 static inline void cds_hlist_del_init(struct cds_hlist_node
*n
)
1050 if (!hlist_unhashed(n
)) {
1056 static inline void cds_hlist_add_head(struct cds_hlist_node
*n
, struct cds_hlist_head
*h
)
1058 struct cds_hlist_node
*first
= h
->first
;
1061 first
->pprev
= &n
->next
;
1063 n
->pprev
= &h
->first
;
1066 /* next must be != NULL */
1067 static inline void hlist_add_before(struct cds_hlist_node
*n
,
1068 struct cds_hlist_node
*next
)
1070 n
->pprev
= next
->pprev
;
1072 next
->pprev
= &n
->next
;
1076 static inline void hlist_add_after(struct cds_hlist_node
*n
,
1077 struct cds_hlist_node
*next
)
1079 next
->next
= n
->next
;
1081 next
->pprev
= &n
->next
;
1084 next
->next
->pprev
= &next
->next
;
1088 * Move a list from one list head to another. Fixup the pprev
1089 * reference of the first entry if it exists.
1091 static inline void hlist_move_list(struct cds_hlist_head
*old
,
1092 struct cds_hlist_head
*new)
1094 new->first
= old
->first
;
1096 new->first
->pprev
= &new->first
;
1100 #define cds_hlist_entry(ptr, type, member) caa_container_of(ptr,type,member)
1102 #define cds_hlist_for_each(pos, head) \
1103 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1106 #define cds_hlist_for_each_safe(pos, n, head) \
1107 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1111 * cds_hlist_for_each_entry - iterate over list of given type
1112 * @tpos: the type * to use as a loop cursor.
1113 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1114 * @head: the head for your list.
1115 * @member: the name of the cds_hlist_node within the struct.
1117 #define cds_hlist_for_each_entry(tpos, pos, head, member) \
1118 for (pos = (head)->first; \
1119 pos && ({ prefetch(pos->next); 1;}) && \
1120 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1124 * cds_hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1125 * @tpos: the type * to use as a loop cursor.
1126 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1127 * @member: the name of the cds_hlist_node within the struct.
1129 #define cds_hlist_for_each_entry_continue(tpos, pos, member) \
1130 for (pos = (pos)->next; \
1131 pos && ({ prefetch(pos->next); 1;}) && \
1132 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1136 * cds_hlist_for_each_entry_from - iterate over a hlist continuing from current point
1137 * @tpos: the type * to use as a loop cursor.
1138 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1139 * @member: the name of the cds_hlist_node within the struct.
1141 #define cds_hlist_for_each_entry_from(tpos, pos, member) \
1142 for (; pos && ({ prefetch(pos->next); 1;}) && \
1143 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1147 * cds_hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1148 * @tpos: the type * to use as a loop cursor.
1149 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1150 * @n: another &struct cds_hlist_node to use as temporary storage
1151 * @head: the head for your list.
1152 * @member: the name of the cds_hlist_node within the struct.
1154 #define cds_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1155 for (pos = (head)->first; \
1156 pos && ({ n = pos->next; 1; }) && \
1157 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \