try.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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, NCD_string_id_t name, NCDObject *out_object);
  75. static int process_caller_object_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object);
  76. static void start_terminating (struct instance *o);
  77. static void instance_free (struct instance *o);
  78. enum {STRING_CALLER, STRING_TRY, STRING_TRY_TRY, STRING_SUCCEEDED};
  79. static struct NCD_string_request strings[] = {
  80. {"_caller"}, {"_try"}, {"try.try"}, {"succeeded"}, {NULL}
  81. };
  82. static void process_handler_event (struct instance *o, int event)
  83. {
  84. switch (event) {
  85. case NCDMODULEPROCESS_EVENT_UP: {
  86. ASSERT(o->state == STATE_INIT)
  87. // start terminating
  88. start_terminating(o);
  89. } break;
  90. case NCDMODULEPROCESS_EVENT_DOWN: {
  91. ASSERT(o->state == STATE_INIT)
  92. // continue
  93. NCDModuleProcess_Continue(&o->process);
  94. } break;
  95. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  96. ASSERT(o->state == STATE_DEINIT)
  97. // free process
  98. NCDModuleProcess_Free(&o->process);
  99. // die finally if requested
  100. if (o->dying) {
  101. instance_free(o);
  102. return;
  103. }
  104. // signal up
  105. NCDModuleInst_Backend_Up(o->i);
  106. // set state finished
  107. o->state = STATE_FINISHED;
  108. } break;
  109. }
  110. }
  111. static int process_func_getspecialobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
  112. {
  113. ASSERT(o->state == STATE_INIT || o->state == STATE_DEINIT)
  114. if (name == strings[STRING_CALLER].id) {
  115. *out_object = NCDObject_Build(-1, o, NULL, (NCDObject_func_getobj)process_caller_object_func_getobj);
  116. return 1;
  117. }
  118. if (name == strings[STRING_TRY].id) {
  119. *out_object = NCDObject_Build(strings[STRING_TRY_TRY].id, o, NULL, NULL);
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. static int process_caller_object_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
  125. {
  126. ASSERT(o->state == STATE_INIT || o->state == STATE_DEINIT)
  127. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  128. }
  129. static void start_terminating (struct instance *o)
  130. {
  131. ASSERT(o->state == STATE_INIT)
  132. // request process termination
  133. NCDModuleProcess_Terminate(&o->process);
  134. // set state deinit
  135. o->state = STATE_DEINIT;
  136. }
  137. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  138. {
  139. struct instance *o = vo;
  140. o->i = i;
  141. // check arguments
  142. NCDValRef template_name_arg;
  143. NCDValRef args_arg;
  144. if (!NCDVal_ListRead(params->args, 2, &template_name_arg, &args_arg)) {
  145. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  146. goto fail0;
  147. }
  148. if (!NCDVal_IsString(template_name_arg) || !NCDVal_IsList(args_arg)) {
  149. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  150. goto fail0;
  151. }
  152. // start process
  153. if (!NCDModuleProcess_InitValue(&o->process, i, template_name_arg, args_arg, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  154. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  155. goto fail0;
  156. }
  157. // set special object function
  158. NCDModuleProcess_SetSpecialFuncs(&o->process, (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj);
  159. // set state init, not dying, assume succeeded
  160. o->state = STATE_INIT;
  161. o->dying = 0;
  162. o->succeeded = 1;
  163. return;
  164. fail0:
  165. NCDModuleInst_Backend_SetError(i);
  166. NCDModuleInst_Backend_Dead(i);
  167. }
  168. static void instance_free (struct instance *o)
  169. {
  170. NCDModuleInst_Backend_Dead(o->i);
  171. }
  172. static void func_die (void *vo)
  173. {
  174. struct instance *o = vo;
  175. ASSERT(!o->dying)
  176. // if we're finished, die immediately
  177. if (o->state == STATE_FINISHED) {
  178. instance_free(o);
  179. return;
  180. }
  181. // set dying
  182. o->dying = 1;
  183. // start terminating if not already
  184. if (o->state == STATE_INIT) {
  185. start_terminating(o);
  186. }
  187. }
  188. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  189. {
  190. struct instance *o = vo;
  191. ASSERT(o->state == STATE_FINISHED)
  192. ASSERT(!o->dying)
  193. if (name == strings[STRING_SUCCEEDED].id) {
  194. const char *str = (o->succeeded ? "true" : "false");
  195. *out = NCDVal_NewString(mem, str);
  196. if (NCDVal_IsInvalid(*out)) {
  197. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  198. }
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. static void assert_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  204. {
  205. // check arguments
  206. NCDValRef cond_arg;
  207. if (!NCDVal_ListRead(params->args, 1, &cond_arg)) {
  208. ModuleLog(i, BLOG_ERROR, "wrong arity");
  209. goto fail1;
  210. }
  211. if (!NCDVal_IsString(cond_arg)) {
  212. ModuleLog(i, BLOG_ERROR, "wrong type");
  213. goto fail1;
  214. }
  215. // get instance
  216. struct instance *mo = params->method_user;
  217. ASSERT(mo->state == STATE_INIT || mo->state == STATE_DEINIT)
  218. // signal up
  219. NCDModuleInst_Backend_Up(i);
  220. if (!NCDVal_StringEquals(cond_arg, "true")) {
  221. // mark not succeeded
  222. mo->succeeded = 0;
  223. // start terminating if not already
  224. if (mo->state == STATE_INIT) {
  225. start_terminating(mo);
  226. }
  227. }
  228. return;
  229. fail1:
  230. NCDModuleInst_Backend_SetError(i);
  231. NCDModuleInst_Backend_Dead(i);
  232. }
  233. static struct NCDModule modules[] = {
  234. {
  235. .type = "try",
  236. .func_new2 = func_new,
  237. .func_die = func_die,
  238. .func_getvar2 = func_getvar2,
  239. .alloc_size = sizeof(struct instance)
  240. }, {
  241. .type = "try.try::assert",
  242. .func_new2 = assert_func_new
  243. }, {
  244. .type = NULL
  245. }
  246. };
  247. const struct NCDModuleGroup ncdmodule_try = {
  248. .modules = modules,
  249. .strings = strings
  250. };