Alt Package <alt.h>

Top  Previous  Next

The alt.h functions allow a program to detect which of a group of input channel becomes ready first.

 

There are two sets of functions: the nowait functions return a status value if none of the specified channels is ready to communicate; the others wait until a channel becomes ready.

 

There are two ways to specify which channels are to be tested: the vec functions use an array of pointers to the channels; the others use a variable-length

argument list of pointers to channels.

 


alt_nowait

is any one of an explicit list of channels trying to send?


alt_nowait_vec

is any one of an array of channels trying to send?


alt_wait

await input from any one of an explicit list of channels


alt_wait_vec

await input from any one of an array of channels

 

Dragons003The alt.h functions may be used only on virtual or internal channels. They cannot be used directly on physical channels that run over a hardware link.

 

If you need to use the alt.h functions on a physical channel, consider adding an intermediate guard thread to echo messages from the physical channel to an internal channel, as shown below.

 

 

#include <chan.h>

#define SIZE 64

struct map {

   CHAN *phys_chan;

   CHAN *internal_chan;

};

CHAN internal0, internal1;

 

void guard(void *arg) {

   struct map *s = (struct map *)arg;

   char buf[SIZE];

   for (;;) {

      chan_in_message(SIZE, buf, s->phys_chan);

      chan_out_message(SIZE, buf, s->internal_chan);

   }

}

 

main(int argc, char *argv[], char *envp[],

     CHAN *in[], int ins, CHAN *out[], int outs) {

   struct map s0, s1;

   s0.phys_chan = in[0]; s0.internal_chan = &internal0;

   s1.phys_chan = in[1]; s1.internal_chan = &internal1;

   chan_init(&internal0); thread_new(guard, 1024, &s0);

   chan_init(&internal1); thread_new(guard, 1024, &s1);

   for (;;) {

      int i = alt_wait(2, &internal0, &internal1);

      ...

   }

}

 

Here, the alt_wait function is applied to two internal channels, internal0 and internal1, but the effect is to alt on the underlying ports, in[0] and in[1], which may be physical channels. Clearly, to echo them properly to the internal channels, guard threads must know the format of the expected messages.