try.c 9.1 KB

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