Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Metered-concurrency/Ada/metered-concurrency-1.ada
Normal file
9
Task/Metered-concurrency/Ada/metered-concurrency-1.ada
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package Semaphores is
|
||||
protected type Counting_Semaphore(Max : Positive) is
|
||||
entry Acquire;
|
||||
procedure Release;
|
||||
function Count return Natural;
|
||||
private
|
||||
Lock_Count : Natural := 0;
|
||||
end Counting_Semaphore;
|
||||
end Semaphores;
|
||||
40
Task/Metered-concurrency/Ada/metered-concurrency-2.ada
Normal file
40
Task/Metered-concurrency/Ada/metered-concurrency-2.ada
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package body Semaphores is
|
||||
|
||||
------------------------
|
||||
-- Counting_Semaphore --
|
||||
------------------------
|
||||
|
||||
protected body Counting_Semaphore is
|
||||
|
||||
-------------
|
||||
-- Acquire --
|
||||
-------------
|
||||
|
||||
entry Acquire when Lock_Count < Max is
|
||||
begin
|
||||
Lock_Count := Lock_Count + 1;
|
||||
end Acquire;
|
||||
|
||||
-----------
|
||||
-- Count --
|
||||
-----------
|
||||
|
||||
function Count return Natural is
|
||||
begin
|
||||
return Lock_Count;
|
||||
end Count;
|
||||
|
||||
-------------
|
||||
-- Release --
|
||||
-------------
|
||||
|
||||
procedure Release is
|
||||
begin
|
||||
if Lock_Count > 0 then
|
||||
Lock_Count := Lock_Count - 1;
|
||||
end if;
|
||||
end Release;
|
||||
|
||||
end Counting_Semaphore;
|
||||
|
||||
end Semaphores;
|
||||
37
Task/Metered-concurrency/Ada/metered-concurrency-3.ada
Normal file
37
Task/Metered-concurrency/Ada/metered-concurrency-3.ada
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
with Semaphores;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Semaphores_Main is
|
||||
-- Create an instance of a Counting_Semaphore with Max set to 3
|
||||
Lock : Semaphores.Counting_Semaphore(3);
|
||||
|
||||
-- Define a task type to interact with the Lock object declared above
|
||||
task type Worker is
|
||||
entry Start (Sleep : in Duration; Id : in Positive);
|
||||
end Worker;
|
||||
|
||||
task body Worker is
|
||||
Sleep_Time : Duration;
|
||||
My_Id : Positive;
|
||||
begin
|
||||
accept Start(Sleep : in Duration; Id : in Positive) do
|
||||
My_Id := Id;
|
||||
Sleep_Time := Sleep;
|
||||
end Start;
|
||||
--Acquire the lock. The task will suspend until the Acquire call completes
|
||||
Lock.Acquire;
|
||||
Put_Line("Task #" & Positive'Image(My_Id) & " acquired the lock.");
|
||||
-- Suspend the task for Sleep_Time seconds
|
||||
delay Sleep_Time;
|
||||
-- Release the lock. Release is unconditional and happens without suspension
|
||||
Lock.Release;
|
||||
end Worker;
|
||||
|
||||
-- Create an array of 5 Workers
|
||||
type Staff is array(Positive range 1..5) of Worker;
|
||||
Crew : Staff;
|
||||
begin
|
||||
for I in Crew'range loop
|
||||
Crew(I).Start(2.0, I);
|
||||
end loop;
|
||||
end Semaphores_Main;
|
||||
Loading…
Add table
Add a link
Reference in a new issue