gnoga_2.1.2_5f127c56/deps/simple_components/os/windows/synchronization-interprocess.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
--                                                                    --
--  package                         Copyright (c)  Dmitry A. Kazakov  --
--     Synchronization.Interprocess                Luebeck            --
--  Implementation                                 Spring, 2018       --
--                                                                    --
--                                Last revision :  01:09 01 May 2018  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either version  2  of  --
--  the License, or (at your option) any later version. This library  --
--  is distributed in the hope that it will be useful,  but  WITHOUT  --
--  ANY   WARRANTY;   without   even   the   implied   warranty   of  --
--  MERCHANTABILITY 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 along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 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.IO_Exceptions;    use Ada.IO_Exceptions;
with Ada.Tags;             use Ada.Tags;
with Strings_Edit.Quoted;  use Strings_Edit.Quoted;

with System.Address_To_Access_Conversions;

package body Synchronization.Interprocess is

   package Conversions is
      new System.Address_To_Access_Conversions (Head);

   package body Generic_Memory_Mapper is
      package Conversions is
         new System.Address_To_Access_Conversions (Object_Type);

      function Map
               (  Location : System.Address;
                  Owner    : Boolean
               )  return Object_Type_Ptr is
      begin
         if Owner then
            declare
               Mapping : Memory_Mapper;
               type Type_Ptr is access all Object_Type;
               for Type_Ptr'Storage_Pool use Mapping;
               Ptr : Type_Ptr;
            begin
               Mapping.Location := Location;
               Ptr := new Object_Type;
               return Ptr.all'Unchecked_Access;
            end;
         else
            declare
               use Conversions;
            begin
               return To_Pointer (Location).all'Unchecked_Access;
            end;
         end if;
      end Map;
   end Generic_Memory_Mapper;

   procedure Allocate
             (  Pool            : in out Memory_Mapper;
                Storage_Address : out Address;
                Size            : Storage_Count;
                Alignment       : Storage_Count
             )  is
   begin
      Storage_Address := Pool.Location;
   end Allocate;

   procedure Check_Open (Shared : Abstract_Shared_Environment) is
   begin
      if Shared.File = INVALID_HANDLE_VALUE then
         Raise_Exception
         (  Status_Error'Identity,
            "Shared environment is not open"
         );
      end if;
   end Check_Open;

   procedure Clean_Up
             (  Shared : in out Abstract_Shared_Environment'Class
             )  is
      Result : BOOL;
   begin
      if Shared.Map /= null then
         declare
            This : Abstract_Shared_Object_Ptr := Shared.First;
            Prev : Abstract_Shared_Object_Ptr;
            Next : Abstract_Shared_Object_Ptr;
         begin
            while This /= null loop
               Next := This.Next;
               This.Next := Prev;
               Prev := This;
               This := Next;
            end loop;
            This := Prev;
            while This /= null loop
               Prev := This.Next;
               Unmap (This.all, Shared, Shared.Owner);
               This := Prev;
            end loop;
         end;
         Result := UnmapViewOfFile (Shared.Map.all'Address);
         Shared.Map := null;
      end if;
      if Shared.File /= INVALID_HANDLE_VALUE then
         Result      := CloseHandle (Shared.File);
         Shared.File := INVALID_HANDLE_VALUE;
      end if;
      Free (Shared.Name);
      if Shared.Server /= INVALID_HANDLE_VALUE then
         Result        := CloseHandle (Shared.Server);
         Shared.Server := INVALID_HANDLE_VALUE;
      end if;
   end Clean_Up;

   procedure Close (Shared : in out Abstract_Shared_Environment) is
      Result : int;
   begin
      if Shared.Map = null then
         Raise_Exception
         (  Status_Error'Identity,
            "Shared environment is not open"
         );
      end if;
   end Close;

   procedure Deallocate
             (  Pool            : in out Memory_Mapper;
                Storage_Address : Address;
                Size            : Storage_Count;
                Alignment       : Storage_Count
             )  is
   begin
      raise Program_Error;
   end Deallocate;

  procedure Start
             (  Shared : in out Abstract_Shared_Environment'Class;
                Stream : Walker
             )  is
      This     : Abstract_Shared_Object_Ptr := Stream.First;
      Location : Address :=
                    (  Shared.Map.all'Address
                    +  Round (Head'Max_Size_In_Storage_Elements)
                    );
   begin
      while This /= null loop
         Map
         (  Object   => This.all,
            Shared   => Shared,
            Location => Location,
            Size     => This.Shared_Size,
            Owner    => Shared.Owner
         );
         Location := Location + This.Shared_Size;
         This     := This.Next;
      end loop;
      This := Stream.First;
      while This /= null loop
         Start
         (  Object => This.all,
            Shared => Shared,
            Owner  => Shared.Owner
         );
         This := This.Next;
      end loop;
   end Start;

   procedure Do_Create
             (  Shared : in out Abstract_Shared_Environment'Class;
                Stream : Walker
             )  is
   begin
      Shared.Owner := True;
      declare
         Location : constant Address :=
                    MapViewOfFile
                    (  FileMappingObject  => Shared.File,
                       NumberOfBytesToMap => size_t (Stream.Size)
                    );
      begin
         if Location = Null_Address then
            declare
               Error : constant DWORD := GetLastError;
            begin
               Raise_From_LastError
               (  Data_Error'Identity,
                  (  "Mapping "
                  &  Quote (Value (Shared.Name))
                  &  " view fault: "
                  ),
                  Error
               );
            end;
         end if;
         Shared.Map :=
            Conversions.To_Pointer (Location).all'Unchecked_Access;
         Shared.Map.Checksum    := Stream.Sum_1 * 2**16 + Stream.Sum_2;
         Shared.Map.Size        := Stream.Size;
         Shared.Map.Initialized := False;
      end;
      Shared.Map.Owner := GetCurrentProcessId;
      Start (Shared, Stream);
      Shared.Map.Initialized := True;
   exception
      when others =>
         Clean_Up (Shared);
         raise;
   end Do_Create;

   procedure Do_Open
             (  Shared : in out Abstract_Shared_Environment'Class;
                Stream : Walker
             )  is
   begin
      Shared.Owner := False;
      declare
         Location : constant Address :=
                    MapViewOfFile
                    (  FileMappingObject  => Shared.File,
                       NumberOfBytesToMap => size_t (Stream.Size)
                    );
      begin
         if Location = Null_Address then
            declare
               Error : constant DWORD := GetLastError;
            begin
               Raise_From_LastError
               (  Data_Error'Identity,
                  (  "Mapping "
                  &  Quote (Value (Shared.Name))
                  &  " view fault: "
                  ),
                  Error
               );
            end;
         end if;
         Shared.Map :=
            Conversions.To_Pointer (Location).all'Unchecked_Access;
      end;
      while not Shared.Map.Initialized loop
         delay 0.1;
      end loop;
      if Shared.Map.Size /= Stream.Size then
         Raise_Exception
         (  Data_Error'Identity,
            (  "Invalid memory map size"
            &  Storage_Count'Image (Shared.Map.Size)
            &  ", expected"
            &  Storage_Count'Image (Stream.Size)
         )  );
      elsif (  Shared.Map.Checksum
            /= Stream.Sum_1 * 2**16 + Stream.Sum_2
            )  then
         Raise_Exception
         (  Data_Error'Identity,
            (  "Invalid memory map checksum"
            &  Unsigned_32'Image (Stream.Sum_1 * 2**16 + Stream.Sum_2)
            &  ", expected"
            &  Unsigned_32'Image (Shared.Map.Checksum)
         )  );
      end if;
      Shared.Server := OpenProcess
                       (  DesiredAccess => PROCESS_DUP_HANDLE,
                          ProcessId     => Shared.Map.Owner
                       );
      if Shared.Server = 0 then
         Shared.Server := INVALID_HANDLE_VALUE;
         declare
            Error : constant DWORD := GetLastError;
         begin
            Raise_From_LastError
            (  Data_Error'Identity,
               "Cannot obtain mapping owner process handle: ",
               Error
            );
         end;
      end if;
      Start (Shared, Stream);
   exception
      when others =>
         Clean_Up (Shared);
         raise;
   end Do_Open;

   procedure Create
             (  Shared : in out Abstract_Shared_Environment;
                Name   : String
             )  is
      Stream : aliased Walker;
   begin
      if Shared.Map /= null then
         Raise_Exception
         (  Status_Error'Identity,
            "Shared environment is already open"
         );
      end if;
      Stream.Size := Round (Head'Max_Size_In_Storage_Elements);
      Abstract_Shared_Environment'Class'Write
      (  Stream'Access,
         Abstract_Shared_Environment'Class (Shared)
      );
      Shared.First := Stream.First;
      Shared.Name  := New_String (Name);
      Shared.File  := CreateFileMapping
                      (  Protect        => PAGE_READWRITE,
                         MaximumSizeLow => DWORD (Stream.Size),
                         Name           => Shared.Name
                      );
      declare
         Error : constant DWORD := GetLastError;
      begin
         if Shared.File = 0 then
            Shared.File := INVALID_HANDLE_VALUE;
               Raise_From_LastError
               (  Data_Error'Identity,
                  (  "Mapping "
                  &  Quote (Value (Shared.Name))
                  &  " creation fault: "
                  ),
                  Error
               );
         elsif Error = ERROR_ALREADY_EXISTS then
            Raise_Exception
            (  Use_Error'Identity,
               (  "Mapping "
               &  Quote (Value (Shared.Name))
               &  " alreay exists"
            )  );
         end if;
      end;
      Do_Create (Shared, Stream);
   end Create;

   procedure Enumerate
             (  Stream : access Root_Stream_Type'Class;
                Object : Abstract_Shared_Object
             )  is
      Self : Walker'Class renames Walker'Class (Stream.all);
      This : Abstract_Shared_Object'Class renames Object.Self.all;
   begin
      This.Offset      := Self.Size;
      This.Shared_Size := Round (Get_Size (This));
      Self.Size        := Self.Size + This.Shared_Size;
      if Self.First = null then
         Self.First := This.Self;
         Self.Last  := This.Self;
      else
         Self.Last.Next := This.Self;
         Self.Last      := This.Self;
      end if;
      declare
         Sum_1    : Unsigned_32 renames Self.Sum_1;
         Sum_2    : Unsigned_32 renames Self.Sum_2;
         Position : Unsigned_32 renames Self.Position;
      begin
         Sum_1 := (  Sum_1
                  +  Unsigned_32 (Get_Signature (This))
                  *  Position
                  );
         Sum_2 := (Sum_2 + Sum_1) mod 16#FFFF#;
         Sum_1 := Sum_1 mod 16#FFFF#;
         Position := Position + 1;
      end;
   end Enumerate;

   procedure Finalize (Shared : in out Abstract_Shared_Environment) is
   begin
      Clean_Up (Shared);
   end Finalize;

   function Get_Offset
            (  Object : Abstract_Shared_Object
            )  return Storage_Offset is
   begin
      return Object.Offset;
   end Get_Offset;

   function Get_Signature (Data : String) return Unsigned_16 is
      Sum_1 : Unsigned_16 := 16#FF#;
      Sum_2 : Unsigned_16 := 16#FF#;
   begin
      for Major in 1..Data'Length / 21 loop
         for Index in Major * 21 - 20..Major * 21 loop
            Sum_1 := Sum_1 + Unsigned_16 (Character'Pos (Data (Index)));
            Sum_2 := Sum_2 + Sum_1;
         end loop;
         Sum_1 := (Sum_1 and 16#FF#) + Sum_1 / 2**8;
         Sum_2 := (Sum_2 and 16#FF#) + Sum_2 / 2**8;
      end loop;
      Sum_1 := (Sum_1 and 16#FF#) + Sum_1 / 2**8;
      Sum_2 := (Sum_2 and 16#FF#) + Sum_2 / 2**8;
      return (Sum_1 and 16#FF#) + (Sum_2 and 16#FF#) * 2**16;
   end Get_Signature;

   function Get_Signature
            (  Object : Abstract_Shared_Object
            )  return Unsigned_16 is
   begin
      return
      (  Get_Signature
         (  External_Tag
            (  Abstract_Shared_Object'Class (Object)'Tag
      )  )  );
   end Get_Signature;

   function Get_Size
            (  Shared : Abstract_Shared_Environment
            )  return Storage_Count is
   begin
      if Shared.Map = null then
         Raise_Exception
         (  Status_Error'Identity,
            "Shared environment is not open"
         );
      end if;
      return Shared.Map.Size;
   end Get_Size;

   procedure Initialize (Shared : in out Abstract_Shared_Environment) is
   begin
      null;
   end Initialize;

   procedure Open
             (  Shared : in out Abstract_Shared_Environment;
                Name   : String;
                Create : Boolean := False
             )  is
      Stream : aliased Walker;
   begin
      if Shared.Map /= null then
         Raise_Exception
         (  Status_Error'Identity,
            "Shared environment is already open"
         );
      end if;
      Stream.Size := Round (Head'Max_Size_In_Storage_Elements);
      Abstract_Shared_Environment'Class'Write
      (  Stream'Access,
         Abstract_Shared_Environment'Class (Shared)
      );
      Shared.First := Stream.First;
      Shared.Name  := New_String (Name);
      if Create then
         Shared.File := CreateFileMapping
                        (  Protect        => PAGE_READWRITE,
                           MaximumSizeLow => DWORD (Stream.Size),
                           Name           => Shared.Name
                        );
         declare
            Error : constant DWORD := GetLastError;
         begin
            if Shared.File = 0 then
               Shared.File := INVALID_HANDLE_VALUE;
                  Raise_From_LastError
                  (  Data_Error'Identity,
                     (  "Mapping "
                     &  Quote (Value (Shared.Name))
                     &  " creation or opening fault: "
                     ),
                     Error
                  );
            elsif Error = ERROR_ALREADY_EXISTS then
               Do_Open (Shared, Stream);
            else
               Do_Create (Shared, Stream);
            end if;
         end;
      else
         Shared.File := OpenFileMapping
                        (  DesiredAccess => FILE_MAP_ALL_ACCESS,
                           Name          => Shared.Name
                        );
         if Shared.File = 0 then
            Shared.File := INVALID_HANDLE_VALUE;
            declare
               Error : constant DWORD := GetLastError;
            begin
               case Error is
                  when ERROR_FILE_NOT_FOUND | ERROR_PATH_NOT_FOUND =>
                     Raise_Exception
                     (  Use_Error'Identity,
                        (  "Mapping "
                        &  Quote (Value (Shared.Name))
                        &  " does not exist"
                     )  );
                  when others =>
                     Raise_From_LastError
                     (  Data_Error'Identity,
                        (  "Mapping "
                        &  Quote (Value (Shared.Name))
                        &  " opening fault: "
                        ),
                        Error
                     );
               end case;
            end;
         else
            Do_Open (Shared, Stream);
         end if;
      end if;
   end Open;

   procedure Read
             (  Stream : in out Walker;
                Item   : out Stream_Element_Array;
                Last   : out Stream_Element_Offset
             )  is
   begin
      null;
   end Read;

   function Round (Offset : Storage_Count) return Storage_Count is
   begin
      return ((Offset + 7) / 8) * 8;
   end;

   procedure Start
             (  Object : in out Abstract_Shared_Object;
                Shared : in out Abstract_Shared_Environment'Class;
                Owner  : Boolean
             )  is
   begin
      null;
   end Start;

   function Storage_Size (Pool : Memory_Mapper) return Storage_Count is
   begin
      return Storage_Count'Last;
   end Storage_Size;

   procedure Unmap
             (  Object : in out Abstract_Shared_Object;
                Shared : in out Abstract_Shared_Environment'Class;
                Owner  : Boolean
             )  is
   begin
      null;
   end Unmap;

   procedure Write
             (  Stream      : access Root_Stream_Type'Class;
                Environment : Abstract_Shared_Environment
             )  is
   begin
      null;
   end Write;

   procedure Write
             (  Stream : in out Walker;
                Item   : in Stream_Element_Array
             )  is
   begin
      null;
   end Write;

end Synchronization.Interprocess;