priority_queue.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /* Copyright (C) 2015-2022 Free Software Foundation, Inc.
  2. Contributed by Aldy Hernandez <aldyh@redhat.com>.
  3. This file is part of the GNU Offloading and Multi Processing Library
  4. (libgomp).
  5. Libgomp is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* Header file for a priority queue of GOMP tasks. */
  21. /* ?? Perhaps all the priority_tree_* functions are complex and rare
  22. enough to go out-of-line and be moved to priority_queue.c. ?? */
  23. #ifndef _PRIORITY_QUEUE_H_
  24. #define _PRIORITY_QUEUE_H_
  25. /* One task. */
  26. struct priority_node
  27. {
  28. /* Next and previous chains in a circular doubly linked list for
  29. tasks within this task's priority. */
  30. struct priority_node *next, *prev;
  31. };
  32. /* All tasks within the same priority. */
  33. struct priority_list
  34. {
  35. /* Priority of the tasks in this set. */
  36. int priority;
  37. /* Tasks. */
  38. struct priority_node *tasks;
  39. /* This points to the last of the higher priority WAITING tasks.
  40. Remember that for the children queue, we have:
  41. parent_depends_on WAITING tasks.
  42. !parent_depends_on WAITING tasks.
  43. TIED tasks.
  44. This is a pointer to the last of the parent_depends_on WAITING
  45. tasks which are essentially, higher priority items within their
  46. priority. */
  47. struct priority_node *last_parent_depends_on;
  48. };
  49. /* Another splay tree instantiation, for priority_list's. */
  50. typedef struct prio_splay_tree_node_s *prio_splay_tree_node;
  51. typedef struct prio_splay_tree_s *prio_splay_tree;
  52. typedef struct prio_splay_tree_key_s *prio_splay_tree_key;
  53. struct prio_splay_tree_key_s {
  54. /* This structure must only containing a priority_list, as we cast
  55. prio_splay_tree_key to priority_list throughout. */
  56. struct priority_list l;
  57. };
  58. #define splay_tree_prefix prio
  59. #include "splay-tree.h"
  60. /* The entry point into a priority queue of tasks.
  61. There are two alternate implementations with which to store tasks:
  62. as a balanced tree of sorts, or as a simple list of tasks. If
  63. there are only priority-0 items (ROOT is NULL), we use the simple
  64. list, otherwise (ROOT is non-NULL) we use the tree. */
  65. struct priority_queue
  66. {
  67. /* If t.root != NULL, this is a splay tree of priority_lists to hold
  68. all tasks. This is only used if multiple priorities are in play,
  69. otherwise we use the priority_list `l' below to hold all
  70. (priority-0) tasks. */
  71. struct prio_splay_tree_s t;
  72. /* If T above is NULL, only priority-0 items exist, so keep them
  73. in a simple list. */
  74. struct priority_list l;
  75. };
  76. enum priority_insert_type {
  77. /* Insert at the beginning of a priority list. */
  78. PRIORITY_INSERT_BEGIN,
  79. /* Insert at the end of a priority list. */
  80. PRIORITY_INSERT_END
  81. };
  82. /* Used to determine in which queue a given priority node belongs in.
  83. See pnode field of gomp_task. */
  84. enum priority_queue_type
  85. {
  86. PQ_TEAM, /* Node belongs in gomp_team's task_queue. */
  87. PQ_CHILDREN, /* Node belongs in parent's children_queue. */
  88. PQ_TASKGROUP, /* Node belongs in taskgroup->taskgroup_queue. */
  89. PQ_IGNORED = 999
  90. };
  91. typedef bool (*priority_queue_predicate) (struct gomp_task *);
  92. /* Priority queue implementation prototypes. */
  93. extern bool priority_queue_task_in_queue_p (enum priority_queue_type,
  94. struct priority_queue *,
  95. struct gomp_task *);
  96. extern void priority_queue_dump (enum priority_queue_type,
  97. struct priority_queue *);
  98. extern void priority_queue_verify (enum priority_queue_type,
  99. struct priority_queue *, bool);
  100. extern struct gomp_task *priority_queue_find (enum priority_queue_type,
  101. struct priority_queue *,
  102. priority_queue_predicate);
  103. extern void priority_tree_remove (enum priority_queue_type,
  104. struct priority_queue *,
  105. struct priority_node *);
  106. extern struct gomp_task *priority_tree_next_task (enum priority_queue_type,
  107. struct priority_queue *,
  108. enum priority_queue_type,
  109. struct priority_queue *,
  110. bool *);
  111. /* Return TRUE if there is more than one priority in HEAD. This is
  112. used throughout to to choose between the fast path (priority 0 only
  113. items) and a world with multiple priorities. */
  114. static inline bool
  115. priority_queue_multi_p (struct priority_queue *head)
  116. {
  117. return __builtin_expect (head->t.root != NULL, 0);
  118. }
  119. /* Initialize a priority queue. */
  120. static inline void
  121. priority_queue_init (struct priority_queue *head)
  122. {
  123. head->t.root = NULL;
  124. /* To save a few microseconds, we don't initialize head->l.priority
  125. to 0 here. It is implied that priority will be 0 if head->t.root
  126. == NULL.
  127. priority_tree_insert() will fix this when we encounter multiple
  128. priorities. */
  129. head->l.tasks = NULL;
  130. head->l.last_parent_depends_on = NULL;
  131. }
  132. static inline void
  133. priority_queue_free (struct priority_queue *head)
  134. {
  135. /* There's nothing to do, as tasks were freed as they were removed
  136. in priority_queue_remove. */
  137. }
  138. /* Forward declarations. */
  139. static inline size_t priority_queue_offset (enum priority_queue_type);
  140. static inline struct gomp_task *priority_node_to_task
  141. (enum priority_queue_type,
  142. struct priority_node *);
  143. static inline struct priority_node *task_to_priority_node
  144. (enum priority_queue_type,
  145. struct gomp_task *);
  146. /* Return TRUE if priority queue HEAD is empty.
  147. MODEL IS MEMMODEL_ACQUIRE if we should use an acquire atomic to
  148. read from the root of the queue, otherwise MEMMODEL_RELAXED if we
  149. should use a plain load. */
  150. static inline _Bool
  151. priority_queue_empty_p (struct priority_queue *head, enum memmodel model)
  152. {
  153. /* Note: The acquire barriers on the loads here synchronize with
  154. the write of a NULL in gomp_task_run_post_remove_parent. It is
  155. not necessary that we synchronize with other non-NULL writes at
  156. this point, but we must ensure that all writes to memory by a
  157. child thread task work function are seen before we exit from
  158. GOMP_taskwait. */
  159. if (priority_queue_multi_p (head))
  160. {
  161. if (model == MEMMODEL_ACQUIRE)
  162. return __atomic_load_n (&head->t.root, MEMMODEL_ACQUIRE) == NULL;
  163. return head->t.root == NULL;
  164. }
  165. if (model == MEMMODEL_ACQUIRE)
  166. return __atomic_load_n (&head->l.tasks, MEMMODEL_ACQUIRE) == NULL;
  167. return head->l.tasks == NULL;
  168. }
  169. /* Look for a given PRIORITY in HEAD. Return it if found, otherwise
  170. return NULL. This only applies to the tree variant in HEAD. There
  171. is no point in searching for priorities in HEAD->L. */
  172. static inline struct priority_list *
  173. priority_queue_lookup_priority (struct priority_queue *head, int priority)
  174. {
  175. if (head->t.root == NULL)
  176. return NULL;
  177. struct prio_splay_tree_key_s k;
  178. k.l.priority = priority;
  179. return (struct priority_list *)
  180. prio_splay_tree_lookup (&head->t, &k);
  181. }
  182. /* Insert task in DATA, with PRIORITY, in the priority list in LIST.
  183. LIST contains items of type TYPE.
  184. If POS is PRIORITY_INSERT_BEGIN, the new task is inserted at the
  185. top of its respective priority. If POS is PRIORITY_INSERT_END, the
  186. task is inserted at the end of its priority.
  187. If ADJUST_PARENT_DEPENDS_ON is TRUE, LIST is a children queue, and
  188. we must keep track of higher and lower priority WAITING tasks by
  189. keeping the queue's last_parent_depends_on field accurate. This
  190. only applies to the children queue, and the caller must ensure LIST
  191. is a children queue in this case.
  192. If ADJUST_PARENT_DEPENDS_ON is TRUE, TASK_IS_PARENT_DEPENDS_ON is
  193. set to the task's parent_depends_on field. If
  194. ADJUST_PARENT_DEPENDS_ON is FALSE, this field is irrelevant.
  195. Return the new priority_node. */
  196. static inline void
  197. priority_list_insert (enum priority_queue_type type,
  198. struct priority_list *list,
  199. struct gomp_task *task,
  200. int priority,
  201. enum priority_insert_type pos,
  202. bool adjust_parent_depends_on,
  203. bool task_is_parent_depends_on)
  204. {
  205. struct priority_node *node = task_to_priority_node (type, task);
  206. if (list->tasks)
  207. {
  208. /* If we are keeping track of higher/lower priority items,
  209. but this is a lower priority WAITING task
  210. (parent_depends_on != NULL), put it after all ready to
  211. run tasks. See the comment in
  212. priority_queue_upgrade_task for a visual on how tasks
  213. should be organized. */
  214. if (adjust_parent_depends_on
  215. && pos == PRIORITY_INSERT_BEGIN
  216. && list->last_parent_depends_on
  217. && !task_is_parent_depends_on)
  218. {
  219. struct priority_node *last_parent_depends_on
  220. = list->last_parent_depends_on;
  221. node->next = last_parent_depends_on->next;
  222. node->prev = last_parent_depends_on;
  223. }
  224. /* Otherwise, put it at the top/bottom of the queue. */
  225. else
  226. {
  227. node->next = list->tasks;
  228. node->prev = list->tasks->prev;
  229. if (pos == PRIORITY_INSERT_BEGIN)
  230. list->tasks = node;
  231. }
  232. node->next->prev = node;
  233. node->prev->next = node;
  234. }
  235. else
  236. {
  237. node->next = node;
  238. node->prev = node;
  239. list->tasks = node;
  240. }
  241. if (adjust_parent_depends_on
  242. && list->last_parent_depends_on == NULL
  243. && task_is_parent_depends_on)
  244. list->last_parent_depends_on = node;
  245. }
  246. /* Tree version of priority_list_insert. */
  247. static inline void
  248. priority_tree_insert (enum priority_queue_type type,
  249. struct priority_queue *head,
  250. struct gomp_task *task,
  251. int priority,
  252. enum priority_insert_type pos,
  253. bool adjust_parent_depends_on,
  254. bool task_is_parent_depends_on)
  255. {
  256. if (__builtin_expect (head->t.root == NULL, 0))
  257. {
  258. /* The first time around, transfer any priority 0 items to the
  259. tree. */
  260. if (head->l.tasks != NULL)
  261. {
  262. prio_splay_tree_node k = gomp_malloc (sizeof (*k));
  263. k->left = NULL;
  264. k->right = NULL;
  265. k->key.l.priority = 0;
  266. k->key.l.tasks = head->l.tasks;
  267. k->key.l.last_parent_depends_on = head->l.last_parent_depends_on;
  268. prio_splay_tree_insert (&head->t, k);
  269. head->l.tasks = NULL;
  270. }
  271. }
  272. struct priority_list *list
  273. = priority_queue_lookup_priority (head, priority);
  274. if (!list)
  275. {
  276. prio_splay_tree_node k = gomp_malloc (sizeof (*k));
  277. k->left = NULL;
  278. k->right = NULL;
  279. k->key.l.priority = priority;
  280. k->key.l.tasks = NULL;
  281. k->key.l.last_parent_depends_on = NULL;
  282. prio_splay_tree_insert (&head->t, k);
  283. list = &k->key.l;
  284. }
  285. priority_list_insert (type, list, task, priority, pos,
  286. adjust_parent_depends_on,
  287. task_is_parent_depends_on);
  288. }
  289. /* Generic version of priority_*_insert. */
  290. static inline void
  291. priority_queue_insert (enum priority_queue_type type,
  292. struct priority_queue *head,
  293. struct gomp_task *task,
  294. int priority,
  295. enum priority_insert_type pos,
  296. bool adjust_parent_depends_on,
  297. bool task_is_parent_depends_on)
  298. {
  299. #if _LIBGOMP_CHECKING_
  300. if (priority_queue_task_in_queue_p (type, head, task))
  301. gomp_fatal ("Attempt to insert existing task %p", task);
  302. #endif
  303. if (priority_queue_multi_p (head) || __builtin_expect (priority > 0, 0))
  304. priority_tree_insert (type, head, task, priority, pos,
  305. adjust_parent_depends_on,
  306. task_is_parent_depends_on);
  307. else
  308. priority_list_insert (type, &head->l, task, priority, pos,
  309. adjust_parent_depends_on,
  310. task_is_parent_depends_on);
  311. }
  312. /* If multiple priorities are in play, return the highest priority
  313. task from within Q1 and Q2, while giving preference to tasks from
  314. Q1. If the returned task is chosen from Q1, *Q1_CHOSEN_P is set to
  315. TRUE, otherwise it is set to FALSE.
  316. If multiple priorities are not in play (only 0 priorities are
  317. available), the next task is chosen exclusively from Q1.
  318. As a special case, Q2 can be NULL, in which case, we just choose
  319. the highest priority WAITING task in Q1. This is an optimization
  320. to speed up looking through only one queue.
  321. We assume Q1 has at least one item. */
  322. static inline struct gomp_task *
  323. priority_queue_next_task (enum priority_queue_type t1,
  324. struct priority_queue *q1,
  325. enum priority_queue_type t2,
  326. struct priority_queue *q2,
  327. bool *q1_chosen_p)
  328. {
  329. #if _LIBGOMP_CHECKING_
  330. if (priority_queue_empty_p (q1, MEMMODEL_RELAXED))
  331. gomp_fatal ("priority_queue_next_task: Q1 is empty");
  332. #endif
  333. if (priority_queue_multi_p (q1))
  334. {
  335. struct gomp_task *t
  336. = priority_tree_next_task (t1, q1, t2, q2, q1_chosen_p);
  337. /* If T is NULL, there are no WAITING tasks in Q1. In which
  338. case, return any old (non-waiting) task which will cause the
  339. caller to do the right thing when checking T->KIND ==
  340. GOMP_TASK_WAITING. */
  341. if (!t)
  342. {
  343. #if _LIBGOMP_CHECKING_
  344. if (*q1_chosen_p == false)
  345. gomp_fatal ("priority_queue_next_task inconsistency");
  346. #endif
  347. return priority_node_to_task (t1, q1->t.root->key.l.tasks);
  348. }
  349. return t;
  350. }
  351. else
  352. {
  353. *q1_chosen_p = true;
  354. return priority_node_to_task (t1, q1->l.tasks);
  355. }
  356. }
  357. /* Remove NODE from LIST.
  358. If we are removing the one and only item in the list, and MODEL is
  359. MEMMODEL_RELEASE, use an atomic release to clear the list.
  360. If the list becomes empty after the remove, return TRUE. */
  361. static inline bool
  362. priority_list_remove (struct priority_list *list,
  363. struct priority_node *node,
  364. enum memmodel model)
  365. {
  366. bool empty = false;
  367. node->prev->next = node->next;
  368. node->next->prev = node->prev;
  369. if (list->tasks == node)
  370. {
  371. if (node->next != node)
  372. list->tasks = node->next;
  373. else
  374. {
  375. /* We access task->children in GOMP_taskwait outside of
  376. the task lock mutex region, so need a release barrier
  377. here to ensure memory written by child_task->fn above
  378. is flushed before the NULL is written. */
  379. if (model == MEMMODEL_RELEASE)
  380. __atomic_store_n (&list->tasks, NULL, MEMMODEL_RELEASE);
  381. else
  382. list->tasks = NULL;
  383. empty = true;
  384. goto remove_out;
  385. }
  386. }
  387. remove_out:
  388. #if _LIBGOMP_CHECKING_
  389. memset (node, 0xaf, sizeof (*node));
  390. #endif
  391. return empty;
  392. }
  393. /* This is the generic version of priority_list_remove.
  394. Remove NODE from priority queue HEAD. HEAD contains tasks of type TYPE.
  395. If we are removing the one and only item in the priority queue and
  396. MODEL is MEMMODEL_RELEASE, use an atomic release to clear the queue.
  397. If the queue becomes empty after the remove, return TRUE. */
  398. static inline bool
  399. priority_queue_remove (enum priority_queue_type type,
  400. struct priority_queue *head,
  401. struct gomp_task *task,
  402. enum memmodel model)
  403. {
  404. #if _LIBGOMP_CHECKING_
  405. if (!priority_queue_task_in_queue_p (type, head, task))
  406. gomp_fatal ("Attempt to remove missing task %p", task);
  407. #endif
  408. if (priority_queue_multi_p (head))
  409. {
  410. priority_tree_remove (type, head, task_to_priority_node (type, task));
  411. if (head->t.root == NULL)
  412. {
  413. if (model == MEMMODEL_RELEASE)
  414. /* Errr, we store NULL twice, the alternative would be to
  415. use an atomic release directly in the splay tree
  416. routines. Worth it? */
  417. __atomic_store_n (&head->t.root, NULL, MEMMODEL_RELEASE);
  418. return true;
  419. }
  420. return false;
  421. }
  422. else
  423. return priority_list_remove (&head->l,
  424. task_to_priority_node (type, task), model);
  425. }
  426. #endif /* _PRIORITY_QUEUE_H_ */