File: excimer_timer.c

package info (click to toggle)
php-excimer 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: ansic: 2,513; xml: 233; php: 82; makefile: 2
file content (291 lines) | stat: -rw-r--r-- 8,370 bytes parent folder | download
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
/* Copyright 2018 Wikimedia Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <signal.h>
#include <stdint.h>
#include <time.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#include "php.h"
#include "excimer_mutex.h"
#include "excimer_timer.h"
#include "zend_types.h"

#if PHP_VERSION_ID >= 80200
#define excimer_timer_atomic_bool_store(dest, value) zend_atomic_bool_store(dest, value)
#else
#define excimer_timer_atomic_bool_store(dest, value) *dest = value
#endif

excimer_timer_globals_t excimer_timer_globals;
ZEND_TLS excimer_timer_tls_t excimer_timer_tls;

static void excimer_timer_handle(void * data, int overrun_count);
static void excimer_timer_interrupt(zend_execute_data *execute_data);

/**
 * Add a timer to the pending list. Unsynchronised, i.e. the caller is
 * responsible for locking the mutex if required.
 */
static void excimer_timer_list_enqueue(excimer_timer *timer)
{
	excimer_timer **head_pp = &excimer_timer_tls.pending_head;
	if (!timer->pending_next) {
		if (*head_pp) {
			timer->pending_next = *head_pp;
			timer->pending_prev = (*head_pp)->pending_prev;
			(*head_pp)->pending_prev->pending_next = timer;
			(*head_pp)->pending_prev = timer;
		} else {
			*head_pp = timer;
			timer->pending_next = timer;
			timer->pending_prev = timer;
		}
	}
}

/**
 * Remove the first (FIFO) timer from the pending list and provide a pointer
 * to it. (unsynchronised)
 *
 * @param[out] timer_pp
 * @return True if a timer was returned, false if the list was empty
 */
static int excimer_timer_list_dequeue(excimer_timer **timer_pp)
{
	excimer_timer **head_pp = &excimer_timer_tls.pending_head;
	if (*head_pp) {
		// Get the pending timer
		excimer_timer *timer = *timer_pp = *head_pp;
		if (timer->pending_next == timer) {
			// List is now empty
			*head_pp = NULL;
		} else {
			// Relink the head and neighbours
			timer->pending_next->pending_prev = timer->pending_prev;
			*head_pp = timer->pending_prev->pending_next = timer->pending_next;
		}
		// Unlink the timer being returned
		timer->pending_next = NULL;
		timer->pending_prev = NULL;
		return 1;
	} else {
		return 0;
	}
}

/**
 * Remove the specified timer from the pending list, if it is in there. If it
 * is not in the list, do nothing. (unsynchronised)
 */
static void excimer_timer_list_remove(excimer_timer *timer)
{
	excimer_timer **head_pp = &excimer_timer_tls.pending_head;
	if (timer->pending_next) {
		if (timer->pending_next == timer) {
			*head_pp = NULL;
		} else {
			timer->pending_next->pending_prev = timer->pending_prev;
			timer->pending_prev->pending_next = timer->pending_next;
			if (*head_pp == timer) {
				*head_pp = timer->pending_next;
			}
		}
		timer->pending_next = NULL;
		timer->pending_prev = NULL;
	}
}

/**
 * Atomically dequeue a timer and get its event count at the time of removal
 * from the queue. The timer may be immediately re-added to the queue by the
 * event handler.
 *
 * @param[out] timer_pp Where to put the pointer to the timer
 * @param[out] event_count_p Where to put the event count
 * @return True if a timer was removed, false if the list was empty.
 */
static int excimer_timer_pending_dequeue(excimer_timer **timer_pp, zend_long *event_count_p)
{
	excimer_mutex_lock(&excimer_timer_tls.mutex);
	int ret = excimer_timer_list_dequeue(timer_pp);
	if (ret) {
		*event_count_p = (*timer_pp)->event_count;
		(*timer_pp)->event_count = 0;
	}
	excimer_mutex_unlock(&excimer_timer_tls.mutex);
	return ret;
}

// Note: functions with external linkage are documented in the header

void excimer_timer_module_init()
{
	excimer_timer_globals.old_zend_interrupt_function = zend_interrupt_function;
	zend_interrupt_function = excimer_timer_interrupt;
}

void excimer_timer_module_shutdown()
{
}

void excimer_timer_thread_init()
{
	excimer_timer_tls = (excimer_timer_tls_t){
		.mutex = PTHREAD_MUTEX_INITIALIZER
	};
}

void excimer_timer_thread_shutdown()
{
	if (excimer_timer_tls.timers_active) {
		// If this ever happens, it means we've got the logic wrong and we need
		// to rethink. It's very bad for timers to keep existing after thread
		// termination, because the mutex will be a dangling pointer. It's not
		// much help to avoid excimer_mutex_destroy() here because the whole TLS
		// segment will be destroyed and reused.
		php_error_docref(NULL, E_WARNING, "Timer still active at thread termination");
	} else {
		excimer_mutex_destroy(&excimer_timer_tls.mutex);
	}
}

int excimer_timer_init(excimer_timer *timer, int event_type,
	excimer_timer_callback callback, void *user_data)
{
	zval z_timer;

	memset(timer, 0, sizeof(excimer_timer));
	ZVAL_PTR(&z_timer, timer);
	timer->vm_interrupt_ptr = &EG(vm_interrupt);
	timer->callback = callback;
	timer->user_data = user_data;
	timer->tls = &excimer_timer_tls;

	if (timerlib_timer_init(&timer->tl_timer, event_type, &excimer_timer_handle, timer) == FAILURE) {
		timerlib_timer_destroy(&timer->tl_timer);
		return FAILURE;
	}

	excimer_timer_tls.timers_active++;
	timer->is_valid = 1;
	timer->is_running = 0;
	return SUCCESS;
}

void excimer_timer_start(excimer_timer *timer,
	struct timespec *period, struct timespec *initial)
{
	if (!timer->is_valid) {
		php_error_docref(NULL, E_WARNING, "Unable to start uninitialised timer" );
		return;
	}

	/* If a periodic timer has an initial value of 0, use the period instead,
	 * since it_value=0 means disarmed */
	if (timerlib_timespec_is_zero(initial)) {
		initial = period;
	}
	/* If the value is still zero, flag an error */
	if (timerlib_timespec_is_zero(initial)) {
		php_error_docref(NULL, E_WARNING, "Unable to start timer with a value of zero "
			"duration and period");
		return;
	}

	if (timerlib_timer_start(&timer->tl_timer, period, initial) == SUCCESS) {
		timer->is_running = 1;
	}
}

void excimer_timer_stop(excimer_timer *timer)
{
	if (!timer->is_valid) {
		php_error_docref(NULL, E_WARNING, "Unable to start uninitialised timer" );
		return;
	}
	if (timer->is_running) {
		if (timerlib_timer_stop(&timer->tl_timer) == SUCCESS) {
			timer->is_running = 0;
		}
	}
}

void excimer_timer_destroy(excimer_timer *timer)
{
	if (!timer->is_valid) {
		/* This could happen if the timer is manually destroyed after
		 * excimer_timer_thread_shutdown() is called */
		return;
	}
	if (timer->tls != &excimer_timer_tls) {
		php_error_docref(NULL, E_WARNING,
			"Cannot delete a timer belonging to a different thread");
		return;
	}

	/* Stop the timer */
	if (timer->is_running) {
		timer->is_running = 0;
		timerlib_timer_stop(&timer->tl_timer);
	}
	/* Destroy the timer. This will wait until any events are done. */
	timerlib_timer_destroy(&timer->tl_timer);
	excimer_timer_tls.timers_active--;

	/* Remove the timer from the pending list */
	excimer_mutex_lock(&excimer_timer_tls.mutex);
	excimer_timer_list_remove(timer);
	excimer_mutex_unlock(&excimer_timer_tls.mutex);

	timer->is_valid = 0;
	timer->tls = NULL;
}

static void excimer_timer_handle(void * data, int overrun_count)
{
	excimer_timer *timer = (excimer_timer*)data;
	excimer_mutex_lock(&excimer_timer_tls.mutex);
	timer->event_count += overrun_count + 1;
	excimer_timer_list_enqueue(timer);
	excimer_mutex_unlock(&excimer_timer_tls.mutex);
	excimer_timer_atomic_bool_store(timer->vm_interrupt_ptr, 1);
}

static void excimer_timer_interrupt(zend_execute_data *execute_data)
{
	excimer_timer *timer = NULL;
	zend_long count = 0;
	while (excimer_timer_pending_dequeue(&timer, &count)) {
		timer->callback(count, timer->user_data);
	}

	if (excimer_timer_globals.old_zend_interrupt_function) {
		excimer_timer_globals.old_zend_interrupt_function(execute_data);
	}
}

void excimer_timer_get_time(excimer_timer *timer, struct timespec *remaining)
{
	if (!timer->is_valid || !timer->is_running) {
		remaining->tv_sec = 0;
		remaining->tv_nsec = 0;
		return;
	}

	timerlib_timer_get_time(&timer->tl_timer, remaining);
}