Mercurial > hg > orthanc
annotate OrthancFramework/Sources/MultiThreading/SharedMessageQueue.cpp @ 4294:0923247e69f6
log categories: rest -> http + added lua & jobs
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Thu, 05 Nov 2020 11:43:32 +0100 |
parents | e8005a58de16 |
children | b30a8de92ad9 |
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 |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
450 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * the License, or (at your option) any later version. |
450 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
15 * Lesser General Public License for more details. |
450 | 16 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * <http://www.gnu.org/licenses/>. |
450 | 20 **/ |
21 | |
22 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
723
diff
changeset
|
23 #include "../PrecompiledHeaders.h" |
450 | 24 #include "SharedMessageQueue.h" |
25 | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
26 |
3712
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
27 #include "../Compatibility.h" |
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
28 |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
29 |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
30 /** |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
31 * FIFO (queue): |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
32 * |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
33 * back front |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
34 * +--+--+--+--+--+--+--+--+--+--+--+ |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
35 * Enqueue -> | | | | | | | | | | | | |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
36 * | | | | | | | | | | | | -> Dequeue |
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 * Make room here |
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 * |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
43 * LIFO (stack): |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
44 * |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
45 * back front |
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 * | | | | | | | | | | | | <- Enqueue |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
48 * | | | | | | | | | | | | -> Dequeue |
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 * | |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
52 * Make room here |
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 |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
55 |
450 | 56 namespace Orthanc |
57 { | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
58 SharedMessageQueue::SharedMessageQueue(unsigned int maxSize) : |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
59 isFifo_(true), |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
60 maxSize_(maxSize) |
450 | 61 { |
62 } | |
63 | |
64 | |
65 SharedMessageQueue::~SharedMessageQueue() | |
66 { | |
656 | 67 for (Queue::iterator it = queue_.begin(); it != queue_.end(); ++it) |
450 | 68 { |
69 delete *it; | |
70 } | |
71 } | |
72 | |
73 | |
74 void SharedMessageQueue::Enqueue(IDynamicObject* message) | |
75 { | |
76 boost::mutex::scoped_lock lock(mutex_); | |
77 | |
78 if (maxSize_ != 0 && queue_.size() > maxSize_) | |
79 { | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
80 if (isFifo_) |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
81 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
82 // Too many elements in the queue: Make room |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
83 delete queue_.front(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
84 queue_.pop_front(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
85 } |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
86 else |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
87 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
88 // Too many elements in the stack: Make room |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
89 delete queue_.back(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
90 queue_.pop_back(); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
91 } |
450 | 92 } |
93 | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
94 if (isFifo_) |
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 // Queue policy (FIFO) |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
97 queue_.push_back(message); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
98 } |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
99 else |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
100 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
101 // Stack policy (LIFO) |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
102 queue_.push_front(message); |
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 |
450 | 105 elementAvailable_.notify_one(); |
106 } | |
107 | |
108 | |
109 IDynamicObject* SharedMessageQueue::Dequeue(int32_t millisecondsTimeout) | |
110 { | |
111 boost::mutex::scoped_lock lock(mutex_); | |
112 | |
113 // Wait for a message to arrive in the FIFO queue | |
114 while (queue_.empty()) | |
115 { | |
116 if (millisecondsTimeout == 0) | |
117 { | |
118 elementAvailable_.wait(lock); | |
119 } | |
120 else | |
121 { | |
122 bool success = elementAvailable_.timed_wait | |
123 (lock, boost::posix_time::milliseconds(millisecondsTimeout)); | |
124 if (!success) | |
125 { | |
126 return NULL; | |
127 } | |
128 } | |
129 } | |
130 | |
3712
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
131 std::unique_ptr<IDynamicObject> message(queue_.front()); |
450 | 132 queue_.pop_front(); |
723
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
133 |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
134 if (queue_.empty()) |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
135 { |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
136 emptied_.notify_all(); |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
137 } |
450 | 138 |
139 return message.release(); | |
140 } | |
452 | 141 |
142 | |
143 | |
144 bool SharedMessageQueue::WaitEmpty(int32_t millisecondsTimeout) | |
145 { | |
146 boost::mutex::scoped_lock lock(mutex_); | |
147 | |
148 // Wait for the queue to become empty | |
723
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
149 while (!queue_.empty()) |
452 | 150 { |
151 if (millisecondsTimeout == 0) | |
152 { | |
153 emptied_.wait(lock); | |
154 } | |
155 else | |
156 { | |
157 if (!emptied_.timed_wait | |
158 (lock, boost::posix_time::milliseconds(millisecondsTimeout))) | |
159 { | |
160 return false; | |
161 } | |
162 } | |
163 } | |
164 | |
165 return true; | |
166 } | |
1227
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
167 |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
168 |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
169 void SharedMessageQueue::SetFifoPolicy() |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
170 { |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
171 boost::mutex::scoped_lock lock(mutex_); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
172 isFifo_ = true; |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
173 } |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
174 |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
175 void SharedMessageQueue::SetLifoPolicy() |
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 boost::mutex::scoped_lock lock(mutex_); |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
178 isFifo_ = false; |
7266c37d0354
lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
179 } |
2078
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
180 |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
181 void SharedMessageQueue::Clear() |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
182 { |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
183 boost::mutex::scoped_lock lock(mutex_); |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
184 |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
185 if (queue_.empty()) |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
186 { |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
187 return; |
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 else |
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 while (!queue_.empty()) |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
192 { |
3712
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
193 std::unique_ptr<IDynamicObject> message(queue_.front()); |
2078
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
194 queue_.pop_front(); |
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 |
93afa6134ab3
SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
197 emptied_.notify_all(); |
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 } |
4169
e8005a58de16
SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
200 |
e8005a58de16
SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
201 size_t SharedMessageQueue::GetSize() |
e8005a58de16
SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
202 { |
e8005a58de16
SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
203 boost::mutex::scoped_lock lock(mutex_); |
e8005a58de16
SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
204 return queue_.size(); |
e8005a58de16
SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
205 } |
450 | 206 } |