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