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
|
------------------------------------------------------------------------------
-- --
-- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
-- --
-- S Y S T E M . T A S K I N G . Q U E U I N G --
-- --
-- B o d y --
-- --
-- $Revision$
-- --
-- Copyright (C) 1992-2002, Free Software Foundation, Inc. --
-- --
-- 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. (http://www.gnat.com). --
-- --
------------------------------------------------------------------------------
-- This version of the body implements queueing policy according to the
-- policy specified by the pragma Queuing_Policy. When no such pragma
-- is specified FIFO policy is used as default.
with System.Task_Primitives.Operations;
-- used for Write_Lock
-- Unlock
with System.Tasking.Initialization;
-- used for Wakeup_Entry_Caller
with System.Parameters;
-- used for Single_Lock
package body System.Tasking.Queuing is
use Parameters;
use Task_Primitives.Operations;
use Protected_Objects;
use Protected_Objects.Entries;
-- Entry Queues implemented as doubly linked list.
Queuing_Policy : Character;
pragma Import (C, Queuing_Policy, "__gl_queuing_policy");
Priority_Queuing : constant Boolean := Queuing_Policy = 'P';
procedure Send_Program_Error
(Self_ID : Task_ID;
Entry_Call : Entry_Call_Link);
-- Raise Program_Error in the caller of the specified entry call
function Check_Queue (E : Entry_Queue) return Boolean;
-- Check the validity of E.
-- Return True if E is valid, raise Assert_Failure if assertions are
-- enabled and False otherwise.
-----------------------------
-- Broadcast_Program_Error --
-----------------------------
procedure Broadcast_Program_Error
(Self_ID : Task_ID;
Object : Protection_Entries_Access;
Pending_Call : Entry_Call_Link;
RTS_Locked : Boolean := False)
is
Entry_Call : Entry_Call_Link;
begin
if Single_Lock and then not RTS_Locked then
Lock_RTS;
end if;
if Pending_Call /= null then
Send_Program_Error (Self_ID, Pending_Call);
end if;
for E in Object.Entry_Queues'Range loop
Dequeue_Head (Object.Entry_Queues (E), Entry_Call);
while Entry_Call /= null loop
pragma Assert (Entry_Call.Mode /= Conditional_Call);
Send_Program_Error (Self_ID, Entry_Call);
Dequeue_Head (Object.Entry_Queues (E), Entry_Call);
end loop;
end loop;
if Single_Lock and then not RTS_Locked then
Unlock_RTS;
end if;
end Broadcast_Program_Error;
-----------------
-- Check_Queue --
-----------------
function Check_Queue (E : Entry_Queue) return Boolean is
Valid : Boolean := True;
C, Prev : Entry_Call_Link;
begin
if E.Head = null then
if E.Tail /= null then
Valid := False;
pragma Assert (Valid);
end if;
else
if E.Tail = null
or else E.Tail.Next /= E.Head
then
Valid := False;
pragma Assert (Valid);
else
C := E.Head;
loop
Prev := C;
C := C.Next;
if C = null then
Valid := False;
pragma Assert (Valid);
exit;
end if;
if Prev /= C.Prev then
Valid := False;
pragma Assert (Valid);
exit;
end if;
exit when C = E.Head;
end loop;
if Prev /= E.Tail then
Valid := False;
pragma Assert (Valid);
end if;
end if;
end if;
return Valid;
end Check_Queue;
-------------------
-- Count_Waiting --
-------------------
-- Return number of calls on the waiting queue of E
function Count_Waiting (E : in Entry_Queue) return Natural is
Count : Natural;
Temp : Entry_Call_Link;
begin
pragma Assert (Check_Queue (E));
Count := 0;
if E.Head /= null then
Temp := E.Head;
loop
Count := Count + 1;
exit when E.Tail = Temp;
Temp := Temp.Next;
end loop;
end if;
return Count;
end Count_Waiting;
-------------
-- Dequeue --
-------------
-- Dequeue call from entry_queue E
procedure Dequeue (E : in out Entry_Queue; Call : Entry_Call_Link) is
begin
pragma Assert (Check_Queue (E));
pragma Assert (Call /= null);
-- If empty queue, simply return
if E.Head = null then
return;
end if;
pragma Assert (Call.Prev /= null);
pragma Assert (Call.Next /= null);
Call.Prev.Next := Call.Next;
Call.Next.Prev := Call.Prev;
if E.Head = Call then
-- Case of one element
if E.Tail = Call then
E.Head := null;
E.Tail := null;
-- More than one element
else
E.Head := Call.Next;
end if;
elsif E.Tail = Call then
E.Tail := Call.Prev;
end if;
-- Successfully dequeued
Call.Prev := null;
Call.Next := null;
pragma Assert (Check_Queue (E));
end Dequeue;
------------------
-- Dequeue_Call --
------------------
procedure Dequeue_Call (Entry_Call : Entry_Call_Link) is
Called_PO : Protection_Entries_Access;
begin
pragma Assert (Entry_Call /= null);
if Entry_Call.Called_Task /= null then
Dequeue
(Entry_Call.Called_Task.Entry_Queues
(Task_Entry_Index (Entry_Call.E)),
Entry_Call);
else
Called_PO := To_Protection (Entry_Call.Called_PO);
Dequeue (Called_PO.Entry_Queues
(Protected_Entry_Index (Entry_Call.E)),
Entry_Call);
end if;
end Dequeue_Call;
------------------
-- Dequeue_Head --
------------------
-- Remove and return the head of entry_queue E
procedure Dequeue_Head
(E : in out Entry_Queue;
Call : out Entry_Call_Link)
is
Temp : Entry_Call_Link;
begin
pragma Assert (Check_Queue (E));
-- If empty queue, return null pointer
if E.Head = null then
Call := null;
return;
end if;
Temp := E.Head;
-- Case of one element
if E.Head = E.Tail then
E.Head := null;
E.Tail := null;
-- More than one element
else
pragma Assert (Temp /= null);
pragma Assert (Temp.Next /= null);
pragma Assert (Temp.Prev /= null);
E.Head := Temp.Next;
Temp.Prev.Next := Temp.Next;
Temp.Next.Prev := Temp.Prev;
end if;
-- Successfully dequeued
Temp.Prev := null;
Temp.Next := null;
Call := Temp;
pragma Assert (Check_Queue (E));
end Dequeue_Head;
-------------
-- Enqueue --
-------------
-- Enqueue call at the end of entry_queue E, for FIFO queuing policy.
-- Enqueue call priority ordered, FIFO at same priority level, for
-- Priority queuing policy.
procedure Enqueue (E : in out Entry_Queue; Call : Entry_Call_Link) is
Temp : Entry_Call_Link := E.Head;
begin
pragma Assert (Check_Queue (E));
pragma Assert (Call /= null);
-- Priority Queuing
if Priority_Queuing then
if Temp = null then
Call.Prev := Call;
Call.Next := Call;
E.Head := Call;
E.Tail := Call;
else
loop
-- Find the entry that the new guy should precede
exit when Call.Prio > Temp.Prio;
Temp := Temp.Next;
if Temp = E.Head then
Temp := null;
exit;
end if;
end loop;
if Temp = null then
-- Insert at tail
Call.Prev := E.Tail;
Call.Next := E.Head;
E.Tail := Call;
else
Call.Prev := Temp.Prev;
Call.Next := Temp;
-- Insert at head
if Temp = E.Head then
E.Head := Call;
end if;
end if;
pragma Assert (Call.Prev /= null);
pragma Assert (Call.Next /= null);
Call.Prev.Next := Call;
Call.Next.Prev := Call;
end if;
pragma Assert (Check_Queue (E));
return;
end if;
-- FIFO Queuing
if E.Head = null then
E.Head := Call;
else
E.Tail.Next := Call;
Call.Prev := E.Tail;
end if;
E.Head.Prev := Call;
E.Tail := Call;
Call.Next := E.Head;
pragma Assert (Check_Queue (E));
end Enqueue;
------------------
-- Enqueue_Call --
------------------
procedure Enqueue_Call (Entry_Call : Entry_Call_Link) is
Called_PO : Protection_Entries_Access;
begin
pragma Assert (Entry_Call /= null);
if Entry_Call.Called_Task /= null then
Enqueue
(Entry_Call.Called_Task.Entry_Queues
(Task_Entry_Index (Entry_Call.E)),
Entry_Call);
else
Called_PO := To_Protection (Entry_Call.Called_PO);
Enqueue (Called_PO.Entry_Queues
(Protected_Entry_Index (Entry_Call.E)),
Entry_Call);
end if;
end Enqueue_Call;
----------
-- Head --
----------
-- Return the head of entry_queue E
function Head (E : in Entry_Queue) return Entry_Call_Link is
begin
pragma Assert (Check_Queue (E));
return E.Head;
end Head;
-------------
-- Onqueue --
-------------
-- Return True if Call is on any entry_queue at all
function Onqueue (Call : Entry_Call_Link) return Boolean is
begin
pragma Assert (Call /= null);
-- Utilize the fact that every queue is circular, so if Call
-- is on any queue at all, Call.Next must NOT be null.
return Call.Next /= null;
end Onqueue;
--------------------------------
-- Requeue_Call_With_New_Prio --
--------------------------------
procedure Requeue_Call_With_New_Prio
(Entry_Call : Entry_Call_Link; Prio : System.Any_Priority) is
begin
pragma Assert (Entry_Call /= null);
-- Perform a queue reordering only when the policy being used is the
-- Priority Queuing.
if Priority_Queuing then
if Onqueue (Entry_Call) then
Dequeue_Call (Entry_Call);
Entry_Call.Prio := Prio;
Enqueue_Call (Entry_Call);
end if;
end if;
end Requeue_Call_With_New_Prio;
---------------------------------
-- Select_Protected_Entry_Call --
---------------------------------
-- Select an entry of a protected object. Selection depends on the
-- queuing policy being used.
procedure Select_Protected_Entry_Call
(Self_ID : Task_ID;
Object : Protection_Entries_Access;
Call : out Entry_Call_Link)
is
Entry_Call : Entry_Call_Link;
Temp_Call : Entry_Call_Link;
Entry_Index : Protected_Entry_Index := Null_Entry; -- stop warning
-- ??? should add comment as to why Entry_Index is always initialized
begin
Entry_Call := null;
begin
if Priority_Queuing then
-- Priority queuing
for J in Object.Entry_Queues'Range loop
Temp_Call := Head (Object.Entry_Queues (J));
if Temp_Call /= null
and then
Object.Entry_Bodies
(Object.Find_Body_Index
(Object.Compiler_Info, J)).
Barrier (Object.Compiler_Info, J)
then
if (Entry_Call = null or else
Entry_Call.Prio < Temp_Call.Prio)
then
Entry_Call := Temp_Call;
Entry_Index := J;
end if;
end if;
end loop;
else
-- FIFO queuing
for J in Object.Entry_Queues'Range loop
Temp_Call := Head (Object.Entry_Queues (J));
if Temp_Call /= null
and then
Object.Entry_Bodies
(Object.Find_Body_Index
(Object.Compiler_Info, J)).
Barrier (Object.Compiler_Info, J)
then
Entry_Call := Temp_Call;
Entry_Index := J;
exit;
end if;
end loop;
end if;
exception
when others =>
Broadcast_Program_Error (Self_ID, Object, null);
end;
-- If a call was selected, dequeue it and return it for service.
if Entry_Call /= null then
Temp_Call := Entry_Call;
Dequeue_Head (Object.Entry_Queues (Entry_Index), Entry_Call);
pragma Assert (Temp_Call = Entry_Call);
end if;
Call := Entry_Call;
end Select_Protected_Entry_Call;
----------------------------
-- Select_Task_Entry_Call --
----------------------------
-- Select an entry for rendezvous. Selection depends on the queuing policy
-- being used.
procedure Select_Task_Entry_Call
(Acceptor : Task_ID;
Open_Accepts : Accept_List_Access;
Call : out Entry_Call_Link;
Selection : out Select_Index;
Open_Alternative : out Boolean)
is
Entry_Call : Entry_Call_Link;
Temp_Call : Entry_Call_Link;
Entry_Index : Task_Entry_Index := Task_Entry_Index'First;
Temp_Entry : Task_Entry_Index;
begin
Open_Alternative := False;
Entry_Call := null;
Selection := No_Rendezvous;
if Priority_Queuing then
-- Priority queueing case
for J in Open_Accepts'Range loop
Temp_Entry := Open_Accepts (J).S;
if Temp_Entry /= Null_Task_Entry then
Open_Alternative := True;
Temp_Call := Head (Acceptor.Entry_Queues (Temp_Entry));
if Temp_Call /= null
and then (Entry_Call = null
or else Entry_Call.Prio < Temp_Call.Prio)
then
Entry_Call := Head (Acceptor.Entry_Queues (Temp_Entry));
Entry_Index := Temp_Entry;
Selection := J;
end if;
end if;
end loop;
else
-- FIFO Queuing case
for J in Open_Accepts'Range loop
Temp_Entry := Open_Accepts (J).S;
if Temp_Entry /= Null_Task_Entry then
Open_Alternative := True;
Temp_Call := Head (Acceptor.Entry_Queues (Temp_Entry));
if Temp_Call /= null then
Entry_Call := Head (Acceptor.Entry_Queues (Temp_Entry));
Entry_Index := Temp_Entry;
Selection := J;
exit;
end if;
end if;
end loop;
end if;
if Entry_Call /= null then
Dequeue_Head (Acceptor.Entry_Queues (Entry_Index), Entry_Call);
-- Guard is open
end if;
Call := Entry_Call;
end Select_Task_Entry_Call;
------------------------
-- Send_Program_Error --
------------------------
procedure Send_Program_Error
(Self_ID : Task_ID;
Entry_Call : Entry_Call_Link)
is
Caller : Task_ID;
begin
Caller := Entry_Call.Self;
Entry_Call.Exception_To_Raise := Program_Error'Identity;
Write_Lock (Caller);
Initialization.Wakeup_Entry_Caller (Self_ID, Entry_Call, Done);
Unlock (Caller);
end Send_Program_Error;
end System.Tasking.Queuing;
|