Mercurial > hg > orthanc
annotate Core/MultiThreading/SharedMessageQueue.cpp @ 2223:29689b30f9ae
cppcheck
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 13 Dec 2016 15:28:05 +0100 |
parents | 93afa6134ab3 |
children | a3a65de1840f |
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 |
450 | 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 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
723
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
450 | 34 #include "SharedMessageQueue.h" |
35 | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
36 |
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 * FIFO (queue): |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
40 * |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
41 * back front |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
42 * +--+--+--+--+--+--+--+--+--+--+--+ |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
43 * Enqueue -> | | | | | | | | | | | | |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
44 * | | | | | | | | | | | | -> Dequeue |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
45 * +--+--+--+--+--+--+--+--+--+--+--+ |
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 * Make room here |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
49 * |
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 * LIFO (stack): |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
52 * |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
53 * back front |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
54 * +--+--+--+--+--+--+--+--+--+--+--+ |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
55 * | | | | | | | | | | | | <- Enqueue |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
56 * | | | | | | | | | | | | -> Dequeue |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
57 * +--+--+--+--+--+--+--+--+--+--+--+ |
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 * Make room here |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
61 **/ |
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 |
450 | 64 namespace Orthanc |
65 { | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
66 SharedMessageQueue::SharedMessageQueue(unsigned int maxSize) : |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
67 isFifo_(true), |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
68 maxSize_(maxSize) |
450 | 69 { |
70 } | |
71 | |
72 | |
73 SharedMessageQueue::~SharedMessageQueue() | |
74 { | |
656 | 75 for (Queue::iterator it = queue_.begin(); it != queue_.end(); ++it) |
450 | 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 { | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
88 if (isFifo_) |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
89 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
90 // Too many elements in the queue: Make room |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
91 delete queue_.front(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
92 queue_.pop_front(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
93 } |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
94 else |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
95 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
96 // Too many elements in the stack: Make room |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
97 delete queue_.back(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
98 queue_.pop_back(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
99 } |
450 | 100 } |
101 | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
102 if (isFifo_) |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
103 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
104 // Queue policy (FIFO) |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
105 queue_.push_back(message); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
106 } |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
107 else |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
108 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
109 // Stack policy (LIFO) |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
110 queue_.push_front(message); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
111 } |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
112 |
450 | 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(); | |
723
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
141 |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
142 if (queue_.empty()) |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
143 { |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
144 emptied_.notify_all(); |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
145 } |
450 | 146 |
147 return message.release(); | |
148 } | |
452 | 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 | |
723
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
157 while (!queue_.empty()) |
452 | 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 } | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
175 |
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 void SharedMessageQueue::SetFifoPolicy() |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
178 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
179 boost::mutex::scoped_lock lock(mutex_); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
180 isFifo_ = true; |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
181 } |
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 void SharedMessageQueue::SetLifoPolicy() |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
184 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
185 boost::mutex::scoped_lock lock(mutex_); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
186 isFifo_ = false; |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
187 } |
2078
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
188 |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
189 void SharedMessageQueue::Clear() |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
190 { |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
191 boost::mutex::scoped_lock lock(mutex_); |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
192 |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
193 if (queue_.empty()) |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
194 { |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
195 return; |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
196 } |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
197 else |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
198 { |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
199 while (!queue_.empty()) |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
200 { |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
201 std::auto_ptr<IDynamicObject> message(queue_.front()); |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
202 queue_.pop_front(); |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
203 } |
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 emptied_.notify_all(); |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
206 } |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
207 } |
450 | 208 } |