dynamic_depend.c 13 KB

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