php_ticks.c 2.6 KB

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