Section 4.11. Creading and Using Signals


4.11. Creading and Using Signals

The Impulse C programming model emphasizes the buffered movement of data between processes and on the management of streams. The more efficiently your application manages stream data, and the more balanced your application is (in terms of buffer loading and process utilization), the faster it will operate. Streaming makes it possible to create independently synchronized processes that have a minimum number of extraneous inputs and outputs, which simplifies system design and can lead to more reliable, tested systems.

There are times, however, when you need more direct control over the starting and stopping of processes and the synchronization of processes to external events and devices. For these times, Impulse C provides an alternate method of synchronization.

Using signals, you can communicate information from one process to another using a message passing scheme, one in which read operations (more properly called "waits") are blocking, but writes (posts) are nonblocking. The mechanism is simple. It uses a co_signal_post function call in the sending process, and a co_signal_wait function call in the receiving process, as shown in Figure 4-7. In this example, the process is synchronized to a serial input stream, which pipes that stream to an output buffer. After writing eight values to the output stream, it posts a message to a third controlling process and waits for a message back from that process before continuing with the next set of values.

Note that the call to co_signal_post in this example includes a data value indicating the number of stream data elements written. As this demonstrates, signals serve the dual purpose of allowing synchronization of processes and supporting the passing of data values.

Note

When using signals, keep in mind that the co_signal_wait function is blockingthe calling process does not continue until a message is received on the specified signalbut the co_signal_post function is nonblocking. This means that repeated calls to co_signal_post override existing message values on the output signal; the function does not wait until an existing (previously posted) message has been received by the downstream process.


Figure 4-7. A signal can be used to coordinate the operation of two or more processes for those applications in which stream-based synchronization is not sufficient.
 // This process is synchronized to a serial input stream, // and pipes that stream to an output buffer. After writing // eight values to the output stream it posts a message // to a third controlling process, and waits for message // back from that process before continuing with the next // set of values. // void proc1_run(co_stream stream_input,               co_signal ack_signal_input,               co_stream stream_output,              co_signal ready_signal_output) {  uint32 data, i;  co_stream_open(stream_input, O_RDONLY, INT_TYPE(32));  co_stream_open(stream_output, O_WRONLY, INT_TYPE(32));  do {   for (i = 0; i < 8; i++) {    if (co_stream_read(stream_input, &data, sizeof(uint32)) == co_err_none)       co_stream_write(stream_output, &data, sizeof(uint32));    else     printf("Unexpected end of data stream detected in proc1.\ n");   }   co_signal_post(ready_signal_output, i);   co_signal_wait(ack_signal_input);    // The co_wait function will block until                                        //  a message is received }  while (co_stream_eos(stream_input) == co_err_none);   co_stream_close(stream_input);   co_stream_close(stream_output); } 



    Practical FPGA Programming in C
    Practical FPGA Programming in C
    ISBN: 0131543180
    EAN: 2147483647
    Year: 2005
    Pages: 208

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net