Skip to content

Commit

Permalink
Temporary push semaphores
Browse files Browse the repository at this point in the history
  • Loading branch information
noelPellkvist committed Sep 18, 2024
1 parent d41cc1d commit 93ae2e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
18 changes: 16 additions & 2 deletions producerconsumer_sem.adb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ procedure ProducerConsumer_Sem is
-- 1. Semaphore 'NotFull' to indicate that buffer is not full
-- 2. Semaphore 'NotEmpty' to indicate that buffer is not empty
-- 3. Semaphore 'AtomicAccess' to ensure an atomic access to the buffer
NotFull : Semaphores.CountingSemaphore(1, 1);
NotEmpty : Semaphores.CountingSemaphore(1, 0);
AtomicAccess : Semaphores.CountingSemaphore(1, 0);

task type Producer;

Expand All @@ -39,25 +42,35 @@ procedure ProducerConsumer_Sem is
task body Producer is
Next : Time;
begin
Put_Line("producer task");
Next := Clock;
for I in 1..N loop
-- => Complete Code: Write to Buffer

AtomicAccess.Wait;
B(In_Ptr) := I;
In_Ptr := In_Ptr + 1;
Count := Count + 1;
-- Next 'Release' in 50..250ms
Next := Next + Milliseconds(Random(G));
AtomicAccess.Signal;
--delay until Next;
end loop;
end;

task body Consumer is
Next : Time;
begin
Put_Line("consumer task");
Next := Clock;
for I in 1..N loop
-- => Complete Code: Read from Buffer

AtomicAccess.Wait;
--tmp : Integer := B(Out_Ptr);
Out_Ptr := Out_Ptr + 1;
Count := Count - 1;
-- Next 'Release' in 50..250ms
Next := Next + Milliseconds(Random(G));
AtomicAccess.Signal;
delay until Next;
end loop;
end;
Expand All @@ -66,6 +79,7 @@ procedure ProducerConsumer_Sem is
C: array (Integer range 1..X) of Consumer;

begin -- main task

null;
end ProducerConsumer_Sem;

Expand Down
8 changes: 4 additions & 4 deletions semaphores.adb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ package body Semaphores is
entry Wait
when Count > 0 is
begin
Count = Count - 1;
end
Count := Count - 1;
end;
entry Signal
when Count < MaxCount is
begin
Count = Count + 1;
end
Count := Count + 1;
end;
end CountingSemaphore;
end Semaphores;

0 comments on commit 93ae2e0

Please sign in to comment.