depend.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. 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. 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. NCDModuleInst_Backend_SetUser(i, o);
  152. // init arguments
  153. o->i = i;
  154. // read arguments
  155. NCDValue *name_arg;
  156. if (!NCDValue_ListRead(o->i->args, 1, &name_arg)) {
  157. ModuleLog(i, BLOG_ERROR, "wrong arity");
  158. goto fail1;
  159. }
  160. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  161. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  162. goto fail1;
  163. }
  164. o->name = NCDValue_StringValue(name_arg);
  165. // signal up.
  166. // This comes above provide_promote(), so that effects on related depend statements are
  167. // computed before this process advances, avoiding problems like failed variable resolutions.
  168. NCDModuleInst_Backend_Up(o->i);
  169. // check for existing provide with this name
  170. struct provide *ep = find_provide(o->name);
  171. if (ep) {
  172. ASSERT(!ep->is_queued)
  173. if (!event) {
  174. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  175. goto fail1;
  176. }
  177. // set queued
  178. o->is_queued = 1;
  179. // insert to existing provide's queued provides list
  180. LinkedList3Node_InitAfter(&o->queued_node, &ep->queued_provides_firstnode);
  181. } else {
  182. // init first node for queued provides list
  183. LinkedList3Node_InitLonely(&o->queued_provides_firstnode);
  184. // promote provide
  185. provide_promote(o);
  186. }
  187. return;
  188. fail1:
  189. free(o);
  190. fail0:
  191. NCDModuleInst_Backend_SetError(i);
  192. NCDModuleInst_Backend_Dead(i);
  193. }
  194. static void provide_func_new (NCDModuleInst *i)
  195. {
  196. provide_func_new_templ(i, 0);
  197. }
  198. static void provide_event_func_new (NCDModuleInst *i)
  199. {
  200. provide_func_new_templ(i, 1);
  201. }
  202. static void provide_free (struct provide *o)
  203. {
  204. ASSERT(o->is_queued || LinkedList2_IsEmpty(&o->depends))
  205. NCDModuleInst *i = o->i;
  206. if (o->is_queued) {
  207. // remove from existing provide's queued provides list
  208. LinkedList3Node_Free(&o->queued_node);
  209. } else {
  210. // remove from provides list
  211. LinkedList2_Remove(&provides, &o->provides_node);
  212. // if we have provides queued, promote the first one
  213. if (LinkedList3Node_Next(&o->queued_provides_firstnode)) {
  214. // get first queued provide
  215. struct provide *qp = UPPER_OBJECT(LinkedList3Node_Next(&o->queued_provides_firstnode), struct provide, queued_node);
  216. ASSERT(qp->is_queued)
  217. // make it the head of the queued provides list
  218. LinkedList3Node_Free(&qp->queued_node);
  219. LinkedList3Node_InitAfter(&qp->queued_provides_firstnode, &o->queued_provides_firstnode);
  220. LinkedList3Node_Free(&o->queued_provides_firstnode);
  221. // promote provide
  222. provide_promote(qp);
  223. }
  224. }
  225. // free instance
  226. free(o);
  227. NCDModuleInst_Backend_Dead(i);
  228. }
  229. static void provide_func_die (void *vo)
  230. {
  231. struct provide *o = vo;
  232. ASSERT(o->is_queued || !o->dying)
  233. // if we are queued or have no depends, die immediately
  234. if (o->is_queued || LinkedList2_IsEmpty(&o->depends)) {
  235. provide_free(o);
  236. return;
  237. }
  238. // set dying
  239. o->dying = 1;
  240. // signal our depends down
  241. LinkedList2Iterator it;
  242. LinkedList2Iterator_InitForward(&it, &o->depends);
  243. LinkedList2Node *n;
  244. while (n = LinkedList2Iterator_Next(&it)) {
  245. struct depend *d = UPPER_OBJECT(n, struct depend, node);
  246. ASSERT(d->p == o)
  247. // signal down
  248. NCDModuleInst_Backend_Down(d->i);
  249. }
  250. }
  251. static void depend_func_new (NCDModuleInst *i)
  252. {
  253. // allocate instance
  254. struct depend *o = malloc(sizeof(*o));
  255. if (!o) {
  256. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  257. goto fail0;
  258. }
  259. NCDModuleInst_Backend_SetUser(i, o);
  260. // init arguments
  261. o->i = i;
  262. // read arguments
  263. NCDValue *name_arg;
  264. if (!NCDValue_ListRead(o->i->args, 1, &name_arg)) {
  265. ModuleLog(i, BLOG_ERROR, "wrong arity");
  266. goto fail1;
  267. }
  268. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  269. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  270. goto fail1;
  271. }
  272. o->name = NCDValue_StringValue(name_arg);
  273. // find a provide with our name
  274. struct provide *p = find_provide(o->name);
  275. ASSERT(!p || !p->is_queued)
  276. if (p && !p->dying) {
  277. // insert to provide's list
  278. LinkedList2_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. LinkedList2_Append(&free_depends, &o->node);
  286. // set no provide
  287. o->p = NULL;
  288. }
  289. return;
  290. fail1:
  291. free(o);
  292. fail0:
  293. NCDModuleInst_Backend_SetError(i);
  294. NCDModuleInst_Backend_Dead(i);
  295. }
  296. static void depend_free (struct depend *o)
  297. {
  298. NCDModuleInst *i = o->i;
  299. ASSERT(!o->p || !o->p->is_queued)
  300. if (o->p) {
  301. // remove from provide's list
  302. LinkedList2_Remove(&o->p->depends, &o->node);
  303. // if provide is dying and is empty, let it die
  304. if (o->p->dying && LinkedList2_IsEmpty(&o->p->depends)) {
  305. provide_free(o->p);
  306. }
  307. } else {
  308. // remove free depends list
  309. LinkedList2_Remove(&free_depends, &o->node);
  310. }
  311. // free instance
  312. free(o);
  313. NCDModuleInst_Backend_Dead(i);
  314. }
  315. static void depend_func_die (void *vo)
  316. {
  317. struct depend *o = vo;
  318. depend_free(o);
  319. }
  320. static void depend_func_clean (void *vo)
  321. {
  322. struct depend *o = vo;
  323. ASSERT(!o->p || !o->p->is_queued)
  324. if (!(o->p && o->p->dying)) {
  325. return;
  326. }
  327. struct provide *p = o->p;
  328. // remove from provide's list
  329. LinkedList2_Remove(&p->depends, &o->node);
  330. // insert to free depends list
  331. LinkedList2_Append(&free_depends, &o->node);
  332. // set no provide
  333. o->p = NULL;
  334. // if provide is empty, let it die
  335. if (LinkedList2_IsEmpty(&p->depends)) {
  336. provide_free(p);
  337. }
  338. }
  339. static int depend_func_getobj (void *vo, const char *objname, NCDObject *out_object)
  340. {
  341. struct depend *o = vo;
  342. ASSERT(!o->p || !o->p->is_queued)
  343. if (!o->p) {
  344. return 0;
  345. }
  346. return NCDModuleInst_Backend_GetObj(o->p->i, objname, out_object);
  347. }
  348. static const struct NCDModule modules[] = {
  349. {
  350. .type = "provide",
  351. .func_new = provide_func_new,
  352. .func_die = provide_func_die
  353. }, {
  354. .type = "provide_event",
  355. .func_new = provide_event_func_new,
  356. .func_die = provide_func_die
  357. }, {
  358. .type = "depend",
  359. .func_new = depend_func_new,
  360. .func_die = depend_func_die,
  361. .func_clean = depend_func_clean,
  362. .func_getobj = depend_func_getobj,
  363. .can_resolve_when_down = 1
  364. }, {
  365. .type = NULL
  366. }
  367. };
  368. const struct NCDModuleGroup ncdmodule_depend = {
  369. .func_globalinit = func_globalinit,
  370. .modules = modules
  371. };