ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/xthread.h
Revision: 1.8
Committed: Sat May 10 18:06:41 2008 UTC (16 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +0 -0 lines
State: FILE REMOVED
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.2 /* wether word reads are potentially non-atomic.
2     * this is conservatice, likely most arches this runs
3     * on have atomic word read/writes.
4     */
5     #ifndef WORDACCESS_UNSAFE
6     # if __i386 || __x86_64
7     # define WORDACCESS_UNSAFE 0
8     # else
9     # define WORDACCESS_UNSAFE 1
10     # endif
11     #endif
12    
13     /////////////////////////////////////////////////////////////////////////////
14    
15     #ifdef _WIN32
16     typedef int ssize_t;
17    
18     #define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls
19     #define _WIN32_WINNT 0x400
20     #include <stdio.h>//D
21     #include <fcntl.h>
22     #include <io.h>
23 root 1.5 #include <time.h>
24 root 1.2 #include <winsock2.h>
25     #include <process.h>
26     #include <windows.h>
27 root 1.5 #include <pthread.h>
28 root 1.2 #define sigset_t int
29     #define sigfillset(a)
30     #define pthread_sigmask(a,b,c)
31     #define sigaddset(a,b)
32     #define sigemptyset(s)
33     #define sigfillset(s)
34    
35 root 1.5 typedef pthread_mutex_t mutex_t;
36     #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
37     #define X_MUTEX_CHECK(mutex)
38     #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
39     #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
40 root 1.2
41 root 1.5 typedef pthread_cond_t cond_t;
42     #define X_COND_INIT PTHREAD_COND_INITIALIZER
43     #define X_COND_CHECK(cond)
44     #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
45     #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
46     #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
47    
48     typedef pthread_t thread_t;
49     #define X_THREAD_PROC(name) void *name (void *thr_arg)
50     #define X_THREAD_ATFORK(a,b,c)
51 root 1.2
52     static int
53 root 1.5 thread_create (thread_t *tid, void *(*proc)(void *), void *arg)
54 root 1.2 {
55 root 1.5 pthread_attr_t attr;
56 root 1.2
57 root 1.5 pthread_attr_init (&attr);
58     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
59 root 1.2
60 root 1.5 return pthread_create (tid, &attr, proc, arg) == 0;
61 root 1.2 }
62    
63 root 1.4 #define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
64     #define respipe_write(a,b,c) send ((a), (b), (c), 0)
65     #define respipe_close(a) PerlSock_closesocket ((a))
66    
67 root 1.2 #else
68     /////////////////////////////////////////////////////////////////////////////
69    
70 root 1.1 /* solaris */
71     #define _POSIX_PTHREAD_SEMANTICS 1
72    
73     #if __linux && !defined(_GNU_SOURCE)
74     # define _GNU_SOURCE
75     #endif
76    
77     /* just in case */
78     #define _REENTRANT 1
79    
80 root 1.7 #if __solaris
81     /* try to bribe solaris headers into providing a current pthread API
82     * despite perl being configured for an older version.
83     */
84     # define __EXTENSIONS__ 1
85     #endif
86    
87 root 1.2 #include <unistd.h>
88     #include <fcntl.h>
89 root 1.1 #include <signal.h>
90 root 1.6 #include <limits.h>
91 root 1.1 #include <pthread.h>
92    
93     typedef pthread_mutex_t mutex_t;
94     #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
95 root 1.2 # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
96 root 1.1 #else
97 root 1.2 # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
98 root 1.1 #endif
99 root 1.2 #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
100     #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
101 root 1.1
102     typedef pthread_cond_t cond_t;
103 root 1.2 #define X_COND_INIT PTHREAD_COND_INITIALIZER
104     #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
105     #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
106     #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
107 root 1.1
108     typedef pthread_t thread_t;
109 root 1.2 #define X_THREAD_PROC(name) static void *name (void *thr_arg)
110     #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
111 root 1.1
112 root 1.2 static int
113     thread_create (thread_t *tid, void *(*proc)(void *), void *arg)
114 root 1.1 {
115     int retval;
116     sigset_t fullsigset, oldsigset;
117     pthread_attr_t attr;
118    
119     pthread_attr_init (&attr);
120     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
121 root 1.6 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096
122     ? sizeof (long) * 4096 : PTHREAD_STACK_MIN);
123 root 1.1 #ifdef PTHREAD_SCOPE_PROCESS
124     pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
125     #endif
126    
127     sigfillset (&fullsigset);
128    
129     pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
130     retval = pthread_create (tid, &attr, proc, arg) == 0;
131     pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
132    
133     return retval;
134     }
135    
136 root 1.4 #define respipe_read(a,b,c) read ((a), (b), (c))
137     #define respipe_write(a,b,c) write ((a), (b), (c))
138     #define respipe_close(a) close ((a))
139    
140 root 1.2 #endif
141    
142    
143 root 1.1