blocker.c 7.4 KB

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