static void expired_timedwait(void) { semid_t id; u_int elapsed; if (ksem_init(&id, 0) < 0) { fail_errno("ksem_init"); return; } /* This should fail with a timeout and leave the value at 0. */ if (timedwait(id, 2500, &elapsed, ETIMEDOUT) < 0) { ksem_destroy(id); return; } if (!ELAPSED(elapsed, 2500)) { fail_err( "ksem_timedwait() of locked sem took %ums instead of 2500ms", elapsed); ksem_destroy(id); return; } if (checkvalue(id, 0) < 0) { ksem_destroy(id); return; } if (ksem_destroy(id) < 0) { fail_errno("ksem_destroy"); return; } pass(); }
static void unlocked_timedwait(void) { semid_t id; u_int elapsed; if (ksem_init(&id, 1) < 0) { fail_errno("ksem_init"); return; } /* This should succeed right away and set the value to 0. */ if (timedwait(id, 5000, &elapsed, 0) < 0) { ksem_destroy(id); return; } if (!ELAPSED(elapsed, 0)) { fail_err("ksem_timedwait() of unlocked sem took %ums", elapsed); ksem_destroy(id); return; } if (checkvalue(id, 0) < 0) { ksem_destroy(id); return; } if (ksem_destroy(id) < 0) { fail_errno("ksem_destroy"); return; } pass(); }
inline int timedwait(time_t seconds) { struct timespec curtime; #ifdef HAVE_CLOCK_GETTIME clock_gettime(CLOCK_REALTIME, &curtime); #else struct timeval timeofday; gettimeofday(&timeofday, 0); curtime.tv_sec = timeofday.tv_sec; curtime.tv_nsec = timeofday.tv_usec * 1000; #endif curtime.tv_sec += seconds; return timedwait(&curtime); }
static void locked_timedwait(void) { semid_t id; u_int elapsed; if (ksem_init(&id, 0) < 0) { fail_errno("ksem_init"); return; } /* * Schedule a post to trigger after 1000 ms. The subsequent * timedwait should succeed after 1000 ms as a result w/o * timing out. */ if (schedule_post(id, 1000) < 0) { ksem_destroy(id); return; } if (timedwait(id, 2000, &elapsed, 0) < 0) { check_alarm(1); ksem_destroy(id); return; } if (!ELAPSED(elapsed, 1000)) { fail_err( "ksem_timedwait() with delayed post took %ums instead of 1000ms", elapsed); check_alarm(1); ksem_destroy(id); return; } if (check_alarm(0) < 0) { ksem_destroy(id); return; } if (ksem_destroy(id) < 0) { fail_errno("ksem_destroy"); return; } pass(); }
inline int timedwait(time_t seconds) { struct timespec curtime; #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time clock_serv_t cclock; mach_timespec_t mts; host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); clock_get_time(cclock, &mts); mach_port_deallocate(mach_task_self(), cclock); curtime.tv_sec = mts.tv_sec; curtime.tv_nsec = mts.tv_nsec; #elif HAVE_CLOCK_GETTIME clock_gettime(CLOCK_REALTIME, &curtime); #else struct timeval timeofday; gettimeofday(&timeofday, 0); curtime.tv_sec = timeofday.tv_sec; curtime.tv_nsec = timeofday.tv_usec * 1000; #endif curtime.tv_sec += seconds; return timedwait(&curtime); }
bool timedwait(unsigned int sec, int nsec) { return timedwait(create_timespec(sec, nsec)); }