home *** CD-ROM | disk | FTP | other *** search
- /*==============================================================================
- Module Name: ithread.hpp
-
- Description:
- Declaration of the class IThread; objects of this class manage
- OS/2 execution threads.
-
- Copyright: (C) Copyright IBM Corporation 1992
- All Rights Reserved
- Licensed Materials = Property of IBM
-
- Usage:
- This file is #include-d in code requiring access to the IThread
- functions.
-
- Notes:
-
- Related Topics:
-
- History:
- flag yymmdd who description
- ---- ------ --- -----------------------------------------------------------
- 920726 law Initial
- 920914 law Restructure code
- ==============================================================================*/
- #if !defined( _ITHREAD_ )
- #define _ITHREAD_
-
- #if !defined( _IBASETYP_ )
- #include <ibasetyp.hpp>
- #endif
-
- #if !defined( _IREFCNT_ )
- #include <irefcnt.hpp>
- #endif
-
- #if !defined( _IHANDLE_ )
- #include <ihandle.hpp>
- #endif
-
- /*------------------------------------------------------------------------------
- Class Name: IThreadFn
-
- Description:
- Abstract "thread function" class.
-
- Usage:
- Objects of this class are used to represent "functions" to be
- dispatched on secondary threads of execution when an IThread is
- started. These objects are reference counted to manage their
- destruction (after the thread has terminated); a reference to
- such an IThreadFn is passed to IThread::start().
-
- Typical usage is like:
- class MyFunction : public IThreadFn {
- public:
- virtual void
- run ( ) const
- {
- // Place your code here.
- }
- };
- ...
- IThread newThread( new MyFunction );
-
- See IThread::start for details on usage of this class.
- ------------------------------------------------------------------------------*/
- class IThreadFn : public IRefCounted {
- public:
- virtual void
- run ( ) const = 0;
- }; // class IThreadFn
-
- /*------------------------------------------------------------------------------
- Class Name: IThreadMemberFn
-
- Description:
- Template for IThreadMemberFn class (IThreadFn subclass to dispatch
- C++ member function against an object).
-
- Usage:
- Instances of this class are used to dispatch C++ member functions
- on a separate thread. The template argument is the class of the
- object for which the function is called. The constructor for such
- objects requires a reference to the object the member function is
- to be applied to, and, a pointer to the member function.
-
- The member function returns void and accepts no arguments. Support
- for other member functions can be added by subclassing this class.
-
- Typical usage:
- class MyClass {
- public:
- void job()
- {
- // Code to run on separate thread.
- }
- };
- ...
- MyClass myObj;
- IThread thread;
- thread.start( new IThreadMemberFn<MyClass>( myObj, MyClass::job ) );
- ------------------------------------------------------------------------------*/
- template < class T >
- class IThreadMemberFn : public IThreadFn {
- public:
- IThreadMemberFn ( T &obj,
- void ( T :: *mem )( ) )
- : member( mem ), object( obj )
- {
- }
- virtual void
- run ( ) const
- {
- (object.*member)();
- }
- protected:
- T
- &object;
- void ( T ::
- *member )( );
- }; // IThreadMemberFn
-
- /*------------------------------------------------------------------------------
- Class Name: IPriority
-
- Description:
- Thread "priority" class.
-
- Usage:
- This class defines the input and output of the IThread "priority"
- setting/querying member functions.
- ------------------------------------------------------------------------------*/
- class IPriority {
-
- public:
-
- typedef enum
- {
- noChange = 0,
- idleTime = 1,
- regular = 2,
- timeCritical = 3,
- foregroundServer = 4
- } Class;
-
- IPriority ( Class aClass,
- long aDelta = 0 )
- : c( aClass ), d( aDelta )
- {}
-
- IPriority ( long aDelta )
- : c( noChange ), d( aDelta )
- {}
-
- Class
- cls ( ) const
- {
- return this->c;
- }
-
- long
- delta ( ) const
- {
- return this->d;
- }
-
- protected:
-
- Class
- c;
-
- long
- d;
- }; // IPriority
-
- class IStartedThread;
-
- class ICurrentThread;
-
- /*------------------------------------------------------------------------------
- Class Name: IThread
-
- Description:
- Objects of this class represent threads of execution within the current
- program.
-
- Usage:
- This class provides support for implementing multi-threaded programs.
- IThread objects pr/ovide access to all the "tasking" APIs of the
- operating system. In addition, these objects serve as the anchor for
- thread-specific information (for example, the Presentation Manager
- anchor block).
-
- IThread objects are generally used in one of three ways:
-
- 1. To apply thread functions to the current thread. In most cases,
- these functions will be applied to the IThread object reference
- returned by the static member IThread::current.
-
- 2. To create additional threads of execution by instantiating new
- IThread objects and "starting" them.
-
- 3. To manipulate threads of execution initiated via alternate
- means and of which only the thread's ID is known.
-
- Example:
- // This is typical code appearing in main():
- IThread::current.initializePM(); // Initialize PM environment.
- ...// Create windows.
- IThread::current.processMsgs(); // Process window messages.
- IThread::current.terminatePM(); // Terminate PM environment.
-
- // This is typical code to spawn a new thread to execute
- // member function foo() against the "this" object:
- IThread thread;
- thread.start( IThreadMemberFn< MyClass >( this, foo ) );
-
- // This is typical code that operates on a thread with ID tid:
- IThread thread( tid );
- thread.suspend();
- ...
- thread.resume();
- ------------------------------------------------------------------------------*/
- class IThread {
-
- public:
-
- friend class ICurrentThread;
-
- typedef void ( _Optlink *OptlinkFnPtr ) ( void * );
- typedef void ( _System *SystemFnPtr ) ( unsigned long );
-
- /*---------------------------- Instance Creation -------------------------------
-
- There are 3 types of IThread constructors:
-
- 1. default
-
- The default ctor is used to build an IThread to be subsequently
- started via the start() member function.
-
- 2. with specification of code to run
-
- These are used to construct a new IThread and immediately
- dispatch it. These forms of the ctor are equivalent to
- constructing via the default ctor and then dispatching the
- thread via start().
-
- 3. from the thread ID of a previously started thread
-
- This ctor is used to provide IThread functionality to threads
- created via alternate means (either via _beginthread() or via
- DosCreateThread()).
-
- The second type permits specification of whether or not the new
- thread is to be a "PM" thread. If so, then initializePM is called
- automatically after the thread is started.
- ------------------------------------------------------------------------------*/
- IThread ( );
- IThread ( IThreadID anID );
- IThread ( const IReference<IThreadFn> &aFnObjRef,
- Boolean autoInitPM = IThread::defaultAutoInitPM() );
- IThread ( OptlinkFnPtr pfn,
- void *anArg,
- Boolean autoInitPM = IThread::defaultAutoInitPM() );
- IThread ( SystemFnPtr pfn,
- unsigned long anArg,
- Boolean autoInitPM = IThread::defaultAutoInitPM() );
-
- /*---------------------------- Instance Deletion -------------------------------
-
- IThread has a virtual destructor that takes care of deallocating
- resources. The destructor does not stop the thread!
- ------------------------------------------------------------------------------*/
- virtual
- ~IThread ( );
-
- /*------------------------------ Current Thread --------------------------------
-
- The static member "current" provides a reference to the object of class
- ICurrentThread that provides access to the current thread, and, provides
- additional functionality that can only be applied to the current thread.
- ------------------------------------------------------------------------------*/
- static ICurrentThread
- ¤t;
-
- /*------------------------------ Starting/Stopping -----------------------------
-
- These functions enable the starting and stopping of threads:
- start - start an asynchronous thread; threads can be started via
- any of three means:
- o with an arbitrary IThreadFn
- The IThreadFn is passed in via an IReference so that
- the IThreadFn can be deleted (if necessary) when the
- thread terminates.
- o with an Optlink function and void* argument
- This is to provide support for functions otherwise
- startable via _beginthread().
- o with a System function and unsigned long argument
- This is to provide support for functions otherwise
- startable via DosCreateThread().
- stop - stop the thread
- suspend - suspend the thread's execution
- resume - resume thread execution
- ------------------------------------------------------------------------------*/
- virtual void
- start ( const IReference<IThreadFn> &aFnObjRef,
- Boolean autoInitPM = IThread::defaultAutoInitPM() ),
- start ( OptlinkFnPtr pfn,
- void *anArg,
- Boolean autoInitPM = IThread::defaultAutoInitPM() ),
- start ( SystemFnPtr pfn,
- unsigned long anArg,
- Boolean autoInitPM = IThread::defaultAutoInitPM() ),
- stop ( ),
- suspend ( ),
- resume ( );
-
- /*-------------------------------- Accessing -----------------------------------
-
- These functions provide means of getting and setting the various thread
- attributes:
- id - obtain the ID of the thread (0 indicates the
- thread is not started)
- anchorBlock - obtain the anchor block handle for the
- thread (0 if not "initialized")
- msgQueue - obtain the message queue handle for the
- thread (0 if not "initialized")
- stackSize - obtain thread stack size (in bytes)
- setStackSize - set thread stack size (in bytes); if the thread
- is started, then this value will take effect
- only if the thread is stopped and restarted
- defaultStackSize - obtain the default stack size (the value used
- by default for new IThreads)
- setDefaultStackSize - set the default stack size
- autoInitPM - obtain the setting of the "automatically initialize
- PM" flag for this thread
- setAutoInitPM - set "automatically initialize PM" flag for this
- thread
- defaultAutoInitPM - obtain the default "auto initialize PM" flag
- setDefaultAutoInitPM- set the default "auto initialize PM" flag
- priority - obtain the priority of the thread (class/delta)
- setPriority - set the priority of the thread
- defaultPriority - obtain the default priority that will be given to
- new IThreads
- setDefaultPriority - set the default priority to be used for new
- IThreads
-
- The functions that get/set the "default" attribute values are all static
- members.
- ------------------------------------------------------------------------------*/
- virtual IThreadID
- id ( ) const;
-
- virtual IAnchorBlockHandle
- anchorBlock ( ) const;
-
- virtual IMsgQueueHandle
- msgQueue ( ) const;
-
- virtual unsigned long
- stackSize ( ) const;
-
- virtual void
- setStackSize ( unsigned long aSize );
-
- static unsigned long
- defaultStackSize ( );
-
- static void
- setDefaultStackSize ( unsigned long aSize );
-
- virtual Boolean
- autoInitPM ( ) const;
-
- virtual void
- setAutoInitPM ( Boolean initFlag );
-
- static Boolean
- defaultAutoInitPM ( );
-
- static void
- setDefaultAutoInitPM ( Boolean initFlag );
-
- virtual IPriority
- priority ( ) const;
-
- virtual void
- setPriority( IPriority aPriority );
-
- static IPriority
- defaultPriority ( );
-
- static void
- setDefaultPriority ( IPriority aPriority );
-
- protected:
-
- // Pointer to thread implementation object:
- IStartedThread
- *thread;
-
- // Static members (defaults):
- static unsigned long
- dfltStackSize;
-
- static Boolean
- dfltAutoInitPM;
-
- static IPriority
- dfltPriority;
- }; // IThread
-
- /*------------------------------------------------------------------------------
- Class Name: ICurrentThread
-
- Description:
- This class provides additional functions that can only be applied to
- the current thread of execution.
-
- Usage:
- A reference to the sole instance of this class is obtained via the
- static IThread::current member (and cannot be obtained otherwise).
-
- The resulting object is used to invoke the "thread" APIs that can
- only be applied to the current thread of execution. Essentially, these
- are the APIs that operate on a per-thread basis but do not accept
- a thread id as argument.
- ------------------------------------------------------------------------------*/
- class ICurrentThread : public IThread {
-
- friend class IThread;
-
- public:
-
- /*-------------------------------- PM Support ----------------------------------
-
- These functions provide support for performing windowing (PM) activities
- on the current thread:
-
- initializePM - set up PM environment (anchor block and msg queue)
- processMsgs - get and dispatch PM messages till WM_QUIT is received
- terminatePM - shut down PM environment
- ------------------------------------------------------------------------------*/
- virtual void
- initializePM ( long queueSize = 0 ),
- processMsgs ( ),
- terminatePM ( );
-
- /*-------------------------- Current Thread Function ---------------------------
-
- These functions enable thread operations only applicable to the current
- thread:
-
- sleep - suspend thread for given number of milliseconds
- exit - terminate current thread with given return value
- ------------------------------------------------------------------------------*/
- virtual void
- sleep ( unsigned long msecs ),
- exit ( unsigned long rc );
-
- /*--------------------------------- Accessing ----------------------------------
-
- id - return current thread's ID
- ------------------------------------------------------------------------------*/
- virtual IThreadID
- id ( ) const;
-
- protected:
-
- // Protected ctor to prevent object creation.
- ICurrentThread ( );
-
- // Protected static (and only) instance.
- static ICurrentThread
- current;
-
- }; // ICurrentThread
-
- #endif /* _ITHREAD_ */