blocker.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. o->i = i;
  106. NCDModuleInst_Backend_SetUser(i, o);
  107. // check arguments
  108. if (!NCDVal_ListRead(o->i->args, 0)) {
  109. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  110. goto fail1;
  111. }
  112. // init users list
  113. LinkedList2_Init(&o->users);
  114. // init rdownups list
  115. LinkedList0_Init(&o->rdownups_list);
  116. // set not up
  117. o->up = 0;
  118. // set not dying
  119. o->dying = 0;
  120. // signal up
  121. NCDModuleInst_Backend_Up(o->i);
  122. return;
  123. fail1:
  124. free(o);
  125. fail0:
  126. NCDModuleInst_Backend_SetError(i);
  127. NCDModuleInst_Backend_Dead(i);
  128. }
  129. static void instance_free (struct instance *o)
  130. {
  131. ASSERT(LinkedList2_IsEmpty(&o->users))
  132. NCDModuleInst *i = o->i;
  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. // free instance
  142. free(o);
  143. NCDModuleInst_Backend_Dead(i);
  144. }
  145. static void func_die (void *vo)
  146. {
  147. struct instance *o = vo;
  148. ASSERT(!o->dying)
  149. // if we have no users, die right away, else wait for users
  150. if (LinkedList2_IsEmpty(&o->users)) {
  151. instance_free(o);
  152. return;
  153. }
  154. // set dying
  155. o->dying = 1;
  156. }
  157. static void updown_func_new_templ (NCDModuleInst *i, int up, int first_down)
  158. {
  159. ASSERT(!first_down || up)
  160. // check arguments
  161. if (!NCDVal_ListRead(i->args, 0)) {
  162. ModuleLog(i, BLOG_ERROR, "wrong arity");
  163. goto fail0;
  164. }
  165. // signal up
  166. NCDModuleInst_Backend_Up(i);
  167. // get method object
  168. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  169. if (first_down || mo->up != up) {
  170. // signal users
  171. LinkedList2Iterator it;
  172. LinkedList2Iterator_InitForward(&it, &mo->users);
  173. LinkedList2Node *node;
  174. while (node = LinkedList2Iterator_Next(&it)) {
  175. struct use_instance *user = UPPER_OBJECT(node, struct use_instance, blocker_node);
  176. ASSERT(user->blocker == mo)
  177. if (first_down && mo->up) {
  178. NCDModuleInst_Backend_Down(user->i);
  179. }
  180. if (up) {
  181. NCDModuleInst_Backend_Up(user->i);
  182. } else {
  183. NCDModuleInst_Backend_Down(user->i);
  184. }
  185. }
  186. // change up state
  187. mo->up = up;
  188. }
  189. return;
  190. fail0:
  191. NCDModuleInst_Backend_SetError(i);
  192. NCDModuleInst_Backend_Dead(i);
  193. }
  194. static void up_func_new (NCDModuleInst *i)
  195. {
  196. updown_func_new_templ(i, 1, 0);
  197. }
  198. static void down_func_new (NCDModuleInst *i)
  199. {
  200. updown_func_new_templ(i, 0, 0);
  201. }
  202. static void downup_func_new (NCDModuleInst *i)
  203. {
  204. updown_func_new_templ(i, 1, 1);
  205. }
  206. static void rdownup_func_new (NCDModuleInst *i)
  207. {
  208. // allocate structure
  209. struct rdownup_instance *o = malloc(sizeof(*o));
  210. if (!o) {
  211. ModuleLog(i, BLOG_ERROR, "malloc failed");
  212. goto fail0;
  213. }
  214. o->i = i;
  215. NCDModuleInst_Backend_SetUser(i, o);
  216. // check arguments
  217. if (!NCDVal_ListRead(i->args, 0)) {
  218. ModuleLog(i, BLOG_ERROR, "wrong arity");
  219. goto fail1;
  220. }
  221. // get blocker
  222. struct instance *blk = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  223. // set blocker
  224. o->blocker = blk;
  225. // insert to rdownups list
  226. LinkedList0_Prepend(&blk->rdownups_list, &o->rdownups_list_node);
  227. // signal up
  228. NCDModuleInst_Backend_Up(i);
  229. return;
  230. fail1:
  231. free(o);
  232. fail0:
  233. NCDModuleInst_Backend_SetError(i);
  234. NCDModuleInst_Backend_Dead(i);
  235. }
  236. static void rdownup_func_die (void *vo)
  237. {
  238. struct rdownup_instance *o = vo;
  239. NCDModuleInst *i = o->i;
  240. struct instance *blk = o->blocker;
  241. if (blk) {
  242. // remove from rdownups list
  243. LinkedList0_Remove(&blk->rdownups_list, &o->rdownups_list_node);
  244. // downup users
  245. for (LinkedList2Node *ln = LinkedList2_GetFirst(&blk->users); ln; ln = LinkedList2Node_Next(ln)) {
  246. struct use_instance *user = UPPER_OBJECT(ln, struct use_instance, blocker_node);
  247. ASSERT(user->blocker == blk)
  248. if (blk->up) {
  249. NCDModuleInst_Backend_Down(user->i);
  250. }
  251. NCDModuleInst_Backend_Up(user->i);
  252. }
  253. // set up
  254. blk->up = 1;
  255. }
  256. // free instance
  257. free(o);
  258. NCDModuleInst_Backend_Dead(i);
  259. }
  260. static void use_func_new (NCDModuleInst *i)
  261. {
  262. // allocate instance
  263. struct use_instance *o = malloc(sizeof(*o));
  264. if (!o) {
  265. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  266. goto fail0;
  267. }
  268. NCDModuleInst_Backend_SetUser(i, o);
  269. // init arguments
  270. o->i = i;
  271. // check arguments
  272. if (!NCDVal_ListRead(o->i->args, 0)) {
  273. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  274. goto fail1;
  275. }
  276. // set blocker
  277. o->blocker = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  278. // add to blocker's list
  279. LinkedList2_Append(&o->blocker->users, &o->blocker_node);
  280. // signal up if needed
  281. if (o->blocker->up) {
  282. NCDModuleInst_Backend_Up(o->i);
  283. }
  284. return;
  285. fail1:
  286. free(o);
  287. fail0:
  288. NCDModuleInst_Backend_SetError(i);
  289. NCDModuleInst_Backend_Dead(i);
  290. }
  291. static void use_func_die (void *vo)
  292. {
  293. struct use_instance *o = vo;
  294. NCDModuleInst *i = o->i;
  295. // remove from blocker's list
  296. LinkedList2_Remove(&o->blocker->users, &o->blocker_node);
  297. // make the blocker die if needed
  298. if (o->blocker->dying && LinkedList2_IsEmpty(&o->blocker->users)) {
  299. instance_free(o->blocker);
  300. }
  301. // free instance
  302. free(o);
  303. NCDModuleInst_Backend_Dead(i);
  304. }
  305. static const struct NCDModule modules[] = {
  306. {
  307. .type = "blocker",
  308. .func_new = func_new,
  309. .func_die = func_die
  310. }, {
  311. .type = "blocker::up",
  312. .func_new = up_func_new
  313. }, {
  314. .type = "blocker::down",
  315. .func_new = down_func_new
  316. }, {
  317. .type = "blocker::downup",
  318. .func_new = downup_func_new
  319. }, {
  320. .type = "blocker::rdownup",
  321. .func_new = rdownup_func_new,
  322. .func_die = rdownup_func_die
  323. }, {
  324. .type = "blocker::use",
  325. .func_new = use_func_new,
  326. .func_die = use_func_die,
  327. }, {
  328. .type = NULL
  329. }
  330. };
  331. const struct NCDModuleGroup ncdmodule_blocker = {
  332. .modules = modules
  333. };