1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Stig Bakken <ssb@php.net>                                    |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "php.h"
20 #include "php_ticks.h"
21 
22 struct st_tick_function
23 {
24 	void (*func)(int, void *);
25 	void *arg;
26 };
27 
php_startup_ticks(void)28 int php_startup_ticks(void)
29 {
30 	zend_llist_init(&PG(tick_functions), sizeof(struct st_tick_function), NULL, 1);
31 	return SUCCESS;
32 }
33 
php_deactivate_ticks(void)34 void php_deactivate_ticks(void)
35 {
36 	zend_llist_clean(&PG(tick_functions));
37 }
38 
php_shutdown_ticks(void)39 void php_shutdown_ticks(void)
40 {
41 	zend_llist_destroy(&PG(tick_functions));
42 }
43 
php_compare_tick_functions(void *elem1, void *elem2)44 static int php_compare_tick_functions(void *elem1, void *elem2)
45 {
46   struct st_tick_function *e1 = (struct st_tick_function *)elem1;
47   struct st_tick_function *e2 = (struct st_tick_function *)elem2;
48   return e1->func == e2->func && e1->arg == e2->arg;
49 }
50 
php_add_tick_function(void (*func)(int, void*), void * arg)51 PHPAPI void php_add_tick_function(void (*func)(int, void*), void * arg)
52 {
53 	struct st_tick_function tmp = {func, arg};
54 	zend_llist_add_element(&PG(tick_functions), (void *)&tmp);
55 }
56 
php_remove_tick_function(void (*func)(int, void *), void * arg)57 PHPAPI void php_remove_tick_function(void (*func)(int, void *), void * arg)
58 {
59 	struct st_tick_function tmp = {func, arg};
60 	zend_llist_del_element(&PG(tick_functions), (void *)&tmp, (int(*)(void*, void*))php_compare_tick_functions);
61 }
62 
php_tick_iterator(void *d, void *arg)63 static void php_tick_iterator(void *d, void *arg)
64 {
65 	struct st_tick_function *data = (struct st_tick_function *)d;
66 	data->func(*((int *)arg), data->arg);
67 }
68 
php_run_ticks(int count)69 void php_run_ticks(int count)
70 {
71 	zend_llist_apply_with_argument(&PG(tick_functions), (llist_apply_with_arg_func_t) php_tick_iterator, &count);
72 }
73