From 0c777bc62fc1205a46f3d6436270917d2fc256c4 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sun, 26 Feb 2023 11:27:30 -0800 Subject: [PATCH] [util] Add move-assignment operators to node_map, deque Also add them to wait_queue as a wrapper to calling deque's move-assignmen operator. --- src/kernel/wait_queue.cpp | 12 ++++++++++++ src/kernel/wait_queue.h | 8 ++++++++ src/libraries/util/util/deque.h | 17 ++++++++++++++++- src/libraries/util/util/node_map.h | 2 +- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/kernel/wait_queue.cpp b/src/kernel/wait_queue.cpp index 1c9ea79..f226073 100644 --- a/src/kernel/wait_queue.cpp +++ b/src/kernel/wait_queue.cpp @@ -1,8 +1,20 @@ +#include #include "objects/thread.h" #include "wait_queue.h" +wait_queue::wait_queue(wait_queue &&other) : + m_threads {std::move(other.m_threads)} {} + wait_queue::~wait_queue() { clear(); } +wait_queue & +wait_queue::operator=(wait_queue &&other) +{ + clear(); + m_threads = std::move(other.m_threads); + return *this; +} + void wait_queue::add_thread(obj::thread *t) { diff --git a/src/kernel/wait_queue.h b/src/kernel/wait_queue.h index 6daec4d..28332e5 100644 --- a/src/kernel/wait_queue.h +++ b/src/kernel/wait_queue.h @@ -12,6 +12,11 @@ namespace obj { class wait_queue { public: + wait_queue() = default; + wait_queue(wait_queue &&other); + + wait_queue & operator=(wait_queue &&other); + /// Wake all threads when destructing ~wait_queue(); @@ -28,6 +33,9 @@ public: /// \arg value The value passed to thread::wake void clear(uint64_t value = 0); + /// Check if the queue is empty + bool empty() const { return m_threads.empty(); } + private: /// Get rid of any exited threads that are next /// in the queue. Caller must hold the queue lock. diff --git a/src/libraries/util/util/deque.h b/src/libraries/util/util/deque.h index 43b5677..4e40d9c 100644 --- a/src/libraries/util/util/deque.h +++ b/src/libraries/util/util/deque.h @@ -2,6 +2,7 @@ /// \file linked_list.h /// A generic templatized linked list. +#include #include #include #include @@ -37,9 +38,23 @@ public: size_t index; }; - deque() : m_first(0), m_next(N) {} + deque() : m_first {0}, m_next {N} {} + + deque(deque &&other) : + m_first {other.m_first}, + m_next {other.m_next}, + m_list {std::move(other.m_list)} {} + ~deque() { clear(); } + deque & operator=(deque &&other) { + clear(); + m_first = other.m_first; + m_next = other.m_next; + m_list = std::move(other.m_list); + return *this; + } + inline void push_front(const T& item) { if (!m_first) { // need a new block at the start node_type *n = new node_type; diff --git a/src/libraries/util/util/node_map.h b/src/libraries/util/util/node_map.h index 68d867f..e7e34b4 100644 --- a/src/libraries/util/util/node_map.h +++ b/src/libraries/util/util/node_map.h @@ -150,7 +150,7 @@ public: key_type &key_at_slot = get_map_key(node_at_slot); if (open(key_at_slot)) { - node_at_slot = node; + node_at_slot = std::move(node); if (!found) inserted_at = slot; return m_nodes[inserted_at];