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