ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/xthread.h
Revision: 1.19
Committed: Tue Aug 14 09:29:50 2018 UTC (5 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_6, rel-4_7, rel-4_81, rel-4_80, rel-4_53, rel-4_78, rel-4_79, rel-4_54, rel-4_74, rel-4_75, rel-4_76, rel-4_77, rel-4_71, rel-4_72, rel-4_73, HEAD
Changes since 1.18: +17 -6 lines
Log Message:
4.53

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 root 1.17 //#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls, fails with mingw
21 root 1.19 #if _WIN32_WINNT < 0x400
22     # define _WIN32_WINNT 0x400 // maybe working alternative for mingw
23     #endif
24 root 1.1 #include <stdio.h>//D
25     #include <fcntl.h>
26     #include <io.h>
27     #include <time.h>
28     #include <winsock2.h>
29     #include <process.h>
30     #include <windows.h>
31 root 1.17
32     /* work around some bugs in ptw32 */
33     #if defined(__MINGW32__) && defined(_TIMESPEC_DEFINED)
34     #define HAVE_STRUCT_TIMESPEC 1
35     #endif
36    
37 root 1.1 #include <pthread.h>
38 root 1.19 #ifndef pthread_sigmask
39     # define sigset_t int
40     # define sigfillset(a)
41     # define pthread_sigmask(a,b,c)
42     # define sigaddset(a,b)
43     # define sigemptyset(s)
44     #endif
45 root 1.1
46 root 1.8 typedef pthread_mutex_t xmutex_t;
47 root 1.1 #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
48 root 1.10 #define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
49 root 1.1 #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
50     #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
51    
52 root 1.8 typedef pthread_cond_t xcond_t;
53 root 1.1 #define X_COND_INIT PTHREAD_COND_INITIALIZER
54 root 1.10 #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
55 root 1.1 #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
56     #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
57     #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
58    
59 root 1.8 typedef pthread_t xthread_t;
60 root 1.15 #define X_THREAD_PROC(name) static void *name (void *thr_arg)
61 root 1.1 #define X_THREAD_ATFORK(a,b,c)
62    
63     static int
64 root 1.16 xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
65 root 1.1 {
66 root 1.3 int retval;
67 root 1.19 #if 0
68 root 1.1 pthread_attr_t attr;
69    
70     pthread_attr_init (&attr);
71     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
72    
73 root 1.3 retval = pthread_create (tid, &attr, proc, arg) == 0;
74 root 1.2
75     pthread_attr_destroy (&attr);
76 root 1.19 #else
77     retval = pthread_create (tid, 0, proc, arg) == 0;
78    
79     if (retval)
80     pthread_detach (*tid);
81     #endif
82 root 1.2
83 root 1.3 return retval;
84 root 1.1 }
85    
86     #define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
87     #define respipe_write(a,b,c) send ((a), (b), (c), 0)
88     #define respipe_close(a) PerlSock_closesocket ((a))
89    
90     #else
91     /////////////////////////////////////////////////////////////////////////////
92    
93     #if __linux && !defined(_GNU_SOURCE)
94     # define _GNU_SOURCE
95     #endif
96    
97     /* just in case */
98     #define _REENTRANT 1
99    
100     #if __solaris
101     # define _POSIX_PTHREAD_SEMANTICS 1
102     /* try to bribe solaris headers into providing a current pthread API
103     * despite environment being configured for an older version.
104     */
105     # define __EXTENSIONS__ 1
106     #endif
107    
108     #include <unistd.h>
109     #include <fcntl.h>
110     #include <signal.h>
111     #include <limits.h>
112     #include <pthread.h>
113    
114 root 1.8 typedef pthread_mutex_t xmutex_t;
115 root 1.1 #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
116 root 1.10 # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
117     # define X_MUTEX_CREATE(mutex) \
118     do { \
119     pthread_mutexattr_t attr; \
120     pthread_mutexattr_init (&attr); \
121     pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \
122     pthread_mutex_init (&(mutex), &attr); \
123     } while (0)
124 root 1.1 #else
125 root 1.10 # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
126     # define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
127 root 1.1 #endif
128 root 1.10 #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
129     #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
130 root 1.1
131 root 1.8 typedef pthread_cond_t xcond_t;
132 root 1.10 #define X_COND_INIT PTHREAD_COND_INITIALIZER
133     #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
134     #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
135     #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
136     #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
137 root 1.1
138 root 1.8 typedef pthread_t xthread_t;
139 root 1.1 #define X_THREAD_PROC(name) static void *name (void *thr_arg)
140     #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
141    
142 root 1.5 // the broken bsd's once more
143     #ifndef PTHREAD_STACK_MIN
144     # define PTHREAD_STACK_MIN 0
145     #endif
146    
147 root 1.7 #ifndef X_STACKSIZE
148 root 1.12 # define X_STACKSIZE sizeof (void *) * 4096
149 root 1.6 #endif
150    
151 root 1.1 static int
152 root 1.16 xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
153 root 1.1 {
154     int retval;
155     sigset_t fullsigset, oldsigset;
156     pthread_attr_t attr;
157    
158     pthread_attr_init (&attr);
159     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
160 root 1.7 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
161 root 1.1 #ifdef PTHREAD_SCOPE_PROCESS
162     pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
163     #endif
164    
165     sigfillset (&fullsigset);
166    
167     pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
168     retval = pthread_create (tid, &attr, proc, arg) == 0;
169     pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
170    
171 root 1.2 pthread_attr_destroy (&attr);
172    
173 root 1.1 return retval;
174     }
175    
176     #define respipe_read(a,b,c) read ((a), (b), (c))
177     #define respipe_write(a,b,c) write ((a), (b), (c))
178     #define respipe_close(a) close ((a))
179    
180     #endif
181    
182 root 1.18 #if __linux && __GNUC__ >= 4 && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 3 && 0 /* also check arch */
183     /* __thread has little to no advantage over pthread_* in most configurations, so this is not used */
184     # define X_TLS_DECLARE(varname) __thread void *varname
185     # define X_TLS_INIT(varname)
186     # define X_TLS_SET(varname,value) varname = (value)
187     # define X_TLS_GET(varname) varname
188     #else
189     # define X_TLS_DECLARE(varname) pthread_key_t varname
190     # define X_TLS_INIT(varname) do { if (pthread_key_create (&(varname), 0)) abort (); } while (0)
191     # define X_TLS_SET(varname,value) pthread_setspecific (varname, (value))
192     # define X_TLS_GET(varname) pthread_getspecific (varname)
193     #endif
194    
195 root 1.1 #endif
196