timer_proc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include "timer_proc.h"
  28. #include "pt.h"
  29. #include "mem/shm_mem.h"
  30. #include <unistd.h>
  31. /** forks a separate simple sleep() periodic timer.
  32. * Forks a very basic periodic timer process, that just sleep()s for
  33. * the specified interval and then calls the timer function.
  34. * The new "dummy timer" process execution start immediately, the sleep()
  35. * is called first (so the first call to the timer function will happen
  36. * <interval> seconds after the call to fork_dummy_timer)
  37. * @param child_id - @see fork_process()
  38. * @param desc - @see fork_process()
  39. * @param make_sock - @see fork_process()
  40. * @param f - timer function/callback
  41. * @param param - parameter passed to the timer function
  42. * @param interval - interval in seconds.
  43. * @return - pid of the new process on success, -1 on error
  44. * (doesn't return anything in the child process)
  45. */
  46. int fork_dummy_timer(int child_id, char* desc, int make_sock,
  47. timer_function* f, void* param, int interval)
  48. {
  49. int pid;
  50. pid=fork_process(child_id, desc, make_sock);
  51. if (pid<0) return -1;
  52. if (pid==0){
  53. /* child */
  54. for(;;){
  55. sleep(interval);
  56. f(get_ticks(), param); /* ticks in s for compatibility with old
  57. timers */
  58. }
  59. }
  60. /* parent */
  61. return pid;
  62. }
  63. /** forks a timer process based on the local timer.
  64. * Forks a separate timer process running a local_timer.h type of timer
  65. * A pointer to the local_timer handle (allocated in shared memory) is
  66. * returned in lt_h. It can be used to add/delete more timers at runtime
  67. * (via local_timer_add()/local_timer_del() a.s.o).
  68. * If timers are added from separate processes, some form of locking must be
  69. * used (all the calls to local_timer* must be enclosed by locks if it
  70. * cannot be guaranteed that they cannot execute in the same time)
  71. * The timer "engine" must be run manually from the child process. For
  72. * example a very simple local timer process that just runs a single
  73. * periodic timer can be started in the following way:
  74. * struct local_timer* lt_h;
  75. *
  76. * pid=fork_local_timer_process(...., &lt_h);
  77. * if (pid==0){
  78. * timer_init(&my_timer, my_timer_f, 0, 0);
  79. * local_timer_add(&lt_h, &my_timer, S_TO_TICKS(10), get_ticks_raw());
  80. * while(1) { sleep(1); local_timer_run(lt, get_ticks_raw()); }
  81. * }
  82. *
  83. * @param child_id - @see fork_process()
  84. * @param desc - @see fork_process()
  85. * @param make_sock - @see fork_process()
  86. * @param lt_h - local_timer handler
  87. * @return - pid to the parent, 0 to the child, -1 if error.
  88. */
  89. int fork_local_timer_process(int child_id, char* desc, int make_sock,
  90. struct local_timer** lt_h)
  91. {
  92. int pid;
  93. struct local_timer* lt;
  94. lt=shm_malloc(sizeof(*lt));
  95. if (lt==0) goto error;
  96. if (init_local_timer(lt, get_ticks_raw())<0) goto error;
  97. pid=fork_process(child_id, desc, make_sock);
  98. if (pid<0) goto error;
  99. *lt_h=lt;
  100. return pid;
  101. error:
  102. if (lt) shm_free(lt);
  103. return -1;
  104. }
  105. /* vi: set ts=4 sw=4 tw=79:ai:cindent: */