tests api: remove atomics
[urcu.git] / tests / api_x86.h
1 /* MECHANICALLY GENERATED, DO NOT EDIT!!! */
2
3 #ifndef _INCLUDE_API_H
4 #define _INCLUDE_API_H
5
6 #include "../config.h"
7
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
31 #include <urcu/arch.h>
32
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
79 /* #define CAA_CACHE_LINE_SIZE 64 */
80 #define ____cacheline_internodealigned_in_smp \
81 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
82
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
116 /*
117 * Default machine parameters.
118 */
119
120 #ifndef CAA_CACHE_LINE_SIZE
121 /* #define CAA_CACHE_LINE_SIZE 128 */
122 #endif /* #ifndef CAA_CACHE_LINE_SIZE */
123
124 /*
125 * Exclusive locking primitives.
126 */
127
128 typedef 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
133 static 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
141 static 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
149 static 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
160 /*
161 * Thread creation/destruction primitives.
162 */
163
164 typedef pthread_t thread_id_t;
165
166 #define NR_THREADS 128
167
168 #define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0)
169 #define __THREAD_ID_MAP_WAITING ((thread_id_t) 1)
170 thread_id_t __thread_id_map[NR_THREADS];
171 spinlock_t __thread_id_map_mutex;
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
181 pthread_key_t thread_id_key;
182
183 static 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);
206 fprintf(stderr, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
207 (int)tid, (int)tid);
208 exit(-1);
209 }
210
211 static 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
221 static 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
246 static 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){
256 fprintf(stderr, "wait_thread: bad tid = %d(%#x)\n",
257 (int)tid, (int)tid);
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
268 static 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
281 #ifndef HAVE_CPU_SET_T
282 typedef unsigned long cpu_set_t;
283 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
284 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
285 #endif
286
287 static void run_on(int cpu)
288 {
289 #if HAVE_SCHED_SETAFFINITY
290 cpu_set_t mask;
291
292 CPU_ZERO(&mask);
293 CPU_SET(cpu, &mask);
294 #if SCHED_SETAFFINITY_ARGS == 2
295 sched_setaffinity(0, &mask);
296 #else
297 sched_setaffinity(0, sizeof(mask), &mask);
298 #endif
299 #endif /* HAVE_SCHED_SETAFFINITY */
300 }
301
302 /*
303 * timekeeping -- very crude -- should use MONOTONIC...
304 */
305
306 long 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 \
322 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
323 } __per_thread_##name[NR_THREADS];
324 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
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 \
356 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
357 } __per_cpu_##name[NR_CPUS]
358 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
359
360 DEFINE_PER_THREAD(int, smp_processor_id);
361
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
389 struct 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
447 static 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
470 #if 0
471
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
482 struct cds_list_head {
483 struct cds_list_head *next, *prev;
484 };
485
486 #define CDS_LIST_HEAD_INIT(name) { &(name), &(name) }
487
488 #define CDS_LIST_HEAD(name) \
489 struct cds_list_head name = CDS_LIST_HEAD_INIT(name)
490
491 static inline void CDS_INIT_LIST_HEAD(struct cds_list_head *list)
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
504 static inline void __cds_list_add(struct cds_list_head *new,
505 struct cds_list_head *prev,
506 struct cds_list_head *next)
507 {
508 next->prev = new;
509 new->next = next;
510 new->prev = prev;
511 prev->next = new;
512 }
513 #else
514 extern void __cds_list_add(struct cds_list_head *new,
515 struct cds_list_head *prev,
516 struct cds_list_head *next);
517 #endif
518
519 /**
520 * cds_list_add - add a new entry
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 */
527 static inline void cds_list_add(struct cds_list_head *new, struct cds_list_head *head)
528 {
529 __cds_list_add(new, head, head->next);
530 }
531
532
533 /**
534 * cds_list_add_tail - add a new entry
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 */
541 static inline void cds_list_add_tail(struct cds_list_head *new, struct cds_list_head *head)
542 {
543 __cds_list_add(new, head->prev, head);
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 */
553 static inline void __cds_list_del(struct cds_list_head * prev, struct cds_list_head * next)
554 {
555 next->prev = prev;
556 prev->next = next;
557 }
558
559 /**
560 * cds_list_del - deletes entry from list.
561 * @entry: the element to delete from the list.
562 * Note: cds_list_empty() on entry does not return true after this, the entry is
563 * in an undefined state.
564 */
565 #ifndef CONFIG_DEBUG_LIST
566 static inline void cds_list_del(struct cds_list_head *entry)
567 {
568 __cds_list_del(entry->prev, entry->next);
569 entry->next = LIST_POISON1;
570 entry->prev = LIST_POISON2;
571 }
572 #else
573 extern void cds_list_del(struct cds_list_head *entry);
574 #endif
575
576 /**
577 * cds_list_replace - replace old entry by new one
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 */
583 static inline void cds_list_replace(struct cds_list_head *old,
584 struct cds_list_head *new)
585 {
586 new->next = old->next;
587 new->next->prev = new;
588 new->prev = old->prev;
589 new->prev->next = new;
590 }
591
592 static inline void cds_list_replace_init(struct cds_list_head *old,
593 struct cds_list_head *new)
594 {
595 cds_list_replace(old, new);
596 CDS_INIT_LIST_HEAD(old);
597 }
598
599 /**
600 * cds_list_del_init - deletes entry from list and reinitialize it.
601 * @entry: the element to delete from the list.
602 */
603 static inline void cds_list_del_init(struct cds_list_head *entry)
604 {
605 __cds_list_del(entry->prev, entry->next);
606 CDS_INIT_LIST_HEAD(entry);
607 }
608
609 /**
610 * cds_list_move - delete from one list and add as another's head
611 * @list: the entry to move
612 * @head: the head that will precede our entry
613 */
614 static inline void cds_list_move(struct cds_list_head *list, struct cds_list_head *head)
615 {
616 __cds_list_del(list->prev, list->next);
617 cds_list_add(list, head);
618 }
619
620 /**
621 * cds_list_move_tail - delete from one list and add as another's tail
622 * @list: the entry to move
623 * @head: the head that will follow our entry
624 */
625 static inline void cds_list_move_tail(struct cds_list_head *list,
626 struct cds_list_head *head)
627 {
628 __cds_list_del(list->prev, list->next);
629 cds_list_add_tail(list, head);
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 */
637 static inline int list_is_last(const struct cds_list_head *list,
638 const struct cds_list_head *head)
639 {
640 return list->next == head;
641 }
642
643 /**
644 * cds_list_empty - tests whether a list is empty
645 * @head: the list to test.
646 */
647 static inline int cds_list_empty(const struct cds_list_head *head)
648 {
649 return head->next == head;
650 }
651
652 /**
653 * cds_list_empty_careful - tests whether a list is empty and not being modified
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 *
660 * NOTE: using cds_list_empty_careful() without synchronization
661 * can only be safe if the only activity that can happen
662 * to the list entry is cds_list_del_init(). Eg. it cannot be used
663 * if another CPU could re-list_add() it.
664 */
665 static inline int cds_list_empty_careful(const struct cds_list_head *head)
666 {
667 struct cds_list_head *next = head->next;
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 */
675 static inline int list_is_singular(const struct cds_list_head *head)
676 {
677 return !list_empty(head) && (head->next == head->prev);
678 }
679
680 static inline void __list_cut_position(struct cds_list_head *list,
681 struct cds_list_head *head, struct cds_list_head *entry)
682 {
683 struct cds_list_head *new_first = entry->next;
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 */
706 static inline void list_cut_position(struct cds_list_head *list,
707 struct cds_list_head *head, struct cds_list_head *entry)
708 {
709 if (cds_list_empty(head))
710 return;
711 if (list_is_singular(head) &&
712 (head->next != entry && head != entry))
713 return;
714 if (entry == head)
715 CDS_INIT_LIST_HEAD(list);
716 else
717 __list_cut_position(list, head, entry);
718 }
719
720 static inline void __cds_list_splice(const struct cds_list_head *list,
721 struct cds_list_head *prev,
722 struct cds_list_head *next)
723 {
724 struct cds_list_head *first = list->next;
725 struct cds_list_head *last = list->prev;
726
727 first->prev = prev;
728 prev->next = first;
729
730 last->next = next;
731 next->prev = last;
732 }
733
734 /**
735 * cds_list_splice - join two lists, this is designed for stacks
736 * @list: the new list to add.
737 * @head: the place to add it in the first list.
738 */
739 static inline void cds_list_splice(const struct cds_list_head *list,
740 struct cds_list_head *head)
741 {
742 if (!cds_list_empty(list))
743 __cds_list_splice(list, head, head->next);
744 }
745
746 /**
747 * cds_list_splice_tail - join two lists, each list being a queue
748 * @list: the new list to add.
749 * @head: the place to add it in the first list.
750 */
751 static inline void cds_list_splice_tail(struct cds_list_head *list,
752 struct cds_list_head *head)
753 {
754 if (!cds_list_empty(list))
755 __cds_list_splice(list, head->prev, head);
756 }
757
758 /**
759 * cds_list_splice_init - join two lists and reinitialise the emptied list.
760 * @list: the new list to add.
761 * @head: the place to add it in the first list.
762 *
763 * The list at @list is reinitialised
764 */
765 static inline void cds_list_splice_init(struct cds_list_head *list,
766 struct cds_list_head *head)
767 {
768 if (!cds_list_empty(list)) {
769 __cds_list_splice(list, head, head->next);
770 CDS_INIT_LIST_HEAD(list);
771 }
772 }
773
774 /**
775 * cds_list_splice_tail_init - join two lists and reinitialise the emptied list
776 * @list: the new list to add.
777 * @head: the place to add it in the first list.
778 *
779 * Each of the lists is a queue.
780 * The list at @list is reinitialised
781 */
782 static inline void cds_list_splice_tail_init(struct cds_list_head *list,
783 struct cds_list_head *head)
784 {
785 if (!cds_list_empty(list)) {
786 __cds_list_splice(list, head->prev, head);
787 CDS_INIT_LIST_HEAD(list);
788 }
789 }
790
791 /**
792 * cds_list_entry - get the struct for this entry
793 * @ptr: the &struct cds_list_head pointer.
794 * @type: the type of the struct this is embedded in.
795 * @member: the name of the list_struct within the struct.
796 */
797 #define cds_list_entry(ptr, type, member) \
798 caa_container_of(ptr, type, member)
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) \
809 cds_list_entry((ptr)->next, type, member)
810
811 /**
812 * cds_list_for_each - iterate over a list
813 * @pos: the &struct cds_list_head to use as a loop cursor.
814 * @head: the head for your list.
815 */
816 #define cds_list_for_each(pos, head) \
817 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
818 pos = pos->next)
819
820 /**
821 * __cds_list_for_each - iterate over a list
822 * @pos: the &struct cds_list_head to use as a loop cursor.
823 * @head: the head for your list.
824 *
825 * This variant differs from cds_list_for_each() in that it's the
826 * simplest possible list iteration code, no prefetching is done.
827 * Use this for code that knows the list to be very short (empty
828 * or 1 entry) most of the time.
829 */
830 #define __cds_list_for_each(pos, head) \
831 for (pos = (head)->next; pos != (head); pos = pos->next)
832
833 /**
834 * cds_list_for_each_prev - iterate over a list backwards
835 * @pos: the &struct cds_list_head to use as a loop cursor.
836 * @head: the head for your list.
837 */
838 #define cds_list_for_each_prev(pos, head) \
839 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
840 pos = pos->prev)
841
842 /**
843 * cds_list_for_each_safe - iterate over a list safe against removal of list entry
844 * @pos: the &struct cds_list_head to use as a loop cursor.
845 * @n: another &struct cds_list_head to use as temporary storage
846 * @head: the head for your list.
847 */
848 #define cds_list_for_each_safe(pos, n, head) \
849 for (pos = (head)->next, n = pos->next; pos != (head); \
850 pos = n, n = pos->next)
851
852 /**
853 * cds_list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
854 * @pos: the &struct cds_list_head to use as a loop cursor.
855 * @n: another &struct cds_list_head to use as temporary storage
856 * @head: the head for your list.
857 */
858 #define cds_list_for_each_prev_safe(pos, n, head) \
859 for (pos = (head)->prev, n = pos->prev; \
860 prefetch(pos->prev), pos != (head); \
861 pos = n, n = pos->prev)
862
863 /**
864 * cds_list_for_each_entry - iterate over list of given type
865 * @pos: the type * to use as a loop cursor.
866 * @head: the head for your list.
867 * @member: the name of the list_struct within the struct.
868 */
869 #define cds_list_for_each_entry(pos, head, member) \
870 for (pos = cds_list_entry((head)->next, typeof(*pos), member); \
871 prefetch(pos->member.next), &pos->member != (head); \
872 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
873
874 /**
875 * cds_list_for_each_entry_reverse - iterate backwards over list of given type.
876 * @pos: the type * to use as a loop cursor.
877 * @head: the head for your list.
878 * @member: the name of the list_struct within the struct.
879 */
880 #define cds_list_for_each_entry_reverse(pos, head, member) \
881 for (pos = cds_list_entry((head)->prev, typeof(*pos), member); \
882 prefetch(pos->member.prev), &pos->member != (head); \
883 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
884
885 /**
886 * list_prepare_entry - prepare a pos entry for use in cds_list_for_each_entry_continue()
887 * @pos: the type * to use as a start point
888 * @head: the head of the list
889 * @member: the name of the list_struct within the struct.
890 *
891 * Prepares a pos entry for use as a start point in cds_list_for_each_entry_continue().
892 */
893 #define list_prepare_entry(pos, head, member) \
894 ((pos) ? : cds_list_entry(head, typeof(*pos), member))
895
896 /**
897 * cds_list_for_each_entry_continue - continue iteration over list of given type
898 * @pos: the type * to use as a loop cursor.
899 * @head: the head for your list.
900 * @member: the name of the list_struct within the struct.
901 *
902 * Continue to iterate over list of given type, continuing after
903 * the current position.
904 */
905 #define cds_list_for_each_entry_continue(pos, head, member) \
906 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member); \
907 prefetch(pos->member.next), &pos->member != (head); \
908 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
909
910 /**
911 * cds_list_for_each_entry_continue_reverse - iterate backwards from the given point
912 * @pos: the type * to use as a loop cursor.
913 * @head: the head for your list.
914 * @member: the name of the list_struct within the struct.
915 *
916 * Start to iterate over list of given type backwards, continuing after
917 * the current position.
918 */
919 #define cds_list_for_each_entry_continue_reverse(pos, head, member) \
920 for (pos = cds_list_entry(pos->member.prev, typeof(*pos), member); \
921 prefetch(pos->member.prev), &pos->member != (head); \
922 pos = cds_list_entry(pos->member.prev, typeof(*pos), member))
923
924 /**
925 * cds_list_for_each_entry_from - iterate over list of given type from the current point
926 * @pos: the type * to use as a loop cursor.
927 * @head: the head for your list.
928 * @member: the name of the list_struct within the struct.
929 *
930 * Iterate over list of given type, continuing from current position.
931 */
932 #define cds_list_for_each_entry_from(pos, head, member) \
933 for (; prefetch(pos->member.next), &pos->member != (head); \
934 pos = cds_list_entry(pos->member.next, typeof(*pos), member))
935
936 /**
937 * cds_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
938 * @pos: the type * to use as a loop cursor.
939 * @n: another type * to use as temporary storage
940 * @head: the head for your list.
941 * @member: the name of the list_struct within the struct.
942 */
943 #define cds_list_for_each_entry_safe(pos, n, head, member) \
944 for (pos = cds_list_entry((head)->next, typeof(*pos), member), \
945 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
946 &pos->member != (head); \
947 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
948
949 /**
950 * cds_list_for_each_entry_safe_continue
951 * @pos: the type * to use as a loop cursor.
952 * @n: another type * to use as temporary storage
953 * @head: the head for your list.
954 * @member: the name of the list_struct within the struct.
955 *
956 * Iterate over list of given type, continuing after current point,
957 * safe against removal of list entry.
958 */
959 #define cds_list_for_each_entry_safe_continue(pos, n, head, member) \
960 for (pos = cds_list_entry(pos->member.next, typeof(*pos), member), \
961 n = cds_list_entry(pos->member.next, typeof(*pos), member); \
962 &pos->member != (head); \
963 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
964
965 /**
966 * cds_list_for_each_entry_safe_from
967 * @pos: the type * to use as a loop cursor.
968 * @n: another type * to use as temporary storage
969 * @head: the head for your list.
970 * @member: the name of the list_struct within the struct.
971 *
972 * Iterate over list of given type from current point, safe against
973 * removal of list entry.
974 */
975 #define cds_list_for_each_entry_safe_from(pos, n, head, member) \
976 for (n = cds_list_entry(pos->member.next, typeof(*pos), member); \
977 &pos->member != (head); \
978 pos = n, n = cds_list_entry(n->member.next, typeof(*n), member))
979
980 /**
981 * cds_list_for_each_entry_safe_reverse
982 * @pos: the type * to use as a loop cursor.
983 * @n: another type * to use as temporary storage
984 * @head: the head for your list.
985 * @member: the name of the list_struct within the struct.
986 *
987 * Iterate backwards over list of given type, safe against removal
988 * of list entry.
989 */
990 #define cds_list_for_each_entry_safe_reverse(pos, n, head, member) \
991 for (pos = cds_list_entry((head)->prev, typeof(*pos), member), \
992 n = cds_list_entry(pos->member.prev, typeof(*pos), member); \
993 &pos->member != (head); \
994 pos = n, n = cds_list_entry(n->member.prev, typeof(*n), member))
995
996 #endif //0
997
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
1005 struct cds_hlist_head {
1006 struct cds_hlist_node *first;
1007 };
1008
1009 struct cds_hlist_node {
1010 struct cds_hlist_node *next, **pprev;
1011 };
1012
1013 #define HLIST_HEAD_INIT { .first = NULL }
1014 #define HLIST_HEAD(name) struct cds_hlist_head name = { .first = NULL }
1015 #define CDS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1016 static inline void INIT_HLIST_NODE(struct cds_hlist_node *h)
1017 {
1018 h->next = NULL;
1019 h->pprev = NULL;
1020 }
1021
1022 static inline int hlist_unhashed(const struct cds_hlist_node *h)
1023 {
1024 return !h->pprev;
1025 }
1026
1027 static inline int hlist_empty(const struct cds_hlist_head *h)
1028 {
1029 return !h->first;
1030 }
1031
1032 static inline void __cds_hlist_del(struct cds_hlist_node *n)
1033 {
1034 struct cds_hlist_node *next = n->next;
1035 struct cds_hlist_node **pprev = n->pprev;
1036 *pprev = next;
1037 if (next)
1038 next->pprev = pprev;
1039 }
1040
1041 static inline void cds_hlist_del(struct cds_hlist_node *n)
1042 {
1043 __cds_hlist_del(n);
1044 n->next = LIST_POISON1;
1045 n->pprev = LIST_POISON2;
1046 }
1047
1048 static inline void cds_hlist_del_init(struct cds_hlist_node *n)
1049 {
1050 if (!hlist_unhashed(n)) {
1051 __cds_hlist_del(n);
1052 INIT_HLIST_NODE(n);
1053 }
1054 }
1055
1056 static inline void cds_hlist_add_head(struct cds_hlist_node *n, struct cds_hlist_head *h)
1057 {
1058 struct cds_hlist_node *first = h->first;
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 */
1067 static inline void hlist_add_before(struct cds_hlist_node *n,
1068 struct cds_hlist_node *next)
1069 {
1070 n->pprev = next->pprev;
1071 n->next = next;
1072 next->pprev = &n->next;
1073 *(n->pprev) = n;
1074 }
1075
1076 static inline void hlist_add_after(struct cds_hlist_node *n,
1077 struct cds_hlist_node *next)
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 */
1091 static inline void hlist_move_list(struct cds_hlist_head *old,
1092 struct cds_hlist_head *new)
1093 {
1094 new->first = old->first;
1095 if (new->first)
1096 new->first->pprev = &new->first;
1097 old->first = NULL;
1098 }
1099
1100 #define cds_hlist_entry(ptr, type, member) caa_container_of(ptr,type,member)
1101
1102 #define cds_hlist_for_each(pos, head) \
1103 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1104 pos = pos->next)
1105
1106 #define cds_hlist_for_each_safe(pos, n, head) \
1107 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1108 pos = n)
1109
1110 /**
1111 * cds_hlist_for_each_entry - iterate over list of given type
1112 * @tpos: the type * to use as a loop cursor.
1113 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1114 * @head: the head for your list.
1115 * @member: the name of the cds_hlist_node within the struct.
1116 */
1117 #define cds_hlist_for_each_entry(tpos, pos, head, member) \
1118 for (pos = (head)->first; \
1119 pos && ({ prefetch(pos->next); 1;}) && \
1120 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1121 pos = pos->next)
1122
1123 /**
1124 * cds_hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1125 * @tpos: the type * to use as a loop cursor.
1126 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1127 * @member: the name of the cds_hlist_node within the struct.
1128 */
1129 #define cds_hlist_for_each_entry_continue(tpos, pos, member) \
1130 for (pos = (pos)->next; \
1131 pos && ({ prefetch(pos->next); 1;}) && \
1132 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1133 pos = pos->next)
1134
1135 /**
1136 * cds_hlist_for_each_entry_from - iterate over a hlist continuing from current point
1137 * @tpos: the type * to use as a loop cursor.
1138 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1139 * @member: the name of the cds_hlist_node within the struct.
1140 */
1141 #define cds_hlist_for_each_entry_from(tpos, pos, member) \
1142 for (; pos && ({ prefetch(pos->next); 1;}) && \
1143 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1144 pos = pos->next)
1145
1146 /**
1147 * cds_hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1148 * @tpos: the type * to use as a loop cursor.
1149 * @pos: the &struct cds_hlist_node to use as a loop cursor.
1150 * @n: another &struct cds_hlist_node to use as temporary storage
1151 * @head: the head for your list.
1152 * @member: the name of the cds_hlist_node within the struct.
1153 */
1154 #define cds_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1155 for (pos = (head)->first; \
1156 pos && ({ n = pos->next; 1; }) && \
1157 ({ tpos = cds_hlist_entry(pos, typeof(*tpos), member); 1;}); \
1158 pos = n)
1159
1160 #endif
1161
1162 #endif
This page took 0.056056 seconds and 4 git commands to generate.