try.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * @file try.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. * try(string template_name, list args)
  33. *
  34. * Description:
  35. * Does the following:
  36. * 1. Starts a template process from the specified template and arguments.
  37. * 2. Waits for the process to initialize completely, or for a _try->assert()
  38. * assertion to fail.
  39. * 3. Initiates termination of the process and waits for it to terminate.
  40. * 4. Goes to up state. The "succeeded" variable reflects whether the process
  41. * managed to initialize, or an assertion failed.
  42. * If at any point during these steps termination of the try statement is
  43. * requested, requests the process to terminate (if not already), and dies
  44. * when it terminates.
  45. *
  46. * Variables:
  47. * string succeeded - "true" if the template process finished, "false" if assert
  48. * was called.
  49. *
  50. * Synopsis:
  51. * try.try::assert(string cond)
  52. *
  53. * Description:
  54. * Call as _try->assert() from the template process. If cond is "true",
  55. * does nothing. Else, initiates termination of the process (if not already),
  56. * and marks the try operation as not succeeded.
  57. */
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <ncd/NCDModule.h>
  61. #include <generated/blog_channel_ncd_try.h>
  62. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  63. struct instance {
  64. NCDModuleInst *i;
  65. NCDModuleProcess process;
  66. int state;
  67. int dying;
  68. int succeeded;
  69. };
  70. #define STATE_INIT 1
  71. #define STATE_DEINIT 2
  72. #define STATE_FINISHED 3
  73. static void process_handler_event (struct instance *o, int event);
  74. static int process_func_getspecialobj (struct instance *o, const char *name, NCDObject *out_object);
  75. static int process_caller_object_func_getobj (struct instance *o, const char *name, NCDObject *out_object);
  76. static void start_terminating (struct instance *o);
  77. static void instance_free (struct instance *o);
  78. static void process_handler_event (struct instance *o, int event)
  79. {
  80. switch (event) {
  81. case NCDMODULEPROCESS_EVENT_UP: {
  82. ASSERT(o->state == STATE_INIT)
  83. // start terminating
  84. start_terminating(o);
  85. } break;
  86. case NCDMODULEPROCESS_EVENT_DOWN: {
  87. ASSERT(o->state == STATE_INIT)
  88. // continue
  89. NCDModuleProcess_Continue(&o->process);
  90. } break;
  91. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  92. ASSERT(o->state == STATE_DEINIT)
  93. // free process
  94. NCDModuleProcess_Free(&o->process);
  95. // die finally if requested
  96. if (o->dying) {
  97. instance_free(o);
  98. return;
  99. }
  100. // signal up
  101. NCDModuleInst_Backend_Up(o->i);
  102. // set state finished
  103. o->state = STATE_FINISHED;
  104. } break;
  105. }
  106. }
  107. static int process_func_getspecialobj (struct instance *o, const char *name, NCDObject *out_object)
  108. {
  109. ASSERT(o->state == STATE_INIT || o->state == STATE_DEINIT)
  110. if (!strcmp(name, "_caller")) {
  111. *out_object = NCDObject_Build(NULL, o, NULL, (NCDObject_func_getobj)process_caller_object_func_getobj);
  112. return 1;
  113. }
  114. if (!strcmp(name, "_try")) {
  115. *out_object = NCDObject_Build("try.try", o, NULL, NULL);
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. static int process_caller_object_func_getobj (struct instance *o, const char *name, NCDObject *out_object)
  121. {
  122. ASSERT(o->state == STATE_INIT || o->state == STATE_DEINIT)
  123. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  124. }
  125. static void start_terminating (struct instance *o)
  126. {
  127. ASSERT(o->state == STATE_INIT)
  128. // request process termination
  129. NCDModuleProcess_Terminate(&o->process);
  130. // set state deinit
  131. o->state = STATE_DEINIT;
  132. }
  133. static void func_new (NCDModuleInst *i)
  134. {
  135. // allocate structure
  136. struct instance *o = malloc(sizeof(*o));
  137. if (!o) {
  138. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  139. goto fail0;
  140. }
  141. o->i = i;
  142. NCDModuleInst_Backend_SetUser(i, o);
  143. // check arguments
  144. NCDValue *template_name_arg;
  145. NCDValue *args_arg;
  146. if (!NCDValue_ListRead(i->args, 2, &template_name_arg, &args_arg)) {
  147. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  148. goto fail1;
  149. }
  150. if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  151. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  152. goto fail1;
  153. }
  154. char *template_name = NCDValue_StringValue(template_name_arg);
  155. // copy arguments
  156. NCDValue args;
  157. if (!NCDValue_InitCopy(&args, args_arg)) {
  158. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  159. goto fail1;
  160. }
  161. // start process
  162. if (!NCDModuleProcess_Init(&o->process, i, template_name, args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  163. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  164. NCDValue_Free(&args);
  165. goto fail1;
  166. }
  167. // set special object function
  168. NCDModuleProcess_SetSpecialFuncs(&o->process, (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj);
  169. // set state init, not dying, assume succeeded
  170. o->state = STATE_INIT;
  171. o->dying = 0;
  172. o->succeeded = 1;
  173. return;
  174. fail1:
  175. free(o);
  176. fail0:
  177. NCDModuleInst_Backend_SetError(i);
  178. NCDModuleInst_Backend_Dead(i);
  179. }
  180. static void instance_free (struct instance *o)
  181. {
  182. NCDModuleInst *i = o->i;
  183. // free structure
  184. free(o);
  185. NCDModuleInst_Backend_Dead(i);
  186. }
  187. static void func_die (void *vo)
  188. {
  189. struct instance *o = vo;
  190. ASSERT(!o->dying)
  191. // if we're finished, die immediately
  192. if (o->state == STATE_FINISHED) {
  193. instance_free(o);
  194. return;
  195. }
  196. // set dying
  197. o->dying = 1;
  198. // start terminating if not already
  199. if (o->state == STATE_INIT) {
  200. start_terminating(o);
  201. }
  202. }
  203. static int func_getvar (void *vo, const char *name, NCDValue *out)
  204. {
  205. struct instance *o = vo;
  206. ASSERT(o->state == STATE_FINISHED)
  207. ASSERT(!o->dying)
  208. if (!strcmp(name, "succeeded")) {
  209. const char *str = (o->succeeded ? "true" : "false");
  210. if (!NCDValue_InitString(out, str)) {
  211. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  212. return 0;
  213. }
  214. return 1;
  215. }
  216. return 0;
  217. }
  218. static void assert_func_new (NCDModuleInst *i)
  219. {
  220. // check arguments
  221. NCDValue *cond_arg;
  222. if (!NCDValue_ListRead(i->args, 1, &cond_arg)) {
  223. ModuleLog(i, BLOG_ERROR, "wrong arity");
  224. goto fail1;
  225. }
  226. if (NCDValue_Type(cond_arg) != NCDVALUE_STRING) {
  227. ModuleLog(i, BLOG_ERROR, "wrong type");
  228. goto fail1;
  229. }
  230. char *cond = NCDValue_StringValue(cond_arg);
  231. // get instance
  232. struct instance *mo = i->method_user;
  233. ASSERT(mo->state == STATE_INIT || mo->state == STATE_DEINIT)
  234. // signal up
  235. NCDModuleInst_Backend_Up(i);
  236. if (strcmp(cond, "true")) {
  237. // mark not succeeded
  238. mo->succeeded = 0;
  239. // start terminating if not already
  240. if (mo->state == STATE_INIT) {
  241. start_terminating(mo);
  242. }
  243. }
  244. return;
  245. fail1:
  246. NCDModuleInst_Backend_SetError(i);
  247. NCDModuleInst_Backend_Dead(i);
  248. }
  249. static const struct NCDModule modules[] = {
  250. {
  251. .type = "try",
  252. .func_new = func_new,
  253. .func_die = func_die,
  254. .func_getvar = func_getvar
  255. }, {
  256. .type = "try.try::assert",
  257. .func_new = assert_func_new
  258. }, {
  259. .type = NULL
  260. }
  261. };
  262. const struct NCDModuleGroup ncdmodule_try = {
  263. .modules = modules
  264. };