ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/xthread.h
Revision: 1.1
Committed: Sat May 10 17:16:39 2008 UTC (16 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_01, rel-3_02
Log Message:
initial check-in

File Contents

# Content
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 sigfillset(a)
33 #define pthread_sigmask(a,b,c)
34 #define sigaddset(a,b)
35 #define sigemptyset(s)
36 #define sigfillset(s)
37
38 typedef pthread_mutex_t mutex_t;
39 #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
40 #define X_MUTEX_CHECK(mutex)
41 #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
42 #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
43
44 typedef pthread_cond_t cond_t;
45 #define X_COND_INIT PTHREAD_COND_INITIALIZER
46 #define X_COND_CHECK(cond)
47 #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 typedef pthread_t thread_t;
52 #define X_THREAD_PROC(name) void *name (void *thr_arg)
53 #define X_THREAD_ATFORK(a,b,c)
54
55 static int
56 thread_create (thread_t *tid, void *(*proc)(void *), void *arg)
57 {
58 pthread_attr_t attr;
59
60 pthread_attr_init (&attr);
61 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
62
63 return pthread_create (tid, &attr, proc, arg) == 0;
64 }
65
66 #define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
67 #define respipe_write(a,b,c) send ((a), (b), (c), 0)
68 #define respipe_close(a) PerlSock_closesocket ((a))
69
70 #else
71 /////////////////////////////////////////////////////////////////////////////
72
73 #if __linux && !defined(_GNU_SOURCE)
74 # define _GNU_SOURCE
75 #endif
76
77 /* just in case */
78 #define _REENTRANT 1
79
80 #if __solaris
81 # define _POSIX_PTHREAD_SEMANTICS 1
82 /* try to bribe solaris headers into providing a current pthread API
83 * despite environment being configured for an older version.
84 */
85 # define __EXTENSIONS__ 1
86 #endif
87
88 #include <unistd.h>
89 #include <fcntl.h>
90 #include <signal.h>
91 #include <limits.h>
92 #include <pthread.h>
93
94 typedef pthread_mutex_t mutex_t;
95 #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
96 # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
97 #else
98 # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
99 #endif
100 #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
101 #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
102
103 typedef pthread_cond_t cond_t;
104 #define X_COND_INIT PTHREAD_COND_INITIALIZER
105 #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
106 #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))
108
109 typedef pthread_t thread_t;
110 #define X_THREAD_PROC(name) static void *name (void *thr_arg)
111 #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
112
113 static int
114 thread_create (thread_t *tid, void *(*proc)(void *), void *arg)
115 {
116 int retval;
117 sigset_t fullsigset, oldsigset;
118 pthread_attr_t attr;
119
120 pthread_attr_init (&attr);
121 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
122 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096
123 ? sizeof (long) * 4096 : PTHREAD_STACK_MIN);
124 #ifdef PTHREAD_SCOPE_PROCESS
125 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
126 #endif
127
128 sigfillset (&fullsigset);
129
130 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
131 retval = pthread_create (tid, &attr, proc, arg) == 0;
132 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
133
134 return retval;
135 }
136
137 #define respipe_read(a,b,c) read ((a), (b), (c))
138 #define respipe_write(a,b,c) write ((a), (b), (c))
139 #define respipe_close(a) close ((a))
140
141 #endif
142
143 #endif
144