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

236 lines
5.6 KiB
C++
Raw Normal View History

/*
2012-12-31 23:15:50 +01:00
* Copyright (C) 2008-2013 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
#include "Threading.h"
#include "Errors.h"
2009-05-14 13:25:35 +02:00
#include <ace/OS_NS_unistd.h>
#include <ace/Sched_Params.h>
#include <vector>
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
using namespace ACE_Based;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
ThreadPriority::ThreadPriority()
{
for (int i = Idle; i < MAXPRIORITYNUM; ++i)
m_priority[i] = ACE_THR_PRI_OTHER_DEF;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
m_priority[Idle] = ACE_Sched_Params::priority_min(ACE_SCHED_OTHER);
m_priority[Realtime] = ACE_Sched_Params::priority_max(ACE_SCHED_OTHER);
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
std::vector<int> _tmp;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
ACE_Sched_Params::Policy _policy = ACE_SCHED_OTHER;
ACE_Sched_Priority_Iterator pr_iter(_policy);
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
while (pr_iter.more())
{
_tmp.push_back(pr_iter.priority());
pr_iter.next();
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
ASSERT (!_tmp.empty());
2009-10-17 15:51:44 -07:00
2011-09-29 12:43:05 +02:00
if (_tmp.size() >= MAXPRIORITYNUM)
2009-05-14 13:25:35 +02:00
{
const size_t max_pos = _tmp.size();
size_t min_pos = 1;
size_t norm_pos = 0;
for (size_t i = 0; i < max_pos; ++i)
{
2011-09-29 12:43:05 +02:00
if (_tmp[i] == ACE_THR_PRI_OTHER_DEF)
2009-05-14 13:25:35 +02:00
{
norm_pos = i + 1;
break;
}
}
2009-10-17 15:51:44 -07:00
// since we have only 7(seven) values in enum Priority
// and 3 we know already (Idle, Normal, Realtime) so
// we need to split each list [Idle...Normal] and [Normal...Realtime]
// into pieces
2009-05-14 13:25:35 +02:00
const size_t _divider = 4;
size_t _div = (norm_pos - min_pos) / _divider;
2011-09-29 12:43:05 +02:00
if (_div == 0)
2009-05-14 13:25:35 +02:00
_div = 1;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
min_pos = (norm_pos - 1);
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
m_priority[Low] = _tmp[min_pos -= _div];
m_priority[Lowest] = _tmp[min_pos -= _div ];
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
_div = (max_pos - norm_pos) / _divider;
2011-09-29 12:43:05 +02:00
if (_div == 0)
2009-05-14 13:25:35 +02:00
_div = 1;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
min_pos = norm_pos - 1;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
m_priority[High] = _tmp[min_pos += _div];
m_priority[Highest] = _tmp[min_pos += _div];
}
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
int ThreadPriority::getPriority(Priority p) const
{
2011-09-29 12:43:05 +02:00
if (p < Idle)
2009-05-14 13:25:35 +02:00
p = Idle;
2009-10-17 15:51:44 -07:00
2011-09-29 12:43:05 +02:00
if (p > Realtime)
2009-05-14 13:25:35 +02:00
p = Realtime;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
return m_priority[p];
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
#define THREADFLAG (THR_NEW_LWP | THR_SCHED_DEFAULT| THR_JOINABLE)
2009-10-17 15:51:44 -07:00
2010-08-21 20:08:47 +02:00
Thread::Thread(): m_iThreadId(0), m_hThreadHandle(0), m_task(0)
2009-05-14 13:25:35 +02:00
{
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
}
2009-10-17 15:51:44 -07:00
2010-08-21 20:08:47 +02:00
Thread::Thread(Runnable* instance): m_iThreadId(0), m_hThreadHandle(0), m_task(instance)
2009-05-14 13:25:35 +02:00
{
// register reference to m_task to prevent it deeltion until destructor
if (m_task)
m_task->incReference();
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
bool _start = start();
ASSERT (_start);
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
Thread::~Thread()
{
//Wait();
2009-10-17 15:51:44 -07:00
// deleted runnable object (if no other references)
if (m_task)
m_task->decReference();
2009-05-14 13:25:35 +02:00
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
//initialize Thread's class static member
Thread::ThreadStorage Thread::m_ThreadStorage;
ThreadPriority Thread::m_TpEnum;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
bool Thread::start()
{
if (m_task == 0 || m_iThreadId != 0)
2009-05-14 13:25:35 +02:00
return false;
2009-10-17 15:51:44 -07:00
// incRef before spawing the thread, otherwise Thread::ThreadTask() might call decRef and delete m_task
m_task->incReference();
bool res = (ACE_Thread::spawn(&Thread::ThreadTask, (void*)m_task, THREADFLAG, &m_iThreadId, &m_hThreadHandle) == 0);
2009-10-17 15:51:44 -07:00
if (!res)
m_task->decReference();
2009-10-17 15:51:44 -07:00
return res;
2009-05-14 13:25:35 +02:00
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
bool Thread::wait()
{
if (!m_hThreadHandle || !m_task)
2009-05-14 13:25:35 +02:00
return false;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
ACE_THR_FUNC_RETURN _value = ACE_THR_FUNC_RETURN(-1);
int _res = ACE_Thread::join(m_hThreadHandle, &_value);
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
m_iThreadId = 0;
m_hThreadHandle = 0;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
return (_res == 0);
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
void Thread::destroy()
{
if (!m_iThreadId || !m_task)
return;
2009-10-17 15:51:44 -07:00
if (ACE_Thread::kill(m_iThreadId, -1) != 0)
return;
2009-10-17 15:51:44 -07:00
m_iThreadId = 0;
m_hThreadHandle = 0;
2009-10-17 15:51:44 -07:00
// reference set at ACE_Thread::spawn
m_task->decReference();
2009-05-14 13:25:35 +02:00
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
void Thread::suspend()
{
ACE_Thread::suspend(m_hThreadHandle);
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
void Thread::resume()
{
ACE_Thread::resume(m_hThreadHandle);
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
ACE_THR_FUNC_RETURN Thread::ThreadTask(void * param)
{
Runnable* _task = (Runnable*)param;
2009-05-14 13:25:35 +02:00
_task->run();
2009-10-17 15:51:44 -07:00
2009-10-17 16:20:24 -07:00
// task execution complete, free referecne added at
_task->decReference();
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
return (ACE_THR_FUNC_RETURN)0;
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
ACE_thread_t Thread::currentId()
{
return ACE_Thread::self();
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
ACE_hthread_t Thread::currentHandle()
{
ACE_hthread_t _handle;
ACE_Thread::self(_handle);
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
return _handle;
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
Thread * Thread::current()
{
Thread * _thread = m_ThreadStorage.ts_object();
2011-09-29 12:43:05 +02:00
if (!_thread)
2009-05-14 13:25:35 +02:00
{
_thread = new Thread();
_thread->m_iThreadId = Thread::currentId();
_thread->m_hThreadHandle = Thread::currentHandle();
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
Thread * _oldValue = m_ThreadStorage.ts_object(_thread);
2011-09-29 12:43:05 +02:00
if (_oldValue)
2009-05-14 13:25:35 +02:00
delete _oldValue;
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
return _thread;
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
void Thread::setPriority(Priority type)
{
int _priority = m_TpEnum.getPriority(type);
int _ok = ACE_Thread::setprio(m_hThreadHandle, _priority);
//remove this ASSERT in case you don't want to know is thread priority change was successful or not
ASSERT (_ok == 0);
}
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
void Thread::Sleep(unsigned long msecs)
{
ACE_OS::sleep(ACE_Time_Value(0, 1000 * msecs));
}