blocker.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 use_instance {
  62. NCDModuleInst *i;
  63. struct instance *blocker;
  64. LinkedList2Node blocker_node;
  65. };
  66. static void func_new (NCDModuleInst *i)
  67. {
  68. // allocate instance
  69. struct instance *o = malloc(sizeof(*o));
  70. if (!o) {
  71. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  72. goto fail0;
  73. }
  74. NCDModuleInst_Backend_SetUser(i, o);
  75. // init arguments
  76. o->i = i;
  77. // check arguments
  78. if (!NCDValue_ListRead(o->i->args, 0)) {
  79. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  80. goto fail1;
  81. }
  82. // init users list
  83. LinkedList2_Init(&o->users);
  84. // set not up
  85. o->up = 0;
  86. // set not dying
  87. o->dying = 0;
  88. // signal up
  89. NCDModuleInst_Backend_Up(o->i);
  90. return;
  91. fail1:
  92. free(o);
  93. fail0:
  94. NCDModuleInst_Backend_SetError(i);
  95. NCDModuleInst_Backend_Dead(i);
  96. }
  97. static void instance_free (struct instance *o)
  98. {
  99. ASSERT(LinkedList2_IsEmpty(&o->users))
  100. NCDModuleInst *i = o->i;
  101. // free instance
  102. free(o);
  103. NCDModuleInst_Backend_Dead(i);
  104. }
  105. static void func_die (void *vo)
  106. {
  107. struct instance *o = vo;
  108. ASSERT(!o->dying)
  109. // if we have no users, die right away, else wait for users
  110. if (LinkedList2_IsEmpty(&o->users)) {
  111. instance_free(o);
  112. return;
  113. }
  114. // set dying
  115. o->dying = 1;
  116. }
  117. static void updown_func_new_templ (NCDModuleInst *i, int up)
  118. {
  119. // check arguments
  120. if (!NCDValue_ListRead(i->args, 0)) {
  121. ModuleLog(i, BLOG_ERROR, "wrong arity");
  122. goto fail0;
  123. }
  124. // signal up
  125. NCDModuleInst_Backend_Up(i);
  126. // get method object
  127. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  128. if (mo->up != up) {
  129. // change up state
  130. mo->up = up;
  131. // signal users
  132. LinkedList2Iterator it;
  133. LinkedList2Iterator_InitForward(&it, &mo->users);
  134. LinkedList2Node *node;
  135. while (node = LinkedList2Iterator_Next(&it)) {
  136. struct use_instance *user = UPPER_OBJECT(node, struct use_instance, blocker_node);
  137. ASSERT(user->blocker == mo)
  138. if (up) {
  139. NCDModuleInst_Backend_Up(user->i);
  140. } else {
  141. NCDModuleInst_Backend_Down(user->i);
  142. }
  143. }
  144. }
  145. return;
  146. fail0:
  147. NCDModuleInst_Backend_SetError(i);
  148. NCDModuleInst_Backend_Dead(i);
  149. }
  150. static void up_func_new (NCDModuleInst *i)
  151. {
  152. updown_func_new_templ(i, 1);
  153. }
  154. static void down_func_new (NCDModuleInst *i)
  155. {
  156. updown_func_new_templ(i, 0);
  157. }
  158. static void use_func_new (NCDModuleInst *i)
  159. {
  160. // allocate instance
  161. struct use_instance *o = malloc(sizeof(*o));
  162. if (!o) {
  163. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  164. goto fail0;
  165. }
  166. NCDModuleInst_Backend_SetUser(i, o);
  167. // init arguments
  168. o->i = i;
  169. // check arguments
  170. if (!NCDValue_ListRead(o->i->args, 0)) {
  171. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  172. goto fail1;
  173. }
  174. // set blocker
  175. o->blocker = ((NCDModuleInst *)i->method_user)->inst_user;
  176. // add to blocker's list
  177. LinkedList2_Append(&o->blocker->users, &o->blocker_node);
  178. // signal up if needed
  179. if (o->blocker->up) {
  180. NCDModuleInst_Backend_Up(o->i);
  181. }
  182. return;
  183. fail1:
  184. free(o);
  185. fail0:
  186. NCDModuleInst_Backend_SetError(i);
  187. NCDModuleInst_Backend_Dead(i);
  188. }
  189. static void use_func_die (void *vo)
  190. {
  191. struct use_instance *o = vo;
  192. NCDModuleInst *i = o->i;
  193. // remove from blocker's list
  194. LinkedList2_Remove(&o->blocker->users, &o->blocker_node);
  195. // make the blocker die if needed
  196. if (o->blocker->dying && LinkedList2_IsEmpty(&o->blocker->users)) {
  197. instance_free(o->blocker);
  198. }
  199. // free instance
  200. free(o);
  201. NCDModuleInst_Backend_Dead(i);
  202. }
  203. static const struct NCDModule modules[] = {
  204. {
  205. .type = "blocker",
  206. .func_new = func_new,
  207. .func_die = func_die
  208. }, {
  209. .type = "blocker::up",
  210. .func_new = up_func_new
  211. }, {
  212. .type = "blocker::down",
  213. .func_new = down_func_new
  214. }, {
  215. .type = "blocker::use",
  216. .func_new = use_func_new,
  217. .func_die = use_func_die,
  218. }, {
  219. .type = NULL
  220. }
  221. };
  222. const struct NCDModuleGroup ncdmodule_blocker = {
  223. .modules = modules
  224. };