spawn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /**
  2. * @file spawn.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. * Module which starts a process from a process template on initialization, and
  32. * stops it on deinitialization.
  33. *
  34. * Synopsis:
  35. * spawn(string template_name, list args)
  36. *
  37. * Description:
  38. * On initialization, creates a new process from the template named
  39. * 'template_name', with arguments 'args'. On deinitialization, initiates termination
  40. * of the process and waits for it to terminate. The process can access objects
  41. * as seen from 'spawn' via the _caller special object.
  42. *
  43. * Synopsis:
  44. * spawn::join()
  45. *
  46. * Description:
  47. * A join() on a spawn() is like a depend() on a provide() which is located at the
  48. * end of the spawned process.
  49. *
  50. * Variables:
  51. * Exposes objects from the spawned process.
  52. */
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <misc/offset.h>
  56. #include <misc/debug.h>
  57. #include <structure/LinkedList0.h>
  58. #include <ncd/module_common.h>
  59. #include <generated/blog_channel_ncd_spawn.h>
  60. #define STATE_WORKING 1
  61. #define STATE_UP 2
  62. #define STATE_WAITING 3
  63. #define STATE_WAITING_TERMINATING 4
  64. #define STATE_TERMINATING 5
  65. struct instance {
  66. NCDModuleInst *i;
  67. NCDModuleProcess process;
  68. LinkedList0 clean_list;
  69. LinkedList0 dirty_list;
  70. int state;
  71. };
  72. struct join_instance {
  73. NCDModuleInst *i;
  74. struct instance *spawn;
  75. LinkedList0Node list_node;
  76. int is_dirty;
  77. };
  78. static void assert_dirty_state (struct instance *o);
  79. static void process_handler_event (NCDModuleProcess *process, int event);
  80. static int process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  81. static int caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object);
  82. static void bring_joins_down (struct instance *o);
  83. static void continue_working (struct instance *o);
  84. static void continue_terminating (struct instance *o);
  85. static void instance_free (struct instance *o);
  86. static void assert_dirty_state (struct instance *o)
  87. {
  88. ASSERT(!LinkedList0_IsEmpty(&o->dirty_list) == (o->state == STATE_WAITING || o->state == STATE_WAITING_TERMINATING))
  89. }
  90. static void process_handler_event (NCDModuleProcess *process, int event)
  91. {
  92. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  93. assert_dirty_state(o);
  94. switch (event) {
  95. case NCDMODULEPROCESS_EVENT_UP: {
  96. ASSERT(o->state == STATE_WORKING)
  97. ASSERT(LinkedList0_IsEmpty(&o->dirty_list))
  98. // set state up
  99. o->state = STATE_UP;
  100. // bring joins up
  101. for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->clean_list); ln; ln = LinkedList0Node_Next(ln)) {
  102. struct join_instance *join = UPPER_OBJECT(ln, struct join_instance, list_node);
  103. ASSERT(join->spawn == o)
  104. ASSERT(!join->is_dirty)
  105. NCDModuleInst_Backend_Up(join->i);
  106. }
  107. } break;
  108. case NCDMODULEPROCESS_EVENT_DOWN: {
  109. ASSERT(o->state == STATE_UP)
  110. ASSERT(LinkedList0_IsEmpty(&o->dirty_list))
  111. // bring joins down, moving them to the dirty list
  112. bring_joins_down(o);
  113. // set state waiting
  114. o->state = STATE_WAITING;
  115. // if we have no joins, continue immediately
  116. if (LinkedList0_IsEmpty(&o->dirty_list)) {
  117. continue_working(o);
  118. return;
  119. }
  120. } break;
  121. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  122. ASSERT(o->state == STATE_TERMINATING)
  123. ASSERT(LinkedList0_IsEmpty(&o->dirty_list))
  124. // die finally
  125. instance_free(o);
  126. return;
  127. } break;
  128. default: ASSERT(0);
  129. }
  130. }
  131. static int process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  132. {
  133. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  134. if (name == NCD_STRING_CALLER) {
  135. *out_object = NCDObject_Build(-1, o, NCDObject_no_getvar, caller_obj_func_getobj);
  136. return 1;
  137. }
  138. return 0;
  139. }
  140. static int caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  141. {
  142. struct instance *o = NCDObject_DataPtr(obj);
  143. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  144. }
  145. static void bring_joins_down (struct instance *o)
  146. {
  147. LinkedList0Node *ln;
  148. while (ln = LinkedList0_GetFirst(&o->clean_list)) {
  149. struct join_instance *join = UPPER_OBJECT(ln, struct join_instance, list_node);
  150. ASSERT(join->spawn == o)
  151. ASSERT(!join->is_dirty)
  152. NCDModuleInst_Backend_Down(join->i);
  153. LinkedList0_Remove(&o->clean_list, &join->list_node);
  154. LinkedList0_Prepend(&o->dirty_list, &join->list_node);
  155. join->is_dirty = 1;
  156. }
  157. }
  158. static void continue_working (struct instance *o)
  159. {
  160. ASSERT(o->state == STATE_WAITING)
  161. ASSERT(LinkedList0_IsEmpty(&o->dirty_list))
  162. // continue process
  163. NCDModuleProcess_Continue(&o->process);
  164. // set state working
  165. o->state = STATE_WORKING;
  166. }
  167. static void continue_terminating (struct instance *o)
  168. {
  169. ASSERT(o->state == STATE_WAITING_TERMINATING)
  170. ASSERT(LinkedList0_IsEmpty(&o->dirty_list))
  171. // request process to terminate
  172. NCDModuleProcess_Terminate(&o->process);
  173. // set state terminating
  174. o->state = STATE_TERMINATING;
  175. }
  176. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  177. {
  178. struct instance *o = vo;
  179. o->i = i;
  180. // check arguments
  181. NCDValRef template_name_arg;
  182. NCDValRef args_arg;
  183. if (!NCDVal_ListRead(params->args, 2, &template_name_arg, &args_arg)) {
  184. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  185. goto fail0;
  186. }
  187. if (!NCDVal_IsString(template_name_arg) || !NCDVal_IsList(args_arg)) {
  188. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  189. goto fail0;
  190. }
  191. // signal up.
  192. // Do it before creating the process so that the process starts initializing before our own process continues.
  193. NCDModuleInst_Backend_Up(o->i);
  194. // create process
  195. if (!NCDModuleProcess_InitValue(&o->process, o->i, template_name_arg, args_arg, process_handler_event)) {
  196. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  197. goto fail0;
  198. }
  199. // set object resolution function
  200. NCDModuleProcess_SetSpecialFuncs(&o->process, process_func_getspecialobj);
  201. // init lists
  202. LinkedList0_Init(&o->clean_list);
  203. LinkedList0_Init(&o->dirty_list);
  204. // set state working
  205. o->state = STATE_WORKING;
  206. return;
  207. fail0:
  208. NCDModuleInst_Backend_DeadError(i);
  209. }
  210. void instance_free (struct instance *o)
  211. {
  212. ASSERT(LinkedList0_IsEmpty(&o->dirty_list))
  213. // unlink joins
  214. LinkedList0Node *ln;
  215. while (ln = LinkedList0_GetFirst(&o->clean_list)) {
  216. struct join_instance *join = UPPER_OBJECT(ln, struct join_instance, list_node);
  217. ASSERT(join->spawn == o)
  218. ASSERT(!join->is_dirty)
  219. LinkedList0_Remove(&o->clean_list, &join->list_node);
  220. join->spawn = NULL;
  221. }
  222. // free process
  223. NCDModuleProcess_Free(&o->process);
  224. NCDModuleInst_Backend_Dead(o->i);
  225. }
  226. static void func_die (void *vo)
  227. {
  228. struct instance *o = vo;
  229. ASSERT(o->state != STATE_WAITING_TERMINATING)
  230. ASSERT(o->state != STATE_TERMINATING)
  231. assert_dirty_state(o);
  232. // bring joins down
  233. if (o->state == STATE_UP) {
  234. bring_joins_down(o);
  235. }
  236. // set state waiting terminating
  237. o->state = STATE_WAITING_TERMINATING;
  238. // start terminating now if possible
  239. if (LinkedList0_IsEmpty(&o->dirty_list)) {
  240. continue_terminating(o);
  241. return;
  242. }
  243. }
  244. static void join_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  245. {
  246. struct join_instance *o = vo;
  247. o->i = i;
  248. if (!NCDVal_ListRead(params->args, 0)) {
  249. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  250. goto fail0;
  251. }
  252. o->spawn = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  253. assert_dirty_state(o->spawn);
  254. LinkedList0_Prepend(&o->spawn->clean_list, &o->list_node);
  255. o->is_dirty = 0;
  256. if (o->spawn->state == STATE_UP) {
  257. NCDModuleInst_Backend_Up(i);
  258. }
  259. return;
  260. fail0:
  261. NCDModuleInst_Backend_DeadError(i);
  262. }
  263. static void join_func_die (void *vo)
  264. {
  265. struct join_instance *o = vo;
  266. if (o->spawn) {
  267. assert_dirty_state(o->spawn);
  268. // remove from list
  269. if (o->is_dirty) {
  270. LinkedList0_Remove(&o->spawn->dirty_list, &o->list_node);
  271. } else {
  272. LinkedList0_Remove(&o->spawn->clean_list, &o->list_node);
  273. }
  274. if (o->is_dirty && LinkedList0_IsEmpty(&o->spawn->dirty_list)) {
  275. ASSERT(o->spawn->state == STATE_WAITING || o->spawn->state == STATE_WAITING_TERMINATING)
  276. if (o->spawn->state == STATE_WAITING) {
  277. continue_working(o->spawn);
  278. } else {
  279. continue_terminating(o->spawn);
  280. }
  281. }
  282. }
  283. NCDModuleInst_Backend_Dead(o->i);
  284. }
  285. static int join_func_getobj (void *vo, NCD_string_id_t name, NCDObject *out)
  286. {
  287. struct join_instance *o = vo;
  288. if (!o->spawn) {
  289. return 0;
  290. }
  291. return NCDModuleProcess_GetObj(&o->spawn->process, name, out);
  292. }
  293. static void join_func_clean (void *vo)
  294. {
  295. struct join_instance *o = vo;
  296. if (!(o->spawn && o->is_dirty)) {
  297. return;
  298. }
  299. assert_dirty_state(o->spawn);
  300. ASSERT(o->spawn->state == STATE_WAITING || o->spawn->state == STATE_WAITING_TERMINATING)
  301. LinkedList0_Remove(&o->spawn->dirty_list, &o->list_node);
  302. LinkedList0_Prepend(&o->spawn->clean_list, &o->list_node);
  303. o->is_dirty = 0;
  304. if (LinkedList0_IsEmpty(&o->spawn->dirty_list)) {
  305. if (o->spawn->state == STATE_WAITING) {
  306. continue_working(o->spawn);
  307. } else {
  308. continue_terminating(o->spawn);
  309. }
  310. }
  311. }
  312. static struct NCDModule modules[] = {
  313. {
  314. .type = "spawn",
  315. .func_new2 = func_new,
  316. .func_die = func_die,
  317. .alloc_size = sizeof(struct instance)
  318. }, {
  319. .type = "synchronous_process", // deprecated name
  320. .func_new2 = func_new,
  321. .func_die = func_die,
  322. .alloc_size = sizeof(struct instance)
  323. }, {
  324. .type = "spawn::join",
  325. .func_new2 = join_func_new,
  326. .func_die = join_func_die,
  327. .func_getobj = join_func_getobj,
  328. .func_clean = join_func_clean,
  329. .alloc_size = sizeof(struct join_instance),
  330. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
  331. }, {
  332. .type = NULL
  333. }
  334. };
  335. const struct NCDModuleGroup ncdmodule_spawn = {
  336. .modules = modules
  337. };