api: make api_gcc.h a superset of the other headers
[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 77#define ____cacheline_internodealigned_in_smp \
901bbb44 78 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
6ee91d83 79
6d0ce021
PM
80/*
81 * api_pthreads.h: API mapping to pthreads environment.
82 *
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.
89 *
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.
94 *
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.
98 *
99 * Copyright (c) 2006 Paul E. McKenney, IBM.
100 */
101
102#include <stdio.h>
103#include <stdlib.h>
104#include <errno.h>
105#include <limits.h>
106#include <sys/types.h>
107#define __USE_GNU
108#include <pthread.h>
109#include <sched.h>
110#include <sys/param.h>
111/* #include "atomic.h" */
112
6d0ce021
PM
113/*
114 * Default machine parameters.
115 */
116
06f22bdb
DG
117#ifndef CAA_CACHE_LINE_SIZE
118/* #define CAA_CACHE_LINE_SIZE 128 */
119#endif /* #ifndef CAA_CACHE_LINE_SIZE */
6d0ce021
PM
120
121/*
122 * Exclusive locking primitives.
123 */
124
125typedef pthread_mutex_t spinlock_t;
126
127#define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
128#define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
129
130static void spin_lock_init(spinlock_t *sp)
131{
132 if (pthread_mutex_init(sp, NULL) != 0) {
133 perror("spin_lock_init:pthread_mutex_init");
134 exit(-1);
135 }
136}
137
138static void spin_lock(spinlock_t *sp)
139{
140 if (pthread_mutex_lock(sp) != 0) {
141 perror("spin_lock:pthread_mutex_lock");
142 exit(-1);
143 }
144}
145
6d0ce021
PM
146static void spin_unlock(spinlock_t *sp)
147{
148 if (pthread_mutex_unlock(sp) != 0) {
149 perror("spin_unlock:pthread_mutex_unlock");
150 exit(-1);
151 }
152}
153
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)
156
6d0ce021
PM
157/*
158 * Thread creation/destruction primitives.
159 */
160
161typedef pthread_t thread_id_t;
162
163#define NR_THREADS 128
164
165#define __THREAD_ID_MAP_EMPTY 0
166#define __THREAD_ID_MAP_WAITING 1
167thread_id_t __thread_id_map[NR_THREADS];
168spinlock_t __thread_id_map_mutex;
169
170#define for_each_thread(t) \
171 for (t = 0; t < NR_THREADS; t++)
172
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))
177
178#define for_each_tid(t, tid) \
179 for (t = 0; t < NR_THREADS; t++) \
180 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
181 ((tid) != __THREAD_ID_MAP_WAITING))
182
183pthread_key_t thread_id_key;
184
185static int __smp_thread_id(void)
186{
187 int i;
188 thread_id_t tid = pthread_self();
189
190 for (i = 0; i < NR_THREADS; i++) {
191 if (__thread_id_map[i] == tid) {
192 long v = i + 1; /* must be non-NULL. */
193
194 if (pthread_setspecific(thread_id_key, (void *)v) != 0) {
195 perror("pthread_setspecific");
196 exit(-1);
197 }
198 return i;
199 }
200 }
201 spin_lock(&__thread_id_map_mutex);
202 for (i = 0; i < NR_THREADS; i++) {
203 if (__thread_id_map[i] == tid)
204 spin_unlock(&__thread_id_map_mutex);
205 return i;
206 }
207 spin_unlock(&__thread_id_map_mutex);
0578089f
PM
208 fprintf(stderr, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
209 (int)tid, (int)tid);
6d0ce021
PM
210 exit(-1);
211}
212
213static int smp_thread_id(void)
214{
215 void *id;
216
217 id = pthread_getspecific(thread_id_key);
218 if (id == NULL)
219 return __smp_thread_id();
220 return (long)(id - 1);
221}
222
223static thread_id_t create_thread(void *(*func)(void *), void *arg)
224{
225 thread_id_t tid;
226 int i;
227
228 spin_lock(&__thread_id_map_mutex);
229 for (i = 0; i < NR_THREADS; i++) {
230 if (__thread_id_map[i] == __THREAD_ID_MAP_EMPTY)
231 break;
232 }
233 if (i >= NR_THREADS) {
234 spin_unlock(&__thread_id_map_mutex);
235 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
236 exit(-1);
237 }
238 __thread_id_map[i] = __THREAD_ID_MAP_WAITING;
239 spin_unlock(&__thread_id_map_mutex);
240 if (pthread_create(&tid, NULL, func, arg) != 0) {
241 perror("create_thread:pthread_create");
242 exit(-1);
243 }
244 __thread_id_map[i] = tid;
245 return tid;
246}
247
248static void *wait_thread(thread_id_t tid)
249{
250 int i;
251 void *vp;
252
253 for (i = 0; i < NR_THREADS; i++) {
254 if (__thread_id_map[i] == tid)
255 break;
256 }
257 if (i >= NR_THREADS){
0578089f
PM
258 fprintf(stderr, "wait_thread: bad tid = %d(%#x)\n",
259 (int)tid, (int)tid);
6d0ce021
PM
260 exit(-1);
261 }
262 if (pthread_join(tid, &vp) != 0) {
263 perror("wait_thread:pthread_join");
264 exit(-1);
265 }
266 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
267 return vp;
268}
269
270static void wait_all_threads(void)
271{
272 int i;
273 thread_id_t tid;
274
275 for (i = 1; i < NR_THREADS; i++) {
276 tid = __thread_id_map[i];
277 if (tid != __THREAD_ID_MAP_EMPTY &&
278 tid != __THREAD_ID_MAP_WAITING)
279 (void)wait_thread(tid);
280 }
281}
282
283static void run_on(int cpu)
284{
285 cpu_set_t mask;
286
287 CPU_ZERO(&mask);
288 CPU_SET(cpu, &mask);
289 sched_setaffinity(0, sizeof(mask), &mask);
290}
291
292/*
293 * timekeeping -- very crude -- should use MONOTONIC...
294 */
295
296long long get_microseconds(void)
297{
298 struct timeval tv;
299
300 if (gettimeofday(&tv, NULL) != 0)
301 abort();
302 return ((long long)tv.tv_sec) * 1000000LL + (long long)tv.tv_usec;
303}
304
305/*
306 * Per-thread variables.
307 */
308
309#define DEFINE_PER_THREAD(type, name) \
310 struct { \
311 __typeof__(type) v \
06f22bdb 312 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
6d0ce021
PM
313 } __per_thread_##name[NR_THREADS];
314#define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
315
316#define per_thread(name, thread) __per_thread_##name[thread].v
317#define __get_thread_var(name) per_thread(name, smp_thread_id())
318
319#define init_per_thread(name, v) \
320 do { \
321 int __i_p_t_i; \
322 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
323 per_thread(name, __i_p_t_i) = v; \
324 } while (0)
325
326/*
327 * CPU traversal primitives.
328 */
329
330#ifndef NR_CPUS
331#define NR_CPUS 16
332#endif /* #ifndef NR_CPUS */
333
334#define for_each_possible_cpu(cpu) \
335 for (cpu = 0; cpu < NR_CPUS; cpu++)
336#define for_each_online_cpu(cpu) \
337 for (cpu = 0; cpu < NR_CPUS; cpu++)
338
339/*
340 * Per-CPU variables.
341 */
342
343#define DEFINE_PER_CPU(type, name) \
344 struct { \
345 __typeof__(type) v \
06f22bdb 346 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
6d0ce021
PM
347 } __per_cpu_##name[NR_CPUS]
348#define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
349
350DEFINE_PER_THREAD(int, smp_processor_id);
351
6d0ce021
PM
352#define per_cpu(name, thread) __per_cpu_##name[thread].v
353#define __get_cpu_var(name) per_cpu(name, smp_processor_id())
354
355#define init_per_cpu(name, v) \
356 do { \
357 int __i_p_c_i; \
358 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
359 per_cpu(name, __i_p_c_i) = v; \
360 } while (0)
361
362/*
363 * CPU state checking (crowbarred).
364 */
365
366#define idle_cpu(cpu) 0
367#define in_softirq() 1
368#define hardirq_count() 0
369#define PREEMPT_SHIFT 0
370#define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
371#define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
372#define PREEMPT_BITS 8
373#define SOFTIRQ_BITS 8
374
375/*
376 * CPU hotplug.
377 */
378
379struct notifier_block {
380 int (*notifier_call)(struct notifier_block *, unsigned long, void *);
381 struct notifier_block *next;
382 int priority;
383};
384
385#define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
386#define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
387#define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
388#define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
389#define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
390#define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
391#define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
392 * not handling interrupts, soon dead */
393#define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
394 * lock is dropped */
395
396/* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
397 * operation in progress
398 */
399#define CPU_TASKS_FROZEN 0x0010
400
401#define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
402#define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
403#define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
404#define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
405#define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
406#define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
407#define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
408
409/* Hibernation and suspend events */
410#define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
411#define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
412#define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
413#define PM_POST_SUSPEND 0x0004 /* Suspend finished */
414#define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
415#define PM_POST_RESTORE 0x0006 /* Restore failed */
416
417#define NOTIFY_DONE 0x0000 /* Don't care */
418#define NOTIFY_OK 0x0001 /* Suits me */
419#define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
420#define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
421 /* Bad/Veto action */
422/*
423 * Clean way to return from the notifier and stop further calls.
424 */
425#define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
426
427/*
428 * Bug checks.
429 */
430
431#define BUG_ON(c) do { if (!(c)) abort(); } while (0)
432
433/*
434 * Initialization -- Must be called before calling any primitives.
435 */
436
437static void smp_init(void)
438{
439 int i;
440
441 spin_lock_init(&__thread_id_map_mutex);
442 __thread_id_map[0] = pthread_self();
443 for (i = 1; i < NR_THREADS; i++)
444 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
445 init_per_thread(smp_processor_id, 0);
446 if (pthread_key_create(&thread_id_key, NULL) != 0) {
447 perror("pthread_key_create");
448 exit(-1);
449 }
450}
451
452/* Taken from the Linux kernel source tree, so GPLv2-only!!! */
453
454#ifndef _LINUX_LIST_H
455#define _LINUX_LIST_H
456
457#define LIST_POISON1 ((void *) 0x00100100)
458#define LIST_POISON2 ((void *) 0x00200200)
459
63ff4873
MD
460#if 0
461
6d0ce021
PM
462/*
463 * Simple doubly linked list implementation.
464 *
465 * Some of the internal functions ("__xxx") are useful when
466 * manipulating whole lists rather than single entries, as
467 * sometimes we already know the next/prev entries and we can
468 * generate better code by using them directly rather than
469 * using the generic single-entry routines.
470 */
471
16aa9ee8
DG
472struct cds_list_head {
473 struct cds_list_head *next, *prev;
6d0ce021
PM
474};
475
16aa9ee8 476#define CDS_LIST_HEAD_INIT(name) { &(name), &(name) }
6d0ce021 477
16aa9ee8
DG
478#define CDS_LIST_HEAD(name) \
479 struct cds_list_head name = CDS_LIST_HEAD_INIT(name)
6d0ce021 480
16aa9ee8 481static inline void CDS_INIT_LIST_HEAD(struct cds_list_head *list)
6d0ce021
PM
482{
483 list->next = list;
484 list->prev = list;
485}
486
487/*
488 * Insert a new entry between two known consecutive entries.
489 *
490 * This is only for internal list manipulation where we know
491 * the prev/next entries already!
492 */
493#ifndef CONFIG_DEBUG_LIST
16aa9ee8
DG
494static inline void __cds_list_add(struct cds_list_head *new,
495 struct cds_list_head *prev,
496 struct cds_list_head *next)
6d0ce021
PM
497{
498 next->prev = new;
499 new->next = next;
500 new->prev = prev;
501 prev->next = new;
502}
503#else
16aa9ee8
DG
504extern void __cds_list_add(struct cds_list_head *new,
505 struct cds_list_head *prev,
506 struct cds_list_head *next);
6d0ce021
PM
507#endif
508
509/**
16aa9ee8 510 * cds_list_add - add a new entry
6d0ce021
PM
511 * @new: new entry to be added
512 * @head: list head to add it after
513 *
514 * Insert a new entry after the specified head.
515 * This is good for implementing stacks.
516 */
16aa9ee8 517static inline void cds_list_add(struct cds_list_head *new, struct cds_list_head *head)
6d0ce021 518{
16aa9ee8 519 __cds_list_add(new, head, head->next);
6d0ce021
PM
520}
521
522
523/**
16aa9ee8 524 * cds_list_add_tail - add a new entry
6d0ce021
PM
525 * @new: new entry to be added
526 * @head: list head to add it before
527 *
528 * Insert a new entry before the specified head.
529 * This is useful for implementing queues.
530 */
16aa9ee8 531static inline void cds_list_add_tail(struct cds_list_head *new, struct cds_list_head *head)
6d0ce021 532{
16aa9ee8 533 __cds_list_add(new, head->prev, head);
6d0ce021
PM
534}
535
536/*
537 * Delete a list entry by making the prev/next entries
538 * point to each other.
539 *
540 * This is only for internal list manipulation where we know
541 * the prev/next entries already!
542 */
16aa9ee8 543static inline void __cds_list_del(struct cds_list_head * prev, struct cds_list_head * next)
6d0ce021
PM
544{
545 next->prev = prev;
546 prev->next = next;
547}
548
549/**
16aa9ee8 550 * cds_list_del - deletes entry from list.
6d0ce021 551 * @entry: the element to delete from the list.
16aa9ee8 552 * Note: cds_list_empty() on entry does not return true after this, the entry is
6d0ce021
PM
553 * in an undefined state.
554 */
555#ifndef CONFIG_DEBUG_LIST
16aa9ee8 556static inline void cds_list_del(struct cds_list_head *entry)
6d0ce021 557{
16aa9ee8 558 __cds_list_del(entry->prev, entry->next);
6d0ce021
PM
559 entry->next = LIST_POISON1;
560 entry->prev = LIST_POISON2;
561}
562#else
16aa9ee8 563extern void cds_list_del(struct cds_list_head *entry);
6d0ce021
PM
564#endif
565
566/**
16aa9ee8 567 * cds_list_replace - replace old entry by new one
6d0ce021
PM
568 * @old : the element to be replaced
569 * @new : the new element to insert
570 *
571 * If @old was empty, it will be overwritten.
572 */
16aa9ee8
DG
573static inline void cds_list_replace(struct cds_list_head *old,
574 struct cds_list_head *new)
6d0ce021
PM
575{
576 new->next = old->next;
577 new->next->prev = new;
578 new->prev = old->prev;
579 new->prev->next = new;
580}
581
16aa9ee8
DG
582static inline void cds_list_replace_init(struct cds_list_head *old,
583 struct cds_list_head *new)
6d0ce021 584{
16aa9ee8
DG
585 cds_list_replace(old, new);
586 CDS_INIT_LIST_HEAD(old);
6d0ce021
PM
587}
588
589/**
16aa9ee8 590 * cds_list_del_init - deletes entry from list and reinitialize it.
6d0ce021
PM
591 * @entry: the element to delete from the list.
592 */
16aa9ee8 593static inline void cds_list_del_init(struct cds_list_head *entry)
6d0ce021 594{
16aa9ee8
DG
595 __cds_list_del(entry->prev, entry->next);
596 CDS_INIT_LIST_HEAD(entry);
6d0ce021
PM
597}
598
599/**
16aa9ee8 600 * cds_list_move - delete from one list and add as another's head
6d0ce021
PM
601 * @list: the entry to move
602 * @head: the head that will precede our entry
603 */
16aa9ee8 604static inline void cds_list_move(struct cds_list_head *list, struct cds_list_head *head)
6d0ce021 605{
16aa9ee8
DG
606 __cds_list_del(list->prev, list->next);
607 cds_list_add(list, head);
6d0ce021
PM
608}
609
610/**
16aa9ee8 611 * cds_list_move_tail - delete from one list and add as another's tail
6d0ce021
PM
612 * @list: the entry to move
613 * @head: the head that will follow our entry
614 */
16aa9ee8
DG
615static inline void cds_list_move_tail(struct cds_list_head *list,
616 struct cds_list_head *head)
6d0ce021 617{
16aa9ee8
DG
618 __cds_list_del(list->prev, list->next);
619 cds_list_add_tail(list, head);
6d0ce021
PM
620}
621
622/**
623 * list_is_last - tests whether @list is the last entry in list @head
624 * @list: the entry to test
625 * @head: the head of the list
626 */
16aa9ee8
DG
627static inline int list_is_last(const struct cds_list_head *list,
628 const struct cds_list_head *head)
6d0ce021
PM
629{
630 return list->next == head;
631}
632
633/**
16aa9ee8 634 * cds_list_empty - tests whether a list is empty
6d0ce021
PM
635 * @head: the list to test.
636 */
16aa9ee8 637static inline int cds_list_empty(const struct cds_list_head *head)
6d0ce021
PM
638{
639 return head->next == head;
640}
641
642/**
16aa9ee8 643 * cds_list_empty_careful - tests whether a list is empty and not being modified
6d0ce021
PM
644 * @head: the list to test
645 *
646 * Description:
647 * tests whether a list is empty _and_ checks that no other CPU might be
648 * in the process of modifying either member (next or prev)
649 *
16aa9ee8 650 * NOTE: using cds_list_empty_careful() without synchronization
6d0ce021 651 * can only be safe if the only activity that can happen
16aa9ee8 652 * to the list entry is cds_list_del_init(). Eg. it cannot be used
6d0ce021
PM
653 * if another CPU could re-list_add() it.
654 */
16aa9ee8 655static inline int cds_list_empty_careful(const struct cds_list_head *head)
6d0ce021 656{
16aa9ee8 657 struct cds_list_head *next = head->next;
6d0ce021
PM
658 return (next == head) && (next == head->prev);
659}
660
661/**
662 * list_is_singular - tests whether a list has just one entry.
663 * @head: the list to test.
664 */
16aa9ee8 665static inline int list_is_singular(const struct cds_list_head *head)
6d0ce021
PM
666{
667 return !list_empty(head) && (head->next == head->prev);
668}
669
16aa9ee8
DG
670static inline void __list_cut_position(struct cds_list_head *list,
671 struct cds_list_head *head, struct cds_list_head *entry)
6d0ce021 672{
16aa9ee8 673 struct cds_list_head *new_first = entry->next;
6d0ce021
PM
674 list->next = head->next;
675 list->next->prev = list;
676 list->prev = entry;
677 entry->next = list;
678 head->next = new_first;
679 new_first->prev = head;
680}
681
682/**
683 * list_cut_position - cut a list into two
684 * @list: a new list to add all removed entries
685 * @head: a list with entries
686 * @entry: an entry within head, could be the head itself
687 * and if so we won't cut the list
688 *
689 * This helper moves the initial part of @head, up to and
690 * including @entry, from @head to @list. You should
691 * pass on @entry an element you know is on @head. @list
692 * should be an empty list or a list you do not care about
693 * losing its data.
694 *
695 */
16aa9ee8
DG
696static inline void list_cut_position(struct cds_list_head *list,
697 struct cds_list_head *head, struct cds_list_head *entry)
6d0ce021 698{
16aa9ee8 699 if (cds_list_empty(head))
6d0ce021
PM
700 return;
701 if (list_is_singular(head) &&
702 (head->next != entry && head != entry))
703 return;
704 if (entry == head)
16aa9ee8 705 CDS_INIT_LIST_HEAD(list);
6d0ce021
PM
706 else
707 __list_cut_position(list, head, entry);
708}
709
16aa9ee8
DG
710static inline void __cds_list_splice(const struct cds_list_head *list,
711 struct cds_list_head *prev,
712 struct cds_list_head *next)
6d0ce021 713{
16aa9ee8
DG
714 struct cds_list_head *first = list->next;
715 struct cds_list_head *last = list->prev;
6d0ce021
PM
716
717 first->prev = prev;
718 prev->next = first;
719
720 last->next = next;
721 next->prev = last;
722}
723
724/**
16aa9ee8 725 * cds_list_splice - join two lists, this is designed for stacks
6d0ce021
PM
726 * @list: the new list to add.
727 * @head: the place to add it in the first list.
728 */
16aa9ee8
DG
729static inline void cds_list_splice(const struct cds_list_head *list,
730 struct cds_list_head *head)
6d0ce021 731{
16aa9ee8
DG
732 if (!cds_list_empty(list))
733 __cds_list_splice(list, head, head->next);
6d0ce021
PM
734}
735
736/**
16aa9ee8 737 * cds_list_splice_tail - join two lists, each list being a queue
6d0ce021
PM
738 * @list: the new list to add.
739 * @head: the place to add it in the first list.
740 */
16aa9ee8
DG
741static inline void cds_list_splice_tail(struct cds_list_head *list,
742 struct cds_list_head *head)
6d0ce021 743{
16aa9ee8
DG
744 if (!cds_list_empty(list))
745 __cds_list_splice(list, head->prev, head);
6d0ce021
PM
746}
747
748/**
16aa9ee8 749 * cds_list_splice_init - join two lists and reinitialise the emptied list.
6d0ce021
PM
750 * @list: the new list to add.
751 * @head: the place to add it in the first list.
752 *
753 * The list at @list is reinitialised
754 */
16aa9ee8
DG
755static inline void cds_list_splice_init(struct cds_list_head *list,
756 struct cds_list_head *head)
6d0ce021 757{
16aa9ee8
DG
758 if (!cds_list_empty(list)) {
759 __cds_list_splice(list, head, head->next);
760 CDS_INIT_LIST_HEAD(list);
6d0ce021
PM
761 }
762}
763
764/**
16aa9ee8 765 * cds_list_splice_tail_init - join two lists and reinitialise the emptied list
6d0ce021
PM
766 * @list: the new list to add.
767 * @head: the place to add it in the first list.
768 *
769 * Each of the lists is a queue.
770 * The list at @list is reinitialised
771 */
16aa9ee8
DG
772static inline void cds_list_splice_tail_init(struct cds_list_head *list,
773 struct cds_list_head *head)
6d0ce021 774{
16aa9ee8
DG
775 if (!cds_list_empty(list)) {
776 __cds_list_splice(list, head->prev, head);
777 CDS_INIT_LIST_HEAD(list);
6d0ce021
PM
778 }
779}
780
781/**
16aa9ee8
DG
782 * cds_list_entry - get the struct for this entry
783 * @ptr: the &struct cds_list_head pointer.
6d0ce021
PM
784 * @type: the type of the struct this is embedded in.
785 * @member: the name of the list_struct within the struct.
786 */
16aa9ee8 787#define cds_list_entry(ptr, type, member) \
06f22bdb 788 caa_container_of(ptr, type, member)
6d0ce021
PM
789
790/**
791 * list_first_entry - get the first element from a list
792 * @ptr: the list head to take the element from.
793 * @type: the type of the struct this is embedded in.
794 * @member: the name of the list_struct within the struct.
795 *
796 * Note, that list is expected to be not empty.
797 */
798#define list_first_entry(ptr, type, member) \
16aa9ee8 799 cds_list_entry((ptr)->next, type, member)
6d0ce021
PM
800
801/**
16aa9ee8
DG
802 * cds_list_for_each - iterate over a list
803 * @pos: the &struct cds_list_head to use as a loop cursor.
6d0ce021
PM
804 * @head: the head for your list.
805 */
16aa9ee8 806#define cds_list_for_each(pos, head) \
6d0ce021
PM
807 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
808 pos = pos->next)
809
810/**
16aa9ee8
DG
811 * __cds_list_for_each - iterate over a list
812 * @pos: the &struct cds_list_head to use as a loop cursor.
6d0ce021
PM
813 * @head: the head for your list.
814 *
16aa9ee8 815 * This variant differs from cds_list_for_each() in that it's the
6d0ce021
PM
816 * simplest possible list iteration code, no prefetching is done.
817 * Use this for code that knows the list to be very short (empty
818 * or 1 entry) most of the time.
819 */
16aa9ee8 820#define __cds_list_for_each(pos, head) \
6d0ce021
PM
821 for (pos = (head)->next; pos != (head); pos = pos->next)
822
823/**
16aa9ee8
DG
824 * cds_list_for_each_prev - iterate over a list backwards
825 * @pos: the &struct cds_list_head to use as a loop cursor.
6d0ce021
PM
826 * @head: the head for your list.
827 */
16aa9ee8 828#define cds_list_for_each_prev(pos, head) \
6d0ce021
PM
829 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
830 pos = pos->prev)
831
832/**
16aa9ee8
DG
833 * cds_list_for_each_safe - iterate over a list safe against removal of list entry
834 * @pos: the &struct cds_list_head to use as a loop cursor.
835 * @n: another &struct cds_list_head to use as temporary storage
6d0ce021
PM
836 * @head: the head for your list.
837 */
16aa9ee8 838#define cds_list_for_each_safe(pos, n, head) \
6d0ce021
PM
839 for (pos = (head)->next, n = pos->next; pos != (head); \
840 pos = n, n = pos->next)
841
842/**
16aa9ee8
DG
843 * cds_list_for_each_prev_safe - iterate over a list backwards 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
6d0ce021
PM
846 * @head: the head for your list.
847 */
16aa9ee8 848#define cds_list_for_each_prev_safe(pos, n, head) \
6d0ce021
PM
849 for (pos = (head)->prev, n = pos->prev; \
850 prefetch(pos->prev), pos != (head); \
851 pos = n, n = pos->prev)
852
853/**
16aa9ee8 854 * cds_list_for_each_entry - iterate over list of given type
6d0ce021
PM
855 * @pos: the type * to use as a loop cursor.
856 * @head: the head for your list.
857 * @member: the name of the list_struct within the struct.
858 */
16aa9ee8
DG
859#define cds_list_for_each_entry(pos, head, member) \
860 for (pos = cds_list_entry((head)->next, typeof(*pos), member); \
6d0ce021 861 prefetch(pos->member.next), &pos->member != (head); \
16aa9ee8 862 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
6d0ce021
PM
863
864/**
16aa9ee8 865 * cds_list_for_each_entry_reverse - iterate backwards over list of given type.
6d0ce021
PM
866 * @pos: the type * to use as a loop cursor.
867 * @head: the head for your list.
868 * @member: the name of the list_struct within the struct.
869 */
16aa9ee8
DG
870#define cds_list_for_each_entry_reverse(pos, head, member) \
871 for (pos = cds_list_entry((head)->prev, typeof(*pos), member); \
6d0ce021 872 prefetch(pos->member.prev), &pos->member != (head); \
16aa9ee8 873 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
6d0ce021
PM
874
875/**
16aa9ee8 876 * list_prepare_entry - prepare a pos entry for use in cds_list_for_each_entry_continue()
6d0ce021
PM
877 * @pos: the type * to use as a start point
878 * @head: the head of the list
879 * @member: the name of the list_struct within the struct.
880 *
16aa9ee8 881 * Prepares a pos entry for use as a start point in cds_list_for_each_entry_continue().
6d0ce021
PM
882 */
883#define list_prepare_entry(pos, head, member) \
16aa9ee8 884 ((pos) ? : cds_list_entry(head, typeof(*pos), member))
6d0ce021
PM
885
886/**
16aa9ee8 887 * cds_list_for_each_entry_continue - continue iteration over list of given type
6d0ce021
PM
888 * @pos: the type * to use as a loop cursor.
889 * @head: the head for your list.
890 * @member: the name of the list_struct within the struct.
891 *
892 * Continue to iterate over list of given type, continuing after
893 * the current position.
894 */
16aa9ee8
DG
895#define cds_list_for_each_entry_continue(pos, head, member) \
896 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member); \
6d0ce021 897 prefetch(pos->member.next), &pos->member != (head); \
16aa9ee8 898 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
6d0ce021
PM
899
900/**
16aa9ee8 901 * cds_list_for_each_entry_continue_reverse - iterate backwards from the given point
6d0ce021
PM
902 * @pos: the type * to use as a loop cursor.
903 * @head: the head for your list.
904 * @member: the name of the list_struct within the struct.
905 *
906 * Start to iterate over list of given type backwards, continuing after
907 * the current position.
908 */
16aa9ee8
DG
909#define cds_list_for_each_entry_continue_reverse(pos, head, member) \
910 for (pos = cds_list_entry(pos->member.prev, typeof(*pos), member); \
6d0ce021 911 prefetch(pos->member.prev), &pos->member != (head); \
16aa9ee8 912 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
6d0ce021
PM
913
914/**
16aa9ee8 915 * cds_list_for_each_entry_from - iterate over list of given type from the current point
6d0ce021
PM
916 * @pos: the type * to use as a loop cursor.
917 * @head: the head for your list.
918 * @member: the name of the list_struct within the struct.
919 *
920 * Iterate over list of given type, continuing from current position.
921 */
16aa9ee8 922#define cds_list_for_each_entry_from(pos, head, member) \
6d0ce021 923 for (; prefetch(pos->member.next), &pos->member != (head); \
16aa9ee8 924 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
6d0ce021
PM
925
926/**
16aa9ee8 927 * cds_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
6d0ce021
PM
928 * @pos: the type * to use as a loop cursor.
929 * @n: another type * to use as temporary storage
930 * @head: the head for your list.
931 * @member: the name of the list_struct within the struct.
932 */
16aa9ee8
DG
933#define cds_list_for_each_entry_safe(pos, n, head, member) \
934 for (pos = cds_list_entry((head)->next, typeof(*pos), member), \
935 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
6d0ce021 936 &pos->member != (head); \
16aa9ee8 937 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
6d0ce021
PM
938
939/**
16aa9ee8 940 * cds_list_for_each_entry_safe_continue
6d0ce021
PM
941 * @pos: the type * to use as a loop cursor.
942 * @n: another type * to use as temporary storage
943 * @head: the head for your list.
944 * @member: the name of the list_struct within the struct.
945 *
946 * Iterate over list of given type, continuing after current point,
947 * safe against removal of list entry.
948 */
16aa9ee8
DG
949#define cds_list_for_each_entry_safe_continue(pos, n, head, member) \
950 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member), \
951 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
6d0ce021 952 &pos->member != (head); \
16aa9ee8 953 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
6d0ce021
PM
954
955/**
16aa9ee8 956 * cds_list_for_each_entry_safe_from
6d0ce021
PM
957 * @pos: the type * to use as a loop cursor.
958 * @n: another type * to use as temporary storage
959 * @head: the head for your list.
960 * @member: the name of the list_struct within the struct.
961 *
962 * Iterate over list of given type from current point, safe against
963 * removal of list entry.
964 */
16aa9ee8
DG
965#define cds_list_for_each_entry_safe_from(pos, n, head, member) \
966 for (n = cds_list_entry(pos->member.next, typeof(*pos), member); \
6d0ce021 967 &pos->member != (head); \
16aa9ee8 968 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
6d0ce021
PM
969
970/**
16aa9ee8 971 * cds_list_for_each_entry_safe_reverse
6d0ce021
PM
972 * @pos: the type * to use as a loop cursor.
973 * @n: another type * to use as temporary storage
974 * @head: the head for your list.
975 * @member: the name of the list_struct within the struct.
976 *
977 * Iterate backwards over list of given type, safe against removal
978 * of list entry.
979 */
16aa9ee8
DG
980#define cds_list_for_each_entry_safe_reverse(pos, n, head, member) \
981 for (pos = cds_list_entry((head)->prev, typeof(*pos), member), \
982 n = cds_list_entry(pos->member.prev, typeof(*pos), member); \
6d0ce021 983 &pos->member != (head); \
16aa9ee8 984 pos = n, n = cds_list_entry(n->member.prev, typeof(*n), member))
6d0ce021 985
63ff4873
MD
986#endif //0
987
6d0ce021
PM
988/*
989 * Double linked lists with a single pointer list head.
990 * Mostly useful for hash tables where the two pointer list head is
991 * too wasteful.
992 * You lose the ability to access the tail in O(1).
993 */
994
16aa9ee8
DG
995struct cds_hlist_head {
996 struct cds_hlist_node *first;
6d0ce021
PM
997};
998
16aa9ee8
DG
999struct cds_hlist_node {
1000 struct cds_hlist_node *next, **pprev;
6d0ce021
PM
1001};
1002
1003#define HLIST_HEAD_INIT { .first = NULL }
16aa9ee8
DG
1004#define HLIST_HEAD(name) struct cds_hlist_head name = { .first = NULL }
1005#define CDS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1006static inline void INIT_HLIST_NODE(struct cds_hlist_node *h)
6d0ce021
PM
1007{
1008 h->next = NULL;
1009 h->pprev = NULL;
1010}
1011
16aa9ee8 1012static inline int hlist_unhashed(const struct cds_hlist_node *h)
6d0ce021
PM
1013{
1014 return !h->pprev;
1015}
1016
16aa9ee8 1017static inline int hlist_empty(const struct cds_hlist_head *h)
6d0ce021
PM
1018{
1019 return !h->first;
1020}
1021
16aa9ee8 1022static inline void __cds_hlist_del(struct cds_hlist_node *n)
6d0ce021 1023{
16aa9ee8
DG
1024 struct cds_hlist_node *next = n->next;
1025 struct cds_hlist_node **pprev = n->pprev;
6d0ce021
PM
1026 *pprev = next;
1027 if (next)
1028 next->pprev = pprev;
1029}
1030
16aa9ee8 1031static inline void cds_hlist_del(struct cds_hlist_node *n)
6d0ce021 1032{
16aa9ee8 1033 __cds_hlist_del(n);
6d0ce021
PM
1034 n->next = LIST_POISON1;
1035 n->pprev = LIST_POISON2;
1036}
1037
16aa9ee8 1038static inline void cds_hlist_del_init(struct cds_hlist_node *n)
6d0ce021
PM
1039{
1040 if (!hlist_unhashed(n)) {
16aa9ee8 1041 __cds_hlist_del(n);
6d0ce021
PM
1042 INIT_HLIST_NODE(n);
1043 }
1044}
1045
16aa9ee8 1046static inline void cds_hlist_add_head(struct cds_hlist_node *n, struct cds_hlist_head *h)
6d0ce021 1047{
16aa9ee8 1048 struct cds_hlist_node *first = h->first;
6d0ce021
PM
1049 n->next = first;
1050 if (first)
1051 first->pprev = &n->next;
1052 h->first = n;
1053 n->pprev = &h->first;
1054}
1055
1056/* next must be != NULL */
16aa9ee8
DG
1057static inline void hlist_add_before(struct cds_hlist_node *n,
1058 struct cds_hlist_node *next)
6d0ce021
PM
1059{
1060 n->pprev = next->pprev;
1061 n->next = next;
1062 next->pprev = &n->next;
1063 *(n->pprev) = n;
1064}
1065
16aa9ee8
DG
1066static inline void hlist_add_after(struct cds_hlist_node *n,
1067 struct cds_hlist_node *next)
6d0ce021
PM
1068{
1069 next->next = n->next;
1070 n->next = next;
1071 next->pprev = &n->next;
1072
1073 if(next->next)
1074 next->next->pprev = &next->next;
1075}
1076
1077/*
1078 * Move a list from one list head to another. Fixup the pprev
1079 * reference of the first entry if it exists.
1080 */
16aa9ee8
DG
1081static inline void hlist_move_list(struct cds_hlist_head *old,
1082 struct cds_hlist_head *new)
6d0ce021
PM
1083{
1084 new->first = old->first;
1085 if (new->first)
1086 new->first->pprev = &new->first;
1087 old->first = NULL;
1088}
1089
16aa9ee8 1090#define cds_hlist_entry(ptr, type, member) caa_container_of(ptr,type,member)
6d0ce021 1091
16aa9ee8 1092#define cds_hlist_for_each(pos, head) \
6d0ce021
PM
1093 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1094 pos = pos->next)
1095
16aa9ee8 1096#define cds_hlist_for_each_safe(pos, n, head) \
6d0ce021
PM
1097 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1098 pos = n)
1099
1100/**
16aa9ee8 1101 * cds_hlist_for_each_entry - iterate over list of given type
6d0ce021 1102 * @tpos: the type * to use as a loop cursor.
16aa9ee8 1103 * @pos: the &struct cds_hlist_node to use as a loop cursor.
6d0ce021 1104 * @head: the head for your list.
16aa9ee8 1105 * @member: the name of the cds_hlist_node within the struct.
6d0ce021 1106 */
16aa9ee8 1107#define cds_hlist_for_each_entry(tpos, pos, head, member) \
6d0ce021
PM
1108 for (pos = (head)->first; \
1109 pos && ({ prefetch(pos->next); 1;}) && \
16aa9ee8 1110 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
6d0ce021
PM
1111 pos = pos->next)
1112
1113/**
16aa9ee8 1114 * cds_hlist_for_each_entry_continue - iterate over a hlist continuing after current point
6d0ce021 1115 * @tpos: the type * to use as a loop cursor.
16aa9ee8
DG
1116 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1117 * @member: the name of the cds_hlist_node within the struct.
6d0ce021 1118 */
16aa9ee8 1119#define cds_hlist_for_each_entry_continue(tpos, pos, member) \
6d0ce021
PM
1120 for (pos = (pos)->next; \
1121 pos && ({ prefetch(pos->next); 1;}) && \
16aa9ee8 1122 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
6d0ce021
PM
1123 pos = pos->next)
1124
1125/**
16aa9ee8 1126 * cds_hlist_for_each_entry_from - iterate over a hlist continuing from current point
6d0ce021 1127 * @tpos: the type * to use as a loop cursor.
16aa9ee8
DG
1128 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1129 * @member: the name of the cds_hlist_node within the struct.
6d0ce021 1130 */
16aa9ee8 1131#define cds_hlist_for_each_entry_from(tpos, pos, member) \
6d0ce021 1132 for (; pos && ({ prefetch(pos->next); 1;}) && \
16aa9ee8 1133 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
6d0ce021
PM
1134 pos = pos->next)
1135
1136/**
16aa9ee8 1137 * cds_hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
6d0ce021 1138 * @tpos: the type * to use as a loop cursor.
16aa9ee8
DG
1139 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1140 * @n: another &struct cds_hlist_node to use as temporary storage
6d0ce021 1141 * @head: the head for your list.
16aa9ee8 1142 * @member: the name of the cds_hlist_node within the struct.
6d0ce021 1143 */
16aa9ee8 1144#define cds_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
6d0ce021
PM
1145 for (pos = (head)->first; \
1146 pos && ({ n = pos->next; 1; }) && \
16aa9ee8 1147 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
6d0ce021
PM
1148 pos = n)
1149
1150#endif
1a43bbd8
MD
1151
1152#endif
This page took 0.073407 seconds and 4 git commands to generate.