Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5b323798 authored by Myles Watson's avatar Myles Watson
Browse files

OS: Hold the mutex when decreasing the semaphore

Test: bluetooth_test_gd # Run in a loop
Change-Id: I72e9f28c417986f63390cbbbe6467f64df6e9237
parent 3e6123e8
Loading
Loading
Loading
Loading
+7 −12
Original line number Diff line number Diff line
@@ -15,8 +15,7 @@
 */

template <typename T>
Queue<T>::Queue(size_t capacity)
    : enqueue_(capacity), dequeue_(0){};
Queue<T>::Queue(size_t capacity) : enqueue_(capacity), dequeue_(0){};

template <typename T>
Queue<T>::~Queue() {
@@ -49,8 +48,8 @@ void Queue<T>::RegisterDequeue(Handler* handler, DequeueCallback callback) {
  ASSERT(dequeue_.handler_ == nullptr);
  ASSERT(dequeue_.reactable_ == nullptr);
  dequeue_.handler_ = handler;
  dequeue_.reactable_ = dequeue_.handler_->reactor_->Register(dequeue_.reactive_semaphore_.GetFd(),
                                                                           [callback] { callback(); }, nullptr);
  dequeue_.reactable_ =
      dequeue_.handler_->reactor_->Register(dequeue_.reactive_semaphore_.GetFd(), [callback] { callback(); }, nullptr);
}

template <typename T>
@@ -82,14 +81,10 @@ std::unique_ptr<T> Queue<T>::TryDequeue() {

template <typename T>
void Queue<T>::EnqueueCallbackInternal(EnqueueCallback callback) {
  enqueue_.reactive_semaphore_.Decrease();

  {
  std::unique_ptr<T> data = callback();
  ASSERT(data != nullptr);
  std::lock_guard<std::mutex> lock(mutex_);
  enqueue_.reactive_semaphore_.Decrease();
  queue_.push(std::move(data));
  }

  dequeue_.reactive_semaphore_.Increase();
}