From 7c603854bd80b617239ac3967b06aeec5a295716 Mon Sep 17 00:00:00 2001 From: Scott R Charlton Date: Fri, 26 Apr 2013 00:49:59 +0000 Subject: [PATCH] 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 --- IPhreeqcLib.cpp | 26 ++++++++++---------- Makefile.am | 1 + thread.h | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 thread.h diff --git a/IPhreeqcLib.cpp b/IPhreeqcLib.cpp index f5030f99..46980933 100644 --- a/IPhreeqcLib.cpp +++ b/IPhreeqcLib.cpp @@ -4,6 +4,7 @@ #include "IPhreeqc.h" #include "IPhreeqc.hpp" +#include "thread.h" class IPhreeqcLib { @@ -903,10 +904,9 @@ IPhreeqcLib::CreateIPhreeqc(void) IPhreeqc* IPhreeqcPtr; try { - #pragma omp critical(IPhreeqcLib) - { - IPhreeqcPtr = new IPhreeqc; - } + mutex_lock(&map_lock); + IPhreeqcPtr = new IPhreeqc; + mutex_unlock(&map_lock); n = (int) IPhreeqcPtr->Index; } catch(...) @@ -924,11 +924,10 @@ IPhreeqcLib::DestroyIPhreeqc(int id) { if (IPhreeqc *ptr = IPhreeqcLib::GetInstance(id)) { - #pragma omp critical(IPhreeqcLib) - { - delete ptr; - } - retval = IPQ_OK; + mutex_lock(&map_lock); + delete ptr; + mutex_unlock(&map_lock); + retval = IPQ_OK; } } return retval; @@ -939,11 +938,10 @@ IPhreeqcLib::GetInstance(int id) { std::map::iterator it; bool found=false; - #pragma omp critical(IPhreeqcLib) - { - it = IPhreeqc::Instances.find(size_t(id)); - found = (it != IPhreeqc::Instances.end()); - } + mutex_lock(&map_lock); + it = IPhreeqc::Instances.find(size_t(id)); + found = (it != IPhreeqc::Instances.end()); + mutex_unlock(&map_lock); if (found) { return (*it).second; diff --git a/Makefile.am b/Makefile.am index 501cf12c..ae3ee373 100644 --- a/Makefile.am +++ b/Makefile.am @@ -32,6 +32,7 @@ libiphreeqc_la_SOURCES=\ IPhreeqcLib.cpp\ SelectedOutput.cpp\ SelectedOutput.hxx\ + thread.h\ Var.c\ phreeqcpp/cxxKinetics.cxx\ phreeqcpp/cxxKinetics.h\ diff --git a/thread.h b/thread.h new file mode 100644 index 00000000..674769f7 --- /dev/null +++ b/thread.h @@ -0,0 +1,64 @@ +#if defined(WIN32) + + #include + +/* 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 + + 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