blocker.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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).
  37. *
  38. * Synopsis: blocker::up()
  39. * Description: sets the blocking state to up.
  40. *
  41. * Synopsis: blocker::down()
  42. * Description: sets the blocking state to down.
  43. *
  44. * Synopsis: blocker::downup()
  45. * Description: atomically sets the blocker to down state (if it was up), then (back) to up state.
  46. * Note that this is not equivalent to calling down() and immediately up(); in that case,
  47. * the interpreter will first handle the immediate effects of any use() statements
  48. * going down as a result of having called down() and will only later execute the up()
  49. * statement. In fact, it is possible that the effects of down() will prevent up() from
  50. * executing, which may leave the program in an undesirable state.
  51. *
  52. * Synopsis: blocker::use()
  53. * Description: blocks on the blocker. This module is in up state if and only if the blocking state of
  54. * the blocker is up. Multiple use statements may be used with the same blocker.
  55. */
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <misc/offset.h>
  59. #include <misc/debug.h>
  60. #include <ncd/NCDModule.h>
  61. #include <generated/blog_channel_ncd_blocker.h>
  62. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  63. struct instance {
  64. NCDModuleInst *i;
  65. LinkedList2 users;
  66. int up;
  67. int dying;
  68. };
  69. struct use_instance {
  70. NCDModuleInst *i;
  71. struct instance *blocker;
  72. LinkedList2Node blocker_node;
  73. };
  74. static void func_new (NCDModuleInst *i)
  75. {
  76. // allocate instance
  77. struct instance *o = malloc(sizeof(*o));
  78. if (!o) {
  79. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  80. goto fail0;
  81. }
  82. NCDModuleInst_Backend_SetUser(i, o);
  83. // init arguments
  84. o->i = i;
  85. // check arguments
  86. if (!NCDValue_ListRead(o->i->args, 0)) {
  87. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  88. goto fail1;
  89. }
  90. // init users list
  91. LinkedList2_Init(&o->users);
  92. // set not up
  93. o->up = 0;
  94. // set not dying
  95. o->dying = 0;
  96. // signal up
  97. NCDModuleInst_Backend_Up(o->i);
  98. return;
  99. fail1:
  100. free(o);
  101. fail0:
  102. NCDModuleInst_Backend_SetError(i);
  103. NCDModuleInst_Backend_Dead(i);
  104. }
  105. static void instance_free (struct instance *o)
  106. {
  107. ASSERT(LinkedList2_IsEmpty(&o->users))
  108. NCDModuleInst *i = o->i;
  109. // free instance
  110. free(o);
  111. NCDModuleInst_Backend_Dead(i);
  112. }
  113. static void func_die (void *vo)
  114. {
  115. struct instance *o = vo;
  116. ASSERT(!o->dying)
  117. // if we have no users, die right away, else wait for users
  118. if (LinkedList2_IsEmpty(&o->users)) {
  119. instance_free(o);
  120. return;
  121. }
  122. // set dying
  123. o->dying = 1;
  124. }
  125. static void updown_func_new_templ (NCDModuleInst *i, int up, int first_down)
  126. {
  127. ASSERT(!first_down || up)
  128. // check arguments
  129. if (!NCDValue_ListRead(i->args, 0)) {
  130. ModuleLog(i, BLOG_ERROR, "wrong arity");
  131. goto fail0;
  132. }
  133. // signal up
  134. NCDModuleInst_Backend_Up(i);
  135. // get method object
  136. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  137. if (first_down || mo->up != up) {
  138. // signal users
  139. LinkedList2Iterator it;
  140. LinkedList2Iterator_InitForward(&it, &mo->users);
  141. LinkedList2Node *node;
  142. while (node = LinkedList2Iterator_Next(&it)) {
  143. struct use_instance *user = UPPER_OBJECT(node, struct use_instance, blocker_node);
  144. ASSERT(user->blocker == mo)
  145. if (first_down && mo->up) {
  146. NCDModuleInst_Backend_Down(user->i);
  147. }
  148. if (up) {
  149. NCDModuleInst_Backend_Up(user->i);
  150. } else {
  151. NCDModuleInst_Backend_Down(user->i);
  152. }
  153. }
  154. // change up state
  155. mo->up = up;
  156. }
  157. return;
  158. fail0:
  159. NCDModuleInst_Backend_SetError(i);
  160. NCDModuleInst_Backend_Dead(i);
  161. }
  162. static void up_func_new (NCDModuleInst *i)
  163. {
  164. updown_func_new_templ(i, 1, 0);
  165. }
  166. static void down_func_new (NCDModuleInst *i)
  167. {
  168. updown_func_new_templ(i, 0, 0);
  169. }
  170. static void downup_func_new (NCDModuleInst *i)
  171. {
  172. updown_func_new_templ(i, 1, 1);
  173. }
  174. static void use_func_new (NCDModuleInst *i)
  175. {
  176. // allocate instance
  177. struct use_instance *o = malloc(sizeof(*o));
  178. if (!o) {
  179. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  180. goto fail0;
  181. }
  182. NCDModuleInst_Backend_SetUser(i, o);
  183. // init arguments
  184. o->i = i;
  185. // check arguments
  186. if (!NCDValue_ListRead(o->i->args, 0)) {
  187. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  188. goto fail1;
  189. }
  190. // set blocker
  191. o->blocker = ((NCDModuleInst *)i->method_user)->inst_user;
  192. // add to blocker's list
  193. LinkedList2_Append(&o->blocker->users, &o->blocker_node);
  194. // signal up if needed
  195. if (o->blocker->up) {
  196. NCDModuleInst_Backend_Up(o->i);
  197. }
  198. return;
  199. fail1:
  200. free(o);
  201. fail0:
  202. NCDModuleInst_Backend_SetError(i);
  203. NCDModuleInst_Backend_Dead(i);
  204. }
  205. static void use_func_die (void *vo)
  206. {
  207. struct use_instance *o = vo;
  208. NCDModuleInst *i = o->i;
  209. // remove from blocker's list
  210. LinkedList2_Remove(&o->blocker->users, &o->blocker_node);
  211. // make the blocker die if needed
  212. if (o->blocker->dying && LinkedList2_IsEmpty(&o->blocker->users)) {
  213. instance_free(o->blocker);
  214. }
  215. // free instance
  216. free(o);
  217. NCDModuleInst_Backend_Dead(i);
  218. }
  219. static const struct NCDModule modules[] = {
  220. {
  221. .type = "blocker",
  222. .func_new = func_new,
  223. .func_die = func_die
  224. }, {
  225. .type = "blocker::up",
  226. .func_new = up_func_new
  227. }, {
  228. .type = "blocker::down",
  229. .func_new = down_func_new
  230. }, {
  231. .type = "blocker::downup",
  232. .func_new = downup_func_new
  233. }, {
  234. .type = "blocker::use",
  235. .func_new = use_func_new,
  236. .func_die = use_func_die,
  237. }, {
  238. .type = NULL
  239. }
  240. };
  241. const struct NCDModuleGroup ncdmodule_blocker = {
  242. .modules = modules
  243. };