DSPatch  v.2.42
C++ Cross-Platform, Object-Oriented, Flow-Based Programming Library
 All Classes Pages
DspComponentThread.cpp
1 /************************************************************************
2 DSPatch - Cross-Platform, Object-Oriented, Flow-Based Programming Library
3 Copyright (c) 2012-2013 Marcus Tomlinson
4 
5 This file is part of DSPatch.
6 
7 GNU Lesser General Public License Usage
8 This file may be used under the terms of the GNU Lesser General Public
9 License version 3.0 as published by the Free Software Foundation and
10 appearing in the file LGPLv3.txt included in the packaging of this
11 file. Please review the following information to ensure the GNU Lesser
12 General Public License version 3.0 requirements will be met:
13 http://www.gnu.org/copyleft/lgpl.html.
14 
15 Other Usage
16 Alternatively, this file may be used in accordance with the terms and
17 conditions contained in a signed written agreement between you and
18 Marcus Tomlinson.
19 
20 DSPatch is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 ************************************************************************/
24 
25 #include "DspComponentThread.h"
26 #include "DspComponent.h"
27 
28 //=================================================================================================
29 
30 DspComponentThread::DspComponentThread()
31 : _component( NULL ),
32  _stop( false ),
33  _pause( false ),
34  _stopped( true ) {}
35 
36 //-------------------------------------------------------------------------------------------------
37 
38 DspComponentThread::~DspComponentThread()
39 {
40  Stop();
41 }
42 
43 //=================================================================================================
44 
45 void DspComponentThread::Initialise( DspComponent* component )
46 {
47  _component = component;
48 }
49 
50 //-------------------------------------------------------------------------------------------------
51 
52 bool DspComponentThread::IsStopped() const
53 {
54  return _stopped;
55 }
56 
57 //-------------------------------------------------------------------------------------------------
58 
59 void DspComponentThread::Start( Priority priority )
60 {
61  if( _stopped )
62  {
63  _stop = false;
64  _stopped = false;
65  _pause = false;
66  DspThread::Start( priority );
67  }
68 }
69 
70 //-------------------------------------------------------------------------------------------------
71 
72 void DspComponentThread::Stop()
73 {
74  _stop = true;
75 
76  while( _stopped != true )
77  {
78  _pauseCondt.WakeAll();
79  _resumeCondt.WakeAll();
80  MsSleep( 1 );
81  }
82 
83  DspThread::Stop();
84 }
85 
86 //-------------------------------------------------------------------------------------------------
87 
88 void DspComponentThread::Pause()
89 {
90  _resumeMutex.Lock();
91 
92  _pause = true;
93  _pauseCondt.Wait( _resumeMutex ); // wait for resume
94  _pause = false;
95 
96  _resumeMutex.Unlock();
97 }
98 
99 //-------------------------------------------------------------------------------------------------
100 
101 void DspComponentThread::Resume()
102 {
103  _resumeMutex.Lock();
104  _resumeCondt.WakeAll();
105  _resumeMutex.Unlock();
106 }
107 
108 //=================================================================================================
109 
110 void DspComponentThread::_Run()
111 {
112  if( _component != NULL )
113  {
114  while( !_stop )
115  {
116  _component->Tick();
117  _component->Reset();
118 
119  if( _pause )
120  {
121  _resumeMutex.Lock();
122 
123  _pauseCondt.WakeAll();
124 
125  _resumeCondt.Wait( _resumeMutex ); // wait for resume
126 
127  _resumeMutex.Unlock();
128  }
129  }
130  }
131 
132  _stopped = true;
133 }
134 
135 //=================================================================================================