multidepend.c 9.9 KB

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