Mercurial > hg > orthanc-wsi
comparison Resources/Orthanc/Core/MultiThreading/SharedMessageQueue.cpp @ 59:7a3853d51c45
Move "Framework/Orthanc/" as "Resources/Orthanc/"
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 25 Nov 2016 16:34:34 +0100 |
parents | Framework/Orthanc/Core/MultiThreading/SharedMessageQueue.cpp@dc730d11b101 |
children | ff0ef01c332c |
comparison
equal
deleted
inserted
replaced
58:35468714a38e | 59:7a3853d51c45 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, 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 "../PrecompiledHeaders.h" | |
34 #include "SharedMessageQueue.h" | |
35 | |
36 | |
37 | |
38 /** | |
39 * FIFO (queue): | |
40 * | |
41 * back front | |
42 * +--+--+--+--+--+--+--+--+--+--+--+ | |
43 * Enqueue -> | | | | | | | | | | | | | |
44 * | | | | | | | | | | | | -> Dequeue | |
45 * +--+--+--+--+--+--+--+--+--+--+--+ | |
46 * ^ | |
47 * | | |
48 * Make room here | |
49 * | |
50 * | |
51 * LIFO (stack): | |
52 * | |
53 * back front | |
54 * +--+--+--+--+--+--+--+--+--+--+--+ | |
55 * | | | | | | | | | | | | <- Enqueue | |
56 * | | | | | | | | | | | | -> Dequeue | |
57 * +--+--+--+--+--+--+--+--+--+--+--+ | |
58 * ^ | |
59 * | | |
60 * Make room here | |
61 **/ | |
62 | |
63 | |
64 namespace Orthanc | |
65 { | |
66 SharedMessageQueue::SharedMessageQueue(unsigned int maxSize) : | |
67 isFifo_(true), | |
68 maxSize_(maxSize) | |
69 { | |
70 } | |
71 | |
72 | |
73 SharedMessageQueue::~SharedMessageQueue() | |
74 { | |
75 for (Queue::iterator it = queue_.begin(); it != queue_.end(); ++it) | |
76 { | |
77 delete *it; | |
78 } | |
79 } | |
80 | |
81 | |
82 void SharedMessageQueue::Enqueue(IDynamicObject* message) | |
83 { | |
84 boost::mutex::scoped_lock lock(mutex_); | |
85 | |
86 if (maxSize_ != 0 && queue_.size() > maxSize_) | |
87 { | |
88 if (isFifo_) | |
89 { | |
90 // Too many elements in the queue: Make room | |
91 delete queue_.front(); | |
92 queue_.pop_front(); | |
93 } | |
94 else | |
95 { | |
96 // Too many elements in the stack: Make room | |
97 delete queue_.back(); | |
98 queue_.pop_back(); | |
99 } | |
100 } | |
101 | |
102 if (isFifo_) | |
103 { | |
104 // Queue policy (FIFO) | |
105 queue_.push_back(message); | |
106 } | |
107 else | |
108 { | |
109 // Stack policy (LIFO) | |
110 queue_.push_front(message); | |
111 } | |
112 | |
113 elementAvailable_.notify_one(); | |
114 } | |
115 | |
116 | |
117 IDynamicObject* SharedMessageQueue::Dequeue(int32_t millisecondsTimeout) | |
118 { | |
119 boost::mutex::scoped_lock lock(mutex_); | |
120 | |
121 // Wait for a message to arrive in the FIFO queue | |
122 while (queue_.empty()) | |
123 { | |
124 if (millisecondsTimeout == 0) | |
125 { | |
126 elementAvailable_.wait(lock); | |
127 } | |
128 else | |
129 { | |
130 bool success = elementAvailable_.timed_wait | |
131 (lock, boost::posix_time::milliseconds(millisecondsTimeout)); | |
132 if (!success) | |
133 { | |
134 return NULL; | |
135 } | |
136 } | |
137 } | |
138 | |
139 std::auto_ptr<IDynamicObject> message(queue_.front()); | |
140 queue_.pop_front(); | |
141 | |
142 if (queue_.empty()) | |
143 { | |
144 emptied_.notify_all(); | |
145 } | |
146 | |
147 return message.release(); | |
148 } | |
149 | |
150 | |
151 | |
152 bool SharedMessageQueue::WaitEmpty(int32_t millisecondsTimeout) | |
153 { | |
154 boost::mutex::scoped_lock lock(mutex_); | |
155 | |
156 // Wait for the queue to become empty | |
157 while (!queue_.empty()) | |
158 { | |
159 if (millisecondsTimeout == 0) | |
160 { | |
161 emptied_.wait(lock); | |
162 } | |
163 else | |
164 { | |
165 if (!emptied_.timed_wait | |
166 (lock, boost::posix_time::milliseconds(millisecondsTimeout))) | |
167 { | |
168 return false; | |
169 } | |
170 } | |
171 } | |
172 | |
173 return true; | |
174 } | |
175 | |
176 | |
177 void SharedMessageQueue::SetFifoPolicy() | |
178 { | |
179 boost::mutex::scoped_lock lock(mutex_); | |
180 isFifo_ = true; | |
181 } | |
182 | |
183 void SharedMessageQueue::SetLifoPolicy() | |
184 { | |
185 boost::mutex::scoped_lock lock(mutex_); | |
186 isFifo_ = false; | |
187 } | |
188 | |
189 void SharedMessageQueue::Clear() | |
190 { | |
191 boost::mutex::scoped_lock lock(mutex_); | |
192 | |
193 if (queue_.empty()) | |
194 { | |
195 return; | |
196 } | |
197 else | |
198 { | |
199 while (!queue_.empty()) | |
200 { | |
201 std::auto_ptr<IDynamicObject> message(queue_.front()); | |
202 queue_.pop_front(); | |
203 } | |
204 | |
205 emptied_.notify_all(); | |
206 } | |
207 } | |
208 } |