async_task.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Copyright (C) 2014 Daniel-Constantin Mierla (asipto.com)
  3. *
  4. * This file is part of Extensible SIP Router, a free SIP server.
  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. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/socket.h>
  23. #include <sys/types.h>
  24. #include <sys/un.h>
  25. #include <netinet/in.h>
  26. #include <arpa/inet.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include "dprint.h"
  30. #include "sr_module.h"
  31. #include "ut.h"
  32. #include "pt.h"
  33. #include "cfg/cfg_struct.h"
  34. #include "async_task.h"
  35. static int _async_task_workers = 0;
  36. static int _async_task_sockets[2];
  37. int async_task_run(int idx);
  38. /**
  39. *
  40. */
  41. int async_task_init_sockets(void)
  42. {
  43. if (socketpair(PF_UNIX, SOCK_DGRAM, 0, _async_task_sockets) < 0) {
  44. LM_ERR("opening tasks dgram socket pair\n");
  45. return -1;
  46. }
  47. LM_DBG("inter-process event notification sockets initialized\n");
  48. return 0;
  49. }
  50. /**
  51. *
  52. */
  53. void async_task_close_sockets_child(void)
  54. {
  55. LM_DBG("closing the notification socket used by children\n");
  56. close(_async_task_sockets[1]);
  57. }
  58. /**
  59. *
  60. */
  61. void async_task_close_sockets_parent(void)
  62. {
  63. LM_DBG("closing the notification socket used by parent\n");
  64. close(_async_task_sockets[0]);
  65. }
  66. /**
  67. *
  68. */
  69. int async_task_init(void)
  70. {
  71. LM_DBG("start initializing asynk task framework\n");
  72. if(_async_task_workers<=0)
  73. return 0;
  74. /* advertise new processes to core */
  75. register_procs(_async_task_workers);
  76. /* advertise new processes to cfg framework */
  77. cfg_register_child(_async_task_workers);
  78. return 0;
  79. }
  80. /**
  81. *
  82. */
  83. int async_task_initialized(void)
  84. {
  85. if(_async_task_workers<=0)
  86. return 0;
  87. return 1;
  88. }
  89. /**
  90. *
  91. */
  92. int async_task_child_init(int rank)
  93. {
  94. int pid;
  95. int i;
  96. if(_async_task_workers<=0)
  97. return 0;
  98. LM_DBG("child initializing asynk task framework\n");
  99. if (rank==PROC_INIT) {
  100. if(async_task_init_sockets()<0) {
  101. LM_ERR("failed to initialize tasks sockets\n");
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. if(rank>0) {
  107. async_task_close_sockets_parent();
  108. return 0;
  109. }
  110. if (rank!=PROC_MAIN)
  111. return 0;
  112. for(i=0; i<_async_task_workers; i++) {
  113. pid=fork_process(PROC_RPC, "Async Task Worker", 1);
  114. if (pid<0)
  115. return -1; /* error */
  116. if(pid==0) {
  117. /* child */
  118. /* initialize the config framework */
  119. if (cfg_child_init())
  120. return -1;
  121. /* main function for workers */
  122. if(async_task_run(i+1)<0) {
  123. LM_ERR("failed to initialize task worker process: %d\n", i);
  124. return -1;
  125. }
  126. }
  127. }
  128. return 0;
  129. }
  130. /**
  131. *
  132. */
  133. int async_task_set_workers(int n)
  134. {
  135. if(_async_task_workers>0) {
  136. LM_WARN("task workers already set\n");
  137. return 0;
  138. }
  139. if(n<=0)
  140. return 0;
  141. _async_task_workers = n;
  142. return 0;
  143. }
  144. /**
  145. *
  146. */
  147. int async_task_push(async_task_t *task)
  148. {
  149. int len;
  150. if(_async_task_workers<=0)
  151. return 0;
  152. len = write(_async_task_sockets[1], &task, sizeof(async_task_t*));
  153. if(len<=0) {
  154. LM_ERR("failed to pass the task to asynk workers\n");
  155. return -1;
  156. }
  157. LM_DBG("task sent [%p]\n", task);
  158. return 0;
  159. }
  160. /**
  161. *
  162. */
  163. int async_task_run(int idx)
  164. {
  165. async_task_t *ptask;
  166. int received;
  167. LM_DBG("async task worker %d ready\n", idx);
  168. for( ; ; ) {
  169. if ((received = recvfrom(_async_task_sockets[0],
  170. &ptask, sizeof(async_task_t*),
  171. 0, NULL, 0)) < 0) {
  172. LM_ERR("failed to received task (%d: %s)\n", errno, strerror(errno));
  173. continue;
  174. }
  175. if(received != sizeof(async_task_t*)) {
  176. LM_ERR("invalid task size %d\n", received);
  177. continue;
  178. }
  179. if(ptask->exec!=NULL) {
  180. LM_DBG("task executed [%p] (%p/%p)\n", ptask,
  181. ptask->exec, ptask->param);
  182. ptask->exec(ptask->param);
  183. }
  184. shm_free(ptask);
  185. }
  186. return 0;
  187. }