# HG changeset patch
# User Balazs Dezso <deba@inf.elte.hu>
# Date 1254386644 -7200
# Node ID 4e4e2b7e8ab5538cfe4cfb4a7912ebd7a3001e83
# Parent 8e68671af7898439dc9790c9d546b76ae0e8e6f6
Proof-of-concept for lock based concurrent map access (#223)
diff -r 8e68671af789 -r 4e4e2b7e8ab5 lemon/CMakeLists.txt
a
|
b
|
|
12 | 12 | arg_parser.cc |
13 | 13 | base.cc |
14 | 14 | color.cc |
| 15 | lock.cc |
15 | 16 | lp_base.cc |
16 | 17 | lp_skeleton.cc |
17 | 18 | random.cc |
diff -r 8e68671af789 -r 4e4e2b7e8ab5 lemon/Makefile.am
a
|
b
|
|
11 | 11 | lemon/arg_parser.cc \ |
12 | 12 | lemon/base.cc \ |
13 | 13 | lemon/color.cc \ |
| 14 | lemon/lock.cc \ |
14 | 15 | lemon/lp_base.cc \ |
15 | 16 | lemon/lp_skeleton.cc \ |
16 | 17 | lemon/random.cc \ |
… |
… |
|
93 | 94 | lemon/lgf_reader.h \ |
94 | 95 | lemon/lgf_writer.h \ |
95 | 96 | lemon/list_graph.h \ |
| 97 | lemon/lock.h \ |
96 | 98 | lemon/lp.h \ |
97 | 99 | lemon/lp_base.h \ |
98 | 100 | lemon/lp_skeleton.h \ |
diff -r 8e68671af789 -r 4e4e2b7e8ab5 lemon/bits/alteration_notifier.h
a
|
b
|
|
23 | 23 | #include <list> |
24 | 24 | |
25 | 25 | #include <lemon/core.h> |
| 26 | #include <lemon/lock.h> |
26 | 27 | |
27 | 28 | //\ingroup graphbits |
28 | 29 | //\file |
… |
… |
|
251 | 252 | |
252 | 253 | typedef std::list<ObserverBase*> Observers; |
253 | 254 | Observers _observers; |
| 255 | Lock *_lock; |
254 | 256 | |
255 | 257 | |
256 | 258 | public: |
… |
… |
|
260 | 262 | // The default constructor of the AlterationNotifier. |
261 | 263 | // It creates an empty notifier. |
262 | 264 | AlterationNotifier() |
263 | | : container(0) {} |
| 265 | : container(0) { |
| 266 | _lock = LockFactory::createDefaultLock(); |
| 267 | } |
264 | 268 | |
265 | 269 | // \brief Constructor. |
266 | 270 | // |
267 | 271 | // Constructor with the observed container parameter. |
268 | 272 | AlterationNotifier(const Container& _container) |
269 | | : container(&_container) {} |
| 273 | : container(&_container) { |
| 274 | _lock = LockFactory::createDefaultLock(); |
| 275 | } |
270 | 276 | |
271 | 277 | // \brief Copy Constructor of the AlterationNotifier. |
272 | 278 | // |
… |
… |
|
274 | 280 | // It creates only an empty notifier because the copiable |
275 | 281 | // notifier's observers have to be registered still into that notifier. |
276 | 282 | AlterationNotifier(const AlterationNotifier& _notifier) |
277 | | : container(_notifier.container) {} |
| 283 | : container(_notifier.container) { |
| 284 | _lock = LockFactory::createDefaultLock(); |
| 285 | } |
278 | 286 | |
279 | 287 | // \brief Destructor. |
280 | 288 | // |
… |
… |
|
284 | 292 | for (it = _observers.begin(); it != _observers.end(); ++it) { |
285 | 293 | (*it)->_notifier = 0; |
286 | 294 | } |
| 295 | delete _lock; |
287 | 296 | } |
288 | 297 | |
289 | 298 | // \brief Sets the container. |
… |
… |
|
332 | 341 | protected: |
333 | 342 | |
334 | 343 | void attach(ObserverBase& observer) { |
| 344 | if (_lock) _lock->lock(); |
335 | 345 | observer._index = _observers.insert(_observers.begin(), &observer); |
336 | 346 | observer._notifier = this; |
| 347 | if (_lock) _lock->unlock(); |
337 | 348 | } |
338 | 349 | |
339 | 350 | void detach(ObserverBase& observer) { |
| 351 | if (_lock) _lock->lock(); |
340 | 352 | _observers.erase(observer._index); |
341 | 353 | observer._index = _observers.end(); |
342 | 354 | observer._notifier = 0; |
| 355 | if (_lock) _lock->unlock(); |
343 | 356 | } |
344 | 357 | |
345 | 358 | public: |
diff -r 8e68671af789 -r 4e4e2b7e8ab5 lemon/lock.cc
-
|
+
|
|
| 1 | #include <lemon/lock.h> |
| 2 | |
| 3 | namespace lemon { |
| 4 | const LockFactory* LockFactory::_factory = 0; |
| 5 | } |
diff -r 8e68671af789 -r 4e4e2b7e8ab5 lemon/lock.h
-
|
+
|
|
| 1 | // -*- C++ -*- |
| 2 | |
| 3 | #ifndef LEMON_LOCK_H |
| 4 | #define LEMON_LOCK_H |
| 5 | |
| 6 | #include <pthread.h> |
| 7 | |
| 8 | namespace lemon { |
| 9 | |
| 10 | class Lock { |
| 11 | public: |
| 12 | virtual void lock() = 0; |
| 13 | virtual void unlock() = 0; |
| 14 | virtual ~Lock() {} |
| 15 | }; |
| 16 | |
| 17 | class LockFactory { |
| 18 | private: |
| 19 | static const LockFactory* _factory; |
| 20 | public: |
| 21 | |
| 22 | static void setDefaultFactory(const LockFactory *factory) { |
| 23 | if (_factory) delete _factory; |
| 24 | _factory = factory; |
| 25 | } |
| 26 | |
| 27 | static const LockFactory* defaultFactory() { |
| 28 | return _factory; |
| 29 | } |
| 30 | |
| 31 | static Lock* createDefaultLock() { |
| 32 | if (_factory == 0) return 0; |
| 33 | else return _factory->createLock(); |
| 34 | } |
| 35 | |
| 36 | virtual Lock* createLock() const = 0; |
| 37 | virtual ~LockFactory() {} |
| 38 | }; |
| 39 | |
| 40 | class PThreadLock : public Lock { |
| 41 | private: |
| 42 | pthread_mutex_t _mutex; |
| 43 | public: |
| 44 | PThreadLock() { |
| 45 | pthread_mutex_init(&_mutex, 0); |
| 46 | } |
| 47 | virtual void lock() { |
| 48 | pthread_mutex_lock(&_mutex); |
| 49 | } |
| 50 | virtual void unlock() { |
| 51 | pthread_mutex_unlock(&_mutex); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | class PThreadLockFactory : public LockFactory { |
| 56 | public: |
| 57 | virtual PThreadLock* createLock() const { |
| 58 | return new PThreadLock(); |
| 59 | } |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | #endif |