#include <WaitCallback.h>
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. | |
Definition at line 27 of file WaitCallback.h.
|
|
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 }
|
|
|
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 }
|
|
|
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 }
|
|
||||||||||||||||||||||||
|
Registers a wait callback.
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 }
|
|
|
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 }
|
|
|
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 }
|
1.2.13.1 written by Dimitri van Heesch,
© 1997-2001