ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/xthread.h
Revision: 1.10
Committed: Tue Feb 15 03:15:16 2011 UTC (13 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_93, rel-3_92, rel-3_91, rel-3_9, rel-3_8
Changes since 1.9: +20 -8 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.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 root 1.10 #define sigfillset(a)
33 root 1.1 #define pthread_sigmask(a,b,c)
34     #define sigaddset(a,b)
35     #define sigemptyset(s)
36     #define sigfillset(s)
37    
38 root 1.8 typedef pthread_mutex_t xmutex_t;
39 root 1.1 #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
40 root 1.10 #define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
41 root 1.1 #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
42     #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
43    
44 root 1.8 typedef pthread_cond_t xcond_t;
45 root 1.1 #define X_COND_INIT PTHREAD_COND_INITIALIZER
46 root 1.10 #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
47 root 1.1 #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
48     #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))
50    
51 root 1.8 typedef pthread_t xthread_t;
52 root 1.1 #define X_THREAD_PROC(name) void *name (void *thr_arg)
53     #define X_THREAD_ATFORK(a,b,c)
54    
55     static int
56 root 1.8 thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
57 root 1.1 {
58 root 1.3 int retval;
59 root 1.1 pthread_attr_t attr;
60    
61     pthread_attr_init (&attr);
62     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
63    
64 root 1.3 retval = pthread_create (tid, &attr, proc, arg) == 0;
65 root 1.2
66     pthread_attr_destroy (&attr);
67    
68 root 1.3 return retval;
69 root 1.1 }
70    
71     #define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
72     #define respipe_write(a,b,c) send ((a), (b), (c), 0)
73     #define respipe_close(a) PerlSock_closesocket ((a))
74    
75     #else
76     /////////////////////////////////////////////////////////////////////////////
77    
78     #if __linux && !defined(_GNU_SOURCE)
79     # define _GNU_SOURCE
80     #endif
81    
82     /* just in case */
83     #define _REENTRANT 1
84    
85     #if __solaris
86     # define _POSIX_PTHREAD_SEMANTICS 1
87     /* try to bribe solaris headers into providing a current pthread API
88     * despite environment being configured for an older version.
89     */
90     # define __EXTENSIONS__ 1
91     #endif
92    
93     #include <unistd.h>
94     #include <fcntl.h>
95     #include <signal.h>
96     #include <limits.h>
97     #include <pthread.h>
98    
99 root 1.8 typedef pthread_mutex_t xmutex_t;
100 root 1.1 #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
101 root 1.10 # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
102     # define X_MUTEX_CREATE(mutex) \
103     do { \
104     pthread_mutexattr_t attr; \
105     pthread_mutexattr_init (&attr); \
106     pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \
107     pthread_mutex_init (&(mutex), &attr); \
108     } while (0)
109 root 1.1 #else
110 root 1.10 # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
111     # define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
112 root 1.1 #endif
113 root 1.10 #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
114     #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
115 root 1.1
116 root 1.8 typedef pthread_cond_t xcond_t;
117 root 1.10 #define X_COND_INIT PTHREAD_COND_INITIALIZER
118     #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
119     #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
120     #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
121     #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
122 root 1.1
123 root 1.8 typedef pthread_t xthread_t;
124 root 1.1 #define X_THREAD_PROC(name) static void *name (void *thr_arg)
125     #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
126    
127 root 1.5 // the broken bsd's once more
128     #ifndef PTHREAD_STACK_MIN
129     # define PTHREAD_STACK_MIN 0
130     #endif
131    
132 root 1.7 #ifndef X_STACKSIZE
133     # define X_STACKSIZE sizeof (long) * 4096
134 root 1.6 #endif
135    
136 root 1.1 static int
137 root 1.8 thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
138 root 1.1 {
139     int retval;
140     sigset_t fullsigset, oldsigset;
141     pthread_attr_t attr;
142    
143     pthread_attr_init (&attr);
144     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
145 root 1.7 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
146 root 1.1 #ifdef PTHREAD_SCOPE_PROCESS
147     pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
148     #endif
149    
150     sigfillset (&fullsigset);
151    
152     pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
153     retval = pthread_create (tid, &attr, proc, arg) == 0;
154     pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
155    
156 root 1.2 pthread_attr_destroy (&attr);
157    
158 root 1.1 return retval;
159     }
160    
161     #define respipe_read(a,b,c) read ((a), (b), (c))
162     #define respipe_write(a,b,c) write ((a), (b), (c))
163     #define respipe_close(a) close ((a))
164    
165     #endif
166    
167     #endif
168