Concepts of Programming Languages

(Sean Pound) #1

602 Chapter 13 Concurrency


When tasks are assigned priorities, those priorities are used by the task
scheduler to determine which task to choose from the task-ready queue when
the currently executing task is either blocked, reaches the end of its allocated
time, or completes its execution. Furthermore, if a task with a higher priority
than that of the currently executing task enters the task-ready queue, the lower-
priority task that is executing is preempted and the higher-priority task begins
its execution (or resumes its execution if it had previously been in execution).
A preempted task loses the processor and is placed in the task-ready queue.

13.6.6 Protected Objects


As we have seen, access to shared data can be controlled by enclosing the data
in a task and allowing access only through task entries, which implicitly provide
competition synchronization. One problem with this method is that it is dif-
ficult to implement the rendezvous mechanism efficiently. Ada 95 protected
objects provide an alternative method of providing competition synchroniza-
tion that need not involve the rendezvous mechanism.
A protected object is not a task; it is more like a monitor, as described in
Section 13.4. Protected objects can be accessed either by protected subpro-
grams or by entries that are syntactically similar to the accept clauses in tasks.^4
The protected subprograms can be either protected procedures, which provide
mutually exclusive read-write access to the data of the protected object, or
protected functions, which provide concurrent read-only access to that data.
Entries differ from protected subprograms in that they can have guards.
Within the body of a protected procedure, the current instance of the
enclosing protected unit is defined to be a variable; within the body of a pro-
tected function, the current instance of the enclosing protected unit is defined
to be a constant, which allows concurrent read-only access.
Entry calls to a protected object provide synchronous communication with
one or more tasks using the same protected object. These entry calls provide
access similar to that provided to the data enclosed in a task.
The buffer problem that is solved with a task in the previous subsection
can be more simply solved with a protected object. Note that this example does
not include protected subprograms.

protected Buffer is
entry Deposit(Item : in Integer);
entry Fetch(Item : out Integer);
private
Bufsize : constant Integer := 100;
Buf : array (1..Bufsize) of Integer;
Filled : Integer range 0..Bufsize := 0;


  1. Entries in protected object bodies use the reserved word entry, rather than the accept
    used in task bodies.

Free download pdf