home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / thread / piserve / service.cpp < prev    next >
Text File  |  1996-10-29  |  5KB  |  135 lines

  1. /***************************************************************
  2. * FILE NAME: service.cpp                                       *
  3. *                                                              *
  4. * DESCRIPTION:                                                 *
  5. *   This file contains the implementation of                   *
  6. *   classes/functions declared in service.hpp.                 *
  7. *                                                              *
  8. * COPYRIGHT:                                                   *
  9. *   Licensed Materials - Property of Solution Frameworks       *
  10. *   Copyright (C) 1996, Solution Frameworks                    *
  11. *   All Rights Reserved                                        *
  12. ***************************************************************/
  13. #include "service.hpp"
  14.  
  15. #ifndef _ITHREAD_
  16.   #include <ithread.hpp>
  17. #endif
  18. #ifndef _IOBJWIN_
  19.   #include <iobjwin.hpp>
  20. #endif
  21. #ifndef _USEREVT_
  22.   #include "userevt.hpp"
  23. #endif
  24. #ifndef _SIGNAL_
  25.   #include "signal.hpp"
  26. #endif
  27.  
  28. struct ServiceThreadData {
  29.   ServiceThreadData ( ) {
  30.     objWin = 0;
  31.   }
  32. IThread
  33.   thread;
  34. IObjectWindow
  35.  *objWin;
  36. Signal
  37.   signal;
  38. ServiceThreadData ( const ServiceThreadData& );
  39. operator = ( const ServiceThreadData& );
  40. }; // ServiceThreadData
  41.  
  42. /*--------------- ServiceThread::ServiceThread -----------------
  43. | The constructor allocates this object's implementation data. |
  44. | It then starts the secondary thread and waits for that       |
  45. | thread to become "ready."                                    |
  46. --------------------------------------------------------------*/
  47. ServiceThread :: ServiceThread ( )
  48.   : data( new ServiceThreadData ) {
  49.   data->thread.start( (IThread::OptlinkFnPtr)ServiceThread::run, 
  50.                       data );
  51.   data->signal.wait();
  52. }
  53.  
  54. /*-------------- ServiceThread::~ServiceThread -----------------
  55. | The destructor posts a WM_QUIT to the secondary thread       |
  56. | (using IThread::stopProcessingMsgs).  It does *not* delete   |
  57. | the object's data as the secondary thread is still using it. |
  58. | The service thread will delete this object's data when it    |
  59. | terminates.                                                  |
  60. --------------------------------------------------------------*/
  61. ServiceThread :: ~ServiceThread ( ) {
  62.   data->thread.stopProcessingMsgs();
  63. }
  64.  
  65. /*---------------- ServiceThread::postRequest ------------------
  66. | A user-defined event is posted to the object window on the   |
  67. | service thread.  We pass the address (effectively) of the    |
  68. | IThreadFn object.                                            |
  69. --------------------------------------------------------------*/
  70. void ServiceThread :: postRequest 
  71.                       ( const IReference<IThreadFn> &request ) {
  72.   (*request).addRef();
  73.   UserEvent( 0, (void*)(IThreadFn*)request )
  74.     .postTo( data->objWin->handle() );
  75. }
  76.  
  77. /*---------------------- ServiceHandler ------------------------
  78. | This class handles user-defined events posted to the service |
  79. | thread's object window.                                      |
  80. |                                                              |
  81. | It simply invokes the run() function against the IThreadFn   |
  82. | referenced by mp1.                                           |
  83. --------------------------------------------------------------*/
  84. struct ServiceHandler : public UserHandler {
  85.   ServiceHandler ( IObjectWindow *objWin ) {
  86.     handleEventsFor( objWin );
  87.   }
  88.  
  89. virtual Boolean
  90.   dispatchHandlerEvent ( IEvent &event ) {
  91.     return UserHandler::dispatchHandlerEvent( event );
  92.   }
  93.  
  94. virtual Boolean
  95.   handleUserEvent ( UserEvent &event ) {
  96.     IThreadFn
  97.      *request = (IThreadFn*)(void*)event.parameter1();
  98.  
  99.     request->run();
  100.  
  101.     request->removeRef();
  102.  
  103.     return true;
  104.   }
  105. }; // ServiceHandler
  106.  
  107. /*-------------------- ServiceThread::run ----------------------
  108. | This static member function executes on the service thread.  |
  109. | It is passed the ServiceThreadData as input.  We create an   |
  110. | object window, post the signal (to tell the thread that      |
  111. | created this service thread that it's now ready), and        |
  112. | process user-defined events that come in to that object      |
  113. | window.                                                      |
  114. |                                                              |
  115. | When the owner cancels this thread (by posting a WM_QUIT,    |
  116. | most likely via IThread::stopProcessingMsgs), we delete      |
  117. | the argument ServiceThreadData.                              |
  118. --------------------------------------------------------------*/
  119. void ServiceThread :: run ( void *arg ) {
  120.   ServiceThreadData
  121.     *data = (ServiceThreadData*)arg;
  122.  
  123.   data->objWin = new IObjectWindow;
  124.   data->objWin->setAutoDeleteObject( true );
  125.  
  126.   data->signal.signal();
  127.  
  128.   ServiceHandler
  129.     handler( data->objWin );
  130.  
  131.   IThread::current().processMsgs();
  132.  
  133.   delete data;
  134. }
  135.