foreach.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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/map collection, 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 - (lists only) index of the list element corresponding to the template,
  46. * process, as a decimal string, starting from zero
  47. * _elem - (lists only) element of list corresponding to the template process
  48. * _key - (maps only) key of the current map entry
  49. * _val - (maps only) value of the current map entry
  50. * _caller.X - X as seen from the foreach() statement
  51. *
  52. * Synopsis:
  53. * foreach_emb(list/map collection, string template, string name1 [, string name2])
  54. *
  55. * Description:
  56. * Foreach for embedded templates; the desugaring process converts Foreach
  57. * clauses into this statement. The called templates have direct access to
  58. * objects as seen from this statement, and also some kind of access to the
  59. * current element of the iteration, depending on the type of collection
  60. * being iterated, and whether 'name2' is provided:
  61. * List and one name: current element is named 'name1'.
  62. * List and both names: current index is named 'name1', current element 'name2'.
  63. * Map and one name: current key is named 'name1'.
  64. * Map and both names: current key is named 'name1', current value 'name2'.
  65. */
  66. #include <stdlib.h>
  67. #include <string.h>
  68. #include <limits.h>
  69. #include <misc/balloc.h>
  70. #include <misc/string_begins_with.h>
  71. #include <misc/debug.h>
  72. #include <system/BReactor.h>
  73. #include <ncd/NCDModule.h>
  74. #include <generated/blog_channel_ncd_foreach.h>
  75. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  76. #define ISTATE_WORKING 1
  77. #define ISTATE_UP 2
  78. #define ISTATE_WAITING 3
  79. #define ISTATE_TERMINATING 4
  80. #define ESTATE_FORGOTTEN 1
  81. #define ESTATE_DOWN 2
  82. #define ESTATE_UP 3
  83. #define ESTATE_WAITING 4
  84. #define ESTATE_TERMINATING 5
  85. struct element;
  86. struct instance {
  87. NCDModuleInst *i;
  88. const char *template_name;
  89. NCDValRef args;
  90. const char *name1;
  91. const char *name2;
  92. BTimer timer;
  93. struct element *elems;
  94. int type;
  95. int num_elems;
  96. int gp; // good pointer
  97. int ip; // initialized pointer
  98. int state;
  99. };
  100. struct element {
  101. struct instance *inst;
  102. union {
  103. struct {
  104. NCDValRef list_elem;
  105. };
  106. struct {
  107. NCDValRef map_key;
  108. NCDValRef map_val;
  109. };
  110. };
  111. NCDModuleProcess process;
  112. int i;
  113. int state;
  114. };
  115. static void assert_state (struct instance *o);
  116. static void work (struct instance *o);
  117. static void advance (struct instance *o);
  118. static void timer_handler (struct instance *o);
  119. static void element_process_handler_event (struct element *e, int event);
  120. static int element_process_func_getspecialobj (struct element *e, const char *name, NCDObject *out_object);
  121. static int element_caller_object_func_getobj (struct element *e, const char *name, NCDObject *out_object);
  122. static int element_list_index_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out);
  123. static int element_list_elem_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out);
  124. static int element_map_key_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out);
  125. static int element_map_val_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out);
  126. static void instance_free (struct instance *o);
  127. static void assert_state (struct instance *o)
  128. {
  129. ASSERT(o->num_elems >= 0)
  130. ASSERT(o->gp >= 0)
  131. ASSERT(o->ip >= 0)
  132. ASSERT(o->gp <= o->num_elems)
  133. ASSERT(o->ip <= o->num_elems)
  134. ASSERT(o->gp <= o->ip)
  135. #ifndef NDEBUG
  136. // check GP
  137. for (int i = 0; i < o->gp; i++) {
  138. if (i == o->gp - 1) {
  139. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  140. o->elems[i].state == ESTATE_WAITING)
  141. } else {
  142. ASSERT(o->elems[i].state == ESTATE_UP)
  143. }
  144. }
  145. // check IP
  146. int ip = o->num_elems;
  147. while (ip > 0 && o->elems[ip - 1].state == ESTATE_FORGOTTEN) {
  148. ip--;
  149. }
  150. ASSERT(o->ip == ip)
  151. // check gap
  152. for (int i = o->gp; i < o->ip; i++) {
  153. if (i == o->ip - 1) {
  154. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  155. o->elems[i].state == ESTATE_WAITING || o->elems[i].state == ESTATE_TERMINATING)
  156. } else {
  157. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  158. o->elems[i].state == ESTATE_WAITING)
  159. }
  160. }
  161. #endif
  162. }
  163. static void work (struct instance *o)
  164. {
  165. assert_state(o);
  166. // stop timer
  167. BReactor_RemoveTimer(o->i->iparams->reactor, &o->timer);
  168. if (o->state == ISTATE_WAITING) {
  169. return;
  170. }
  171. 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))) {
  172. // signal down
  173. NCDModuleInst_Backend_Down(o->i);
  174. // set state waiting
  175. o->state = ISTATE_WAITING;
  176. return;
  177. }
  178. if (o->gp < o->ip) {
  179. // get last element
  180. struct element *le = &o->elems[o->ip - 1];
  181. ASSERT(le->state != ESTATE_FORGOTTEN)
  182. // start terminating if not already
  183. if (le->state != ESTATE_TERMINATING) {
  184. // request termination
  185. NCDModuleProcess_Terminate(&le->process);
  186. // set element state terminating
  187. le->state = ESTATE_TERMINATING;
  188. }
  189. return;
  190. }
  191. if (o->state == ISTATE_TERMINATING) {
  192. // finally die
  193. instance_free(o);
  194. return;
  195. }
  196. if (o->gp == o->num_elems && (o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)) {
  197. if (o->state == ISTATE_WORKING) {
  198. // signal up
  199. NCDModuleInst_Backend_Up(o->i);
  200. // set state up
  201. o->state = ISTATE_UP;
  202. }
  203. return;
  204. }
  205. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_WAITING) {
  206. // get last element
  207. struct element *le = &o->elems[o->gp - 1];
  208. // continue process
  209. NCDModuleProcess_Continue(&le->process);
  210. // set state down
  211. le->state = ESTATE_DOWN;
  212. return;
  213. }
  214. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_DOWN) {
  215. return;
  216. }
  217. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  218. advance(o);
  219. return;
  220. }
  221. static void advance (struct instance *o)
  222. {
  223. assert_state(o);
  224. ASSERT(o->gp == o->ip)
  225. ASSERT(o->gp < o->num_elems)
  226. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  227. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  228. // get next element
  229. struct element *e = &o->elems[o->gp];
  230. // init process
  231. if (!NCDModuleProcess_Init(&e->process, o->i, o->template_name, o->args, e, (NCDModuleProcess_handler_event)element_process_handler_event)) {
  232. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  233. goto fail;
  234. }
  235. // set special functions
  236. NCDModuleProcess_SetSpecialFuncs(&e->process, (NCDModuleProcess_func_getspecialobj)element_process_func_getspecialobj);
  237. // set element state down
  238. e->state = ESTATE_DOWN;
  239. // increment GP and IP
  240. o->gp++;
  241. o->ip++;
  242. return;
  243. fail:
  244. // set timer
  245. BReactor_SetTimer(o->i->iparams->reactor, &o->timer);
  246. }
  247. static void timer_handler (struct instance *o)
  248. {
  249. assert_state(o);
  250. ASSERT(o->gp == o->ip)
  251. ASSERT(o->gp < o->num_elems)
  252. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  253. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  254. advance(o);
  255. return;
  256. }
  257. static void element_process_handler_event (struct element *e, int event)
  258. {
  259. struct instance *o = e->inst;
  260. assert_state(o);
  261. ASSERT(e->i < o->ip)
  262. ASSERT(e->state != ESTATE_FORGOTTEN)
  263. switch (event) {
  264. case NCDMODULEPROCESS_EVENT_UP: {
  265. ASSERT(e->state == ESTATE_DOWN)
  266. ASSERT(o->gp == o->ip)
  267. ASSERT(o->gp == e->i + 1)
  268. // set element state up
  269. e->state = ESTATE_UP;
  270. } break;
  271. case NCDMODULEPROCESS_EVENT_DOWN: {
  272. ASSERT(e->state == ESTATE_UP)
  273. // set element state waiting
  274. e->state = ESTATE_WAITING;
  275. // bump down GP
  276. if (o->gp > e->i + 1) {
  277. o->gp = e->i + 1;
  278. }
  279. } break;
  280. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  281. ASSERT(e->state == ESTATE_TERMINATING)
  282. ASSERT(o->gp < o->ip)
  283. ASSERT(o->ip == e->i + 1)
  284. // free process
  285. NCDModuleProcess_Free(&e->process);
  286. // set element state forgotten
  287. e->state = ESTATE_FORGOTTEN;
  288. // decrement IP
  289. o->ip--;
  290. } break;
  291. default: ASSERT(0);
  292. }
  293. work(o);
  294. return;
  295. }
  296. static int element_process_func_getspecialobj (struct element *e, const char *name, NCDObject *out_object)
  297. {
  298. struct instance *o = e->inst;
  299. ASSERT(e->state != ESTATE_FORGOTTEN)
  300. switch (o->type) {
  301. case NCDVAL_LIST: {
  302. const char *index_name = (o->name2 ? o->name1 : NULL);
  303. const char *elem_name = (o->name2 ? o->name2 : o->name1);
  304. if (index_name && !strcmp(name, index_name)) {
  305. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_list_index_object_func_getvar, NULL);
  306. return 1;
  307. }
  308. if (!strcmp(name, elem_name)) {
  309. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_list_elem_object_func_getvar, NULL);
  310. return 1;
  311. }
  312. } break;
  313. case NCDVAL_MAP: {
  314. const char *key_name = o->name1;
  315. const char *val_name = o->name2;
  316. if (!strcmp(name, key_name)) {
  317. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_map_key_object_func_getvar, NULL);
  318. return 1;
  319. }
  320. if (val_name && !strcmp(name, val_name)) {
  321. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_map_val_object_func_getvar, NULL);
  322. return 1;
  323. }
  324. } break;
  325. }
  326. if (NCDVal_IsInvalid(o->args)) {
  327. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  328. }
  329. if (!strcmp(name, "_caller")) {
  330. *out_object = NCDObject_Build(NULL, e, NULL, (NCDObject_func_getobj)element_caller_object_func_getobj);
  331. return 1;
  332. }
  333. return 0;
  334. }
  335. static int element_caller_object_func_getobj (struct element *e, const char *name, NCDObject *out_object)
  336. {
  337. struct instance *o = e->inst;
  338. ASSERT(e->state != ESTATE_FORGOTTEN)
  339. ASSERT(!NCDVal_IsInvalid(o->args))
  340. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  341. }
  342. static int element_list_index_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out)
  343. {
  344. struct instance *o = e->inst;
  345. ASSERT(e->state != ESTATE_FORGOTTEN)
  346. ASSERT(o->type == NCDVAL_LIST)
  347. if (strcmp(name, "")) {
  348. return 0;
  349. }
  350. char str[64];
  351. snprintf(str, sizeof(str), "%d", e->i);
  352. *out = NCDVal_NewString(mem, str);
  353. if (NCDVal_IsInvalid(*out)) {
  354. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  355. }
  356. return 1;
  357. }
  358. static int element_list_elem_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out)
  359. {
  360. struct instance *o = e->inst;
  361. ASSERT(e->state != ESTATE_FORGOTTEN)
  362. ASSERT(o->type == NCDVAL_LIST)
  363. if (strcmp(name, "")) {
  364. return 0;
  365. }
  366. *out = NCDVal_NewCopy(mem, e->list_elem);
  367. if (NCDVal_IsInvalid(*out)) {
  368. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  369. }
  370. return 1;
  371. }
  372. static int element_map_key_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out)
  373. {
  374. struct instance *o = e->inst;
  375. ASSERT(e->state != ESTATE_FORGOTTEN)
  376. ASSERT(o->type == NCDVAL_MAP)
  377. if (strcmp(name, "")) {
  378. return 0;
  379. }
  380. *out = NCDVal_NewCopy(mem, e->map_key);
  381. if (NCDVal_IsInvalid(*out)) {
  382. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  383. }
  384. return 1;
  385. }
  386. static int element_map_val_object_func_getvar (struct element *e, const char *name, NCDValMem *mem, NCDValRef *out)
  387. {
  388. struct instance *o = e->inst;
  389. ASSERT(e->state != ESTATE_FORGOTTEN)
  390. ASSERT(o->type == NCDVAL_MAP)
  391. if (strcmp(name, "")) {
  392. return 0;
  393. }
  394. *out = NCDVal_NewCopy(mem, e->map_val);
  395. if (NCDVal_IsInvalid(*out)) {
  396. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  397. }
  398. return 1;
  399. }
  400. static void func_new_common (void *vo, NCDModuleInst *i, NCDValRef collection, const char *template_name, NCDValRef args, const char *name1, const char *name2)
  401. {
  402. ASSERT(!NCDVal_IsInvalid(collection))
  403. ASSERT(template_name)
  404. ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
  405. ASSERT(name1)
  406. struct instance *o = vo;
  407. o->i = i;
  408. o->type = NCDVal_Type(collection);
  409. o->template_name = template_name;
  410. o->args = args;
  411. o->name1 = name1;
  412. o->name2 = name2;
  413. // init timer
  414. btime_t retry_time = NCDModuleInst_Backend_InterpGetRetryTime(i);
  415. BTimer_Init(&o->timer, retry_time, (BTimer_handler)timer_handler, o);
  416. size_t num_elems;
  417. NCDValMapElem cur_map_elem;
  418. switch (o->type) {
  419. case NCDVAL_LIST: {
  420. num_elems = NCDVal_ListCount(collection);
  421. } break;
  422. case NCDVAL_MAP: {
  423. num_elems = NCDVal_MapCount(collection);
  424. cur_map_elem = NCDVal_MapOrderedFirst(collection);
  425. } break;
  426. default:
  427. ModuleLog(i, BLOG_ERROR, "invalid collection type");
  428. goto fail0;
  429. }
  430. if (num_elems > INT_MAX) {
  431. ModuleLog(i, BLOG_ERROR, "too many elements");
  432. goto fail0;
  433. }
  434. o->num_elems = num_elems;
  435. // allocate elements
  436. if (!(o->elems = BAllocArray(o->num_elems, sizeof(o->elems[0])))) {
  437. ModuleLog(i, BLOG_ERROR, "BAllocArray failed");
  438. goto fail0;
  439. }
  440. for (int j = 0; j < o->num_elems; j++) {
  441. struct element *e = &o->elems[j];
  442. // set instance
  443. e->inst = o;
  444. // set index
  445. e->i = j;
  446. // set state forgotten
  447. e->state = ESTATE_FORGOTTEN;
  448. // set values
  449. switch (o->type) {
  450. case NCDVAL_LIST: {
  451. e->list_elem = NCDVal_ListGet(collection, j);
  452. } break;
  453. case NCDVAL_MAP: {
  454. e->map_key = NCDVal_MapElemKey(collection, cur_map_elem);
  455. e->map_val = NCDVal_MapElemVal(collection, cur_map_elem);
  456. cur_map_elem = NCDVal_MapOrderedNext(collection, cur_map_elem);
  457. } break;
  458. }
  459. }
  460. // set GP and IP zero
  461. o->gp = 0;
  462. o->ip = 0;
  463. // set state working
  464. o->state = ISTATE_WORKING;
  465. work(o);
  466. return;
  467. fail0:
  468. NCDModuleInst_Backend_SetError(i);
  469. NCDModuleInst_Backend_Dead(i);
  470. }
  471. static void func_new_foreach (void *vo, NCDModuleInst *i)
  472. {
  473. // read arguments
  474. NCDValRef arg_collection;
  475. NCDValRef arg_template;
  476. NCDValRef arg_args;
  477. if (!NCDVal_ListRead(i->args, 3, &arg_collection, &arg_template, &arg_args)) {
  478. ModuleLog(i, BLOG_ERROR, "wrong arity");
  479. goto fail0;
  480. }
  481. if (!NCDVal_IsStringNoNulls(arg_template) || !NCDVal_IsList(arg_args)) {
  482. ModuleLog(i, BLOG_ERROR, "wrong type");
  483. goto fail0;
  484. }
  485. const char *template_name = NCDVal_StringValue(arg_template);
  486. const char *name1;
  487. const char *name2;
  488. switch (NCDVal_Type(arg_collection)) {
  489. case NCDVAL_LIST: {
  490. name1 = "_index";
  491. name2 = "_elem";
  492. } break;
  493. case NCDVAL_MAP: {
  494. name1 = "_key";
  495. name2 = "_val";
  496. } break;
  497. default:
  498. ModuleLog(i, BLOG_ERROR, "invalid collection type");
  499. goto fail0;
  500. }
  501. func_new_common(vo, i, arg_collection, template_name, arg_args, name1, name2);
  502. return;
  503. fail0:
  504. NCDModuleInst_Backend_SetError(i);
  505. NCDModuleInst_Backend_Dead(i);
  506. }
  507. static void func_new_foreach_emb (void *vo, NCDModuleInst *i)
  508. {
  509. // read arguments
  510. NCDValRef arg_collection;
  511. NCDValRef arg_template;
  512. NCDValRef arg_name1;
  513. NCDValRef arg_name2 = NCDVal_NewInvalid();
  514. if (!NCDVal_ListRead(i->args, 3, &arg_collection, &arg_template, &arg_name1) && !NCDVal_ListRead(i->args, 4, &arg_collection, &arg_template, &arg_name1, &arg_name2)) {
  515. ModuleLog(i, BLOG_ERROR, "wrong arity");
  516. goto fail0;
  517. }
  518. if (!NCDVal_IsStringNoNulls(arg_template) || !NCDVal_IsStringNoNulls(arg_name1) || (!NCDVal_IsInvalid(arg_name2) && !NCDVal_IsStringNoNulls(arg_name2))) {
  519. ModuleLog(i, BLOG_ERROR, "wrong type");
  520. goto fail0;
  521. }
  522. const char *template_name = NCDVal_StringValue(arg_template);
  523. const char *name1 = NCDVal_StringValue(arg_name1);
  524. const char *name2 = (NCDVal_IsInvalid(arg_name2) ? NULL : NCDVal_StringValue(arg_name2));
  525. func_new_common(vo, i, arg_collection, template_name, NCDVal_NewInvalid(), name1, name2);
  526. return;
  527. fail0:
  528. NCDModuleInst_Backend_SetError(i);
  529. NCDModuleInst_Backend_Dead(i);
  530. }
  531. static void instance_free (struct instance *o)
  532. {
  533. ASSERT(o->gp == 0)
  534. ASSERT(o->ip == 0)
  535. // free elements
  536. BFree(o->elems);
  537. // free timer
  538. BReactor_RemoveTimer(o->i->iparams->reactor, &o->timer);
  539. NCDModuleInst_Backend_Dead(o->i);
  540. }
  541. static void func_die (void *vo)
  542. {
  543. struct instance *o = vo;
  544. assert_state(o);
  545. ASSERT(o->state != ISTATE_TERMINATING)
  546. // set GP zero
  547. o->gp = 0;
  548. // set state terminating
  549. o->state = ISTATE_TERMINATING;
  550. work(o);
  551. return;
  552. }
  553. static void func_clean (void *vo)
  554. {
  555. struct instance *o = vo;
  556. if (o->state != ISTATE_WAITING) {
  557. return;
  558. }
  559. // set state working
  560. o->state = ISTATE_WORKING;
  561. work(o);
  562. return;
  563. }
  564. static const struct NCDModule modules[] = {
  565. {
  566. .type = "foreach",
  567. .func_new2 = func_new_foreach,
  568. .func_die = func_die,
  569. .func_clean = func_clean,
  570. .alloc_size = sizeof(struct instance)
  571. }, {
  572. .type = "foreach_emb",
  573. .func_new2 = func_new_foreach_emb,
  574. .func_die = func_die,
  575. .func_clean = func_clean,
  576. .alloc_size = sizeof(struct instance)
  577. }, {
  578. .type = NULL
  579. }
  580. };
  581. const struct NCDModuleGroup ncdmodule_foreach = {
  582. .modules = modules
  583. };