timeval: Preserve quiescence across time_poll().
[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_init();
131     ovsrcu_perthread_get()->seqno = seq_read(global_seqno);
132     seq_change(global_seqno);
133
134     ovsrcu_quiesced();
135 }
136
137 bool
138 ovsrcu_is_quiescent(void)
139 {
140     ovsrcu_init();
141     return pthread_getspecific(perthread_key) == NULL;
142 }
143
144 static void
145 ovsrcu_synchronize(void)
146 {
147     uint64_t target_seqno;
148
149     if (single_threaded()) {
150         return;
151     }
152
153     target_seqno = seq_read(global_seqno);
154     ovsrcu_quiesce_start();
155
156     for (;;) {
157         uint64_t cur_seqno = seq_read(global_seqno);
158         struct ovsrcu_perthread *perthread;
159         bool done = true;
160
161         ovs_mutex_lock(&ovsrcu_threads_mutex);
162         LIST_FOR_EACH (perthread, list_node, &ovsrcu_threads) {
163             if (perthread->seqno <= target_seqno) {
164                 done = false;
165                 break;
166             }
167         }
168         ovs_mutex_unlock(&ovsrcu_threads_mutex);
169
170         if (done) {
171             break;
172         }
173
174         seq_wait(global_seqno, cur_seqno);
175         poll_block();
176     }
177     ovsrcu_quiesce_end();
178 }
179
180 /* Registers 'function' to be called, passing 'aux' as argument, after the
181  * next grace period.
182  *
183  * This function is more conveniently called through the ovsrcu_postpone()
184  * macro, which provides a type-safe way to allow 'function''s parameter to be
185  * any pointer type. */
186 void
187 ovsrcu_postpone__(void (*function)(void *aux), void *aux)
188 {
189     struct ovsrcu_perthread *perthread = ovsrcu_perthread_get();
190     struct ovsrcu_cbset *cbset;
191     struct ovsrcu_cb *cb;
192
193     cbset = perthread->cbset;
194     if (!cbset) {
195         cbset = perthread->cbset = xmalloc(sizeof *perthread->cbset);
196         cbset->n_cbs = 0;
197     }
198
199     cb = &cbset->cbs[cbset->n_cbs++];
200     cb->function = function;
201     cb->aux = aux;
202
203     if (cbset->n_cbs >= ARRAY_SIZE(cbset->cbs)) {
204         ovsrcu_flush_cbset(perthread);
205     }
206 }
207
208 static bool
209 ovsrcu_call_postponed(void)
210 {
211     struct ovsrcu_cbset *cbset, *next_cbset;
212     struct list cbsets;
213
214     guarded_list_pop_all(&flushed_cbsets, &cbsets);
215     if (list_is_empty(&cbsets)) {
216         return false;
217     }
218
219     ovsrcu_synchronize();
220
221     LIST_FOR_EACH_SAFE (cbset, next_cbset, list_node, &cbsets) {
222         struct ovsrcu_cb *cb;
223
224         for (cb = cbset->cbs; cb < &cbset->cbs[cbset->n_cbs]; cb++) {
225             cb->function(cb->aux);
226         }
227         list_remove(&cbset->list_node);
228         free(cbset);
229     }
230
231     return true;
232 }
233
234 static void *
235 ovsrcu_postpone_thread(void *arg OVS_UNUSED)
236 {
237     set_subprogram_name("urcu");
238     pthread_detach(pthread_self());
239
240     for (;;) {
241         uint64_t seqno = seq_read(flushed_cbsets_seq);
242         if (!ovsrcu_call_postponed()) {
243             seq_wait(flushed_cbsets_seq, seqno);
244             poll_block();
245         }
246     }
247
248     OVS_NOT_REACHED();
249 }
250
251 static void
252 ovsrcu_flush_cbset(struct ovsrcu_perthread *perthread)
253 {
254     struct ovsrcu_cbset *cbset = perthread->cbset;
255
256     if (cbset) {
257         guarded_list_push_back(&flushed_cbsets, &cbset->list_node, SIZE_MAX);
258         perthread->cbset = NULL;
259
260         seq_change(flushed_cbsets_seq);
261     }
262 }
263
264 static void
265 ovsrcu_unregister__(struct ovsrcu_perthread *perthread)
266 {
267     if (perthread->cbset) {
268         ovsrcu_flush_cbset(perthread);
269     }
270
271     ovs_mutex_lock(&ovsrcu_threads_mutex);
272     list_remove(&perthread->list_node);
273     ovs_mutex_unlock(&ovsrcu_threads_mutex);
274
275     ovs_mutex_destroy(&perthread->mutex);
276     free(perthread);
277
278     seq_change(global_seqno);
279 }
280
281 static void
282 ovsrcu_thread_exit_cb(void *perthread)
283 {
284     ovsrcu_unregister__(perthread);
285 }
286
287 static void
288 ovsrcu_init(void)
289 {
290     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
291     if (ovsthread_once_start(&once)) {
292         global_seqno = seq_create();
293         xpthread_key_create(&perthread_key, ovsrcu_thread_exit_cb);
294         list_init(&ovsrcu_threads);
295         ovs_mutex_init(&ovsrcu_threads_mutex);
296
297         guarded_list_init(&flushed_cbsets);
298         flushed_cbsets_seq = seq_create();
299
300         ovsthread_once_done(&once);
301     }
302 }