blocker.c 11 KB

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