From 4b9786f8afcc091fe672b11bf8054bc373db3877 Mon Sep 17 00:00:00 2001 From: Warmist Date: Thu, 4 Aug 2011 22:33:54 +0300 Subject: [PATCH] Thread safety for void* sharing part of DFHack::Core --- library/Core.cpp | 15 ++++++++++++++- library/include/dfhack/Core.h | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/library/Core.cpp b/library/Core.cpp index bf9b9fa2f..e3fcc2329 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -383,6 +383,7 @@ Core::Core() hotkey_set = false; HotkeyMutex = 0; HotkeyCond = 0; + misc_data_mutex=0; }; bool Core::Init() @@ -437,6 +438,7 @@ bool Core::Init() HotkeyMutex = new mutex(); HotkeyCond = new condition_variable(); thread * HK = new thread(fHKthread, (void *) temp); + misc_data_mutex=new mutex(); started = true; return true; } @@ -471,16 +473,27 @@ std::string Core::getHotkeyCmd( void ) void Core::RegisterData(void *p,std::string key) { + misc_data_mutex->lock(); misc_data_map[key]=p; + misc_data_mutex->unlock(); } void *Core::GetData(std::string key) { + misc_data_mutex->lock(); std::map::iterator it=misc_data_map.find(key); + if (it!=misc_data_map.end()) - return misc_data_map[key]; + { + void *p=it->second; + misc_data_mutex->unlock(); + return p; + } else + { + misc_data_mutex->unlock(); return 0;// or throw an error. + } } void Core::Suspend() diff --git a/library/include/dfhack/Core.h b/library/include/dfhack/Core.h index dab39f858..2cc9ff149 100644 --- a/library/include/dfhack/Core.h +++ b/library/include/dfhack/Core.h @@ -180,6 +180,7 @@ namespace DFHack // Very important! bool started; + tthread::mutex * misc_data_mutex; std::map misc_data_map; }; }