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 XPTHREAD_FUNC1(FUNCTION, PARAM1) \
48 x##FUNCTION(PARAM1 arg1) \
50 int error = FUNCTION(arg1); \
51 if (OVS_UNLIKELY(error)) { \
52 ovs_abort(error, "%s failed", #FUNCTION); \
55 #define XPTHREAD_TRY_FUNC1(FUNCTION, PARAM1) \
57 x##FUNCTION(PARAM1 arg1) \
59 int error = FUNCTION(arg1); \
60 if (OVS_UNLIKELY(error && error != EBUSY)) { \
61 ovs_abort(error, "%s failed", #FUNCTION); \
65 #define XPTHREAD_FUNC2(FUNCTION, PARAM1, PARAM2) \
67 x##FUNCTION(PARAM1 arg1, PARAM2 arg2) \
69 int error = FUNCTION(arg1, arg2); \
70 if (OVS_UNLIKELY(error)) { \
71 ovs_abort(error, "%s failed", #FUNCTION); \
75 XPTHREAD_FUNC2(pthread_mutex_init, pthread_mutex_t *, pthread_mutexattr_t *);
76 XPTHREAD_FUNC1(pthread_mutex_lock, pthread_mutex_t *);
77 XPTHREAD_FUNC1(pthread_mutex_unlock, pthread_mutex_t *);
78 XPTHREAD_TRY_FUNC1(pthread_mutex_trylock, pthread_mutex_t *);
80 XPTHREAD_FUNC2(pthread_rwlock_init,
81 pthread_rwlock_t *, pthread_rwlockattr_t *);
82 XPTHREAD_FUNC1(pthread_rwlock_rdlock, pthread_rwlock_t *);
83 XPTHREAD_FUNC1(pthread_rwlock_wrlock, pthread_rwlock_t *);
84 XPTHREAD_FUNC1(pthread_rwlock_unlock, pthread_rwlock_t *);
85 XPTHREAD_TRY_FUNC1(pthread_rwlock_tryrdlock, pthread_rwlock_t *);
86 XPTHREAD_TRY_FUNC1(pthread_rwlock_trywrlock, pthread_rwlock_t *);
88 XPTHREAD_FUNC2(pthread_cond_init, pthread_cond_t *, pthread_condattr_t *);
89 XPTHREAD_FUNC1(pthread_cond_signal, pthread_cond_t *);
90 XPTHREAD_FUNC1(pthread_cond_broadcast, pthread_cond_t *);
91 XPTHREAD_FUNC2(pthread_cond_wait, pthread_cond_t *, pthread_mutex_t *);
93 typedef void destructor_func(void *);
94 XPTHREAD_FUNC2(pthread_key_create, pthread_key_t *, destructor_func *);
97 xpthread_create(pthread_t *threadp, pthread_attr_t *attr,
98 void *(*start)(void *), void *arg)
103 forbid_forking("multiple threads exist");
104 multithreaded = true;
106 error = pthread_create(threadp ? threadp : &thread, attr, start, arg);
108 ovs_abort(error, "pthread_create failed");
113 ovsthread_once_start__(struct ovsthread_once *once)
115 xpthread_mutex_lock(&once->mutex);
116 if (!ovsthread_once_is_done__(once)) {
119 xpthread_mutex_unlock(&once->mutex);
123 void OVS_RELEASES(once)
124 ovsthread_once_done(struct ovsthread_once *once)
126 atomic_store(&once->done, true);
127 xpthread_mutex_unlock(&once->mutex);
130 /* Asserts that the process has not yet created any threads (beyond the initial
133 (assert_single_threaded)(const char *where)
136 VLOG_FATAL("%s: attempted operation not allowed when multithreaded",
141 /* Forks the current process (checking that this is allowed). Aborts with
142 * VLOG_FATAL if fork() returns an error, and otherwise returns the value
143 * returned by fork(). */
145 (xfork)(const char *where)
150 VLOG_FATAL("%s: attempted to fork but forking not allowed (%s)",
151 where, must_not_fork);
156 VLOG_FATAL("fork failed (%s)", ovs_strerror(errno));
161 /* Notes that the process must not call fork() from now on, for the specified
162 * 'reason'. (The process may still fork() if it execs itself immediately
165 forbid_forking(const char *reason)
167 ovs_assert(reason != NULL);
168 must_not_fork = reason;
171 /* Returns true if the process is allowed to fork, false otherwise. */
175 return !must_not_fork;