ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/xthread.h
(Generate patch)

Comparing libeio/xthread.h (file contents):
Revision 1.5 by root, Tue May 20 05:50:49 2008 UTC vs.
Revision 1.15 by root, Tue Apr 24 18:47:50 2012 UTC

1#ifndef XTHREAD_H_ 1#ifndef XTHREAD_H_
2#define XTHREAD_H_ 2#define XTHREAD_H_
3 3
4/* whether word reads are potentially non-atomic. 4/* whether word reads are potentially non-atomic.
5 * this is conservatice, likely most arches this runs 5 * this is conservative, likely most arches this runs
6 * on have atomic word read/writes. 6 * on have atomic word read/writes.
7 */ 7 */
8#ifndef WORDACCESS_UNSAFE 8#ifndef WORDACCESS_UNSAFE
9# if __i386 || __x86_64 9# if __i386 || __x86_64
10# define WORDACCESS_UNSAFE 0 10# define WORDACCESS_UNSAFE 0
14#endif 14#endif
15 15
16///////////////////////////////////////////////////////////////////////////// 16/////////////////////////////////////////////////////////////////////////////
17 17
18#ifdef _WIN32 18#ifdef _WIN32
19typedef int ssize_t;
20 19
21#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls 20#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls
22#define _WIN32_WINNT 0x400 21#define _WIN32_WINNT 0x400
23#include <stdio.h>//D 22#include <stdio.h>//D
24#include <fcntl.h> 23#include <fcntl.h>
31#define sigset_t int 30#define sigset_t int
32#define sigfillset(a) 31#define sigfillset(a)
33#define pthread_sigmask(a,b,c) 32#define pthread_sigmask(a,b,c)
34#define sigaddset(a,b) 33#define sigaddset(a,b)
35#define sigemptyset(s) 34#define sigemptyset(s)
36#define sigfillset(s)
37 35
38typedef pthread_mutex_t mutex_t; 36typedef pthread_mutex_t xmutex_t;
39#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 37#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
38#define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
40#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) 39#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
41#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 40#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
42 41
43typedef pthread_cond_t cond_t; 42typedef pthread_cond_t xcond_t;
44#define X_COND_INIT PTHREAD_COND_INITIALIZER 43#define X_COND_INIT PTHREAD_COND_INITIALIZER
44#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
45#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 45#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
46#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 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)) 47#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
48 48
49typedef pthread_t thread_t; 49typedef pthread_t xthread_t;
50#define X_THREAD_PROC(name) void *name (void *thr_arg) 50#define X_THREAD_PROC(name) static void *name (void *thr_arg)
51#define X_THREAD_ATFORK(a,b,c) 51#define X_THREAD_ATFORK(a,b,c)
52 52
53static int 53static int
54thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 54thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
55{ 55{
56 int retval; 56 int retval;
57 pthread_attr_t attr; 57 pthread_attr_t attr;
58 58
59 pthread_attr_init (&attr); 59 pthread_attr_init (&attr);
92#include <fcntl.h> 92#include <fcntl.h>
93#include <signal.h> 93#include <signal.h>
94#include <limits.h> 94#include <limits.h>
95#include <pthread.h> 95#include <pthread.h>
96 96
97typedef pthread_mutex_t mutex_t; 97typedef pthread_mutex_t xmutex_t;
98#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 98#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
99# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 99# 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)
100#else 107#else
101# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 108# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
109# define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
102#endif 110#endif
103#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) 111#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
104#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 112#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
105 113
106typedef pthread_cond_t cond_t; 114typedef pthread_cond_t xcond_t;
107#define X_COND_INIT PTHREAD_COND_INITIALIZER 115#define X_COND_INIT PTHREAD_COND_INITIALIZER
116#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
108#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 117#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
109#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 118#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
110#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) 119#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
111 120
112typedef pthread_t thread_t; 121typedef pthread_t xthread_t;
113#define X_THREAD_PROC(name) static void *name (void *thr_arg) 122#define X_THREAD_PROC(name) static void *name (void *thr_arg)
114#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) 123#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
115 124
116// the broken bsd's once more 125// the broken bsd's once more
117#ifndef PTHREAD_STACK_MIN 126#ifndef PTHREAD_STACK_MIN
118# define PTHREAD_STACK_MIN 0 127# define PTHREAD_STACK_MIN 0
119#endif 128#endif
120 129
130#ifndef X_STACKSIZE
131# define X_STACKSIZE sizeof (void *) * 4096
132#endif
133
121static int 134static int
122thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 135thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
123{ 136{
124 int retval; 137 int retval;
125 sigset_t fullsigset, oldsigset; 138 sigset_t fullsigset, oldsigset;
126 pthread_attr_t attr; 139 pthread_attr_t attr;
127 140
128 pthread_attr_init (&attr); 141 pthread_attr_init (&attr);
129 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 142 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
130 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096 143 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
131 ? sizeof (long) * 4096 : PTHREAD_STACK_MIN);
132#ifdef PTHREAD_SCOPE_PROCESS 144#ifdef PTHREAD_SCOPE_PROCESS
133 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); 145 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
134#endif 146#endif
135 147
136 sigfillset (&fullsigset); 148 sigfillset (&fullsigset);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines