prio_list.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016-2017 CNRS
  4. * Copyright (C) 2017 Inria
  5. * Copyright (C) 2015-2017 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. * This implements list with priorities (as an int), by using two stages:
  20. * - an RB tree stage sorted by priority, whose leaves are...
  21. * - ... double-linked lists sorted by insertion order.
  22. *
  23. * We always keep the 0-priority list allocated, to avoid keeping
  24. * allocating/deallocating it when all priorities are 0.
  25. *
  26. * We maintain an "empty" flag, to allow lockless FOO_prio_list_empty call.
  27. *
  28. * PRIO_LIST_TYPE(FOO, priority_field)
  29. *
  30. * - Declares the following type:
  31. * + priority list: struct FOO_prio_list
  32. *
  33. * - Declares the following inlines (all O(1) except stated otherwise, n is the
  34. * number of elements, p is the number of different priorities):
  35. *
  36. * * Initialize a new priority list
  37. * void FOO_prio_list_init(struct FOO_prio_list*)
  38. *
  39. * * Free an empty priority list
  40. * void FOO_prio_list_deinit(struct FOO_prio_list*)
  41. *
  42. * * Add a new cell at the end of the list of the priority of the cell (O(log2 p))
  43. * void FOO_prio_list_push_back(struct FOO_prio_list*, struct FOO*)
  44. *
  45. * * Add a new cell at the beginning of the list of the priority of the cell (O(log2 p))
  46. * void FOO_prio_list_push_front(struct FOO_prio_list*, struct FOO*)
  47. *
  48. * * Test whether the priority list is empty
  49. * void FOO_prio_list_empty(struct FOO_prio_list*)
  50. *
  51. * * Remove given cell from the priority list
  52. * void FOO_prio_list_erase(struct FOO_prio_list*, struct FOO*)
  53. *
  54. * * Return and remove the first cell of highest priority of the priority list
  55. * void FOO_prio_list_pop_front_highest(struct FOO_prio_list*)
  56. * * Return and remove the first cell of lowest priority of the priority list
  57. * void FOO_prio_list_pop_front_lowest(struct FOO_prio_list*)
  58. *
  59. * * Return and remove the last cell of highest priority of the priority list
  60. * void FOO_prio_list_pop_back_highest(struct FOO_prio_list*)
  61. * * Return and remove the last cell of lowest priority of the priority list
  62. * void FOO_prio_list_pop_back_lowest(struct FOO_prio_list*)
  63. *
  64. * * Return the first cell of highest priority of the priority list
  65. * void FOO_prio_list_front_highest(struct FOO_prio_list*)
  66. * * Return the first cell of lowest priority of the priority list
  67. * void FOO_prio_list_front_lowest(struct FOO_prio_list*)
  68. *
  69. * * Return the last cell of highest priority of sthe priority list
  70. * void FOO_prio_list_back_highest(struct FOO_prio_list*)
  71. * * Return the last cell of lowest priority of sthe priority list
  72. * void FOO_prio_list_back_lowest(struct FOO_prio_list*)
  73. *
  74. * * Append second priority list at ends of the first priority list (O(log2 p))
  75. * void FOO_prio_list_push_prio_list_back(struct FOO_prio_list*, struct FOO_prio_list*)
  76. *
  77. * * Test whether cell is part of the list (O(n))
  78. * void FOO_prio_list_ismember(struct FOO_prio_list*, struct FOO*)
  79. *
  80. * * Return the first cell of the list
  81. * struct FOO* FOO_prio_list_begin(struct FOO_prio_list*);
  82. *
  83. * * Return the value to test at the end of the list
  84. * struct FOO* FOO_prio_list_end(struct FOO_prio_list*);
  85. *
  86. * * Return the next cell of the list
  87. * struct FOO* FOO_prio_list_next(struct FOO_prio_list*, struct FOO*)
  88. *
  89. * * Return the last cell of the list
  90. * struct FOO* FOO_prio_list_last(struct FOO_prio_list*);
  91. *
  92. * * Return the value to test at the beginning of the list
  93. * struct FOO* FOO_prio_list_alpha(struct FOO_prio_list*);
  94. *
  95. * * Return the previous cell of the list
  96. * struct FOO* FOO_prio_list_prev(struct FOO_prio_list*, struct FOO*)
  97. *
  98. * PRIO_LIST_TYPE assumes that LIST_TYPE has already been called to create the
  99. * final structure.
  100. *
  101. * *********************************************************
  102. * Usage example:
  103. * LIST_TYPE(my_struct,
  104. * int a;
  105. * int b;
  106. * int prio;
  107. * );
  108. * PRIO_LIST_TYPE(my_struct, prio);
  109. *
  110. * and then my_struct_prio_list_* inlines are available
  111. */
  112. #ifndef __PRIO_LIST_H__
  113. #define __PRIO_LIST_H__
  114. #include <common/rbtree.h>
  115. #ifndef PRIO_LIST_INLINE
  116. #define PRIO_LIST_INLINE static inline
  117. #endif
  118. #define PRIO_LIST_TYPE(ENAME, PRIOFIELD) \
  119. PRIO_LIST_CREATE_TYPE(ENAME, PRIOFIELD)
  120. #ifndef STARPU_DEBUG
  121. #define PRIO_LIST_CREATE_TYPE(ENAME, PRIOFIELD) \
  122. /* The main type: an RB binary tree */ \
  123. struct ENAME##_prio_list { \
  124. struct starpu_rbtree tree; \
  125. int empty; \
  126. }; \
  127. /* The second stage: a list */ \
  128. struct ENAME##_prio_list_stage { \
  129. struct starpu_rbtree_node node; /* Keep this first so ENAME##_node_to_list_stage can work. */ \
  130. int prio; \
  131. struct ENAME##_list list; \
  132. }; \
  133. PRIO_LIST_INLINE struct ENAME##_prio_list_stage *ENAME##_node_to_list_stage(struct starpu_rbtree_node *node) \
  134. { \
  135. /* This assumes node is first member of stage */ \
  136. return (struct ENAME##_prio_list_stage *) node; \
  137. } \
  138. PRIO_LIST_INLINE const struct ENAME##_prio_list_stage *ENAME##_node_to_list_stage_const(const struct starpu_rbtree_node *node) \
  139. { \
  140. /* This assumes node is first member of stage */ \
  141. return (struct ENAME##_prio_list_stage *) node; \
  142. } \
  143. PRIO_LIST_INLINE void ENAME##_prio_list_init(struct ENAME##_prio_list *priolist) \
  144. { \
  145. starpu_rbtree_init(&priolist->tree); \
  146. priolist->empty = 1; \
  147. } \
  148. PRIO_LIST_INLINE void ENAME##_prio_list_deinit(struct ENAME##_prio_list *priolist) \
  149. { \
  150. if (starpu_rbtree_empty(&priolist->tree)) \
  151. return; \
  152. struct starpu_rbtree_node *root = priolist->tree.root; \
  153. struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage(root); \
  154. assert(ENAME##_list_empty(&stage->list)); \
  155. assert(!root->children[0] && !root->children[1]); \
  156. starpu_rbtree_remove(&priolist->tree, root); \
  157. free(stage); \
  158. } \
  159. PRIO_LIST_INLINE int ENAME##_prio_list_cmp_fn(int prio, const struct starpu_rbtree_node *node) \
  160. { \
  161. /* Sort by decreasing order */ \
  162. const struct ENAME##_prio_list_stage *e2 = ENAME##_node_to_list_stage_const(node); \
  163. if (e2->prio < prio) \
  164. return -1; \
  165. if (e2->prio == prio) \
  166. return 0; \
  167. /* e2->prio > prio */ \
  168. return 1; \
  169. } \
  170. PRIO_LIST_INLINE struct ENAME##_prio_list_stage *ENAME##_prio_list_add(struct ENAME##_prio_list *priolist, int prio) \
  171. { \
  172. unsigned long slot; \
  173. struct starpu_rbtree_node *node; \
  174. struct ENAME##_prio_list_stage *stage; \
  175. node = starpu_rbtree_lookup_slot(&priolist->tree, prio, ENAME##_prio_list_cmp_fn, slot); \
  176. if (node) \
  177. stage = ENAME##_node_to_list_stage(node); \
  178. else { \
  179. _STARPU_MALLOC(stage, sizeof(*stage)); \
  180. starpu_rbtree_node_init(&stage->node); \
  181. stage->prio = prio; \
  182. ENAME##_list_init(&stage->list); \
  183. starpu_rbtree_insert_slot(&priolist->tree, slot, &stage->node); \
  184. } \
  185. return stage; \
  186. } \
  187. PRIO_LIST_INLINE void ENAME##_prio_list_push_back(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  188. { \
  189. struct ENAME##_prio_list_stage *stage = ENAME##_prio_list_add(priolist, e->PRIOFIELD); \
  190. ENAME##_list_push_back(&stage->list, e); \
  191. priolist->empty = 0; \
  192. } \
  193. PRIO_LIST_INLINE void ENAME##_prio_list_push_front(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  194. { \
  195. struct ENAME##_prio_list_stage *stage = ENAME##_prio_list_add(priolist, e->PRIOFIELD); \
  196. ENAME##_list_push_front(&stage->list, e); \
  197. priolist->empty = 0; \
  198. } \
  199. PRIO_LIST_INLINE int ENAME##_prio_list_empty(const struct ENAME##_prio_list *priolist) \
  200. { \
  201. return priolist->empty; \
  202. } \
  203. /* Version of list_empty which does not use the cached empty flag,
  204. * typically used to compute the value of the flag */ \
  205. PRIO_LIST_INLINE int ENAME##_prio_list_empty_slow(const struct ENAME##_prio_list *priolist) \
  206. { \
  207. if (starpu_rbtree_empty(&priolist->tree)) \
  208. return 1; \
  209. struct starpu_rbtree_node *root = priolist->tree.root; \
  210. const struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage_const(root); \
  211. if (ENAME##_list_empty(&stage->list) && !root->children[0] && !root->children[1]) \
  212. /* Just one empty list */ \
  213. return 1; \
  214. return 0; \
  215. } \
  216. /* To be called when removing an element from a stage, to potentially remove this stage */ \
  217. PRIO_LIST_INLINE void ENAME##_prio_list_check_empty_stage(struct ENAME##_prio_list *priolist, struct ENAME##_prio_list_stage *stage) \
  218. { \
  219. if (ENAME##_list_empty(&stage->list)) { \
  220. if (stage->prio != 0) \
  221. { \
  222. /* stage got empty, remove it */ \
  223. starpu_rbtree_remove(&priolist->tree, &stage->node); \
  224. free(stage); \
  225. } \
  226. priolist->empty = ENAME##_prio_list_empty_slow(priolist); \
  227. } \
  228. } \
  229. PRIO_LIST_INLINE void ENAME##_prio_list_erase(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  230. { \
  231. struct starpu_rbtree_node *node = starpu_rbtree_lookup(&priolist->tree, e->PRIOFIELD, ENAME##_prio_list_cmp_fn); \
  232. struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage(node); \
  233. ENAME##_list_erase(&stage->list, e); \
  234. ENAME##_prio_list_check_empty_stage(priolist, stage); \
  235. } \
  236. PRIO_LIST_INLINE int ENAME##_prio_list_get_next_nonempty_stage(struct ENAME##_prio_list *priolist, struct starpu_rbtree_node *node, struct starpu_rbtree_node **pnode, struct ENAME##_prio_list_stage **pstage) \
  237. { \
  238. struct ENAME##_prio_list_stage *stage; \
  239. while(1) { \
  240. struct starpu_rbtree_node *next; \
  241. if (!node) \
  242. /* Tree is empty */ \
  243. return 0; \
  244. stage = ENAME##_node_to_list_stage(node); \
  245. if (!ENAME##_list_empty(&stage->list)) \
  246. break; \
  247. /* Empty list, skip to next tree entry */ \
  248. next = starpu_rbtree_next(node); \
  249. /* drop it if not 0-prio */ \
  250. if (stage->prio != 0) \
  251. { \
  252. starpu_rbtree_remove(&priolist->tree, node); \
  253. free(stage); \
  254. } \
  255. node = next; \
  256. } \
  257. *pnode = node; \
  258. *pstage = stage; \
  259. return 1; \
  260. } \
  261. PRIO_LIST_INLINE int ENAME##_prio_list_get_prev_nonempty_stage(struct ENAME##_prio_list *priolist, struct starpu_rbtree_node *node, struct starpu_rbtree_node **pnode, struct ENAME##_prio_list_stage **pstage) \
  262. { \
  263. struct ENAME##_prio_list_stage *stage; \
  264. while(1) { \
  265. struct starpu_rbtree_node *prev; \
  266. if (!node) \
  267. /* Tree is empty */ \
  268. return 0; \
  269. stage = ENAME##_node_to_list_stage(node); \
  270. if (!ENAME##_list_empty(&stage->list)) \
  271. break; \
  272. /* Empty list, skip to prev tree entry */ \
  273. prev = starpu_rbtree_prev(node); \
  274. /* drop it if not 0-prio */ \
  275. if (stage->prio != 0) \
  276. { \
  277. starpu_rbtree_remove(&priolist->tree, node); \
  278. free(stage); \
  279. } \
  280. node = prev; \
  281. } \
  282. *pnode = node; \
  283. *pstage = stage; \
  284. return 1; \
  285. } \
  286. PRIO_LIST_INLINE int ENAME##_prio_list_get_first_nonempty_stage(struct ENAME##_prio_list *priolist, struct starpu_rbtree_node **pnode, struct ENAME##_prio_list_stage **pstage) \
  287. { \
  288. struct starpu_rbtree_node *node = starpu_rbtree_first(&priolist->tree); \
  289. return ENAME##_prio_list_get_next_nonempty_stage(priolist, node, pnode, pstage); \
  290. } \
  291. PRIO_LIST_INLINE int ENAME##_prio_list_get_last_nonempty_stage(struct ENAME##_prio_list *priolist, struct starpu_rbtree_node **pnode, struct ENAME##_prio_list_stage **pstage) \
  292. { \
  293. struct starpu_rbtree_node *node = starpu_rbtree_last(&priolist->tree); \
  294. return ENAME##_prio_list_get_prev_nonempty_stage(priolist, node, pnode, pstage); \
  295. } \
  296. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_pop_front_highest(struct ENAME##_prio_list *priolist) \
  297. { \
  298. struct starpu_rbtree_node *node; \
  299. struct ENAME##_prio_list_stage *stage; \
  300. struct ENAME *ret; \
  301. if (!ENAME##_prio_list_get_first_nonempty_stage(priolist, &node, &stage)) \
  302. return NULL; \
  303. ret = ENAME##_list_pop_front(&stage->list); \
  304. ENAME##_prio_list_check_empty_stage(priolist, stage); \
  305. return ret; \
  306. } \
  307. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_pop_front_lowest(struct ENAME##_prio_list *priolist) \
  308. { \
  309. struct starpu_rbtree_node *node; \
  310. struct ENAME##_prio_list_stage *stage; \
  311. struct ENAME *ret; \
  312. if (!ENAME##_prio_list_get_last_nonempty_stage(priolist, &node, &stage)) \
  313. return NULL; \
  314. ret = ENAME##_list_pop_front(&stage->list); \
  315. ENAME##_prio_list_check_empty_stage(priolist, stage); \
  316. return ret; \
  317. } \
  318. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_front_highest(struct ENAME##_prio_list *priolist) \
  319. { \
  320. struct starpu_rbtree_node *node; \
  321. struct ENAME##_prio_list_stage *stage; \
  322. if (!ENAME##_prio_list_get_first_nonempty_stage(priolist, &node, &stage)) \
  323. return NULL; \
  324. return ENAME##_list_front(&stage->list); \
  325. } \
  326. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_front_lowest(struct ENAME##_prio_list *priolist) \
  327. { \
  328. struct starpu_rbtree_node *node; \
  329. struct ENAME##_prio_list_stage *stage; \
  330. if (!ENAME##_prio_list_get_last_nonempty_stage(priolist, &node, &stage)) \
  331. return NULL; \
  332. return ENAME##_list_front(&stage->list); \
  333. } \
  334. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_pop_back_highest(struct ENAME##_prio_list *priolist) \
  335. { \
  336. struct starpu_rbtree_node *node; \
  337. struct ENAME##_prio_list_stage *stage; \
  338. struct ENAME *ret; \
  339. if (!ENAME##_prio_list_get_first_nonempty_stage(priolist, &node, &stage)) \
  340. return NULL; \
  341. ret = ENAME##_list_pop_back(&stage->list); \
  342. ENAME##_prio_list_check_empty_stage(priolist, stage); \
  343. return ret; \
  344. } \
  345. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_pop_back_lowest(struct ENAME##_prio_list *priolist) \
  346. { \
  347. struct starpu_rbtree_node *node; \
  348. struct ENAME##_prio_list_stage *stage; \
  349. struct ENAME *ret; \
  350. if (!ENAME##_prio_list_get_last_nonempty_stage(priolist, &node, &stage)) \
  351. return NULL; \
  352. ret = ENAME##_list_pop_back(&stage->list); \
  353. ENAME##_prio_list_check_empty_stage(priolist, stage); \
  354. return ret; \
  355. } \
  356. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_back_highest(struct ENAME##_prio_list *priolist) \
  357. { \
  358. struct starpu_rbtree_node *node; \
  359. struct ENAME##_prio_list_stage *stage; \
  360. if (!ENAME##_prio_list_get_first_nonempty_stage(priolist, &node, &stage)) \
  361. return NULL; \
  362. return ENAME##_list_back(&stage->list); \
  363. } \
  364. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_back_lowest(struct ENAME##_prio_list *priolist) \
  365. { \
  366. struct starpu_rbtree_node *node; \
  367. struct ENAME##_prio_list_stage *stage; \
  368. if (!ENAME##_prio_list_get_last_nonempty_stage(priolist, &node, &stage)) \
  369. return NULL; \
  370. return ENAME##_list_back(&stage->list); \
  371. } \
  372. PRIO_LIST_INLINE void ENAME##_prio_list_push_prio_list_back(struct ENAME##_prio_list *priolist, struct ENAME##_prio_list *priolist_toadd) \
  373. { \
  374. struct starpu_rbtree_node *node_toadd, *tmp; \
  375. starpu_rbtree_for_each_remove(&priolist_toadd->tree, node_toadd, tmp) { \
  376. struct ENAME##_prio_list_stage *stage_toadd = ENAME##_node_to_list_stage(node_toadd); \
  377. unsigned long slot; \
  378. struct starpu_rbtree_node *node = starpu_rbtree_lookup_slot(&priolist->tree, stage_toadd->prio, ENAME##_prio_list_cmp_fn, slot); \
  379. if (node) \
  380. { \
  381. /* Catenate the lists */ \
  382. if (!ENAME##_list_empty(&stage_toadd->list)) { \
  383. struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage(node); \
  384. ENAME##_list_push_list_back(&stage->list, &stage_toadd->list); \
  385. free(node_toadd); \
  386. priolist->empty = 0; \
  387. } \
  388. } \
  389. else \
  390. { \
  391. if (!ENAME##_list_empty(&stage_toadd->list)) { \
  392. /* Just move the node between the trees */ \
  393. starpu_rbtree_insert_slot(&priolist->tree, slot, node_toadd); \
  394. priolist->empty = 0; \
  395. } \
  396. else \
  397. { \
  398. /* Actually empty, don't bother moving the list */ \
  399. free(node_toadd); \
  400. } \
  401. } \
  402. } \
  403. } \
  404. PRIO_LIST_INLINE int ENAME##_prio_list_ismember(const struct ENAME##_prio_list *priolist, const struct ENAME *e) \
  405. { \
  406. struct starpu_rbtree_node *node = starpu_rbtree_lookup(&priolist->tree, e->PRIOFIELD, ENAME##_prio_list_cmp_fn); \
  407. if (node) { \
  408. const struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage_const(node); \
  409. return ENAME##_list_ismember(&stage->list, e); \
  410. } \
  411. return 0; \
  412. } \
  413. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_begin(struct ENAME##_prio_list *priolist) \
  414. { \
  415. struct starpu_rbtree_node *node; \
  416. struct ENAME##_prio_list_stage *stage; \
  417. if (!ENAME##_prio_list_get_first_nonempty_stage(priolist, &node, &stage)) \
  418. return NULL; \
  419. return ENAME##_list_begin(&stage->list); \
  420. } \
  421. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_end(struct ENAME##_prio_list *priolist STARPU_ATTRIBUTE_UNUSED) \
  422. { return NULL; } \
  423. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_next(struct ENAME##_prio_list *priolist, const struct ENAME *i) \
  424. { \
  425. struct ENAME *next = ENAME##_list_next(i); \
  426. if (next != ENAME##_list_end(NULL)) \
  427. return next; \
  428. struct starpu_rbtree_node *node = starpu_rbtree_lookup(&priolist->tree, i->PRIOFIELD, ENAME##_prio_list_cmp_fn); \
  429. struct ENAME##_prio_list_stage *stage; \
  430. node = starpu_rbtree_next(node); \
  431. if (!ENAME##_prio_list_get_next_nonempty_stage(priolist, node, &node, &stage)) \
  432. return NULL; \
  433. return ENAME##_list_begin(&stage->list); \
  434. } \
  435. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_last(struct ENAME##_prio_list *priolist) \
  436. { \
  437. struct starpu_rbtree_node *node; \
  438. struct ENAME##_prio_list_stage *stage; \
  439. if (!ENAME##_prio_list_get_last_nonempty_stage(priolist, &node, &stage)) \
  440. return NULL; \
  441. return ENAME##_list_last(&stage->list); \
  442. } \
  443. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_alpha(struct ENAME##_prio_list *priolist STARPU_ATTRIBUTE_UNUSED) \
  444. { return NULL; } \
  445. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_prev(struct ENAME##_prio_list *priolist, const struct ENAME *i) \
  446. { \
  447. struct ENAME *next = ENAME##_list_prev(i); \
  448. if (next != ENAME##_list_alpha(NULL)) \
  449. return next; \
  450. struct starpu_rbtree_node *node = starpu_rbtree_lookup(&priolist->tree, i->PRIOFIELD, ENAME##_prio_list_cmp_fn); \
  451. struct ENAME##_prio_list_stage *stage; \
  452. node = starpu_rbtree_prev(node); \
  453. if (!ENAME##_prio_list_get_prev_nonempty_stage(priolist, node, &node, &stage)) \
  454. return NULL; \
  455. return ENAME##_list_last(&stage->list); \
  456. } \
  457. #else
  458. /* gdbinit can't recurse in a tree. Use a mere list in debugging mode. */
  459. #define PRIO_LIST_CREATE_TYPE(ENAME, PRIOFIELD) \
  460. struct ENAME##_prio_list { struct ENAME##_list list; }; \
  461. PRIO_LIST_INLINE void ENAME##_prio_list_init(struct ENAME##_prio_list *priolist) \
  462. { ENAME##_list_init(&(priolist)->list); } \
  463. PRIO_LIST_INLINE void ENAME##_prio_list_deinit(struct ENAME##_prio_list *priolist) \
  464. { (void) (priolist); /* ENAME##_list_deinit(&(priolist)->list); */ } \
  465. PRIO_LIST_INLINE void ENAME##_prio_list_push_back(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  466. { \
  467. struct ENAME *cur; \
  468. for (cur = ENAME##_list_begin(&(priolist)->list); \
  469. cur != ENAME##_list_end(&(priolist)->list); \
  470. cur = ENAME##_list_next(cur)) \
  471. if ((e)->PRIOFIELD > cur->PRIOFIELD) \
  472. break; \
  473. if (cur == ENAME##_list_end(&(priolist)->list)) \
  474. ENAME##_list_push_back(&(priolist)->list, (e)); \
  475. else \
  476. ENAME##_list_insert_before(&(priolist)->list, (e), cur); \
  477. } \
  478. PRIO_LIST_INLINE void ENAME##_prio_list_push_front(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  479. { \
  480. struct ENAME *cur; \
  481. for (cur = ENAME##_list_begin(&(priolist)->list); \
  482. cur != ENAME##_list_end(&(priolist)->list); \
  483. cur = ENAME##_list_next(cur)) \
  484. if ((e)->PRIOFIELD >= cur->PRIOFIELD) \
  485. break; \
  486. if (cur == ENAME##_list_end(&(priolist)->list)) \
  487. ENAME##_list_push_back(&(priolist)->list, (e)); \
  488. else \
  489. ENAME##_list_insert_before(&(priolist)->list, (e), cur); \
  490. } \
  491. PRIO_LIST_INLINE int ENAME##_prio_list_empty(const struct ENAME##_prio_list *priolist) \
  492. { return ENAME##_list_empty(&(priolist)->list); } \
  493. PRIO_LIST_INLINE void ENAME##_prio_list_erase(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  494. { ENAME##_list_erase(&(priolist)->list, (e)); } \
  495. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_pop_front_highest(struct ENAME##_prio_list *priolist) \
  496. { return ENAME##_list_pop_front(&(priolist)->list); } \
  497. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_pop_front_lowest(struct ENAME##_prio_list *priolist) \
  498. { return ENAME##_list_pop_front(&(priolist)->list); } \
  499. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_pop_back_highest(struct ENAME##_prio_list *priolist) \
  500. { return ENAME##_list_pop_back(&(priolist)->list); } \
  501. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_pop_back_lowest(struct ENAME##_prio_list *priolist) \
  502. { return ENAME##_list_pop_back(&(priolist)->list); } \
  503. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_front_highest(struct ENAME##_prio_list *priolist) \
  504. { return ENAME##_list_front(&(priolist)->list); } \
  505. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_front_lowest(struct ENAME##_prio_list *priolist) \
  506. { return ENAME##_list_front(&(priolist)->list); } \
  507. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_back_highest(struct ENAME##_prio_list *priolist) \
  508. { return ENAME##_list_back(&(priolist)->list); } \
  509. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_back_lowest(struct ENAME##_prio_list *priolist) \
  510. { return ENAME##_list_back(&(priolist)->list); } \
  511. PRIO_LIST_INLINE void ENAME##_prio_list_push_prio_list_back(struct ENAME##_prio_list *priolist, struct ENAME##_prio_list *priolist_toadd) \
  512. { ENAME##_list_push_list_back(&(priolist)->list, &(priolist_toadd)->list); } \
  513. PRIO_LIST_INLINE int ENAME##_prio_list_ismember(const struct ENAME##_prio_list *priolist, const struct ENAME *e) \
  514. { return ENAME##_list_ismember(&(priolist)->list, (e)); } \
  515. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_begin(struct ENAME##_prio_list *priolist) \
  516. { return ENAME##_list_begin(&(priolist)->list); } \
  517. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_end(struct ENAME##_prio_list *priolist) \
  518. { return ENAME##_list_end(&(priolist)->list); } \
  519. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_next(struct ENAME##_prio_list *priolist STARPU_ATTRIBUTE_UNUSED, const struct ENAME *i) \
  520. { return ENAME##_list_next(i); } \
  521. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_last(struct ENAME##_prio_list *priolist) \
  522. { return ENAME##_list_last(&(priolist)->list); } \
  523. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_alpha(struct ENAME##_prio_list *priolist) \
  524. { return ENAME##_list_alpha(&(priolist)->list); } \
  525. PRIO_LIST_INLINE struct ENAME *ENAME##_prio_list_prev(struct ENAME##_prio_list *priolist STARPU_ATTRIBUTE_UNUSED, const struct ENAME *i) \
  526. { return ENAME##_list_prev(i); } \
  527. #endif
  528. #endif // __PRIO_LIST_H__