struct timespec nc_millisec_to_timespec (int n_millisec) { struct timeval tv = {n_millisec/1000LL, (n_millisec%1000LL)*1000LL}; struct timespec ts; TIMEVAL_TO_TIMESPEC(&tv, &ts); return ts; }
static inline int realtime_gettime (struct timespec *tp) { struct timeval tv; int retval = gettimeofday (&tv, NULL); if (retval == 0) /* Convert into `timespec'. */ TIMEVAL_TO_TIMESPEC (&tv, tp); return retval; }
/* * Convert ms to timespec structure */ struct timespec nc_ms_to_timespec (int ms) { struct timeval tv = {ms/1000LL, (ms%1000LL)*1000LL}; struct timespec ts; TIMEVAL_TO_TIMESPEC(&tv, &ts); return ts; }
struct timespec getPthreadTimeout(int millisec) { timeval now; struct timespec timeout; gettimeofday(&now, nullptr); TIMEVAL_TO_TIMESPEC(&now, &timeout); timeout.tv_sec += (millisec / 1000); timeout.tv_nsec += (millisec % 1000) * 1000000; return timeout; }
/* Change the access time of the file associated with FD to TVP[0] and the modification time of FILE to TVP[1]. Starting with 2.6.22 the Linux kernel has the utimensat syscall which can be used to implement futimes. */ int __futimes (int fd, const struct timeval tvp[2]) { /* The utimensat system call expects timespec not timeval. */ struct timespec ts[2]; if (tvp != NULL) { if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 || tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000) { __set_errno (EINVAL); return -1; } TIMEVAL_TO_TIMESPEC (&tvp[0], &ts[0]); TIMEVAL_TO_TIMESPEC (&tvp[1], &ts[1]); } return INLINE_SYSCALL (utimensat, 4, fd, NULL, tvp ? &ts : NULL, 0); }
/* time t is wall clock time, convert to time compatible * with our clock_gettime clock */ time_t unwall_ts(time_t t) { struct timespec booth_clk_now, now_tv, res; struct timeval now; get_time(&booth_clk_now); gettimeofday(&now, NULL); TIMEVAL_TO_TIMESPEC(&now, &now_tv); time_sub(&now_tv, &booth_clk_now, &res); return t - res.tv_sec; }
/* Set the current time of day and timezone information. This call is restricted to the super-user. */ int __settimeofday (const struct timeval *p, const struct timezone *z) { struct timespec tp; TIMEVAL_TO_TIMESPEC (p, &tp); if (setntptimeofday (&tp, z)) { return -1; } return 0; }
int clock_gettime(clockid_t clock_id, struct timespec *tp) { struct timeval tv; int n; n = gettimeofday(&tv, NULL); TIMEVAL_TO_TIMESPEC(&tv, tp); return n; }
/* Change the access time of FILE relative to FD to TVP[0] and the modification time of FILE to TVP[1]. */ int futimesat (int fd, const char *file, const struct timeval tvp[2]) { struct timespec tsp[2]; int result; if (tvp) { if (tvp[0].tv_usec >= 1000000 || tvp[0].tv_usec < 0 || tvp[1].tv_usec >= 1000000 || tvp[1].tv_usec < 0) { __set_errno (EINVAL); return -1; } TIMEVAL_TO_TIMESPEC (&tvp[0], &tsp[0]); TIMEVAL_TO_TIMESPEC (&tvp[1], &tsp[1]); } result = INLINE_SYSCALL (utimensat, 4, fd, file, tvp ? tsp : NULL, 0); return result; }
/* * Initialize the time of day register. */ oskit_error_t oskit_rtc_get(struct oskit_timespec *time) { struct timeval t; struct timespec ts; int r = NATIVEOS(gettimeofday)(&t, 0); /* lets fake local time as MST */ t.tv_sec -= 6 * 60 * 60; TIMEVAL_TO_TIMESPEC(&t, &ts); memcpy(time, &ts, sizeof *time); return r; }
int clock_gettime( clockid_t clock_id, /* clock ID (always CLOCK_REALTIME) */ struct timespec *tp /* where to store current time */ ) { struct timeval tv; int n; n = gettimeofday( &tv, NULL ); TIMEVAL_TO_TIMESPEC( &tv, tp ); return n; }
static void * reader_thread (void *nr) { struct timespec ts; struct timespec delay; int n; delay.tv_sec = 0; delay.tv_nsec = DELAY; for (n = 0; n < READTRIES; ++n) { int e; do { struct timeval tv; (void) gettimeofday (&tv, NULL); TIMEVAL_TO_TIMESPEC (&tv, &ts); ts.tv_nsec += TIMEOUT; if (ts.tv_nsec >= 1000000000) { ts.tv_nsec -= 1000000000; ++ts.tv_sec; } printf ("reader thread %ld tries again\n", (long int) nr); e = pthread_rwlock_timedrdlock (&lock, &ts); if (e != 0 && e != ETIMEDOUT) { puts ("timedrdlock failed"); exit (1); } } while (e == ETIMEDOUT); printf ("reader thread %ld succeeded\n", (long int) nr); nanosleep (&delay, NULL); if (pthread_rwlock_unlock (&lock) != 0) { puts ("unlock for reader failed"); exit (1); } printf ("reader thread %ld released\n", (long int) nr); } return NULL; }
/** * thread which regularly scans hashtable for expired buckets/flows */ void BaseAggregator::exporterThread() { struct timeval inttimer; gettimeofday(&inttimer, 0); //struct timeval difftime; //REQUIRE(timeval_subtract(&difftime, &stoptime, &starttime) == 0); /*timespec req; req.tv_sec = pollInterval / 1000; req.tv_nsec = (pollInterval % 1000) * 1000;*/ registerCurrentThread(); msg(MSG_INFO, "Polling aggregator each %u msec", pollInterval); while (!exitFlag) { addToCurTime(&inttimer, pollInterval); struct timeval curtime; gettimeofday(&curtime, 0); struct timeval difftime; if (timeval_subtract(&difftime, &inttimer, &curtime)!=1) { // restart nanosleep with the remaining sleep time // if we got interrupted by a signal struct timespec ts; TIMEVAL_TO_TIMESPEC(&difftime, &ts); while (nanosleep(&ts, &ts) == -1 && errno == EINTR); } gettimeofday(&curtime, 0); msg(MSG_VDEBUG,"Aggregator: starting Export"); for (size_t i = 0; i < rules->count; i++) { rules->rule[i]->hashtable->expireFlows(); } struct timeval endtime; gettimeofday(&endtime, 0); timeval_subtract(&difftime, &endtime, &curtime); msg (MSG_VDEBUG,"Aggregator: export took %.03f secs", (float)difftime.tv_usec/1000000+difftime.tv_sec); } if (getShutdownProperly()) { for (size_t i = 0; i < rules->count; i++) { rules->rule[i]->hashtable->expireFlows(true); } } unregisterCurrentThread(); }
extern "C" int pthread_timedjoin_np(pthread_t thread, void **retval, const struct timespec *abstime) { int ret; if (!dmtcp::ProcessInfo::instance().beginPthreadJoin(thread)) { return EINVAL; } /* * We continue to call pthread_tryjoin_np (and sleep) until we have gone past * the abstime provided by the caller */ while (1) { struct timeval tv; struct timespec ts; JASSERT(gettimeofday(&tv, NULL) == 0); TIMEVAL_TO_TIMESPEC(&tv, &ts); WRAPPER_EXECUTION_DISABLE_CKPT(); ret = _real_pthread_tryjoin_np(thread, retval); WRAPPER_EXECUTION_ENABLE_CKPT(); if (ret == 0) { break; } if (ts.tv_sec > abstime->tv_sec || (ts.tv_sec == abstime->tv_sec && ts.tv_nsec > abstime->tv_nsec)) { ret = ETIMEDOUT; break; } const struct timespec timeout = {(time_t) 0, (long)100 * 1000 * 1000}; nanosleep(&timeout, NULL); } #ifdef PTRACE /* Wrap the call to pthread_join() to make sure we call * delete_thread_on_pthread_join(). * FIXME: MTCP:process_pthread_join(thread) is calling threadisdead() THIS * SHOULDN'T BE NECESSARY. */ if (ret == 0) { mtcpFuncPtrs.process_pthread_join(thread); } #endif dmtcp::ProcessInfo::instance().endPthreadJoin(thread); return ret; }
struct timespec ca_get_current_time() { #if defined(__ANDROID__) || _POSIX_TIMERS > 0 struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return ts; #else struct timeval tv; gettimeofday(&tv, NULL); struct timespec ts; TIMEVAL_TO_TIMESPEC(&tv, &ts); return ts; #endif }
static void * tf (void *a) { /* Block SIGUSR1. */ sigset_t ss; pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME_NP); sigemptyset (&ss); sigaddset (&ss, SIGUSR1); if (BLOCK_SIG && pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0) { puts ("child: sigmask failed"); exit (1); } if (pthread_mutex_lock (&m) != 0) { puts ("child: mutex_lock failed"); exit (1); } int e = pthread_barrier_wait (&b); if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) { puts ("child: barrier_wait failed"); exit (1); } /* Compute timeout. */ struct timeval tv; (void) gettimeofday (&tv, NULL); struct timespec ts; TIMEVAL_TO_TIMESPEC (&tv, &ts); // RTAI clock_gettime(CLOCK_MONOTONIC, &ts); /* Timeout: 1sec. */ ts.tv_sec += 1; /* This call should never return. */ if (pthread_cond_timedwait (&c, &m, &ts) != ETIMEDOUT) { puts ("cond_timedwait didn't time out"); exit (1); } return NULL; }
FskErr KplConditionTimedWait(KplCondition condition, KplMutex mutex, KplTime timeout) { struct timespec sleep; struct timeval day; gettimeofday(&day, NULL); TIMEVAL_TO_TIMESPEC(&day, &sleep); sleep.tv_nsec += timeout->useconds * kNsecPerUsec; sleep.tv_sec += timeout->seconds + sleep.tv_nsec / kNsecPerSec; sleep.tv_nsec %= kNsecPerSec; if (ETIMEDOUT == pthread_cond_timedwait(&condition->cond, &mutex->mutex, &sleep)) { // fprintf(stderr, "condition awoken - timed out\n"); } return kFskErrNone; }
virtual void GetData (std::vector<double> & data, Timestamp & tupper, Timestamp & tlower) { data.resize(m_lidar->nscans); for (size_t ii(0); ii < m_lidar->nscans; ++ii) { data[ii] = m_lidar->GetNoisyRho(ii); } struct timeval tv; gettimeofday(&tv, 0); timespec_t ts; TIMEVAL_TO_TIMESPEC(&tv, &ts); tupper = ts; tlower = tupper; }
/* Check the first NFDS descriptors each in READFDS (if not NULL) for read readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS (if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out after waiting the interval specified therein. Returns the number of ready descriptors, or -1 for errors. */ int __select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { struct timespec ts, *to; if (timeout) { to = &ts; TIMEVAL_TO_TIMESPEC (timeout, to); } else to = NULL; return _hurd_select (nfds, NULL, readfds, writefds, exceptfds, to, NULL); }
bool DSEventSemaphore::WaitForEvent( SInt32 milliSecs ) { pthread_mutex_lock( &fMutex ); // we only lock if we didn't have a broadcast if( fbEvent == false ) { if( milliSecs == 0 ) // wait forever { // pthread_cond states we should check our predicate boolean to ensure we didn't get spuriously woken while ( fbEvent == false ) pthread_cond_wait( &fCondition, &fMutex ); } else if( milliSecs > 0 ) { // if we've already exceeded how long we are willing to wait, we just return false if (fMilliSecsTotal >= milliSecs) { pthread_mutex_unlock( &fMutex ); return false; } else { struct timeval tvNow; struct timespec tsTimeout; gettimeofday( &tvNow, NULL ); TIMEVAL_TO_TIMESPEC ( &tvNow, &tsTimeout ); tsTimeout.tv_sec += (milliSecs / 1000); tsTimeout.tv_nsec += ((milliSecs % 1000) * 1000000); if( pthread_cond_timedwait(&fCondition, &fMutex, &tsTimeout) == ETIMEDOUT ) { // we grab the lock as a result of the timeout bool bReturn = fbEvent; fMilliSecsTotal += milliSecs; // be sure to increment how long we waited pthread_mutex_unlock( &fMutex ); return bReturn; } } } } pthread_mutex_unlock( &fMutex ); return true; }
int OSAL_timedjoin_thread(OSAL_thread_t thread_id, unsigned int milliseconds, void **status) { #ifndef WIMAX_SYS_WINDOWS struct timespec ts; struct timeval tv; int ret; if ( thread_id == 0 ) return EINVAL; gettimeofday(&tv, NULL); TIMEVAL_TO_TIMESPEC(&tv, &ts); // Split the incoming millisecs into seconds and nano-seconds struct as required by the timedjoin method ts.tv_sec += (milliseconds / 1000); ts.tv_nsec += ((milliseconds % 1000) * 1000 * 1000); // 1 ms = 1000000 ns if (ts.tv_nsec >= 1000000000) { ts.tv_nsec -= 1000000000; ++ts.tv_sec; } ret = pthread_timedjoin_np((pthread_t)thread_id, status, &ts); if ( ret != 0 ) { OSALTRACE(OSAL_ERROR, ("thread join failed. err: %d", errno)); switch (errno) { case ETIMEDOUT: ret = WAIT_TIMEOUT; break; case EINVAL: case ESRCH: ret = WAIT_FAILED; break; } } return ret; #else #endif }
static void *spotify_loop(void *arg) { gazify_t *gazify = (gazify_t *)arg; sp_session *session = gazify->session; int next_timeout = 0; pthread_mutex_init(&g_notify_mutex, NULL); pthread_cond_init(&g_notify_cond, NULL); while (!g_gazify.want_exit) { if (next_timeout == 0) { while(!g_notify_do) pthread_cond_wait(&g_notify_cond, &g_notify_mutex); } else { struct timespec ts; #if _POSIX_TIMERS > 0 clock_gettime(CLOCK_REALTIME, &ts); #else struct timeval tv; gettimeofday(&tv, NULL); TIMEVAL_TO_TIMESPEC(&tv, &ts); #endif ts.tv_sec += next_timeout / 1000; ts.tv_nsec += (next_timeout % 1000) * 1000000; pthread_cond_timedwait(&g_notify_cond, &g_notify_mutex, &ts); } g_notify_do = 0; pthread_mutex_unlock(&g_notify_mutex); do { sp_session_process_events(session, &next_timeout); } while (next_timeout == 0); //FIXME: If we are ever to call any blocking functions here, make sure we use a more granual lock! if(g_gazify.inbox.size() > 0) { pthread_mutex_lock(&g_gazify.inbox_mutex); for(std::list<boost::function<void()> >::iterator it = g_gazify.inbox.begin(); it != g_gazify.inbox.end();++it) { (*it)(); } g_gazify.inbox.clear(); pthread_mutex_unlock(&g_gazify.inbox_mutex); } pthread_mutex_lock(&g_notify_mutex); } }
static void * tf2 (void *p) { int err; if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0 || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0) { puts ("cannot set cancellation options"); exit (1); } err = pthread_mutex_lock (&mut); if (err != 0) { puts ("child: cannot get mutex"); exit (1); } err = pthread_barrier_wait (&bar); if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD) { printf ("barrier_wait returned %d\n", err); exit (1); } puts ("child: got mutex; waiting"); pthread_cleanup_push (ch, NULL); /* Current time. */ struct timeval tv; (void) gettimeofday (&tv, NULL); /* +1000 seconds in correct format. */ struct timespec ts; TIMEVAL_TO_TIMESPEC (&tv, &ts); ts.tv_sec += 1000; pthread_cond_timedwait (&cond, &mut, &ts); pthread_cleanup_pop (0); puts ("child: cond_wait should not have returned"); return NULL; }
/* * Interface between xdr serializer and tcp connection. * Behaves like the system calls, read & write, but keeps some error state * around for the rpc level. */ static int read_vc(char *ctp, char *buf, int len) { struct ct_data *ct = (struct ct_data *)(void *)ctp; struct pollfd fd; struct timespec ts; ssize_t nread; if (len == 0) return (0); TIMEVAL_TO_TIMESPEC(&ct->ct_wait, &ts); fd.fd = ct->ct_fd; fd.events = POLLIN; for (;;) { switch (pollts(&fd, 1, &ts, NULL)) { case 0: ct->ct_error.re_status = RPC_TIMEDOUT; return (-1); case -1: if (errno == EINTR) continue; ct->ct_error.re_status = RPC_CANTRECV; ct->ct_error.re_errno = errno; return (-1); } break; } switch (nread = read(ct->ct_fd, buf, (size_t)len)) { case 0: /* premature eof */ ct->ct_error.re_errno = ECONNRESET; ct->ct_error.re_status = RPC_CANTRECV; nread = -1; /* it's really an error */ break; case -1: ct->ct_error.re_errno = errno; ct->ct_error.re_status = RPC_CANTRECV; break; } return (int)nread; }
int __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { struct pthread *curthread = _get_curthread(); struct timespec ts; int ret; if (numfds == 0 && timeout != NULL) { TIMEVAL_TO_TIMESPEC(timeout, &ts); ret = _nanosleep(&ts, NULL); } else { _thr_cancel_enter(curthread); ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout); _thr_cancel_leave(curthread, 1); } return (ret); }
static void * tf1 (void *arg) { struct timespec ts; struct timeval tv; gettimeofday (&tv, NULL); TIMEVAL_TO_TIMESPEC (&tv, &ts); ts.tv_sec += 10000; /* This call must never return. */ int e = pthread_mutex_timedlock (&m1, &ts); char buf[100]; printf ("tf1: mutex_timedlock returned: %s\n", strerror_r (e, buf, sizeof (buf))); exit (1); }
C_RESULT vp_os_cond_timed_wait(vp_os_cond_t *cond, uint32_t ms) { struct timespec ts; struct timeval tv; gettimeofday(&tv, NULL); TIMEVAL_TO_TIMESPEC(&tv, &ts); /* ts.tv_sec += ms/1000; // ts.tv_nsec += (ms%1000)*1000; ts.tv_nsec += (ms%1000)*1000000;*/ int tmp; ts.tv_nsec += ms * 1000000; tmp = ts.tv_nsec / (1000 * 1000000); ts.tv_sec += tmp; ts.tv_nsec -= tmp * (1000 * 1000000); return ( pthread_cond_timedwait(&cond->cond, (pthread_mutex_t *)cond->mutex, &ts) == ETIMEDOUT ? FAIL : SUCCESS ); }
bool get_boottime(struct timespec *ts) { struct utmpx *ut, key; debug_decl(get_boottime, SUDOERS_DEBUG_UTIL) memset(&key, 0, sizeof(key)); key.ut_type = BOOT_TIME; setutxent(); if ((ut = getutxid(&key)) != NULL) { sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO, "BOOT_TIME: %lld, %ld", (long long)ut->ut_tv.tv_sec, (long)ut->ut_tv.tv_usec); TIMEVAL_TO_TIMESPEC(&ut->ut_tv, ts); } endutxent(); debug_return_bool(ut != NULL); }
/* * Get the time of day clock and return it in ts. * Return 0 on success, an error number otherwise. */ static int tegra_rtc_gettime(device_t dev, struct timespec *ts) { struct tegra_rtc_softc *sc; struct timeval tv; uint32_t msec, sec; sc = device_get_softc(dev); LOCK(sc); msec = RD4(sc, RTC_MILLI_SECONDS); sec = RD4(sc, RTC_SHADOW_SECONDS); UNLOCK(sc); tv.tv_sec = sec; tv.tv_usec = msec * 1000; TIMEVAL_TO_TIMESPEC(&tv, ts); return (0); }
/* Get current value of CLOCK and store it in TP. */ int __clock_gettime (clockid_t clock_id, struct timespec *tp) { int retval = -1; switch (clock_id) { #ifdef SYSDEP_GETTIME SYSDEP_GETTIME; #endif #ifndef HANDLED_REALTIME case CLOCK_REALTIME: { struct timeval tv; retval = gettimeofday (&tv, NULL); if (retval == 0) TIMEVAL_TO_TIMESPEC (&tv, tp); } break; #endif default: #ifdef SYSDEP_GETTIME_CPU SYSDEP_GETTIME_CPU (clock_id, tp); #endif #if HP_TIMING_AVAIL if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1)) == CLOCK_THREAD_CPUTIME_ID) retval = hp_timing_gettime (clock_id, tp); else #endif __set_errno (EINVAL); break; #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME case CLOCK_PROCESS_CPUTIME_ID: retval = hp_timing_gettime (clock_id, tp); break; #endif } return retval; }