added patch from tmishima@jcity.maeda.co.jp for multi-threaded use

git-svn-id: svn://136.177.114.72/svn_GW/IPhreeqc/trunk@7648 1feff8c3-07ed-0310-ac33-dd36852eb9cd
This commit is contained in:
Scott R Charlton 2013-04-26 00:49:59 +00:00
parent 25cbfeeb64
commit 46d8c60d6d
3 changed files with 77 additions and 14 deletions

View File

@ -4,6 +4,7 @@
#include "IPhreeqc.h" #include "IPhreeqc.h"
#include "IPhreeqc.hpp" #include "IPhreeqc.hpp"
#include "thread.h"
class IPhreeqcLib class IPhreeqcLib
{ {
@ -903,10 +904,9 @@ IPhreeqcLib::CreateIPhreeqc(void)
IPhreeqc* IPhreeqcPtr; IPhreeqc* IPhreeqcPtr;
try try
{ {
#pragma omp critical(IPhreeqcLib) 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,11 +924,10 @@ IPhreeqcLib::DestroyIPhreeqc(int id)
{ {
if (IPhreeqc *ptr = IPhreeqcLib::GetInstance(id)) if (IPhreeqc *ptr = IPhreeqcLib::GetInstance(id))
{ {
#pragma omp critical(IPhreeqcLib) mutex_lock(&map_lock);
{ delete ptr;
delete ptr; mutex_unlock(&map_lock);
} retval = IPQ_OK;
retval = IPQ_OK;
} }
} }
return retval; return retval;
@ -939,11 +938,10 @@ IPhreeqcLib::GetInstance(int id)
{ {
std::map<size_t, IPhreeqc*>::iterator it; std::map<size_t, IPhreeqc*>::iterator it;
bool found=false; bool found=false;
#pragma omp critical(IPhreeqcLib) mutex_lock(&map_lock);
{ it = IPhreeqc::Instances.find(size_t(id));
it = IPhreeqc::Instances.find(size_t(id)); found = (it != IPhreeqc::Instances.end());
found = (it != IPhreeqc::Instances.end()); mutex_unlock(&map_lock);
}
if (found) if (found)
{ {
return (*it).second; return (*it).second;

View File

@ -32,6 +32,7 @@ libiphreeqc_la_SOURCES=\
IPhreeqcLib.cpp\ IPhreeqcLib.cpp\
SelectedOutput.cpp\ SelectedOutput.cpp\
SelectedOutput.hxx\ SelectedOutput.hxx\
thread.h\
Var.c\ Var.c\
phreeqcpp/cxxKinetics.cxx\ phreeqcpp/cxxKinetics.cxx\
phreeqcpp/cxxKinetics.h\ phreeqcpp/cxxKinetics.h\

64
src/thread.h Normal file
View File

@ -0,0 +1,64 @@
#if defined(WIN32)
#include <windows.h>
/* This is CriticalSection version */
/*
typedef CRITICAL_SECTION mutex_t;
#define mutex_init(m) InitializeCriticalSection(m)
#define mutex_delete(m) DeleteCriticalSection(m)
#define mutex_lock(m) EnterCriticalSection(m)
#define mutex_trylock(m) TryEnterCriticalSection(m)
#define mutex_unlock(m) LeaveCriticalSection(m)
#define atomic_xchg_1(m) InterlockedExchange(m, 1)
mutex_t map_lock;
#define MUTEX_INITILIZER(m) \
static unsigned int volatile m_init = 0; \
static bool volatile m_wait = true; \
if (!InterlockedExchange(&m_init, 1)) \
{ mutex_init(&m); m_wait = false; } \
while(m_wait);
*/
/* This is simplified spinlock version */
typedef volatile unsigned int mutex_t;
#define mutex_init(m) InterlockedExchange(m, 0)
#define mutex_delete(m)
#define mutex_lock(m) while(InterlockedExchange(m, 1)) Sleep(0)
#define mutex_trylock(m) InterlockedExchange(m, 1)
#define mutex_unlock(m) InterlockedExchange(m, 0)
#define MUTEX_INITIALIZER 0
#else
#include <pthread.h>
typedef pthread_mutex_t mutex_t;
#define mutex_init(m) pthread_mutex_init(m, NULL)
#define mutex_delete(m) pthread_mutex_destroy(m)
#define mutex_lock(m) pthread_mutex_lock(m)
#define mutex_trylock(m) pthread_mutex_trylock(m)
#define mutex_unlock(m) pthread_mutex_unlock(m)
#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#endif
#ifndef _INC_PHREEQC_H
mutex_t map_lock = MUTEX_INITIALIZER;
mutex_t qsort_lock = MUTEX_INITIALIZER;
#else
extern mutex_t map_lock;
extern mutex_t qsort_lock;
#endif
#ifndef WIN32
#define qsort(a, b, c, d) mutex_lock(&qsort_lock); \
qsort(a, b, c, d); \
mutex_unlock(&qsort_lock)
#endif