Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Tasking and Concurrency

Rendezvous and Entries

Understand Ada's rendezvous: task entries, accept statements, entry parameters, queues and synchronous hand-offs between tasks, with a runnable C stand-in.

Tasks that never talk to each other are rarely useful. Ada's classic way for tasks to communicate is the rendezvous: one task offers an entry, another task calls it, and the two meet. While they are together, the called task runs the code in an accept statement; afterwards both continue on their own. Because both sides must arrive, a rendezvous is also a synchronization point.

Remember that the Ada code here is read-only on this site (no Ada compiler is available). The runnable parts are C stand-ins built with POSIX threads and clearly labelled.

Entries and accept statements

An entry is declared in the task specification. The task body contains an accept for it.

ADA
task Buffer is
   entry Put (Item : in  Integer);
   entry Get (Item : out Integer);
end Buffer;

task body Buffer is
   Stored : Integer := 0;
begin
   loop
      accept Put (Item : in Integer) do
         Stored := Item;              --  runs during the rendezvous
      end Put;

      accept Get (Item : out Integer) do
         Item := Stored;
      end Get;
   end loop;
end Buffer;

A caller writes Buffer.Put (5); just like a procedure call. The difference is what happens when the other side is not ready:

  • If the caller arrives first, it blocks in the entry queue until the task reaches the matching accept.
  • If the task reaches the accept first, it waits for a caller.

Only the statements inside do ... end are executed while both are linked. Keep that part short, because the caller is stuck until it ends. Anything after end Put; runs concurrently with the caller, which is back on its way.

Anatomy of a rendezvous

Caller calls Buffer.Put (5)
Entry declared in the spec
Accept task runs do-block
Release both continue

The first to arrive waits for the other.

1 / 2

Parameters and direction

Entry parameters use the same modes as subprograms: in, out and in out. Values travel into the accept body at the start of the rendezvous and out values travel back at its end. Entries can also be families (entry Wait (1 .. 4);) when you want several indexed queues, and callers in each queue are served first-in, first-out by default.

ADA
task Calculator is
   entry Square (X : in Integer; Result : out Integer);
end Calculator;

task body Calculator is
begin
   accept Square (X : in Integer; Result : out Integer) do
      Result := X * X;
   end Square;
end Calculator;

--  in the caller
Calculator.Square (7, R);     --  R = 49 after the rendezvous

Here the caller sends 7 and receives 49. The task handles one request and then ends because the body has no loop. Add a loop around the accept to serve forever, and use select (see select, delay and termination) when the task must wait for several kinds of requests at once.

Timed and conditional calls

A plain entry call waits as long as necessary. Ada lets the caller bound the wait:

ADA
select
   Printer.Print (Doc);
or
   delay 2.0;
   Put_Line ("printer busy, giving up");
end select;

This is a timed entry call: if the task does not accept within two seconds, the call is cancelled and the or delay branch runs. A conditional entry call uses else instead and gives up immediately if the task is not already waiting at the accept. These constructs make it possible to write tasks that never hang forever on a partner that has died.

Stand-in: a synchronous hand-off (C, runnable)

The C program imitates a rendezvous with a mutex and two condition variables. The producer "calls" put; it only continues after the consumer has taken the value, which is the synchronous behavior of a rendezvous. Output is deterministic because each step waits for the previous one.

C C stand-in: rendezvous hand-off

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.

The two prints from different threads can interleave, so the exact order of "server accepted" and "caller released" lines may vary between runs: that is real concurrency. What never varies is that the caller is released only after its value was accepted.

Stand-in: out parameters (C, runnable)

Entry calls can return results. Here a "server" fills in a reply while the caller waits.

C C stand-in: request with a reply

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.

Common mistakes

  • Long accept bodies. The caller is blocked for the whole do part; do only the exchange there.
  • Calling your own entry. A task calling its own entry waits forever (deadlock); Ada raises Program_Error where it can detect it.
  • Two tasks calling each other's entries while both are inside accept bodies is a classic deadlock; design one direction of calls.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens if a caller reaches an entry call before the task reaches the matching accept?

    A rendezvous needs both sides; the first to arrive waits, and callers queue FIFO.
  2. 2. Which statements run while the caller is blocked in a rendezvous?

    Only the accept body is executed during the rendezvous; code after it runs concurrently with the caller.
  3. 3. How can a caller avoid waiting forever on a task entry?

    select Entry_Call; or delay N; end select; cancels the call if it is not accepted in time.
  4. 4. Which entry parameter mode returns data to the caller at the end of the rendezvous?

    out and in out parameters are copied back when the accept body ends.

Summary

  • An entry is a named meeting point declared in a task specification; callers use ordinary call syntax.
  • The accept body runs while caller and task are synchronized; keep it short.
  • Entry queues are FIFO, and families give several indexed queues.
  • Timed and conditional entry calls let callers give up.
  • The C stand-ins use mutexes and condition variables to imitate the synchronous hand-off.

Next: Protected Objects