xref: /PHP-7.2/main/php_ticks.c (revision 7a7ec01a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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 /* $Id$ */
20 
21 #include "php.h"
22 #include "php_ticks.h"
23 
24 struct st_tick_function
25 {
26 	void (*func)(int, void *);
27 	void *arg;
28 };
29 
php_startup_ticks(void)30 int php_startup_ticks(void)
31 {
32 	zend_llist_init(&PG(tick_functions), sizeof(struct st_tick_function), NULL, 1);
33 	return SUCCESS;
34 }
35 
php_deactivate_ticks(void)36 void php_deactivate_ticks(void)
37 {
38 	zend_llist_clean(&PG(tick_functions));
39 }
40 
php_shutdown_ticks(void)41 void php_shutdown_ticks(void)
42 {
43 	zend_llist_destroy(&PG(tick_functions));
44 }
45 
php_compare_tick_functions(void * elem1,void * elem2)46 static int php_compare_tick_functions(void *elem1, void *elem2)
47 {
48   struct st_tick_function *e1 = (struct st_tick_function *)elem1;
49   struct st_tick_function *e2 = (struct st_tick_function *)elem2;
50   return e1->func == e2->func && e1->arg == e2->arg;
51 }
52 
php_add_tick_function(void (* func)(int,void *),void * arg)53 PHPAPI void php_add_tick_function(void (*func)(int, void*), void * arg)
54 {
55 	struct st_tick_function tmp = {func, arg};
56 	zend_llist_add_element(&PG(tick_functions), (void *)&tmp);
57 }
58 
php_remove_tick_function(void (* func)(int,void *),void * arg)59 PHPAPI void php_remove_tick_function(void (*func)(int, void *), void * arg)
60 {
61 	struct st_tick_function tmp = {func, arg};
62 	zend_llist_del_element(&PG(tick_functions), (void *)&tmp, (int(*)(void*, void*))php_compare_tick_functions);
63 }
64 
php_tick_iterator(void * d,void * arg)65 static void php_tick_iterator(void *d, void *arg)
66 {
67 	struct st_tick_function *data = (struct st_tick_function *)d;
68 	data->func(*((int *)arg), data->arg);
69 }
70 
php_run_ticks(int count)71 void php_run_ticks(int count)
72 {
73 	zend_llist_apply_with_argument(&PG(tick_functions), (llist_apply_with_arg_func_t) php_tick_iterator, &count);
74 }
75 
76 /*
77  * Local variables:
78  * tab-width: 4
79  * c-basic-offset: 4
80  * End:
81  * vim600: sw=4 ts=4 fdm=marker
82  * vim<600: sw=4 ts=4
83  */
84