home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / include / ithread.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  10.8 KB  |  417 lines

  1. #ifndef _ITHREAD_
  2. #define _ITHREAD_
  3. /*******************************************************************************
  4. * FILE NAME: ithread.hpp                                                       *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the classes:                                                *
  8. *     IThread                                                                  *
  9. *     IThread::Cursor                                                          *
  10. *     IThreadFn                                                                *
  11. *     IThreadMemberFn                                                          *
  12. *     ICurrentThread                                                           *
  13. *                                                                              *
  14. * COPYRIGHT:                                                                   *
  15. *   IBM Open Class Library                                                     *
  16. *   (C) Copyright International Business Machines Corporation 1992, 1996       *
  17. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  18. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  19. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  20. *                                                                              *
  21. *******************************************************************************/
  22. #include <ivbase.hpp>
  23. #include <iapp.hpp>
  24. #include <ihandle.hpp>
  25. #include <irefcnt.hpp>
  26.  
  27. struct tib_s;
  28. class ICurrentThread;
  29. class ICurrentThreadData;
  30. class IDialogControls;
  31. class IRelatedHandlesList;
  32. class IStartedThread;
  33. class IThreadCursorData;
  34. class IThreadData;
  35. class IWindowList;
  36.  
  37. #pragma pack(4)
  38.  
  39. class IThreadFn : public IRefCounted {
  40. typedef IRefCounted
  41.   Inherited;
  42. public:
  43. /*------------------------------- Constructors -------------------------------*/
  44.   IThreadFn ( );
  45. virtual
  46.  ~IThreadFn ( );
  47.  
  48. /*------------------------------- Run Function -------------------------------*/
  49. virtual void
  50.   run ( ) = 0;
  51.  
  52. private:
  53. /*------------------------------ Hidden Members ------------------------------*/
  54.   IThreadFn ( const IThreadFn& function );
  55. IThreadFn
  56.  &operator= ( const IThreadFn& function );
  57. }; // class IThreadFn
  58.  
  59.  
  60. template < class T >
  61. class IThreadMemberFn : public IThreadFn {
  62. typedef IThreadFn
  63.   Inherited;
  64. public:
  65. /*------------------------------- Constructors -------------------------------*/
  66. IThreadMemberFn ( T &obj,
  67.                   void ( T :: *mem )( ) )
  68.   : member( mem ), object( obj )
  69.   { }
  70. virtual
  71.  ~IThreadMemberFn ( )
  72.   { }
  73.  
  74. /*------------------------------- Run Function -------------------------------*/
  75. virtual void
  76.   run ( )
  77.   {
  78.     (object.*member)();
  79.   }
  80.  
  81. private:
  82. /*------------------------------ Hidden Members ------------------------------*/
  83.   IThreadMemberFn ( const IThreadMemberFn<T>& threadFunction );
  84. IThreadMemberFn<T>
  85.  &operator=       ( const IThreadMemberFn<T>& threadFunction );
  86.  
  87. /*--------------------------------- Private ----------------------------------*/
  88. T
  89.  &object;
  90. void ( T ::
  91.  *member )( );
  92. }; // IThreadMemberFn
  93.  
  94.  
  95. class IThread : public IVBase {
  96. typedef IVBase
  97.   Inherited;
  98. public:
  99. /*------------------------------ Function Types ------------------------------*/
  100. typedef void ( _Optlink* OptlinkFnPtr ) ( void* );
  101. typedef void ( _System*  SystemFnPtr  ) ( unsigned long );
  102.  
  103. /*------------------------------- Constructors -------------------------------*/
  104.   IThread ( );
  105.   IThread ( const IThread&       thread );
  106.   IThread ( const IThreadId&     threadID,
  107.             const IThreadHandle& threadHandle = IThreadHandle::noHandle );
  108.  
  109.   IThread ( const IReference<IThreadFn>& threadFunction,
  110.             Boolean              autoInitGUI = defaultAutoInitGUI() );
  111.   IThread ( OptlinkFnPtr         function,
  112.             void*                functionArgument,
  113.             Boolean              autoInitGUI = defaultAutoInitGUI() );
  114.   IThread ( SystemFnPtr          function,
  115.             unsigned long        functionArgument,
  116.             Boolean              autoInitGUI = defaultAutoInitGUI() );
  117.  
  118. virtual
  119.  ~IThread ( );
  120.  
  121. /*---------------------- Starting and Stopping Threads -----------------------*/
  122. void
  123.   start   ( const IReference<IThreadFn>& threadFunction,
  124.             Boolean        autoInitGUI = defaultAutoInitGUI() ),
  125.   start   ( OptlinkFnPtr   function,
  126.             void*          functionArgument,
  127.             Boolean        autoInitGUI = defaultAutoInitGUI() ),
  128.   start   ( SystemFnPtr    function,
  129.             unsigned long  functionArgument,
  130.             Boolean        autoInitGUI = defaultAutoInitGUI() );
  131.  
  132. virtual void
  133.   stop    ( );
  134.  
  135. virtual void
  136.   suspend ( ),
  137.   resume  ( );
  138.  
  139. /*----------------------------- Thread Information ---------------------------*/
  140. static ICurrentThread
  141.  ¤t     ( );
  142.  
  143. virtual IThreadId
  144.   id          ( ) const;
  145.  
  146. static IThreadId
  147.   currentId   ( );
  148.  
  149. virtual IThreadHandle
  150.   handle      ( ) const;
  151.  
  152. static IThreadHandle
  153.   currentHandle ( );
  154.  
  155. IString
  156.   variable    ( const IString& key ) const;
  157.  
  158. IThread
  159.  &setVariable ( const IString& key,
  160.                 const IString& value );
  161.  
  162. virtual Boolean
  163.   isStarted   ( ) const;
  164.  
  165. /*-------------------------------- Diagnostics -------------------------------*/
  166. virtual IString
  167.   asString    ( ) const,
  168.   asDebugInfo ( ) const;
  169.  
  170. /*-------------------- Graphical User Interface (GUI) Support ----------------*/
  171. virtual Boolean
  172.   autoInitGUI           ( ) const;
  173.  
  174. virtual IThread
  175.  &setAutoInitGUI        ( Boolean initFlag = true );
  176.  
  177. static Boolean
  178.   defaultAutoInitGUI    ( );
  179.  
  180. static void
  181.   setDefaultAutoInitGUI ( Boolean initFlag = true );
  182.  
  183. virtual IThread
  184.  &stopProcessingMsgs    ( );
  185.  
  186. /*-------------------------------- Stack Size --------------------------------*/
  187. virtual unsigned long
  188.   stackSize           ( ) const;
  189.  
  190. virtual IThread
  191.  &setStackSize        ( unsigned long size );
  192.  
  193. static unsigned long
  194.   defaultStackSize    ( );
  195.  
  196. static void
  197.   setDefaultStackSize ( unsigned long size );
  198.  
  199. /*---------------------------- Message Queue ---------------------------------*/
  200. #ifndef IC_MOTIFWIN_FLAGNOP
  201. virtual long
  202.   queueSize           ( ) const;
  203.  
  204. virtual IThread
  205.  &setQueueSize        ( long queueSize );
  206.  
  207. static long
  208.   defaultQueueSize    ( );
  209.  
  210. static void
  211.   setDefaultQueueSize ( long queueSize );
  212. #endif // not IC_MOTIFWIN_FLAGNOP
  213.  
  214. IMessageQueueHandle
  215.   messageQueue        ( ),
  216.   messageQueue        ( ) const;
  217.  
  218. /*----------------------------- Thread Priority ------------------------------*/
  219. virtual unsigned
  220.   priorityLevel  ( ) const;
  221.  
  222. virtual IApplication::PriorityClass
  223.   priorityClass  ( ) const;
  224.  
  225. virtual IThread
  226.  &setPriority    ( IApplication::PriorityClass priority,
  227.                    unsigned                    level ),
  228.  &adjustPriority ( int delta );
  229.  
  230. /*------------------------------ Implementation ------------------------------*/
  231. IWindowList
  232.  *windowList    ( ) const;
  233.  
  234. IThread
  235.  &setWindowList ( IWindowList* list );
  236.  
  237.  
  238. IDialogControls
  239.   *dialogControls ( ) const;
  240.  
  241. class Cursor : public IVBase {
  242. typedef IVBase
  243.   Inherited;
  244. public:
  245. /*------------------------------- Constructors -------------------------------*/
  246.   Cursor ( Boolean allThreads = true );
  247. virtual
  248.  ~Cursor ( );
  249.  
  250. /*----------------------------- Thread Iteration -----------------------------*/
  251. Boolean
  252.   setToFirst ( ),
  253.   setToNext  ( ),
  254.   isValid    ( ) const;
  255.  
  256. virtual void
  257.   invalidate ( );
  258.  
  259. IThreadId
  260.   threadId   ( ) const;
  261.  
  262. private:
  263. /*--------------------------------- Private ----------------------------------*/
  264. friend class IStartedThread;
  265. friend class IWindow;
  266.  
  267. Boolean
  268.   all,
  269.   iteratedAll,
  270.   advance ( );
  271.  
  272. unsigned
  273.   cursor,
  274.   created;
  275.  
  276. static unsigned
  277.   timeStamp;
  278.  
  279. IThreadCursorData
  280.  *fCursorData;
  281. }; // IThread::Cursor
  282.  
  283. protected:
  284. /*------------------------------ Implementation ------------------------------*/
  285. virtual IStartedThread
  286.  *startedThread    ( ) const;
  287.  
  288. static IStartedThread
  289.  *newStartedThread ( );
  290.  
  291. /*------------------------------- Constructors -------------------------------*/
  292. IThread
  293.  &operator = ( const IThread& thread );
  294.  
  295. private:
  296. /*--------------------------------- Private ----------------------------------*/
  297. friend class ICurrentThread;
  298. friend class IWindow;
  299.  
  300. IStartedThread
  301.  *prepareToStart ( const IReference<IThreadFn>& threadFunction,
  302.                    Boolean         autoInitGUI );
  303.  
  304. void
  305.   threadStarted  ( IStartedThread* thread,
  306.                    int             rc );
  307.  
  308. static Boolean
  309.   inGUISession   ( );
  310.  
  311. Boolean
  312.   inMsgLoop      ( ) const;
  313.  
  314. IStartedThread
  315.  *thread;
  316.  
  317. static unsigned long
  318.   dfltStackSize;
  319.  
  320. static Boolean
  321.   dfltAutoInitGUI;
  322.  
  323.  
  324. static long
  325.   dfltQueueSize;
  326.  
  327. static ICurrentThread
  328.  *pCurrent;
  329.  
  330. IThreadData
  331.  *fThreadData;
  332.  
  333. }; // IThread
  334.  
  335.  
  336. class ICurrentThread : public IThread {
  337. typedef IThread
  338.   Inherited;
  339. public:
  340. /*------------------ Graphical User Interface (GUI) Support ------------------*/
  341. virtual void
  342.   initializeGUI    ( long queueSize = 30 ),
  343. #ifndef IC_WIN_FLAGNOP
  344.   terminateGUI     ( ),
  345. #endif
  346.   processMsgs      ( );
  347.  
  348. virtual Boolean
  349.   isGUIInitialized ( ) const;
  350.  
  351. #ifndef IC_MOTIFWIN_FLAGNOP
  352. virtual IAnchorBlockHandle
  353.   anchorBlock      ( ) const;
  354. #endif
  355.  
  356. virtual IMessageQueueHandle
  357.   messageQueue     ( ),
  358.   messageQueue     ( ) const;
  359.  
  360.  
  361. /*-------------------------- Current Thread Support --------------------------*/
  362. #ifndef IC_MOTIFWIN_FLAGNOP
  363. virtual unsigned long
  364.   remainingStack        ( ) const;
  365. #endif
  366.  
  367. virtual ICurrentThread
  368.  &sleep                 ( unsigned long milliseconds );
  369.  
  370. virtual ICurrentThread
  371.  &waitFor               ( const IThread &anotherThread ),
  372.  &waitForAllThreads     ( );
  373.  
  374. virtual IThreadId
  375.   waitForAnyThread      ( );
  376.  
  377. virtual void
  378.   exit                  ( unsigned long returnCode );
  379.  
  380.  
  381. /*------------------------ Current Thread Information ------------------------*/
  382. virtual IThreadId
  383.   id ( ) const;
  384. virtual IThreadHandle
  385.   handle ( ) const;
  386.  
  387. /*----------------------- Starting and Stopping Threads ----------------------*/
  388. virtual void
  389.   suspend ( );
  390.  
  391. protected:
  392. /*------------------------------- Constructors -------------------------------*/
  393.   ICurrentThread ( );
  394.  
  395. /*------------------------------ Implementation ------------------------------*/
  396. virtual IStartedThread
  397.  *startedThread ( ) const;
  398.  
  399. private:
  400. /*------------------------------ Hidden Members ------------------------------*/
  401. virtual
  402.  ~ICurrentThread ( );
  403.  
  404. /*--------------------------------- Private ----------------------------------*/
  405. friend class IThread;
  406.  
  407. ICurrentThreadData
  408.  *fCurrentThreadData;
  409.  
  410. }; // ICurrentThread
  411.  
  412. #pragma pack()
  413.  
  414.   #include <ithread.inl>
  415.  
  416. #endif /* _ITHREAD_ */
  417.