Add more files to the openvswitch library on MSVC
[cascardo/ovs.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 #include "timeval.h"
25 #include "vlog.h"
26
27 VLOG_DEFINE_THIS_MODULE(ovs_rcu);
28
29 struct ovsrcu_cb {
30     void (*function)(void *aux);
31     void *aux;
32 };
33
34 struct ovsrcu_cbset {
35     struct list list_node;
36     struct ovsrcu_cb cbs[16];
37     int n_cbs;
38 };
39
40 struct ovsrcu_perthread {
41     struct list list_node;      /* In global list. */
42
43     struct ovs_mutex mutex;
44     uint64_t seqno;
45     struct ovsrcu_cbset *cbset;
46     char name[16];              /* This thread's name. */
47 };
48
49 static struct seq *global_seqno;
50
51 static pthread_key_t perthread_key;
52 static struct list ovsrcu_threads;
53 static struct ovs_mutex ovsrcu_threads_mutex;
54
55 static struct guarded_list flushed_cbsets;
56 static struct seq *flushed_cbsets_seq;
57
58 static void ovsrcu_init_module(void);
59 static void ovsrcu_flush_cbset(struct ovsrcu_perthread *);
60 static void ovsrcu_unregister__(struct ovsrcu_perthread *);
61 static bool ovsrcu_call_postponed(void);
62 static void *ovsrcu_postpone_thread(void *arg OVS_UNUSED);
63
64 static struct ovsrcu_perthread *
65 ovsrcu_perthread_get(void)
66 {
67     struct ovsrcu_perthread *perthread;
68
69     ovsrcu_init_module();
70
71     perthread = pthread_getspecific(perthread_key);
72     if (!perthread) {
73         const char *name = get_subprogram_name();
74
75         perthread = xmalloc(sizeof *perthread);
76         ovs_mutex_init(&perthread->mutex);
77         perthread->seqno = seq_read(global_seqno);
78         perthread->cbset = NULL;
79         ovs_strlcpy(perthread->name, name[0] ? name : "main",
80                     sizeof perthread->name);
81
82         ovs_mutex_lock(&ovsrcu_threads_mutex);
83         list_push_back(&ovsrcu_threads, &perthread->list_node);
84         ovs_mutex_unlock(&ovsrcu_threads_mutex);
85
86         pthread_setspecific(perthread_key, perthread);
87     }
88     return perthread;
89 }
90
91 /* Indicates the end of a quiescent state.  See "Details" near the top of
92  * ovs-rcu.h.
93  *
94  * Quiescent states don't stack or nest, so this always ends a quiescent state
95  * even if ovsrcu_quiesce_start() was called multiple times in a row. */
96 void
97 ovsrcu_quiesce_end(void)
98 {
99     ovsrcu_perthread_get();
100 }
101
102 static void
103 ovsrcu_quiesced(void)
104 {
105     if (single_threaded()) {
106         ovsrcu_call_postponed();
107     } else {
108         static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
109         if (ovsthread_once_start(&once)) {
110             ovs_thread_create("urcu", ovsrcu_postpone_thread, NULL);
111             ovsthread_once_done(&once);
112         }
113     }
114 }
115
116 /* Indicates the beginning of a quiescent state.  See "Details" near the top of
117  * ovs-rcu.h. */
118 void
119 ovsrcu_quiesce_start(void)
120 {
121     struct ovsrcu_perthread *perthread;
122
123     ovsrcu_init_module();
124     perthread = pthread_getspecific(perthread_key);
125     if (perthread) {
126         pthread_setspecific(perthread_key, NULL);
127         ovsrcu_unregister__(perthread);
128     }
129
130     ovsrcu_quiesced();
131 }
132
133 /* Indicates a momentary quiescent state.  See "Details" near the top of
134  * ovs-rcu.h.
135  *
136  * Provides a full memory barrier via seq_change().
137  */
138 void
139 ovsrcu_quiesce(void)
140 {
141     ovsrcu_init_module();
142     ovsrcu_perthread_get()->seqno = seq_read(global_seqno);
143     seq_change(global_seqno);
144
145     ovsrcu_quiesced();
146 }
147
148 bool
149 ovsrcu_is_quiescent(void)
150 {
151     ovsrcu_init_module();
152     return pthread_getspecific(perthread_key) == NULL;
153 }
154
155 void
156 ovsrcu_synchronize(void)
157 {
158     unsigned int warning_threshold = 1000;
159     uint64_t target_seqno;
160     long long int start;
161
162     if (single_threaded()) {
163         return;
164     }
165
166     target_seqno = seq_read(global_seqno);
167     ovsrcu_quiesce_start();
168     start = time_msec();
169
170     for (;;) {
171         uint64_t cur_seqno = seq_read(global_seqno);
172         struct ovsrcu_perthread *perthread;
173         char stalled_thread[16];
174         unsigned int elapsed;
175         bool done = true;
176
177         ovs_mutex_lock(&ovsrcu_threads_mutex);
178         LIST_FOR_EACH (perthread, list_node, &ovsrcu_threads) {
179             if (perthread->seqno <= target_seqno) {
180                 ovs_strlcpy(stalled_thread, perthread->name,
181                             sizeof stalled_thread);
182                 done = false;
183                 break;
184             }
185         }
186         ovs_mutex_unlock(&ovsrcu_threads_mutex);
187
188         if (done) {
189             break;
190         }
191
192         elapsed = time_msec() - start;
193         if (elapsed >= warning_threshold) {
194             VLOG_WARN("blocked %u ms waiting for %s to quiesce",
195                       elapsed, stalled_thread);
196             warning_threshold *= 2;
197         }
198         poll_timer_wait_until(start + warning_threshold);
199
200         seq_wait(global_seqno, cur_seqno);
201         poll_block();
202     }
203     ovsrcu_quiesce_end();
204 }
205
206 /* Registers 'function' to be called, passing 'aux' as argument, after the
207  * next grace period.
208  *
209  * This function is more conveniently called through the ovsrcu_postpone()
210  * macro, which provides a type-safe way to allow 'function''s parameter to be
211  * any pointer type. */
212 void
213 ovsrcu_postpone__(void (*function)(void *aux), void *aux)
214 {
215     struct ovsrcu_perthread *perthread = ovsrcu_perthread_get();
216     struct ovsrcu_cbset *cbset;
217     struct ovsrcu_cb *cb;
218
219     cbset = perthread->cbset;
220     if (!cbset) {
221         cbset = perthread->cbset = xmalloc(sizeof *perthread->cbset);
222         cbset->n_cbs = 0;
223     }
224
225     cb = &cbset->cbs[cbset->n_cbs++];
226     cb->function = function;
227     cb->aux = aux;
228
229     if (cbset->n_cbs >= ARRAY_SIZE(cbset->cbs)) {
230         ovsrcu_flush_cbset(perthread);
231     }
232 }
233
234 static bool
235 ovsrcu_call_postponed(void)
236 {
237     struct ovsrcu_cbset *cbset, *next_cbset;
238     struct list cbsets;
239
240     guarded_list_pop_all(&flushed_cbsets, &cbsets);
241     if (list_is_empty(&cbsets)) {
242         return false;
243     }
244
245     ovsrcu_synchronize();
246
247     LIST_FOR_EACH_SAFE (cbset, next_cbset, list_node, &cbsets) {
248         struct ovsrcu_cb *cb;
249
250         for (cb = cbset->cbs; cb < &cbset->cbs[cbset->n_cbs]; cb++) {
251             cb->function(cb->aux);
252         }
253         list_remove(&cbset->list_node);
254         free(cbset);
255     }
256
257     return true;
258 }
259
260 static void *
261 ovsrcu_postpone_thread(void *arg OVS_UNUSED)
262 {
263     pthread_detach(pthread_self());
264
265     for (;;) {
266         uint64_t seqno = seq_read(flushed_cbsets_seq);
267         if (!ovsrcu_call_postponed()) {
268             seq_wait(flushed_cbsets_seq, seqno);
269             poll_block();
270         }
271     }
272
273     OVS_NOT_REACHED();
274 }
275
276 static void
277 ovsrcu_flush_cbset(struct ovsrcu_perthread *perthread)
278 {
279     struct ovsrcu_cbset *cbset = perthread->cbset;
280
281     if (cbset) {
282         guarded_list_push_back(&flushed_cbsets, &cbset->list_node, SIZE_MAX);
283         perthread->cbset = NULL;
284
285         seq_change(flushed_cbsets_seq);
286     }
287 }
288
289 static void
290 ovsrcu_unregister__(struct ovsrcu_perthread *perthread)
291 {
292     if (perthread->cbset) {
293         ovsrcu_flush_cbset(perthread);
294     }
295
296     ovs_mutex_lock(&ovsrcu_threads_mutex);
297     list_remove(&perthread->list_node);
298     ovs_mutex_unlock(&ovsrcu_threads_mutex);
299
300     ovs_mutex_destroy(&perthread->mutex);
301     free(perthread);
302
303     seq_change(global_seqno);
304 }
305
306 static void
307 ovsrcu_thread_exit_cb(void *perthread)
308 {
309     ovsrcu_unregister__(perthread);
310 }
311
312 static void
313 ovsrcu_init_module(void)
314 {
315     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
316     if (ovsthread_once_start(&once)) {
317         global_seqno = seq_create();
318         xpthread_key_create(&perthread_key, ovsrcu_thread_exit_cb);
319         list_init(&ovsrcu_threads);
320         ovs_mutex_init(&ovsrcu_threads_mutex);
321
322         guarded_list_init(&flushed_cbsets);
323         flushed_cbsets_seq = seq_create();
324
325         ovsthread_once_done(&once);
326     }
327 }