1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . A S T _ H A N D L I N G --
10 -- Copyright (C) 1996-2002 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 -- This is the OpenVMS/Alpha version.
37 with System
; use System
;
41 with System
.Machine_Code
;
42 with System
.Storage_Elements
;
45 with System
.Tasking
.Rendezvous
;
46 with System
.Tasking
.Initialization
;
47 with System
.Tasking
.Utilities
;
49 with System
.Task_Primitives
;
50 with System
.Task_Primitives
.Operations
;
51 with System
.Task_Primitives
.Operations
.DEC
;
53 -- with Ada.Finalization;
54 -- removed, because of problem with controlled attribute ???
56 with Ada
.Task_Attributes
;
57 with Ada
.Task_Identification
;
59 with Ada
.Exceptions
; use Ada
.Exceptions
;
61 with Ada
.Unchecked_Conversion
;
63 package body System
.AST_Handling
is
65 package ATID
renames Ada
.Task_Identification
;
67 package ST
renames System
.Tasking
;
68 package STR
renames System
.Tasking
.Rendezvous
;
69 package STI
renames System
.Tasking
.Initialization
;
70 package STU
renames System
.Tasking
.Utilities
;
72 package SSE
renames System
.Storage_Elements
;
73 package STPO
renames System
.Task_Primitives
.Operations
;
74 package STPOD
renames System
.Task_Primitives
.Operations
.DEC
;
76 AST_Lock
: aliased System
.Task_Primitives
.RTS_Lock
;
77 -- This is a global lock; it is used to execute in mutual exclusion
78 -- from all other AST tasks. It is only used by Lock_AST and
81 procedure Lock_AST
(Self_ID
: ST
.Task_ID
);
82 -- Locks out other AST tasks. Preceding a section of code by Lock_AST and
83 -- following it by Unlock_AST creates a critical region.
85 procedure Unlock_AST
(Self_ID
: ST
.Task_ID
);
86 -- Releases lock previously set by call to Lock_AST.
87 -- All nested locks must be released before other tasks competing for the
88 -- tasking lock are released.
94 procedure Lock_AST
(Self_ID
: ST
.Task_ID
) is
96 STI
.Defer_Abort_Nestable
(Self_ID
);
97 STPO
.Write_Lock
(AST_Lock
'Access);
104 procedure Unlock_AST
(Self_ID
: ST
.Task_ID
) is
106 STPO
.Unlock
(AST_Lock
'Access);
107 STI
.Undefer_Abort_Nestable
(Self_ID
);
110 ---------------------------------
111 -- AST_Handler Data Structures --
112 ---------------------------------
114 -- As noted in the private part of the spec of System.Aux_DEC, the
115 -- AST_Handler type is simply a pointer to a procedure that takes
116 -- a single 64bit parameter. The following is a local copy
117 -- of that definition.
119 -- We need our own copy because we need to get our hands on this
120 -- and we cannot see the private part of System.Aux_DEC. We don't
121 -- want to be a child of Aux_Dec because of complications resulting
122 -- from the use of pragma Extend_System. We will use unchecked
123 -- conversions between the two versions of the declarations.
125 type AST_Handler
is access procedure (Param
: Long_Integer);
127 -- However, this declaration is somewhat misleading, since the values
128 -- referenced by AST_Handler values (all produced in this package by
129 -- calls to Create_AST_Handler) are highly stylized.
131 -- The first point is that in VMS/Alpha, procedure pointers do not in
132 -- fact point to code, but rather to a 48-byte procedure descriptor.
133 -- So a value of type AST_Handler is in fact a pointer to one of these
134 -- 48-byte descriptors.
136 type Descriptor_Type
is new SSE
.Storage_Array
(1 .. 48);
137 for Descriptor_Type
'Alignment use Standard
'Maximum_Alignment;
138 type Descriptor_Ref
is access all Descriptor_Type
;
140 -- Normally, there is only one such descriptor for a given procedure, but
141 -- it works fine to make a copy of the single allocated descriptor, and
142 -- use the copy itself, and we take advantage of this in the design here.
143 -- The idea is that AST_Handler values will all point to a record with the
144 -- following structure:
146 -- Note: When we say it works fine, there is one delicate point, which
147 -- is that the code for the AST procedure itself requires the original
148 -- descriptor address. We handle this by saving the orignal descriptor
149 -- address in this structure and restoring in Process_AST.
151 type AST_Handler_Data
is record
152 Descriptor
: Descriptor_Type
;
153 Original_Descriptor_Ref
: Descriptor_Ref
;
154 Taskid
: ATID
.Task_Id
;
158 type AST_Handler_Data_Ref
is access all AST_Handler_Data
;
160 function To_AST_Handler
is new Ada
.Unchecked_Conversion
161 (AST_Handler_Data_Ref
, System
.Aux_DEC
.AST_Handler
);
163 -- Each time Create_AST_Handler is called, a new value of this record
164 -- type is created, containing a copy of the procedure descriptor for
165 -- the routine used to handle all AST's (Process_AST), and the Task_Id
166 -- and entry number parameters identifying the task entry involved.
168 -- The AST_Handler value returned is a pointer to this record. Since
169 -- the record starts with the procedure descriptor, it can be used
170 -- by the system in the normal way to call the procedure. But now
171 -- when the procedure gets control, it can determine the address of
172 -- the procedure descriptor used to call it (since the ABI specifies
173 -- that this is left sitting in register r27 on entry), and then use
174 -- that address to retrieve the Task_Id and entry number so that it
175 -- knows on which entry to queue the AST request.
177 -- The next issue is where are these records placed. Since we intend
178 -- to pass pointers to these records to asynchronous system service
179 -- routines, they have to be on the heap, which means we have to worry
180 -- about when to allocate them and deallocate them.
182 -- We solve this problem by introducing a task attribute that points to
183 -- a vector, indexed by the entry number, of AST_Handler_Data records
184 -- for a given task. The pointer itself is a controlled object allowing
185 -- us to write a finalization routine that frees the referenced vector.
187 -- An entry in this vector is either initialized (Entryno non-zero) and
188 -- can be used for any subsequent reference to the same entry, or it is
189 -- unused, marked by the Entryno value being zero.
191 type AST_Handler_Vector
is array (Natural range <>) of AST_Handler_Data
;
192 type AST_Handler_Vector_Ref
is access all AST_Handler_Vector
;
194 -- type AST_Vector_Ptr is new Ada.Finalization.Controlled with record
195 -- removed due to problem with controlled attribute, consequence is that
196 -- we have a memory leak if a task that has AST attribute entries is
199 type AST_Vector_Ptr
is record
200 Vector
: AST_Handler_Vector_Ref
;
203 AST_Vector_Init
: AST_Vector_Ptr
;
204 -- Initial value, treated as constant, Vector will be null.
206 package AST_Attribute
is new Ada
.Task_Attributes
207 (Attribute
=> AST_Vector_Ptr
,
208 Initial_Value
=> AST_Vector_Init
);
212 -----------------------
213 -- AST Service Queue --
214 -----------------------
216 -- The following global data structures are used to queue pending
217 -- AST requests. When an AST is signalled, the AST service routine
218 -- Process_AST is called, and it makes an entry in this structure.
220 type AST_Instance
is record
221 Taskid
: ATID
.Task_Id
;
223 Param
: Long_Integer;
225 -- The Taskid and Entryno indicate the entry on which this AST is to
226 -- be queued, and Param is the parameter provided from the AST itself.
228 AST_Service_Queue_Size
: constant := 256;
229 AST_Service_Queue_Limit
: constant := 250;
230 type AST_Service_Queue_Index
is mod AST_Service_Queue_Size
;
231 -- Index used to refer to entries in the circular buffer which holds
232 -- active AST_Instance values. The upper bound reflects the maximum
233 -- number of AST instances that can be stored in the buffer. Since
234 -- these entries are immediately serviced by the high priority server
235 -- task that does the actual entry queuing, it is very unusual to have
236 -- any significant number of entries simulaneously queued.
238 AST_Service_Queue
: array (AST_Service_Queue_Index
) of AST_Instance
;
239 pragma Volatile_Components
(AST_Service_Queue
);
240 -- The circular buffer used to store active AST requests.
242 AST_Service_Queue_Put
: AST_Service_Queue_Index
:= 0;
243 AST_Service_Queue_Get
: AST_Service_Queue_Index
:= 0;
244 pragma Atomic
(AST_Service_Queue_Put
);
245 pragma Atomic
(AST_Service_Queue_Get
);
246 -- These two variables point to the next slots in the AST_Service_Queue
247 -- to be used for putting a new entry in and taking an entry out. This
248 -- is a circular buffer, so these pointers wrap around. If the two values
249 -- are equal the buffer is currently empty. The pointers are atomic to
250 -- ensure proper synchronization between the single producer (namely the
251 -- Process_AST procedure), and the single consumer (the AST_Service_Task).
253 --------------------------------
254 -- AST Server Task Structures --
255 --------------------------------
257 -- The basic approach is that when an AST comes in, a call is made to
258 -- the Process_AST procedure. It queues the request in the service queue
259 -- and then wakes up an AST server task to perform the actual call to the
260 -- required entry. We use this intermediate server task, since the AST
261 -- procedure itself cannot wait to return, and we need some caller for
262 -- the rendezvous so that we can use the normal rendezvous mechanism.
264 -- It would work to have only one AST server task, but then we would lose
265 -- all overlap in AST processing, and furthermore, we could get priority
266 -- inversion effects resulting in starvation of AST requests.
268 -- We therefore maintain a small pool of AST server tasks. We adjust
269 -- the size of the pool dynamically to reflect traffic, so that we have
270 -- a sufficient number of server tasks to avoid starvation.
272 Max_AST_Servers
: constant Natural := 16;
273 -- Maximum number of AST server tasks that can be allocated
275 Num_AST_Servers
: Natural := 0;
276 -- Number of AST server tasks currently active
278 Num_Waiting_AST_Servers
: Natural := 0;
279 -- This is the number of AST server tasks that are either waiting for
280 -- work, or just about to go to sleep and wait for work.
282 Is_Waiting
: array (1 .. Max_AST_Servers
) of Boolean := (others => False);
283 -- An array of flags showing which AST server tasks are currently waiting
285 AST_Task_Ids
: array (1 .. Max_AST_Servers
) of ST
.Task_ID
;
286 -- Task Id's of allocated AST server tasks
288 task type AST_Server_Task
(Num
: Natural) is
289 pragma Priority
(Priority
'Last);
291 -- Declaration for AST server task. This task has no entries, it is
292 -- controlled by sleep and wakeup calls at the task primitives level.
294 type AST_Server_Task_Ptr
is access all AST_Server_Task
;
295 -- Type used to allocate server tasks
297 -----------------------
298 -- Local Subprograms --
299 -----------------------
301 procedure Allocate_New_AST_Server
;
302 -- Allocate an additional AST server task
304 procedure Process_AST
(Param
: Long_Integer);
305 -- This is the central routine for processing all AST's, it is referenced
306 -- as the code address of all created AST_Handler values. See detailed
307 -- description in body to understand how it works to have a single such
308 -- procedure for all AST's even though it does not get any indication of
309 -- the entry involved passed as an explicit parameter. The single explicit
310 -- parameter Param is the parameter passed by the system with the AST.
312 -----------------------------
313 -- Allocate_New_AST_Server --
314 -----------------------------
316 procedure Allocate_New_AST_Server
is
317 Dummy
: AST_Server_Task_Ptr
;
320 if Num_AST_Servers
= Max_AST_Servers
then
324 -- Note: it is safe to increment Num_AST_Servers immediately, since
325 -- no one will try to activate this task until it indicates that it
326 -- is sleeping by setting its entry in Is_Waiting to True.
328 Num_AST_Servers
:= Num_AST_Servers
+ 1;
329 Dummy
:= new AST_Server_Task
(Num_AST_Servers
);
331 end Allocate_New_AST_Server
;
333 ---------------------
334 -- AST_Server_Task --
335 ---------------------
337 task body AST_Server_Task
is
338 Taskid
: ATID
.Task_Id
;
340 Param
: aliased Long_Integer;
341 Self_Id
: constant ST
.Task_ID
:= ST
.Self
;
343 pragma Volatile
(Param
);
346 -- By making this task independent of master, when the environment
347 -- task is finalizing, the AST_Server_Task will be notified that it
350 STU
.Make_Independent
;
352 -- Record our task Id for access by Process_AST
354 AST_Task_Ids
(Num
) := Self_Id
;
356 -- Note: this entire task operates with the main task lock set, except
357 -- when it is sleeping waiting for work, or busy doing a rendezvous
358 -- with an AST server. This lock protects the data structures that
359 -- are shared by multiple instances of the server task.
363 -- This is the main infinite loop of the task. We go to sleep and
364 -- wait to be woken up by Process_AST when there is some work to do.
367 Num_Waiting_AST_Servers
:= Num_Waiting_AST_Servers
+ 1;
369 Unlock_AST
(Self_Id
);
371 STI
.Defer_Abort
(Self_Id
);
372 STPO
.Write_Lock
(Self_Id
);
374 Is_Waiting
(Num
) := True;
376 Self_Id
.Common
.State
:= ST
.AST_Server_Sleep
;
377 STPO
.Sleep
(Self_Id
, ST
.AST_Server_Sleep
);
378 Self_Id
.Common
.State
:= ST
.Runnable
;
380 STPO
.Unlock
(Self_Id
);
382 -- If the process is finalizing, Undefer_Abort will simply end
385 STI
.Undefer_Abort
(Self_Id
);
387 -- We are awake, there is something to do!
390 Num_Waiting_AST_Servers
:= Num_Waiting_AST_Servers
- 1;
392 -- Loop here to service outstanding requests. We are always
393 -- locked on entry to this loop.
395 while AST_Service_Queue_Get
/= AST_Service_Queue_Put
loop
396 Taskid
:= AST_Service_Queue
(AST_Service_Queue_Get
).Taskid
;
397 Entryno
:= AST_Service_Queue
(AST_Service_Queue_Get
).Entryno
;
398 Param
:= AST_Service_Queue
(AST_Service_Queue_Get
).Param
;
400 AST_Service_Queue_Get
:= AST_Service_Queue_Get
+ 1;
402 -- This is a manual expansion of the normal call simple code
405 type AA
is access all Long_Integer;
406 P
: AA
:= Param
'Unrestricted_Access;
408 function To_ST_Task_Id
is new Ada
.Unchecked_Conversion
409 (ATID
.Task_Id
, ST
.Task_ID
);
412 Unlock_AST
(Self_Id
);
414 (Acceptor
=> To_ST_Task_Id
(Taskid
),
415 E
=> ST
.Task_Entry_Index
(Entryno
),
416 Uninterpreted_Data
=> P
'Address);
419 System
.IO
.Put_Line
("%Debugging event");
420 System
.IO
.Put_Line
(Exception_Name
(E
) &
421 " raised when trying to deliver an AST.");
422 if Exception_Message
(E
)'Length /= 0 then
423 System
.IO
.Put_Line
(Exception_Message
(E
));
425 System
.IO
.Put_Line
("Task type is " & "Receiver_Type");
426 System
.IO
.Put_Line
("Task id is " & ATID
.Image
(Taskid
));
434 ------------------------
435 -- Create_AST_Handler --
436 ------------------------
438 function Create_AST_Handler
439 (Taskid
: ATID
.Task_Id
;
441 return System
.Aux_DEC
.AST_Handler
443 Attr_Ref
: Attribute_Handle
;
445 Process_AST_Ptr
: constant AST_Handler
:= Process_AST
'Access;
446 -- Reference to standard procedure descriptor for Process_AST
448 function To_Descriptor_Ref
is new Ada
.Unchecked_Conversion
449 (AST_Handler
, Descriptor_Ref
);
451 Original_Descriptor_Ref
: Descriptor_Ref
:=
452 To_Descriptor_Ref
(Process_AST_Ptr
);
455 if ATID
.Is_Terminated
(Taskid
) then
459 Attr_Ref
:= Reference
(Taskid
);
461 -- Allocate another server if supply is getting low
463 if Num_Waiting_AST_Servers
< 2 then
464 Allocate_New_AST_Server
;
467 -- No point in creating more if we have zillions waiting to
470 while AST_Service_Queue_Put
- AST_Service_Queue_Get
471 > AST_Service_Queue_Limit
476 -- If no AST vector allocated, or the one we have is too short, then
477 -- allocate one of right size and initialize all entries except the
478 -- one we will use to unused. Note that the assignment automatically
479 -- frees the old allocated table if there is one.
481 if Attr_Ref
.Vector
= null
482 or else Attr_Ref
.Vector
'Length < Entryno
484 Attr_Ref
.Vector
:= new AST_Handler_Vector
(1 .. Entryno
);
486 for E
in 1 .. Entryno
loop
487 Attr_Ref
.Vector
(E
).Descriptor
:=
488 Original_Descriptor_Ref
.all;
489 Attr_Ref
.Vector
(E
).Original_Descriptor_Ref
:=
490 Original_Descriptor_Ref
;
491 Attr_Ref
.Vector
(E
).Taskid
:= Taskid
;
492 Attr_Ref
.Vector
(E
).Entryno
:= E
;
496 return To_AST_Handler
(Attr_Ref
.Vector
(Entryno
)'Unrestricted_Access);
497 end Create_AST_Handler
;
499 ----------------------------
500 -- Expand_AST_Packet_Pool --
501 ----------------------------
503 procedure Expand_AST_Packet_Pool
504 (Requested_Packets
: in Natural;
505 Actual_Number
: out Natural;
506 Total_Number
: out Natural)
509 -- The AST implementation of GNAT does not permit dynamic expansion
510 -- of the pool, so we simply add no entries and return the total. If
511 -- it is necessary to expand the allocation, then this package body
512 -- must be recompiled with a larger value for AST_Service_Queue_Size.
515 Total_Number
:= AST_Service_Queue_Size
;
516 end Expand_AST_Packet_Pool
;
522 procedure Process_AST
(Param
: Long_Integer) is
524 Handler_Data_Ptr
: AST_Handler_Data_Ref
;
525 -- This variable is set to the address of the descriptor through
526 -- which Process_AST is called. Since the descriptor is part of
527 -- an AST_Handler value, this is also the address of this value,
528 -- from which we can obtain the task and entry number information.
530 function To_Address
is new Ada
.Unchecked_Conversion
531 (ST
.Task_ID
, System
.Address
);
534 System
.Machine_Code
.Asm
535 (Template
=> "addl $27,0,%0",
536 Outputs
=> AST_Handler_Data_Ref
'Asm_Output ("=r", Handler_Data_Ptr
),
539 System
.Machine_Code
.Asm
540 (Template
=> "ldl $27,%0",
541 Inputs
=> Descriptor_Ref
'Asm_Input
542 ("m", Handler_Data_Ptr
.Original_Descriptor_Ref
),
545 AST_Service_Queue
(AST_Service_Queue_Put
) := AST_Instance
'
546 (Taskid => Handler_Data_Ptr.Taskid,
547 Entryno => Handler_Data_Ptr.Entryno,
550 -- ??? What is the protection of this variable ?
551 -- It seems that trying to use any lock in this procedure will get
554 AST_Service_Queue_Put := AST_Service_Queue_Put + 1;
556 -- Need to wake up processing task. If there is no waiting server
557 -- then we have temporarily run out, but things should still be
558 -- OK, since one of the active ones will eventually pick up the
559 -- service request queued in the AST_Service_Queue.
561 for J in 1 .. Num_AST_Servers loop
562 if Is_Waiting (J) then
563 Is_Waiting (J) := False;
565 -- Sleeps are handled by ASTs on VMS, so don't call Wakeup.
566 -- ??? We should lock AST_Task_Ids (J) here. What's the story ?
568 STPOD.Interrupt_AST_Handler
569 (To_Address (AST_Task_Ids (J)));
576 STPO.Initialize_Lock (AST_Lock'Access, STPO.Global_Task_Level);
577 end System.AST_Handling;