/* * Conduit.cpp * * Definition of the Conduit class * * Author: Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 * Modified: Emil Larsson, lime@isy.liu.se, 2012-01-13 */ #include #include "Conduit.h" #include // Constructor Conduit::Conduit() { ptrShm = NULL; isServer = false; iNumSems = 0; hMapObject = NULL; for(int i=0; i MAX_NUM_SEM) return false; // WIN32 IPC: Shared memory based on file mapping. char strKey[KEY_LEN+2]; SECURITY_ATTRIBUTES attr; // Fill struct with security attributes attr.nLength = sizeof(SECURITY_ATTRIBUTES); attr.lpSecurityDescriptor = NULL; attr.bInheritHandle = FALSE; // Convert integer array 'key' to char array 'strKey' for(i=0; i 0))); } // Wait for an event (reserve associated semaphore) WaitResult Conduit::WaitForEvent(int index) { bool ret,flag; if(!ValidIndex(index)) return WAIT_FAIL; // Perform wait ret = Wait(index + FIRST_EVENT_SEM); if(!ret || !GetShutdownFlag(flag)) return WAIT_FAIL; if(flag) return WAIT_CLOSE; return WAIT_OK; } // Wait for an event with timeout WaitResult Conduit::WaitForEvent(int index, int timeout) { bool ret,flag; if(!ValidIndex(index)) return WAIT_FAIL; // Perform wait ret = Wait(index + FIRST_EVENT_SEM,timeout); if(!ret || !GetShutdownFlag(flag)) return WAIT_FAIL; if(flag) return WAIT_CLOSE; return WAIT_OK; } // Cause an event (release associated sempahore) bool Conduit::CauseEvent(int index) { if(!ValidIndex(index)) return false; else return Signal(index + FIRST_EVENT_SEM); } // Cause shutdown event bool Conduit::CauseShutdown() { int i; // Indicate shutdown if(!SetShutdownFlag(true)) return false; // Release all semaphores but the mutex for(i=1; iFlag; else ret = false; // Release lock if(!ReleaseLock()) return false; return ret; } // Set state flag bool Conduit::SetFlag(FlagType flag) { bool ret = true; CommonData *ptr; // Request lock if(!RequestLock()) return false; if((ptr = GetCommonData()) != NULL) ptr->Flag = flag; else ret = false; // Release lock if(!ReleaseLock()) return false; // Indicate change if(!IndicateChangedFlag()) return false; return ret; } // Get shutdown boolean bool Conduit::GetShutdownFlag(bool& flag) { bool ret = true; CommonData *ptr; // Request lock if(!RequestLock()) return false; if((ptr = GetCommonData()) != NULL) flag = ptr->DoShutdown; else ret = false; // Release lock if(!ReleaseLock()) return false; return ret; } // Set shutdown boolean bool Conduit::SetShutdownFlag(bool flag) { bool ret = true; CommonData *ptr; // Request lock if(!RequestLock()) return false; if((ptr = GetCommonData()) != NULL) ptr->DoShutdown = flag; else ret = false; // Release lock if(!ReleaseLock()) return false; return ret; } // Check validity of semaphore index bool Conduit::ValidIndex(int index) { if(index < 0 || index > (iNumSems - FIRST_EVENT_SEM)) return false; return true; } // Calculate number of semaphores int Conduit::NumberOfSemaphores() { return(FIRST_EVENT_SEM + GetNumEvents()); }