foreach.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. char *template_name;
  70. NCDValue *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. NCDValue *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, NCDValue *out_value);
  93. static int element_elem_object_func_getvar (struct element *e, const char *name, NCDValue *out_value);
  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. // copy arguments
  196. NCDValue args;
  197. if (!NCDValue_InitCopy(&args, o->args)) {
  198. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  199. goto fail;
  200. }
  201. // init process
  202. if (!NCDModuleProcess_Init(&e->process, o->i, o->template_name, args, e, (NCDModuleProcess_handler_event)element_process_handler_event)) {
  203. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  204. NCDValue_Free(&args);
  205. goto fail;
  206. }
  207. // set special functions
  208. NCDModuleProcess_SetSpecialFuncs(&e->process, (NCDModuleProcess_func_getspecialobj)element_process_func_getspecialobj);
  209. // set element state down
  210. e->state = ESTATE_DOWN;
  211. // increment GP and IP
  212. o->gp++;
  213. o->ip++;
  214. return;
  215. fail:
  216. // set timer
  217. BReactor_SetTimer(o->i->iparams->reactor, &o->timer);
  218. }
  219. static void timer_handler (struct instance *o)
  220. {
  221. assert_state(o);
  222. ASSERT(o->gp == o->ip)
  223. ASSERT(o->gp < o->num_elems)
  224. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  225. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  226. advance(o);
  227. return;
  228. }
  229. static void element_process_handler_event (struct element *e, int event)
  230. {
  231. struct instance *o = e->inst;
  232. assert_state(o);
  233. ASSERT(e->i < o->ip)
  234. ASSERT(e->state != ESTATE_FORGOTTEN)
  235. switch (event) {
  236. case NCDMODULEPROCESS_EVENT_UP: {
  237. ASSERT(e->state == ESTATE_DOWN)
  238. ASSERT(o->gp == o->ip)
  239. ASSERT(o->gp == e->i + 1)
  240. // set element state up
  241. e->state = ESTATE_UP;
  242. } break;
  243. case NCDMODULEPROCESS_EVENT_DOWN: {
  244. ASSERT(e->state == ESTATE_UP)
  245. // set element state waiting
  246. e->state = ESTATE_WAITING;
  247. // bump down GP
  248. if (o->gp > e->i + 1) {
  249. o->gp = e->i + 1;
  250. }
  251. } break;
  252. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  253. ASSERT(e->state == ESTATE_TERMINATING)
  254. ASSERT(o->gp < o->ip)
  255. ASSERT(o->ip == e->i + 1)
  256. // free process
  257. NCDModuleProcess_Free(&e->process);
  258. // set element state forgotten
  259. e->state = ESTATE_FORGOTTEN;
  260. // decrement IP
  261. o->ip--;
  262. } break;
  263. default: ASSERT(0);
  264. }
  265. work(o);
  266. return;
  267. }
  268. static int element_process_func_getspecialobj (struct element *e, const char *name, NCDObject *out_object)
  269. {
  270. struct instance *o = e->inst;
  271. ASSERT(e->state != ESTATE_FORGOTTEN)
  272. if (!strcmp(name, "_caller")) {
  273. *out_object = NCDObject_Build(NULL, e, NULL, (NCDObject_func_getobj)element_caller_object_func_getobj);
  274. return 1;
  275. }
  276. if (!strcmp(name, "_index")) {
  277. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_index_object_func_getvar, NULL);
  278. return 1;
  279. }
  280. if (!strcmp(name, "_elem")) {
  281. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_elem_object_func_getvar, NULL);
  282. return 1;
  283. }
  284. return 0;
  285. }
  286. static int element_caller_object_func_getobj (struct element *e, const char *name, NCDObject *out_object)
  287. {
  288. struct instance *o = e->inst;
  289. ASSERT(e->state != ESTATE_FORGOTTEN)
  290. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  291. }
  292. static int element_index_object_func_getvar (struct element *e, const char *name, NCDValue *out_value)
  293. {
  294. struct instance *o = e->inst;
  295. ASSERT(e->state != ESTATE_FORGOTTEN)
  296. if (strcmp(name, "")) {
  297. return 0;
  298. }
  299. char str[64];
  300. snprintf(str, sizeof(str), "%zu", e->i);
  301. if (!NCDValue_InitString(out_value, str)) {
  302. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  303. return 0;
  304. }
  305. return 1;
  306. }
  307. static int element_elem_object_func_getvar (struct element *e, const char *name, NCDValue *out_value)
  308. {
  309. struct instance *o = e->inst;
  310. ASSERT(e->state != ESTATE_FORGOTTEN)
  311. if (strcmp(name, "")) {
  312. return 0;
  313. }
  314. if (!NCDValue_InitCopy(out_value, e->value)) {
  315. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  316. return 0;
  317. }
  318. return 1;
  319. }
  320. static void func_new (NCDModuleInst *i)
  321. {
  322. // allocate instance
  323. struct instance *o = malloc(sizeof(*o));
  324. if (!o) {
  325. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  326. goto fail0;
  327. }
  328. NCDModuleInst_Backend_SetUser(i, o);
  329. // init arguments
  330. o->i = i;
  331. // read arguments
  332. NCDValue *arg_list;
  333. NCDValue *arg_template;
  334. NCDValue *arg_args;
  335. if (!NCDValue_ListRead(i->args, 3, &arg_list, &arg_template, &arg_args)) {
  336. ModuleLog(i, BLOG_ERROR, "wrong arity");
  337. goto fail1;
  338. }
  339. if (NCDValue_Type(arg_list) != NCDVALUE_LIST || !NCDValue_IsStringNoNulls(arg_template) ||
  340. NCDValue_Type(arg_args) != NCDVALUE_LIST
  341. ) {
  342. ModuleLog(i, BLOG_ERROR, "wrong type");
  343. goto fail1;
  344. }
  345. o->template_name = NCDValue_StringValue(arg_template);
  346. o->args = arg_args;
  347. // init timer
  348. btime_t retry_time = NCDModuleInst_Backend_InterpGetRetryTime(i);
  349. BTimer_Init(&o->timer, retry_time, (BTimer_handler)timer_handler, o);
  350. // count elements
  351. o->num_elems = NCDValue_ListCount(arg_list);
  352. // allocate elements
  353. if (!(o->elems = BAllocArray(o->num_elems, sizeof(o->elems[0])))) {
  354. ModuleLog(i, BLOG_ERROR, "BAllocArray failed");
  355. goto fail1;
  356. }
  357. NCDValue *ev = NCDValue_ListFirst(arg_list);
  358. for (size_t i = 0; i < o->num_elems; i++) {
  359. struct element *e = &o->elems[i];
  360. // set instance
  361. e->inst = o;
  362. // set index
  363. e->i = i;
  364. // set value
  365. e->value = ev;
  366. // set state forgotten
  367. e->state = ESTATE_FORGOTTEN;
  368. ev = NCDValue_ListNext(arg_list, ev);
  369. }
  370. // set GP and IP zero
  371. o->gp = 0;
  372. o->ip = 0;
  373. // set state working
  374. o->state = ISTATE_WORKING;
  375. work(o);
  376. return;
  377. fail1:
  378. free(o);
  379. fail0:
  380. NCDModuleInst_Backend_SetError(i);
  381. NCDModuleInst_Backend_Dead(i);
  382. }
  383. static void instance_free (struct instance *o)
  384. {
  385. NCDModuleInst *i = o->i;
  386. ASSERT(o->gp == 0)
  387. ASSERT(o->ip == 0)
  388. // free elements
  389. BFree(o->elems);
  390. // free timer
  391. BReactor_RemoveTimer(o->i->iparams->reactor, &o->timer);
  392. // free instance
  393. free(o);
  394. NCDModuleInst_Backend_Dead(i);
  395. }
  396. static void func_die (void *vo)
  397. {
  398. struct instance *o = vo;
  399. assert_state(o);
  400. ASSERT(o->state != ISTATE_TERMINATING)
  401. // set GP zero
  402. o->gp = 0;
  403. // set state terminating
  404. o->state = ISTATE_TERMINATING;
  405. work(o);
  406. return;
  407. }
  408. static void func_clean (void *vo)
  409. {
  410. struct instance *o = vo;
  411. if (o->state != ISTATE_WAITING) {
  412. return;
  413. }
  414. // set state working
  415. o->state = ISTATE_WORKING;
  416. work(o);
  417. return;
  418. }
  419. static const struct NCDModule modules[] = {
  420. {
  421. .type = "foreach",
  422. .func_new = func_new,
  423. .func_die = func_die,
  424. .func_clean = func_clean
  425. }, {
  426. .type = NULL
  427. }
  428. };
  429. const struct NCDModuleGroup ncdmodule_foreach = {
  430. .modules = modules
  431. };