Ejemplo n.º 1
0
cv_status condition_variable::wait_until(unique_lock<mutex>& lock,
                                         const time_point& timeout_time) {
  xtimer_t timer;
  // todo: use function to wait for absolute timepoint once available
  timex_t before;
  xtimer_now_timex(&before);
  auto diff = timex_sub(timeout_time.native_handle(), before);
  xtimer_set_wakeup(&timer, timex_uint64(diff), sched_active_pid);
  wait(lock);
  timex_t after;
  xtimer_now_timex(&after);
  xtimer_remove(&timer);
  auto cmp = timex_cmp(after, timeout_time.native_handle());
  return cmp < 1 ? cv_status::no_timeout : cv_status::timeout;
}
Ejemplo n.º 2
0
Archivo: queue.hpp Proyecto: ebnn/retro
    /*! Retroactively revert a previous operation.
     *  \param t The time point to revert.
     */
    void revert(const time_point &t)
    {
      if (t.operation() == queue::push)
      {
        // This element was never pushed.
        --size_;

        // If t occurs before front, then that means we've previously popped an
        // element that no longer exists, so it should have popped the element
        // after it. Hence, move the front to its successor.
        if (t.it->second)
        {
          move_front_succ();
        }
        else if (front_ == t.it)
        {
          // We're removing the front, so move it right to ensure validity.
          move_front_succ();
        }

        // Remove the element corresponding to the push.
        data_.erase(t.it);
      }
      else
      {
        move_front_pred();
        ++size_; // One less pop means the size increases by 1
      }
    }
Ejemplo n.º 3
0
std::time_t
VirtualClock::to_time_t(time_point point)
{
    return static_cast<std::time_t>(
        std::chrono::duration_cast<std::chrono::seconds>(
            point.time_since_epoch()).count());
}
Ejemplo n.º 4
0
	static struct timespec to_timespec(const time_point& tp) {
		using std::chrono::seconds;
		using std::chrono::nanoseconds;
		using std::chrono::duration_cast;
		duration d = tp.time_since_epoch();
		seconds sec = duration_cast<seconds> (d);
		nanoseconds nsec = duration_cast<nanoseconds> (d - sec);
		struct timespec result = { sec.count(), nsec.count() };
		return result;
	}
Ejemplo n.º 5
0
void
timer::schedule_next_tick
    ( time_point const& expiration_time )
{
    // This will cancel any pending task.
    timer_.expires_at( expiration_time );

    LOG_DEBUG( timer, this ) << "schedule callback at "
            << expiration_time.time_since_epoch().count()
            << "." << std::endl;

    auto on_fire = [ this ]( boost::system::error_code const& failure )
    {
        // The current timeout has been canceled
        // hence stop right there.
        if ( failure == boost::asio::error::operation_aborted )
            return;

        if ( failure )
            throw std::system_error{ make_error_code( TIMER_MALFUNCTION ) };

        // The callbacks to execute are the first
        // n callbacks with the same keys.
        auto begin = timeouts_.begin();
        auto end = timeouts_.upper_bound( begin->first );
        // Call the user callbacks.
        for ( auto i = begin; i != end; ++ i )
            i->second();

        LOG_DEBUG( timer, this )
                << "remove " << std::distance( begin, end )
                << " callback(s) scheduled at "
                << begin->first.time_since_epoch().count()
                << "." << std::endl;

        // And remove the timeout.
        timeouts_.erase( begin, end );

        // If there is a remaining timeout, schedule it.
        if ( ! timeouts_.empty() )
        {
            LOG_DEBUG( timer, this )
                    << "schedule remaining timers" << std::endl;
            schedule_next_tick( timeouts_.begin()->first );
        }
    };

    timer_.async_wait( on_fire );
}
Ejemplo n.º 6
0
Archivo: log.cpp Proyecto: qis/ice
std::string format(time_point tp, bool date, bool milliseconds) {
  auto time = clock::to_time_t(tp);
  tm tm = {};
#ifndef _WIN32
  localtime_r(&time, &tm);
#else
  localtime_s(&tm, &time);
#endif

  auto Y = tm.tm_year + 1900;
  auto M = tm.tm_mon + 1;
  auto D = tm.tm_mday;
  auto h = tm.tm_hour;
  auto m = tm.tm_min;
  auto s = tm.tm_sec;
  auto ms = 0;

  if (milliseconds) {
    const auto t = tp.time_since_epoch();
    const auto s = std::chrono::duration_cast<std::chrono::seconds>(t);
    const auto m = std::chrono::duration_cast<std::chrono::milliseconds>(t) - s;
    ms = static_cast<int>(m.count());
  }

  int size = 0;
  std::string str;
  if (date && milliseconds) {
    str.resize(23 + 1);
    size = std::snprintf(&str[0], str.size(), "%04d-%02d-%02d %02d:%02d:%02d.%03d", Y, M, D, h, m, s, ms);
  } else if (date && !milliseconds) {
    str.resize(19 + 1);
    size = std::snprintf(&str[0], str.size(), "%04d-%02d-%02d %02d:%02d:%02d", Y, M, D, h, m, s);
  } else if (!date && milliseconds) {
    str.resize(12 + 1);
    size = std::snprintf(&str[0], str.size(), "%02d:%02d:%02d.%03d", h, m, s, ms);
  } else {
    str.resize(8 + 1);
    size = std::snprintf(&str[0], str.size(), "%02d:%02d:%02d", h, m, s);
  }
  str.resize(size > 0 ? size : 0);
  return str;
}
Ejemplo n.º 7
0
TouchArray TouchTracker::getTestTouches(time_point<system_clock> now, int maxTouches)
{
	TouchArray t{};

	setMaxTouches(maxTouches);
	
	system_clock::duration dtn = now.time_since_epoch();
	auto m = std::chrono::duration_cast<std::chrono::milliseconds>(dtn).count();
	auto c = m%60000;
	int ic = c;
	float fc = ic;
	
	for(int i=0; i<maxTouches; ++i)
	{
		float minusI = 16-i;
		float rpm = 2.f*(i + 1);
		float theta = fc*rpm*kTwoPi/60000.f;
		float rx = 12.f/16.*minusI;
		float ry = 2.f/16.*minusI;
		float cx = 15.f;
		float cy = 2.5f;
		
		float x = cosf(theta)*rx + cx;
		float y = sinf(theta)*ry + cy;
		
		float theta2 = theta*8.f;
		float amp = sinf(theta2)*0.25f;
		
		t[i] = Touch{.x = x, .y = y, .z = amp};
	}
	
	mTouches = t;
	// asymmetrical z filter from user setting. Ages are created here.
	mTouches = filterTouchesZ(mTouches, mTouches2, mLopassZ*2.f, mLopassZ*0.25f);
	mTouches2 = mTouches;
	mTouches = clampAndScaleTouches(mTouches);
	
	clearAndSendNextFrameIfNeeded();
	return mTouches;
}
Ejemplo n.º 8
0
      iter_type put(time_point_units<CharT> const& units_facet, iter_type s, std::ios_base& ios, char_type fill,
          time_point<Clock, Duration> const& tp, const CharT* pattern, const CharT* pat_end) const
      {

        const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
        for (; pattern != pat_end; ++pattern)
        {
          if (ct.narrow(*pattern, 0) == '%')
          {
            if (++pattern == pat_end)
            {
              *s++ = pattern[-1];
              break;
            }
            char fmt = ct.narrow(*pattern, 0);
            switch (fmt)
            {
            case 'd':
            {
              s = put_duration(s, ios, fill, tp.time_since_epoch());
              break;
            }
            case 'e':
            {
              s = put_epoch<Clock> (units_facet, s, ios);
              break;
            }
            default:
              AUTOBOOST_ASSERT(false && "Boost::Chrono internal error.");
              break;
            }
          }
          else
            *s++ = *pattern;
        }
        return s;
      }
Ejemplo n.º 9
0
Archivo: time.hpp Proyecto: VicHao/fc
 time_point_sec( const time_point& t )
 :utc_seconds( t.time_since_epoch().count() / 1000000ll ){}
Ejemplo n.º 10
0
time_t cf_clock::to_time_t(const time_point &__t) noexcept
{
    return time_t(__t.time_since_epoch().count() + kCFAbsoluteTimeIntervalSince1970);
}
Ejemplo n.º 11
0
/* Conversion to system time */
static
time_point to_system_time(const time_point& t) {
	static std::atomic<int64_t> ns_offset(0);
	static std::atomic<int64_t> offset_timestamp(0);
	static std::atomic_flag lock = ATOMIC_FLAG_INIT;

	static const duration OFFSET_TIMEOUT (15 * (int64_t)1E9, time_unit::NSEC);
	static const duration CLOSE_DISTANCE (15 * (int64_t)1E3, time_unit::NSEC);
	static const uint64_t MAX_UPDATE_TRIES (100);

	time_point current_tsc_time = tsc_clock::now();
	time_unit tsc_unit = current_tsc_time.time_since_epoch().unit();

	int64_t offset_ts = offset_timestamp.load(std::memory_order_acquire);

	if (offset_ts == 0 ||
			current_tsc_time.time_since_epoch() - duration(offset_ts, tsc_unit) > OFFSET_TIMEOUT
		)
	{
		if (!lock.test_and_set(std::memory_order_acquire)) {
			time_point cycles_start, cycles_end;
			time_point current_system_time;

			bool close_pair_found = false;
			for (uint64_t update_try = 0; update_try < MAX_UPDATE_TRIES; ++update_try) {
				cycles_start = tsc_clock::now();
				current_system_time = system_clock::now();
				cycles_end = tsc_clock::now();

				if (cycles_end - cycles_start < CLOSE_DISTANCE) {
					close_pair_found = true;
					break;
				}
			}

			if (close_pair_found) {
				time_point cycles_middle = cycles_start + (cycles_end - cycles_start) / 2;
				int64_t new_offset =
					duration::convert_to(
						time_unit::NSEC,
						current_system_time.time_since_epoch() - cycles_middle.time_since_epoch()
					)
					.count();

				ns_offset.store(new_offset, std::memory_order_release);
				offset_timestamp.store(cycles_middle.time_since_epoch().count(), std::memory_order_release);
			}

			lock.clear(std::memory_order_release);
		}
	}

	return
		time_point(
			duration::convert_to(
				time_unit::NSEC,
				t.time_since_epoch() + duration(ns_offset.load(std::memory_order_acquire), time_unit::NSEC)
			),
			clock_type::SYSTEM
		);
}
			time_point(time_point<clock, Duration2> const & other) : duration_(other.time_since_epoch()) {}
Ejemplo n.º 13
0
	static ::timespec
	to_native(const time_point& tp)
	{ return to_native_duration(tp.time_since_epoch()); }