Exemplo n.º 1
0
void
WorkerThread::Run()
{
  PeriodClock clock;

  while (true) {
    /* wait for work */
    event_trigger.Wait();

    /* got the "stop" trigger? */
    if (delay > 0
        ? WaitForStopped(delay)
        : CheckStoppedOrSuspended())
      break;

    /* reset the trigger here, because our client might have called
       Trigger() a few more times while we were suspended in
       CheckStoppedOrSuspended() */
    event_trigger.Reset();

    if (IsCommandPending()) {
      /* just in case we got another suspend/stop command after
         CheckStoppedOrSuspended() returned and before the trigger got
         reset: restore the trigger and skip this iteration, to fix
         the race condition */
      event_trigger.Signal();
      continue;
    }

    /* do the actual work */
    if (period_min > 0)
      clock.Update();

    Tick();

    unsigned idle = idle_min;
    if (period_min > 0) {
      unsigned elapsed = clock.Elapsed();
      if (elapsed + idle < period_min)
        idle = period_min - elapsed;
    }

    if (idle > 0 && WaitForStopped(idle))
      break;
  }
}
Exemplo n.º 2
0
void
TTYPort::Run()
{
  char buffer[1024];

  while (true) {
    /* wait for data to arrive on the port */
    switch (WaitRead(200)) {
    case WaitResult::READY:
      /* linger for a few more milliseconds so the device can send
         some more data; without this, we would be waking up for every
         single byte */
      if (WaitForStopped(10))
        return;

      break;

    case WaitResult::FAILED:
      if (errno != EAGAIN && errno != EINTR) {
        valid.Reset();
        return;
      }

      /* non-fatal error, fall through */

    case WaitResult::TIMEOUT:
    case WaitResult::CANCELLED:
      /* throttle */
      if (WaitForStopped(500))
        return;

      continue;
    }

    ssize_t nbytes = read(fd, buffer, sizeof(buffer));
    if (nbytes == 0 || (nbytes < 0 && errno != EAGAIN && errno != EINTR)) {
      valid.Reset();
      return;
    }

    if (nbytes > 0)
      handler.DataReceived(buffer, nbytes);
  }

  Flush();
}
Exemplo n.º 3
0
bool
SuspensibleThread::WaitForStopped(unsigned timeout_ms)
{
  assert(Thread::IsInside());

  const ScopeLock lock(mutex);
  return WaitForStopped(timeout_ms);
}
Exemplo n.º 4
0
void
TTYPort::Run()
{
  char buffer[1024];

  // XXX use poll()
  while (!WaitForStopped(50)) {
    ssize_t nbytes = read(fd, buffer, sizeof(buffer));
    for (ssize_t i = 0; i < nbytes; ++i)
      ProcessChar(buffer[i]);
  }

  Flush();
}
Exemplo n.º 5
0
void
TTYPort::Run()
{
  char buffer[1024];

  // XXX use poll()
  while (!WaitForStopped(50)) {
    ssize_t nbytes = read(fd, buffer, sizeof(buffer));
    if (nbytes > 0)
      handler.DataReceived(buffer, nbytes);
  }

  Flush();
}