dynamic_depend.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /**
  2. * @file dynamic_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. * Dynamic dependencies module.
  32. *
  33. * Synopsis: dynamic_provide(string name, order_value)
  34. * Synopsis: dynamic_depend(string name)
  35. */
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <misc/offset.h>
  39. #include <misc/debug.h>
  40. #include <misc/compare.h>
  41. #include <structure/LinkedList0.h>
  42. #include <structure/BAVL.h>
  43. #include <ncd/NCDModule.h>
  44. #include <generated/blog_channel_ncd_dynamic_depend.h>
  45. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  46. struct provide;
  47. struct name {
  48. char *name;
  49. BAVLNode names_tree_node;
  50. BAVL provides_tree;
  51. LinkedList0 waiting_depends_list;
  52. struct provide *cur_p;
  53. LinkedList0 cur_bound_depends_list;
  54. int cur_resetting;
  55. };
  56. struct provide {
  57. NCDModuleInst *i;
  58. struct name *n;
  59. NCDValRef order_value;
  60. BAVLNode provides_tree_node;
  61. int dying;
  62. };
  63. struct depend {
  64. NCDModuleInst *i;
  65. struct name *n;
  66. int is_bound;
  67. LinkedList0Node depends_list_node;
  68. };
  69. static BAVL names_tree;
  70. static void provide_free (struct provide *o);
  71. static void depend_free (struct depend *o);
  72. static int stringptr_comparator (void *user, char **v1, char **v2)
  73. {
  74. int cmp = strcmp(*v1, *v2);
  75. return B_COMPARE(cmp, 0);
  76. }
  77. static int val_comparator (void *user, NCDValRef *v1, NCDValRef *v2)
  78. {
  79. return NCDVal_Compare(*v1, *v2);
  80. }
  81. static struct name * find_name (const char *name)
  82. {
  83. BAVLNode *tn = BAVL_LookupExact(&names_tree, &name);
  84. if (!tn) {
  85. return NULL;
  86. }
  87. struct name *n = UPPER_OBJECT(tn, struct name, names_tree_node);
  88. ASSERT(!strcmp(n->name, name))
  89. return n;
  90. }
  91. static struct name * name_init (NCDModuleInst *i, const char *name)
  92. {
  93. ASSERT(!find_name(name))
  94. // allocate structure
  95. struct name *o = malloc(sizeof(*o));
  96. if (!o) {
  97. ModuleLog(i, BLOG_ERROR, "malloc failed");
  98. goto fail0;
  99. }
  100. // copy name
  101. if (!(o->name = strdup(name))) {
  102. ModuleLog(i, BLOG_ERROR, "strdup failed");
  103. goto fail1;
  104. }
  105. // insert to names tree
  106. ASSERT_EXECUTE(BAVL_Insert(&names_tree, &o->names_tree_node, NULL))
  107. // init provides tree
  108. BAVL_Init(&o->provides_tree, OFFSET_DIFF(struct provide, order_value, provides_tree_node), (BAVL_comparator)val_comparator, NULL);
  109. // init waiting depends list
  110. LinkedList0_Init(&o->waiting_depends_list);
  111. // set no current provide
  112. o->cur_p = NULL;
  113. return o;
  114. fail1:
  115. free(o);
  116. fail0:
  117. return NULL;
  118. }
  119. static void name_free (struct name *o)
  120. {
  121. ASSERT(BAVL_IsEmpty(&o->provides_tree))
  122. ASSERT(LinkedList0_IsEmpty(&o->waiting_depends_list))
  123. ASSERT(!o->cur_p)
  124. // remove from names tree
  125. BAVL_Remove(&names_tree, &o->names_tree_node);
  126. // free name
  127. free(o->name);
  128. // free structure
  129. free(o);
  130. }
  131. static struct provide * name_get_first_provide (struct name *o)
  132. {
  133. BAVLNode *tn = BAVL_GetFirst(&o->provides_tree);
  134. if (!tn) {
  135. return NULL;
  136. }
  137. struct provide *p = UPPER_OBJECT(tn, struct provide, provides_tree_node);
  138. ASSERT(p->n == o)
  139. return p;
  140. }
  141. static void name_new_current (struct name *o)
  142. {
  143. ASSERT(!o->cur_p)
  144. ASSERT(!BAVL_IsEmpty(&o->provides_tree))
  145. // set current provide
  146. o->cur_p = name_get_first_provide(o);
  147. // init bound depends list
  148. LinkedList0_Init(&o->cur_bound_depends_list);
  149. // set not resetting
  150. o->cur_resetting = 0;
  151. // bind waiting depends
  152. while (!LinkedList0_IsEmpty(&o->waiting_depends_list)) {
  153. struct depend *d = UPPER_OBJECT(LinkedList0_GetFirst(&o->waiting_depends_list), struct depend, depends_list_node);
  154. ASSERT(d->n == o)
  155. ASSERT(!d->is_bound)
  156. // remove from waiting depends list
  157. LinkedList0_Remove(&o->waiting_depends_list, &d->depends_list_node);
  158. // set bound
  159. d->is_bound = 1;
  160. // add to bound depends list
  161. LinkedList0_Prepend(&o->cur_bound_depends_list, &d->depends_list_node);
  162. // signal up
  163. NCDModuleInst_Backend_Up(d->i);
  164. }
  165. }
  166. static void name_free_if_unused (struct name *o)
  167. {
  168. if (BAVL_IsEmpty(&o->provides_tree) && LinkedList0_IsEmpty(&o->waiting_depends_list)) {
  169. name_free(o);
  170. }
  171. }
  172. static void name_continue_resetting (struct name *o)
  173. {
  174. ASSERT(o->cur_p)
  175. ASSERT(o->cur_resetting)
  176. // still have bound depends?
  177. if (!LinkedList0_IsEmpty(&o->cur_bound_depends_list)) {
  178. return;
  179. }
  180. struct provide *old_p = o->cur_p;
  181. // set no current provide
  182. o->cur_p = NULL;
  183. // free old current provide if it's dying
  184. if (old_p->dying) {
  185. provide_free(old_p);
  186. }
  187. if (!BAVL_IsEmpty(&o->provides_tree)) {
  188. // get new current provide
  189. name_new_current(o);
  190. } else {
  191. // free name if unused
  192. name_free_if_unused(o);
  193. }
  194. }
  195. static void name_start_resetting (struct name *o)
  196. {
  197. ASSERT(o->cur_p)
  198. ASSERT(!o->cur_resetting)
  199. // set resetting
  200. o->cur_resetting = 1;
  201. // signal bound depends down
  202. for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->cur_bound_depends_list); ln; ln = LinkedList0Node_Next(ln)) {
  203. struct depend *d = UPPER_OBJECT(ln, struct depend, depends_list_node);
  204. ASSERT(d->n == o)
  205. ASSERT(d->is_bound)
  206. NCDModuleInst_Backend_Down(d->i);
  207. }
  208. // if there were no bound depends, continue right away
  209. name_continue_resetting(o);
  210. }
  211. static int func_globalinit (struct NCDModuleInitParams params)
  212. {
  213. // init names tree
  214. BAVL_Init(&names_tree, OFFSET_DIFF(struct name, name, names_tree_node), (BAVL_comparator)stringptr_comparator, NULL);
  215. return 1;
  216. }
  217. static void provide_func_new (NCDModuleInst *i)
  218. {
  219. // allocate instance
  220. struct provide *o = malloc(sizeof(*o));
  221. if (!o) {
  222. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  223. goto fail0;
  224. }
  225. NCDModuleInst_Backend_SetUser(i, o);
  226. // init arguments
  227. o->i = i;
  228. // read arguments
  229. NCDValRef name_arg;
  230. if (!NCDVal_ListRead(i->args, 2, &name_arg, &o->order_value)) {
  231. ModuleLog(i, BLOG_ERROR, "wrong arity");
  232. goto fail1;
  233. }
  234. if (!NCDVal_IsStringNoNulls(name_arg)) {
  235. ModuleLog(i, BLOG_ERROR, "wrong type");
  236. goto fail1;
  237. }
  238. const char *name_str = NCDVal_StringValue(name_arg);
  239. // find name, create new if needed
  240. struct name *n = find_name(name_str);
  241. if (!n && !(n = name_init(i, name_str))) {
  242. goto fail1;
  243. }
  244. // set name
  245. o->n = n;
  246. // check for order value conflict
  247. if (BAVL_LookupExact(&n->provides_tree, &o->order_value)) {
  248. ModuleLog(i, BLOG_ERROR, "order value already exists");
  249. goto fail1;
  250. }
  251. // add to name's provides tree
  252. ASSERT_EXECUTE(BAVL_Insert(&n->provides_tree, &o->provides_tree_node, NULL))
  253. // set not dying
  254. o->dying = 0;
  255. // signal up
  256. NCDModuleInst_Backend_Up(i);
  257. // should this be the current provide?
  258. if (o == name_get_first_provide(n)) {
  259. if (!n->cur_p) {
  260. name_new_current(n);
  261. }
  262. else if (!n->cur_resetting) {
  263. name_start_resetting(n);
  264. }
  265. }
  266. return;
  267. fail1:
  268. free(o);
  269. fail0:
  270. NCDModuleInst_Backend_SetError(i);
  271. NCDModuleInst_Backend_Dead(i);
  272. }
  273. static void provide_free (struct provide *o)
  274. {
  275. struct name *n = o->n;
  276. ASSERT(o->dying)
  277. ASSERT(o != n->cur_p)
  278. NCDModuleInst *i = o->i;
  279. // remove from name's provides tree
  280. BAVL_Remove(&n->provides_tree, &o->provides_tree_node);
  281. // free instance
  282. free(o);
  283. NCDModuleInst_Backend_Dead(i);
  284. }
  285. static void provide_func_die (void *vo)
  286. {
  287. struct provide *o = vo;
  288. struct name *n = o->n;
  289. ASSERT(!o->dying)
  290. // set dying
  291. o->dying = 1;
  292. // if this is not the current provide, die right away
  293. if (o != n->cur_p) {
  294. // free provide
  295. provide_free(o);
  296. // free name if unused
  297. name_free_if_unused(n);
  298. return;
  299. }
  300. ASSERT(!n->cur_resetting)
  301. // start resetting
  302. name_start_resetting(n);
  303. }
  304. static void depend_func_new (NCDModuleInst *i)
  305. {
  306. // allocate instance
  307. struct depend *o = malloc(sizeof(*o));
  308. if (!o) {
  309. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  310. goto fail0;
  311. }
  312. o->i = i;
  313. NCDModuleInst_Backend_SetUser(i, o);
  314. // read arguments
  315. NCDValRef name_arg;
  316. if (!NCDVal_ListRead(i->args, 1, &name_arg)) {
  317. ModuleLog(i, BLOG_ERROR, "wrong arity");
  318. goto fail1;
  319. }
  320. if (!NCDVal_IsStringNoNulls(name_arg)) {
  321. ModuleLog(i, BLOG_ERROR, "wrong type");
  322. goto fail1;
  323. }
  324. const char *name_str = NCDVal_StringValue(name_arg);
  325. // find name, create new if needed
  326. struct name *n = find_name(name_str);
  327. if (!n && !(n = name_init(i, name_str))) {
  328. goto fail1;
  329. }
  330. // set name
  331. o->n = n;
  332. if (n->cur_p && !n->cur_resetting) {
  333. // set bound
  334. o->is_bound = 1;
  335. // add to bound depends list
  336. LinkedList0_Prepend(&n->cur_bound_depends_list, &o->depends_list_node);
  337. // signal up
  338. NCDModuleInst_Backend_Up(i);
  339. } else {
  340. // set not bound
  341. o->is_bound = 0;
  342. // add to waiting depends list
  343. LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
  344. }
  345. return;
  346. fail1:
  347. free(o);
  348. fail0:
  349. NCDModuleInst_Backend_SetError(i);
  350. NCDModuleInst_Backend_Dead(i);
  351. }
  352. static void depend_func_die (void *vo)
  353. {
  354. struct depend *o = vo;
  355. NCDModuleInst *i = o->i;
  356. struct name *n = o->n;
  357. if (o->is_bound) {
  358. ASSERT(n->cur_p)
  359. // remove from bound depends list
  360. LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
  361. // continue resetting
  362. if (n->cur_resetting) {
  363. name_continue_resetting(n);
  364. }
  365. } else {
  366. // remove from waiting depends list
  367. LinkedList0_Remove(&n->waiting_depends_list, &o->depends_list_node);
  368. // free name if unused
  369. name_free_if_unused(n);
  370. }
  371. // free instance
  372. free(o);
  373. NCDModuleInst_Backend_Dead(i);
  374. }
  375. static void depend_func_clean (void *vo)
  376. {
  377. struct depend *o = vo;
  378. struct name *n = o->n;
  379. ASSERT(!o->is_bound || n->cur_p)
  380. if (!(o->is_bound && n->cur_resetting)) {
  381. return;
  382. }
  383. // remove from bound depends list
  384. LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
  385. // set not bound
  386. o->is_bound = 0;
  387. // add to waiting depends list
  388. LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
  389. // continue resetting
  390. name_continue_resetting(n);
  391. }
  392. static int depend_func_getobj (void *vo, const char *objname, NCDObject *out_object)
  393. {
  394. struct depend *o = vo;
  395. struct name *n = o->n;
  396. ASSERT(!o->is_bound || n->cur_p)
  397. if (!o->is_bound) {
  398. return 0;
  399. }
  400. return NCDModuleInst_Backend_GetObj(n->cur_p->i, objname, out_object);
  401. }
  402. static const struct NCDModule modules[] = {
  403. {
  404. .type = "dynamic_provide",
  405. .func_new = provide_func_new,
  406. .func_die = provide_func_die
  407. }, {
  408. .type = "dynamic_depend",
  409. .func_new = depend_func_new,
  410. .func_die = depend_func_die,
  411. .func_clean = depend_func_clean,
  412. .func_getobj = depend_func_getobj,
  413. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
  414. }, {
  415. .type = NULL
  416. }
  417. };
  418. const struct NCDModuleGroup ncdmodule_dynamic_depend = {
  419. .func_globalinit = func_globalinit,
  420. .modules = modules
  421. };