Mercurial > hg > orthanc
annotate Core/MultiThreading/SharedMessageQueue.cpp @ 3004:80d1a3452807
fix NEWS
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 12 Dec 2018 09:08:34 +0100 |
parents | 878b59270859 |
children | 4e43e67f8ecf |
rev | line source |
---|---|
450 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1227
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
450 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
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 | |
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 | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
723
diff
changeset
|
34 #include "../PrecompiledHeaders.h" |
450 | 35 #include "SharedMessageQueue.h" |
36 | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
37 |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
38 |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
39 /** |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
40 * FIFO (queue): |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
41 * |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
42 * back front |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
43 * +--+--+--+--+--+--+--+--+--+--+--+ |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
44 * Enqueue -> | | | | | | | | | | | | |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
45 * | | | | | | | | | | | | -> Dequeue |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
46 * +--+--+--+--+--+--+--+--+--+--+--+ |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
47 * ^ |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
48 * | |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
49 * Make room here |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
50 * |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
51 * |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
52 * LIFO (stack): |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
53 * |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
54 * back front |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
55 * +--+--+--+--+--+--+--+--+--+--+--+ |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
56 * | | | | | | | | | | | | <- Enqueue |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
57 * | | | | | | | | | | | | -> Dequeue |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
58 * +--+--+--+--+--+--+--+--+--+--+--+ |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
59 * ^ |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
60 * | |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
61 * Make room here |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
62 **/ |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
63 |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
64 |
450 | 65 namespace Orthanc |
66 { | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
67 SharedMessageQueue::SharedMessageQueue(unsigned int maxSize) : |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
68 isFifo_(true), |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
69 maxSize_(maxSize) |
450 | 70 { |
71 } | |
72 | |
73 | |
74 SharedMessageQueue::~SharedMessageQueue() | |
75 { | |
656 | 76 for (Queue::iterator it = queue_.begin(); it != queue_.end(); ++it) |
450 | 77 { |
78 delete *it; | |
79 } | |
80 } | |
81 | |
82 | |
83 void SharedMessageQueue::Enqueue(IDynamicObject* message) | |
84 { | |
85 boost::mutex::scoped_lock lock(mutex_); | |
86 | |
87 if (maxSize_ != 0 && queue_.size() > maxSize_) | |
88 { | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
89 if (isFifo_) |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
90 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
91 // Too many elements in the queue: Make room |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
92 delete queue_.front(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
93 queue_.pop_front(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
94 } |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
95 else |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
96 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
97 // Too many elements in the stack: Make room |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
98 delete queue_.back(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
99 queue_.pop_back(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
100 } |
450 | 101 } |
102 | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
103 if (isFifo_) |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
104 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
105 // Queue policy (FIFO) |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
106 queue_.push_back(message); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
107 } |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
108 else |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
109 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
110 // Stack policy (LIFO) |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
111 queue_.push_front(message); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
112 } |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
113 |
450 | 114 elementAvailable_.notify_one(); |
115 } | |
116 | |
117 | |
118 IDynamicObject* SharedMessageQueue::Dequeue(int32_t millisecondsTimeout) | |
119 { | |
120 boost::mutex::scoped_lock lock(mutex_); | |
121 | |
122 // Wait for a message to arrive in the FIFO queue | |
123 while (queue_.empty()) | |
124 { | |
125 if (millisecondsTimeout == 0) | |
126 { | |
127 elementAvailable_.wait(lock); | |
128 } | |
129 else | |
130 { | |
131 bool success = elementAvailable_.timed_wait | |
132 (lock, boost::posix_time::milliseconds(millisecondsTimeout)); | |
133 if (!success) | |
134 { | |
135 return NULL; | |
136 } | |
137 } | |
138 } | |
139 | |
140 std::auto_ptr<IDynamicObject> message(queue_.front()); | |
141 queue_.pop_front(); | |
723
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
142 |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
143 if (queue_.empty()) |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
144 { |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
145 emptied_.notify_all(); |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
146 } |
450 | 147 |
148 return message.release(); | |
149 } | |
452 | 150 |
151 | |
152 | |
153 bool SharedMessageQueue::WaitEmpty(int32_t millisecondsTimeout) | |
154 { | |
155 boost::mutex::scoped_lock lock(mutex_); | |
156 | |
157 // Wait for the queue to become empty | |
723
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
158 while (!queue_.empty()) |
452 | 159 { |
160 if (millisecondsTimeout == 0) | |
161 { | |
162 emptied_.wait(lock); | |
163 } | |
164 else | |
165 { | |
166 if (!emptied_.timed_wait | |
167 (lock, boost::posix_time::milliseconds(millisecondsTimeout))) | |
168 { | |
169 return false; | |
170 } | |
171 } | |
172 } | |
173 | |
174 return true; | |
175 } | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
176 |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
177 |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
178 void SharedMessageQueue::SetFifoPolicy() |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
179 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
180 boost::mutex::scoped_lock lock(mutex_); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
181 isFifo_ = true; |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
182 } |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
183 |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
184 void SharedMessageQueue::SetLifoPolicy() |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
185 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
186 boost::mutex::scoped_lock lock(mutex_); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
187 isFifo_ = false; |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
188 } |
2078
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
189 |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
190 void SharedMessageQueue::Clear() |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
191 { |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
192 boost::mutex::scoped_lock lock(mutex_); |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
193 |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
194 if (queue_.empty()) |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
195 { |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
196 return; |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
197 } |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
198 else |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
199 { |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
200 while (!queue_.empty()) |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
201 { |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
202 std::auto_ptr<IDynamicObject> message(queue_.front()); |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
203 queue_.pop_front(); |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
204 } |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
205 |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
206 emptied_.notify_all(); |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
207 } |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
208 } |
450 | 209 } |