ring_buf.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //============================================================================
  2. // Lock-Free Ring Buffer (LFRB) for embedded systems
  3. // GitHub: https://github.com/QuantumLeaps/lock-free-ring-buffer
  4. //
  5. // Q u a n t u m L e a P s
  6. // ------------------------
  7. // Modern Embedded Software
  8. //
  9. // Copyright (C) 2005 Quantum Leaps, <state-machine.com>.
  10. //
  11. // SPDX-License-Identifier: MIT
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining a
  14. // copy of this software and associated documentation files (the "Software"),
  15. // to deal in the Software without restriction, including without limitation
  16. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. // and/or sell copies of the Software, and to permit persons to whom the
  18. // Software is furnished to do so, subject to the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be included in
  21. // all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. // DEALINGS IN THE SOFTWARE.
  30. //============================================================================
  31. #include <stdint.h>
  32. #include <stdbool.h>
  33. #include "ring_buf.h"
  34. //............................................................................
  35. void RingBuf_ctor(RingBuf * const me,
  36. RingBufElement sto[], RingBufCtr sto_len) {
  37. me->buf = &sto[0];
  38. me->end = sto_len;
  39. atomic_store(&me->head, 0U); // initialize head atomically
  40. atomic_store(&me->tail, 0U); // initialize tail atomically
  41. }
  42. //............................................................................
  43. bool RingBuf_put(RingBuf * const me, RingBufElement const el) {
  44. RingBufCtr head =
  45. atomic_load_explicit(&me->head, memory_order_relaxed) + 1U;
  46. if (head == me->end) {
  47. head = 0U;
  48. }
  49. RingBufCtr tail = atomic_load_explicit(&me->tail, memory_order_acquire);
  50. if (head != tail) { // buffer NOT full?
  51. me->buf[atomic_load_explicit(&me->head, memory_order_relaxed)] = el;
  52. atomic_store_explicit(&me->head, head, memory_order_release);
  53. return true;
  54. }
  55. else {
  56. return false; // buffer full
  57. }
  58. }
  59. //............................................................................
  60. bool RingBuf_get(RingBuf * const me, RingBufElement *pel) {
  61. RingBufCtr tail = atomic_load_explicit(&me->tail, memory_order_relaxed);
  62. RingBufCtr head = atomic_load_explicit(&me->head, memory_order_acquire);
  63. if (head != tail) { // buffer NOT empty?
  64. *pel = me->buf[tail];
  65. ++tail;
  66. if (tail == me->end) {
  67. tail = 0U;
  68. }
  69. atomic_store_explicit(&me->tail, tail, memory_order_release);
  70. return true;
  71. }
  72. else {
  73. return false; // buffer empty
  74. }
  75. }
  76. //............................................................................
  77. RingBufCtr RingBuf_num_free(RingBuf * const me) {
  78. RingBufCtr head = atomic_load_explicit(&me->head, memory_order_acquire);
  79. RingBufCtr tail = atomic_load_explicit(&me->tail, memory_order_relaxed);
  80. if (head == tail) { // buffer empty?
  81. return (RingBufCtr)(me->end - 1U);
  82. }
  83. else if (head < tail) {
  84. return (RingBufCtr)(tail - head - 1U);
  85. }
  86. else {
  87. return (RingBufCtr)(me->end + tail - head - 1U);
  88. }
  89. }
  90. //............................................................................
  91. void RingBuf_process_all(RingBuf * const me, RingBufHandler handler) {
  92. RingBufCtr tail = atomic_load_explicit(&me->tail, memory_order_relaxed);
  93. RingBufCtr head = atomic_load_explicit(&me->head, memory_order_acquire);
  94. while (head != tail) { // buffer NOT empty?
  95. (*handler)(me->buf[tail]);
  96. ++tail;
  97. if (tail == me->end) {
  98. tail = 0U;
  99. }
  100. atomic_store_explicit(&me->tail, tail, memory_order_release);
  101. }
  102. }