depend.c 13 KB

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