1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
#ifndef TARANTOOL_PTHREAD_H_INCLUDED
#define TARANTOOL_PTHREAD_H_INCLUDED
/*
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "trivia/config.h"
#include "trivia/util.h"
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#if HAVE_PTHREAD_NP_H || (__OpenBSD__)
#include <pthread_np.h>
#endif
#if (__OpenBSD__)
#include <signal.h>
#include <sys/signal.h>
#endif
#include "say.h"
/**
* Assert on any pthread* error in debug mode. In release,
* write into the log file where and what has failed.
*
* Still give the user an opportunity to manually
* check for error, by assigning pthread_* function status
* to errno and returning -1.
*/
#define tt_pthread_error(e) \
if (e != 0) { \
say_syserror("%s error %d", __func__, e);\
errno = e; \
} \
assert(e == 0); \
e != 0 ? -1 : 0
/**
* Debug/logging friendly wrappers around pthread
* functions.
*/
#ifndef NDEBUG
#define tt_pthread_mutex_init(mutex, attr) \
({ \
pthread_mutexattr_t *p_attr = attr; \
pthread_mutexattr_t errorcheck; \
if (p_attr == NULL) { \
(void) tt_pthread_mutexattr_init(&errorcheck);\
(void) pthread_mutexattr_settype(&errorcheck, \
PTHREAD_MUTEX_ERRORCHECK); \
p_attr = &errorcheck; \
} \
int e__ = pthread_mutex_init(mutex, p_attr);\
(void) tt_pthread_mutexattr_destroy(&errorcheck); \
tt_pthread_error(e__); \
})
#else
#define tt_pthread_mutex_init(mutex, attr) \
({ \
int e__ = pthread_mutex_init(mutex, attr);\
tt_pthread_error(e__); \
})
#endif
#define tt_pthread_mutex_destroy(mutex) \
({ int e__ = pthread_mutex_destroy(mutex); \
tt_pthread_error(e__); \
})
#define tt_pthread_mutex_lock(mutex) \
({ int e__ = pthread_mutex_lock(mutex); \
say_debug("%s: locking %s", __func__, #mutex);\
tt_pthread_error(e__);\
})
#define tt_pthread_mutex_trylock(mutex) \
({ int e__ = pthread_mutex_trylock(mutex); \
if (e__ != 0 && e__ != EBUSY) \
say_error("%s error %d at %s:%d", __func__, e__, __FILE__, __LINE__);\
assert(e__ == 0 || e__ == EBUSY); \
e__; \
})
#define tt_pthread_mutex_unlock(mutex) \
({ int e__ = pthread_mutex_unlock(mutex); \
say_debug("%s: unlocking %s", __func__, #mutex);\
tt_pthread_error(e__); \
})
#define tt_pthread_mutex_destroy(mutex) \
({ int e__ = pthread_mutex_destroy(mutex); \
tt_pthread_error(e__); \
})
#define tt_pthread_mutexattr_init(attr) \
({ int e__ = pthread_mutexattr_init(attr); \
tt_pthread_error(e__); \
})
#define tt_pthread_mutexattr_destroy(attr) \
({ int e__ = pthread_mutexattr_destroy(attr);\
tt_pthread_error(e__); \
})
#define tt_pthread_mutexattr_gettype(attr, type)\
({ int e__ = pthread_mutexattr_gettype(attr, type);\
tt_pthread_error(e__); \
})
#define tt_pthread_mutexattr_settype(attr, type)\
({ int e__ = pthread_mutexattr_settype(attr, type);\
tt_pthread_error(e__); \
})
#define tt_pthread_rwlock_init(rwlock, attr) \
({ \
int e__ = pthread_rwlock_init(rwlock, attr);\
tt_pthread_error(e__); \
})
#define tt_pthread_rwlock_destroy(rwlock) \
({ int e__ = pthread_rwlock_destroy(rwlock); \
tt_pthread_error(e__); \
})
#define tt_pthread_rwlock_rdlock(rwlock) \
({ int e__ = pthread_rwlock_rdlock(rwlock); \
say_debug("%s: locking %s", __func__, #rwlock);\
tt_pthread_error(e__);\
})
#define tt_pthread_rwlock_tryrdlock(rwlock) \
({ int e__ = pthread_rwlock_tryrdlock(rwlock); \
if (e__ != 0 && e__ != EBUSY) \
say_error("%s error %d at %s:%d", __func__, e__, __FILE__, __LINE__);\
assert(e__ == 0 || e__ == EBUSY); \
e__; \
})
#define tt_pthread_rwlock_wrlock(rwlock) \
({ int e__ = pthread_rwlock_wrlock(rwlock); \
say_debug("%s: locking %s", __func__, #rwlock);\
tt_pthread_error(e__);\
})
#define tt_pthread_rwlock_trywrlock(rwlock) \
({ int e__ = pthread_rwlock_trywrlock(rwlock); \
if (e__ != 0 && e__ != EBUSY) \
say_error("%s error %d at %s:%d", __func__, e__, __FILE__, __LINE__);\
assert(e__ == 0 || e__ == EBUSY); \
e__; \
})
#define tt_pthread_rwlock_unlock(rwlock) \
({ int e__ = pthread_rwlock_unlock(rwlock); \
say_debug("%s: unlocking %s", __func__, #rwlock);\
tt_pthread_error(e__); \
})
#define tt_pthread_rwlock_destroy(rwlock) \
({ int e__ = pthread_rwlock_destroy(rwlock); \
tt_pthread_error(e__); \
})
#define tt_pthread_rwlockattr_init(attr) \
({ int e__ = pthread_rwlockattr_init(attr); \
tt_pthread_error(e__); \
})
#define tt_pthread_rwlockattr_destroy(attr) \
({ int e__ = pthread_rwlockattr_destroy(attr);\
tt_pthread_error(e__); \
})
#define tt_pthread_rwlockattr_gettype(attr, type)\
({ int e__ = pthread_rwlockattr_gettype(attr, type);\
tt_pthread_error(e__); \
})
#define tt_pthread_rwlockattr_settype(attr, type)\
({ int e__ = pthread_rwlockattr_settype(attr, type);\
tt_pthread_error(e__); \
})
#define tt_pthread_condattr_init(attr) \
({ int e__ = pthread_condattr_init(attr); \
tt_pthread_error(e__); \
})
#define tt_pthread_condattr_destroy(attr) \
({ int e__ = pthread_condattr_destroy(attr); \
tt_pthread_error(e__); \
})
#define tt_pthread_cond_init(cond, attr) \
({ int e__ = pthread_cond_init(cond, attr);\
tt_pthread_error(e__); \
})
#define tt_pthread_cond_destroy(cond) \
({ int e__ = pthread_cond_destroy(cond); \
tt_pthread_error(e__); \
})
#define tt_pthread_cond_signal(cond) \
({ int e__ = pthread_cond_signal(cond); \
tt_pthread_error(e__); \
})
#define tt_pthread_cond_broadcast(cond) \
({ int e__ = pthread_cond_broadcast(cond); \
tt_pthread_error(e__); \
})
#define tt_pthread_cond_wait(cond, mutex) \
({ int e__ = pthread_cond_wait(cond, mutex);\
tt_pthread_error(e__); \
})
#define tt_pthread_cond_timedwait(cond, mutex, timeout) \
({ int e__ = pthread_cond_timedwait(cond, mutex, timeout);\
if (ETIMEDOUT != e__ && e__ != 0) \
say_error("%s error %d", __func__, e__);\
assert(e__ == 0 || e__ == ETIMEDOUT); \
e__; \
})
#define tt_pthread_once(control, function) \
({ int e__ = pthread_once(control, function);\
tt_pthread_error(e__); \
})
#define tt_pthread_atfork(prepare, parent, child)\
({ int e__ = pthread_atfork(prepare, parent, child);\
tt_pthread_error(e__); \
})
/** Make sure the created thread blocks all signals,
* they are handled in the main thread.
*/
#define tt_pthread_create(thread, attr, run, arg) \
({ sigset_t set, oldset; \
sigfillset(&set); \
pthread_sigmask(SIG_BLOCK, &set, &oldset); \
int e__ = pthread_create(thread, attr, run, arg);\
pthread_sigmask(SIG_SETMASK, &oldset, NULL); \
tt_pthread_error(e__); \
})
#define tt_pthread_join(thread, ret) \
({ int e__ = pthread_join(thread, ret); \
tt_pthread_error(e__); \
})
#define tt_pthread_cancel(thread) \
({ int e__ = pthread_cancel(thread); \
assert(e__ == 0 || e__ == ESRCH); \
e__; \
})
#define tt_pthread_key_create(key, dtor) \
({ int e__ = pthread_key_create(key, dtor); \
tt_pthread_error(e__); \
})
#define tt_pthread_key_delete(key) \
({ int e__ = pthread_key_delete(key); \
tt_pthread_error(e__); \
})
#define tt_pthread_setspecific(key, value) \
({ int e__ = pthread_setspecific(key, value); \
tt_pthread_error(e__); \
})
#define tt_pthread_getspecific(key) pthread_getspecific(key)
#define tt_pthread_setcancelstate(state, oldstate) \
({ int e__ = pthread_setcancelstate(state, oldstate);\
tt_pthread_error(e__); \
})
/** Set the current thread's name
*/
static inline void
tt_pthread_setname(const char *name)
{
/* Setting the name fails if the name was too long. Linux limits a
* name to 16 bytes (including the trailing NUL), other OS don't
* even bother to document the limit.
*/
char short_name[16];
snprintf(short_name, sizeof(short_name), "%s", name);
#if HAVE_PTHREAD_SETNAME_NP
pthread_setname_np(pthread_self(), short_name);
#elif HAVE_PTHREAD_SETNAME_NP_1
pthread_setname_np(short_name);
#elif HAVE_PTHREAD_SET_NAME_NP
pthread_set_name_np(pthread_self(), short_name);
#endif
}
static inline void
tt_pthread_attr_getstack(pthread_t thread, void **stackaddr, size_t *stacksize)
{
#if HAVE_PTHREAD_GETATTR_NP
/*
* GLIBC
*
* From glib-2.24/sysdeps/nptl/pthread.h pthread_getattr_np():
* It shall be called on uninitialized ATTR and destroyed with
* pthread_attr_destroy when no longer needed.
*/
pthread_attr_t thread_attr;
pthread_getattr_np(thread, &thread_attr);
pthread_attr_getstack(&thread_attr, stackaddr, stacksize);
pthread_attr_destroy(&thread_attr);
#elif HAVE_PTHREAD_ATTR_GET_NP
/*
* xBSD/new macOS
*
* From pthread_attr_get_np(3):
* It is HIGHLY RECOMMENDED to use pthread_attr_init(3) function to
* allocate attribute storage.
*/
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
pthread_attr_get_np(thread, &thread_attr);
pthread_attr_getstack(&thread_attr, stackaddr, stacksize);
pthread_attr_destroy(&thread_attr);
#elif (HAVE_PTHREAD_GET_STACKSIZE_NP && HAVE_PTHREAD_GET_STACKADDR_NP)
/* Old macOS */
*stacksize = pthread_get_stacksize_np(thread);
*stackaddr = pthread_get_stackaddr_np(thread);
#elif (__OpenBSD__)
stack_t *sinfo = (stack_t*)malloc(sizeof(stack_t));
pthread_stackseg_np(thread, sinfo);
*stacksize = sinfo->ss_size;
*stackaddr = sinfo->ss_sp;
#else
#error Unable to get thread stack
#endif
}
#endif /* TARANTOOL_PTHREAD_H_INCLUDED */
|