deque.cpp 1.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <mutex>

#include "pls/internal/base/deque.h"

namespace pls {
    namespace internal {
        namespace base {
            deque_item* deque_internal::pop_head_internal() {
                std::lock_guard<spin_lock> lock{lock_};

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

                deque_item* result = head_;
                head_ = head_->prev_;
                if (head_ == nullptr) {
                    tail_ = nullptr;
                } else {
                    head_->next_ = nullptr;
                }

                return result;
            }

            deque_item* deque_internal::pop_tail_internal() {
                std::lock_guard<spin_lock> lock{lock_};

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

                deque_item* result = tail_;
                tail_ = tail_->next_;
                if (tail_ == nullptr) {
                    head_ = nullptr;
                } else {
                    tail_->prev_ = nullptr;
                }

                return result;
            }

            void deque_internal::push_tail_internal(deque_item *new_item) {
                std::lock_guard<spin_lock> lock{lock_};

                if (tail_ != nullptr) {
                    tail_->prev_ = new_item;
                } else {
                    head_ = new_item;
                }
                new_item->next_ = tail_;
                new_item->prev_ = nullptr;
                tail_ = new_item;
            }
        }
    }
}