fatal-signal: Make thread-safe.
[sliver-openvswitch.git] / lib / ovs-thread.c
1 /*
2  * Copyright (c) 2013 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-thread.h"
19 #include <errno.h>
20 #include <poll.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include "compiler.h"
24 #include "poll-loop.h"
25 #include "socket-util.h"
26 #include "util.h"
27
28 #ifdef __CHECKER__
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. */
33 #else
34 #include "vlog.h"
35
36 VLOG_DEFINE_THIS_MODULE(ovs_thread);
37
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
40  * explains why. */
41 static const char *must_not_fork;
42
43 /* True if we created any threads beyond the main initial thread. */
44 static bool multithreaded;
45
46 #define XPTHREAD_FUNC1(FUNCTION, PARAM1)                \
47     void                                                \
48     x##FUNCTION(PARAM1 arg1)                            \
49     {                                                   \
50         int error = FUNCTION(arg1);                     \
51         if (OVS_UNLIKELY(error)) {                      \
52             ovs_abort(error, "%s failed", #FUNCTION);   \
53         }                                               \
54     }
55 #define XPTHREAD_TRY_FUNC1(FUNCTION, PARAM1)            \
56     int                                                 \
57     x##FUNCTION(PARAM1 arg1)                            \
58     {                                                   \
59         int error = FUNCTION(arg1);                     \
60         if (OVS_UNLIKELY(error && error != EBUSY)) {    \
61             ovs_abort(error, "%s failed", #FUNCTION);   \
62         }                                               \
63         return error;                                   \
64     }
65 #define XPTHREAD_FUNC2(FUNCTION, PARAM1, PARAM2)        \
66     void                                                \
67     x##FUNCTION(PARAM1 arg1, PARAM2 arg2)               \
68     {                                                   \
69         int error = FUNCTION(arg1, arg2);               \
70         if (OVS_UNLIKELY(error)) {                      \
71             ovs_abort(error, "%s failed", #FUNCTION);   \
72         }                                               \
73     }
74
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 *);
79
80 XPTHREAD_FUNC1(pthread_mutexattr_init, pthread_mutexattr_t *);
81 XPTHREAD_FUNC1(pthread_mutexattr_destroy, pthread_mutexattr_t *);
82 XPTHREAD_FUNC2(pthread_mutexattr_settype, pthread_mutexattr_t *, int);
83 XPTHREAD_FUNC2(pthread_mutexattr_gettype, pthread_mutexattr_t *, int *);
84
85 XPTHREAD_FUNC2(pthread_rwlock_init,
86                pthread_rwlock_t *, pthread_rwlockattr_t *);
87 XPTHREAD_FUNC1(pthread_rwlock_rdlock, pthread_rwlock_t *);
88 XPTHREAD_FUNC1(pthread_rwlock_wrlock, pthread_rwlock_t *);
89 XPTHREAD_FUNC1(pthread_rwlock_unlock, pthread_rwlock_t *);
90 XPTHREAD_TRY_FUNC1(pthread_rwlock_tryrdlock, pthread_rwlock_t *);
91 XPTHREAD_TRY_FUNC1(pthread_rwlock_trywrlock, pthread_rwlock_t *);
92
93 XPTHREAD_FUNC2(pthread_cond_init, pthread_cond_t *, pthread_condattr_t *);
94 XPTHREAD_FUNC1(pthread_cond_signal, pthread_cond_t *);
95 XPTHREAD_FUNC1(pthread_cond_broadcast, pthread_cond_t *);
96 XPTHREAD_FUNC2(pthread_cond_wait, pthread_cond_t *, pthread_mutex_t *);
97
98 typedef void destructor_func(void *);
99 XPTHREAD_FUNC2(pthread_key_create, pthread_key_t *, destructor_func *);
100
101 void
102 xpthread_create(pthread_t *threadp, pthread_attr_t *attr,
103                 void *(*start)(void *), void *arg)
104 {
105     pthread_t thread;
106     int error;
107
108     forbid_forking("multiple threads exist");
109     multithreaded = true;
110
111     error = pthread_create(threadp ? threadp : &thread, attr, start, arg);
112     if (error) {
113         ovs_abort(error, "pthread_create failed");
114     }
115 }
116 \f
117 bool
118 ovsthread_once_start__(struct ovsthread_once *once)
119 {
120     xpthread_mutex_lock(&once->mutex);
121     if (!ovsthread_once_is_done__(once)) {
122         return false;
123     }
124     xpthread_mutex_unlock(&once->mutex);
125     return true;
126 }
127
128 void OVS_RELEASES(once)
129 ovsthread_once_done(struct ovsthread_once *once)
130 {
131     atomic_store(&once->done, true);
132     xpthread_mutex_unlock(&once->mutex);
133 }
134 \f
135 /* Asserts that the process has not yet created any threads (beyond the initial
136  * thread).  */
137 void
138 (assert_single_threaded)(const char *where)
139 {
140     if (multithreaded) {
141         VLOG_FATAL("%s: attempted operation not allowed when multithreaded",
142                    where);
143     }
144 }
145
146 /* Forks the current process (checking that this is allowed).  Aborts with
147  * VLOG_FATAL if fork() returns an error, and otherwise returns the value
148  * returned by fork().  */
149 pid_t
150 (xfork)(const char *where)
151 {
152     pid_t pid;
153
154     if (must_not_fork) {
155         VLOG_FATAL("%s: attempted to fork but forking not allowed (%s)",
156                    where, must_not_fork);
157     }
158
159     pid = fork();
160     if (pid < 0) {
161         VLOG_FATAL("fork failed (%s)", ovs_strerror(errno));
162     }
163     return pid;
164 }
165
166 /* Notes that the process must not call fork() from now on, for the specified
167  * 'reason'.  (The process may still fork() if it execs itself immediately
168  * afterward.) */
169 void
170 forbid_forking(const char *reason)
171 {
172     ovs_assert(reason != NULL);
173     must_not_fork = reason;
174 }
175
176 /* Returns true if the process is allowed to fork, false otherwise. */
177 bool
178 may_fork(void)
179 {
180     return !must_not_fork;
181 }
182 #endif