foreach.c 20 KB

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