XN_C_API XnStatus xnOSCloseMutex(XN_MUTEX_HANDLE* pMutexHandle) { // Local function variables XnStatus nRetVal = XN_STATUS_OK; // Validate the input/output pointers (to make sure none of them is NULL) XN_VALIDATE_INPUT_PTR(pMutexHandle); // Make sure the actual mutex handle isn't NULL XN_RET_IF_NULL(*pMutexHandle, XN_STATUS_OS_INVALID_MUTEX); XnMutex* pMutex = *pMutexHandle; // check the kind of mutex if (pMutex->bIsNamed) { #ifndef XN_PLATFORM_LINUX_NO_SYSV // decrement second sem struct sembuf op; op.sem_num = 1; op.sem_op = -1; op.sem_flg = SEM_UNDO; if (0 != semop(pMutex->NamedSem, &op, 1)) { return (XN_STATUS_OS_MUTEX_CLOSE_FAILED); } // check if sem reached 0 (if so, it can be deleted) int val = semctl(pMutex->NamedSem, 1, GETVAL); if (val == 0) { // destroy the semaphore semctl(pMutex->NamedSem, 0, IPC_RMID); // and remove file xnOSDeleteFile(pMutex->csSemFileName); } // in any case, close the file close(pMutex->hSemFile); #endif } else { // destroy the mutex via the OS if (0 != pthread_mutex_destroy(&pMutex->ThreadMutex)) { return (XN_STATUS_OS_MUTEX_CLOSE_FAILED); } } // free the handle XN_FREE_AND_NULL(*pMutexHandle); // All is good... return (XN_STATUS_OK); }
XnStatus XnLinuxSysVNamedEvent::Destroy() { // dec ref count struct sembuf op; op.sem_num = XN_EVENT_SEM_REF_COUNT; op.sem_op = -1; op.sem_flg = IPC_NOWAIT | SEM_UNDO; semop(m_hSem, &op, 1); // check current ref count. If 0, destroy it int val = semctl(m_hSem, 0, GETVAL); if (0 == val) { // destroy the semaphore semctl(m_hSem, 0, IPC_RMID); // and remove file xnOSDeleteFile(m_csSemFileName); } return (XN_STATUS_OK); }