spsc_queue.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2017 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. #ifndef AMD_SCHEDULER_SPSC_QUEUE_H_
  24. #define AMD_SCHEDULER_SPSC_QUEUE_H_
  25. #include <linux/atomic.h>
  26. /** SPSC lockless queue */
  27. struct spsc_node {
  28. /* Stores spsc_node* */
  29. struct spsc_node *next;
  30. };
  31. struct spsc_queue {
  32. struct spsc_node *head;
  33. /* atomic pointer to struct spsc_node* */
  34. atomic_long_t tail;
  35. atomic_t job_count;
  36. };
  37. static inline void spsc_queue_init(struct spsc_queue *queue)
  38. {
  39. queue->head = NULL;
  40. atomic_long_set(&queue->tail, (long)&queue->head);
  41. atomic_set(&queue->job_count, 0);
  42. }
  43. static inline struct spsc_node *spsc_queue_peek(struct spsc_queue *queue)
  44. {
  45. return queue->head;
  46. }
  47. static inline int spsc_queue_count(struct spsc_queue *queue)
  48. {
  49. return atomic_read(&queue->job_count);
  50. }
  51. static inline bool spsc_queue_push(struct spsc_queue *queue, struct spsc_node *node)
  52. {
  53. struct spsc_node **tail;
  54. node->next = NULL;
  55. preempt_disable();
  56. tail = (struct spsc_node **)atomic_long_xchg(&queue->tail, (long)&node->next);
  57. WRITE_ONCE(*tail, node);
  58. atomic_inc(&queue->job_count);
  59. /*
  60. * In case of first element verify new node will be visible to the consumer
  61. * thread when we ping the kernel thread that there is new work to do.
  62. */
  63. smp_wmb();
  64. preempt_enable();
  65. return tail == &queue->head;
  66. }
  67. static inline struct spsc_node *spsc_queue_pop(struct spsc_queue *queue)
  68. {
  69. struct spsc_node *next, *node;
  70. /* Verify reading from memory and not the cache */
  71. smp_rmb();
  72. node = READ_ONCE(queue->head);
  73. if (!node)
  74. return NULL;
  75. next = READ_ONCE(node->next);
  76. WRITE_ONCE(queue->head, next);
  77. if (unlikely(!next)) {
  78. /* slowpath for the last element in the queue */
  79. if (atomic_long_cmpxchg(&queue->tail,
  80. (long)&node->next, (long) &queue->head) != (long)&node->next) {
  81. /* Updating tail failed wait for new next to appear */
  82. do {
  83. smp_rmb();
  84. } while (unlikely(!(queue->head = READ_ONCE(node->next))));
  85. }
  86. }
  87. atomic_dec(&queue->job_count);
  88. return node;
  89. }
  90. #endif /* AMD_SCHEDULER_SPSC_QUEUE_H_ */