gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Thread safe map construction and destruction (#223) It currently support pthread and windows threads.
0 7 1
default
8 files changed with 124 insertions and 1 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2012
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BITS_LOCK_H
20
#define LEMON_BITS_LOCK_H
21

	
22
#include <lemon/config.h>
23
#if defined(LEMON_USE_PTHREAD)
24
#include <pthread.h>
25
#elif defined(LEMON_USE_WIN32_THREADS)
26
#include <lemon/bits/windows.h>
27
#endif
28

	
29
namespace lemon {
30
  namespace bits {
31

	
32
#if defined(LEMON_USE_PTHREAD)
33
    class Lock {
34
    public:
35
      Lock() {
36
	pthread_mutex_init(&_lock, 0);
37
      }
38
      ~Lock() {
39
	pthread_mutex_destroy(&_lock);
40
      }
41
      void lock() {
42
	pthread_mutex_lock(&_lock);
43
      }
44
      void unlock() {
45
	pthread_mutex_unlock(&_lock);
46
      }
47

	
48
    private:
49
      pthread_mutex_t _lock;
50
    };
51
#elif defined(LEMON_USE_WIN32_THREADS)
52
    class Lock : public WinLock {};
53
#else
54
    class Lock {
55
    public:
56
      Lock() {}
57
      ~Lock() {}
58
      void lock() {}
59
      void unlock() {}
60
    };    
61
#endif
62
  }
63
}
64

	
65
#endif
Ignore white space 24 line context
... ...
@@ -105,24 +105,28 @@
105 105
  SET(CMAKE_BUILD_TYPE "Release")
106 106
ENDIF()
107 107

	
108 108
SET( CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
109 109
    "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel Maintainer."
110 110
    FORCE )
111 111

	
112 112

	
113 113
INCLUDE(CheckTypeSize)
114 114
CHECK_TYPE_SIZE("long long" LONG_LONG)
115 115
SET(LEMON_HAVE_LONG_LONG ${HAVE_LONG_LONG})
116 116

	
117
INCLUDE(FindThreads)
118
SET(LEMON_USE_PTHREAD ${CMAKE_USE_PTHREADS_INIT})
119
SET(LEMON_USE_WIN32_THREADS ${CMAKE_USE_WIN32_THREADS_INIT})
120

	
117 121
ENABLE_TESTING()
118 122

	
119 123
IF(${CMAKE_BUILD_TYPE} STREQUAL "Maintainer")
120 124
  ADD_CUSTOM_TARGET(check ALL COMMAND ${CMAKE_CTEST_COMMAND})
121 125
ELSE()
122 126
  ADD_CUSTOM_TARGET(check COMMAND ${CMAKE_CTEST_COMMAND})
123 127
ENDIF()
124 128

	
125 129
ADD_SUBDIRECTORY(lemon)
126 130
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
127 131
  ADD_SUBDIRECTORY(contrib)
128 132
  ADD_SUBDIRECTORY(demo)
Ignore white space 6 line context
... ...
@@ -129,24 +129,25 @@
129 129
	lemon/unionfind.h \
130 130
	lemon/bits/windows.h
131 131

	
132 132
bits_HEADERS += \
133 133
	lemon/bits/alteration_notifier.h \
134 134
	lemon/bits/array_map.h \
135 135
	lemon/bits/bezier.h \
136 136
	lemon/bits/default_map.h \
137 137
	lemon/bits/edge_set_extender.h \
138 138
	lemon/bits/enable_if.h \
139 139
	lemon/bits/graph_adaptor_extender.h \
140 140
	lemon/bits/graph_extender.h \
141
	lemon/bits/lock.h \
141 142
	lemon/bits/map_extender.h \
142 143
	lemon/bits/path_dump.h \
143 144
	lemon/bits/solver_bits.h \
144 145
	lemon/bits/traits.h \
145 146
	lemon/bits/variant.h \
146 147
	lemon/bits/vector_map.h
147 148

	
148 149
concept_HEADERS += \
149 150
	lemon/concepts/digraph.h \
150 151
	lemon/concepts/graph.h \
151 152
	lemon/concepts/graph_components.h \
152 153
	lemon/concepts/heap.h \
Ignore white space 6 line context
... ...
@@ -14,24 +14,25 @@
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_ALTERATION_NOTIFIER_H
20 20
#define LEMON_BITS_ALTERATION_NOTIFIER_H
21 21

	
22 22
#include <vector>
23 23
#include <list>
24 24

	
25 25
#include <lemon/core.h>
26
#include <lemon/bits/lock.h>
26 27

	
27 28
//\ingroup graphbits
28 29
//\file
29 30
//\brief Observer notifier for graph alteration observers.
30 31

	
31 32
namespace lemon {
32 33

	
33 34
  // \ingroup graphbits
34 35
  //
35 36
  // \brief Notifier class to notify observes about alterations in
36 37
  // a container.
37 38
  //
... ...
@@ -242,25 +243,25 @@
242 243
      // items are erased from the container. It have to be overrided in
243 244
      // the subclasses.
244 245
      virtual void clear() = 0;
245 246

	
246 247
    };
247 248

	
248 249
  protected:
249 250

	
250 251
    const Container* container;
251 252

	
252 253
    typedef std::list<ObserverBase*> Observers;
253 254
    Observers _observers;
254

	
255
    lemon::bits::Lock _lock;
255 256

	
256 257
  public:
257 258

	
258 259
    // \brief Default constructor.
259 260
    //
260 261
    // The default constructor of the AlterationNotifier.
261 262
    // It creates an empty notifier.
262 263
    AlterationNotifier()
263 264
      : container(0) {}
264 265

	
265 266
    // \brief Constructor.
266 267
    //
... ...
@@ -323,32 +324,36 @@
323 324
    }
324 325

	
325 326
    // \brief Returns the maximum id of the container.
326 327
    //
327 328
    // Returns the maximum id of the container.
328 329
    int maxId() const {
329 330
      return container->maxId(Item());
330 331
    }
331 332

	
332 333
  protected:
333 334

	
334 335
    void attach(ObserverBase& observer) {
336
      _lock.lock();
335 337
      observer._index = _observers.insert(_observers.begin(), &observer);
336 338
      observer._notifier = this;
339
      _lock.unlock();
337 340
    }
338 341

	
339 342
    void detach(ObserverBase& observer) {
343
      _lock.lock();
340 344
      _observers.erase(observer._index);
341 345
      observer._index = _observers.end();
342 346
      observer._notifier = 0;
347
      _lock.unlock();
343 348
    }
344 349

	
345 350
  public:
346 351

	
347 352
    // \brief Notifies all the registed observers about an item added to
348 353
    // the container.
349 354
    //
350 355
    // It notifies all the registed observers about an item added to
351 356
    // the container.
352 357
    void add(const Item& item) {
353 358
      typename Observers::reverse_iterator it;
354 359
      try {
Ignore white space 6 line context
... ...
@@ -121,14 +121,44 @@
121 121
    int getWinRndSeed()
122 122
    {
123 123
#ifdef WIN32
124 124
      FILETIME time;
125 125
      GetSystemTimeAsFileTime(&time);
126 126
      return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
127 127
#else
128 128
      timeval tv;
129 129
      gettimeofday(&tv, 0);
130 130
      return getpid() + tv.tv_sec + tv.tv_usec;
131 131
#endif
132 132
    }
133

	
134
    WinLock::WinLock() {
135
#ifdef WIN32
136
      CRITICAL_SECTION *lock = new CRITICAL_SECTION;
137
      InitializeCriticalSection(lock);
138
      _repr = lock;
139
#endif
140
    }
141
    
142
    WinLock::~WinLock() {
143
#ifdef WIN32
144
      CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
145
      DeleteCriticalSection(lock);
146
      delete lock;
147
#endif
148
    }
149

	
150
    void WinLock::lock() {
151
#ifdef WIN32
152
      CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
153
      EnterCriticalSection(lock);
154
#endif
155
    }
156

	
157
    void WinLock::unlock() {
158
#ifdef WIN32
159
      CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
160
      LeaveCriticalSection(lock);
161
#endif
162
    }
133 163
  }
134 164
}
Ignore white space 6 line context
... ...
@@ -19,16 +19,26 @@
19 19
#ifndef LEMON_BITS_WINDOWS_H
20 20
#define LEMON_BITS_WINDOWS_H
21 21

	
22 22
#include <string>
23 23

	
24 24
namespace lemon {
25 25
  namespace bits {
26 26
    void getWinProcTimes(double &rtime,
27 27
                         double &utime, double &stime,
28 28
                         double &cutime, double &cstime);
29 29
    std::string getWinFormattedDate();
30 30
    int getWinRndSeed();
31

	
32
    class WinLock {
33
    public:
34
      WinLock();
35
      ~WinLock();
36
      void lock();
37
      void unlock();
38
    private:
39
      void *_repr;
40
    };
31 41
  }
32 42
}
33 43

	
34 44
#endif
Ignore white space 6 line context
1 1
#define LEMON_VERSION "@PROJECT_VERSION@"
2 2
#cmakedefine LEMON_HAVE_LONG_LONG 1
3 3
#cmakedefine LEMON_HAVE_LP 1
4 4
#cmakedefine LEMON_HAVE_MIP 1
5 5
#cmakedefine LEMON_HAVE_GLPK 1
6 6
#cmakedefine LEMON_HAVE_CPLEX 1
7 7
#cmakedefine LEMON_HAVE_CLP 1
8 8
#cmakedefine LEMON_HAVE_CBC 1
9
#cmakedefine LEMON_USE_PTHREAD 1
10
#cmakedefine LEMON_USE_WIN32_THREADS 1
Ignore white space 6 line context
... ...
@@ -15,12 +15,18 @@
15 15

	
16 16
/* Define to 1 if you have GLPK. */
17 17
#undef LEMON_HAVE_GLPK
18 18

	
19 19
/* Define to 1 if you have SOPLEX */
20 20
#undef LEMON_HAVE_SOPLEX
21 21

	
22 22
/* Define to 1 if you have CLP */
23 23
#undef LEMON_HAVE_CLP
24 24

	
25 25
/* Define to 1 if you have CBC */
26 26
#undef LEMON_HAVE_CBC
27

	
28
/* Define to 1 if you have pthread */
29
#undef LEMON_USE_PTHREAD
30

	
31
/* Define to 1 if you have win32 threads */
32
#undef LEMON_USE_WIN32_THREADS
0 comments (0 inline)