-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunc.c
More file actions
4095 lines (3460 loc) · 110 KB
/
func.c
File metadata and controls
4095 lines (3460 loc) · 110 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
#define _GNU_SOURCE // memrchr, rawmemchr
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/prctl.h>
#include <signal.h>
#include <limits.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/statvfs.h>
#include <php.h>
#include <php_main.h>
#include <SAPI.h>
#include <php_output.h>
#include <zend_smart_str.h>
#include <standard/php_var.h>
#include <standard/info.h>
#include <php_network.h>
#include <sockets/php_sockets.h>
#include <zend_exceptions.h>
#include <spl/spl_functions.h>
#include <zend_builtin_functions.h>
#include <zend_signal.h>
#include "func.h"
#include "hash.h"
static sem_t rsem;
static pthread_mutex_t nlock, wlock;
static pthread_cond_t ncond;
static volatile unsigned int threads = 0;
static volatile unsigned int wthreads = 0;
static volatile zend_bool isRun = 1;
static pthread_t mthread;
static pthread_key_t pkey;
volatile unsigned int maxthreads = 256;
volatile unsigned int delay = 1;
volatile zend_bool isDebug = 0;
volatile zend_bool isReload = 0;
volatile int isPerf = 0;
static ts_rsrc_id php_threadtask_globals_id;
#define SINFO(v) ZEND_TSRMG(php_threadtask_globals_id, php_threadtask_globals_struct *, v)
static long le_threadtask_descriptor;
#define PHP_THREADTASK_DESCRIPTOR "threadtask"
static long le_ts_var_descriptor;
#define PHP_TS_VAR_DESCRIPTOR "ts_var_t"
static ts_hash_table_t ts_var;
typedef struct _timeout_t {
pthread_t tid;
double sec;
struct _timeout_t *prev;
struct _timeout_t *next;
} timeout_t;
static timeout_t *thead = NULL;
static pthread_mutex_t tlock;
typedef struct _php_threadtask_globals_struct {
char strftime[20];
zend_bool is_throw_exit;
int timestamp;
timeout_t *timeout;
} php_threadtask_globals_struct;
typedef struct _wait_t {
pthread_t tid;
sem_t sem;
zend_bool isExit;
} wait_t;
typedef struct _task_t {
pthread_t thread;
char *name;
int argc;
char **argv;
char *logfile;
char *logmode;
FILE *fp;
wait_t *wait;
struct _task_t *prev;
struct _task_t *next;
} task_t;
static task_t *taskn = NULL;
static task_t *head_task = NULL;
static task_t *tail_task = NULL;
const char *gettimeofstr() {
time_t t;
struct tm *tmp;
t = time(NULL);
if(SINFO(timestamp) == t) return SINFO(strftime);
SINFO(timestamp) = t;
tmp = localtime(&t);
if (tmp == NULL) {
perror("localtime error");
return "0000-00-00 00:00:00";
}
if (strftime(SINFO(strftime), sizeof(SINFO(strftime)), "%F %T", tmp) == 0) {
perror("strftime error");
return "";
}
return SINFO(strftime);
}
#define MICRO_IN_SEC 1000000.00
double microtime() {
struct timeval tp = {0};
if (gettimeofday(&tp, NULL)) {
return 0;
}
return (double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC);
}
void free_task(task_t *task) {
register int i;
free(task->name);
for(i=0; i<task->argc; i++) {
free(task->argv[i]);
}
free(task->argv);
if(task->logfile) free(task->logfile);
if(task->logmode) free(task->logmode);
if(task->fp) fclose(task->fp);
free(task);
}
void thread_sigmask() {
register int sig;
sigset_t set;
sigemptyset(&set);
for(sig=1; sig<=NSIG; sig++) {
sigaddset(&set, sig);
}
pthread_sigmask(SIG_SETMASK, &set, NULL);
}
void thread_init() {
sem_init(&rsem, 0, 0);
pthread_mutex_init(&nlock, NULL);
pthread_mutex_init(&wlock, NULL);
pthread_cond_init(&ncond, NULL);
pthread_key_create(&pkey, NULL);
pthread_setspecific(pkey, NULL);
#ifdef LOCK_TIMEOUT
pthread_key_create(&tskey, ts_table_table_tid_destroy);
pthread_setspecific(tskey, NULL);
#endif
pthread_mutex_init(&tlock, NULL);
ts_hash_table_init(&ts_var, 2);
thread_sigmask();
mthread = pthread_self();
}
#if defined(MYSQLI_USE_MYSQLND) || PHP_VERSION_ID >= 80200
zend_class_entry *mysqli_link_class_entry;
zend_class_entry *mysqli_stmt_class_entry;
#endif
void thread_running() {
if(PG(error_log) == NULL) PG(display_errors) = 0;
#if defined(MYSQLI_USE_MYSQLND) || PHP_VERSION_ID >= 80200
{
zend_string *mysqli_str = zend_string_init(ZEND_STRL("mysqli"), 0);
zend_string *stmt_str = zend_string_init(ZEND_STRL("mysqli_stmt"), 0);
mysqli_link_class_entry = zend_lookup_class(mysqli_str);
dprintf("mysqli_link_class_entry: %p\n", mysqli_link_class_entry);
mysqli_stmt_class_entry = zend_lookup_class(stmt_str);
dprintf("mysqli_stmt_class_entry: %p\n", mysqli_stmt_class_entry);
zend_string_release_ex(mysqli_str, 0);
zend_string_release_ex(stmt_str, 0);
}
#endif
dprintf("sizeof(Bucket) = %lu\n", sizeof(Bucket));
dprintf("sizeof(HashTable) = %lu\n", sizeof(HashTable));
dprintf("sizeof(zval) = %lu\n", sizeof(zval));
dprintf("sizeof(type_t) = %lu\n", sizeof(type_t));
dprintf("sizeof(value_t) = %lu\n", sizeof(value_t));
dprintf("sizeof(bucket_t) = %lu\n", sizeof(bucket_t));
dprintf("sizeof(hash_table_t) = %lu\n", sizeof(hash_table_t));
dprintf("sizeof(ts_hash_table_t) = %lu\n", sizeof(ts_hash_table_t));
#ifdef LOCK_TIMEOUT
dprintf("sizeof(tskey_hash_table_t) = %lu\n", sizeof(tskey_hash_table_t));
#endif
dprintf("sizeof(sem_t) = %lu\n", sizeof(sem_t));
dprintf("sizeof(pthread_t) = %lu\n", sizeof(pthread_t));
dprintf("sizeof(pthread_mutex_t) = %lu\n", sizeof(pthread_mutex_t));
dprintf("sizeof(pthread_rwlock_t) = %lu\n", sizeof(pthread_rwlock_t));
}
void thread_destroy() {
if(taskn) {
free_task(taskn);
taskn = NULL;
}
ts_hash_table_destroy_ex(&ts_var, 0);
pthread_mutex_destroy(&tlock);
#ifdef LOCK_TIMEOUT
pthread_key_delete(tskey);
#endif
pthread_key_delete(pkey);
pthread_cond_destroy(&ncond);
pthread_mutex_destroy(&nlock);
pthread_mutex_destroy(&wlock);
sem_destroy(&rsem);
}
size_t (*old_ub_write_handler)(const char *str, size_t str_length);
void (*old_flush_handler)(void *server_context);
size_t php_thread_ub_write_handler(const char *str, size_t str_length) {
task_t *task = (task_t *) pthread_getspecific(pkey);
if(task == NULL) {
return old_ub_write_handler(str, str_length);
}
try:
if(task->fp == NULL) {
task->fp = fopen(task->logfile, task->logmode);
if(task->fp == NULL) {
dprintf("[%s] open file %s is failure, code is %d, error is %s\n", task->name, task->logfile, errno, strerror(errno));
return FAILURE;
}
}
if(fwrite(str, 1, str_length, task->fp) != str_length) {
if(errno == EACCES) {
dprintf("[%s] write file %s is failure, code is %d, error is %s\n", task->name, task->logfile, errno, strerror(errno));
return old_ub_write_handler(str, str_length);
} else {
fclose(task->fp);
task->fp = NULL;
goto try;
}
}
return str_length;
}
void php_thread_flush_handler(void *server_context) {
task_t *task = (task_t *) pthread_getspecific(pkey);
if(task == NULL) {
old_flush_handler(server_context);
} else if(task->fp) {
if(fflush(task->fp) == EOF) {
fclose(task->fp);
task->fp = NULL;
}
}
}
void cli_register_file_handles(void);
void *thread_task(task_t *task) {
zend_file_handle file_handle;
sigset_t waitset;
siginfo_t info;
struct timespec timeout;
char path[PATH_MAX];
sigemptyset(&waitset);
sigaddset(&waitset, SIGINT);
sigaddset(&waitset, SIGTERM);
thread_sigmask();
task->thread = pthread_self();
if(task->wait) {
task->wait->tid = task->thread;
}
pthread_mutex_lock(&nlock);
if(tail_task) {
tail_task->next = task;
task->prev = tail_task;
tail_task = task;
} else {
head_task = tail_task = task;
}
pthread_mutex_unlock(&nlock);
ts_resource(0);
#ifdef LOCK_TIMEOUT
pthread_setspecific(tskey, NULL);
#endif
dprintf("begin thread\n");
newtask:
dprintf("[%s] newtask\n", task->name);
prctl(PR_SET_NAME, (unsigned long) task->name);
if(task->logfile && task->logmode) {
pthread_setspecific(pkey, task);
} else {
pthread_setspecific(pkey, NULL);
}
SG(options) |= SAPI_OPTION_NO_CHDIR;
SG(request_info).argc = task->argc;
SG(request_info).argv = task->argv;
SG(request_info).path_translated = task->argv[0];
loop:
if(php_request_startup() == FAILURE) {
goto err;
}
thread_sigmask();
SG(headers_sent) = 1;
SG(request_info).no_headers = 1;
if(PG(error_log) == NULL) PG(display_errors) = 0;
zend_register_string_constant(ZEND_STRL("THREAD_TASK_NAME"), task->name, CONST_CS, PHP_USER_CONSTANT);
cli_register_file_handles();
dprintf("[%s] running\n", task->name);
#if PHP_VERSION_ID >= 70400
CG(skip_shebang) = 1;
#endif
zend_first_try {
if(realpath(task->argv[0], path) == NULL) {
dprintf("[%s] %d %s\n", task->name, errno, strerror(errno));
} else {
zend_stream_init_filename(&file_handle, path);
php_execute_script(&file_handle);
dprintf("[%s] oktask\n", task->name);
}
} zend_end_try();
if(EG(exit_status)) {
dprintf("[%s] exit_status = %d\n", task->name, EG(exit_status));
php_request_shutdown(NULL);
if(!task->wait) {
timeout.tv_sec = delay;
timeout.tv_nsec = 0;
sigprocmask(SIG_BLOCK, &waitset, NULL);
sigtimedwait(&waitset, &info, &timeout);
if(isRun) goto loop;
}
} else {
php_request_shutdown(NULL);
}
SG(request_info).argc = 0;
SG(request_info).argv = NULL;
if(task->wait) {
sem_post(&task->wait->sem);
if(task->wait->isExit) {
goto err;
} else {
task->wait->isExit = 1;
}
}
if(!isRun) goto err;
dprintf("[%s] waittask\n", task->name);
pthread_mutex_lock(&wlock);
wthreads++;
pthread_mutex_unlock(&wlock);
if(!clock_gettime(CLOCK_REALTIME, &timeout)) {
timeout.tv_sec += delay;
timeout.tv_nsec = 0;
sem_timedwait(&rsem, &timeout);
} else sem_wait(&rsem);
pthread_mutex_lock(&wlock);
wthreads--;
if(taskn) {
pthread_mutex_lock(&nlock);
taskn->thread = task->thread;
taskn->prev = task->prev;
taskn->next = task->next;
if(taskn->prev) {
taskn->prev->next = taskn;
} else {
head_task = taskn;
}
if(taskn->next) {
taskn->next->prev = taskn;
} else {
tail_task = taskn;
}
pthread_mutex_unlock(&nlock);
dprintf("[%s] endtask\n", task->name);
free_task(task);
task = taskn;
taskn = NULL;
pthread_mutex_unlock(&wlock);
goto newtask;
} else pthread_mutex_unlock(&wlock);
err:
dprintf("[%s] endtask\n", task->name);
dprintf("end thread\n");
thread_sigmask();
ts_free_thread();
pthread_mutex_lock(&nlock);
threads--;
if(head_task == task) {
head_task = head_task->next;
if(head_task == NULL) {
tail_task = NULL;
} else {
head_task->prev = NULL;
}
} else if(tail_task == task) {
tail_task = tail_task->prev;
tail_task->next = NULL;
} else {
task->prev->next = task->next;
task->next->prev = task->prev;
}
free_task(task);
pthread_cond_signal(&ncond);
pthread_mutex_unlock(&nlock);
pthread_exit(NULL);
}
ZEND_BEGIN_ARG_INFO(arginfo_create_task, 3)
ZEND_ARG_INFO(0, taskname)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_ARRAY_INFO(0, params, 0)
ZEND_ARG_INFO(0, logfile)
ZEND_ARG_INFO(0, logmode)
ZEND_ARG_INFO(1, res)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(create_task) {
zval *params;
char *taskname, *filename, *logfile = NULL, *logmode = "ab";
size_t taskname_len, filename_len, logfile_len = 0, logmode_len = 2;
task_t *task;
HashTable *ht;
zval *val, *res = NULL;
pthread_t thread;
pthread_attr_t attr;
int ret;
ZEND_PARSE_PARAMETERS_START(3, 6)
Z_PARAM_STRING(taskname, taskname_len)
Z_PARAM_STRING(filename, filename_len)
Z_PARAM_ARRAY(params)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(logfile, logfile_len)
Z_PARAM_STRING(logmode, logmode_len)
Z_PARAM_ZVAL_DEREF(res)
ZEND_PARSE_PARAMETERS_END();
if(access(filename, F_OK|R_OK) != 0) {
fprintf(stderr, "Could not open input file: %s\n", filename);
RETURN_FALSE;
}
ht = Z_ARRVAL_P(params);
task = (task_t *) malloc(sizeof(task_t));
memset(task, 0, sizeof(task_t));
task->name = strndup(taskname, taskname_len);
dprintf("CREATE_TASK: %s\n", task->name);
task->argc = zend_hash_num_elements(ht) + 1;
task->argv = (char**) malloc(sizeof(char*) * task->argc);
task->argv[0] = strndup(filename, filename_len);
if(logfile && logfile_len) {
task->logfile = strndup(logfile, logfile_len);
}
if(logmode && logmode_len) {
task->logmode = strndup(logmode, logmode_len);
}
if(res) {
task->wait = (wait_t*) malloc(sizeof(wait_t));
memset(task->wait, 0, sizeof(wait_t));
sem_init(&task->wait->sem, 0, 0);
zval_ptr_dtor(res);
ZVAL_RES(res, zend_register_resource(task->wait, le_threadtask_descriptor));
dprintf("RESOURCE %p register\n", task->wait);
}
ret = 0;
ZEND_HASH_FOREACH_VAL(ht, val) {
convert_to_string(val);
task->argv[++ret] = strndup(Z_STRVAL_P(val), Z_STRLEN_P(val));
} ZEND_HASH_FOREACH_END();
dprintf("TASK0: %s\n", taskname);
idle:
pthread_mutex_lock(&wlock);
if(wthreads) {
if(taskn) {
pthread_mutex_unlock(&wlock);
usleep(500);
goto idle;
}
taskn = task;
pthread_mutex_unlock(&wlock);
sem_post(&rsem);
dprintf("TASK1: %s\n", taskname);
RETURN_TRUE;
} else {
pthread_mutex_unlock(&wlock);
pthread_mutex_lock(&nlock);
if(threads >= maxthreads) {
pthread_mutex_unlock(&nlock);
usleep(500);
goto idle;
}
threads++;
pthread_mutex_unlock(&nlock);
}
dprintf("TASK1: %s\n", taskname);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
ret = pthread_create(&thread, &attr, (void*(*)(void*)) thread_task, task);
if(ret) {
errno = ret;
perror("pthread_create() is error");
errno = 0;
free_task(task);
} else if(task->wait) {
task->wait->tid = thread;
}
pthread_attr_destroy(&attr);
RETURN_BOOL(ret == 0);
}
ZEND_BEGIN_ARG_INFO(arginfo_task_is_run, 1)
ZEND_ARG_TYPE_INFO(0, res, IS_RESOURCE, 0)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_is_run) {
zval *res;
wait_t *ptr;
int v = 0;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_RESOURCE(res)
ZEND_PARSE_PARAMETERS_END();
ptr = (wait_t*) zend_fetch_resource_ex(res, PHP_THREADTASK_DESCRIPTOR, le_threadtask_descriptor);
if(ptr && !sem_getvalue(&ptr->sem, &v) && !v) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
ZEND_BEGIN_ARG_INFO(arginfo_task_join, 1)
ZEND_ARG_TYPE_INFO(0, res, IS_RESOURCE, 0)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_join) {
zval *res;
wait_t *ptr;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_RESOURCE(res)
ZEND_PARSE_PARAMETERS_END();
ptr = (wait_t*) zend_fetch_resource_ex(res, PHP_THREADTASK_DESCRIPTOR, le_threadtask_descriptor);
if(ptr) sem_wait(&ptr->sem);
RETURN_BOOL(ptr);
}
ZEND_BEGIN_ARG_INFO(arginfo_task_kill, 1)
ZEND_ARG_TYPE_INFO(0, res, IS_RESOURCE, 0)
ZEND_ARG_TYPE_INFO(0, sig, IS_LONG, 0)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_kill) {
zval *res;
zend_long sig = SIGINT;
wait_t *ptr;
int ret;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_RESOURCE(res)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(sig)
ZEND_PARSE_PARAMETERS_END();
ptr = (wait_t*) zend_fetch_resource_ex(res, PHP_THREADTASK_DESCRIPTOR, le_threadtask_descriptor);
if(ptr) {
if(!ptr->isExit) {
ptr->isExit = 1;
dprintf("pthread_kill %d\n", pthread_tid_ex(ptr->tid));
ret = pthread_kill(ptr->tid, (int) sig);
if(ret) {
errno = ret;
perror("pthread_create() is error");
errno = 0;
ptr->isExit = 0;
}
}
RETURN_BOOL(ptr->isExit);
} else {
RETURN_FALSE;
}
}
ZEND_BEGIN_ARG_INFO(arginfo_task_wait, 0)
ZEND_ARG_INFO(0, sig)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_wait) {
task_t *task;
pthread_t thread;
zend_long sig;
int i;
if(mthread != pthread_self()) {
php_printf("The task_wait function can only be executed in main thread\n");
return;
}
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(sig)
ZEND_PARSE_PARAMETERS_END();
if(sig == SIGUSR1 || sig == SIGUSR2) {
sig = SIGINT;
isReload = 1;
}
isRun = 0;
dprintf("TASK_WAIT begin\n");
pthread_mutex_lock(&nlock);
for(i=0; i<threads; i++) sem_post(&rsem);
task = head_task;
while(task) {
thread = task->thread;
task = task->next;
dprintf("pthread_kill %d\n", pthread_tid_ex(thread));
pthread_kill(thread, (int) sig);
}
while(threads > 0) {
pthread_cond_wait(&ncond, &nlock);
}
pthread_mutex_unlock(&nlock);
dprintf("TASK_WAIT end\n");
}
ZEND_BEGIN_ARG_INFO(arginfo_task_set_delay, 0)
ZEND_ARG_INFO(0, delay)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_set_delay) {
zend_long d;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(d)
ZEND_PARSE_PARAMETERS_END();
RETVAL_LONG(delay);
if(delay > 0) delay = d;
}
ZEND_BEGIN_ARG_INFO(arginfo_task_set_threads, 0)
ZEND_ARG_INFO(0, threads)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_set_threads) {
zend_long d;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(d)
ZEND_PARSE_PARAMETERS_END();
RETVAL_LONG(maxthreads);
if(d > 1) maxthreads = d;
}
ZEND_BEGIN_ARG_INFO(arginfo_task_set_debug, 0)
ZEND_ARG_INFO(0, isDebug)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_set_debug) {
zend_bool d;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_BOOL(d)
ZEND_PARSE_PARAMETERS_END();
RETVAL_BOOL(isDebug);
isDebug = d;
}
ZEND_BEGIN_ARG_INFO(arginfo_task_set_run, 0)
ZEND_ARG_INFO(0, isRun)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_set_run) {
zend_bool d = 0;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(d)
ZEND_PARSE_PARAMETERS_END();
RETVAL_BOOL(isRun);
isRun = d;
}
ZEND_BEGIN_ARG_INFO(arginfo_task_get_num, 0)
ZEND_ARG_INFO(0, is_max)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_get_num) {
zend_bool is_max = 0;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(is_max)
ZEND_PARSE_PARAMETERS_END();
if(is_max) {
RETVAL_LONG(maxthreads);
} else {
pthread_mutex_lock(&nlock);
RETVAL_LONG(threads);
pthread_mutex_unlock(&nlock);
}
}
ZEND_BEGIN_ARG_INFO(arginfo_task_get_run, 0)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_get_run) {
ZEND_PARSE_PARAMETERS_NONE();
RETVAL_BOOL(isRun);
}
ZEND_BEGIN_ARG_INFO(arginfo_task_get_debug, 0)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_get_debug) {
ZEND_PARSE_PARAMETERS_NONE();
RETVAL_BOOL(isDebug);
}
ZEND_BEGIN_ARG_INFO(arginfo_task_get_delay, 0)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(task_get_delay) {
ZEND_PARSE_PARAMETERS_NONE();
RETVAL_LONG(delay);
}
ZEND_BEGIN_ARG_INFO(arginfo_is_main_task, 0)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(is_main_task) {
ZEND_PARSE_PARAMETERS_NONE();
RETURN_BOOL(mthread == pthread_self());
}
// -----------------------------------------------------------------------------------------------------------
void debug_print_backtrace(const char *prefix, int skip_last, int options, int limit) {
char name[64];
prctl(PR_GET_NAME, (unsigned long) name);
#if PHP_VERSION_ID >= 80100
zval backtrace;
zend_fetch_debug_backtrace(&backtrace, skip_last, options, limit);
ZEND_ASSERT(Z_TYPE(backtrace) == IS_ARRAY);
zend_string *str = zend_trace_to_string(Z_ARRVAL(backtrace), /* include_main */ false);
if(ZSTR_LEN(str)) {
fprintf(stderr, "%s[%s] %*s", prefix, name, (int) ZSTR_LEN(str), ZSTR_VAL(str));
}
zend_string_release(str);
zval_ptr_dtor(&backtrace);
#else
zval func, retval, params[2];
ZVAL_LONG(¶ms[0], options);
ZVAL_LONG(¶ms[1], limit);
ZVAL_STRING(&func, "debug_print_backtrace");
zend_printf("%s[%s] ", prefix, name);
call_user_function(NULL, NULL, &func, &retval, 2, params);
zval_ptr_dtor(&func);
zval_ptr_dtor(&retval);
zval_ptr_dtor(¶ms[0]);
zval_ptr_dtor(¶ms[1]);
#endif
}
#ifdef HAVE_STRUCT_SIGINFO_T
static void segv_signal_handler(int signo, siginfo_t *siginfo, void *context) {
#else
static void segv_signal_handler(int signo) {
#endif
debug_print_backtrace("[SIGSEGV]", 0, 0, 0);
zend_bailout();
}
static void segv_signal(int restart, int mask_all) {
struct sigaction act,oact;
#ifdef HAVE_STRUCT_SIGINFO_T
act.sa_sigaction = segv_signal_handler;
#else
act.sa_handler = segv_signal_handler;
#endif
if (mask_all) {
sigfillset(&act.sa_mask);
} else {
sigemptyset(&act.sa_mask);
}
act.sa_flags = 0;
#ifdef HAVE_STRUCT_SIGINFO_T
act.sa_flags |= SA_SIGINFO;
#endif
if (!restart) {
#ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT; /* SunOS */
#endif
} else {
#ifdef SA_RESTART
act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
#endif
}
zend_sigaction(SIGSEGV, &act, &oact);
}
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pthread_sigmask, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, mode, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, signals, IS_ARRAY, 0)
ZEND_ARG_INFO(1, old_signals)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(pthread_sigmask) {
zend_long how, signo;
zval *user_set, *user_oldset = NULL, *user_signo;
sigset_t set, oldset;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "la|z/", &how, &user_set, &user_oldset) == FAILURE) {
RETURN_FALSE;
}
if (sigemptyset(&set) != 0 || sigemptyset(&oldset) != 0) {
php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
RETURN_FALSE;
}
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(user_set), user_signo) {
signo = zval_get_long(user_signo);
if (sigaddset(&set, signo) != 0) {
php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
RETURN_FALSE;
}
} ZEND_HASH_FOREACH_END();
if (pthread_sigmask(how, &set, &oldset) != 0) {
php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
RETURN_FALSE;
}
if (user_oldset != NULL) {
if (Z_TYPE_P(user_oldset) != IS_ARRAY) {
zval_ptr_dtor(user_oldset);
array_init(user_oldset);
} else {
zend_hash_clean(Z_ARRVAL_P(user_oldset));
}
for (signo = 1; signo < NSIG; ++signo) {
if (sigismember(&oldset, signo) != 1) {
continue;
}
add_next_index_long(user_oldset, signo);
}
}
segv_signal(1, 1);
RETURN_TRUE;
}
ZEND_BEGIN_ARG_INFO(arginfo_pthread_yield, 0)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(pthread_yield) {
ZEND_PARSE_PARAMETERS_NONE();
RETURN_BOOL(sched_yield() == 0);
}
// ===========================================================================================================
static zend_class_entry *spl_ce_GoExitException;
#if PHP_VERSION_ID < 80400
static int go_exit_handler(zend_execute_data *execute_data) {
if(SINFO(is_throw_exit)) {
const zend_op *opline = EX(opline);
zval ex;
zend_object *obj;
zval _exit_status;
zval *exit_status = NULL;
if (opline->op1_type != IS_UNUSED) {
if (opline->op1_type == IS_CONST) {
// see: https://github.com/php/php-src/commit/e70618aff6f447a298605d07648f2ce9e5a284f5
#ifdef EX_CONSTANT
exit_status = EX_CONSTANT(opline->op1);
#else
exit_status = RT_CONSTANT(opline, opline->op1);
#endif
} else {
exit_status = EX_VAR(opline->op1.var);
}
if (Z_ISREF_P(exit_status)) {
exit_status = Z_REFVAL_P(exit_status);
}
ZVAL_DUP(&_exit_status, exit_status);