-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathphp_sync.h
More file actions
211 lines (162 loc) · 4.14 KB
/
php_sync.h
File metadata and controls
211 lines (162 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Direct port of and compatible with the cross platform 'sync' library: https://github.com/cubiclesoft/cross-platform-cpp
This source file is under the MIT license.
(C) 2016 CubicleSoft. All rights reserved.
*/
/* $Id$ */
#ifndef PHP_SYNC_H
#define PHP_SYNC_H
extern zend_module_entry sync_module_entry;
#define phpext_sync_ptr &sync_module_entry
#define PHP_SYNC_VERSION "1.1.3"
#ifdef PHP_WIN32
# define PHP_SYNC_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
# define PHP_SYNC_API __attribute__ ((visibility("default")))
#else
# define PHP_SYNC_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
#ifdef PHP_WIN32
#include <windows.h>
#else
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <limits.h>
#ifdef __APPLE__
#include <mach/clock.h>
#include <mach/mach.h>
#ifndef SHM_NAME_MAX
# define SHM_NAME_MAX 31
#endif
#else
#ifndef SHM_NAME_MAX
# define SHM_NAME_MAX 255
#endif
#endif
#endif
PHP_MINIT_FUNCTION(sync);
PHP_MSHUTDOWN_FUNCTION(sync);
PHP_MINFO_FUNCTION(sync);
#if defined(PHP_WIN32)
typedef DWORD sync_ThreadIDType;
#else
typedef pthread_t sync_ThreadIDType;
#endif
#if PHP_MAJOR_VERSION >= 7
#define PHP_SYNC_PHP_5_zend_object_std
#define PHP_SYNC_PHP_7_zend_object_std zend_object std;
#else
#define PHP_SYNC_PHP_5_zend_object_std zend_object std;
#define PHP_SYNC_PHP_7_zend_object_std
#endif
/* Generic structures */
#if defined(PHP_WIN32)
/* Nothing for Windows at the moment. */
#else
/* Some platforms are broken even for unnamed semaphores (e.g. Mac OSX). */
/* This allows for implementing all semaphores directly, bypassing POSIX semaphores. */
typedef struct _sync_UnixSemaphoreWrapper {
pthread_mutex_t *MxMutex;
volatile uint32_t *MxCount;
volatile uint32_t *MxMax;
pthread_cond_t *MxCond;
} sync_UnixSemaphoreWrapper;
/* Implements a more efficient (and portable) event object interface than trying to use semaphores. */
typedef struct _sync_UnixEventWrapper {
pthread_mutex_t *MxMutex;
volatile char *MxManual;
volatile char *MxSignaled;
volatile uint32_t *MxWaiting;
pthread_cond_t *MxCond;
} sync_UnixEventWrapper;
#endif
/* Mutex */
typedef struct _sync_Mutex_object {
PHP_SYNC_PHP_5_zend_object_std
#if defined(PHP_WIN32)
CRITICAL_SECTION MxWinCritSection;
HANDLE MxWinMutex;
#else
pthread_mutex_t MxPthreadCritSection;
int MxNamed;
char *MxMem;
sync_UnixSemaphoreWrapper MxPthreadMutex;
#endif
volatile sync_ThreadIDType MxOwnerID;
volatile unsigned int MxCount;
PHP_SYNC_PHP_7_zend_object_std
} sync_Mutex_object;
/* Semaphore */
typedef struct _sync_Semaphore_object {
PHP_SYNC_PHP_5_zend_object_std
#if defined(PHP_WIN32)
HANDLE MxWinSemaphore;
#else
int MxNamed;
char *MxMem;
sync_UnixSemaphoreWrapper MxPthreadSemaphore;
#endif
int MxAutoUnlock;
volatile unsigned int MxCount;
PHP_SYNC_PHP_7_zend_object_std
} sync_Semaphore_object;
/* Event */
typedef struct _sync_Event_object {
PHP_SYNC_PHP_5_zend_object_std
#if defined(PHP_WIN32)
HANDLE MxWinWaitEvent;
#else
int MxNamed;
char *MxMem;
sync_UnixEventWrapper MxPthreadEvent;
#endif
PHP_SYNC_PHP_7_zend_object_std
} sync_Event_object;
/* Reader-Writer */
typedef struct _sync_ReaderWriter_object {
PHP_SYNC_PHP_5_zend_object_std
#if defined(PHP_WIN32)
HANDLE MxWinRSemMutex, MxWinRSemaphore, MxWinRWaitEvent, MxWinWWaitMutex;
#else
int MxNamed;
char *MxMem;
sync_UnixSemaphoreWrapper MxPthreadRCountMutex;
volatile uint32_t *MxRCount;
sync_UnixEventWrapper MxPthreadRWaitEvent;
sync_UnixSemaphoreWrapper MxPthreadWWaitMutex;
#endif
int MxAutoUnlock;
volatile unsigned int MxReadLocks, MxWriteLock;
PHP_SYNC_PHP_7_zend_object_std
} sync_ReaderWriter_object;
/* Named shared memory */
typedef struct _sync_SharedMemory_object {
PHP_SYNC_PHP_5_zend_object_std
int MxFirst;
size_t MxSize;
char *MxMem;
#if defined(PHP_WIN32)
HANDLE MxFile;
#else
char *MxMemInternal;
#endif
PHP_SYNC_PHP_7_zend_object_std
} sync_SharedMemory_object;
#endif /* PHP_SYNC_H */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/