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"
|
|
|
|
|
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
|
|
|
*/
|
2008-02-10 14:50:38 +00:00
|
|
|
class CoreExport Thread : public Extensible
|
|
|
|
{
|
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;
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2009-03-23 18:48:32 +00:00
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
state->FreeThread(this);
|
|
|
|
delete state;
|
|
|
|
}
|
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-03-23 18:48:32 +00:00
|
|
|
void SetExitFlag(bool value)
|
2008-02-21 17:27:55 +00:00
|
|
|
{
|
2009-03-23 18:48:32 +00:00
|
|
|
ExitFlag = value;
|
2008-02-21 17:27:55 +00:00
|
|
|
}
|
|
|
|
|
2008-09-06 12:29:19 +00:00
|
|
|
/** Get thread's current exit status.
|
|
|
|
* (are we being asked to exit?)
|
|
|
|
*/
|
2008-02-21 17:27:55 +00:00
|
|
|
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
|
|
|
|