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