From 71b5c5f2e4450eda5352eccf01a3c7c3488dd5e8 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Mon, 4 Mar 2013 23:05:22 -0300 Subject: [PATCH] Use boost::thread instead of std::thread --- modules/game_npctrade/npctrade.lua | 2 ++ src/framework/core/asyncdispatcher.cpp | 6 ++--- src/framework/core/asyncdispatcher.h | 12 +++++----- src/framework/core/logger.cpp | 8 +++---- src/framework/core/logger.h | 2 +- src/framework/sound/soundmanager.cpp | 6 ++--- src/framework/sound/soundmanager.h | 3 ++- src/framework/stdext/thread.h | 33 ++++++++++++++++++++++---- 8 files changed, 49 insertions(+), 23 deletions(-) diff --git a/modules/game_npctrade/npctrade.lua b/modules/game_npctrade/npctrade.lua index 905dad2a..f87e4e9d 100644 --- a/modules/game_npctrade/npctrade.lua +++ b/modules/game_npctrade/npctrade.lua @@ -252,6 +252,7 @@ function getItemPrice(item, single) if not single then amount = quantityScroll:getValue() end + if getCurrentTradeType() == BUY then if buyWithBackpack:isChecked() then if item.ptr:isStackable() then @@ -391,6 +392,7 @@ function refreshPlayerGoods() end function onOpenNpcTrade(items) + table.dump(items) tradeItems[BUY] = {} tradeItems[SELL] = {} diff --git a/src/framework/core/asyncdispatcher.cpp b/src/framework/core/asyncdispatcher.cpp index f4ea33b4..ca23d3d8 100644 --- a/src/framework/core/asyncdispatcher.cpp +++ b/src/framework/core/asyncdispatcher.cpp @@ -38,7 +38,7 @@ void AsyncDispatcher::terminate() void AsyncDispatcher::spawn_thread() { m_running = true; - m_threads.push_back(std::thread(std::bind(&AsyncDispatcher::exec_loop, this))); + m_threads.push_back(stdext::thread(std::bind(&AsyncDispatcher::exec_loop, this))); } void AsyncDispatcher::stop() @@ -47,13 +47,13 @@ void AsyncDispatcher::stop() m_running = false; m_condition.notify_all(); m_mutex.unlock(); - for(std::thread& thread : m_threads) + for(stdext::thread& thread : m_threads) thread.join(); m_threads.clear(); }; void AsyncDispatcher::exec_loop() { - std::unique_lock lock(m_mutex); + stdext::unique_lock lock(m_mutex); while(true) { while(m_tasks.size() == 0 && m_running) m_condition.wait(lock); diff --git a/src/framework/core/asyncdispatcher.h b/src/framework/core/asyncdispatcher.h index 75290550..b97155ff 100644 --- a/src/framework/core/asyncdispatcher.h +++ b/src/framework/core/asyncdispatcher.h @@ -35,9 +35,9 @@ public: void stop(); template - std::future::type> schedule(const F& task) { - std::lock_guard lock(m_mutex); - auto prom = std::make_shared::type>>(); + stdext::future::type> schedule(const F& task) { + stdext::lock_guard lock(m_mutex); + auto prom = std::make_shared::type>>(); m_tasks.push_back([=]() { prom->set_value(task()); }); m_condition.notify_all(); return prom->get_future(); @@ -48,9 +48,9 @@ protected: private: std::list> m_tasks; - std::mutex m_mutex; - std::list m_threads; - std::condition_variable m_condition; + stdext::mutex m_mutex; + std::list m_threads; + stdext::condition_variable m_condition; stdext::boolean m_running; }; diff --git a/src/framework/core/logger.cpp b/src/framework/core/logger.cpp index f67a6cc4..ab43e72e 100644 --- a/src/framework/core/logger.cpp +++ b/src/framework/core/logger.cpp @@ -35,7 +35,7 @@ Logger g_logger; void Logger::log(Fw::LogLevel level, const std::string& message) { - std::lock_guard lock(m_mutex); + stdext::lock_guard lock(m_mutex); #ifdef NDEBUG if(level == Fw::LogDebug) @@ -97,7 +97,7 @@ void Logger::log(Fw::LogLevel level, const std::string& message) void Logger::logFunc(Fw::LogLevel level, const std::string& message, std::string prettyFunction) { - std::lock_guard lock(m_mutex); + stdext::lock_guard lock(m_mutex); prettyFunction = prettyFunction.substr(0, prettyFunction.find_first_of('(')); if(prettyFunction.find_last_of(' ') != std::string::npos) @@ -118,7 +118,7 @@ void Logger::logFunc(Fw::LogLevel level, const std::string& message, std::string void Logger::fireOldMessages() { - std::lock_guard lock(m_mutex); + stdext::lock_guard lock(m_mutex); if(m_onLog) { auto backup = m_logMessages; @@ -130,7 +130,7 @@ void Logger::fireOldMessages() void Logger::setLogFile(const std::string& file) { - std::lock_guard lock(m_mutex); + stdext::lock_guard lock(m_mutex); m_outFile.open(stdext::utf8_to_latin1(file.c_str()).c_str(), std::ios::out | std::ios::app); if(!m_outFile.is_open() || !m_outFile.good()) { diff --git a/src/framework/core/logger.h b/src/framework/core/logger.h index d56c4060..a3989e37 100644 --- a/src/framework/core/logger.h +++ b/src/framework/core/logger.h @@ -62,7 +62,7 @@ private: std::list m_logMessages; OnLogCallback m_onLog; std::ofstream m_outFile; - std::recursive_mutex m_mutex; + stdext::recursive_mutex m_mutex; }; extern Logger g_logger; diff --git a/src/framework/sound/soundmanager.cpp b/src/framework/sound/soundmanager.cpp index 7120bd0a..07032c23 100644 --- a/src/framework/sound/soundmanager.cpp +++ b/src/framework/sound/soundmanager.cpp @@ -96,9 +96,9 @@ void SoundManager::poll() for(auto it = m_streamFiles.begin(); it != m_streamFiles.end();) { StreamSoundSourcePtr source = it->first; - std::future& future = it->second; + stdext::future& future = it->second; - if(std::is_ready(future)) { + if(stdext::is_ready(future)) { SoundFilePtr sound = future.get(); if(sound) source->setSoundFile(sound); @@ -264,7 +264,7 @@ SoundSourcePtr SoundManager::createSoundSource(const std::string& filename) source = combinedSource; #else StreamSoundSourcePtr streamSource(new StreamSoundSource); - m_streamFiles[streamSource] = m_loadJobs [=]() -> SoundFilePtr { + m_streamFiles[streamSource] = g_asyncDispatcher.schedule([=]() -> SoundFilePtr { try { return SoundFile::loadSoundFile(filename); } catch(std::exception& e) { diff --git a/src/framework/sound/soundmanager.h b/src/framework/sound/soundmanager.h index aa7eab78..9576da35 100644 --- a/src/framework/sound/soundmanager.h +++ b/src/framework/sound/soundmanager.h @@ -25,6 +25,7 @@ #include "declarations.h" #include "soundchannel.h" +#include //@bindsingleton g_sounds class SoundManager @@ -57,7 +58,7 @@ private: ALCdevice *m_device; ALCcontext *m_context; - std::map> m_streamFiles; + std::map> m_streamFiles; std::unordered_map m_buffers; std::vector m_sources; stdext::boolean m_audioEnabled; diff --git a/src/framework/stdext/thread.h b/src/framework/stdext/thread.h index 06737e18..85775a8c 100644 --- a/src/framework/stdext/thread.h +++ b/src/framework/stdext/thread.h @@ -24,14 +24,15 @@ #define THREAD_H // hack to enable std::thread on mingw32 4.6 -#if !defined(_GLIBCXX_HAS_GTHREADS) && defined(__GNUG__) +#if 1 +//#if !defined(_GLIBCXX_HAS_GTHREADS) && defined(__GNUG__) #include #include #include #include #include #include -namespace std { +namespace stdext { using boost::thread; using boost::future; using boost::future_status; @@ -49,21 +50,43 @@ namespace std { using boost::condition_variable_any; template - bool is_ready(std::future& f) + bool is_ready(stdext::future& f) { return f.wait_for(boost::chrono::seconds(0)) == future_status::ready; } } - + #else + #include #include #include #include -namespace std { +namespace stdext { template bool is_ready(std::future& f) { return f.wait_for(chrono::seconds(0)) == future_status::ready; } }; +namespace stdext { + using std::thread; + using std::future; + using std::future_status; + using std::promise; + + using std::mutex; + using std::timed_mutex; + using std::recursive_mutex; + using std::recursive_timed_mutex; + + using std::lock_guard; + using std::unique_lock; + + using std::condition_variable; + using std::condition_variable_any; + + template + bool is_ready(std::future& f) + { return f.wait_for(boost::chrono::seconds(0)) == future_status::ready; } +} #endif