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