Daily bump.
[official-gcc.git] / gcc / ada / s-asthan-vms-alpha.adb
blob2e04081f41080034f7ad76fce95ac20397672427
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . A S T _ H A N D L I N G --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1996-2009, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 -- This is the OpenVMS/Alpha version
34 with System; use System;
36 with System.IO;
38 with System.Machine_Code;
39 with System.Parameters;
40 with System.Storage_Elements;
42 with System.Tasking;
43 with System.Tasking.Rendezvous;
44 with System.Tasking.Initialization;
45 with System.Tasking.Utilities;
47 with System.Task_Primitives;
48 with System.Task_Primitives.Operations;
49 with System.Task_Primitives.Operations.DEC;
51 -- with Ada.Finalization;
52 -- removed, because of problem with controlled attribute ???
54 with Ada.Task_Attributes;
56 with Ada.Exceptions; use Ada.Exceptions;
58 with Ada.Unchecked_Conversion;
60 package body System.AST_Handling is
62 package ATID renames Ada.Task_Identification;
64 package SP renames System.Parameters;
65 package ST renames System.Tasking;
66 package STR renames System.Tasking.Rendezvous;
67 package STI renames System.Tasking.Initialization;
68 package STU renames System.Tasking.Utilities;
70 package SSE renames System.Storage_Elements;
71 package STPO renames System.Task_Primitives.Operations;
72 package STPOD renames System.Task_Primitives.Operations.DEC;
74 AST_Lock : aliased System.Task_Primitives.RTS_Lock;
75 -- This is a global lock; it is used to execute in mutual exclusion
76 -- from all other AST tasks. It is only used by Lock_AST and
77 -- Unlock_AST.
79 procedure Lock_AST (Self_ID : ST.Task_Id);
80 -- Locks out other AST tasks. Preceding a section of code by Lock_AST and
81 -- following it by Unlock_AST creates a critical region.
83 procedure Unlock_AST (Self_ID : ST.Task_Id);
84 -- Releases lock previously set by call to Lock_AST.
85 -- All nested locks must be released before other tasks competing for the
86 -- tasking lock are released.
88 --------------
89 -- Lock_AST --
90 --------------
92 procedure Lock_AST (Self_ID : ST.Task_Id) is
93 begin
94 STI.Defer_Abort_Nestable (Self_ID);
95 STPO.Write_Lock (AST_Lock'Access, Global_Lock => True);
96 end Lock_AST;
98 ----------------
99 -- Unlock_AST --
100 ----------------
102 procedure Unlock_AST (Self_ID : ST.Task_Id) is
103 begin
104 STPO.Unlock (AST_Lock'Access, Global_Lock => True);
105 STI.Undefer_Abort_Nestable (Self_ID);
106 end Unlock_AST;
108 ---------------------------------
109 -- AST_Handler Data Structures --
110 ---------------------------------
112 -- As noted in the private part of the spec of System.Aux_DEC, the
113 -- AST_Handler type is simply a pointer to a procedure that takes
114 -- a single 64bit parameter. The following is a local copy
115 -- of that definition.
117 -- We need our own copy because we need to get our hands on this
118 -- and we cannot see the private part of System.Aux_DEC. We don't
119 -- want to be a child of Aux_Dec because of complications resulting
120 -- from the use of pragma Extend_System. We will use unchecked
121 -- conversions between the two versions of the declarations.
123 type AST_Handler is access procedure (Param : Long_Integer);
125 -- However, this declaration is somewhat misleading, since the values
126 -- referenced by AST_Handler values (all produced in this package by
127 -- calls to Create_AST_Handler) are highly stylized.
129 -- The first point is that in VMS/Alpha, procedure pointers do not in
130 -- fact point to code, but rather to a 48-byte procedure descriptor.
131 -- So a value of type AST_Handler is in fact a pointer to one of these
132 -- 48-byte descriptors.
134 type Descriptor_Type is new SSE.Storage_Array (1 .. 48);
135 for Descriptor_Type'Alignment use Standard'Maximum_Alignment;
137 type Descriptor_Ref is access all Descriptor_Type;
139 -- Normally, there is only one such descriptor for a given procedure, but
140 -- it works fine to make a copy of the single allocated descriptor, and
141 -- use the copy itself, and we take advantage of this in the design here.
142 -- The idea is that AST_Handler values will all point to a record with the
143 -- following structure:
145 -- Note: When we say it works fine, there is one delicate point, which
146 -- is that the code for the AST procedure itself requires the original
147 -- descriptor address. We handle this by saving the original descriptor
148 -- address in this structure and restoring in Process_AST.
150 type AST_Handler_Data is record
151 Descriptor : Descriptor_Type;
152 Original_Descriptor_Ref : Descriptor_Ref;
153 Taskid : ATID.Task_Id;
154 Entryno : Natural;
155 end record;
157 type AST_Handler_Data_Ref is access all AST_Handler_Data;
159 function To_AST_Handler is new Ada.Unchecked_Conversion
160 (AST_Handler_Data_Ref, System.Aux_DEC.AST_Handler);
162 -- Each time Create_AST_Handler is called, a new value of this record
163 -- type is created, containing a copy of the procedure descriptor for
164 -- the routine used to handle all AST's (Process_AST), and the Task_Id
165 -- and entry number parameters identifying the task entry involved.
167 -- The AST_Handler value returned is a pointer to this record. Since
168 -- the record starts with the procedure descriptor, it can be used
169 -- by the system in the normal way to call the procedure. But now
170 -- when the procedure gets control, it can determine the address of
171 -- the procedure descriptor used to call it (since the ABI specifies
172 -- that this is left sitting in register r27 on entry), and then use
173 -- that address to retrieve the Task_Id and entry number so that it
174 -- knows on which entry to queue the AST request.
176 -- The next issue is where are these records placed. Since we intend
177 -- to pass pointers to these records to asynchronous system service
178 -- routines, they have to be on the heap, which means we have to worry
179 -- about when to allocate them and deallocate them.
181 -- We solve this problem by introducing a task attribute that points to
182 -- a vector, indexed by the entry number, of AST_Handler_Data records
183 -- for a given task. The pointer itself is a controlled object allowing
184 -- us to write a finalization routine that frees the referenced vector.
186 -- An entry in this vector is either initialized (Entryno non-zero) and
187 -- can be used for any subsequent reference to the same entry, or it is
188 -- unused, marked by the Entryno value being zero.
190 type AST_Handler_Vector is array (Natural range <>) of AST_Handler_Data;
191 type AST_Handler_Vector_Ref is access all AST_Handler_Vector;
193 -- type AST_Vector_Ptr is new Ada.Finalization.Controlled with record
194 -- removed due to problem with controlled attribute, consequence is that
195 -- we have a memory leak if a task that has AST attribute entries is
196 -- terminated. ???
198 type AST_Vector_Ptr is record
199 Vector : AST_Handler_Vector_Ref;
200 end record;
202 AST_Vector_Init : AST_Vector_Ptr;
203 -- Initial value, treated as constant, Vector will be null
205 package AST_Attribute is new Ada.Task_Attributes
206 (Attribute => AST_Vector_Ptr,
207 Initial_Value => AST_Vector_Init);
209 use AST_Attribute;
211 -----------------------
212 -- AST Service Queue --
213 -----------------------
215 -- The following global data structures are used to queue pending
216 -- AST requests. When an AST is signalled, the AST service routine
217 -- Process_AST is called, and it makes an entry in this structure.
219 type AST_Instance is record
220 Taskid : ATID.Task_Id;
221 Entryno : Natural;
222 Param : Long_Integer;
223 end record;
224 -- The Taskid and Entryno indicate the entry on which this AST is to
225 -- be queued, and Param is the parameter provided from the AST itself.
227 AST_Service_Queue_Size : constant := 256;
228 AST_Service_Queue_Limit : constant := 250;
229 type AST_Service_Queue_Index is mod AST_Service_Queue_Size;
230 -- Index used to refer to entries in the circular buffer which holds
231 -- active AST_Instance values. The upper bound reflects the maximum
232 -- number of AST instances that can be stored in the buffer. Since
233 -- these entries are immediately serviced by the high priority server
234 -- task that does the actual entry queuing, it is very unusual to have
235 -- any significant number of entries simultaneously queued.
237 AST_Service_Queue : array (AST_Service_Queue_Index) of AST_Instance;
238 pragma Volatile_Components (AST_Service_Queue);
239 -- The circular buffer used to store active AST requests
241 AST_Service_Queue_Put : AST_Service_Queue_Index := 0;
242 AST_Service_Queue_Get : AST_Service_Queue_Index := 0;
243 pragma Atomic (AST_Service_Queue_Put);
244 pragma Atomic (AST_Service_Queue_Get);
245 -- These two variables point to the next slots in the AST_Service_Queue
246 -- to be used for putting a new entry in and taking an entry out. This
247 -- is a circular buffer, so these pointers wrap around. If the two values
248 -- are equal the buffer is currently empty. The pointers are atomic to
249 -- ensure proper synchronization between the single producer (namely the
250 -- Process_AST procedure), and the single consumer (the AST_Service_Task).
252 --------------------------------
253 -- AST Server Task Structures --
254 --------------------------------
256 -- The basic approach is that when an AST comes in, a call is made to
257 -- the Process_AST procedure. It queues the request in the service queue
258 -- and then wakes up an AST server task to perform the actual call to the
259 -- required entry. We use this intermediate server task, since the AST
260 -- procedure itself cannot wait to return, and we need some caller for
261 -- the rendezvous so that we can use the normal rendezvous mechanism.
263 -- It would work to have only one AST server task, but then we would lose
264 -- all overlap in AST processing, and furthermore, we could get priority
265 -- inversion effects resulting in starvation of AST requests.
267 -- We therefore maintain a small pool of AST server tasks. We adjust
268 -- the size of the pool dynamically to reflect traffic, so that we have
269 -- a sufficient number of server tasks to avoid starvation.
271 Max_AST_Servers : constant Natural := 16;
272 -- Maximum number of AST server tasks that can be allocated
274 Num_AST_Servers : Natural := 0;
275 -- Number of AST server tasks currently active
277 Num_Waiting_AST_Servers : Natural := 0;
278 -- This is the number of AST server tasks that are either waiting for
279 -- work, or just about to go to sleep and wait for work.
281 Is_Waiting : array (1 .. Max_AST_Servers) of Boolean := (others => False);
282 -- An array of flags showing which AST server tasks are currently waiting
284 AST_Task_Ids : array (1 .. Max_AST_Servers) of ST.Task_Id;
285 -- Task Id's of allocated AST server tasks
287 task type AST_Server_Task (Num : Natural) is
288 pragma Priority (Priority'Last);
289 end AST_Server_Task;
290 -- Declaration for AST server task. This task has no entries, it is
291 -- controlled by sleep and wakeup calls at the task primitives level.
293 type AST_Server_Task_Ptr is access all AST_Server_Task;
294 -- Type used to allocate server tasks
296 -----------------------
297 -- Local Subprograms --
298 -----------------------
300 procedure Allocate_New_AST_Server;
301 -- Allocate an additional AST server task
303 procedure Process_AST (Param : Long_Integer);
304 -- This is the central routine for processing all AST's, it is referenced
305 -- as the code address of all created AST_Handler values. See detailed
306 -- description in body to understand how it works to have a single such
307 -- procedure for all AST's even though it does not get any indication of
308 -- the entry involved passed as an explicit parameter. The single explicit
309 -- parameter Param is the parameter passed by the system with the AST.
311 -----------------------------
312 -- Allocate_New_AST_Server --
313 -----------------------------
315 procedure Allocate_New_AST_Server is
316 Dummy : AST_Server_Task_Ptr;
317 pragma Unreferenced (Dummy);
319 begin
320 if Num_AST_Servers = Max_AST_Servers then
321 return;
323 else
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);
330 end if;
331 end Allocate_New_AST_Server;
333 ---------------------
334 -- AST_Server_Task --
335 ---------------------
337 task body AST_Server_Task is
338 Taskid : ATID.Task_Id;
339 Entryno : Natural;
340 Param : aliased Long_Integer;
341 Self_Id : constant ST.Task_Id := ST.Self;
343 pragma Volatile (Param);
345 begin
346 -- By making this task independent of master, when the environment
347 -- task is finalizing, the AST_Server_Task will be notified that it
348 -- should terminate.
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.
361 Lock_AST (Self_Id);
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.
366 loop
367 Num_Waiting_AST_Servers := Num_Waiting_AST_Servers + 1;
369 Unlock_AST (Self_Id);
371 STI.Defer_Abort (Self_Id);
373 if SP.Single_Lock then
374 STPO.Lock_RTS;
375 end if;
377 STPO.Write_Lock (Self_Id);
379 Is_Waiting (Num) := True;
381 Self_Id.Common.State := ST.AST_Server_Sleep;
382 STPO.Sleep (Self_Id, ST.AST_Server_Sleep);
383 Self_Id.Common.State := ST.Runnable;
385 STPO.Unlock (Self_Id);
387 if SP.Single_Lock then
388 STPO.Unlock_RTS;
389 end if;
391 -- If the process is finalizing, Undefer_Abort will simply end
392 -- this task.
394 STI.Undefer_Abort (Self_Id);
396 -- We are awake, there is something to do!
398 Lock_AST (Self_Id);
399 Num_Waiting_AST_Servers := Num_Waiting_AST_Servers - 1;
401 -- Loop here to service outstanding requests. We are always
402 -- locked on entry to this loop.
404 while AST_Service_Queue_Get /= AST_Service_Queue_Put loop
405 Taskid := AST_Service_Queue (AST_Service_Queue_Get).Taskid;
406 Entryno := AST_Service_Queue (AST_Service_Queue_Get).Entryno;
407 Param := AST_Service_Queue (AST_Service_Queue_Get).Param;
409 AST_Service_Queue_Get := AST_Service_Queue_Get + 1;
411 -- This is a manual expansion of the normal call simple code
413 declare
414 type AA is access all Long_Integer;
415 P : AA := Param'Unrestricted_Access;
417 function To_ST_Task_Id is new Ada.Unchecked_Conversion
418 (ATID.Task_Id, ST.Task_Id);
420 begin
421 Unlock_AST (Self_Id);
422 STR.Call_Simple
423 (Acceptor => To_ST_Task_Id (Taskid),
424 E => ST.Task_Entry_Index (Entryno),
425 Uninterpreted_Data => P'Address);
427 exception
428 when E : others =>
429 System.IO.Put_Line ("%Debugging event");
430 System.IO.Put_Line (Exception_Name (E) &
431 " raised when trying to deliver an AST.");
433 if Exception_Message (E)'Length /= 0 then
434 System.IO.Put_Line (Exception_Message (E));
435 end if;
437 System.IO.Put_Line ("Task type is " & "Receiver_Type");
438 System.IO.Put_Line ("Task id is " & ATID.Image (Taskid));
439 end;
441 Lock_AST (Self_Id);
442 end loop;
443 end loop;
444 end AST_Server_Task;
446 ------------------------
447 -- Create_AST_Handler --
448 ------------------------
450 function Create_AST_Handler
451 (Taskid : ATID.Task_Id;
452 Entryno : Natural) return System.Aux_DEC.AST_Handler
454 Attr_Ref : Attribute_Handle;
456 Process_AST_Ptr : constant AST_Handler := Process_AST'Access;
457 -- Reference to standard procedure descriptor for Process_AST
459 pragma Warnings (Off, "*alignment*");
460 -- Suppress harmless warnings about alignment.
461 -- Should explain why this warning is harmless ???
463 function To_Descriptor_Ref is new Ada.Unchecked_Conversion
464 (AST_Handler, Descriptor_Ref);
466 Original_Descriptor_Ref : constant Descriptor_Ref :=
467 To_Descriptor_Ref (Process_AST_Ptr);
469 pragma Warnings (On, "*alignment*");
471 begin
472 if ATID.Is_Terminated (Taskid) then
473 raise Program_Error;
474 end if;
476 Attr_Ref := Reference (Taskid);
478 -- Allocate another server if supply is getting low
480 if Num_Waiting_AST_Servers < 2 then
481 Allocate_New_AST_Server;
482 end if;
484 -- No point in creating more if we have zillions waiting to
485 -- be serviced.
487 while AST_Service_Queue_Put - AST_Service_Queue_Get
488 > AST_Service_Queue_Limit
489 loop
490 delay 0.01;
491 end loop;
493 -- If no AST vector allocated, or the one we have is too short, then
494 -- allocate one of right size and initialize all entries except the
495 -- one we will use to unused. Note that the assignment automatically
496 -- frees the old allocated table if there is one.
498 if Attr_Ref.Vector = null
499 or else Attr_Ref.Vector'Length < Entryno
500 then
501 Attr_Ref.Vector := new AST_Handler_Vector (1 .. Entryno);
503 for E in 1 .. Entryno loop
504 Attr_Ref.Vector (E).Descriptor :=
505 Original_Descriptor_Ref.all;
506 Attr_Ref.Vector (E).Original_Descriptor_Ref :=
507 Original_Descriptor_Ref;
508 Attr_Ref.Vector (E).Taskid := Taskid;
509 Attr_Ref.Vector (E).Entryno := E;
510 end loop;
511 end if;
513 return To_AST_Handler (Attr_Ref.Vector (Entryno)'Unrestricted_Access);
514 end Create_AST_Handler;
516 ----------------------------
517 -- Expand_AST_Packet_Pool --
518 ----------------------------
520 procedure Expand_AST_Packet_Pool
521 (Requested_Packets : Natural;
522 Actual_Number : out Natural;
523 Total_Number : out Natural)
525 pragma Unreferenced (Requested_Packets);
526 begin
527 -- The AST implementation of GNAT does not permit dynamic expansion
528 -- of the pool, so we simply add no entries and return the total. If
529 -- it is necessary to expand the allocation, then this package body
530 -- must be recompiled with a larger value for AST_Service_Queue_Size.
532 Actual_Number := 0;
533 Total_Number := AST_Service_Queue_Size;
534 end Expand_AST_Packet_Pool;
536 -----------------
537 -- Process_AST --
538 -----------------
540 procedure Process_AST (Param : Long_Integer) is
542 Handler_Data_Ptr : AST_Handler_Data_Ref;
543 -- This variable is set to the address of the descriptor through
544 -- which Process_AST is called. Since the descriptor is part of
545 -- an AST_Handler value, this is also the address of this value,
546 -- from which we can obtain the task and entry number information.
548 function To_Address is new Ada.Unchecked_Conversion
549 (ST.Task_Id, System.Task_Primitives.Task_Address);
551 begin
552 System.Machine_Code.Asm
553 (Template => "addq $27,0,%0",
554 Outputs => AST_Handler_Data_Ref'Asm_Output ("=r", Handler_Data_Ptr),
555 Volatile => True);
557 System.Machine_Code.Asm
558 (Template => "ldq $27,%0",
559 Inputs => Descriptor_Ref'Asm_Input
560 ("m", Handler_Data_Ptr.Original_Descriptor_Ref),
561 Volatile => True);
563 AST_Service_Queue (AST_Service_Queue_Put) := AST_Instance'
564 (Taskid => Handler_Data_Ptr.Taskid,
565 Entryno => Handler_Data_Ptr.Entryno,
566 Param => Param);
568 -- OpenVMS Programming Concepts manual, chapter 8.2.3:
569 -- "Implicit synchronization can be achieved for data that is shared
570 -- for write by using only AST routines to write the data, since only
571 -- one AST can be running at any one time."
573 -- This subprogram runs at AST level so is guaranteed to be
574 -- called sequentially at a given access level.
576 AST_Service_Queue_Put := AST_Service_Queue_Put + 1;
578 -- Need to wake up processing task. If there is no waiting server
579 -- then we have temporarily run out, but things should still be
580 -- OK, since one of the active ones will eventually pick up the
581 -- service request queued in the AST_Service_Queue.
583 for J in 1 .. Num_AST_Servers loop
584 if Is_Waiting (J) then
585 Is_Waiting (J) := False;
587 -- Sleeps are handled by ASTs on VMS, so don't call Wakeup
589 STPOD.Interrupt_AST_Handler (To_Address (AST_Task_Ids (J)));
590 exit;
591 end if;
592 end loop;
593 end Process_AST;
595 begin
596 STPO.Initialize_Lock (AST_Lock'Access, STPO.Global_Task_Level);
597 end System.AST_Handling;