2 * Copyright (c) 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "fatal-signal.h"
21 #include "guarded-list.h"
22 #include "openvswitch/list.h"
23 #include "ovs-thread.h"
24 #include "poll-loop.h"
28 #include "openvswitch/vlog.h"
30 VLOG_DEFINE_THIS_MODULE(ovs_rcu);
33 void (*function)(void *aux);
38 struct ovs_list list_node;
39 struct ovsrcu_cb cbs[16];
43 struct ovsrcu_perthread {
44 struct ovs_list list_node; /* In global list. */
46 struct ovs_mutex mutex;
48 struct ovsrcu_cbset *cbset;
49 char name[16]; /* This thread's name. */
52 static struct seq *global_seqno;
54 static pthread_key_t perthread_key;
55 static struct ovs_list ovsrcu_threads;
56 static struct ovs_mutex ovsrcu_threads_mutex;
58 static struct guarded_list flushed_cbsets;
59 static struct seq *flushed_cbsets_seq;
61 static void ovsrcu_init_module(void);
62 static void ovsrcu_flush_cbset__(struct ovsrcu_perthread *, bool);
63 static void ovsrcu_flush_cbset(struct ovsrcu_perthread *);
64 static void ovsrcu_unregister__(struct ovsrcu_perthread *);
65 static bool ovsrcu_call_postponed(void);
66 static void *ovsrcu_postpone_thread(void *arg OVS_UNUSED);
68 static struct ovsrcu_perthread *
69 ovsrcu_perthread_get(void)
71 struct ovsrcu_perthread *perthread;
75 perthread = pthread_getspecific(perthread_key);
77 const char *name = get_subprogram_name();
79 perthread = xmalloc(sizeof *perthread);
80 ovs_mutex_init(&perthread->mutex);
81 perthread->seqno = seq_read(global_seqno);
82 perthread->cbset = NULL;
83 ovs_strlcpy(perthread->name, name[0] ? name : "main",
84 sizeof perthread->name);
86 ovs_mutex_lock(&ovsrcu_threads_mutex);
87 ovs_list_push_back(&ovsrcu_threads, &perthread->list_node);
88 ovs_mutex_unlock(&ovsrcu_threads_mutex);
90 pthread_setspecific(perthread_key, perthread);
95 /* Indicates the end of a quiescent state. See "Details" near the top of
98 * Quiescent states don't stack or nest, so this always ends a quiescent state
99 * even if ovsrcu_quiesce_start() was called multiple times in a row. */
101 ovsrcu_quiesce_end(void)
103 ovsrcu_perthread_get();
107 ovsrcu_quiesced(void)
109 if (single_threaded()) {
110 ovsrcu_call_postponed();
112 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
113 if (ovsthread_once_start(&once)) {
114 ovs_thread_create("urcu", ovsrcu_postpone_thread, NULL);
115 ovsthread_once_done(&once);
120 /* Indicates the beginning of a quiescent state. See "Details" near the top of
123 ovsrcu_quiesce_start(void)
125 struct ovsrcu_perthread *perthread;
127 ovsrcu_init_module();
128 perthread = pthread_getspecific(perthread_key);
130 pthread_setspecific(perthread_key, NULL);
131 ovsrcu_unregister__(perthread);
137 /* Indicates a momentary quiescent state. See "Details" near the top of
140 * Provides a full memory barrier via seq_change().
145 struct ovsrcu_perthread *perthread;
147 perthread = ovsrcu_perthread_get();
148 perthread->seqno = seq_read(global_seqno);
149 if (perthread->cbset) {
150 ovsrcu_flush_cbset(perthread);
152 seq_change(global_seqno);
158 ovsrcu_try_quiesce(void)
160 struct ovsrcu_perthread *perthread;
163 ovs_assert(!single_threaded());
164 perthread = ovsrcu_perthread_get();
165 if (!seq_try_lock()) {
166 perthread->seqno = seq_read_protected(global_seqno);
167 if (perthread->cbset) {
168 ovsrcu_flush_cbset__(perthread, true);
170 seq_change_protected(global_seqno);
179 ovsrcu_is_quiescent(void)
181 ovsrcu_init_module();
182 return pthread_getspecific(perthread_key) == NULL;
186 ovsrcu_synchronize(void)
188 unsigned int warning_threshold = 1000;
189 uint64_t target_seqno;
192 if (single_threaded()) {
196 target_seqno = seq_read(global_seqno);
197 ovsrcu_quiesce_start();
201 uint64_t cur_seqno = seq_read(global_seqno);
202 struct ovsrcu_perthread *perthread;
203 char stalled_thread[16];
204 unsigned int elapsed;
207 ovs_mutex_lock(&ovsrcu_threads_mutex);
208 LIST_FOR_EACH (perthread, list_node, &ovsrcu_threads) {
209 if (perthread->seqno <= target_seqno) {
210 ovs_strlcpy(stalled_thread, perthread->name,
211 sizeof stalled_thread);
216 ovs_mutex_unlock(&ovsrcu_threads_mutex);
222 elapsed = time_msec() - start;
223 if (elapsed >= warning_threshold) {
224 VLOG_WARN("blocked %u ms waiting for %s to quiesce",
225 elapsed, stalled_thread);
226 warning_threshold *= 2;
228 poll_timer_wait_until(start + warning_threshold);
230 seq_wait(global_seqno, cur_seqno);
233 ovsrcu_quiesce_end();
236 /* Registers 'function' to be called, passing 'aux' as argument, after the
239 * The call is guaranteed to happen after the next time all participating
240 * threads have quiesced at least once, but there is no quarantee that all
241 * registered functions are called as early as possible, or that the functions
242 * registered by different threads would be called in the order the
243 * registrations took place. In particular, even if two threads provably
244 * register a function each in a specific order, the functions may still be
245 * called in the opposite order, depending on the timing of when the threads
246 * call ovsrcu_quiesce(), how many functions they postpone, and when the
247 * ovs-rcu thread happens to grab the functions to be called.
249 * All functions registered by a single thread are guaranteed to execute in the
250 * registering order, however.
252 * This function is more conveniently called through the ovsrcu_postpone()
253 * macro, which provides a type-safe way to allow 'function''s parameter to be
254 * any pointer type. */
256 ovsrcu_postpone__(void (*function)(void *aux), void *aux)
258 struct ovsrcu_perthread *perthread = ovsrcu_perthread_get();
259 struct ovsrcu_cbset *cbset;
260 struct ovsrcu_cb *cb;
262 cbset = perthread->cbset;
264 cbset = perthread->cbset = xmalloc(sizeof *perthread->cbset);
268 cb = &cbset->cbs[cbset->n_cbs++];
269 cb->function = function;
272 if (cbset->n_cbs >= ARRAY_SIZE(cbset->cbs)) {
273 ovsrcu_flush_cbset(perthread);
278 ovsrcu_call_postponed(void)
280 struct ovsrcu_cbset *cbset;
281 struct ovs_list cbsets;
283 guarded_list_pop_all(&flushed_cbsets, &cbsets);
284 if (ovs_list_is_empty(&cbsets)) {
288 ovsrcu_synchronize();
290 LIST_FOR_EACH_POP (cbset, list_node, &cbsets) {
291 struct ovsrcu_cb *cb;
293 for (cb = cbset->cbs; cb < &cbset->cbs[cbset->n_cbs]; cb++) {
294 cb->function(cb->aux);
303 ovsrcu_postpone_thread(void *arg OVS_UNUSED)
305 pthread_detach(pthread_self());
308 uint64_t seqno = seq_read(flushed_cbsets_seq);
309 if (!ovsrcu_call_postponed()) {
310 seq_wait(flushed_cbsets_seq, seqno);
319 ovsrcu_flush_cbset__(struct ovsrcu_perthread *perthread, bool protected)
321 struct ovsrcu_cbset *cbset = perthread->cbset;
324 guarded_list_push_back(&flushed_cbsets, &cbset->list_node, SIZE_MAX);
325 perthread->cbset = NULL;
328 seq_change_protected(flushed_cbsets_seq);
330 seq_change(flushed_cbsets_seq);
336 ovsrcu_flush_cbset(struct ovsrcu_perthread *perthread)
338 ovsrcu_flush_cbset__(perthread, false);
342 ovsrcu_unregister__(struct ovsrcu_perthread *perthread)
344 if (perthread->cbset) {
345 ovsrcu_flush_cbset(perthread);
348 ovs_mutex_lock(&ovsrcu_threads_mutex);
349 ovs_list_remove(&perthread->list_node);
350 ovs_mutex_unlock(&ovsrcu_threads_mutex);
352 ovs_mutex_destroy(&perthread->mutex);
355 seq_change(global_seqno);
359 ovsrcu_thread_exit_cb(void *perthread)
361 ovsrcu_unregister__(perthread);
364 /* Cancels the callback to ovsrcu_thread_exit_cb().
366 * Cancelling the call to the destructor during the main thread exit
367 * is needed while using pthreads-win32 library in Windows. It has been
368 * observed that in pthreads-win32, a call to the destructor during
369 * main thread exit causes undefined behavior. */
371 ovsrcu_cancel_thread_exit_cb(void *aux OVS_UNUSED)
373 pthread_setspecific(perthread_key, NULL);
377 ovsrcu_init_module(void)
379 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
380 if (ovsthread_once_start(&once)) {
381 global_seqno = seq_create();
382 xpthread_key_create(&perthread_key, ovsrcu_thread_exit_cb);
383 fatal_signal_add_hook(ovsrcu_cancel_thread_exit_cb, NULL, NULL, true);
384 ovs_list_init(&ovsrcu_threads);
385 ovs_mutex_init(&ovsrcu_threads_mutex);
387 guarded_list_init(&flushed_cbsets);
388 flushed_cbsets_seq = seq_create();
390 ovsthread_once_done(&once);