php_ticks.c 2.4 KB

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