call.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /**
  2. * @file call.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. * Synopsis:
  32. * callrefhere()
  33. *
  34. * Description:
  35. * Exposes variables and objects to call() statements as seen from this
  36. * callrefhere() statement.
  37. *
  38. * Synopsis:
  39. * call(string template_name, list args)
  40. * callhefhere::call(string template_name, list args)
  41. *
  42. * Description:
  43. * Module which allows using a single statement to represent multiple statements
  44. * in a process template, allowing reuse of repetitive code.
  45. * The created template process can access variables and objects as seen from the
  46. * call statement via "_caller.variable".
  47. * The second form also exposes variables and objects from the corresponding
  48. * callrefhere() statement via "_ref.variable".
  49. * If template_name is "<none>", then the call() is a no-op - it goes up
  50. * immediately and immediately terminates on request.
  51. *
  52. * Variables:
  53. * Exposes variables as seen from the end of the called process template.
  54. *
  55. * Behavior in detail (assuming template_name is not "<none>"):
  56. * - On initialization, creates a new process from the template named
  57. * template_name, with arguments args.
  58. * - When all the statements in the created process go UP, transitions UP.
  59. * - When one of the statements is no longer UP, transitions DOWN. The
  60. * created process remais paused until the call statement receives the
  61. * clean signal, to wait for following statements to deinitialize.
  62. * - On deinitialization, initiates termination of the created process and waits
  63. * for all its statements to deinitialize.
  64. */
  65. #include <stdlib.h>
  66. #include <misc/string_begins_with.h>
  67. #include <misc/offset.h>
  68. #include <structure/LinkedList0.h>
  69. #include <ncd/NCDModule.h>
  70. #include <ncd/value_utils.h>
  71. #include <generated/blog_channel_ncd_call.h>
  72. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  73. #define STATE_WORKING 1
  74. #define STATE_UP 2
  75. #define STATE_WAITING 3
  76. #define STATE_TERMINATING 4
  77. #define STATE_NONE 5
  78. struct callrefhere_instance {
  79. NCDModuleInst *i;
  80. LinkedList0 calls_list;
  81. };
  82. struct instance {
  83. NCDModuleInst *i;
  84. NCDValMem args_mem;
  85. NCDModuleProcess process;
  86. int state;
  87. struct callrefhere_instance *crh;
  88. LinkedList0Node calls_list_node;
  89. };
  90. static void instance_free (struct instance *o);
  91. static int caller_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object);
  92. static int ref_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object);
  93. enum {STRING_CALLER, STRING_REF};
  94. static struct NCD_string_request strings[] = {
  95. {"_caller"}, {"_ref"}, {NULL}
  96. };
  97. static void process_handler_event (struct instance *o, int event)
  98. {
  99. switch (event) {
  100. case NCDMODULEPROCESS_EVENT_UP: {
  101. ASSERT(o->state == STATE_WORKING)
  102. // signal up
  103. NCDModuleInst_Backend_Up(o->i);
  104. // set state up
  105. o->state = STATE_UP;
  106. } break;
  107. case NCDMODULEPROCESS_EVENT_DOWN: {
  108. ASSERT(o->state == STATE_UP)
  109. // signal down
  110. NCDModuleInst_Backend_Down(o->i);
  111. // set state waiting
  112. o->state = STATE_WAITING;
  113. } break;
  114. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  115. ASSERT(o->state == STATE_TERMINATING)
  116. // die finally
  117. instance_free(o);
  118. return;
  119. } break;
  120. default: ASSERT(0);
  121. }
  122. }
  123. static int process_func_getspecialobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
  124. {
  125. if (name == strings[STRING_CALLER].id) {
  126. *out_object = NCDObject_Build(-1, o, NULL, (NCDObject_func_getobj)caller_obj_func_getobj);
  127. return 1;
  128. }
  129. if (name == strings[STRING_REF].id) {
  130. *out_object = NCDObject_Build(-1, o, NULL, (NCDObject_func_getobj)ref_obj_func_getobj);
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. static void callrefhere_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  136. {
  137. struct callrefhere_instance *o = vo;
  138. o->i = i;
  139. // init calls list
  140. LinkedList0_Init(&o->calls_list);
  141. // signal up
  142. NCDModuleInst_Backend_Up(o->i);
  143. }
  144. static void callrefhere_func_die (void *vo)
  145. {
  146. struct callrefhere_instance *o = vo;
  147. // disconnect calls
  148. while (!LinkedList0_IsEmpty(&o->calls_list)) {
  149. struct instance *inst = UPPER_OBJECT(LinkedList0_GetFirst(&o->calls_list), struct instance, calls_list_node);
  150. ASSERT(inst->crh == o)
  151. LinkedList0_Remove(&o->calls_list, &inst->calls_list_node);
  152. inst->crh = NULL;
  153. }
  154. NCDModuleInst_Backend_Dead(o->i);
  155. }
  156. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  157. {
  158. struct instance *o = vo;
  159. o->i = i;
  160. // check arguments
  161. NCDValRef template_name_arg;
  162. NCDValRef args_arg;
  163. if (!NCDVal_ListRead(params->args, 2, &template_name_arg, &args_arg)) {
  164. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  165. goto fail0;
  166. }
  167. if (!NCDVal_IsString(template_name_arg) || !NCDVal_IsList(args_arg)) {
  168. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  169. goto fail0;
  170. }
  171. // calling none?
  172. if (ncd_is_none(template_name_arg)) {
  173. // signal up
  174. NCDModuleInst_Backend_Up(o->i);
  175. // set state none
  176. o->state = STATE_NONE;
  177. } else {
  178. // init args mem
  179. NCDValMem_Init(&o->args_mem);
  180. // copy arguments
  181. NCDValRef args = NCDVal_NewCopy(&o->args_mem, args_arg);
  182. if (NCDVal_IsInvalid(args)) {
  183. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  184. NCDValMem_Free(&o->args_mem);
  185. goto fail0;
  186. }
  187. // create process
  188. if (!NCDModuleProcess_InitValue(&o->process, o->i, template_name_arg, args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  189. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  190. NCDValMem_Free(&o->args_mem);
  191. goto fail0;
  192. }
  193. // set special functions
  194. NCDModuleProcess_SetSpecialFuncs(&o->process,
  195. (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj);
  196. // set callrefhere
  197. o->crh = (params->method_user ? NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user) : NULL);
  198. // add to callrefhere's calls list
  199. if (o->crh) {
  200. LinkedList0_Prepend(&o->crh->calls_list, &o->calls_list_node);
  201. }
  202. // set state working
  203. o->state = STATE_WORKING;
  204. }
  205. return;
  206. fail0:
  207. NCDModuleInst_Backend_SetError(i);
  208. NCDModuleInst_Backend_Dead(i);
  209. }
  210. void instance_free (struct instance *o)
  211. {
  212. if (o->state != STATE_NONE) {
  213. // remove from callrefhere's calls list
  214. if (o->crh) {
  215. LinkedList0_Remove(&o->crh->calls_list, &o->calls_list_node);
  216. }
  217. // free args mem
  218. NCDValMem_Free(&o->args_mem);
  219. // free process
  220. NCDModuleProcess_Free(&o->process);
  221. }
  222. NCDModuleInst_Backend_Dead(o->i);
  223. }
  224. static void func_die (void *vo)
  225. {
  226. struct instance *o = vo;
  227. ASSERT(o->state != STATE_TERMINATING)
  228. // if none, die now
  229. if (o->state == STATE_NONE) {
  230. instance_free(o);
  231. return;
  232. }
  233. // request process to terminate
  234. NCDModuleProcess_Terminate(&o->process);
  235. // set state terminating
  236. o->state = STATE_TERMINATING;
  237. }
  238. static void func_clean (void *vo)
  239. {
  240. struct instance *o = vo;
  241. if (o->state != STATE_WAITING) {
  242. return;
  243. }
  244. // allow process to continue
  245. NCDModuleProcess_Continue(&o->process);
  246. // set state working
  247. o->state = STATE_WORKING;
  248. }
  249. static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
  250. {
  251. struct instance *o = vo;
  252. if (o->state == STATE_NONE) {
  253. return 0;
  254. }
  255. return NCDModuleProcess_GetObj(&o->process, name, out_object);
  256. }
  257. static int caller_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
  258. {
  259. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  260. }
  261. static int ref_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
  262. {
  263. if (!o->crh) {
  264. return 0;
  265. }
  266. return NCDModuleInst_Backend_GetObj(o->crh->i, name, out_object);
  267. }
  268. static struct NCDModule modules[] = {
  269. {
  270. .type = "callrefhere",
  271. .func_new2 = callrefhere_func_new,
  272. .func_die = callrefhere_func_die,
  273. .alloc_size = sizeof(struct callrefhere_instance)
  274. }, {
  275. .type = "call",
  276. .func_new2 = func_new,
  277. .func_die = func_die,
  278. .func_clean = func_clean,
  279. .func_getobj = func_getobj,
  280. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  281. .alloc_size = sizeof(struct instance)
  282. }, {
  283. .type = "callrefhere::call",
  284. .func_new2 = func_new,
  285. .func_die = func_die,
  286. .func_clean = func_clean,
  287. .func_getobj = func_getobj,
  288. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  289. .alloc_size = sizeof(struct instance)
  290. }, {
  291. .type = NULL
  292. }
  293. };
  294. const struct NCDModuleGroup ncdmodule_call = {
  295. .modules = modules,
  296. .strings = strings
  297. };