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