call.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <string.h>
  67. #include <misc/string_begins_with.h>
  68. #include <misc/offset.h>
  69. #include <structure/LinkedList0.h>
  70. #include <ncd/NCDModule.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. NCDModuleProcess process;
  85. int state;
  86. struct callrefhere_instance *crh;
  87. LinkedList0Node calls_list_node;
  88. };
  89. static void instance_free (struct instance *o);
  90. static void process_handler_event (struct instance *o, int event)
  91. {
  92. switch (event) {
  93. case NCDMODULEPROCESS_EVENT_UP: {
  94. ASSERT(o->state == STATE_WORKING)
  95. // signal up
  96. NCDModuleInst_Backend_Up(o->i);
  97. // set state up
  98. o->state = STATE_UP;
  99. } break;
  100. case NCDMODULEPROCESS_EVENT_DOWN: {
  101. ASSERT(o->state == STATE_UP)
  102. // signal down
  103. NCDModuleInst_Backend_Down(o->i);
  104. // set state waiting
  105. o->state = STATE_WAITING;
  106. } break;
  107. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  108. ASSERT(o->state == STATE_TERMINATING)
  109. // die finally
  110. instance_free(o);
  111. return;
  112. } break;
  113. default: ASSERT(0);
  114. }
  115. }
  116. static int process_func_getspecialvar (struct instance *o, const char *name, NCDValue *out)
  117. {
  118. size_t p;
  119. if (p = string_begins_with(name, "_caller.")) {
  120. return NCDModuleInst_Backend_GetVar(o->i, name + p, out);
  121. }
  122. if (o->crh && (p = string_begins_with(name, "_ref."))) {
  123. return NCDModuleInst_Backend_GetVar(o->crh->i, name + p, out);
  124. }
  125. return 0;
  126. }
  127. static NCDModuleInst * process_func_getspecialobj (struct instance *o, const char *name)
  128. {
  129. size_t p;
  130. if (p = string_begins_with(name, "_caller.")) {
  131. return NCDModuleInst_Backend_GetObj(o->i, name + p);
  132. }
  133. if (o->crh && (p = string_begins_with(name, "_ref."))) {
  134. return NCDModuleInst_Backend_GetObj(o->crh->i, name + p);
  135. }
  136. return NULL;
  137. }
  138. static void callrefhere_func_new (NCDModuleInst *i)
  139. {
  140. // allocate instance
  141. struct callrefhere_instance *o = malloc(sizeof(*o));
  142. if (!o) {
  143. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  144. goto fail0;
  145. }
  146. NCDModuleInst_Backend_SetUser(i, o);
  147. // set arguments
  148. o->i = i;
  149. // init calls list
  150. LinkedList0_Init(&o->calls_list);
  151. // signal up
  152. NCDModuleInst_Backend_Up(o->i);
  153. return;
  154. fail0:
  155. NCDModuleInst_Backend_SetError(i);
  156. NCDModuleInst_Backend_Dead(i);
  157. }
  158. static void callrefhere_func_die (void *vo)
  159. {
  160. struct callrefhere_instance *o = vo;
  161. NCDModuleInst *i = o->i;
  162. // disconnect calls
  163. while (!LinkedList0_IsEmpty(&o->calls_list)) {
  164. struct instance *inst = UPPER_OBJECT(LinkedList0_GetFirst(&o->calls_list), struct instance, calls_list_node);
  165. ASSERT(inst->crh == o)
  166. LinkedList0_Remove(&o->calls_list, &inst->calls_list_node);
  167. inst->crh = NULL;
  168. }
  169. // free instance
  170. free(o);
  171. NCDModuleInst_Backend_Dead(i);
  172. }
  173. static void func_new (NCDModuleInst *i)
  174. {
  175. // allocate instance
  176. struct instance *o = malloc(sizeof(*o));
  177. if (!o) {
  178. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  179. goto fail0;
  180. }
  181. NCDModuleInst_Backend_SetUser(i, o);
  182. // init arguments
  183. o->i = i;
  184. // check arguments
  185. NCDValue *template_name_arg;
  186. NCDValue *args_arg;
  187. if (!NCDValue_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) {
  188. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  189. goto fail1;
  190. }
  191. if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  192. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  193. goto fail1;
  194. }
  195. char *template_name = NCDValue_StringValue(template_name_arg);
  196. // calling none?
  197. if (!strcmp(template_name, "<none>")) {
  198. // signal up
  199. NCDModuleInst_Backend_Up(o->i);
  200. // set state none
  201. o->state = STATE_NONE;
  202. } else {
  203. // copy arguments
  204. NCDValue args;
  205. if (!NCDValue_InitCopy(&args, args_arg)) {
  206. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  207. goto fail1;
  208. }
  209. // create process
  210. if (!NCDModuleProcess_Init(&o->process, o->i, template_name, args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  211. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  212. NCDValue_Free(&args);
  213. goto fail1;
  214. }
  215. // set special functions
  216. NCDModuleProcess_SetSpecialFuncs(&o->process,
  217. (NCDModuleProcess_func_getspecialvar)process_func_getspecialvar,
  218. (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj);
  219. // set callrefhere
  220. o->crh = (o->i->method_object ? o->i->method_object->inst_user : NULL);
  221. // add to callrefhere's calls list
  222. if (o->crh) {
  223. LinkedList0_Prepend(&o->crh->calls_list, &o->calls_list_node);
  224. }
  225. // set state working
  226. o->state = STATE_WORKING;
  227. }
  228. return;
  229. fail1:
  230. free(o);
  231. fail0:
  232. NCDModuleInst_Backend_SetError(i);
  233. NCDModuleInst_Backend_Dead(i);
  234. }
  235. void instance_free (struct instance *o)
  236. {
  237. NCDModuleInst *i = o->i;
  238. if (o->state != STATE_NONE) {
  239. // remove from callrefhere's calls list
  240. if (o->crh) {
  241. LinkedList0_Remove(&o->crh->calls_list, &o->calls_list_node);
  242. }
  243. // free process
  244. NCDModuleProcess_Free(&o->process);
  245. }
  246. // free instance
  247. free(o);
  248. NCDModuleInst_Backend_Dead(i);
  249. }
  250. static void func_die (void *vo)
  251. {
  252. struct instance *o = vo;
  253. ASSERT(o->state != STATE_TERMINATING)
  254. // if none, die now
  255. if (o->state == STATE_NONE) {
  256. instance_free(o);
  257. return;
  258. }
  259. // request process to terminate
  260. NCDModuleProcess_Terminate(&o->process);
  261. // set state terminating
  262. o->state = STATE_TERMINATING;
  263. }
  264. static void func_clean (void *vo)
  265. {
  266. struct instance *o = vo;
  267. if (o->state != STATE_WAITING) {
  268. return;
  269. }
  270. // allow process to continue
  271. NCDModuleProcess_Continue(&o->process);
  272. // set state working
  273. o->state = STATE_WORKING;
  274. }
  275. static int func_getvar (void *vo, const char *name, NCDValue *out)
  276. {
  277. struct instance *o = vo;
  278. if (o->state == STATE_NONE) {
  279. return 0;
  280. }
  281. return NCDModuleProcess_GetVar(&o->process, name, out);
  282. }
  283. static NCDModuleInst * func_getobj (void *vo, const char *name)
  284. {
  285. struct instance *o = vo;
  286. if (o->state == STATE_NONE) {
  287. return NULL;
  288. }
  289. return NCDModuleProcess_GetObj(&o->process, name);
  290. }
  291. static const struct NCDModule modules[] = {
  292. {
  293. .type = "callrefhere",
  294. .func_new = callrefhere_func_new,
  295. .func_die = callrefhere_func_die
  296. }, {
  297. .type = "call",
  298. .func_new = func_new,
  299. .func_die = func_die,
  300. .func_clean = func_clean,
  301. .func_getvar = func_getvar,
  302. .func_getobj = func_getobj,
  303. .can_resolve_when_down = 1
  304. }, {
  305. .type = "callrefhere::call",
  306. .func_new = func_new,
  307. .func_die = func_die,
  308. .func_clean = func_clean,
  309. .func_getvar = func_getvar,
  310. .func_getobj = func_getobj,
  311. .can_resolve_when_down = 1
  312. }, {
  313. .type = NULL
  314. }
  315. };
  316. const struct NCDModuleGroup ncdmodule_call = {
  317. .modules = modules
  318. };