Spinning Topp Logo BlackTopp Studios
inc
asynchronousfileloadingworkunit.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 - 2016 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 _asynchronousfileloadingworkunit_cpp
42 #define _asynchronousfileloadingworkunit_cpp
43 
45 #include "atomicoperations.h"
46 
47 #include <iterator>
48 
49 /// @file
50 /// @brief The implementation of the @ref Mezzanine::Threading::AsynchronousFileLoadWorkUnit "AsynchronousFileLoadWorkUnit" a workunit that loads a listing of files asynchronously.
51 
52 namespace Mezzanine
53 {
54  namespace Threading
55  {
56  void ThreadLoading(void* WU)
57  {
59  try{
60  Unit.FilesRaw.reserve(Unit.Filenames.size());
61  }catch (std::exception&){
62  while(Unit.Status!=AtomicCompareAndSwap32(&(Unit.Status), Unit.Status, Failed));
63  return;
64  }
65  for(std::vector<String>::iterator CurrentFileName = Unit.Filenames.begin(); CurrentFileName!=Unit.Filenames.end(); CurrentFileName++)
66  {
67  std::ifstream File(CurrentFileName->c_str(), std::ios::binary | std::ios::ate);
68  if (File)
69  {
70  MaxInt FileLength = File.tellg();
71  File.seekg(0,std::ios::beg);
72 
73  RawFile* Loading;
74  try{
75  Loading = new RawFile(FileLength);
76  }catch (std::exception&){
77  while(Unit.Status!=AtomicCompareAndSwap32(&(Unit.Status), Unit.Status, Failed));
78  return;
79  }
80 
81  File.read((char*)Loading->Data, FileLength);
82  Unit.FilesRaw.push_back(Loading);
83  }else{
84  while(Unit.Status!=AtomicCompareAndSwap32(&(Unit.Status), Unit.Status, Failed));
85  break;
86  }
87 
88  }
89  while(Unit.Status!=AtomicCompareAndSwap32(&(Unit.Status), Unit.Status, Complete));
90  }
91 
93  LoadingThread(0),
94  Status(NotStarted)
95  {}
96 
98  { DeleteLoadedFiles(); }
99 
100  RunningState AsynchronousFileLoadWorkUnit::BeginLoading(const std::vector<String>& Filenames_)
101  {
103  {
104  //DeleteLoadedFiles();
105  FilesRaw.clear();
106  Filenames = Filenames_;
107  LoadingThread = new Thread(ThreadLoading, this);
108  return Starting;
109  }else{
110  //throw?
111  return (RunningState)Status;
112  }
113  }
114 
116  {
117  if(Complete==Status&&0!=LoadingThread)
118  {
119  LoadingThread->join();
120  delete LoadingThread;
121  LoadingThread=0;
122  }
123  }
124 
126  { return (RunningState)Status; }
127 
129  {
130  Whole Index=0;
131  for(std::vector<String>::const_iterator CurrentFileName = Filenames.begin();
132  CurrentFileName!=Filenames.end();
133  CurrentFileName++, Index++)
134  {
135  if(*CurrentFileName==FileName)
136  { return GetFile(Index); }
137  }
138  return 0;
139  }
140 
142  {
143  if(FilesRaw.size()<=Index)
144  { return 0; }
145  return FilesRaw[Index];
146  }
147 
149  {
150  if(LoadingThread)
151  { delete LoadingThread; }
152  for(RawFileContainer::iterator Iter=FilesRaw.begin(); Iter!=FilesRaw.end(); ++Iter)
153  { delete *Iter; }
154  }
155  } // \Threading
156 } // \Mezzanine
157 #endif
UInt8 * Data
A pointer to a block of memory as it was loaded raw from the file.
void ThreadLoading(void *WU)
An Internal helper function for the AsynchronousFileLoadWorkUnit. This is the function that Asynchron...
RunningState
Used to track whether a thread has started, completed, etc...
RawFileContainer FilesRaw
The contents of the files once they have been loaded. No partial files will exists here...
The declaration of the AsynchronousFileLoadWorkUnit a workunit that loads a listing of files asynchro...
virtual void DoWork(DefaultThreadSpecificStorage::Type &)
This checks if Asynchronous loading thread has completed and if so it cleans up that threads' resourc...
Indicates an abnormal termination of a Workunit or other failure, Likely the whole application will n...
Only used when a thread successfully attempts to gain ownership of a task, or some other tasks succes...
A thread specific collection of double-buffered and algorithm specific resources. ...
A small wrapper around the system thread.
Definition: thread.h:94
A simple in memory representation of a file.
Int32 AtomicCompareAndSwap32(Int32 *VariableToChange, const Int32 &OldValue, const Int32 &NewValue)
Atomically Compares And Swaps a 32 bit value.
Task is running when the value was check, it could become Complete or Failed with no notice...
std::vector< String > Filenames
The names of the files this batch of loading will retrieve.
RawFile * GetFile(const String &FileName) const
Get a loaded RawFile in linear time.
virtual ~AsynchronousFileLoadWorkUnit()
Destructor, deletes all loaded files.
This is intended to load files asynchronously and continue doing so whether or not other the FrameSch...
void DeleteLoadedFiles()
This deletes all the loaded files from the last call of BeginLoading .
virtual RunningState IsWorkDone()
Get the RunningState of the file loading.
RunningState BeginLoading(const std::vector< String > &Filenames_)
Begin loading a list of files based on their names.
Task is not yet started this frame, this can change without notice.
Simple thread safe ways to check and change a specified variable atomically.
long long MaxInt
A large integer type suitable for compile time math and long term microsecond time keeping...
Definition: datatypes.h:190
Thread has completed all work this from frame, will not change until this frame ends.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Thread * LoadingThread
This is either 0 or the other thread that is loading.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
void join()
Wait for the thread to finish (join execution flows).