File: 7stasren.adb

package info (click to toggle)
gnat 3.10p-3
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 49,492 kB
  • ctags: 33,976
  • sloc: ansic: 347,844; ada: 227,415; sh: 8,759; yacc: 7,861; asm: 5,252; makefile: 3,632; objc: 475; cpp: 400; sed: 261; pascal: 95
file content (1490 lines) | stat: -rw-r--r-- 47,341 bytes parent folder | download
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
------------------------------------------------------------------------------
--                                                                          --
--                GNU ADA RUNTIME LIBRARY (GNARL) COMPONENTS                --
--                                                                          --
--            S Y S T E M . T A S K I N G . R E N D E Z V O U S             --
--                                                                          --
--                                 B o d y                                  --
--                         (Version for new GNARL)                          --
--                                                                          --
--                            $Revision: 1.65 $                             --
--                                                                          --
--            Copyright (C) 1991-1997, Florida State University             --
--                                                                          --
-- GNARL 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. GNARL is distributed in the hope that it will be useful, but WITH- --
-- OUT 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  distributed with GNARL; 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.                                      --
--                                                                          --
-- GNARL was developed by the GNARL team at Florida State University. It is --
-- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
-- State University (http://www.gnat.com).                                  --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Exceptions;
--  Used for Exception_ID
--           Null_Id

with System.Error_Reporting;
--  used for Shutdown

with System.Task_Primitives.Operations;
--  used for Get_Priority
--           Set_Priority
--           Write_Lock
--           Unlock
--           Sleep
--           Wakeup
--           Sleep_Until

with System.Tasking.Entry_Calls;
--  Used for Wait_For_Completion
--           Wait_For_Completion_With_Timeout
--           Wait_Until_Abortable

with System.Tasking.Initialization;
--  used for Defer_Abort
--           Undefer_Abort
--           Change_Base_Priority

with System.Tasking.Queuing;
--  used for Enqueue
--           Dequeue_Head
--           Select_Task_Entry_Call
--           Count_Waiting

with System.Tasking.Utilities;
--  Used for Abort_To_Level
--           Reset_Priority
--           Terminate_Alternative
--           Check_Exception

with Unchecked_Conversion;

package body System.Tasking.Rendezvous is

   use System.Task_Primitives.Operations;
   use System.Error_Reporting;

   procedure Change_Base_Priority (Self_ID : Task_ID) renames
     System.Tasking.Initialization.Change_Base_Priority;

   procedure Defer_Abort (Self_ID : Task_ID) renames
     System.Tasking.Initialization.Defer_Abort;

   procedure Undefer_Abort (Self_ID : Task_ID) renames
     System.Tasking.Initialization.Undefer_Abort;

   type Select_Treatment is (
     Accept_Alternative_Selected,
     Accept_Alternative_Completed,
     Else_Selected,
     Terminate_Selected,
     Accept_Alternative_Open,
     No_Alternative_Open);

   Default_Treatment : constant array (Select_Modes) of Select_Treatment :=
     (Simple_Mode         => No_Alternative_Open,
      Else_Mode           => Else_Selected,
      Terminate_Mode      => Terminate_Selected,
      Delay_Mode          => No_Alternative_Open);

   -----------------------
   -- Local Subprograms --
   -----------------------

   procedure Boost_Priority
     (Call     : Entry_Call_Link;
      Acceptor : Task_ID);
   pragma Inline (Boost_Priority);

   procedure Call_Synchronous
     (Acceptor              : Task_ID;
      E                     : Task_Entry_Index;
      Uninterpreted_Data    : System.Address;
      Mode                  : Call_Modes;
      Rendezvous_Successful : out Boolean);
   pragma Inline (Call_Synchronous);
   --  This call is used to make a simple or conditional entry call.

   procedure Do_Or_Queue
     (Self_ID    : Task_ID;
      Entry_Call : in out Entry_Call_Link);
   --  Either initiate the entry call, such that the accepting task is
   --  free to execute the rendezvous, queue the call on the acceptor's
   --  queue, or cancel the call.  Conditional calls that cannot be
   --  accepted immediately are cancelled.

   procedure Adjust_For_Terminate_Alternative_Call
     (Self_ID  : Task_ID;
      Acceptor : Task_ID);
   --  Called by caller to wake up the acceptor if it is waiting on
   --  terminate_alternative.

   procedure Setup_For_Rendezvous_With_Body
     (Entry_Call : Entry_Call_Link; Acceptor : Task_ID);
   --  When a rendezvous selected (ready for rendezvous) we need to save
   --  privious caller and adjust the priority. Also we need to make
   --  this call not Abortable (Cancellable) since the rendezvous has
   --  already been started.
   pragma Inline (Setup_For_Rendezvous_With_Body);

   procedure Await_Abortion (Acceptor : Task_ID);

   function Is_Entry_Open (T : Task_ID; E : Task_Entry_Index) return Boolean;
   pragma Inline (Is_Entry_Open);

   procedure Wait_For_Call (Self_ID : Task_ID);
   pragma Inline (Wait_For_Call);
   --  An accepting task goes into Sleep by calling this routine
   --  waiting for a call from the caller or waiting for an abortion.
   --  Make sure Self_ID is locked before calling this routine.

   --------------------
   -- Boost_Priority --
   --------------------

   procedure Boost_Priority (Call : Entry_Call_Link; Acceptor : Task_ID) is
      Caller        : Task_ID := Call.Self;
      Caller_Prio   : System.Any_Priority := Get_Priority (Caller);
      Acceptor_Prio : System.Any_Priority := Get_Priority (Acceptor);
   begin

      if Caller_Prio > Acceptor_Prio then
         Call.Acceptor_Prev_Priority := Acceptor_Prio;
         Set_Priority (Acceptor, Caller_Prio);
      else
         Call.Acceptor_Prev_Priority := Priority_Not_Boosted;
      end if;
   end Boost_Priority;

   -----------------
   -- Do_Or_Queue --
   -----------------

   procedure Do_Or_Queue
     (Self_ID       : Task_ID;
      Entry_Call : in out Entry_Call_Link)
   is
      E        : Task_Entry_Index := Task_Entry_Index (Entry_Call.E);
      Acceptor : Task_ID          := Entry_Call.Called_Task;

   begin
      if Acceptor.Accepting = Not_Accepting then
         if Callable (Acceptor) then
            if Entry_Call.Mode /= Conditional_Call
              or else Entry_Call.Abortable = False
            then
               Queuing.Enqueue (Acceptor.Entry_Queues (E), Entry_Call);
            end if;

         else
            --  If the acceptor is not callable, cancel the call
            --  and raise Tasking_Error.  The call is not cancelled
            --  for an asynchronous call, since Cancel_Task_Entry_Call
            --  will do the decrement in that case.

            --  ??? It would be better if all entry call cancellation
            --      and the raising of Tasking_Error could be isolated
            --      to Wait_For_Completion.

            if Entry_Call.Mode /= Asynchronous_Call then
               Entry_Call.Self.ATC_Nesting_Level :=
                 Entry_Call.Self.ATC_Nesting_Level - 1;
            end if;

            Unlock (Acceptor);
            Undefer_Abort (Self_ID);
            raise Tasking_Error;
         end if;

      else
         --  ??? This should have a special case for Trivial_Accept, so that
         --      we don't have the loop setup overhead.

         for J in Acceptor.Open_Accepts'Range loop
            if Entry_Call.E = Entry_Index (Acceptor.Open_Accepts (J).S) then

               --  Do rendezvous

               Acceptor.Accepting := Not_Accepting;
               Acceptor.Chosen_Index := J;

               --  Not abortable while in progress.

               if Entry_Call.Abortable /= Never then
                  Entry_Call.Abortable := False;
               end if;

               if Acceptor.Open_Accepts (J).Null_Body then

                  --  Normally, this would have to be protected by
                  --  the caller's mutex.  However, in this case we
                  --  know that the acceptor is accepting, which means
                  --  that it has yet to remove a call from its queue,
                  --  and it will need to lock its own mutex to do that,
                  --  which we hold.  It won't look at Entry_Call.Done
                  --  until it has the call, so it should be safe to
                  --  set it here.

                  Entry_Call.Done := True;

                  Wakeup (Acceptor);

               else
                  Setup_For_Rendezvous_With_Body (Entry_Call, Acceptor);
                  Wakeup (Acceptor);
               end if;

               exit;
            end if;

         end loop;

         --  If the acceptor was ready to accept this call,
         --  Acceptor.Accepting will have been set to Not_Accepting
         --  in the above loop. Otherwise, the acceptor is accepting,
         --  but not this entry. Try to queue the call.

         if Acceptor.Accepting /= Not_Accepting and then
           (Entry_Call.Mode /= Conditional_Call or else
              Entry_Call.Abortable = False) then
            Queuing.Enqueue (Acceptor.Entry_Queues (E), Entry_Call);
         end if;

      end if;
   end Do_Or_Queue;

   -------------------------------------------
   -- Adjust_For_Terminate_Alternative_Call --
   -------------------------------------------

   procedure Adjust_For_Terminate_Alternative_Call
     (Self_ID  : Task_ID;
      Acceptor : Task_ID)
   is
      P : Task_ID;

   begin
      --  This call is made with the Acceptor locked.

      Acceptor.Stage := Active;

      --  Need to set this flag off in order not to make subsequent calls
      --  to be treated to calls to Select With Terminate Alternative.

      Acceptor.Terminate_Alternative := False;

      Acceptor.Awake_Count := Acceptor.Awake_Count + 1;

      --  At this point, T.Awake_Count and P.Awaited_Dependent_Count could
      --  be out of synchronization. However, we know that
      --  P.Awaited_Dependent_Count cannot be zero, and cannot go to zero,
      --  since some other dependent must have just called us. There should
      --  therefore be no danger of the parent terminating before we
      --  increment P.Awaited_Dependent_Count below.

      if Acceptor.Awake_Count = 1 then
         Unlock (Acceptor);

         if Acceptor.Pending_ATC_Level < Acceptor.ATC_Nesting_Level then
            Undefer_Abort (Self_ID);
            pragma Assert (Shutdown ("Continuing after being aborted!"));
         end if;

         P := Acceptor.Parent;
         Write_Lock (P);

         if P.Awake_Count /= 0 then
            P.Awake_Count := P.Awake_Count + 1;

         else
            Unlock (P);
            Utilities.Abort_To_Level (Acceptor, 0);
            Undefer_Abort (Self_ID);
            pragma Assert (Shutdown ("Continuing after being aborted!"));
         end if;

         --  Conservative checks which should only matter when an interrupt
         --  entry was chosen. In this case, the current task completes if
         --  the parent has already been signaled that all children have
         --  terminated.

         if Acceptor.Master_of_Task = P.Master_Within then
            if P.Awaited_Dependent_Count /= 0 then
               P.Awaited_Dependent_Count := P.Awaited_Dependent_Count + 1;

            elsif P.Stage = Await_Dependents then
               Unlock (P);
               Utilities.Abort_To_Level (Acceptor, 0);
               Undefer_Abort (Self_ID);
               pragma Assert (Shutdown ("Continuing after being aborted!"));
            end if;
         end if;

         Unlock (P);

      else
         Unlock (Acceptor);

         if Acceptor.Pending_ATC_Level < Acceptor.ATC_Nesting_Level then
            Undefer_Abort (Self_ID);
            pragma Assert (Shutdown ("Continuing after being aborted!"));
         end if;
      end if;

      Write_Lock (Acceptor);

   end Adjust_For_Terminate_Alternative_Call;

   ------------------------------------
   -- Setup_For_Rendezvous_With_Body --
   ------------------------------------

   procedure Setup_For_Rendezvous_With_Body
     (Entry_Call : Entry_Call_Link;
      Acceptor   : Task_ID)
   is
   begin
      Entry_Call.Acceptor_Prev_Call := Acceptor.Call;
      Acceptor.Call := Entry_Call;

      if Entry_Call.Abortable /= Never then
         Entry_Call.Abortable := False;
      end if;

      Boost_Priority (Entry_Call, Acceptor);
   end Setup_For_Rendezvous_With_Body;

   ----------------------
   -- Call_Synchronous --
   ----------------------

   procedure Call_Synchronous
     (Acceptor              : Task_ID;
      E                     : Task_Entry_Index;
      Uninterpreted_Data    : System.Address;
      Mode                  : Call_Modes;
      Rendezvous_Successful : out Boolean)
   is
      Caller     : constant Task_ID := Self;
      Level      : ATC_Level;
      Entry_Call : Entry_Call_Link;

   begin
      pragma Assert (Mode /= Asynchronous_Call
        or else Shutdown ("Asynchronous call being treated synchronously."));

      Defer_Abort (Caller);
      Caller.ATC_Nesting_Level := Caller.ATC_Nesting_Level + 1;
      Level := Caller.ATC_Nesting_Level;

      Entry_Call := Caller.Entry_Calls (Level)'Access;

      Entry_Call.Next := null;
      Entry_Call.Mode := Mode;

      --  If this is a call made inside of an abort deferred region,
      --  the call should be never abortable.

      if Caller.Deferral_Level > 1 then
         Entry_Call.Abortable := Never;
      else
         Entry_Call.Abortable := True;
      end if;

      Entry_Call.Done := False;
      Entry_Call.E := Entry_Index (E);
      Entry_Call.Prio := Get_Priority (Caller);
      Entry_Call.Uninterpreted_Data := Uninterpreted_Data;
      Entry_Call.Called_Task := Acceptor;
      Entry_Call.Exception_To_Raise := Ada.Exceptions.Null_Id;

      --  Note: the caller will undefer abortion on return (see WARNING above)

      Write_Lock (Acceptor);

      if Acceptor.Terminate_Alternative
        and then Is_Entry_Open (Acceptor, E)
      then
         Adjust_For_Terminate_Alternative_Call (Caller, Acceptor);
      end if;

      Do_Or_Queue (Caller, Entry_Call);
      Unlock (Acceptor);
      System.Tasking.Entry_Calls.Wait_For_Completion (Entry_Call);
      Rendezvous_Successful := Entry_Call.Done;
      Undefer_Abort (Caller);

      pragma Assert (
        Caller.Pending_ATC_Level >= Caller.ATC_Nesting_Level
        or else Shutdown ("Continuing after aborting self!"));

      Utilities.Check_Exception (Caller);
   end Call_Synchronous;

   -----------------
   -- Call_Simple --
   -----------------

   procedure Call_Simple
     (Acceptor           : Task_ID;
      E                  : Task_Entry_Index;
      Uninterpreted_Data : System.Address)
   is
      Rendezvous_Successful : Boolean;

   begin
      Call_Synchronous
        (Acceptor, E, Uninterpreted_Data, Simple_Call, Rendezvous_Successful);
   end Call_Simple;

   ----------------------------
   -- Cancel_Task_Entry_Call --
   ----------------------------

   procedure Cancel_Task_Entry_Call (Cancelled : out Boolean) is
      Caller : constant Task_ID := Self;
      Call   : Entry_Call_Link;

   begin
      pragma Assert (Caller.ATC_Nesting_Level > ATC_Level_Base'First
        or else Shutdown ("Attempt to cancel nonexistent task entry call"));

      Call := Caller.Entry_Calls (Caller.ATC_Nesting_Level)'Access;

      pragma Assert (Call.Mode = Asynchronous_Call
        or else Shutdown ("Attempt to do ATC on non-async task entry call"));

      pragma Assert (Call.Called_PO = Null_PO
        or else Shutdown
         ("Attempt to use Cancel_Task_Entry_Call on protected entry call."));

      Defer_Abort (Caller);

      Utilities.Abort_To_Level (Caller, Call.Level - 1);
      Entry_Calls.Wait_For_Completion (Call);

      --  Allow the triggered statements to be skipped

      Cancelled := not Call.Done;

      Undefer_Abort (Caller);
      Utilities.Check_Exception (Caller);
   end Cancel_Task_Entry_Call;

   ------------------------
   -- Requeue_Task_Entry --
   ------------------------

   procedure Requeue_Task_Entry
     (Acceptor   : Task_ID;
      E          : Task_Entry_Index;
      With_Abort : Boolean)
   is
      Old_Acceptor : constant Task_ID := Self;
      Caller       : Task_ID;
      Entry_Call   : Entry_Call_Link;

   begin
      Defer_Abort (Old_Acceptor);
      Write_Lock (Old_Acceptor);
      Entry_Call := Old_Acceptor.Call;
      Caller := Entry_Call.Self;

      Old_Acceptor.Call := Entry_Call.Acceptor_Prev_Call;

      --  Don't permit this call to be aborted until we have switched to
      --  the new acceptor.  Otherwise, we may queue a cancelled call below.

      if Entry_Call.Abortable /= Never then
         Entry_Call.Abortable := False;
      end if;

      Unlock (Old_Acceptor);

      Entry_Call.E := Entry_Index (E);

      Write_Lock (Acceptor);
      Entry_Call.Called_Task := Acceptor;

      if Entry_Call.Abortable /= Never then
         if With_Abort then
            Entry_Call.Abortable := True;
         else
            Entry_Call.Abortable := False;
         end if;
      end if;

      Entry_Call.Has_Been_Abortable :=
        With_Abort or Entry_Call.Has_Been_Abortable;
      Do_Or_Queue (Old_Acceptor, Entry_Call);
      Unlock (Acceptor);

      --  At this point, the caller is waiting for this call to
      --  be completed. We notify the call in following cases:
      --  1) If the call is done.
      --  2) If this is a conditional entry call, and has just become
      --     abortable, the caller should be awakened to cancel the call.
      --  3) When there is a pending action.

      if Entry_Call.Done
        or else (Entry_Call.Mode = Conditional_Call
                   and then Entry_Call.Abortable = True)
        or else Caller.Pending_Action
      then
         Write_Lock (Caller);
         Caller.Pending_Action := True;
         Wakeup (Caller);
         Unlock (Caller);
      end if;

      Undefer_Abort (Old_Acceptor);
   end Requeue_Task_Entry;

   -------------------------------------
   -- Requeue_Protected_To_Task_Entry --
   -------------------------------------

   procedure Requeue_Protected_To_Task_Entry
     (Object     : Protection_Access;
      Acceptor   : Task_ID;
      E          : Task_Entry_Index;
      With_Abort : Boolean)
   is
      Self_ID  : Task_ID := Self;
      Entry_Call : Entry_Call_Link := Object.Call_In_Progress;
      Caller     : Task_ID         := Entry_Call.Self;

   begin
      Defer_Abort (Self_ID);
      Entry_Call.E := Entry_Index (E);
      Object.Call_In_Progress := null;

      Write_Lock (Acceptor);
      Entry_Call.Called_Task := Acceptor;
      Entry_Call.Called_PO := Null_PO;

      if Entry_Call.Abortable /= Never then
         if With_Abort then
            Entry_Call.Abortable := True;
         else
            Entry_Call.Abortable := False;
         end if;
      end if;

      Entry_Call.Has_Been_Abortable :=
        With_Abort or Entry_Call.Has_Been_Abortable;
      Do_Or_Queue (Self_ID, Entry_Call);
      Unlock (Acceptor);

      Entry_Call.E := Entry_Index (E);

      --  At this point, the caller is waiting for this call to
      --  be completed. We notify the call in following cases:
      --  1) If the call is done.
      --  2) If this is a conditional entry call, and has just become
      --     abortable, the caller should be awakened to cancel the call.
      --  3) When there is a pending action.

      if Entry_Call.Done
        or else (Entry_Call.Mode = Conditional_Call
                   and then Entry_Call.Abortable = True)
        or else Caller.Pending_Action
      then
         Write_Lock (Caller);
         Caller.Pending_Action := True;
         Wakeup (Caller);
         Unlock (Caller);
      end if;

      Undefer_Abort (Self_ID);
   end Requeue_Protected_To_Task_Entry;

   ---------------------
   -- Task_Entry_Call --
   ---------------------

   procedure Task_Entry_Call
     (Acceptor              : Task_ID;
      E                     : Task_Entry_Index;
      Uninterpreted_Data    : System.Address;
      Mode                  : Call_Modes;
      Rendezvous_Successful : out Boolean)
   is
      Caller              : constant Task_ID := Self;
      Entry_Call          : Entry_Call_Link;
      Initially_Abortable : Boolean;

   begin
      --  Simple or conditional call

      if Mode = Simple_Call or else Mode = Conditional_Call then
         Call_Synchronous
           (Acceptor, E, Uninterpreted_Data, Mode, Rendezvous_Successful);

      --  Asynchronous call

      else
         --  Abortion must already be deferred by the compiler-generated
         --  code.  Without this, an abortion that occurs between the time
         --  that this call is made and the time that the abortable part's
         --  cleanup handler is set up might miss the cleanup handler and
         --  leave the call pending.

         Caller.ATC_Nesting_Level := Caller.ATC_Nesting_Level + 1;

         Entry_Call := Caller.Entry_Calls (Caller.ATC_Nesting_Level)'Access;

         Entry_Call.Next := null;
         Entry_Call.Mode := Mode;
         Entry_Call.Abortable := True;
         Entry_Call.Done := False;
         Entry_Call.E := Entry_Index (E);
         Entry_Call.Prio := Get_Priority (Caller);
         Entry_Call.Uninterpreted_Data := Uninterpreted_Data;
         Entry_Call.Called_Task := Acceptor;
         Entry_Call.Called_PO := Null_PO;
         Entry_Call.Exception_To_Raise := Ada.Exceptions.Null_Id;

         Write_Lock (Acceptor);

         if Acceptor.Terminate_Alternative
           and then Is_Entry_Open (Acceptor, E)
         then
            Adjust_For_Terminate_Alternative_Call (Caller, Acceptor);
         end if;

         Do_Or_Queue (Caller, Entry_Call);

         Initially_Abortable := Entry_Call.Abortable = True;

         Unlock (Acceptor);

         --  If the call was not queued abortably, we need to wait until
         --  it is before proceeding with the abortable part.
         --  Wait_Until_Abortable can be called unconditionally here,
         --  but it is expensive.

         if not Initially_Abortable then
            Entry_Calls.Wait_Until_Abortable (Caller, Entry_Call);
         end if;

         --  Note: following assignment needs to be atomic.

         Rendezvous_Successful := Entry_Call.Done;

      end if;
   end Task_Entry_Call;

   ---------------------------
   -- Timed_Task_Entry_Call --
   ---------------------------

   procedure Timed_Task_Entry_Call
     (Acceptor              : Task_ID;
      E                     : Task_Entry_Index;
      Uninterpreted_Data    : System.Address;
      Timeout               : access Time_Value;
      Rendezvous_Successful : out Boolean)
   is
      Caller     : constant Task_ID := Self;
      Level      : ATC_Level;
      Entry_Call : Entry_Call_Link;
      Cancelled  : Boolean := false;
      Timedout   : Boolean := False;

   begin
      Defer_Abort (Caller);
      Caller.ATC_Nesting_Level := Caller.ATC_Nesting_Level + 1;
      Level := Caller.ATC_Nesting_Level;

      Entry_Call := Caller.Entry_Calls (Level)'Access;

      Entry_Call.Next := null;
      Entry_Call.Mode := Timed_Call;

      --  If this is a call made inside of an abort deferred region,
      --  the call should be never abortable.

      if Caller.Deferral_Level > 1 then
         Entry_Call.Abortable := Never;
      else
         Entry_Call.Abortable := True;
      end if;

      Entry_Call.Done := False;
      Entry_Call.E := Entry_Index (E);
      Entry_Call.Prio := Get_Priority (Caller);
      Entry_Call.Uninterpreted_Data := Uninterpreted_Data;
      Entry_Call.Called_Task := Acceptor;
      Entry_Call.Exception_To_Raise := Ada.Exceptions.Null_Id;

      --  Note: the caller will undefer abortion on return (see WARNING above)

      Write_Lock (Acceptor);

      if Acceptor.Terminate_Alternative and then
        Is_Entry_Open (Acceptor, E) then
         Adjust_For_Terminate_Alternative_Call (Caller, Acceptor);
      end if;

      Do_Or_Queue (Caller, Entry_Call);
      Unlock (Acceptor);

      Entry_Calls.Wait_For_Completion_With_Timeout
        (Entry_Call, To_Duration (Timeout.all));

      Rendezvous_Successful := Entry_Call.Done;

      Undefer_Abort (Caller);
      pragma Assert (
        Caller.Pending_ATC_Level >= Caller.ATC_Nesting_Level
          or else
        Shutdown ("Continuing after aborting self!"));
      Utilities.Check_Exception (Caller);

   end Timed_Task_Entry_Call;

   -----------------
   -- Accept_Call --
   -----------------

   procedure Accept_Call
     (E                  : Task_Entry_Index;
      Uninterpreted_Data : out System.Address)
   is
      Acceptor     : constant Task_ID := Self;
      Caller       : Task_ID := null;
      Open_Accepts : aliased Accept_List (1 .. 1);
      Entry_Call   : Entry_Call_Link;

   begin
      Defer_Abort (Acceptor);
      Write_Lock (Acceptor);

      --  If someone completed this task, this task should not try to
      --  access its pending entry calls or queues in this case, as they
      --  are being emptied. Wait for abortion to kill us.

      if Acceptor.Stage >= Complete then
         Await_Abortion (Acceptor);
      end if;

      Queuing.Dequeue_Head (Acceptor.Entry_Queues (E), Entry_Call);

      if Entry_Call /= null then
         Caller := Entry_Call.Self;
         Setup_For_Rendezvous_With_Body (Entry_Call, Acceptor);
         Uninterpreted_Data := Entry_Call.Uninterpreted_Data;

      else
         --  Wait for a caller

         Open_Accepts (1).Null_Body := False;
         Open_Accepts (1).S := E;
         Acceptor.Open_Accepts := Open_Accepts'Unrestricted_Access;

         Acceptor.Accepting := Simple_Accept;

         --  Wait for normal call

         Wait_For_Call (Acceptor);

         if Acceptor.Pending_ATC_Level >= Acceptor.ATC_Nesting_Level then
            Caller := Acceptor.Call.Self;
            Uninterpreted_Data :=
              Caller.Entry_Calls (Caller.ATC_Nesting_Level).Uninterpreted_Data;
         end if;

         --  If this task has been aborted, skip the Uninterpreted_Data load
         --  (Caller will not be reliable) and fall through to
         --  Undefer_Abortion which will allow the task to be killed.
      end if;

      --  Acceptor.Call should already be updated by the Caller

      Unlock (Acceptor);
      Undefer_Abort (Acceptor);

      --  Start rendezvous

   end Accept_Call;

   --------------------
   -- Accept_Trivial --
   --------------------

   procedure Accept_Trivial (E : Task_Entry_Index) is
      Acceptor      : constant Task_ID := Self;
      Caller        : Task_ID := null;
      Open_Accepts  : aliased Accept_List (1 .. 1);
      Entry_Call    : Entry_Call_Link;
      Prev_Priority : Rendezvous_Priority := Priority_Not_Boosted;

   begin
      Defer_Abort (Acceptor);
      Write_Lock (Acceptor);

      --  If someone completed this task, this task should not try to
      --  access its pending entry calls or queues in this case, as they
      --  are being emptied. Wait for abortion to kill us.

      if Acceptor.Stage >= Complete then
         Await_Abortion (Acceptor);
      end if;

      Queuing.Dequeue_Head (Acceptor.Entry_Queues (E), Entry_Call);

      if Entry_Call = null then

         --  Need to wait for call

         Open_Accepts (1).Null_Body := False;
         Open_Accepts (1).S := E;
         Acceptor.Open_Accepts := Open_Accepts'Unrestricted_Access;

         Acceptor.Accepting := Trivial_Accept;

         --  Wait for normal entry call
         Wait_For_Call (Acceptor);

         if Acceptor.Pending_ATC_Level < Acceptor.ATC_Nesting_Level then
            Unlock (Acceptor);
            Undefer_Abort (Acceptor);
            pragma Assert (Shutdown ("Continuing after being aborted!"));

         else
            Entry_Call := Acceptor.Call;
            Acceptor.Call := Entry_Call.Acceptor_Prev_Call;
         end if;

      else
         --  No longer abortable

         Entry_Call.Abortable := False;
      end if;

      Unlock (Acceptor);
      Caller := Entry_Call.Self;
      Write_Lock (Caller);

      --  Done with mutex locked to make sure that signal is not lost.

      Entry_Call.Done := True;

      Prev_Priority := Entry_Call.Acceptor_Prev_Priority;

      if Entry_Call.Mode = Asynchronous_Call then
         Unlock (Caller);

         Utilities.Abort_To_Level (Caller, Entry_Call.Level - 1);
      else
         Wakeup (Caller);
         Unlock (Caller);
      end if;

      Utilities.Reset_Priority (Prev_Priority, Acceptor);

      Undefer_Abort (Acceptor);
   end Accept_Trivial;

   -------------------------------------
   -- Exceptional_Complete_Rendezvous --
   -------------------------------------

   procedure Exceptional_Complete_Rendezvous
     (Ex : Ada.Exceptions.Exception_ID)
   is
      Acceptor      : constant Task_ID := Self;
      Caller        : Task_ID;
      Call          : Entry_Call_Link;
      Prev_Priority : Rendezvous_Priority;

      use Ada.Exceptions;

   begin
      Defer_Abort (Acceptor);
      Call := Acceptor.Call;
      Acceptor.Call := Call.Acceptor_Prev_Call;
      Prev_Priority := Call.Acceptor_Prev_Priority;
      Call.Exception_To_Raise := Ex;
      Caller := Call.Self;

      --  Lock the Caller to ensure the caller will not miss
      --  the Wakeup call, in case it has not yet quite gone to sleep.

      Write_Lock (Caller);

      Acceptor.Exception_To_Raise := Ex;

      if Ex /= Ada.Exceptions.Null_Id then
         Duplex_Message : declare
            Len : Natural :=  Acceptor.Compiler_Data.Message_Length;
            type Thin_Ptr is access String (Positive);

            function To_Ptr is
              new Unchecked_Conversion (Address, Thin_Ptr);

         begin
            Caller.Compiler_Data.GNAT_Exception :=
              Acceptor.Compiler_Data.GNAT_Exception;

            Caller.Compiler_Data.Message_Length := Len;

            To_Ptr (Caller.Compiler_Data.Message_Addr) (1 .. Len) :=
              To_Ptr (Acceptor.Compiler_Data.Message_Addr) (1 .. Len);
         end Duplex_Message;
      end if;

      Call.Done := True;

      if Call.Mode = Asynchronous_Call then
         Unlock (Caller);
         Utilities.Abort_To_Level (Caller, Call.Level - 1);
      else
         Wakeup (Caller);
         Unlock (Caller);
      end if;

      Utilities.Reset_Priority (Prev_Priority, Acceptor);

      Undefer_Abort (Acceptor);
      Utilities.Check_Exception (Acceptor);
   end Exceptional_Complete_Rendezvous;

   -------------------------
   -- Complete_Rendezvous --
   -------------------------

   procedure Complete_Rendezvous is
   begin
      Exceptional_Complete_Rendezvous (Ada.Exceptions.Null_Id);
   end Complete_Rendezvous;

   --------------------
   -- Selective_Wait --
   --------------------

   procedure Selective_Wait
     (Open_Accepts       : Accept_List_Access;
      Select_Mode        : Select_Modes;
      Uninterpreted_Data : out System.Address;
      Index              : out Select_Index)
   is
      Acceptor         : constant Task_ID := Self;
      Treatment        : Select_Treatment;
      Entry_Call       : Entry_Call_Link;
      Caller           : Task_ID;
      Selection        : Select_Index;
      Open_Alternative : Boolean;

   begin
      Defer_Abort (Acceptor);
      Write_Lock (Acceptor);

      --  If someone completed this task, this task should not try to
      --  access its pending entry calls or queues in this case, as they
      --  are being emptied. Wait for abortion to kill us.

      if Acceptor.Stage >= Complete then
         Await_Abortion (Acceptor);
      end if;

      Queuing.Select_Task_Entry_Call
        (Acceptor, Open_Accepts, Entry_Call, Selection, Open_Alternative);

      --  Determine the kind and disposition of the select.

      Treatment := Default_Treatment (Select_Mode);
      Acceptor.Chosen_Index := No_Rendezvous;

      if Open_Alternative then
         if Entry_Call /= null then
            if Open_Accepts (Selection).Null_Body then
               Treatment := Accept_Alternative_Completed;
            else
               Setup_For_Rendezvous_With_Body (Entry_Call, Acceptor);
               Treatment := Accept_Alternative_Selected;
            end if;

            Acceptor.Chosen_Index := Selection;

         elsif Treatment = No_Alternative_Open then
            Treatment := Accept_Alternative_Open;
         end if;
      end if;

      --  Handle the select according to the disposition selected above.

      case Treatment is

      when Accept_Alternative_Selected =>

         --  Ready to rendezvous already

         Uninterpreted_Data := Acceptor.Call.Uninterpreted_Data;

         --  In this case the accept body is not Null_Body. Defer abortion
         --  until it gets into the accept body.
         Defer_Abort (Acceptor);
         Unlock (Acceptor);

      when Accept_Alternative_Completed =>

         --  Rendezvous is over

         Unlock (Acceptor);
         Caller := Entry_Call.Self;
         Write_Lock (Caller);
         Entry_Call.Done := True;

         if Entry_Call.Mode = Asynchronous_Call then
            Unlock (Caller);
            Utilities.Abort_To_Level (Caller, Entry_Call.Level - 1);
         else
            Wakeup (Caller);
            Unlock (Caller);
         end if;

      when Accept_Alternative_Open =>

         --  Wait for caller.

         Acceptor.Open_Accepts := Open_Accepts;

         Acceptor.Accepting := Select_Wait;

         Wait_For_Call (Acceptor);

         --  Acceptor.Call should already be updated by the Caller if
         --  not aborted. It might also be ready to do rendezvous even if
         --  this wakes up due to an abortion.
         --  Therefore, if the call is not empty we need to do the rendezvous
         --  if the accept body is not Null_Body.

         if Acceptor.Chosen_Index /= No_Rendezvous and then
           Acceptor.Call /= null and then
           not Open_Accepts (Acceptor.Chosen_Index).Null_Body
         then
            Uninterpreted_Data := Acceptor.Call.Uninterpreted_Data;
            Defer_Abort (Acceptor);
         end if;

         Unlock (Acceptor);

      when Else_Selected =>
         Acceptor.Accepting := Not_Accepting;
         Unlock (Acceptor);

      when Terminate_Selected =>

         --  Terminate alternative is open

         Acceptor.Open_Accepts := Open_Accepts;

         Acceptor.Accepting := Select_Wait;

         --  We need to check if a signal is pending on an open interrupt
         --  entry. Otherwise this task would become passive (since terminate
         --  alternative is open) and, if none of the siblings are active
         --  any more, the task could not wake up any more, even though a
         --  signal might be pending on an open interrupt entry.

         Unlock (Acceptor);
         Utilities.Terminate_Alternative (Acceptor);

         --  Wait for normal entry call or termination

         --  consider letting Terminate_Alternative assume mutex L
         --  is already locked, and return with it locked, so
         --  this code could be simplified???

         --  No return here if Acceptor completes, otherwise
         --  Acceptor.Call should already be updated by the Caller

         Write_Lock (Acceptor);

         Index := Acceptor.Chosen_Index;

         if Acceptor.Chosen_Index /= No_Rendezvous
           and then not Open_Accepts (Acceptor.Chosen_Index).Null_Body
         then
            Uninterpreted_Data := Acceptor.Call.Uninterpreted_Data;
            Defer_Abort (Acceptor);
         end if;

         Unlock (Acceptor);

         Undefer_Abort (Acceptor);
         return;

      when No_Alternative_Open =>

         --  In this case, Index will be No_Rendezvous on return, which
         --  should cause a Program_Error if it is not a Delay_Mode.

         --  If delay altenative exists (Delay_Mode) we should suspend
         --  until the delay expires.

         if Select_Mode = Delay_Mode then
            while not Acceptor.Pending_Action loop
               Sleep (Acceptor);
            end loop;

            Unlock (Acceptor);

         else
            Unlock (Acceptor);
            Undefer_Abort (Acceptor);
            raise Program_Error;
         end if;

      end case;

      --  Caller has been chosen

      --  Acceptor.Call should already be updated by the Caller

      --  Acceptor.Chosen_Index should either be updated by the Caller
      --  or by Test_Selective_Wait

      Index := Acceptor.Chosen_Index;
      Undefer_Abort (Acceptor);

      --  Start rendezvous, if not already completed

   end Selective_Wait;

   --------------------------
   -- Timed_Selective_Wait --
   --------------------------

   procedure Timed_Selective_Wait
     (Open_Accepts       : Accept_List_Access;
      Select_Mode        : Select_Modes;
      Uninterpreted_Data : out System.Address;
      Timeout            : access Time_Value;
      Index              : out Select_Index)
   is
      Acceptor         : constant Task_ID := Self;
      Treatment        : Select_Treatment;
      Entry_Call       : Entry_Call_Link;
      Caller           : Task_ID;
      Selection        : Select_Index;
      Open_Alternative : Boolean;
      Wakeup_Time      : Duration;
      Timedout         : Boolean := False;

   begin
      pragma Assert (Select_Mode = Delay_Mode or else
        Shutdown ("Non-delay selective accepts are handled differently"));

      Defer_Abort (Acceptor);
      Write_Lock (Acceptor);

      --  If someone completed this task, this task should not try to
      --  access its pending entry calls or queues in this case, as they
      --  are being emptied. Wait for abortion to kill us.

      if Acceptor.Stage >= Complete then
         Await_Abortion (Acceptor);
      end if;

      Queuing.Select_Task_Entry_Call
        (Acceptor, Open_Accepts, Entry_Call, Selection, Open_Alternative);

      --  Determine the kind and disposition of the select.

      Treatment := Default_Treatment (Select_Mode);
      Acceptor.Chosen_Index := No_Rendezvous;

      if Open_Alternative then
         if Entry_Call /= null then
            if Open_Accepts (Selection).Null_Body then
               Treatment := Accept_Alternative_Completed;
            else
               Setup_For_Rendezvous_With_Body (Entry_Call, Acceptor);
               Treatment := Accept_Alternative_Selected;
            end if;

            Acceptor.Chosen_Index := Selection;

         elsif Treatment = No_Alternative_Open then
            Treatment := Accept_Alternative_Open;
         end if;
      end if;

      --  Handle the select according to the disposition selected above.

      case Treatment is

      when Accept_Alternative_Selected =>

         --  Ready to rendezvous already

         Uninterpreted_Data := Acceptor.Call.Uninterpreted_Data;

         --  In this case the accept body is not Null_Body. Defer abortion
         --  until it gets into the accept body.

         Defer_Abort (Acceptor);
         Unlock (Acceptor);

      when Accept_Alternative_Completed =>

         --  Rendezvous is over

         Unlock (Acceptor);
         Caller := Entry_Call.Self;
         Write_Lock (Caller);
         Entry_Call.Done := True;

         if Entry_Call.Mode = Asynchronous_Call then
            Unlock (Caller);
            Utilities.Abort_To_Level (Caller, Entry_Call.Level - 1);
         else
            Wakeup (Caller);
            Unlock (Caller);
         end if;

      when Accept_Alternative_Open =>

         Wakeup_Time := To_Duration (Timeout.all);

         --  Wait for caller.

         Acceptor.Open_Accepts := Open_Accepts;

         Acceptor.Accepting := Select_Wait;

         --  Wait for a normal call and a pending action until the
         --  Wakeup_Time is reached.
         while Acceptor.Accepting /= Not_Accepting loop
            if Acceptor.Pending_Action then
               if Acceptor.Pending_Priority_Change then
                  Change_Base_Priority (Acceptor);
               end if;

               if Acceptor.Pending_ATC_Level < Acceptor.ATC_Nesting_Level then
                  Acceptor.Accepting := Not_Accepting;
                  exit;
               end if;

               Acceptor.Pending_Action := False;
            end if;

            --  Wait for a signal or timeout. A wakeup can be made
            --  for several reasons:
            --  1) An entry call is made.
            --  2) Pending_Aciton needs to be checked
            --     (Abortion, Priority change)
            --  3) Spurious wakeup
            --  4) Delay is expired

            Sleep_Until (Acceptor, Wakeup_Time, Timedout);

            if Timedout then
               exit;
            end if;
         end loop;

         --  Acceptor.Call should already be updated by the Caller if
         --  not aborted. It might also be ready to do rendezvous even if
         --  this wakes up due to an abortion.
         --  Therefore, if the call is not empty we need to do the rendezvous
         --  if the accept body is not Null_Body.

         if Acceptor.Chosen_Index /= No_Rendezvous
           and then Acceptor.Call /= null
           and then not Open_Accepts (Acceptor.Chosen_Index).Null_Body
         then
            Uninterpreted_Data := Acceptor.Call.Uninterpreted_Data;
            Defer_Abort (Acceptor);
         end if;

         Unlock (Acceptor);

      when No_Alternative_Open =>

         Wakeup_Time := To_Duration (Timeout.all);

         --  In this case, Index will be No_Rendezvous on return. We sleep
         --  for the time we need to.

         loop
            if Acceptor.Pending_Action then
               if Acceptor.Pending_Priority_Change then
                  Change_Base_Priority (Acceptor);
               end if;

               if Acceptor.Pending_ATC_Level < Acceptor.ATC_Nesting_Level then
                  Acceptor.Accepting := Not_Accepting;
                  exit;
               end if;

               Acceptor.Pending_Action := False;
            end if;

            --  Wait for a signal or timeout. A wakeup can be made
            --  for several reasons:
            --  1) Delay is expired
            --  2) Pending_Aciton needs to be checked
            --     (Abortion, Priority change)
            --  3) Spurious wakeup

            Sleep_Until (Acceptor, Wakeup_Time, Timedout);

            if Timedout then
               exit;
            end if;
         end loop;

         Unlock (Acceptor);

      when others =>
         pragma Assert
           (Shutdown ("Should not take this branch"));
         null;
      end case;

      --  Caller has been chosen

      --  Acceptor.Call should already be updated by the Caller

      --  Acceptor.Chosen_Index should either be updated by the Caller
      --  or by Test_Selective_Wait

      Index := Acceptor.Chosen_Index;
      Undefer_Abort (Acceptor);

      --  Start rendezvous, if not already completed

   end Timed_Selective_Wait;

   ----------------
   -- Task_Count --
   ----------------

   function Task_Count (E : Task_Entry_Index) return Natural is
      Self_ID : constant Task_ID := Self;
      Return_Count : Natural;

   begin
      Defer_Abort (Self_ID);
      Write_Lock (Self_ID);
      Return_Count := Queuing.Count_Waiting (Self_ID.Entry_Queues (E));
      Unlock (Self_ID);
      Undefer_Abort (Self_ID);
      return Return_Count;
   end Task_Count;

   --------------
   -- Callable --
   --------------

   function Callable (T : Task_ID) return Boolean is
   begin
      return T.Stage < Complete and then
        T.Pending_ATC_Level > ATC_Level_Base'First;
   end Callable;

   --------------------
   -- Await_Abortion --
   --------------------

   procedure Await_Abortion (Acceptor : Task_ID) is
   begin
      loop
         if Acceptor.Pending_Action then
            if Acceptor.Pending_Priority_Change then
               Change_Base_Priority (Acceptor);
            end if;

            exit when
               Acceptor.Pending_ATC_Level < Acceptor.ATC_Nesting_Level;
            Acceptor.Pending_Action := False;
         end if;

         Sleep (Acceptor);
      end loop;

      Unlock (Acceptor);
      Undefer_Abort (Acceptor);
      pragma Assert (Shutdown ("Continuing execution after being aborted"));
   end Await_Abortion;

   -------------------
   -- Is_Entry_Open --
   -------------------

   function Is_Entry_Open (T : Task_ID; E : Task_Entry_Index) return Boolean is
   begin
      for J in T.Open_Accepts'Range loop
         if E = T.Open_Accepts (J).S then
            return True;
         end if;
      end loop;

      return False;
   end Is_Entry_Open;

   -----------------------
   -- Task_Entry_Caller --
   -----------------------

   function Task_Entry_Caller (D : Task_Entry_Nesting_Depth) return Task_ID is
      Me         : constant Task_ID := Self;
      Entry_Call : Entry_Call_Link;

   begin
      Entry_Call := Me.Call;
      for Depth in 1 .. D loop
         Entry_Call := Entry_Call.Acceptor_Prev_Call;
         pragma Assert (Entry_Call /= null);
      end loop;

      return Entry_Call.Self;
   end Task_Entry_Caller;

   -------------------
   -- Wait_For_Call --
   -------------------

   procedure Wait_For_Call (Self_ID : Task_ID) is
   begin
      --  Wait for normal call and a pending action.

      while Self_ID.Accepting /= Not_Accepting loop
         if Self_ID.Pending_Action then
            if Self_ID.Pending_Priority_Change then
               Change_Base_Priority (Self_ID);
            end if;

            if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level then
               Self_ID.Accepting := Not_Accepting;
               exit;
            end if;

            Self_ID.Pending_Action := False;
         end if;

         Sleep (Self_ID);
      end loop;
   end Wait_For_Call;

end System.Tasking.Rendezvous;