2008-02-10 14:32:02 +00:00
|
|
|
/* +------------------------------------+
|
|
|
|
* | Inspire Internet Relay Chat Daemon |
|
|
|
|
* +------------------------------------+
|
|
|
|
*
|
|
|
|
* InspIRCd: (C) 2002-2008 InspIRCd Development Team
|
|
|
|
* See: http://www.inspircd.org/wiki/index.php/Credits
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
class InspIRCd;
|
2008-02-10 14:50:38 +00:00
|
|
|
class Thread;
|
2008-02-10 14:32:02 +00:00
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** The ThreadEngine class has the responsibility of initialising
|
|
|
|
* Thread derived classes. It does this by creating operating system
|
|
|
|
* level threads which are then associated with the class transparently.
|
|
|
|
* This allows Thread classes to be derived without needing to know how
|
|
|
|
* the OS implements threads. You should ensure that any sections of code
|
|
|
|
* that use threads are threadsafe and do not interact with any other
|
|
|
|
* parts of the code which are NOT known threadsafe!
|
|
|
|
*/
|
2008-02-10 14:32:02 +00:00
|
|
|
class CoreExport ThreadEngine : public Extensible
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Creator instance
|
|
|
|
*/
|
2008-02-10 14:32:02 +00:00
|
|
|
InspIRCd* ServerInstance;
|
2008-02-10 18:37:06 +00:00
|
|
|
/** New Thread being created.
|
|
|
|
*/
|
2008-02-10 14:32:02 +00:00
|
|
|
Thread* NewThread;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Constructor
|
|
|
|
*/
|
2008-02-10 14:32:02 +00:00
|
|
|
ThreadEngine(InspIRCd* Instance);
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Destructor
|
|
|
|
*/
|
2008-02-10 14:32:02 +00:00
|
|
|
virtual ~ThreadEngine();
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Enable or disable system-wide mutex for threading.
|
|
|
|
* This MUST be called when you deal with ANYTHING that
|
|
|
|
* isnt known thread-safe, this INCLUDES STL!
|
|
|
|
* Remember that if you toggle the mutex you MUST UNSET
|
|
|
|
* IT LATER otherwise the program will DEADLOCK!
|
|
|
|
*/
|
2008-02-10 14:32:02 +00:00
|
|
|
virtual bool Mutex(bool enable) = 0;
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Run the newly created thread
|
|
|
|
*/
|
2008-02-10 14:32:02 +00:00
|
|
|
virtual void Run() = 0;
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Create a new thread. This takes an already allocated
|
|
|
|
* Thread* pointer and initializes it to use this threading
|
|
|
|
* engine. On failure, this function may throw a CoreException.
|
|
|
|
*/
|
2008-02-10 14:32:02 +00:00
|
|
|
virtual void Create(Thread* thread_to_init) = 0;
|
2008-02-10 14:50:38 +00:00
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** This is called by the default destructor of the Thread
|
|
|
|
* class to ensure that the thread engine which created the thread
|
|
|
|
* is responsible for destroying it.
|
|
|
|
*/
|
2008-02-10 14:50:38 +00:00
|
|
|
virtual void FreeThread(Thread* thread) = 0;
|
2008-02-21 17:59:26 +00:00
|
|
|
|
|
|
|
virtual const std::string GetName()
|
|
|
|
{
|
|
|
|
return "<pure-virtual>";
|
|
|
|
}
|
2008-02-10 14:32:02 +00:00
|
|
|
};
|
|
|
|
|
2008-09-04 10:06:59 +00:00
|
|
|
class CoreExport Mutex : public Extensible
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
InspIRCd* ServerInstance;
|
|
|
|
public:
|
|
|
|
Mutex(InspIRCd* Instance);
|
|
|
|
virtual void Enable(bool enable) = 0;
|
|
|
|
~Mutex() { }
|
|
|
|
};
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Derive from this class to implement your own threaded sections of
|
|
|
|
* code.
|
|
|
|
*/
|
2008-02-10 14:50:38 +00:00
|
|
|
class CoreExport Thread : public Extensible
|
|
|
|
{
|
2008-02-21 17:27:55 +00:00
|
|
|
private:
|
|
|
|
bool ExitFlag;
|
2008-02-10 14:50:38 +00:00
|
|
|
public:
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Creator thread engine
|
|
|
|
*/
|
2008-02-10 14:50:38 +00:00
|
|
|
ThreadEngine* Creator;
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Set Creator to NULL at this point
|
|
|
|
*/
|
2008-02-21 17:27:55 +00:00
|
|
|
Thread() : ExitFlag(false), Creator(NULL)
|
2008-02-10 14:50:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** If this thread has a Creator set, call it to
|
|
|
|
* free the thread
|
|
|
|
*/
|
2008-02-10 14:50:38 +00:00
|
|
|
virtual ~Thread()
|
|
|
|
{
|
|
|
|
if (Creator)
|
|
|
|
Creator->FreeThread(this);
|
|
|
|
}
|
|
|
|
|
2008-02-10 18:37:06 +00:00
|
|
|
/** Override this method to put your actual
|
|
|
|
* threaded code here
|
|
|
|
*/
|
2008-02-10 14:50:38 +00:00
|
|
|
virtual void Run() = 0;
|
2008-02-21 17:27:55 +00:00
|
|
|
|
|
|
|
void SetExitFlag()
|
|
|
|
{
|
|
|
|
ExitFlag = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearExitFlag()
|
|
|
|
{
|
|
|
|
ExitFlag = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetExitFlag()
|
|
|
|
{
|
|
|
|
return ExitFlag;
|
|
|
|
}
|
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
|
|
|
|