tests api: remove atomics
[urcu.git] / tests / api_ppc.h
CommitLineData
6d0ce021
PM
1/* MECHANICALLY GENERATED, DO NOT EDIT!!! */
2
1a43bbd8 3#ifndef _INCLUDE_API_H
6d0ce021
PM
4#define _INCLUDE_API_H
5
6/*
7 * common.h: Common Linux kernel-isms.
8 *
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.
13 *
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.
18 *
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.
22 *
23 * Copyright (c) 2006 Paul E. McKenney, IBM.
24 *
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.
27 */
28
22b63ec4
MD
29#include <urcu/arch.h>
30
6d0ce021
PM
31#ifndef __always_inline
32#define __always_inline inline
33#endif
34
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)
37
38#ifdef __ASSEMBLY__
39# define stringify_in_c(...) __VA_ARGS__
40# define ASM_CONST(x) x
41#else
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)
47#endif
48
49
50/*
51 * arch-ppc64.h: Expose PowerPC atomic instructions.
52 *
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.
57 *
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.
62 *
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.
66 *
67 * Copyright (c) 2006 Paul E. McKenney, IBM.
68 *
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.
71 */
72
73/*
74 * Machine parameters.
75 */
76
6d0ce021
PM
77#define CONFIG_PPC64
78
6d0ce021 79#define ____cacheline_internodealigned_in_smp \
901bbb44 80 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
6ee91d83 81
6d0ce021
PM
82/*
83 * api_pthreads.h: API mapping to pthreads environment.
84 *
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.
91 *
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.
96 *
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.
100 *
101 * Copyright (c) 2006 Paul E. McKenney, IBM.
102 */
103
104#include <stdio.h>
105#include <stdlib.h>
106#include <errno.h>
107#include <limits.h>
108#include <sys/types.h>
109#define __USE_GNU
110#include <pthread.h>
111#include <sched.h>
112#include <sys/param.h>
113/* #include "atomic.h" */
114
6d0ce021
PM
115/*
116 * Default machine parameters.
117 */
118
06f22bdb
DG
119#ifndef CAA_CACHE_LINE_SIZE
120/* #define CAA_CACHE_LINE_SIZE 128 */
121#endif /* #ifndef CAA_CACHE_LINE_SIZE */
6d0ce021
PM
122
123/*
124 * Exclusive locking primitives.
125 */
126
127typedef pthread_mutex_t spinlock_t;
128
129#define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
130#define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
131
132static void spin_lock_init(spinlock_t *sp)
133{
134 if (pthread_mutex_init(sp, NULL) != 0) {
135 perror("spin_lock_init:pthread_mutex_init");
136 exit(-1);
137 }
138}
139
140static void spin_lock(spinlock_t *sp)
141{
142 if (pthread_mutex_lock(sp) != 0) {
143 perror("spin_lock:pthread_mutex_lock");
144 exit(-1);
145 }
146}
147
6d0ce021
PM
148static void spin_unlock(spinlock_t *sp)
149{
150 if (pthread_mutex_unlock(sp) != 0) {
151 perror("spin_unlock:pthread_mutex_unlock");
152 exit(-1);
153 }
154}
155
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)
158
6d0ce021
PM
159/*
160 * Thread creation/destruction primitives.
161 */
162
163typedef pthread_t thread_id_t;
164
165#define NR_THREADS 128
166
167#define __THREAD_ID_MAP_EMPTY 0
168#define __THREAD_ID_MAP_WAITING 1
169thread_id_t __thread_id_map[NR_THREADS];
170spinlock_t __thread_id_map_mutex;
171
172#define for_each_thread(t) \
173 for (t = 0; t < NR_THREADS; t++)
174
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))
179
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))
184
185pthread_key_t thread_id_key;
186
187static int __smp_thread_id(void)
188{
189 int i;
190 thread_id_t tid = pthread_self();
191
192 for (i = 0; i < NR_THREADS; i++) {
193 if (__thread_id_map[i] == tid) {
194 long v = i + 1; /* must be non-NULL. */
195
196 if (pthread_setspecific(thread_id_key, (void *)v) != 0) {
197 perror("pthread_setspecific");
198 exit(-1);
199 }
200 return i;
201 }
202 }
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);
207 return i;
208 }
209 spin_unlock(&__thread_id_map_mutex);
0578089f
PM
210 fprintf(stderr, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
211 (int)tid, (int)tid);
6d0ce021
PM
212 exit(-1);
213}
214
215static int smp_thread_id(void)
216{
217 void *id;
218
219 id = pthread_getspecific(thread_id_key);
220 if (id == NULL)
221 return __smp_thread_id();
222 return (long)(id - 1);
223}
224
225static thread_id_t create_thread(void *(*func)(void *), void *arg)
226{
227 thread_id_t tid;
228 int i;
229
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)
233 break;
234 }
235 if (i >= NR_THREADS) {
236 spin_unlock(&__thread_id_map_mutex);
237 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
238 exit(-1);
239 }
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");
244 exit(-1);
245 }
246 __thread_id_map[i] = tid;
247 return tid;
248}
249
250static void *wait_thread(thread_id_t tid)
251{
252 int i;
253 void *vp;
254
255 for (i = 0; i < NR_THREADS; i++) {
256 if (__thread_id_map[i] == tid)
257 break;
258 }
259 if (i >= NR_THREADS){
0578089f
PM
260 fprintf(stderr, "wait_thread: bad tid = %d(%#x)\n",
261 (int)tid, (int)tid);
6d0ce021
PM
262 exit(-1);
263 }
264 if (pthread_join(tid, &vp) != 0) {
265 perror("wait_thread:pthread_join");
266 exit(-1);
267 }
268 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
269 return vp;
270}
271
272static void wait_all_threads(void)
273{
274 int i;
275 thread_id_t tid;
276
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);
282 }
283}
284
285static void run_on(int cpu)
286{
287 cpu_set_t mask;
288
289 CPU_ZERO(&mask);
290 CPU_SET(cpu, &mask);
291 sched_setaffinity(0, sizeof(mask), &mask);
292}
293
294/*
295 * timekeeping -- very crude -- should use MONOTONIC...
296 */
297
298long long get_microseconds(void)
299{
300 struct timeval tv;
301
302 if (gettimeofday(&tv, NULL) != 0)
303 abort();
304 return ((long long)tv.tv_sec) * 1000000LL + (long long)tv.tv_usec;
305}
306
307/*
308 * Per-thread variables.
309 */
310
311#define DEFINE_PER_THREAD(type, name) \
312 struct { \
313 __typeof__(type) v \
06f22bdb 314 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
6d0ce021
PM
315 } __per_thread_##name[NR_THREADS];
316#define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
317
318#define per_thread(name, thread) __per_thread_##name[thread].v
319#define __get_thread_var(name) per_thread(name, smp_thread_id())
320
321#define init_per_thread(name, v) \
322 do { \
323 int __i_p_t_i; \
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; \
326 } while (0)
327
328/*
329 * CPU traversal primitives.
330 */
331
332#ifndef NR_CPUS
333#define NR_CPUS 16
334#endif /* #ifndef NR_CPUS */
335
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++)
340
341/*
342 * Per-CPU variables.
343 */
344
345#define DEFINE_PER_CPU(type, name) \
346 struct { \
347 __typeof__(type) v \
06f22bdb 348 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
6d0ce021
PM
349 } __per_cpu_##name[NR_CPUS]
350#define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
351
352DEFINE_PER_THREAD(int, smp_processor_id);
353
6d0ce021
PM
354#define per_cpu(name, thread) __per_cpu_##name[thread].v
355#define __get_cpu_var(name) per_cpu(name, smp_processor_id())
356
357#define init_per_cpu(name, v) \
358 do { \
359 int __i_p_c_i; \
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; \
362 } while (0)
363
364/*
365 * CPU state checking (crowbarred).
366 */
367
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
376
377/*
378 * CPU hotplug.
379 */
380
381struct notifier_block {
382 int (*notifier_call)(struct notifier_block *, unsigned long, void *);
383 struct notifier_block *next;
384 int priority;
385};
386
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
396 * lock is dropped */
397
398/* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
399 * operation in progress
400 */
401#define CPU_TASKS_FROZEN 0x0010
402
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)
410
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 */
418
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 */
424/*
425 * Clean way to return from the notifier and stop further calls.
426 */
427#define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
428
429/*
430 * Bug checks.
431 */
432
433#define BUG_ON(c) do { if (!(c)) abort(); } while (0)
434
435/*
436 * Initialization -- Must be called before calling any primitives.
437 */
438
439static void smp_init(void)
440{
441 int i;
442
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");
450 exit(-1);
451 }
452}
453
454/* Taken from the Linux kernel source tree, so GPLv2-only!!! */
455
456#ifndef _LINUX_LIST_H
457#define _LINUX_LIST_H
458
459#define LIST_POISON1 ((void *) 0x00100100)
460#define LIST_POISON2 ((void *) 0x00200200)
461
63ff4873
MD
462#if 0
463
6d0ce021
PM
464/*
465 * Simple doubly linked list implementation.
466 *
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.
472 */
473
16aa9ee8
DG
474struct cds_list_head {
475 struct cds_list_head *next, *prev;
6d0ce021
PM
476};
477
16aa9ee8 478#define CDS_LIST_HEAD_INIT(name) { &(name), &(name) }
6d0ce021 479
16aa9ee8
DG
480#define CDS_LIST_HEAD(name) \
481 struct cds_list_head name = CDS_LIST_HEAD_INIT(name)
6d0ce021 482
16aa9ee8 483static inline void CDS_INIT_LIST_HEAD(struct cds_list_head *list)
6d0ce021
PM
484{
485 list->next = list;
486 list->prev = list;
487}
488
489/*
490 * Insert a new entry between two known consecutive entries.
491 *
492 * This is only for internal list manipulation where we know
493 * the prev/next entries already!
494 */
495#ifndef CONFIG_DEBUG_LIST
16aa9ee8
DG
496static inline void __cds_list_add(struct cds_list_head *new,
497 struct cds_list_head *prev,
498 struct cds_list_head *next)
6d0ce021
PM
499{
500 next->prev = new;
501 new->next = next;
502 new->prev = prev;
503 prev->next = new;
504}
505#else
16aa9ee8
DG
506extern void __cds_list_add(struct cds_list_head *new,
507 struct cds_list_head *prev,
508 struct cds_list_head *next);
6d0ce021
PM
509#endif
510
511/**
16aa9ee8 512 * cds_list_add - add a new entry
6d0ce021
PM
513 * @new: new entry to be added
514 * @head: list head to add it after
515 *
516 * Insert a new entry after the specified head.
517 * This is good for implementing stacks.
518 */
16aa9ee8 519static inline void cds_list_add(struct cds_list_head *new, struct cds_list_head *head)
6d0ce021 520{
16aa9ee8 521 __cds_list_add(new, head, head->next);
6d0ce021
PM
522}
523
524
525/**
16aa9ee8 526 * cds_list_add_tail - add a new entry
6d0ce021
PM
527 * @new: new entry to be added
528 * @head: list head to add it before
529 *
530 * Insert a new entry before the specified head.
531 * This is useful for implementing queues.
532 */
16aa9ee8 533static inline void cds_list_add_tail(struct cds_list_head *new, struct cds_list_head *head)
6d0ce021 534{
16aa9ee8 535 __cds_list_add(new, head->prev, head);
6d0ce021
PM
536}
537
538/*
539 * Delete a list entry by making the prev/next entries
540 * point to each other.
541 *
542 * This is only for internal list manipulation where we know
543 * the prev/next entries already!
544 */
16aa9ee8 545static inline void __cds_list_del(struct cds_list_head * prev, struct cds_list_head * next)
6d0ce021
PM
546{
547 next->prev = prev;
548 prev->next = next;
549}
550
551/**
16aa9ee8 552 * cds_list_del - deletes entry from list.
6d0ce021 553 * @entry: the element to delete from the list.
16aa9ee8 554 * Note: cds_list_empty() on entry does not return true after this, the entry is
6d0ce021
PM
555 * in an undefined state.
556 */
557#ifndef CONFIG_DEBUG_LIST
16aa9ee8 558static inline void cds_list_del(struct cds_list_head *entry)
6d0ce021 559{
16aa9ee8 560 __cds_list_del(entry->prev, entry->next);
6d0ce021
PM
561 entry->next = LIST_POISON1;
562 entry->prev = LIST_POISON2;
563}
564#else
16aa9ee8 565extern void cds_list_del(struct cds_list_head *entry);
6d0ce021
PM
566#endif
567
568/**
16aa9ee8 569 * cds_list_replace - replace old entry by new one
6d0ce021
PM
570 * @old : the element to be replaced
571 * @new : the new element to insert
572 *
573 * If @old was empty, it will be overwritten.
574 */
16aa9ee8
DG
575static inline void cds_list_replace(struct cds_list_head *old,
576 struct cds_list_head *new)
6d0ce021
PM
577{
578 new->next = old->next;
579 new->next->prev = new;
580 new->prev = old->prev;
581 new->prev->next = new;
582}
583
16aa9ee8
DG
584static inline void cds_list_replace_init(struct cds_list_head *old,
585 struct cds_list_head *new)
6d0ce021 586{
16aa9ee8
DG
587 cds_list_replace(old, new);
588 CDS_INIT_LIST_HEAD(old);
6d0ce021
PM
589}
590
591/**
16aa9ee8 592 * cds_list_del_init - deletes entry from list and reinitialize it.
6d0ce021
PM
593 * @entry: the element to delete from the list.
594 */
16aa9ee8 595static inline void cds_list_del_init(struct cds_list_head *entry)
6d0ce021 596{
16aa9ee8
DG
597 __cds_list_del(entry->prev, entry->next);
598 CDS_INIT_LIST_HEAD(entry);
6d0ce021
PM
599}
600
601/**
16aa9ee8 602 * cds_list_move - delete from one list and add as another's head
6d0ce021
PM
603 * @list: the entry to move
604 * @head: the head that will precede our entry
605 */
16aa9ee8 606static inline void cds_list_move(struct cds_list_head *list, struct cds_list_head *head)
6d0ce021 607{
16aa9ee8
DG
608 __cds_list_del(list->prev, list->next);
609 cds_list_add(list, head);
6d0ce021
PM
610}
611
612/**
16aa9ee8 613 * cds_list_move_tail - delete from one list and add as another's tail
6d0ce021
PM
614 * @list: the entry to move
615 * @head: the head that will follow our entry
616 */
16aa9ee8
DG
617static inline void cds_list_move_tail(struct cds_list_head *list,
618 struct cds_list_head *head)
6d0ce021 619{
16aa9ee8
DG
620 __cds_list_del(list->prev, list->next);
621 cds_list_add_tail(list, head);
6d0ce021
PM
622}
623
624/**
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
628 */
16aa9ee8
DG
629static inline int list_is_last(const struct cds_list_head *list,
630 const struct cds_list_head *head)
6d0ce021
PM
631{
632 return list->next == head;
633}
634
635/**
16aa9ee8 636 * cds_list_empty - tests whether a list is empty
6d0ce021
PM
637 * @head: the list to test.
638 */
16aa9ee8 639static inline int cds_list_empty(const struct cds_list_head *head)
6d0ce021
PM
640{
641 return head->next == head;
642}
643
644/**
16aa9ee8 645 * cds_list_empty_careful - tests whether a list is empty and not being modified
6d0ce021
PM
646 * @head: the list to test
647 *
648 * Description:
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)
651 *
16aa9ee8 652 * NOTE: using cds_list_empty_careful() without synchronization
6d0ce021 653 * can only be safe if the only activity that can happen
16aa9ee8 654 * to the list entry is cds_list_del_init(). Eg. it cannot be used
6d0ce021
PM
655 * if another CPU could re-list_add() it.
656 */
16aa9ee8 657static inline int cds_list_empty_careful(const struct cds_list_head *head)
6d0ce021 658{
16aa9ee8 659 struct cds_list_head *next = head->next;
6d0ce021
PM
660 return (next == head) && (next == head->prev);
661}
662
663/**
664 * list_is_singular - tests whether a list has just one entry.
665 * @head: the list to test.
666 */
16aa9ee8 667static inline int list_is_singular(const struct cds_list_head *head)
6d0ce021
PM
668{
669 return !list_empty(head) && (head->next == head->prev);
670}
671
16aa9ee8
DG
672static inline void __list_cut_position(struct cds_list_head *list,
673 struct cds_list_head *head, struct cds_list_head *entry)
6d0ce021 674{
16aa9ee8 675 struct cds_list_head *new_first = entry->next;
6d0ce021
PM
676 list->next = head->next;
677 list->next->prev = list;
678 list->prev = entry;
679 entry->next = list;
680 head->next = new_first;
681 new_first->prev = head;
682}
683
684/**
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
690 *
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
695 * losing its data.
696 *
697 */
16aa9ee8
DG
698static inline void list_cut_position(struct cds_list_head *list,
699 struct cds_list_head *head, struct cds_list_head *entry)
6d0ce021 700{
16aa9ee8 701 if (cds_list_empty(head))
6d0ce021
PM
702 return;
703 if (list_is_singular(head) &&
704 (head->next != entry && head != entry))
705 return;
706 if (entry == head)
16aa9ee8 707 CDS_INIT_LIST_HEAD(list);
6d0ce021
PM
708 else
709 __list_cut_position(list, head, entry);
710}
711
16aa9ee8
DG
712static inline void __cds_list_splice(const struct cds_list_head *list,
713 struct cds_list_head *prev,
714 struct cds_list_head *next)
6d0ce021 715{
16aa9ee8
DG
716 struct cds_list_head *first = list->next;
717 struct cds_list_head *last = list->prev;
6d0ce021
PM
718
719 first->prev = prev;
720 prev->next = first;
721
722 last->next = next;
723 next->prev = last;
724}
725
726/**
16aa9ee8 727 * cds_list_splice - join two lists, this is designed for stacks
6d0ce021
PM
728 * @list: the new list to add.
729 * @head: the place to add it in the first list.
730 */
16aa9ee8
DG
731static inline void cds_list_splice(const struct cds_list_head *list,
732 struct cds_list_head *head)
6d0ce021 733{
16aa9ee8
DG
734 if (!cds_list_empty(list))
735 __cds_list_splice(list, head, head->next);
6d0ce021
PM
736}
737
738/**
16aa9ee8 739 * cds_list_splice_tail - join two lists, each list being a queue
6d0ce021
PM
740 * @list: the new list to add.
741 * @head: the place to add it in the first list.
742 */
16aa9ee8
DG
743static inline void cds_list_splice_tail(struct cds_list_head *list,
744 struct cds_list_head *head)
6d0ce021 745{
16aa9ee8
DG
746 if (!cds_list_empty(list))
747 __cds_list_splice(list, head->prev, head);
6d0ce021
PM
748}
749
750/**
16aa9ee8 751 * cds_list_splice_init - join two lists and reinitialise the emptied list.
6d0ce021
PM
752 * @list: the new list to add.
753 * @head: the place to add it in the first list.
754 *
755 * The list at @list is reinitialised
756 */
16aa9ee8
DG
757static inline void cds_list_splice_init(struct cds_list_head *list,
758 struct cds_list_head *head)
6d0ce021 759{
16aa9ee8
DG
760 if (!cds_list_empty(list)) {
761 __cds_list_splice(list, head, head->next);
762 CDS_INIT_LIST_HEAD(list);
6d0ce021
PM
763 }
764}
765
766/**
16aa9ee8 767 * cds_list_splice_tail_init - join two lists and reinitialise the emptied list
6d0ce021
PM
768 * @list: the new list to add.
769 * @head: the place to add it in the first list.
770 *
771 * Each of the lists is a queue.
772 * The list at @list is reinitialised
773 */
16aa9ee8
DG
774static inline void cds_list_splice_tail_init(struct cds_list_head *list,
775 struct cds_list_head *head)
6d0ce021 776{
16aa9ee8
DG
777 if (!cds_list_empty(list)) {
778 __cds_list_splice(list, head->prev, head);
779 CDS_INIT_LIST_HEAD(list);
6d0ce021
PM
780 }
781}
782
783/**
16aa9ee8
DG
784 * cds_list_entry - get the struct for this entry
785 * @ptr: the &struct cds_list_head pointer.
6d0ce021
PM
786 * @type: the type of the struct this is embedded in.
787 * @member: the name of the list_struct within the struct.
788 */
16aa9ee8 789#define cds_list_entry(ptr, type, member) \
06f22bdb 790 caa_container_of(ptr, type, member)
6d0ce021
PM
791
792/**
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.
797 *
798 * Note, that list is expected to be not empty.
799 */
800#define list_first_entry(ptr, type, member) \
16aa9ee8 801 cds_list_entry((ptr)->next, type, member)
6d0ce021
PM
802
803/**
16aa9ee8
DG
804 * cds_list_for_each - iterate over a list
805 * @pos: the &struct cds_list_head to use as a loop cursor.
6d0ce021
PM
806 * @head: the head for your list.
807 */
16aa9ee8 808#define cds_list_for_each(pos, head) \
6d0ce021
PM
809 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
810 pos = pos->next)
811
812/**
16aa9ee8
DG
813 * __cds_list_for_each - iterate over a list
814 * @pos: the &struct cds_list_head to use as a loop cursor.
6d0ce021
PM
815 * @head: the head for your list.
816 *
16aa9ee8 817 * This variant differs from cds_list_for_each() in that it's the
6d0ce021
PM
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.
821 */
16aa9ee8 822#define __cds_list_for_each(pos, head) \
6d0ce021
PM
823 for (pos = (head)->next; pos != (head); pos = pos->next)
824
825/**
16aa9ee8
DG
826 * cds_list_for_each_prev - iterate over a list backwards
827 * @pos: the &struct cds_list_head to use as a loop cursor.
6d0ce021
PM
828 * @head: the head for your list.
829 */
16aa9ee8 830#define cds_list_for_each_prev(pos, head) \
6d0ce021
PM
831 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
832 pos = pos->prev)
833
834/**
16aa9ee8
DG
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
6d0ce021
PM
838 * @head: the head for your list.
839 */
16aa9ee8 840#define cds_list_for_each_safe(pos, n, head) \
6d0ce021
PM
841 for (pos = (head)->next, n = pos->next; pos != (head); \
842 pos = n, n = pos->next)
843
844/**
16aa9ee8
DG
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
6d0ce021
PM
848 * @head: the head for your list.
849 */
16aa9ee8 850#define cds_list_for_each_prev_safe(pos, n, head) \
6d0ce021
PM
851 for (pos = (head)->prev, n = pos->prev; \
852 prefetch(pos->prev), pos != (head); \
853 pos = n, n = pos->prev)
854
855/**
16aa9ee8 856 * cds_list_for_each_entry - iterate over list of given type
6d0ce021
PM
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.
860 */
16aa9ee8
DG
861#define cds_list_for_each_entry(pos, head, member) \
862 for (pos = cds_list_entry((head)->next, typeof(*pos), member); \
6d0ce021 863 prefetch(pos->member.next), &pos->member != (head); \
16aa9ee8 864 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
6d0ce021
PM
865
866/**
16aa9ee8 867 * cds_list_for_each_entry_reverse - iterate backwards over list of given type.
6d0ce021
PM
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.
871 */
16aa9ee8
DG
872#define cds_list_for_each_entry_reverse(pos, head, member) \
873 for (pos = cds_list_entry((head)->prev, typeof(*pos), member); \
6d0ce021 874 prefetch(pos->member.prev), &pos->member != (head); \
16aa9ee8 875 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
6d0ce021
PM
876
877/**
16aa9ee8 878 * list_prepare_entry - prepare a pos entry for use in cds_list_for_each_entry_continue()
6d0ce021
PM
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.
882 *
16aa9ee8 883 * Prepares a pos entry for use as a start point in cds_list_for_each_entry_continue().
6d0ce021
PM
884 */
885#define list_prepare_entry(pos, head, member) \
16aa9ee8 886 ((pos) ? : cds_list_entry(head, typeof(*pos), member))
6d0ce021
PM
887
888/**
16aa9ee8 889 * cds_list_for_each_entry_continue - continue iteration over list of given type
6d0ce021
PM
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.
893 *
894 * Continue to iterate over list of given type, continuing after
895 * the current position.
896 */
16aa9ee8
DG
897#define cds_list_for_each_entry_continue(pos, head, member) \
898 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member); \
6d0ce021 899 prefetch(pos->member.next), &pos->member != (head); \
16aa9ee8 900 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
6d0ce021
PM
901
902/**
16aa9ee8 903 * cds_list_for_each_entry_continue_reverse - iterate backwards from the given point
6d0ce021
PM
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.
907 *
908 * Start to iterate over list of given type backwards, continuing after
909 * the current position.
910 */
16aa9ee8
DG
911#define cds_list_for_each_entry_continue_reverse(pos, head, member) \
912 for (pos = cds_list_entry(pos->member.prev, typeof(*pos), member); \
6d0ce021 913 prefetch(pos->member.prev), &pos->member != (head); \
16aa9ee8 914 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
6d0ce021
PM
915
916/**
16aa9ee8 917 * cds_list_for_each_entry_from - iterate over list of given type from the current point
6d0ce021
PM
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.
921 *
922 * Iterate over list of given type, continuing from current position.
923 */
16aa9ee8 924#define cds_list_for_each_entry_from(pos, head, member) \
6d0ce021 925 for (; prefetch(pos->member.next), &pos->member != (head); \
16aa9ee8 926 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
6d0ce021
PM
927
928/**
16aa9ee8 929 * cds_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
6d0ce021
PM
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.
934 */
16aa9ee8
DG
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); \
6d0ce021 938 &pos->member != (head); \
16aa9ee8 939 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
6d0ce021
PM
940
941/**
16aa9ee8 942 * cds_list_for_each_entry_safe_continue
6d0ce021
PM
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.
947 *
948 * Iterate over list of given type, continuing after current point,
949 * safe against removal of list entry.
950 */
16aa9ee8
DG
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); \
6d0ce021 954 &pos->member != (head); \
16aa9ee8 955 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
6d0ce021
PM
956
957/**
16aa9ee8 958 * cds_list_for_each_entry_safe_from
6d0ce021
PM
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.
963 *
964 * Iterate over list of given type from current point, safe against
965 * removal of list entry.
966 */
16aa9ee8
DG
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); \
6d0ce021 969 &pos->member != (head); \
16aa9ee8 970 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
6d0ce021
PM
971
972/**
16aa9ee8 973 * cds_list_for_each_entry_safe_reverse
6d0ce021
PM
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.
978 *
979 * Iterate backwards over list of given type, safe against removal
980 * of list entry.
981 */
16aa9ee8
DG
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); \
6d0ce021 985 &pos->member != (head); \
16aa9ee8 986 pos = n, n = cds_list_entry(n->member.prev, typeof(*n), member))
6d0ce021 987
63ff4873
MD
988#endif //0
989
6d0ce021
PM
990/*
991 * Double linked lists with a single pointer list head.
992 * Mostly useful for hash tables where the two pointer list head is
993 * too wasteful.
994 * You lose the ability to access the tail in O(1).
995 */
996
16aa9ee8
DG
997struct cds_hlist_head {
998 struct cds_hlist_node *first;
6d0ce021
PM
999};
1000
16aa9ee8
DG
1001struct cds_hlist_node {
1002 struct cds_hlist_node *next, **pprev;
6d0ce021
PM
1003};
1004
1005#define HLIST_HEAD_INIT { .first = NULL }
16aa9ee8
DG
1006#define HLIST_HEAD(name) struct cds_hlist_head name = { .first = NULL }
1007#define CDS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1008static inline void INIT_HLIST_NODE(struct cds_hlist_node *h)
6d0ce021
PM
1009{
1010 h->next = NULL;
1011 h->pprev = NULL;
1012}
1013
16aa9ee8 1014static inline int hlist_unhashed(const struct cds_hlist_node *h)
6d0ce021
PM
1015{
1016 return !h->pprev;
1017}
1018
16aa9ee8 1019static inline int hlist_empty(const struct cds_hlist_head *h)
6d0ce021
PM
1020{
1021 return !h->first;
1022}
1023
16aa9ee8 1024static inline void __cds_hlist_del(struct cds_hlist_node *n)
6d0ce021 1025{
16aa9ee8
DG
1026 struct cds_hlist_node *next = n->next;
1027 struct cds_hlist_node **pprev = n->pprev;
6d0ce021
PM
1028 *pprev = next;
1029 if (next)
1030 next->pprev = pprev;
1031}
1032
16aa9ee8 1033static inline void cds_hlist_del(struct cds_hlist_node *n)
6d0ce021 1034{
16aa9ee8 1035 __cds_hlist_del(n);
6d0ce021
PM
1036 n->next = LIST_POISON1;
1037 n->pprev = LIST_POISON2;
1038}
1039
16aa9ee8 1040static inline void cds_hlist_del_init(struct cds_hlist_node *n)
6d0ce021
PM
1041{
1042 if (!hlist_unhashed(n)) {
16aa9ee8 1043 __cds_hlist_del(n);
6d0ce021
PM
1044 INIT_HLIST_NODE(n);
1045 }
1046}
1047
16aa9ee8 1048static inline void cds_hlist_add_head(struct cds_hlist_node *n, struct cds_hlist_head *h)
6d0ce021 1049{
16aa9ee8 1050 struct cds_hlist_node *first = h->first;
6d0ce021
PM
1051 n->next = first;
1052 if (first)
1053 first->pprev = &n->next;
1054 h->first = n;
1055 n->pprev = &h->first;
1056}
1057
1058/* next must be != NULL */
16aa9ee8
DG
1059static inline void hlist_add_before(struct cds_hlist_node *n,
1060 struct cds_hlist_node *next)
6d0ce021
PM
1061{
1062 n->pprev = next->pprev;
1063 n->next = next;
1064 next->pprev = &n->next;
1065 *(n->pprev) = n;
1066}
1067
16aa9ee8
DG
1068static inline void hlist_add_after(struct cds_hlist_node *n,
1069 struct cds_hlist_node *next)
6d0ce021
PM
1070{
1071 next->next = n->next;
1072 n->next = next;
1073 next->pprev = &n->next;
1074
1075 if(next->next)
1076 next->next->pprev = &next->next;
1077}
1078
1079/*
1080 * Move a list from one list head to another. Fixup the pprev
1081 * reference of the first entry if it exists.
1082 */
16aa9ee8
DG
1083static inline void hlist_move_list(struct cds_hlist_head *old,
1084 struct cds_hlist_head *new)
6d0ce021
PM
1085{
1086 new->first = old->first;
1087 if (new->first)
1088 new->first->pprev = &new->first;
1089 old->first = NULL;
1090}
1091
16aa9ee8 1092#define cds_hlist_entry(ptr, type, member) caa_container_of(ptr,type,member)
6d0ce021 1093
16aa9ee8 1094#define cds_hlist_for_each(pos, head) \
6d0ce021
PM
1095 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1096 pos = pos->next)
1097
16aa9ee8 1098#define cds_hlist_for_each_safe(pos, n, head) \
6d0ce021
PM
1099 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1100 pos = n)
1101
1102/**
16aa9ee8 1103 * cds_hlist_for_each_entry - iterate over list of given type
6d0ce021 1104 * @tpos: the type * to use as a loop cursor.
16aa9ee8 1105 * @pos: the &struct cds_hlist_node to use as a loop cursor.
6d0ce021 1106 * @head: the head for your list.
16aa9ee8 1107 * @member: the name of the cds_hlist_node within the struct.
6d0ce021 1108 */
16aa9ee8 1109#define cds_hlist_for_each_entry(tpos, pos, head, member) \
6d0ce021
PM
1110 for (pos = (head)->first; \
1111 pos && ({ prefetch(pos->next); 1;}) && \
16aa9ee8 1112 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
6d0ce021
PM
1113 pos = pos->next)
1114
1115/**
16aa9ee8 1116 * cds_hlist_for_each_entry_continue - iterate over a hlist continuing after current point
6d0ce021 1117 * @tpos: the type * to use as a loop cursor.
16aa9ee8
DG
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.
6d0ce021 1120 */
16aa9ee8 1121#define cds_hlist_for_each_entry_continue(tpos, pos, member) \
6d0ce021
PM
1122 for (pos = (pos)->next; \
1123 pos && ({ prefetch(pos->next); 1;}) && \
16aa9ee8 1124 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
6d0ce021
PM
1125 pos = pos->next)
1126
1127/**
16aa9ee8 1128 * cds_hlist_for_each_entry_from - iterate over a hlist continuing from current point
6d0ce021 1129 * @tpos: the type * to use as a loop cursor.
16aa9ee8
DG
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.
6d0ce021 1132 */
16aa9ee8 1133#define cds_hlist_for_each_entry_from(tpos, pos, member) \
6d0ce021 1134 for (; pos && ({ prefetch(pos->next); 1;}) && \
16aa9ee8 1135 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
6d0ce021
PM
1136 pos = pos->next)
1137
1138/**
16aa9ee8 1139 * cds_hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
6d0ce021 1140 * @tpos: the type * to use as a loop cursor.
16aa9ee8
DG
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
6d0ce021 1143 * @head: the head for your list.
16aa9ee8 1144 * @member: the name of the cds_hlist_node within the struct.
6d0ce021 1145 */
16aa9ee8 1146#define cds_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
6d0ce021
PM
1147 for (pos = (head)->first; \
1148 pos && ({ n = pos->next; 1; }) && \
16aa9ee8 1149 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
6d0ce021
PM
1150 pos = n)
1151
1152#endif
1a43bbd8
MD
1153
1154#endif
This page took 0.074261 seconds and 4 git commands to generate.