workers_0.1.0_939a508c/src/workers-once.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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
with Ada.Exceptions;
with GNAT.IO; use GNAT.IO;
with Workers.Mutexes;

package body Workers.Once is

   ---------
   -- Add --
   ---------

   procedure Add (This : in out Pool; Job : Input) is
   begin
      This.Jobs.Add (Job);
   end Add;

   -----------------
   -- Set_Parents --
   -----------------

   procedure Set_Parents (This : in out Pool) is
   begin
      for Worker of This.Workers loop
         Worker.Set_Parent (This'Unchecked_Access);
         --  No risks as the parent will wait for all tasks terminated to go
         --  out of scope.
      end loop;
   end Set_Parents;

   ------------------
   -- No_More_Jobs --
   ------------------

   procedure No_More_Jobs (This : in out Pool) is
   begin
      This.Jobs.No_More_Jobs;
      This.Jobs.Wait_For_All;
   end No_More_Jobs;

   -----------
   -- Store --
   -----------

   protected body Store is

      ---------
      -- Add --
      ---------

      procedure Add (Job : Input) is
      begin
         if Done then
            raise Program_Error with "Adding job after marking done";
         end if;

         Jobs.Append (Job);
         Jobs_Created := Jobs_Created + 1;
      end Add;

      ---------
      -- Get --
      ---------

      entry Get (Job : out Input_Lists.List)
        when not Jobs.Is_Empty or else Done
      is
      begin
         Busy := Busy + 1;

         if Done and then Jobs.Is_Empty then
            raise Die_Now;
         elsif Early_Abort and then Exception_Identity (Error) /= Null_Id then
            raise Die_Now;
         end if;

         Job.Clear;
         Job.Append (Jobs.First_Element);
         Jobs.Delete_First;
      end Get;

      -----------
      -- Ready --
      -----------

      procedure Ready is
      begin
         Busy := Busy - 1;
      end Ready;

      ----------------
      -- Completion --
      ----------------

      function Completion return Rate
      is (Rate ((Float (Jobs_Created) - Float (Jobs.Length)) / Float (Jobs_Created)));

      -------------
      -- Is_Done --
      -------------

      function Is_Done return Boolean is (Done);

      ----------
      -- Load --
      ----------

      function Load return Rate is (Rate (Float (Busy) / Float (Size)));

      ------------------
      -- No_More_Jobs --
      ------------------

      procedure No_More_Jobs is
      begin
         Done := True;
      end No_More_Jobs;

      --------------
      -- Checkout --
      --------------

      procedure Checkout is
      begin
         Ended := Ended + 1;
      end Checkout;

      ------------------
      -- Wait_For_All --
      ------------------

      entry Wait_For_All when Ended = Size is
      begin
         if Exception_Identity (Error) /= Null_Id then
            Reraise_Occurrence (Error);
         end if;
      end Wait_For_All;

      -----------------
      -- Store_Error --
      -----------------

      procedure Store_Error (E : Exception_Occurrence) is
      begin
         if Exception_Identity (Error) = Null_Id then
            Save_Occurrence (Error, E);
         end if;
      end Store_Error;

   end Store;

   ---------------
   -- Activator --
   ---------------

   task body Activator is
   begin
      for Worker of Parent.Workers loop
         Worker.Set_Parent (Parent);
      end loop;
   end Activator;

   ------------
   -- Worker --
   ------------

   task body Worker is
      Parent : Pool_Access;
      Job    : Input_Lists.List;
   begin
      accept Set_Parent (Parent : Pool_Access) do
         Worker.Parent := Parent;
      end;

      loop
         begin
            Parent.Jobs.Get (Job);
            Process (Job.Constant_Reference (Job.First));
            Parent.Jobs.Ready;
         exception
            when Die_Now =>
               Parent.Jobs.Ready;
               exit;
            when E : others =>
               Parent.Jobs.Ready;
               Parent.Jobs.Store_Error (E);
         end;
      end loop;

      Parent.Jobs.Checkout;
   exception
      when E : others =>
         Put_Line ("Worker: "
                   & Ada.Exceptions.Exception_Name (E) & ": "
                   & Ada.Exceptions.Exception_Message (E));
         raise;
   end Worker;

end Workers.Once;