dynamic_depend.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 (const struct NCDModuleInst_iparams *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 (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  218. {
  219. struct provide *o = vo;
  220. o->i = i;
  221. // read arguments
  222. NCDValRef name_arg;
  223. if (!NCDVal_ListRead(params->args, 2, &name_arg, &o->order_value)) {
  224. ModuleLog(i, BLOG_ERROR, "wrong arity");
  225. goto fail0;
  226. }
  227. if (!NCDVal_IsStringNoNulls(name_arg)) {
  228. ModuleLog(i, BLOG_ERROR, "wrong type");
  229. goto fail0;
  230. }
  231. const char *name_str = NCDVal_StringValue(name_arg);
  232. // find name, create new if needed
  233. struct name *n = find_name(name_str);
  234. if (!n && !(n = name_init(i, name_str))) {
  235. goto fail0;
  236. }
  237. // set name
  238. o->n = n;
  239. // check for order value conflict
  240. if (BAVL_LookupExact(&n->provides_tree, &o->order_value)) {
  241. ModuleLog(i, BLOG_ERROR, "order value already exists");
  242. goto fail0;
  243. }
  244. // add to name's provides tree
  245. ASSERT_EXECUTE(BAVL_Insert(&n->provides_tree, &o->provides_tree_node, NULL))
  246. // set not dying
  247. o->dying = 0;
  248. // signal up
  249. NCDModuleInst_Backend_Up(i);
  250. // should this be the current provide?
  251. if (o == name_get_first_provide(n)) {
  252. if (!n->cur_p) {
  253. name_new_current(n);
  254. }
  255. else if (!n->cur_resetting) {
  256. name_start_resetting(n);
  257. }
  258. }
  259. return;
  260. fail0:
  261. NCDModuleInst_Backend_SetError(i);
  262. NCDModuleInst_Backend_Dead(i);
  263. }
  264. static void provide_free (struct provide *o)
  265. {
  266. struct name *n = o->n;
  267. ASSERT(o->dying)
  268. ASSERT(o != n->cur_p)
  269. // remove from name's provides tree
  270. BAVL_Remove(&n->provides_tree, &o->provides_tree_node);
  271. NCDModuleInst_Backend_Dead(o->i);
  272. }
  273. static void provide_func_die (void *vo)
  274. {
  275. struct provide *o = vo;
  276. struct name *n = o->n;
  277. ASSERT(!o->dying)
  278. // set dying
  279. o->dying = 1;
  280. // if this is not the current provide, die right away
  281. if (o != n->cur_p) {
  282. // free provide
  283. provide_free(o);
  284. // free name if unused
  285. name_free_if_unused(n);
  286. return;
  287. }
  288. ASSERT(!n->cur_resetting)
  289. // start resetting
  290. name_start_resetting(n);
  291. }
  292. static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  293. {
  294. struct depend *o = vo;
  295. o->i = i;
  296. // read arguments
  297. NCDValRef name_arg;
  298. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  299. ModuleLog(i, BLOG_ERROR, "wrong arity");
  300. goto fail0;
  301. }
  302. if (!NCDVal_IsStringNoNulls(name_arg)) {
  303. ModuleLog(i, BLOG_ERROR, "wrong type");
  304. goto fail0;
  305. }
  306. const char *name_str = NCDVal_StringValue(name_arg);
  307. // find name, create new if needed
  308. struct name *n = find_name(name_str);
  309. if (!n && !(n = name_init(i, name_str))) {
  310. goto fail0;
  311. }
  312. // set name
  313. o->n = n;
  314. if (n->cur_p && !n->cur_resetting) {
  315. // set bound
  316. o->is_bound = 1;
  317. // add to bound depends list
  318. LinkedList0_Prepend(&n->cur_bound_depends_list, &o->depends_list_node);
  319. // signal up
  320. NCDModuleInst_Backend_Up(i);
  321. } else {
  322. // set not bound
  323. o->is_bound = 0;
  324. // add to waiting depends list
  325. LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
  326. }
  327. return;
  328. fail0:
  329. NCDModuleInst_Backend_SetError(i);
  330. NCDModuleInst_Backend_Dead(i);
  331. }
  332. static void depend_func_die (void *vo)
  333. {
  334. struct depend *o = vo;
  335. struct name *n = o->n;
  336. if (o->is_bound) {
  337. ASSERT(n->cur_p)
  338. // remove from bound depends list
  339. LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
  340. // continue resetting
  341. if (n->cur_resetting) {
  342. name_continue_resetting(n);
  343. }
  344. } else {
  345. // remove from waiting depends list
  346. LinkedList0_Remove(&n->waiting_depends_list, &o->depends_list_node);
  347. // free name if unused
  348. name_free_if_unused(n);
  349. }
  350. NCDModuleInst_Backend_Dead(o->i);
  351. }
  352. static void depend_func_clean (void *vo)
  353. {
  354. struct depend *o = vo;
  355. struct name *n = o->n;
  356. ASSERT(!o->is_bound || n->cur_p)
  357. if (!(o->is_bound && n->cur_resetting)) {
  358. return;
  359. }
  360. // remove from bound depends list
  361. LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
  362. // set not bound
  363. o->is_bound = 0;
  364. // add to waiting depends list
  365. LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
  366. // continue resetting
  367. name_continue_resetting(n);
  368. }
  369. static int depend_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  370. {
  371. struct depend *o = vo;
  372. struct name *n = o->n;
  373. ASSERT(!o->is_bound || n->cur_p)
  374. if (!o->is_bound) {
  375. return 0;
  376. }
  377. return NCDModuleInst_Backend_GetObj(n->cur_p->i, objname, out_object);
  378. }
  379. static struct NCDModule modules[] = {
  380. {
  381. .type = "dynamic_provide",
  382. .func_new2 = provide_func_new,
  383. .func_die = provide_func_die,
  384. .alloc_size = sizeof(struct provide)
  385. }, {
  386. .type = "dynamic_depend",
  387. .func_new2 = depend_func_new,
  388. .func_die = depend_func_die,
  389. .func_clean = depend_func_clean,
  390. .func_getobj = depend_func_getobj,
  391. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  392. .alloc_size = sizeof(struct depend)
  393. }, {
  394. .type = NULL
  395. }
  396. };
  397. const struct NCDModuleGroup ncdmodule_dynamic_depend = {
  398. .func_globalinit = func_globalinit,
  399. .modules = modules
  400. };