forked from krakjoe/pthreads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_pthreads.c
348 lines (274 loc) · 11 KB
/
php_pthreads.c
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
+----------------------------------------------------------------------+
| pthreads |
+----------------------------------------------------------------------+
| Copyright (c) Joe Watkins 2012 - 2014 |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Joe Watkins <joe.watkins@live.co.uk> |
+----------------------------------------------------------------------+
*/
#ifndef HAVE_PHP_PTHREADS
#define HAVE_PHP_PTHREADS
#ifndef HAVE_PTHREADS_H
# include <src/pthreads.h>
#endif
#ifndef HAVE_PHP_PTHREADS_H
# include <php_pthreads.h>
#endif
#ifndef HAVE_PTHREADS_OBJECT_H
# include <src/object.h>
#endif
#ifndef ZTS
# error "pthreads requires that Thread Safety is enabled, add --enable-maintainer-zts to your PHP build configuration"
#endif
#if COMPILE_DL_PTHREADS
ZEND_TSRMLS_CACHE_DEFINE();
ZEND_GET_MODULE(pthreads)
#endif
#ifndef HAVE_PTHREADS_GLOBALS_H
# include <src/globals.h>
#endif
#ifndef HAVE_PTHREADS_COPY_H
# include <src/copy.h>
#endif
typedef void (*zend_throw_exception_hook_func)(zval * TSRMLS_DC);
zend_throw_exception_hook_func zend_throw_exception_hook_function = NULL;
ZEND_BEGIN_ARG_INFO_EX(pthreads_no_sleeping_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, timeout)
ZEND_END_ARG_INFO()
PHP_FUNCTION(pthreads_no_sleeping) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0,
"%s is not suitable for use in multi threaded applications",
ZSTR_VAL(EX(func)->common.function_name));
}
static zend_function_entry pthreads_functions[] = {
PHP_FE(pthreads_no_sleeping, pthreads_no_sleeping_arginfo)
PHP_FE_END
};
zend_module_entry pthreads_module_entry = {
STANDARD_MODULE_HEADER,
PHP_PTHREADS_EXTNAME,
pthreads_functions,
PHP_MINIT(pthreads),
PHP_MSHUTDOWN(pthreads),
PHP_RINIT(pthreads),
PHP_RSHUTDOWN(pthreads),
PHP_MINFO(pthreads),
PHP_PTHREADS_VERSION,
NO_MODULE_GLOBALS,
ZEND_MODULE_POST_ZEND_DEACTIVATE_N(pthreads),
STANDARD_MODULE_PROPERTIES_EX
};
zend_class_entry *pthreads_threaded_entry;
zend_class_entry *pthreads_thread_entry;
zend_class_entry *pthreads_worker_entry;
zend_class_entry *pthreads_collectable_entry;
zend_class_entry *pthreads_pool_entry;
zend_object_handlers pthreads_handlers;
zend_object_handlers *zend_handlers;
void ***pthreads_instance = NULL;
#ifndef HAVE_SPL
zend_class_entry *spl_ce_InvalidArgumentException;
zend_class_entry *spl_ce_Countable;
zend_class_entry *spl_ce_RuntimeException;
#endif
#ifndef HAVE_PTHREADS_OBJECT_H
# include <src/object.h>
#endif
ZEND_DECLARE_MODULE_GLOBALS(pthreads)
static inline void pthreads_globals_ctor(zend_pthreads_globals *pg TSRMLS_DC) {
ZVAL_UNDEF(&pg->this);
pg->pid = 0L;
pg->signal = 0;
pg->resources = NULL;
}
void pthreads_throw_exception_hook(zval *ex TSRMLS_DC) {
if (!ex)
return;
if (Z_TYPE(PTHREADS_ZG(this)) != IS_UNDEF) {
if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
zval retval;
zend_string *cname;
ZVAL_UNDEF(&retval);
if (zend_fcall_info_init(&EG(user_exception_handler), IS_CALLABLE_CHECK_SILENT, &fci, &fcc, &cname, NULL) == SUCCESS) {
fci.retval = &retval;
EG(exception) = NULL;
zend_fcall_info_argn(&fci, 1, ex);
zend_call_function(&fci, &fcc);
zend_fcall_info_args_clear(&fci, 1);
}
if (Z_TYPE(retval) != IS_UNDEF)
zval_dtor(&retval);
if (cname)
zend_string_release(cname);
}
}
if (zend_throw_exception_hook_function) {
zend_throw_exception_hook_function(ex);
}
}
static inline void pthreads_replace_internal_function(char *oname, size_t olen, char *rname, size_t rlen) {
zend_function *functions[2];
functions[0] = zend_hash_str_find_ptr(CG(function_table), oname, olen);
functions[1] = zend_hash_str_find_ptr(CG(function_table), rname, rlen);
if (functions[0] && functions[1]) {
zend_string *rename = zend_string_copy(functions[0]->common.function_name);
if ((functions[1] = zend_hash_str_update_mem(
CG(function_table), oname, olen, functions[1], sizeof(zend_function)))) {
functions[1]->common.function_name = rename;
} else zend_string_release(rename);
}
}
PHP_MINIT_FUNCTION(pthreads)
{
zend_class_entry ce;
REGISTER_LONG_CONSTANT("PTHREADS_INHERIT_ALL", PTHREADS_INHERIT_ALL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PTHREADS_INHERIT_NONE", PTHREADS_INHERIT_NONE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PTHREADS_INHERIT_INI", PTHREADS_INHERIT_INI, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PTHREADS_INHERIT_CONSTANTS", PTHREADS_INHERIT_CONSTANTS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PTHREADS_INHERIT_CLASSES", PTHREADS_INHERIT_CLASSES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PTHREADS_INHERIT_FUNCTIONS", PTHREADS_INHERIT_FUNCTIONS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PTHREADS_INHERIT_INCLUDES", PTHREADS_INHERIT_INCLUDES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PTHREADS_INHERIT_COMMENTS", PTHREADS_INHERIT_COMMENTS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PTHREADS_ALLOW_HEADERS", PTHREADS_ALLOW_HEADERS, CONST_CS | CONST_PERSISTENT);
INIT_CLASS_ENTRY(ce, "Threaded", pthreads_threaded_methods);
pthreads_threaded_entry=zend_register_internal_class(&ce);
pthreads_threaded_entry->get_iterator = pthreads_object_iterator_create;
pthreads_threaded_entry->create_object = pthreads_threaded_ctor;
pthreads_threaded_entry->serialize = pthreads_threaded_serialize;
pthreads_threaded_entry->unserialize = pthreads_threaded_unserialize;
zend_class_implements(pthreads_threaded_entry, 1, zend_ce_traversable);
INIT_CLASS_ENTRY(ce, "Thread", pthreads_thread_methods);
pthreads_thread_entry=zend_register_internal_class_ex(&ce, pthreads_threaded_entry);
pthreads_thread_entry->create_object = pthreads_thread_ctor;
INIT_CLASS_ENTRY(ce, "Collectable", pthreads_collectable_methods);
pthreads_collectable_entry = zend_register_internal_class_ex(&ce, pthreads_threaded_entry);
zend_declare_property_bool(pthreads_collectable_entry, ZEND_STRL("garbage"), 0, ZEND_ACC_PROTECTED);
INIT_CLASS_ENTRY(ce, "Worker", pthreads_worker_methods);
pthreads_worker_entry=zend_register_internal_class_ex(&ce, pthreads_thread_entry);
pthreads_worker_entry->create_object = pthreads_worker_ctor;
INIT_CLASS_ENTRY(ce, "Pool", pthreads_pool_methods);
pthreads_pool_entry=zend_register_internal_class(&ce);
zend_declare_property_long(pthreads_pool_entry, ZEND_STRL("size"), 1, ZEND_ACC_PROTECTED);
zend_declare_property_null(pthreads_pool_entry, ZEND_STRL("class"), ZEND_ACC_PROTECTED);
zend_declare_property_null(pthreads_pool_entry, ZEND_STRL("workers"), ZEND_ACC_PROTECTED);
zend_declare_property_null(pthreads_pool_entry, ZEND_STRL("ctor"), ZEND_ACC_PROTECTED);
zend_declare_property_long(pthreads_pool_entry, ZEND_STRL("last"), 0, ZEND_ACC_PROTECTED);
/*
* Setup standard and threaded object handlers
*/
zend_handlers = zend_get_std_object_handlers();
memcpy(&pthreads_handlers, zend_handlers, sizeof(zend_object_handlers));
pthreads_handlers.offset = XtOffsetOf(pthreads_object_t, std);
pthreads_handlers.free_obj = pthreads_base_free;
pthreads_handlers.cast_object = pthreads_cast_object;
pthreads_handlers.count_elements = pthreads_count_properties;
pthreads_handlers.get_debug_info = pthreads_read_debug;
pthreads_handlers.get_properties = pthreads_read_properties;
pthreads_handlers.read_property = pthreads_read_property;
pthreads_handlers.write_property = pthreads_write_property;
pthreads_handlers.has_property = pthreads_has_property;
pthreads_handlers.unset_property = pthreads_unset_property;
pthreads_handlers.read_dimension = pthreads_read_dimension;
pthreads_handlers.write_dimension = pthreads_write_dimension;
pthreads_handlers.has_dimension = pthreads_has_dimension;
pthreads_handlers.unset_dimension = pthreads_unset_dimension;
pthreads_handlers.get_property_ptr_ptr = NULL;
pthreads_handlers.get = NULL;
pthreads_handlers.set = NULL;
pthreads_handlers.get_gc = NULL;
pthreads_handlers.clone_obj = pthreads_clone_object;
ZEND_INIT_MODULE_GLOBALS(pthreads, pthreads_globals_ctor, NULL);
if (pthreads_globals_init()) {
TSRMLS_CACHE_UPDATE();
/*
* Global Init
*/
pthreads_instance = TSRMLS_CACHE;
}
#ifndef HAVE_SPL
spl_ce_InvalidArgumentException = zend_exception_get_default();
spl_ce_Countable = zend_exception_get_default();
spl_ce_RuntimeException = zend_exception_get_default();
#endif
zend_throw_exception_hook_function = zend_throw_exception_hook;
zend_throw_exception_hook = pthreads_throw_exception_hook;
pthreads_replace_internal_function(ZEND_STRL("usleep"), ZEND_STRL("pthreads_no_sleeping"));
pthreads_replace_internal_function(ZEND_STRL("sleep"), ZEND_STRL("pthreads_no_sleeping"));
return SUCCESS;
}
static inline int sapi_cli_deactivate(void)
{
fflush(stdout);
if (SG(request_info).argv0) {
free(SG(request_info).argv0);
SG(request_info).argv0 = NULL;
}
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(pthreads)
{
if (pthreads_instance == TSRMLS_CACHE) {
pthreads_globals_shutdown();
if (memcmp(sapi_module.name, ZEND_STRL("cli")) == SUCCESS) {
sapi_module.deactivate = sapi_cli_deactivate;
}
}
zend_throw_exception_hook = zend_throw_exception_hook_function;
return SUCCESS;
}
ZEND_MODULE_POST_ZEND_DEACTIVATE_D(pthreads)
{
if (PTHREADS_ZG(resources)) {
zend_hash_destroy(PTHREADS_ZG(resources));
FREE_HASHTABLE(PTHREADS_ZG(resources));
PTHREADS_ZG(resources) = NULL;
}
return SUCCESS;
}
PHP_RINIT_FUNCTION(pthreads) {
ZEND_TSRMLS_CACHE_UPDATE();
zend_hash_init(&PTHREADS_ZG(resolve), 15, NULL, NULL, 0);
if (pthreads_instance != TSRMLS_CACHE) {
if (memcmp(sapi_module.name, ZEND_STRL("cli")) == SUCCESS) {
sapi_module.deactivate = NULL;
}
}
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(pthreads) {
zend_hash_destroy(&PTHREADS_ZG(resolve));
return SUCCESS;
}
PHP_MINFO_FUNCTION(pthreads)
{
php_info_print_table_start();
php_info_print_table_row(2, "Version", PHP_PTHREADS_VERSION);
php_info_print_table_end();
}
#ifndef HAVE_PTHREADS_CLASS_THREADED
# include <classes/threaded.h>
#endif
#ifndef HAVE_PTHREADS_CLASS_THREAD
# include <classes/thread.h>
#endif
#ifndef HAVE_PTHREADS_CLASS_COLLECTABLE
# include <classes/collectable.h>
#endif
#ifndef HAVE_PTHREADS_CLASS_WORKER
# include <classes/worker.h>
#endif
#ifndef HAVE_PTHREADS_CLASS_POOL
# include <classes/pool.h>
#endif
#endif