foreach.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /**
  2. * @file foreach.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. * foreach(list list, string template, list args)
  33. *
  34. * Description:
  35. * Initializes a template process for each element of list, sequentially,
  36. * obeying to the usual execution model of NCD.
  37. * It's equivalent to (except for special variables):
  38. *
  39. * call(template, args);
  40. * ...
  41. * call(template, args); # one call() for every element of list
  42. *
  43. * Template process specials:
  44. *
  45. * _index - index of the list element corresponding to the template process,
  46. * as a decimal string, starting from zero
  47. * _elem - element of list corresponding to the template process
  48. * _caller.X - X as seen from the foreach() statement
  49. */
  50. #include <stdlib.h>
  51. #include <misc/balloc.h>
  52. #include <misc/string_begins_with.h>
  53. #include <system/BReactor.h>
  54. #include <ncd/NCDModule.h>
  55. #include <generated/blog_channel_ncd_foreach.h>
  56. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  57. #define ISTATE_WORKING 1
  58. #define ISTATE_UP 2
  59. #define ISTATE_WAITING 3
  60. #define ISTATE_TERMINATING 4
  61. #define ESTATE_FORGOTTEN 1
  62. #define ESTATE_DOWN 2
  63. #define ESTATE_UP 3
  64. #define ESTATE_WAITING 4
  65. #define ESTATE_TERMINATING 5
  66. struct element;
  67. struct instance {
  68. NCDModuleInst *i;
  69. const char *template_name;
  70. NCDValRef args;
  71. BTimer timer;
  72. size_t num_elems;
  73. struct element *elems;
  74. size_t gp; // good pointer
  75. size_t ip; // initialized pointer
  76. int state;
  77. };
  78. struct element {
  79. struct instance *inst;
  80. size_t i;
  81. NCDValRef value;
  82. NCDModuleProcess process;
  83. int state;
  84. };
  85. static void assert_state (struct instance *o);
  86. static void work (struct instance *o);
  87. static void advance (struct instance *o);
  88. static void timer_handler (struct instance *o);
  89. static void element_process_handler_event (struct element *e, int event);
  90. static int element_process_func_getspecialobj (struct element *e, const char *name, NCDObject *out_object);
  91. static int element_caller_object_func_getobj (struct element *e, const char *name, NCDObject *out_object);
  92. static int element_index_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out);
  93. static int element_elem_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out);
  94. static void instance_free (struct instance *o);
  95. static void assert_state (struct instance *o)
  96. {
  97. ASSERT(o->gp <= o->num_elems)
  98. ASSERT(o->ip <= o->num_elems)
  99. ASSERT(o->gp <= o->ip)
  100. #ifndef NDEBUG
  101. // check GP
  102. for (size_t i = 0; i < o->gp; i++) {
  103. if (i == o->gp - 1) {
  104. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  105. o->elems[i].state == ESTATE_WAITING)
  106. } else {
  107. ASSERT(o->elems[i].state == ESTATE_UP)
  108. }
  109. }
  110. // check IP
  111. size_t ip = o->num_elems;
  112. while (ip > 0 && o->elems[ip - 1].state == ESTATE_FORGOTTEN) {
  113. ip--;
  114. }
  115. ASSERT(o->ip == ip)
  116. // check gap
  117. for (size_t i = o->gp; i < o->ip; i++) {
  118. if (i == o->ip - 1) {
  119. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  120. o->elems[i].state == ESTATE_WAITING || o->elems[i].state == ESTATE_TERMINATING)
  121. } else {
  122. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  123. o->elems[i].state == ESTATE_WAITING)
  124. }
  125. }
  126. #endif
  127. }
  128. static void work (struct instance *o)
  129. {
  130. assert_state(o);
  131. // stop timer
  132. BReactor_RemoveTimer(o->i->iparams->reactor, &o->timer);
  133. if (o->state == ISTATE_WAITING) {
  134. return;
  135. }
  136. if (o->state == ISTATE_UP && !(o->gp == o->ip && o->gp == o->num_elems && (o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP))) {
  137. // signal down
  138. NCDModuleInst_Backend_Down(o->i);
  139. // set state waiting
  140. o->state = ISTATE_WAITING;
  141. return;
  142. }
  143. if (o->gp < o->ip) {
  144. // get last element
  145. struct element *le = &o->elems[o->ip - 1];
  146. ASSERT(le->state != ESTATE_FORGOTTEN)
  147. // start terminating if not already
  148. if (le->state != ESTATE_TERMINATING) {
  149. // request termination
  150. NCDModuleProcess_Terminate(&le->process);
  151. // set element state terminating
  152. le->state = ESTATE_TERMINATING;
  153. }
  154. return;
  155. }
  156. if (o->state == ISTATE_TERMINATING) {
  157. // finally die
  158. instance_free(o);
  159. return;
  160. }
  161. if (o->gp == o->num_elems && (o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)) {
  162. if (o->state == ISTATE_WORKING) {
  163. // signal up
  164. NCDModuleInst_Backend_Up(o->i);
  165. // set state up
  166. o->state = ISTATE_UP;
  167. }
  168. return;
  169. }
  170. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_WAITING) {
  171. // get last element
  172. struct element *le = &o->elems[o->gp - 1];
  173. // continue process
  174. NCDModuleProcess_Continue(&le->process);
  175. // set state down
  176. le->state = ESTATE_DOWN;
  177. return;
  178. }
  179. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_DOWN) {
  180. return;
  181. }
  182. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  183. advance(o);
  184. return;
  185. }
  186. static void advance (struct instance *o)
  187. {
  188. assert_state(o);
  189. ASSERT(o->gp == o->ip)
  190. ASSERT(o->gp < o->num_elems)
  191. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  192. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  193. // get next element
  194. struct element *e = &o->elems[o->gp];
  195. // init process
  196. if (!NCDModuleProcess_Init(&e->process, o->i, o->template_name, o->args, e, (NCDModuleProcess_handler_event)element_process_handler_event)) {
  197. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  198. goto fail;
  199. }
  200. // set special functions
  201. NCDModuleProcess_SetSpecialFuncs(&e->process, (NCDModuleProcess_func_getspecialobj)element_process_func_getspecialobj);
  202. // set element state down
  203. e->state = ESTATE_DOWN;
  204. // increment GP and IP
  205. o->gp++;
  206. o->ip++;
  207. return;
  208. fail:
  209. // set timer
  210. BReactor_SetTimer(o->i->iparams->reactor, &o->timer);
  211. }
  212. static void timer_handler (struct instance *o)
  213. {
  214. assert_state(o);
  215. ASSERT(o->gp == o->ip)
  216. ASSERT(o->gp < o->num_elems)
  217. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  218. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  219. advance(o);
  220. return;
  221. }
  222. static void element_process_handler_event (struct element *e, int event)
  223. {
  224. struct instance *o = e->inst;
  225. assert_state(o);
  226. ASSERT(e->i < o->ip)
  227. ASSERT(e->state != ESTATE_FORGOTTEN)
  228. switch (event) {
  229. case NCDMODULEPROCESS_EVENT_UP: {
  230. ASSERT(e->state == ESTATE_DOWN)
  231. ASSERT(o->gp == o->ip)
  232. ASSERT(o->gp == e->i + 1)
  233. // set element state up
  234. e->state = ESTATE_UP;
  235. } break;
  236. case NCDMODULEPROCESS_EVENT_DOWN: {
  237. ASSERT(e->state == ESTATE_UP)
  238. // set element state waiting
  239. e->state = ESTATE_WAITING;
  240. // bump down GP
  241. if (o->gp > e->i + 1) {
  242. o->gp = e->i + 1;
  243. }
  244. } break;
  245. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  246. ASSERT(e->state == ESTATE_TERMINATING)
  247. ASSERT(o->gp < o->ip)
  248. ASSERT(o->ip == e->i + 1)
  249. // free process
  250. NCDModuleProcess_Free(&e->process);
  251. // set element state forgotten
  252. e->state = ESTATE_FORGOTTEN;
  253. // decrement IP
  254. o->ip--;
  255. } break;
  256. default: ASSERT(0);
  257. }
  258. work(o);
  259. return;
  260. }
  261. static int element_process_func_getspecialobj (struct element *e, const char *name, NCDObject *out_object)
  262. {
  263. struct instance *o = e->inst;
  264. ASSERT(e->state != ESTATE_FORGOTTEN)
  265. if (!strcmp(name, "_caller")) {
  266. *out_object = NCDObject_Build(NULL, e, NULL, (NCDObject_func_getobj)element_caller_object_func_getobj);
  267. return 1;
  268. }
  269. if (!strcmp(name, "_index")) {
  270. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_index_object_func_getvar, NULL);
  271. return 1;
  272. }
  273. if (!strcmp(name, "_elem")) {
  274. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_elem_object_func_getvar, NULL);
  275. return 1;
  276. }
  277. return 0;
  278. }
  279. static int element_caller_object_func_getobj (struct element *e, const char *name, NCDObject *out_object)
  280. {
  281. struct instance *o = e->inst;
  282. ASSERT(e->state != ESTATE_FORGOTTEN)
  283. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  284. }
  285. static int element_index_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out)
  286. {
  287. struct instance *o = e->inst;
  288. ASSERT(e->state != ESTATE_FORGOTTEN)
  289. if (strcmp(name, "")) {
  290. return 0;
  291. }
  292. char str[64];
  293. snprintf(str, sizeof(str), "%zu", e->i);
  294. *out = NCDVal_NewString(mem, str);
  295. if (NCDVal_IsInvalid(*out)) {
  296. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  297. }
  298. return 1;
  299. }
  300. static int element_elem_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out)
  301. {
  302. struct instance *o = e->inst;
  303. ASSERT(e->state != ESTATE_FORGOTTEN)
  304. if (strcmp(name, "")) {
  305. return 0;
  306. }
  307. *out = NCDVal_NewCopy(mem, e->value);
  308. if (NCDVal_IsInvalid(*out)) {
  309. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  310. }
  311. return 1;
  312. }
  313. static void func_new (NCDModuleInst *i)
  314. {
  315. // allocate instance
  316. struct instance *o = malloc(sizeof(*o));
  317. if (!o) {
  318. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  319. goto fail0;
  320. }
  321. NCDModuleInst_Backend_SetUser(i, o);
  322. // init arguments
  323. o->i = i;
  324. // read arguments
  325. NCDValRef arg_list;
  326. NCDValRef arg_template;
  327. NCDValRef arg_args;
  328. if (!NCDVal_ListRead(i->args, 3, &arg_list, &arg_template, &arg_args)) {
  329. ModuleLog(i, BLOG_ERROR, "wrong arity");
  330. goto fail1;
  331. }
  332. if (!NCDVal_IsList(arg_list) || !NCDVal_IsStringNoNulls(arg_template) ||
  333. !NCDVal_IsList(arg_args)
  334. ) {
  335. ModuleLog(i, BLOG_ERROR, "wrong type");
  336. goto fail1;
  337. }
  338. o->template_name = NCDVal_StringValue(arg_template);
  339. o->args = arg_args;
  340. // init timer
  341. btime_t retry_time = NCDModuleInst_Backend_InterpGetRetryTime(i);
  342. BTimer_Init(&o->timer, retry_time, (BTimer_handler)timer_handler, o);
  343. // count elements
  344. o->num_elems = NCDVal_ListCount(arg_list);
  345. // allocate elements
  346. if (!(o->elems = BAllocArray(o->num_elems, sizeof(o->elems[0])))) {
  347. ModuleLog(i, BLOG_ERROR, "BAllocArray failed");
  348. goto fail1;
  349. }
  350. for (size_t j = 0; j < o->num_elems; j++) {
  351. struct element *e = &o->elems[j];
  352. NCDValRef ev = NCDVal_ListGet(arg_list, j);
  353. // set instance
  354. e->inst = o;
  355. // set index
  356. e->i = j;
  357. // set value
  358. e->value = ev;
  359. // set state forgotten
  360. e->state = ESTATE_FORGOTTEN;
  361. }
  362. // set GP and IP zero
  363. o->gp = 0;
  364. o->ip = 0;
  365. // set state working
  366. o->state = ISTATE_WORKING;
  367. work(o);
  368. return;
  369. fail1:
  370. free(o);
  371. fail0:
  372. NCDModuleInst_Backend_SetError(i);
  373. NCDModuleInst_Backend_Dead(i);
  374. }
  375. static void instance_free (struct instance *o)
  376. {
  377. NCDModuleInst *i = o->i;
  378. ASSERT(o->gp == 0)
  379. ASSERT(o->ip == 0)
  380. // free elements
  381. BFree(o->elems);
  382. // free timer
  383. BReactor_RemoveTimer(o->i->iparams->reactor, &o->timer);
  384. // free instance
  385. free(o);
  386. NCDModuleInst_Backend_Dead(i);
  387. }
  388. static void func_die (void *vo)
  389. {
  390. struct instance *o = vo;
  391. assert_state(o);
  392. ASSERT(o->state != ISTATE_TERMINATING)
  393. // set GP zero
  394. o->gp = 0;
  395. // set state terminating
  396. o->state = ISTATE_TERMINATING;
  397. work(o);
  398. return;
  399. }
  400. static void func_clean (void *vo)
  401. {
  402. struct instance *o = vo;
  403. if (o->state != ISTATE_WAITING) {
  404. return;
  405. }
  406. // set state working
  407. o->state = ISTATE_WORKING;
  408. work(o);
  409. return;
  410. }
  411. static const struct NCDModule modules[] = {
  412. {
  413. .type = "foreach",
  414. .func_new = func_new,
  415. .func_die = func_die,
  416. .func_clean = func_clean
  417. }, {
  418. .type = NULL
  419. }
  420. };
  421. const struct NCDModuleGroup ncdmodule_foreach = {
  422. .modules = modules
  423. };