TuttleOFX  1
ThreadEnv.hpp
Go to the documentation of this file.
00001 #ifndef _TUTTLE_HOST_CORE_THREADENV_HPP_
00002 #define _TUTTLE_HOST_CORE_THREADENV_HPP_
00003 
00004 #include <tuttle/host/NodeListArg.hpp>
00005 #include <tuttle/host/ComputeOptions.hpp>
00006 #include <tuttle/host/memory/MemoryCache.hpp>
00007 
00008 #include <boost/thread/thread.hpp>
00009 #include <boost/thread/recursive_mutex.hpp>
00010 #include <boost/thread/tss.hpp>
00011 #include <boost/atomic.hpp>
00012 #include <boost/bind.hpp>
00013 
00014 #include <boost/signals2.hpp>
00015 
00016 
00017 namespace tuttle {
00018 namespace host {
00019 
00020 class Graph;
00021 
00022 class ThreadEnv
00023 {
00024 public:
00025         typedef ThreadEnv This;
00026         typedef boost::signals2::signal<void ()> SignalType;
00027         
00028         ThreadEnv( const bool asynchronous = true )
00029         : _asynchronous( asynchronous )
00030         , _result(false)
00031         {
00032                 _options.setReturnBuffers(false);
00033         }
00034         
00035         ComputeOptions& getComputeOptions() { return _options; }
00036         const ComputeOptions& getComputeOptions() const { return _options; }
00037         
00038         This& setAsynchronous( const bool v = true )
00039         {
00040                 _asynchronous = v;
00041                 return *this;
00042         }
00043         
00044         /// @brief Main functions
00045         /// @{
00046         
00047         /**
00048          * @brief Launch the graph computation in a synchrone or asynchrone way.
00049          */
00050         void compute( Graph& graph, const NodeListArg& nodes = NodeListArg() );
00051         
00052         /**
00053          * @brief The application would like to abort the process (from another thread).
00054          */
00055         void abort() { _options.abort(); }
00056         
00057         void join() { _thread.join(); }
00058         
00059         bool getResult() const { return _result.load( boost::memory_order_relaxed ); }
00060         
00061         SignalType& getSignalEnd() { return _signalEnd; }
00062         /// @}
00063         
00064 private:
00065         static void runProcessFunc( ThreadEnv* threadEnv, Graph* graph, const std::list<std::string>& nodes, const ComputeOptions* const options );
00066         
00067         void setResult( const bool res ) { _result.store( res, boost::memory_order_relaxed ); }
00068         
00069 private:
00070         boost::thread _thread;
00071         
00072         
00073         bool _asynchronous;
00074         memory::MemoryCache _memoryCache;
00075         ComputeOptions _options;
00076         
00077         boost::atomic_bool _result;
00078         
00079         SignalType _signalEnd;
00080 };
00081 
00082 }
00083 }
00084 
00085 #endif