anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

threadengine.h (2032B)

      1 /*
      2  *
      3  * (C) 2003-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  *
      8  * Based on the original code of Epona by Lara.
      9  * Based on the original code of Services by Andy Church.
     10  */
     11 
     12 #ifndef THREADENGINE_H
     13 #define THREADENGINE_H
     14 
     15 #include "sockets.h"
     16 #include "extensible.h"
     17 
     18 class CoreExport Thread : public Pipe, public Extensible
     19 {
     20  private:
     21 	/* Set to true to tell the thread to finish and we are waiting for it */
     22 	bool exit;
     23 
     24  public:
     25 	/* Handle for this thread */
     26 	pthread_t handle;
     27 
     28 	/** Threads constructor
     29 	 */
     30 	Thread();
     31 
     32 	/** Threads destructor
     33 	 */
     34 	virtual ~Thread();
     35 
     36 	/** Join to the thread, sets the exit state to true
     37 	 */
     38 	void Join();
     39 
     40 	/** Sets the exit state as true informing the thread we want it to shut down
     41 	 */
     42 	void SetExitState();
     43 
     44 	/** Exit the thread. Note that the thread still must be joined to free resources!
     45 	 */
     46 	void Exit();
     47 
     48 	/** Launch the thread
     49 	 */
     50 	void Start();
     51 
     52 	/** Returns the exit state of the thread
     53 	 * @return true if we want to exit
     54 	 */
     55 	bool GetExitState() const;
     56 
     57 	/** Called when this thread should be joined to
     58 	 */
     59 	void OnNotify();
     60 
     61 	/** Called when the thread is run.
     62 	 */
     63 	virtual void Run() = 0;
     64 };
     65 
     66 class CoreExport Mutex
     67 {
     68  protected:
     69 	/* A mutex, used to keep threads in sync */
     70 	pthread_mutex_t mutex;
     71 
     72  public:
     73 	/** Constructor
     74 	 */
     75 	Mutex();
     76 
     77 	/** Destructor
     78 	 */
     79 	~Mutex();
     80 
     81 	/** Attempt to lock the mutex, will hang until a lock can be achieved
     82 	 */
     83 	void Lock();
     84 
     85 	/** Unlock the mutex, it must be locked first
     86 	 */
     87 	void Unlock();
     88 
     89 	/** Attempt to lock the mutex, will return true on success and false on fail
     90 	 * Does not block
     91 	 * @return true or false
     92 	 */
     93 	bool TryLock();
     94 };
     95 
     96 class CoreExport Condition : public Mutex
     97 {
     98  private:
     99 	/* A condition */
    100 	pthread_cond_t cond;
    101 
    102  public:
    103 	/** Constructor
    104 	 */
    105 	Condition();
    106 
    107 	/** Destructor
    108 	 */
    109 	~Condition();
    110 
    111 	/** Called to wakeup the waiter
    112 	 */
    113 	void Wakeup();
    114 
    115 	/** Called to wait for a Wakeup() call
    116 	 */
    117 	void Wait();
    118 };
    119 
    120 #endif // THREADENGINE_H