From e912cfc19ddfa54d4aeb5eb5f2f99b728e0b418b Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 19 Aug 2023 15:28:45 +0100 Subject: [PATCH] Ring buffer: Use as backing for std::queue uses --- src/core/CMakeLists.txt | 1 + src/core/ring_buffer.hpp | 5 +++++ src/core/ring_buffer_queue.hpp | 20 ++++++++++++++++++++ src/linkgraph/demands.cpp | 4 ++-- src/network/core/http_curl.cpp | 4 ++-- src/script/api/script_event.cpp | 4 ++-- src/worker_thread.h | 4 ++-- 7 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 src/core/ring_buffer_queue.hpp diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index ebb7efa509..d060255b96 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -29,6 +29,7 @@ add_files( random_func.cpp random_func.hpp ring_buffer.hpp + ring_buffer_queue.hpp serialisation.cpp serialisation.hpp smallstack_type.hpp diff --git a/src/core/ring_buffer.hpp b/src/core/ring_buffer.hpp index 01484f2cca..8ff914eb45 100644 --- a/src/core/ring_buffer.hpp +++ b/src/core/ring_buffer.hpp @@ -192,6 +192,11 @@ public: } }; + using difference_type = std::ptrdiff_t; + using size_type = size_t; + using value_type = T; + using reference = T &; + using const_reference = const T &; typedef ring_buffer_iterator iterator; typedef ring_buffer_iterator const_iterator; typedef ring_buffer_iterator reverse_iterator; diff --git a/src/core/ring_buffer_queue.hpp b/src/core/ring_buffer_queue.hpp new file mode 100644 index 0000000000..894cc8bdf2 --- /dev/null +++ b/src/core/ring_buffer_queue.hpp @@ -0,0 +1,20 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file ring_buffer_queue.hpp std::queue backed by a ring buffer. */ + +#ifndef RING_BUFFER_QUEUE_HPP +#define RING_BUFFER_QUEUE_HPP + +#include "ring_buffer.hpp" + +#include + +template +using ring_buffer_queue = std::queue>; + +#endif /* RING_BUFFER_QUEUE_HPP */ diff --git a/src/linkgraph/demands.cpp b/src/linkgraph/demands.cpp index 88128daced..4e7c71c309 100644 --- a/src/linkgraph/demands.cpp +++ b/src/linkgraph/demands.cpp @@ -2,13 +2,13 @@ #include "../stdafx.h" #include "demands.h" -#include +#include "../core/ring_buffer_queue.hpp" #include #include #include "../safeguards.h" -typedef std::queue NodeList; +typedef ring_buffer_queue NodeList; /** * Scale various things according to symmetric/asymmetric distribution. diff --git a/src/network/core/http_curl.cpp b/src/network/core/http_curl.cpp index c3422d0789..9368533fcf 100644 --- a/src/network/core/http_curl.cpp +++ b/src/network/core/http_curl.cpp @@ -14,6 +14,7 @@ #include "../../fileio_func.h" #include "../../rev.h" #include "../../thread.h" +#include "../../core/ring_buffer_queue.hpp" #include "../network_internal.h" #include "http.h" @@ -23,7 +24,6 @@ #include #include #include -#include #include "../../safeguards.h" @@ -69,7 +69,7 @@ public: static std::thread _http_thread; static std::atomic _http_thread_exit = false; -static std::queue> _http_requests; +static ring_buffer_queue> _http_requests; static std::mutex _http_mutex; static std::condition_variable _http_cv; #if defined(UNIX) diff --git a/src/script/api/script_event.cpp b/src/script/api/script_event.cpp index efdc720cf3..4f9cfcaaba 100644 --- a/src/script/api/script_event.cpp +++ b/src/script/api/script_event.cpp @@ -10,13 +10,13 @@ #include "../../stdafx.h" #include "script_event_types.hpp" -#include +#include "../../core/ring_buffer_queue.hpp" #include "../../safeguards.h" /** The queue of events for a script. */ struct ScriptEventData { - std::queue stack; ///< The actual queue. + ring_buffer_queue stack; ///< The actual queue. }; /* static */ void ScriptEventController::CreateEventPointer() diff --git a/src/worker_thread.h b/src/worker_thread.h index 6000af3c78..82ae5b415a 100644 --- a/src/worker_thread.h +++ b/src/worker_thread.h @@ -10,7 +10,7 @@ #ifndef WORKER_THREAD_H #define WORKER_THREAD_H -#include +#include "core/ring_buffer_queue.hpp" #include #include #if defined(__MINGW32__) @@ -33,7 +33,7 @@ private: uint workers_waiting = 0; bool exit = false; std::mutex lock; - std::queue jobs; + ring_buffer_queue jobs; std::condition_variable worker_wait_cv; std::condition_variable done_cv;