aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Maps
diff options
context:
space:
mode:
authorleak <leak@bitmx.net>2014-07-02 00:41:30 +0200
committerleak <leak@bitmx.net>2014-07-02 00:41:30 +0200
commit25e633aa34ef227841b4092270d862b0864dc372 (patch)
treeddbc654fed3dc969dac57c32338e7a501354823e /src/server/game/Maps
parentf8e829da65c41cb753cec6cd9c59ae8a65434bce (diff)
Replaced ACE_Method_Request based DelayExecutor by PCQ impl
Untested due to worldserver still breaking because of ACE threading fails
Diffstat (limited to 'src/server/game/Maps')
-rw-r--r--src/server/game/Maps/MapManager.cpp4
-rw-r--r--src/server/game/Maps/MapUpdater.cpp71
-rw-r--r--src/server/game/Maps/MapUpdater.h21
3 files changed, 54 insertions, 42 deletions
diff --git a/src/server/game/Maps/MapManager.cpp b/src/server/game/Maps/MapManager.cpp
index 2b8dbde8280..1de13d137c4 100644
--- a/src/server/game/Maps/MapManager.cpp
+++ b/src/server/game/Maps/MapManager.cpp
@@ -52,8 +52,8 @@ void MapManager::Initialize()
int num_threads(sWorld->getIntConfig(CONFIG_NUMTHREADS));
// Start mtmaps if needed.
- if (num_threads > 0 && m_updater.activate(num_threads) == -1)
- abort();
+ if (num_threads > 0)
+ m_updater.activate(num_threads);
}
void MapManager::InitializeVisibilityDistanceInfo()
diff --git a/src/server/game/Maps/MapUpdater.cpp b/src/server/game/Maps/MapUpdater.cpp
index 2a6b810fcef..4ab95d87d48 100644
--- a/src/server/game/Maps/MapUpdater.cpp
+++ b/src/server/game/Maps/MapUpdater.cpp
@@ -18,57 +18,61 @@
#include <mutex>
#include <condition_variable>
-#include <ace/Method_Request.h>
#include "MapUpdater.h"
-#include "DelayExecutor.h"
#include "Map.h"
-#include "DatabaseEnv.h"
-class MapUpdateRequest : public ACE_Method_Request
+class MapUpdateRequest
{
private:
Map& m_map;
MapUpdater& m_updater;
- ACE_UINT32 m_diff;
+ uint32 m_diff;
public:
- MapUpdateRequest(Map& m, MapUpdater& u, ACE_UINT32 d)
+ MapUpdateRequest(Map& m, MapUpdater& u, uint32 d)
: m_map(m), m_updater(u), m_diff(d)
{
}
- virtual int call()
+ void call()
{
m_map.Update (m_diff);
m_updater.update_finished();
- return 0;
}
};
-MapUpdater::MapUpdater(): m_executor(), pending_requests(0) { }
+MapUpdater::MapUpdater(): pending_requests(0) { }
MapUpdater::~MapUpdater()
{
deactivate();
}
-int MapUpdater::activate(size_t num_threads)
+void MapUpdater::activate(size_t num_threads)
{
- return m_executor.start((int)num_threads);
+ for (size_t i = 0; i < num_threads; ++i)
+ {
+ _workerThreads.push_back(std::thread(&MapUpdater::WorkerThread, this));
+ }
}
-int MapUpdater::deactivate()
+void MapUpdater::deactivate()
{
wait();
- return m_executor.deactivate();
+ _queue.Cancel();
+
+ for (auto& thread : _workerThreads)
+ {
+ thread.join();
+ }
}
-int MapUpdater::wait()
+void MapUpdater::wait()
{
std::unique_lock<std::mutex> lock(_lock);
@@ -76,43 +80,44 @@ int MapUpdater::wait()
_condition.wait(lock);
lock.unlock();
-
- return 0;
}
-int MapUpdater::schedule_update(Map& map, ACE_UINT32 diff)
+void MapUpdater::schedule_update(Map& map, uint32 diff)
{
std::lock_guard<std::mutex> lock(_lock);
++pending_requests;
- if (m_executor.execute(new MapUpdateRequest(map, *this, diff)) == -1)
- {
- ACE_DEBUG((LM_ERROR, ACE_TEXT("(%t) \n"), ACE_TEXT("Failed to schedule Map Update")));
-
- --pending_requests;
- return -1;
- }
-
- return 0;
+ _queue.Push(new MapUpdateRequest(map, *this, diff));
}
bool MapUpdater::activated()
{
- return m_executor.activated();
+ return _workerThreads.size() > 0;
}
void MapUpdater::update_finished()
{
std::lock_guard<std::mutex> lock(_lock);
- if (pending_requests == 0)
- {
- ACE_ERROR((LM_ERROR, ACE_TEXT("(%t)\n"), ACE_TEXT("MapUpdater::update_finished BUG, report to devs")));
- return;
- }
-
--pending_requests;
_condition.notify_all();
}
+
+void MapUpdater::WorkerThread()
+{
+ while (1)
+ {
+ MapUpdateRequest* request = nullptr;
+
+ _queue.WaitAndPop(request);
+
+ if (_cancelationToken)
+ return;
+
+ request->call();
+
+ delete request;
+ }
+}
diff --git a/src/server/game/Maps/MapUpdater.h b/src/server/game/Maps/MapUpdater.h
index 8461b53e992..ff1d85a23e9 100644
--- a/src/server/game/Maps/MapUpdater.h
+++ b/src/server/game/Maps/MapUpdater.h
@@ -19,10 +19,11 @@
#ifndef _MAP_UPDATER_H_INCLUDED
#define _MAP_UPDATER_H_INCLUDED
+#include "Define.h"
#include <mutex>
+#include <thread>
#include <condition_variable>
-
-#include "DelayExecutor.h"
+#include "ProducerConsumerQueue.h"
class Map;
@@ -35,24 +36,30 @@ class MapUpdater
friend class MapUpdateRequest;
- int schedule_update(Map& map, ACE_UINT32 diff);
+ void schedule_update(Map& map, uint32 diff);
- int wait();
+ void wait();
- int activate(size_t num_threads);
+ void activate(size_t num_threads);
- int deactivate();
+ void deactivate();
bool activated();
private:
- DelayExecutor m_executor;
+ ProducerConsumerQueue <MapUpdateRequest*> _queue;
+
+ std::vector<std::thread> _workerThreads;
+ std::atomic_bool _cancelationToken;
+
std::mutex _lock;
std::condition_variable _condition;
size_t pending_requests;
void update_finished();
+
+ void WorkerThread();
};
#endif //_MAP_UPDATER_H_INCLUDED