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-2002, 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 PRI
renames System
.Task_Primitives
;
132 package POP
renames System
.Task_Primitives
.Operations
;
133 package PIO
renames System
.Task_Primitives
.Interrupt_Operations
;
134 package IMNG
renames System
.Interrupt_Management
;
135 package IMOP
renames System
.Interrupt_Management
.Operations
;
137 function To_System
is new Unchecked_Conversion
138 (Ada
.Task_Identification
.Task_Id
, Task_ID
);
144 -- WARNING: System.Tasking.Stages performs calls to this task
145 -- with low-level constructs. Do not change this spec without synchro-
148 task Interrupt_Manager
is
149 entry Detach_Interrupt_Entries
(T
: Task_ID
);
151 entry Initialize
(Mask
: IMNG
.Interrupt_Mask
);
154 (New_Handler
: in Parameterless_Handler
;
155 Interrupt
: in Interrupt_ID
;
157 Restoration
: in Boolean := False);
159 entry Exchange_Handler
160 (Old_Handler
: out Parameterless_Handler
;
161 New_Handler
: in Parameterless_Handler
;
162 Interrupt
: in Interrupt_ID
;
163 Static
: in Boolean);
166 (Interrupt
: in Interrupt_ID
;
167 Static
: in Boolean);
169 entry Bind_Interrupt_To_Entry
171 E
: Task_Entry_Index
;
172 Interrupt
: Interrupt_ID
);
174 entry Block_Interrupt
(Interrupt
: Interrupt_ID
);
176 entry Unblock_Interrupt
(Interrupt
: Interrupt_ID
);
178 entry Ignore_Interrupt
(Interrupt
: Interrupt_ID
);
180 entry Unignore_Interrupt
(Interrupt
: Interrupt_ID
);
182 pragma Interrupt_Priority
(System
.Interrupt_Priority
'Last);
183 end Interrupt_Manager
;
185 task type Server_Task
(Interrupt
: Interrupt_ID
) is
186 pragma Priority
(System
.Interrupt_Priority
'Last);
189 type Server_Task_Access
is access Server_Task
;
191 --------------------------------
192 -- Local Types and Variables --
193 --------------------------------
195 type Entry_Assoc
is record
197 E
: Task_Entry_Index
;
200 type Handler_Assoc
is record
201 H
: Parameterless_Handler
;
202 Static
: Boolean; -- Indicates static binding;
205 User_Handler
: array (Interrupt_ID
'Range) of Handler_Assoc
:=
206 (others => (null, Static
=> False));
207 pragma Volatile_Components
(User_Handler
);
208 -- Holds the protected procedure handler (if any) and its Static
209 -- information for each interrupt. A handler is a Static one if
210 -- it is specified through the pragma Attach_Handler.
211 -- Attach_Handler. Otherwise, not static)
213 User_Entry
: array (Interrupt_ID
'Range) of Entry_Assoc
:=
214 (others => (T
=> Null_Task
, E
=> Null_Task_Entry
));
215 pragma Volatile_Components
(User_Entry
);
216 -- Holds the task and entry index (if any) for each interrupt
218 Blocked
: array (Interrupt_ID
'Range) of Boolean := (others => False);
219 pragma Volatile_Components
(Blocked
);
220 -- True iff the corresponding interrupt is blocked in the process level
222 Ignored
: array (Interrupt_ID
'Range) of Boolean := (others => False);
223 pragma Volatile_Components
(Ignored
);
224 -- True iff the corresponding interrupt is blocked in the process level
227 array (Interrupt_ID
'Range) of Task_ID
:= (others => Null_Task
);
228 pragma Volatile_Components
(Last_Unblocker
);
229 -- Holds the ID of the last Task which Unblocked this Interrupt.
230 -- It contains Null_Task if no tasks have ever requested the
231 -- Unblocking operation or the Interrupt is currently Blocked.
233 Server_ID
: array (Interrupt_ID
'Range) of Task_ID
:=
234 (others => Null_Task
);
235 pragma Atomic_Components
(Server_ID
);
236 -- Holds the Task_ID of the Server_Task for each interrupt.
237 -- Task_ID is needed to accomplish locking per Interrupt base. Also
238 -- is needed to decide whether to create a new Server_Task.
240 -- Type and Head, Tail of the list containing Registered Interrupt
241 -- Handlers. These definitions are used to register the handlers
242 -- specified by the pragma Interrupt_Handler.
244 type Registered_Handler
;
245 type R_Link
is access all Registered_Handler
;
247 type Registered_Handler
is record
248 H
: System
.Address
:= System
.Null_Address
;
249 Next
: R_Link
:= null;
252 Registered_Handler_Head
: R_Link
:= null;
253 Registered_Handler_Tail
: R_Link
:= null;
255 Access_Hold
: Server_Task_Access
;
256 -- variable used to allocate Server_Task using "new".
258 -----------------------
259 -- Local Subprograms --
260 -----------------------
262 function Is_Registered
(Handler
: Parameterless_Handler
) return Boolean;
263 -- See if the Handler has been "pragma"ed using Interrupt_Handler.
264 -- Always consider a null handler as registered.
266 --------------------------------
267 -- Register_Interrupt_Handler --
268 --------------------------------
270 procedure Register_Interrupt_Handler
(Handler_Addr
: System
.Address
) is
271 New_Node_Ptr
: R_Link
;
273 -- This routine registers the Handler as usable for Dynamic
274 -- Interrupt Handler. Routines attaching and detaching Handler
275 -- dynamically should first consult if the Handler is rgistered.
276 -- A Program Error should be raised if it is not registered.
278 -- The pragma Interrupt_Handler can only appear in the library
279 -- level PO definition and instantiation. Therefore, we do not need
280 -- to implement Unregistering operation. Neither we need to
281 -- protect the queue structure using a Lock.
283 pragma Assert
(Handler_Addr
/= System
.Null_Address
);
285 New_Node_Ptr
:= new Registered_Handler
;
286 New_Node_Ptr
.H
:= Handler_Addr
;
288 if Registered_Handler_Head
= null then
289 Registered_Handler_Head
:= New_Node_Ptr
;
290 Registered_Handler_Tail
:= New_Node_Ptr
;
293 Registered_Handler_Tail
.Next
:= New_Node_Ptr
;
294 Registered_Handler_Tail
:= New_Node_Ptr
;
296 end Register_Interrupt_Handler
;
302 function Is_Registered
(Handler
: Parameterless_Handler
) return Boolean is
303 type Fat_Ptr
is record
304 Object_Addr
: System
.Address
;
305 Handler_Addr
: System
.Address
;
308 function To_Fat_Ptr
is new Unchecked_Conversion
309 (Parameterless_Handler
, Fat_Ptr
);
315 if Handler
= null then
319 Fat
:= To_Fat_Ptr
(Handler
);
321 Ptr
:= Registered_Handler_Head
;
323 while (Ptr
/= null) loop
324 if Ptr
.H
= Fat
.Handler_Addr
then
339 function Is_Reserved
(Interrupt
: Interrupt_ID
) return Boolean is
341 return IMNG
.Reserve
(IMNG
.Interrupt_ID
(Interrupt
));
344 -----------------------
345 -- Is_Entry_Attached --
346 -----------------------
348 function Is_Entry_Attached
(Interrupt
: Interrupt_ID
) return Boolean is
350 if Is_Reserved
(Interrupt
) then
351 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
352 Interrupt_ID
'Image (Interrupt
) & " is reserved");
355 return User_Entry
(Interrupt
).T
/= Null_Task
;
356 end Is_Entry_Attached
;
358 -------------------------
359 -- Is_Handler_Attached --
360 -------------------------
362 function Is_Handler_Attached
(Interrupt
: Interrupt_ID
) return Boolean is
364 if Is_Reserved
(Interrupt
) then
365 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
366 Interrupt_ID
'Image (Interrupt
) & " is reserved");
369 return User_Handler
(Interrupt
).H
/= null;
370 end Is_Handler_Attached
;
376 function Is_Blocked
(Interrupt
: Interrupt_ID
) return Boolean is
378 if Is_Reserved
(Interrupt
) then
379 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
380 Interrupt_ID
'Image (Interrupt
) & " is reserved");
383 return Blocked
(Interrupt
);
390 function Is_Ignored
(Interrupt
: Interrupt_ID
) return Boolean is
392 if Is_Reserved
(Interrupt
) then
393 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
394 Interrupt_ID
'Image (Interrupt
) & " is reserved");
397 return Ignored
(Interrupt
);
400 ---------------------
401 -- Current_Handler --
402 ---------------------
404 function Current_Handler
(Interrupt
: Interrupt_ID
)
405 return Parameterless_Handler
is
407 if Is_Reserved
(Interrupt
) then
408 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
409 Interrupt_ID
'Image (Interrupt
) & " is reserved");
412 -- ??? Since Parameterless_Handler is not Atomic, the
413 -- current implementation is wrong. We need a new service in
414 -- Interrupt_Manager to ensure atomicity.
416 return User_Handler
(Interrupt
).H
;
423 -- Calling this procedure with New_Handler = null and Static = True
424 -- means we want to detach the current handler regardless of the
425 -- previous handler's binding status (ie. do not care if it is a
426 -- dynamic or static handler).
428 -- This option is needed so that during the finalization of a PO, we
429 -- can detach handlers attached through pragma Attach_Handler.
431 procedure Attach_Handler
432 (New_Handler
: in Parameterless_Handler
;
433 Interrupt
: in Interrupt_ID
;
434 Static
: in Boolean := False) is
436 if Is_Reserved
(Interrupt
) then
437 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
438 Interrupt_ID
'Image (Interrupt
) & " is reserved");
441 Interrupt_Manager
.Attach_Handler
(New_Handler
, Interrupt
, Static
);
445 ----------------------
446 -- Exchange_Handler --
447 ----------------------
449 -- Calling this procedure with New_Handler = null and Static = True
450 -- means we want to detach the current handler regardless of the
451 -- previous handler's binding status (ie. do not care if it is a
452 -- dynamic or static handler).
454 -- This option is needed so that during the finalization of a PO, we
455 -- can detach handlers attached through pragma Attach_Handler.
457 procedure Exchange_Handler
458 (Old_Handler
: out Parameterless_Handler
;
459 New_Handler
: in Parameterless_Handler
;
460 Interrupt
: in Interrupt_ID
;
461 Static
: in Boolean := False) is
463 if Is_Reserved
(Interrupt
) then
464 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
465 Interrupt_ID
'Image (Interrupt
) & " is reserved");
468 Interrupt_Manager
.Exchange_Handler
469 (Old_Handler
, New_Handler
, Interrupt
, Static
);
471 end Exchange_Handler
;
477 -- Calling this procedure with Static = True means we want to Detach the
478 -- current handler regardless of the previous handler's binding status
479 -- (i.e. do not care if it is a dynamic or static handler).
481 -- This option is needed so that during the finalization of a PO, we can
482 -- detach handlers attached through pragma Attach_Handler.
484 procedure Detach_Handler
485 (Interrupt
: in Interrupt_ID
;
486 Static
: in Boolean := False) is
488 if Is_Reserved
(Interrupt
) then
489 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
490 Interrupt_ID
'Image (Interrupt
) & " is reserved");
493 Interrupt_Manager
.Detach_Handler
(Interrupt
, Static
);
500 function Reference
(Interrupt
: Interrupt_ID
) return System
.Address
is
502 if Is_Reserved
(Interrupt
) then
503 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
504 Interrupt_ID
'Image (Interrupt
) & " is reserved");
507 return Storage_Elements
.To_Address
508 (Storage_Elements
.Integer_Address
(Interrupt
));
511 -----------------------------
512 -- Bind_Interrupt_To_Entry --
513 -----------------------------
515 -- This procedure raises a Program_Error if it tries to
516 -- bind an interrupt to which an Entry or a Procedure is
519 procedure Bind_Interrupt_To_Entry
521 E
: Task_Entry_Index
;
522 Int_Ref
: System
.Address
)
524 Interrupt
: constant Interrupt_ID
:=
525 Interrupt_ID
(Storage_Elements
.To_Integer
(Int_Ref
));
528 if Is_Reserved
(Interrupt
) then
529 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
530 Interrupt_ID
'Image (Interrupt
) & " is reserved");
533 Interrupt_Manager
.Bind_Interrupt_To_Entry
(T
, E
, Interrupt
);
535 end Bind_Interrupt_To_Entry
;
537 ------------------------------
538 -- Detach_Interrupt_Entries --
539 ------------------------------
541 procedure Detach_Interrupt_Entries
(T
: Task_ID
) is
543 Interrupt_Manager
.Detach_Interrupt_Entries
(T
);
544 end Detach_Interrupt_Entries
;
546 ---------------------
547 -- Block_Interrupt --
548 ---------------------
550 procedure Block_Interrupt
(Interrupt
: Interrupt_ID
) is
552 if Is_Reserved
(Interrupt
) then
553 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
554 Interrupt_ID
'Image (Interrupt
) & " is reserved");
557 Interrupt_Manager
.Block_Interrupt
(Interrupt
);
560 -----------------------
561 -- Unblock_Interrupt --
562 -----------------------
564 procedure Unblock_Interrupt
(Interrupt
: Interrupt_ID
) is
566 if Is_Reserved
(Interrupt
) then
567 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
568 Interrupt_ID
'Image (Interrupt
) & " is reserved");
571 Interrupt_Manager
.Unblock_Interrupt
(Interrupt
);
572 end Unblock_Interrupt
;
578 function Unblocked_By
579 (Interrupt
: Interrupt_ID
) return System
.Tasking
.Task_ID
is
581 if Is_Reserved
(Interrupt
) then
582 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
583 Interrupt_ID
'Image (Interrupt
) & " is reserved");
586 return Last_Unblocker
(Interrupt
);
589 ----------------------
590 -- Ignore_Interrupt --
591 ----------------------
593 procedure Ignore_Interrupt
(Interrupt
: Interrupt_ID
) is
595 if Is_Reserved
(Interrupt
) then
596 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
597 Interrupt_ID
'Image (Interrupt
) & " is reserved");
600 Interrupt_Manager
.Ignore_Interrupt
(Interrupt
);
601 end Ignore_Interrupt
;
603 ------------------------
604 -- Unignore_Interrupt --
605 ------------------------
607 procedure Unignore_Interrupt
(Interrupt
: Interrupt_ID
) is
609 if Is_Reserved
(Interrupt
) then
610 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
611 Interrupt_ID
'Image (Interrupt
) & " is reserved");
614 Interrupt_Manager
.Unignore_Interrupt
(Interrupt
);
615 end Unignore_Interrupt
;
617 -----------------------
618 -- Interrupt_Manager --
619 -----------------------
621 task body Interrupt_Manager
is
623 ---------------------
624 -- Local Variables --
625 ---------------------
627 Intwait_Mask
: aliased IMNG
.Interrupt_Mask
;
628 Ret_Interrupt
: Interrupt_ID
;
629 Old_Mask
: aliased IMNG
.Interrupt_Mask
;
630 Self_ID
: Task_ID
:= POP
.Self
;
632 ---------------------
634 ---------------------
636 procedure Unprotected_Exchange_Handler
637 (Old_Handler
: out Parameterless_Handler
;
638 New_Handler
: in Parameterless_Handler
;
639 Interrupt
: in Interrupt_ID
;
641 Restoration
: in Boolean := False);
643 procedure Unprotected_Detach_Handler
644 (Interrupt
: in Interrupt_ID
;
645 Static
: in Boolean);
647 ----------------------------------
648 -- Unprotected_Exchange_Handler --
649 ----------------------------------
651 procedure Unprotected_Exchange_Handler
652 (Old_Handler
: out Parameterless_Handler
;
653 New_Handler
: in Parameterless_Handler
;
654 Interrupt
: in Interrupt_ID
;
656 Restoration
: in Boolean := False) is
658 if User_Entry
(Interrupt
).T
/= Null_Task
then
659 -- In case we have an Interrupt Entry already installed.
660 -- raise a program error. (propagate it to the caller).
662 Raise_Exception
(Program_Error
'Identity,
663 "An interrupt is already installed");
666 -- Note : A null handler with Static = True will
667 -- pass the following check. That is the case when we want to
668 -- Detach a handler regardless of the Static status
669 -- of the current_Handler.
670 -- We don't check anything if Restoration is True, since we
671 -- may be detaching a static handler to restore a dynamic one.
673 if not Restoration
and then not Static
674 -- Tries to overwrite a static Interrupt Handler with a
677 and then (User_Handler
(Interrupt
).Static
679 -- The new handler is not specified as an
680 -- Interrupt Handler by a pragma.
682 or else not Is_Registered
(New_Handler
))
684 Raise_Exception
(Program_Error
'Identity,
685 "Trying to overwrite a static Interrupt Handler with a " &
689 -- The interrupt should no longer be ingnored if
690 -- it was ever ignored.
692 Ignored
(Interrupt
) := False;
694 -- Save the old handler
696 Old_Handler
:= User_Handler
(Interrupt
).H
;
700 User_Handler
(Interrupt
).H
:= New_Handler
;
702 if New_Handler
= null then
704 -- The null handler means we are detaching the handler.
706 User_Handler
(Interrupt
).Static
:= False;
709 User_Handler
(Interrupt
).Static
:= Static
;
712 -- Invoke a corresponding Server_Task if not yet created.
713 -- Place Task_ID info in Server_ID array.
715 if Server_ID
(Interrupt
) = Null_Task
then
716 Access_Hold
:= new Server_Task
(Interrupt
);
717 Server_ID
(Interrupt
) := To_System
(Access_Hold
.all'Identity);
719 POP
.Wakeup
(Server_ID
(Interrupt
), Interrupt_Server_Idle_Sleep
);
722 end Unprotected_Exchange_Handler
;
724 --------------------------------
725 -- Unprotected_Detach_Handler --
726 --------------------------------
728 procedure Unprotected_Detach_Handler
729 (Interrupt
: in Interrupt_ID
;
732 Old_Handler
: Parameterless_Handler
;
735 if User_Entry
(Interrupt
).T
/= Null_Task
then
736 -- In case we have an Interrupt Entry installed.
737 -- raise a program error. (propagate it to the caller).
739 Raise_Exception
(Program_Error
'Identity,
740 "An interrupt entry is already installed");
743 -- Note : Static = True will pass the following check. That is the
744 -- case when we want to detach a handler regardless of the static
745 -- status of the current_Handler.
747 if not Static
and then User_Handler
(Interrupt
).Static
then
748 -- Tries to detach a static Interrupt Handler.
749 -- raise a program error.
751 Raise_Exception
(Program_Error
'Identity,
752 "Trying to detach a static Interrupt Handler");
755 -- The interrupt should no longer be ignored if
756 -- it was ever ignored.
758 Ignored
(Interrupt
) := False;
760 Old_Handler
:= User_Handler
(Interrupt
).H
;
764 User_Handler
(Interrupt
).H
:= null;
765 User_Handler
(Interrupt
).Static
:= False;
766 IMOP
.Interrupt_Self_Process
(IMNG
.Interrupt_ID
(Interrupt
));
768 end Unprotected_Detach_Handler
;
770 -- Start of processing for Interrupt_Manager
773 -- By making this task independent of master, when the process
774 -- goes away, the Interrupt_Manager will terminate gracefully.
776 System
.Tasking
.Utilities
.Make_Independent
;
778 -- Environmen task gets its own interrupt mask, saves it,
779 -- and then masks all interrupts except the Keep_Unmasked set.
781 -- During rendezvous, the Interrupt_Manager receives the old
782 -- interrupt mask of the environment task, and sets its own
783 -- interrupt mask to that value.
785 -- The environment task will call the entry of Interrupt_Manager some
786 -- during elaboration of the body of this package.
788 accept Initialize
(Mask
: IMNG
.Interrupt_Mask
) do
792 -- Note: All tasks in RTS will have all the Reserve Interrupts
793 -- being masked (except the Interrupt_Manager) and Keep_Unmasked
794 -- unmasked when created.
796 -- Abort_Task_Interrupt is one of the Interrupt unmasked
797 -- in all tasks. We mask the Interrupt in this particular task
798 -- so that "sigwait" is possible to catch an explicitly sent
799 -- Abort_Task_Interrupt from the Server_Tasks.
801 -- This sigwaiting is needed so that we make sure a Server_Task is
802 -- out of its own sigwait state. This extra synchronization is
803 -- necessary to prevent following senarios.
805 -- 1) Interrupt_Manager sends an Abort_Task_Interrupt to the
806 -- Server_Task then changes its own interrupt mask (OS level).
807 -- If an interrupt (corresponding to the Server_Task) arrives
808 -- in the nean time we have the Interrupt_Manager umnasked and
809 -- the Server_Task waiting on sigwait.
811 -- 2) For unbinding handler, we install a default action in the
812 -- Interrupt_Manager. POSIX.1c states that the result of using
813 -- "sigwait" and "sigaction" simaltaneously on the same interrupt
814 -- is undefined. Therefore, we need to be informed from the
815 -- Server_Task of the fact that the Server_Task is out of its
819 -- A block is needed to absorb Program_Error exception
822 Old_Handler
: Parameterless_Handler
;
826 accept Attach_Handler
827 (New_Handler
: in Parameterless_Handler
;
828 Interrupt
: in Interrupt_ID
;
830 Restoration
: in Boolean := False)
832 Unprotected_Exchange_Handler
833 (Old_Handler
, New_Handler
, Interrupt
, Static
, Restoration
);
836 or accept Exchange_Handler
837 (Old_Handler
: out Parameterless_Handler
;
838 New_Handler
: in Parameterless_Handler
;
839 Interrupt
: in Interrupt_ID
;
842 Unprotected_Exchange_Handler
843 (Old_Handler
, New_Handler
, Interrupt
, Static
);
844 end Exchange_Handler
;
846 or accept Detach_Handler
847 (Interrupt
: in Interrupt_ID
;
850 Unprotected_Detach_Handler
(Interrupt
, Static
);
853 or accept Bind_Interrupt_To_Entry
855 E
: Task_Entry_Index
;
856 Interrupt
: Interrupt_ID
)
858 -- if there is a binding already (either a procedure or an
859 -- entry), raise Program_Error (propagate it to the caller).
861 if User_Handler
(Interrupt
).H
/= null
862 or else User_Entry
(Interrupt
).T
/= Null_Task
864 Raise_Exception
(Program_Error
'Identity,
865 "A binding for this interrupt is already present");
868 -- The interrupt should no longer be ingnored if
869 -- it was ever ignored.
871 Ignored
(Interrupt
) := False;
872 User_Entry
(Interrupt
) := Entry_Assoc
' (T => T, E => E);
874 -- Indicate the attachment of Interrupt Entry in ATCB.
875 -- This is need so that when an Interrupt Entry task
876 -- terminates the binding can be cleaned.
877 -- The call to unbinding must be
878 -- make by the task before it terminates.
880 T.Interrupt_Entry := True;
882 -- Invoke a corresponding Server_Task if not yet created.
883 -- Place Task_ID info in Server_ID array.
885 if Server_ID (Interrupt) = Null_Task then
887 Access_Hold := new Server_Task (Interrupt);
888 Server_ID (Interrupt) :=
889 To_System (Access_Hold.all'Identity);
891 POP.Wakeup (Server_ID (Interrupt),
892 Interrupt_Server_Idle_Sleep);
894 end Bind_Interrupt_To_Entry;
896 or accept Detach_Interrupt_Entries (T : Task_ID)
898 for I in Interrupt_ID'Range loop
899 if not Is_Reserved (I) then
900 if User_Entry (I).T = T then
902 -- The interrupt should no longer be ignored if
903 -- it was ever ignored.
905 Ignored (I) := False;
906 User_Entry (I) := Entry_Assoc'
907 (T
=> Null_Task
, E
=> Null_Task_Entry
);
908 IMOP
.Interrupt_Self_Process
(IMNG
.Interrupt_ID
(I
));
913 -- Indicate in ATCB that no Interrupt Entries are attached.
915 T
.Interrupt_Entry
:= False;
916 end Detach_Interrupt_Entries
;
918 or accept Block_Interrupt
(Interrupt
: Interrupt_ID
) do
922 or accept Unblock_Interrupt
(Interrupt
: Interrupt_ID
) do
924 end Unblock_Interrupt
;
926 or accept Ignore_Interrupt
(Interrupt
: Interrupt_ID
) do
928 end Ignore_Interrupt
;
930 or accept Unignore_Interrupt
(Interrupt
: Interrupt_ID
) do
932 end Unignore_Interrupt
;
937 -- If there is a program error we just want to propagate it
938 -- to the caller and do not want to stop this task.
940 when Program_Error
=>
944 pragma Assert
(False);
948 end Interrupt_Manager
;
954 task body Server_Task
is
955 Self_ID
: Task_ID
:= Self
;
956 Tmp_Handler
: Parameterless_Handler
;
958 Tmp_Entry_Index
: Task_Entry_Index
;
959 Intwait_Mask
: aliased IMNG
.Interrupt_Mask
;
960 Ret_Interrupt
: IMNG
.Interrupt_ID
;
963 -- By making this task independent of master, when the process
964 -- goes away, the Server_Task will terminate gracefully.
966 System
.Tasking
.Utilities
.Make_Independent
;
968 -- Install default action in system level.
970 IMOP
.Install_Default_Action
(IMNG
.Interrupt_ID
(Interrupt
));
972 -- Set up the mask (also clears the event flag)
974 IMOP
.Empty_Interrupt_Mask
(Intwait_Mask
'Access);
975 IMOP
.Add_To_Interrupt_Mask
976 (Intwait_Mask
'Access, IMNG
.Interrupt_ID
(Interrupt
));
978 -- Remember the Interrupt_ID for Abort_Task.
980 PIO
.Set_Interrupt_ID
(IMNG
.Interrupt_ID
(Interrupt
), Self_ID
);
982 -- Note: All tasks in RTS will have all the Reserve Interrupts
983 -- being masked (except the Interrupt_Manager) and Keep_Unmasked
984 -- unmasked when created.
987 System
.Tasking
.Initialization
.Defer_Abort
(Self_ID
);
989 -- A Handler or an Entry is installed. At this point all tasks
990 -- mask for the Interrupt is masked. Catch the Interrupt using
993 -- This task may wake up from sigwait by receiving an interrupt
994 -- (Abort_Task_Interrupt) from the Interrupt_Manager for unbinding
995 -- a Procedure Handler or an Entry. Or it could be a wake up
996 -- from status change (Unblocked -> Blocked). If that is not
997 -- the case, we should exceute the attached Procedure or Entry.
1003 POP
.Write_Lock
(Self_ID
);
1005 if User_Handler
(Interrupt
).H
= null
1006 and then User_Entry
(Interrupt
).T
= Null_Task
1008 -- No Interrupt binding. If there is an interrupt,
1009 -- Interrupt_Manager will take default action.
1011 Self_ID
.Common
.State
:= Interrupt_Server_Idle_Sleep
;
1012 POP
.Sleep
(Self_ID
, Interrupt_Server_Idle_Sleep
);
1013 Self_ID
.Common
.State
:= Runnable
;
1016 Self_ID
.Common
.State
:= Interrupt_Server_Blocked_On_Event_Flag
;
1017 Ret_Interrupt
:= IMOP
.Interrupt_Wait
(Intwait_Mask
'Access);
1018 Self_ID
.Common
.State
:= Runnable
;
1020 if not (Self_ID
.Deferral_Level
= 0
1021 and then Self_ID
.Pending_ATC_Level
1022 < Self_ID
.ATC_Nesting_Level
)
1024 if User_Handler
(Interrupt
).H
/= null then
1025 Tmp_Handler
:= User_Handler
(Interrupt
).H
;
1027 -- RTS calls should not be made with self being locked.
1029 POP
.Unlock
(Self_ID
);
1036 POP
.Write_Lock
(Self_ID
);
1042 elsif User_Entry
(Interrupt
).T
/= Null_Task
then
1043 Tmp_ID
:= User_Entry
(Interrupt
).T
;
1044 Tmp_Entry_Index
:= User_Entry
(Interrupt
).E
;
1046 -- RTS calls should not be made with self being locked.
1048 POP
.Unlock
(Self_ID
);
1054 System
.Tasking
.Rendezvous
.Call_Simple
1055 (Tmp_ID
, Tmp_Entry_Index
, System
.Null_Address
);
1057 POP
.Write_Lock
(Self_ID
);
1066 POP
.Unlock
(Self_ID
);
1072 System
.Tasking
.Initialization
.Undefer_Abort
(Self_ID
);
1074 -- Undefer abort here to allow a window for this task
1075 -- to be aborted at the time of system shutdown.
1079 -------------------------------------
1080 -- Has_Interrupt_Or_Attach_Handler --
1081 -------------------------------------
1083 function Has_Interrupt_Or_Attach_Handler
1084 (Object
: access Dynamic_Interrupt_Protection
) return Boolean is
1087 end Has_Interrupt_Or_Attach_Handler
;
1093 procedure Finalize
(Object
: in out Static_Interrupt_Protection
) is
1095 -- ??? loop to be executed only when we're not doing library level
1096 -- finalization, since in this case all interrupt tasks are gone.
1097 if not Interrupt_Manager
'Terminated then
1098 for N
in reverse Object
.Previous_Handlers
'Range loop
1099 Interrupt_Manager
.Attach_Handler
1100 (New_Handler
=> Object
.Previous_Handlers
(N
).Handler
,
1101 Interrupt
=> Object
.Previous_Handlers
(N
).Interrupt
,
1102 Static
=> Object
.Previous_Handlers
(N
).Static
,
1103 Restoration
=> True);
1107 Tasking
.Protected_Objects
.Entries
.Finalize
1108 (Tasking
.Protected_Objects
.Entries
.Protection_Entries
(Object
));
1111 -------------------------------------
1112 -- Has_Interrupt_Or_Attach_Handler --
1113 -------------------------------------
1115 function Has_Interrupt_Or_Attach_Handler
1116 (Object
: access Static_Interrupt_Protection
)
1121 end Has_Interrupt_Or_Attach_Handler
;
1123 ----------------------
1124 -- Install_Handlers --
1125 ----------------------
1127 procedure Install_Handlers
1128 (Object
: access Static_Interrupt_Protection
;
1129 New_Handlers
: in New_Handler_Array
) is
1131 for N
in New_Handlers
'Range loop
1133 -- We need a lock around this ???
1135 Object
.Previous_Handlers
(N
).Interrupt
:= New_Handlers
(N
).Interrupt
;
1136 Object
.Previous_Handlers
(N
).Static
:= User_Handler
1137 (New_Handlers
(N
).Interrupt
).Static
;
1139 -- We call Exchange_Handler and not directly Interrupt_Manager.
1140 -- Exchange_Handler so we get the Is_Reserved check.
1143 (Old_Handler
=> Object
.Previous_Handlers
(N
).Handler
,
1144 New_Handler
=> New_Handlers
(N
).Handler
,
1145 Interrupt
=> New_Handlers
(N
).Interrupt
,
1148 end Install_Handlers
;
1150 -- Elaboration code for package System.Interrupts
1153 -- Get Interrupt_Manager's ID so that Abort_Interrupt can be sent.
1155 Interrupt_Manager_ID
:= To_System
(Interrupt_Manager
'Identity);
1157 -- During the elaboration of this package body we want RTS to
1158 -- inherit the interrupt mask from the Environment Task.
1160 -- The Environment Task should have gotten its mask from
1161 -- the enclosing process during the RTS start up. (See
1162 -- in s-inmaop.adb). Pass the Interrupt_Mask of the Environment
1163 -- task to the Interrupt_Manager.
1165 -- Note : At this point we know that all tasks (including
1166 -- RTS internal servers) are masked for non-reserved signals
1167 -- (see s-taprop.adb). Only the Interrupt_Manager will have
1168 -- masks set up differently inheriting the original Environment
1171 Interrupt_Manager
.Initialize
(IMOP
.Environment_Mask
);
1172 end System
.Interrupts
;