ondemand.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**
  2. * @file ondemand.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * On-demand process manager.
  32. *
  33. * Synopsis:
  34. * ondemand(string template_name, list args)
  35. *
  36. * Description:
  37. * Manages an on-demand template process using a process template named
  38. * template_name.
  39. * On deinitialization, if the process is running, reqests its termination
  40. * and waits for it to terminate.
  41. *
  42. * Synopsis:
  43. * ondemand::demand()
  44. *
  45. * Description:
  46. * Demands the availability of an on-demand template process.
  47. * This statement is in UP state if and only if the template process of the
  48. * corresponding ondemand object is completely up.
  49. *
  50. * Variables:
  51. * Exposes variables and objects from the template process corresponding to
  52. * the ondemand object.
  53. */
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <misc/offset.h>
  57. #include <misc/debug.h>
  58. #include <structure/LinkedList1.h>
  59. #include <ncd/module_common.h>
  60. #include <generated/blog_channel_ncd_ondemand.h>
  61. struct ondemand {
  62. NCDModuleInst *i;
  63. NCDValRef template_name;
  64. NCDValRef args;
  65. LinkedList1 demands_list;
  66. int dying;
  67. int have_process;
  68. NCDModuleProcess process;
  69. int process_terminating;
  70. int process_up;
  71. };
  72. struct demand {
  73. NCDModuleInst *i;
  74. struct ondemand *od;
  75. LinkedList1Node demands_list_node;
  76. };
  77. static int ondemand_start_process (struct ondemand *o);
  78. static void ondemand_terminate_process (struct ondemand *o);
  79. static void ondemand_process_handler (NCDModuleProcess *process, int event);
  80. static void ondemand_free (struct ondemand *o);
  81. static void demand_free (struct demand *o, int is_error);
  82. static int ondemand_start_process (struct ondemand *o)
  83. {
  84. ASSERT(!o->dying)
  85. ASSERT(!o->have_process)
  86. // start process
  87. if (!NCDModuleProcess_InitValue(&o->process, o->i, o->template_name, o->args, ondemand_process_handler)) {
  88. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  89. goto fail0;
  90. }
  91. // set have process
  92. o->have_process = 1;
  93. // set process not terminating
  94. o->process_terminating = 0;
  95. // set process not up
  96. o->process_up = 0;
  97. return 1;
  98. fail0:
  99. return 0;
  100. }
  101. static void ondemand_terminate_process (struct ondemand *o)
  102. {
  103. ASSERT(o->have_process)
  104. ASSERT(!o->process_terminating)
  105. // request termination
  106. NCDModuleProcess_Terminate(&o->process);
  107. // set process terminating
  108. o->process_terminating = 1;
  109. if (o->process_up) {
  110. // set process down
  111. o->process_up = 0;
  112. // signal demands down
  113. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
  114. struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
  115. ASSERT(demand->od == o)
  116. NCDModuleInst_Backend_Down(demand->i);
  117. }
  118. }
  119. }
  120. static void ondemand_process_handler (NCDModuleProcess *process, int event)
  121. {
  122. struct ondemand *o = UPPER_OBJECT(process, struct ondemand, process);
  123. ASSERT(o->have_process)
  124. switch (event) {
  125. case NCDMODULEPROCESS_EVENT_UP: {
  126. ASSERT(!o->process_terminating)
  127. ASSERT(!o->process_up)
  128. // set process up
  129. o->process_up = 1;
  130. // signal demands up
  131. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
  132. struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
  133. ASSERT(demand->od == o)
  134. NCDModuleInst_Backend_Up(demand->i);
  135. }
  136. } break;
  137. case NCDMODULEPROCESS_EVENT_DOWN: {
  138. ASSERT(!o->process_terminating)
  139. ASSERT(o->process_up)
  140. // continue process
  141. NCDModuleProcess_Continue(&o->process);
  142. // set process down
  143. o->process_up = 0;
  144. // signal demands down
  145. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
  146. struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
  147. ASSERT(demand->od == o)
  148. NCDModuleInst_Backend_Down(demand->i);
  149. }
  150. } break;
  151. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  152. ASSERT(o->process_terminating)
  153. ASSERT(!o->process_up)
  154. // free process
  155. NCDModuleProcess_Free(&o->process);
  156. // set have no process
  157. o->have_process = 0;
  158. // if dying, die finally
  159. if (o->dying) {
  160. ondemand_free(o);
  161. return;
  162. }
  163. // if demands arrivied, restart process
  164. if (!LinkedList1_IsEmpty(&o->demands_list)) {
  165. if (!ondemand_start_process(o)) {
  166. // error demands
  167. while (!LinkedList1_IsEmpty(&o->demands_list)) {
  168. struct demand *demand = UPPER_OBJECT(LinkedList1_GetFirst(&o->demands_list), struct demand, demands_list_node);
  169. ASSERT(demand->od == o)
  170. demand_free(demand, 1);
  171. }
  172. }
  173. }
  174. } break;
  175. }
  176. }
  177. static void ondemand_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  178. {
  179. struct ondemand *o = vo;
  180. o->i = i;
  181. // read arguments
  182. NCDValRef arg_template_name;
  183. NCDValRef arg_args;
  184. if (!NCDVal_ListRead(params->args, 2, &arg_template_name, &arg_args)) {
  185. ModuleLog(i, BLOG_ERROR, "wrong arity");
  186. goto fail0;
  187. }
  188. if (!NCDVal_IsString(arg_template_name) || !NCDVal_IsList(arg_args)) {
  189. ModuleLog(i, BLOG_ERROR, "wrong type");
  190. goto fail0;
  191. }
  192. o->template_name = arg_template_name;
  193. o->args = arg_args;
  194. // init demands list
  195. LinkedList1_Init(&o->demands_list);
  196. // set not dying
  197. o->dying = 0;
  198. // set have no process
  199. o->have_process = 0;
  200. // signal up
  201. NCDModuleInst_Backend_Up(i);
  202. return;
  203. fail0:
  204. NCDModuleInst_Backend_DeadError(i);
  205. }
  206. static void ondemand_free (struct ondemand *o)
  207. {
  208. ASSERT(!o->have_process)
  209. // die demands
  210. while (!LinkedList1_IsEmpty(&o->demands_list)) {
  211. struct demand *demand = UPPER_OBJECT(LinkedList1_GetFirst(&o->demands_list), struct demand, demands_list_node);
  212. ASSERT(demand->od == o)
  213. demand_free(demand, 0);
  214. }
  215. NCDModuleInst_Backend_Dead(o->i);
  216. }
  217. static void ondemand_func_die (void *vo)
  218. {
  219. struct ondemand *o = vo;
  220. ASSERT(!o->dying)
  221. // if not have process, die right away
  222. if (!o->have_process) {
  223. ondemand_free(o);
  224. return;
  225. }
  226. // set dying
  227. o->dying = 1;
  228. // request process termination if not already
  229. if (!o->process_terminating) {
  230. ondemand_terminate_process(o);
  231. }
  232. }
  233. static void demand_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  234. {
  235. struct demand *o = vo;
  236. o->i = i;
  237. // read arguments
  238. if (!NCDVal_ListRead(params->args, 0)) {
  239. ModuleLog(i, BLOG_ERROR, "wrong arity");
  240. goto fail0;
  241. }
  242. // set ondemand
  243. o->od = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  244. // add to ondemand's demands list
  245. LinkedList1_Append(&o->od->demands_list, &o->demands_list_node);
  246. // start process if needed
  247. if (!o->od->have_process) {
  248. ASSERT(!o->od->dying)
  249. if (!ondemand_start_process(o->od)) {
  250. goto fail1;
  251. }
  252. }
  253. // if process is up, signal up
  254. if (o->od->process_up) {
  255. NCDModuleInst_Backend_Up(i);
  256. }
  257. return;
  258. fail1:
  259. LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
  260. fail0:
  261. NCDModuleInst_Backend_DeadError(i);
  262. }
  263. static void demand_free (struct demand *o, int is_error)
  264. {
  265. // remove from ondemand's demands list
  266. LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
  267. // request process termination if no longer needed
  268. if (o->od->have_process && !o->od->process_terminating && LinkedList1_IsEmpty(&o->od->demands_list)) {
  269. ondemand_terminate_process(o->od);
  270. }
  271. if (is_error) {
  272. NCDModuleInst_Backend_DeadError(o->i);
  273. } else {
  274. NCDModuleInst_Backend_Dead(o->i);
  275. }
  276. }
  277. static void demand_func_die (void *vo)
  278. {
  279. struct demand *o = vo;
  280. demand_free(o, 0);
  281. }
  282. static int demand_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  283. {
  284. struct demand *o = vo;
  285. ASSERT(o->od->have_process)
  286. ASSERT(o->od->process_up)
  287. return NCDModuleProcess_GetObj(&o->od->process, objname, out_object);
  288. }
  289. static struct NCDModule modules[] = {
  290. {
  291. .type = "ondemand",
  292. .func_new2 = ondemand_func_new,
  293. .func_die = ondemand_func_die,
  294. .alloc_size = sizeof(struct ondemand)
  295. }, {
  296. .type = "ondemand::demand",
  297. .func_new2 = demand_func_new,
  298. .func_die = demand_func_die,
  299. .func_getobj = demand_func_getobj,
  300. .alloc_size = sizeof(struct demand)
  301. }, {
  302. .type = NULL
  303. }
  304. };
  305. const struct NCDModuleGroup ncdmodule_ondemand = {
  306. .modules = modules
  307. };