forked from krakjoe/pthreads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_pthreads.c
1111 lines (935 loc) · 35.3 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
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
/*
+----------------------------------------------------------------------+
| pthreads |
+----------------------------------------------------------------------+
| Copyright (c) Joe Watkins 2012 |
+----------------------------------------------------------------------+
| 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_STATE_H
# include <src/state.h>
#endif
#ifndef HAVE_PTHREADS_THREAD_H
# include <src/thread.h>
#endif
#ifndef HAVE_PTHREADS_SERIAL_H
# include <src/serial.h>
#endif
#ifndef HAVE_PTHREADS_GLOBALS_H
# include <src/globals.h>
#endif
#ifndef HAVE_PTHREADS_OBJECT_H
# include <src/object.h>
#endif
#ifndef HAVE_PTHREADS_COMPAT_H
# include <src/compat.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_GET_MODULE(pthreads)
#endif
zend_class_entry *pthreads_class_entry = NULL;
zend_class_entry *pthreads_mutex_class_entry = NULL;
zend_class_entry *pthreads_condition_class_entry = NULL;
zend_object_handlers *zsh = NULL;
zend_object_handlers poh;
/* {{{ defmutex setup
Choose the NP type if it exists as it targets the current system */
#ifndef _WIN32
# ifdef PTHREAD_MUTEX_ERRORCHECK_NP
# define DEFAULT_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK_NP
# elifdef PTHREAD_MUTEX_ERRORCHECK
# define DEFAULT_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK
# endif
#endif
/* }}} */
#ifndef HAVE_PTHREADS_SERIAL_H
#include <src/serial.h>
#endif
#ifndef HAVE_PTHREADS_GLOBALS_H
#include <src/globals.h>
#endif
/* {{{ basic */
ZEND_BEGIN_ARG_INFO_EX(Thread_start, 0, 0, 0)
ZEND_ARG_INFO(0, synchronized)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Thread_run, 0, 0, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ advanced */
ZEND_BEGIN_ARG_INFO_EX(Thread_yield, 0, 0, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ synchronization */
ZEND_BEGIN_ARG_INFO_EX(Thread_wait, 0, 0, 0)
ZEND_ARG_INFO(0, timeout)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Thread_notify, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Thread_join, 0, 0, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ importing */
ZEND_BEGIN_ARG_INFO_EX(Thread_getThread, 0, 0, 1)
ZEND_ARG_INFO(0, tid)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ identification */
ZEND_BEGIN_ARG_INFO_EX(Thread_getThreadId, 0, 0, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ state detection */
ZEND_BEGIN_ARG_INFO_EX(Thread_isStarted, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Thread_isRunning, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Thread_isJoined, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Thread_isWaiting, 0, 0, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ stacking */
ZEND_BEGIN_ARG_INFO_EX(Thread_stack, 0, 0, 1)
ZEND_ARG_INFO(0, work)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Thread_unstack, 0, 0, 0)
ZEND_ARG_INFO(0, work)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Thread_getStacked, 0, 0, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ statics/globals */
ZEND_BEGIN_ARG_INFO_EX(Thread_getCount, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Thread_getMax, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Thread_getPeak, 0, 0, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ mutex */
ZEND_BEGIN_ARG_INFO_EX(Mutex_create, 0, 0, 0)
ZEND_ARG_INFO(0, lock)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Mutex_lock, 0, 0, 1)
ZEND_ARG_INFO(0, mutex)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Mutex_trylock, 0, 0, 1)
ZEND_ARG_INFO(0, mutex)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Mutex_unlock, 0, 0, 1)
ZEND_ARG_INFO(0, mutex)
ZEND_ARG_INFO(0, destroy)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Mutex_destroy, 0, 0, 1)
ZEND_ARG_INFO(0, mutex)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ conditions */
ZEND_BEGIN_ARG_INFO_EX(Cond_create, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Cond_signal, 0, 0, 1)
ZEND_ARG_INFO(0, condition)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Cond_wait, 0, 0, 2)
ZEND_ARG_INFO(0, condition)
ZEND_ARG_INFO(0, mutex)
ZEND_ARG_INFO(0, timeout)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Cond_broadcast, 0, 0, 1)
ZEND_ARG_INFO(0, condition)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(Cond_destroy, 0, 0, 1)
ZEND_ARG_INFO(0, condition)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ Thread method entries */
zend_function_entry pthreads_methods[] = {
PHP_ME(Thread, start, Thread_start, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ABSTRACT_ME(Thread, run, Thread_run)
PHP_ME(Thread, wait, Thread_wait, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Thread, notify, Thread_notify, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Thread, join, Thread_join, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Thread, isStarted, Thread_isStarted, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Thread, isRunning, Thread_isRunning, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Thread, isJoined, Thread_isJoined, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Thread, isWaiting, Thread_isWaiting, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Thread, stack, Thread_stack, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Thread, unstack, Thread_unstack, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Thread, getStacked, Thread_getStacked, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Thread, getThread, Thread_getThread, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL|ZEND_ACC_STATIC)
PHP_ME(Thread, getThreadId, Thread_getThreadId, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Thread, getCount, Thread_getCount, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
PHP_ME(Thread, getMax, Thread_getMax, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
PHP_ME(Thread, getPeak, Thread_getPeak, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
{NULL, NULL, NULL}
}; /* }}} */
/* {{{ Mutex method entries */
zend_function_entry pthreads_mutex_methods[] = {
PHP_ME(Mutex, create, Mutex_create, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
PHP_ME(Mutex, lock, Mutex_lock, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
PHP_ME(Mutex, trylock, Mutex_trylock, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
PHP_ME(Mutex, unlock, Mutex_unlock, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
PHP_ME(Mutex, destroy, Mutex_destroy, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
{NULL, NULL, NULL}
}; /* }}} */
/* {{{ Cond method entries */
zend_function_entry pthreads_condition_methods[] = {
PHP_ME(Cond, create, Cond_create, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
PHP_ME(Cond, signal, Cond_signal, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
PHP_ME(Cond, wait, Cond_wait, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
PHP_ME(Cond, broadcast, Cond_broadcast, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
PHP_ME(Cond, destroy, Cond_destroy, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
{NULL, NULL, NULL}
}; /* }}} */
/* {{{ pthreads module entry */
zend_module_entry pthreads_module_entry = {
STANDARD_MODULE_HEADER,
PHP_PTHREADS_EXTNAME,
NULL,
PHP_MINIT(pthreads),
PHP_MSHUTDOWN(pthreads),
NULL,
NULL,
PHP_MINFO(pthreads),
PHP_PTHREADS_VERSION,
STANDARD_MODULE_PROPERTIES
}; /* }}} */
/* {{{ INI Entries */
PHP_INI_BEGIN()
/* {{{ pthreads.max allows admins some control over how many threads a user can create */
PHP_INI_ENTRY("pthreads.max", "0", PHP_INI_SYSTEM, NULL) /* }}} */
/* {{{ pthreads.importing allows importing threads to be disabled where it poses a security risk */
PHP_INI_ENTRY("pthreads.importing", "1", PHP_INI_SYSTEM, NULL) /* }}} */
PHP_INI_END()
/* }}} */
/* {{{ Brains of the operation */
#ifndef HAVE_PTHREADS_OBJECT_H
# include <src/object.h>
#endif /* }}} */
/* {{{ Access modification */
#ifndef HAVE_PTHREADS_MODIFIERS_H
# include <src/modifiers.h>
#endif /* }}} */
/* {{{ module initialization */
PHP_MINIT_FUNCTION(pthreads)
{
zend_class_entry te;
zend_class_entry me;
zend_class_entry ce;
REGISTER_INI_ENTRIES();
/*
* Global Init
*/
if (!PTHREADS_G(init))
pthreads_globals_init();
INIT_CLASS_ENTRY(te, "Thread", pthreads_methods);
te.create_object = pthreads_attach_to_context;
te.serialize = zend_class_serialize_deny;
te.unserialize = zend_class_unserialize_deny;
pthreads_class_entry=zend_register_internal_class(&te TSRMLS_CC);
INIT_CLASS_ENTRY(me, "Mutex", pthreads_mutex_methods);
me.serialize = zend_class_serialize_deny;
me.unserialize = zend_class_unserialize_deny;
pthreads_mutex_class_entry=zend_register_internal_class(&me TSRMLS_CC);
INIT_CLASS_ENTRY(ce, "Cond", pthreads_condition_methods);
ce.serialize = zend_class_serialize_deny;
ce.unserialize = zend_class_unserialize_deny;
pthreads_condition_class_entry=zend_register_internal_class(&ce TSRMLS_CC);
if ( pthread_mutexattr_init(&defmutex)==SUCCESS ) {
#ifdef DEFAULT_MUTEX_TYPE
pthread_mutexattr_settype(
&defmutex,
DEFAULT_MUTEX_TYPE
);
#endif
}
/*
* Setup standard and pthreads object handlers
*/
zsh = zend_get_std_object_handlers();
memcpy(&poh, zsh, sizeof(zend_object_handlers));
poh.get_method = pthreads_get_method;
poh.call_method = pthreads_call_method;
poh.read_property = pthreads_read_property;
poh.write_property = pthreads_write_property;
poh.has_property = pthreads_has_property;
poh.unset_property = pthreads_unset_property;
poh.get_property_ptr_ptr = NULL;
return SUCCESS;
} /* }}} */
/* {{{ module shutdown */
PHP_MSHUTDOWN_FUNCTION(pthreads)
{
UNREGISTER_INI_ENTRIES();
pthread_mutexattr_destroy(
&defmutex
);
return SUCCESS;
} /* }}} */
/* {{{ minfo */
PHP_MINFO_FUNCTION(pthreads)
{
char numbers[10];
php_info_print_table_start();
php_info_print_table_row(2, "Version", PHP_PTHREADS_VERSION);
if (PTHREADS_G(max)) {
php_info_print_table_row(2, "Maximum Threads", INI_STR("pthreads.max"));
} else php_info_print_table_row(2, "Maximum Threads", "No Limits");
snprintf(numbers, sizeof(numbers), "%d", pthreads_globals_count());
php_info_print_table_row(2, "Current Threads", numbers);
snprintf(numbers, sizeof(numbers), "%d", pthreads_globals_peak());
php_info_print_table_row(2, "Peak Threads", numbers);
php_info_print_table_end();
} /* }}} */
/* {{{ proto boolean Thread::start([boolean sync])
Starts executing your implemented run method in a thread, will return a boolean indication of success
If sync is true, your new thread runs synchronized with the current thread ( causing the current thread to block ) until you call notify from within the new thread or the thread ends
If sync is false or void, the new thread will run asynchronously to the current thread and this call will return as quickly as possible */
PHP_METHOD(Thread, start)
{
PTHREAD thread = PTHREADS_FETCH;
int result = FAILURE;
int acquire = 0;
int valid = 1;
zval *props;
/*
* See if there are any limits in this environment
*/
if (PTHREADS_G(max) && !(pthreads_globals_count()<PTHREADS_G(max))) {
zend_error(E_WARNING, "pthreads has reached the maximum numbers of threads allowed (%lu) by your server administrator", PTHREADS_G(max));
RETURN_FALSE;
}
/*
* Find out if the calling thread will wait for notification before continuing
*/
if (ZEND_NUM_ARGS()){
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &thread->synchronized);
} else thread->synchronized = 0;
acquire = PTHREADS_LOCK(thread);
if (acquire == SUCCESS || acquire == EDEADLK) {
if (!pthreads_state_isset(thread->state, PTHREADS_ST_STARTED)) {
/*
* Serializing here keeps the heap happy, so here is where we'll do it ...
*/
{
ALLOC_INIT_ZVAL(props);
Z_TYPE_P(props)=IS_ARRAY;
Z_ARRVAL_P(props)=thread->std.properties;
thread->serial = pthreads_serialize(props TSRMLS_CC);
FREE_ZVAL(props);
/* if the serial does not end in } then serialization has failed on account of private/protected members */
if (thread->serial[strlen(thread->serial)-1] != '}') {
free(thread->serial);
thread->serial = NULL;
if (acquire != EDEADLK)
PTHREADS_UNLOCK(thread);
valid = 0;
zend_error_noreturn(E_WARNING, "pthreads detected failure while preparing members for threading context, only public members can be used");
}
}
/*
* Check validity before continuing
*/
if (valid) {
/*
* Don't allow restarting this object
*/
pthreads_set_state(thread, PTHREADS_ST_STARTED);
/*
* Set running flag and store thread in globals
*/
pthreads_set_state(thread, PTHREADS_ST_RUNNING);
/*
* Attempt to start the thread
*/
if ((result = pthread_create(&thread->thread, NULL, PHP_PTHREAD_ROUTINE, (void*)thread)) == SUCCESS) {
/*
* Wait for notification to continue
*/
PTHREADS_WAIT(thread);
} else {
/*
* Do not attempt to join failed threads at dtor time
*/
pthreads_unset_state(thread, PTHREADS_ST_RUNNING);
/*
* Report the error, there's no chance of recovery ...
*/
switch(result) {
case EAGAIN:
zend_error_noreturn(E_WARNING, "pthreads has detected that the thread could not be started, the system lacks the necessary resources or the system-imposed limit would be exceeded");
break;
default: zend_error_noreturn(E_WARNING, "pthreads has detected that the thread could not be started because of an unspecified system error");
}
}
} else { result = FAILURE; }
} else zend_error_noreturn(E_WARNING, "pthreads has detected an attempt to start a thread that has already been started");
} else zend_error_noreturn(E_ERROR, "pthreads has experienced an internal error and cannot continue");
if (acquire != EDEADLK)
PTHREADS_UNLOCK(thread);
if (result==SUCCESS) {
RETURN_TRUE;
}
RETURN_FALSE;
} /* }}} */
/* {{{ proto int Thread::stack(Thread $work)
Pushes an item onto the Thread Stack, returns the size of stack */
PHP_METHOD(Thread, stack)
{
PTHREAD thread = PTHREADS_FETCH;
zval *work;
int size;
if (thread) {
if (!thread->copy) {
if (!PTHREADS_IS_JOINED(thread)) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &work, thread->std.ce)==SUCCESS) {
size = pthreads_stack_push(thread, PTHREADS_FETCH_FROM(work));
if (size) {
RETURN_LONG(size);
} else zend_error(E_WARNING, "pthreads has experienced your mum");
}
} else zend_error(E_ERROR, "pthreads has detected an attempt to stack onto a joined Thread (%lu)", thread->tid);
} else zend_error(E_WARNING, "pthreads has detected an attempt to call and external method on %lu, you cannot stack in this context", thread->tid);
} else zend_error(E_ERROR, "pthreads has experienced an internal error and cannot continue");
RETURN_FALSE;
} /* }}} */
/* {{{ proto int Thread::unstack([Thread $work])
Removes an item from the Thread stack, if no item is specified the stack is cleared, returns the size of stack */
PHP_METHOD(Thread, unstack)
{
PTHREAD thread = PTHREADS_FETCH;
zval * work;
if (thread) {
if (!thread->copy) {
if (ZEND_NUM_ARGS() > 0) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &work, thread->std.ce)==SUCCESS) {
RETURN_LONG(pthreads_stack_pop_ex(thread, PTHREADS_FETCH_FROM(work) TSRMLS_CC));
}
} else RETURN_LONG(pthreads_stack_pop_ex(thread, NULL TSRMLS_CC));
} else zend_error(E_WARNING, "pthreads has detected an attempt to call an external method, you cannot unstack in this context");
} else zend_error(E_ERROR, "pthreads has experienced an internal error and cannot continue");
RETURN_FALSE;
}
/* {{{ proto int Thread::getStacked()
Returns the current size of the stack */
PHP_METHOD(Thread, getStacked)
{
PTHREAD thread = PTHREADS_FETCH;
if (thread) {
RETURN_LONG(pthreads_stack_length(thread));
} else zend_error(E_ERROR, "pthreads has experienced an internal error and cannot continue");
RETURN_FALSE;
}
/* {{{ proto Thread::isStarted()
Will return true if a Thread has been started */
PHP_METHOD(Thread, isStarted)
{
PTHREAD thread = PTHREADS_FETCH;
if (thread) {
RETURN_BOOL(pthreads_state_isset(thread->state, PTHREADS_ST_STARTED));
} else zend_error(E_ERROR, "pthreads has experienced an internal error and cannot continue");
} /* }}} */
/* {{{ proto Thread::isRunning()
Will return true while a thread is executing */
PHP_METHOD(Thread, isRunning)
{
PTHREAD thread = PTHREADS_FETCH;
if (thread) {
RETURN_BOOL(pthreads_state_isset(thread->state, PTHREADS_ST_RUNNING));
} else zend_error(E_ERROR, "pthreads has experienced an internal error and cannot continue");
} /* }}} */
/* {{{ proto Thread::isJoined()
Will return true if a Thread has been joined already */
PHP_METHOD(Thread, isJoined)
{
PTHREAD thread = PTHREADS_FETCH;
if (thread) {
RETURN_BOOL(pthreads_state_isset(thread->state, PTHREADS_ST_JOINED));
} else zend_error(E_ERROR, "pthreads has experienced an internal error and cannot continue");
} /* }}} */
/* {{{ proto boolean Thread::isWaiting()
Will return true if the referenced thread is waiting for notification */
PHP_METHOD(Thread, isWaiting)
{
PTHREAD thread = PTHREADS_FETCH;
if (thread) {
RETURN_BOOL(pthreads_state_isset(thread->state, PTHREADS_ST_WAITING));
} else zend_error(E_ERROR, "pthreads has expereinced an internal error and cannot continue");
}
/* {{{ proto boolean Thread::wait([long timeout])
Will cause the calling process or thread to wait for the referenced thread to notify
When a timeout is used an reached boolean false will return
When no timeout is used a boolean indication of success will return */
PHP_METHOD(Thread, wait)
{
PTHREAD thread = PTHREADS_FETCH;
long timeout = 0L;
if(ZEND_NUM_ARGS()){
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &timeout)!=SUCCESS)
RETURN_FALSE;
}
if (thread) {
if (pthreads_state_isset(thread->state, PTHREADS_ST_RUNNING)) {
ZVAL_BOOL(return_value, PTHREADS_WAIT_EX(thread, timeout)>0);
} else RETURN_FALSE;
} else zend_error(E_ERROR, "pthreads has experienced an internal error and cannot continue");
} /* }}} */
/* {{{ proto boolean Thread::notify()
Notify anyone waiting that they can continue
A call to Thread::notify when no one is waiting will cause the thread or process that called it to block until someone waits
Will return a boolean indication of success
@TODO make this handle imported threads */
PHP_METHOD(Thread, notify)
{
PTHREAD thread = PTHREADS_FETCH;
if (thread) {
if (pthreads_state_isset(thread->state, PTHREADS_ST_WAITING)) {
RETURN_BOOL(PTHREADS_NOTIFY(thread)>0);
} else RETURN_TRUE;
} else zend_error(E_ERROR, "pthreads has experienced an internal error and cannot continue");
RETURN_FALSE;
} /* }}} */
/* {{{ proto mixed Thread::join([boolean discard])
Will cause the calling thread to block and wait for the output of the referenced thread
If discard is void or false, properties set in the threading context during runtime will replace the properties of the referenced thread in the current context */
PHP_METHOD(Thread, join)
{
PTHREAD thread = PTHREADS_FETCH;
zval *symbols = NULL;
char *result = NULL;
zend_bool discard = 0;
/*
* Check for discard argument
*/
if (ZEND_NUM_ARGS())
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &discard);
if (thread) {
if (!PTHREADS_IN_THREAD(thread)) {
/*
* Ensure this thread was started
*/
if (PTHREADS_IS_STARTED(thread)) {
/*
* Ensure this thread wasn't already joined
*/
if (!PTHREADS_IS_JOINED(thread)) {
/*
* Ensure nothing attempts to join this thread again
*/
pthreads_set_state(thread, PTHREADS_ST_JOINED);
/*
* We must force threads to workers to close at this point
*/
{
if (pthreads_is_worker(thread)) {
if (pthreads_state_isset(thread->state, PTHREADS_ST_WAITING)) {
do {
PTHREADS_NOTIFY(thread);
} while(pthreads_state_isset(thread->state, PTHREADS_ST_WAITING));
}
}
}
/*
* Carry out joining and deserialize result
*/
if (pthread_join(thread->thread, (void**)&result)==SUCCESS) {
if (result) {
pthreads_unserialize_into(result, return_value TSRMLS_CC);
free(result);
}
/*
* Replace symbol tables
*/
if (thread->serial) {
if (!discard) {
if ((symbols=pthreads_unserialize(thread->serial TSRMLS_CC))!=NULL){
if (thread->std.properties) {
zend_hash_destroy(thread->std.properties);
FREE_HASHTABLE(thread->std.properties);
}
thread->std.properties = Z_ARRVAL_P(symbols);
FREE_ZVAL(symbols);
}
} else {
/*
* Discarding in any context affects all contexts and provides an easy way to release resources that aren't going to be used
*/
free(thread->serial);
thread->serial = NULL;
}
}
} else {
zend_error(E_WARNING, "pthreads detected failure while joining with the referenced Thread (%lu)", thread->tid);
RETURN_FALSE;
}
} else {
zend_error(E_WARNING, "pthreads has detected that the referenced Thread (%lu) has already been joined", thread->tid);
RETURN_FALSE;
}
} else {
zend_error(E_WARNING, "pthreads has detected an attempt to join a thread that is not yet started");
RETURN_FALSE;
}
} else {
zend_error(E_WARNING, "pthreads has detected an attempt to join in an incorrect context");
RETURN_FALSE;
}
} else {
zend_error(E_ERROR, "pthreads has expereinced an internal error and cannot continue");
RETURN_FALSE;
}
} /* }}} */
/* {{{ proto boolean Thread::getThread(long $tid)
Thread::find shall attempt to import the Thread identified by tid into the current context, for the purposes of synchronization */
PHP_METHOD(Thread, getThread)
{
PTHREAD found = NULL;
ulong tid = 0L;
if (PTHREADS_G(importing)) {
if (EG(called_scope) == pthreads_class_entry) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tid)==SUCCESS) {
if (tid && tid >0L) {
if ((found=pthreads_globals_find((ulong)tid))!=NULL) {
if (!PTHREADS_IS_CREATOR(found)) {
if (pthreads_import(found, &return_value TSRMLS_CC)) {
return;
} else zend_error(E_WARNING, "pthreads has detected that Thread::getThread(%lu) has failed", tid);
} else zend_error(E_WARNING, "pthreads has detected that Thread::getThread(%lu) has nothing to import as the requested thread was created in the current", tid);
RETURN_FALSE;
} else zend_error(E_WARNING, "pthreads has detected that Thread::getThread(%lu) has nothing to import as the requested thread is no longer running", tid);
} else zend_error(E_WARNING, "pthreads has detected incorrect use of Thread::getThread, no valid Thread identifier was specified");
}
} else zend_error(E_WARNING, "pthreads has detected incorrect use of Thread::getThread, this method may only be called statically as Thread::getThread");
} else zend_error(E_WARNING, "pthreads has detected an attempt to use a prohibited method (Thread::getThread), importing Threads is disabled in this environment");
RETURN_NULL();
}
/* }}} */
/* {{{ proto long Thread::getThreadId()
Will return the identifier of the referenced Thread */
PHP_METHOD(Thread, getThreadId)
{
if (getThis()) {
PTHREAD thread = PTHREADS_FETCH;
if (thread) {
ZVAL_LONG(return_value, thread->tid);
}
} else ZVAL_LONG(return_value, pthreads_self());
} /* }}} */
/* {{{ proto long Thread::getCount()
Will return the number of threads currently running */
PHP_METHOD(Thread, getCount)
{
RETURN_LONG(pthreads_globals_count());
} /* }}} */
/* {{{ proto long Thread::getMax()
Will return the maximum number of threads you may create in this environment
@NOTE this setting very much depends on the kind of environment you're executing php in */
PHP_METHOD(Thread, getMax)
{
RETURN_LONG(PTHREADS_G(max));
} /* }}} */
/* {{{ proto long Thread::getPeak()
Will return the peak number of threads executing */
PHP_METHOD(Thread, getPeak)
{
RETURN_LONG(pthreads_globals_peak());
} /* }}} */
/* {{{ proto long Mutex::create([boolean lock])
Will create a new mutex and return it's handle. If lock is true it will lock the mutex before returning the handle to the calling thread.
Because of their nature you need to destroy these explicitly with Mutex::destroy */
PHP_METHOD(Mutex, create)
{
zend_bool lock;
pthread_mutex_t *mutex;
if ((mutex=(pthread_mutex_t*) calloc(1, sizeof(pthread_mutex_t)))!=NULL) {
switch(pthread_mutex_init(mutex, &defmutex)){
case SUCCESS:
if (ZEND_NUM_ARGS()) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &lock)==SUCCESS) {
if (lock) {
switch(pthread_mutex_lock(mutex)){
case SUCCESS: RETURN_LONG((ulong)mutex); break;
case EDEADLK: RETURN_LONG((ulong)mutex); break;
default:
zend_error(E_ERROR, "pthreads detected an unspecified error while attempting to lock new mutex");
pthread_mutex_destroy(mutex);
free(mutex);
}
} else { RETURN_LONG((ulong)mutex); }
}
} else { RETURN_LONG((ulong)mutex); }
break;
case EAGAIN:
zend_error(E_ERROR, "pthreads detected that the system lacks the necessary resources (other than memory) to initialise another mutex");
free(mutex);
break;
case ENOMEM: /* I would imagine we would fail to write this message to output if we are really out of memory */
zend_error(E_ERROR, "pthreads detected that the system lacks the necessary memory to initialise another mutex");
free(mutex);
break;
case EPERM:
zend_error(E_ERROR, "pthreads detected that the caller does not have permission to initialize mutex");
free(mutex);
break;
default:
zend_error(E_ERROR, "pthreads detected an internal error while attempting to initialize mutex");
free(mutex);
}
} else zend_error(E_ERROR, "pthreads has failed to allocate memory for mutex and cannot continue");
RETURN_FALSE;
} /* }}} */
/* {{{ proto boolean Mutex::lock(long mutex)
Locks the mutex referenced so that the calling thread owns the mutex */
PHP_METHOD(Mutex, lock)
{
pthread_mutex_t *mutex;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &mutex)==SUCCESS && mutex) {
switch (pthread_mutex_lock(mutex)) {
case SUCCESS: RETURN_TRUE; break;
case EDEADLK:
zend_error(E_WARNING, "pthreads has detected that the calling thread already owns the mutex");
RETURN_TRUE;
break;
case EINVAL:
zend_error(E_WARNING, "pthreads has detected that the variable passed is not a valid mutex");
break;
/* the default type of mutex prohibits this error from being raised
there may be more control over mutex types in the future so left for completeness */
case E_ERROR:
zend_error(E_WARNING, "The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded");
break;
default:
zend_error(E_ERROR, "pthreads detected an internal error while locking mutex and cannot continue");
}
}
RETURN_FALSE;
} /* }}} */
/* {{{ proto boolean Mutex::trylock(long mutex)
Will attempt to lock the mutex without causing the calling thread to block */
PHP_METHOD(Mutex, trylock)
{
pthread_mutex_t *mutex;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &mutex)==SUCCESS && mutex) {
switch (pthread_mutex_trylock(mutex)) {
case SUCCESS: RETURN_TRUE; break;
case EBUSY: RETURN_FALSE; break;
case EDEADLK:
zend_error(E_WARNING, "pthreads has detected that the calling thread already owns the mutex");
RETURN_TRUE;
break;
case EINVAL:
zend_error(E_WARNING, "pthreads has detected that the variable passed is not a valid mutex");
break;
/* again the defmutex setup prohibits this from occuring, present for completeness */
case EAGAIN:
zend_error(E_WARNING, "pthreads detected that the mutex could not be acquired because the maximum number of recursive locks has been exceeded");
break;
default:
zend_error(E_ERROR, "pthreads detected an internal error while trying to lock mutex and cannot continue");
}
}
RETURN_FALSE;
} /* }}} */
/* {{{ proto boolean Mutex::unlock(long mutex, [bool destroy])
Will unlock the mutex referenced, and optionally destroy it */
PHP_METHOD(Mutex, unlock)
{
pthread_mutex_t *mutex;
zend_bool destroy = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &mutex, &destroy)==SUCCESS && mutex) {
switch (pthread_mutex_unlock(mutex)) {
case SUCCESS:
if (destroy > 0) {
pthread_mutex_destroy(mutex);
free(mutex);
}
RETURN_TRUE;
break;
case EINVAL:
zend_error(E_WARNING, "pthreads has detected that the variable passed is not a valid mutex");
break;
case EPERM:
zend_error(E_WARNING, "pthreads has detected that the calling thread does not own the mutex");
break;
default:
zend_error(E_ERROR, "pthreads detected an internal error while unlocking mutex and cannot continue");
}
}
RETURN_FALSE;
} /* }}} */
/* {{{ proto boolean Mutex::destroy(long mutex)
Will destroy the mutex referenced and free memory associated */
PHP_METHOD(Mutex, destroy)
{
pthread_mutex_t *mutex;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &mutex)==SUCCESS && mutex) {
switch (pthread_mutex_destroy(mutex)) {
case SUCCESS:
free(mutex);
RETURN_TRUE;
break;
case EBUSY:
zend_error(E_WARNING, "pthreads has detected an attempt to destroy mutex while it is locked or referenced");
break;
case EINVAL:
zend_error(E_WARNING, "pthreads has detected that the variable passed is not a valid mutex");
break;
default:
zend_error(E_ERROR, "pthreads detected an internal error while attempting to destroy mutex and cannot continue");
}
}
RETURN_FALSE;
} /* }}} */
/* {{{ proto long Cond::create()
This will create a condition, because of their nature you need to destroy these explicitly with Cond::destroy */
PHP_METHOD(Cond, create)
{
pthread_cond_t *condition;
if ((condition=(pthread_cond_t*) calloc(1, sizeof(pthread_cond_t)))!=NULL) {
switch (pthread_cond_init(condition, NULL)) {
case SUCCESS:
RETURN_LONG((ulong)condition);
break;
case EAGAIN:
zend_error(E_ERROR, "pthreads detected that the system lacks the necessary resources (other than memory) to initialise another condition");
free(condition);
break;
case ENOMEM: /* I would imagine we would fail to write this message to output if we are really out of memory */
zend_error(E_ERROR, "pthreads detected that the system lacks the necessary memory to initialise another condition");
free(condition);
break;
default:
zend_error(E_ERROR, "pthreads detected an internal error while initializing new condition and cannot conitnue");
free(condition);
}
} else zend_error(E_ERROR, "pthreads failed to allocate memory for new condition and cannot continue");
RETURN_FALSE;
} /* }}} */
/* {{{ proto boolean Cond::signal(long condition)
This will signal a condition */
PHP_METHOD(Cond, signal)
{
pthread_cond_t *condition;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &condition) == SUCCESS && condition) {
switch (pthread_cond_signal(condition)) {
case SUCCESS: RETURN_TRUE; break;
case EINVAL:
zend_error(E_WARNING, "pthreads has detected the condition referenced does not refer to a valid condition");
break;
default:
zend_error(E_ERROR, "pthreads detected an internal error while signaling condition and cannot continue");
}
}