Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 36 ยท Tasking and Concurrency

Protected Objects

Protected objects give Ada tasks safe shared data: functions, procedures and entries with barriers, mutual exclusion without explicit locks, plus runnable C stand-ins.

Sometimes tasks do not need to meet; they just need to share data safely: a counter, a queue, a sensor reading. Using a rendezvous for this means a whole task acting as a gatekeeper. Ada offers something lighter: the protected object. It bundles data with operations, and the language guarantees the operations never overlap in harmful ways. You never write lock or unlock.

The Ada code below is read-only on this site; the runnable examples are C stand-ins using POSIX mutexes and condition variables, which are what a protected object compiles down to conceptually.

Three kinds of operations

A protected type has a public part with operations and a private part with the data they guard.

ADA
protected type Counter is
   procedure Increment;                 --  read/write access
   function  Value return Natural;      --  read-only access
   entry     Wait_Until_At_Least (N : Natural);  --  guarded by a barrier
private
   Count : Natural := 0;
end Counter;

protected body Counter is

   procedure Increment is
   begin
      Count := Count + 1;
   end Increment;

   function Value return Natural is
   begin
      return Count;
   end Value;

   entry Wait_Until_At_Least (N : Natural) when Count >= N is
   begin
      null;
   end Wait_Until_At_Least;

end Counter;
  • A procedure has exclusive read-write access: no other task may be inside the object at the same time.
  • A function has read-only access. Several tasks may run functions at once, but never together with a procedure or entry. This is a readers-writer lock built into the language.
  • An entry works like a procedure but has a barrier: a Boolean condition after when. A caller is queued until the barrier is true.

Protected operations

Procedure exclusive read/write
Function shared read-only
Entry waits on a barrier

No explicit locks anywhere.

1 / 2

Barriers and re-evaluation

When a procedure or entry body finishes, the runtime re-evaluates the barriers of the entries that have waiting callers. If one is now true, that caller proceeds. That is how a bounded buffer works: Put has the barrier when Count < Capacity and Get has when Count > 0. A producer calling Put on a full buffer sleeps and is woken once a consumer's Get makes room.

ADA
protected type Bounded_Buffer (Capacity : Positive) is
   entry Put (Item : in  Integer);
   entry Get (Item : out Integer);
private
   Data  : array (1 .. Capacity) of Integer;
   First : Positive := 1;
   Count : Natural  := 0;
end Bounded_Buffer;

protected body Bounded_Buffer is

   entry Put (Item : in Integer) when Count < Capacity is
   begin
      Data ((First + Count - 1) mod Capacity + 1) := Item;
      Count := Count + 1;
   end Put;

   entry Get (Item : out Integer) when Count > 0 is
   begin
      Item  := Data (First);
      First := First mod Capacity + 1;
      Count := Count - 1;
   end Get;

end Bounded_Buffer;

There is no if Full then wait code and no risk of a forgotten notify: the barrier is the only place the condition is written. The project Bounded Ring Buffer builds a runnable version of this idea.

The rules that keep it safe

Because a protected operation holds the object, it must finish quickly and must never wait for something outside. The language forbids, in a protected body, any potentially blocking operation: delay, accept, task entry calls and input/output that might block. Compilers reject these, or raise Program_Error for indirect cases. Calls to other protected objects are allowed only in a fixed direction (a call to a different object, not back into one that is already held).

Stand-in: an atomic counter (C, runnable)

Four threads each increment a shared counter 25,000 times. With the mutex around the increment, the final value is always 100000. This is what a protected procedure Increment guarantees.

C C stand-in: protected counter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Remove the lock calls in your own copy and run it a few times: the total usually falls below 100000, because two threads read the same old value. Protected objects make that bug impossible to write.

Stand-in: an entry with a barrier (C, runnable)

wait_until_at_least below is the barrier version: it sleeps on a condition variable until count >= n, and every change broadcasts so waiters re-check, just as barriers are re-evaluated.

C C stand-in: barrier entry

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which protected operation may run at the same time as another call to a protected function on the same object?

    Functions are read-only, so many can run together; procedures and entries need exclusive access.
  2. 2. What is a barrier on a protected entry?

    The when condition holds callers in a queue until it becomes true, and it is re-evaluated when the object changes.
  3. 3. Why is delay forbidden inside a protected body?

    Potentially blocking operations would hold the lock and starve every other caller.
  4. 4. In a protected bounded buffer, which barrier does Put need?

    Put must wait while the buffer is full; Get uses Count > 0.

Summary

  • A protected object guards its private data with built-in mutual exclusion.
  • Procedures are exclusive, functions are shared read-only, entries add barrier conditions.
  • Barriers are re-evaluated whenever the object changes, so no manual signalling is needed.
  • Protected bodies must not block; keep them short.
  • The C stand-ins show the mutex and condition-variable ideas underneath.

Next: Select, Delay and Termination