blocker.c 11 KB

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