// routines to use it (p blocks, cond p does not) // 1 on success, -1 fail int vrpn_Semaphore::p() { #ifdef sgi if (fUsingLock) { if (ussetlock(l) != 1) { perror("vrpn_Semaphore::p: ussetlock:"); return -1; } } else { if (uspsema(ps) != 1) { perror("vrpn_Semaphore::p: uspsema:"); return -1; } } #elif defined(_WIN32) switch (WaitForSingleObject(hSemaphore, INFINITE)) { case WAIT_OBJECT_0: // got the resource break; case WAIT_TIMEOUT: ALL_ASSERT(0, "vrpn_Semaphore::p: infinite wait time timed out!"); return -1; break; case WAIT_ABANDONED: ALL_ASSERT(0, "vrpn_Semaphore::p: thread holding resource died"); return -1; break; case WAIT_FAILED: // get error info from windows (from FormatMessage help page) LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR)&lpMsgBuf, 0, NULL); fprintf(stderr, "vrpn_Semaphore::p: error waiting for resource, " "WIN32 WaitForSingleObject call caused the following error: %s", (LPTSTR)lpMsgBuf); // Free the buffer. LocalFree(lpMsgBuf); return -1; break; default: ALL_ASSERT(0, "vrpn_Semaphore::p: unknown return code"); return -1; } #else // Posix by default if (sem_wait(semaphore) != 0) { perror("vrpn_Semaphore::p: "); return -1; } #endif return 1; }
void accept_mutex_on() { switch(ussetlock(uslock)) { case 1: /* got lock */ break; case 0: fprintf(stderr, "didn't get lock\n"); exit(-1); case -1: perror("ussetlock"); exit(-1); } }