new threading mutex patch from tmishima@jcity.maeda.co.jp

git-svn-id: svn://136.177.114.72/svn_GW/IPhreeqc/trunk@7705 1feff8c3-07ed-0310-ac33-dd36852eb9cd
This commit is contained in:
Scott R Charlton 2013-05-10 22:15:00 +00:00
parent 9969a86f94
commit 21898660f9
3 changed files with 10 additions and 7 deletions

View File

@ -3,6 +3,8 @@
#include "IPhreeqc.hpp" // IPhreeqc #include "IPhreeqc.hpp" // IPhreeqc
#include "Phreeqc.h" // Phreeqc #include "Phreeqc.h" // Phreeqc
#define _INC_PHREEQC_H
#include "thread.h"
#include "Debug.h" // ASSERT #include "Debug.h" // ASSERT
#include "ErrorReporter.hxx" // CErrorReporter #include "ErrorReporter.hxx" // CErrorReporter
@ -41,7 +43,7 @@ IPhreeqc::IPhreeqc(void)
, PhreeqcPtr(0) , PhreeqcPtr(0)
, input_file(0) , input_file(0)
, database_file(0) , database_file(0)
, Index(IPhreeqc::InstancesIndex++) /* , Index(IPhreeqc::InstancesIndex++) */
{ {
char buffer[80]; char buffer[80];
@ -53,8 +55,11 @@ IPhreeqc::IPhreeqc(void)
ASSERT(this->PhreeqcPtr->phast == 0); ASSERT(this->PhreeqcPtr->phast == 0);
this->UnLoadDatabase(); this->UnLoadDatabase();
mutex_lock(&map_lock);
this->Index = IPhreeqc::InstancesIndex++;
std::map<size_t, IPhreeqc*>::value_type instance(this->Index, this); std::map<size_t, IPhreeqc*>::value_type instance(this->Index, this);
std::pair<std::map<size_t, IPhreeqc*>::iterator, bool> pr = IPhreeqc::Instances.insert(instance); std::pair<std::map<size_t, IPhreeqc*>::iterator, bool> pr = IPhreeqc::Instances.insert(instance);
mutex_unlock(&map_lock);
::sprintf(buffer, PUNCH_FILENAME_FORMAT, this->Index); ::sprintf(buffer, PUNCH_FILENAME_FORMAT, this->Index);
this->SelectedOutputFileName = buffer; this->SelectedOutputFileName = buffer;
@ -83,11 +88,13 @@ IPhreeqc::~IPhreeqc(void)
delete this->WarningReporter; delete this->WarningReporter;
delete this->ErrorReporter; delete this->ErrorReporter;
mutex_lock(&map_lock);
std::map<size_t, IPhreeqc*>::iterator it = IPhreeqc::Instances.find(this->Index); std::map<size_t, IPhreeqc*>::iterator it = IPhreeqc::Instances.find(this->Index);
if (it != IPhreeqc::Instances.end()) if (it != IPhreeqc::Instances.end())
{ {
IPhreeqc::Instances.erase(it); IPhreeqc::Instances.erase(it);
} }
mutex_unlock(&map_lock);
} }
VRESULT IPhreeqc::AccumulateLine(const char *line) VRESULT IPhreeqc::AccumulateLine(const char *line)

View File

@ -885,7 +885,7 @@ protected:
friend class IPhreeqcLib; friend class IPhreeqcLib;
static std::map<size_t, IPhreeqc*> Instances; static std::map<size_t, IPhreeqc*> Instances;
static size_t InstancesIndex; static size_t InstancesIndex;
const size_t Index; size_t Index;
#if defined(_MSC_VER) #if defined(_MSC_VER)
/* reset warning C4251 */ /* reset warning C4251 */

View File

@ -904,9 +904,7 @@ IPhreeqcLib::CreateIPhreeqc(void)
IPhreeqc* IPhreeqcPtr; IPhreeqc* IPhreeqcPtr;
try try
{ {
mutex_lock(&map_lock);
IPhreeqcPtr = new IPhreeqc; IPhreeqcPtr = new IPhreeqc;
mutex_unlock(&map_lock);
n = (int) IPhreeqcPtr->Index; n = (int) IPhreeqcPtr->Index;
} }
catch(...) catch(...)
@ -924,9 +922,7 @@ IPhreeqcLib::DestroyIPhreeqc(int id)
{ {
if (IPhreeqc *ptr = IPhreeqcLib::GetInstance(id)) if (IPhreeqc *ptr = IPhreeqcLib::GetInstance(id))
{ {
mutex_lock(&map_lock);
delete ptr; delete ptr;
mutex_unlock(&map_lock);
retval = IPQ_OK; retval = IPQ_OK;
} }
} }
@ -961,4 +957,4 @@ IPhreeqcLib::GetInstance(int id)
// { // {
// delete ip_list[i]; // delete ip_list[i];
// } // }
//} //}