2019-05-15 16:22:24 +01:00
|
|
|
/*
|
|
|
|
* InspIRCd -- Internet Relay Chat Daemon
|
|
|
|
*
|
2024-06-07 10:37:56 +01:00
|
|
|
* Copyright (C) 2019, 2021 Sadie Powell <sadie@witchery.services>
|
2019-05-15 16:22:24 +01:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2021-12-20 20:00:03 +00:00
|
|
|
class CoreExport SocketThread
|
|
|
|
: public Thread
|
2019-05-15 16:22:24 +01:00
|
|
|
{
|
2022-01-25 13:59:42 +00:00
|
|
|
private:
|
2019-05-15 16:22:24 +01:00
|
|
|
std::mutex mutex;
|
|
|
|
std::condition_variable_any condvar;
|
|
|
|
ThreadSignalSocket* socket;
|
|
|
|
|
2022-01-25 13:59:42 +00:00
|
|
|
protected:
|
2019-05-15 16:22:24 +01:00
|
|
|
/** 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);
|
|
|
|
}
|
2022-01-25 13:59:42 +00:00
|
|
|
public:
|
2019-05-15 16:22:24 +01:00
|
|
|
/** Notifies parent by making the SignalFD ready to read
|
|
|
|
* No requirements on locking
|
|
|
|
*/
|
|
|
|
void NotifyParent();
|
|
|
|
SocketThread();
|
2021-04-04 23:42:15 +01:00
|
|
|
~SocketThread() override;
|
2019-05-15 16:22:24 +01:00
|
|
|
/** 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;
|
|
|
|
};
|