spawn_24.0.0_8f4c2fa8/testsuite/spawn/wait_all.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
--
--  Copyright (C) 2023, AdaCore
--
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--

--  Spawn several subprocess and wait all of them is finished.

with Ada.Command_Line;
with Ada.Calendar;
with Ada.Directories;

with Spawn.String_Vectors;
with Spawn.Processes;
with Spawn.Processes.Monitor_Loop;

procedure Wait_All is
   use all type Spawn.Process_Status;

   package Listeners is
      type Listener is limited new Spawn.Processes.Process_Listener with record
         Process : Spawn.Processes.Process;
      end record;
   end Listeners;

   Command : constant String := Ada.Directories.Full_Name
     (Ada.Command_Line.Command_Name);

   Args : Spawn.String_Vectors.UTF_8_String_Vector;
   List : array (1 .. 10) of aliased Listeners.Listener;

begin
   if Ada.Command_Line.Argument_Count > 0 then
      --  Child process: wait next second boundary and exit
      declare
         Seconds : constant Duration :=
           Ada.Calendar.Seconds (Ada.Calendar.Clock);

         Sleep   : constant Duration :=
          Duration (Integer (Seconds + 0.5)) - Seconds;
      begin
         delay Sleep;

         return;
      end;
   end if;

   Args.Append ("wait");

   for Item of List loop
      Item.Process.Set_Program (Command);
      Item.Process.Set_Arguments (Args);
      Item.Process.Set_Listener (Item'Unchecked_Access);
      Item.Process.Start;
   end loop;

   for J in 1 .. 6 loop
      Spawn.Processes.Monitor_Loop (0.001);
      delay 0.5;

      if (for all Item of List => Item.Process.Status = Not_Running) then
         --  Success
         return;
      end if;
   end loop;

   --  Some process is till running
   Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
end Wait_All;