ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/xthread.h
Revision: 1.16
Committed: Thu Oct 11 03:20:52 2012 UTC (11 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_17, rel-4_18, rel-4_19
Changes since 1.15: +2 -2 lines
Log Message:
4.17

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