Wireless sensor networks and software for tiny systems - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Wireless sensor networks and software for tiny systems

Description:

By using the C switch statement. Wireless sensor networks and software for tiny ... 'Many of us use the switch/case construct to explicitly implement concurrent ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 17
Provided by: sics
Category:

less

Transcript and Presenter's Notes

Title: Wireless sensor networks and software for tiny systems


1
Wireless sensor networks andsoftware for tiny
systems
Adam Dunkels Ph Lic, Researcher Swedish
Institute of Computer Science Computer and
Network Architectures laboratory 22 November
2005 Ericsson Mobile Platform AB, Lund
2
Protothreads a replacement for explicit state
machines for flow control
3
Background event-drivenprogramming
  • Often used in memory constrained systems
  • Uses less memory than threads
  • Backwards to normal programming
  • Code is executed only when events occur
  • Incoming packets, I/O input, mouse clicks,
    time-out
  • Nothing happens without events
  • Programs have no idle loop

4
Event-driven programs
  • Programs are not threads
  • Programs are not sequential
  • Programs are event-handlers
  • An event-handler is a C function
  • An event-handler must always return
  • An event-handler cannot perform blocking waits

5
Threaded vs event-driven
f1()
f() if() BLOCK() else BLOCK()
while() BLOCK()
f2()
f3()
f4()
6
The problem flow control with an explicit state
machine
  • State machine keeps track of flow of control
  • Control flow difficult to follow
  • Not evident from reading the code
  • Code hard to understand, maintain, debug, write

7
Protothreads no state machine needed
PT_THREAD(f()) if() PT_WAIT_UNTIL()
else PT_WAIT_UNTIL() while()
PT_WAIT_UNTIL()
  • With protothreads, we can write code like this
  • Flow of control apparent
  • C statements used
  • Code easier to understand, maintain, debug, write

8
Protothreads
  • PT_WAIT_UNTIL() conditional blocking
  • Protothreads are a mixture of the event-driven
    and the threaded model
  • Inherits most of the good sides from both, but
    some of the bad sides too
  • A protothread can be driven by an event-handler

9
Protothreads can be implementedwith 100 ANSI
C!By using the C switch statement
10
Can be implemented using the C switch statement!
  • Unconventional use of the C switch statement
  • But fully standards compatible
  • Similar to Duff's device
  • 100 portable no assembly language or stack
    fiddling
  • Very, very low memory overhead 2 bytes
  • Implemented as a set of C macros

11
C switch statement example
PT_THREAD(example(void)) PT_BEGIN() while(1)
PT_WAIT_UNTIL(timer_expired()) call_a_function(
) PT_WAIT_UNTIL(timer_expired()) PT_END()
12
C switch statement example
int example(void) switch(pt-gtlc) case
0 while(1) case 4 pt-gtlc 4 if(!timer_expire
d()) return 1 call_a_function() case 6 pt-gtlc
6 if(!timer_expired()) return 1 return
0
13
The relation between protothreads and the C
switch statement
Dan Henry (Boulder, Co USA), in adiscussion
about protothreads on a 8052message board Many
of us use the switch/case construct to explicitly
implement concurrent state machines in our code.
The protothread macros merely provide a level
ofabstraction above that so that the code
appears more linear and the overall logic more
visible.
14
Limitations
  • Automatic variables (stack variables) not saved
    across a blocking wait
  • Limitation inherited from the event-driven model
  • Programmer must manually save automatic variables
  • However, static local variables still work
    asexpected
  • Standard C compiler issues a warning
  • Switches cannot be (freely) mixed with
    protothreads

15
How well does it work?
  • We reimplemented 7 different event-driven
    programs with protothreads
  • Lines of code 31 reduction
  • Explicit states, state transitions almost
    entirely removed
  • Much easier to write, read, debug code!
  • Others are successfully using it

16
Download!
http//www.sics.se/adam/pt/
Write a Comment
User Comments (0)
About PowerShow.com