depend.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. 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. static LinkedList1 provides;
  90. static LinkedList1 free_depends;
  91. static struct provide * find_provide (const char *name, size_t name_len)
  92. {
  93. for (LinkedList1Node *n = LinkedList1_GetFirst(&provides); n; n = LinkedList1Node_Next(n)) {
  94. struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
  95. ASSERT(!p->is_queued)
  96. if (p->name_len == name_len && !memcmp(p->name, name, name_len)) {
  97. return p;
  98. }
  99. }
  100. return NULL;
  101. }
  102. static void provide_promote (struct provide *o)
  103. {
  104. ASSERT(!find_provide(o->name, o->name_len))
  105. // set not queued
  106. o->is_queued = 0;
  107. // insert to provides list
  108. LinkedList1_Append(&provides, &o->provides_node);
  109. // init depends list
  110. LinkedList1_Init(&o->depends);
  111. // set not dying
  112. o->dying = 0;
  113. // attach free depends with this name
  114. LinkedList1Node *n = LinkedList1_GetFirst(&free_depends);
  115. while (n) {
  116. LinkedList1Node *next = LinkedList1Node_Next(n);
  117. struct depend *d = UPPER_OBJECT(n, struct depend, node);
  118. ASSERT(!d->p)
  119. if (d->name_len != o->name_len || memcmp(d->name, o->name, d->name_len)) {
  120. n = next;
  121. continue;
  122. }
  123. // remove from free depends list
  124. LinkedList1_Remove(&free_depends, &d->node);
  125. // insert to provide's list
  126. LinkedList1_Append(&o->depends, &d->node);
  127. // set provide
  128. d->p = o;
  129. // signal up
  130. NCDModuleInst_Backend_Up(d->i);
  131. n = next;
  132. }
  133. }
  134. static int func_globalinit (const struct NCDModuleInst_iparams *params)
  135. {
  136. // init provides list
  137. LinkedList1_Init(&provides);
  138. // init free depends list
  139. LinkedList1_Init(&free_depends);
  140. return 1;
  141. }
  142. static void provide_func_new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int event)
  143. {
  144. struct provide *o = vo;
  145. o->i = i;
  146. // read arguments
  147. NCDValRef name_arg;
  148. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  149. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  150. goto fail0;
  151. }
  152. if (!NCDVal_IsString(name_arg)) {
  153. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  154. goto fail0;
  155. }
  156. o->name = NCDVal_StringData(name_arg);
  157. o->name_len = NCDVal_StringLength(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, o->name_len);
  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, const struct NCDModuleInst_new_params *params)
  186. {
  187. provide_func_new_templ(vo, i, params, 0);
  188. }
  189. static void provide_event_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  190. {
  191. provide_func_new_templ(vo, i, params, 1);
  192. }
  193. static void provide_free (struct provide *o)
  194. {
  195. ASSERT(o->is_queued || LinkedList1_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. LinkedList1_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 || LinkedList1_IsEmpty(&o->depends)) {
  223. provide_free(o);
  224. return;
  225. }
  226. // set dying
  227. o->dying = 1;
  228. // signal our depends down
  229. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->depends); n; n = LinkedList1Node_Next(n)) {
  230. struct depend *d = UPPER_OBJECT(n, struct depend, node);
  231. ASSERT(d->p == o)
  232. // signal down
  233. NCDModuleInst_Backend_Down(d->i);
  234. }
  235. }
  236. static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  237. {
  238. struct depend *o = vo;
  239. o->i = i;
  240. // read arguments
  241. NCDValRef name_arg;
  242. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  243. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  244. goto fail0;
  245. }
  246. if (!NCDVal_IsString(name_arg)) {
  247. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  248. goto fail0;
  249. }
  250. o->name = NCDVal_StringData(name_arg);
  251. o->name_len = NCDVal_StringLength(name_arg);
  252. // find a provide with our name
  253. struct provide *p = find_provide(o->name, o->name_len);
  254. ASSERT(!p || !p->is_queued)
  255. if (p && !p->dying) {
  256. // insert to provide's list
  257. LinkedList1_Append(&p->depends, &o->node);
  258. // set provide
  259. o->p = p;
  260. // signal up
  261. NCDModuleInst_Backend_Up(o->i);
  262. } else {
  263. // insert to free depends list
  264. LinkedList1_Append(&free_depends, &o->node);
  265. // set no provide
  266. o->p = NULL;
  267. }
  268. return;
  269. fail0:
  270. NCDModuleInst_Backend_SetError(i);
  271. NCDModuleInst_Backend_Dead(i);
  272. }
  273. static void depend_free (struct depend *o)
  274. {
  275. ASSERT(!o->p || !o->p->is_queued)
  276. if (o->p) {
  277. // remove from provide's list
  278. LinkedList1_Remove(&o->p->depends, &o->node);
  279. // if provide is dying and is empty, let it die
  280. if (o->p->dying && LinkedList1_IsEmpty(&o->p->depends)) {
  281. provide_free(o->p);
  282. }
  283. } else {
  284. // remove free depends list
  285. LinkedList1_Remove(&free_depends, &o->node);
  286. }
  287. NCDModuleInst_Backend_Dead(o->i);
  288. }
  289. static void depend_func_die (void *vo)
  290. {
  291. struct depend *o = vo;
  292. depend_free(o);
  293. }
  294. static void depend_func_clean (void *vo)
  295. {
  296. struct depend *o = vo;
  297. ASSERT(!o->p || !o->p->is_queued)
  298. if (!(o->p && o->p->dying)) {
  299. return;
  300. }
  301. struct provide *p = o->p;
  302. // remove from provide's list
  303. LinkedList1_Remove(&p->depends, &o->node);
  304. // insert to free depends list
  305. LinkedList1_Append(&free_depends, &o->node);
  306. // set no provide
  307. o->p = NULL;
  308. // if provide is empty, let it die
  309. if (LinkedList1_IsEmpty(&p->depends)) {
  310. provide_free(p);
  311. }
  312. }
  313. static int depend_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  314. {
  315. struct depend *o = vo;
  316. ASSERT(!o->p || !o->p->is_queued)
  317. if (!o->p) {
  318. return 0;
  319. }
  320. return NCDModuleInst_Backend_GetObj(o->p->i, objname, out_object);
  321. }
  322. static struct NCDModule modules[] = {
  323. {
  324. .type = "provide",
  325. .func_new2 = provide_func_new,
  326. .func_die = provide_func_die,
  327. .alloc_size = sizeof(struct provide)
  328. }, {
  329. .type = "provide_event",
  330. .func_new2 = provide_event_func_new,
  331. .func_die = provide_func_die,
  332. .alloc_size = sizeof(struct provide)
  333. }, {
  334. .type = "depend",
  335. .func_new2 = depend_func_new,
  336. .func_die = depend_func_die,
  337. .func_clean = depend_func_clean,
  338. .func_getobj = depend_func_getobj,
  339. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  340. .alloc_size = sizeof(struct depend)
  341. }, {
  342. .type = NULL
  343. }
  344. };
  345. const struct NCDModuleGroup ncdmodule_depend = {
  346. .func_globalinit = func_globalinit,
  347. .modules = modules
  348. };