forked from birdwyx/phpgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpgo.cc
1737 lines (1426 loc) · 50 KB
/
phpgo.cc
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-2015 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: |
+----------------------------------------------------------------------+
*/
/* $Id$ */
/*
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"*/
#include "stdinc.h"
#include <signal.h> /* for signal */
#include <execinfo.h> /* for backtrace() */
#include <libgo/coroutine.h>
/*remove the libgo go() included from the coroutine.h since
we'll also have function go defined later on in this file*/
#undef go
#include "php_phpgo.h"
#include "go.h"
#include "go_scheduler.h"
#include "go_chan.h"
#include "go_mutex.h"
#include "go_runtime.h"
#include "go_wait_group.h"
#include "go_time.h"
#include "go_select.h"
#include "zend_interfaces.h"
#include "defer.h"
#include "phpgo_context.h"
/* If you declare any globals in php_phpgo.h uncomment this:*/
ZEND_DECLARE_MODULE_GLOBALS(phpgo)
/* True global resources - no need for thread safety here */
//static int le_phpgo;
zend_class_entry ce_go_chan, *ce_go_chan_ptr;
zend_class_entry ce_go_mutex, *ce_go_mutex_ptr;
zend_class_entry ce_go_wait_group,*ce_go_wait_group_ptr;
zend_class_entry ce_go_scheduler, *ce_go_scheduler_ptr;
zend_class_entry ce_go_selector, *ce_go_selector_ptr;
zend_class_entry ce_go_time, *ce_go_time_ptr;
zend_class_entry ce_go_runtime, *ce_go_runtime_ptr;
/* {{{ arginfo_go_chan_push[]
*
*/
//ZEND_BEGIN_ARG_INFO_EX(arginfo_go_chan_push, pass_rest_by_reference, return_reference, required_num_args)
ZEND_BEGIN_ARG_INFO_EX(arginfo_go_chan_push, 0, 0, 1)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_go_wait_group_add, 0, 0, 1)
ZEND_ARG_INFO(0, delta)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_go_selector_ctor, 0, 0, 1)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_go_time_tick, 0, 0, 1)
ZEND_ARG_INFO(0, nanoseconds)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_go_time_after, 0, 0, 1)
ZEND_ARG_INFO(0, nanoseconds)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_go_time_sleep, 0, 0, 1)
ZEND_ARG_INFO(0, nanoseconds)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_go_selector_loop, 0, 0, 1)
ZEND_ARG_INFO(0, done_chan)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ phpgo_functions[]
*
* Every user visible function must have an entry in phpgo_functions[].
*/
const zend_function_entry phpgo_functions[] = {
//PHP_FE(confirm_phpgo_compiled, NULL) /* For testing, remove later. */
PHP_FE(go, NULL)
PHP_FE(goo, NULL)
PHP_FE(go_debug, NULL)
//ZEND_NS_NAMED_FE(PHPGO_NS, go_debug, ZEND_FN(go_go_debug), NULL)
PHP_FE(select, NULL)
PHP_FE_END /* Must be the last line in phpgo_functions[] */
};
/* }}} */
/* {{{ phpgo_channel_functions[]
*
*/
const zend_function_entry go_chan_methods[] = {
PHP_ME(Chan, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
PHP_ME(Chan, Push, arginfo_go_chan_push, ZEND_ACC_PUBLIC )
PHP_ME(Chan, Pop, NULL, ZEND_ACC_PUBLIC )
PHP_ME(Chan, TryPush, NULL, ZEND_ACC_PUBLIC )
PHP_ME(Chan, TryPop, NULL, ZEND_ACC_PUBLIC )
PHP_ME(Chan, Close, NULL, ZEND_ACC_PUBLIC )
PHP_ME(Chan, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR )
PHP_FE_END /* Must be the last line */
};
const zend_function_entry go_mutex_methods[] = {
PHP_ME(Mutex, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
PHP_ME(Mutex, Lock, NULL, ZEND_ACC_PUBLIC )
PHP_ME(Mutex, Unlock, NULL, ZEND_ACC_PUBLIC )
PHP_ME(Mutex, TryLock, NULL, ZEND_ACC_PUBLIC )
PHP_ME(Mutex, IsLock, NULL, ZEND_ACC_PUBLIC )
PHP_ME(Mutex, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR )
PHP_FE_END /* Must be the last line */
};
const zend_function_entry go_wait_group_methods[] = {
PHP_ME(WaitGroup, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
PHP_ME(WaitGroup, Add, arginfo_go_wait_group_add, ZEND_ACC_PUBLIC )
PHP_ME(WaitGroup, Done, NULL, ZEND_ACC_PUBLIC )
PHP_ME(WaitGroup, Wait, NULL, ZEND_ACC_PUBLIC )
PHP_ME(WaitGroup, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR )
PHP_FE_END /* Must be the last line */
};
const zend_function_entry go_scheduler_methods[] = {
PHP_ME(Scheduler, Run, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Scheduler, Join, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Scheduler, Loop, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_FE_END /* Must be the last line */
};
const zend_function_entry go_selector_methods[] = {
PHP_ME(Selector, __construct, arginfo_go_selector_ctor, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
PHP_ME(Selector, Select, NULL, ZEND_ACC_PUBLIC )
PHP_ME(Selector, Loop, arginfo_go_selector_loop, ZEND_ACC_PUBLIC )
PHP_ME(Selector, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR )
PHP_FE_END /* Must be the last line */
};
const zend_function_entry go_time_methods[] = {
PHP_ME(Time, Tick, arginfo_go_time_tick, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Time, After, arginfo_go_time_after, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Time, Sleep, arginfo_go_time_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_FE_END /* Must be the last line */
};
const zend_function_entry go_runtime_methods[] = {
PHP_ME(Runtime, NumGoroutine, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Runtime, Gosched, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Runtime, Goid, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Runtime, Quit, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_FE_END /* Must be the last line */
};
/* }}} */
/* {{{ phpgo_module_entry
*/
zend_module_entry phpgo_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"phpgo",
phpgo_functions,
PHP_MINIT(phpgo),
PHP_MSHUTDOWN(phpgo),
PHP_RINIT(phpgo), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(phpgo), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(phpgo),
#if ZEND_MODULE_API_NO >= 20010901
PHP_PHPGO_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_PHPGO
ZEND_GET_MODULE(phpgo)
#endif
/* {{{ phpgo debug functions
*/
void phpgo_hex_dump(void* buff, size_t n){
int i = 0 ;
unsigned char* b = (unsigned char*)buff;
while( i< (int)n ){
if(i%16 == 0 && i > 0) {
int j = i - 16;
if(j >= 0){
while(j < i){
unsigned char c = isprint(b[j])? b[j] : '.';
php_printf("%c", c);
j++;
}
}
php_printf("\n");
}
php_printf("%02x ", b[i]);
i++;
}
php_printf("\n");
}
#if PHP_MAJOR_VERSION < 7
void phpgo_zval_dump(zval* zv){
php_printf("zval %p------>\n", zv);
php_printf("type: %d\n", zv->type);
php_printf("refcount__gc: %d\n", zv->refcount__gc);
php_printf("is_ref__gc: %d\n", zv->is_ref__gc);
php_printf("value: \n");
phpgo_hex_dump(&(zv->value), sizeof(zv->value));
php_printf("handle: %x\n", zv->value.obj.handle);
php_printf("handlers: %x\n", zv->value.obj.handlers);
php_printf("<------\n");
//zv->refcount__gc = 3;
}
#else
void phpgo_zval_dump(zval* zv){
const char* val_types[] = {
//0..10
"IS_UNDEF",
"IS_NULL",
"IS_FALSE",
"IS_TRUE",
"IS_LONG",
"IS_DOUBLE",
"IS_STRING",
"IS_ARRAY",
"IS_OBJECT",
"IS_RESOURCE",
"IS_REFERENCE",
"IS_CONSTANT", //11
"IS_CONSTANT_AST", //12
"_IS_BOOL", //13
"IS_CALLABLE", //14
"IS_INDIRECT", //15
"",
"IS_PTR", //17
"IS_VOID", //18
"IS_ITERABLE", //19
"_IS_ERROR" //20
};
php_printf("zval %p------>\n", zv);
php_printf("u1.v.type: %02x(%s)\n", zv->u1.v.type, val_types[zv->u1.v.type]);
php_printf("u1.v.type_flags: %02x\n", zv->u1.v.type_flags);
//php_printf("u1.v.const_flags: %02x\n", zv->u1.v.const_flags);
//php_printf("u1.v.reserved: %02x\n", zv->u1.v.reserved);
php_printf("u1.v == u1.type_info ==: %08x\n", zv->u1.type_info);
php_printf("u2: %08x\n", zv->u2.next);
php_printf("value: %016x\n", zv->value.lval);
if( Z_REFCOUNTED_P(zv) ){
php_printf("value.counted->\n");
php_printf(" gc.refcount: %08x\n", zv->value.counted->gc.refcount);
php_printf(" gc.u.v.type: %02x\n", zv->value.counted->gc.u.v.type);
php_printf(" gc.u.v.flags: %02x\n", zv->value.counted->gc.u.v.flags);
php_printf(" gc.u.v.gc_info: %04x\n", zv->value.counted->gc.u.v.gc_info);
php_printf(" gc.u.type_info(==gc.u.v): %08x\n", zv->value.counted->gc.u.type_info);
}
switch( Z_TYPE_P(zv) ){
case IS_STRING:
php_printf(" string(%d) = %s\n", zv->value.str->len, zv->value.str->val);
break;
case IS_REFERENCE:
php_printf("referece zval:\n");
phpgo_zval_dump( &zv->value.ref->val);
break;
}
}
void phpgo_var_dump(zval* zv){
zval func_name, retval;
ZVAL_STR(&func_name, zend_string_init("var_dump", sizeof("var_dump") - 1, 0));
if (Z_REFCOUNTED_P(zv)){
php_printf("&%d:", zval_refcount_p(zv));
}
assert(call_user_function(&EG(function_table), NULL, &func_name, &retval, 1, zv) == SUCCESS);
zval_ptr_dtor(&func_name);
zval_ptr_dtor(&retval);
}
#endif
/* }}} */
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("phpgo.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_phpgo_globals, phpgo_globals)
STD_PHP_INI_ENTRY("phpgo.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_phpgo_globals, phpgo_globals)
PHP_INI_END()
*/
/* }}} */
/* {{{ php_phpgo_init_globals
*/
/* Uncomment this function if you have INI entries*/
static void php_phpgo_init_globals(zend_phpgo_globals *phpgo_globals)
{
phpgo_globals->phpgo_initialized = false;
phpgo_globals->running_internal_go_routines = 0;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(phpgo)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
ZEND_INIT_MODULE_GLOBALS(phpgo, php_phpgo_init_globals, NULL);
INIT_NS_CLASS_ENTRY(ce_go_chan, PHPGO_NS, "Chan", go_chan_methods); // 类名为 go\Chan
INIT_NS_CLASS_ENTRY(ce_go_mutex, PHPGO_NS, "Mutex", go_mutex_methods);
INIT_NS_CLASS_ENTRY(ce_go_wait_group,PHPGO_NS, "WaitGroup", go_wait_group_methods);
INIT_NS_CLASS_ENTRY(ce_go_scheduler, PHPGO_NS, "Scheduler", go_scheduler_methods);
INIT_NS_CLASS_ENTRY(ce_go_selector, PHPGO_NS, "Selector", go_selector_methods);
INIT_NS_CLASS_ENTRY(ce_go_time, PHPGO_NS, "Time", go_time_methods);
INIT_NS_CLASS_ENTRY(ce_go_runtime, PHPGO_NS, "Runtime", go_runtime_methods);
ce_go_chan_ptr = zend_register_internal_class(&ce_go_chan TSRMLS_CC);
ce_go_mutex_ptr = zend_register_internal_class(&ce_go_mutex TSRMLS_CC);
ce_go_wait_group_ptr= zend_register_internal_class(&ce_go_wait_group TSRMLS_CC);
ce_go_scheduler_ptr = zend_register_internal_class(&ce_go_scheduler TSRMLS_CC);
ce_go_selector_ptr = zend_register_internal_class(&ce_go_selector TSRMLS_CC);
ce_go_time_ptr = zend_register_internal_class(&ce_go_time TSRMLS_CC);
ce_go_runtime_ptr = zend_register_internal_class(&ce_go_runtime TSRMLS_CC);
//zend_declare_property_long(ce_go_chan_ptr,"handle", strlen("handle"), -1, ZEND_ACC_PUBLIC TSRMLS_CC);
//zend_declare_property_long(ce_go_chan_ptr,"capacity",strlen("capacity"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_class_constant_long(ce_go_time_ptr, "NANOSECOND", sizeof("NANOSECOND")-1, GoTime::Nanosecond TSRMLS_CC);
zend_declare_class_constant_long(ce_go_time_ptr, "MICROSECOND", sizeof("MICROSECOND")-1, GoTime::Microsecond TSRMLS_CC);
zend_declare_class_constant_long(ce_go_time_ptr, "MILLISECOND", sizeof("MILLISECOND")-1, GoTime::Millisecond TSRMLS_CC);
zend_declare_class_constant_long(ce_go_time_ptr, "SECOND", sizeof("SECOND")-1, GoTime::Second TSRMLS_CC);
zend_declare_class_constant_long(ce_go_time_ptr, "MINUTE", sizeof("MINUTE")-1, GoTime::Minute TSRMLS_CC);
zend_declare_class_constant_long(ce_go_time_ptr, "HOUR", sizeof("HOUR")-1, GoTime::Hour TSRMLS_CC);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(phpgo)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(phpgo)
{
phpgo_initialize();
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(phpgo)
{
/*
need to cleanup the scheduler_ctx earlier here instead of in the
scheduler_ctx destructor, since php (debug mode) will report memleaks
and free memory on behave ealier than the C++ destructors
*/
scheduler_ctx.Cleanup();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(phpgo)
{
php_info_print_table_start();
php_info_print_table_header(2, "phpgo support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/*
* 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
*/
/* {{{ proto Chan::__construct
* Create a go channel object
*/
PHP_METHOD(Chan,__construct){
//printf("Chan::__construct\n");
long capacity = 0;
char* name = NULL;
size_t name_len = 0;
zval* z1 = NULL;
zval* z2 = NULL;
zval* z3 = NULL;
bool copy = false;
if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &z1) == FAILURE ){
zend_error(E_ERROR, "phpgo: Chan::__construct: getting parameter failure");
RETURN_NULL();
}
if( z1 ){
if( Z_TYPE_P(z1) == IS_LONG ){
capacity = Z_LVAL_P(z1);
}else if( Z_TYPE_P(z1) == IS_ARRAY /*&& z1->value.ht not necessary,remove for php7*/){
void** data = NULL;
#define zvalp_data PHP5_VS_7( *(zval**)data, (zval*)data )
if( phpgo_zend_hash_find(HASH_OF(z1), "name", sizeof("name"), (void**)&data) == SUCCESS ){
if(Z_TYPE_P( zvalp_data ) != IS_STRING){
zend_error(E_ERROR, "phpgo: Chan( $options ): option \"name\" must be string");
RETURN_NULL();
}
name = Z_STRVAL_P( zvalp_data );
name_len = Z_STRLEN_P( zvalp_data );
}
if( phpgo_zend_hash_find(HASH_OF(z1), "capacity", sizeof("capacity"), (void**)&data) == SUCCESS ){
if(Z_TYPE_P( zvalp_data ) != IS_LONG){
zend_error(E_ERROR, "phpgo: Chan( $options ): option \"capacity\" must be long");
RETURN_NULL();
}
capacity = Z_LVAL_P( zvalp_data );
}
if( phpgo_zend_hash_find(HASH_OF(z1), "copy", sizeof("copy"), (void**)&data) == SUCCESS ){
if( !PHPGO_ZVAL_IS_BOOL( zvalp_data ) ){
zend_error(E_ERROR, "phpgo: Chan( $options ): option \"copy\" must be bool");
RETURN_NULL();
}
copy = Z_BVAL_P( zvalp_data );
}
}else{
zend_error(E_ERROR, "phpgo: Chan( long $capacity| array $options ): parameter 1 must be long or array");
RETURN_NULL();
//invalid argument
}
}else{
//no argument provided
}
if( capacity < 0 ){
zend_error(E_ERROR, "phpgo: Chan(): the capacity must be greater than or equal to 0");
RETURN_NULL();
}
void* chan = GoChan::Create(capacity, name, name_len, copy);
return_value = getThis();
zend_update_property_long (ce_go_chan_ptr, return_value, "handle", sizeof("handle")-1, (long)chan TSRMLS_CC);
zend_update_property_string(ce_go_chan_ptr, return_value, "name", sizeof("name")-1, name?name:"" TSRMLS_CC);
zend_update_property_long (ce_go_chan_ptr, return_value, "capacity", sizeof("capacity")-1, capacity TSRMLS_CC);
zend_update_property_bool (ce_go_chan_ptr, return_value, "copy", sizeof("copy")-1, copy TSRMLS_CC);
}
/* }}} */
/* {{{ proto Chan::push
* Push object to channel, block if channel not available to write
*/
PHP_METHOD(Chan,Push){
//printf("Chan::Push\n");
zval* chan = NULL;
zval* z = NULL;
if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &z) == FAILURE ){
zend_error(E_ERROR, "phpgo: Chan::push: getting parameter failure");
RETURN_FALSE;
}
auto self = getThis();
chan = phpgo_zend_read_property(ce_go_chan_ptr, self, "handle", sizeof("handle")-1, true TSRMLS_CC);
if(!chan)
RETURN_FALSE;
auto lchan = Z_LVAL_P(chan);
auto rc = GoChan::Push( (void*)lchan, z TSRMLS_CC);
if( rc==GoChan::RCode::channel_closed ){
zend_error(E_WARNING, "phpgo: Chan::push(): try to push to an allready closed channel");
RETURN_NULL();
}
RETURN_BOOL( rc==GoChan::RCode::success );
}
/* }}} */
/* {{{ proto Chan::tryPush
* Try to push object to channel, if writable
* otherwise return immediately, with channel unwritten
*/
PHP_METHOD(Chan,TryPush){
//printf("Chan::TryPush\n");
zval* chan = NULL;
zval* z = NULL;
if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &z) == FAILURE ){
zend_error(E_ERROR, "phpgo: Chan::tryPush: getting parameter failure");
RETURN_FALSE;
}
auto self = getThis();
chan = phpgo_zend_read_property(ce_go_chan_ptr, self, "handle", sizeof("handle")-1, true TSRMLS_CC);
if(!chan)
RETURN_FALSE;
auto lchan = Z_LVAL_P(chan);
auto rc = GoChan::TryPush( (void*)lchan, z TSRMLS_CC);
if( rc==GoChan::RCode::channel_closed ){
zend_error(E_WARNING, "phpgo: Chan::tryPush(): try to push to an allready closed channel");
RETURN_NULL();
}
RETURN_BOOL( rc==GoChan::RCode::success );
}
/* }}} */
/* {{{ proto Chan::pop
* pop an object from channel, block if no data to read
* return
* data read if available, or
* false on error, or
* NULL if channel closed and no data ready to read
*/
PHP_METHOD(Chan,Pop){
//printf("Chan::Pop\n");
zval* self = getThis();
zval* chan = phpgo_zend_read_property(ce_go_chan_ptr, self, "handle", sizeof("handle")-1, true TSRMLS_CC);
if(!chan)
RETURN_FALSE;
auto lchan = Z_LVAL_P(chan);
zval* z = GoChan::Pop( (void*)lchan );
//Pop return false on error
if(!z)
RETURN_FALSE;
// php7: this z was emalloc'ed in ChannelData::ChannelData()
// or inside the GoChan::Pop()
// we'll need to efree it by ourselves since the zval dtor's
// won't do this for us in php7
// note: the PHPGO_FREE_PZVAL has no effect in php5
defer{ PHPGO_FREE_PZVAL(z); };
//return the zval, or NULL if channel closed and no data ready to read
RETURN_ZVAL(z, 1, 1);
}
/* }}} */
/* {{{ proto Chan::tryPop
* Try to pop data from channel, if readable
* return immediately if no data to read
* return:
* data read if available
* false if channel not ready for reading
* NULL if channel closed and no data ready to read
*/
PHP_METHOD(Chan,TryPop){
//printf("Chan::TryPop\n");
zval* self = getThis();
zval* chan = phpgo_zend_read_property(ce_go_chan_ptr, self, "handle", sizeof("handle")-1, true TSRMLS_CC);
if(!chan)
RETURN_FALSE;
auto lchan = Z_LVAL_P(chan);
zval* z = GoChan::TryPop( (void*)lchan );
// GoChan::TryPop will
// return nullptr if not ready
// return ZVAL_NULL if closed and no data ready to read
// otherwise return data read
// return false if channel not ready
if(!z)
RETURN_FALSE;
// php7: this z was emalloc'ed in ChannelData::ChannelData()
// or inside the GoChan::TryPop()
// we'll need to efree it by ourselves since the zval dtor's
// won't do this for us in php7
// note: the PHPGO_FREE_PZVAL has no effect in php5
defer{ PHPGO_FREE_PZVAL(z); };
// return the zval, or NULL if channel closed and no data ready to read
RETURN_ZVAL(z, 1, 1);
}
/* }}} */
/* {{{ proto Chan::close
* Close a channel
*/
PHP_METHOD(Chan,Close){
//printf("Chan::Close\n");
zval* self = getThis();
zval* chan = phpgo_zend_read_property(ce_go_chan_ptr, self, "handle", sizeof("handle")-1, true TSRMLS_CC);
if(!chan)
RETURN_FALSE;
auto lchan = Z_LVAL_P(chan);
bool ok = GoChan::Close( (void*)lchan );
if(!ok){
zend_error(E_WARNING, "phpgo: Chan::close(): try to close an allready closed channel");
}
RETURN_BOOL(ok);
}
/* }}} */
/* {{{ proto Chan::__destruct
* Destroy a go channel object
*/
PHP_METHOD(Chan,__destruct){
//printf("Chan::__destruct\n");
zval* self = getThis();
zval* chan = phpgo_zend_read_property(ce_go_chan_ptr, self, "handle", sizeof("handle")-1, true TSRMLS_CC);
if(!chan)
RETURN_FALSE;
auto lchan = Z_LVAL_P(chan);
if(lchan){
GoChan::Destroy((void*)lchan);
}
}
/* }}} */
/* {{{ proto Selector::__construct
* Create a selector object, it won't be called by the PHP code
* it's called and resulting object returned by the select()
*/
PHP_METHOD(Selector,__construct){
long selector = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &selector) == FAILURE) {
zend_error(E_ERROR, "phpgo: Selector::__construct: getting parameter failure");
return;
}
GO_SELECTOR* sel = (GO_SELECTOR*)selector;
return_value = getThis();
zend_update_property_long(ce_go_selector_ptr, return_value, "handle", sizeof("handle")-1, selector TSRMLS_CC);
}
/* }}} */
/* {{{ proto Selector::select
* Do select and return a selector object (for a faster call next time)
*/
PHP_METHOD(Selector, Select){
zval* self = getThis();
zval* z_selector = phpgo_zend_read_property(ce_go_selector_ptr, self, "handle", sizeof("handle")-1, true TSRMLS_CC);
if(!z_selector || Z_TYPE_P(z_selector) == IS_NULL){
zend_error(E_ERROR, "phpgo: Selector::select(): error reading object handle");
return;
}
auto selector = (GO_SELECTOR*)Z_LVAL_P(z_selector);
if( !selector ){
zend_error(E_ERROR, "phpgo: Selector::select(): null object handle");
return;
}
phpgo_select(selector->case_array, selector->case_count TSRMLS_CC);
RETURN_ZVAL(self, 1, 0);
}
/* }}} */
/* {{{ proto Selector::loop
* Do select in a loop until the channel is written to or closed
*/
PHP_METHOD(Selector, Loop){
zval* z_chan = NULL;
if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &z_chan) == FAILURE ){
zend_error(E_ERROR, "phpgo: Selector::loop(): getting parameter failure");
RETURN_FALSE;
}
zval* chan = phpgo_zend_read_property(ce_go_chan_ptr, z_chan, "handle", sizeof("handle")-1, true TSRMLS_CC);
if(!chan || Z_TYPE_P(chan) == IS_NULL ){
zend_error(E_ERROR, "phpgo: Selector::loop(): null channel handle");
RETURN_FALSE;
}
auto lchan = Z_LVAL_P(chan);
//--
zval* self = getThis();
zval* z_selector = phpgo_zend_read_property(ce_go_selector_ptr, self, "handle", sizeof("handle")-1, true TSRMLS_CC);
if(!z_selector || Z_TYPE_P(z_selector) == IS_NULL){
zend_error(E_ERROR, "phpgo: Selector::loop(): error reading object handle");
RETURN_FALSE;
}
auto selector = (GO_SELECTOR*)Z_LVAL_P(z_selector);
if( !selector ){
zend_error(E_ERROR, "phpgo: Selector::loop(): null object handle");
RETURN_FALSE;
}
// GoChan::TryPop will
// - return nullptr if not ready
// return ZVAL_NULL if closed
// otherwise return data read
zval* z = nullptr;
while( !( z = GoChan::TryPop( (void*)lchan ) ) ){
phpgo_select(selector->case_array, selector->case_count TSRMLS_CC);
}
PHP7_AND_ABOVE( defer{ PHPGO_FREE_PZVAL(z); }; );
//todo: php7: test push ref-counted variable to chan and check mem-leak
//since add_ref to non-ref-counted has no effect but to ref-counted does have
//phpgo_zval_add_ref(&z);
RETURN_ZVAL(z, 1, 1);
}
/* }}} */
/* {{{ proto Selector::__destruct
* Destroy the selector
*/
PHP_METHOD(Selector,__destruct){
zval* self = getThis();
zval* selector = phpgo_zend_read_property(ce_go_selector_ptr, self, "handle", sizeof("handle")-1, true TSRMLS_CC);
if(!selector)
RETURN_FALSE;
auto lselector = Z_LVAL_P(selector);
if(lselector){
GO_SELECTOR* sel = (GO_SELECTOR*)lselector;
auto case_count = sel->case_count;
auto case_array = sel->case_array;
for(auto i = 0; i < case_count; i++){
if( case_array[i].chan /*&&
Z_TYPE_P(case_array[i].chan) != IS_NULL*/ ) {
phpgo_zval_ptr_dtor(&case_array[i].chan);
PHPGO_FREE_PZVAL(case_array[i].chan);
}
if( case_array[i].value /*&&
Z_TYPE_P(case_array[i].value) != IS_NULL*/ ) {
phpgo_zval_ptr_dtor(&case_array[i].value);
PHPGO_FREE_PZVAL(case_array[i].value);
}
if( case_array[i].callback /*&&
Z_TYPE_P(case_array[i].callback) != IS_NULL*/) {
phpgo_zval_ptr_dtor(&case_array[i].callback);
PHPGO_FREE_PZVAL(case_array[i].callback);
}
}
efree(sel->case_array);
efree(sel);
}
}
/* }}} */
/* {{{ proto int go( callable $func, ...$args )
* run the $func as in a go routine in the current thread context,
* do not wait the function to complete
* returns:
* true
*/
PHP_FUNCTION(go)
{
int argc = ZEND_NUM_ARGS();
if(argc < 1){
zend_error(E_ERROR, "phpgo: go(): callable missing in parameter list");
RETURN_FALSE;
}
PHPGO_ARG_TYPE* args = NULL;
args = (PHPGO_ARG_TYPE*)safe_emalloc(argc, sizeof(PHPGO_ARG_TYPE), 0);
defer{
if(args) efree(args);
};
if( zend_get_parameters_array_ex(argc, args) == FAILURE ){
zend_error(E_ERROR, "phpgo: go(): error getting parameters");
RETURN_FALSE;
}
FUNC_NAME_TYPE func_name = NULL;
defer{
if(func_name) FREE_FUNC_NAME(func_name);
};
if(!zend_is_callable( PHPGO_ARG_TO_PZVAL(args[0]), 0, &func_name TSRMLS_CC)){
zend_error(E_ERROR, "phpgo: go(): function '%s' is not callable", func_name);
RETURN_FALSE;
}
if( argc > 1 && Z_TYPE_P( PHPGO_ARG_TO_PZVAL(args[1]) ) != IS_ARRAY ){
zend_error(E_ERROR, "phpgo: go(): parameter 2 expected to be an array");
RETURN_FALSE;
}
bool ok = phpgo_go( GoRoutineOptions::gro_default, 0/*stack_size*/, argc, args TSRMLS_CC);
RETURN_BOOL( ok);
}
/* {{{ proto int go( array $options, callable $func, ...$args )
* run the $func as in a go routine in the current thread context,
* do not wait the function to complete
* the available options are:
* $options['stack_size']:
* provide a customized stack size to the go routine
* if the provided stack size is less than 32K bytes, it will be round
* up to 32K bytes.
* Default is 1024K bytes if not provided
* Note: the stack size provide a max size of the stack can reach, the
* underlying mechanism increases stack bit by bit thus a large stack
* size does not necessarily mean a waste of memory
* $options['isolate_http_globals']
* denotes whether the http "super globals" - $_GET, $_POST ... etc should
* be isolated from the go routine's parent
* if ture: the super globals will be copied from parent on the go routine
* creation, and then both sets of super globals are maintained seperately,
* i.e, change of super golbals in go routine does not change those of the
* parent (can be a go routine or the scheduler) ans vise versa
* if false: the super globals are shared by the go routine and it's parent
* returns:
* true
*/
PHP_FUNCTION(goo)
{
bool isolate_http_globals = false;
uint32_t stack_size = 0;
int argc = ZEND_NUM_ARGS();
if(argc < 2){
zend_error(E_ERROR, "phpgo: goo(): at least 2 parameters required");
RETURN_FALSE;
}
PHPGO_ARG_TYPE* args = NULL;
args = (PHPGO_ARG_TYPE*)safe_emalloc(argc, sizeof(PHPGO_ARG_TYPE), 0);
defer{
if(args) efree(args);
};
if ( zend_get_parameters_array_ex(argc, args) == FAILURE) {
zend_error(E_ERROR, "phpgo: goo(): error getting parameters");
RETURN_FALSE;
}
zval* arr_optoins = PHP5_VS_7(*args[0], &args[0]);
if( Z_TYPE_P(arr_optoins) != IS_ARRAY ){
zend_error(E_ERROR, "phpgo: goo(): options must be array");
RETURN_FALSE;
}
void* data = NULL;
#define zval_data PHP5_VS_7( **(zval**)data, *(zval*)data )
if( phpgo_zend_hash_find(HASH_OF(arr_optoins), "stack_size", sizeof("stack_size"), (void**)&data) == SUCCESS ){
zval var = zval_data;
zval_copy_ctor(&var);
convert_to_long(&var);
stack_size = Z_LVAL(var);
}
if( phpgo_zend_hash_find(HASH_OF(arr_optoins), "isolate_http_globals", sizeof("isolate_http_globals"), (void**)&data) == SUCCESS ){
zval var = zval_data;
zval_copy_ctor(&var);
convert_to_boolean(&var);
isolate_http_globals = Z_BVAL(var);
}
FUNC_NAME_TYPE func_name = NULL;
defer{
if(func_name) FREE_FUNC_NAME(func_name);
};
zval* callable = PHP5_VS_7(*args[1], &args[1]);
if (!zend_is_callable(callable, 0, &func_name TSRMLS_CC))
{
zend_error(E_ERROR, "phpgo: go(): function '%s' is not callable", func_name);
RETURN_FALSE;
}
uint64_t options = isolate_http_globals ? GoRoutineOptions::gro_isolate_http_globals : GoRoutineOptions::gro_default;
bool ok = phpgo_go( options, stack_size, argc - 1, &args[1] TSRMLS_CC);
RETURN_BOOL( ok );
}
/* {{{ proto void go_debug($flag)
* set debug flag, -1 for all
*/
PHP_FUNCTION( go_debug )
{
long debug_flag;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &debug_flag) == FAILURE)
{
php_printf("go: getting parameter failure");
return;
}
phpgo_go_debug(debug_flag);
}
/* }}} */
/* {{{ proto void select()
* do go-style select
*/
PHP_FUNCTION(select)
{
#define zvalp_data PHP5_VS_7( *(zval**)data, (zval*)data )
#define GO_SELECT_FREE_RESOURCE() \
do { \
if(args) efree(args); \
if(case_array) efree(case_array); \