From 03d3563ef95efc6ab8076d66ebda48545c6089c4 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Wed, 15 May 2019 16:22:24 +0100 Subject: Replace socketengine_{pthread,win32} with C++11 threads. --- include/thread.h | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 include/thread.h (limited to 'include/thread.h') diff --git a/include/thread.h b/include/thread.h new file mode 100644 index 000000000..4145c9e88 --- /dev/null +++ b/include/thread.h @@ -0,0 +1,67 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2019 Peter Powell + * + * This file is part of InspIRCd. InspIRCd 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. + * + * This program 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 this program. If not, see . + */ + + +#pragma once + +#include + +class CoreExport Thread +{ + private: + /** Whether this thread is in the process of stopping. */ + std::atomic_bool stopping = { false }; + + /** The underlying C++ thread. */ + std::thread* thread = nullptr; + + /** Internal callback which sets up the thread and calls OnStart. + * @param thread A thread which is starting up. + */ + static void StartInternal(Thread* thread); + + protected: + /** Callback which is executed on this thread after it has started. */ + virtual void OnStart() = 0; + + /** Callback which is executed on the calling thread before this thread is stopped. */ + virtual void OnStop() { }; + + /** Initialises an instance of the Thread class. */ + Thread() = default; + + public: + /** Destroys an instance of the Thread class. */ + virtual ~Thread() = default; + + /** Determines whether this thread is currently running. */ + bool IsRunning() const { return thread; } + + /** Determines whether this thread is currently stopping. */ + bool IsStopping() const { return stopping.load(); } + + /** Starts the execution of this thread. + * @return True if the thread was started, false if it was already running. + */ + bool Start(); + + /** Stops the execution of this thread. + * @return True if the thread was stopped, false if it was already stopped. + */ + bool Stop(); +}; -- cgit v1.3.1-10-gc9f91