simple_components_4.68.0_da9b0f3a/atomic-access/gcc-long-offsets/generic_blackboard.ads

  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
--                                                                    --
--  package Generic_Blackboard      Copyright (c)  Dmitry A. Kazakov  --
--  Interface                                      Luebeck            --
--                                                 Autumn, 2013       --
--                                                                    --
--                                Last revision :  17:44 21 Jul 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.       --
--____________________________________________________________________--
--
--  Blackboard  is  a container which can be used for creating lock-free
--  notification queueus and storage pools. The elements are  stored  in
--  the blackboard temporarily. Newly coming elements override the  most
--  elder  ones.  The  elements  in  the blackboard are accessed through
--  references which know if the destination is still valid. Because the
--  elements in the blackboard are destructed automatically, it  is  not
--  allowed  to use elements of controlled types. Otherwise, indefinite,
--  tagged  and  class-wide  elements can be used. The generic parameter
--  is:
--
--      Element_Type - Of the elements in the blackboard
--
--  Typically a blackboard is  read  in  a  lock-free  way  using  plain
--  subprograms.  The  function  Get dereferences a blackboard reference
--  and returns the corresponding element if  the  reference  is  valid.
--  Othwerwise   Constraint_Error   is  propagated.  This  can  be  done
--  concurrently to other tasks doing Get  or  Put.  The  procedure  Put
--  shall  be  used in mutually exclusive way with other concurrent Put.
--  Thus if several tasks should call it, that must be done from  inside
--  a protected action or else through a monitor  task.  See  the  child
--  package Generic_Task_Safe which provides blackboards with  task-safe
--  operations.
--
--  Blackboard  elements can be iterated as follows:
--
--     Data : Blackboard;
--     ...
--     Item    : Reference;
--     Sequent : Boolean;
--     ...
--     Item := First (Data);
--     while not (Item > Data) loop
--        begin
--           ... Get (Data, Item) ... -- Use Item
--        exception
--           when Constraint_Error =>
--              ... -- The element is lost
--        end;
--        Next (Data, Item, Sequent);
--        if not Sequent then
--           ... -- Some elements were lost
--        end if;
--     end loop;
------------------------------------------------------------------------
-- This implementation uses GCC built-in  primitives.  It should be used
-- with GNAT compiler,  in the cases when pragma Atomic is not supported
-- for 64-bit types.
------------------------------------------------------------------------
with System.Storage_Elements;  use System.Storage_Elements;
with System.Storage_Pools;     use System.Storage_Pools;

with Ada.Finalization;
with Interfaces.C;

generic
   type Element_Type (<>) is private;
package Generic_Blackboard is
--
-- Blackboard -- The blackboard
--
--    Size - The size of, in storage elements
--
-- The  blackboard  size detemines how long survives an element put into
-- the balckboard, after consequent placing other elements into it.
--
   type Blackboard (Size : Storage_Count) is
      new Ada.Finalization.Limited_Controlled with private;
   type Reference is private;
--
-- First_Reference -- The first reference
--
   First_Reference : constant Reference;
--
-- First -- Get a reference of the first element in
--
--    Storage - The blackboard
--
-- Note  that  this  function  can return an invalid reference when used
-- concurrently. It that case the caller should call  it  again,  unless
-- the  result  is  greater than Storage according to >. That would mean
-- that the blackboard is empty.
--
   function First (Storage : Blackboard) return Reference;
--
-- Get -- Dereferencing
--
--    Storage - The blackboard
--    Pointer - A reference to an item in Storage
--
-- Returns :
--
--    The item referenced by Pointer
--
-- Exceptions :
--
--    Constraint_Error - The reference is no longer valid
--
   function Get (Storage : Blackboard; Pointer : Reference)
      return Element_Type;
--
-- Image -- String representation of a reference
--
--    Pointer - A reference to an item in Storage
--
-- Returns :
--
--    Text representation of Pointer
--
   function Image (Pointer : Reference) return String;
--
-- Is_Valid -- Validity check
--
--    Storage - The blackboard
--    Pointer - A reference to an item in Storage
--
-- Is_Valid (B, R) is equivalent to not (B < R or B > R)
--
-- Returns :
--
--    True if Pointer is a valid reference
--
   function Is_Valid (Storage : Blackboard; Pointer : Reference)
      return Boolean;
--
-- Next -- Advance reference to a next element
--
--    Storage - The blackboard
--    Pointer - A reference
--    Sequent - Set to True if the result is next to the argument
--
-- The  procedure  advances Pointer to the next element. When Pointer is
-- valid and there is a next element then Sequent is  set  to  True  and
-- Pointer will  refer  to  that  element. When Pointer refers to a lost
-- element then it is set to the first available element and Sequent  is
-- set to False. When Pointer refers to a not yet available element,  it
-- is not changed and Sequent is set to True.
--
   procedure Next
             (  Storage : Blackboard;
                Pointer : in out Reference;
                Sequent : out Boolean
             );
--
-- Put -- A new item into the blackboard
--
--    Storage   - The blackboard
--    Element   - The item to put
--  [ Pointer ] - A reference to the item
--
-- This  procedure  puts Element into Storage and returns a reference to
-- it. The operation overrides the most outdated items in the blackboard
-- making references to them invalid.
--
-- Exceptions :
--
--    Storage_Error - Element is too large to fit into
--
   procedure Put
             (  Storage : in out Blackboard;
                Element : Element_Type;
                Pointer : out Reference
             );
   procedure Put
             (  Storage : in out Blackboard;
                Element : Element_Type
             );
--
-- Put -- A new item into the blackboard
--
--    Storage   - The blackboard
--    Element   - The item to put
--    Preserve  - A reference to an non-overriddable item
--  [ Pointer ] - A reference to the item
--    Success   - The outcome of the operation
--
-- This  procedure  puts Element into Storage and returns a reference to
-- it. The operation overrides the most outdated items in the blackboard
-- except  for  ones  indicated  by Preserve or any later. The parameter
-- Success is True when Element was put into the blackboard. It is False
-- when putting Element there would require removing items references by
-- Preserve.
--
-- Exceptions :
--
--    Storage_Error - Element is too large to fit into
--
   procedure Put
             (  Storage  : in out Blackboard;
                Element  : Element_Type;
                Preserve : Reference;
                Pointer  : out Reference;
                Success  : out Boolean
             );
   procedure Put
             (  Storage  : in out Blackboard;
                Element  : Element_Type;
                Preserve : Reference;
                Success  : out Boolean
             );
--
-- Upper -- Get a reference of the least upper bound of the blackboard
--
--    Storage - The blackboard
--
-- The  function  returns  the least upper bound of Storage. That is the
-- reference to the element which will be put next into.
--
-- Returns :
--
--    The least upper bound
--
   function Upper (Storage : Blackboard) return Reference;
--
-- <, > -- Reference validity checks
--
-- When the blackboard is compared to  reference,  it  is  less  than  a
-- reference if the latter refer to a not  yet  written  object.  It  is
-- greater than a reference if the object is already lost (overwritten).
--
-- Returns :
--
--    Comparison result
--
   function "<" (Storage : Blackboard; Pointer : Reference)
      return Boolean;
   function ">" (Storage : Blackboard; Pointer : Reference)
      return Boolean;
   function "<" (Pointer : Reference; Storage : Blackboard)
      return Boolean;
   function ">" (Pointer : Reference; Storage : Blackboard)
      return Boolean;
--
-- <, <=, >= , > -- Reference comparisons
--
   function "<"  (Left, Right : Reference) return Boolean;
   function ">"  (Left, Right : Reference) return Boolean;
   function "<=" (Left, Right : Reference) return Boolean;
   function ">=" (Left, Right : Reference) return Boolean;
--
-- Atomic_Reference -- Reference type  used for atomic access.
--
-- This type  is  implementation  dependent  and  should   not  be  used
-- directly.  It represents a reference  type compatible with Reference.
-- An instance must be declared aliased.
--
   type Atomic_Reference is private;
   type Atomic_Reference_Ptr is access constant Atomic_Reference;
--
-- Load -- Atomically load
--
--    Source - Memory location to read
--
-- Returns :
--
--    Value at the location
--
   function Load
            (  Source : Atomic_Reference_Ptr;
               Model  : Interfaces.C.int := 0
            )  return Reference;
--
-- Store -- Atomically load
--
--    Destination - Memory location to write
--    Value       - To store at the location
--
   procedure Store
             (  Destination : access Atomic_Reference;
                Value       : Reference;
                Model       : Interfaces.C.int := 0
             );
private
   pragma Inline (First);
   pragma Inline (Upper);
   pragma Inline (Get);
   pragma Inline (Is_Valid);
   pragma Inline (Put);

   type Modular is mod 2**64;
   function LT (Left, Right : Modular) return Boolean renames "<";
   function GT (Left, Right : Modular) return Boolean renames ">";
   function LE (Left, Right : Modular) return Boolean renames "<=";
   function GE (Left, Right : Modular) return Boolean renames ">=";
--
-- Reference -- Is a flat index, the lower part modulus  Blackboard.Size
--              is the offset in the buffer. The upper part is the frame
-- number. When the buffer is wrapped the frame number is incremented.
--
   type Reference is new Modular;

   function "<"
            (  Left  : Reference;
               Right : Atomic_Reference_Ptr
            )  return Boolean;
   function ">="
            (  Left  : Reference;
               Right : Atomic_Reference_Ptr
            )  return Boolean;

   pragma Inline (">", "<", "<=", ">=");

   First_Reference : constant Reference := 0;

   type Atomic_Reference is new Interfaces.Unsigned_64;
   for Atomic_Reference'Alignment use 8;

   type Buffer is array (Storage_Count range <>) of Storage_Element;
   pragma Atomic_Components (Buffer);
   type Blackboard (Size : Storage_Count) is
      new Root_Storage_Pool with
   record
      Upper     : aliased Atomic_Reference := 0;
      Lower     : aliased Atomic_Reference := 0;
      Preserved : Reference;
      Pointer   : Reference;       -- The reference to the last element
      Index     : Storage_Count;   -- The index of the last element
      Length    : Storage_Count;   -- The length of the last element
      Address   : System.Address;
      Data      : Buffer (1..Size);
   end record;

   procedure Allocate
             (  Storage   : in out Blackboard;
                Address   : out System.Address;
                Size      : Storage_Count;
                Alignment : Storage_Count
             );
   procedure Deallocate
             (  Storage   : in out Blackboard;
                Address   : System.Address;
                Size      : Storage_Count;
                Alignment : Storage_Count
             );
   function Storage_Size (Storage : Blackboard) return Storage_Count;

   pragma Import (Intrinsic, Load,  "__atomic_load_8");
   pragma Import (Intrinsic, Store, "__atomic_store_8");

end Generic_Blackboard;