annotate OrthancFramework/Sources/MultiThreading/SharedMessageQueue.cpp @ 4733:1db3b79d97bd

Error code and description of jobs are now saved into the Orthanc database
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 28 Jun 2021 14:25:37 +0200
parents d9473bd5ed43
children 7053502fbf97
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
4437
d9473bd5ed43 upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4300
diff changeset
5 * Copyright (C) 2017-2021 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
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
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
23 #include "../PrecompiledHeaders.h"
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "SharedMessageQueue.h"
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 namespace Orthanc
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 SharedMessageQueue::~SharedMessageQueue()
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 {
656
08eca5d86aad fixes to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 452
diff changeset
67 for (Queue::iterator it = queue_.begin(); it != queue_.end(); ++it)
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 delete *it;
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 }
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 void SharedMessageQueue::Enqueue(IDynamicObject* message)
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 boost::mutex::scoped_lock lock(mutex_);
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 if (maxSize_ != 0 && queue_.size() > maxSize_)
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 elementAvailable_.notify_one();
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 IDynamicObject* SharedMessageQueue::Dequeue(int32_t millisecondsTimeout)
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 boost::mutex::scoped_lock lock(mutex_);
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 // Wait for a message to arrive in the FIFO queue
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 while (queue_.empty())
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 if (millisecondsTimeout == 0)
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 elementAvailable_.wait(lock);
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 else
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 bool success = elementAvailable_.timed_wait
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 (lock, boost::posix_time::milliseconds(millisecondsTimeout));
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 if (!success)
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 {
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 return NULL;
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 }
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 return message.release();
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
452
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
141
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
142
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
143
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
144 bool SharedMessageQueue::WaitEmpty(int32_t millisecondsTimeout)
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
145 {
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
146 boost::mutex::scoped_lock lock(mutex_);
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
147
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
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
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
150 {
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
151 if (millisecondsTimeout == 0)
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 emptied_.wait(lock);
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 else
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 if (!emptied_.timed_wait
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
158 (lock, boost::posix_time::milliseconds(millisecondsTimeout)))
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
159 {
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
160 return false;
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 }
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
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
165 return true;
80f7539147a2 WaitEmpty
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 450
diff changeset
166 }
1227
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
167
4300
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4169
diff changeset
168 bool SharedMessageQueue::IsFifoPolicy() const
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4169
diff changeset
169 {
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4169
diff changeset
170 return isFifo_;
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4169
diff changeset
171 }
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4169
diff changeset
172
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4169
diff changeset
173 bool SharedMessageQueue::IsLifoPolicy() const
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4169
diff changeset
174 {
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4169
diff changeset
175 return !isFifo_;
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4169
diff changeset
176 }
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4169
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 void SharedMessageQueue::SetFifoPolicy()
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
180 {
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
181 boost::mutex::scoped_lock lock(mutex_);
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
182 isFifo_ = true;
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
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
185 void SharedMessageQueue::SetLifoPolicy()
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
186 {
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
187 boost::mutex::scoped_lock lock(mutex_);
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
188 isFifo_ = false;
7266c37d0354 lifo policy in SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
189 }
2078
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 void SharedMessageQueue::Clear()
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 boost::mutex::scoped_lock lock(mutex_);
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 if (queue_.empty())
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 return;
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 else
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 while (!queue_.empty())
93afa6134ab3 SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
202 {
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
203 std::unique_ptr<IDynamicObject> message(queue_.front());
2078
93afa6134ab3 SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
204 queue_.pop_front();
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
93afa6134ab3 SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
207 emptied_.notify_all();
93afa6134ab3 SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
208 }
93afa6134ab3 SharedMessageQueue::Clear
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
209 }
4169
e8005a58de16 SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
210
e8005a58de16 SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
211 size_t SharedMessageQueue::GetSize()
e8005a58de16 SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
212 {
e8005a58de16 SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
213 boost::mutex::scoped_lock lock(mutex_);
e8005a58de16 SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
214 return queue_.size();
e8005a58de16 SharedMessageQueue::GetSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
215 }
450
58b433bb9762 SharedMessageQueue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 }