You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
6.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_ATOMICOPS_H_
  28. #define TALK_BASE_ATOMICOPS_H_
  29. #include <string>
  30. #include "talk/base/basictypes.h"
  31. #include "talk/base/common.h"
  32. #include "talk/base/logging.h"
  33. #include "talk/base/scoped_ptr.h"
  34. namespace talk_base {
  35. // A single-producer, single-consumer, fixed-size queue.
  36. // All methods not ending in Unsafe can be safely called without locking,
  37. // provided that calls to consumer methods (Peek/Pop) or producer methods (Push)
  38. // only happen on a single thread per method type. If multiple threads need to
  39. // read simultaneously or write simultaneously, other synchronization is
  40. // necessary. Synchronization is also required if a call into any Unsafe method
  41. // could happen at the same time as a call to any other method.
  42. template <typename T>
  43. class FixedSizeLockFreeQueue {
  44. private:
  45. // Atomic primitives and memory barrier
  46. #if defined(__arm__)
  47. typedef uint32 Atomic32;
  48. // Copied from google3/base/atomicops-internals-arm-v6plus.h
  49. static inline void MemoryBarrier() {
  50. asm volatile("dmb":::"memory");
  51. }
  52. // Adapted from google3/base/atomicops-internals-arm-v6plus.h
  53. static inline void AtomicIncrement(volatile Atomic32* ptr) {
  54. Atomic32 str_success, value;
  55. asm volatile (
  56. "1:\n"
  57. "ldrex %1, [%2]\n"
  58. "add %1, %1, #1\n"
  59. "strex %0, %1, [%2]\n"
  60. "teq %0, #0\n"
  61. "bne 1b"
  62. : "=&r"(str_success), "=&r"(value)
  63. : "r" (ptr)
  64. : "cc", "memory");
  65. }
  66. #elif !defined(SKIP_ATOMIC_CHECK)
  67. #error "No atomic operations defined for the given architecture."
  68. #endif
  69. public:
  70. // Constructs an empty queue, with capacity 0.
  71. FixedSizeLockFreeQueue() : pushed_count_(0),
  72. popped_count_(0),
  73. capacity_(0),
  74. data_(NULL) {}
  75. // Constructs an empty queue with the given capacity.
  76. FixedSizeLockFreeQueue(size_t capacity) : pushed_count_(0),
  77. popped_count_(0),
  78. capacity_(capacity),
  79. data_(new T[capacity]) {}
  80. // Pushes a value onto the queue. Returns true if the value was successfully
  81. // pushed (there was space in the queue). This method can be safely called at
  82. // the same time as PeekFront/PopFront.
  83. bool PushBack(T value) {
  84. if (capacity_ == 0) {
  85. LOG(LS_WARNING) << "Queue capacity is 0.";
  86. return false;
  87. }
  88. if (IsFull()) {
  89. return false;
  90. }
  91. data_[pushed_count_ % capacity_] = value;
  92. // Make sure the data is written before the count is incremented, so other
  93. // threads can't see the value exists before being able to read it.
  94. MemoryBarrier();
  95. AtomicIncrement(&pushed_count_);
  96. return true;
  97. }
  98. // Retrieves the oldest value pushed onto the queue. Returns true if there was
  99. // an item to peek (the queue was non-empty). This method can be safely called
  100. // at the same time as PushBack.
  101. bool PeekFront(T* value_out) {
  102. if (capacity_ == 0) {
  103. LOG(LS_WARNING) << "Queue capacity is 0.";
  104. return false;
  105. }
  106. if (IsEmpty()) {
  107. return false;
  108. }
  109. *value_out = data_[popped_count_ % capacity_];
  110. return true;
  111. }
  112. // Retrieves the oldest value pushed onto the queue and removes it from the
  113. // queue. Returns true if there was an item to pop (the queue was non-empty).
  114. // This method can be safely called at the same time as PushBack.
  115. bool PopFront(T* value_out) {
  116. if (PeekFront(value_out)) {
  117. AtomicIncrement(&popped_count_);
  118. return true;
  119. }
  120. return false;
  121. }
  122. // Clears the current items in the queue and sets the new (fixed) size. This
  123. // method cannot be called at the same time as any other method.
  124. void ClearAndResizeUnsafe(int new_capacity) {
  125. capacity_ = new_capacity;
  126. data_.reset(new T[new_capacity]);
  127. pushed_count_ = 0;
  128. popped_count_ = 0;
  129. }
  130. // Returns true if there is no space left in the queue for new elements.
  131. int IsFull() const { return pushed_count_ == popped_count_ + capacity_; }
  132. // Returns true if there are no elements in the queue.
  133. int IsEmpty() const { return pushed_count_ == popped_count_; }
  134. // Returns the current number of elements in the queue. This is always in the
  135. // range [0, capacity]
  136. size_t Size() const { return pushed_count_ - popped_count_; }
  137. // Returns the capacity of the queue (max size).
  138. size_t capacity() const { return capacity_; }
  139. private:
  140. volatile Atomic32 pushed_count_;
  141. volatile Atomic32 popped_count_;
  142. size_t capacity_;
  143. talk_base::scoped_array<T> data_;
  144. DISALLOW_COPY_AND_ASSIGN(FixedSizeLockFreeQueue);
  145. };
  146. }
  147. #endif // TALK_BASE_ATOMICOPS_H_