Files
TCPlayerbotCore/src/server/shared/Threading/Threading.h
T

109 lines
2.7 KiB
C++
Raw Normal View History

2010-10-07 15:54:07 +02:00
/*
* Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2009-05-14 13:25:35 +02:00
*/
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
#ifndef THREADING_H
#define THREADING_H
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
#include <ace/Thread.h>
#include <ace/TSS_T.h>
#include <ace/Atomic_Op.h>
2009-05-14 13:25:35 +02:00
#include <assert.h>
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
namespace ACE_Based
{
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
class Runnable
{
public:
virtual ~Runnable() {}
virtual void run() = 0;
2009-10-17 15:51:44 -07:00
void incReference() { ++m_refs; }
void decReference()
{
if(!--m_refs)
delete this;
}
private:
2010-02-10 18:36:08 +03:00
ACE_Atomic_Op<ACE_Thread_Mutex, long> m_refs;
2009-05-14 13:25:35 +02:00
};
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
enum Priority
{
Idle,
Lowest,
Low,
Normal,
High,
Highest,
Realtime,
};
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
#define MAXPRIORITYNUM (Realtime + 1)
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
class ThreadPriority
{
public:
ThreadPriority();
int getPriority(Priority p) const;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
private:
int m_priority[MAXPRIORITYNUM];
};
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
class Thread
{
public:
Thread();
explicit Thread(Runnable* instance);
2009-05-14 13:25:35 +02:00
~Thread();
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
bool start();
bool wait();
void destroy();
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
void suspend();
void resume();
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
void setPriority(Priority type);
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
static void Sleep(unsigned long msecs);
static ACE_thread_t currentId();
static ACE_hthread_t currentHandle();
static Thread * current();
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
private:
Thread(const Thread&);
Thread& operator=(const Thread&);
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
static ACE_THR_FUNC_RETURN ThreadTask(void * param);
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
ACE_thread_t m_iThreadId;
ACE_hthread_t m_hThreadHandle;
Runnable * m_task;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
typedef ACE_TSS<Thread> ThreadStorage;
//global object - container for Thread class representation of every thread
static ThreadStorage m_ThreadStorage;
//use this object to determine current OS thread priority values mapped to enum Priority{}
static ThreadPriority m_TpEnum;
};
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
}
#endif