4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "Thread/SuspensibleThread.hpp"
29 SuspensibleThread::Start(bool _suspended
)
32 stop_received
= false;
33 suspend_received
= _suspended
;
37 suspend_trigger
.Signal();
39 suspend_trigger
.Reset();
41 command_trigger
.Reset();
45 return Thread::Start();
49 SuspensibleThread::BeginStop()
54 command_trigger
.Signal();
57 stop_trigger
.Signal();
58 command_trigger
.Signal();
63 SuspensibleThread::BeginSuspend()
65 assert(!Thread::IsInside());
66 assert(Thread::IsDefined());
70 suspend_received
= true;
71 command_trigger
.Signal();
74 suspend_trigger
.Signal();
75 command_trigger
.Signal();
80 SuspensibleThread::WaitUntilSuspended()
82 assert(!Thread::IsInside());
83 assert(Thread::IsDefined());
87 assert(suspend_received
);
90 client_trigger
.Wait(mutex
);
93 assert(suspend_trigger
.Test());
100 SuspensibleThread::Suspend()
103 WaitUntilSuspended();
107 SuspensibleThread::Resume()
111 suspend_received
= false;
112 command_trigger
.Signal();
115 suspend_trigger
.Reset();
116 command_trigger
.Signal();
121 SuspensibleThread::IsCommandPending()
123 assert(Thread::IsInside());
127 bool result
= stop_received
|| suspend_received
;
131 return stop_trigger
.Test() || suspend_trigger
.Test();
136 SuspensibleThread::CheckStoppedOrSuspended()
138 assert(Thread::IsInside());
145 if (!stop_received
&& suspend_received
) {
147 client_trigger
.Signal();
148 while (!stop_received
&& suspend_received
)
149 command_trigger
.Wait(mutex
);
153 bool stop
= stop_received
;
157 if (stop_trigger
.Test())
160 if (suspend_trigger
.Test()) {
162 while (suspend_trigger
.Test() && !stop_trigger
.Test())
163 command_trigger
.WaitAndReset();
168 return stop_trigger
.Test();
173 SuspensibleThread::WaitForStopped(unsigned timeout_ms
)
175 assert(Thread::IsInside());
184 command_trigger
.Wait(mutex
, timeout_ms
);
186 if (!stop_received
&& suspend_received
) {
187 client_trigger
.Signal();
188 while (!stop_received
&& suspend_received
)
189 command_trigger
.Wait(mutex
);
193 bool stop
= stop_received
;
197 if (stop_trigger
.Test())
202 command_trigger
.WaitAndReset(timeout_ms
);
203 while (suspend_trigger
.Test() && !stop_trigger
.Test())
204 command_trigger
.WaitAndReset();
208 return stop_trigger
.Test();