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/threadsocket.h | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 include/threadsocket.h (limited to 'include/threadsocket.h') diff --git a/include/threadsocket.h b/include/threadsocket.h new file mode 100644 index 000000000..5d0d035ec --- /dev/null +++ b/include/threadsocket.h @@ -0,0 +1,83 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2009 Daniel De Graaf + * Copyright (C) 2008 Craig Edwards + * + * 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 +#include + +class ThreadSignalSocket; + +class CoreExport SocketThread : public Thread +{ + private: + std::mutex mutex; + std::condition_variable_any condvar; + ThreadSignalSocket* socket; + + protected: + /** Waits for an enqueue operation to complete + * You MUST hold the queue lock when you call this. + * It will be unlocked while you wait, and will be relocked + * before the function returns + */ + void WaitForQueue() + { + condvar.wait(mutex); + } + public: + /** Notifies parent by making the SignalFD ready to read + * No requirements on locking + */ + void NotifyParent(); + SocketThread(); + virtual ~SocketThread(); + /** Lock queue. + */ + void LockQueue() + { + mutex.lock(); + } + /** Unlock queue. + */ + void UnlockQueue() + { + mutex.unlock(); + } + /** Unlock queue and send wakeup to worker + */ + void UnlockQueueWakeup() + { + condvar.notify_all(); + mutex.unlock(); + } + void OnStop() override + { + mutex.lock(); + condvar.notify_all(); + mutex.unlock(); + } + + /** + * Called in the context of the parent thread after a notification + * has passed through the socket + */ + virtual void OnNotify() = 0; +}; -- cgit v1.3.1-10-gc9f91