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.
19 #include "fatal-signal.h"
20 #include "guarded-list.h"
22 #include "ovs-thread.h"
23 #include "poll-loop.h"
26 #include "openvswitch/vlog.h"
28 VLOG_DEFINE_THIS_MODULE(ovs_rcu
);
31 void (*function
)(void *aux
);
36 struct ovs_list list_node
;
37 struct ovsrcu_cb cbs
[16];
41 struct ovsrcu_perthread
{
42 struct ovs_list list_node
; /* In global list. */
44 struct ovs_mutex mutex
;
46 struct ovsrcu_cbset
*cbset
;
47 char name
[16]; /* This thread's name. */
50 static struct seq
*global_seqno
;
52 static pthread_key_t perthread_key
;
53 static struct ovs_list ovsrcu_threads
;
54 static struct ovs_mutex ovsrcu_threads_mutex
;
56 static struct guarded_list flushed_cbsets
;
57 static struct seq
*flushed_cbsets_seq
;
59 static void ovsrcu_init_module(void);
60 static void ovsrcu_flush_cbset(struct ovsrcu_perthread
*);
61 static void ovsrcu_unregister__(struct ovsrcu_perthread
*);
62 static bool ovsrcu_call_postponed(void);
63 static void *ovsrcu_postpone_thread(void *arg OVS_UNUSED
);
65 static struct ovsrcu_perthread
*
66 ovsrcu_perthread_get(void)
68 struct ovsrcu_perthread
*perthread
;
72 perthread
= pthread_getspecific(perthread_key
);
74 const char *name
= get_subprogram_name();
76 perthread
= xmalloc(sizeof *perthread
);
77 ovs_mutex_init(&perthread
->mutex
);
78 perthread
->seqno
= seq_read(global_seqno
);
79 perthread
->cbset
= NULL
;
80 ovs_strlcpy(perthread
->name
, name
[0] ? name
: "main",
81 sizeof perthread
->name
);
83 ovs_mutex_lock(&ovsrcu_threads_mutex
);
84 list_push_back(&ovsrcu_threads
, &perthread
->list_node
);
85 ovs_mutex_unlock(&ovsrcu_threads_mutex
);
87 pthread_setspecific(perthread_key
, perthread
);
92 /* Indicates the end of a quiescent state. See "Details" near the top of
95 * Quiescent states don't stack or nest, so this always ends a quiescent state
96 * even if ovsrcu_quiesce_start() was called multiple times in a row. */
98 ovsrcu_quiesce_end(void)
100 ovsrcu_perthread_get();
104 ovsrcu_quiesced(void)
106 if (single_threaded()) {
107 ovsrcu_call_postponed();
109 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
110 if (ovsthread_once_start(&once
)) {
111 ovs_thread_create("urcu", ovsrcu_postpone_thread
, NULL
);
112 ovsthread_once_done(&once
);
117 /* Indicates the beginning of a quiescent state. See "Details" near the top of
120 ovsrcu_quiesce_start(void)
122 struct ovsrcu_perthread
*perthread
;
124 ovsrcu_init_module();
125 perthread
= pthread_getspecific(perthread_key
);
127 pthread_setspecific(perthread_key
, NULL
);
128 ovsrcu_unregister__(perthread
);
134 /* Indicates a momentary quiescent state. See "Details" near the top of
137 * Provides a full memory barrier via seq_change().
142 struct ovsrcu_perthread
*perthread
;
144 perthread
= ovsrcu_perthread_get();
145 perthread
->seqno
= seq_read(global_seqno
);
146 if (perthread
->cbset
) {
147 ovsrcu_flush_cbset(perthread
);
149 seq_change(global_seqno
);
155 ovsrcu_is_quiescent(void)
157 ovsrcu_init_module();
158 return pthread_getspecific(perthread_key
) == NULL
;
162 ovsrcu_synchronize(void)
164 unsigned int warning_threshold
= 1000;
165 uint64_t target_seqno
;
168 if (single_threaded()) {
172 target_seqno
= seq_read(global_seqno
);
173 ovsrcu_quiesce_start();
177 uint64_t cur_seqno
= seq_read(global_seqno
);
178 struct ovsrcu_perthread
*perthread
;
179 char stalled_thread
[16];
180 unsigned int elapsed
;
183 ovs_mutex_lock(&ovsrcu_threads_mutex
);
184 LIST_FOR_EACH (perthread
, list_node
, &ovsrcu_threads
) {
185 if (perthread
->seqno
<= target_seqno
) {
186 ovs_strlcpy(stalled_thread
, perthread
->name
,
187 sizeof stalled_thread
);
192 ovs_mutex_unlock(&ovsrcu_threads_mutex
);
198 elapsed
= time_msec() - start
;
199 if (elapsed
>= warning_threshold
) {
200 VLOG_WARN("blocked %u ms waiting for %s to quiesce",
201 elapsed
, stalled_thread
);
202 warning_threshold
*= 2;
204 poll_timer_wait_until(start
+ warning_threshold
);
206 seq_wait(global_seqno
, cur_seqno
);
209 ovsrcu_quiesce_end();
212 /* Registers 'function' to be called, passing 'aux' as argument, after the
215 * This function is more conveniently called through the ovsrcu_postpone()
216 * macro, which provides a type-safe way to allow 'function''s parameter to be
217 * any pointer type. */
219 ovsrcu_postpone__(void (*function
)(void *aux
), void *aux
)
221 struct ovsrcu_perthread
*perthread
= ovsrcu_perthread_get();
222 struct ovsrcu_cbset
*cbset
;
223 struct ovsrcu_cb
*cb
;
225 cbset
= perthread
->cbset
;
227 cbset
= perthread
->cbset
= xmalloc(sizeof *perthread
->cbset
);
231 cb
= &cbset
->cbs
[cbset
->n_cbs
++];
232 cb
->function
= function
;
235 if (cbset
->n_cbs
>= ARRAY_SIZE(cbset
->cbs
)) {
236 ovsrcu_flush_cbset(perthread
);
241 ovsrcu_call_postponed(void)
243 struct ovsrcu_cbset
*cbset
;
244 struct ovs_list cbsets
;
246 guarded_list_pop_all(&flushed_cbsets
, &cbsets
);
247 if (list_is_empty(&cbsets
)) {
251 ovsrcu_synchronize();
253 LIST_FOR_EACH_POP (cbset
, list_node
, &cbsets
) {
254 struct ovsrcu_cb
*cb
;
256 for (cb
= cbset
->cbs
; cb
< &cbset
->cbs
[cbset
->n_cbs
]; cb
++) {
257 cb
->function(cb
->aux
);
266 ovsrcu_postpone_thread(void *arg OVS_UNUSED
)
268 pthread_detach(pthread_self());
271 uint64_t seqno
= seq_read(flushed_cbsets_seq
);
272 if (!ovsrcu_call_postponed()) {
273 seq_wait(flushed_cbsets_seq
, seqno
);
282 ovsrcu_flush_cbset(struct ovsrcu_perthread
*perthread
)
284 struct ovsrcu_cbset
*cbset
= perthread
->cbset
;
287 guarded_list_push_back(&flushed_cbsets
, &cbset
->list_node
, SIZE_MAX
);
288 perthread
->cbset
= NULL
;
290 seq_change(flushed_cbsets_seq
);
295 ovsrcu_unregister__(struct ovsrcu_perthread
*perthread
)
297 if (perthread
->cbset
) {
298 ovsrcu_flush_cbset(perthread
);
301 ovs_mutex_lock(&ovsrcu_threads_mutex
);
302 list_remove(&perthread
->list_node
);
303 ovs_mutex_unlock(&ovsrcu_threads_mutex
);
305 ovs_mutex_destroy(&perthread
->mutex
);
308 seq_change(global_seqno
);
312 ovsrcu_thread_exit_cb(void *perthread
)
314 ovsrcu_unregister__(perthread
);
317 /* Cancels the callback to ovsrcu_thread_exit_cb().
319 * Cancelling the call to the destructor during the main thread exit
320 * is needed while using pthreads-win32 library in Windows. It has been
321 * observed that in pthreads-win32, a call to the destructor during
322 * main thread exit causes undefined behavior. */
324 ovsrcu_cancel_thread_exit_cb(void *aux OVS_UNUSED
)
326 pthread_setspecific(perthread_key
, NULL
);
330 ovsrcu_init_module(void)
332 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
333 if (ovsthread_once_start(&once
)) {
334 global_seqno
= seq_create();
335 xpthread_key_create(&perthread_key
, ovsrcu_thread_exit_cb
);
336 fatal_signal_add_hook(ovsrcu_cancel_thread_exit_cb
, NULL
, NULL
, true);
337 list_init(&ovsrcu_threads
);
338 ovs_mutex_init(&ovsrcu_threads_mutex
);
340 guarded_list_init(&flushed_cbsets
);
341 flushed_cbsets_seq
= seq_create();
343 ovsthread_once_done(&once
);