Skip to content

Commit

Permalink
Semaphores next
Browse files Browse the repository at this point in the history
  • Loading branch information
noelPellkvist committed Sep 16, 2024
1 parent 5f8c8ff commit aec8052
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
5 changes: 4 additions & 1 deletion producerconsumer_prot.adb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ procedure ProducerConsumer_Prot is
use Random_Delay;
G : Generator;

m_Buffer : Buffer.CircularBuffer;

-- ==> Complete code: Use Buffer

task type Producer;
Expand All @@ -34,6 +36,7 @@ procedure ProducerConsumer_Prot is

-- ==> Complete code: Write to Buffer

m_Buffer.Put(i);
-- Next 'Release' in 50..250ms
Next := Next + Milliseconds(Random(G));
delay until Next;
Expand All @@ -49,7 +52,7 @@ procedure ProducerConsumer_Prot is
-- Read from X

-- ==> Complete code: Read from Buffer

m_Buffer.Get(X);
Put_Line(Integer'Image(X));
Next := Next + Milliseconds(Random(G));
delay until Next;
Expand Down
18 changes: 16 additions & 2 deletions producerconsumer_rndzvs.adb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,24 @@ procedure ProducerConsumer_Rndzvs is
begin
loop
select
when Count < Index'Last =>
accept Append(I : in Integer) do
B(In_Ptr) := I;
In_Ptr := In_Ptr + 1;
Count := Count + 1;
end Append;
-- => Complete Code: Service Append
or
when Count > 0 =>
accept Take(I : out Integer) do
I := B(Out_Ptr);
Out_Ptr := Out_Ptr + 1;
Count := Count - 1;
end Take;
-- => Complete Code: Service Take
or
when Count = 0 =>
terminate;
-- => Termination
end select;
end loop;
Expand All @@ -51,7 +65,7 @@ procedure ProducerConsumer_Rndzvs is
for I in 1..N loop

-- => Complete code: Write to X

Buffer.Append(I);
-- Next 'Release' in 50..250ms
Next := Next + Milliseconds(Random(G));
delay until Next;
Expand All @@ -65,7 +79,7 @@ procedure ProducerConsumer_Rndzvs is
Next := Clock;
for I in 1..N loop
-- Complete Code: Read from X

Buffer.Take(X);
Put_Line(Integer'Image(X));
Next := Next + Milliseconds(Random(G));
delay until Next;
Expand Down
13 changes: 10 additions & 3 deletions semaphores.adb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

package body Semaphores is
protected body CountingSemaphore is
entry Wait -- To be completed

entry Signal -- To be completed
entry Wait
when Count > 0 is
begin
Count = Count - 1;
end
entry Signal
when Count < MaxCount is
begin
Count = Count + 1;
end
end CountingSemaphore;
end Semaphores;

0 comments on commit aec8052

Please sign in to comment.