blocker.c 11 KB

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