Mercurial > hg > orthanc
comparison OrthancFramework/Sources/MultiThreading/Semaphore.h @ 4068:408ac60c3cf8 framework
integration mainline->framework
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 12 Jun 2020 09:29:03 +0200 |
parents | Core/MultiThreading/Semaphore.h@7176ebf08765 Core/MultiThreading/Semaphore.h@d25f4c0fa160 |
children | d5c09b5f882f |
comparison
equal
deleted
inserted
replaced
4067:3d7afb01dc14 | 4068:408ac60c3cf8 |
---|---|
18 * modify file(s) with this exception, you may extend this exception to | 18 * modify file(s) with this exception, you may extend this exception to |
19 * your version of the file(s), but you are not obligated to do so. If | 19 * your version of the file(s), but you are not obligated to do so. If |
20 * you do not wish to do so, delete this exception statement from your | 20 * you do not wish to do so, delete this exception statement from your |
21 * version. If you delete this exception statement from all source files | 21 * version. If you delete this exception statement from all source files |
22 * in the program, then also delete it here. | 22 * in the program, then also delete it here. |
23 * | 23 * |
24 * This program is distributed in the hope that it will be useful, but | 24 * This program is distributed in the hope that it will be useful, but |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | 25 * WITHOUT ANY WARRANTY; without even the implied warranty of |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
27 * General Public License for more details. | 27 * General Public License for more details. |
28 * | 28 * |
42 { | 42 { |
43 private: | 43 private: |
44 unsigned int availableResources_; | 44 unsigned int availableResources_; |
45 boost::mutex mutex_; | 45 boost::mutex mutex_; |
46 boost::condition_variable condition_; | 46 boost::condition_variable condition_; |
47 | |
48 void Release(); | |
49 | 47 |
50 void Acquire(); | 48 void Release(unsigned int resourceCount = 1); |
51 | 49 |
52 bool TryAcquire(); | 50 void Acquire(unsigned int resourceCount = 1); |
51 | |
52 bool TryAcquire(unsigned int resourceCount = 1); | |
53 public: | 53 public: |
54 explicit Semaphore(unsigned int availableResources); | 54 explicit Semaphore(unsigned int availableResources); |
55 | 55 |
56 unsigned int GetAvailableResourcesCount() const | 56 unsigned int GetAvailableResourcesCount() const |
57 { | 57 { |
61 | 61 |
62 class Locker : public boost::noncopyable | 62 class Locker : public boost::noncopyable |
63 { | 63 { |
64 private: | 64 private: |
65 Semaphore& that_; | 65 Semaphore& that_; |
66 unsigned int resourceCount_; | |
66 | 67 |
67 public: | 68 public: |
68 explicit Locker(Semaphore& that) : | 69 explicit Locker(Semaphore& that, unsigned int resourceCount = 1) : |
69 that_(that) | 70 that_(that), |
71 resourceCount_(resourceCount) | |
70 { | 72 { |
71 that_.Acquire(); | 73 that_.Acquire(resourceCount_); |
72 } | 74 } |
73 | 75 |
74 ~Locker() | 76 ~Locker() |
75 { | 77 { |
76 that_.Release(); | 78 that_.Release(resourceCount_); |
77 } | 79 } |
78 }; | 80 }; |
79 | 81 |
80 class TryLocker : public boost::noncopyable | 82 class TryLocker : public boost::noncopyable |
81 { | 83 { |
82 private: | 84 private: |
83 Semaphore& that_; | 85 Semaphore& that_; |
84 bool isAcquired_; | 86 unsigned int resourceCount_; |
87 bool isAcquired_; | |
85 | 88 |
86 public: | 89 public: |
87 explicit TryLocker(Semaphore& that) : | 90 explicit TryLocker(Semaphore& that, unsigned int resourceCount = 1) : |
88 that_(that) | 91 that_(that), |
92 resourceCount_(resourceCount) | |
89 { | 93 { |
90 isAcquired_ = that_.TryAcquire(); | 94 isAcquired_ = that_.TryAcquire(resourceCount_); |
91 } | 95 } |
92 | 96 |
93 ~TryLocker() | 97 ~TryLocker() |
94 { | 98 { |
95 if (isAcquired_) | 99 if (isAcquired_) |
96 { | 100 { |
97 that_.Release(); | 101 that_.Release(resourceCount_); |
98 } | 102 } |
99 } | 103 } |
100 | 104 |
101 bool IsAcquired() const | 105 bool IsAcquired() const |
102 { | 106 { |