spawn.c 12 KB

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