inline bool posix_mutex::timed_lock(const hexerboost::posix_time::ptime &abs_time) { if(abs_time == hexerboost::posix_time::pos_infin){ this->lock(); return true; } #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS timespec ts = ptime_to_timespec(abs_time); int res = pthread_mutex_timedlock(&m_mut, &ts); if (res != 0 && res != ETIMEDOUT) throw lock_exception(); return res == 0; #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS //Obtain current count and target time hexerboost::posix_time::ptime now = microsec_clock::universal_time(); do{ if(this->try_lock()){ break; } now = microsec_clock::universal_time(); if(now >= abs_time){ return false; } // relinquish current time slice thread_yield(); }while (true); return true; #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS }
inline bool semaphore_timed_wait(sem_t *handle, const boost::posix_time::ptime &abs_time) { #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS //Posix does not support infinity absolute time so handle it here if(abs_time == boost::posix_time::pos_infin){ semaphore_wait(handle); return true; } timespec tspec = ptime_to_timespec(abs_time); for (;;){ int res = sem_timedwait(handle, &tspec); if(res == 0) return true; if (res > 0){ //buggy glibc, copy the returned error code to errno errno = res; } if(system_error_code() == ETIMEDOUT){ return false; } error_info err = system_error_code(); throw interprocess_exception(err); } return false; #else //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS semaphore_wrapper_try_wrapper swtw(handle); ipcdetail::lock_to_wait<semaphore_wrapper_try_wrapper> lw(swtw); return ipcdetail::try_based_timed_lock(lw, abs_time); #endif //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS }
inline bool semaphore_timed_wait(sem_t *handle, const boost::posix_time::ptime &abs_time) { if(abs_time == boost::posix_time::pos_infin){ semaphore_wait(handle); return true; } #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS timespec tspec = ptime_to_timespec(abs_time); for (;;){ int res = sem_timedwait(handle, &tspec); if(res == 0) return true; if (res > 0){ //buggy glibc, copy the returned error code to errno errno = res; } if(system_error_code() == ETIMEDOUT){ return false; } throw interprocess_exception(system_error_code()); } return false; #else //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS boost::posix_time::ptime now; do{ if(semaphore_try_wait(handle)) return true; thread_yield(); }while((now = microsec_clock::universal_time()) < abs_time); return false; #endif //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS }
inline bool posix_condition::do_timed_wait (const hexerboost::posix_time::ptime &abs_time, posix_mutex &mut) { timespec ts = ptime_to_timespec(abs_time); pthread_mutex_t* pmutex = &mut.m_mut; int res = 0; res = pthread_cond_timedwait(&m_condition, pmutex, &ts); BOOST_ASSERT(res == 0 || res == ETIMEDOUT); return res != ETIMEDOUT; }
inline bool posix_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time) { #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS //Posix does not support infinity absolute time so handle it here if(abs_time == boost::posix_time::pos_infin){ this->lock(); return true; } timespec ts = ptime_to_timespec(abs_time); int res = pthread_mutex_timedlock(&m_mut, &ts); if (res != 0 && res != ETIMEDOUT) throw lock_exception(); return res == 0; #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS return ipcdetail::try_based_timed_lock(*this, abs_time); #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS }