/**@ingroup tsk_condwait_group * Block the current thread until the condition is opened or until @a ms milliseconds have passed. * @param handle condwait handle created using @ref tsk_condwait_create. * @param ms The number of milliseconds to wait for a given condition. * @retval Zero if succeed and non-zero error code otherwise. * @sa @ref tsk_condwait_wait. */ int tsk_condwait_timedwait(tsk_condwait_handle_t* handle, uint64_t ms) { #if TSK_UNDER_WINDOWS DWORD ret = WAIT_FAILED; #else int ret = EINVAL; #endif tsk_condwait_t *condwait = (tsk_condwait_t*)handle; #if TSK_UNDER_WINDOWS if((ret = WaitForSingleObject(condwait->pcond, (DWORD)ms)) != WAIT_OBJECT_0){ if(ret == TIMED_OUT){ /* TSK_DEBUG_INFO("WaitForSingleObject function timedout: %d", ret); */ } else{ TSK_DEBUG_ERROR("WaitForSingleObject function failed: %d", ret); } return ((ret == TIMED_OUT) ? 0 : ret); } #else if(condwait && condwait->mutex){ struct timespec ts = {0, 0}; struct timeval tv = {0, 0}; /*int rc =*/ tsk_gettimeofday(&tv, 0); ts.tv_sec = ( tv.tv_sec + ((long)ms/1000) ); ts.tv_nsec += ( (tv.tv_usec * 1000) + ((long)ms % 1000 * 1000000) ); if(ts.tv_nsec > 999999999) ts.tv_sec+=1, ts.tv_nsec = ts.tv_nsec % 1000000000; tsk_mutex_lock(condwait->mutex); if((ret = pthread_cond_timedwait(condwait->pcond, (pthread_mutex_t*)condwait->mutex, &ts))){ if(ret == TIMED_OUT){ /* TSK_DEBUG_INFO("pthread_cond_timedwait function timedout: %d", ret); */ } else{ TSK_DEBUG_ERROR("pthread_cond_timedwait function failed: %d", ret); } } tsk_mutex_unlock(condwait->mutex); return ((ret == TIMED_OUT) ? 0 : ret); } #endif return ret; }
/**@ingroup tsk_time_group */ uint64_t tsk_gettimeofday_ms() { struct timeval tv; tsk_gettimeofday(&tv, tsk_null); return (((uint64_t)tv.tv_sec)*(uint64_t)1000) + (((uint64_t)tv.tv_usec)/(uint64_t)1000); }