]>
git.proxmox.com Git - mirror_ovs.git/blob - include/openvswitch/thread.h
2 * Copyright (c) 2013, 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.
17 #ifndef OPENVSWITCH_THREAD_H
18 #define OPENVSWITCH_THREAD_H 1
23 #include <sys/types.h>
24 #include "openvswitch/compiler.h"
27 struct OVS_LOCKABLE ovs_mutex
{
29 const char *where
; /* NULL if and only if uninitialized. */
32 /* "struct ovs_mutex" initializer. */
33 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
34 #define OVS_MUTEX_INITIALIZER { PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, \
37 #define OVS_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, "<unlocked>" }
40 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
41 #define OVS_ADAPTIVE_MUTEX_INITIALIZER \
42 { PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, "<unlocked>" }
44 #define OVS_ADAPTIVE_MUTEX_INITIALIZER OVS_MUTEX_INITIALIZER
47 /* ovs_mutex functions analogous to pthread_mutex_*() functions.
49 * Most of these functions abort the process with an error message on any
50 * error. ovs_mutex_trylock() is an exception: it passes through a 0 or EBUSY
51 * return value to the caller and aborts on any other error. */
52 void ovs_mutex_init(const struct ovs_mutex
*);
53 void ovs_mutex_init_recursive(const struct ovs_mutex
*);
54 void ovs_mutex_init_adaptive(const struct ovs_mutex
*);
55 void ovs_mutex_destroy(const struct ovs_mutex
*);
56 void ovs_mutex_unlock(const struct ovs_mutex
*mutex
) OVS_RELEASES(mutex
);
57 void ovs_mutex_lock_at(const struct ovs_mutex
*mutex
, const char *where
)
59 #define ovs_mutex_lock(mutex) \
60 ovs_mutex_lock_at(mutex, OVS_SOURCE_LOCATOR)
62 int ovs_mutex_trylock_at(const struct ovs_mutex
*mutex
, const char *where
)
63 OVS_TRY_LOCK(0, mutex
);
64 #define ovs_mutex_trylock(mutex) \
65 ovs_mutex_trylock_at(mutex, OVS_SOURCE_LOCATOR)
67 void ovs_mutex_cond_wait(pthread_cond_t
*, const struct ovs_mutex
*);
69 /* Convenient once-only execution.
75 * POSIX provides pthread_once_t and pthread_once() as primitives for running a
76 * set of code only once per process execution. They are used like this:
78 * static void run_once(void) { ...initialization... }
79 * static pthread_once_t once = PTHREAD_ONCE_INIT;
81 * pthread_once(&once, run_once);
83 * pthread_once() does not allow passing any parameters to the initialization
84 * function, which is often inconvenient, because it means that the function
85 * can only access data declared at file scope.
91 * Use ovsthread_once, like this, instead:
93 * static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
95 * if (ovsthread_once_start(&once)) {
96 * ...initialization...
97 * ovsthread_once_done(&once);
101 struct ovsthread_once
{
102 bool done
; /* Non-atomic, false negatives possible. */
103 struct ovs_mutex mutex
;
106 #define OVSTHREAD_ONCE_INITIALIZER \
109 OVS_MUTEX_INITIALIZER, \
112 static inline bool ovsthread_once_start(struct ovsthread_once
*once
)
113 OVS_TRY_LOCK(true, once
->mutex
);
114 void ovsthread_once_done(struct ovsthread_once
*once
)
115 OVS_RELEASES(once
->mutex
);
117 bool ovsthread_once_start__(struct ovsthread_once
*once
)
118 OVS_TRY_LOCK(true, once
->mutex
);
120 /* Returns true if this is the first call to ovsthread_once_start() for
121 * 'once'. In this case, the caller should perform whatever initialization
122 * actions it needs to do, then call ovsthread_once_done() for 'once'.
124 * Returns false if this is not the first call to ovsthread_once_start() for
125 * 'once'. In this case, the call will not return until after
126 * ovsthread_once_done() has been called. */
128 ovsthread_once_start(struct ovsthread_once
*once
)
130 /* We may be reading 'done' at the same time as the first thread
131 * is writing on it, or we can be using a stale copy of it. The
132 * worst that can happen is that we call ovsthread_once_start__()
133 * once when strictly not necessary. */
134 return OVS_UNLIKELY(!once
->done
&& ovsthread_once_start__(once
));
137 #endif /* ovs-thread.h */