ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/xthread.h
Revision: 1.13
Committed: Tue Jul 19 04:57:10 2011 UTC (12 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +0 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 #define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls
21 #define _WIN32_WINNT 0x400
22 #include <stdio.h>//D
23 #include <fcntl.h>
24 #include <io.h>
25 #include <time.h>
26 #include <winsock2.h>
27 #include <process.h>
28 #include <windows.h>
29 #include <pthread.h>
30 #define sigset_t int
31 #define sigfillset(a)
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_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
40 #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
41 #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
42
43 typedef pthread_cond_t xcond_t;
44 #define X_COND_INIT PTHREAD_COND_INITIALIZER
45 #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
46 #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
47 #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
48 #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
49
50 typedef pthread_t xthread_t;
51 #define X_THREAD_PROC(name) void *name (void *thr_arg)
52 #define X_THREAD_ATFORK(a,b,c)
53
54 static int
55 thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
56 {
57 int retval;
58 pthread_attr_t attr;
59
60 pthread_attr_init (&attr);
61 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
62
63 retval = pthread_create (tid, &attr, proc, arg) == 0;
64
65 pthread_attr_destroy (&attr);
66
67 return retval;
68 }
69
70 #define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
71 #define respipe_write(a,b,c) send ((a), (b), (c), 0)
72 #define respipe_close(a) PerlSock_closesocket ((a))
73
74 #else
75 /////////////////////////////////////////////////////////////////////////////
76
77 #if __linux && !defined(_GNU_SOURCE)
78 # define _GNU_SOURCE
79 #endif
80
81 /* just in case */
82 #define _REENTRANT 1
83
84 #if __solaris
85 # define _POSIX_PTHREAD_SEMANTICS 1
86 /* try to bribe solaris headers into providing a current pthread API
87 * despite environment being configured for an older version.
88 */
89 # define __EXTENSIONS__ 1
90 #endif
91
92 #include <unistd.h>
93 #include <fcntl.h>
94 #include <signal.h>
95 #include <limits.h>
96 #include <pthread.h>
97
98 typedef pthread_mutex_t xmutex_t;
99 #if __linux && defined (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)
108 #else
109 # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
110 # define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
111 #endif
112 #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
113 #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
114
115 typedef pthread_cond_t xcond_t;
116 #define X_COND_INIT PTHREAD_COND_INITIALIZER
117 #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
118 #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
119 #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
120 #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
121
122 typedef pthread_t xthread_t;
123 #define X_THREAD_PROC(name) static void *name (void *thr_arg)
124 #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
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
135 static int
136 thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
137 {
138 int retval;
139 sigset_t fullsigset, oldsigset;
140 pthread_attr_t attr;
141
142 pthread_attr_init (&attr);
143 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
144 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
145 #ifdef PTHREAD_SCOPE_PROCESS
146 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
147 #endif
148
149 sigfillset (&fullsigset);
150
151 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
152 retval = pthread_create (tid, &attr, proc, arg) == 0;
153 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
154
155 pthread_attr_destroy (&attr);
156
157 return retval;
158 }
159
160 #define respipe_read(a,b,c) read ((a), (b), (c))
161 #define respipe_write(a,b,c) write ((a), (b), (c))
162 #define respipe_close(a) close ((a))
163
164 #endif
165
166 #endif
167