ovs-thread: Add support for various thread-related assertions.
[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_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 *);
87
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 *);
92
93 typedef void destructor_func(void *);
94 XPTHREAD_FUNC2(pthread_key_create, pthread_key_t *, destructor_func *);
95
96 void
97 xpthread_create(pthread_t *threadp, pthread_attr_t *attr,
98                 void *(*start)(void *), void *arg)
99 {
100     pthread_t thread;
101     int error;
102
103     forbid_forking("multiple threads exist");
104     multithreaded = true;
105
106     error = pthread_create(threadp ? threadp : &thread, attr, start, arg);
107     if (error) {
108         ovs_abort(error, "pthread_create failed");
109     }
110 }
111 \f
112 bool
113 ovsthread_once_start__(struct ovsthread_once *once)
114 {
115     xpthread_mutex_lock(&once->mutex);
116     if (!ovsthread_once_is_done__(once)) {
117         return false;
118     }
119     xpthread_mutex_unlock(&once->mutex);
120     return true;
121 }
122
123 void OVS_RELEASES(once)
124 ovsthread_once_done(struct ovsthread_once *once)
125 {
126     atomic_store(&once->done, true);
127     xpthread_mutex_unlock(&once->mutex);
128 }
129 \f
130 /* Asserts that the process has not yet created any threads (beyond the initial
131  * thread).  */
132 void
133 (assert_single_threaded)(const char *where)
134 {
135     if (multithreaded) {
136         VLOG_FATAL("%s: attempted operation not allowed when multithreaded",
137                    where);
138     }
139 }
140
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().  */
144 pid_t
145 (xfork)(const char *where)
146 {
147     pid_t pid;
148
149     if (must_not_fork) {
150         VLOG_FATAL("%s: attempted to fork but forking not allowed (%s)",
151                    where, must_not_fork);
152     }
153
154     pid = fork();
155     if (pid < 0) {
156         VLOG_FATAL("fork failed (%s)", strerror(errno));
157     }
158     return pid;
159 }
160
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
163  * afterward.) */
164 void
165 forbid_forking(const char *reason)
166 {
167     ovs_assert(reason != NULL);
168     must_not_fork = reason;
169 }
170
171 /* Returns true if the process is allowed to fork, false otherwise. */
172 bool
173 may_fork(void)
174 {
175     return !must_not_fork;
176 }
177 #endif