PageRenderTime 17ms CodeModel.GetById 6ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/unix/eio/xthread.h

http://github.com/joyent/libuv
C++ Header | 164 lines | 120 code | 33 blank | 11 comment | 7 complexity | 19b2da986b07922da5276a351dc77b2e MD5 | raw file
  1#ifndef XTHREAD_H_
  2#define XTHREAD_H_
  3
  4/* whether word reads are potentially non-atomic.
  5 * this is conservative, likely most arches this runs
  6 * on have atomic word read/writes.
  7 */
  8#ifndef WORDACCESS_UNSAFE
  9# if __i386 || __x86_64
 10#  define WORDACCESS_UNSAFE 0
 11# else
 12#  define WORDACCESS_UNSAFE 1
 13# endif
 14#endif
 15
 16/////////////////////////////////////////////////////////////////////////////
 17
 18#ifdef _WIN32
 19
 20#include <stdio.h>//D
 21#include <fcntl.h>
 22#include <io.h>
 23#include <time.h>
 24#include <winsock2.h>
 25#include <process.h>
 26#include <windows.h>
 27#include <pthread.h>
 28#define sigset_t int
 29#define sigfillset(a)
 30#define pthread_sigmask(a,b,c)
 31#define sigaddset(a,b)
 32#define sigemptyset(s)
 33
 34typedef pthread_mutex_t xmutex_t;
 35#define X_MUTEX_INIT           PTHREAD_MUTEX_INITIALIZER
 36#define X_MUTEX_CREATE(mutex)  pthread_mutex_init (&(mutex), 0)
 37#define X_LOCK(mutex)          pthread_mutex_lock (&(mutex))
 38#define X_UNLOCK(mutex)        pthread_mutex_unlock (&(mutex))
 39
 40typedef pthread_cond_t xcond_t;
 41#define X_COND_INIT                     PTHREAD_COND_INITIALIZER
 42#define X_COND_CREATE(cond)		pthread_cond_init (&(cond), 0)
 43#define X_COND_SIGNAL(cond)             pthread_cond_signal (&(cond))
 44#define X_COND_WAIT(cond,mutex)         pthread_cond_wait (&(cond), &(mutex))
 45#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
 46
 47typedef pthread_t xthread_t;
 48#define X_THREAD_PROC(name) void *name (void *thr_arg)
 49#define X_THREAD_ATFORK(a,b,c)
 50
 51static int
 52thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
 53{
 54  int retval;
 55  pthread_attr_t attr;
 56
 57  pthread_attr_init (&attr);
 58  pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
 59
 60  retval = pthread_create (tid, &attr, proc, arg) == 0;
 61
 62  pthread_attr_destroy (&attr);
 63
 64  return retval;
 65}
 66
 67#define respipe_read(a,b,c)  PerlSock_recv ((a), (b), (c), 0)
 68#define respipe_write(a,b,c) send ((a), (b), (c), 0)
 69#define respipe_close(a)     PerlSock_closesocket ((a))
 70
 71#else
 72/////////////////////////////////////////////////////////////////////////////
 73
 74#if __linux && !defined(_GNU_SOURCE)
 75# define _GNU_SOURCE
 76#endif
 77
 78/* just in case */
 79#define _REENTRANT 1
 80
 81#if __solaris
 82# define _POSIX_PTHREAD_SEMANTICS 1
 83/* try to bribe solaris headers into providing a current pthread API
 84 * despite environment being configured for an older version.
 85 */
 86# define __EXTENSIONS__ 1
 87#endif
 88
 89#include <unistd.h>
 90#include <fcntl.h>
 91#include <signal.h>
 92#include <limits.h>
 93#include <pthread.h>
 94
 95typedef pthread_mutex_t xmutex_t;
 96#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
 97# define X_MUTEX_INIT		PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
 98# define X_MUTEX_CREATE(mutex)						\
 99  do {									\
100    pthread_mutexattr_t attr;						\
101    pthread_mutexattr_init (&attr);					\
102    pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP);	\
103    pthread_mutex_init (&(mutex), &attr);				\
104  } while (0)
105#else
106# define X_MUTEX_INIT		PTHREAD_MUTEX_INITIALIZER
107# define X_MUTEX_CREATE(mutex)	pthread_mutex_init (&(mutex), 0)
108#endif
109#define X_LOCK(mutex)		pthread_mutex_lock   (&(mutex))
110#define X_UNLOCK(mutex)		pthread_mutex_unlock (&(mutex))
111
112typedef pthread_cond_t xcond_t;
113#define X_COND_INIT			PTHREAD_COND_INITIALIZER
114#define X_COND_CREATE(cond)		pthread_cond_init (&(cond), 0)
115#define X_COND_SIGNAL(cond)		pthread_cond_signal (&(cond))
116#define X_COND_WAIT(cond,mutex)		pthread_cond_wait (&(cond), &(mutex))
117#define X_COND_TIMEDWAIT(cond,mutex,to)	pthread_cond_timedwait (&(cond), &(mutex), &(to))
118
119typedef pthread_t xthread_t;
120#define X_THREAD_PROC(name) static void *name (void *thr_arg)
121#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
122
123// the broken bsd's once more
124#ifndef PTHREAD_STACK_MIN
125# define PTHREAD_STACK_MIN 0
126#endif
127
128#ifndef XTHREAD_STACKSIZE
129# define XTHREAD_STACKSIZE sizeof (void *) * 4096
130#endif
131
132static int
133thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
134{
135  int retval;
136  sigset_t fullsigset, oldsigset;
137  pthread_attr_t attr;
138
139  pthread_attr_init (&attr);
140  pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
141  pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
142#ifdef PTHREAD_SCOPE_PROCESS
143  pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
144#endif
145
146  sigfillset (&fullsigset);
147
148  pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
149  retval = pthread_create (tid, &attr, proc, arg) == 0;
150  pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
151
152  pthread_attr_destroy (&attr);
153
154  return retval;
155}
156
157#define respipe_read(a,b,c)  read  ((a), (b), (c))
158#define respipe_write(a,b,c) write ((a), (b), (c))
159#define respipe_close(a)     close ((a))
160
161#endif
162
163#endif
164