Mercurial > hg > orthanc
annotate Core/MultiThreading/ReaderWriterLock.cpp @ 815:5dcfdd3bfca9
endianness
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 08 May 2014 15:52:37 +0200 |
parents | 3f946e5c3802 |
children | a811bdf8b8eb |
rev | line source |
---|---|
760 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "ReaderWriterLock.h" | |
34 | |
35 #include <boost/thread/shared_mutex.hpp> | |
36 | |
37 namespace Orthanc | |
38 { | |
39 namespace | |
40 { | |
41 // Anonymous namespace to avoid clashes between compilation | |
42 // modules. | |
43 | |
44 class ReaderLockable : public ILockable | |
45 { | |
46 private: | |
47 boost::shared_mutex& lock_; | |
48 | |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
49 protected: |
760 | 50 virtual void Lock() |
51 { | |
52 lock_.lock_shared(); | |
53 } | |
54 | |
55 virtual void Unlock() | |
56 { | |
57 lock_.unlock_shared(); | |
58 } | |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
59 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
60 public: |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
61 ReaderLockable(boost::shared_mutex& lock) : lock_(lock) |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
62 { |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
63 } |
760 | 64 }; |
65 | |
66 | |
67 class WriterLockable : public ILockable | |
68 { | |
69 private: | |
70 boost::shared_mutex& lock_; | |
71 | |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
72 protected: |
760 | 73 virtual void Lock() |
74 { | |
75 lock_.lock(); | |
76 } | |
77 | |
78 virtual void Unlock() | |
79 { | |
80 lock_.unlock(); | |
81 } | |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
82 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
83 public: |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
84 WriterLockable(boost::shared_mutex& lock) : lock_(lock) |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
85 { |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
86 } |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
87 |
760 | 88 }; |
89 } | |
90 | |
91 struct ReaderWriterLock::PImpl | |
92 { | |
93 boost::shared_mutex lock_; | |
94 ReaderLockable reader_; | |
95 WriterLockable writer_; | |
96 | |
97 PImpl() : reader_(lock_), writer_(lock_) | |
98 { | |
99 } | |
100 }; | |
101 | |
102 | |
103 ReaderWriterLock::ReaderWriterLock() | |
104 { | |
105 pimpl_ = new PImpl; | |
106 } | |
107 | |
108 | |
109 ReaderWriterLock::~ReaderWriterLock() | |
110 { | |
111 delete pimpl_; | |
112 } | |
113 | |
114 | |
115 ILockable& ReaderWriterLock::ForReader() | |
116 { | |
117 return pimpl_->reader_; | |
118 } | |
119 | |
120 | |
121 ILockable& ReaderWriterLock::ForWriter() | |
122 { | |
123 return pimpl_->writer_; | |
124 } | |
125 } |