ovs-rcu: New library.
[sliver-openvswitch.git] / lib / ovs-rcu.c
1 /*
2  * Copyright (c) 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ovs-rcu.h"
19 #include "guarded-list.h"
20 #include "list.h"
21 #include "ovs-thread.h"
22 #include "poll-loop.h"
23 #include "seq.h"
24
25 struct ovsrcu_cb {
26     void (*function)(void *aux);
27     void *aux;
28 };
29
30 struct ovsrcu_cbset {
31     struct list list_node;
32     struct ovsrcu_cb cbs[16];
33     int n_cbs;
34 };
35
36 struct ovsrcu_perthread {
37     struct list list_node;           /* In global list. */
38
39     struct ovs_mutex mutex;
40     uint64_t seqno;
41     struct ovsrcu_cbset *cbset;
42 };
43
44 static struct seq *global_seqno;
45
46 static pthread_key_t perthread_key;
47 static struct list ovsrcu_threads;
48 static struct ovs_mutex ovsrcu_threads_mutex;
49
50 static struct guarded_list flushed_cbsets;
51 static struct seq *flushed_cbsets_seq;
52
53 static void ovsrcu_init(void);
54 static void ovsrcu_flush_cbset(struct ovsrcu_perthread *);
55 static void ovsrcu_unregister__(struct ovsrcu_perthread *);
56 static bool ovsrcu_call_postponed(void);
57 static void *ovsrcu_postpone_thread(void *arg OVS_UNUSED);
58 static void ovsrcu_synchronize(void);
59
60 static struct ovsrcu_perthread *
61 ovsrcu_perthread_get(void)
62 {
63     struct ovsrcu_perthread *perthread;
64
65     ovsrcu_init();
66
67     perthread = pthread_getspecific(perthread_key);
68     if (!perthread) {
69         perthread = xmalloc(sizeof *perthread);
70         ovs_mutex_init(&perthread->mutex);
71         perthread->seqno = seq_read(global_seqno);
72         perthread->cbset = NULL;
73
74         ovs_mutex_lock(&ovsrcu_threads_mutex);
75         list_push_back(&ovsrcu_threads, &perthread->list_node);
76         ovs_mutex_unlock(&ovsrcu_threads_mutex);
77
78         pthread_setspecific(perthread_key, perthread);
79     }
80     return perthread;
81 }
82
83 /* Indicates the end of a quiescent state.  See "Details" near the top of
84  * ovs-rcu.h.
85  *
86  * Quiescent states don't stack or nest, so this always ends a quiescent state
87  * even if ovsrcu_quiesce_start() was called multiple times in a row. */
88 void
89 ovsrcu_quiesce_end(void)
90 {
91     ovsrcu_perthread_get();
92 }
93
94 static void
95 ovsrcu_quiesced(void)
96 {
97     if (single_threaded()) {
98         ovsrcu_call_postponed();
99     } else {
100         static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
101         if (ovsthread_once_start(&once)) {
102             xpthread_create(NULL, NULL, ovsrcu_postpone_thread, NULL);
103             ovsthread_once_done(&once);
104         }
105     }
106 }
107
108 /* Indicates the beginning of a quiescent state.  See "Details" near the top of
109  * ovs-rcu.h. */
110 void
111 ovsrcu_quiesce_start(void)
112 {
113     struct ovsrcu_perthread *perthread;
114
115     ovsrcu_init();
116     perthread = pthread_getspecific(perthread_key);
117     if (perthread) {
118         pthread_setspecific(perthread_key, NULL);
119         ovsrcu_unregister__(perthread);
120     }
121
122     ovsrcu_quiesced();
123 }
124
125 /* Indicates a momentary quiescent state.  See "Details" near the top of
126  * ovs-rcu.h. */
127 void
128 ovsrcu_quiesce(void)
129 {
130     ovsrcu_perthread_get()->seqno = seq_read(global_seqno);
131     seq_change(global_seqno);
132
133     ovsrcu_quiesced();
134 }
135
136 static void
137 ovsrcu_synchronize(void)
138 {
139     uint64_t target_seqno;
140
141     if (single_threaded()) {
142         return;
143     }
144
145     target_seqno = seq_read(global_seqno);
146     ovsrcu_quiesce_start();
147
148     for (;;) {
149         uint64_t cur_seqno = seq_read(global_seqno);
150         struct ovsrcu_perthread *perthread;
151         bool done = true;
152
153         ovs_mutex_lock(&ovsrcu_threads_mutex);
154         LIST_FOR_EACH (perthread, list_node, &ovsrcu_threads) {
155             if (perthread->seqno <= target_seqno) {
156                 done = false;
157                 break;
158             }
159         }
160         ovs_mutex_unlock(&ovsrcu_threads_mutex);
161
162         if (done) {
163             break;
164         }
165
166         seq_wait(global_seqno, cur_seqno);
167         poll_block();
168     }
169     ovsrcu_quiesce_end();
170 }
171
172 /* Registers 'function' to be called, passing 'aux' as argument, after the
173  * next grace period.
174  *
175  * This function is more conveniently called through the ovsrcu_postpone()
176  * macro, which provides a type-safe way to allow 'function''s parameter to be
177  * any pointer type. */
178 void
179 ovsrcu_postpone__(void (*function)(void *aux), void *aux)
180 {
181     struct ovsrcu_perthread *perthread = ovsrcu_perthread_get();
182     struct ovsrcu_cbset *cbset;
183     struct ovsrcu_cb *cb;
184
185     cbset = perthread->cbset;
186     if (!cbset) {
187         cbset = perthread->cbset = xmalloc(sizeof *perthread->cbset);
188         cbset->n_cbs = 0;
189     }
190
191     cb = &cbset->cbs[cbset->n_cbs++];
192     cb->function = function;
193     cb->aux = aux;
194
195     if (cbset->n_cbs >= ARRAY_SIZE(cbset->cbs)) {
196         ovsrcu_flush_cbset(perthread);
197     }
198 }
199
200 static bool
201 ovsrcu_call_postponed(void)
202 {
203     struct ovsrcu_cbset *cbset, *next_cbset;
204     struct list cbsets;
205
206     guarded_list_pop_all(&flushed_cbsets, &cbsets);
207     if (list_is_empty(&cbsets)) {
208         return false;
209     }
210
211     ovsrcu_synchronize();
212
213     LIST_FOR_EACH_SAFE (cbset, next_cbset, list_node, &cbsets) {
214         struct ovsrcu_cb *cb;
215
216         for (cb = cbset->cbs; cb < &cbset->cbs[cbset->n_cbs]; cb++) {
217             cb->function(cb->aux);
218         }
219         list_remove(&cbset->list_node);
220         free(cbset);
221     }
222
223     return true;
224 }
225
226 static void *
227 ovsrcu_postpone_thread(void *arg OVS_UNUSED)
228 {
229     pthread_detach(pthread_self());
230
231     for (;;) {
232         uint64_t seqno = seq_read(flushed_cbsets_seq);
233         if (!ovsrcu_call_postponed()) {
234             seq_wait(flushed_cbsets_seq, seqno);
235             poll_block();
236         }
237     }
238
239     OVS_NOT_REACHED();
240 }
241
242 static void
243 ovsrcu_flush_cbset(struct ovsrcu_perthread *perthread)
244 {
245     struct ovsrcu_cbset *cbset = perthread->cbset;
246
247     if (cbset) {
248         guarded_list_push_back(&flushed_cbsets, &cbset->list_node, SIZE_MAX);
249         perthread->cbset = NULL;
250
251         seq_change(flushed_cbsets_seq);
252     }
253 }
254
255 static void
256 ovsrcu_unregister__(struct ovsrcu_perthread *perthread)
257 {
258     if (perthread->cbset) {
259         ovsrcu_flush_cbset(perthread);
260     }
261
262     ovs_mutex_lock(&ovsrcu_threads_mutex);
263     list_remove(&perthread->list_node);
264     ovs_mutex_unlock(&ovsrcu_threads_mutex);
265
266     ovs_mutex_destroy(&perthread->mutex);
267     free(perthread);
268
269     seq_change(global_seqno);
270 }
271
272 static void
273 ovsrcu_thread_exit_cb(void *perthread)
274 {
275     ovsrcu_unregister__(perthread);
276 }
277
278 static void
279 ovsrcu_init(void)
280 {
281     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
282     if (ovsthread_once_start(&once)) {
283         global_seqno = seq_create();
284         xpthread_key_create(&perthread_key, ovsrcu_thread_exit_cb);
285         list_init(&ovsrcu_threads);
286         ovs_mutex_init(&ovsrcu_threads_mutex);
287
288         guarded_list_init(&flushed_cbsets);
289         flushed_cbsets_seq = seq_create();
290
291         ovsthread_once_done(&once);
292     }
293 }