OregonCore  revision 3611e8a-git
Your Favourite TBC server
Threading.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef THREADING_H
19 #define THREADING_H
20 
21 #include <set>
22 #include <ace/Thread.h>
23 #include <ace/TSS_T.h>
24 #include "ace/Atomic_Op.h"
25 #include <assert.h>
26 
27 namespace ACE_Based
28 {
29 
30 class Runnable
31 {
32  public:
33  virtual ~Runnable() {}
34  virtual void run() = 0;
35 
36  void incReference()
37  {
38  ++m_refs;
39  }
40  void decReference()
41  {
42  if (!--m_refs)
43  delete this;
44  }
45  private:
46  ACE_Atomic_Op<ACE_Thread_Mutex, long> m_refs;
47 };
48 
50 {
53  Low,
58 };
59 
60 #define MAXPRIORITYNUM (Realtime + 1)
61 
63 {
64  public:
66  int getPriority(Priority p) const;
67 
68  private:
69  int m_priority[MAXPRIORITYNUM];
70 };
71 
72 class Thread
73 {
74  public:
75  Thread();
76  explicit Thread(Runnable* instance);
77  ~Thread();
78 
79  bool start();
80  bool wait();
81 
82  void suspend();
83  bool kill(int signal);
84  void resume();
85 
86  void setPriority(Priority type);
87 
88  static void Sleep(unsigned long msecs);
89  static ACE_thread_t currentId();
90  static ACE_hthread_t currentHandle();
91  static Thread* current();
92 
93  ACE_thread_t getId() const { return m_iThreadId; }
94 
95  private:
96  Thread(const Thread&);
97  Thread& operator=(const Thread&);
98 
99  static ACE_THR_FUNC_RETURN ThreadTask(void* param);
100 
101  ACE_thread_t m_iThreadId;
102  ACE_hthread_t m_hThreadHandle;
104 
105  typedef ACE_TSS<Thread> ThreadStorage;
106  //global object - container for Thread class representation of every thread
107  static ThreadStorage m_ThreadStorage;
108  //use this object to determine current OS thread priority values mapped to enum Priority{}
110 };
111 
112 }
113 #endif
114 
virtual void run()=0
static ThreadStorage m_ThreadStorage
Definition: Threading.h:107
ACE_hthread_t m_hThreadHandle
Definition: Threading.h:102
ACE_thread_t m_iThreadId
Definition: Threading.h:101
#define MAXPRIORITYNUM
Definition: Threading.h:60
Runnable * m_task
Definition: Threading.h:103
static ThreadPriority m_TpEnum
Definition: Threading.h:109
virtual ~Runnable()
Definition: Threading.h:33
ACE_Atomic_Op< ACE_Thread_Mutex, long > m_refs
Definition: Threading.h:46
ACE_thread_t getId() const
Definition: Threading.h:93
ACE_TSS< Thread > ThreadStorage
Definition: Threading.h:105