php_ticks.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include "php.h"
  17. #include "php_ticks.h"
  18. struct st_tick_function
  19. {
  20. void (*func)(int, void *);
  21. void *arg;
  22. };
  23. int php_startup_ticks(void)
  24. {
  25. zend_llist_init(&PG(tick_functions), sizeof(struct st_tick_function), NULL, 1);
  26. return SUCCESS;
  27. }
  28. void php_deactivate_ticks(void)
  29. {
  30. zend_llist_clean(&PG(tick_functions));
  31. }
  32. void php_shutdown_ticks(void)
  33. {
  34. zend_llist_destroy(&PG(tick_functions));
  35. }
  36. static int php_compare_tick_functions(void *elem1, void *elem2)
  37. {
  38. struct st_tick_function *e1 = (struct st_tick_function *)elem1;
  39. struct st_tick_function *e2 = (struct st_tick_function *)elem2;
  40. return e1->func == e2->func && e1->arg == e2->arg;
  41. }
  42. PHPAPI void php_add_tick_function(void (*func)(int, void*), void * arg)
  43. {
  44. struct st_tick_function tmp = {func, arg};
  45. zend_llist_add_element(&PG(tick_functions), (void *)&tmp);
  46. }
  47. PHPAPI void php_remove_tick_function(void (*func)(int, void *), void * arg)
  48. {
  49. struct st_tick_function tmp = {func, arg};
  50. zend_llist_del_element(&PG(tick_functions), (void *)&tmp, (int(*)(void*, void*))php_compare_tick_functions);
  51. }
  52. static void php_tick_iterator(void *d, void *arg)
  53. {
  54. struct st_tick_function *data = (struct st_tick_function *)d;
  55. data->func(*((int *)arg), data->arg);
  56. }
  57. void php_run_ticks(int count)
  58. {
  59. zend_llist_apply_with_argument(&PG(tick_functions), (llist_apply_with_arg_func_t) php_tick_iterator, &count);
  60. }