aboutsummaryrefslogtreecommitdiff
path: root/include/threadsocket.h
diff options
context:
space:
mode:
authorGravatar Sadie Powell2019-05-15 16:22:24 +0100
committerGravatar Sadie Powell2019-05-15 21:06:09 +0100
commit03d3563ef95efc6ab8076d66ebda48545c6089c4 (patch)
tree0160e0a3b333a000c7c880d9373b46cfa3968e79 /include/threadsocket.h
parentMerge branch 'insp3' into master. (diff)
Replace socketengine_{pthread,win32} with C++11 threads.
Diffstat (limited to 'include/threadsocket.h')
-rw-r--r--include/threadsocket.h83
1 files changed, 83 insertions, 0 deletions
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 <danieldg@inspircd.org>
+ * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
+ *
+ * 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 <condition_variable>
+#include <mutex>
+
+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;
+};