The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / libeio / xthread.h @ 822d7fa2

History | View | Annotate | Download (4.2 KB)

1
#ifndef XTHREAD_H_
2
#define XTHREAD_H_
3

    
4
/* whether word reads are potentially non-atomic.
5
 * this is conservatice, likely most arches this runs
6
 * 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
typedef 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 sigfillset(a)
33
#define pthread_sigmask(a,b,c)
34
#define sigaddset(a,b)
35
#define sigemptyset(s)
36
#define sigfillset(s)
37

    
38
typedef pthread_mutex_t mutex_t;
39
#define X_MUTEX_INIT           PTHREAD_MUTEX_INITIALIZER
40
#define X_LOCK(mutex)          pthread_mutex_lock (&(mutex))
41
#define X_UNLOCK(mutex)        pthread_mutex_unlock (&(mutex))
42

    
43
typedef pthread_cond_t cond_t;
44
#define X_COND_INIT                     PTHREAD_COND_INITIALIZER
45
#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
typedef pthread_t thread_t;
50
#define X_THREAD_PROC(name) void *name (void *thr_arg)
51
#define X_THREAD_ATFORK(a,b,c)
52

    
53
static int
54
thread_create (thread_t *tid, void *(*proc)(void *), void *arg)
55
{
56
  int retval;
57
  pthread_attr_t attr;
58

    
59
  pthread_attr_init (&attr);
60
  pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
61

    
62
  retval = pthread_create (tid, &attr, proc, arg) == 0;
63

    
64
  pthread_attr_destroy (&attr);
65

    
66
  return retval;
67
}
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
typedef pthread_mutex_t mutex_t;
98
#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
99
# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
100
#else
101
# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
102
#endif
103
#define X_LOCK(mutex)   pthread_mutex_lock   (&(mutex))
104
#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
105

    
106
typedef pthread_cond_t cond_t;
107
#define X_COND_INIT PTHREAD_COND_INITIALIZER
108
#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
109
#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))
111

    
112
typedef pthread_t thread_t;
113
#define X_THREAD_PROC(name) static void *name (void *thr_arg)
114
#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
115

    
116
// the broken bsd's once more
117
#ifndef PTHREAD_STACK_MIN
118
# define PTHREAD_STACK_MIN 0
119
#endif
120

    
121
static int
122
thread_create (thread_t *tid, void *(*proc)(void *), void *arg)
123
{
124
  int retval;
125
  sigset_t fullsigset, oldsigset;
126
  pthread_attr_t attr;
127

    
128
  pthread_attr_init (&attr);
129
  pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
130
  pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096 * 4
131
                                    ? sizeof (long) * 4096 * 4 : PTHREAD_STACK_MIN);
132
#ifdef PTHREAD_SCOPE_PROCESS
133
  pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
134
#endif
135

    
136
  sigfillset (&fullsigset);
137

    
138
  pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
139
  retval = pthread_create (tid, &attr, proc, arg) == 0;
140
  pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
141

    
142
  pthread_attr_destroy (&attr);
143

    
144
  return retval;
145
}
146

    
147
#define respipe_read(a,b,c)  read  ((a), (b), (c))
148
#define respipe_write(a,b,c) write ((a), (b), (c))
149
#define respipe_close(a)     close ((a))
150

    
151
#endif
152

    
153
#endif
154