Ejemplo n.º 1
0
inline absolute_time_duration make_absolute_time_duration(std::chrono::nanoseconds time) {
  auto& t = impl::get_mach_timebase_info_data();
  if (t.numer != t.denom && t.numer != 0) {
    return absolute_time_duration(time.count() * t.denom / t.numer);
  }
  return absolute_time_duration(time.count());
}
Ejemplo n.º 2
0
SharedPtr<Event> HLERequestContext::SleepClientThread(SharedPtr<Thread> thread,
                                                      const std::string& reason,
                                                      std::chrono::nanoseconds timeout,
                                                      WakeupCallback&& callback) {
    // Put the client thread to sleep until the wait event is signaled or the timeout expires.
    thread->wakeup_callback = [context = *this, callback](ThreadWakeupReason reason,
                                                          SharedPtr<Thread> thread,
                                                          SharedPtr<WaitObject> object) mutable {
        ASSERT(thread->status == THREADSTATUS_WAIT_HLE_EVENT);
        callback(thread, context, reason);

        auto& process = thread->owner_process;
        // We must copy the entire command buffer *plus* the entire static buffers area, since
        // the translation might need to read from it in order to retrieve the StaticBuffer
        // target addresses.
        std::array<u32_le, IPC::COMMAND_BUFFER_LENGTH + 2 * IPC::MAX_STATIC_BUFFERS> cmd_buff;
        Memory::ReadBlock(*process, thread->GetCommandBufferAddress(), cmd_buff.data(),
                          cmd_buff.size() * sizeof(u32));
        context.WriteToOutgoingCommandBuffer(cmd_buff.data(), *process, Kernel::g_handle_table);
        // Copy the translated command buffer back into the thread's command buffer area.
        Memory::WriteBlock(*process, thread->GetCommandBufferAddress(), cmd_buff.data(),
                           cmd_buff.size() * sizeof(u32));
    };

    auto event = Kernel::Event::Create(Kernel::ResetType::OneShot, "HLE Pause Event: " + reason);
    thread->status = THREADSTATUS_WAIT_HLE_EVENT;
    thread->wait_objects = {event};
    event->AddWaitingThread(thread);

    if (timeout.count() > 0)
        thread->WakeAfterDelay(timeout.count());

    return event;
}
Ejemplo n.º 3
0
void Timer::sleep(std::chrono::nanoseconds duration) {
  assert(dispatcher != nullptr);
  assert(context == nullptr);
  if (stopped) {
    throw InterruptedException();
  }

  LARGE_INTEGER duration2;
  duration2.QuadPart = static_cast<LONGLONG>(duration.count() / -100);
  Context context2 = {dispatcher, GetCurrentFiber(), false};
  if (SetWaitableTimer(timer, &duration2, 0, callbackProcedure, &context2, FALSE) != TRUE) {
    std::cerr << "SetWaitableTimer failed, result=" << GetLastError() << '.' << std::endl;
    throw std::runtime_error("Timer::sleep");
  }

  context = &context2;
  dispatcher->yield();
  assert(dispatcher != nullptr);
  assert(context2.context == nullptr);
  assert(context == &context2);
  context = nullptr;
  if (context2.interrupted) {
    throw InterruptedException();
  }
}
Ejemplo n.º 4
0
std::shared_ptr<LooperSource>
LooperSource::AsTimer(std::chrono::nanoseconds repeatInterval) {

    WaitSetUpdateHandler updateHandler =
        [repeatInterval](LooperSource *source, WaitSet::Handle waitsetHandle,
                         Handle readHandle, bool adding) {

            LooperSource_UpdateKeventSource(waitsetHandle,
                                            readHandle,
                                            EVFILT_TIMER,
                                            adding ? EV_ADD : EV_DELETE,
                                            NOTE_NSECONDS,
                                            repeatInterval.count(),
                                            source);

        };

    auto timer = std::make_shared<LooperSource>(nullptr,
                                                nullptr,
                                                nullptr,
                                                nullptr);

    timer->setCustomWaitSetUpdateHandler(updateHandler);

    return timer;
}
Ejemplo n.º 5
0
 double get_mb_per_s() const {
     if (d_num_iterations <= 0 || d_duration.count() <= 0 || d_num_bytes <= 0) {
         return 0;
     }
     return ((double(d_num_bytes) * double(d_num_iterations) / double(1e6)) /
             double(std::chrono::duration_cast<std::chrono::seconds>(d_duration).count()));
 }
Ejemplo n.º 6
0
void Timer::sleep(std::chrono::nanoseconds duration) {
  assert(dispatcher != nullptr);
  assert(context == nullptr);
  if (stopped) {
    throw InterruptedException();
  }

  Dispatcher::OperationContext timerContext;
  timerContext.context = dispatcher->getCurrentContext();
  timerContext.interrupted = false;
  timer = dispatcher->getTimer();

  struct kevent event;
  EV_SET(&event, timer, EVFILT_TIMER, EV_ADD | EV_ENABLE | EV_ONESHOT, 0, duration.count() / 1000000, &timerContext);

  if (kevent(dispatcher->getKqueue(), &event, 1, NULL, 0, NULL) == -1) {
    throw std::runtime_error("Timer::stop, kevent() failed, errno=" + std::to_string(errno));
  }

  context = &timerContext;
  dispatcher->dispatch();
  assert(dispatcher != nullptr);
  assert(timerContext.context == dispatcher->getCurrentContext());
  assert(context == &timerContext);
  context = nullptr;
  timerContext.context = nullptr;
  dispatcher->pushTimer(timer);
  if (timerContext.interrupted) {
    throw InterruptedException();
  }
}
Ejemplo n.º 7
0
mir::EventUPtr mie::LibInputDevice::convert_event(libinput_event_keyboard* keyboard)
{
    std::chrono::nanoseconds const time = std::chrono::microseconds(libinput_event_keyboard_get_time_usec(keyboard));
    auto const action = libinput_event_keyboard_get_key_state(keyboard) == LIBINPUT_KEY_STATE_PRESSED ?
                      mir_keyboard_action_down :
                      mir_keyboard_action_up;
    auto const code = libinput_event_keyboard_get_key(keyboard);
    report->received_event_from_kernel(time.count(), EV_KEY, code, action);

    return builder->key_event(time, action, xkb_keysym_t{0}, code);
}
Ejemplo n.º 8
0
bool file::can_write(std::chrono::seconds s, std::chrono::nanoseconds n)
{
    timespec time = { static_cast<std::time_t>(s.count()), static_cast<long>(n.count()) };

    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(_M_fd, &fds);

    int count = pselect(_M_fd+1, 0, &fds, 0, &time, nullptr);
    if(count == -1) throw errno_error();

    return count;
}
Ejemplo n.º 9
0
std::tuple<VkResult, uint32_t> acquire_next_image(swapchain_type &swapchain,
		std::chrono::nanoseconds timeout,
		const semaphore::semaphore_type &semaphore,
		const fence::fence_type &fence) {
	uint32_t image_index;
	std::lock(internal::get_mutex(swapchain), internal::get_mutex(semaphore));
	std::lock_guard<std::mutex> swapchain_lock(internal::get_mutex(swapchain), std::adopt_lock);
	std::lock_guard<std::mutex> semaphore_lock(internal::get_mutex(semaphore), std::adopt_lock);
	std::lock_guard<std::mutex> fence_lock(internal::get_mutex(fence), std::adopt_lock);
	const VkResult result(vkAcquireNextImageKHR(
		internal::get_instance(*internal::get_parent(swapchain)),
		internal::get_instance(swapchain), timeout.count(),
		internal::get_instance(semaphore), internal::get_instance(fence),
		&image_index));
	return std::make_tuple(result, image_index);
}
Ejemplo n.º 10
0
mir::EventUPtr mie::LibInputDevice::convert_motion_event(libinput_event_pointer* pointer)
{
    std::chrono::nanoseconds const time = std::chrono::microseconds(libinput_event_pointer_get_time_usec(pointer));
    auto const action = mir_pointer_action_motion;
    auto const hscroll_value = 0.0f;
    auto const vscroll_value = 0.0f;

    report->received_event_from_kernel(time.count(), EV_REL, 0, 0);

    mir::geometry::Displacement const movement{
        libinput_event_pointer_get_dx(pointer),
        libinput_event_pointer_get_dy(pointer)};

    return builder->pointer_event(time, action, button_state, hscroll_value, vscroll_value, movement.dx.as_float(),
                                  movement.dy.as_float());
}
Ejemplo n.º 11
0
 result run() {
     size_t n = 1;
     run_n(n);
     while (d_duration < d_benchtime && n < 1e9) {
         size_t last = n;
         if (get_ns_per_op() == 0) {
             n = 1e9;
         } else {
             n = d_duration.count() / get_ns_per_op();
         }
         n = std::max(std::min(n+n/2, 100*last), last+1);
         n = round_up(n);
         run_n(n);
     }
     return result(n, d_duration, d_num_bytes);
 }
Ejemplo n.º 12
0
mir::EventUPtr mie::LibInputDevice::convert_axis_event(libinput_event_pointer* pointer)
{
    std::chrono::nanoseconds const time = std::chrono::microseconds(libinput_event_pointer_get_time_usec(pointer));
    auto const action = mir_pointer_action_motion;
    auto const relative_x_value = 0.0f;
    auto const relative_y_value = 0.0f;
    auto const hscroll_value = libinput_event_pointer_has_axis(pointer, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)
        ? horizontal_scroll_scale * libinput_event_pointer_get_axis_value(pointer,
                                                                          LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)
        : 0.0f;
    auto const vscroll_value = libinput_event_pointer_has_axis(pointer, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)
        ? vertical_scroll_scale * libinput_event_pointer_get_axis_value(pointer,
                                                                        LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)
        : 0.0f;

    report->received_event_from_kernel(time.count(), EV_REL, 0, 0);
    return builder->pointer_event(time, action, button_state, hscroll_value, vscroll_value, relative_x_value,
                                  relative_y_value);
}
Ejemplo n.º 13
0
mir::EventUPtr mie::LibInputDevice::convert_absolute_motion_event(libinput_event_pointer* pointer)
{
    // a pointing device that emits absolute coordinates
    std::chrono::nanoseconds const time = std::chrono::microseconds(libinput_event_pointer_get_time_usec(pointer));
    auto const action = mir_pointer_action_motion;
    auto const hscroll_value = 0.0f;
    auto const vscroll_value = 0.0f;
    auto const screen = sink->bounding_rectangle();
    uint32_t const width = screen.size.width.as_int();
    uint32_t const height = screen.size.height.as_int();

    report->received_event_from_kernel(time.count(), EV_ABS, 0, 0);
    auto const old_pointer_pos = pointer_pos;
    pointer_pos = mir::geometry::Point{
        libinput_event_pointer_get_absolute_x_transformed(pointer, width),
        libinput_event_pointer_get_absolute_y_transformed(pointer, height)};
    auto const movement = pointer_pos - old_pointer_pos;

    return builder->pointer_event(time, action, button_state, hscroll_value, vscroll_value, movement.dx.as_float(), movement.dy.as_float());
}
Ejemplo n.º 14
0
mir::EventUPtr mie::LibInputDevice::convert_button_event(libinput_event_pointer* pointer)
{
    std::chrono::nanoseconds const time = std::chrono::microseconds(libinput_event_pointer_get_time_usec(pointer));
    auto const button = libinput_event_pointer_get_button(pointer);
    auto const action = (libinput_event_pointer_get_button_state(pointer) == LIBINPUT_BUTTON_STATE_PRESSED)?
        mir_pointer_action_button_down : mir_pointer_action_button_up;

    auto const do_not_swap_buttons = mir_pointer_handedness_right;
    auto const pointer_button = mie::to_pointer_button(button, do_not_swap_buttons);
    auto const relative_x_value = 0.0f;
    auto const relative_y_value = 0.0f;
    auto const hscroll_value = 0.0f;
    auto const vscroll_value = 0.0f;

    report->received_event_from_kernel(time.count(), EV_KEY, pointer_button, action);

    if (action == mir_pointer_action_button_down)
        button_state = MirPointerButton(button_state | uint32_t(pointer_button));
    else
        button_state = MirPointerButton(button_state & ~uint32_t(pointer_button));

    return builder->pointer_event(time, action, button_state, hscroll_value, vscroll_value, relative_x_value, relative_y_value);
}
Ejemplo n.º 15
0
void Timer::sleep(std::chrono::nanoseconds duration) {
  assert(dispatcher != nullptr);
  assert(context == nullptr);
  if (dispatcher->interrupted()) {
    throw InterruptedException();
  }

  LARGE_INTEGER frequency;
  LARGE_INTEGER ticks;
  QueryPerformanceCounter(&ticks);
  QueryPerformanceFrequency(&frequency);
  uint64_t currentTime = ticks.QuadPart / (frequency.QuadPart / 1000);
  uint64_t time = currentTime + duration.count() / 1000000;
  TimerContext timerContext{ time, dispatcher->getCurrentContext(), false };
  context = &timerContext;
  dispatcher->addTimer(time, dispatcher->getCurrentContext());
  dispatcher->getCurrentContext()->interruptProcedure = [&]() {
    assert(dispatcher != nullptr);
    assert(context != nullptr);
    TimerContext* timerContext = static_cast<TimerContext*>(context);
    if (!timerContext->interrupted) {
      dispatcher->interruptTimer(timerContext->time, timerContext->context);
      timerContext->interrupted = true;
    }
  };

  dispatcher->dispatch();
  dispatcher->getCurrentContext()->interruptProcedure = nullptr;
  assert(timerContext.context == dispatcher->getCurrentContext());
  assert(dispatcher != nullptr);
  assert(context == &timerContext);
  context = nullptr;
  if (timerContext.interrupted) {
    throw InterruptedException();
  }
}
void Timer::sleep(std::chrono::nanoseconds duration) {
  assert(dispatcher != nullptr);
  assert(context == nullptr);
  if (dispatcher->interrupted()) {
    throw InterruptedException();
  }

  if(duration.count() == 0 ) {
    dispatcher->yield();
  } else {
    timer = dispatcher->getTimer();

    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
    itimerspec expires;
    expires.it_interval.tv_nsec = expires.it_interval.tv_sec = 0;
    expires.it_value.tv_sec = seconds.count();
    expires.it_value.tv_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(duration - seconds).count();
    timerfd_settime(timer, 0, &expires, NULL);

    ContextPair contextPair;
    OperationContext timerContext;
    timerContext.interrupted = false;
    timerContext.context = dispatcher->getCurrentContext();
    contextPair.writeContext = nullptr;
    contextPair.readContext = &timerContext;

    epoll_event timerEvent;
    timerEvent.events = EPOLLIN | EPOLLONESHOT;
    timerEvent.data.ptr = &contextPair;

    if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_MOD, timer, &timerEvent) == -1) {
      throw std::runtime_error("Timer::sleep, epoll_ctl failed, " + lastErrorMessage());
    }
    dispatcher->getCurrentContext()->interruptProcedure = [&]() {
        assert(dispatcher != nullptr);
        assert(context != nullptr);
        OperationContext* timerContext = static_cast<OperationContext*>(context);
        if (!timerContext->interrupted) {
          uint64_t value = 0;
          if(::read(timer, &value, sizeof value) == -1 ){
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wlogical-op"
            if(errno == EAGAIN || errno == EWOULDBLOCK) {
#pragma GCC diagnostic pop
              timerContext->interrupted = true;
              dispatcher->pushContext(timerContext->context);
            } else {
              throw std::runtime_error("Timer::sleep, interrupt procedure, read failed, "  + lastErrorMessage());
            }
          } else {
            assert(value>0);
            dispatcher->pushContext(timerContext->context);
          }

          epoll_event timerEvent;
          timerEvent.events = EPOLLONESHOT;
          timerEvent.data.ptr = nullptr;

          if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_MOD, timer, &timerEvent) == -1) {
            throw std::runtime_error("Timer::sleep, interrupt procedure, epoll_ctl failed, " + lastErrorMessage());
          }
        }
    };

    context = &timerContext;
    dispatcher->dispatch();
    dispatcher->getCurrentContext()->interruptProcedure = nullptr;
    assert(dispatcher != nullptr);
    assert(timerContext.context == dispatcher->getCurrentContext());
    assert(contextPair.writeContext == nullptr);
    assert(context == &timerContext);
    context = nullptr;
    timerContext.context = nullptr;
    dispatcher->pushTimer(timer);
    if (timerContext.interrupted) {
      throw InterruptedException();
    }
  }
}
Ejemplo n.º 17
0
 size_t get_ns_per_op() {
     if (d_num_iterations <= 0) {
         return 0;
     }
     return d_duration.count() / d_num_iterations;
 }
Ejemplo n.º 18
0
std::tuple<VkResult, uint32_t> acquire_next_image(swapchain_type &swapchain, std::chrono::nanoseconds timeout, const semaphore::semaphore_type &semaphore) {
	uint32_t image_index;
	const VkResult result(vkAcquireNextImageKHR(internal::get_instance(*internal::get_parent(swapchain)), internal::get_instance(swapchain), timeout.count(), internal::get_instance(semaphore), VK_NULL_HANDLE, &image_index));
	return std::make_tuple(result, image_index);
}
Ejemplo n.º 19
0
Archivo: main.cpp Proyecto: CCJY/coliru
 Duration(std::chrono::nanoseconds inNanosconds) : mNanoseconds(inNanosconds.count()) {}
 /**
 * Sets the playback to a specified time point of the played data
 * \param[in] time  The time point to which playback should seek, expressed in units of nanoseconds (zero value = start)
 */
 void seek(std::chrono::nanoseconds time)
 {
     rs2_error* e = nullptr;
     rs2_playback_seek(_dev.get(), time.count(), &e);
     error::handle(e);
 }
Ejemplo n.º 21
0
void Timer::sleep(std::chrono::nanoseconds duration) {
  assert(dispatcher != nullptr);
  assert(context == nullptr);
  if (dispatcher->interrupted()) {
    throw InterruptedException();
  }

  OperationContext timerContext;
  timerContext.context = dispatcher->getCurrentContext();
  timerContext.interrupted = false;
  timer = dispatcher->getTimer();

  struct kevent event;
  EV_SET(&event, timer, EVFILT_TIMER, EV_ADD | EV_ENABLE | EV_ONESHOT, NOTE_NSECONDS, duration.count(), &timerContext);

  if (kevent(dispatcher->getKqueue(), &event, 1, NULL, 0, NULL) == -1) {
    throw std::runtime_error("Timer::stop, kevent failed, " + lastErrorMessage());
  }

  context = &timerContext;
  dispatcher->getCurrentContext()->interruptProcedure = [&] {
    assert(dispatcher != nullptr);
    assert(context != nullptr);
    OperationContext* timerContext = static_cast<OperationContext*>(context);
    if (!timerContext->interrupted) {
      struct kevent event;
      EV_SET(&event, timer, EVFILT_TIMER, EV_DELETE, 0, 0, NULL);

      if (kevent(dispatcher->getKqueue(), &event, 1, NULL, 0, NULL) == -1) {
        throw std::runtime_error("Timer::stop, kevent failed, " + lastErrorMessage());
      }

      dispatcher->pushContext(timerContext->context);
      timerContext->interrupted = true;
    }
  };
  
  dispatcher->dispatch();
  dispatcher->getCurrentContext()->interruptProcedure = nullptr;
  assert(dispatcher != nullptr);
  assert(timerContext.context == dispatcher->getCurrentContext());
  assert(context == &timerContext);
  context = nullptr;
  timerContext.context = nullptr;
  dispatcher->pushTimer(timer);
  if (timerContext.interrupted) {
    throw InterruptedException();
  }
}
Ejemplo n.º 22
0
autoboost::chrono::nanoseconds NanosecondsForFutureWait(const std::chrono::nanoseconds& time) {
  return autoboost::chrono::nanoseconds(time.count());
}