1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . I N T E R R U P T S --
9 -- Copyright (C) 1992-2003, Free Software Foundation, Inc. --
11 -- GNARL 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 2, or (at your option) any later ver- --
14 -- sion. GNARL 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This is an OpenVMS/Alpha version of this package.
38 -- Once we associate a Server_Task with an interrupt, the task never
39 -- goes away, and we never remove the association.
41 -- There is no more than one interrupt per Server_Task and no more than
42 -- one Server_Task per interrupt.
44 -- Within this package, the lock L is used to protect the various status
45 -- tables. If there is a Server_Task associated with an interrupt, we use
46 -- the per-task lock of the Server_Task instead so that we protect the
47 -- status between Interrupt_Manager and Server_Task. Protection among
48 -- service requests are done using User Request to Interrupt_Manager
51 with Ada.Task_Identification;
52 -- used for Task_ID type
55 -- used for Raise_Exception
57 with System.Task_Primitives;
61 with System.Interrupt_Management;
65 -- Abort_Task_Interrupt
67 with System.Interrupt_Management.Operations;
68 -- used for Thread_Block_Interrupt
69 -- Thread_Unblock_Interrupt
70 -- Install_Default_Action
71 -- Install_Ignore_Action
72 -- Copy_Interrupt_Mask
74 -- Empty_Interrupt_Mask
75 -- Fill_Interrupt_Mask
76 -- Add_To_Interrupt_Mask
77 -- Delete_From_Interrupt_Mask
79 -- Interrupt_Self_Process
84 pragma Elaborate_All (System.Interrupt_Management.Operations);
86 with System.Task_Primitives.Operations;
87 -- used for Write_Lock
94 with System.Task_Primitives.Interrupt_Operations;
95 -- used for Set_Interrupt_ID
97 with System.Storage_Elements;
98 -- used for To_Address
107 -- Interrupt_Manager_ID
109 with System.Tasking.Utilities;
110 -- used for Make_Independent
112 with System.Tasking.Rendezvous;
113 -- used for Call_Simple
114 pragma Elaborate_All (System.Tasking.Rendezvous);
116 with System.Tasking.Initialization;
117 -- used for Defer_Abort
120 with System.Parameters;
121 -- used for Single_Lock
123 with Unchecked_Conversion;
125 package body System.Interrupts is
128 use System.Parameters;
131 package POP renames System.Task_Primitives.Operations;
132 package PIO renames System.Task_Primitives.Interrupt_Operations;
133 package IMNG renames System.Interrupt_Management;
134 package IMOP renames System.Interrupt_Management.Operations;
136 function To_System is new Unchecked_Conversion
137 (Ada.Task_Identification.Task_Id, Task_ID);
143 -- WARNING: System.Tasking.Stages performs calls to this task
144 -- with low-level constructs. Do not change this spec without synchro-
147 task Interrupt_Manager is
148 entry Detach_Interrupt_Entries (T : Task_ID);
150 entry Initialize (Mask : IMNG.Interrupt_Mask);
153 (New_Handler : Parameterless_Handler;
154 Interrupt : Interrupt_ID;
156 Restoration : Boolean := False);
158 entry Exchange_Handler
159 (Old_Handler : out Parameterless_Handler;
160 New_Handler : Parameterless_Handler;
161 Interrupt : Interrupt_ID;
165 (Interrupt : Interrupt_ID;
168 entry Bind_Interrupt_To_Entry
170 E : Task_Entry_Index;
171 Interrupt : Interrupt_ID);
173 entry Block_Interrupt (Interrupt : Interrupt_ID);
175 entry Unblock_Interrupt (Interrupt : Interrupt_ID);
177 entry Ignore_Interrupt (Interrupt : Interrupt_ID);
179 entry Unignore_Interrupt (Interrupt : Interrupt_ID);
181 pragma Interrupt_Priority (System.Interrupt_Priority'Last);
182 end Interrupt_Manager;
184 task type Server_Task (Interrupt : Interrupt_ID) is
185 pragma Priority (System.Interrupt_Priority'Last);
186 -- Note: the above pragma Priority is strictly speaking improper
187 -- since it is outside the range of allowed priorities, but the
188 -- compiler treats system units specially and does not apply
189 -- this range checking rule to system units.
193 type Server_Task_Access is access Server_Task;
195 --------------------------------
196 -- Local Types and Variables --
197 --------------------------------
199 type Entry_Assoc is record
201 E : Task_Entry_Index;
204 type Handler_Assoc is record
205 H : Parameterless_Handler;
206 Static : Boolean; -- Indicates static binding;
209 User_Handler : array (Interrupt_ID'Range) of Handler_Assoc :=
210 (others => (null, Static => False));
211 pragma Volatile_Components (User_Handler);
212 -- Holds the protected procedure handler (if any) and its Static
213 -- information for each interrupt. A handler is a Static one if
214 -- it is specified through the pragma Attach_Handler.
215 -- Attach_Handler. Otherwise, not static)
217 User_Entry : array (Interrupt_ID'Range) of Entry_Assoc :=
218 (others => (T => Null_Task, E => Null_Task_Entry));
219 pragma Volatile_Components (User_Entry);
220 -- Holds the task and entry index (if any) for each interrupt
222 Blocked : array (Interrupt_ID'Range) of Boolean := (others => False);
223 pragma Volatile_Components (Blocked);
224 -- True iff the corresponding interrupt is blocked in the process level
226 Ignored : array (Interrupt_ID'Range) of Boolean := (others => False);
227 pragma Volatile_Components (Ignored);
228 -- True iff the corresponding interrupt is blocked in the process level
231 array (Interrupt_ID'Range) of Task_ID := (others => Null_Task);
232 pragma Volatile_Components (Last_Unblocker);
233 -- Holds the ID of the last Task which Unblocked this Interrupt.
234 -- It contains Null_Task if no tasks have ever requested the
235 -- Unblocking operation or the Interrupt is currently Blocked.
237 Server_ID : array (Interrupt_ID'Range) of Task_ID :=
238 (others => Null_Task);
239 pragma Atomic_Components (Server_ID);
240 -- Holds the Task_ID of the Server_Task for each interrupt.
241 -- Task_ID is needed to accomplish locking per Interrupt base. Also
242 -- is needed to decide whether to create a new Server_Task.
244 -- Type and Head, Tail of the list containing Registered Interrupt
245 -- Handlers. These definitions are used to register the handlers
246 -- specified by the pragma Interrupt_Handler.
248 type Registered_Handler;
249 type R_Link is access all Registered_Handler;
251 type Registered_Handler is record
252 H : System.Address := System.Null_Address;
253 Next : R_Link := null;
256 Registered_Handler_Head : R_Link := null;
257 Registered_Handler_Tail : R_Link := null;
259 Access_Hold : Server_Task_Access;
260 -- variable used to allocate Server_Task using "new".
262 -----------------------
263 -- Local Subprograms --
264 -----------------------
266 function Is_Registered (Handler : Parameterless_Handler) return Boolean;
267 -- See if the Handler has been "pragma"ed using Interrupt_Handler.
268 -- Always consider a null handler as registered.
270 --------------------------------
271 -- Register_Interrupt_Handler --
272 --------------------------------
274 procedure Register_Interrupt_Handler (Handler_Addr : System.Address) is
275 New_Node_Ptr : R_Link;
277 -- This routine registers the Handler as usable for Dynamic
278 -- Interrupt Handler. Routines attaching and detaching Handler
279 -- dynamically should first consult if the Handler is rgistered.
280 -- A Program Error should be raised if it is not registered.
282 -- The pragma Interrupt_Handler can only appear in the library
283 -- level PO definition and instantiation. Therefore, we do not need
284 -- to implement Unregistering operation. Neither we need to
285 -- protect the queue structure using a Lock.
287 pragma Assert (Handler_Addr /= System.Null_Address);
289 New_Node_Ptr := new Registered_Handler;
290 New_Node_Ptr.H := Handler_Addr;
292 if Registered_Handler_Head = null then
293 Registered_Handler_Head := New_Node_Ptr;
294 Registered_Handler_Tail := New_Node_Ptr;
297 Registered_Handler_Tail.Next := New_Node_Ptr;
298 Registered_Handler_Tail := New_Node_Ptr;
300 end Register_Interrupt_Handler;
306 function Is_Registered (Handler : Parameterless_Handler) return Boolean is
307 type Fat_Ptr is record
308 Object_Addr : System.Address;
309 Handler_Addr : System.Address;
312 function To_Fat_Ptr is new Unchecked_Conversion
313 (Parameterless_Handler, Fat_Ptr);
319 if Handler = null then
323 Fat := To_Fat_Ptr (Handler);
325 Ptr := Registered_Handler_Head;
327 while (Ptr /= null) loop
328 if Ptr.H = Fat.Handler_Addr then
343 function Is_Reserved (Interrupt : Interrupt_ID) return Boolean is
345 return IMNG.Reserve (IMNG.Interrupt_ID (Interrupt));
348 -----------------------
349 -- Is_Entry_Attached --
350 -----------------------
352 function Is_Entry_Attached (Interrupt : Interrupt_ID) return Boolean is
354 if Is_Reserved (Interrupt) then
355 Raise_Exception (Program_Error'Identity, "Interrupt" &
356 Interrupt_ID'Image (Interrupt) & " is reserved");
359 return User_Entry (Interrupt).T /= Null_Task;
360 end Is_Entry_Attached;
362 -------------------------
363 -- Is_Handler_Attached --
364 -------------------------
366 function Is_Handler_Attached (Interrupt : Interrupt_ID) return Boolean is
368 if Is_Reserved (Interrupt) then
369 Raise_Exception (Program_Error'Identity, "Interrupt" &
370 Interrupt_ID'Image (Interrupt) & " is reserved");
373 return User_Handler (Interrupt).H /= null;
374 end Is_Handler_Attached;
380 function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
382 if Is_Reserved (Interrupt) then
383 Raise_Exception (Program_Error'Identity, "Interrupt" &
384 Interrupt_ID'Image (Interrupt) & " is reserved");
387 return Blocked (Interrupt);
394 function Is_Ignored (Interrupt : Interrupt_ID) return Boolean is
396 if Is_Reserved (Interrupt) then
397 Raise_Exception (Program_Error'Identity, "Interrupt" &
398 Interrupt_ID'Image (Interrupt) & " is reserved");
401 return Ignored (Interrupt);
404 ---------------------
405 -- Current_Handler --
406 ---------------------
408 function Current_Handler (Interrupt : Interrupt_ID)
409 return Parameterless_Handler is
411 if Is_Reserved (Interrupt) then
412 Raise_Exception (Program_Error'Identity, "Interrupt" &
413 Interrupt_ID'Image (Interrupt) & " is reserved");
416 -- ??? Since Parameterless_Handler is not Atomic, the
417 -- current implementation is wrong. We need a new service in
418 -- Interrupt_Manager to ensure atomicity.
420 return User_Handler (Interrupt).H;
427 -- Calling this procedure with New_Handler = null and Static = True
428 -- means we want to detach the current handler regardless of the
429 -- previous handler's binding status (ie. do not care if it is a
430 -- dynamic or static handler).
432 -- This option is needed so that during the finalization of a PO, we
433 -- can detach handlers attached through pragma Attach_Handler.
435 procedure Attach_Handler
436 (New_Handler : Parameterless_Handler;
437 Interrupt : Interrupt_ID;
438 Static : Boolean := False) is
440 if Is_Reserved (Interrupt) then
441 Raise_Exception (Program_Error'Identity, "Interrupt" &
442 Interrupt_ID'Image (Interrupt) & " is reserved");
445 Interrupt_Manager.Attach_Handler (New_Handler, Interrupt, Static);
449 ----------------------
450 -- Exchange_Handler --
451 ----------------------
453 -- Calling this procedure with New_Handler = null and Static = True
454 -- means we want to detach the current handler regardless of the
455 -- previous handler's binding status (ie. do not care if it is a
456 -- dynamic or static handler).
458 -- This option is needed so that during the finalization of a PO, we
459 -- can detach handlers attached through pragma Attach_Handler.
461 procedure Exchange_Handler
462 (Old_Handler : out Parameterless_Handler;
463 New_Handler : Parameterless_Handler;
464 Interrupt : Interrupt_ID;
465 Static : Boolean := False) is
467 if Is_Reserved (Interrupt) then
468 Raise_Exception (Program_Error'Identity, "Interrupt" &
469 Interrupt_ID'Image (Interrupt) & " is reserved");
472 Interrupt_Manager.Exchange_Handler
473 (Old_Handler, New_Handler, Interrupt, Static);
475 end Exchange_Handler;
481 -- Calling this procedure with Static = True means we want to Detach the
482 -- current handler regardless of the previous handler's binding status
483 -- (i.e. do not care if it is a dynamic or static handler).
485 -- This option is needed so that during the finalization of a PO, we can
486 -- detach handlers attached through pragma Attach_Handler.
488 procedure Detach_Handler
489 (Interrupt : Interrupt_ID;
490 Static : Boolean := False)
493 if Is_Reserved (Interrupt) then
494 Raise_Exception (Program_Error'Identity, "Interrupt" &
495 Interrupt_ID'Image (Interrupt) & " is reserved");
498 Interrupt_Manager.Detach_Handler (Interrupt, Static);
505 function Reference (Interrupt : Interrupt_ID) return System.Address is
507 if Is_Reserved (Interrupt) then
508 Raise_Exception (Program_Error'Identity, "Interrupt" &
509 Interrupt_ID'Image (Interrupt) & " is reserved");
512 return Storage_Elements.To_Address
513 (Storage_Elements.Integer_Address (Interrupt));
516 -----------------------------
517 -- Bind_Interrupt_To_Entry --
518 -----------------------------
520 -- This procedure raises a Program_Error if it tries to
521 -- bind an interrupt to which an Entry or a Procedure is
524 procedure Bind_Interrupt_To_Entry
526 E : Task_Entry_Index;
527 Int_Ref : System.Address)
529 Interrupt : constant Interrupt_ID :=
530 Interrupt_ID (Storage_Elements.To_Integer (Int_Ref));
533 if Is_Reserved (Interrupt) then
534 Raise_Exception (Program_Error'Identity, "Interrupt" &
535 Interrupt_ID'Image (Interrupt) & " is reserved");
538 Interrupt_Manager.Bind_Interrupt_To_Entry (T, E, Interrupt);
540 end Bind_Interrupt_To_Entry;
542 ------------------------------
543 -- Detach_Interrupt_Entries --
544 ------------------------------
546 procedure Detach_Interrupt_Entries (T : Task_ID) is
548 Interrupt_Manager.Detach_Interrupt_Entries (T);
549 end Detach_Interrupt_Entries;
551 ---------------------
552 -- Block_Interrupt --
553 ---------------------
555 procedure Block_Interrupt (Interrupt : Interrupt_ID) is
557 if Is_Reserved (Interrupt) then
558 Raise_Exception (Program_Error'Identity, "Interrupt" &
559 Interrupt_ID'Image (Interrupt) & " is reserved");
562 Interrupt_Manager.Block_Interrupt (Interrupt);
565 -----------------------
566 -- Unblock_Interrupt --
567 -----------------------
569 procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
571 if Is_Reserved (Interrupt) then
572 Raise_Exception (Program_Error'Identity, "Interrupt" &
573 Interrupt_ID'Image (Interrupt) & " is reserved");
576 Interrupt_Manager.Unblock_Interrupt (Interrupt);
577 end Unblock_Interrupt;
583 function Unblocked_By
584 (Interrupt : Interrupt_ID) return System.Tasking.Task_ID is
586 if Is_Reserved (Interrupt) then
587 Raise_Exception (Program_Error'Identity, "Interrupt" &
588 Interrupt_ID'Image (Interrupt) & " is reserved");
591 return Last_Unblocker (Interrupt);
594 ----------------------
595 -- Ignore_Interrupt --
596 ----------------------
598 procedure Ignore_Interrupt (Interrupt : Interrupt_ID) is
600 if Is_Reserved (Interrupt) then
601 Raise_Exception (Program_Error'Identity, "Interrupt" &
602 Interrupt_ID'Image (Interrupt) & " is reserved");
605 Interrupt_Manager.Ignore_Interrupt (Interrupt);
606 end Ignore_Interrupt;
608 ------------------------
609 -- Unignore_Interrupt --
610 ------------------------
612 procedure Unignore_Interrupt (Interrupt : Interrupt_ID) is
614 if Is_Reserved (Interrupt) then
615 Raise_Exception (Program_Error'Identity, "Interrupt" &
616 Interrupt_ID'Image (Interrupt) & " is reserved");
619 Interrupt_Manager.Unignore_Interrupt (Interrupt);
620 end Unignore_Interrupt;
622 -----------------------
623 -- Interrupt_Manager --
624 -----------------------
626 task body Interrupt_Manager is
628 ---------------------
630 ---------------------
632 procedure Unprotected_Exchange_Handler
633 (Old_Handler : out Parameterless_Handler;
634 New_Handler : Parameterless_Handler;
635 Interrupt : Interrupt_ID;
637 Restoration : Boolean := False);
639 procedure Unprotected_Detach_Handler
640 (Interrupt : Interrupt_ID;
643 ----------------------------------
644 -- Unprotected_Exchange_Handler --
645 ----------------------------------
647 procedure Unprotected_Exchange_Handler
648 (Old_Handler : out Parameterless_Handler;
649 New_Handler : Parameterless_Handler;
650 Interrupt : Interrupt_ID;
652 Restoration : Boolean := False)
655 if User_Entry (Interrupt).T /= Null_Task then
656 -- In case we have an Interrupt Entry already installed.
657 -- raise a program error. (propagate it to the caller).
659 Raise_Exception (Program_Error'Identity,
660 "An interrupt is already installed");
663 -- Note : A null handler with Static = True will
664 -- pass the following check. That is the case when we want to
665 -- Detach a handler regardless of the Static status
666 -- of the current_Handler.
667 -- We don't check anything if Restoration is True, since we
668 -- may be detaching a static handler to restore a dynamic one.
670 if not Restoration and then not Static
671 -- Tries to overwrite a static Interrupt Handler with a
674 and then (User_Handler (Interrupt).Static
676 -- The new handler is not specified as an
677 -- Interrupt Handler by a pragma.
679 or else not Is_Registered (New_Handler))
681 Raise_Exception (Program_Error'Identity,
682 "Trying to overwrite a static Interrupt Handler with a " &
686 -- The interrupt should no longer be ingnored if
687 -- it was ever ignored.
689 Ignored (Interrupt) := False;
691 -- Save the old handler
693 Old_Handler := User_Handler (Interrupt).H;
697 User_Handler (Interrupt).H := New_Handler;
699 if New_Handler = null then
701 -- The null handler means we are detaching the handler.
703 User_Handler (Interrupt).Static := False;
706 User_Handler (Interrupt).Static := Static;
709 -- Invoke a corresponding Server_Task if not yet created.
710 -- Place Task_ID info in Server_ID array.
712 if Server_ID (Interrupt) = Null_Task then
713 Access_Hold := new Server_Task (Interrupt);
714 Server_ID (Interrupt) := To_System (Access_Hold.all'Identity);
716 POP.Wakeup (Server_ID (Interrupt), Interrupt_Server_Idle_Sleep);
719 end Unprotected_Exchange_Handler;
721 --------------------------------
722 -- Unprotected_Detach_Handler --
723 --------------------------------
725 procedure Unprotected_Detach_Handler
726 (Interrupt : Interrupt_ID;
729 Old_Handler : Parameterless_Handler;
732 if User_Entry (Interrupt).T /= Null_Task then
733 -- In case we have an Interrupt Entry installed.
734 -- raise a program error. (propagate it to the caller).
736 Raise_Exception (Program_Error'Identity,
737 "An interrupt entry is already installed");
740 -- Note : Static = True will pass the following check. That is the
741 -- case when we want to detach a handler regardless of the static
742 -- status of the current_Handler.
744 if not Static and then User_Handler (Interrupt).Static then
745 -- Tries to detach a static Interrupt Handler.
746 -- raise a program error.
748 Raise_Exception (Program_Error'Identity,
749 "Trying to detach a static Interrupt Handler");
752 -- The interrupt should no longer be ignored if
753 -- it was ever ignored.
755 Ignored (Interrupt) := False;
757 Old_Handler := User_Handler (Interrupt).H;
761 User_Handler (Interrupt).H := null;
762 User_Handler (Interrupt).Static := False;
763 IMOP.Interrupt_Self_Process (IMNG.Interrupt_ID (Interrupt));
765 end Unprotected_Detach_Handler;
767 -- Start of processing for Interrupt_Manager
770 -- By making this task independent of master, when the process
771 -- goes away, the Interrupt_Manager will terminate gracefully.
773 System.Tasking.Utilities.Make_Independent;
775 -- Environmen task gets its own interrupt mask, saves it,
776 -- and then masks all interrupts except the Keep_Unmasked set.
778 -- During rendezvous, the Interrupt_Manager receives the old
779 -- interrupt mask of the environment task, and sets its own
780 -- interrupt mask to that value.
782 -- The environment task will call the entry of Interrupt_Manager some
783 -- during elaboration of the body of this package.
785 accept Initialize (Mask : IMNG.Interrupt_Mask) do
786 pragma Warnings (Off, Mask);
790 -- Note: All tasks in RTS will have all the Reserve Interrupts
791 -- being masked (except the Interrupt_Manager) and Keep_Unmasked
792 -- unmasked when created.
794 -- Abort_Task_Interrupt is one of the Interrupt unmasked
795 -- in all tasks. We mask the Interrupt in this particular task
796 -- so that "sigwait" is possible to catch an explicitely sent
797 -- Abort_Task_Interrupt from the Server_Tasks.
799 -- This sigwaiting is needed so that we make sure a Server_Task is
800 -- out of its own sigwait state. This extra synchronization is
801 -- necessary to prevent following senarios.
803 -- 1) Interrupt_Manager sends an Abort_Task_Interrupt to the
804 -- Server_Task then changes its own interrupt mask (OS level).
805 -- If an interrupt (corresponding to the Server_Task) arrives
806 -- in the nean time we have the Interrupt_Manager umnasked and
807 -- the Server_Task waiting on sigwait.
809 -- 2) For unbinding handler, we install a default action in the
810 -- Interrupt_Manager. POSIX.1c states that the result of using
811 -- "sigwait" and "sigaction" simaltaneously on the same interrupt
812 -- is undefined. Therefore, we need to be informed from the
813 -- Server_Task of the fact that the Server_Task is out of its
817 -- A block is needed to absorb Program_Error exception
820 Old_Handler : Parameterless_Handler;
824 accept Attach_Handler
825 (New_Handler : Parameterless_Handler;
826 Interrupt : Interrupt_ID;
828 Restoration : Boolean := False)
830 Unprotected_Exchange_Handler
831 (Old_Handler, New_Handler, Interrupt, Static, Restoration);
834 or accept Exchange_Handler
835 (Old_Handler : out Parameterless_Handler;
836 New_Handler : Parameterless_Handler;
837 Interrupt : Interrupt_ID;
840 Unprotected_Exchange_Handler
841 (Old_Handler, New_Handler, Interrupt, Static);
842 end Exchange_Handler;
844 or accept Detach_Handler
845 (Interrupt : Interrupt_ID;
848 Unprotected_Detach_Handler (Interrupt, Static);
851 or accept Bind_Interrupt_To_Entry
853 E : Task_Entry_Index;
854 Interrupt : Interrupt_ID)
856 -- if there is a binding already (either a procedure or an
857 -- entry), raise Program_Error (propagate it to the caller).
859 if User_Handler (Interrupt).H /= null
860 or else User_Entry (Interrupt).T /= Null_Task
862 Raise_Exception (Program_Error'Identity,
863 "A binding for this interrupt is already present");
866 -- The interrupt should no longer be ingnored if
867 -- it was ever ignored.
869 Ignored (Interrupt) := False;
870 User_Entry (Interrupt) := Entry_Assoc'(T => T, E => E);
872 -- Indicate the attachment of Interrupt Entry in ATCB.
873 -- This is need so that when an Interrupt Entry task
874 -- terminates the binding can be cleaned.
875 -- The call to unbinding must be
876 -- make by the task before it terminates.
878 T.Interrupt_Entry := True;
880 -- Invoke a corresponding Server_Task if not yet created.
881 -- Place Task_ID info in Server_ID array.
883 if Server_ID (Interrupt) = Null_Task then
885 Access_Hold := new Server_Task (Interrupt);
886 Server_ID (Interrupt) :=
887 To_System (Access_Hold.all'Identity);
889 POP.Wakeup (Server_ID (Interrupt),
890 Interrupt_Server_Idle_Sleep);
892 end Bind_Interrupt_To_Entry;
894 or accept Detach_Interrupt_Entries (T : Task_ID)
896 for J in Interrupt_ID'Range loop
897 if not Is_Reserved (J) then
898 if User_Entry (J).T = T then
900 -- The interrupt should no longer be ignored if
901 -- it was ever ignored.
903 Ignored (J) := False;
905 Entry_Assoc'(T => Null_Task, E => Null_Task_Entry);
906 IMOP.Interrupt_Self_Process (IMNG.Interrupt_ID (J));
911 -- Indicate in ATCB that no Interrupt Entries are attached.
913 T.Interrupt_Entry := False;
914 end Detach_Interrupt_Entries;
916 or accept Block_Interrupt (Interrupt : Interrupt_ID) do
917 pragma Warnings (Off, Interrupt);
921 or accept Unblock_Interrupt (Interrupt : Interrupt_ID) do
922 pragma Warnings (Off, Interrupt);
924 end Unblock_Interrupt;
926 or accept Ignore_Interrupt (Interrupt : Interrupt_ID) do
927 pragma Warnings (Off, Interrupt);
929 end Ignore_Interrupt;
931 or accept Unignore_Interrupt (Interrupt : Interrupt_ID) do
932 pragma Warnings (Off, Interrupt);
934 end Unignore_Interrupt;
939 -- If there is a program error we just want to propagate it
940 -- to the caller and do not want to stop this task.
942 when Program_Error =>
946 pragma Assert (False);
950 end Interrupt_Manager;
956 task body Server_Task is
957 Self_ID : Task_ID := Self;
958 Tmp_Handler : Parameterless_Handler;
960 Tmp_Entry_Index : Task_Entry_Index;
961 Intwait_Mask : aliased IMNG.Interrupt_Mask;
962 Ret_Interrupt : IMNG.Interrupt_ID;
965 -- By making this task independent of master, when the process
966 -- goes away, the Server_Task will terminate gracefully.
968 System.Tasking.Utilities.Make_Independent;
970 -- Install default action in system level.
972 IMOP.Install_Default_Action (IMNG.Interrupt_ID (Interrupt));
974 -- Set up the mask (also clears the event flag)
976 IMOP.Empty_Interrupt_Mask (Intwait_Mask'Access);
977 IMOP.Add_To_Interrupt_Mask
978 (Intwait_Mask'Access, IMNG.Interrupt_ID (Interrupt));
980 -- Remember the Interrupt_ID for Abort_Task.
982 PIO.Set_Interrupt_ID (IMNG.Interrupt_ID (Interrupt), Self_ID);
984 -- Note: All tasks in RTS will have all the Reserve Interrupts
985 -- being masked (except the Interrupt_Manager) and Keep_Unmasked
986 -- unmasked when created.
989 System.Tasking.Initialization.Defer_Abort (Self_ID);
991 -- A Handler or an Entry is installed. At this point all tasks
992 -- mask for the Interrupt is masked. Catch the Interrupt using
995 -- This task may wake up from sigwait by receiving an interrupt
996 -- (Abort_Task_Interrupt) from the Interrupt_Manager for unbinding
997 -- a Procedure Handler or an Entry. Or it could be a wake up
998 -- from status change (Unblocked -> Blocked). If that is not
999 -- the case, we should exceute the attached Procedure or Entry.
1005 POP.Write_Lock (Self_ID);
1007 if User_Handler (Interrupt).H = null
1008 and then User_Entry (Interrupt).T = Null_Task
1010 -- No Interrupt binding. If there is an interrupt,
1011 -- Interrupt_Manager will take default action.
1013 Self_ID.Common.State := Interrupt_Server_Idle_Sleep;
1014 POP.Sleep (Self_ID, Interrupt_Server_Idle_Sleep);
1015 Self_ID.Common.State := Runnable;
1018 Self_ID.Common.State := Interrupt_Server_Blocked_On_Event_Flag;
1019 Ret_Interrupt := IMOP.Interrupt_Wait (Intwait_Mask'Access);
1020 Self_ID.Common.State := Runnable;
1022 if not (Self_ID.Deferral_Level = 0
1023 and then Self_ID.Pending_ATC_Level
1024 < Self_ID.ATC_Nesting_Level)
1026 if User_Handler (Interrupt).H /= null then
1027 Tmp_Handler := User_Handler (Interrupt).H;
1029 -- RTS calls should not be made with self being locked.
1031 POP.Unlock (Self_ID);
1043 POP.Write_Lock (Self_ID);
1045 elsif User_Entry (Interrupt).T /= Null_Task then
1046 Tmp_ID := User_Entry (Interrupt).T;
1047 Tmp_Entry_Index := User_Entry (Interrupt).E;
1049 -- RTS calls should not be made with self being locked.
1051 POP.Unlock (Self_ID);
1057 System.Tasking.Rendezvous.Call_Simple
1058 (Tmp_ID, Tmp_Entry_Index, System.Null_Address);
1064 POP.Write_Lock (Self_ID);
1069 POP.Unlock (Self_ID);
1075 System.Tasking.Initialization.Undefer_Abort (Self_ID);
1077 -- Undefer abort here to allow a window for this task
1078 -- to be aborted at the time of system shutdown.
1082 -------------------------------------
1083 -- Has_Interrupt_Or_Attach_Handler --
1084 -------------------------------------
1086 function Has_Interrupt_Or_Attach_Handler
1087 (Object : access Dynamic_Interrupt_Protection)
1090 pragma Warnings (Off, Object);
1094 end Has_Interrupt_Or_Attach_Handler;
1100 procedure Finalize (Object : in out Static_Interrupt_Protection) is
1102 -- ??? loop to be executed only when we're not doing library level
1103 -- finalization, since in this case all interrupt tasks are gone.
1104 if not Interrupt_Manager'Terminated then
1105 for N in reverse Object.Previous_Handlers'Range loop
1106 Interrupt_Manager.Attach_Handler
1107 (New_Handler => Object.Previous_Handlers (N).Handler,
1108 Interrupt => Object.Previous_Handlers (N).Interrupt,
1109 Static => Object.Previous_Handlers (N).Static,
1110 Restoration => True);
1114 Tasking.Protected_Objects.Entries.Finalize
1115 (Tasking.Protected_Objects.Entries.Protection_Entries (Object));
1118 -------------------------------------
1119 -- Has_Interrupt_Or_Attach_Handler --
1120 -------------------------------------
1122 function Has_Interrupt_Or_Attach_Handler
1123 (Object : access Static_Interrupt_Protection)
1126 pragma Warnings (Off, Object);
1129 end Has_Interrupt_Or_Attach_Handler;
1131 ----------------------
1132 -- Install_Handlers --
1133 ----------------------
1135 procedure Install_Handlers
1136 (Object : access Static_Interrupt_Protection;
1137 New_Handlers : New_Handler_Array)
1140 for N in New_Handlers'Range loop
1142 -- We need a lock around this ???
1144 Object.Previous_Handlers (N).Interrupt := New_Handlers (N).Interrupt;
1145 Object.Previous_Handlers (N).Static := User_Handler
1146 (New_Handlers (N).Interrupt).Static;
1148 -- We call Exchange_Handler and not directly Interrupt_Manager.
1149 -- Exchange_Handler so we get the Is_Reserved check.
1152 (Old_Handler => Object.Previous_Handlers (N).Handler,
1153 New_Handler => New_Handlers (N).Handler,
1154 Interrupt => New_Handlers (N).Interrupt,
1157 end Install_Handlers;
1159 -- Elaboration code for package System.Interrupts
1162 -- Get Interrupt_Manager's ID so that Abort_Interrupt can be sent.
1164 Interrupt_Manager_ID := To_System (Interrupt_Manager'Identity);
1166 -- During the elaboration of this package body we want RTS to
1167 -- inherit the interrupt mask from the Environment Task.
1169 -- The Environment Task should have gotten its mask from
1170 -- the enclosing process during the RTS start up. (See
1171 -- in s-inmaop.adb). Pass the Interrupt_Mask of the Environment
1172 -- task to the Interrupt_Manager.
1174 -- Note : At this point we know that all tasks (including
1175 -- RTS internal servers) are masked for non-reserved signals
1176 -- (see s-taprop.adb). Only the Interrupt_Manager will have
1177 -- masks set up differently inheriting the original Environment
1180 Interrupt_Manager.Initialize (IMOP.Environment_Mask);
1181 end System.Interrupts;