locking_deque.cpp 1.1 KB
Newer Older
1 2
#include <mutex>

3
#include "pls/internal/data_structures/locking_deque.h"
4 5

namespace pls {
6 7 8
namespace internal {
namespace data_structures {

9
locking_deque_item *locking_deque_internal::pop_head_internal() {
10 11 12 13 14 15
  std::lock_guard<base::spin_lock> lock{lock_};

  if (head_ == nullptr) {
    return nullptr;
  }

16
  locking_deque_item *result = head_;
17
  head_ = head_->next_;
18 19 20
  if (head_ == nullptr) {
    tail_ = nullptr;
  } else {
21
    head_->prev_ = nullptr;
22 23 24 25 26
  }

  return result;
}

27
locking_deque_item *locking_deque_internal::pop_tail_internal() {
28 29 30 31 32 33
  std::lock_guard<base::spin_lock> lock{lock_};

  if (tail_ == nullptr) {
    return nullptr;
  }

34
  locking_deque_item *result = tail_;
35
  tail_ = tail_->prev_;
36 37 38
  if (tail_ == nullptr) {
    head_ = nullptr;
  } else {
39
    tail_->next_ = nullptr;
40 41 42 43 44
  }

  return result;
}

45
void locking_deque_internal::push_tail_internal(locking_deque_item *new_item) {
46 47 48
  std::lock_guard<base::spin_lock> lock{lock_};

  if (tail_ != nullptr) {
49
    tail_->next_ = new_item;
50 51 52
  } else {
    head_ = new_item;
  }
53 54
  new_item->prev_ = tail_;
  new_item->next_ = nullptr;
55 56 57 58 59
  tail_ = new_item;
}

}
}
60
}