depend.c 13 KB

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