/*! Executes the task function. */ void operator() (void) const { if(m_function) { if(m_break_s > 0 || m_break_ns > 0) { // Sleep some time before first execution xtime xt; xtime_get(&xt, TIME_UTC_); xt.nsec += m_break_ns; xt.sec += m_break_s; thread::sleep(xt); } while(m_function()) { if(m_break_s > 0 || m_break_ns > 0) { xtime xt; xtime_get(&xt, TIME_UTC_); xt.nsec += m_break_ns; xt.sec += m_break_s; thread::sleep(xt); } else { thread::yield(); // Be fair to other threads } } } }
void _Thrd_sleep(const xtime *xt) { /* suspend thread until time xt */ xtime now; xtime_get(&now, TIME_UTC); do { /* sleep and check time */ Sleep(_Xtime_diff_to_millis2(xt, &now)); xtime_get(&now, TIME_UTC); } while (now.sec < xt->sec || now.sec == xt->sec && now.nsec < xt->nsec); }
static void thread_sleep(unsigned long _sec, unsigned long _nanosec) { struct xtime xt = {0, 0}; xtime_get(&xt, TIME_UTC); xt.sec += _sec; xt.nsec += _nanosec; thrd_sleep(&xt); }
void yield() { # if defined(BOOST_HAS_SCHED_YIELD) BOOST_VERIFY(!sched_yield()); # elif defined(BOOST_HAS_PTHREAD_YIELD) BOOST_VERIFY(!pthread_yield()); # else xtime xt; xtime_get(&xt, TIME_UTC); sleep(xt); # endif }
boost::xtime CCondition::getWaitTime(int timeoutms) { boost::xtime xt; xtime_get(&xt, boost::TIME_UTC); if( timeoutms>1000 ) { xt.sec+=timeoutms/1000; timeoutms=timeoutms%1000; } xt.nsec+=timeoutms*1000000; return xt; }
main() { thrd_t thrd1; printf("%d\n", mtx_init(&mtx1, mtx_plain)); thrd_create(&thrd1, thread1, 0); thrd_join(thrd1, NULL); mtx_destroy(&mtx1); printf("\n%d\n", mtx_init(&mtx1, mtx_plain | mtx_recursive)); thrd_create(&thrd1, thread1, 0); thrd_join(thrd1, NULL); mtx_destroy(&mtx1); mtx_init(&mtx1, mtx_try); thrd_create(&thrd1, thread3, 0); Sleep(500); printf("%d\n", mtx_trylock(&mtx1)); xtime xt; xtime_get(&xt, TIME_UTC); xt.nsec += 200000000; printf("%d\n", mtx_timedlock(&mtx1, &xt)); xt.nsec += 500000000; printf("%d\n", mtx_timedlock(&mtx1, &xt)); mtx_unlock(&mtx1); printf("%d\n", mtx_trylock(&mtx1)); mtx_unlock(&mtx1); thrd_create(&thrd1, thread4, 0); thrd_detach(thrd1); thrd_create(&thrd1, thread5, 0); thrd_join(thrd1, NULL); thrd_create(&thrd1, thread2, 0); thrd_detach(thrd1); thrd_create(&thrd1, thread2, (void *)1); thrd_detach(thrd1); Sleep(4000); thrd_create(&thrd1, thread2, (void *)2); thrd_detach(thrd1); thrd_create(&thrd1, thread2, (void *)3); thrd_detach(thrd1); Sleep(4000); mtx_destroy(&mtx1); }
void thread::sleep(const xtime& xt) { for (int foo=0; foo < 5; ++foo) { #if defined(BOOST_HAS_WINTHREADS) int milliseconds; to_duration(xt, milliseconds); Sleep(milliseconds); #elif defined(BOOST_HAS_PTHREADS) # if defined(BOOST_HAS_PTHREAD_DELAY_NP) timespec ts; to_timespec_duration(xt, ts); int res = 0; res = pthread_delay_np(&ts); assert(res == 0); # elif defined(BOOST_HAS_NANOSLEEP) timespec ts; to_timespec_duration(xt, ts); // nanosleep takes a timespec that is an offset, not // an absolute time. nanosleep(&ts, 0); # else mutex mx; mutex::scoped_lock lock(mx); condition cond; cond.timed_wait(lock, xt); # endif #elif defined(BOOST_HAS_MPTASKS) int microseconds; to_microduration(xt, microseconds); Duration lMicroseconds(kDurationMicrosecond * microseconds); AbsoluteTime sWakeTime(DurationToAbsolute(lMicroseconds)); threads::mac::detail::safe_delay_until(&sWakeTime); #endif xtime cur; xtime_get(&cur, TIME_UTC); if (xtime_cmp(xt, cur) <= 0) return; } }
void sleep(const system_time& st) { detail::thread_data_base* const thread_info=detail::get_current_thread_data(); if(thread_info) { unique_lock<mutex> lk(thread_info->sleep_mutex); while(thread_info->sleep_condition.timed_wait(lk,st)); } else { xtime const xt=get_xtime(st); for (int foo=0; foo < 5; ++foo) { # if defined(BOOST_HAS_PTHREAD_DELAY_NP) timespec ts; to_timespec_duration(xt, ts); BOOST_VERIFY(!pthread_delay_np(&ts)); # elif defined(BOOST_HAS_NANOSLEEP) timespec ts; to_timespec_duration(xt, ts); // nanosleep takes a timespec that is an offset, not // an absolute time. nanosleep(&ts, 0); # else mutex mx; mutex::scoped_lock lock(mx); condition cond; cond.timed_wait(lock, xt); # endif xtime cur; xtime_get(&cur, TIME_UTC); if (xtime_cmp(xt, cur) <= 0) return; } } }
void thread::yield() { #if defined(BOOST_HAS_WINTHREADS) Sleep(0); #elif defined(BOOST_HAS_PTHREADS) # if defined(BOOST_HAS_SCHED_YIELD) int res = 0; res = sched_yield(); assert(res == 0); # elif defined(BOOST_HAS_PTHREAD_YIELD) int res = 0; res = pthread_yield(); assert(res == 0); # else xtime xt; xtime_get(&xt, TIME_UTC); sleep(xt); # endif #elif defined(BOOST_HAS_MPTASKS) MPYield(); #endif }
void MessagesQueue::insert( SMSRequest::PTR req, SMSMessage::ID msgid, list< string > gateways ) { xtime now; xtime_get( &now, TIME_UTC ); MessageInfo mi; mi.msgid = msgid; mi.gateway = *gateways.begin(); mi.added= now.sec; try { mi.partner_priority = PartnerManager::get_mutable_instance().findById( req->pid ).pPriority; } catch (const PartnerNotFoundError&) { mi.partner_priority = 0; } mi.message_priority; mi.idp = req->pid; mi.txt = req->msg; mi.to = req->to[ msgid.msg_num ]; mi.gateways = gateways; mi.req = req; recursive_mutex::scoped_lock lck( mq_lock ); data.get< SEQUENCE >().push_back( mi ); }
long _Xtime_diff_to_millis(const xtime *xt) { /* convert time to milliseconds */ xtime now; xtime_get(&now, TIME_UTC); return (_Xtime_diff_to_millis2(xt, &now)); }
clock() { xtime_get(this); }
static int mtx_do_lock(_Mtx_t mtx, const xtime *target) { /* lock mutex */ if ((mtx->type & ~_Mtx_recursive) == _Mtx_plain) { /* set the lock */ if (mtx->thread_id != static_cast<long>(GetCurrentThreadId())) { /* not current thread, do lock */ mtx->_get_cs()->lock(); mtx->thread_id = static_cast<long>(GetCurrentThreadId()); } ++mtx->count; return (_Thrd_success); } else { /* handle timed or recursive mutex */ int res = WAIT_TIMEOUT; if (target == 0) { /* no target --> plain wait (i.e. infinite timeout) */ if (mtx->thread_id != static_cast<long>(GetCurrentThreadId())) mtx->_get_cs()->lock(); res = WAIT_OBJECT_0; } else if (target->sec < 0 || target->sec == 0 && target->nsec <= 0) { /* target time <= 0 --> plain trylock or timed wait for */ /* time that has passed; try to lock with 0 timeout */ if (mtx->thread_id != static_cast<long>(GetCurrentThreadId())) { /* not this thread, lock it */ if (mtx->_get_cs()->try_lock()) res = WAIT_OBJECT_0; else res = WAIT_TIMEOUT; } else res = WAIT_OBJECT_0; } else { /* check timeout */ xtime now; xtime_get(&now, TIME_UTC); while (now.sec < target->sec || now.sec == target->sec && now.nsec < target->nsec) { /* time has not expired */ if (mtx->thread_id == static_cast<long>(GetCurrentThreadId()) || mtx->_get_cs()->try_lock_for( _Xtime_diff_to_millis2(target, &now))) { /* stop waiting */ res = WAIT_OBJECT_0; break; } else res = WAIT_TIMEOUT; xtime_get(&now, TIME_UTC); } } if (res != WAIT_OBJECT_0 && res != WAIT_ABANDONED) ; else if (1 < ++mtx->count) { /* check count */ if ((mtx->type & _Mtx_recursive) != _Mtx_recursive) { /* not recursive, fixup count */ --mtx->count; res = WAIT_TIMEOUT; } } else mtx->thread_id = static_cast<long>(GetCurrentThreadId()); switch (res) { case WAIT_OBJECT_0: case WAIT_ABANDONED: return (_Thrd_success); case WAIT_TIMEOUT: if (target == 0 || (target->sec == 0 && target->nsec == 0)) return (_Thrd_busy); else return (_Thrd_timedout); default: return (_Thrd_error); } } }