8 * common.h: Common Linux kernel-isms.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; but version 2 of the License only due
13 * to code included from the Linux kernel.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Copyright (c) 2006 Paul E. McKenney, IBM.
26 * Much code taken from the Linux kernel. For such code, the option
27 * to redistribute under later versions of GPL might not be available.
30 #ifndef __always_inline
31 #define __always_inline inline
34 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
35 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
38 # define stringify_in_c(...) __VA_ARGS__
39 # define ASM_CONST(x) x
41 /* This version of stringify will deal with commas... */
42 # define __stringify_in_c(...) #__VA_ARGS__
43 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
44 # define __ASM_CONST(x) x##UL
45 # define ASM_CONST(x) __ASM_CONST(x)
50 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
52 * This program is free software; you can redistribute it and/or modify
53 * it under the terms of the GNU General Public License as published by
54 * the Free Software Foundation, but version 2 only due to inclusion
55 * of Linux-kernel code.
57 * This program is distributed in the hope that it will be useful,
58 * but WITHOUT ANY WARRANTY; without even the implied warranty of
59 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 * GNU General Public License for more details.
62 * You should have received a copy of the GNU General Public License
63 * along with this program; if not, write to the Free Software
64 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
66 * Copyright (c) 2006 Paul E. McKenney, IBM.
68 * Much code taken from the Linux kernel. For such code, the option
69 * to redistribute under later versions of GPL might not be available.
76 /* #define CAA_CACHE_LINE_SIZE 64 */
77 #define ____cacheline_internodealigned_in_smp \
78 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
81 * api_pthreads.h: API mapping to pthreads environment.
83 * This program is free software; you can redistribute it and/or modify
84 * it under the terms of the GNU General Public License as published by
85 * the Free Software Foundation; either version 2 of the License, or
86 * (at your option) any later version. However, please note that much
87 * of the code in this file derives from the Linux kernel, and that such
88 * code may not be available except under GPLv2.
90 * This program is distributed in the hope that it will be useful,
91 * but WITHOUT ANY WARRANTY; without even the implied warranty of
92 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
93 * GNU General Public License for more details.
95 * You should have received a copy of the GNU General Public License
96 * along with this program; if not, write to the Free Software
97 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
99 * Copyright (c) 2006 Paul E. McKenney, IBM.
106 #include <sys/types.h>
110 #include <sys/param.h>
111 /* #include "atomic.h" */
114 * Default machine parameters.
117 #ifndef CAA_CACHE_LINE_SIZE
118 /* #define CAA_CACHE_LINE_SIZE 128 */
119 #endif /* #ifndef CAA_CACHE_LINE_SIZE */
122 * Exclusive locking primitives.
125 typedef pthread_mutex_t spinlock_t
;
127 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
128 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
130 static void spin_lock_init(spinlock_t
*sp
)
132 if (pthread_mutex_init(sp
, NULL
) != 0) {
133 perror("spin_lock_init:pthread_mutex_init");
138 static void spin_lock(spinlock_t
*sp
)
140 if (pthread_mutex_lock(sp
) != 0) {
141 perror("spin_lock:pthread_mutex_lock");
146 static void spin_unlock(spinlock_t
*sp
)
148 if (pthread_mutex_unlock(sp
) != 0) {
149 perror("spin_unlock:pthread_mutex_unlock");
154 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
155 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
158 * Thread creation/destruction primitives.
161 typedef pthread_t thread_id_t
;
163 #define NR_THREADS 128
165 #define __THREAD_ID_MAP_EMPTY 0
166 #define __THREAD_ID_MAP_WAITING 1
167 thread_id_t __thread_id_map
[NR_THREADS
];
168 spinlock_t __thread_id_map_mutex
;
170 #define for_each_thread(t) \
171 for (t = 0; t < NR_THREADS; t++)
173 #define for_each_running_thread(t) \
174 for (t = 0; t < NR_THREADS; t++) \
175 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
176 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
178 pthread_key_t thread_id_key
;
180 static int __smp_thread_id(void)
183 thread_id_t tid
= pthread_self();
185 for (i
= 0; i
< NR_THREADS
; i
++) {
186 if (__thread_id_map
[i
] == tid
) {
187 long v
= i
+ 1; /* must be non-NULL. */
189 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
190 perror("pthread_setspecific");
196 spin_lock(&__thread_id_map_mutex
);
197 for (i
= 0; i
< NR_THREADS
; i
++) {
198 if (__thread_id_map
[i
] == tid
)
199 spin_unlock(&__thread_id_map_mutex
);
202 spin_unlock(&__thread_id_map_mutex
);
203 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
208 static int smp_thread_id(void)
212 id
= pthread_getspecific(thread_id_key
);
214 return __smp_thread_id();
215 return (long)(id
- 1);
218 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
223 spin_lock(&__thread_id_map_mutex
);
224 for (i
= 0; i
< NR_THREADS
; i
++) {
225 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
228 if (i
>= NR_THREADS
) {
229 spin_unlock(&__thread_id_map_mutex
);
230 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
233 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
234 spin_unlock(&__thread_id_map_mutex
);
235 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
236 perror("create_thread:pthread_create");
239 __thread_id_map
[i
] = tid
;
243 static void *wait_thread(thread_id_t tid
)
248 for (i
= 0; i
< NR_THREADS
; i
++) {
249 if (__thread_id_map
[i
] == tid
)
252 if (i
>= NR_THREADS
){
253 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
257 if (pthread_join(tid
, &vp
) != 0) {
258 perror("wait_thread:pthread_join");
261 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
265 static void wait_all_threads(void)
270 for (i
= 1; i
< NR_THREADS
; i
++) {
271 tid
= __thread_id_map
[i
];
272 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
273 tid
!= __THREAD_ID_MAP_WAITING
)
274 (void)wait_thread(tid
);
278 #ifndef HAVE_CPU_SET_T
279 typedef unsigned long cpu_set_t
;
280 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
281 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
284 static void run_on(int cpu
)
286 #if HAVE_SCHED_SETAFFINITY
291 #if SCHED_SETAFFINITY_ARGS == 2
292 sched_setaffinity(0, &mask
);
294 sched_setaffinity(0, sizeof(mask
), &mask
);
296 #endif /* HAVE_SCHED_SETAFFINITY */
300 * timekeeping -- very crude -- should use MONOTONIC...
303 long long get_microseconds(void)
307 if (gettimeofday(&tv
, NULL
) != 0)
309 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
313 * Per-thread variables.
316 #define DEFINE_PER_THREAD(type, name) \
319 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
320 } __per_thread_##name[NR_THREADS];
321 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
323 #define per_thread(name, thread) __per_thread_##name[thread].v
324 #define __get_thread_var(name) per_thread(name, smp_thread_id())
326 #define init_per_thread(name, v) \
329 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
330 per_thread(name, __i_p_t_i) = v; \
334 * CPU traversal primitives.
339 #endif /* #ifndef NR_CPUS */
341 #define for_each_possible_cpu(cpu) \
342 for (cpu = 0; cpu < NR_CPUS; cpu++)
343 #define for_each_online_cpu(cpu) \
344 for (cpu = 0; cpu < NR_CPUS; cpu++)
350 #define DEFINE_PER_CPU(type, name) \
353 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
354 } __per_cpu_##name[NR_CPUS]
355 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
357 DEFINE_PER_THREAD(int, smp_processor_id
);
359 #define per_cpu(name, thread) __per_cpu_##name[thread].v
360 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
362 #define init_per_cpu(name, v) \
365 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
366 per_cpu(name, __i_p_c_i) = v; \
370 * CPU state checking (crowbarred).
373 #define idle_cpu(cpu) 0
374 #define in_softirq() 1
375 #define hardirq_count() 0
376 #define PREEMPT_SHIFT 0
377 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
378 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
379 #define PREEMPT_BITS 8
380 #define SOFTIRQ_BITS 8
386 struct notifier_block
{
387 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
388 struct notifier_block
*next
;
392 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
393 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
394 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
395 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
396 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
397 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
398 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
399 * not handling interrupts, soon dead */
400 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
403 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
404 * operation in progress
406 #define CPU_TASKS_FROZEN 0x0010
408 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
409 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
410 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
411 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
412 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
413 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
414 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
416 /* Hibernation and suspend events */
417 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
418 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
419 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
420 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
421 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
422 #define PM_POST_RESTORE 0x0006 /* Restore failed */
424 #define NOTIFY_DONE 0x0000 /* Don't care */
425 #define NOTIFY_OK 0x0001 /* Suits me */
426 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
427 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
428 /* Bad/Veto action */
430 * Clean way to return from the notifier and stop further calls.
432 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
438 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
441 * Initialization -- Must be called before calling any primitives.
444 static void smp_init(void)
448 spin_lock_init(&__thread_id_map_mutex
);
449 __thread_id_map
[0] = pthread_self();
450 for (i
= 1; i
< NR_THREADS
; i
++)
451 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
452 init_per_thread(smp_processor_id
, 0);
453 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
454 perror("pthread_key_create");
459 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
461 #ifndef _LINUX_LIST_H
462 #define _LINUX_LIST_H
464 #define LIST_POISON1 ((void *) 0x00100100)
465 #define LIST_POISON2 ((void *) 0x00200200)
469 * Simple doubly linked list implementation.
471 * Some of the internal functions ("__xxx") are useful when
472 * manipulating whole lists rather than single entries, as
473 * sometimes we already know the next/prev entries and we can
474 * generate better code by using them directly rather than
475 * using the generic single-entry routines.
478 struct cds_list_head
{
479 struct cds_list_head
*next
, *prev
;
482 #define CDS_LIST_HEAD_INIT(name) { &(name), &(name) }
484 #define CDS_LIST_HEAD(name) \
485 struct cds_list_head name = CDS_LIST_HEAD_INIT(name)
487 static inline void CDS_INIT_LIST_HEAD(struct cds_list_head
*list
)
494 * Insert a new entry between two known consecutive entries.
496 * This is only for internal list manipulation where we know
497 * the prev/next entries already!
499 #ifndef CONFIG_DEBUG_LIST
500 static inline void __cds_list_add(struct cds_list_head
*new,
501 struct cds_list_head
*prev
,
502 struct cds_list_head
*next
)
510 extern void __cds_list_add(struct cds_list_head
*new,
511 struct cds_list_head
*prev
,
512 struct cds_list_head
*next
);
516 * cds_list_add - add a new entry
517 * @new: new entry to be added
518 * @head: list head to add it after
520 * Insert a new entry after the specified head.
521 * This is good for implementing stacks.
523 static inline void cds_list_add(struct cds_list_head
*new, struct cds_list_head
*head
)
525 __cds_list_add(new, head
, head
->next
);
530 * cds_list_add_tail - add a new entry
531 * @new: new entry to be added
532 * @head: list head to add it before
534 * Insert a new entry before the specified head.
535 * This is useful for implementing queues.
537 static inline void cds_list_add_tail(struct cds_list_head
*new, struct cds_list_head
*head
)
539 __cds_list_add(new, head
->prev
, head
);
543 * Delete a list entry by making the prev/next entries
544 * point to each other.
546 * This is only for internal list manipulation where we know
547 * the prev/next entries already!
549 static inline void __cds_list_del(struct cds_list_head
* prev
, struct cds_list_head
* next
)
556 * cds_list_del - deletes entry from list.
557 * @entry: the element to delete from the list.
558 * Note: cds_list_empty() on entry does not return true after this, the entry is
559 * in an undefined state.
561 #ifndef CONFIG_DEBUG_LIST
562 static inline void cds_list_del(struct cds_list_head
*entry
)
564 __cds_list_del(entry
->prev
, entry
->next
);
565 entry
->next
= LIST_POISON1
;
566 entry
->prev
= LIST_POISON2
;
569 extern void cds_list_del(struct cds_list_head
*entry
);
573 * cds_list_replace - replace old entry by new one
574 * @old : the element to be replaced
575 * @new : the new element to insert
577 * If @old was empty, it will be overwritten.
579 static inline void cds_list_replace(struct cds_list_head
*old
,
580 struct cds_list_head
*new)
582 new->next
= old
->next
;
583 new->next
->prev
= new;
584 new->prev
= old
->prev
;
585 new->prev
->next
= new;
588 static inline void cds_list_replace_init(struct cds_list_head
*old
,
589 struct cds_list_head
*new)
591 cds_list_replace(old
, new);
592 CDS_INIT_LIST_HEAD(old
);
596 * cds_list_del_init - deletes entry from list and reinitialize it.
597 * @entry: the element to delete from the list.
599 static inline void cds_list_del_init(struct cds_list_head
*entry
)
601 __cds_list_del(entry
->prev
, entry
->next
);
602 CDS_INIT_LIST_HEAD(entry
);
606 * cds_list_move - delete from one list and add as another's head
607 * @list: the entry to move
608 * @head: the head that will precede our entry
610 static inline void cds_list_move(struct cds_list_head
*list
, struct cds_list_head
*head
)
612 __cds_list_del(list
->prev
, list
->next
);
613 cds_list_add(list
, head
);
617 * cds_list_move_tail - delete from one list and add as another's tail
618 * @list: the entry to move
619 * @head: the head that will follow our entry
621 static inline void cds_list_move_tail(struct cds_list_head
*list
,
622 struct cds_list_head
*head
)
624 __cds_list_del(list
->prev
, list
->next
);
625 cds_list_add_tail(list
, head
);
629 * list_is_last - tests whether @list is the last entry in list @head
630 * @list: the entry to test
631 * @head: the head of the list
633 static inline int list_is_last(const struct cds_list_head
*list
,
634 const struct cds_list_head
*head
)
636 return list
->next
== head
;
640 * cds_list_empty - tests whether a list is empty
641 * @head: the list to test.
643 static inline int cds_list_empty(const struct cds_list_head
*head
)
645 return head
->next
== head
;
649 * cds_list_empty_careful - tests whether a list is empty and not being modified
650 * @head: the list to test
653 * tests whether a list is empty _and_ checks that no other CPU might be
654 * in the process of modifying either member (next or prev)
656 * NOTE: using cds_list_empty_careful() without synchronization
657 * can only be safe if the only activity that can happen
658 * to the list entry is cds_list_del_init(). Eg. it cannot be used
659 * if another CPU could re-list_add() it.
661 static inline int cds_list_empty_careful(const struct cds_list_head
*head
)
663 struct cds_list_head
*next
= head
->next
;
664 return (next
== head
) && (next
== head
->prev
);
668 * list_is_singular - tests whether a list has just one entry.
669 * @head: the list to test.
671 static inline int list_is_singular(const struct cds_list_head
*head
)
673 return !list_empty(head
) && (head
->next
== head
->prev
);
676 static inline void __list_cut_position(struct cds_list_head
*list
,
677 struct cds_list_head
*head
, struct cds_list_head
*entry
)
679 struct cds_list_head
*new_first
= entry
->next
;
680 list
->next
= head
->next
;
681 list
->next
->prev
= list
;
684 head
->next
= new_first
;
685 new_first
->prev
= head
;
689 * list_cut_position - cut a list into two
690 * @list: a new list to add all removed entries
691 * @head: a list with entries
692 * @entry: an entry within head, could be the head itself
693 * and if so we won't cut the list
695 * This helper moves the initial part of @head, up to and
696 * including @entry, from @head to @list. You should
697 * pass on @entry an element you know is on @head. @list
698 * should be an empty list or a list you do not care about
702 static inline void list_cut_position(struct cds_list_head
*list
,
703 struct cds_list_head
*head
, struct cds_list_head
*entry
)
705 if (cds_list_empty(head
))
707 if (list_is_singular(head
) &&
708 (head
->next
!= entry
&& head
!= entry
))
711 CDS_INIT_LIST_HEAD(list
);
713 __list_cut_position(list
, head
, entry
);
716 static inline void __cds_list_splice(const struct cds_list_head
*list
,
717 struct cds_list_head
*prev
,
718 struct cds_list_head
*next
)
720 struct cds_list_head
*first
= list
->next
;
721 struct cds_list_head
*last
= list
->prev
;
731 * cds_list_splice - join two lists, this is designed for stacks
732 * @list: the new list to add.
733 * @head: the place to add it in the first list.
735 static inline void cds_list_splice(const struct cds_list_head
*list
,
736 struct cds_list_head
*head
)
738 if (!cds_list_empty(list
))
739 __cds_list_splice(list
, head
, head
->next
);
743 * cds_list_splice_tail - join two lists, each list being a queue
744 * @list: the new list to add.
745 * @head: the place to add it in the first list.
747 static inline void cds_list_splice_tail(struct cds_list_head
*list
,
748 struct cds_list_head
*head
)
750 if (!cds_list_empty(list
))
751 __cds_list_splice(list
, head
->prev
, head
);
755 * cds_list_splice_init - join two lists and reinitialise the emptied list.
756 * @list: the new list to add.
757 * @head: the place to add it in the first list.
759 * The list at @list is reinitialised
761 static inline void cds_list_splice_init(struct cds_list_head
*list
,
762 struct cds_list_head
*head
)
764 if (!cds_list_empty(list
)) {
765 __cds_list_splice(list
, head
, head
->next
);
766 CDS_INIT_LIST_HEAD(list
);
771 * cds_list_splice_tail_init - join two lists and reinitialise the emptied list
772 * @list: the new list to add.
773 * @head: the place to add it in the first list.
775 * Each of the lists is a queue.
776 * The list at @list is reinitialised
778 static inline void cds_list_splice_tail_init(struct cds_list_head
*list
,
779 struct cds_list_head
*head
)
781 if (!cds_list_empty(list
)) {
782 __cds_list_splice(list
, head
->prev
, head
);
783 CDS_INIT_LIST_HEAD(list
);
788 * cds_list_entry - get the struct for this entry
789 * @ptr: the &struct cds_list_head pointer.
790 * @type: the type of the struct this is embedded in.
791 * @member: the name of the list_struct within the struct.
793 #define cds_list_entry(ptr, type, member) \
794 caa_container_of(ptr, type, member)
797 * list_first_entry - get the first element from a list
798 * @ptr: the list head to take the element from.
799 * @type: the type of the struct this is embedded in.
800 * @member: the name of the list_struct within the struct.
802 * Note, that list is expected to be not empty.
804 #define list_first_entry(ptr, type, member) \
805 cds_list_entry((ptr)->next, type, member)
808 * cds_list_for_each - iterate over a list
809 * @pos: the &struct cds_list_head to use as a loop cursor.
810 * @head: the head for your list.
812 #define cds_list_for_each(pos, head) \
813 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
817 * __cds_list_for_each - iterate over a list
818 * @pos: the &struct cds_list_head to use as a loop cursor.
819 * @head: the head for your list.
821 * This variant differs from cds_list_for_each() in that it's the
822 * simplest possible list iteration code, no prefetching is done.
823 * Use this for code that knows the list to be very short (empty
824 * or 1 entry) most of the time.
826 #define __cds_list_for_each(pos, head) \
827 for (pos = (head)->next; pos != (head); pos = pos->next)
830 * cds_list_for_each_prev - iterate over a list backwards
831 * @pos: the &struct cds_list_head to use as a loop cursor.
832 * @head: the head for your list.
834 #define cds_list_for_each_prev(pos, head) \
835 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
839 * cds_list_for_each_safe - iterate over a list safe against removal of list entry
840 * @pos: the &struct cds_list_head to use as a loop cursor.
841 * @n: another &struct cds_list_head to use as temporary storage
842 * @head: the head for your list.
844 #define cds_list_for_each_safe(pos, n, head) \
845 for (pos = (head)->next, n = pos->next; pos != (head); \
846 pos = n, n = pos->next)
849 * cds_list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
850 * @pos: the &struct cds_list_head to use as a loop cursor.
851 * @n: another &struct cds_list_head to use as temporary storage
852 * @head: the head for your list.
854 #define cds_list_for_each_prev_safe(pos, n, head) \
855 for (pos = (head)->prev, n = pos->prev; \
856 prefetch(pos->prev), pos != (head); \
857 pos = n, n = pos->prev)
860 * cds_list_for_each_entry - iterate over list of given type
861 * @pos: the type * to use as a loop cursor.
862 * @head: the head for your list.
863 * @member: the name of the list_struct within the struct.
865 #define cds_list_for_each_entry(pos, head, member) \
866 for (pos = cds_list_entry((head)->next, typeof(*pos), member); \
867 prefetch(pos->member.next), &pos->member != (head); \
868 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
871 * cds_list_for_each_entry_reverse - iterate backwards over list of given type.
872 * @pos: the type * to use as a loop cursor.
873 * @head: the head for your list.
874 * @member: the name of the list_struct within the struct.
876 #define cds_list_for_each_entry_reverse(pos, head, member) \
877 for (pos = cds_list_entry((head)->prev, typeof(*pos), member); \
878 prefetch(pos->member.prev), &pos->member != (head); \
879 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
882 * list_prepare_entry - prepare a pos entry for use in cds_list_for_each_entry_continue()
883 * @pos: the type * to use as a start point
884 * @head: the head of the list
885 * @member: the name of the list_struct within the struct.
887 * Prepares a pos entry for use as a start point in cds_list_for_each_entry_continue().
889 #define list_prepare_entry(pos, head, member) \
890 ((pos) ? : cds_list_entry(head, typeof(*pos), member))
893 * cds_list_for_each_entry_continue - continue iteration over list of given type
894 * @pos: the type * to use as a loop cursor.
895 * @head: the head for your list.
896 * @member: the name of the list_struct within the struct.
898 * Continue to iterate over list of given type, continuing after
899 * the current position.
901 #define cds_list_for_each_entry_continue(pos, head, member) \
902 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member); \
903 prefetch(pos->member.next), &pos->member != (head); \
904 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
907 * cds_list_for_each_entry_continue_reverse - iterate backwards from the given point
908 * @pos: the type * to use as a loop cursor.
909 * @head: the head for your list.
910 * @member: the name of the list_struct within the struct.
912 * Start to iterate over list of given type backwards, continuing after
913 * the current position.
915 #define cds_list_for_each_entry_continue_reverse(pos, head, member) \
916 for (pos = cds_list_entry(pos->member.prev, typeof(*pos), member); \
917 prefetch(pos->member.prev), &pos->member != (head); \
918 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
921 * cds_list_for_each_entry_from - iterate over list of given type from the current point
922 * @pos: the type * to use as a loop cursor.
923 * @head: the head for your list.
924 * @member: the name of the list_struct within the struct.
926 * Iterate over list of given type, continuing from current position.
928 #define cds_list_for_each_entry_from(pos, head, member) \
929 for (; prefetch(pos->member.next), &pos->member != (head); \
930 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
933 * cds_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
934 * @pos: the type * to use as a loop cursor.
935 * @n: another type * to use as temporary storage
936 * @head: the head for your list.
937 * @member: the name of the list_struct within the struct.
939 #define cds_list_for_each_entry_safe(pos, n, head, member) \
940 for (pos = cds_list_entry((head)->next, typeof(*pos), member), \
941 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
942 &pos->member != (head); \
943 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
946 * cds_list_for_each_entry_safe_continue
947 * @pos: the type * to use as a loop cursor.
948 * @n: another type * to use as temporary storage
949 * @head: the head for your list.
950 * @member: the name of the list_struct within the struct.
952 * Iterate over list of given type, continuing after current point,
953 * safe against removal of list entry.
955 #define cds_list_for_each_entry_safe_continue(pos, n, head, member) \
956 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member), \
957 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
958 &pos->member != (head); \
959 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
962 * cds_list_for_each_entry_safe_from
963 * @pos: the type * to use as a loop cursor.
964 * @n: another type * to use as temporary storage
965 * @head: the head for your list.
966 * @member: the name of the list_struct within the struct.
968 * Iterate over list of given type from current point, safe against
969 * removal of list entry.
971 #define cds_list_for_each_entry_safe_from(pos, n, head, member) \
972 for (n = cds_list_entry(pos->member.next, typeof(*pos), member); \
973 &pos->member != (head); \
974 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
977 * cds_list_for_each_entry_safe_reverse
978 * @pos: the type * to use as a loop cursor.
979 * @n: another type * to use as temporary storage
980 * @head: the head for your list.
981 * @member: the name of the list_struct within the struct.
983 * Iterate backwards over list of given type, safe against removal
986 #define cds_list_for_each_entry_safe_reverse(pos, n, head, member) \
987 for (pos = cds_list_entry((head)->prev, typeof(*pos), member), \
988 n = cds_list_entry(pos->member.prev, typeof(*pos), member); \
989 &pos->member != (head); \
990 pos = n, n = cds_list_entry(n->member.prev, typeof(*n), member))
995 * Double linked lists with a single pointer list head.
996 * Mostly useful for hash tables where the two pointer list head is
998 * You lose the ability to access the tail in O(1).
1001 struct cds_hlist_head
{
1002 struct cds_hlist_node
*first
;
1005 struct cds_hlist_node
{
1006 struct cds_hlist_node
*next
, **pprev
;
1009 #define HLIST_HEAD_INIT { .first = NULL }
1010 #define HLIST_HEAD(name) struct cds_hlist_head name = { .first = NULL }
1011 #define CDS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1012 static inline void INIT_HLIST_NODE(struct cds_hlist_node
*h
)
1018 static inline int hlist_unhashed(const struct cds_hlist_node
*h
)
1023 static inline int hlist_empty(const struct cds_hlist_head
*h
)
1028 static inline void __cds_hlist_del(struct cds_hlist_node
*n
)
1030 struct cds_hlist_node
*next
= n
->next
;
1031 struct cds_hlist_node
**pprev
= n
->pprev
;
1034 next
->pprev
= pprev
;
1037 static inline void cds_hlist_del(struct cds_hlist_node
*n
)
1040 n
->next
= LIST_POISON1
;
1041 n
->pprev
= LIST_POISON2
;
1044 static inline void cds_hlist_del_init(struct cds_hlist_node
*n
)
1046 if (!hlist_unhashed(n
)) {
1052 static inline void cds_hlist_add_head(struct cds_hlist_node
*n
, struct cds_hlist_head
*h
)
1054 struct cds_hlist_node
*first
= h
->first
;
1057 first
->pprev
= &n
->next
;
1059 n
->pprev
= &h
->first
;
1062 /* next must be != NULL */
1063 static inline void hlist_add_before(struct cds_hlist_node
*n
,
1064 struct cds_hlist_node
*next
)
1066 n
->pprev
= next
->pprev
;
1068 next
->pprev
= &n
->next
;
1072 static inline void hlist_add_after(struct cds_hlist_node
*n
,
1073 struct cds_hlist_node
*next
)
1075 next
->next
= n
->next
;
1077 next
->pprev
= &n
->next
;
1080 next
->next
->pprev
= &next
->next
;
1084 * Move a list from one list head to another. Fixup the pprev
1085 * reference of the first entry if it exists.
1087 static inline void hlist_move_list(struct cds_hlist_head
*old
,
1088 struct cds_hlist_head
*new)
1090 new->first
= old
->first
;
1092 new->first
->pprev
= &new->first
;
1096 #define cds_hlist_entry(ptr, type, member) caa_container_of(ptr,type,member)
1098 #define cds_hlist_for_each(pos, head) \
1099 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1102 #define cds_hlist_for_each_safe(pos, n, head) \
1103 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1107 * cds_hlist_for_each_entry - iterate over list of given type
1108 * @tpos: the type * to use as a loop cursor.
1109 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1110 * @head: the head for your list.
1111 * @member: the name of the cds_hlist_node within the struct.
1113 #define cds_hlist_for_each_entry(tpos, pos, head, member) \
1114 for (pos = (head)->first; \
1115 pos && ({ prefetch(pos->next); 1;}) && \
1116 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1120 * cds_hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1121 * @tpos: the type * to use as a loop cursor.
1122 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1123 * @member: the name of the cds_hlist_node within the struct.
1125 #define cds_hlist_for_each_entry_continue(tpos, pos, member) \
1126 for (pos = (pos)->next; \
1127 pos && ({ prefetch(pos->next); 1;}) && \
1128 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1132 * cds_hlist_for_each_entry_from - iterate over a hlist continuing from current point
1133 * @tpos: the type * to use as a loop cursor.
1134 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1135 * @member: the name of the cds_hlist_node within the struct.
1137 #define cds_hlist_for_each_entry_from(tpos, pos, member) \
1138 for (; pos && ({ prefetch(pos->next); 1;}) && \
1139 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1143 * cds_hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1144 * @tpos: the type * to use as a loop cursor.
1145 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1146 * @n: another &struct cds_hlist_node to use as temporary storage
1147 * @head: the head for your list.
1148 * @member: the name of the cds_hlist_node within the struct.
1150 #define cds_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1151 for (pos = (head)->first; \
1152 pos && ({ n = pos->next; 1; }) && \
1153 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \