Thread safety for void* sharing part of DFHack::Core

develop
Warmist 2011-08-04 22:33:54 +03:00
parent 9985b5de0f
commit 4b9786f8af
2 changed files with 15 additions and 1 deletions

@ -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<std::string,void*>::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()

@ -180,6 +180,7 @@ namespace DFHack
// Very important!
bool started;
tthread::mutex * misc_data_mutex;
std::map<std::string,void*> misc_data_map;
};
}