2008-02-10 14:32:02 +00:00
|
|
|
/* +------------------------------------+
|
|
|
|
* | Inspire Internet Relay Chat Daemon |
|
|
|
|
* +------------------------------------+
|
|
|
|
*
|
2009-01-02 18:16:05 +00:00
|
|
|
* InspIRCd: (C) 2002-2009 InspIRCd Development Team
|
2009-03-15 12:42:35 +00:00
|
|
|
* See: http://wiki.inspircd.org/Credits
|
2008-02-10 14:32:02 +00:00
|
|
|
*
|
|
|
|
* This program is free but copyrighted software; see
|
|
|
|
* the file COPYING for details.
|
|
|
|
*
|
|
|
|
* ---------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __THREADENGINE__
|
|
|
|
#define __THREADENGINE__
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include "inspircd_config.h"
|
|
|
|
#include "base.h"
|
|
|
|
|
2009-03-25 11:37:06 +00:00
|
|
|
#ifdef WINDOWS
|
|
|
|
#include "threadengines/threadengine_win32.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class ThreadData;
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Derive from this class to implement your own threaded sections of
|
2008-09-06 12:29:19 +00:00
|
|
|
* code. Be sure to keep your code thread-safe and not prone to deadlocks
|
|
|
|
* and race conditions if you MUST use threading!
|
2008-02-10 18:37:06 +00:00
|
|
|
*/
|
2009-08-10 19:55:07 +00:00
|
|
|
class CoreExport Thread
|
2008-02-10 14:50:38 +00:00
|
|
|
{
|
2008-02-21 17:27:55 +00:00
|
|
|
private:
|
2008-09-06 12:29:19 +00:00
|
|
|
/** Set to true when the thread is to exit
|
|
|
|
*/
|
2008-02-21 17:27:55 +00:00
|
|
|
bool ExitFlag;
|
2009-03-23 20:42:17 +00:00
|
|
|
protected:
|
2009-03-23 18:48:51 +00:00
|
|
|
/** Get thread's current exit status.
|
|
|
|
* (are we being asked to exit?)
|
|
|
|
*/
|
|
|
|
bool GetExitFlag()
|
|
|
|
{
|
|
|
|
return ExitFlag;
|
|
|
|
}
|
2008-02-10 14:50:38 +00:00
|
|
|
public:
|
2009-03-23 18:48:32 +00:00
|
|
|
/** Opaque thread state managed by threading engine
|
2008-02-10 18:37:06 +00:00
|
|
|
*/
|
2009-03-23 18:48:32 +00:00
|
|
|
ThreadData* state;
|
2008-02-10 14:50:38 +00:00
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Set Creator to NULL at this point
|
|
|
|
*/
|
2009-03-23 18:48:32 +00:00
|
|
|
Thread() : ExitFlag(false), state(NULL)
|
2008-02-10 14:50:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-08-10 19:55:07 +00:00
|
|
|
/* If the thread is running, you MUST join BEFORE deletion */
|
2009-03-25 11:37:06 +00:00
|
|
|
virtual ~Thread();
|
2008-02-10 14:50:38 +00:00
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Override this method to put your actual
|
2008-09-06 12:29:19 +00:00
|
|
|
* threaded code here.
|
2008-02-10 18:37:06 +00:00
|
|
|
*/
|
2008-02-10 14:50:38 +00:00
|
|
|
virtual void Run() = 0;
|
2008-02-21 17:27:55 +00:00
|
|
|
|
2008-09-06 12:29:19 +00:00
|
|
|
/** Signal the thread to exit gracefully.
|
|
|
|
*/
|
2009-08-10 19:55:07 +00:00
|
|
|
virtual void SetExitFlag();
|
|
|
|
|
|
|
|
/** Join the thread (calls SetExitFlag and waits for exit)
|
|
|
|
*/
|
|
|
|
void join();
|
2009-03-23 18:48:51 +00:00
|
|
|
};
|
2008-02-21 17:27:55 +00:00
|
|
|
|
2009-03-23 18:48:51 +00:00
|
|
|
|
|
|
|
class CoreExport QueuedThread : public Thread
|
|
|
|
{
|
|
|
|
ThreadQueueData queue;
|
|
|
|
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
|
2008-09-06 12:29:19 +00:00
|
|
|
*/
|
2009-03-23 18:48:51 +00:00
|
|
|
void WaitForQueue()
|
2008-02-21 17:27:55 +00:00
|
|
|
{
|
2009-03-23 18:48:51 +00:00
|
|
|
queue.Wait();
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
/** Lock queue.
|
|
|
|
*/
|
|
|
|
void LockQueue()
|
|
|
|
{
|
|
|
|
queue.Lock();
|
|
|
|
}
|
|
|
|
/** Unlock queue.
|
|
|
|
*/
|
|
|
|
void UnlockQueue()
|
|
|
|
{
|
|
|
|
queue.Unlock();
|
|
|
|
}
|
|
|
|
/** Unlock queue and wake up worker
|
|
|
|
*/
|
|
|
|
void UnlockQueueWakeup()
|
|
|
|
{
|
|
|
|
queue.Wakeup();
|
|
|
|
queue.Unlock();
|
|
|
|
}
|
|
|
|
virtual void SetExitFlag()
|
|
|
|
{
|
|
|
|
queue.Lock();
|
|
|
|
Thread::SetExitFlag();
|
|
|
|
queue.Wakeup();
|
|
|
|
queue.Unlock();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CoreExport SocketThread : public Thread
|
|
|
|
{
|
|
|
|
ThreadQueueData queue;
|
|
|
|
ThreadSignalData signal;
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
queue.Wait();
|
|
|
|
}
|
|
|
|
/** Notifies parent by making the SignalFD ready to read
|
|
|
|
* No requirements on locking
|
|
|
|
*/
|
|
|
|
void NotifyParent();
|
|
|
|
public:
|
|
|
|
SocketThread(InspIRCd* SI);
|
|
|
|
virtual ~SocketThread();
|
|
|
|
/** Lock queue.
|
|
|
|
*/
|
|
|
|
void LockQueue()
|
|
|
|
{
|
|
|
|
queue.Lock();
|
|
|
|
}
|
|
|
|
/** Unlock queue.
|
|
|
|
*/
|
|
|
|
void UnlockQueue()
|
|
|
|
{
|
|
|
|
queue.Unlock();
|
|
|
|
}
|
|
|
|
/** Unlock queue and send wakeup to worker
|
|
|
|
*/
|
|
|
|
void UnlockQueueWakeup()
|
|
|
|
{
|
|
|
|
queue.Wakeup();
|
|
|
|
queue.Unlock();
|
|
|
|
}
|
|
|
|
virtual void SetExitFlag()
|
|
|
|
{
|
|
|
|
queue.Lock();
|
|
|
|
Thread::SetExitFlag();
|
|
|
|
queue.Wakeup();
|
|
|
|
queue.Unlock();
|
2008-02-21 17:27:55 +00:00
|
|
|
}
|
2009-03-23 18:48:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called in the context of the parent thread after a notification
|
|
|
|
* has passed through the socket
|
|
|
|
*/
|
|
|
|
virtual void OnNotify() = 0;
|
2008-02-10 14:50:38 +00:00
|
|
|
};
|
|
|
|
|
2008-02-10 14:32:02 +00:00
|
|
|
#endif
|
2008-02-10 14:50:38 +00:00
|
|
|
|