task_barrier.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2019 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <linux/semaphore.h>
  24. #include <linux/atomic.h>
  25. /*
  26. * Reusable 2 PHASE task barrier (randevouz point) implementation for N tasks.
  27. * Based on the Little book of sempahores - https://greenteapress.com/wp/semaphores/
  28. */
  29. #ifndef DRM_TASK_BARRIER_H_
  30. #define DRM_TASK_BARRIER_H_
  31. /*
  32. * Represents an instance of a task barrier.
  33. */
  34. struct task_barrier {
  35. unsigned int n;
  36. atomic_t count;
  37. struct semaphore enter_turnstile;
  38. struct semaphore exit_turnstile;
  39. };
  40. static inline void task_barrier_signal_turnstile(struct semaphore *turnstile,
  41. unsigned int n)
  42. {
  43. int i;
  44. for (i = 0 ; i < n; i++)
  45. up(turnstile);
  46. }
  47. static inline void task_barrier_init(struct task_barrier *tb)
  48. {
  49. tb->n = 0;
  50. atomic_set(&tb->count, 0);
  51. sema_init(&tb->enter_turnstile, 0);
  52. sema_init(&tb->exit_turnstile, 0);
  53. }
  54. static inline void task_barrier_add_task(struct task_barrier *tb)
  55. {
  56. tb->n++;
  57. }
  58. static inline void task_barrier_rem_task(struct task_barrier *tb)
  59. {
  60. tb->n--;
  61. }
  62. /*
  63. * Lines up all the threads BEFORE the critical point.
  64. *
  65. * When all thread passed this code the entry barrier is back to locked state.
  66. */
  67. static inline void task_barrier_enter(struct task_barrier *tb)
  68. {
  69. if (atomic_inc_return(&tb->count) == tb->n)
  70. task_barrier_signal_turnstile(&tb->enter_turnstile, tb->n);
  71. down(&tb->enter_turnstile);
  72. }
  73. /*
  74. * Lines up all the threads AFTER the critical point.
  75. *
  76. * This function is used to avoid any one thread running ahead if the barrier is
  77. * used repeatedly .
  78. */
  79. static inline void task_barrier_exit(struct task_barrier *tb)
  80. {
  81. if (atomic_dec_return(&tb->count) == 0)
  82. task_barrier_signal_turnstile(&tb->exit_turnstile, tb->n);
  83. down(&tb->exit_turnstile);
  84. }
  85. /* Convinieince function when nothing to be done in between entry and exit */
  86. static inline void task_barrier_full(struct task_barrier *tb)
  87. {
  88. task_barrier_enter(tb);
  89. task_barrier_exit(tb);
  90. }
  91. #endif