2014-03-18 16:34:28 -07:00
|
|
|
/*
|
2017-04-05 22:49:27 -07:00
|
|
|
* Copyright (c) 2014, 2017 Nicira, Inc.
|
2014-03-18 16:34:28 -07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2016-07-05 10:33:38 -03:00
|
|
|
#include <errno.h>
|
2014-03-18 16:34:28 -07:00
|
|
|
#include "ovs-rcu.h"
|
2015-04-07 17:34:27 -07:00
|
|
|
#include "fatal-signal.h"
|
2014-03-18 16:34:28 -07:00
|
|
|
#include "guarded-list.h"
|
2018-01-25 15:39:48 -08:00
|
|
|
#include "latch.h"
|
2016-03-25 14:10:21 -07:00
|
|
|
#include "openvswitch/list.h"
|
2014-03-18 16:34:28 -07:00
|
|
|
#include "ovs-thread.h"
|
2017-11-03 13:53:53 +08:00
|
|
|
#include "openvswitch/poll-loop.h"
|
2014-03-18 16:34:28 -07:00
|
|
|
#include "seq.h"
|
2014-04-28 15:25:19 -07:00
|
|
|
#include "timeval.h"
|
2016-07-12 16:37:34 -05:00
|
|
|
#include "util.h"
|
2014-12-15 14:10:38 +01:00
|
|
|
#include "openvswitch/vlog.h"
|
2014-04-28 15:25:19 -07:00
|
|
|
|
|
|
|
VLOG_DEFINE_THIS_MODULE(ovs_rcu);
|
2014-03-18 16:34:28 -07:00
|
|
|
|
|
|
|
struct ovsrcu_cb {
|
|
|
|
void (*function)(void *aux);
|
|
|
|
void *aux;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ovsrcu_cbset {
|
2014-12-15 14:10:38 +01:00
|
|
|
struct ovs_list list_node;
|
2014-03-18 16:34:28 -07:00
|
|
|
struct ovsrcu_cb cbs[16];
|
|
|
|
int n_cbs;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ovsrcu_perthread {
|
2014-12-15 14:10:38 +01:00
|
|
|
struct ovs_list list_node; /* In global list. */
|
2014-03-18 16:34:28 -07:00
|
|
|
|
|
|
|
struct ovs_mutex mutex;
|
|
|
|
uint64_t seqno;
|
|
|
|
struct ovsrcu_cbset *cbset;
|
2014-04-28 15:25:19 -07:00
|
|
|
char name[16]; /* This thread's name. */
|
2014-03-18 16:34:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct seq *global_seqno;
|
|
|
|
|
|
|
|
static pthread_key_t perthread_key;
|
2014-12-15 14:10:38 +01:00
|
|
|
static struct ovs_list ovsrcu_threads;
|
2014-03-18 16:34:28 -07:00
|
|
|
static struct ovs_mutex ovsrcu_threads_mutex;
|
|
|
|
|
|
|
|
static struct guarded_list flushed_cbsets;
|
|
|
|
static struct seq *flushed_cbsets_seq;
|
|
|
|
|
2018-01-25 15:39:48 -08:00
|
|
|
static struct latch postpone_exit;
|
|
|
|
static struct ovs_barrier postpone_barrier;
|
|
|
|
|
2014-05-28 16:56:29 -07:00
|
|
|
static void ovsrcu_init_module(void);
|
2016-07-05 10:33:38 -03:00
|
|
|
static void ovsrcu_flush_cbset__(struct ovsrcu_perthread *, bool);
|
2014-03-18 16:34:28 -07:00
|
|
|
static void ovsrcu_flush_cbset(struct ovsrcu_perthread *);
|
|
|
|
static void ovsrcu_unregister__(struct ovsrcu_perthread *);
|
|
|
|
static bool ovsrcu_call_postponed(void);
|
|
|
|
static void *ovsrcu_postpone_thread(void *arg OVS_UNUSED);
|
|
|
|
|
|
|
|
static struct ovsrcu_perthread *
|
|
|
|
ovsrcu_perthread_get(void)
|
|
|
|
{
|
|
|
|
struct ovsrcu_perthread *perthread;
|
|
|
|
|
2014-05-28 16:56:29 -07:00
|
|
|
ovsrcu_init_module();
|
2014-03-18 16:34:28 -07:00
|
|
|
|
|
|
|
perthread = pthread_getspecific(perthread_key);
|
|
|
|
if (!perthread) {
|
2014-04-29 14:44:39 -07:00
|
|
|
const char *name = get_subprogram_name();
|
|
|
|
|
2014-03-18 16:34:28 -07:00
|
|
|
perthread = xmalloc(sizeof *perthread);
|
|
|
|
ovs_mutex_init(&perthread->mutex);
|
|
|
|
perthread->seqno = seq_read(global_seqno);
|
|
|
|
perthread->cbset = NULL;
|
2014-04-29 14:44:39 -07:00
|
|
|
ovs_strlcpy(perthread->name, name[0] ? name : "main",
|
2014-04-28 15:25:19 -07:00
|
|
|
sizeof perthread->name);
|
2014-03-18 16:34:28 -07:00
|
|
|
|
|
|
|
ovs_mutex_lock(&ovsrcu_threads_mutex);
|
2016-03-25 14:10:22 -07:00
|
|
|
ovs_list_push_back(&ovsrcu_threads, &perthread->list_node);
|
2014-03-18 16:34:28 -07:00
|
|
|
ovs_mutex_unlock(&ovsrcu_threads_mutex);
|
|
|
|
|
|
|
|
pthread_setspecific(perthread_key, perthread);
|
|
|
|
}
|
|
|
|
return perthread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Indicates the end of a quiescent state. See "Details" near the top of
|
|
|
|
* ovs-rcu.h.
|
|
|
|
*
|
|
|
|
* Quiescent states don't stack or nest, so this always ends a quiescent state
|
|
|
|
* even if ovsrcu_quiesce_start() was called multiple times in a row. */
|
|
|
|
void
|
|
|
|
ovsrcu_quiesce_end(void)
|
|
|
|
{
|
|
|
|
ovsrcu_perthread_get();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ovsrcu_quiesced(void)
|
|
|
|
{
|
|
|
|
if (single_threaded()) {
|
|
|
|
ovsrcu_call_postponed();
|
|
|
|
} else {
|
|
|
|
static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
|
|
|
|
if (ovsthread_once_start(&once)) {
|
2018-01-25 15:39:48 -08:00
|
|
|
latch_init(&postpone_exit);
|
|
|
|
ovs_barrier_init(&postpone_barrier, 2);
|
ovs-thread: Make caller provide thread name when creating a thread.
Thread names are occasionally very useful for debugging, but from time to
time we've forgotten to set one. This commit adds the new thread's name
as a parameter to the function to start a thread, to make that mistake
impossible. This also simplifies code, since two function calls become
only one.
This makes a few other changes to the thread creation function:
* Since it is no longer a direct wrapper around a pthread function,
rename it to avoid giving that impression.
* Remove 'pthread_attr_t *' param that every caller supplied as NULL.
* Change 'pthread *' parameter into a return value, for convenience.
The system-stats code hadn't set a thread name, so this fixes that issue.
This patch is a prerequisite for making RCU report the name of a thread
that is blocking RCU synchronization, because the easiest way to do that is
for ovsrcu_quiesce_end() to record the current thread's name.
ovsrcu_quiesce_end() is called before the thread function is called, so it
won't get a name set within the thread function itself. Setting the thread
name earlier, as in this patch, avoids the problem.
Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Alex Wang <alexw@nicira.com>
2014-04-25 17:46:21 -07:00
|
|
|
ovs_thread_create("urcu", ovsrcu_postpone_thread, NULL);
|
2014-03-18 16:34:28 -07:00
|
|
|
ovsthread_once_done(&once);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Indicates the beginning of a quiescent state. See "Details" near the top of
|
|
|
|
* ovs-rcu.h. */
|
|
|
|
void
|
|
|
|
ovsrcu_quiesce_start(void)
|
|
|
|
{
|
|
|
|
struct ovsrcu_perthread *perthread;
|
|
|
|
|
2014-05-28 16:56:29 -07:00
|
|
|
ovsrcu_init_module();
|
2014-03-18 16:34:28 -07:00
|
|
|
perthread = pthread_getspecific(perthread_key);
|
|
|
|
if (perthread) {
|
|
|
|
pthread_setspecific(perthread_key, NULL);
|
|
|
|
ovsrcu_unregister__(perthread);
|
|
|
|
}
|
|
|
|
|
|
|
|
ovsrcu_quiesced();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Indicates a momentary quiescent state. See "Details" near the top of
|
lib/ovs-atomic: Add ovs_refcount_unref_relaxed(), ovs_refcount_try_ref_rcu().
When a reference counted object is also RCU protected the deletion of
the object's memory is always postponed. This allows
memory_order_relaxed to be used also for unreferencing, as RCU
quiescing provides a full memory barrier (it has to, or otherwise
there could be lingering accesses to objects after they are recycled).
Also, when access to the reference counted object is protected via a
mutex or a lock, the locking primitives provide the required memory
barrier functionality.
Also, add ovs_refcount_try_ref_rcu(), which takes a reference only if
the refcount is non-zero and returns true if a reference was taken,
false otherwise. This can be used in combined RCU/refcount scenarios
where we have an RCU protected reference to an refcounted object, but
which may be unref'ed at any time. If ovs_refcount_try_ref_rcu()
fails, the object may still be safely used until the current thread
quiesces.
Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2014-07-07 13:18:46 -07:00
|
|
|
* ovs-rcu.h.
|
|
|
|
*
|
|
|
|
* Provides a full memory barrier via seq_change().
|
|
|
|
*/
|
2014-03-18 16:34:28 -07:00
|
|
|
void
|
|
|
|
ovsrcu_quiesce(void)
|
|
|
|
{
|
2014-09-09 11:01:52 -07:00
|
|
|
struct ovsrcu_perthread *perthread;
|
|
|
|
|
|
|
|
perthread = ovsrcu_perthread_get();
|
|
|
|
perthread->seqno = seq_read(global_seqno);
|
|
|
|
if (perthread->cbset) {
|
|
|
|
ovsrcu_flush_cbset(perthread);
|
|
|
|
}
|
2014-03-18 16:34:28 -07:00
|
|
|
seq_change(global_seqno);
|
|
|
|
|
|
|
|
ovsrcu_quiesced();
|
|
|
|
}
|
|
|
|
|
2016-07-05 10:33:38 -03:00
|
|
|
int
|
|
|
|
ovsrcu_try_quiesce(void)
|
|
|
|
{
|
|
|
|
struct ovsrcu_perthread *perthread;
|
|
|
|
int ret = EBUSY;
|
|
|
|
|
|
|
|
ovs_assert(!single_threaded());
|
|
|
|
perthread = ovsrcu_perthread_get();
|
|
|
|
if (!seq_try_lock()) {
|
|
|
|
perthread->seqno = seq_read_protected(global_seqno);
|
|
|
|
if (perthread->cbset) {
|
|
|
|
ovsrcu_flush_cbset__(perthread, true);
|
|
|
|
}
|
|
|
|
seq_change_protected(global_seqno);
|
|
|
|
seq_unlock();
|
|
|
|
ovsrcu_quiesced();
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-25 18:25:06 -07:00
|
|
|
bool
|
|
|
|
ovsrcu_is_quiescent(void)
|
|
|
|
{
|
2014-05-28 16:56:29 -07:00
|
|
|
ovsrcu_init_module();
|
2014-04-25 18:25:06 -07:00
|
|
|
return pthread_getspecific(perthread_key) == NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-11 02:29:07 -07:00
|
|
|
void
|
2014-03-18 16:34:28 -07:00
|
|
|
ovsrcu_synchronize(void)
|
|
|
|
{
|
2014-04-28 15:25:19 -07:00
|
|
|
unsigned int warning_threshold = 1000;
|
2014-03-18 16:34:28 -07:00
|
|
|
uint64_t target_seqno;
|
2014-04-28 15:25:19 -07:00
|
|
|
long long int start;
|
2014-03-18 16:34:28 -07:00
|
|
|
|
|
|
|
if (single_threaded()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
target_seqno = seq_read(global_seqno);
|
|
|
|
ovsrcu_quiesce_start();
|
2014-04-28 15:25:19 -07:00
|
|
|
start = time_msec();
|
2014-03-18 16:34:28 -07:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
uint64_t cur_seqno = seq_read(global_seqno);
|
|
|
|
struct ovsrcu_perthread *perthread;
|
2014-04-28 15:25:19 -07:00
|
|
|
char stalled_thread[16];
|
|
|
|
unsigned int elapsed;
|
2014-03-18 16:34:28 -07:00
|
|
|
bool done = true;
|
|
|
|
|
|
|
|
ovs_mutex_lock(&ovsrcu_threads_mutex);
|
|
|
|
LIST_FOR_EACH (perthread, list_node, &ovsrcu_threads) {
|
|
|
|
if (perthread->seqno <= target_seqno) {
|
2017-04-05 22:49:27 -07:00
|
|
|
ovs_strlcpy_arrays(stalled_thread, perthread->name);
|
2014-03-18 16:34:28 -07:00
|
|
|
done = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ovs_mutex_unlock(&ovsrcu_threads_mutex);
|
|
|
|
|
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-28 15:25:19 -07:00
|
|
|
elapsed = time_msec() - start;
|
|
|
|
if (elapsed >= warning_threshold) {
|
|
|
|
VLOG_WARN("blocked %u ms waiting for %s to quiesce",
|
|
|
|
elapsed, stalled_thread);
|
|
|
|
warning_threshold *= 2;
|
|
|
|
}
|
|
|
|
poll_timer_wait_until(start + warning_threshold);
|
|
|
|
|
2014-03-18 16:34:28 -07:00
|
|
|
seq_wait(global_seqno, cur_seqno);
|
|
|
|
poll_block();
|
|
|
|
}
|
|
|
|
ovsrcu_quiesce_end();
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:39:48 -08:00
|
|
|
/* Waits until as many postponed callbacks as possible have executed.
|
|
|
|
*
|
|
|
|
* As a side effect, stops the background thread that calls the callbacks and
|
|
|
|
* prevents it from being restarted. This means that this function should only
|
|
|
|
* be called soon before a process exits, as a mechanism for releasing memory
|
|
|
|
* to make memory leaks easier to detect, since any further postponed callbacks
|
|
|
|
* won't actually get called.
|
|
|
|
*
|
|
|
|
* This function can only wait for callbacks registered by the current thread
|
|
|
|
* and the background thread that calls the callbacks. Thus, it will be most
|
|
|
|
* effective if other threads have already exited. */
|
|
|
|
void
|
|
|
|
ovsrcu_exit(void)
|
|
|
|
{
|
|
|
|
/* Stop the postpone thread and wait for it to exit. Otherwise, there's no
|
|
|
|
* way to wait for that thread to finish calling callbacks itself. */
|
|
|
|
if (!single_threaded()) {
|
|
|
|
ovsrcu_quiesced(); /* Ensure that the postpone thread exists. */
|
|
|
|
latch_set(&postpone_exit);
|
|
|
|
ovs_barrier_block(&postpone_barrier);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Repeatedly:
|
|
|
|
*
|
|
|
|
* - Wait for a grace period. One important side effect is to push the
|
|
|
|
* running thread's cbset into 'flushed_cbsets' so that the next call
|
|
|
|
* has something to call.
|
|
|
|
*
|
|
|
|
* - Call all the callbacks in 'flushed_cbsets'. If there aren't any,
|
|
|
|
* we're done, otherwise the callbacks themselves might have requested
|
|
|
|
* more deferred callbacks so we go around again.
|
|
|
|
*
|
|
|
|
* We limit the number of iterations just in case some bug causes an
|
|
|
|
* infinite loop. This function is just for making memory leaks easier to
|
|
|
|
* spot so there's no point in breaking things on that basis. */
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
ovsrcu_synchronize();
|
|
|
|
if (!ovsrcu_call_postponed()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 16:34:28 -07:00
|
|
|
/* Registers 'function' to be called, passing 'aux' as argument, after the
|
|
|
|
* next grace period.
|
|
|
|
*
|
2015-06-11 17:28:37 -07:00
|
|
|
* The call is guaranteed to happen after the next time all participating
|
|
|
|
* threads have quiesced at least once, but there is no quarantee that all
|
|
|
|
* registered functions are called as early as possible, or that the functions
|
|
|
|
* registered by different threads would be called in the order the
|
|
|
|
* registrations took place. In particular, even if two threads provably
|
|
|
|
* register a function each in a specific order, the functions may still be
|
|
|
|
* called in the opposite order, depending on the timing of when the threads
|
|
|
|
* call ovsrcu_quiesce(), how many functions they postpone, and when the
|
|
|
|
* ovs-rcu thread happens to grab the functions to be called.
|
|
|
|
*
|
|
|
|
* All functions registered by a single thread are guaranteed to execute in the
|
|
|
|
* registering order, however.
|
|
|
|
*
|
2014-03-18 16:34:28 -07:00
|
|
|
* This function is more conveniently called through the ovsrcu_postpone()
|
|
|
|
* macro, which provides a type-safe way to allow 'function''s parameter to be
|
|
|
|
* any pointer type. */
|
|
|
|
void
|
|
|
|
ovsrcu_postpone__(void (*function)(void *aux), void *aux)
|
|
|
|
{
|
|
|
|
struct ovsrcu_perthread *perthread = ovsrcu_perthread_get();
|
|
|
|
struct ovsrcu_cbset *cbset;
|
|
|
|
struct ovsrcu_cb *cb;
|
|
|
|
|
|
|
|
cbset = perthread->cbset;
|
|
|
|
if (!cbset) {
|
|
|
|
cbset = perthread->cbset = xmalloc(sizeof *perthread->cbset);
|
|
|
|
cbset->n_cbs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb = &cbset->cbs[cbset->n_cbs++];
|
|
|
|
cb->function = function;
|
|
|
|
cb->aux = aux;
|
|
|
|
|
|
|
|
if (cbset->n_cbs >= ARRAY_SIZE(cbset->cbs)) {
|
|
|
|
ovsrcu_flush_cbset(perthread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
ovsrcu_call_postponed(void)
|
|
|
|
{
|
2015-04-06 14:02:28 -07:00
|
|
|
struct ovsrcu_cbset *cbset;
|
2014-12-15 14:10:38 +01:00
|
|
|
struct ovs_list cbsets;
|
2014-03-18 16:34:28 -07:00
|
|
|
|
|
|
|
guarded_list_pop_all(&flushed_cbsets, &cbsets);
|
2016-03-25 14:10:22 -07:00
|
|
|
if (ovs_list_is_empty(&cbsets)) {
|
2014-03-18 16:34:28 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ovsrcu_synchronize();
|
|
|
|
|
2015-04-06 14:02:28 -07:00
|
|
|
LIST_FOR_EACH_POP (cbset, list_node, &cbsets) {
|
2014-03-18 16:34:28 -07:00
|
|
|
struct ovsrcu_cb *cb;
|
|
|
|
|
|
|
|
for (cb = cbset->cbs; cb < &cbset->cbs[cbset->n_cbs]; cb++) {
|
|
|
|
cb->function(cb->aux);
|
|
|
|
}
|
|
|
|
free(cbset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
ovsrcu_postpone_thread(void *arg OVS_UNUSED)
|
|
|
|
{
|
|
|
|
pthread_detach(pthread_self());
|
|
|
|
|
2018-01-25 15:39:48 -08:00
|
|
|
while (!latch_is_set(&postpone_exit)) {
|
2014-03-18 16:34:28 -07:00
|
|
|
uint64_t seqno = seq_read(flushed_cbsets_seq);
|
|
|
|
if (!ovsrcu_call_postponed()) {
|
|
|
|
seq_wait(flushed_cbsets_seq, seqno);
|
2018-01-25 15:39:48 -08:00
|
|
|
latch_wait(&postpone_exit);
|
2014-03-18 16:34:28 -07:00
|
|
|
poll_block();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:39:48 -08:00
|
|
|
ovs_barrier_block(&postpone_barrier);
|
|
|
|
return NULL;
|
2014-03-18 16:34:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-07-05 10:33:38 -03:00
|
|
|
ovsrcu_flush_cbset__(struct ovsrcu_perthread *perthread, bool protected)
|
2014-03-18 16:34:28 -07:00
|
|
|
{
|
|
|
|
struct ovsrcu_cbset *cbset = perthread->cbset;
|
|
|
|
|
|
|
|
if (cbset) {
|
|
|
|
guarded_list_push_back(&flushed_cbsets, &cbset->list_node, SIZE_MAX);
|
|
|
|
perthread->cbset = NULL;
|
|
|
|
|
2016-07-05 10:33:38 -03:00
|
|
|
if (protected) {
|
|
|
|
seq_change_protected(flushed_cbsets_seq);
|
|
|
|
} else {
|
|
|
|
seq_change(flushed_cbsets_seq);
|
|
|
|
}
|
2014-03-18 16:34:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-05 10:33:38 -03:00
|
|
|
static void
|
|
|
|
ovsrcu_flush_cbset(struct ovsrcu_perthread *perthread)
|
|
|
|
{
|
|
|
|
ovsrcu_flush_cbset__(perthread, false);
|
|
|
|
}
|
|
|
|
|
2014-03-18 16:34:28 -07:00
|
|
|
static void
|
|
|
|
ovsrcu_unregister__(struct ovsrcu_perthread *perthread)
|
|
|
|
{
|
|
|
|
if (perthread->cbset) {
|
|
|
|
ovsrcu_flush_cbset(perthread);
|
|
|
|
}
|
|
|
|
|
|
|
|
ovs_mutex_lock(&ovsrcu_threads_mutex);
|
2016-03-25 14:10:22 -07:00
|
|
|
ovs_list_remove(&perthread->list_node);
|
2014-03-18 16:34:28 -07:00
|
|
|
ovs_mutex_unlock(&ovsrcu_threads_mutex);
|
|
|
|
|
|
|
|
ovs_mutex_destroy(&perthread->mutex);
|
|
|
|
free(perthread);
|
|
|
|
|
|
|
|
seq_change(global_seqno);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ovsrcu_thread_exit_cb(void *perthread)
|
|
|
|
{
|
|
|
|
ovsrcu_unregister__(perthread);
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:34:27 -07:00
|
|
|
/* Cancels the callback to ovsrcu_thread_exit_cb().
|
|
|
|
*
|
|
|
|
* Cancelling the call to the destructor during the main thread exit
|
|
|
|
* is needed while using pthreads-win32 library in Windows. It has been
|
|
|
|
* observed that in pthreads-win32, a call to the destructor during
|
|
|
|
* main thread exit causes undefined behavior. */
|
|
|
|
static void
|
|
|
|
ovsrcu_cancel_thread_exit_cb(void *aux OVS_UNUSED)
|
|
|
|
{
|
|
|
|
pthread_setspecific(perthread_key, NULL);
|
|
|
|
}
|
|
|
|
|
2014-03-18 16:34:28 -07:00
|
|
|
static void
|
2014-05-28 16:56:29 -07:00
|
|
|
ovsrcu_init_module(void)
|
2014-03-18 16:34:28 -07:00
|
|
|
{
|
|
|
|
static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
|
|
|
|
if (ovsthread_once_start(&once)) {
|
|
|
|
global_seqno = seq_create();
|
|
|
|
xpthread_key_create(&perthread_key, ovsrcu_thread_exit_cb);
|
2015-04-07 17:34:27 -07:00
|
|
|
fatal_signal_add_hook(ovsrcu_cancel_thread_exit_cb, NULL, NULL, true);
|
2016-03-25 14:10:22 -07:00
|
|
|
ovs_list_init(&ovsrcu_threads);
|
2014-03-18 16:34:28 -07:00
|
|
|
ovs_mutex_init(&ovsrcu_threads_mutex);
|
|
|
|
|
|
|
|
guarded_list_init(&flushed_cbsets);
|
|
|
|
flushed_cbsets_seq = seq_create();
|
|
|
|
|
|
|
|
ovsthread_once_done(&once);
|
|
|
|
}
|
|
|
|
}
|