COIN-OR::LEMON - Graph Library

Ticket #223: 18cbd451f2b7.patch

File 18cbd451f2b7.patch, 5.8 KB (added by Balazs Dezso, 14 years ago)
  • CMakeLists.txt

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1327832921 -3600
    # Node ID 18cbd451f2b7acbd49c9d0ca13f04549a07cfdbb
    # Parent  b1744d7bdb478e2145c094578242b8e897d4df49
    Thread safe map construction and destruction
    
    diff -r b1744d7bdb47 -r 18cbd451f2b7 CMakeLists.txt
    a b  
    114114CHECK_TYPE_SIZE("long long" LONG_LONG)
    115115SET(LEMON_HAVE_LONG_LONG ${HAVE_LONG_LONG})
    116116
     117INCLUDE(FindThreads)
     118SET(LEMON_USE_PTHREAD ${CMAKE_USE_PTHREADS_INIT})
     119SET(LEMON_USE_WIN32_THREADS ${CMAKE_USE_WIN32_THREADS_INIT})
     120
    117121ENABLE_TESTING()
    118122
    119123IF(${CMAKE_BUILD_TYPE} STREQUAL "Maintainer")
  • lemon/Makefile.am

    diff -r b1744d7bdb47 -r 18cbd451f2b7 lemon/Makefile.am
    a b  
    138138        lemon/bits/enable_if.h \
    139139        lemon/bits/graph_adaptor_extender.h \
    140140        lemon/bits/graph_extender.h \
     141        lemon/bits/lock.h \
    141142        lemon/bits/map_extender.h \
    142143        lemon/bits/path_dump.h \
    143144        lemon/bits/solver_bits.h \
  • lemon/bits/alteration_notifier.h

    diff -r b1744d7bdb47 -r 18cbd451f2b7 lemon/bits/alteration_notifier.h
    a b  
    2323#include <list>
    2424
    2525#include <lemon/core.h>
     26#include <lemon/bits/lock.h>
    2627
    2728//\ingroup graphbits
    2829//\file
     
    251252
    252253    typedef std::list<ObserverBase*> Observers;
    253254    Observers _observers;
    254 
     255    lemon::bits::Lock _lock;
    255256
    256257  public:
    257258
     
    332333  protected:
    333334
    334335    void attach(ObserverBase& observer) {
     336      _lock.lock();
    335337      observer._index = _observers.insert(_observers.begin(), &observer);
    336338      observer._notifier = this;
     339      _lock.unlock();
    337340    }
    338341
    339342    void detach(ObserverBase& observer) {
     343      _lock.lock();
    340344      _observers.erase(observer._index);
    341345      observer._index = _observers.end();
    342346      observer._notifier = 0;
     347      _lock.unlock();
    343348    }
    344349
    345350  public:
  • new file lemon/bits/lock.h

    diff -r b1744d7bdb47 -r 18cbd451f2b7 lemon/bits/lock.h
    - +  
     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
     29namespace 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
  • lemon/bits/windows.cc

    diff -r b1744d7bdb47 -r 18cbd451f2b7 lemon/bits/windows.cc
    a b  
    130130      return getpid() + tv.tv_sec + tv.tv_usec;
    131131#endif
    132132    }
     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    }
    133163  }
    134164}
  • lemon/bits/windows.h

    diff -r b1744d7bdb47 -r 18cbd451f2b7 lemon/bits/windows.h
    a b  
    2828                         double &cutime, double &cstime);
    2929    std::string getWinFormattedDate();
    3030    int getWinRndSeed();
     31
     32    class WinLock {
     33    public:
     34      WinLock();
     35      ~WinLock();
     36      void lock();
     37      void unlock();
     38    private:
     39      void *_repr;
     40    };
    3141  }
    3242}
    3343
  • lemon/config.h.cmake

    diff -r b1744d7bdb47 -r 18cbd451f2b7 lemon/config.h.cmake
    a b  
    66#cmakedefine LEMON_HAVE_CPLEX 1
    77#cmakedefine LEMON_HAVE_CLP 1
    88#cmakedefine LEMON_HAVE_CBC 1
     9#cmakedefine LEMON_USE_PTHREAD 1
     10#cmakedefine LEMON_USE_WIN32_THREADS 1
  • lemon/config.h.in

    diff -r b1744d7bdb47 -r 18cbd451f2b7 lemon/config.h.in
    a b  
    2424
    2525/* Define to 1 if you have CBC */
    2626#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