comp.lang.ada
 help / color / mirror / Atom feed
* Ada Protected Object Turorial #2: Overview of Tasks
@ 1999-12-18  0:00 James S. Rogers
  1999-12-19  0:00 ` Robert Dewar
  0 siblings, 1 reply; 10+ messages in thread
From: James S. Rogers @ 1999-12-18  0:00 UTC (permalink / raw)


Overview of Ada Tasks.

Tasks are entities whose execution may proceed in parallel.

A task is an object of a task type. As with other types in Ada,
the task type may be explicitly named by the programmer, or
it may be an anonymous type.

Task objects are created in the same ways as other objects:
they may be declared by an object declaration, or created
dynamically using an allocator. Tasks may be nested within
other program units, in the same manner as subprograms
 (procedures and functions) and packages.

Common task activities:

Task Creation:

A task is created when it is declared or dynamically allocated.
The creator can communicate with the task during creation by
 initializing discriminants to the task.

Task Activation:

After creation the creator waits for the task to activate.
No explicit action is required by the creator. The task will
automatically activate as a result of its creation.

Task Termination:

Every task has a master. The master cannot terminate until
all the tasks it created terminates. The created tasks are called
children of the master. A task will terminate when it completes
all its activities. It can also be terminated by the master through
an abort command.

Rendezvous:

A rendezvous is a method for tasks to communicate directly
with one another. The rendezvous synchronizes tasks at the
point of communication. A calling task calls an entry in another
 task. The called task accepts the entry call. If the calling task
 calls the entry before the called task is ready to accept it, the
calling task will wait for the called task. If the called task reaches
 the accept point before the calling task calls the entry, the
called task will wait for a calling task.  Entry parameters allow
data to be passed between tasks during a rendezvous.

Protected Objects:

Protected objects provide mutual exclusion for data in shared
memory between tasks. They allow tasks to communicate
either synchronously or asynchronously. Tasks communicate
indirectly by reading and writing components of the protected
objects.

Unprotected Shared Variables:

Ada does allow you to use unprotected shared variables so
that you can implement user-defined low level synchronization
between tasks. It is the responsibility of the developer to
ensure the integrity of the data.

In addition to the basic tools described above, Ada provides
finer control of the interactions between tasks through the many
uses of the select clause.

The select clause can be used to allow a task to accept several
different entry calls each time through a loop, much like
processing a case statement.

The select clause can have a delay alternative, allowing a calling
task to limit the amount of time it waits for the called task to
accept the entry, or for a protected object to open the block on an entry.

The select clause can have an abort option, allowing two activities
to proceed simultaneously. When one of the options finishes the
other option is automatically aborted. This is called Asynchronous
Transfer of Control. One of the options must be an entry call or a delay.
The other option is defined as the abortable part of the statement.
An example lifted from the Ada 95 Rationale concerns having a
database operation cancelled by typing a special key on the input device.

declare
   Database_Txn : Transaction;
   -- declare a transaction, commit or abort during finalization
begin
   select
      -- wait for a cancel key from the input device
      Input_Device.Wait_For_Cancel;
      -- the Satus remains Incomplets, so that the transaction will not
commit
   then abort
      -- do the transaction
      Read(Database_Txn, ...);
      Write(Database_Txn, ...);
       ...
       Set_Status(Database_Txn, Succeeded);
       -- set status to ensure the transaction is committed
   end select;
   -- Finialize of Database_Txn will be called here and,
   -- based on the recorded status, will either commit or
   -- abort the transaction.
end;


I have left out the details of how the transaction type is defined so
that Finalization handles the roll-back or commit of the database.
In C++ terms, this would be handled by the destructor for Database_Txn.

Ada clearly has more to offer concurrent programming than the
ability to create threads and create mutexes. Most of your
concurrent programming needs can be handled by the tools
Ada provides, without forcing the programmer into more risky
low level programming. At the same time, Ada allows the
programmer to perform all the risky low level programming
required to do the job.

Jim Rogers
Colorado Springs, Colorado






^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~1999-12-27  0:00 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-12-18  0:00 Ada Protected Object Turorial #2: Overview of Tasks James S. Rogers
1999-12-19  0:00 ` Robert Dewar
1999-12-20  0:00   ` Tucker Taft
1999-12-21  0:00     ` Robert Dewar
1999-12-21  0:00   ` Robert I. Eachus
1999-12-22  0:00   ` Robert A Duff
1999-12-23  0:00     ` Robert Dewar
1999-12-23  0:00       ` Robert A Duff
1999-12-23  0:00         ` Robert Dewar
1999-12-27  0:00           ` Robert A Duff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox