async_sleep.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2011 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * This file is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. *
  14. * This file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "../../dprint.h"
  29. #include "../../ut.h"
  30. #include "../../locking.h"
  31. #include "../../timer.h"
  32. #include "../../modules/tm/tm_load.h"
  33. #include "async_sleep.h"
  34. /* tm */
  35. extern struct tm_binds tmb;
  36. typedef struct async_item {
  37. unsigned int tindex;
  38. unsigned int tlabel;
  39. unsigned int ticks;
  40. cfg_action_t *act;
  41. struct async_item *next;
  42. } async_item_t;
  43. typedef struct async_slot {
  44. async_item_t *lstart;
  45. async_item_t *lend;
  46. gen_lock_t lock;
  47. } async_slot_t;
  48. #define ASYNC_RING_SIZE 100
  49. static struct async_list_head {
  50. async_slot_t ring[ASYNC_RING_SIZE];
  51. async_slot_t *later;
  52. } *_async_list_head = NULL;
  53. int async_init_timer_list(void)
  54. {
  55. int i;
  56. _async_list_head = (struct async_list_head*)
  57. shm_malloc(sizeof(struct async_list_head));
  58. if(_async_list_head==NULL)
  59. {
  60. LM_ERR("no more shm\n");
  61. return -1;
  62. }
  63. memset(_async_list_head, 0, sizeof(struct async_list_head));
  64. for(i=0; i<ASYNC_RING_SIZE; i++)
  65. {
  66. if(lock_init(&_async_list_head->ring[i].lock)==0)
  67. {
  68. LM_ERR("cannot init lock at %d\n", i);
  69. i--;
  70. while(i>=0)
  71. {
  72. lock_destroy(&_async_list_head->ring[i].lock);
  73. i--;
  74. }
  75. shm_free(_async_list_head);
  76. _async_list_head = 0;
  77. return -1;
  78. }
  79. }
  80. return 0;
  81. }
  82. int async_destroy_timer_list(void)
  83. {
  84. int i;
  85. if(_async_list_head==NULL)
  86. return 0;
  87. for(i=0; i<ASYNC_RING_SIZE; i++)
  88. {
  89. /* TODO: clean the list */
  90. lock_destroy(&_async_list_head->ring[i].lock);
  91. }
  92. shm_free(_async_list_head);
  93. _async_list_head = 0;
  94. return 0;
  95. }
  96. int async_sleep(struct sip_msg* msg, int seconds, cfg_action_t *act)
  97. {
  98. int slot;
  99. unsigned int ticks;
  100. async_item_t *ai;
  101. tm_cell_t *t = 0;
  102. if(seconds<=0) {
  103. LM_ERR("negative or zero sleep time (%d)\n", seconds);
  104. return -1;
  105. }
  106. if(seconds>=ASYNC_RING_SIZE)
  107. {
  108. LM_ERR("max sleep time is %d sec (%d)\n", ASYNC_RING_SIZE, seconds);
  109. return -1;
  110. }
  111. t = tmb.t_gett();
  112. if (t==NULL || t==T_UNDEFINED)
  113. {
  114. if(tmb.t_newtran(msg)<0)
  115. {
  116. LM_ERR("cannot create the transaction\n");
  117. return -1;
  118. }
  119. t = tmb.t_gett();
  120. if (t==NULL || t==T_UNDEFINED)
  121. {
  122. LM_ERR("cannot lookup the transaction\n");
  123. return -1;
  124. }
  125. }
  126. ticks = seconds + get_ticks();
  127. slot = ticks % ASYNC_RING_SIZE;
  128. ai = (async_item_t*)shm_malloc(sizeof(async_item_t));
  129. if(ai==NULL)
  130. {
  131. LM_ERR("no more shm\n");
  132. return -1;
  133. }
  134. memset(ai, 0, sizeof(async_item_t));
  135. ai->ticks = ticks;
  136. ai->act = act;
  137. if(tmb.t_suspend(msg, &ai->tindex, &ai->tlabel)<0)
  138. {
  139. LM_ERR("failed to suppend the processing\n");
  140. shm_free(ai);
  141. return -1;
  142. }
  143. lock_get(&_async_list_head->ring[slot].lock);
  144. ai->next = _async_list_head->ring[slot].lstart;
  145. _async_list_head->ring[slot].lstart = ai;
  146. lock_release(&_async_list_head->ring[slot].lock);
  147. return 0;
  148. }
  149. void async_timer_exec(unsigned int ticks, void *param)
  150. {
  151. int slot;
  152. async_item_t *ai;
  153. if(_async_list_head==NULL)
  154. return;
  155. slot = ticks % ASYNC_RING_SIZE;
  156. while(1) {
  157. lock_get(&_async_list_head->ring[slot].lock);
  158. ai = _async_list_head->ring[slot].lstart;
  159. if(ai!=NULL)
  160. _async_list_head->ring[slot].lstart = ai->next;
  161. lock_release(&_async_list_head->ring[slot].lock);
  162. if(ai==NULL)
  163. break;
  164. if(ai->act!=NULL)
  165. tmb.t_continue(ai->tindex, ai->tlabel, ai->act);
  166. shm_free(ai);
  167. }
  168. }