/* $Revision: 17 $ $Date: 9/10/03 15:44 $ Copyright © 1997-2002, Obsideon Limited. Contact "http://www.obsideon.com". */ #ifndef FOST_TASK_HPP #define FOST_TASK_HPP #pragma once #include #include #ifdef F3UTIL_EXPORTS #define FOST_TASK_HPP_ARCHIVE L"$Archive: /FOST.3/INCLUDE/FOST.task.hpp $" #define FOST_TASK_HPP_REVISION L"$Revision: 17 $" #define FOST_TASK_HPP_DATE L"$Date: 9/10/03 15:44 $" #endif namespace FSLib { class Thread; class F3UTIL_DECLSPEC Task { public: inline virtual ~Task() throw (); void start(); inline void stop(); inline bool completed(); inline void wait(); inline bool wait( const FSLib::TimePeriod &howLong ); Period< void > timing() const; Nullable< FSLib::wstring > fault() const; Nullable< TimePeriod > kernel() const; Nullable< TimePeriod > user() const; protected: inline Task(); virtual void process() = 0; volatile bool m_stop; private: boost::shared_ptr< Thread > m_thread; mutable ExclusiveWrite m_attributes; Period< void > m_timing; Nullable< FSLib::wstring > m_fault; Nullable< TimePeriod > m_kernel; Nullable< TimePeriod > m_user; static unsigned int __cdecl loader( void *self ); }; template< typename F > class FunctorTask : public Task { public: typedef boost::shared_ptr< Task > t_ptask; typedef boost::shared_ptr< F > t_pfunctor; typedef std::pair< t_ptask, t_pfunctor > t_execute; inline FunctorTask( t_pfunctor ); protected: F &m_functor; private: boost::shared_ptr< F > m_f; }; template< typename F > class Iterations : public FunctorTask< F > { public: inline Iterations( const t_pfunctor &, int i ); private: int m_iterations; void process(); }; template< typename F > typename Iterations< F >::t_execute execute( typename Iterations< F >::t_pfunctor f, int iterations ) { typename Iterations< F >::t_execute ret( typename Iterations< F >::t_ptask( new typename Iterations< F >( f, iterations ) ), f ); ret.first->start(); return ret; } template< typename F > class Functor : public FunctorTask< F > { public: inline Functor( const t_pfunctor & ); private: void process(); }; template< typename F > typename Functor< F >::t_execute execute( typename Functor< F >::t_pfunctor f ) { typename Functor< F >::t_execute ret( typename Functor< F >::t_ptask( new typename Functor< F >( f ) ), f ); ret.first->start(); return ret; } } /* FSLib::Process::FunctorTask<> */ template< typename F > inline FSLib::FunctorTask< F >::FunctorTask( boost::shared_ptr< F > f ) : m_f( f ), m_functor( *f ) { } /* FSLib::Process::Iterations<> */ template< typename F > inline FSLib::Iterations< F >::Iterations( const t_pfunctor &f, int i ) : FunctorTask< F >( f ), m_iterations( i ) { } template< typename F > inline void FSLib::Iterations< F >::process() { for ( int a( 0 ); a != m_iterations && !m_stop; ++a ) { m_functor( a ); } } /* FSLib::Process::Functor<> */ template< typename F > inline FSLib::Functor< F >::Functor( const t_pfunctor &f ) : FunctorTask< F >( f ) { } template< typename F > inline void FSLib::Functor< F >::process() { m_functor(); } #endif // FOST_TASK_HPP /* $History: FOST.task.hpp $ * * ***************** Version 17 ***************** * User: Kirit Date: 9/10/03 Time: 15:44 * Updated in $/FOST.3/INCLUDE * Headers re-arranged to give a proper SDK feel and to make determination * of required headers simpler. * * ***************** Version 16 ***************** * User: Kirit Date: 9/10/03 Time: 15:13 * Updated in $/FOST.3/F3Util * Check in ahead of change to FOST.task.hpp * * ***************** Version 14 ***************** * User: Kirit Date: 3/10/03 Time: 18:44 * Updated in $/FOST.3/F3Util * * ***************** Version 13 ***************** * User: Kirit Date: 7/05/02 Time: 12:01 * Updated in $/FOST/Cpp/FSCppUtil * Wrapped std::basic_string<> in order to change the copy semantics to * stop the problems with using C++ in the COM layer for IIS. * * ***************** Version 12 ***************** * User: Kirit Date: 19/03/02 Time: 20:54 * Updated in $/FOST/Cpp/FSCppUtil * Corrected history from last check-in. * Updated TestAX.IDL to remove registry entries from removed classes. * * ***************** Version 11 ***************** * User: Kirit Date: 19/03/02 Time: 19:48 * Updated in $/FOST/Cpp/FSCppUtil * Debug compiles now all end in '_d'. * The C++ implementation names (Type.cppName) have been changed to also * include the DLL that the implementation is in as per the documentation. * In all source files the following have been done: * * Ensured that every header file has a revision object. * * Ensured that every translation unit has a revision object. * * Ensured that every source file (which understands comments) * has a history section. * * Changed all copyright notices to be for Obsideon Ltd. * * Changed copyright notices to use the longest possible timeframe. * * ***************** Version 10 ***************** * User: Kirit Date: 20/02/02 Time: 0:51 * Updated in $/FOST/Cpp/FSCppUtil * Destructor behavour under exceptions should now be sorted. * * ***************** Version 9 ***************** * User: Kirit Date: 11/02/02 Time: 16:18 * Updated in $/FOST/Cpp/FSCppUtil * Small changes to reflect the updated copy semantics of the * boost::shared_ptr<>. * * ***************** Version 8 ***************** * User: Kirit Date: 25/12/01 Time: 19:12 * Updated in $/FOST/Cpp/FSCppUtil * Some debugging of and extension of TimePeriod. * Added new reporting to Task and sorted out synchronised access to its * data members. * * ***************** Version 7 ***************** * User: Kirit Date: 4/11/01 Time: 3:42 * Updated in $/FOST/Cpp/FSCppUtil * Updates to support Mutex lock testing. * Header revision information now captured. * Periods<> container updated to better support error reporting and fault * finding. * * ***************** Version 6 ***************** * User: Kirit Date: 23/10/01 Time: 22:49 * Updated in $/FOST/FSCppUtil * Added new wait() member to thread so that controlling threads can * specify a timeout for waiting for a thread to complete. * * ***************** Version 5 ***************** * User: Kirit Date: 20/07/01 Time: 12:05 * Updated in $/FOST/FSCppUtil * FostCache is now per-thread. * Threading support for generic functor tasks. * * ***************** Version 4 ***************** * User: Kirit Date: 19/07/01 Time: 17:33 * Updated in $/FOST/FSCppUtil * Improved thread control. * ID pre-assignment now works correctly even under heavy load. * * ***************** Version 3 ***************** * User: Kirit Date: 19/07/01 Time: 15:08 * Updated in $/FOST/FSCppUtil * ID pre-assignment still issues the occasional ID multiple times under * heavy load. * Threading support now handles exceptions more properly. * * ***************** Version 2 ***************** * User: Kirit Date: 19/07/01 Time: 14:02 * Updated in $/FOST/FSCppUtil * First (crude) implementation of multi-threading support. * * ***************** Version 1 ***************** * User: Kirit Date: 18/07/01 Time: 17:37 * Created in $/FOST/FSCppUtil */