Pt

Collaboration diagram for Pt:

Files

file  PT.H

Modules

 Local continuations
 Protothread semaphores

Data Structures

struct  pt

Defines

#define PT_BEGIN(pt)
#define PT_END(pt)
#define PT_EXIT(pt)
#define PT_INIT(pt)
#define PT_RESTART(pt)
#define PT_SCHEDULE(f)
#define PT_SPAWN(pt, child, thread)
#define PT_THREAD(name_args)
#define PT_THREAD_EXITED   1
#define PT_THREAD_WAITING   0
#define PT_WAIT_THREAD(pt, thread)
#define PT_WAIT_UNTIL(pt, condition)
#define PT_WAIT_WHILE(pt, cond)
#define PT_YIELD(pt)
#define PT_YIELDING()

Define Documentation

#define PT_BEGIN ( pt   ) 

Declare the start of a protothread inside the C function implementing the protothread.

This macro is used to declare the starting point of a protothread. It should be placed at the start of the function in which the protothread runs. All C statements above the PT_BEGIN() invokation will be executed each time the protothread is scheduled.

Parameters:
pt A pointer to the protothread control structure.
Example:

 PT_THREAD(producer(struct pt *p, int event)) {
   PT_BEGIN(p);
   while(1) {
     PT_WAIT_UNTIL(p, event == CONSUMED || event == DROPPED);
     produce();
     PT_WAIT_UNTIL(p, event == PRODUCED);
   }
   
   PT_END(p);
 }

Definition at line 144 of file PT.H.

Referenced by PT_THREAD().

#define PT_END ( pt   ) 

Declare the end of a protothread.

This macro is used for declaring that a protothread ends. It should always be used together with a matching PT_BEGIN() macro.

Parameters:
pt A pointer to the protothread control structure.

Definition at line 319 of file PT.H.

Referenced by PT_THREAD().

#define PT_EXIT ( pt   ) 

Exit the protothread.

This macro causes the protothread to exit. If the protothread was spawned by another protothread, the parent protothread will become unblocked and can continue to run.

Parameters:
pt A pointer to the protothread control structure.

Definition at line 303 of file PT.H.

#define PT_INIT ( pt   ) 

Initialize a protothread.

Initializes a protothread. Initialization must be done prior to starting to execute the protothread.

Parameters:
pt A pointer to the protothread control structure.
Example:

 void main(void) {
   struct pt p;
   int event;
   
   PT_INIT(&p);
   while(PT_SCHEDULE(consumer(&p, event))) {
     event = get_event();
   }
 }

See also:
PT_SPAWN()

Definition at line 114 of file PT.H.

Referenced by main().

#define PT_RESTART ( pt   ) 

Restart the protothread.

This macro will block and cause the running protothread to restart its execution at the place of the PT_BEGIN() call.

Parameters:
pt A pointer to the protothread control structure.

Definition at line 286 of file PT.H.

#define PT_SCHEDULE (  ) 

Schedule a protothread.

This function shedules a protothread. The return value of the function is non-zero if the protothread is running or zero if the protothread has exited.

Example

 void main(void) {
   struct pt p;
   int event;
   
   PT_INIT(&p);
   while(PT_SCHEDULE(consumer(&p, event))) {
     event = get_event();
   }   
 }

Parameters:
f The call to the C function implementing the protothread to be scheduled

Definition at line 347 of file PT.H.

Referenced by main().

#define PT_SPAWN ( pt,
child,
thread   ) 

Spawn a child protothread and wait until it exits.

This macro spawns a child protothread and waits until it exits. The macro can only be used within a protothread.

Example:

 static struct pt parent_pt, child_pt;
 int should_spawn_flag;

 PT_THREAD(child(struct pt *pt)) {
   PT_BEGIN(pt);

   while(all_items_processed()) {
     process_item();
     PT_WAIT_UNTIL(pt, item_processed());
   }
   
   PT_END(pt);
 }
 
 PT_THREAD(parent(void)) {
   PT_BEGIN(&parent_pt);

   if(should_spawn_flag) {
     PT_SPAWN(&parent_pt, &child_pt, child(&child_pt));
   }
   
   PT_END(&parent_pt);
 }

Parameters:
pt A pointer to the protothread control structure.
child A pointer to the child protothread's control structure.
thread The child protothread with arguments

Definition at line 270 of file PT.H.

#define PT_THREAD ( name_args   ) 

Declaration of a protothread.

This macro is used to declare a protothread. All protothreads must be declared with this macro.

Example:

 PT_THREAD(consumer(struct pt *p, int event)) {
   PT_BEGIN(p);
   while(1) {
     PT_WAIT_UNTIL(p, event == AVAILABLE);
     consume();
     PT_WAIT_UNTIL(p, event == CONSUMED);
     acknowledge_consumed();
   }
   PT_END(p);
 }

Parameters:
name_args The name and arguments of the C function implementing the protothread.

Definition at line 86 of file PT.H.

#define PT_THREAD_EXITED   1

Definition at line 59 of file PT.H.

#define PT_THREAD_WAITING   0

Definition at line 58 of file PT.H.

#define PT_WAIT_THREAD ( pt,
thread   ) 

Block and wait until a child protothread completes.

This macro schedules a child protothread. The current protothread will block until the child protothread completes.

Note:
The child protothread must be manually initialized with the PT_INIT() function before this function is used.
Parameters:
pt A pointer to the protothread control structure.
thread The child protothread with arguments
Example:
 PT_THREAD(child(struct pt *p, int event)) {
   PT_BEGIN(p);

   PT_WAIT_UNTIL(p, event == EVENT1);   
   
   PT_END(p);
 }

 PT_THREAD(parent(struct pt *p, struct pt *child_pt, int event)) {
   PT_BEGIN(p);

   PT_INIT(child_pt);
   
   PT_WAIT_THREAD(p, child(child_pt, event));
   
   PT_END(p);
 }

See also:
PT_SPAWN()

Definition at line 228 of file PT.H.

#define PT_WAIT_UNTIL ( pt,
condition   ) 

Block and wait until condition is true.

This macro blocks the protothread until the specified condition is true.

Parameters:
pt A pointer to the protothread control structure.
condition The condition.
Example:
 PT_THREAD(seconds(struct pt *p)) {
   PT_BEGIN(p);

   PT_WAIT_UNTIL(p, time >= 2 * SECOND);
   printf("Two seconds have passed\n");
   
   PT_END(p);
 }

Definition at line 169 of file PT.H.

Referenced by PT_THREAD().

#define PT_WAIT_WHILE ( pt,
cond   ) 

Block and wait while condition is true.

This function blocks and waits while condition is true. See PT_WAIT_UNTIL().

Parameters:
pt A pointer to the protothread control structure.
cond The condition.

Definition at line 188 of file PT.H.

#define PT_YIELD ( pt   ) 

Yield from the current protothread.

This function will yield the protothread, thereby allowing other processing to take place in the system.

Note:
The PT_YIELDING() flag must be placed first in the protothread's body if the PT_YIELD() function should be used.
Example
static
PT_THREAD(fade(struct pt *pt))
{
  PT_YIELDING();
  static int delay;
  
  PT_BEGIN(pt);
  
  for(delay = 3980; delay > 20; delay -= 20) {
    leds_red(LEDS_ON);
    clock_delay(4000 - delay);
    leds_red(LEDS_OFF);
    clock_delay(delay);
    PT_YIELD(pt);
  }
  
  PT_END(pt);
}
Parameters:
pt A pointer to the protothread control structure.

Definition at line 413 of file PT.H.

 
#define PT_YIELDING (  ) 

Declarare that a protothread can yield.

If a protothread should be able to yield with the PT_YIELD() statement, this flag must be placed first in the protothread's function body.

Example:

 static
 PT_THREAD(loop_thread(struct pt *pt))
 {
   PT_YIELDING();
   static int i;

   PT_BEGIN(pt);
   
   for(i = 0; i < 200; ++i) {
     handle_item(i);
     PT_YIELD(pt);
   }
   
   PT_END(pt);
 }

Definition at line 377 of file PT.H.


Generated on Wed Jun 3 12:06:10 2009 for PowWow v1.0 by  doxygen 1.5.6