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

Comparing libeio/xthread.h (file contents):
Revision 1.1 by root, Sat May 10 17:16:39 2008 UTC vs.
Revision 1.13 by root, Tue Jul 19 04:57:10 2011 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
22#define _WIN32_WINNT 0x400 21#define _WIN32_WINNT 0x400
23#include <stdio.h>//D 22#include <stdio.h>//D
24#include <fcntl.h> 23#include <fcntl.h>
33#define pthread_sigmask(a,b,c) 32#define pthread_sigmask(a,b,c)
34#define sigaddset(a,b) 33#define sigaddset(a,b)
35#define sigemptyset(s) 34#define sigemptyset(s)
36#define sigfillset(s) 35#define sigfillset(s)
37 36
38typedef pthread_mutex_t mutex_t; 37typedef pthread_mutex_t xmutex_t;
39#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 38#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
40#define X_MUTEX_CHECK(mutex) 39#define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
41#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) 40#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
42#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 41#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
43 42
44typedef pthread_cond_t cond_t; 43typedef pthread_cond_t xcond_t;
45#define X_COND_INIT PTHREAD_COND_INITIALIZER 44#define X_COND_INIT PTHREAD_COND_INITIALIZER
46#define X_COND_CHECK(cond) 45#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
47#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 46#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
48#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 47#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)) 48#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
50 49
51typedef pthread_t thread_t; 50typedef pthread_t xthread_t;
52#define X_THREAD_PROC(name) void *name (void *thr_arg) 51#define X_THREAD_PROC(name) void *name (void *thr_arg)
53#define X_THREAD_ATFORK(a,b,c) 52#define X_THREAD_ATFORK(a,b,c)
54 53
55static int 54static int
56thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 55thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
57{ 56{
57 int retval;
58 pthread_attr_t attr; 58 pthread_attr_t attr;
59 59
60 pthread_attr_init (&attr); 60 pthread_attr_init (&attr);
61 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 61 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
62 62
63 return pthread_create (tid, &attr, proc, arg) == 0; 63 retval = pthread_create (tid, &attr, proc, arg) == 0;
64
65 pthread_attr_destroy (&attr);
66
67 return retval;
64} 68}
65 69
66#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0) 70#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
67#define respipe_write(a,b,c) send ((a), (b), (c), 0) 71#define respipe_write(a,b,c) send ((a), (b), (c), 0)
68#define respipe_close(a) PerlSock_closesocket ((a)) 72#define respipe_close(a) PerlSock_closesocket ((a))
89#include <fcntl.h> 93#include <fcntl.h>
90#include <signal.h> 94#include <signal.h>
91#include <limits.h> 95#include <limits.h>
92#include <pthread.h> 96#include <pthread.h>
93 97
94typedef pthread_mutex_t mutex_t; 98typedef pthread_mutex_t xmutex_t;
95#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 99#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
96# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 100# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
101# define X_MUTEX_CREATE(mutex) \
102 do { \
103 pthread_mutexattr_t attr; \
104 pthread_mutexattr_init (&attr); \
105 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \
106 pthread_mutex_init (&(mutex), &attr); \
107 } while (0)
97#else 108#else
98# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 109# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
110# define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
99#endif 111#endif
100#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) 112#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
101#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 113#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
102 114
103typedef pthread_cond_t cond_t; 115typedef pthread_cond_t xcond_t;
104#define X_COND_INIT PTHREAD_COND_INITIALIZER 116#define X_COND_INIT PTHREAD_COND_INITIALIZER
117#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
105#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 118#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
106#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 119#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
107#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) 120#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
108 121
109typedef pthread_t thread_t; 122typedef pthread_t xthread_t;
110#define X_THREAD_PROC(name) static void *name (void *thr_arg) 123#define X_THREAD_PROC(name) static void *name (void *thr_arg)
111#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) 124#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
112 125
126// the broken bsd's once more
127#ifndef PTHREAD_STACK_MIN
128# define PTHREAD_STACK_MIN 0
129#endif
130
131#ifndef X_STACKSIZE
132# define X_STACKSIZE sizeof (void *) * 4096
133#endif
134
113static int 135static int
114thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 136thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
115{ 137{
116 int retval; 138 int retval;
117 sigset_t fullsigset, oldsigset; 139 sigset_t fullsigset, oldsigset;
118 pthread_attr_t attr; 140 pthread_attr_t attr;
119 141
120 pthread_attr_init (&attr); 142 pthread_attr_init (&attr);
121 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 143 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
122 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096 144 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
123 ? sizeof (long) * 4096 : PTHREAD_STACK_MIN);
124#ifdef PTHREAD_SCOPE_PROCESS 145#ifdef PTHREAD_SCOPE_PROCESS
125 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); 146 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
126#endif 147#endif
127 148
128 sigfillset (&fullsigset); 149 sigfillset (&fullsigset);
129 150
130 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); 151 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
131 retval = pthread_create (tid, &attr, proc, arg) == 0; 152 retval = pthread_create (tid, &attr, proc, arg) == 0;
132 pthread_sigmask (SIG_SETMASK, &oldsigset, 0); 153 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
154
155 pthread_attr_destroy (&attr);
133 156
134 return retval; 157 return retval;
135} 158}
136 159
137#define respipe_read(a,b,c) read ((a), (b), (c)) 160#define respipe_read(a,b,c) read ((a), (b), (c))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines