depend.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /**
  2. * @file depend.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. * Dependencies module.
  32. *
  33. * Synopsis: provide(string name)
  34. * Description: Provides a resource. On initialization, transitions any depend()-s
  35. * waiting for this resource to UP state. On deinitialization, transitions
  36. * depend()-s using this resource to DOWN state, and waits for all of them to
  37. * receive the clean signal (i.e. wait for all of the statements following them in
  38. * their processes to terminate). Initialization fails if a provide() already
  39. * exists for this resource (including if it is being deinitialized).
  40. *
  41. * Synopsis: provide_event(string name)
  42. * Description: Like provide(), but if another provide() already exists for this
  43. * resource, initialization does not fail, and the request is queued to the active
  44. * provide() for this resource. When an active provide() disappears that has
  45. * queued provide()-s, one of them is promoted to be the active provide() for this
  46. * resource, and the remaining queue is transferred to it.
  47. * (mentions of provide() in this text also apply to provide_event())
  48. *
  49. * Synopsis: depend(string name)
  50. * Description: Depends on a resource. Is in UP state when a provide()
  51. * for this resource is available, and in DOWN state when it is not (either
  52. * it does not exist or is being terminated).
  53. * Variables: Provides variables available from the corresponding provide,
  54. * ("modname.varname" or "modname").
  55. */
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <misc/offset.h>
  59. #include <misc/debug.h>
  60. #include <structure/LinkedList2.h>
  61. #include <structure/LinkedList3.h>
  62. #include <ncd/NCDModule.h>
  63. #include <generated/blog_channel_ncd_depend.h>
  64. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  65. struct provide {
  66. NCDModuleInst *i;
  67. const char *name;
  68. int is_queued;
  69. union {
  70. struct {
  71. LinkedList3Node queued_node; // node in list which begins with provide.queued_provides_firstnode
  72. };
  73. struct {
  74. LinkedList2Node provides_node; // node in provides
  75. LinkedList2 depends;
  76. LinkedList3Node queued_provides_firstnode;
  77. int dying;
  78. };
  79. };
  80. };
  81. struct depend {
  82. NCDModuleInst *i;
  83. const char *name;
  84. struct provide *p;
  85. LinkedList2Node node;
  86. };
  87. static LinkedList2 provides;
  88. static LinkedList2 free_depends;
  89. static struct provide * find_provide (const char *name)
  90. {
  91. LinkedList2Iterator it;
  92. LinkedList2Iterator_InitForward(&it, &provides);
  93. LinkedList2Node *n;
  94. while (n = LinkedList2Iterator_Next(&it)) {
  95. struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
  96. ASSERT(!p->is_queued)
  97. if (!strcmp(p->name, name)) {
  98. LinkedList2Iterator_Free(&it);
  99. return p;
  100. }
  101. }
  102. return NULL;
  103. }
  104. static void provide_promote (struct provide *o)
  105. {
  106. ASSERT(!find_provide(o->name))
  107. // set not queued
  108. o->is_queued = 0;
  109. // insert to provides list
  110. LinkedList2_Append(&provides, &o->provides_node);
  111. // init depends list
  112. LinkedList2_Init(&o->depends);
  113. // set not dying
  114. o->dying = 0;
  115. // attach free depends with this name
  116. LinkedList2Iterator it;
  117. LinkedList2Iterator_InitForward(&it, &free_depends);
  118. LinkedList2Node *n;
  119. while (n = LinkedList2Iterator_Next(&it)) {
  120. struct depend *d = UPPER_OBJECT(n, struct depend, node);
  121. ASSERT(!d->p)
  122. if (strcmp(d->name, o->name)) {
  123. continue;
  124. }
  125. // remove from free depends list
  126. LinkedList2_Remove(&free_depends, &d->node);
  127. // insert to provide's list
  128. LinkedList2_Append(&o->depends, &d->node);
  129. // set provide
  130. d->p = o;
  131. // signal up
  132. NCDModuleInst_Backend_Up(d->i);
  133. }
  134. }
  135. static int func_globalinit (struct NCDModuleInitParams params)
  136. {
  137. // init provides list
  138. LinkedList2_Init(&provides);
  139. // init free depends list
  140. LinkedList2_Init(&free_depends);
  141. return 1;
  142. }
  143. static void provide_func_new_templ (void *vo, NCDModuleInst *i, int event)
  144. {
  145. struct provide *o = vo;
  146. o->i = i;
  147. // read arguments
  148. NCDValRef name_arg;
  149. if (!NCDVal_ListRead(i->args, 1, &name_arg)) {
  150. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  151. goto fail0;
  152. }
  153. if (!NCDVal_IsStringNoNulls(name_arg)) {
  154. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  155. goto fail0;
  156. }
  157. o->name = NCDVal_StringValue(name_arg);
  158. // signal up.
  159. // This comes above provide_promote(), so that effects on related depend statements are
  160. // computed before this process advances, avoiding problems like failed variable resolutions.
  161. NCDModuleInst_Backend_Up(o->i);
  162. // check for existing provide with this name
  163. struct provide *ep = find_provide(o->name);
  164. if (ep) {
  165. ASSERT(!ep->is_queued)
  166. if (!event) {
  167. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  168. goto fail0;
  169. }
  170. // set queued
  171. o->is_queued = 1;
  172. // insert to existing provide's queued provides list
  173. LinkedList3Node_InitAfter(&o->queued_node, &ep->queued_provides_firstnode);
  174. } else {
  175. // init first node for queued provides list
  176. LinkedList3Node_InitLonely(&o->queued_provides_firstnode);
  177. // promote provide
  178. provide_promote(o);
  179. }
  180. return;
  181. fail0:
  182. NCDModuleInst_Backend_SetError(i);
  183. NCDModuleInst_Backend_Dead(i);
  184. }
  185. static void provide_func_new (void *vo, NCDModuleInst *i)
  186. {
  187. provide_func_new_templ(vo, i, 0);
  188. }
  189. static void provide_event_func_new (void *vo, NCDModuleInst *i)
  190. {
  191. provide_func_new_templ(vo, i, 1);
  192. }
  193. static void provide_free (struct provide *o)
  194. {
  195. ASSERT(o->is_queued || LinkedList2_IsEmpty(&o->depends))
  196. if (o->is_queued) {
  197. // remove from existing provide's queued provides list
  198. LinkedList3Node_Free(&o->queued_node);
  199. } else {
  200. // remove from provides list
  201. LinkedList2_Remove(&provides, &o->provides_node);
  202. // if we have provides queued, promote the first one
  203. if (LinkedList3Node_Next(&o->queued_provides_firstnode)) {
  204. // get first queued provide
  205. struct provide *qp = UPPER_OBJECT(LinkedList3Node_Next(&o->queued_provides_firstnode), struct provide, queued_node);
  206. ASSERT(qp->is_queued)
  207. // make it the head of the queued provides list
  208. LinkedList3Node_Free(&qp->queued_node);
  209. LinkedList3Node_InitAfter(&qp->queued_provides_firstnode, &o->queued_provides_firstnode);
  210. LinkedList3Node_Free(&o->queued_provides_firstnode);
  211. // promote provide
  212. provide_promote(qp);
  213. }
  214. }
  215. NCDModuleInst_Backend_Dead(o->i);
  216. }
  217. static void provide_func_die (void *vo)
  218. {
  219. struct provide *o = vo;
  220. ASSERT(o->is_queued || !o->dying)
  221. // if we are queued or have no depends, die immediately
  222. if (o->is_queued || LinkedList2_IsEmpty(&o->depends)) {
  223. provide_free(o);
  224. return;
  225. }
  226. // set dying
  227. o->dying = 1;
  228. // signal our depends down
  229. LinkedList2Iterator it;
  230. LinkedList2Iterator_InitForward(&it, &o->depends);
  231. LinkedList2Node *n;
  232. while (n = LinkedList2Iterator_Next(&it)) {
  233. struct depend *d = UPPER_OBJECT(n, struct depend, node);
  234. ASSERT(d->p == o)
  235. // signal down
  236. NCDModuleInst_Backend_Down(d->i);
  237. }
  238. }
  239. static void depend_func_new (void *vo, NCDModuleInst *i)
  240. {
  241. struct depend *o = vo;
  242. o->i = i;
  243. // read arguments
  244. NCDValRef name_arg;
  245. if (!NCDVal_ListRead(i->args, 1, &name_arg)) {
  246. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  247. goto fail0;
  248. }
  249. if (!NCDVal_IsStringNoNulls(name_arg)) {
  250. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  251. goto fail0;
  252. }
  253. o->name = NCDVal_StringValue(name_arg);
  254. // find a provide with our name
  255. struct provide *p = find_provide(o->name);
  256. ASSERT(!p || !p->is_queued)
  257. if (p && !p->dying) {
  258. // insert to provide's list
  259. LinkedList2_Append(&p->depends, &o->node);
  260. // set provide
  261. o->p = p;
  262. // signal up
  263. NCDModuleInst_Backend_Up(o->i);
  264. } else {
  265. // insert to free depends list
  266. LinkedList2_Append(&free_depends, &o->node);
  267. // set no provide
  268. o->p = NULL;
  269. }
  270. return;
  271. fail0:
  272. NCDModuleInst_Backend_SetError(i);
  273. NCDModuleInst_Backend_Dead(i);
  274. }
  275. static void depend_free (struct depend *o)
  276. {
  277. ASSERT(!o->p || !o->p->is_queued)
  278. if (o->p) {
  279. // remove from provide's list
  280. LinkedList2_Remove(&o->p->depends, &o->node);
  281. // if provide is dying and is empty, let it die
  282. if (o->p->dying && LinkedList2_IsEmpty(&o->p->depends)) {
  283. provide_free(o->p);
  284. }
  285. } else {
  286. // remove free depends list
  287. LinkedList2_Remove(&free_depends, &o->node);
  288. }
  289. NCDModuleInst_Backend_Dead(o->i);
  290. }
  291. static void depend_func_die (void *vo)
  292. {
  293. struct depend *o = vo;
  294. depend_free(o);
  295. }
  296. static void depend_func_clean (void *vo)
  297. {
  298. struct depend *o = vo;
  299. ASSERT(!o->p || !o->p->is_queued)
  300. if (!(o->p && o->p->dying)) {
  301. return;
  302. }
  303. struct provide *p = o->p;
  304. // remove from provide's list
  305. LinkedList2_Remove(&p->depends, &o->node);
  306. // insert to free depends list
  307. LinkedList2_Append(&free_depends, &o->node);
  308. // set no provide
  309. o->p = NULL;
  310. // if provide is empty, let it die
  311. if (LinkedList2_IsEmpty(&p->depends)) {
  312. provide_free(p);
  313. }
  314. }
  315. static int depend_func_getobj (void *vo, const char *objname, NCDObject *out_object)
  316. {
  317. struct depend *o = vo;
  318. ASSERT(!o->p || !o->p->is_queued)
  319. if (!o->p) {
  320. return 0;
  321. }
  322. return NCDModuleInst_Backend_GetObj(o->p->i, objname, out_object);
  323. }
  324. static const struct NCDModule modules[] = {
  325. {
  326. .type = "provide",
  327. .func_new2 = provide_func_new,
  328. .func_die = provide_func_die,
  329. .alloc_size = sizeof(struct provide)
  330. }, {
  331. .type = "provide_event",
  332. .func_new2 = provide_event_func_new,
  333. .func_die = provide_func_die,
  334. .alloc_size = sizeof(struct provide)
  335. }, {
  336. .type = "depend",
  337. .func_new2 = depend_func_new,
  338. .func_die = depend_func_die,
  339. .func_clean = depend_func_clean,
  340. .func_getobj = depend_func_getobj,
  341. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  342. .alloc_size = sizeof(struct depend)
  343. }, {
  344. .type = NULL
  345. }
  346. };
  347. const struct NCDModuleGroup ncdmodule_depend = {
  348. .func_globalinit = func_globalinit,
  349. .modules = modules
  350. };