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 "guarded-list.h"
21 #include "ovs-thread.h"
22 #include "poll-loop.h"
26 void (*function)(void *aux);
31 struct list list_node;
32 struct ovsrcu_cb cbs[16];
36 struct ovsrcu_perthread {
37 struct list list_node; /* In global list. */
39 struct ovs_mutex mutex;
41 struct ovsrcu_cbset *cbset;
44 static struct seq *global_seqno;
46 static pthread_key_t perthread_key;
47 static struct list ovsrcu_threads;
48 static struct ovs_mutex ovsrcu_threads_mutex;
50 static struct guarded_list flushed_cbsets;
51 static struct seq *flushed_cbsets_seq;
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);
60 static struct ovsrcu_perthread *
61 ovsrcu_perthread_get(void)
63 struct ovsrcu_perthread *perthread;
67 perthread = pthread_getspecific(perthread_key);
69 perthread = xmalloc(sizeof *perthread);
70 ovs_mutex_init(&perthread->mutex);
71 perthread->seqno = seq_read(global_seqno);
72 perthread->cbset = NULL;
74 ovs_mutex_lock(&ovsrcu_threads_mutex);
75 list_push_back(&ovsrcu_threads, &perthread->list_node);
76 ovs_mutex_unlock(&ovsrcu_threads_mutex);
78 pthread_setspecific(perthread_key, perthread);
83 /* Indicates the end of a quiescent state. See "Details" near the top of
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. */
89 ovsrcu_quiesce_end(void)
91 ovsrcu_perthread_get();
97 if (single_threaded()) {
98 ovsrcu_call_postponed();
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);
108 /* Indicates the beginning of a quiescent state. See "Details" near the top of
111 ovsrcu_quiesce_start(void)
113 struct ovsrcu_perthread *perthread;
116 perthread = pthread_getspecific(perthread_key);
118 pthread_setspecific(perthread_key, NULL);
119 ovsrcu_unregister__(perthread);
125 /* Indicates a momentary quiescent state. See "Details" near the top of
131 ovsrcu_perthread_get()->seqno = seq_read(global_seqno);
132 seq_change(global_seqno);
138 ovsrcu_synchronize(void)
140 uint64_t target_seqno;
142 if (single_threaded()) {
146 target_seqno = seq_read(global_seqno);
147 ovsrcu_quiesce_start();
150 uint64_t cur_seqno = seq_read(global_seqno);
151 struct ovsrcu_perthread *perthread;
154 ovs_mutex_lock(&ovsrcu_threads_mutex);
155 LIST_FOR_EACH (perthread, list_node, &ovsrcu_threads) {
156 if (perthread->seqno <= target_seqno) {
161 ovs_mutex_unlock(&ovsrcu_threads_mutex);
167 seq_wait(global_seqno, cur_seqno);
170 ovsrcu_quiesce_end();
173 /* Registers 'function' to be called, passing 'aux' as argument, after the
176 * This function is more conveniently called through the ovsrcu_postpone()
177 * macro, which provides a type-safe way to allow 'function''s parameter to be
178 * any pointer type. */
180 ovsrcu_postpone__(void (*function)(void *aux), void *aux)
182 struct ovsrcu_perthread *perthread = ovsrcu_perthread_get();
183 struct ovsrcu_cbset *cbset;
184 struct ovsrcu_cb *cb;
186 cbset = perthread->cbset;
188 cbset = perthread->cbset = xmalloc(sizeof *perthread->cbset);
192 cb = &cbset->cbs[cbset->n_cbs++];
193 cb->function = function;
196 if (cbset->n_cbs >= ARRAY_SIZE(cbset->cbs)) {
197 ovsrcu_flush_cbset(perthread);
202 ovsrcu_call_postponed(void)
204 struct ovsrcu_cbset *cbset, *next_cbset;
207 guarded_list_pop_all(&flushed_cbsets, &cbsets);
208 if (list_is_empty(&cbsets)) {
212 ovsrcu_synchronize();
214 LIST_FOR_EACH_SAFE (cbset, next_cbset, list_node, &cbsets) {
215 struct ovsrcu_cb *cb;
217 for (cb = cbset->cbs; cb < &cbset->cbs[cbset->n_cbs]; cb++) {
218 cb->function(cb->aux);
220 list_remove(&cbset->list_node);
228 ovsrcu_postpone_thread(void *arg OVS_UNUSED)
230 pthread_detach(pthread_self());
233 uint64_t seqno = seq_read(flushed_cbsets_seq);
234 if (!ovsrcu_call_postponed()) {
235 seq_wait(flushed_cbsets_seq, seqno);
244 ovsrcu_flush_cbset(struct ovsrcu_perthread *perthread)
246 struct ovsrcu_cbset *cbset = perthread->cbset;
249 guarded_list_push_back(&flushed_cbsets, &cbset->list_node, SIZE_MAX);
250 perthread->cbset = NULL;
252 seq_change(flushed_cbsets_seq);
257 ovsrcu_unregister__(struct ovsrcu_perthread *perthread)
259 if (perthread->cbset) {
260 ovsrcu_flush_cbset(perthread);
263 ovs_mutex_lock(&ovsrcu_threads_mutex);
264 list_remove(&perthread->list_node);
265 ovs_mutex_unlock(&ovsrcu_threads_mutex);
267 ovs_mutex_destroy(&perthread->mutex);
270 seq_change(global_seqno);
274 ovsrcu_thread_exit_cb(void *perthread)
276 ovsrcu_unregister__(perthread);
282 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
283 if (ovsthread_once_start(&once)) {
284 global_seqno = seq_create();
285 xpthread_key_create(&perthread_key, ovsrcu_thread_exit_cb);
286 list_init(&ovsrcu_threads);
287 ovs_mutex_init(&ovsrcu_threads_mutex);
289 guarded_list_init(&flushed_cbsets);
290 flushed_cbsets_seq = seq_create();
292 ovsthread_once_done(&once);