foreach.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 RETRY_TIME 5000
  58. #define ISTATE_WORKING 1
  59. #define ISTATE_UP 2
  60. #define ISTATE_WAITING 3
  61. #define ISTATE_TERMINATING 4
  62. #define ESTATE_FORGOTTEN 1
  63. #define ESTATE_DOWN 2
  64. #define ESTATE_UP 3
  65. #define ESTATE_WAITING 4
  66. #define ESTATE_TERMINATING 5
  67. struct element;
  68. struct instance {
  69. NCDModuleInst *i;
  70. char *template_name;
  71. NCDValue *args;
  72. BTimer timer;
  73. size_t num_elems;
  74. struct element *elems;
  75. size_t gp; // good pointer
  76. size_t ip; // initialized pointer
  77. int state;
  78. };
  79. struct element {
  80. struct instance *inst;
  81. size_t i;
  82. NCDValue *value;
  83. NCDModuleProcess process;
  84. int state;
  85. };
  86. static void assert_state (struct instance *o);
  87. static void work (struct instance *o);
  88. static void advance (struct instance *o);
  89. static void timer_handler (struct instance *o);
  90. static void element_process_handler_event (struct element *e, int event);
  91. static int element_process_func_getspecialvar (struct element *e, const char *name, NCDValue *out);
  92. static NCDModuleInst * element_process_func_getspecialobj (struct element *e, const char *name);
  93. static void instance_free (struct instance *o);
  94. static void assert_state (struct instance *o)
  95. {
  96. ASSERT(o->gp <= o->num_elems)
  97. ASSERT(o->ip <= o->num_elems)
  98. ASSERT(o->gp <= o->ip)
  99. #ifndef NDEBUG
  100. // check GP
  101. for (size_t i = 0; i < o->gp; i++) {
  102. if (i == o->gp - 1) {
  103. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  104. o->elems[i].state == ESTATE_WAITING)
  105. } else {
  106. ASSERT(o->elems[i].state == ESTATE_UP)
  107. }
  108. }
  109. // check IP
  110. size_t ip = o->num_elems;
  111. while (ip > 0 && o->elems[ip - 1].state == ESTATE_FORGOTTEN) {
  112. ip--;
  113. }
  114. ASSERT(o->ip == ip)
  115. // check gap
  116. for (size_t i = o->gp; i < o->ip; i++) {
  117. if (i == o->ip - 1) {
  118. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  119. o->elems[i].state == ESTATE_WAITING || o->elems[i].state == ESTATE_TERMINATING)
  120. } else {
  121. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  122. o->elems[i].state == ESTATE_WAITING)
  123. }
  124. }
  125. #endif
  126. }
  127. static void work (struct instance *o)
  128. {
  129. assert_state(o);
  130. // stop timer
  131. BReactor_RemoveTimer(o->i->reactor, &o->timer);
  132. if (o->state == ISTATE_WAITING) {
  133. return;
  134. }
  135. 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))) {
  136. // signal down
  137. NCDModuleInst_Backend_Down(o->i);
  138. // set state waiting
  139. o->state = ISTATE_WAITING;
  140. return;
  141. }
  142. if (o->gp < o->ip) {
  143. // get last element
  144. struct element *le = &o->elems[o->ip - 1];
  145. ASSERT(le->state != ESTATE_FORGOTTEN)
  146. // start terminating if not already
  147. if (le->state != ESTATE_TERMINATING) {
  148. // request termination
  149. NCDModuleProcess_Terminate(&le->process);
  150. // set element state terminating
  151. le->state = ESTATE_TERMINATING;
  152. }
  153. return;
  154. }
  155. if (o->state == ISTATE_TERMINATING) {
  156. // finally die
  157. instance_free(o);
  158. return;
  159. }
  160. if (o->gp == o->num_elems && (o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)) {
  161. if (o->state == ISTATE_WORKING) {
  162. // signal up
  163. NCDModuleInst_Backend_Up(o->i);
  164. // set state up
  165. o->state = ISTATE_UP;
  166. }
  167. return;
  168. }
  169. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_WAITING) {
  170. // get last element
  171. struct element *le = &o->elems[o->gp - 1];
  172. // continue process
  173. NCDModuleProcess_Continue(&le->process);
  174. // set state down
  175. le->state = ESTATE_DOWN;
  176. return;
  177. }
  178. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_DOWN) {
  179. return;
  180. }
  181. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  182. advance(o);
  183. return;
  184. }
  185. static void advance (struct instance *o)
  186. {
  187. assert_state(o);
  188. ASSERT(o->gp == o->ip)
  189. ASSERT(o->gp < o->num_elems)
  190. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  191. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  192. // get next element
  193. struct element *e = &o->elems[o->gp];
  194. // copy arguments
  195. NCDValue args;
  196. if (!NCDValue_InitCopy(&args, o->args)) {
  197. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  198. goto fail;
  199. }
  200. // init process
  201. if (!NCDModuleProcess_Init(&e->process, o->i, o->template_name, args, e, (NCDModuleProcess_handler_event)element_process_handler_event)) {
  202. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  203. NCDValue_Free(&args);
  204. goto fail;
  205. }
  206. // set special functions
  207. NCDModuleProcess_SetSpecialFuncs(&e->process,
  208. (NCDModuleProcess_func_getspecialvar)element_process_func_getspecialvar,
  209. (NCDModuleProcess_func_getspecialobj)element_process_func_getspecialobj);
  210. // set element state down
  211. e->state = ESTATE_DOWN;
  212. // increment GP and IP
  213. o->gp++;
  214. o->ip++;
  215. return;
  216. fail:
  217. // set timer
  218. BReactor_SetTimer(o->i->reactor, &o->timer);
  219. }
  220. static void timer_handler (struct instance *o)
  221. {
  222. assert_state(o);
  223. ASSERT(o->gp == o->ip)
  224. ASSERT(o->gp < o->num_elems)
  225. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  226. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  227. advance(o);
  228. return;
  229. }
  230. static void element_process_handler_event (struct element *e, int event)
  231. {
  232. struct instance *o = e->inst;
  233. assert_state(o);
  234. ASSERT(e->i < o->ip)
  235. ASSERT(e->state != ESTATE_FORGOTTEN)
  236. switch (event) {
  237. case NCDMODULEPROCESS_EVENT_UP: {
  238. ASSERT(e->state == ESTATE_DOWN)
  239. ASSERT(o->gp == o->ip)
  240. ASSERT(o->gp == e->i + 1)
  241. // set element state up
  242. e->state = ESTATE_UP;
  243. } break;
  244. case NCDMODULEPROCESS_EVENT_DOWN: {
  245. ASSERT(e->state == ESTATE_UP)
  246. // set element state waiting
  247. e->state = ESTATE_WAITING;
  248. // bump down GP
  249. if (o->gp > e->i + 1) {
  250. o->gp = e->i + 1;
  251. }
  252. } break;
  253. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  254. ASSERT(e->state == ESTATE_TERMINATING)
  255. ASSERT(o->gp < o->ip)
  256. ASSERT(o->ip == e->i + 1)
  257. // free process
  258. NCDModuleProcess_Free(&e->process);
  259. // set element state forgotten
  260. e->state = ESTATE_FORGOTTEN;
  261. // decrement IP
  262. o->ip--;
  263. } break;
  264. default: ASSERT(0);
  265. }
  266. work(o);
  267. return;
  268. }
  269. static int element_process_func_getspecialvar (struct element *e, const char *name, NCDValue *out)
  270. {
  271. struct instance *o = e->inst;
  272. ASSERT(e->state != ESTATE_FORGOTTEN)
  273. if (e->i >= o->gp) {
  274. BLog(BLOG_ERROR, "tried to resolve variable %s but it's dirty", name);
  275. return 0;
  276. }
  277. if (!strcmp(name, "_index")) {
  278. char str[64];
  279. snprintf(str, sizeof(str), "%zu", e->i);
  280. if (!NCDValue_InitString(out, str)) {
  281. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  282. return 0;
  283. }
  284. return 1;
  285. }
  286. if (!strcmp(name, "_elem")) {
  287. if (!NCDValue_InitCopy(out, e->value)) {
  288. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  289. return 0;
  290. }
  291. return 1;
  292. }
  293. size_t p;
  294. if (p = string_begins_with(name, "_caller.")) {
  295. return NCDModuleInst_Backend_GetVar(o->i, name + p, out);
  296. }
  297. return 0;
  298. }
  299. static NCDModuleInst * element_process_func_getspecialobj (struct element *e, const char *name)
  300. {
  301. struct instance *o = e->inst;
  302. ASSERT(e->state != ESTATE_FORGOTTEN)
  303. if (e->i >= o->gp) {
  304. BLog(BLOG_ERROR, "tried to resolve object %s but it's dirty", name);
  305. return NULL;
  306. }
  307. size_t p;
  308. if (p = string_begins_with(name, "_caller.")) {
  309. return NCDModuleInst_Backend_GetObj(o->i, name + p);
  310. }
  311. return NULL;
  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. NCDValue *arg_list;
  326. NCDValue *arg_template;
  327. NCDValue *arg_args;
  328. if (!NCDValue_ListRead(i->args, 3, &arg_list, &arg_template, &arg_args)) {
  329. ModuleLog(i, BLOG_ERROR, "wrong arity");
  330. goto fail1;
  331. }
  332. if (NCDValue_Type(arg_list) != NCDVALUE_LIST || NCDValue_Type(arg_template) != NCDVALUE_STRING ||
  333. NCDValue_Type(arg_args) != NCDVALUE_LIST
  334. ) {
  335. ModuleLog(i, BLOG_ERROR, "wrong type");
  336. goto fail1;
  337. }
  338. o->template_name = NCDValue_StringValue(arg_template);
  339. o->args = arg_args;
  340. // init timer
  341. BTimer_Init(&o->timer, RETRY_TIME, (BTimer_handler)timer_handler, o);
  342. // count elements
  343. o->num_elems = NCDValue_ListCount(arg_list);
  344. // allocate elements
  345. if (!(o->elems = BAllocArray(o->num_elems, sizeof(o->elems[0])))) {
  346. ModuleLog(i, BLOG_ERROR, "BAllocArray failed");
  347. goto fail1;
  348. }
  349. NCDValue *ev = NCDValue_ListFirst(arg_list);
  350. for (size_t i = 0; i < o->num_elems; i++) {
  351. struct element *e = &o->elems[i];
  352. // set instance
  353. e->inst = o;
  354. // set index
  355. e->i = i;
  356. // set value
  357. e->value = ev;
  358. // set state forgotten
  359. e->state = ESTATE_FORGOTTEN;
  360. ev = NCDValue_ListNext(arg_list, ev);
  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->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. };