Package: liblemon / 1.3.1+dfsg-6

0002-Threadsafe-CplexEnv-473.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
From: Alpar Juttner <alpar@cs.elte.hu>
Date: Wed, 6 May 2015 16:01:26 +0200
Subject: Threadsafe CplexEnv (#473)

---
 lemon/cplex.cc | 43 ++++++++++++++++++++++++++++++-------------
 lemon/cplex.h  |  5 +++++
 2 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/lemon/cplex.cc b/lemon/cplex.cc
index 029a3ef..4d519fd 100644
--- a/lemon/cplex.cc
+++ b/lemon/cplex.cc
@@ -37,37 +37,54 @@ namespace lemon {
     }
   }
 
+  void CplexEnv::incCnt()
+  {
+    _cnt_lock->lock();
+    ++(*_cnt);
+    _cnt_lock->unlock();
+  }
+
+  void CplexEnv::decCnt()
+  {
+    _cnt_lock->lock();
+    --(*_cnt);
+    if (*_cnt == 0) {
+      delete _cnt;
+      _cnt_lock->unlock();
+      delete _cnt_lock;
+      CPXcloseCPLEX(&_env);
+    }
+    else _cnt_lock->unlock();
+  }
+  
   CplexEnv::CplexEnv() {
     int status;
-    _cnt = new int;
-    (*_cnt) = 1;
     _env = CPXopenCPLEX(&status);
-    if (_env == 0) {
-      delete _cnt;
-      _cnt = 0;
+    if (_env == 0)
       throw LicenseError(status);
-    }
+    _cnt = new int;
+    (*_cnt) = 1;
+    _cnt_lock = new bits::Lock;
   }
 
   CplexEnv::CplexEnv(const CplexEnv& other) {
     _env = other._env;
     _cnt = other._cnt;
-    ++(*_cnt);
+    _cnt_lock = other._cnt_lock;
+    incCnt();
   }
 
   CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
+    decCnt();
     _env = other._env;
     _cnt = other._cnt;
-    ++(*_cnt);
+    _cnt_lock = other._cnt_lock;
+    incCnt();
     return *this;
   }
 
   CplexEnv::~CplexEnv() {
-    --(*_cnt);
-    if (*_cnt == 0) {
-      delete _cnt;
-      CPXcloseCPLEX(&_env);
-    }
+    decCnt();
   }
 
   CplexBase::CplexBase() : LpBase() {
diff --git a/lemon/cplex.h b/lemon/cplex.h
index c17e792..2397f9f 100644
--- a/lemon/cplex.h
+++ b/lemon/cplex.h
@@ -23,6 +23,7 @@
 ///\brief Header of the LEMON-CPLEX lp solver interface.
 
 #include <lemon/lp_base.h>
+#include <lemon/bits/lock.h>
 
 struct cpxenv;
 struct cpxlp;
@@ -40,7 +41,11 @@ namespace lemon {
   private:
     cpxenv* _env;
     mutable int* _cnt;
+    mutable bits::Lock* _cnt_lock;
 
+    void incCnt();
+    void decCnt();
+    
   public:
 
     /// \brief This exception is thrown when the license check is not