DSPatch  v.2.42
C++ Cross-Platform, Object-Oriented, Flow-Based Programming Library
 All Classes Pages
DspCircuitThread.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 "DspCircuitThread.h"
26 #include "DspComponent.h"
27 
28 //=================================================================================================
29 
30 DspCircuitThread::DspCircuitThread()
31 : _components( NULL ),
32  _threadNo( 0 ),
33  _stop( false ),
34  _stopped( true ),
35  _gotResume( false ),
36  _gotSync( false ) {}
37 
38 //-------------------------------------------------------------------------------------------------
39 
40 DspCircuitThread::~DspCircuitThread()
41 {
42  Stop();
43 }
44 
45 //=================================================================================================
46 
47 void DspCircuitThread::Initialise( std::vector< DspComponent* >* components, unsigned short threadNo )
48 {
49  _components = components;
50  _threadNo = threadNo;
51 }
52 
53 //-------------------------------------------------------------------------------------------------
54 
55 void DspCircuitThread::Start( Priority priority )
56 {
57  if( _stopped )
58  {
59  _stop = false;
60  _stopped = false;
61  _gotResume = false;
62  _gotSync = true;
63  DspThread::Start( priority );
64  }
65 }
66 
67 //-------------------------------------------------------------------------------------------------
68 
69 void DspCircuitThread::Stop()
70 {
71  _stop = true;
72 
73  while( _stopped != true )
74  {
75  _syncCondt.WakeAll();
76  _resumeCondt.WakeAll();
77  MsSleep( 1 );
78  }
79 
80  DspThread::Stop();
81 }
82 
83 //-------------------------------------------------------------------------------------------------
84 
85 void DspCircuitThread::Sync()
86 {
87  _resumeMutex.Lock();
88 
89  if( !_gotSync ) // if haven't already got sync
90  {
91  _syncCondt.Wait( _resumeMutex ); // wait for sync
92  }
93 
94  _resumeMutex.Unlock();
95 }
96 
97 //-------------------------------------------------------------------------------------------------
98 
99 void DspCircuitThread::Resume()
100 {
101  _resumeMutex.Lock();
102 
103  _gotSync = false; // reset the sync flag
104 
105  _gotResume = true; // set the resume flag
106  _resumeCondt.WakeAll();
107 
108  _resumeMutex.Unlock();
109 }
110 
111 //=================================================================================================
112 
113 void DspCircuitThread::_Run()
114 {
115  if( _components != NULL )
116  {
117  while( !_stop )
118  {
119  _resumeMutex.Lock();
120 
121  _gotSync = true; // set the sync flag
122 
123  _syncCondt.WakeAll();
124 
125  if( !_gotResume ) // if haven't already got resume
126  {
127  _resumeCondt.Wait( _resumeMutex ); // wait for resume
128  }
129  _gotResume = false; // reset the resume flag
130 
131  _resumeMutex.Unlock();
132 
133  if( !_stop )
134  {
135  for( unsigned short i = 0; i < _components->size(); i++ )
136  {
137  ( *_components )[i]->ThreadTick( _threadNo );
138  }
139  for( unsigned short i = 0; i < _components->size(); i++ )
140  {
141  ( *_components )[i]->ThreadReset( _threadNo );
142  }
143  }
144  }
145  }
146 
147  _stopped = true;
148 }
149 
150 //=================================================================================================