# HG changeset patch
# User Balazs Dezso <deba@inf.elte.hu>
# Date 1327155512 -3600
# Node ID 25f977576e5c37a84499f401d28a46363b1358df
# Parent  b1744d7bdb478e2145c094578242b8e897d4df49
Thread safe map construction and destruction

diff -r b1744d7bdb47 -r 25f977576e5c CMakeLists.txt
--- a/CMakeLists.txt	Wed Jan 11 14:12:36 2012 +0100
+++ b/CMakeLists.txt	Sat Jan 21 15:18:32 2012 +0100
@@ -114,6 +114,10 @@
 CHECK_TYPE_SIZE("long long" LONG_LONG)
 SET(LEMON_HAVE_LONG_LONG ${HAVE_LONG_LONG})
 
+INCLUDE(FindThreads)
+SET(LEMON_USE_PTHREAD ${CMAKE_USE_PTHREADS_INIT})
+SET(LEMON_USE_WIN32_THREADS ${CMAKE_USE_WIN32_THREADS_INIT})
+
 ENABLE_TESTING()
 
 IF(${CMAKE_BUILD_TYPE} STREQUAL "Maintainer")
diff -r b1744d7bdb47 -r 25f977576e5c lemon/Makefile.am
--- a/lemon/Makefile.am	Wed Jan 11 14:12:36 2012 +0100
+++ b/lemon/Makefile.am	Sat Jan 21 15:18:32 2012 +0100
@@ -138,6 +138,7 @@
 	lemon/bits/enable_if.h \
 	lemon/bits/graph_adaptor_extender.h \
 	lemon/bits/graph_extender.h \
+	lemon/bits/lock.h \
 	lemon/bits/map_extender.h \
 	lemon/bits/path_dump.h \
 	lemon/bits/solver_bits.h \
diff -r b1744d7bdb47 -r 25f977576e5c lemon/bits/alteration_notifier.h
--- a/lemon/bits/alteration_notifier.h	Wed Jan 11 14:12:36 2012 +0100
+++ b/lemon/bits/alteration_notifier.h	Sat Jan 21 15:18:32 2012 +0100
@@ -23,6 +23,7 @@
 #include <list>
 
 #include <lemon/core.h>
+#include <lemon/bits/lock.h>
 
 //\ingroup graphbits
 //\file
@@ -251,7 +252,7 @@
 
     typedef std::list<ObserverBase*> Observers;
     Observers _observers;
-
+    lemon::bits::Lock _lock;
 
   public:
 
@@ -332,14 +333,18 @@
   protected:
 
     void attach(ObserverBase& observer) {
+      _lock.lock();
       observer._index = _observers.insert(_observers.begin(), &observer);
       observer._notifier = this;
+      _lock.unlock();
     }
 
     void detach(ObserverBase& observer) {
+      _lock.lock();
       _observers.erase(observer._index);
       observer._index = _observers.end();
       observer._notifier = 0;
+      _lock.unlock();
     }
 
   public:
diff -r b1744d7bdb47 -r 25f977576e5c lemon/bits/windows.cc
--- a/lemon/bits/windows.cc	Wed Jan 11 14:12:36 2012 +0100
+++ b/lemon/bits/windows.cc	Sat Jan 21 15:18:32 2012 +0100
@@ -130,5 +130,35 @@
       return getpid() + tv.tv_sec + tv.tv_usec;
 #endif
     }
+
+    WinLock::WinLock() {
+#ifdef WIN32
+      CRITICAL_SECTION *lock = new CRITICAL_SECTION;
+      InitializeCriticalSection(lock);
+      _repr = lock;
+#endif
+    }
+    
+    WinLock::~WinLock() {
+#ifdef WIN32
+      CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
+      DeleteCriticalSection(lock);
+      delete lock;
+#endif
+    }
+
+    void WinLock::lock() {
+#ifdef WIN32
+      CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
+      EnterCriticalSection(lock);
+#endif
+    }
+
+    void WinLock::unlock() {
+#ifdef WIN32
+      CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
+      LeaveCriticalSection(lock);
+#endif
+    }
   }
 }
diff -r b1744d7bdb47 -r 25f977576e5c lemon/bits/windows.h
--- a/lemon/bits/windows.h	Wed Jan 11 14:12:36 2012 +0100
+++ b/lemon/bits/windows.h	Sat Jan 21 15:18:32 2012 +0100
@@ -28,6 +28,16 @@
                          double &cutime, double &cstime);
     std::string getWinFormattedDate();
     int getWinRndSeed();
+
+    class WinLock {
+    public:
+      WinLock();
+      ~WinLock();
+      void lock();
+      void unlock();
+    private:
+      void *_repr;
+    };
   }
 }
 
diff -r b1744d7bdb47 -r 25f977576e5c lemon/config.h.cmake
--- a/lemon/config.h.cmake	Wed Jan 11 14:12:36 2012 +0100
+++ b/lemon/config.h.cmake	Sat Jan 21 15:18:32 2012 +0100
@@ -6,3 +6,5 @@
 #cmakedefine LEMON_HAVE_CPLEX 1
 #cmakedefine LEMON_HAVE_CLP 1
 #cmakedefine LEMON_HAVE_CBC 1
+#cmakedefine LEMON_USE_PTHREAD 1
+#cmakedefine LEMON_USE_WIN32_THREADS 1
diff -r b1744d7bdb47 -r 25f977576e5c lemon/config.h.in
--- a/lemon/config.h.in	Wed Jan 11 14:12:36 2012 +0100
+++ b/lemon/config.h.in	Sat Jan 21 15:18:32 2012 +0100
@@ -24,3 +24,9 @@
 
 /* Define to 1 if you have CBC */
 #undef LEMON_HAVE_CBC
+
+/* Define to 1 if you have pthread */
+#undef LEMON_USE_PTHREAD
+
+/* Define to 1 if you have win32 threads */
+#undef LEMON_USE_WIN32_THREADS
