Hardy Griech wrote:
Grant Edwards wrote:
On 2007-11-14, Grant Edwards <[email protected]> wrote:
:
Protothreads looks promising:

 http://www.sics.se/~adam/pt/

Thanks for the inspiration, Grant!

At the moment I'm looking for a light-weight mechanism for doing 'multi-threading'. Protothread could be one way to do it.

I'm yet struggling a little bit with low power modes. How could this be implemented with protothreads?

To me it seems that protothreads are doing some kind of active wait. And that's definitely not my goal, because the device should be battery driven.

The trick is to invoke the protothread only when there is a chance that the protothread will do something else than waiting. When no protothread should be invoked, the system can go into low power mode. For example, you can check the return code from the protothread to distinguish from protothreads that are waiting and those that have yielded. Based on this information, you can decide to for example go to sleep when all protothreads are waiting, or when all protothreads have yielded.

This is the approach taken by Contiki (http://www.sics.se/contiki/). A Contiki process consists of, among other things, a protothread. This protothread is invoked when another process posts an event to the process, or when an interrupt handler has requested the process to be invoked (polled). For example, there is a timer interrupt that polls a process that performs more complex timer management.

When no messages are to be delivered, and when no interrupt has requested an invocation, the CPU is set to low power mode. The central loop looks something like this:

while(1) {
  invoke_protothreads_that_needs_to_be_invoked();
  if(event_queue_length == 0 && num_process_that_needs_a_poll == 0) {
    LPM();
  }
}

The watchdog can be kicked in this loop as well.

/adam
--
Adam Dunkels <[email protected]>
http://www.sics.se/~adam/


Reply via email to