37
37
38
38
ZEND_API void zend_timer_create (void ) /* {{{ */
39
39
{
40
- # ifdef TIMER_DEBUG
41
- fprintf (stderr , "Trying to create timer on thread %d\n" , (pid_t ) syscall (SYS_gettid ));
42
- # endif
43
-
44
40
struct sigevent sev ;
45
41
sev .sigev_notify = SIGEV_THREAD_ID ;
46
42
sev .sigev_value .sival_ptr = & EG (timer );
@@ -53,8 +49,9 @@ ZEND_API void zend_timer_create(void) /* {{{ */
53
49
sev .sigev_signo = SIGIO ;
54
50
sev .sigev_notify_thread_id = (pid_t ) syscall (SYS_gettid );
55
51
56
- if (timer_create (CLOCK_THREAD_CPUTIME_ID , & sev , & EG (timer )) != 0 )
52
+ if (timer_create (CLOCK_THREAD_CPUTIME_ID , & sev , & EG (timer )) != 0 ) {
57
53
zend_strerror_noreturn (E_ERROR , errno , "Could not create timer" );
54
+ }
58
55
59
56
# ifdef TIMER_DEBUG
60
57
fprintf (stderr , "Timer %#jx created on thread %d\n" , (uintmax_t ) EG (timer ), sev .sigev_notify_thread_id );
@@ -68,34 +65,45 @@ ZEND_API void zend_timer_settime(zend_long seconds) /* {{{ }*/
68
65
{
69
66
timer_t timer = EG (timer );
70
67
71
- # ifdef TIMER_DEBUG
72
- fprintf (stderr , "Trying to set timer %#jx on thread %d (%ld seconds)\n" , (uintmax_t ) timer , (pid_t ) syscall (SYS_gettid ), seconds );
73
- # endif
74
-
75
- if (timer == 0 ) zend_error_noreturn (E_ERROR , "Timer not created" );
68
+ if (timer == 0 ) {
69
+ zend_error_noreturn (E_ERROR , "Timer not created" );
70
+ }
76
71
77
72
struct itimerspec its ;
78
73
its .it_value .tv_sec = seconds ;
79
74
its .it_value .tv_nsec = its .it_interval .tv_sec = its .it_interval .tv_nsec = 0 ;
80
75
81
- if (timer_settime (timer , 0 , & its , NULL ) != 0 )
76
+ # ifdef TIMER_DEBUG
77
+ fprintf (stderr , "Setting timer %#jx on thread %d (%ld seconds)...\n" , (uintmax_t ) timer , (pid_t ) syscall (SYS_gettid ), seconds );
78
+ # endif
79
+
80
+ if (timer_settime (timer , 0 , & its , NULL ) != 0 ) {
82
81
zend_strerror_noreturn (E_ERROR , errno , "Could not set timer" );
82
+ }
83
83
}
84
84
/* }}} */
85
85
86
86
ZEND_API void zend_timer_delete (void ) /* {{{ */
87
87
{
88
- # ifdef TIMER_DEBUG
89
- fprintf (stderr , "Trying to delete timer %#jx thread %d\n" , (uintmax_t ) EG (timer ), (pid_t ) syscall (SYS_gettid ));
90
- # endif
91
-
92
88
timer_t timer = EG (timer );
93
- if (timer == 0 ) zend_error_noreturn (E_ERROR , "Timer not created" );
89
+ if (timer == 0 ) {
90
+ /* Don't trigger an error here because the timer may not be initialized when PHP fail early, and on threads created by PHP but not managed by it. */
91
+ # ifdef TIMER_DEBUG
92
+ fprintf (stderr , "Could not delete timer that has not been created on thread %d\n" , (uintmax_t ) timer , (pid_t ) syscall (SYS_gettid ));
93
+ # endif
94
+
95
+ return ;
96
+ }
97
+
98
+ # ifdef TIMER_DEBUG
99
+ fprintf (stderr , "Deleting timer %#jx on thread %d...\n" , (uintmax_t ) timer , (pid_t ) syscall (SYS_gettid ));
100
+ # endif
94
101
95
102
int err = timer_delete (timer );
96
103
EG (timer ) = 0 ;
97
- if (err != 0 )
104
+ if (err != 0 ) {
98
105
zend_strerror_noreturn (E_ERROR , errno , "Could not delete timer" );
106
+ }
99
107
}
100
108
/* }}}} */
101
109
0 commit comments