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