foundationdb/fdbrpc/libeio/xthread.h

153 lines
4.9 KiB
C
Raw Normal View History

2017-05-26 04:48:44 +08:00
#ifndef XTHREAD_H_
#define XTHREAD_H_
/////////////////////////////////////////////////////////////////////////////
#ifdef _WIN32
#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls
#define _WIN32_WINNT 0x400
#include <stdio.h> //D
2017-05-26 04:48:44 +08:00
#include <fcntl.h>
#include <io.h>
#include <time.h>
#include <winsock2.h>
#include <process.h>
#include <windows.h>
#include <pthread.h>
#define sigset_t int
#define sigfillset(a)
#define pthread_sigmask(a, b, c)
#define sigaddset(a, b)
2017-05-26 04:48:44 +08:00
#define sigemptyset(s)
typedef pthread_mutex_t xmutex_t;
#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
#define X_MUTEX_CREATE(mutex) pthread_mutex_init(&(mutex), 0)
#define X_LOCK(mutex) pthread_mutex_lock(&(mutex))
#define X_UNLOCK(mutex) pthread_mutex_unlock(&(mutex))
2017-05-26 04:48:44 +08:00
typedef pthread_cond_t xcond_t;
#define X_COND_INIT PTHREAD_COND_INITIALIZER
#define X_COND_CREATE(cond) pthread_cond_init(&(cond), 0)
#define X_COND_SIGNAL(cond) pthread_cond_signal(&(cond))
#define X_COND_WAIT(cond, mutex) pthread_cond_wait(&(cond), &(mutex))
#define X_COND_TIMEDWAIT(cond, mutex, to) pthread_cond_timedwait(&(cond), &(mutex), &(to))
2017-05-26 04:48:44 +08:00
typedef pthread_t xthread_t;
#define X_THREAD_PROC(name) void* name(void* thr_arg)
#define X_THREAD_ATFORK(a, b, c)
2017-05-26 04:48:44 +08:00
static int thread_create(xthread_t* tid, void* (*proc)(void*), void* arg) {
int retval;
pthread_attr_t attr;
2017-05-26 04:48:44 +08:00
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2017-05-26 04:48:44 +08:00
retval = pthread_create(tid, &attr, proc, arg) == 0;
2017-05-26 04:48:44 +08:00
pthread_attr_destroy(&attr);
2017-05-26 04:48:44 +08:00
return retval;
2017-05-26 04:48:44 +08:00
}
#define respipe_read(a, b, c) PerlSock_recv((a), (b), (c), 0)
#define respipe_write(a, b, c) send((a), (b), (c), 0)
#define respipe_close(a) PerlSock_closesocket((a))
2017-05-26 04:48:44 +08:00
#else
/////////////////////////////////////////////////////////////////////////////
#if __linux && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
2017-05-26 04:48:44 +08:00
#endif
/* just in case */
#define _REENTRANT 1
#if __solaris
#define _POSIX_PTHREAD_SEMANTICS 1
2017-05-26 04:48:44 +08:00
/* try to bribe solaris headers into providing a current pthread API
* despite environment being configured for an older version.
*/
#define __EXTENSIONS__ 1
2017-05-26 04:48:44 +08:00
#endif
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <limits.h>
#include <pthread.h>
typedef pthread_mutex_t xmutex_t;
#if __linux && defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
#define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
#define X_MUTEX_CREATE(mutex) \
do { \
pthread_mutexattr_t attr; \
pthread_mutexattr_init(&attr); \
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \
pthread_mutex_init(&(mutex), &attr); \
} while (0)
2017-05-26 04:48:44 +08:00
#else
#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
#define X_MUTEX_CREATE(mutex) pthread_mutex_init(&(mutex), 0)
2017-05-26 04:48:44 +08:00
#endif
#define X_LOCK(mutex) pthread_mutex_lock(&(mutex))
#define X_UNLOCK(mutex) pthread_mutex_unlock(&(mutex))
2017-05-26 04:48:44 +08:00
typedef pthread_cond_t xcond_t;
#define X_COND_INIT PTHREAD_COND_INITIALIZER
#define X_COND_CREATE(cond) pthread_cond_init(&(cond), 0)
#define X_COND_SIGNAL(cond) pthread_cond_signal(&(cond))
#define X_COND_WAIT(cond, mutex) pthread_cond_wait(&(cond), &(mutex))
#define X_COND_TIMEDWAIT(cond, mutex, to) pthread_cond_timedwait(&(cond), &(mutex), &(to))
2017-05-26 04:48:44 +08:00
typedef pthread_t xthread_t;
#define X_THREAD_PROC(name) static void* name(void* thr_arg)
#define X_THREAD_ATFORK(prepare, parent, child) pthread_atfork(prepare, parent, child)
2017-05-26 04:48:44 +08:00
// the broken bsd's once more
#ifndef PTHREAD_STACK_MIN
#define PTHREAD_STACK_MIN 0
2017-05-26 04:48:44 +08:00
#endif
#ifndef X_STACKSIZE
#define X_STACKSIZE sizeof(void*) * 4096
2017-05-26 04:48:44 +08:00
#endif
static int thread_create(xthread_t* tid, void* (*proc)(void*), void* arg) {
int retval;
sigset_t fullsigset, oldsigset;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
// Fix stack overflow when using jemalloc by using the default stack size. See
// https://github.com/apple/foundationdb/issues/4386#issuecomment-786986483.
//
// pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
2017-05-26 04:48:44 +08:00
#ifdef PTHREAD_SCOPE_PROCESS
pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
2017-05-26 04:48:44 +08:00
#endif
sigfillset(&fullsigset);
2017-05-26 04:48:44 +08:00
pthread_sigmask(SIG_SETMASK, &fullsigset, &oldsigset);
retval = pthread_create(tid, &attr, proc, arg) == 0;
pthread_sigmask(SIG_SETMASK, &oldsigset, 0);
2017-05-26 04:48:44 +08:00
pthread_attr_destroy(&attr);
2017-05-26 04:48:44 +08:00
return retval;
2017-05-26 04:48:44 +08:00
}
#define respipe_read(a, b, c) read((a), (b), (c))
#define respipe_write(a, b, c) write((a), (b), (c))
#define respipe_close(a) close((a))
2017-05-26 04:48:44 +08:00
#endif
#endif