depend.c 13 KB

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