Updated Copyright year to 2013
[getmangos.git] / src / shared / Threading.h
blobdcf27c2157ee60e244c65b602db8ac64c7f4b230
1 /*
2 * Copyright (C) 2009-2013 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef THREADING_H
20 #define THREADING_H
22 #include <ace/Thread.h>
23 #include <ace/TSS_T.h>
24 #include "ace/Atomic_Op.h"
25 #include <assert.h>
27 namespace ACE_Based
29 class Runnable
31 public:
32 virtual ~Runnable() {}
33 virtual void run() = 0;
35 void incReference() { ++m_refs; }
36 void decReference()
38 if (!--m_refs)
39 delete this;
41 private:
42 ACE_Atomic_Op<ACE_Thread_Mutex, long> m_refs;
45 enum Priority
47 Idle,
48 Lowest,
49 Low,
50 Normal,
51 High,
52 Highest,
53 Realtime,
56 #define MAXPRIORITYNUM (Realtime + 1)
58 class ThreadPriority
60 public:
61 ThreadPriority();
62 int getPriority(Priority p) const;
64 private:
65 int m_priority[MAXPRIORITYNUM];
68 class Thread
70 public:
71 Thread();
72 explicit Thread(Runnable* instance);
73 ~Thread();
75 bool start();
76 bool wait();
77 void destroy();
79 void suspend();
80 void resume();
82 void setPriority(Priority type);
84 static void Sleep(unsigned long msecs);
85 static ACE_thread_t currentId();
86 static ACE_hthread_t currentHandle();
87 static Thread* current();
89 private:
90 Thread(const Thread&);
91 Thread& operator=(const Thread&);
93 static ACE_THR_FUNC_RETURN ThreadTask(void* param);
95 ACE_thread_t m_iThreadId;
96 ACE_hthread_t m_hThreadHandle;
97 Runnable* m_task;
99 typedef ACE_TSS<Thread> ThreadStorage;
100 // global object - container for Thread class representation of every thread
101 static ThreadStorage m_ThreadStorage;
102 // use this object to determine current OS thread priority values mapped to enum Priority{}
103 static ThreadPriority m_TpEnum;
106 #endif