foreach.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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_getspecialobj (struct element *e, const char *name, NCDObject *out_object);
  92. static int element_caller_object_func_getobj (struct element *e, const char *name, NCDObject *out_object);
  93. static int element_index_object_func_getvar (struct element *e, const char *name, NCDValue *out_value);
  94. static int element_elem_object_func_getvar (struct element *e, const char *name, NCDValue *out_value);
  95. static void instance_free (struct instance *o);
  96. static void assert_state (struct instance *o)
  97. {
  98. ASSERT(o->gp <= o->num_elems)
  99. ASSERT(o->ip <= o->num_elems)
  100. ASSERT(o->gp <= o->ip)
  101. #ifndef NDEBUG
  102. // check GP
  103. for (size_t i = 0; i < o->gp; i++) {
  104. if (i == o->gp - 1) {
  105. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  106. o->elems[i].state == ESTATE_WAITING)
  107. } else {
  108. ASSERT(o->elems[i].state == ESTATE_UP)
  109. }
  110. }
  111. // check IP
  112. size_t ip = o->num_elems;
  113. while (ip > 0 && o->elems[ip - 1].state == ESTATE_FORGOTTEN) {
  114. ip--;
  115. }
  116. ASSERT(o->ip == ip)
  117. // check gap
  118. for (size_t i = o->gp; i < o->ip; i++) {
  119. if (i == o->ip - 1) {
  120. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  121. o->elems[i].state == ESTATE_WAITING || o->elems[i].state == ESTATE_TERMINATING)
  122. } else {
  123. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  124. o->elems[i].state == ESTATE_WAITING)
  125. }
  126. }
  127. #endif
  128. }
  129. static void work (struct instance *o)
  130. {
  131. assert_state(o);
  132. // stop timer
  133. BReactor_RemoveTimer(o->i->iparams->reactor, &o->timer);
  134. if (o->state == ISTATE_WAITING) {
  135. return;
  136. }
  137. 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))) {
  138. // signal down
  139. NCDModuleInst_Backend_Down(o->i);
  140. // set state waiting
  141. o->state = ISTATE_WAITING;
  142. return;
  143. }
  144. if (o->gp < o->ip) {
  145. // get last element
  146. struct element *le = &o->elems[o->ip - 1];
  147. ASSERT(le->state != ESTATE_FORGOTTEN)
  148. // start terminating if not already
  149. if (le->state != ESTATE_TERMINATING) {
  150. // request termination
  151. NCDModuleProcess_Terminate(&le->process);
  152. // set element state terminating
  153. le->state = ESTATE_TERMINATING;
  154. }
  155. return;
  156. }
  157. if (o->state == ISTATE_TERMINATING) {
  158. // finally die
  159. instance_free(o);
  160. return;
  161. }
  162. if (o->gp == o->num_elems && (o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)) {
  163. if (o->state == ISTATE_WORKING) {
  164. // signal up
  165. NCDModuleInst_Backend_Up(o->i);
  166. // set state up
  167. o->state = ISTATE_UP;
  168. }
  169. return;
  170. }
  171. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_WAITING) {
  172. // get last element
  173. struct element *le = &o->elems[o->gp - 1];
  174. // continue process
  175. NCDModuleProcess_Continue(&le->process);
  176. // set state down
  177. le->state = ESTATE_DOWN;
  178. return;
  179. }
  180. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_DOWN) {
  181. return;
  182. }
  183. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  184. advance(o);
  185. return;
  186. }
  187. static void advance (struct instance *o)
  188. {
  189. assert_state(o);
  190. ASSERT(o->gp == o->ip)
  191. ASSERT(o->gp < o->num_elems)
  192. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  193. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  194. // get next element
  195. struct element *e = &o->elems[o->gp];
  196. // copy arguments
  197. NCDValue args;
  198. if (!NCDValue_InitCopy(&args, o->args)) {
  199. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  200. goto fail;
  201. }
  202. // init process
  203. if (!NCDModuleProcess_Init(&e->process, o->i, o->template_name, args, e, (NCDModuleProcess_handler_event)element_process_handler_event)) {
  204. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  205. NCDValue_Free(&args);
  206. goto fail;
  207. }
  208. // set special functions
  209. NCDModuleProcess_SetSpecialFuncs(&e->process, (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->iparams->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_getspecialobj (struct element *e, const char *name, NCDObject *out_object)
  270. {
  271. struct instance *o = e->inst;
  272. ASSERT(e->state != ESTATE_FORGOTTEN)
  273. if (!strcmp(name, "_caller")) {
  274. *out_object = NCDObject_Build(NULL, e, NULL, (NCDObject_func_getobj)element_caller_object_func_getobj);
  275. return 1;
  276. }
  277. if (!strcmp(name, "_index")) {
  278. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_index_object_func_getvar, NULL);
  279. return 1;
  280. }
  281. if (!strcmp(name, "_elem")) {
  282. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_elem_object_func_getvar, NULL);
  283. return 1;
  284. }
  285. return 0;
  286. }
  287. static int element_caller_object_func_getobj (struct element *e, const char *name, NCDObject *out_object)
  288. {
  289. struct instance *o = e->inst;
  290. ASSERT(e->state != ESTATE_FORGOTTEN)
  291. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  292. }
  293. static int element_index_object_func_getvar (struct element *e, const char *name, NCDValue *out_value)
  294. {
  295. struct instance *o = e->inst;
  296. ASSERT(e->state != ESTATE_FORGOTTEN)
  297. if (strcmp(name, "")) {
  298. return 0;
  299. }
  300. char str[64];
  301. snprintf(str, sizeof(str), "%zu", e->i);
  302. if (!NCDValue_InitString(out_value, str)) {
  303. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  304. return 0;
  305. }
  306. return 1;
  307. }
  308. static int element_elem_object_func_getvar (struct element *e, const char *name, NCDValue *out_value)
  309. {
  310. struct instance *o = e->inst;
  311. ASSERT(e->state != ESTATE_FORGOTTEN)
  312. if (strcmp(name, "")) {
  313. return 0;
  314. }
  315. if (!NCDValue_InitCopy(out_value, e->value)) {
  316. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  317. return 0;
  318. }
  319. return 1;
  320. }
  321. static void func_new (NCDModuleInst *i)
  322. {
  323. // allocate instance
  324. struct instance *o = malloc(sizeof(*o));
  325. if (!o) {
  326. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  327. goto fail0;
  328. }
  329. NCDModuleInst_Backend_SetUser(i, o);
  330. // init arguments
  331. o->i = i;
  332. // read arguments
  333. NCDValue *arg_list;
  334. NCDValue *arg_template;
  335. NCDValue *arg_args;
  336. if (!NCDValue_ListRead(i->args, 3, &arg_list, &arg_template, &arg_args)) {
  337. ModuleLog(i, BLOG_ERROR, "wrong arity");
  338. goto fail1;
  339. }
  340. if (NCDValue_Type(arg_list) != NCDVALUE_LIST || !NCDValue_IsStringNoNulls(arg_template) ||
  341. NCDValue_Type(arg_args) != NCDVALUE_LIST
  342. ) {
  343. ModuleLog(i, BLOG_ERROR, "wrong type");
  344. goto fail1;
  345. }
  346. o->template_name = NCDValue_StringValue(arg_template);
  347. o->args = arg_args;
  348. // init timer
  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. };