inline bool Unlock(HTmutex &mutex) { return htMutexUnlock(&mutex) == 0; }
inline bool Unlock(const HTmutex &mutex) { return htMutexUnlock(&(HTmutex &)mutex) == 0; }
HL_EXP int HL_APIENTRY htCondWait(HTcond *cond, HTmutex *mutex, int timeout) { if(cond == NULL) { return EINVAL; } if(*cond == NULL) { return EINVAL; } if(mutex == NULL) { return EINVAL; } if(*mutex == NULL) { return EINVAL; } if(timeout <= 0) { HTcond cv = *cond; HTmutex mx = *mutex; #ifdef HT_WIN_THREADS DWORD result; // This is incredibly dodgy and broken... What I really want is my own library // on top of pthreads - screw windows threading >:C htMutexUnlock(mutex); result = WaitForMultipleObjects (2,cv->events_, FALSE, INFINITE); htMutexLock(mutex); if(result == WAIT_FAILED) { return EINVAL; } #else int result = 0; pthread_mutex_t *p_mutex = (pthread_mutex_t *)&mx->mutex; (void)pthread_mutex_lock(p_mutex); result = pthread_cond_wait((pthread_cond_t *)&cv->cond, p_mutex); if(result != 0) { return result; } #endif } else { HTcond cv = *cond; HTmutex mx = *mutex; #ifdef HT_WIN_THREADS DWORD result; // Also incredibly broken and dodgy: htMutexUnlock(mutex); result = WaitForMultipleObjects (2, cv->events_, FALSE, (DWORD)timeout); htMutexLock(mutex); if(result == WAIT_FAILED) { return EINVAL; } else if(result == (DWORD)WAIT_TIMEOUT) { return (int)ETIMEDOUT; } } #else int result = 0; struct timespec tv; struct timeb t; long ms; /* convert timeout to an absolute time */ ftime(&t); ms = t.millitm + timeout; tv.tv_sec = t.time + (ms / 1000); tv.tv_nsec = (ms % 1000) * 1000000; pthread_mutex_t *p_mutex = (pthread_mutex_t *)&mx->mutex; (void)pthread_mutex_lock(p_mutex); result = pthread_cond_timedwait((pthread_cond_t *)&cv->cond, p_mutex, &tv); if(result == ETIMEDOUT) { return ETIMEDOUT; } else if(result != 0) { return result; } }