diff options
| author | 2019-05-15 16:22:24 +0100 | |
|---|---|---|
| committer | 2019-05-15 21:06:09 +0100 | |
| commit | 03d3563ef95efc6ab8076d66ebda48545c6089c4 (patch) | |
| tree | 0160e0a3b333a000c7c880d9373b46cfa3968e79 /include/thread.h | |
| parent | Merge branch 'insp3' into master. (diff) | |
Replace socketengine_{pthread,win32} with C++11 threads.
Diffstat (limited to 'include/thread.h')
| -rw-r--r-- | include/thread.h | 67 |
1 files changed, 67 insertions, 0 deletions
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 <petpow@saberuk.com> + * + * 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 <http://www.gnu.org/licenses/>. + */ + + +#pragma once + +#include <thread> + +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(); +}; |
