static void test_semaphore(void) { HANDLE handle, handle2; /* test case sensitivity */ SetLastError(0xdeadbeef); handle = CreateSemaphoreA(NULL, 0, 1, __FILE__ ": Test Semaphore"); ok(handle != NULL, "CreateSemaphore failed with error %u\n", GetLastError()); ok(GetLastError() == 0, "wrong error %u\n", GetLastError()); SetLastError(0xdeadbeef); handle2 = CreateSemaphoreA(NULL, 0, 1, __FILE__ ": Test Semaphore"); ok( handle2 != NULL, "CreateSemaphore failed with error %d\n", GetLastError()); ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError()); CloseHandle( handle2 ); SetLastError(0xdeadbeef); handle2 = CreateSemaphoreA(NULL, 0, 1, __FILE__ ": TEST SEMAPHORE"); ok( handle2 != NULL, "CreateSemaphore failed with error %d\n", GetLastError()); ok( GetLastError() == 0, "wrong error %u\n", GetLastError()); CloseHandle( handle2 ); SetLastError(0xdeadbeef); handle2 = OpenSemaphoreA( SEMAPHORE_ALL_ACCESS, FALSE, __FILE__ ": Test Semaphore"); ok( handle2 != NULL, "OpenSemaphore failed with error %d\n", GetLastError()); CloseHandle( handle2 ); SetLastError(0xdeadbeef); handle2 = OpenSemaphoreA( SEMAPHORE_ALL_ACCESS, FALSE, __FILE__ ": TEST SEMAPHORE"); ok( !handle2, "OpenSemaphore succeeded\n"); ok( GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %u\n", GetLastError()); CloseHandle( handle ); }
bool CShareMem::Open(std::string strSMName) { bool bResult = false; do { if(strSMName.empty()) break; m_strSMName = strSMName; if(!m_hMutexSyncData) { m_hFileMapObj = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, strSMName.c_str()); if(NULL == m_hFileMapObj) break; m_pMapView = (char *)MapViewOfFile(m_hFileMapObj, FILE_MAP_ALL_ACCESS,0,0,0); if(NULL == m_pMapView) break; std::string strMutexName = strSMName + "Mutex"; m_hMutexSyncData = OpenMutexA(MUTEX_ALL_ACCESS, FALSE, strMutexName.c_str()); if(NULL == m_hMutexSyncData) break; std::string strSemName = strSMName + "Recv"; m_hWait[0] = OpenSemaphoreA(SEMAPHORE_ALL_ACCESS , FALSE, strSemName.c_str()); if(NULL == m_hWait[0]) break; std::string strSemExit = strSMName + "Exit"; m_hWait[1] = OpenSemaphoreA(SEMAPHORE_ALL_ACCESS, FALSE, strSemExit.c_str()); if(NULL == m_hWait[0]) break; m_pWRPos = reinterpret_cast<PSWRPos>(m_pMapView); m_pUserBufBasePos = m_pMapView + sizeof(SWRPos); } m_bSMSuccess = bResult = true; } while (false); if(!bResult) close(); return bResult; }
HOOKFUNC HANDLE WINAPI MyOpenSemaphoreA(DWORD dwDesiredAccess,BOOL bInheritHandle,LPCSTR lpName) { HANDLE rv = OpenSemaphoreA(dwDesiredAccess, bInheritHandle, lpName); debuglog(LCF_SYNCOBJ, __FUNCTION__ " returned 0x%X.\n", rv); EnterCriticalSection(&s_handleCS); std::set<HANDLE>& handles = s_threadIdHandles[GetCurrentThreadId()]; handles.insert(rv); LeaveCriticalSection(&s_handleCS); return rv; }
HOOKFUNC HANDLE WINAPI MyOpenSemaphoreA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName) { ENTER(); HANDLE rv = OpenSemaphoreA(dwDesiredAccess, bInheritHandle, lpName); LEAVE(rv); EnterCriticalSection(&s_handleCS); std::set<HANDLE>& handles = s_threadIdHandles[GetCurrentThreadId()]; handles.insert(rv); LeaveCriticalSection(&s_handleCS); return rv; }
XCODE_IPC_DESCR_T *xcode_ipc_open(unsigned int sz, key_t key) { XCODE_IPC_DESCR_T *pIpc = NULL; pIpc = avc_calloc(1, sizeof(XCODE_IPC_DESCR_T)); pIpc->sz = sz; if((pIpc->hFile = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, XCODE_FILE_MAPPING_NAME)) == NULL) { LOG(X_ERROR("OpenFileMapping %s failed (error:%d)"), XCODE_FILE_MAPPING_NAME, GetLastError()); xcode_ipc_close(pIpc); return NULL; } if((pIpc->pmem = MapViewOfFile(pIpc->hFile, FILE_MAP_ALL_ACCESS, 0, 0, pIpc->sz)) == NULL) { LOG(X_ERROR("MapViewOfFile %s failed (error:%d)"), XCODE_FILE_MAPPING_NAME, GetLastError()); xcode_ipc_close(pIpc); return NULL; } if((pIpc->hSemSrv = OpenSemaphoreA(SYNCHRONIZE | SEMAPHORE_MODIFY_STATE, FALSE, XCODE_SEM_SRV_NAME)) == NULL) { LOG(X_ERROR("OpenSemaphore %s failed (error:%d)"), XCODE_SEM_SRV_NAME, GetLastError()); xcode_ipc_close(pIpc); return NULL; } if((pIpc->hSemCli = OpenSemaphoreA(SYNCHRONIZE | SEMAPHORE_MODIFY_STATE, FALSE, XCODE_SEM_CLI_NAME)) == NULL) { LOG(X_ERROR("OpenSemaphore %s failed (error:%d)"), XCODE_SEM_CLI_NAME, GetLastError()); xcode_ipc_close(pIpc); return NULL; } pIpc->pmem->hdr.cmd = XCODE_IPC_CMD_NONE; return pIpc; }