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