int usbi_cond_timedwait(usbi_cond_t *cond, usbi_mutex_t *mutex, const struct timespec *abstime) { FILETIME filetime; ULARGE_INTEGER rtime; struct timeval targ_time, cur_time, delta_time; struct timespec cur_time_ns; DWORD millis; // GetSystemTimeAsFileTime() is not available on CE SYSTEMTIME st; GetSystemTime(&st); SystemTimeToFileTime(&st, &filetime); rtime.LowPart = filetime.dwLowDateTime; rtime.HighPart = filetime.dwHighDateTime; rtime.QuadPart -= epoch_time; cur_time_ns.tv_sec = (long)(rtime.QuadPart / 10000000); cur_time_ns.tv_nsec = (long)((rtime.QuadPart % 10000000)*100); TIMESPEC_TO_TIMEVAL(&cur_time, &cur_time_ns); TIMESPEC_TO_TIMEVAL(&targ_time, abstime); timersub(&targ_time, &cur_time, &delta_time); if(delta_time.tv_sec < 0) // abstime already passed? millis = 0; else { millis = delta_time.tv_usec/1000; millis += delta_time.tv_sec *1000; if (delta_time.tv_usec % 1000) // round up to next millisecond millis++; } return usbi_cond_intwait(cond, mutex, millis); }
int usbi_cond_timedwait(usbi_cond_t *cond, usbi_mutex_t *mutex, const struct timespec *abstime) { FILETIME filetime; ULARGE_INTEGER rtime; struct timeval targ_time, cur_time, delta_time; struct timespec cur_time_ns; DWORD millis; extern const uint64_t epoch_time; GetSystemTimeAsFileTime(&filetime); rtime.LowPart = filetime.dwLowDateTime; rtime.HighPart = filetime.dwHighDateTime; rtime.QuadPart -= epoch_time; cur_time_ns.tv_sec = (long)(rtime.QuadPart / 10000000); cur_time_ns.tv_nsec = (long)((rtime.QuadPart % 10000000)*100); TIMESPEC_TO_TIMEVAL(&cur_time, &cur_time_ns); TIMESPEC_TO_TIMEVAL(&targ_time, abstime); timersub(&targ_time, &cur_time, &delta_time); if(delta_time.tv_sec < 0) millis = 0; else { millis = delta_time.tv_usec/1000; millis += delta_time.tv_sec *1000; if (delta_time.tv_usec % 1000) millis++; } return usbi_cond_intwait(cond, mutex, millis); }
// N.B.: usbi_cond_*wait() can also return ENOMEM, even though pthread_cond_*wait cannot! int usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex) { return usbi_cond_intwait(cond, mutex, INFINITE); }