imperative.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /**
  2. * @file imperative.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. * Imperative statement.
  32. *
  33. * Synopsis:
  34. * imperative(string init_template, list init_args, string deinit_template, list deinit_args, string deinit_timeout)
  35. *
  36. * Description:
  37. * Does the following, in order:
  38. * 1. Starts a template process from (init_template, init_args) and waits for it to
  39. * initialize completely.
  40. * 2. Initiates termination of the process and wait for it to terminate.
  41. * 3. Puts the statement UP, then waits for a statement termination request (which may
  42. * already have been received).
  43. * 4. Starts a template process from (deinit_template, deinit_args) and waits for it
  44. * to initialize completely, or for the timeout to elapse.
  45. * 5. Initiates termination of the process and wait for it to terminate.
  46. * 6. Terminates the statement.
  47. *
  48. * If init_template="<none>", steps (1-2) are skipped.
  49. * If deinit_template="<none>", steps (4-5) are skipped.
  50. */
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <misc/string_begins_with.h>
  54. #include <misc/offset.h>
  55. #include <ncd/NCDModule.h>
  56. #include <ncd/extra/value_utils.h>
  57. #include <generated/blog_channel_ncd_imperative.h>
  58. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  59. #define STATE_INIT_WORKING 1
  60. #define STATE_INIT_CLEANING 2
  61. #define STATE_UP 3
  62. #define STATE_DEINIT_WORKING 4
  63. #define STATE_DEINIT_CLEANING 5
  64. struct instance {
  65. NCDModuleInst *i;
  66. NCDValRef deinit_template;
  67. NCDValRef deinit_args;
  68. BTimer deinit_timer;
  69. NCDModuleProcess process;
  70. int state;
  71. int dying;
  72. };
  73. static int start_process (struct instance *o, NCDValRef template_name, NCDValRef args, NCDModuleProcess_handler_event handler);
  74. static void go_deinit (struct instance *o);
  75. static void init_process_handler_event (NCDModuleProcess *process, int event);
  76. static void deinit_process_handler_event (NCDModuleProcess *process, int event);
  77. static int process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  78. static int process_caller_object_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object);
  79. static void deinit_timer_handler (struct instance *o);
  80. static void instance_free (struct instance *o);
  81. enum {STRING_CALLER};
  82. static struct NCD_string_request strings[] = {
  83. {"_caller"}, {NULL}
  84. };
  85. static int start_process (struct instance *o, NCDValRef template_name, NCDValRef args, NCDModuleProcess_handler_event handler)
  86. {
  87. ASSERT(NCDVal_IsString(template_name))
  88. ASSERT(NCDVal_IsList(args))
  89. // create process
  90. if (!NCDModuleProcess_InitValue(&o->process, o->i, template_name, args, handler)) {
  91. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  92. return 0;
  93. }
  94. // set special functions
  95. NCDModuleProcess_SetSpecialFuncs(&o->process, process_func_getspecialobj);
  96. return 1;
  97. }
  98. static void go_deinit (struct instance *o)
  99. {
  100. ASSERT(o->dying)
  101. // deinit is no-op?
  102. if (ncd_is_none(o->deinit_template)) {
  103. instance_free(o);
  104. return;
  105. }
  106. // start deinit process
  107. if (!start_process(o, o->deinit_template, o->deinit_args, deinit_process_handler_event)) {
  108. instance_free(o);
  109. return;
  110. }
  111. // start timer
  112. BReactor_SetTimer(o->i->params->iparams->reactor, &o->deinit_timer);
  113. // set state deinit working
  114. o->state = STATE_DEINIT_WORKING;
  115. }
  116. static void init_process_handler_event (NCDModuleProcess *process, int event)
  117. {
  118. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  119. switch (event) {
  120. case NCDMODULEPROCESS_EVENT_UP: {
  121. ASSERT(o->state == STATE_INIT_WORKING)
  122. // start terminating
  123. NCDModuleProcess_Terminate(&o->process);
  124. // set state init cleaning
  125. o->state = STATE_INIT_CLEANING;
  126. } break;
  127. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  128. ASSERT(o->state == STATE_INIT_CLEANING)
  129. // free process
  130. NCDModuleProcess_Free(&o->process);
  131. // were we requested to die aleady?
  132. if (o->dying) {
  133. go_deinit(o);
  134. return;
  135. }
  136. // signal up
  137. NCDModuleInst_Backend_Up(o->i);
  138. // set state up
  139. o->state = STATE_UP;
  140. } break;
  141. default: ASSERT(0);
  142. }
  143. }
  144. static void deinit_process_handler_event (NCDModuleProcess *process, int event)
  145. {
  146. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  147. ASSERT(o->dying)
  148. switch (event) {
  149. case NCDMODULEPROCESS_EVENT_UP: {
  150. ASSERT(o->state == STATE_DEINIT_WORKING)
  151. // stop timer
  152. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->deinit_timer);
  153. // start terminating
  154. NCDModuleProcess_Terminate(&o->process);
  155. // set state deinit cleaning
  156. o->state = STATE_DEINIT_CLEANING;
  157. } break;
  158. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  159. ASSERT(o->state == STATE_DEINIT_CLEANING)
  160. // free process
  161. NCDModuleProcess_Free(&o->process);
  162. // die
  163. instance_free(o);
  164. return;
  165. } break;
  166. default: ASSERT(0);
  167. }
  168. }
  169. static int process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  170. {
  171. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  172. ASSERT(o->state != STATE_UP)
  173. if (name == strings[STRING_CALLER].id) {
  174. *out_object = NCDObject_Build(-1, o, NCDObject_no_getvar, process_caller_object_func_getobj);
  175. return 1;
  176. }
  177. return 0;
  178. }
  179. static int process_caller_object_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  180. {
  181. struct instance *o = NCDObject_DataPtr(obj);
  182. ASSERT(o->state != STATE_UP)
  183. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  184. }
  185. static void deinit_timer_handler (struct instance *o)
  186. {
  187. ASSERT(o->state == STATE_DEINIT_WORKING)
  188. ModuleLog(o->i, BLOG_ERROR, "imperative deinit timeout elapsed");
  189. // start terminating
  190. NCDModuleProcess_Terminate(&o->process);
  191. // set state deinit cleaning
  192. o->state = STATE_DEINIT_CLEANING;
  193. }
  194. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  195. {
  196. struct instance *o = vo;
  197. o->i = i;
  198. // check arguments
  199. NCDValRef init_template_arg;
  200. NCDValRef init_args;
  201. NCDValRef deinit_template_arg;
  202. NCDValRef deinit_timeout_arg;
  203. if (!NCDVal_ListRead(params->args, 5, &init_template_arg, &init_args, &deinit_template_arg, &o->deinit_args, &deinit_timeout_arg)) {
  204. ModuleLog(i, BLOG_ERROR, "wrong arity");
  205. goto fail0;
  206. }
  207. if (!NCDVal_IsString(init_template_arg) || !NCDVal_IsList(init_args) ||
  208. !NCDVal_IsString(deinit_template_arg) || !NCDVal_IsList(o->deinit_args) ||
  209. !NCDVal_IsString(deinit_timeout_arg)) {
  210. ModuleLog(i, BLOG_ERROR, "wrong type");
  211. goto fail0;
  212. }
  213. o->deinit_template = deinit_template_arg;
  214. // read timeout
  215. uintmax_t timeout;
  216. if (!ncd_read_uintmax(deinit_timeout_arg, &timeout) || timeout > UINT64_MAX){
  217. ModuleLog(i, BLOG_ERROR, "wrong timeout");
  218. goto fail0;
  219. }
  220. // init timer
  221. BTimer_Init(&o->deinit_timer, timeout, (BTimer_handler)deinit_timer_handler, o);
  222. if (ncd_is_none(init_template_arg)) {
  223. // signal up
  224. NCDModuleInst_Backend_Up(i);
  225. // set state up
  226. o->state = STATE_UP;
  227. } else {
  228. // start init process
  229. if (!start_process(o, init_template_arg, init_args, init_process_handler_event)) {
  230. goto fail0;
  231. }
  232. // set state init working
  233. o->state = STATE_INIT_WORKING;
  234. }
  235. // set not dying
  236. o->dying = 0;
  237. return;
  238. fail0:
  239. NCDModuleInst_Backend_DeadError(i);
  240. }
  241. static void instance_free (struct instance *o)
  242. {
  243. NCDModuleInst_Backend_Dead(o->i);
  244. }
  245. static void func_die (void *vo)
  246. {
  247. struct instance *o = vo;
  248. ASSERT(!o->dying)
  249. // set dying
  250. o->dying = 1;
  251. if (o->state == STATE_UP) {
  252. go_deinit(o);
  253. return;
  254. }
  255. }
  256. static struct NCDModule modules[] = {
  257. {
  258. .type = "imperative",
  259. .func_new2 = func_new,
  260. .func_die = func_die,
  261. .alloc_size = sizeof(struct instance)
  262. }, {
  263. .type = NULL
  264. }
  265. };
  266. const struct NCDModuleGroup ncdmodule_imperative = {
  267. .modules = modules,
  268. .strings = strings
  269. };