2 * Copyright (c) 2013 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.
18 #include "ovs-thread.h"
24 #include "poll-loop.h"
25 #include "socket-util.h"
29 /* Omit the definitions in this file because they are somewhat difficult to
30 * write without prompting "sparse" complaints, without ugliness or
31 * cut-and-paste. Since "sparse" is just a checker, not a compiler, it
32 * doesn't matter that we don't define them. */
36 VLOG_DEFINE_THIS_MODULE(ovs_thread);
38 /* If there is a reason that we cannot fork anymore (unless the fork will be
39 * immediately followed by an exec), then this points to a string that
41 static const char *must_not_fork;
43 /* True if we created any threads beyond the main initial thread. */
44 static bool multithreaded;
46 #define LOCK_FUNCTION(TYPE, FUN) \
48 ovs_##TYPE##_##FUN##_at(const struct ovs_##TYPE *l_, \
51 struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
52 int error = pthread_##TYPE##_##FUN(&l->lock); \
53 if (OVS_UNLIKELY(error)) { \
54 ovs_abort(error, "pthread_%s_%s failed", #TYPE, #FUN); \
58 LOCK_FUNCTION(mutex, lock);
59 LOCK_FUNCTION(rwlock, rdlock);
60 LOCK_FUNCTION(rwlock, wrlock);
62 #define TRY_LOCK_FUNCTION(TYPE, FUN) \
64 ovs_##TYPE##_##FUN##_at(const struct ovs_##TYPE *l_, \
67 struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
68 int error = pthread_##TYPE##_##FUN(&l->lock); \
69 if (OVS_UNLIKELY(error) && error != EBUSY) { \
70 ovs_abort(error, "pthread_%s_%s failed", #TYPE, #FUN); \
77 TRY_LOCK_FUNCTION(mutex, trylock);
78 TRY_LOCK_FUNCTION(rwlock, tryrdlock);
79 TRY_LOCK_FUNCTION(rwlock, trywrlock);
81 #define UNLOCK_FUNCTION(TYPE, FUN) \
83 ovs_##TYPE##_##FUN(const struct ovs_##TYPE *l_) \
85 struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
88 error = pthread_##TYPE##_##FUN(&l->lock); \
89 if (OVS_UNLIKELY(error)) { \
90 ovs_abort(error, "pthread_%s_%sfailed", #TYPE, #FUN); \
93 UNLOCK_FUNCTION(mutex, unlock);
94 UNLOCK_FUNCTION(mutex, destroy);
95 UNLOCK_FUNCTION(rwlock, unlock);
96 UNLOCK_FUNCTION(rwlock, destroy);
98 #define XPTHREAD_FUNC1(FUNCTION, PARAM1) \
100 x##FUNCTION(PARAM1 arg1) \
102 int error = FUNCTION(arg1); \
103 if (OVS_UNLIKELY(error)) { \
104 ovs_abort(error, "%s failed", #FUNCTION); \
107 #define XPTHREAD_FUNC2(FUNCTION, PARAM1, PARAM2) \
109 x##FUNCTION(PARAM1 arg1, PARAM2 arg2) \
111 int error = FUNCTION(arg1, arg2); \
112 if (OVS_UNLIKELY(error)) { \
113 ovs_abort(error, "%s failed", #FUNCTION); \
117 XPTHREAD_FUNC1(pthread_mutex_lock, pthread_mutex_t *);
118 XPTHREAD_FUNC1(pthread_mutex_unlock, pthread_mutex_t *);
119 XPTHREAD_FUNC1(pthread_mutexattr_init, pthread_mutexattr_t *);
120 XPTHREAD_FUNC1(pthread_mutexattr_destroy, pthread_mutexattr_t *);
121 XPTHREAD_FUNC2(pthread_mutexattr_settype, pthread_mutexattr_t *, int);
122 XPTHREAD_FUNC2(pthread_mutexattr_gettype, pthread_mutexattr_t *, int *);
124 XPTHREAD_FUNC2(pthread_cond_init, pthread_cond_t *, pthread_condattr_t *);
125 XPTHREAD_FUNC1(pthread_cond_destroy, pthread_cond_t *);
126 XPTHREAD_FUNC1(pthread_cond_signal, pthread_cond_t *);
127 XPTHREAD_FUNC1(pthread_cond_broadcast, pthread_cond_t *);
129 typedef void destructor_func(void *);
130 XPTHREAD_FUNC2(pthread_key_create, pthread_key_t *, destructor_func *);
133 ovs_mutex_init(const struct ovs_mutex *l_, int type)
135 struct ovs_mutex *l = CONST_CAST(struct ovs_mutex *, l_);
136 pthread_mutexattr_t attr;
140 xpthread_mutexattr_init(&attr);
141 xpthread_mutexattr_settype(&attr, type);
142 error = pthread_mutex_init(&l->lock, &attr);
143 if (OVS_UNLIKELY(error)) {
144 ovs_abort(error, "pthread_mutex_init failed");
146 xpthread_mutexattr_destroy(&attr);
150 ovs_rwlock_init(const struct ovs_rwlock *l_)
152 struct ovs_rwlock *l = CONST_CAST(struct ovs_rwlock *, l_);
156 error = pthread_rwlock_init(&l->lock, NULL);
157 if (OVS_UNLIKELY(error)) {
158 ovs_abort(error, "pthread_rwlock_init failed");
163 ovs_mutex_cond_wait(pthread_cond_t *cond, const struct ovs_mutex *mutex_)
165 struct ovs_mutex *mutex = CONST_CAST(struct ovs_mutex *, mutex_);
166 int error = pthread_cond_wait(cond, &mutex->lock);
167 if (OVS_UNLIKELY(error)) {
168 ovs_abort(error, "pthread_cond_wait failed");
173 xpthread_create(pthread_t *threadp, pthread_attr_t *attr,
174 void *(*start)(void *), void *arg)
179 forbid_forking("multiple threads exist");
180 multithreaded = true;
182 error = pthread_create(threadp ? threadp : &thread, attr, start, arg);
184 ovs_abort(error, "pthread_create failed");
189 ovsthread_once_start__(struct ovsthread_once *once)
191 ovs_mutex_lock(&once->mutex);
192 if (!ovsthread_once_is_done__(once)) {
195 ovs_mutex_unlock(&once->mutex);
200 ovsthread_once_done(struct ovsthread_once *once)
202 atomic_store(&once->done, true);
203 ovs_mutex_unlock(&once->mutex);
206 /* Asserts that the process has not yet created any threads (beyond the initial
209 * ('where' is used in logging. Commonly one would use
210 * assert_single_threaded() to automatically provide the caller's source file
211 * and line number for 'where'.) */
213 assert_single_threaded_at(const char *where)
216 VLOG_FATAL("%s: attempted operation not allowed when multithreaded",
221 /* Forks the current process (checking that this is allowed). Aborts with
222 * VLOG_FATAL if fork() returns an error, and otherwise returns the value
223 * returned by fork().
225 * ('where' is used in logging. Commonly one would use xfork() to
226 * automatically provide the caller's source file and line number for
229 xfork_at(const char *where)
234 VLOG_FATAL("%s: attempted to fork but forking not allowed (%s)",
235 where, must_not_fork);
240 VLOG_FATAL("%s: fork failed (%s)", where, ovs_strerror(errno));
245 /* Notes that the process must not call fork() from now on, for the specified
246 * 'reason'. (The process may still fork() if it execs itself immediately
249 forbid_forking(const char *reason)
251 ovs_assert(reason != NULL);
252 must_not_fork = reason;
255 /* Returns true if the process is allowed to fork, false otherwise. */
259 return !must_not_fork;