7.1 Introduction

One of the distinguishing characteristics of real-time and embedded systems
is the concern over management of finite resources. This chapter provides a
number of patterns to help organize, manage, use, and share such resources.
There is some overlap of concerns with the patterns in this and other chapters.
For example, the Smart Pointer Pattern provides a robust access to resources of
a certain type: those that are dynamically allocated. However, that pattern has
already been discussed in Chapter 6. Similarly, the synchronization of
concurrent threads may be thought of as resource management, but it is dealt
with using the Rendezvous Pattern from Chapter 5. This chapter focuses on
patterns that deal with the sharing and management of resources themselves and
not the memory they use. To this end, we'll examine a number of ways to
manage resources among different, possibly concurrent, clients.

A resource, as used here, is a thing (an object) that provides
services to clients that have finite properties or characteristics. This
definition is consistent with the so-called Real-Time UML
Profile1, where a resource is defined as
follows.

blockquote

An element that has resource services whose effectiveness is represented by
one or more /blockquote

Quality of Service (QoS) characteristics.

The QoS properties are the quantitative properties of the resource, such as
its capacity, execution speed, reliability, and so on. In real-time and embedded
systems, it is this quantifiable finiteness that must be managed. For instance,
it is common for a resource to provide services in an atomic fashion; this means
that the client somehow "locks" the resource while it needs it,
preventing other clients from accessing that resource until the original client
is done. This accomplishes the more general purpose of serialization of
resource usage, crucial to the correct operation in systems with concurrent
threads. This is often accomplished with a mutex semaphore (see the Guarded Call
Pattern in Chapter 5) or may be done by serializing the requests themselves (see
the Message Queuing Pattern, also in Chapter 5).

The management of resources with potentially many clients is one of the
more thorny aspects of system design, and a number of patterns have evolved or
been designed over time to deal specifically with just that.

The first few patterns (Priority Inheritance, Highest Locker, Priority
Ceiling) address the schedulability of resources in a priority-based preemptive
multitasking environment, which can be a major concern for real-time systems
design. In static priority scheduling approaches (see, for example, the Static
Priority Pattern in Chapter 5), the priorities of the tasks are known at design
time. The priority of the task determines which tasks will run preferentially
when multiple tasks are ready to runthe highest-priority task that is
ready. This makes the timing analysis of such systems very easy to compute, as
long as certain assumptions are not violated too badly. These assumptions are
the following.

Tasks are periodic with the deadlines coming at the end of the
periods.

Infinite preemptibilitya lower-priority task can be preempted
immediately when a higher-priority task becomes ready to run.

Independencetasks are independent from each other.

When these conditions are true, then the following standard rate monotonic
analysis formula may be applied.

Note that it is "2 raised to the power of (1/n)", where Cj
is the worst-case amount of time required for task j to execute,
Tj is its period, and n is the number of
tasks.2, 3 If
the inequality is true, then the system is schedulablethat is, the
system will always meet its deadlines. Aperiodic tasks are generally
handled by assuming they are periodic and using the minimum arrival time between
task invocations as the period, often resulting in an overly strong but
sufficient condition. The assumption of infinite preemptibility is usually not a
problem if the task has very short critical sections during which it cannot be
preemptedshort with respect to the execution and period times. The problem
of independence is, however, much stickier.

If resources are sharable (in the sense that they can permit simultaneous
access by multiple clients), then no problem exists. However many, if not most,
resources cannot be shared. The common solution to this problem was addressed in
the Guarded Call Pattern of Chapter 5 using a mutual-exclusion semaphore to
serialize access to the resource. This means that if a low-priority task locks a
resource and then a higher-priority task that needs the resource becomes ready
to run, it must block and allow the low-priority task to run until it can
release the resource so that the higher-priority task can run. A simple example
of this is shown in the timing diagram in Figure 7-1.

In the figure, Task 1 is the higher-priority task. Since Task 2 runs first
and locks the resource, when Task 1 is ready to run, it cannot because the
needed resource is unavailable. It therefore must block and allow Task 2 to
complete its use of the resource. During the period of time between marks C and
D, Task 1 is said to be blocked. A task is blocked when it is prevented
from running by a lower-priority task. This can only occur when resources are
shared via mutual exclusion.

The problem with blocking is that the analysis of the timeliness becomes more
difficult. When Task 1 is blocked, the system is said to be in a state of
priority inversion because a lower-priority task has the thread focus
even though a higher-priority task is ready to run. One can imagine third and
fourth tasks of intermediate priority that don't share the resource (and
are therefore able to preempt Task 2) running and preempting Task 2, thereby
lengthening the amount of time before Task 2 releases the resource and allowing
Task 1 to run. Because an arbitrary number of tasks can be fit in the priority
scheme between Task 1 and Task 2, this problem is called unbounded priority
inversion and is a serious problem for the schedulability of tasks. Figure
7-2 illustrates this problem by adding intermediate-priority Tasks X and Y to
the system. Note that for some period of time, Task 1, the highest-priority task
in the system, is blocked by all three remaining tasks.

To compute the schedulability for task sets with blocking, the modified RMA
inequality is used.

where Bj is the blocking time for task jthat is, the
worst-case time that the task can be prevented from execution by a
lower-priority task owning a needed resource. The problem is clear from the
inequalityunbounded blocking means unbounded blocking time, and nothing
useful can be said about the ability of such a system to meet its deadlines.

Unbounded priority inversion is a problem that is addressed by the first
three patterns in this chapter. Note that priority inversion is a necessary
consequence of resource sharing with mutual exclusion locking, but it can be
bounded using these patterns.

These first three patterns solve, or at least address, the problem of
resource sharing for schedulability purposes, but for the most part they
don't deal with the issue of deadlock. A deadlock is a condition in which
clients of resources are waiting for conditions to arise that cannot in
principle ever occur. An example of deadlock is shown in Figure 7-3.

In Figure 7-3, there are two tasks, Task 1 and Task 2, that share two
resources, R1 and R2. Task 1 plans to lock R2 and then lock R1 and release them
in the opposite order. Task 2 plans to lock R1 and then R2 and release them in
the reverse order. The problem arises when Task 1 preempts Task 2 when it has a
single resource (R1) locked. Task 1 is a higher priority, so it can preempt Task
1, and it doesn't need a currently locked resource, so things are fine. It
goes ahead and locks R2. Now it decides that it needs the other resource, R1,
which, unfortunately is locked by the blocked task, Task 2. So Task 1 cannot
move forward and must block in order to allow Task 2 to run until it can release
the now needed resource (R1). So Task 2 runs but finds that it now needs the
other resource (R2) owned by the blocked Task 1. At this point, each task is
waiting for a condition that can never be satisfied, and the system stops.

In principle, a deadlock needs the following four conditions to occur.

Mutual exclusion (locking) of resources

Resources are held (locked) while others are waited for

Preemption while holding resources is permitted

A circular wait condition exists (for example, P1 waits on P2, which
waits on P3, which waits on P1)

The patterns for addressing deadlock try to ensure that at least one of the
four necessary conditions for deadlock cannot occur. The Simultaneous Locking
Pattern breaks condition 2, while the Ordered Locking Pattern breaks condition
4. The Priority Ceiling Pattern is a pattern that solves both the scheduling
problem and the deadlock problem.