Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

WaitCallback Class Reference

A class which implements most of the Win2000 function functionality. More...

#include <WaitCallback.h>

List of all members.

Public Methods

bool hasWaitRegistered () const
void unregisterWait ()
bool registerWaitForSingleObject (HANDLE, const WAITCALLBACK, const PVOID, const DWORD, const bool=true)
 WaitCallback ()
virtual ~WaitCallback ()

Static Private Methods

DWORD WINAPI waitThread (LPVOID)

Private Attributes

HANDLE hWaitThread
 Handle to the wait/worker thread.

bool justOnce
 Boolean true if wait once.

DWORD timeOut
 Wait Timeout.

HANDLE waitEvents [2]
 The waitable events.

WAITCALLBACK cBack
 Pointer to the callback function.

PVOID cBackParam
 Pointer to the callback function parameter.


Detailed Description

When an event becomes signaled the callback function registered is called. This is implemented fully in Windows 2000 but not NT 4 (hence this implementation). Refer to MS Windows Platform SDK for details.

Definition at line 27 of file WaitCallback.h.


Constructor & Destructor Documentation

WaitCallback::WaitCallback  
 

Default Constructor

Definition at line 16 of file WaitCallback.cpp.

References waitEvents.

00017 {
00018     waitEvents[0] = CreateEvent(NULL, true, false, NULL);
00019     waitEvents[1] = INVALID_HANDLE_VALUE;   // Important, see hasWaitRegistered
00020 }

WaitCallback::~WaitCallback   [virtual]
 

Object Destructor

Definition at line 25 of file WaitCallback.cpp.

References unregisterWait(), and waitEvents.

00026 {
00027     unregisterWait();
00028     CloseHandle(waitEvents[0]);
00029     CloseHandle(waitEvents[1]);
00030 }


Member Function Documentation

bool WaitCallback::hasWaitRegistered   const
 

Returns true is there is a registered wait (completed or not)

Definition at line 39 of file WaitCallback.cpp.

References waitEvents.

Referenced by unregisterWait().

00040 {
00041     return waitEvents[1] != INVALID_HANDLE_VALUE;
00042 }

bool WaitCallback::registerWaitForSingleObject HANDLE    hEvent,
const WAITCALLBACK    cB,
const PVOID    cBP,
const DWORD    tO,
const bool    once = true
 

Registers a wait callback.

Parameters:
hEvent  : A waitable kernel object (note that this class takes possesion so pass a duplicated handle if it is required after this call.
cB  : A pointer to a WAITCALLBACK function
cBP  : A void pointer passed as the callbacks first parameter
t0  : The wait timeout (ms) can be INFINITE
once  : True if should Wait and callback just once
Returns:
bool : true iff wait successfully registered
Note:
the wait object is not possesed if this function fails (ie returns false)

Definition at line 59 of file WaitCallback.cpp.

References WAITCALLBACK.

Referenced by Connect().

00065 {
00066     if( hasWaitRegistered() )
00067         return false;
00068 
00069     hWaitThread = chBEGINTHREADEX(
00070         NULL,           // Default Security, non inheritable
00071         0,              // use default stack size  
00072         &waitThread,    // thread function
00073         this,           // thread argument
00074         CREATE_SUSPENDED,   // Create suspended
00075         NULL);          // don't need thread identifier
00076 
00077     if(hWaitThread == NULL)
00078         return false;
00079 
00080     // Take possesion of the event handle
00081     waitEvents[1] = hEvent;
00082     cBack = cB;
00083     cBackParam = cBP;
00084     timeOut = tO;
00085     justOnce = once;
00086 
00087     ResumeThread(hWaitThread);
00088 
00089     return true;
00090 }

void WaitCallback::unregisterWait  
 

Unregister the wait blocks for upto 6 seconds and then forces termination

Definition at line 95 of file WaitCallback.cpp.

References hasWaitRegistered(), hWaitThread, and waitEvents.

Referenced by Connect(), and ~WaitCallback().

00096 {
00097     if( hasWaitRegistered() ){
00098         SetEvent(waitEvents[0]);    // Signal waitThread to exit
00099 
00100         if( WaitForSingleObject(hWaitThread, 6000) == WAIT_TIMEOUT ){
00101             TerminateThread(hWaitThread, 0);
00102             chMB("Warning couldn't unregister a wait gracefully.\nCheck for Deadlock");
00103         }
00104 
00105         CloseHandle(waitEvents[1]);
00106         waitEvents[1] = INVALID_HANDLE_VALUE;
00107         CloseHandle(hWaitThread);
00108         ResetEvent(waitEvents[0]);
00109     }
00110 }

DWORD WINAPI WaitCallback::waitThread LPVOID    param [static, private]
 

The thread funcion which performs the wait and calls the callback function

Definition at line 115 of file WaitCallback.cpp.

References cBack, cBackParam, justOnce, timeOut, and waitEvents.

00116 {
00117     WaitCallback* thisWait = (WaitCallback*) param;
00118 
00119     do{
00120         switch( WaitForMultipleObjects(2, thisWait->waitEvents, false, thisWait->timeOut) ) {
00121         case WAIT_OBJECT_0: // wait is being unregistered
00122             return true;
00123             break;
00124         case WAIT_FAILED:
00125             assert(WAIT_FAILED != WAIT_FAILED);
00126             return false;
00127             break;
00128         case WAIT_TIMEOUT:
00129             (*thisWait->cBack)(thisWait->cBackParam, true);
00130             break;
00131         default:
00132             (*thisWait->cBack)(thisWait->cBackParam, false);
00133             break;
00134         }
00135 
00136     } while ( !thisWait->justOnce );
00137     
00138     return true;
00139 }


The documentation for this class was generated from the following files:
Generated on Mon Mar 25 06:30:00 2002 for Window Dressing by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001