ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/xthread.h
(Generate patch)

Comparing libeio/xthread.h (file contents):
Revision 1.2 by root, Mon May 12 12:36:21 2008 UTC vs.
Revision 1.19 by root, Tue Aug 14 09:29:50 2018 UTC

1#ifndef XTHREAD_H_ 1#ifndef XTHREAD_H_
2#define XTHREAD_H_ 2#define XTHREAD_H_
3 3
4/* whether word reads are potentially non-atomic. 4/* whether word reads are potentially non-atomic.
5 * this is conservatice, likely most arches this runs 5 * this is conservative, likely most arches this runs
6 * on have atomic word read/writes. 6 * on have atomic word read/writes.
7 */ 7 */
8#ifndef WORDACCESS_UNSAFE 8#ifndef WORDACCESS_UNSAFE
9# if __i386 || __x86_64 9# if __i386 || __x86_64
10# define WORDACCESS_UNSAFE 0 10# define WORDACCESS_UNSAFE 0
14#endif 14#endif
15 15
16///////////////////////////////////////////////////////////////////////////// 16/////////////////////////////////////////////////////////////////////////////
17 17
18#ifdef _WIN32 18#ifdef _WIN32
19typedef int ssize_t;
20 19
21#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls 20//#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls, fails with mingw
22#define _WIN32_WINNT 0x400 21#if _WIN32_WINNT < 0x400
22# define _WIN32_WINNT 0x400 // maybe working alternative for mingw
23#endif
23#include <stdio.h>//D 24#include <stdio.h>//D
24#include <fcntl.h> 25#include <fcntl.h>
25#include <io.h> 26#include <io.h>
26#include <time.h> 27#include <time.h>
27#include <winsock2.h> 28#include <winsock2.h>
28#include <process.h> 29#include <process.h>
29#include <windows.h> 30#include <windows.h>
31
32/* work around some bugs in ptw32 */
33#if defined(__MINGW32__) && defined(_TIMESPEC_DEFINED)
34#define HAVE_STRUCT_TIMESPEC 1
35#endif
36
30#include <pthread.h> 37#include <pthread.h>
38#ifndef pthread_sigmask
31#define sigset_t int 39# define sigset_t int
32#define sigfillset(a) 40# define sigfillset(a)
33#define pthread_sigmask(a,b,c) 41# define pthread_sigmask(a,b,c)
34#define sigaddset(a,b) 42# define sigaddset(a,b)
35#define sigemptyset(s) 43# define sigemptyset(s)
36#define sigfillset(s) 44#endif
37 45
38typedef pthread_mutex_t mutex_t; 46typedef pthread_mutex_t xmutex_t;
39#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 47#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
40#define X_MUTEX_CHECK(mutex) 48#define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
41#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) 49#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
42#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 50#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
43 51
44typedef pthread_cond_t cond_t; 52typedef pthread_cond_t xcond_t;
45#define X_COND_INIT PTHREAD_COND_INITIALIZER 53#define X_COND_INIT PTHREAD_COND_INITIALIZER
46#define X_COND_CHECK(cond) 54#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
47#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 55#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
48#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 56#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
49#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) 57#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
50 58
51typedef pthread_t thread_t; 59typedef pthread_t xthread_t;
52#define X_THREAD_PROC(name) void *name (void *thr_arg) 60#define X_THREAD_PROC(name) static void *name (void *thr_arg)
53#define X_THREAD_ATFORK(a,b,c) 61#define X_THREAD_ATFORK(a,b,c)
54 62
55static int 63static int
56thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 64xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
57{ 65{
58 int res; 66 int retval;
67#if 0
59 pthread_attr_t attr; 68 pthread_attr_t attr;
60 69
61 pthread_attr_init (&attr); 70 pthread_attr_init (&attr);
62 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 71 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
63 72
64 res = pthread_create (tid, &attr, proc, arg) == 0; 73 retval = pthread_create (tid, &attr, proc, arg) == 0;
65 74
66 pthread_attr_destroy (&attr); 75 pthread_attr_destroy (&attr);
76#else
77 retval = pthread_create (tid, 0, proc, arg) == 0;
67 78
79 if (retval)
80 pthread_detach (*tid);
81#endif
82
68 return res; 83 return retval;
69} 84}
70 85
71#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0) 86#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
72#define respipe_write(a,b,c) send ((a), (b), (c), 0) 87#define respipe_write(a,b,c) send ((a), (b), (c), 0)
73#define respipe_close(a) PerlSock_closesocket ((a)) 88#define respipe_close(a) PerlSock_closesocket ((a))
94#include <fcntl.h> 109#include <fcntl.h>
95#include <signal.h> 110#include <signal.h>
96#include <limits.h> 111#include <limits.h>
97#include <pthread.h> 112#include <pthread.h>
98 113
99typedef pthread_mutex_t mutex_t; 114typedef pthread_mutex_t xmutex_t;
100#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 115#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
101# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 116# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
117# define X_MUTEX_CREATE(mutex) \
118 do { \
119 pthread_mutexattr_t attr; \
120 pthread_mutexattr_init (&attr); \
121 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \
122 pthread_mutex_init (&(mutex), &attr); \
123 } while (0)
102#else 124#else
103# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 125# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
126# define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
104#endif 127#endif
105#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) 128#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
106#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 129#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
107 130
108typedef pthread_cond_t cond_t; 131typedef pthread_cond_t xcond_t;
109#define X_COND_INIT PTHREAD_COND_INITIALIZER 132#define X_COND_INIT PTHREAD_COND_INITIALIZER
133#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
110#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 134#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
111#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 135#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
112#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) 136#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
113 137
114typedef pthread_t thread_t; 138typedef pthread_t xthread_t;
115#define X_THREAD_PROC(name) static void *name (void *thr_arg) 139#define X_THREAD_PROC(name) static void *name (void *thr_arg)
116#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) 140#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
117 141
142// the broken bsd's once more
143#ifndef PTHREAD_STACK_MIN
144# define PTHREAD_STACK_MIN 0
145#endif
146
147#ifndef X_STACKSIZE
148# define X_STACKSIZE sizeof (void *) * 4096
149#endif
150
118static int 151static int
119thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 152xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
120{ 153{
121 int retval; 154 int retval;
122 sigset_t fullsigset, oldsigset; 155 sigset_t fullsigset, oldsigset;
123 pthread_attr_t attr; 156 pthread_attr_t attr;
124 157
125 pthread_attr_init (&attr); 158 pthread_attr_init (&attr);
126 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 159 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
127 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096 160 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
128 ? sizeof (long) * 4096 : PTHREAD_STACK_MIN);
129#ifdef PTHREAD_SCOPE_PROCESS 161#ifdef PTHREAD_SCOPE_PROCESS
130 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); 162 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
131#endif 163#endif
132 164
133 sigfillset (&fullsigset); 165 sigfillset (&fullsigset);
145#define respipe_write(a,b,c) write ((a), (b), (c)) 177#define respipe_write(a,b,c) write ((a), (b), (c))
146#define respipe_close(a) close ((a)) 178#define respipe_close(a) close ((a))
147 179
148#endif 180#endif
149 181
182#if __linux && __GNUC__ >= 4 && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 3 && 0 /* also check arch */
183/* __thread has little to no advantage over pthread_* in most configurations, so this is not used */
184# define X_TLS_DECLARE(varname) __thread void *varname
185# define X_TLS_INIT(varname)
186# define X_TLS_SET(varname,value) varname = (value)
187# define X_TLS_GET(varname) varname
188#else
189# define X_TLS_DECLARE(varname) pthread_key_t varname
190# define X_TLS_INIT(varname) do { if (pthread_key_create (&(varname), 0)) abort (); } while (0)
191# define X_TLS_SET(varname,value) pthread_setspecific (varname, (value))
192# define X_TLS_GET(varname) pthread_getspecific (varname)
150#endif 193#endif
151 194
195#endif
196

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines