aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/c9/c954023.a
blob: bfa69dc60540672d219d29238584f3ef07b70487 (plain)
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
-- C954023.A
--
--                             Grant of Unlimited Rights
--
--     Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687,
--     F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained 
--     unlimited rights in the software and documentation contained herein.
--     Unlimited rights are defined in DFAR 252.227-7013(a)(19).  By making 
--     this public release, the Government intends to confer upon all 
--     recipients unlimited rights  equal to those held by the Government.  
--     These rights include rights to use, duplicate, release or disclose the 
--     released technical data and computer software in whole or in part, in 
--     any manner and for any purpose whatsoever, and to have or permit others 
--     to do so.
--
--                                    DISCLAIMER
--
--     ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR
--     DISCLOSED ARE AS IS.  THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED 
--     WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE
--     SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE 
--     OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A
--     PARTICULAR PURPOSE OF SAID MATERIAL.
--*
--
-- OBJECTIVE:
--     Check that a requeue within a protected entry to a family of entries 
--     in a different protected object is queued correctly
--                      Call with parameters
--                      Requeue with abort
--
-- TEST DESCRIPTION: 
--      One transaction is sent through to check the paths.  After processing
--      this, the Credit task sets the "overloaded" indicator.  Once this
--      indicator is set the Distributor (a protected object) queues lower
--      priority transactions on a family of queues (Wait_for_Underload) in
--      another protected object using a requeue.  The Distributor still
--      delivers high priority transactions. After two more high priority
--      transactions have been processed by the Credit task the artificial
--      test code clears the overload condition to the threshold level that
--      allows only the items on the Medium priority queue of the family to be
--      released.  When these have been processed and checked the test code
--      then lowers the priority threshold once again, allowing the Low
--      priority items from the last queue in the family to be released,
--      processed and checked. Note: the High priority queue in the family is 
--      not used.   
--     
--      This series of tests uses a simulation of a transaction driven
--      processing system.  Line Drivers accept input from an external source
--      and build them into transaction records.  These records are then
--      encapsulated in message tasks which remain extant for the life of the
--      transaction in the system.  The message tasks put themselves on the
--      input queue of a Distributor which, from information in the
--      transaction and/or system load conditions forwards them to other
--      operating tasks. These in turn might forward the transactions to yet
--      other tasks for further action.  The routing is, in real life, dynamic
--      and unpredictable at the time of message generation.  All rerouting in
--      this  model is done by means of requeues.
--     
--
-- CHANGE HISTORY:
--      06 Dec 94   SAIC    ACVC 2.0
--
--!

with Report;
with ImpDef;
         
procedure C954023 is

   -- Artificial: number of messages required for this test
   subtype TC_Trans_Range is integer range 1..8;

   TC_Credit_Messages_Expected  : constant integer 
                                          := TC_Trans_Range'Last - 1;

   TC_Debit_Message_Complete    : Boolean := false;


   -- Mechanism for handshaking between tasks
   protected TC_PO is
      procedure Increment_Tasks_Completed_Count;
      function  Tasks_Completed_Count return integer;
      function  First_Message_Has_Arrived return Boolean;
      procedure  Set_First_Message_Has_Arrived;
   private
      Number_Complete : integer := 0;
         Message_Arrived_Flag  : Boolean := false;
   end TC_PO;
   -- 
   protected body TC_PO is
      procedure Increment_Tasks_Completed_Count is
      begin
         Number_Complete := Number_Complete + 1;
      end Increment_Tasks_Completed_Count;
  
      function Tasks_Completed_Count return integer is
      begin
         return Number_Complete;
      end Tasks_Completed_Count;

      function  First_Message_Has_Arrived return Boolean is
         begin
            return Message_Arrived_Flag;
         end First_Message_Has_Arrived;

      procedure  Set_First_Message_Has_Arrived is
         begin
            Message_Arrived_Flag := true;
      end Set_First_Message_Has_Arrived;

   end TC_PO;

begin
   
   Report.Test ("C954023", "Requeue from within a protected object" &
                      " to a family of entries in another protected object");

 
   declare -- encapsulate the test
   
      -- Arbitrary test values
      Credit_Return : constant := 1;
      Debit_Return  : constant := 2;

      type Transaction_Code is (Credit, Debit);
      type App_Priority     is (Low, Medium, High);
      type Priority_Block   is array (App_Priority) of Boolean;

      type Transaction_Record;
      type acc_Transaction_Record is access Transaction_Record;
      type Transaction_Record is 
         record
            ID               : integer := 0;
            Code             : Transaction_Code := Debit;
            Priority         : App_Priority := High;
            Account_Number   : integer := 0;
            Stock_Number     : integer := 0;
            Quantity         : integer := 0;
            Return_Value     : integer := 0;
            TC_Message_Count : integer := 0;
            TC_Thru_Distrib  : Boolean := false;
         end record;
   
   
      task type Message_Task is 
         entry Accept_Transaction (In_Transaction : acc_Transaction_Record);
      end Message_Task;
      type acc_Message_Task is access Message_Task;
   
      task Line_Driver is
         entry Start;
      end Line_Driver;
   
      protected Distributor is
         procedure Set_Credit_Overloaded;
         procedure Clear_Overload_to_Medium;
         procedure Clear_Overload_to_Low;
         entry     Input (Transaction : acc_Transaction_Record);
      private
         Credit_Overloaded : Boolean := false;
      end Distributor;
   
      protected Hold is
         procedure Release_Medium;
         procedure Release_Low;
         -- Family of entry queues indexed by App_Priority
         entry Wait_for_Underload (App_Priority)
                                     (Transaction : acc_Transaction_Record);
      private
         Release : Priority_Block := (others => false);
      end Hold;
   
      task Credit_Computation is
         entry Input(Transaction : acc_Transaction_Record);
      end Credit_Computation;
   
      task Debit_Computation is
         entry Input(Transaction : acc_Transaction_Record);
      end Debit_Computation;
   
      --
      -- Dispose each input Transaction_Record to the appropriate
      -- computation tasks
      --
      protected body Distributor is
   
         procedure Set_Credit_Overloaded is
         begin
            Credit_Overloaded := true;
         end Set_Credit_Overloaded;
   
         procedure Clear_Overload_to_Medium is
         begin
            Credit_Overloaded := false;
            Hold.Release_Medium;       -- Release all held messages on Medium
                                       -- priority queue
         end Clear_Overload_to_Medium;
     
         procedure Clear_Overload_to_Low is
         begin
            Credit_Overloaded := false;
            Hold.Release_Low;          -- Release all held messages on Low
                                       -- priority queue
         end Clear_Overload_to_Low;
     
   
   
         entry Input (Transaction : acc_Transaction_Record)   when true is
                                                   -- barrier is always open
         begin
            -- Test Control: Set the indicator in the message to show it has
            -- passed through the Distributor object
            Transaction.TC_thru_Distrib := true;
    
            -- Pass this transaction on to the appropriate computation
            -- task but temporarily hold low-priority transactions under
            -- overload conditions
            case Transaction.Code is
               when Credit =>
                  if Credit_Overloaded and Transaction.Priority /= High then
                     -- use the appropriate queue in the family 
                     requeue Hold.Wait_for_Underload(Transaction.Priority)
                                                         with abort;
                  else
                     requeue Credit_Computation.Input with abort;
                 end if;
               when Debit =>
                 requeue Debit_Computation.Input with abort;
            end case;
         end Input;
      end Distributor;
   
   
      -- Low priority Message tasks are held on the Wait_for_Underload queue
      -- while the Credit computation system is overloaded.  Once the Credit
      -- system reached underload send all queued messages immediately
      --
      protected body Hold is
         
         -- Once these are executed the barrier conditions for the entries
         -- are evaluated 
         procedure Release_Medium is
         begin 
            Release(Medium) := true;
         end Release_Medium;
         --
         procedure Release_Low is
         begin 
            Release(Low) := true;
         end Release_Low;
   
         -- This is a family of entry queues indexed by App_Priority
         entry Wait_for_Underload  (for AP in App_Priority)
                                   (Transaction : acc_Transaction_Record)
                                                when Release(AP) is
         begin
            requeue Credit_Computation.Input with abort;
            if Wait_for_Underload(AP)'count = 0 then
               -- Queue is purged.  Set up to hold next batch
               Release(AP) := false;
            end if; 
         end Wait_for_Underload;
   
      end Hold;
      
   
   
   
      -- Assemble messages received from an external source
      --   Creates a message task for each. The message tasks remain extant
      --   for the life of the messages in the system.
      --      The Line Driver task would normally be designed to loop 
      --      creating the messages as input is received.  Simulate this 
      --      but limit it to the required number of dummy messages needed for
      --      this test and allow it to terminate at that point.  Artificially
      --      cycle the generation of High medium and Low priority Credit 
      --      transactions for this test. Send out one final Debit message
      --
      task body Line_Driver is
         Current_ID       : integer := 1;
         Current_Priority : App_Priority := High;      
   
         procedure Build_Credit_Record 
                              ( Next_Transaction : acc_Transaction_Record ) is
            Dummy_Account : constant integer := 100;
         begin
               Next_Transaction.ID := Current_ID;
               Next_Transaction.Code := Credit; 
               Next_Transaction.Priority := Current_Priority;
   
               Next_Transaction.Account_Number := Dummy_Account;
               Current_ID := Current_ID + 1;
         end Build_Credit_Record;     
   
   
         procedure Build_Debit_Record 
                            ( Next_Transaction : acc_Transaction_Record ) is
            Dummy_Account : constant integer := 200;
         begin
               Next_Transaction.ID := Current_ID;
               Next_Transaction.Code := Debit; 
   
               Next_Transaction.Account_Number := Dummy_Account;
               Current_ID := Current_ID + 1;
         end Build_Debit_Record;     
   
      begin
         
         for Transaction_Numb in TC_Trans_Range loop  -- TC: limit the loop
            declare 
               -- Create a task for the next message
               Next_Message_Task : acc_Message_Task := new Message_Task;
               -- Create a record for it
               Next_Transaction : acc_Transaction_Record := 
                                                   new Transaction_Record;
            begin
               if Transaction_Numb = TC_Trans_Range'first then
                  -- Send the first Credit message
                  Build_Credit_Record ( Next_Transaction );
                  Next_Message_Task.Accept_Transaction ( Next_Transaction );  
                  -- TC: Wait until the first message has been received by the
                  -- Credit task and it has set the Overload indicator for the 
                  -- Distributor
                  while not TC_PO.First_Message_Has_Arrived loop
                     delay ImpDef.Minimum_Task_Switch;   
                  end loop;
               elsif Transaction_Numb = TC_Trans_Range'last then
                  -- For this test send the last transaction to the Debit task
                  -- to improve the mix
                  Build_Debit_Record( Next_Transaction );
                  Next_Message_Task.Accept_Transaction ( Next_Transaction );  
               else
                  -- TC: Cycle generation of  high medium and low priority 
                  -- transactions
                  if Current_Priority = High then
                     Current_Priority := Medium;
                  elsif  
                     Current_Priority = Medium then
                     Current_Priority := Low;
                  else
                     Current_Priority := High;
                  end if;
                  Build_Credit_Record( Next_Transaction );
                  Next_Message_Task.Accept_Transaction ( Next_Transaction );  
               end if;
            end;   -- declare
         end loop;
   
      exception
         when others => 
            Report.Failed ("Unexpected exception in Line_Driver");
      end Line_Driver;
   
      
   
   
      task body Message_Task is
   
         TC_Original_Transaction_Code : Transaction_Code;  
         This_Transaction : acc_Transaction_Record := new Transaction_Record;
   
      begin
         
         accept Accept_Transaction(In_Transaction : acc_Transaction_Record) do
            This_Transaction.all := In_Transaction.all;
         end Accept_Transaction;
   
         -- Note the original code to ensure correct return
         TC_Original_Transaction_Code := This_Transaction.Code;
    
         -- Queue up on Distributor's Input queue
         Distributor.Input ( This_Transaction );
         -- This task will now wait for the requeued rendezvous 
         -- to complete before proceeding
   
         -- After the required computations have been performed
         -- return the Transaction_Record appropriately (probably to an output
         -- line driver)
         null;            -- stub
         
         -- For the test check that the return values are as expected
         if TC_Original_Transaction_Code /= This_Transaction.Code then
            -- Incorrect rendezvous
            Report.Failed ("Message Task: Incorrect code returned");
         end if;
   
         if This_Transaction.Code = Credit then
            if This_Transaction.Return_Value  /= Credit_Return   or
            not This_Transaction.TC_thru_Distrib                 then
               Report.Failed ("Expected path not traversed - Credit");
            end if;
            TC_PO.Increment_Tasks_Completed_Count;
         else
            if This_Transaction.Return_Value  /= Debit_Return or   
               This_Transaction.TC_Message_Count /= 1         or
               not This_Transaction.TC_thru_Distrib           then
                  Report.Failed ("Expected path not traversed - Debit");
            end if;
            TC_Debit_Message_Complete := true;
         end if;
   
      exception
         when others => 
            Report.Failed ("Unexpected exception in Message_Task");
      end Message_Task;
   
   
   
   
     
      -- Computation task.  After the computation is performed the rendezvous
      -- in the original message task is completed.                              
      task body Credit_Computation is
   
         Message_Count   : integer := 0;
         
      begin
         loop
            select 
               accept Input ( Transaction : acc_Transaction_Record) do
                  
                  -- Perform the computations required for this transaction
                  null; -- stub
   
                  
                  -- The following is all Test Control code:
                 
                  if not Transaction.TC_thru_Distrib then
                     Report.Failed 
                            ("Credit Task: Wrong queue, Distributor bypassed");
                  end if;
   
                  if Transaction.code /= Credit then
                     Report.Failed
                         ("Credit Task: Requeue delivered to the wrong queue");
                  end if;
   
                  -- This is checked by the Message_Task:
                  Transaction.Return_Value := Credit_Return;
   
                  -- Now take special action depending on which Message.
                  -- Note: The count gives the order in which the messages are
                  --       arriving at this task NOT the order in which they
                  --       were originally generated and sent out.
   
                  Message_Count := Message_Count + 1;
   
                  if Message_Count < 4 then 
                     -- This is one of the first three messages which must 
                     -- be High priority because we will set "Overload" after 
                     -- the first, which is known to be High. The lower
                     -- priority should be waiting on the queues
                     if Transaction.Priority /= High then  
                     Report.Failed 
                        ("Credit Task: Lower priority trans. during overload");
                     end if;
                     if Message_Count = 1 then 
                        -- After the first message :
                        Distributor.Set_Credit_Overloaded;
                        -- Now flag the Line_Driver that the second and 
                        -- subsequent messages may now be sent
                        TC_PO.Set_First_Message_Has_Arrived;
                     elsif
                        Message_Count = 3 then
                        -- The two high priority transactions created 
                        -- subsequent to the overload have now been processed,
                        --  release the Medium priority items
                        Distributor.Clear_Overload_to_Medium; 
                     end if;
                  elsif Message_Count < 6 then
                     -- This must be one of the Medium priority messages
                     if Transaction.Priority /= Medium then  
                     Report.Failed 
                           ("Credit Task: Second group not Medium Priority");
                     end if;
                     if Message_Count = 5 then 
                        -- The two medium priority transactions
                        -- have now been processed - release the
                        -- Low priority items
                        Distributor.Clear_Overload_to_Low; 
                     end if;
                  elsif Message_Count < TC_Trans_Range'Last then
                     -- This must be one of the Low priority messages
                     if Transaction.Priority /= Low then  
                     Report.Failed 
                           ("Credit Task: Third group not Low Priority");
                     end if;
                  else
                     -- Too many transactions have arrived.  Duplicates?
                     -- the Debit transaction?
                     Report.Failed 
                           ("Credit Task: Too many transactions");
                  end if;
               end Input;            
            or
               terminate;
            end select;
         end loop;
      exception
         when others => 
            Report.Failed ("Unexpected exception in Credit_Computation");
      end Credit_Computation;
   
   
   
      -- Computation task.  After the computation is performed the rendezvous
      -- in the original message task is completed.        
      --                      
      task body Debit_Computation is
         Message_Count   : integer := 0;
      begin
         loop
            select
               accept Input (Transaction : acc_Transaction_Record) do
                  -- Perform the computations required for this message
                  null;      -- stub
   
                  -- For the test: 
                  if not Transaction.TC_thru_Distrib then
                     Report.Failed 
                         ("Debit Task: Wrong queue, Distributor bypassed");
                  end if;
                  if Transaction.code /= Debit then
                     Report.Failed
                         ("Debit Task: Requeue delivered to the wrong queue");
                  end if;
   
                  -- for the test plug a known value and count
                  Transaction.Return_Value := Debit_Return;
                  -- one, and only one, message should pass through
                  Message_Count := Message_Count + 1;
                  Transaction.TC_Message_Count := Message_Count;
               end Input;            
            or
               terminate;
            end select;
         end loop;
      exception
         when others => 
            Report.Failed ("Unexpected exception in Debit_Computation");
      end Debit_Computation;
   

   begin -- declare 

      null;

   end; -- declare (test encapsulation)

   if (TC_PO.Tasks_Completed_Count /= TC_Credit_Messages_Expected) 
                                    and not TC_Debit_Message_Complete then
      Report.Failed ("Incorrect number of Message Tasks completed");
   end if;

   Report.Result;

end C954023;