Metered concurrency: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 6: Line 6:


The interface for the counting semaphore is defined in an Ada package specification:
The interface for the counting semaphore is defined in an Ada package specification:
<Ada> package Semaphores is
<lang ada> package Semaphores is
protected type Counting_Semaphore(Max : Positive) is
protected type Counting_Semaphore(Max : Positive) is
entry Acquire;
entry Acquire;
Line 14: Line 14:
Lock_Count : Natural := 0;
Lock_Count : Natural := 0;
end Counting_Semaphore;
end Counting_Semaphore;
end Semaphores;</Ada>
end Semaphores;</lang>
The ''Acquire'' entry has a condition associated with it. A task can only execute the ''Acquire'' entry when ''Lock_Count'' is less than ''Max''. This is the key to making this structure behave as a counting semaphore. This condition, and all the other aspects of ''Counting_Semaphore'' are contained in the package body.
The ''Acquire'' entry has a condition associated with it. A task can only execute the ''Acquire'' entry when ''Lock_Count'' is less than ''Max''. This is the key to making this structure behave as a counting semaphore. This condition, and all the other aspects of ''Counting_Semaphore'' are contained in the package body.
<Ada> package body Semaphores is
<lang ada> package body Semaphores is
------------------------
------------------------
Line 55: Line 55:
end Counting_Semaphore;
end Counting_Semaphore;
end Semaphores;</Ada>
end Semaphores;</lang>
We now need a set of tasks to properly call an instance of ''Counting_Semaphore''.
We now need a set of tasks to properly call an instance of ''Counting_Semaphore''.
<Ada> with Semaphores;
<lang ada> with Semaphores;
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Text_Io; use Ada.Text_Io;