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

Comparing BDB/xthread.h (file contents):
Revision 1.1 by root, Sat May 12 21:18:13 2007 UTC vs.
Revision 1.10 by root, Sat Oct 23 09:40:53 2010 UTC

1/* solaris */ 1#ifndef XTHREAD_H_
2#define _POSIX_PTHREAD_SEMANTICS 1 2#define XTHREAD_H_
3 3
4#if __linux && !defined(_GNU_SOURCE)
5# define _GNU_SOURCE
6#endif
7
8/* just in case */
9#define _REENTRANT 1
10
11#include <signal.h>
12#include <pthread.h>
13
14#ifndef PTHREAD_STACK_MIN
15/* care for broken platforms, e.g. windows */
16# define PTHREAD_STACK_MIN 16384
17#endif
18
19#if __ia64
20# define STACKSIZE 65536
21#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
22# define STACKSIZE PTHREAD_STACK_MIN
23#else
24# define STACKSIZE 16384
25#endif
26
27/* wether word reads are potentially non-atomic. 4/* whether word reads are potentially non-atomic.
28 * this is conservatice, likely most arches this runs 5 * this is conservatice, likely most arches this runs
29 * on have atomic word read/writes. 6 * on have atomic word read/writes.
30 */ 7 */
31#ifndef WORDACCESS_UNSAFE 8#ifndef WORDACCESS_UNSAFE
32# if __i386 || __x86_64 9# if __i386 || __x86_64
34# else 11# else
35# define WORDACCESS_UNSAFE 1 12# define WORDACCESS_UNSAFE 1
36# endif 13# endif
37#endif 14#endif
38 15
16/////////////////////////////////////////////////////////////////////////////
17
18#ifdef _WIN32
19typedef 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 pthread_sigmask(a,b,c)
33#define sigaddset(a,b)
34#define sigemptyset(s)
35#define sigfillset(s)
36
39typedef pthread_mutex_t mutex_t; 37typedef pthread_mutex_t xmutex_t;
40#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
41# define MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 38#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
39#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
40#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
41
42typedef pthread_cond_t xcond_t;
43#define X_COND_INIT PTHREAD_COND_INITIALIZER
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
48typedef pthread_t xthread_t;
49#define X_THREAD_PROC(name) void *name (void *thr_arg)
50#define X_THREAD_ATFORK(a,b,c)
51
52static int
53thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
54{
55 int retval;
56 pthread_attr_t attr;
57
58 pthread_attr_init (&attr);
59 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
60
61 retval = pthread_create (tid, &attr, proc, arg) == 0;
62
63 pthread_attr_destroy (&attr);
64
65 return retval;
66}
67
68#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
69#define respipe_write(a,b,c) send ((a), (b), (c), 0)
70#define respipe_close(a) PerlSock_closesocket ((a))
71
42#else 72#else
43# define MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 73/////////////////////////////////////////////////////////////////////////////
74
75#if __linux && !defined(_GNU_SOURCE)
76# define _GNU_SOURCE
44#endif 77#endif
45 78
46typedef pthread_cond_t cond_t; 79/* just in case */
47#define COND_INIT PTHREAD_COND_INITIALIZER 80#define _REENTRANT 1
48 81
49#define LOCK(mutex) pthread_mutex_lock (&(mutex)) 82#if __solaris
50#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 83# define _POSIX_PTHREAD_SEMANTICS 1
84/* try to bribe solaris headers into providing a current pthread API
85 * despite environment being configured for an older version.
86 */
87# define __EXTENSIONS__ 1
88#endif
51 89
52#define COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 90#include <unistd.h>
53#define COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 91#include <fcntl.h>
54#define COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) 92#include <signal.h>
93#include <limits.h>
94#include <pthread.h>
55 95
56typedef pthread_t thread_t; 96typedef pthread_mutex_t xmutex_t;
97#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
98# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
99#else
100# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
101#endif
102#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
103#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
57 104
105typedef pthread_cond_t xcond_t;
106#define X_COND_INIT PTHREAD_COND_INITIALIZER
107#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
108#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
109#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
110
111typedef pthread_t xthread_t;
112#define X_THREAD_PROC(name) static void *name (void *thr_arg)
113#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
114
115// the broken bsd's once more
116#ifndef PTHREAD_STACK_MIN
117# define PTHREAD_STACK_MIN 0
118#endif
119
120#ifndef X_STACKSIZE
121# define X_STACKSIZE sizeof (long) * 4096
122#endif
123
124static int
58static int thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 125thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
59{ 126{
60 int retval; 127 int retval;
61 sigset_t fullsigset, oldsigset; 128 sigset_t fullsigset, oldsigset;
62 pthread_attr_t attr; 129 pthread_attr_t attr;
63 130
64 pthread_attr_init (&attr); 131 pthread_attr_init (&attr);
65 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 132 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
133 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
66#ifdef PTHREAD_SCOPE_PROCESS 134#ifdef PTHREAD_SCOPE_PROCESS
67 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); 135 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
68#endif 136#endif
69 137
70 sigfillset (&fullsigset); 138 sigfillset (&fullsigset);
71 139
72 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); 140 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
73 retval = pthread_create (tid, &attr, proc, arg) == 0; 141 retval = pthread_create (tid, &attr, proc, arg) == 0;
74 pthread_sigmask (SIG_SETMASK, &oldsigset, 0); 142 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
75 143
144 pthread_attr_destroy (&attr);
145
76 return retval; 146 return retval;
77} 147}
78 148
79#define ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) 149#define respipe_read(a,b,c) read ((a), (b), (c))
150#define respipe_write(a,b,c) write ((a), (b), (c))
151#define respipe_close(a) close ((a))
80 152
153#endif
154
155#endif
156

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines