1 ------------------------------------------------------------------------------
3 -- GNAT 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-2009, 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 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. --
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. --
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/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
34 -- All user-handleable interrupts are masked at all times in all
35 -- tasks/threads except possibly for the Interrupt_Manager task.
37 -- When a user task wants to have the effect of masking/unmasking an
38 -- interrupt, it must call Block_Interrupt/Unblock_Interrupt, which
39 -- will have the effect of unmasking/masking the interrupt in the
40 -- Interrupt_Manager task.
42 -- Note : Direct calls to sigaction, sigprocmask, pthread_sigsetmask or any
43 -- other low-level interface that changes the interrupt action or
44 -- interrupt mask needs a careful thought.
45 -- One may achieve the effect of system calls first masking RTS blocked
46 -- (by calling Block_Interrupt) for the interrupt under consideration.
47 -- This will make all the tasks in RTS blocked for the Interrupt.
49 -- Once we associate a Server_Task with an interrupt, the task never
50 -- goes away, and we never remove the association.
52 -- There is no more than one interrupt per Server_Task and no more than
53 -- one Server_Task per interrupt.
55 with Ada
.Task_Identification
;
57 with System
.Task_Primitives
;
58 with System
.Interrupt_Management
;
60 with System
.Interrupt_Management
.Operations
;
61 pragma Elaborate_All
(System
.Interrupt_Management
.Operations
);
63 with System
.Task_Primitives
.Operations
;
64 with System
.Task_Primitives
.Interrupt_Operations
;
65 with System
.Storage_Elements
;
66 with System
.Tasking
.Utilities
;
68 with System
.Tasking
.Rendezvous
;
69 pragma Elaborate_All
(System
.Tasking
.Rendezvous
);
71 with System
.Tasking
.Initialization
;
72 with System
.Parameters
;
74 with Ada
.Unchecked_Conversion
;
76 package body System
.Interrupts
is
81 package POP
renames System
.Task_Primitives
.Operations
;
82 package PIO
renames System
.Task_Primitives
.Interrupt_Operations
;
83 package IMNG
renames System
.Interrupt_Management
;
84 package IMOP
renames System
.Interrupt_Management
.Operations
;
86 function To_System
is new Ada
.Unchecked_Conversion
87 (Ada
.Task_Identification
.Task_Id
, Task_Id
);
93 -- WARNING: System.Tasking.Stages performs calls to this task with
94 -- low-level constructs. Do not change this spec without synchronizing it.
96 task Interrupt_Manager
is
97 entry Detach_Interrupt_Entries
(T
: Task_Id
);
99 entry Initialize
(Mask
: IMNG
.Interrupt_Mask
);
102 (New_Handler
: Parameterless_Handler
;
103 Interrupt
: Interrupt_ID
;
105 Restoration
: Boolean := False);
107 entry Exchange_Handler
108 (Old_Handler
: out Parameterless_Handler
;
109 New_Handler
: Parameterless_Handler
;
110 Interrupt
: Interrupt_ID
;
114 (Interrupt
: Interrupt_ID
;
117 entry Bind_Interrupt_To_Entry
119 E
: Task_Entry_Index
;
120 Interrupt
: Interrupt_ID
);
122 entry Block_Interrupt
(Interrupt
: Interrupt_ID
);
124 entry Unblock_Interrupt
(Interrupt
: Interrupt_ID
);
126 entry Ignore_Interrupt
(Interrupt
: Interrupt_ID
);
128 entry Unignore_Interrupt
(Interrupt
: Interrupt_ID
);
130 pragma Interrupt_Priority
(System
.Interrupt_Priority
'Last);
131 end Interrupt_Manager
;
133 task type Server_Task
(Interrupt
: Interrupt_ID
) is
134 pragma Priority
(System
.Interrupt_Priority
'Last);
135 -- Note: the above pragma Priority is strictly speaking improper since
136 -- it is outside the range of allowed priorities, but the compiler
137 -- treats system units specially and does not apply this range checking
138 -- rule to system units.
142 type Server_Task_Access
is access Server_Task
;
144 -------------------------------
145 -- Local Types and Variables --
146 -------------------------------
148 type Entry_Assoc
is record
150 E
: Task_Entry_Index
;
153 type Handler_Assoc
is record
154 H
: Parameterless_Handler
;
155 Static
: Boolean; -- Indicates static binding;
158 User_Handler
: array (Interrupt_ID
'Range) of Handler_Assoc
:=
159 (others => (null, Static
=> False));
160 pragma Volatile_Components
(User_Handler
);
161 -- Holds the protected procedure handler (if any) and its Static
162 -- information for each interrupt. A handler is a Static one if it is
163 -- specified through the pragma Attach_Handler. Attach_Handler. Otherwise,
166 User_Entry
: array (Interrupt_ID
'Range) of Entry_Assoc
:=
167 (others => (T
=> Null_Task
, E
=> Null_Task_Entry
));
168 pragma Volatile_Components
(User_Entry
);
169 -- Holds the task and entry index (if any) for each interrupt
171 Blocked
: array (Interrupt_ID
'Range) of Boolean := (others => False);
172 pragma Atomic_Components
(Blocked
);
173 -- True iff the corresponding interrupt is blocked in the process level
175 Ignored
: array (Interrupt_ID
'Range) of Boolean := (others => False);
176 pragma Atomic_Components
(Ignored
);
177 -- True iff the corresponding interrupt is blocked in the process level
180 array (Interrupt_ID
'Range) of Task_Id
:= (others => Null_Task
);
181 pragma Atomic_Components
(Last_Unblocker
);
182 -- Holds the ID of the last Task which Unblocked this Interrupt. It
183 -- contains Null_Task if no tasks have ever requested the Unblocking
184 -- operation or the Interrupt is currently Blocked.
186 Server_ID
: array (Interrupt_ID
'Range) of Task_Id
:=
187 (others => Null_Task
);
188 pragma Atomic_Components
(Server_ID
);
189 -- Holds the Task_Id of the Server_Task for each interrupt. Task_Id is
190 -- needed to accomplish locking per Interrupt base. Also is needed to
191 -- decide whether to create a new Server_Task.
193 -- Type and Head, Tail of the list containing Registered Interrupt
194 -- Handlers. These definitions are used to register the handlers
195 -- specified by the pragma Interrupt_Handler.
197 type Registered_Handler
;
198 type R_Link
is access all Registered_Handler
;
200 type Registered_Handler
is record
201 H
: System
.Address
:= System
.Null_Address
;
202 Next
: R_Link
:= null;
205 Registered_Handler_Head
: R_Link
:= null;
206 Registered_Handler_Tail
: R_Link
:= null;
208 Access_Hold
: Server_Task_Access
;
209 -- Variable used to allocate Server_Task using "new"
211 -----------------------
212 -- Local Subprograms --
213 -----------------------
215 function Is_Registered
(Handler
: Parameterless_Handler
) return Boolean;
216 -- See if the Handler has been "pragma"ed using Interrupt_Handler. Always
217 -- consider a null handler as registered.
223 -- Calling this procedure with New_Handler = null and Static = True means
224 -- we want to detach the current handler regardless of the previous
225 -- handler's binding status (i.e. do not care if it is a dynamic or static
228 -- This option is needed so that during the finalization of a PO, we can
229 -- detach handlers attached through pragma Attach_Handler.
231 procedure Attach_Handler
232 (New_Handler
: Parameterless_Handler
;
233 Interrupt
: Interrupt_ID
;
234 Static
: Boolean := False)
237 if Is_Reserved
(Interrupt
) then
238 raise Program_Error
with
239 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
242 Interrupt_Manager
.Attach_Handler
(New_Handler
, Interrupt
, Static
);
246 -----------------------------
247 -- Bind_Interrupt_To_Entry --
248 -----------------------------
250 -- This procedure raises a Program_Error if it tries to bind an interrupt
251 -- to which an Entry or a Procedure is already bound.
253 procedure Bind_Interrupt_To_Entry
255 E
: Task_Entry_Index
;
256 Int_Ref
: System
.Address
)
258 Interrupt
: constant Interrupt_ID
:=
259 Interrupt_ID
(Storage_Elements
.To_Integer
(Int_Ref
));
262 if Is_Reserved
(Interrupt
) then
263 raise Program_Error
with
264 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
267 Interrupt_Manager
.Bind_Interrupt_To_Entry
(T
, E
, Interrupt
);
268 end Bind_Interrupt_To_Entry
;
270 ---------------------
271 -- Block_Interrupt --
272 ---------------------
274 procedure Block_Interrupt
(Interrupt
: Interrupt_ID
) is
276 if Is_Reserved
(Interrupt
) then
277 raise Program_Error
with
278 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
281 Interrupt_Manager
.Block_Interrupt
(Interrupt
);
284 ---------------------
285 -- Current_Handler --
286 ---------------------
288 function Current_Handler
289 (Interrupt
: Interrupt_ID
) return Parameterless_Handler
292 if Is_Reserved
(Interrupt
) then
293 raise Program_Error
with
294 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
297 -- ??? Since Parameterless_Handler is not Atomic, the current
298 -- implementation is wrong. We need a new service in Interrupt_Manager
299 -- to ensure atomicity.
301 return User_Handler
(Interrupt
).H
;
308 -- Calling this procedure with Static = True means we want to Detach the
309 -- current handler regardless of the previous handler's binding status
310 -- (i.e. do not care if it is a dynamic or static handler).
312 -- This option is needed so that during the finalization of a PO, we can
313 -- detach handlers attached through pragma Attach_Handler.
315 procedure Detach_Handler
316 (Interrupt
: Interrupt_ID
;
317 Static
: Boolean := False)
320 if Is_Reserved
(Interrupt
) then
321 raise Program_Error
with
322 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
325 Interrupt_Manager
.Detach_Handler
(Interrupt
, Static
);
328 ------------------------------
329 -- Detach_Interrupt_Entries --
330 ------------------------------
332 procedure Detach_Interrupt_Entries
(T
: Task_Id
) is
334 Interrupt_Manager
.Detach_Interrupt_Entries
(T
);
335 end Detach_Interrupt_Entries
;
337 ----------------------
338 -- Exchange_Handler --
339 ----------------------
341 -- Calling this procedure with New_Handler = null and Static = True means
342 -- we want to detach the current handler regardless of the previous
343 -- handler's binding status (i.e. do not care if it is a dynamic or static
346 -- This option is needed so that during the finalization of a PO, we can
347 -- detach handlers attached through pragma Attach_Handler.
349 procedure Exchange_Handler
350 (Old_Handler
: out Parameterless_Handler
;
351 New_Handler
: Parameterless_Handler
;
352 Interrupt
: Interrupt_ID
;
353 Static
: Boolean := False)
356 if Is_Reserved
(Interrupt
) then
357 raise Program_Error
with
358 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
361 Interrupt_Manager
.Exchange_Handler
362 (Old_Handler
, New_Handler
, Interrupt
, Static
);
363 end Exchange_Handler
;
369 procedure Finalize
(Object
: in out Static_Interrupt_Protection
) is
371 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
372 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
373 -- Get interrupt state for interrupt number Int. Defined in init.c
375 Default
: constant Character := 's';
376 -- 's' Interrupt_State pragma set state to System (use "default"
380 -- ??? loop to be executed only when we're not doing library level
381 -- finalization, since in this case all interrupt tasks are gone.
383 -- If the Abort_Task signal is set to system, it means that we cannot
384 -- reset interrupt handlers since this would require sending the abort
385 -- signal to the Server_Task
387 if not Interrupt_Manager
'Terminated
388 and then State
(System
.Interrupt_Management
.Abort_Task_Interrupt
)
391 for N
in reverse Object
.Previous_Handlers
'Range loop
392 Interrupt_Manager
.Attach_Handler
393 (New_Handler
=> Object
.Previous_Handlers
(N
).Handler
,
394 Interrupt
=> Object
.Previous_Handlers
(N
).Interrupt
,
395 Static
=> Object
.Previous_Handlers
(N
).Static
,
396 Restoration
=> True);
400 Tasking
.Protected_Objects
.Entries
.Finalize
401 (Tasking
.Protected_Objects
.Entries
.Protection_Entries
(Object
));
404 -------------------------------------
405 -- Has_Interrupt_Or_Attach_Handler --
406 -------------------------------------
408 -- Need comments as to why these always return True ???
410 function Has_Interrupt_Or_Attach_Handler
411 (Object
: access Dynamic_Interrupt_Protection
) return Boolean
413 pragma Unreferenced
(Object
);
416 end Has_Interrupt_Or_Attach_Handler
;
418 function Has_Interrupt_Or_Attach_Handler
419 (Object
: access Static_Interrupt_Protection
) return Boolean
421 pragma Unreferenced
(Object
);
424 end Has_Interrupt_Or_Attach_Handler
;
426 ----------------------
427 -- Ignore_Interrupt --
428 ----------------------
430 procedure Ignore_Interrupt
(Interrupt
: Interrupt_ID
) is
432 if Is_Reserved
(Interrupt
) then
433 raise Program_Error
with
434 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
437 Interrupt_Manager
.Ignore_Interrupt
(Interrupt
);
438 end Ignore_Interrupt
;
440 ----------------------
441 -- Install_Handlers --
442 ----------------------
444 procedure Install_Handlers
445 (Object
: access Static_Interrupt_Protection
;
446 New_Handlers
: New_Handler_Array
)
449 for N
in New_Handlers
'Range loop
451 -- We need a lock around this ???
453 Object
.Previous_Handlers
(N
).Interrupt
:= New_Handlers
(N
).Interrupt
;
454 Object
.Previous_Handlers
(N
).Static
:= User_Handler
455 (New_Handlers
(N
).Interrupt
).Static
;
457 -- We call Exchange_Handler and not directly Interrupt_Manager.
458 -- Exchange_Handler so we get the Is_Reserved check.
461 (Old_Handler
=> Object
.Previous_Handlers
(N
).Handler
,
462 New_Handler
=> New_Handlers
(N
).Handler
,
463 Interrupt
=> New_Handlers
(N
).Interrupt
,
466 end Install_Handlers
;
468 ---------------------------------
469 -- Install_Restricted_Handlers --
470 ---------------------------------
472 procedure Install_Restricted_Handlers
(Handlers
: New_Handler_Array
) is
474 for N
in Handlers
'Range loop
475 Attach_Handler
(Handlers
(N
).Handler
, Handlers
(N
).Interrupt
, True);
477 end Install_Restricted_Handlers
;
483 function Is_Blocked
(Interrupt
: Interrupt_ID
) return Boolean is
485 if Is_Reserved
(Interrupt
) then
486 raise Program_Error
with
487 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
490 return Blocked
(Interrupt
);
493 -----------------------
494 -- Is_Entry_Attached --
495 -----------------------
497 function Is_Entry_Attached
(Interrupt
: Interrupt_ID
) return Boolean is
499 if Is_Reserved
(Interrupt
) then
500 raise Program_Error
with
501 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
504 return User_Entry
(Interrupt
).T
/= Null_Task
;
505 end Is_Entry_Attached
;
507 -------------------------
508 -- Is_Handler_Attached --
509 -------------------------
511 function Is_Handler_Attached
(Interrupt
: Interrupt_ID
) return Boolean is
513 if Is_Reserved
(Interrupt
) then
514 raise Program_Error
with
515 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
518 return User_Handler
(Interrupt
).H
/= null;
519 end Is_Handler_Attached
;
525 function Is_Ignored
(Interrupt
: Interrupt_ID
) return Boolean is
527 if Is_Reserved
(Interrupt
) then
528 raise Program_Error
with
529 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
532 return Ignored
(Interrupt
);
539 function Is_Registered
(Handler
: Parameterless_Handler
) return Boolean is
541 type Fat_Ptr
is record
542 Object_Addr
: System
.Address
;
543 Handler_Addr
: System
.Address
;
546 function To_Fat_Ptr
is new Ada
.Unchecked_Conversion
547 (Parameterless_Handler
, Fat_Ptr
);
553 if Handler
= null then
557 Fat
:= To_Fat_Ptr
(Handler
);
559 Ptr
:= Registered_Handler_Head
;
561 while Ptr
/= null loop
562 if Ptr
.H
= Fat
.Handler_Addr
then
576 function Is_Reserved
(Interrupt
: Interrupt_ID
) return Boolean is
578 return IMNG
.Reserve
(IMNG
.Interrupt_ID
(Interrupt
));
585 function Reference
(Interrupt
: Interrupt_ID
) return System
.Address
is
587 if Is_Reserved
(Interrupt
) then
588 raise Program_Error
with
589 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
592 return Storage_Elements
.To_Address
593 (Storage_Elements
.Integer_Address
(Interrupt
));
596 ---------------------------------
597 -- Register_Interrupt_Handler --
598 ---------------------------------
600 procedure Register_Interrupt_Handler
(Handler_Addr
: System
.Address
) is
601 New_Node_Ptr
: R_Link
;
604 -- This routine registers the Handler as usable for Dynamic Interrupt
605 -- Handler. Routines attaching and detaching Handler dynamically should
606 -- first consult if the Handler is registered. A Program Error should
607 -- be raised if it is not registered.
609 -- The pragma Interrupt_Handler can only appear in the library level PO
610 -- definition and instantiation. Therefore, we do not need to implement
611 -- Unregistering operation. Neither we need to protect the queue
612 -- structure using a Lock.
614 pragma Assert
(Handler_Addr
/= System
.Null_Address
);
616 New_Node_Ptr
:= new Registered_Handler
;
617 New_Node_Ptr
.H
:= Handler_Addr
;
619 if Registered_Handler_Head
= null then
620 Registered_Handler_Head
:= New_Node_Ptr
;
621 Registered_Handler_Tail
:= New_Node_Ptr
;
624 Registered_Handler_Tail
.Next
:= New_Node_Ptr
;
625 Registered_Handler_Tail
:= New_Node_Ptr
;
627 end Register_Interrupt_Handler
;
629 -----------------------
630 -- Unblock_Interrupt --
631 -----------------------
633 procedure Unblock_Interrupt
(Interrupt
: Interrupt_ID
) is
635 if Is_Reserved
(Interrupt
) then
636 raise Program_Error
with
637 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
640 Interrupt_Manager
.Unblock_Interrupt
(Interrupt
);
641 end Unblock_Interrupt
;
647 function Unblocked_By
648 (Interrupt
: Interrupt_ID
) return System
.Tasking
.Task_Id
651 if Is_Reserved
(Interrupt
) then
652 raise Program_Error
with
653 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
656 return Last_Unblocker
(Interrupt
);
659 ------------------------
660 -- Unignore_Interrupt --
661 ------------------------
663 procedure Unignore_Interrupt
(Interrupt
: Interrupt_ID
) is
665 if Is_Reserved
(Interrupt
) then
666 raise Program_Error
with
667 "Interrupt" & Interrupt_ID
'Image (Interrupt
) & " is reserved";
670 Interrupt_Manager
.Unignore_Interrupt
(Interrupt
);
671 end Unignore_Interrupt
;
673 -----------------------
674 -- Interrupt_Manager --
675 -----------------------
677 task body Interrupt_Manager
is
679 ---------------------
680 -- Local Variables --
681 ---------------------
683 Intwait_Mask
: aliased IMNG
.Interrupt_Mask
;
684 Ret_Interrupt
: Interrupt_ID
;
685 Old_Mask
: aliased IMNG
.Interrupt_Mask
;
686 Old_Handler
: Parameterless_Handler
;
692 procedure Bind_Handler
(Interrupt
: Interrupt_ID
);
693 -- This procedure does not do anything if the Interrupt is blocked.
694 -- Otherwise, we have to interrupt Server_Task for status change through
697 procedure Unbind_Handler
(Interrupt
: Interrupt_ID
);
698 -- This procedure does not do anything if the Interrupt is blocked.
699 -- Otherwise, we have to interrupt Server_Task for status change
700 -- through abort interrupt.
702 procedure Unprotected_Exchange_Handler
703 (Old_Handler
: out Parameterless_Handler
;
704 New_Handler
: Parameterless_Handler
;
705 Interrupt
: Interrupt_ID
;
707 Restoration
: Boolean := False);
709 procedure Unprotected_Detach_Handler
710 (Interrupt
: Interrupt_ID
;
717 procedure Bind_Handler
(Interrupt
: Interrupt_ID
) is
719 if not Blocked
(Interrupt
) then
721 -- Mask this task for the given Interrupt so that all tasks
722 -- are masked for the Interrupt and the actual delivery of the
723 -- Interrupt will be caught using "sigwait" by the
724 -- corresponding Server_Task.
726 IMOP
.Thread_Block_Interrupt
(IMNG
.Interrupt_ID
(Interrupt
));
728 -- We have installed a Handler or an Entry before we called
729 -- this procedure. If the Handler Task is waiting to be awakened,
730 -- do it here. Otherwise, the interrupt will be discarded.
732 POP
.Wakeup
(Server_ID
(Interrupt
), Interrupt_Server_Idle_Sleep
);
740 procedure Unbind_Handler
(Interrupt
: Interrupt_ID
) is
741 Server
: System
.Tasking
.Task_Id
;
743 if not Blocked
(Interrupt
) then
744 -- Currently, there is a Handler or an Entry attached and
745 -- corresponding Server_Task is waiting on "sigwait."
746 -- We have to wake up the Server_Task and make it
747 -- wait on condition variable by sending an
748 -- Abort_Task_Interrupt
750 Server
:= Server_ID
(Interrupt
);
752 case Server
.Common
.State
is
753 when Interrupt_Server_Idle_Sleep |
754 Interrupt_Server_Blocked_Interrupt_Sleep
756 POP
.Wakeup
(Server
, Server
.Common
.State
);
758 when Interrupt_Server_Blocked_On_Event_Flag
=>
759 POP
.Abort_Task
(Server
);
761 -- Make sure corresponding Server_Task is out of its
762 -- own sigwait state.
765 Interrupt_ID
(IMOP
.Interrupt_Wait
(Intwait_Mask
'Access));
767 (Ret_Interrupt
= Interrupt_ID
(IMNG
.Abort_Task_Interrupt
));
773 pragma Assert
(False);
777 IMOP
.Install_Default_Action
(IMNG
.Interrupt_ID
(Interrupt
));
779 -- Unmake the Interrupt for this task in order to allow default
782 IMOP
.Thread_Unblock_Interrupt
(IMNG
.Interrupt_ID
(Interrupt
));
785 IMOP
.Install_Default_Action
(IMNG
.Interrupt_ID
(Interrupt
));
789 --------------------------------
790 -- Unprotected_Detach_Handler --
791 --------------------------------
793 procedure Unprotected_Detach_Handler
794 (Interrupt
: Interrupt_ID
;
797 Old_Handler
: Parameterless_Handler
;
800 if User_Entry
(Interrupt
).T
/= Null_Task
then
802 -- In case we have an Interrupt Entry installed.
803 -- raise a program error. (propagate it to the caller).
805 raise Program_Error
with
806 "An interrupt entry is already installed";
809 -- Note : Static = True will pass the following check. That is the
810 -- case when we want to detach a handler regardless of the static
811 -- status of the current_Handler.
813 if not Static
and then User_Handler
(Interrupt
).Static
then
815 -- Tries to detach a static Interrupt Handler.
816 -- raise a program error.
818 raise Program_Error
with
819 "Trying to detach a static Interrupt Handler";
822 -- The interrupt should no longer be ignored if
823 -- it was ever ignored.
825 Ignored
(Interrupt
) := False;
827 Old_Handler
:= User_Handler
(Interrupt
).H
;
831 User_Handler
(Interrupt
).H
:= null;
832 User_Handler
(Interrupt
).Static
:= False;
834 if Old_Handler
/= null then
835 Unbind_Handler
(Interrupt
);
837 end Unprotected_Detach_Handler
;
839 ----------------------------------
840 -- Unprotected_Exchange_Handler --
841 ----------------------------------
843 procedure Unprotected_Exchange_Handler
844 (Old_Handler
: out Parameterless_Handler
;
845 New_Handler
: Parameterless_Handler
;
846 Interrupt
: Interrupt_ID
;
848 Restoration
: Boolean := False)
851 if User_Entry
(Interrupt
).T
/= Null_Task
then
853 -- In case we have an Interrupt Entry already installed.
854 -- raise a program error. (propagate it to the caller).
856 raise Program_Error
with
857 "An interrupt is already installed";
860 -- Note : A null handler with Static = True will pass the
861 -- following check. That is the case when we want to Detach a
862 -- handler regardless of the Static status of the current_Handler.
864 -- We don't check anything if Restoration is True, since we
865 -- may be detaching a static handler to restore a dynamic one.
867 if not Restoration
and then not Static
869 -- Tries to overwrite a static Interrupt Handler with a
872 and then (User_Handler
(Interrupt
).Static
874 -- The new handler is not specified as an
875 -- Interrupt Handler by a pragma.
877 or else not Is_Registered
(New_Handler
))
879 raise Program_Error
with
880 "Trying to overwrite a static Interrupt Handler with a " &
884 -- The interrupt should no longer be ignored if
885 -- it was ever ignored.
887 Ignored
(Interrupt
) := False;
889 -- Save the old handler
891 Old_Handler
:= User_Handler
(Interrupt
).H
;
895 User_Handler
(Interrupt
).H
:= New_Handler
;
897 if New_Handler
= null then
899 -- The null handler means we are detaching the handler
901 User_Handler
(Interrupt
).Static
:= False;
904 User_Handler
(Interrupt
).Static
:= Static
;
907 -- Invoke a corresponding Server_Task if not yet created.
908 -- Place Task_Id info in Server_ID array.
910 if Server_ID
(Interrupt
) = Null_Task
then
912 -- When a new Server_Task is created, it should have its
913 -- signal mask set to the All_Tasks_Mask.
915 IMOP
.Set_Interrupt_Mask
916 (IMOP
.All_Tasks_Mask
'Access, Old_Mask
'Access);
917 Access_Hold
:= new Server_Task
(Interrupt
);
918 IMOP
.Set_Interrupt_Mask
(Old_Mask
'Access);
920 Server_ID
(Interrupt
) := To_System
(Access_Hold
.all'Identity);
923 if New_Handler
= null then
924 if Old_Handler
/= null then
925 Unbind_Handler
(Interrupt
);
931 if Old_Handler
= null then
932 Bind_Handler
(Interrupt
);
934 end Unprotected_Exchange_Handler
;
936 -- Start of processing for Interrupt_Manager
939 -- By making this task independent of master, when the process
940 -- goes away, the Interrupt_Manager will terminate gracefully.
942 System
.Tasking
.Utilities
.Make_Independent
;
944 -- Environment task gets its own interrupt mask, saves it,
945 -- and then masks all interrupts except the Keep_Unmasked set.
947 -- During rendezvous, the Interrupt_Manager receives the old
948 -- interrupt mask of the environment task, and sets its own
949 -- interrupt mask to that value.
951 -- The environment task will call the entry of Interrupt_Manager some
952 -- during elaboration of the body of this package.
954 accept Initialize
(Mask
: IMNG
.Interrupt_Mask
) do
956 The_Mask
: aliased IMNG
.Interrupt_Mask
;
959 IMOP
.Copy_Interrupt_Mask
(The_Mask
, Mask
);
960 IMOP
.Set_Interrupt_Mask
(The_Mask
'Access);
964 -- Note: All tasks in RTS will have all the Reserve Interrupts
965 -- being masked (except the Interrupt_Manager) and Keep_Unmasked
966 -- unmasked when created.
968 -- Abort_Task_Interrupt is one of the Interrupt unmasked
969 -- in all tasks. We mask the Interrupt in this particular task
970 -- so that "sigwait" is possible to catch an explicitly sent
971 -- Abort_Task_Interrupt from the Server_Tasks.
973 -- This sigwaiting is needed so that we make sure a Server_Task is
974 -- out of its own sigwait state. This extra synchronization is
975 -- necessary to prevent following scenarios.
977 -- 1) Interrupt_Manager sends an Abort_Task_Interrupt to the
978 -- Server_Task then changes its own interrupt mask (OS level).
979 -- If an interrupt (corresponding to the Server_Task) arrives
980 -- in the mean time we have the Interrupt_Manager unmasked and
981 -- the Server_Task waiting on sigwait.
983 -- 2) For unbinding handler, we install a default action in the
984 -- Interrupt_Manager. POSIX.1c states that the result of using
985 -- "sigwait" and "sigaction" simultaneously on the same interrupt
986 -- is undefined. Therefore, we need to be informed from the
987 -- Server_Task of the fact that the Server_Task is out of its
990 IMOP
.Empty_Interrupt_Mask
(Intwait_Mask
'Access);
991 IMOP
.Add_To_Interrupt_Mask
992 (Intwait_Mask
'Access, IMNG
.Abort_Task_Interrupt
);
993 IMOP
.Thread_Block_Interrupt
994 (IMNG
.Abort_Task_Interrupt
);
997 -- A block is needed to absorb Program_Error exception
1001 accept Attach_Handler
1002 (New_Handler
: Parameterless_Handler
;
1003 Interrupt
: Interrupt_ID
;
1005 Restoration
: Boolean := False)
1007 Unprotected_Exchange_Handler
1008 (Old_Handler
, New_Handler
, Interrupt
, Static
, Restoration
);
1012 accept Exchange_Handler
1013 (Old_Handler
: out Parameterless_Handler
;
1014 New_Handler
: Parameterless_Handler
;
1015 Interrupt
: Interrupt_ID
;
1018 Unprotected_Exchange_Handler
1019 (Old_Handler
, New_Handler
, Interrupt
, Static
);
1020 end Exchange_Handler
;
1023 accept Detach_Handler
1024 (Interrupt
: Interrupt_ID
;
1027 Unprotected_Detach_Handler
(Interrupt
, Static
);
1031 accept Bind_Interrupt_To_Entry
1033 E
: Task_Entry_Index
;
1034 Interrupt
: Interrupt_ID
)
1036 -- if there is a binding already (either a procedure or an
1037 -- entry), raise Program_Error (propagate it to the caller).
1039 if User_Handler
(Interrupt
).H
/= null
1040 or else User_Entry
(Interrupt
).T
/= Null_Task
1042 raise Program_Error
with
1043 "A binding for this interrupt is already present";
1046 -- The interrupt should no longer be ignored if
1047 -- it was ever ignored.
1049 Ignored
(Interrupt
) := False;
1050 User_Entry
(Interrupt
) := Entry_Assoc
'(T => T, E => E);
1052 -- Indicate the attachment of Interrupt Entry in ATCB.
1053 -- This is need so that when an Interrupt Entry task
1054 -- terminates the binding can be cleaned. The call to
1055 -- unbinding must be made by the task before it terminates.
1057 T.Interrupt_Entry := True;
1059 -- Invoke a corresponding Server_Task if not yet created.
1060 -- Place Task_Id info in Server_ID array.
1062 if Server_ID (Interrupt) = Null_Task then
1064 -- When a new Server_Task is created, it should have its
1065 -- signal mask set to the All_Tasks_Mask.
1067 IMOP.Set_Interrupt_Mask
1068 (IMOP.All_Tasks_Mask'Access, Old_Mask'Access);
1069 Access_Hold := new Server_Task (Interrupt);
1070 IMOP.Set_Interrupt_Mask (Old_Mask'Access);
1071 Server_ID (Interrupt) :=
1072 To_System (Access_Hold.all'Identity);
1075 Bind_Handler (Interrupt);
1076 end Bind_Interrupt_To_Entry;
1079 accept Detach_Interrupt_Entries (T : Task_Id) do
1080 for J in Interrupt_ID'Range loop
1081 if not Is_Reserved (J) then
1082 if User_Entry (J).T = T then
1084 -- The interrupt should no longer be ignored if
1085 -- it was ever ignored.
1087 Ignored (J) := False;
1088 User_Entry (J) := Entry_Assoc'
1089 (T
=> Null_Task
, E
=> Null_Task_Entry
);
1095 -- Indicate in ATCB that no Interrupt Entries are attached
1097 T
.Interrupt_Entry
:= False;
1098 end Detach_Interrupt_Entries
;
1101 accept Block_Interrupt
(Interrupt
: Interrupt_ID
) do
1102 if Blocked
(Interrupt
) then
1106 Blocked
(Interrupt
) := True;
1107 Last_Unblocker
(Interrupt
) := Null_Task
;
1109 -- Mask this task for the given Interrupt so that all tasks
1110 -- are masked for the Interrupt.
1112 IMOP
.Thread_Block_Interrupt
(IMNG
.Interrupt_ID
(Interrupt
));
1114 if User_Handler
(Interrupt
).H
/= null
1115 or else User_Entry
(Interrupt
).T
/= Null_Task
1117 -- This is the case where the Server_Task is waiting
1118 -- on "sigwait." Wake it up by sending an
1119 -- Abort_Task_Interrupt so that the Server_Task
1122 POP
.Abort_Task
(Server_ID
(Interrupt
));
1124 -- Make sure corresponding Server_Task is out of its own
1127 Ret_Interrupt
:= Interrupt_ID
1128 (IMOP
.Interrupt_Wait
(Intwait_Mask
'Access));
1131 Interrupt_ID
(IMNG
.Abort_Task_Interrupt
));
1133 end Block_Interrupt
;
1136 accept Unblock_Interrupt
(Interrupt
: Interrupt_ID
) do
1137 if not Blocked
(Interrupt
) then
1141 Blocked
(Interrupt
) := False;
1142 Last_Unblocker
(Interrupt
) :=
1143 To_System
(Unblock_Interrupt
'Caller);
1145 if User_Handler
(Interrupt
).H
= null
1146 and then User_Entry
(Interrupt
).T
= Null_Task
1148 -- No handler is attached. Unmask the Interrupt so that
1149 -- the default action can be carried out.
1151 IMOP
.Thread_Unblock_Interrupt
1152 (IMNG
.Interrupt_ID
(Interrupt
));
1155 -- The Server_Task must be waiting on the Cond variable
1156 -- since it was being blocked and an Interrupt Hander or
1157 -- an Entry was there. Wake it up and let it change
1158 -- it place of waiting according to its new state.
1160 POP
.Wakeup
(Server_ID
(Interrupt
),
1161 Interrupt_Server_Blocked_Interrupt_Sleep
);
1163 end Unblock_Interrupt
;
1166 accept Ignore_Interrupt
(Interrupt
: Interrupt_ID
) do
1167 if Ignored
(Interrupt
) then
1171 Ignored
(Interrupt
) := True;
1173 -- If there is a handler associated with the Interrupt,
1174 -- detach it first. In this way we make sure that the
1175 -- Server_Task is not on sigwait. This is legal since
1176 -- Unignore_Interrupt is to install the default action.
1178 if User_Handler
(Interrupt
).H
/= null then
1179 Unprotected_Detach_Handler
1180 (Interrupt
=> Interrupt
, Static
=> True);
1182 elsif User_Entry
(Interrupt
).T
/= Null_Task
then
1183 User_Entry
(Interrupt
) := Entry_Assoc
'
1184 (T => Null_Task, E => Null_Task_Entry);
1185 Unbind_Handler (Interrupt);
1188 IMOP.Install_Ignore_Action (IMNG.Interrupt_ID (Interrupt));
1189 end Ignore_Interrupt;
1192 accept Unignore_Interrupt (Interrupt : Interrupt_ID) do
1193 Ignored (Interrupt) := False;
1195 -- If there is a handler associated with the Interrupt,
1196 -- detach it first. In this way we make sure that the
1197 -- Server_Task is not on sigwait. This is legal since
1198 -- Unignore_Interrupt is to install the default action.
1200 if User_Handler (Interrupt).H /= null then
1201 Unprotected_Detach_Handler
1202 (Interrupt => Interrupt, Static => True);
1204 elsif User_Entry (Interrupt).T /= Null_Task then
1205 User_Entry (Interrupt) := Entry_Assoc'
1206 (T
=> Null_Task
, E
=> Null_Task_Entry
);
1207 Unbind_Handler
(Interrupt
);
1210 IMOP
.Install_Default_Action
(IMNG
.Interrupt_ID
(Interrupt
));
1211 end Unignore_Interrupt
;
1215 -- If there is a program error we just want to propagate it to
1216 -- the caller and do not want to stop this task.
1218 when Program_Error
=>
1222 pragma Assert
(False);
1226 end Interrupt_Manager
;
1232 task body Server_Task
is
1233 Intwait_Mask
: aliased IMNG
.Interrupt_Mask
;
1234 Ret_Interrupt
: Interrupt_ID
;
1235 Self_ID
: constant Task_Id
:= Self
;
1236 Tmp_Handler
: Parameterless_Handler
;
1238 Tmp_Entry_Index
: Task_Entry_Index
;
1241 -- By making this task independent of master, when the process
1242 -- goes away, the Server_Task will terminate gracefully.
1244 System
.Tasking
.Utilities
.Make_Independent
;
1246 -- Install default action in system level
1248 IMOP
.Install_Default_Action
(IMNG
.Interrupt_ID
(Interrupt
));
1250 -- Note: All tasks in RTS will have all the Reserve Interrupts being
1251 -- masked (except the Interrupt_Manager) and Keep_Unmasked unmasked when
1254 -- Abort_Task_Interrupt is one of the Interrupt unmasked in all tasks.
1255 -- We mask the Interrupt in this particular task so that "sigwait" is
1256 -- possible to catch an explicitly sent Abort_Task_Interrupt from the
1257 -- Interrupt_Manager.
1259 -- There are two Interrupt interrupts that this task catch through
1260 -- "sigwait." One is the Interrupt this task is designated to catch
1261 -- in order to execute user handler or entry. The other one is the
1262 -- Abort_Task_Interrupt. This interrupt is being sent from the
1263 -- Interrupt_Manager to inform status changes (e.g: become Blocked,
1264 -- Handler or Entry is to be detached).
1266 -- Prepare a mask to used for sigwait
1268 IMOP
.Empty_Interrupt_Mask
(Intwait_Mask
'Access);
1270 IMOP
.Add_To_Interrupt_Mask
1271 (Intwait_Mask
'Access, IMNG
.Interrupt_ID
(Interrupt
));
1273 IMOP
.Add_To_Interrupt_Mask
1274 (Intwait_Mask
'Access, IMNG
.Abort_Task_Interrupt
);
1276 IMOP
.Thread_Block_Interrupt
1277 (IMNG
.Abort_Task_Interrupt
);
1279 PIO
.Set_Interrupt_ID
(IMNG
.Interrupt_ID
(Interrupt
), Self_ID
);
1282 System
.Tasking
.Initialization
.Defer_Abort
(Self_ID
);
1288 POP
.Write_Lock
(Self_ID
);
1290 if User_Handler
(Interrupt
).H
= null
1291 and then User_Entry
(Interrupt
).T
= Null_Task
1293 -- No Interrupt binding. If there is an interrupt,
1294 -- Interrupt_Manager will take default action.
1296 Self_ID
.Common
.State
:= Interrupt_Server_Blocked_Interrupt_Sleep
;
1297 POP
.Sleep
(Self_ID
, Interrupt_Server_Idle_Sleep
);
1298 Self_ID
.Common
.State
:= Runnable
;
1300 elsif Blocked
(Interrupt
) then
1302 -- Interrupt is blocked. Stay here, so we won't catch
1305 Self_ID
.Common
.State
:= Interrupt_Server_Blocked_Interrupt_Sleep
;
1306 POP
.Sleep
(Self_ID
, Interrupt_Server_Blocked_Interrupt_Sleep
);
1307 Self_ID
.Common
.State
:= Runnable
;
1310 -- A Handler or an Entry is installed. At this point all tasks
1311 -- mask for the Interrupt is masked. Catch the Interrupt using
1314 -- This task may wake up from sigwait by receiving an interrupt
1315 -- (Abort_Task_Interrupt) from the Interrupt_Manager for unbinding
1316 -- a Procedure Handler or an Entry. Or it could be a wake up
1317 -- from status change (Unblocked -> Blocked). If that is not
1318 -- the case, we should execute the attached Procedure or Entry.
1320 Self_ID
.Common
.State
:= Interrupt_Server_Blocked_On_Event_Flag
;
1321 POP
.Unlock
(Self_ID
);
1327 -- Avoid race condition when terminating application and
1328 -- System.Parameters.No_Abort is True.
1330 if Parameters
.No_Abort
and then Self_ID
.Pending_Action
then
1331 Initialization
.Do_Pending_Action
(Self_ID
);
1335 Interrupt_ID
(IMOP
.Interrupt_Wait
(Intwait_Mask
'Access));
1336 Self_ID
.Common
.State
:= Runnable
;
1338 if Ret_Interrupt
= Interrupt_ID
(IMNG
.Abort_Task_Interrupt
) then
1340 -- Inform the Interrupt_Manager of wakeup from above sigwait
1342 POP
.Abort_Task
(Interrupt_Manager_ID
);
1348 POP
.Write_Lock
(Self_ID
);
1355 POP
.Write_Lock
(Self_ID
);
1357 if Ret_Interrupt
/= Interrupt
then
1359 -- On some systems (e.g. recent linux kernels), sigwait
1360 -- may return unexpectedly (with errno set to EINTR).
1365 -- Even though we have received an Interrupt the status may
1366 -- have changed already before we got the Self_ID lock above
1367 -- Therefore we make sure a Handler or an Entry is still
1368 -- there and make appropriate call.
1370 -- If there is no calls to make we need to regenerate the
1371 -- Interrupt in order not to lose it.
1373 if User_Handler
(Interrupt
).H
/= null then
1374 Tmp_Handler
:= User_Handler
(Interrupt
).H
;
1376 -- RTS calls should not be made with self being locked
1378 POP
.Unlock
(Self_ID
);
1390 POP
.Write_Lock
(Self_ID
);
1392 elsif User_Entry
(Interrupt
).T
/= Null_Task
then
1393 Tmp_ID
:= User_Entry
(Interrupt
).T
;
1394 Tmp_Entry_Index
:= User_Entry
(Interrupt
).E
;
1396 -- RTS calls should not be made with self being locked
1402 POP
.Unlock
(Self_ID
);
1404 System
.Tasking
.Rendezvous
.Call_Simple
1405 (Tmp_ID
, Tmp_Entry_Index
, System
.Null_Address
);
1407 POP
.Write_Lock
(Self_ID
);
1414 -- This is a situation that this task wakes up receiving
1415 -- an Interrupt and before it gets the lock the Interrupt
1416 -- is blocked. We do not want to lose the interrupt in
1417 -- this case so we regenerate the Interrupt to process
1420 IMOP
.Interrupt_Self_Process
1421 (IMNG
.Interrupt_ID
(Interrupt
));
1427 POP
.Unlock
(Self_ID
);
1433 System
.Tasking
.Initialization
.Undefer_Abort
(Self_ID
);
1435 if Self_ID
.Pending_Action
then
1436 Initialization
.Do_Pending_Action
(Self_ID
);
1439 -- Undefer abort here to allow a window for this task to be aborted
1440 -- at the time of system shutdown. We also explicitly test for
1441 -- Pending_Action in case System.Parameters.No_Abort is True.
1446 -- Elaboration code for package System.Interrupts
1449 -- Get Interrupt_Manager's ID so that Abort_Interrupt can be sent
1451 Interrupt_Manager_ID
:= To_System
(Interrupt_Manager
'Identity);
1453 -- During the elaboration of this package body we want the RTS
1454 -- to inherit the interrupt mask from the Environment Task.
1456 IMOP
.Setup_Interrupt_Mask
;
1458 -- The environment task should have gotten its mask from the enclosing
1459 -- process during the RTS start up. (See processing in s-inmaop.adb). Pass
1460 -- the Interrupt_Mask of the environment task to the Interrupt_Manager.
1462 -- Note: At this point we know that all tasks are masked for non-reserved
1463 -- signals. Only the Interrupt_Manager will have masks set up differently
1464 -- inheriting the original environment task's mask.
1466 Interrupt_Manager
.Initialize
(IMOP
.Environment_Mask
);
1467 end System
.Interrupts
;