timer_proc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2009 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * timer_proc.c - separate process timers
  20. * (unrelated to the main fast and slow timers)
  21. */
  22. /*
  23. * History:
  24. * --------
  25. * 2009-03-10 initial version (andrei)
  26. */
  27. /**
  28. * @file
  29. * @brief SIP-router core :: timer - separate process timers
  30. *
  31. * (unrelated to the main fast and slow timers)
  32. *
  33. * @ingroup core
  34. * Module: @ref core
  35. */
  36. #include "timer_proc.h"
  37. #include "pt.h"
  38. #include "mem/shm_mem.h"
  39. #include <unistd.h>
  40. /** forks a separate simple sleep() periodic timer.
  41. * Forks a very basic periodic timer process, that just sleep()s for
  42. * the specified interval and then calls the timer function.
  43. * The new "dummy timer" process execution start immediately, the sleep()
  44. * is called first (so the first call to the timer function will happen
  45. * <interval> seconds after the call to fork_dummy_timer)
  46. * @param child_id - @see fork_process()
  47. * @param desc - @see fork_process()
  48. * @param make_sock - @see fork_process()
  49. * @param f - timer function/callback
  50. * @param param - parameter passed to the timer function
  51. * @param interval - interval in seconds.
  52. * @return - pid of the new process on success, -1 on error
  53. * (doesn't return anything in the child process)
  54. */
  55. int fork_dummy_timer(int child_id, char* desc, int make_sock,
  56. timer_function* f, void* param, int interval)
  57. {
  58. int pid;
  59. pid=fork_process(child_id, desc, make_sock);
  60. if (pid<0) return -1;
  61. if (pid==0){
  62. /* child */
  63. for(;;){
  64. sleep(interval);
  65. f(get_ticks(), param); /* ticks in s for compatibility with old
  66. timers */
  67. }
  68. }
  69. /* parent */
  70. return pid;
  71. }
  72. /** forks a timer process based on the local timer.
  73. * Forks a separate timer process running a local_timer.h type of timer
  74. * A pointer to the local_timer handle (allocated in shared memory) is
  75. * returned in lt_h. It can be used to add/delete more timers at runtime
  76. * (via local_timer_add()/local_timer_del() a.s.o).
  77. * If timers are added from separate processes, some form of locking must be
  78. * used (all the calls to local_timer* must be enclosed by locks if it
  79. * cannot be guaranteed that they cannot execute in the same time)
  80. * The timer "engine" must be run manually from the child process. For
  81. * example a very simple local timer process that just runs a single
  82. * periodic timer can be started in the following way:
  83. * struct local_timer* lt_h;
  84. *
  85. * pid=fork_local_timer_process(...., &lt_h);
  86. * if (pid==0){
  87. * timer_init(&my_timer, my_timer_f, 0, 0);
  88. * local_timer_add(&lt_h, &my_timer, S_TO_TICKS(10), get_ticks_raw());
  89. * while(1) { sleep(1); local_timer_run(lt, get_ticks_raw()); }
  90. * }
  91. *
  92. * @param child_id - @see fork_process()
  93. * @param desc - @see fork_process()
  94. * @param make_sock - @see fork_process()
  95. * @param lt_h - local_timer handler
  96. * @return - pid to the parent, 0 to the child, -1 if error.
  97. */
  98. int fork_local_timer_process(int child_id, char* desc, int make_sock,
  99. struct local_timer** lt_h)
  100. {
  101. int pid;
  102. struct local_timer* lt;
  103. lt=shm_malloc(sizeof(*lt));
  104. if (lt==0) goto error;
  105. if (init_local_timer(lt, get_ticks_raw())<0) goto error;
  106. pid=fork_process(child_id, desc, make_sock);
  107. if (pid<0) goto error;
  108. *lt_h=lt;
  109. return pid;
  110. error:
  111. if (lt) shm_free(lt);
  112. return -1;
  113. }
  114. /* vi: set ts=4 sw=4 tw=79:ai:cindent: */