Avoid C preprocessor trick where macro has the same name as a function.
[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_destroy, pthread_mutex_t *);
77 XPTHREAD_FUNC1(pthread_mutex_lock, pthread_mutex_t *);
78 XPTHREAD_FUNC1(pthread_mutex_unlock, pthread_mutex_t *);
79 XPTHREAD_TRY_FUNC1(pthread_mutex_trylock, pthread_mutex_t *);
80
81 XPTHREAD_FUNC1(pthread_mutexattr_init, pthread_mutexattr_t *);
82 XPTHREAD_FUNC1(pthread_mutexattr_destroy, pthread_mutexattr_t *);
83 XPTHREAD_FUNC2(pthread_mutexattr_settype, pthread_mutexattr_t *, int);
84 XPTHREAD_FUNC2(pthread_mutexattr_gettype, pthread_mutexattr_t *, int *);
85
86 XPTHREAD_FUNC2(pthread_rwlock_init,
87                pthread_rwlock_t *, pthread_rwlockattr_t *);
88 XPTHREAD_FUNC1(pthread_rwlock_destroy, pthread_rwlock_t *);
89 XPTHREAD_FUNC1(pthread_rwlock_rdlock, pthread_rwlock_t *);
90 XPTHREAD_FUNC1(pthread_rwlock_wrlock, pthread_rwlock_t *);
91 XPTHREAD_FUNC1(pthread_rwlock_unlock, pthread_rwlock_t *);
92 XPTHREAD_TRY_FUNC1(pthread_rwlock_tryrdlock, pthread_rwlock_t *);
93 XPTHREAD_TRY_FUNC1(pthread_rwlock_trywrlock, pthread_rwlock_t *);
94
95 XPTHREAD_FUNC2(pthread_cond_init, pthread_cond_t *, pthread_condattr_t *);
96 XPTHREAD_FUNC1(pthread_cond_destroy, pthread_cond_t *);
97 XPTHREAD_FUNC1(pthread_cond_signal, pthread_cond_t *);
98 XPTHREAD_FUNC1(pthread_cond_broadcast, pthread_cond_t *);
99 XPTHREAD_FUNC2(pthread_cond_wait, pthread_cond_t *, pthread_mutex_t *);
100
101 typedef void destructor_func(void *);
102 XPTHREAD_FUNC2(pthread_key_create, pthread_key_t *, destructor_func *);
103
104 void
105 xpthread_create(pthread_t *threadp, pthread_attr_t *attr,
106                 void *(*start)(void *), void *arg)
107 {
108     pthread_t thread;
109     int error;
110
111     forbid_forking("multiple threads exist");
112     multithreaded = true;
113
114     error = pthread_create(threadp ? threadp : &thread, attr, start, arg);
115     if (error) {
116         ovs_abort(error, "pthread_create failed");
117     }
118 }
119 \f
120 bool
121 ovsthread_once_start__(struct ovsthread_once *once)
122 {
123     xpthread_mutex_lock(&once->mutex);
124     if (!ovsthread_once_is_done__(once)) {
125         return false;
126     }
127     xpthread_mutex_unlock(&once->mutex);
128     return true;
129 }
130
131 void OVS_RELEASES(once)
132 ovsthread_once_done(struct ovsthread_once *once)
133 {
134     atomic_store(&once->done, true);
135     xpthread_mutex_unlock(&once->mutex);
136 }
137 \f
138 /* Asserts that the process has not yet created any threads (beyond the initial
139  * thread).
140  *
141  * ('where' is used in logging.  Commonly one would use
142  * assert_single_threaded() to automatically provide the caller's source file
143  * and line number for 'where'.) */
144 void
145 assert_single_threaded_at(const char *where)
146 {
147     if (multithreaded) {
148         VLOG_FATAL("%s: attempted operation not allowed when multithreaded",
149                    where);
150     }
151 }
152
153 /* Forks the current process (checking that this is allowed).  Aborts with
154  * VLOG_FATAL if fork() returns an error, and otherwise returns the value
155  * returned by fork().
156  *
157  * ('where' is used in logging.  Commonly one would use xfork() to
158  * automatically provide the caller's source file and line number for
159  * 'where'.) */
160 pid_t
161 xfork_at(const char *where)
162 {
163     pid_t pid;
164
165     if (must_not_fork) {
166         VLOG_FATAL("%s: attempted to fork but forking not allowed (%s)",
167                    where, must_not_fork);
168     }
169
170     pid = fork();
171     if (pid < 0) {
172         VLOG_FATAL("fork failed (%s)", ovs_strerror(errno));
173     }
174     return pid;
175 }
176
177 /* Notes that the process must not call fork() from now on, for the specified
178  * 'reason'.  (The process may still fork() if it execs itself immediately
179  * afterward.) */
180 void
181 forbid_forking(const char *reason)
182 {
183     ovs_assert(reason != NULL);
184     must_not_fork = reason;
185 }
186
187 /* Returns true if the process is allowed to fork, false otherwise. */
188 bool
189 may_fork(void)
190 {
191     return !must_not_fork;
192 }
193 #endif