Download Real-Time and Embedded Guide
Transcript
Chapter 15. Patterns and Frameworks
consume_an_item()
{
while (buffered_items == 0) wait(empty);
// at this point, I m guaranteed to get an item
nextc = buffer[--buffered_items];
consume(nextc);
signal(full); // wake up a producer
}
} //end of monitor scope
An example is a resource allocation system, such as the producer-consumer buffer above: the shared data
structure is the resource, and the (de)allocation procedures check a lot of conditions before each client
can get or release (part of) the resource.
This kind of monitor is very difficult to offer as a general parameterized primitive.
(TODO: give full code example. E.g. coordinating readers and writers example of [Nutt2000], p. 202, but
with more complex conditions than the binary semaphores.)
15.3. Producer-Consumer
Section 14.3 introduced the general concept of a mediator; Section 15.2 explained how the monitor
mediator works. And this Section applies the pattern to the very often used Producer-Consumer IPC
between two tasks. The Producer-Consumer mediator is the object (data and methods) that helps task A
to send data to task B, without having (i) to know anything about task B, and (ii) to worry about the
implementation details of getting the data from A to B. Again, this loose coupling allows for easier
maintenance and updates. For example, if task B is moved to another process or processor, the mediator
can choose, internally, for a more appropriate type of communication and buffering, and neither A nor B
have to be changed.
15.3.1. Terminology
This Section uses the following terminology:
•
The producer is the task that wants to send the data.
•
The consumer is the task that wants to receive the data.
•
The mediator is the task (active) or object (passive) that producer and consumer use to perform their
communication, without having to know each other.
•
The data is also called the message.
•
The asymmetry suggested by the terminology “producer” and “consumer” is not really relevant in the
mediator pattern. So, both producer and consumer are called clients of the mediator.
144