Exemplo n.º 1
0
void
PORTD_Handler(void)
{
        if (pin_physport_from_pin(PROG_BUTTON)->pcr[pin_physpin_from_pin(PROG_BUTTON)].isf) {
                pin_physport_from_pin(PROG_BUTTON)->pcr[pin_physpin_from_pin(PROG_BUTTON)].raw |= 0; /* clear isf */
                statemachine(ev_button);
        }
        if (pin_physport_from_pin(TARGET_RESET)->pcr[pin_physpin_from_pin(TARGET_RESET)].isf) {
                pin_physport_from_pin(TARGET_RESET)->pcr[pin_physpin_from_pin(TARGET_RESET)].raw |= 0; /* clear isf */
                statemachine(ev_reset);
        }
        if (pin_physport_from_pin(TARGET_LED)->pcr[pin_physpin_from_pin(TARGET_LED)].isf) {
                pin_physport_from_pin(TARGET_LED)->pcr[pin_physpin_from_pin(TARGET_LED)].raw |= 0; /* clear isf */
                statemachine(ev_led);
        }
}
Exemplo n.º 2
0
void CC read_from_stdin( struct KTUI * self, uint32_t timeout )
{
    fd_set rfds;
    struct timeval tv;
    int select_res;

    FD_ZERO( &rfds );
    FD_SET ( STDIN_FILENO, &rfds );
    tv.tv_sec  = 0;
    tv.tv_usec = timeout;

    select_res = select( 1, &rfds, NULL, NULL, &tv );
    if ( select_res < 0 )
    {
        /* error ... */
    }
    else if ( select_res == 0 )
    {
        /* no input during timeout... */
    }
    else
    {
        unsigned char in_buffer[ 32 ];
        int i, n = read( STDIN_FILENO, in_buffer, sizeof in_buffer );
        for ( i = 0; i < n; ++i )
        {
            unsigned int x = ( unsigned int ) in_buffer[ i ];
            statemachine( self, x );
        }
    }
}
Exemplo n.º 3
0
static void
walk_state(void *cbdata)
{
        struct EZPORT_STATUS *status = cbdata;

        ezport_status = *status;
        statemachine((uintptr_t)ev_cmd_done);
}
// static
void AIStateMachine::flush(void)
{
  DoutEntering(dc::curl, "AIStateMachine::flush(void)");
  {
	AIReadAccess<csme_type> csme_r(sContinuedStateMachinesAndMainloopEnabled);
	add_continued_statemachines(csme_r);
  }
  // Abort all state machines.
  for (active_statemachines_type::iterator iter = active_statemachines.begin(); iter != active_statemachines.end(); ++iter)
  {
	AIStateMachine& statemachine(iter->statemachine());
	if (statemachine.abortable())
	{
	  // We can't safely call abort() here for non-running (run() was called, but they weren't initialized yet) statemachines,
	  // because that might call kill() which in some cases is undesirable (ie, when it is owned by a partent that will
	  // also call abort() on it when it is aborted itself).
	  if (statemachine.running())
		statemachine.abort();
	  else
		statemachine.idle();		// Stop the statemachine from starting, in the next loop with batch == 0.
	}
  }
  for (int batch = 0;; ++batch)
  {
	// Run mainloop until all state machines are idle (batch == 0) or deleted (batch == 1).
	for(;;)
	{
	  {
		AIReadAccess<csme_type> csme_r(sContinuedStateMachinesAndMainloopEnabled);
		if (!csme_r->mainloop_enabled)
		  break;
	  }
	  mainloop();
	}
	if (batch == 1)
	  break;
	{
	  AIReadAccess<csme_type> csme_r(sContinuedStateMachinesAndMainloopEnabled);
	  add_continued_statemachines(csme_r);
	}
  }
  // At this point all statemachines should be idle.
  AIReadAccess<csme_type> csme_r(sContinuedStateMachinesAndMainloopEnabled);
  llinfos << "Current number of continued statemachines: " << csme_r->continued_statemachines.size() << llendl;
  llinfos << "Current number of active statemachines: " << active_statemachines.size() << llendl;
  llassert(csme_r->continued_statemachines.empty() && active_statemachines.empty());
}
// static
void AIStateMachine::dowork(void)
{
  llassert(!active_statemachines.empty());
  // Run one or more state machines.
  U64 total_clocks = 0;
  for (active_statemachines_type::iterator iter = active_statemachines.begin(); iter != active_statemachines.end(); ++iter)
  {
	AIStateMachine& statemachine(iter->statemachine());
	if (!statemachine.mIdle)
	{
	  U64 start = get_clock_count();
	  // This might call idle() and then pass the statemachine to another thread who then may call cont().
	  // Hence, after this isn't not sure what mIdle is, and it can change from true to false at any moment,
	  // if it is true after this function returns.
	  statemachine.multiplex(start);
	  U64 delta = get_clock_count() - start;
	  iter->add(delta);
	  total_clocks += delta;
	  if (total_clocks >= sMaxCount)
	  {
#ifndef LL_RELEASE_FOR_DOWNLOAD
		llwarns << "AIStateMachine::mainloop did run for " << (total_clocks * 1000 / calc_clock_frequency()) << " ms." << llendl;
#endif
		std::sort(active_statemachines.begin(), active_statemachines.end(), QueueElementComp());
		break;
	  }
	}
  }
  // Remove idle state machines from the loop.
  active_statemachines_type::iterator iter = active_statemachines.begin();
  while (iter != active_statemachines.end())
  {
	AIStateMachine& statemachine(iter->statemachine());
	// Atomic test mIdle and change mActive.
	bool locked = statemachine.mIdleActive.tryLock();
	// If the lock failed, then another thread is in the middle of calling cont(),
	// thus mIdle will end up false. So, there is no reason to block here; just
	// treat mIdle as false already.
	if (locked && statemachine.mIdle)
	{
	  // Without the lock, it would be possible that another thread called cont() right here,
	  // changing mIdle to false again but NOT adding the statemachine to continued_statemachines,
	  // thinking it is in active_statemachines (and it is), while immediately below it is
	  // erased from active_statemachines.
	  statemachine.mActive = as_idle;
	  // Now, calling cont() is ok -- as that will cause the statemachine to be added to
	  // continued_statemachines, so it's fine in that case-- even necessary-- to remove it from
	  // active_statemachines regardless, and we can release the lock here.
	  statemachine.mIdleActive.unlock();
	  Dout(dc::statemachine, "Erasing " << (void*)&statemachine << " from active_statemachines");
	  iter = active_statemachines.erase(iter);
	  if (statemachine.mState == bs_killed)
	  {
	  	Dout(dc::statemachine, "Deleting " << (void*)&statemachine);
		delete &statemachine;
	  }
	}
	else
	{
	  if (locked)
	  {
		statemachine.mIdleActive.unlock();
	  }
	  llassert(statemachine.mActive == as_active);	// It should not be possible that another thread called cont() and changed this when we are we are not idle.
	  llassert(statemachine.mState == bs_run || statemachine.mState == bs_initialize);
	  ++iter;
	}
  }
  if (active_statemachines.empty())
  {
	// If this was the last state machine, remove mainloop from the IdleCallbacks.
	AIReadAccess<csme_type> csme_r(sContinuedStateMachinesAndMainloopEnabled, true);
	if (csme_r->continued_statemachines.empty() && csme_r->mainloop_enabled)
	{
	  Dout(dc::statemachine, "Deactivating AIStateMachine::mainloop: no active state machines left.");
	  AIWriteAccess<csme_type>(csme_r)->mainloop_enabled = false;
	}
  }
}
Exemplo n.º 6
0
static void
timeout(void *data)
{
        statemachine(ev_timeout);
}
Exemplo n.º 7
0
Arquivo: lamp.c Projeto: 13hoop/limo
int main(void){
    state = LAMP_OFF;
    statemachine( TOGGLE_ON );
    statemachine( TOGGLE_ON );
    statemachine( TOGGLE_OFF );
}