garlic_6.0.1_90ef4b6f/Garlic/s-rpstio.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
------------------------------------------------------------------------------
--                                                                          --
--                            GLADE COMPONENTS                              --
--                                                                          --
--                 S Y S T E M . R P C . S T R E A M _ I O                  --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--         Copyright (C) 1996-2020 Free Software Foundation, Inc.           --
--                                                                          --
-- GARLIC is free software;  you can redistribute it and/or modify it under --
-- terms of the  GNU General Public License  as published by the Free Soft- --
-- ware Foundation;  either version 2,  or (at your option)  any later ver- --
-- sion.  GARLIC is distributed  in the hope that  it will be  useful,  but --
-- WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHANTABI- --
-- LITY or  FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public  --
-- License  for more details.  You should have received  a copy of the GNU  --
-- General Public License  distributed with GARLIC;  see file COPYING.  If  --
-- not, write to the Free Software Foundation, 59 Temple Place - Suite 330, --
-- Boston, MA 02111-1307, USA.                                              --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable  to  be --
-- covered  by the  GNU  General  Public  License.  This exception does not --
-- however invalidate  any other reasons why  the executable file  might be --
-- covered by the  GNU Public License.                                      --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Exceptions;           use Ada.Exceptions;
with Ada.Streams;              use Ada.Streams;
with System.Garlic;            use System.Garlic;
with System.Garlic.Debug;      use System.Garlic.Debug;
with System.Garlic.Exceptions; use System.Garlic.Exceptions;
with System.Garlic.Heart;      use System.Garlic.Heart;
with System.Garlic.Soft_Links;
with System.Garlic.Streams;
with System.Garlic.Table;
with System.Garlic.Types;

with System.Garlic.Startup;
pragma Elaborate_All (System.Garlic.Startup);
pragma Warnings (Off, System.Garlic.Startup);

package body System.RPC.Stream_IO is

   --  This package needs comments ???

   Private_Debug_Key : constant Debug_Key :=
     Debug_Initialize ("S_RPSTIO", "(s-rpstio): ");
   procedure D
     (Message : String;
      Key     : Debug_Key := Private_Debug_Key)
     renames Print_Debug_Info;

   Msgcode : constant Any_Opcode := User_Message;

   type Partition_Stream_Record is
      record
         Mode      : Stream_Mode;
         Incoming  : aliased Streams.Params_Stream_Type (0);
         Outgoing  : aliased Streams.Params_Stream_Type (0);
         Consumer  : System.Garlic.Soft_Links.Watcher_Access;
         Available : System.Garlic.Soft_Links.Mutex_Access;
         Critical  : System.Garlic.Soft_Links.Mutex_Access;
      end record;
   type Partition_Stream_Access is access Partition_Stream_Record;

   First_Partition_Id : constant Partition_ID := Any_Partition + 1;

   package Streams is
      new System.Garlic.Table.Medium
        (Partition_ID,
         Any_Partition,
         Types.Partition_ID_Increment,
         Types.Partition_ID_Increment,
         Partition_Stream_Access,
         null);

   Any : Partition_Stream_Access;

   function Fetch
     (Partition : Partition_ID)
     return Partition_Stream_Access;

   procedure Handle_Request
     (Partition : Types.Partition_ID;
      Opcode    : External_Opcode;
      Query     : access Garlic.Streams.Params_Stream_Type;
      Reply     : access Garlic.Streams.Params_Stream_Type;
      Error     : in out Error_Type);

   -----------
   -- Close --
   -----------

   procedure Close
     (Stream : in out Partition_Stream_Type)
   is
      Err : aliased Error_Type;
      Str : Partition_Stream_Access;
   begin

      if not Stream.Open then
         raise Stream_Error;
      end if;

      Str := Streams.Get_Component (Stream.PID);

      pragma Debug (D ("Close stream" & Stream.PID'Img));

      --  When Out_Mode, procedure Close is in charge of sending the
      --  outgoing stream element array.

      if Str.Mode = Out_Mode then
         pragma Debug (D ("Send new message"));

         Send (Types.Partition_ID (Stream.PID),
               Msgcode,
               Str.Outgoing'Access,
               Err);
      end if;

      pragma Debug (D ("Close - Unlock stream" & Stream.PID'Img));
      Stream.Open := False;
      System.Garlic.Soft_Links.Leave (Str.Available);

      if Found (Err) then
         Raise_Exception
           (Communication_Error'Identity,
            Content (Err'Access));
      end if;
   exception when others =>
      pragma Debug (D ("exception raised in Close"));
      null;
   end Close;

   -----------
   -- Fetch --
   -----------

   function Fetch
     (Partition : Partition_ID)
      return Partition_Stream_Access
   is
      Stream : Partition_Stream_Access := Streams.Get_Component (Partition);
   begin
      if Stream = null then
         Streams.Enter;
         Stream := Streams.Get_Component (Partition);
         if Stream = null then
            pragma Debug (D ("Allocate stream" & Partition'Img));
            Stream := new Partition_Stream_Record;
            System.Garlic.Soft_Links.Create (Stream.Consumer);
            System.Garlic.Soft_Links.Create (Stream.Available);
            System.Garlic.Soft_Links.Create (Stream.Critical);
            Streams.Set_Component (Partition, Stream);
         end if;
         Streams.Leave;
      end if;
      return Stream;
   end Fetch;

   --------------------
   -- Handle_Request --
   --------------------

   procedure Handle_Request
     (Partition : Types.Partition_ID;
      Opcode    : External_Opcode;
      Query     : access Garlic.Streams.Params_Stream_Type;
      Reply     : access Garlic.Streams.Params_Stream_Type;
      Error     : in out Error_Type)
   is
      pragma Unreferenced (Opcode);
      pragma Unreferenced (Reply);
      pragma Unreferenced (Error);

      SEA : Stream_Element_Array (1 .. Query.Count);
      Len : Stream_Element_Offset;
      Str : constant Partition_Stream_Access :=
        Fetch (Partition_ID (Partition));
   begin
      pragma Debug (D ("Receive new message"));
      pragma Debug (D ("Receive - Lock stream" & Partition'Img));
      System.Garlic.Soft_Links.Enter (Str.Critical);

      Garlic.Streams.Read (Query.all, SEA, Len);
      Garlic.Streams.Write (Str.Incoming, SEA);

      pragma Debug (D ("Receive - Unlock stream" & Partition'Img));
      System.Garlic.Soft_Links.Leave (Str.Critical);

      --  Signal to consumer connected to Partition and to
      --  Any_Partition.

      pragma Debug (D ("Signal to all streams"));
      System.Garlic.Soft_Links.Update (Str.Consumer);
      System.Garlic.Soft_Links.Update (Any.Consumer);
   end Handle_Request;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize is
   begin
      Streams.Initialize;
      Any := new Partition_Stream_Record;
      System.Garlic.Soft_Links.Create (Any.Consumer);
      System.Garlic.Soft_Links.Create (Any.Available);
      System.Garlic.Soft_Links.Create (Any.Critical);
      Streams.Set_Component (Any_Partition, Any);
      Register_Handler (Msgcode, Handle_Request'Access);
   end Initialize;

   ----------
   -- Open --
   ----------

   procedure Open
     (Stream    : in out Partition_Stream_Type;
      Partition : Partition_ID;
      Mode      : Stream_Mode)
   is
      Str : Partition_Stream_Access;
   begin

      if Stream.Open then
         raise Stream_Error;
      end if;

      Stream.Open := True;

      pragma Debug (D ("Open stream" & Partition'Img));
      if Mode = Out_Mode
        and then Partition = Any_Partition
      then
         pragma Debug (D ("Can't write to all partitions"));
         raise Stream_Error;
      end if;

      Str := Fetch (Partition);
      Stream.PID := Partition;

      --  Only one task at a time

      pragma Debug (D ("Open - Lock stream" & Partition'Img));
      System.Garlic.Soft_Links.Enter (Str.Available);
      Str.Mode := Mode;

      pragma Debug (D ("Open - Resume stream" & Partition'Img));
   exception when others =>
      pragma Debug (D ("exception raised in Open"));
      null;
   end Open;

   ----------
   -- Read --
   ----------

   procedure Read
     (Stream : in out Partition_Stream_Type;
      Item   : out    Ada.Streams.Stream_Element_Array;
      Last   : out    Ada.Streams.Stream_Element_Offset)
   is
      FID     : Partition_ID;
      LID     : Partition_ID;
      Len     : Stream_Element_Offset := 0;
      Str     : Partition_Stream_Access;
      From    : Partition_Stream_Access;
      Version : System.Garlic.Types.Version_Id;

   begin

      if not Stream.Open then
         raise Stream_Error;
      end if;

      Str := Streams.Get_Component (Stream.PID);

      if Str.Mode /= In_Mode then
         pragma Debug (D ("Mode should be In_Mode"));
         raise Stream_Error;
      end if;

      while Len = 0 loop

         --  Is there something to read ?

         pragma Debug (D ("Read - Wait for stream" & Stream.PID'Img));

         --  For Any_Partition, look at all the partitions.

         if Stream.PID = Any_Partition then
            FID := First_Partition_Id;
            LID := Streams.Last;
         else
            FID := Stream.PID;
            LID := Stream.PID;
         end if;

         for P in FID .. LID loop
            From := Streams.Get_Component (P);
            if From /= null then

               pragma Debug (D ("Read - Lock stream" & P'Img));
               System.Garlic.Soft_Links.Enter (From.Critical);

               pragma Debug (D ("Read from stream" & P'Img));
               System.Garlic.Streams.Read (From.Incoming, Item, Len);

               pragma Debug (D ("Read - Unlock stream" & P'Img));
               System.Garlic.Soft_Links.Leave (From.Critical);

               if Len /= 0 then

                  pragma Debug (D ("Read new message"));
                  pragma Debug (D ("Read" & Len'Img & " bytes"));

                  --  There are elements left. Signal to potential
                  --  consumers.

                  if From.Incoming.Count /= 0 then
                     pragma Debug (D ("Read - Signal stream" & P'Img));
                     System.Garlic.Soft_Links.Update (From.Consumer);
                     System.Garlic.Soft_Links.Update (Any.Consumer);
                  end if;
                  exit;
               end if;
            end if;

         end loop;

         exit when Len /= 0;

         System.Garlic.Soft_Links.Lookup (Str.Consumer, Version);
         System.Garlic.Soft_Links.Differ (Str.Consumer, Version);
      end loop;
      Last := Len;
   exception when others =>
      pragma Debug (D ("exception raised in Read"));
      null;
   end Read;

   -----------
   -- Write --
   -----------

   procedure Write
     (Stream : in out Partition_Stream_Type;
      Item   : Ada.Streams.Stream_Element_Array)
   is
      Str : Partition_Stream_Access;
   begin
      if not Stream.Open then
         raise Stream_Error;
      end if;

      Str := Fetch (Stream.PID);

      --  Procedure Write just buffers the stream element
      --  array. Procedure Close really sends them.

      if Str.Mode /= Out_Mode then
         pragma Debug (D ("Mode should be Out_Mode"));
         raise Stream_Error;
      end if;

      pragma Debug (D ("Write - Lock stream" & Stream.PID'Img));
      System.Garlic.Soft_Links.Enter (Str.Critical);

      pragma Debug (D ("Write to stream" & Stream.PID'Img));
      Garlic.Streams.Write (Str.Outgoing, Item);

      pragma Debug (D ("Write - Unlock stream" & Stream.PID'Img));
      System.Garlic.Soft_Links.Leave (Str.Critical);
   exception when others =>
      pragma Debug (D ("exception raised in Write"));
      null;
   end Write;

begin
   Initialize;
end System.RPC.Stream_IO;