|
|
|
|
|
| |
// / This HTML file was produced by the ProjectPublisher WebService, By Asaf Shelly / //
// / WebService URL is: http://Services.AsyncOp.com/ProjectPublisher/ProjectPublisherWSvc.asmx / //
// / Learn more and find the source code here: http://AsyncOp.com / //
// State_Simulation.cpp : Pseudo Implementation of a Stateful Synchronization Object
//
#include "State_Simulation.h"
////////////////////////////////////////
///
// STATEFUL CLASS : IMPLEMENTATION
State_Simulation::State_Simulation(int timeout, char* objName)
{
strncpy(objectName, objName, sizeof(objectName)/sizeof(objectName[0]));
Log("Creating");
lockTimeout = timeout; if (INFINITE==timeout) Err("some error");
hInternalLock = CreateMutex(NULL,true,NULL); if (!hInternalLock) Err("some error");
hStateEvent1 = CreateEvent(NULL,true,false,NULL); if (!hStateEvent1) Err("some error");
hStateEvent2 = CreateEvent(NULL,true,false,NULL); if (!hStateEvent2) Err("some error");
hStateEvent3 = CreateEvent(NULL,true,false,NULL); if (!hStateEvent3) Err("some error");
hStateEvent4 = CreateEvent(NULL,true,false,NULL); if (!hStateEvent4) Err("some error");
Log("Initialized");
ReleaseMutex(hInternalLock);
}
State_Simulation::~State_Simulation()
{
if (hInternalLock)
{
Log("Uninitializing");
switch(WaitForSingleObject(hInternalLock, lockTimeout))
{
case(WAIT_ABANDONED): case(WAIT_OBJECT_0):
{
ResetEvent(hStateEvent1);
ResetEvent(hStateEvent1);
ResetEvent(hStateEvent1);
ResetEvent(hStateEvent1);
break;
}
}
Log("Destroying");
if (hStateEvent4) { CloseHandle(hStateEvent4); hStateEvent4 = NULL; }
if (hStateEvent3) { CloseHandle(hStateEvent3); hStateEvent3 = NULL; }
if (hStateEvent2) { CloseHandle(hStateEvent2); hStateEvent2 = NULL; }
if (hStateEvent1) { CloseHandle(hStateEvent1); hStateEvent1 = NULL; }
Log("Destroyed");
ReleaseMutex(hInternalLock);
CloseHandle(hInternalLock); hInternalLock = NULL;
}
}
bool State_Simulation::SetState(unsigned short int state)
{
switch(WaitForSingleObject(hInternalLock, lockTimeout))
{
case(WAIT_ABANDONED): Err("FATAL ERROR: internal corruption"); ReleaseMutex(hInternalLock); return(false);
case(WAIT_TIMEOUT): Err("FATAL ERROR: Deadlock or system unresponsive"); return(false);
case(WAIT_OBJECT_0):
{
Log("State", state);
if (1==state) SetEvent(hStateEvent1); else ResetEvent(hStateEvent1);
if (2==state) SetEvent(hStateEvent2); else ResetEvent(hStateEvent2);
if (3==state) SetEvent(hStateEvent3); else ResetEvent(hStateEvent3);
if (CAN_DISPOSE==state) SetEvent(hStateEvent4); else ResetEvent(hStateEvent4);
if ((state!=CAN_DISPOSE)&&(state>3)) { Err("FATAL ERROR: internal flow error"); ReleaseMutex(hInternalLock); return(false); }
ReleaseMutex(hInternalLock);
return(true);
}
default: Err("FATAL ERROR: internal flow error"); return(false);
}
}
bool State_Simulation::WaitState(unsigned short int state)
{
HANDLE hLockedEvent[2] = { hInternalLock, NULL };
switch(state)
{
case 1: hLockedEvent[1] = hStateEvent1; break;
case 2: hLockedEvent[1] = hStateEvent2; break;
case 3: hLockedEvent[1] = hStateEvent3; break;
case CAN_DISPOSE:
case 4: hLockedEvent[1] = hStateEvent4; break;
default: Err("FATAL ERROR: internal flow error"); return(false);
}
switch(WaitForMultipleObjects(2, hLockedEvent, true, INFINITE)) // we allow INFINITE here because we don't know when the object will move to the next state
{
case(WAIT_ABANDONED): Err("FATAL ERROR: internal corruption"); ReleaseMutex(hInternalLock); return(false);
case(WAIT_TIMEOUT): Err("FATAL ERROR: internal flow error - this is unexpected"); return(false);
case(WAIT_OBJECT_0):
{
ReleaseMutex(hInternalLock);
return(true);
}
default: Err("FATAL ERROR: internal flow error"); return(false);
}
}
|
|
|
| |
|
|
| |
|
|
|
|
|
|