1 |
#ifndef XTHREAD_H_ |
2 |
#define XTHREAD_H_ |
3 |
|
4 |
/* whether word reads are potentially non-atomic. |
5 |
* this is conservatice, 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 |
typedef int ssize_t; |
20 |
|
21 |
#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls |
22 |
#define _WIN32_WINNT 0x400 |
23 |
#include <stdio.h>//D |
24 |
#include <fcntl.h> |
25 |
#include <io.h> |
26 |
#include <time.h> |
27 |
#include <winsock2.h> |
28 |
#include <process.h> |
29 |
#include <windows.h> |
30 |
#include <pthread.h> |
31 |
#define sigset_t int |
32 |
#define pthread_sigmask(a,b,c) |
33 |
#define sigaddset(a,b) |
34 |
#define sigemptyset(s) |
35 |
#define sigfillset(s) |
36 |
|
37 |
typedef pthread_mutex_t xmutex_t; |
38 |
#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
39 |
#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
40 |
#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
41 |
|
42 |
typedef pthread_cond_t xcond_t; |
43 |
#define X_COND_INIT PTHREAD_COND_INITIALIZER |
44 |
#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
45 |
#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
46 |
#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
47 |
|
48 |
typedef pthread_t xthread_t; |
49 |
#define X_THREAD_PROC(name) void *name (void *thr_arg) |
50 |
#define X_THREAD_ATFORK(a,b,c) |
51 |
|
52 |
static int |
53 |
thread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
54 |
{ |
55 |
int retval; |
56 |
pthread_attr_t attr; |
57 |
|
58 |
pthread_attr_init (&attr); |
59 |
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
60 |
|
61 |
retval = pthread_create (tid, &attr, proc, arg) == 0; |
62 |
|
63 |
pthread_attr_destroy (&attr); |
64 |
|
65 |
return retval; |
66 |
} |
67 |
|
68 |
#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0) |
69 |
#define respipe_write(a,b,c) send ((a), (b), (c), 0) |
70 |
#define respipe_close(a) PerlSock_closesocket ((a)) |
71 |
|
72 |
#else |
73 |
///////////////////////////////////////////////////////////////////////////// |
74 |
|
75 |
#if __linux && !defined(_GNU_SOURCE) |
76 |
# define _GNU_SOURCE |
77 |
#endif |
78 |
|
79 |
/* just in case */ |
80 |
#define _REENTRANT 1 |
81 |
|
82 |
#if __solaris |
83 |
# define _POSIX_PTHREAD_SEMANTICS 1 |
84 |
/* try to bribe solaris headers into providing a current pthread API |
85 |
* despite environment being configured for an older version. |
86 |
*/ |
87 |
# define __EXTENSIONS__ 1 |
88 |
#endif |
89 |
|
90 |
#include <unistd.h> |
91 |
#include <fcntl.h> |
92 |
#include <signal.h> |
93 |
#include <limits.h> |
94 |
#include <pthread.h> |
95 |
|
96 |
typedef pthread_mutex_t xmutex_t; |
97 |
#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
98 |
# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
99 |
#else |
100 |
# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
101 |
#endif |
102 |
#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
103 |
#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
104 |
|
105 |
typedef pthread_cond_t xcond_t; |
106 |
#define X_COND_INIT PTHREAD_COND_INITIALIZER |
107 |
#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
108 |
#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
109 |
#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
110 |
|
111 |
typedef pthread_t xthread_t; |
112 |
#define X_THREAD_PROC(name) static void *name (void *thr_arg) |
113 |
#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) |
114 |
|
115 |
// the broken bsd's once more |
116 |
#ifndef PTHREAD_STACK_MIN |
117 |
# define PTHREAD_STACK_MIN 0 |
118 |
#endif |
119 |
|
120 |
#ifndef X_STACKSIZE |
121 |
# define X_STACKSIZE sizeof (long) * 4096 |
122 |
#endif |
123 |
|
124 |
static int |
125 |
thread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
126 |
{ |
127 |
int retval; |
128 |
sigset_t fullsigset, oldsigset; |
129 |
pthread_attr_t attr; |
130 |
|
131 |
pthread_attr_init (&attr); |
132 |
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
133 |
pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN); |
134 |
#ifdef PTHREAD_SCOPE_PROCESS |
135 |
pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
136 |
#endif |
137 |
|
138 |
sigfillset (&fullsigset); |
139 |
|
140 |
pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); |
141 |
retval = pthread_create (tid, &attr, proc, arg) == 0; |
142 |
pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
143 |
|
144 |
pthread_attr_destroy (&attr); |
145 |
|
146 |
return retval; |
147 |
} |
148 |
|
149 |
#define respipe_read(a,b,c) read ((a), (b), (c)) |
150 |
#define respipe_write(a,b,c) write ((a), (b), (c)) |
151 |
#define respipe_close(a) close ((a)) |
152 |
|
153 |
#endif |
154 |
|
155 |
#endif |
156 |
|