1 | /*
|
---|
2 | * Copyright (c) 2012 Adam Hraska
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup liburcu
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef LIBURCU_RCU_H_
|
---|
37 | #define LIBURCU_RCU_H_
|
---|
38 |
|
---|
39 | #include <compiler/barrier.h>
|
---|
40 | #include <libarch/barrier.h>
|
---|
41 | #include <stdbool.h>
|
---|
42 |
|
---|
43 | /** Use to assign a pointer to newly initialized data to a rcu reader
|
---|
44 | * accessible pointer.
|
---|
45 | *
|
---|
46 | * Example:
|
---|
47 | * @code
|
---|
48 | * typedef struct exam {
|
---|
49 | * struct exam *next;
|
---|
50 | * int grade;
|
---|
51 | * } exam_t;
|
---|
52 | *
|
---|
53 | * exam_t *exam_list;
|
---|
54 | * // ..
|
---|
55 | *
|
---|
56 | * // Insert at the beginning of the list.
|
---|
57 | * exam_t *my_exam = malloc(sizeof(exam_t));
|
---|
58 | * my_exam->grade = 5;
|
---|
59 | * my_exam->next = exam_list;
|
---|
60 | * rcu_assign(exam_list, my_exam);
|
---|
61 | *
|
---|
62 | * // Changes properly propagate. Every reader either sees
|
---|
63 | * // the old version of exam_list or the new version with
|
---|
64 | * // the fully initialized my_exam.
|
---|
65 | * rcu_synchronize();
|
---|
66 | * // Now we can be sure every reader sees my_exam.
|
---|
67 | *
|
---|
68 | * @endcode
|
---|
69 | */
|
---|
70 | #define rcu_assign(ptr, value) \
|
---|
71 | do { \
|
---|
72 | memory_barrier(); \
|
---|
73 | (ptr) = (value); \
|
---|
74 | } while (0)
|
---|
75 |
|
---|
76 | /** Use to access RCU protected data in a reader section.
|
---|
77 | *
|
---|
78 | * Example:
|
---|
79 | * @code
|
---|
80 | * exam_t *exam_list;
|
---|
81 | * // ...
|
---|
82 | *
|
---|
83 | * rcu_read_lock();
|
---|
84 | * exam_t *first_exam = rcu_access(exam_list);
|
---|
85 | * // We can now safely use first_exam, it won't change
|
---|
86 | * // under us while we're using it.
|
---|
87 | *
|
---|
88 | * // ..
|
---|
89 | * rcu_read_unlock();
|
---|
90 | * @endcode
|
---|
91 | */
|
---|
92 | #define rcu_access(ptr) ACCESS_ONCE(ptr)
|
---|
93 |
|
---|
94 | extern void rcu_register_fibril(void);
|
---|
95 | extern void rcu_deregister_fibril(void);
|
---|
96 |
|
---|
97 | extern void rcu_read_lock(void);
|
---|
98 | extern void rcu_read_unlock(void);
|
---|
99 |
|
---|
100 | extern bool rcu_read_locked(void);
|
---|
101 |
|
---|
102 | extern void rcu_synchronize(void);
|
---|
103 |
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | /** @}
|
---|
107 | */
|
---|