/
threading.c
1986 lines (1759 loc) · 51.4 KB
/
threading.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2007 The PHP Group |
+----------------------------------------------------------------------+
| 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: Moriyoshi Koizumi <moriyoshi@php.net> |
| Author: Alec Gorge <alecgorge@gmail.com> |
+----------------------------------------------------------------------+
*/
/* $Id: header,v 1.16.2.1.2.1 2007/01/01 19:32:09 iliaa Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef PHP_WIN32
#define HAVE_STRUCT_TIMESPEC
#define HAVE_STRTOK_R
#include "pthread.h"
#endif
#include "php.h"
#ifndef ZTS
#error "This extention requires PHP to be built with ZTS support"
#endif
#ifndef PTHREAD_H
#error "This extention requires PHP to be built with pthread-win32 or equivelent"
#endif
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_threading.h"
#include "main/streams/php_stream_plain_wrapper.h"
#include "php_network.h"
#include "php_main.h"
#include "main/php_network.h"
#ifndef PHP_WIN32
#include <signal.h>
#endif
#define bail_if_fail(X) if ((X) != 0) zend_bailout()
#ifdef PHP_THREADING_QUESTIONABLE_VARS
php_stream_ops php_stream_socket_ops;
PHPAPI php_stream_ops php_stream_socket_ops;
//PHPAPI php_stream_ops php_stream_generic_socket_ops;
//php_stream_ops php_stream_generic_socket_ops;
PHPAPI php_stream_ops php_stream_stdio_ops;
PHPAPI php_stream_wrapper php_plain_files_wrapper;
php_stream_ops php_stream_stdio_ops;
php_stream_wrapper php_plain_files_wrapper;
#endif
typedef struct _php_thread_message_t php_thread_message_t;
struct _php_thread_entry_t {
php_thread_entry_t *parent;
pthread_t t;
int refcount;
size_t serial;
size_t alive_subthread_count;
struct {
/* FIXME: this kind of list should be implemented in sparse vector */
size_t n;
size_t cap;
php_thread_entry_t **v;
} subthreads;
void ***tsrm_ls;
void *exit_value;
unsigned finished:1;
unsigned destroyed:1;
pthread_mutex_t gc_mtx;
php_thread_message_t *garbage;
};
typedef struct _php_thread_global_ctx_t {
php_thread_entry_t entry;
pthread_t main_thread;
int le_thread;
int le_mutex;
int le_msg_queue;
int le_msg_slot;
int tsrm_id;
} php_thread_global_ctx_t;
enum php_thread_lock_result {
PHP_THREAD_LOCK_FAILED = -1,
PHP_THREAD_LOCK_ACQUIRED = 0,
PHP_THREAD_LOCK_TIMEOUT = 1
};
typedef struct _php_thread_thread_param_t {
pthread_mutex_t ready_cond_mtx;
pthread_cond_t ready_cond;
php_thread_entry_t *entry;
zend_compiler_globals *compiler_globals;
zend_executor_globals *executor_globals;
zend_fcall_info callable;
int status;
} php_thread_thread_param_t;
typedef struct _php_thread_mutex_t {
php_thread_entry_t *owner;
pthread_mutex_t m;
int refcount;
} php_thread_mutex_t;
struct _php_thread_message_t {
zval *value;
php_thread_entry_t *thd;
php_thread_message_t *next;
php_thread_message_t *prev;
};
typedef struct _php_thread_message_queue_t {
pthread_mutex_t mtx;
pthread_cond_t cond;
php_thread_message_t *first;
php_thread_message_t *last;
int refcount;
} php_thread_message_queue_t;
typedef struct _php_thread_message_sender_t php_thread_message_sender_t;
struct _php_thread_message_sender_t {
pthread_mutex_t mtx;
pthread_cond_t cond;
zval *value;
void ***tsrm_ls;
php_thread_message_sender_t *prev;
php_thread_message_sender_t *next;
};
typedef struct _php_thread_message_slot_t {
pthread_mutex_t mtx;
pthread_cond_t cond;
php_thread_message_sender_t *first;
php_thread_message_sender_t *last;
zval *value;
void ***tsrm_ls;
int nsubs;
int refcount;
} php_thread_message_slot_t;
typedef void *(*php_thread_rsrc_clone_fn_t)(const void *src, int persistent, void ***prev_tsrm_ls TSRMLS_DC);
typedef struct _php_thread_rsrc_desc_t {
int id;
unsigned persistent:1;
size_t size;
php_thread_rsrc_clone_fn_t clone;
} php_thread_rsrc_desc_t;
static php_thread_global_ctx_t global_ctx;
static int php_thread_do_exit = 0;
ZEND_BEGIN_ARG_INFO_EX(arginfo_thread_create, 0, 0, 1)
ZEND_ARG_INFO(0, callable)
ZEND_ARG_INFO(0, ...)
ZEND_END_ARG_INFO()
/* {{{ threading_functions[]
*
* Every user visible function must have an entry in threading_functions[].
*/
zend_function_entry threading_functions[] = {
PHP_FE(thread_create, arginfo_thread_create)
PHP_FE(thread_suspend, NULL)
PHP_FE(thread_resume, NULL)
PHP_FE(thread_join, NULL)
PHP_FE(thread_cleanup, NULL)
PHP_FE(thread_kill, NULL)
PHP_FE(thread_mutex_create, NULL)
PHP_FE(thread_mutex_acquire, NULL)
PHP_FE(thread_mutex_release, NULL)
PHP_FE(thread_message_queue_create, NULL)
PHP_FE(thread_message_queue_post, NULL)
PHP_FE(thread_message_queue_poll, NULL)
PHP_FE(thread_message_queue_stop, NULL)
PHP_FE(thread_message_slot_create, NULL)
PHP_FE(thread_message_slot_post, NULL)
PHP_FE(thread_message_slot_subscribe, NULL)
{NULL, NULL, NULL} /* Must be the last line in threading_functions[] */
};
/* }}} */
/* {{{ threading_module_entry
*/
zend_module_entry threading_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"threading",
threading_functions,
PHP_MINIT(threading),
PHP_MSHUTDOWN(threading),
NULL,
PHP_RSHUTDOWN(threading),
PHP_MINFO(threading),
#if ZEND_MODULE_API_NO >= 20010901
"031", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_THREADING
ZEND_GET_MODULE(threading)
#endif
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("threading.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_threading_globals, threading_globals)
STD_PHP_INI_ENTRY("threading.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_threading_globals, threading_globals)
PHP_INI_END()
*/
/* }}} */
static void php_thread_entry_dispose(php_thread_entry_t **entry TSRMLS_DC);
static int php_thread_convert_object_ref(zval **retval, zval *src,
void ***prev_tsrm_ls TSRMLS_DC);
#ifdef PHP_THREADING_QUESTIONABLE_VARS
void zend_class_add_ref(zend_class_entry **ce)
{
(*ce)->refcount++;
}
#endif
/* {{{ php_thread_entry_addref() */
static void php_thread_entry_addref(php_thread_entry_t *entry)
{
++entry->refcount;
}
/* }}} */
/* {{{ php_thread_entry_join() */
static int php_thread_entry_join(php_thread_entry_t *entry,
zval **retval TSRMLS_DC)
{
if (entry == PHP_THREAD_SELF) {
return FAILURE;
}
THR_DEBUG(("calling php_thread_entry_addref in php_thread_entry_join\n"));
php_thread_entry_addref(entry);
if (!entry->finished) {
zval *tmp;
THR_DEBUG(("calling pthread_join in php_thread_entry_join\n"));
bail_if_fail(pthread_join(entry->t, (void **)&tmp));
THR_DEBUG(("calling assert(entry->finished) in php_thread_entry_join\n"));
assert(entry->finished);
if (retval && tmp != NULL) {
*retval = tmp;
}
} else {
if(retval) {
retval = NULL;
}
}
THR_DEBUG(("calling php_thread_entry_dispose in php_thread_entry_join\n"));
php_thread_entry_dispose(&entry TSRMLS_CC);
THR_DEBUG(("returning in php_thread_entry_join\n"));
return SUCCESS;
}
/* }}} */
/* {{{ php_thread_entry_join() */
static int php_thread_entry_kill(php_thread_entry_t *entry TSRMLS_DC)
{
int result;
if (entry == PHP_THREAD_SELF) {
return FAILURE;
}
THR_DEBUG(("calling php_thread_entry_addref in php_thread_entry_kill\n"));
php_thread_entry_addref(entry);
THR_DEBUG(("calling pthread_cancel in php_thread_entry_kill\n"));
result = pthread_cancel(entry->t);
if(result == SUCCESS) {
THR_DEBUG(("calling php_thread_entry_dispose in php_thread_entry_kill\n"));
php_thread_entry_dispose(&entry TSRMLS_CC);
THR_DEBUG(("returning SUCCESS in php_thread_entry_kill\n"));
return SUCCESS;
}
else {
THR_DEBUG(("returning result in php_thread_entry_kill\n"));
return result;
}
}
/* }}} */
/* {{{ php_thread_entry_wait() */
static void php_thread_entry_wait(php_thread_entry_t *entry TSRMLS_DC)
{
php_thread_entry_t **p, **e = entry->subthreads.v + entry->subthreads.n;
for (p = entry->subthreads.v; p < e; ++p) {
if (!*p) {
continue;
}
php_thread_entry_join(*p, NULL TSRMLS_CC);
}
assert(entry->alive_subthread_count == 0);
}
/* }}} */
/* {{{ php_thread_entry_suspend() */
static int php_thread_entry_suspend(php_thread_entry_t *entry)
{
if (entry->destroyed) {
return FAILURE;
}
return FAILURE;
}
/* }}} */
/* {{{ php_thread_entry_resume() */
static int php_thread_entry_resume(php_thread_entry_t *entry)
{
if (entry->destroyed) {
return FAILURE;
}
return FAILURE;
}
/* }}} */
/* {{{ php_thread_entry_clone() */
static php_thread_entry_t *php_thread_entry_clone(
const php_thread_entry_t *src, int persistent,
void ***prev_tsrm_ls TSRMLS_DC)
{
return (php_thread_entry_t *)src;
}
/* }}} */
/* {{{ php_thread_entry_cancel() */
static void php_thread_entry_cancel(php_thread_entry_t *entry TSRMLS_DC)
{
if (!entry->finished) {
pthread_cancel(entry->t);
entry->finished = 1;
--entry->parent->alive_subthread_count;
php_thread_entry_dispose(&entry TSRMLS_CC);
}
}
/* }}} */
/* {{{ php_thread_entry_dtor() */
static void php_thread_entry_dtor(php_thread_entry_t *entry TSRMLS_DC)
{
if (entry->destroyed) {
return;
}
if (!entry->finished) {
php_thread_entry_cancel(entry TSRMLS_CC);
}
if (entry->subthreads.v) {
php_thread_entry_t **p;
php_thread_entry_t **e = entry->subthreads.v
+ entry->subthreads.n;
for (p = entry->subthreads.v; p < e; ++p) {
php_thread_entry_dispose(p TSRMLS_CC);
}
pefree(entry->subthreads.v, 1);
}
entry->destroyed = 1;
}
/* }}} */
/* {{{ php_thread_entry_dispose() */
static void php_thread_entry_dispose(php_thread_entry_t **entry TSRMLS_DC)
{
if (!*entry) {
return;
}
--(*entry)->refcount;
if ((*entry)->refcount <= 0) {
php_thread_entry_t *parent = (*entry)->parent;
size_t serial = (*entry)->serial;
php_thread_entry_dtor(*entry TSRMLS_CC);
pefree(*entry, 1);
if (parent) {
parent->subthreads.v[serial] = NULL;
}
}
}
/* }}} */
/* {{{ php_thread_entry_ctor() */
static int php_thread_entry_ctor(php_thread_entry_t *entry,
php_thread_entry_t *parent, size_t serial)
{
pthread_t tempThread;
entry->serial = serial;
entry->alive_subthread_count = 0;
entry->subthreads.v = NULL;
entry->subthreads.cap = 0;
entry->subthreads.n = 0;
entry->finished = 0;
entry->destroyed = 0;
entry->refcount = 1;
entry->t = tempThread;
entry->parent = parent;
if (pthread_mutex_init(&entry->gc_mtx, NULL) != 0) {
return FAILURE;
}
entry->garbage = NULL;
return SUCCESS;
}
/* }}} */
/* {{{ php_thread_mutex_acquire() */
static enum php_thread_lock_result
php_thread_mutex_acquire(php_thread_mutex_t *mtx, double timeout TSRMLS_DC)
{
int retval;
struct timespec future;
/* Getting system time in seconds */
struct timeval st;
gettimeofday(&st, NULL);
future.tv_sec = st.tv_sec + (long)floor(timeout/1000.0);
future.tv_nsec = (st.tv_usec * 1000) + (long)(1000000000.0* (timeout/1000.0 - floor(timeout/1000.0))); //(long)(timeout - floor(timeout)));
if (timeout >= 0) {
retval = pthread_mutex_timedlock(&mtx->m, &future);
}
else {
retval = pthread_mutex_lock(&mtx->m);
}
if (!retval) {
mtx->owner = PHP_THREAD_SELF;
return PHP_THREAD_LOCK_ACQUIRED;
}
return retval == ETIMEDOUT ? PHP_THREAD_LOCK_TIMEOUT: PHP_THREAD_LOCK_FAILED;
}
/* }}} */
/* {{{ php_thread_mutex_release() */
static int php_thread_mutex_release(php_thread_mutex_t *mtx TSRMLS_DC)
{
if (PHP_THREAD_SELF != mtx->owner) {
return FAILURE;
}
return pthread_mutex_unlock(&mtx->m) ? SUCCESS: FAILURE;
}
/* }}} */
/* {{{ php_thread_mutex_addref() */
static void php_thread_mutex_addref(php_thread_mutex_t *mtx)
{
++mtx->refcount;
}
/* }}} */
/* {{{ php_thread_mutex_clone() */
static php_thread_mutex_t *php_thread_mutex_clone(
const php_thread_mutex_t *src, int persistent,
void ***prev_tsrm_ls TSRMLS_DC)
{
php_thread_mutex_addref((php_thread_mutex_t *)src);
return (php_thread_mutex_t *)src;
}
/* }}} */
/* {{{ php_thread_mutex_dtor() */
static void php_thread_mutex_dtor(php_thread_mutex_t *mtx TSRMLS_DC)
{
if (PHP_THREAD_SELF == mtx->owner) {
pthread_mutex_unlock(&mtx->m);
mtx->owner = NULL;
}
}
/* }}} */
/* {{{ php_thread_mutex_dispose() */
static void php_thread_mutex_dispose(php_thread_mutex_t **mtx TSRMLS_DC)
{
if (!*mtx) {
return;
}
--(*mtx)->refcount;
if ((*mtx)->refcount <= 0) {
php_thread_mutex_dtor(*mtx TSRMLS_CC);
pefree(*mtx, 1);
}
}
/* }}} */
/* {{{ php_thread_mutex_ctor() */
static int php_thread_mutex_ctor(php_thread_mutex_t *mtx)
{
if (pthread_mutex_init(&mtx->m, NULL) != 0) {
return FAILURE;
}
mtx->refcount = 1;
mtx->owner = NULL;
return SUCCESS;
}
/* }}} */
/* {{{ php_thread_message_queue_post */
static int php_thread_message_queue_post(php_thread_message_queue_t *queue, zval *value TSRMLS_DC)
{
php_thread_message_t *msg = pemalloc(sizeof(php_thread_message_t), 1);
if (!msg) {
return FAILURE;
}
msg->value = value;
msg->next = NULL;
if (pthread_mutex_lock(&queue->mtx) != 0) {
pefree(msg, 1);
return FAILURE;
}
if (!queue->last) {
queue->first = msg;
} else {
queue->last->next = msg;
}
msg->prev = queue->last;
msg->thd = PHP_THREAD_SELF;
queue->last = msg;
Z_ADDREF_P(value);
bail_if_fail(pthread_cond_signal(&queue->cond));
bail_if_fail(pthread_mutex_unlock(&queue->mtx));
return SUCCESS;
}
/* }}} */
/* {{{ php_thread_message_queue_stop */
static int php_thread_message_queue_stop(php_thread_message_queue_t *queue TSRMLS_DC)
{
//php_thread_message_t *msg;
THR_DEBUG(("--attempting to stop signal from php_thread_message_queue_stop\n"));
THR_DEBUG(("--%d\n", pthread_mutex_unlock(&queue->mtx)));
php_thread_do_exit = 1;
return (pthread_cond_broadcast(&queue->cond) == 0 ? SUCCESS : FAILURE);
}
/* }}} */
/* {{{ php_thread_message_queue_poll */
static int php_thread_message_queue_poll(php_thread_message_queue_t *queue, zval **retval, double timeout TSRMLS_DC)
{
php_thread_message_t *msg;
zval *break_return_val;
int result;
char *string_contents = "PHP_THREAD_POLL_STOP";
struct timeval st;
struct timespec future;
if(timeout >= 0) {
/* Getting system time in seconds */
gettimeofday(&st, NULL);
future.tv_sec = st.tv_sec + (long)floor(timeout/1000.0);
future.tv_nsec = (st.tv_usec * 1000) + (long)(1000000000.0* (timeout/1000.0 - floor(timeout/1000.0))); //(long)(timeout - floor(timeout)));
THR_DEBUG(("----%f %f\n", (long)floor(timeout/1000.0), (long)(1000000000.0 * (timeout/1000.0 - floor(timeout/1000.0)))));
/*THR_DEBUG(("php_thread_do_exit: %d\n", php_thread_do_exit));
if(php_thread_do_exit == 1) {
pthread_mutex_unlock(&queue->mtx);
pthread_kill(msg->thd->t, 31);
return SUCCESS;
}*/
THR_DEBUG(("calling pthread_mutex_lock(&queue->mtx) in php_thread_message_queue_poll\n"));
if (pthread_mutex_lock(&queue->mtx) != 0) {
return FAILURE;
}
}
THR_DEBUG(("waiting to be first in the queue\n"));
while (!queue->first) {
//THR_DEBUG(("php_thread_do_exit: %d\n", php_thread_do_exit));
if(php_thread_do_exit == 1) {
pthread_mutex_unlock(&queue->mtx);
MAKE_STD_ZVAL(break_return_val);
ZVAL_STRING(break_return_val, string_contents, 1);
*retval = break_return_val;
return SUCCESS;
}
if (timeout >= 0) {
THR_DEBUG(("pthread_cond_timedwait(&queue->cond, &queue->mtx, &future) in php_thread_message_queue_poll\n"));
result = pthread_cond_timedwait(&queue->cond, &queue->mtx, &future);
THR_DEBUG(("result: %d, %d.%d %d.%d\n", result, future.tv_sec, future.tv_nsec, st.tv_sec, st.tv_usec));
if(result == ETIMEDOUT) {
bail_if_fail(pthread_mutex_unlock(&queue->mtx));
return ETIMEDOUT;
}
//Sleep(timeout);
}
else {
THR_DEBUG(("pthread_cond_wait(&queue->cond, &queue->mtx) in php_thread_message_queue_poll\n"));
result = pthread_cond_wait(&queue->cond, &queue->mtx);
}
if (result != 0 && result != ETIMEDOUT) {
bail_if_fail(pthread_mutex_unlock(&queue->mtx));
return FAILURE;
}
}
THR_DEBUG(("done waiting\n"));
msg = queue->first;
if (msg->prev) {
msg->prev->next = msg->next;
} else {
queue->first = msg->next;
}
if (msg->next) {
msg->next->prev = msg->prev;
} else {
queue->last = msg->prev;
}
if (SUCCESS != php_thread_convert_object_ref(retval, msg->value,
msg->thd->tsrm_ls TSRMLS_CC)) {
ALLOC_INIT_ZVAL(*retval);
}
bail_if_fail(pthread_mutex_lock(&msg->thd->gc_mtx));
msg->next = NULL;
msg->prev = msg->thd->garbage;
msg->thd->garbage = msg;
bail_if_fail(pthread_mutex_unlock(&msg->thd->gc_mtx));
bail_if_fail(pthread_mutex_unlock(&queue->mtx));
pthread_kill(msg->thd->t, 31);
return SUCCESS;
}
/* }}} */
/* {{{ php_thread_message_queue_addref */
static void php_thread_message_queue_addref(php_thread_message_queue_t *queue)
{
++queue->refcount;
}
/* }}} */
/* {{{ php_thread_message_queue_clone() */
static php_thread_message_queue_t *php_thread_message_queue_clone(
const php_thread_message_queue_t *src, int persistent,
void ***prev_tsrm_ls TSRMLS_DC)
{
php_thread_message_queue_addref((php_thread_message_queue_t *)src);
return (php_thread_message_queue_t *)src;
}
/* }}} */
/* {{{ php_thread_message_queue_dtor() */
static void php_thread_message_queue_dtor(php_thread_message_queue_t *queue)
{
}
/* }}} */
/* {{{ php_thread_message_queue_ctor() */
static int php_thread_message_queue_ctor(php_thread_message_queue_t *queue)
{
if (pthread_mutex_init(&queue->mtx, NULL) != 0) {
return FAILURE;
}
if (pthread_cond_init(&queue->cond, NULL) != 0) {
return FAILURE;
}
queue->first = queue->last = NULL;
queue->refcount = 1;
return SUCCESS;
}
/* }}} */
/* {{{ php_thread_message_queue_dispose */
static void php_thread_message_queue_dispose(php_thread_message_queue_t **queue TSRMLS_DC)
{
if (!*queue) {
return;
}
--(*queue)->refcount;
if ((*queue)->refcount <= 0) {
php_thread_message_queue_dtor(*queue);
pefree(*queue, 1);
}
}
/* }}} */
/* {{{ php_thread_message_slot_enqueue_self_and_sleep */
static int php_thread_message_slot_enqueue_self_and_sleep(php_thread_message_slot_t *slot, zval *value TSRMLS_DC)
{
php_thread_message_sender_t *sender = pemalloc(sizeof(php_thread_message_sender_t), 1);
if (!sender) {
return FAILURE;
}
if (pthread_mutex_init(&sender->mtx, NULL) != 0) {
pefree(sender, 1);
return FAILURE;
}
if (pthread_cond_init(&sender->cond, NULL) != 0) {
pefree(sender, 1);
return FAILURE;
}
sender->value = value;
sender->tsrm_ls = tsrm_ls;
if (pthread_mutex_lock(&slot->mtx) != 0) {
pefree(sender, 1);
return FAILURE;
}
if (slot->last) {
slot->last->next = sender;
} else {
slot->first = sender;
}
sender->prev = slot->last;
sender->next = NULL;
slot->last = sender;
bail_if_fail(pthread_mutex_unlock(&slot->mtx));
if (pthread_mutex_lock(&sender->mtx) != 0) {
return FAILURE;
}
if (pthread_cond_wait(&sender->cond, &sender->mtx) != 0) {
bail_if_fail(pthread_mutex_unlock(&sender->mtx));
return FAILURE;
}
bail_if_fail(pthread_mutex_unlock(&sender->mtx));
if (!sender->next && !sender->prev)
pefree(sender, 1);
return SUCCESS;
}
/* }}} */
/* {{{ php_thread_message_slot_post */
static int php_thread_message_slot_post(php_thread_message_slot_t *slot, zval *value TSRMLS_DC)
{
if (slot->nsubs == 0) {
return php_thread_message_slot_enqueue_self_and_sleep(slot, value TSRMLS_CC);
}
if (pthread_mutex_lock(&slot->mtx) != 0) {
zval_ptr_dtor(&value);
return FAILURE;
}
Z_ADDREF_P(value);
slot->value = value;
slot->tsrm_ls = tsrm_ls;
bail_if_fail(pthread_cond_broadcast(&slot->cond));
bail_if_fail(pthread_mutex_unlock(&slot->mtx));
return SUCCESS;
}
/* }}} */
/* {{{ php_thread_message_slot_subscribe */
static int php_thread_message_slot_subscribe(php_thread_message_slot_t *slot, zval **retval TSRMLS_DC)
{
//php_thread_message_t *msg;
if (pthread_mutex_lock(&slot->mtx) != 0) {
return FAILURE;
}
if (slot->nsubs == 0 && slot->first) {
php_thread_message_sender_t *sender = slot->first;
if (SUCCESS != php_thread_convert_object_ref(retval, sender->value,
sender->tsrm_ls TSRMLS_CC)) {
ALLOC_INIT_ZVAL(*retval);
}
if (sender->next) {
sender->next->prev = sender->prev;
} else {
slot->last = sender->prev;
}
if (sender->prev) {
sender->prev->next = sender->next;
} else {
slot->first = sender->next;
}
sender->next = sender->prev = NULL;
bail_if_fail(pthread_mutex_lock(&sender->mtx));
bail_if_fail(pthread_cond_signal(&sender->cond));
bail_if_fail(pthread_mutex_unlock(&sender->mtx));
bail_if_fail(pthread_mutex_unlock(&slot->mtx));
return SUCCESS;
}
slot->nsubs++;
if (pthread_cond_wait(&slot->cond, &slot->mtx) != 0) {
bail_if_fail(pthread_mutex_unlock(&slot->mtx));
return FAILURE;
}
bail_if_fail(pthread_mutex_unlock(&slot->mtx));
if (SUCCESS != php_thread_convert_object_ref(retval, slot->value,
slot->tsrm_ls TSRMLS_CC)) {
ALLOC_INIT_ZVAL(*retval);
}
if (slot->nsubs == 0) {
zval_ptr_dtor(&slot->value);
}
return SUCCESS;
}
/* }}} */
/* {{{ php_thread_message_slot_addref */
static void php_thread_message_slot_addref(php_thread_message_slot_t *slot)
{
++slot->refcount;
}
/* }}} */
/* {{{ php_thread_message_slot_clone() */
static php_thread_message_slot_t *php_thread_message_slot_clone(
const php_thread_message_slot_t *src, int persistent,
void ***prev_tsrm_ls TSRMLS_DC)
{
php_thread_message_slot_addref((php_thread_message_slot_t *)src);
return (php_thread_message_slot_t *)src;
}
/* }}} */
/* {{{ php_thread_message_slot_dtor() */
static void php_thread_message_slot_dtor(php_thread_message_slot_t *slot)
{
}
/* }}} */
/* {{{ php_thread_message_slot_ctor() */
static int php_thread_message_slot_ctor(php_thread_message_slot_t *slot)
{
if (pthread_mutex_init(&slot->mtx, NULL) != 0) {
return FAILURE;
}
if (pthread_cond_init(&slot->cond, NULL) != 0) {
return FAILURE;
}
slot->first = slot->last = NULL;
slot->nsubs = 0;
slot->refcount = 1;
return SUCCESS;
}
/* }}} */
/* {{{ php_thread_message_slot_dispose */
static void php_thread_message_slot_dispose(php_thread_message_slot_t **slot TSRMLS_DC)
{
if (!*slot) {
return;
}
--(*slot)->refcount;
if ((*slot)->refcount <= 0) {
php_thread_message_slot_dtor(*slot);
pefree(*slot, 1);
}
}
/* }}} */
/* {{{ php_thread_executor_globals_reinit() */
static void php_thread_executor_globals_reinit(zend_executor_globals *dest,
zend_executor_globals *src)
{
dest->current_module = src->current_module;
}
/* }}} */
/* {{{ php_thread_compiler_globals_reinit() */
static void php_thread_compiler_globals_reinit(zend_compiler_globals *dest,
zend_compiler_globals *src)
{
zend_hash_clean(dest->function_table);
zend_hash_copy(dest->function_table, src->function_table,
(copy_ctor_func_t)function_add_ref, NULL,
sizeof(zend_function));
zend_hash_clean(dest->class_table);
zend_hash_copy(dest->class_table, src->class_table,
(copy_ctor_func_t)zend_class_add_ref, NULL,
sizeof(zend_class_entry*));
}
/* }}} */
/* {{{ php_thread_get_stream_persistent_id() */
static const char *php_thread_get_stream_persistent_id(const php_stream *strm TSRMLS_DC)
{
HashPosition pos;
HashTable *persistent_list = &EG(persistent_list);
zend_hash_key key;
int key_type;
for (zend_hash_internal_pointer_reset_ex(persistent_list, &pos);
HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_ex(
persistent_list, &key.arKey,
&key.nKeyLength, &key.h, 0, &pos));
zend_hash_move_forward_ex(persistent_list, &pos)) {
zend_rsrc_list_entry *i;
zend_hash_get_current_data_ex(persistent_list, (void**)&i, &pos);
if (i->ptr == strm) {
return key.arKey;
}
}
return NULL;
}
/* }}} */
/* {{{ php_thread_stream_data_copy_ctor() */
static int php_thread_netstream_data_copy_ctor(php_netstream_data_t *self,
const php_netstream_data_t *src, int persistent,
void ***prev_tsrm_ls TSRMLS_DC)
{
int holder;
THR_DEBUG(("*self = *src assignment\n"));
*self = *src;
THR_DEBUG(("dupping self->socket\n"));
holder = dup(self->socket);
THR_DEBUG(("done dupping self->socket: %d\n", holder));
if (holder == 0)
self->socket = holder;
return SUCCESS;
}
/* }}} */
/* {{{ php_thread_stream_basic_clone() */
static php_stream *php_thread_stream_basic_clone(
const php_stream *src, int persistent, void ***prev_tsrm_ls TSRMLS_DC)
{
php_stream *retval;
retval = pemalloc(sizeof(*retval), persistent);
if (!retval) {
return NULL;
}
if (src->readbuf) {
retval->readbuf = pemalloc(src->readbuflen, persistent);
if (!retval->readbuf) {
pefree(retval, persistent);
return NULL;
}
memcpy(retval->readbuf, src->readbuf, src->readbuflen);
} else {
retval->readbuf = NULL;
}
if (src->orig_path) {
retval->orig_path = pestrdup(src->orig_path, persistent);
if (!retval->orig_path) {
pefree(retval->readbuf, persistent);
pefree(retval, persistent);
return NULL;
}
} else {
retval->orig_path = NULL;
}
retval->readbuflen = src->readbuflen;
retval->ops = src->ops;
retval->flags = src->flags;
retval->chunk_size = src->chunk_size;
retval->is_persistent = persistent;
retval->abstract = NULL;
retval->wrapperthis = NULL;
retval->wrapperdata = NULL;
retval->context = NULL;
retval->rsrc_id = 0;
retval->in_free = 0;
retval->stdiocast = NULL;
retval->readfilters.stream = retval;
retval->writefilters.stream = retval;
memcpy(retval->mode, src->mode, sizeof(retval->mode));