depend.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 <misc/balloc.h>
  61. #include <structure/LinkedList1.h>
  62. #include <structure/LinkedList3.h>
  63. #include <ncd/module_common.h>
  64. #include <generated/blog_channel_ncd_depend.h>
  65. struct provide {
  66. NCDModuleInst *i;
  67. MemRef 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. LinkedList1Node provides_node; // node in provides
  75. LinkedList1 depends;
  76. LinkedList3Node queued_provides_firstnode;
  77. int dying;
  78. };
  79. };
  80. };
  81. struct depend {
  82. NCDModuleInst *i;
  83. MemRef name;
  84. struct provide *p;
  85. LinkedList1Node node;
  86. };
  87. struct global {
  88. LinkedList1 provides;
  89. LinkedList1 free_depends;
  90. };
  91. static struct provide * find_provide (struct global *g, MemRef name)
  92. {
  93. for (LinkedList1Node *n = LinkedList1_GetFirst(&g->provides); n; n = LinkedList1Node_Next(n)) {
  94. struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
  95. ASSERT(!p->is_queued)
  96. if (MemRef_Equal(p->name, name)) {
  97. return p;
  98. }
  99. }
  100. return NULL;
  101. }
  102. static void provide_promote (struct provide *o)
  103. {
  104. struct global *g = ModuleGlobal(o->i);
  105. ASSERT(!find_provide(g, o->name))
  106. // set not queued
  107. o->is_queued = 0;
  108. // insert to provides list
  109. LinkedList1_Append(&g->provides, &o->provides_node);
  110. // init depends list
  111. LinkedList1_Init(&o->depends);
  112. // set not dying
  113. o->dying = 0;
  114. // attach free depends with this name
  115. LinkedList1Node *n = LinkedList1_GetFirst(&g->free_depends);
  116. while (n) {
  117. LinkedList1Node *next = LinkedList1Node_Next(n);
  118. struct depend *d = UPPER_OBJECT(n, struct depend, node);
  119. ASSERT(!d->p)
  120. if (!MemRef_Equal(d->name, o->name)) {
  121. n = next;
  122. continue;
  123. }
  124. // remove from free depends list
  125. LinkedList1_Remove(&g->free_depends, &d->node);
  126. // insert to provide's list
  127. LinkedList1_Append(&o->depends, &d->node);
  128. // set provide
  129. d->p = o;
  130. // signal up
  131. NCDModuleInst_Backend_Up(d->i);
  132. n = next;
  133. }
  134. }
  135. static int func_globalinit (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params)
  136. {
  137. // allocate global state structure
  138. struct global *g = BAlloc(sizeof(*g));
  139. if (!g) {
  140. BLog(BLOG_ERROR, "BAlloc failed");
  141. return 0;
  142. }
  143. // set group state pointer
  144. group->group_state = g;
  145. // init provides list
  146. LinkedList1_Init(&g->provides);
  147. // init free depends list
  148. LinkedList1_Init(&g->free_depends);
  149. return 1;
  150. }
  151. static void func_globalfree (struct NCDInterpModuleGroup *group)
  152. {
  153. struct global *g = group->group_state;
  154. ASSERT(LinkedList1_IsEmpty(&g->free_depends))
  155. ASSERT(LinkedList1_IsEmpty(&g->provides))
  156. // free global state structure
  157. BFree(g);
  158. }
  159. static void provide_func_new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int event)
  160. {
  161. struct global *g = ModuleGlobal(i);
  162. struct provide *o = vo;
  163. o->i = i;
  164. // read arguments
  165. NCDValRef name_arg;
  166. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  167. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  168. goto fail0;
  169. }
  170. if (!NCDVal_IsString(name_arg)) {
  171. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  172. goto fail0;
  173. }
  174. o->name = NCDVal_StringMemRef(name_arg);
  175. // signal up.
  176. // This comes above provide_promote(), so that effects on related depend statements are
  177. // computed before this process advances, avoiding problems like failed variable resolutions.
  178. NCDModuleInst_Backend_Up(o->i);
  179. // check for existing provide with this name
  180. struct provide *ep = find_provide(g, o->name);
  181. if (ep) {
  182. ASSERT(!ep->is_queued)
  183. if (!event) {
  184. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  185. goto fail0;
  186. }
  187. // set queued
  188. o->is_queued = 1;
  189. // insert to existing provide's queued provides list
  190. LinkedList3Node_InitAfter(&o->queued_node, &ep->queued_provides_firstnode);
  191. } else {
  192. // init first node for queued provides list
  193. LinkedList3Node_InitLonely(&o->queued_provides_firstnode);
  194. // promote provide
  195. provide_promote(o);
  196. }
  197. return;
  198. fail0:
  199. NCDModuleInst_Backend_DeadError(i);
  200. }
  201. static void provide_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  202. {
  203. provide_func_new_templ(vo, i, params, 0);
  204. }
  205. static void provide_event_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  206. {
  207. provide_func_new_templ(vo, i, params, 1);
  208. }
  209. static void provide_free (struct provide *o)
  210. {
  211. struct global *g = ModuleGlobal(o->i);
  212. ASSERT(o->is_queued || LinkedList1_IsEmpty(&o->depends))
  213. if (o->is_queued) {
  214. // remove from existing provide's queued provides list
  215. LinkedList3Node_Free(&o->queued_node);
  216. } else {
  217. // remove from provides list
  218. LinkedList1_Remove(&g->provides, &o->provides_node);
  219. // if we have provides queued, promote the first one
  220. if (LinkedList3Node_Next(&o->queued_provides_firstnode)) {
  221. // get first queued provide
  222. struct provide *qp = UPPER_OBJECT(LinkedList3Node_Next(&o->queued_provides_firstnode), struct provide, queued_node);
  223. ASSERT(qp->is_queued)
  224. // make it the head of the queued provides list
  225. LinkedList3Node_Free(&qp->queued_node);
  226. LinkedList3Node_InitAfter(&qp->queued_provides_firstnode, &o->queued_provides_firstnode);
  227. LinkedList3Node_Free(&o->queued_provides_firstnode);
  228. // promote provide
  229. provide_promote(qp);
  230. }
  231. }
  232. NCDModuleInst_Backend_Dead(o->i);
  233. }
  234. static void provide_func_die (void *vo)
  235. {
  236. struct provide *o = vo;
  237. ASSERT(o->is_queued || !o->dying)
  238. // if we are queued or have no depends, die immediately
  239. if (o->is_queued || LinkedList1_IsEmpty(&o->depends)) {
  240. provide_free(o);
  241. return;
  242. }
  243. // set dying
  244. o->dying = 1;
  245. // signal our depends down
  246. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->depends); n; n = LinkedList1Node_Next(n)) {
  247. struct depend *d = UPPER_OBJECT(n, struct depend, node);
  248. ASSERT(d->p == o)
  249. // signal down
  250. NCDModuleInst_Backend_Down(d->i);
  251. }
  252. }
  253. static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  254. {
  255. struct global *g = ModuleGlobal(i);
  256. struct depend *o = vo;
  257. o->i = i;
  258. // read arguments
  259. NCDValRef name_arg;
  260. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  261. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  262. goto fail0;
  263. }
  264. if (!NCDVal_IsString(name_arg)) {
  265. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  266. goto fail0;
  267. }
  268. o->name = NCDVal_StringMemRef(name_arg);
  269. // find a provide with our name
  270. struct provide *p = find_provide(g, o->name);
  271. ASSERT(!p || !p->is_queued)
  272. if (p && !p->dying) {
  273. // insert to provide's list
  274. LinkedList1_Append(&p->depends, &o->node);
  275. // set provide
  276. o->p = p;
  277. // signal up
  278. NCDModuleInst_Backend_Up(o->i);
  279. } else {
  280. // insert to free depends list
  281. LinkedList1_Append(&g->free_depends, &o->node);
  282. // set no provide
  283. o->p = NULL;
  284. }
  285. return;
  286. fail0:
  287. NCDModuleInst_Backend_DeadError(i);
  288. }
  289. static void depend_free (struct depend *o)
  290. {
  291. struct global *g = ModuleGlobal(o->i);
  292. ASSERT(!o->p || !o->p->is_queued)
  293. if (o->p) {
  294. // remove from provide's list
  295. LinkedList1_Remove(&o->p->depends, &o->node);
  296. // if provide is dying and is empty, let it die
  297. if (o->p->dying && LinkedList1_IsEmpty(&o->p->depends)) {
  298. provide_free(o->p);
  299. }
  300. } else {
  301. // remove free depends list
  302. LinkedList1_Remove(&g->free_depends, &o->node);
  303. }
  304. NCDModuleInst_Backend_Dead(o->i);
  305. }
  306. static void depend_func_die (void *vo)
  307. {
  308. struct depend *o = vo;
  309. depend_free(o);
  310. }
  311. static void depend_func_clean (void *vo)
  312. {
  313. struct depend *o = vo;
  314. struct global *g = ModuleGlobal(o->i);
  315. ASSERT(!o->p || !o->p->is_queued)
  316. if (!(o->p && o->p->dying)) {
  317. return;
  318. }
  319. struct provide *p = o->p;
  320. // remove from provide's list
  321. LinkedList1_Remove(&p->depends, &o->node);
  322. // insert to free depends list
  323. LinkedList1_Append(&g->free_depends, &o->node);
  324. // set no provide
  325. o->p = NULL;
  326. // if provide is empty, let it die
  327. if (LinkedList1_IsEmpty(&p->depends)) {
  328. provide_free(p);
  329. }
  330. }
  331. static int depend_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  332. {
  333. struct depend *o = vo;
  334. ASSERT(!o->p || !o->p->is_queued)
  335. if (!o->p) {
  336. return 0;
  337. }
  338. return NCDModuleInst_Backend_GetObj(o->p->i, objname, out_object);
  339. }
  340. static struct NCDModule modules[] = {
  341. {
  342. .type = "provide",
  343. .func_new2 = provide_func_new,
  344. .func_die = provide_func_die,
  345. .alloc_size = sizeof(struct provide)
  346. }, {
  347. .type = "provide_event",
  348. .func_new2 = provide_event_func_new,
  349. .func_die = provide_func_die,
  350. .alloc_size = sizeof(struct provide)
  351. }, {
  352. .type = "depend",
  353. .func_new2 = depend_func_new,
  354. .func_die = depend_func_die,
  355. .func_clean = depend_func_clean,
  356. .func_getobj = depend_func_getobj,
  357. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  358. .alloc_size = sizeof(struct depend)
  359. }, {
  360. .type = NULL
  361. }
  362. };
  363. const struct NCDModuleGroup ncdmodule_depend = {
  364. .func_globalinit = func_globalinit,
  365. .func_globalfree = func_globalfree,
  366. .modules = modules
  367. };