void process_execute(void) { // Find first non-empty queue int qi = 0; while (qi < NB_PROCESS_EVENT_PRIORITIES && queues[qi].count == 0) { qi += 1; } if (qi == NB_PROCESS_EVENT_PRIORITIES) { // No events to process return; } // Process one event from the selected queue struct event e; struct event_queue* q = &queues[qi]; ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { e = q->queue[q->first]; q->count -= 1; q->first = (q->first + 1) % PROCESS_CONF_EVENT_QUEUE_SIZE; } #ifdef PROCESS_STATS clock_time_t clock_before = clock_get_time(); #endif e.p->thread(e.p, e.ev, e.data); #ifdef PROCESS_STATS clock_time_t duration = clock_get_time() - clock_before; e.p->time += duration; #endif }
/* Refresh rate calculation */ static void nvstusb_print_refresh_rate(void) { static int i_it = 0; static uint64_t i_last = 0; static uint64_t i_first = 0; static uint64_t i_acc_var = 0; double f_mean, f_var; /* First frame */ if(i_it == 0) { struct timespec s_tmp; #ifdef __MACH__ 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); s_tmp.tv_sec = mts.tv_sec; s_tmp.tv_nsec = mts.tv_nsec; #else clock_gettime(CLOCK_REALTIME, &s_tmp); #endif i_first = (uint64_t)s_tmp.tv_sec*1000000+(uint64_t)s_tmp.tv_nsec/1000; i_last = i_first; f_mean = 0; f_var = 0; } else { struct timespec s_tmp; #ifdef __MACH__ 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); s_tmp.tv_sec = mts.tv_sec; s_tmp.tv_nsec = mts.tv_nsec; #else clock_gettime(CLOCK_REALTIME, &s_tmp); #endif uint64_t i_new = (uint64_t)s_tmp.tv_sec*1000000+(uint64_t)s_tmp.tv_nsec/1000; /* Update average */ f_mean = (double)(i_new-i_first)/(i_it); /* Calculate variance */ i_acc_var += pow((double)(i_new-i_last)-f_mean, 2); /* std dev */ f_var = (double)sqrt(i_acc_var/i_it); i_last = i_new; /* Display each 512 frame */ if(i_it % 512 == 0) { fprintf(stderr,"nvstusb: frame:%d (%0.2f s) mean: %f Hz (%0.2f us) sqrt(var): %0.2f us (%0.1f %%)\n",i_it,f_mean*i_it/1000000.0, 1000000/f_mean, f_mean, f_var, 100.0*f_var/f_mean); } } /* Increment frame counter */ i_it++; }
int nanosleep(const struct timespec *requested_time, struct timespec *remaining_time) { kern_return_t kret; int ret; mach_timespec_t current; mach_timespec_t completion; if (__unix_conforming == 0) __unix_conforming = 1; #ifdef VARIANT_CANCELABLE pthread_testcancel(); #endif /* VARIANT_CANCELABLE */ if ((requested_time == NULL) || (requested_time->tv_sec < 0) || (requested_time->tv_nsec >= NSEC_PER_SEC)) { errno = EINVAL; return -1; } if (remaining_time != NULL) { /* once we add requested_time, this will be the completion time */ kret = clock_get_time(clock_port, &completion); if (kret != KERN_SUCCESS) { fprintf(stderr, "clock_get_time() failed: %s\n", mach_error_string(kret)); errno = EINVAL; return -1; } } ret = SEMWAIT_SIGNAL(clock_sem, MACH_PORT_NULL, 1, 1, (int64_t)requested_time->tv_sec, (int32_t)requested_time->tv_nsec); if (ret < 0) { if (errno == ETIMEDOUT) { return 0; } else if (errno == EINTR) { if (remaining_time != NULL) { ret = clock_get_time(clock_port, ¤t); if (ret != KERN_SUCCESS) { fprintf(stderr, "clock_get_time() failed: %s\n", mach_error_string(ret)); return -1; } /* This depends on the layout of a mach_timespec_t and timespec_t being equivalent */ ADD_MACH_TIMESPEC(&completion, requested_time); /* We have to compare first, since mach_timespect_t contains unsigned integers */ if(CMP_MACH_TIMESPEC(&completion, ¤t) > 0) { SUB_MACH_TIMESPEC(&completion, ¤t); remaining_time->tv_sec = completion.tv_sec; remaining_time->tv_nsec = completion.tv_nsec; } else { bzero(remaining_time, sizeof(*remaining_time)); } } } else { errno = EINVAL; } } return -1; }
void smaccm_thread_calendar() { if ((smaccm_calendar_counter % (100 / smaccm_tick_interval)) == 0) { // MWW: modification of time type to match Ivory/Tower (CAmkES time server uses uint64_t) int64_t the_time = (int64_t)clock_get_time(); PixhawkProxy_periodic_dispatcher_write_int64_t(&the_time); }if ((smaccm_calendar_counter % (100 / smaccm_tick_interval)) == 0) { // MWW: modification of time type to match Ivory/Tower (CAmkES time server uses uint64_t) int64_t the_time = (int64_t)clock_get_time(); Input_periodic_dispatcher_write_int64_t(&the_time); } smaccm_calendar_counter = (smaccm_calendar_counter + 1) % smaccm_hyperperiod_subdivisions; smaccm_calendar_ticks++; }
void wTimeNow(wTime* time) { #if defined(__APPLE__) // Mac OS does not have clock_gettime, use clock_get_time // Another option is to use gettimeofday() for both Linux and Mac // but it's deprecated and not recommended due to not being monotonic mach_timespec_t mts; int ret = clock_get_time(gClockServ, &mts); assert(ret == 0); time->tv_sec = mts.tv_sec; time->tv_nsec = mts.tv_nsec; #elif defined(__linux__) int ret = clock_gettime(CLOCK_REALTIME, time); assert(ret == 0); #elif defined(__WIN32__) int ret = QueryPerformanceCounter(time); assert(ret != 0); #endif }
/* * Synopsis: * Emulate Posix clock_gettime() using Mach system routines. */ int clock_gettime(clockid_t id, struct timespec *tp) { clock_serv_t service_id; clock_id_t mach_id; mach_timespec_t now; kern_return_t status; switch (id) { case CLOCK_REALTIME: mach_id = CALENDAR_CLOCK; break; case CLOCK_MONOTONIC: mach_id = SYSTEM_CLOCK; break; default: errno = EINVAL; return -1; } status = host_get_clock_service(mach_host_self(), mach_id, &service_id); if (status != KERN_SUCCESS) { errno = EINVAL; return -1; } status = clock_get_time(service_id, &now); if (status != KERN_SUCCESS) { errno = EINVAL; return -1; } tp->tv_sec = now.tv_sec; tp->tv_nsec = now.tv_nsec; return 0; }
double bsp_time() { //get init data const struct mcbsp_thread_data * const data = mcbsp_internal_const_prefunction(); //get stop time #ifdef __MACH__ //get rights for accessing Mach's timers const kern_return_t rc1 = host_get_clock_service( mach_host_self(), SYSTEM_CLOCK, &(data->clock) ); if( rc1 != KERN_SUCCESS ) { fprintf( stderr, "Could not access the Mach system timer (%s)\n", mach_error_string( rc1 ) ); mcbsp_util_fatal(); } mach_timespec_t stop; const kern_return_t rc2 = clock_get_time( data->clock, &stop ); if( rc2 != KERN_SUCCESS ) { fprintf( stderr, "Could not get time at call to bsp_time (%s)\n", mach_error_string( rc2 ) ); mcbsp_util_fatal(); } #else struct timespec stop; clock_gettime( CLOCK_MONOTONIC, &stop); #endif //return time double time = (stop.tv_sec-data->start.tv_sec); time += (stop.tv_nsec-data->start.tv_nsec)/1000000000.0; return time; }
/* return current monotonic time in milliseconds (ms). */ uint64_t current_time_monotonic(void) { uint64_t time; #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) time = (uint64_t)GetTickCount() + add_monotime; if (time < last_monotime) { /* Prevent time from ever decreasing because of 32 bit wrap. */ uint32_t add = ~0; add_monotime += add; time += add; } last_monotime = time; #else struct timespec monotime; #if defined(__linux__) && defined(CLOCK_MONOTONIC_RAW) clock_gettime(CLOCK_MONOTONIC_RAW, &monotime); #elif defined(__APPLE__) clock_serv_t muhclock; mach_timespec_t machtime; host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &muhclock); clock_get_time(muhclock, &machtime); mach_port_deallocate(mach_task_self(), muhclock); monotime.tv_sec = machtime.tv_sec; monotime.tv_nsec = machtime.tv_nsec; #else clock_gettime(CLOCK_MONOTONIC, &monotime); #endif time = 1000ULL * monotime.tv_sec + (monotime.tv_nsec / 1000000ULL); #endif return time; }
/* std::string timestamp::to_string( bool local ) const { return ctime_adapter(*this).strftime( "%Y-%m-%d, %H:%M:%S" , local); } */ timestamp timestamp::now( void ) { #if defined( __codex_win32__ ) FILETIME ft; GetSystemTimeAsFileTime( &ft ); LARGE_INTEGER qp; qp.HighPart = ft.dwHighDateTime; qp.LowPart = ft.dwLowDateTime; int64_t value = qp.QuadPart / 10; static const int64_t TICK_OFFSET = -11644473600 * 1000 * 1000; value += TICK_OFFSET; return timestamp(value); #elif defined( __codex_linux__ ) struct timespec ts; int64_t value= 0; if( clock_gettime( CLOCK_REALTIME , &ts ) == -1 ) { value = std::time(nullptr) * 1000 * 1000; } else { value = ( ts.tv_sec * 1000 * 1000 ) + (ts.tv_nsec / 1000); } return timestamp(value); #elif defined( __codex_apple__ ) struct timespec ts; int64_t value = 0; 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 ); ts.tv_sec = mts.tv_sec; ts.tv_nsec = mts.tv_nsec; value = ( ts.tv_sec * 1000 * 1000 ) + (ts.tv_nsec / 1000); return timestamp(value); #endif }
bool scond_wait_timeout(scond_t *cond, slock_t *lock, unsigned timeout_ms) { struct timespec now; #ifdef __MACH__ // OSX doesn't have clock_gettime ... :( 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); now.tv_sec = mts.tv_sec; now.tv_nsec = mts.tv_nsec; #else clock_gettime(CLOCK_REALTIME, &now); #endif now.tv_sec += timeout_ms / 1000; now.tv_nsec += timeout_ms * 1000000L; now.tv_sec += now.tv_nsec / 1000000000L; now.tv_nsec = now.tv_nsec % 1000000000L; int ret = pthread_cond_timedwait(&cond->cond, &lock->lock, &now); return ret == 0; }
unsigned long hwtimer_arch_now(void) { struct timespec t; DEBUG("hwtimer_arch_now()\n"); _native_syscall_enter(); #ifdef __MACH__ clock_serv_t cclock; mach_timespec_t mts; host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); clock_get_time(cclock, &mts); mach_port_deallocate(mach_task_self(), cclock); t.tv_sec = mts.tv_sec; t.tv_nsec = mts.tv_nsec; #else if (real_clock_gettime(CLOCK_MONOTONIC, &t) == -1) { err(EXIT_FAILURE, "hwtimer_arch_now: clock_gettime"); } #endif _native_syscall_leave(); native_hwtimer_now = ts2ticks(&t) - time_null; struct timeval tv; ticks2tv(native_hwtimer_now, &tv); DEBUG("hwtimer_arch_now(): it is now %lu s %lu us\n", (unsigned long)tv.tv_sec, (unsigned long)tv.tv_usec); DEBUG("hwtimer_arch_now(): returning %lu\n", native_hwtimer_now); return native_hwtimer_now; }
bool cResetEvent::Wait(unsigned msec)//Wait for signal , then reset { pthread_mutex_lock( &mutx ); if (!state) { struct timespec ts; #if HOST_OS == OS_DARWIN // OSX doesn't have clock_gettime. 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); ts.tv_sec = mts.tv_sec; ts.tv_nsec = mts.tv_nsec; #else clock_gettime(CLOCK_REALTIME, &ts); #endif ts.tv_sec += msec / 1000; ts.tv_nsec += (msec % 1000) * 1000000; while (ts.tv_nsec > 1000000000) { ts.tv_nsec -= 1000000000; ts.tv_sec++; } pthread_cond_timedwait( &cond, &mutx, &ts ); } bool rc = state; state=false; pthread_mutex_unlock( &mutx ); return rc; }
unsigned long hwtimer_arch_now(void) { struct timespec t; DEBUG("hwtimer_arch_now()\n"); _native_in_syscall = 1; #ifdef __MACH__ clock_serv_t cclock; mach_timespec_t mts; host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); clock_get_time(cclock, &mts); mach_port_deallocate(mach_task_self(), cclock); t.tv_sec = mts.tv_sec; t.tv_nsec = mts.tv_nsec; #else if (clock_gettime(CLOCK_MONOTONIC, &t) == -1) { err(1, "hwtimer_arch_now: clock_gettime"); } #endif _native_in_syscall = 0; native_hwtimer_now = ts2ticks(&t) - time_null; DEBUG("hwtimer_arch_now(): it is now %lu s %lu ns\n", (unsigned long)t.tv_sec, (unsigned long)t.tv_nsec); DEBUG("hwtimer_arch_now(): returning %lu\n", native_hwtimer_now); return native_hwtimer_now; }
void osfmach3_get_time( struct timeval *xtime) { tvalspec_t cur_time; #ifdef __powerpc__ if (use_highres && MACH_PORT_VALID(rt_clock)) { kern_return_t kr; /* * We want a higher resolution time: ask the microkernel. * XXX this doesn't work on MP PowerMac systems. */ kr = clock_get_time(rt_clock, &cur_time); if (kr != KERN_SUCCESS) { MACH3_DEBUG(2, kr, ("osfmach3_get_time: clock_get_time(0x%x)", rt_clock)); /* use mapped time */ MTS_TO_TS(serv_mtime, &cur_time); } } else { MTS_TO_TS(serv_mtime, &cur_time); } #else /* __powerpc__ */ MTS_TO_TS(serv_mtime, &cur_time); #endif /* __powerpc__ */ xtime->tv_sec = cur_time.tv_sec + base_time.tv_sec; xtime->tv_usec = (cur_time.tv_nsec + base_time.tv_nsec) / 1000; if (xtime->tv_usec >= USEC_PER_SEC) { xtime->tv_sec++; xtime->tv_usec -= USEC_PER_SEC; } }
/** @This allocates a new uclock structure. * * @param flags flags for the creation of a uclock structure * @return pointer to uclock, or NULL in case of error */ struct uclock *uclock_std_alloc(enum uclock_std_flags flags) { #ifdef __MACH__ clock_serv_t cclock; mach_timespec_t ts; if (unlikely(host_get_clock_service(mach_host_self(), unlikely(flags & UCLOCK_FLAG_REALTIME) ? CALENDAR_CLOCK : REALTIME_CLOCK, &cclock) < 0)) { return NULL; } if(unlikely(clock_get_time(cclock, &ts) < 0)) { mach_port_deallocate(mach_task_self(), cclock); return NULL; } #else struct timespec ts; if (unlikely(clock_gettime(unlikely(flags & UCLOCK_FLAG_REALTIME) ? CLOCK_REALTIME : CLOCK_MONOTONIC, &ts) == -1)) return NULL; #endif struct uclock_std *uclock_std = malloc(sizeof(struct uclock_std)); if (unlikely(uclock_std == NULL)) return NULL; uclock_std->flags = flags; urefcount_init(uclock_std_to_urefcount(uclock_std), uclock_std_free); uclock_std->uclock.refcount = uclock_std_to_urefcount(uclock_std); uclock_std->uclock.uclock_now = uclock_std_now; uclock_std->uclock.uclock_to_real = uclock_std_to_real; uclock_std->uclock.uclock_from_real = uclock_std_from_real; #ifdef __MACH__ memcpy(&uclock_std->cclock, &cclock, sizeof(cclock)); #endif return uclock_std_to_uclock(uclock_std); }
int64_t zclock_usecs (void) { #if (defined (__UTYPE_OSX) || defined (__UTYPE_IOS)) clock_serv_t cclock; mach_timespec_t mts; host_get_clock_service (mach_host_self (), SYSTEM_CLOCK, &cclock); clock_get_time (cclock, &mts); mach_port_deallocate (mach_task_self (), cclock); return (int64_t) ((int64_t) mts.tv_sec * 1000000 + (int64_t) mts.tv_nsec / 1000); #elif defined (__UNIX__) struct timespec ts; clock_gettime (CLOCK_MONOTONIC, &ts); return (int64_t) ((int64_t) ts.tv_sec * 1000000 + (int64_t) ts.tv_nsec / 1000); #elif (defined (__WINDOWS__)) // System frequency does not change at run-time, cache it static int64_t frequency = 0; if (frequency == 0) { LARGE_INTEGER freq; QueryPerformanceFrequency (&freq); // Windows documentation says that XP and later will always return non-zero assert (freq.QuadPart != 0); frequency = freq.QuadPart; } LARGE_INTEGER count; QueryPerformanceCounter (&count); return (int64_t) (count.QuadPart * 1000000) / frequency; #endif }
void h_benchmark_clock_gettime(struct timespec *ts) { if (ts == NULL) return; #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time /* * This returns real time, not CPU time. See http://stackoverflow.com/a/6725161 * Possible solution: http://stackoverflow.com/a/11659289 */ 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); ts->tv_sec = mts.tv_sec; ts->tv_nsec = mts.tv_nsec; #elif defined(__NetBSD__) // NetBSD doesn't have CLOCK_THREAD_CPUTIME_ID. We'll use getrusage instead struct rusage rusage; getrusage(RUSAGE_SELF, &rusage); ts->tv_nsec = (rusage.ru_utime.tv_usec + rusage.ru_stime.tv_usec) * 1000; // not going to overflow; can be at most 2e9-2 ts->tv_sec = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_sec; if (ts->tv_nsec >= 1000000000) { ts->tv_nsec -= 1000000000; // subtract a second ts->tv_sec += 1; // add it back. } assert (ts->tv_nsec <= 1000000000); #else clock_gettime(CLOCK_THREAD_CPUTIME_ID, ts); #endif }
void cron_periodic(void) /* {{{ */ { /* convert time to something useful */ struct clock_datetime_t d; uint32_t timestamp = clock_get_time(); /* Only check the tasks every minute */ if ((timestamp - last_check) < 60) return; clock_datetime(&d, timestamp); struct cron_event_t event; /* check every event for a match */ for (uint8_t i = 0; ; i++) { memcpy_P(&event, &events[i], sizeof(struct cron_event_t)); /* end of task list reached */ if (event.handler == NULL) break; uint8_t r = cron_check_event(&event, &d); /* if it matches, execute the handler function */ if (r > 0) { event.handler(); } } /* save the actual timestamp */ last_check = timestamp - d.sec; } /* }}} */
/** Initializes the object. @return true if initialized correctly, false otherwise */ bool IND_Math::init() { end(); freeVars(); //----- Random seed ------ #ifdef PLATFORM_LINUX struct timespec tp; clock_gettime(CLOCK_MONOTONIC, &tp); srand(tp.tv_nsec); #endif // OS X does not have clock_gettime, use clock_get_time // Great solution from StackOverflow:http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x #if defined (PLATFORM_IOS) || defined (PLATFORM_OSX) struct timespec tp; 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); tp.tv_sec = mts.tv_sec; tp.tv_nsec = mts.tv_nsec; srand(static_cast<unsigned>(tp.tv_nsec)); #endif #ifdef PLATFORM_WIN32 srand(GetTickCount()); #endif _ok = true; return _ok; }
/* Returns the current nanoseconds count from the system clock. Is not monotonic. RETURNS nanoseconds */ u64 current_nanoseconds() { struct timespec ts; #ifdef __MACH__ /* macOS */ 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); ts.tv_sec = mts.tv_sec; ts.tv_nsec = mts.tv_nsec; #else /* Linux, BSD */ /* POSIX.1 conforming */ #ifdef _POSIX_MONOTONIC_CLOCK clock_gettime(CLOCK_MONOTONIC, &ts); #else clock_gettime(CLOCK_REALTIME, &ts); #endif #endif return ts.tv_nsec; }
bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us) { struct timespec now = {0}; #ifdef __MACH__ // OSX doesn't have clock_gettime ... :( 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); now.tv_sec = mts.tv_sec; now.tv_nsec = mts.tv_nsec; #elif defined(__CELLOS_LV2__) sys_time_sec_t s; sys_time_nsec_t n; sys_time_get_current_time(&s, &n); now.tv_sec = s; now.tv_nsec = n; #elif !defined(GEKKO) // timeout on libogc is duration, not end time clock_gettime(CLOCK_REALTIME, &now); #endif now.tv_sec += timeout_us / 1000000LL; now.tv_nsec += timeout_us * 1000LL; now.tv_sec += now.tv_nsec / 1000000000LL; now.tv_nsec = now.tv_nsec % 1000000000LL; int ret = pthread_cond_timedwait(&cond->cond, &lock->lock, &now); return ret == 0; }
unsigned int tx_getticks(void) { #if defined(__linux__) || defined(__FreeBSD__) int err; struct timespec ts; err = clock_gettime(CLOCK_MONOTONIC, &ts); TX_CHECK(err == 0, "clock_gettime failure"); return tx_ticks = (ts.tv_sec * 1000 + ts.tv_nsec / 1000000); #elif defined(WIN32) LARGE_INTEGER now = {0}; static LARGE_INTEGER bootup = {0}; static LARGE_INTEGER frequency = {0}; if (frequency.QuadPart == 0) { QueryPerformanceCounter(&bootup); QueryPerformanceFrequency(&frequency); } QueryPerformanceCounter(&now); return tx_ticks = (now.QuadPart - bootup.QuadPart) * 1000LL / frequency.QuadPart; #elif defined(__APPLE__) clock_serv_t cclock; mach_timespec_t ts; host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); clock_get_time(cclock, &ts); mach_port_deallocate(mach_task_self(), cclock); return tx_ticks = (ts.tv_sec * 1000 + ts.tv_nsec / 1000000); #endif }
unsigned int timer_read(tim_t dev) { if (dev >= TIMER_NUMOF) { return 0; } struct timespec t; DEBUG("timer_read()\n"); _native_syscall_enter(); #ifdef __MACH__ clock_serv_t cclock; mach_timespec_t mts; host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); clock_get_time(cclock, &mts); mach_port_deallocate(mach_task_self(), cclock); t.tv_sec = mts.tv_sec; t.tv_nsec = mts.tv_nsec; #else if (real_clock_gettime(CLOCK_MONOTONIC, &t) == -1) { err(EXIT_FAILURE, "timer_read: clock_gettime"); } #endif _native_syscall_leave(); return ts2ticks(&t) - time_null; }
uint16_t rainmaster_get_levelsensor(void) { uint16_t adc; uint16_t level_average = 0; int8_t i; ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { capture_ts = clock_get_time(); get_ts = capture_ts; for (i=0; i<32; i++) { capt_adc_levelsensor = adc_get(RAINMASTER_LEVEL_SENSOR); level_average += capt_adc_levelsensor; } } adc = level_average / 32L; //long adc_waterlevel = (((long)(adc) * 5080)/1024L); //return (uint16_t)( adc_waterlevel ); return adc; }
static AJ_Status WaitForData(uint32_t ms) { struct timespec ts; int ret; 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); ts.tv_sec = mts.tv_sec; ts.tv_nsec = mts.tv_nsec; //clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += ms / 1000; ts.tv_nsec += (ms % 1000) * 1000000; pthread_mutex_lock(&mutex); ret = pthread_cond_timedwait(&cond, &mutex, &ts); pthread_mutex_unlock(&mutex); if (ret) { return ret == ETIMEDOUT ? AJ_ERR_TIMEOUT : AJ_ERR_DRIVER; } else { return AJ_OK; } }
// Mac OS X static void * thread_func(void * arg) { scoped_timer::imp * st = static_cast<scoped_timer::imp*>(arg); pthread_mutex_t mutex; clock_serv_t host_clock; struct timespec abstime; mach_timespec_t now; unsigned long long nano = static_cast<unsigned long long>(st->m_interval) * 1000000ull; host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &host_clock); if (pthread_mutex_init(&mutex, NULL) != 0) throw default_exception("failed to initialize timer mutex"); if (pthread_cond_init(&st->m_condition_var, NULL) != 0) throw default_exception("failed to initialize timer condition variable"); abstime.tv_sec = nano / 1000000000ull; abstime.tv_nsec = nano % 1000000000ull; pthread_mutex_lock(&mutex); clock_get_time(host_clock, &now); ADD_MACH_TIMESPEC(&abstime, &now); int e = pthread_cond_timedwait(&st->m_condition_var, &mutex, &abstime); if (e != 0 && e != ETIMEDOUT) throw default_exception("failed to start timed wait"); st->m_eh->operator()(); pthread_mutex_unlock(&mutex); if (pthread_mutex_destroy(&mutex) != 0) throw default_exception("failed to destroy pthread mutex"); if (pthread_cond_destroy(&st->m_condition_var) != 0) throw default_exception("failed to destroy pthread condition variable"); return st; }
int clock_gettime(clockid_t clk_id, struct timespec *tp) { kern_return_t ret; clock_serv_t cclock; mach_timespec_t mts; ret = host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); if (ret != 0) { errno = EINVAL; return -1; } ret = clock_get_time(cclock, &mts); if (ret != 0) { goto clock_gettime_out; } tp->tv_sec = mts.tv_sec; tp->tv_nsec = mts.tv_nsec; clock_gettime_out: if (mach_port_deallocate(mach_task_self(), cclock) != 0 || ret != 0) { errno = EINVAL; return -1; } else { return 0; } }
UMicroseconds& UMicroseconds::ReadTime() { // Note: g_get_monotonic_time() may be a viable alternative // (it is on Linux and OSX); if not, this code should really go into libpbd #ifdef PLATFORM_WINDOWS LARGE_INTEGER Frequency, Count ; QueryPerformanceFrequency(&Frequency) ; QueryPerformanceCounter(&Count); theTime = uint64_t((Count.QuadPart * 1000000.0 / Frequency.QuadPart)); #elif defined __MACH__ // OSX, BSD.. clock_serv_t cclock; mach_timespec_t mts; host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); clock_get_time(cclock, &mts); mach_port_deallocate(mach_task_self(), cclock); theTime = (uint64_t)mts.tv_sec * 1e6 + (uint64_t)mts.tv_nsec / 1000; #else // Linux, POSIX struct timespec *ts clock_gettime(CLOCK_MONOTONIC, ts); theTime = (uint64_t)ts.tv_sec * 1e6 + (uint64_t)buf.tv_nsec / 1000; #endif return *this; }
/* increment_time: increments the current time with one second */ void increment_time(void) { //Local time variables int Hours; int Minutes; int Seconds; clock_get_time(&Hours, &Minutes, &Seconds); /* increment time */ Seconds++; if (Seconds > 59) { Seconds = 0; Minutes++; if (Minutes > 59) { Minutes = 0; Hours++; if (Hours > 23) { Hours = 0; } } } clock_set_time(Hours, Minutes, Seconds); }
double getTime( void ) { struct timespec tspec; #ifdef ANDROID int time = clock_gettime( CLOCK_REALTIME, &tspec ); #else //struct timeb currTime; //ftime( &currTime ); //double fMilliSec = (double)( currTime.time * 1000 + currTime.millitm ); 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 ); tspec.tv_sec = mts.tv_sec; tspec.tv_nsec = mts.tv_nsec; #endif // ANDROID unsigned long long nanoSec = (unsigned long long)tspec.tv_sec * 1000000000 + (unsigned long long)tspec.tv_nsec; double fMilliSec = (double)( nanoSec / 1000000 ); return fMilliSec; }