blocker.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * @file blocker.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. * Blocker module. Provides a statement that blocks when initialized, and which can be blocked
  32. * and unblocked from outside.
  33. *
  34. * Synopsis: blocker([string initial_state])
  35. * Description: provides blocking operations. On deinitialization, waits for all corresponding
  36. * use() statements to die before dying itself.
  37. * The optional boolean argument initial_state specifies the initial up-state
  38. * of the blocker. If not given, the default is false (not up).
  39. * Variables: string (empty) - the up-state (false or true).
  40. *
  41. * Synopsis: blocker::up()
  42. * Description: sets the blocking state to up.
  43. * The immediate effects of corresponding use() statements going up are processed before
  44. * this statement goes up; but this statement statement still goes up immediately,
  45. * assuming the effects mentioned haven't resulted in the intepreter scheduling this
  46. * very statement for destruction.
  47. *
  48. * Synopsis: blocker::down()
  49. * Description: sets the blocking state to down.
  50. * The immediate effects of corresponding use() statements going up are processed before
  51. * this statement goes up; but this statement statement still goes up immediately,
  52. * assuming the effects mentioned haven't resulted in the intepreter scheduling this
  53. * very statement for destruction.
  54. *
  55. * Synopsis: blocker::downup()
  56. * Description: atomically sets the blocker to down state (if it was up), then (back) to up state.
  57. * Note that this is not equivalent to calling down() and immediately up(); in that case,
  58. * the interpreter will first handle the immediate effects of any use() statements
  59. * going down as a result of having called down() and will only later execute the up()
  60. * statement. In fact, it is possible that the effects of down() will prevent up() from
  61. * executing, which may leave the program in an undesirable state.
  62. *
  63. * Synopsis: blocker::rdownup()
  64. * Description: on deinitialization, atomically sets the blocker to down state (if it was up), then
  65. * (back) to up state.
  66. * The immediate effects of corresponding use() statements changing state are processed
  67. * *after* the immediate effects of this statement dying (in contrast to downup()).
  68. *
  69. * Synopsis: blocker::use()
  70. * Description: blocks on the blocker. This module is in up state if and only if the blocking state of
  71. * the blocker is up. Multiple use statements may be used with the same blocker.
  72. */
  73. #include <stdlib.h>
  74. #include <string.h>
  75. #include <misc/offset.h>
  76. #include <misc/debug.h>
  77. #include <structure/LinkedList1.h>
  78. #include <structure/LinkedList0.h>
  79. #include <ncd/module_common.h>
  80. #include <generated/blog_channel_ncd_blocker.h>
  81. struct instance {
  82. NCDModuleInst *i;
  83. LinkedList1 users;
  84. LinkedList0 rdownups_list;
  85. int up;
  86. int dying;
  87. };
  88. struct rdownup_instance {
  89. NCDModuleInst *i;
  90. struct instance *blocker;
  91. LinkedList0Node rdownups_list_node;
  92. };
  93. struct use_instance {
  94. NCDModuleInst *i;
  95. struct instance *blocker;
  96. LinkedList1Node blocker_node;
  97. };
  98. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  99. {
  100. struct instance *o = vo;
  101. o->i = i;
  102. // read arguments
  103. NCDValRef initial_state = NCDVal_NewInvalid();
  104. if (!NCDVal_ListRead(params->args, 0) &&
  105. !NCDVal_ListRead(params->args, 1, &initial_state)
  106. ) {
  107. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  108. goto fail0;
  109. }
  110. // get the initial state
  111. o->up = 0;
  112. if (!NCDVal_IsInvalid(initial_state)) {
  113. if (!ncd_read_boolean(initial_state, &o->up)) {
  114. ModuleLog(o->i, BLOG_ERROR, "bad initial_state argument");
  115. goto fail0;
  116. }
  117. }
  118. // init users list
  119. LinkedList1_Init(&o->users);
  120. // init rdownups list
  121. LinkedList0_Init(&o->rdownups_list);
  122. // set not dying
  123. o->dying = 0;
  124. // signal up
  125. NCDModuleInst_Backend_Up(o->i);
  126. return;
  127. fail0:
  128. NCDModuleInst_Backend_DeadError(i);
  129. }
  130. static void instance_free (struct instance *o)
  131. {
  132. ASSERT(LinkedList1_IsEmpty(&o->users))
  133. // break any rdownups
  134. LinkedList0Node *ln;
  135. while (ln = LinkedList0_GetFirst(&o->rdownups_list)) {
  136. struct rdownup_instance *rdu = UPPER_OBJECT(ln, struct rdownup_instance, rdownups_list_node);
  137. ASSERT(rdu->blocker == o)
  138. LinkedList0_Remove(&o->rdownups_list, &rdu->rdownups_list_node);
  139. rdu->blocker = NULL;
  140. }
  141. NCDModuleInst_Backend_Dead(o->i);
  142. }
  143. static void func_die (void *vo)
  144. {
  145. struct instance *o = vo;
  146. ASSERT(!o->dying)
  147. // if we have no users, die right away, else wait for users
  148. if (LinkedList1_IsEmpty(&o->users)) {
  149. instance_free(o);
  150. return;
  151. }
  152. // set dying
  153. o->dying = 1;
  154. }
  155. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  156. {
  157. struct instance *o = vo;
  158. if (name == NCD_STRING_EMPTY) {
  159. *out = ncd_make_boolean(mem, o->up, o->i->params->iparams->string_index);
  160. return 1;
  161. }
  162. return 0;
  163. }
  164. static void updown_func_new_templ (NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int up, int first_down)
  165. {
  166. ASSERT(!first_down || up)
  167. // check arguments
  168. if (!NCDVal_ListRead(params->args, 0)) {
  169. ModuleLog(i, BLOG_ERROR, "wrong arity");
  170. goto fail0;
  171. }
  172. // signal up
  173. NCDModuleInst_Backend_Up(i);
  174. // get method object
  175. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  176. if (first_down || mo->up != up) {
  177. // signal users
  178. for (LinkedList1Node *node = LinkedList1_GetFirst(&mo->users); node; node = LinkedList1Node_Next(node)) {
  179. struct use_instance *user = UPPER_OBJECT(node, struct use_instance, blocker_node);
  180. ASSERT(user->blocker == mo)
  181. if (first_down && mo->up) {
  182. NCDModuleInst_Backend_Down(user->i);
  183. }
  184. if (up) {
  185. NCDModuleInst_Backend_Up(user->i);
  186. } else {
  187. NCDModuleInst_Backend_Down(user->i);
  188. }
  189. }
  190. // change up state
  191. mo->up = up;
  192. }
  193. return;
  194. fail0:
  195. NCDModuleInst_Backend_DeadError(i);
  196. }
  197. static void up_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  198. {
  199. updown_func_new_templ(i, params, 1, 0);
  200. }
  201. static void down_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  202. {
  203. updown_func_new_templ(i, params, 0, 0);
  204. }
  205. static void downup_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  206. {
  207. updown_func_new_templ(i, params, 1, 1);
  208. }
  209. static void rdownup_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  210. {
  211. struct rdownup_instance *o = vo;
  212. o->i = i;
  213. // check arguments
  214. if (!NCDVal_ListRead(params->args, 0)) {
  215. ModuleLog(i, BLOG_ERROR, "wrong arity");
  216. goto fail0;
  217. }
  218. // get blocker
  219. struct instance *blk = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  220. // set blocker
  221. o->blocker = blk;
  222. // insert to rdownups list
  223. LinkedList0_Prepend(&blk->rdownups_list, &o->rdownups_list_node);
  224. // signal up
  225. NCDModuleInst_Backend_Up(i);
  226. return;
  227. fail0:
  228. NCDModuleInst_Backend_DeadError(i);
  229. }
  230. static void rdownup_func_die (void *vo)
  231. {
  232. struct rdownup_instance *o = vo;
  233. struct instance *blk = o->blocker;
  234. if (blk) {
  235. // remove from rdownups list
  236. LinkedList0_Remove(&blk->rdownups_list, &o->rdownups_list_node);
  237. // downup users
  238. for (LinkedList1Node *ln = LinkedList1_GetFirst(&blk->users); ln; ln = LinkedList1Node_Next(ln)) {
  239. struct use_instance *user = UPPER_OBJECT(ln, struct use_instance, blocker_node);
  240. ASSERT(user->blocker == blk)
  241. if (blk->up) {
  242. NCDModuleInst_Backend_Down(user->i);
  243. }
  244. NCDModuleInst_Backend_Up(user->i);
  245. }
  246. // set up
  247. blk->up = 1;
  248. }
  249. NCDModuleInst_Backend_Dead(o->i);
  250. }
  251. static void use_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  252. {
  253. struct use_instance *o = vo;
  254. o->i = i;
  255. // check arguments
  256. if (!NCDVal_ListRead(params->args, 0)) {
  257. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  258. goto fail0;
  259. }
  260. // set blocker
  261. o->blocker = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  262. // add to blocker's list
  263. LinkedList1_Append(&o->blocker->users, &o->blocker_node);
  264. // signal up if needed
  265. if (o->blocker->up) {
  266. NCDModuleInst_Backend_Up(o->i);
  267. }
  268. return;
  269. fail0:
  270. NCDModuleInst_Backend_DeadError(i);
  271. }
  272. static void use_func_die (void *vo)
  273. {
  274. struct use_instance *o = vo;
  275. // remove from blocker's list
  276. LinkedList1_Remove(&o->blocker->users, &o->blocker_node);
  277. // make the blocker die if needed
  278. if (o->blocker->dying && LinkedList1_IsEmpty(&o->blocker->users)) {
  279. instance_free(o->blocker);
  280. }
  281. NCDModuleInst_Backend_Dead(o->i);
  282. }
  283. static struct NCDModule modules[] = {
  284. {
  285. .type = "blocker",
  286. .func_new2 = func_new,
  287. .func_die = func_die,
  288. .func_getvar2 = func_getvar2,
  289. .alloc_size = sizeof(struct instance)
  290. }, {
  291. .type = "blocker::up",
  292. .func_new2 = up_func_new
  293. }, {
  294. .type = "blocker::down",
  295. .func_new2 = down_func_new
  296. }, {
  297. .type = "blocker::downup",
  298. .func_new2 = downup_func_new
  299. }, {
  300. .type = "blocker::rdownup",
  301. .func_new2 = rdownup_func_new,
  302. .func_die = rdownup_func_die,
  303. .alloc_size = sizeof(struct rdownup_instance)
  304. }, {
  305. .type = "blocker::use",
  306. .func_new2 = use_func_new,
  307. .func_die = use_func_die,
  308. .alloc_size = sizeof(struct use_instance)
  309. }, {
  310. .type = NULL
  311. }
  312. };
  313. const struct NCDModuleGroup ncdmodule_blocker = {
  314. .modules = modules
  315. };