depend.c 12 KB

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