multidepend.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /**
  2. * @file multidepend.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. * This is a compatibility module. It behaves exactly like the depend_scope module,
  32. * except that there is a single global scope for dependency names.
  33. *
  34. * Use depend_scope instead. If you are using multidepend between non-template
  35. * processes, make those processes templates instead and start them via
  36. * process_manager(). For example, instead of this:
  37. *
  38. * process foo {
  39. * multiprovide("FOO");
  40. * }
  41. *
  42. * process bar {
  43. * multidepend({"FOO"});
  44. * }
  45. *
  46. * Use this:
  47. *
  48. * process main {
  49. * depend_scope() scope;
  50. * process_manager() mgr;
  51. * mgr->start("foo", "foo", {});
  52. * mgr->start("bar", "bar", {});
  53. * }
  54. *
  55. * template foo {
  56. * _caller.scope->provide("FOO");
  57. * }
  58. *
  59. * template bar {
  60. * _caller.scope->depend({"FOO"});
  61. * }
  62. *
  63. * Synopsis:
  64. * multiprovide(name)
  65. *
  66. * Synopsis:
  67. * multidepend(list names)
  68. */
  69. #include <stdlib.h>
  70. #include <string.h>
  71. #include <misc/offset.h>
  72. #include <misc/debug.h>
  73. #include <structure/LinkedList1.h>
  74. #include <ncd/NCDModule.h>
  75. #include <generated/blog_channel_ncd_multidepend.h>
  76. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  77. struct provide {
  78. NCDModuleInst *i;
  79. const char *name;
  80. size_t name_len;
  81. LinkedList1Node provides_node;
  82. LinkedList1 depends;
  83. int dying;
  84. };
  85. struct depend {
  86. NCDModuleInst *i;
  87. NCDValRef names;
  88. LinkedList1Node depends_node;
  89. struct provide *provide;
  90. LinkedList1Node provide_node;
  91. int provide_collapsing;
  92. };
  93. static LinkedList1 provides;
  94. static LinkedList1 depends;
  95. static struct provide * find_provide (const char *name, size_t name_len)
  96. {
  97. for (LinkedList1Node *n = LinkedList1_GetFirst(&provides); n; n = LinkedList1Node_Next(n)) {
  98. struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
  99. if (p->name_len == name_len && !memcmp(p->name, name, name_len)) {
  100. return p;
  101. }
  102. }
  103. return NULL;
  104. }
  105. static struct provide * depend_find_best_provide (struct depend *o)
  106. {
  107. size_t count = NCDVal_ListCount(o->names);
  108. for (size_t j = 0; j < count; j++) {
  109. NCDValRef e = NCDVal_ListGet(o->names, j);
  110. struct provide *p = find_provide(NCDVal_StringData(e), NCDVal_StringLength(e));
  111. if (p && !p->dying) {
  112. return p;
  113. }
  114. }
  115. return NULL;
  116. }
  117. static void depend_update (struct depend *o)
  118. {
  119. // if we're collapsing, do nothing
  120. if (o->provide && o->provide_collapsing) {
  121. return;
  122. }
  123. // find best provide
  124. struct provide *bp = depend_find_best_provide(o);
  125. ASSERT(!bp || !bp->dying)
  126. // has anything changed?
  127. if (bp == o->provide) {
  128. return;
  129. }
  130. if (o->provide) {
  131. // set collapsing
  132. o->provide_collapsing = 1;
  133. // signal down
  134. NCDModuleInst_Backend_Down(o->i);
  135. } else {
  136. // insert to provide's list
  137. LinkedList1_Append(&bp->depends, &o->provide_node);
  138. // set not collapsing
  139. o->provide_collapsing = 0;
  140. // set provide
  141. o->provide = bp;
  142. // signal up
  143. NCDModuleInst_Backend_Up(o->i);
  144. }
  145. }
  146. static int func_globalinit (const struct NCDModuleInst_iparams *params)
  147. {
  148. // init provides list
  149. LinkedList1_Init(&provides);
  150. // init depends list
  151. LinkedList1_Init(&depends);
  152. return 1;
  153. }
  154. static void provide_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  155. {
  156. struct provide *o = vo;
  157. o->i = i;
  158. // read arguments
  159. NCDValRef name_arg;
  160. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  161. ModuleLog(i, BLOG_ERROR, "wrong arity");
  162. goto fail0;
  163. }
  164. if (!NCDVal_IsString(name_arg)) {
  165. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  166. goto fail0;
  167. }
  168. o->name = NCDVal_StringData(name_arg);
  169. o->name_len = NCDVal_StringLength(name_arg);
  170. // check for existing provide with this name
  171. if (find_provide(o->name, o->name_len)) {
  172. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  173. goto fail0;
  174. }
  175. // insert to provides list
  176. LinkedList1_Append(&provides, &o->provides_node);
  177. // init depends list
  178. LinkedList1_Init(&o->depends);
  179. // set not dying
  180. o->dying = 0;
  181. // signal up.
  182. // This comes above the loop which follows, so that effects on related depend statements are
  183. // computed before this process advances, avoiding problems like failed variable resolutions.
  184. NCDModuleInst_Backend_Up(o->i);
  185. // update depends
  186. for (LinkedList1Node *n = LinkedList1_GetFirst(&depends); n; n = LinkedList1Node_Next(n)) {
  187. struct depend *d = UPPER_OBJECT(n, struct depend, depends_node);
  188. depend_update(d);
  189. }
  190. return;
  191. fail0:
  192. NCDModuleInst_Backend_SetError(i);
  193. NCDModuleInst_Backend_Dead(i);
  194. }
  195. static void provide_free (struct provide *o)
  196. {
  197. ASSERT(LinkedList1_IsEmpty(&o->depends))
  198. // remove from provides list
  199. LinkedList1_Remove(&provides, &o->provides_node);
  200. NCDModuleInst_Backend_Dead(o->i);
  201. }
  202. static void provide_func_die (void *vo)
  203. {
  204. struct provide *o = vo;
  205. ASSERT(!o->dying)
  206. // if we have no depends, die immediately
  207. if (LinkedList1_IsEmpty(&o->depends)) {
  208. provide_free(o);
  209. return;
  210. }
  211. // set dying
  212. o->dying = 1;
  213. // start collapsing our depends
  214. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->depends); n; n = LinkedList1Node_Next(n)) {
  215. struct depend *d = UPPER_OBJECT(n, struct depend, provide_node);
  216. ASSERT(d->provide == o)
  217. // update depend to make sure it is collapsing
  218. depend_update(d);
  219. }
  220. }
  221. static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  222. {
  223. struct depend *o = vo;
  224. o->i = i;
  225. // read arguments
  226. NCDValRef names_arg;
  227. if (!NCDVal_ListRead(params->args, 1, &names_arg)) {
  228. ModuleLog(i, BLOG_ERROR, "wrong arity");
  229. goto fail0;
  230. }
  231. if (!NCDVal_IsList(names_arg)) {
  232. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  233. goto fail0;
  234. }
  235. o->names = names_arg;
  236. // check names list
  237. size_t count = NCDVal_ListCount(o->names);
  238. for (size_t j = 0; j < count; j++) {
  239. NCDValRef e = NCDVal_ListGet(o->names, j);
  240. if (!NCDVal_IsString(e)) {
  241. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  242. goto fail0;
  243. }
  244. }
  245. // insert to depends list
  246. LinkedList1_Append(&depends, &o->depends_node);
  247. // set no provide
  248. o->provide = NULL;
  249. // update
  250. depend_update(o);
  251. return;
  252. fail0:
  253. NCDModuleInst_Backend_SetError(i);
  254. NCDModuleInst_Backend_Dead(i);
  255. }
  256. static void depend_free (struct depend *o)
  257. {
  258. if (o->provide) {
  259. // remove from provide's list
  260. LinkedList1_Remove(&o->provide->depends, &o->provide_node);
  261. // if provide is dying and is empty, let it die
  262. if (o->provide->dying && LinkedList1_IsEmpty(&o->provide->depends)) {
  263. provide_free(o->provide);
  264. }
  265. }
  266. // remove from depends list
  267. LinkedList1_Remove(&depends, &o->depends_node);
  268. NCDModuleInst_Backend_Dead(o->i);
  269. }
  270. static void depend_func_die (void *vo)
  271. {
  272. struct depend *o = vo;
  273. depend_free(o);
  274. }
  275. static void depend_func_clean (void *vo)
  276. {
  277. struct depend *o = vo;
  278. if (!(o->provide && o->provide_collapsing)) {
  279. return;
  280. }
  281. // remove from provide's list
  282. LinkedList1_Remove(&o->provide->depends, &o->provide_node);
  283. // if provide is dying and is empty, let it die
  284. if (o->provide->dying && LinkedList1_IsEmpty(&o->provide->depends)) {
  285. provide_free(o->provide);
  286. }
  287. // set no provide
  288. o->provide = NULL;
  289. // update
  290. depend_update(o);
  291. }
  292. static int depend_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  293. {
  294. struct depend *o = vo;
  295. if (!o->provide) {
  296. return 0;
  297. }
  298. return NCDModuleInst_Backend_GetObj(o->provide->i, objname, out_object);
  299. }
  300. static struct NCDModule modules[] = {
  301. {
  302. .type = "multiprovide",
  303. .func_new2 = provide_func_new,
  304. .func_die = provide_func_die,
  305. .alloc_size = sizeof(struct provide)
  306. }, {
  307. .type = "multidepend",
  308. .func_new2 = depend_func_new,
  309. .func_die = depend_func_die,
  310. .func_clean = depend_func_clean,
  311. .func_getobj = depend_func_getobj,
  312. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  313. .alloc_size = sizeof(struct depend)
  314. }, {
  315. .type = NULL
  316. }
  317. };
  318. const struct NCDModuleGroup ncdmodule_multidepend = {
  319. .func_globalinit = func_globalinit,
  320. .modules = modules
  321. };