Writer Tasks End of trail navigation bar

Reader-Writer Tasks

A "reader-writer" task is a combination of the reader task and the writer task. When a reader-writer task is created, two task buffers are created: one for the read operation and one for the write operation. Each sampling instant, the task reads one sample from the hardware and stores that data in the task read buffer. Then the task reads data from the task write buffer and writes that data to the hardware. Hence, the read and write operations are synchronized. Reader-writer tasks are particularly useful for system identification and other instances where read and write operations need to be synchronized. See the analog_loopback example for demonstration of the synchronization of read and write operations.

This process is illustrated in the figure below. In this case, the application is reading and writing four samples at a time using one of the hil_task_read_write functions. A "sample" constitutes a set of channels that are read in a single sampling instant. Each square in the figure represents a sample. The input buffer passed to the hil_task_read_write function is initially empty (hence portrayed as white), but will be filled with the samples read from the task read buffer before the hil_task_read_write function returns. The light orange squares represent samples that were read in previous calls to the hil_task_read_write function. Notice that the hil_task_read_write function will block because the task ISR still needs to write two more samples to the task read buffer before all four samples requested can be read.

The output buffer passed to the hil_task_read_write function contains the data to write to the task write buffer (hence portrayed as yellow) before the hil_task_read_write function returns. In this case, these samples are shown as already in the task write buffer because the hil_task_read_write function has enough space in the task write buffer to do this transfer without blocking. The hil_task_read_write function always does as much as it can before blocking.

The task read buffer is filled by the task interrupt service routine (ISR) at the sampling rate specified when the task was created. Each sampling instant, the task reads one sample from the channels selected when the task was created. The sample is stored at the next available location in the task read buffer. The dark orange squares represent samples that have been stored in the task read buffer by the task ISR since the last call to the hil_task_read_write function.

The task read buffer operates like a circular buffer. However, new samples are not allowed to overwrite old samples in the buffer. Hence, if the hil_task_read_write function has not been called for a long time and the task read buffer becomes full, then the next call to the hil_task_read_write function will fail with an QERR_BUFFER_OVERFLOW error.

After the task ISR reads a sample from the selected input channels and stores the data in the task read buffer, the task ISR reads the next available sample from the task write buffer and writes it to the output channels selected when the task was created. The light purple squares represent samples that were written to the task write buffer in a previous call to the hil_task_read_write function and have already been written to the hardware by the task. The dark purple squares represent samples that have been stored in the task buffer by the previous call to the hil_task_read_write function but which have not yet been written to the hardware by the task ISR.

The reason the hil_task_read_write function is not writing to the same sample number in the task write buffer as it is reading from the task read buffer is because the task write buffer was filled with four samples before the task was started by a call to the hil_task_write function. These four samples are indicated in green. The hil_task_write function does not block waiting for the data to be written to the hardware - it only blocks if there's not enough space the task write buffer. The task ISR, on the other hand, always reads from the same sample number in the task write buffer as it is writing to in the task read buffer.

Figure 1

The task functions associated with reader-writer tasks are:

 

navigation bar