COIN-OR::LEMON - Graph Library

Ticket #223: 4e4e2b7e8ab5.patch

File 4e4e2b7e8ab5.patch, 4.8 KB (added by Balazs Dezso, 15 years ago)

Proof-of-concept

  • lemon/CMakeLists.txt

    # 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  
    1212  arg_parser.cc
    1313  base.cc
    1414  color.cc
     15  lock.cc
    1516  lp_base.cc
    1617  lp_skeleton.cc
    1718  random.cc
  • lemon/Makefile.am

    diff -r 8e68671af789 -r 4e4e2b7e8ab5 lemon/Makefile.am
    a b  
    1111        lemon/arg_parser.cc \
    1212        lemon/base.cc \
    1313        lemon/color.cc \
     14        lemon/lock.cc \
    1415        lemon/lp_base.cc \
    1516        lemon/lp_skeleton.cc \
    1617        lemon/random.cc \
     
    9394        lemon/lgf_reader.h \
    9495        lemon/lgf_writer.h \
    9596        lemon/list_graph.h \
     97        lemon/lock.h \
    9698        lemon/lp.h \
    9799        lemon/lp_base.h \
    98100        lemon/lp_skeleton.h \
  • lemon/bits/alteration_notifier.h

    diff -r 8e68671af789 -r 4e4e2b7e8ab5 lemon/bits/alteration_notifier.h
    a b  
    2323#include <list>
    2424
    2525#include <lemon/core.h>
     26#include <lemon/lock.h>
    2627
    2728//\ingroup graphbits
    2829//\file
     
    251252
    252253    typedef std::list<ObserverBase*> Observers;
    253254    Observers _observers;
     255    Lock *_lock;
    254256
    255257
    256258  public:
     
    260262    // The default constructor of the AlterationNotifier.
    261263    // It creates an empty notifier.
    262264    AlterationNotifier()
    263       : container(0) {}
     265      : container(0) {
     266      _lock = LockFactory::createDefaultLock();
     267    }
    264268
    265269    // \brief Constructor.
    266270    //
    267271    // Constructor with the observed container parameter.
    268272    AlterationNotifier(const Container& _container)
    269       : container(&_container) {}
     273      : container(&_container) {
     274      _lock = LockFactory::createDefaultLock();
     275    }
    270276
    271277    // \brief Copy Constructor of the AlterationNotifier.
    272278    //
     
    274280    // It creates only an empty notifier because the copiable
    275281    // notifier's observers have to be registered still into that notifier.
    276282    AlterationNotifier(const AlterationNotifier& _notifier)
    277       : container(_notifier.container) {}
     283      : container(_notifier.container) {
     284      _lock = LockFactory::createDefaultLock();
     285    }
    278286
    279287    // \brief Destructor.
    280288    //
     
    284292      for (it = _observers.begin(); it != _observers.end(); ++it) {
    285293        (*it)->_notifier = 0;
    286294      }
     295      delete _lock;
    287296    }
    288297
    289298    // \brief Sets the container.
     
    332341  protected:
    333342
    334343    void attach(ObserverBase& observer) {
     344      if (_lock) _lock->lock();
    335345      observer._index = _observers.insert(_observers.begin(), &observer);
    336346      observer._notifier = this;
     347      if (_lock) _lock->unlock();
    337348    }
    338349
    339350    void detach(ObserverBase& observer) {
     351      if (_lock) _lock->lock();
    340352      _observers.erase(observer._index);
    341353      observer._index = _observers.end();
    342354      observer._notifier = 0;
     355      if (_lock) _lock->unlock();
    343356    }
    344357
    345358  public:
  • new file lemon/lock.cc

    diff -r 8e68671af789 -r 4e4e2b7e8ab5 lemon/lock.cc
    - +  
     1#include <lemon/lock.h>
     2
     3namespace lemon {
     4  const LockFactory* LockFactory::_factory = 0;
     5}
  • new file lemon/lock.h

    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
     8namespace 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