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