Project restructuring [PART.2]

This commit is contained in:
Yehonal
2016-08-23 12:11:46 +02:00
parent 54bd100573
commit 0355064321
141 changed files with 250 additions and 243 deletions

View File

@@ -0,0 +1,300 @@
/*
* Copyright (C)
*
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#ifndef _CALLBACK_H
#define _CALLBACK_H
#include <ace/Future.h>
#include <ace/Future_Set.h>
#include "QueryResult.h"
typedef ACE_Future<QueryResult> QueryResultFuture;
typedef ACE_Future<PreparedQueryResult> PreparedQueryResultFuture;
/*! A simple template using ACE_Future to manage callbacks from the thread and object that
issued the request. <ParamType> is variable type of parameter that is used as parameter
for the callback function.
*/
#define CALLBACK_STAGE_INVALID uint8(-1)
template <typename Result, typename ParamType, bool chain = false>
class QueryCallback
{
public:
QueryCallback() : _param(), _stage(chain ? 0 : CALLBACK_STAGE_INVALID) {}
//! The parameter of this function should be a resultset returned from either .AsyncQuery or .AsyncPQuery
void SetFutureResult(ACE_Future<Result> value)
{
_result = value;
}
ACE_Future<Result> GetFutureResult()
{
return _result;
}
int IsReady()
{
return _result.ready();
}
void GetResult(Result& res)
{
_result.get(res);
}
void FreeResult()
{
_result.cancel();
}
void SetParam(ParamType value)
{
_param = value;
}
ParamType GetParam()
{
return _param;
}
//! Resets the stage of the callback chain
void ResetStage()
{
if (!chain)
return;
_stage = 0;
}
//! Advances the callback chain to the next stage, so upper level code can act on its results accordingly
void NextStage()
{
if (!chain)
return;
++_stage;
}
//! Returns the callback stage (or CALLBACK_STAGE_INVALID if invalid)
uint8 GetStage()
{
return _stage;
}
//! Resets all underlying variables (param, result and stage)
void Reset()
{
SetParam(NULL);
FreeResult();
ResetStage();
}
private:
ACE_Future<Result> _result;
ParamType _param;
uint8 _stage;
};
template <typename Result, typename ParamType1, typename ParamType2, bool chain = false>
class QueryCallback_2
{
public:
QueryCallback_2() : _stage(chain ? 0 : CALLBACK_STAGE_INVALID) {}
//! The parameter of this function should be a resultset returned from either .AsyncQuery or .AsyncPQuery
void SetFutureResult(ACE_Future<Result> value)
{
_result = value;
}
ACE_Future<Result> GetFutureResult()
{
return _result;
}
int IsReady()
{
return _result.ready();
}
void GetResult(Result& res)
{
_result.get(res);
}
void FreeResult()
{
_result.cancel();
}
void SetFirstParam(ParamType1 value)
{
_param_1 = value;
}
void SetSecondParam(ParamType2 value)
{
_param_2 = value;
}
ParamType1 GetFirstParam()
{
return _param_1;
}
ParamType2 GetSecondParam()
{
return _param_2;
}
//! Resets the stage of the callback chain
void ResetStage()
{
if (!chain)
return;
_stage = 0;
}
//! Advances the callback chain to the next stage, so upper level code can act on its results accordingly
void NextStage()
{
if (!chain)
return;
++_stage;
}
//! Returns the callback stage (or CALLBACK_STAGE_INVALID if invalid)
uint8 GetStage()
{
return _stage;
}
//! Resets all underlying variables (param, result and stage)
void Reset()
{
SetFirstParam(0);
SetSecondParam(NULL);
FreeResult();
ResetStage();
}
private:
ACE_Future<Result> _result;
ParamType1 _param_1;
ParamType2 _param_2;
uint8 _stage;
};
template <typename Result, typename ParamType1, typename ParamType2, typename ParamType3, bool chain = false>
class QueryCallback_3
{
public:
QueryCallback_3() : _stage(chain ? 0 : CALLBACK_STAGE_INVALID) {}
//! The parameter of this function should be a resultset returned from either .AsyncQuery or .AsyncPQuery
void SetFutureResult(ACE_Future<Result> value)
{
_result = value;
}
ACE_Future<Result> GetFutureResult()
{
return _result;
}
int IsReady()
{
return _result.ready();
}
void GetResult(Result& res)
{
_result.get(res);
}
void FreeResult()
{
_result.cancel();
}
void SetFirstParam(ParamType1 value)
{
_param_1 = value;
}
void SetSecondParam(ParamType2 value)
{
_param_2 = value;
}
void SetThirdParam(ParamType3 value)
{
_param_3 = value;
}
ParamType1 GetFirstParam()
{
return _param_1;
}
ParamType2 GetSecondParam()
{
return _param_2;
}
ParamType3 GetThirdParam()
{
return _param_3;
}
//! Resets the stage of the callback chain
void ResetStage()
{
if (!chain)
return;
_stage = 0;
}
//! Advances the callback chain to the next stage, so upper level code can act on its results accordingly
void NextStage()
{
if (!chain)
return;
++_stage;
}
//! Returns the callback stage (or CALLBACK_STAGE_INVALID if invalid)
uint8 GetStage()
{
return _stage;
}
//! Resets all underlying variables (param, result and stage)
void Reset()
{
SetFirstParam(NULL);
SetSecondParam(NULL);
SetThirdParam(NULL);
FreeResult();
ResetStage();
}
private:
ACE_Future<Result> _result;
ParamType1 _param_1;
ParamType2 _param_2;
ParamType3 _param_3;
uint8 _stage;
};
#endif

View File

@@ -0,0 +1,122 @@
#include <ace/Singleton.h>
#include <ace/Thread_Mutex.h>
#include <ace/Log_Msg.h>
#include "Threading.h"
#include "DelayExecutor.h"
DelayExecutor* DelayExecutor::instance()
{
return ACE_Singleton<DelayExecutor, ACE_Thread_Mutex>::instance();
}
DelayExecutor::DelayExecutor()
: pre_svc_hook_(0), post_svc_hook_(0), activated_(false), mqueue_(1*1024*1024, 1*1024*1024), queue_(&mqueue_)
{
}
DelayExecutor::~DelayExecutor()
{
if (pre_svc_hook_)
delete pre_svc_hook_;
if (post_svc_hook_)
delete post_svc_hook_;
deactivate();
}
int DelayExecutor::deactivate()
{
if (!activated())
return -1;
activated(false);
queue_.queue()->deactivate();
wait();
return 0;
}
int DelayExecutor::svc()
{
if (pre_svc_hook_)
pre_svc_hook_->call();
for (;;)
{
ACE_Method_Request* rq = queue_.dequeue();
if (!rq)
break;
rq->call();
delete rq;
}
if (post_svc_hook_)
post_svc_hook_->call();
return 0;
}
int DelayExecutor::start(int num_threads, ACE_Method_Request* pre_svc_hook, ACE_Method_Request* post_svc_hook)
{
if (activated())
return -1;
if (num_threads < 1)
return -1;
if (pre_svc_hook_)
delete pre_svc_hook_;
if (post_svc_hook_)
delete post_svc_hook_;
pre_svc_hook_ = pre_svc_hook;
post_svc_hook_ = post_svc_hook;
queue_.queue()->activate();
// pussywizard:
//ACORE::ThreadPriority tp;
//int _priority = tp.getPriority(ACORE::Priority_Highest);
//if (ACE_Task_Base::activate(THR_NEW_LWP | THR_JOINABLE, num_threads, 0, _priority) == -1)
// return -1;
if (ACE_Task_Base::activate(THR_NEW_LWP | THR_JOINABLE | THR_INHERIT_SCHED, num_threads) == -1)
return -1;
activated(true);
return true;
}
int DelayExecutor::execute(ACE_Method_Request* new_req)
{
if (new_req == NULL)
return -1;
// pussywizard: NULL as param for enqueue - wait until the action is possible!
// new tasks are added to the queue during map update (schedule_update in MapInstanced::Update)
// the queue can be momentarily blocked by map threads constantly waiting for tasks (for (;;) { queue_.dequeue();... } in DelayExecutor::svc())
// so just wait a moment, don't drop the task xDddd
if (queue_.enqueue(new_req, /*(ACE_Time_Value*)&ACE_Time_Value::zero*/ NULL) == -1)
{
delete new_req;
ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("(%t) %p\n"), ACE_TEXT("DelayExecutor::execute enqueue")), -1);
}
return 0;
}
bool DelayExecutor::activated()
{
return activated_;
}
void DelayExecutor::activated(bool s)
{
activated_ = s;
}

View File

@@ -0,0 +1,38 @@
#ifndef _M_DELAY_EXECUTOR_H
#define _M_DELAY_EXECUTOR_H
#include <ace/Task.h>
#include <ace/Activation_Queue.h>
#include <ace/Method_Request.h>
class DelayExecutor : protected ACE_Task_Base
{
public:
DelayExecutor();
virtual ~DelayExecutor();
static DelayExecutor* instance();
int execute(ACE_Method_Request* new_req);
int start(int num_threads = 1, ACE_Method_Request* pre_svc_hook = NULL, ACE_Method_Request* post_svc_hook = NULL);
int deactivate();
bool activated();
virtual int svc();
private:
ACE_Message_Queue<ACE_SYNCH> mqueue_;
ACE_Activation_Queue queue_;
ACE_Method_Request* pre_svc_hook_;
ACE_Method_Request* post_svc_hook_;
bool activated_;
void activated(bool s);
};
#endif // _M_DELAY_EXECUTOR_H

View File

@@ -0,0 +1,146 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: http://github.com/azerothcore/azerothcore-wotlk/LICENSE-GPL2
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#ifndef LOCKEDQUEUE_H
#define LOCKEDQUEUE_H
#include <ace/Guard_T.h>
#include <ace/Thread_Mutex.h>
#include <deque>
#include <assert.h>
#include "Debugging/Errors.h"
namespace ACE_Based
{
template <class T, class LockType, typename StorageType=std::deque<T> >
class LockedQueue
{
//! Lock access to the queue.
LockType _lock;
//! Storage backing the queue.
StorageType _queue;
//! Cancellation flag.
volatile bool _canceled;
public:
//! Create a LockedQueue.
LockedQueue()
: _canceled(false)
{
}
//! Destroy a LockedQueue.
virtual ~LockedQueue()
{
}
//! Adds an item to the queue.
void add(const T& item)
{
lock();
//ASSERT(!this->_canceled);
// throw Cancellation_Exception();
_queue.push_back(item);
unlock();
}
//! Gets the next result in the queue, if any.
bool next(T& result)
{
// ACE_Guard<LockType> g(this->_lock);
ACE_GUARD_RETURN (LockType, g, this->_lock, false);
if (_queue.empty())
return false;
//ASSERT (!_queue.empty() || !this->_canceled);
// throw Cancellation_Exception();
result = _queue.front();
_queue.pop_front();
return true;
}
template<class Checker>
bool next(T& result, Checker& check)
{
ACE_Guard<LockType> g(this->_lock);
if (_queue.empty())
return false;
result = _queue.front();
if (!check.Process(result))
return false;
_queue.pop_front();
return true;
}
//! Peeks at the top of the queue. Check if the queue is empty before calling! Remember to unlock after use if autoUnlock == false.
T& peek(bool autoUnlock = false)
{
lock();
T& result = _queue.front();
if (autoUnlock)
unlock();
return result;
}
//! Cancels the queue.
void cancel()
{
lock();
_canceled = true;
unlock();
}
//! Checks if the queue is cancelled.
bool cancelled()
{
ACE_Guard<LockType> g(this->_lock);
return _canceled;
}
//! Locks the queue for access.
void lock()
{
this->_lock.acquire();
}
//! Unlocks the queue.
void unlock()
{
this->_lock.release();
}
///! Calls pop_front of the queue
void pop_front()
{
ACE_GUARD (LockType, g, this->_lock);
_queue.pop_front();
}
///! Checks if we're empty or not with locks held
bool empty()
{
ACE_GUARD_RETURN (LockType, g, this->_lock, false);
return _queue.empty();
}
};
}
#endif

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: http://github.com/azerothcore/azerothcore-wotlk/LICENSE-GPL2
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#include "Threading.h"
#include "Errors.h"
#include <chrono>
#include <system_error>
using namespace ACORE;
Thread::Thread() : m_task(nullptr), m_iThreadId(), m_ThreadImp()
{
}
Thread::Thread(Runnable* instance) : m_task(instance), m_ThreadImp(&Thread::ThreadTask, (void*)m_task)
{
m_iThreadId = m_ThreadImp.get_id();
// register reference to m_task to prevent it deeltion until destructor
if (m_task)
m_task->incReference();
}
Thread::~Thread()
{
// Wait();
// deleted runnable object (if no other references)
if (m_task)
m_task->decReference();
}
bool Thread::wait()
{
if (m_iThreadId == std::thread::id() || !m_task)
return false;
bool res = true;
try
{
m_ThreadImp.join();
}
catch (std::system_error&)
{
res = false;
}
m_iThreadId = std::thread::id();
return res;
}
void Thread::destroy()
{
if (m_iThreadId == std::thread::id() || !m_task)
return;
// FIXME: We need to make sure that all threads can be trusted to
// halt execution on their own as this is not an interrupt
m_ThreadImp.join();
m_iThreadId = std::thread::id();
}
void Thread::ThreadTask(void* param)
{
Runnable* _task = (Runnable*)param;
_task->run();
}
std::thread::id Thread::currentId()
{
return std::this_thread::get_id();
}
void Thread::setPriority(Priority priority)
{
std::thread::native_handle_type handle = m_ThreadImp.native_handle();
bool _ok = true;
#ifdef WIN32
switch (priority)
{
case Priority_Realtime: _ok = SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL); break;
case Priority_Highest: _ok = SetThreadPriority(handle, THREAD_PRIORITY_HIGHEST); break;
case Priority_High: _ok = SetThreadPriority(handle, THREAD_PRIORITY_ABOVE_NORMAL); break;
case Priority_Normal: _ok = SetThreadPriority(handle, THREAD_PRIORITY_NORMAL); break;
case Priority_Low: _ok = SetThreadPriority(handle, THREAD_PRIORITY_BELOW_NORMAL); break;
case Priority_Lowest: _ok = SetThreadPriority(handle, THREAD_PRIORITY_LOWEST); break;
case Priority_Idle: _ok = SetThreadPriority(handle, THREAD_PRIORITY_IDLE); break;
}
#endif
// remove this ASSERT in case you don't want to know is thread priority change was successful or not
ASSERT(_ok);
}
void Thread::Sleep(unsigned long msecs)
{
std::this_thread::sleep_for(std::chrono::milliseconds(msecs));
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: http://github.com/azerothcore/azerothcore-wotlk/LICENSE-GPL2
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#ifndef THREADING_H
#define THREADING_H
#include <thread>
#include <atomic>
#include <thread>
#include <atomic>
namespace ACORE
{
class Runnable
{
public:
virtual ~Runnable() {}
virtual void run() = 0;
void incReference() { ++m_refs; }
void decReference()
{
if (!--m_refs)
delete this;
}
private:
std::atomic_long m_refs;
};
enum Priority
{
Priority_Idle,
Priority_Lowest,
Priority_Low,
Priority_Normal,
Priority_High,
Priority_Highest,
Priority_Realtime,
};
class Thread
{
public:
Thread();
explicit Thread(Runnable* instance);
~Thread();
bool wait();
void destroy();
void setPriority(Priority type);
static void Sleep(unsigned long msecs);
static std::thread::id currentId();
private:
Thread(const Thread&);
Thread& operator=(const Thread&);
static void ThreadTask(void* param);
Runnable* const m_task;
std::thread::id m_iThreadId;
std::thread m_ThreadImp;
};
}
#endif