MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
workunit.cpp
Go to the documentation of this file.
1 // The DAGFrameScheduler is a Multi-Threaded lock free and wait free scheduling library.
2 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
3 /* This file is part of The DAGFrameScheduler.
4 
5  The DAGFrameScheduler is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  The DAGFrameScheduler is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with The DAGFrameScheduler. If not, see <http://www.gnu.org/licenses/>.
17 */
18 /* The original authors have included a copy of the license specified above in the
19  'doc' folder. See 'gpl.txt'
20 */
21 /* We welcome the use of the DAGFrameScheduler to anyone, including companies who wish to
22  Build professional software and charge for their product.
23 
24  However there are some practical restrictions, so if your project involves
25  any of the following you should contact us and we will try to work something
26  out:
27  - DRM or Copy Protection of any kind(except Copyrights)
28  - Software Patents You Do Not Wish to Freely License
29  - Any Kind of Linking to Non-GPL licensed Works
30  - Are Currently In Violation of Another Copyright Holder's GPL License
31  - If You want to change our code and not add a few hundred MB of stuff to
32  your distribution
33 
34  These and other limitations could cause serious legal problems if you ignore
35  them, so it is best to simply contact us or the Free Software Foundation, if
36  you have any questions.
37 
38  Joseph Toppi - toppij@gmail.com
39  John Blackwood - makoenergy02@gmail.com
40 */
41 #ifndef _workunit_cpp
42 #define _workunit_cpp
43 
44 /// @file
45 /// @brief This file has the implementation of the @ref Mezzanine::Threading::DefaultWorkUnit.
46 
47 #include "workunit.h"
48 #include "systemcalls.h"
49 #include "atomicoperations.h"
50 #include "doublebufferedresource.h"
51 
52 #ifdef MEZZ_DEBUG
53 #include <cassert>
54 #endif
55 
56 namespace Mezzanine
57 {
58  namespace Threading
59  {
60  /////////////////////////////////////////////////////////////////////////////////////////////
61  // The Simple Stuff
62 
63  DefaultWorkUnit::DefaultWorkUnit(const DefaultWorkUnit&)
64  {}
65 
66  DefaultWorkUnit& DefaultWorkUnit::operator=(DefaultWorkUnit& Unused)
67  { return Unused; }
68 
70  {}
71 
73  {}
74 
75  /////////////////////////////////////////////////////////////////////////////////////////////
76  // Work with the dependents as in what must not start until this finishes.
77 
79  { return SchedulerToCount.GetDependentCountOf(this); }
80 
81  /////////////////////////////////////////////////////////////////////////////////////////////
82  // Work with the dependencies as in what must finish before we can run this work unit.
83 
85  { return Dependencies.at(Index); }
86 
88  { return Dependencies.size(); }
89 
91  {
92  Whole Results = Dependencies.size();
93  for(std::vector<iWorkUnit*>::const_iterator Iter=Dependencies.begin(); Iter!=Dependencies.end(); ++Iter)
94  { Results += (*Iter)->GetDependencyCount(); }
95  return Results;
96 
97  }
98 
100  { Dependencies.push_back(NewDependency); }
101 
103  {
104  Dependencies.erase(
105  std::remove(Dependencies.begin(),Dependencies.end(),RemoveDependency),
106  Dependencies.end()
107  );
108  }
109 
111  { Dependencies.clear(); }
112 
114  {
115  for (std::vector<iWorkUnit*>::iterator Iter = Dependencies.begin(); Iter!=Dependencies.end(); ++Iter)
116  {
117  if( Complete != (*Iter)->GetRunningState() )
118  { return false; }
119  }
120  return true;
121  }
122 
123  /////////////////////////////////////////////////////////////////////////////////////////////
124  // Work with the ownership and RunningState
126  {
128  { return NotStarted; }
129 
131  { return Starting; } // This is the only place a starting should be generated, and it is never placed in CurrentRunningState
132 
133  return NotStarted;
134  }
135 
137  { return (RunningState)CurrentRunningState; } // This only works because we set all of in RunningState to be unsigned.
138 
140  //{ while(CurrentRunningState!=AtomicCompareAndSwap(&CurrentRunningState,CurrentRunningState,NotStarted)); }
142 
143  /////////////////////////////////////////////////////////////////////////////////////////////
144  // Work with the performance log
146  { return PerformanceLog.GetAverage(); }
147 
149  { return PerformanceLog; }
150 
151  /////////////////////////////////////////////////////////////////////////////////////////////
152  // Deciding when to and doing the work
153 
155  {
157 
158  std::ostream& Out = CurrentThreadStorage.GetUsableLogger();
159  Out << "<WorkUnit id=\"" << std::hex << this << std::dec << "\">" << std::endl;
160  #ifdef MEZZ_DEBUG
161  Out << "<WorkUnitStart BeginTimeStamp=\"" << Begin << "\" ThreadID=\"" << Mezzanine::Threading::this_thread::get_id() << "\" />" << std::endl;
162  #endif
163 
164  this->DoWork(CurrentThreadStorage);
166  this->GetPerformanceLog().Insert( Whole(End-Begin)); // A whole is usually a 32 bit type, which is fine unless a single workunit runs for 35 minutes.
168 
169  #ifdef MEZZ_DEBUG
170  Out << "<WorkUnitEnd EndTimeStamp=\"" << End << "\" Duration=\"" << (End-Begin) << "\" DurationStored=\"" << Whole(End-Begin) << "\" />" << std::endl;
171  #endif
172  Out << "</WorkUnit>" << std::endl;
173  }
174 
176  { return WorkUnitKey( this->GetDependentCount(SchedulerToCount), GetPerformanceLog().GetAverage(), this); }
177 
178  }//Threading
179 }//Mezzanine
180 
181 #endif