multidepend.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. NCDValRef name;
  80. LinkedList1Node provides_node;
  81. LinkedList1 depends;
  82. int dying;
  83. };
  84. struct depend {
  85. NCDModuleInst *i;
  86. NCDValRef names;
  87. LinkedList1Node depends_node;
  88. struct provide *provide;
  89. LinkedList1Node provide_node;
  90. int provide_collapsing;
  91. };
  92. static LinkedList1 provides;
  93. static LinkedList1 depends;
  94. static struct provide * find_provide (NCDValRef name)
  95. {
  96. for (LinkedList1Node *n = LinkedList1_GetFirst(&provides); n; n = LinkedList1Node_Next(n)) {
  97. struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
  98. if (NCDVal_Compare(p->name, name) == 0) {
  99. return p;
  100. }
  101. }
  102. return NULL;
  103. }
  104. static struct provide * depend_find_best_provide (struct depend *o)
  105. {
  106. size_t count = NCDVal_ListCount(o->names);
  107. for (size_t j = 0; j < count; j++) {
  108. NCDValRef name = NCDVal_ListGet(o->names, j);
  109. struct provide *p = find_provide(name);
  110. if (p && !p->dying) {
  111. return p;
  112. }
  113. }
  114. return NULL;
  115. }
  116. static void depend_update (struct depend *o)
  117. {
  118. // if we're collapsing, do nothing
  119. if (o->provide && o->provide_collapsing) {
  120. return;
  121. }
  122. // find best provide
  123. struct provide *bp = depend_find_best_provide(o);
  124. ASSERT(!bp || !bp->dying)
  125. // has anything changed?
  126. if (bp == o->provide) {
  127. return;
  128. }
  129. if (o->provide) {
  130. // set collapsing
  131. o->provide_collapsing = 1;
  132. // signal down
  133. NCDModuleInst_Backend_Down(o->i);
  134. } else {
  135. // insert to provide's list
  136. LinkedList1_Append(&bp->depends, &o->provide_node);
  137. // set not collapsing
  138. o->provide_collapsing = 0;
  139. // set provide
  140. o->provide = bp;
  141. // signal up
  142. NCDModuleInst_Backend_Up(o->i);
  143. }
  144. }
  145. static int func_globalinit (const struct NCDModuleInst_iparams *params)
  146. {
  147. // init provides list
  148. LinkedList1_Init(&provides);
  149. // init depends list
  150. LinkedList1_Init(&depends);
  151. return 1;
  152. }
  153. static void provide_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  154. {
  155. struct provide *o = vo;
  156. o->i = i;
  157. // read arguments
  158. NCDValRef name_arg;
  159. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  160. ModuleLog(i, BLOG_ERROR, "wrong arity");
  161. goto fail0;
  162. }
  163. // remember name
  164. o->name = name_arg;
  165. // check for existing provide with this name
  166. if (find_provide(o->name)) {
  167. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  168. goto fail0;
  169. }
  170. // insert to provides list
  171. LinkedList1_Append(&provides, &o->provides_node);
  172. // init depends list
  173. LinkedList1_Init(&o->depends);
  174. // set not dying
  175. o->dying = 0;
  176. // signal up.
  177. // This comes above the loop which follows, so that effects on related depend statements are
  178. // computed before this process advances, avoiding problems like failed variable resolutions.
  179. NCDModuleInst_Backend_Up(o->i);
  180. // update depends
  181. for (LinkedList1Node *n = LinkedList1_GetFirst(&depends); n; n = LinkedList1Node_Next(n)) {
  182. struct depend *d = UPPER_OBJECT(n, struct depend, depends_node);
  183. depend_update(d);
  184. }
  185. return;
  186. fail0:
  187. NCDModuleInst_Backend_SetError(i);
  188. NCDModuleInst_Backend_Dead(i);
  189. }
  190. static void provide_free (struct provide *o)
  191. {
  192. ASSERT(LinkedList1_IsEmpty(&o->depends))
  193. // remove from provides list
  194. LinkedList1_Remove(&provides, &o->provides_node);
  195. NCDModuleInst_Backend_Dead(o->i);
  196. }
  197. static void provide_func_die (void *vo)
  198. {
  199. struct provide *o = vo;
  200. ASSERT(!o->dying)
  201. // if we have no depends, die immediately
  202. if (LinkedList1_IsEmpty(&o->depends)) {
  203. provide_free(o);
  204. return;
  205. }
  206. // set dying
  207. o->dying = 1;
  208. // start collapsing our depends
  209. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->depends); n; n = LinkedList1Node_Next(n)) {
  210. struct depend *d = UPPER_OBJECT(n, struct depend, provide_node);
  211. ASSERT(d->provide == o)
  212. // update depend to make sure it is collapsing
  213. depend_update(d);
  214. }
  215. }
  216. static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  217. {
  218. struct depend *o = vo;
  219. o->i = i;
  220. // read arguments
  221. NCDValRef names_arg;
  222. if (!NCDVal_ListRead(params->args, 1, &names_arg)) {
  223. ModuleLog(i, BLOG_ERROR, "wrong arity");
  224. goto fail0;
  225. }
  226. if (!NCDVal_IsList(names_arg)) {
  227. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  228. goto fail0;
  229. }
  230. // remember names
  231. o->names = names_arg;
  232. // insert to depends list
  233. LinkedList1_Append(&depends, &o->depends_node);
  234. // set no provide
  235. o->provide = NULL;
  236. // update
  237. depend_update(o);
  238. return;
  239. fail0:
  240. NCDModuleInst_Backend_SetError(i);
  241. NCDModuleInst_Backend_Dead(i);
  242. }
  243. static void depend_free (struct depend *o)
  244. {
  245. if (o->provide) {
  246. // remove from provide's list
  247. LinkedList1_Remove(&o->provide->depends, &o->provide_node);
  248. // if provide is dying and is empty, let it die
  249. if (o->provide->dying && LinkedList1_IsEmpty(&o->provide->depends)) {
  250. provide_free(o->provide);
  251. }
  252. }
  253. // remove from depends list
  254. LinkedList1_Remove(&depends, &o->depends_node);
  255. NCDModuleInst_Backend_Dead(o->i);
  256. }
  257. static void depend_func_die (void *vo)
  258. {
  259. struct depend *o = vo;
  260. depend_free(o);
  261. }
  262. static void depend_func_clean (void *vo)
  263. {
  264. struct depend *o = vo;
  265. if (!(o->provide && o->provide_collapsing)) {
  266. return;
  267. }
  268. // remove from provide's list
  269. LinkedList1_Remove(&o->provide->depends, &o->provide_node);
  270. // if provide is dying and is empty, let it die
  271. if (o->provide->dying && LinkedList1_IsEmpty(&o->provide->depends)) {
  272. provide_free(o->provide);
  273. }
  274. // set no provide
  275. o->provide = NULL;
  276. // update
  277. depend_update(o);
  278. }
  279. static int depend_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  280. {
  281. struct depend *o = vo;
  282. if (!o->provide) {
  283. return 0;
  284. }
  285. return NCDModuleInst_Backend_GetObj(o->provide->i, objname, out_object);
  286. }
  287. static struct NCDModule modules[] = {
  288. {
  289. .type = "multiprovide",
  290. .func_new2 = provide_func_new,
  291. .func_die = provide_func_die,
  292. .alloc_size = sizeof(struct provide)
  293. }, {
  294. .type = "multidepend",
  295. .func_new2 = depend_func_new,
  296. .func_die = depend_func_die,
  297. .func_clean = depend_func_clean,
  298. .func_getobj = depend_func_getobj,
  299. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  300. .alloc_size = sizeof(struct depend)
  301. }, {
  302. .type = NULL
  303. }
  304. };
  305. const struct NCDModuleGroup ncdmodule_multidepend = {
  306. .func_globalinit = func_globalinit,
  307. .modules = modules
  308. };