annotate Core/MultiThreading/SharedMessageQueue.cpp @ 3988:bdd8f952dd47

playing with visibility
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 29 May 2020 19:02:32 +0200
parents 2a170a8f1faf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
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
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * License, or (at your option) any later version.
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * In addition, as a special exception, the copyright holders of this
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * program give permission to link the code of its release with the
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * the linked executables. You must obey the GNU General Public License
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
58b433bb9762 SharedMessageQueue
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
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * you do not wish to do so, delete this exception statement from your
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * version. If you delete this exception statement from all source files
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * in the program, then also delete it here.
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 *
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * This program is distributed in the hope that it will be useful, but
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 * General Public License for more details.
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 *
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * You should have received a copy of the GNU General Public License
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 **/
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
34 #include "../PrecompiledHeaders.h"
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "SharedMessageQueue.h"
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
1227
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
37
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
38 #include "../Compatibility.h"
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
39
1227
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 /**
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
42 * FIFO (queue):
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 * back front
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 * Enqueue -> | | | | | | | | | | | |
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
47 * | | | | | | | | | | | | -> Dequeue
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 * ^
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 * Make room here
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 *
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
54 * LIFO (stack):
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 * back front
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 * | | | | | | | | | | | | <- Enqueue
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
59 * | | | | | | | | | | | | -> Dequeue
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 * ^
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 * Make room here
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
64 **/
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
65
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
66
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 namespace Orthanc
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
1227
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
69 SharedMessageQueue::SharedMessageQueue(unsigned int maxSize) :
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
70 isFifo_(true),
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
71 maxSize_(maxSize)
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 SharedMessageQueue::~SharedMessageQueue()
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 {
656
08eca5d86aad fixes to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 452
diff changeset
78 for (Queue::iterator it = queue_.begin(); it != queue_.end(); ++it)
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 delete *it;
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 void SharedMessageQueue::Enqueue(IDynamicObject* message)
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 boost::mutex::scoped_lock lock(mutex_);
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 if (maxSize_ != 0 && queue_.size() > maxSize_)
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
1227
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
91 if (isFifo_)
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
92 {
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
93 // Too many elements in the queue: Make room
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
94 delete queue_.front();
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
95 queue_.pop_front();
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 else
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 // Too many elements in the stack: Make room
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
100 delete queue_.back();
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
101 queue_.pop_back();
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
102 }
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
1227
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
105 if (isFifo_)
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 // Queue policy (FIFO)
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
108 queue_.push_back(message);
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 else
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 // Stack policy (LIFO)
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
113 queue_.push_front(message);
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
114 }
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
115
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 elementAvailable_.notify_one();
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 IDynamicObject* SharedMessageQueue::Dequeue(int32_t millisecondsTimeout)
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 boost::mutex::scoped_lock lock(mutex_);
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 // Wait for a message to arrive in the FIFO queue
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 while (queue_.empty())
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 if (millisecondsTimeout == 0)
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 elementAvailable_.wait(lock);
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 else
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 bool success = elementAvailable_.timed_wait
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 (lock, boost::posix_time::milliseconds(millisecondsTimeout));
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 if (!success)
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 return NULL;
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
142 std::unique_ptr<IDynamicObject> message(queue_.front());
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 queue_.pop_front();
723
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 if (queue_.empty())
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
146 {
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
147 emptied_.notify_all();
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
148 }
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 return message.release();
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 }
452
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
152
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
153
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
154
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
155 bool SharedMessageQueue::WaitEmpty(int32_t millisecondsTimeout)
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
156 {
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
157 boost::mutex::scoped_lock lock(mutex_);
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
158
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
159 // Wait for the queue to become empty
723
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
160 while (!queue_.empty())
452
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
161 {
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
162 if (millisecondsTimeout == 0)
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
163 {
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
164 emptied_.wait(lock);
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
165 }
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
166 else
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
167 {
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
168 if (!emptied_.timed_wait
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
169 (lock, boost::posix_time::milliseconds(millisecondsTimeout)))
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
170 {
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
171 return false;
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
172 }
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
173 }
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
174 }
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
175
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
176 return true;
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
177 }
1227
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
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
180 void SharedMessageQueue::SetFifoPolicy()
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 boost::mutex::scoped_lock lock(mutex_);
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
183 isFifo_ = true;
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
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
186 void SharedMessageQueue::SetLifoPolicy()
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
187 {
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
188 boost::mutex::scoped_lock lock(mutex_);
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
189 isFifo_ = false;
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
190 }
2078
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 void SharedMessageQueue::Clear()
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 boost::mutex::scoped_lock lock(mutex_);
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 if (queue_.empty())
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 return;
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 else
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 while (!queue_.empty())
93afa6134ab3 SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
203 {
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
204 std::unique_ptr<IDynamicObject> message(queue_.front());
2078
93afa6134ab3 SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
205 queue_.pop_front();
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
93afa6134ab3 SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
208 emptied_.notify_all();
93afa6134ab3 SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
209 }
93afa6134ab3 SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
210 }
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 }