2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / s-interr.adb
blob4cb2ccc57d4aaa400d4bb855bda778425c3d845d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . I N T E R R U P T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
10 -- --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
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. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- Invariants:
36 -- All user-handleable interrupts are masked at all times in all
37 -- tasks/threads except possibly for the Interrupt_Manager task.
39 -- When a user task wants to have the effect of masking/unmasking an
40 -- interrupt, it must call Block_Interrupt/Unblock_Interrupt, which
41 -- will have the effect of unmasking/masking the interrupt in the
42 -- Interrupt_Manager task.
44 -- Note : Direct calls to sigaction, sigprocmask, pthread_sigsetmask or any
45 -- other low-level interface that changes the interrupt action or
46 -- interrupt mask needs a careful thought.
47 -- One may achieve the effect of system calls first masking RTS blocked
48 -- (by calling Block_Interrupt) for the interrupt under consideration.
49 -- This will make all the tasks in RTS blocked for the Interrupt.
51 -- Once we associate a Server_Task with an interrupt, the task never
52 -- goes away, and we never remove the association.
54 -- There is no more than one interrupt per Server_Task and no more than
55 -- one Server_Task per interrupt.
57 with Ada.Task_Identification;
59 with System.Task_Primitives;
60 with System.Interrupt_Management;
62 with System.Interrupt_Management.Operations;
63 pragma Elaborate_All (System.Interrupt_Management.Operations);
65 with System.Task_Primitives.Operations;
66 with System.Task_Primitives.Interrupt_Operations;
67 with System.Storage_Elements;
68 with System.Tasking.Utilities;
70 with System.Tasking.Rendezvous;
71 pragma Elaborate_All (System.Tasking.Rendezvous);
73 with System.Tasking.Initialization;
74 with System.Parameters;
76 with Ada.Unchecked_Conversion;
78 package body System.Interrupts is
80 use Parameters;
81 use Tasking;
83 package POP renames System.Task_Primitives.Operations;
84 package PIO renames System.Task_Primitives.Interrupt_Operations;
85 package IMNG renames System.Interrupt_Management;
86 package IMOP renames System.Interrupt_Management.Operations;
88 function To_System is new Ada.Unchecked_Conversion
89 (Ada.Task_Identification.Task_Id, Task_Id);
91 -----------------
92 -- Local Tasks --
93 -----------------
95 -- WARNING: System.Tasking.Stages performs calls to this task with
96 -- low-level constructs. Do not change this spec without synchronizing it.
98 task Interrupt_Manager is
99 entry Detach_Interrupt_Entries (T : Task_Id);
101 entry Initialize (Mask : IMNG.Interrupt_Mask);
103 entry Attach_Handler
104 (New_Handler : Parameterless_Handler;
105 Interrupt : Interrupt_ID;
106 Static : Boolean;
107 Restoration : Boolean := False);
109 entry Exchange_Handler
110 (Old_Handler : out Parameterless_Handler;
111 New_Handler : Parameterless_Handler;
112 Interrupt : Interrupt_ID;
113 Static : Boolean);
115 entry Detach_Handler
116 (Interrupt : Interrupt_ID;
117 Static : Boolean);
119 entry Bind_Interrupt_To_Entry
120 (T : Task_Id;
121 E : Task_Entry_Index;
122 Interrupt : Interrupt_ID);
124 entry Block_Interrupt (Interrupt : Interrupt_ID);
126 entry Unblock_Interrupt (Interrupt : Interrupt_ID);
128 entry Ignore_Interrupt (Interrupt : Interrupt_ID);
130 entry Unignore_Interrupt (Interrupt : Interrupt_ID);
132 pragma Interrupt_Priority (System.Interrupt_Priority'Last);
133 end Interrupt_Manager;
135 task type Server_Task (Interrupt : Interrupt_ID) is
136 pragma Priority (System.Interrupt_Priority'Last);
137 -- Note: the above pragma Priority is strictly speaking improper since
138 -- it is outside the range of allowed priorities, but the compiler
139 -- treats system units specially and does not apply this range checking
140 -- rule to system units.
142 end Server_Task;
144 type Server_Task_Access is access Server_Task;
146 -------------------------------
147 -- Local Types and Variables --
148 -------------------------------
150 type Entry_Assoc is record
151 T : Task_Id;
152 E : Task_Entry_Index;
153 end record;
155 type Handler_Assoc is record
156 H : Parameterless_Handler;
157 Static : Boolean; -- Indicates static binding;
158 end record;
160 User_Handler : array (Interrupt_ID'Range) of Handler_Assoc :=
161 (others => (null, Static => False));
162 pragma Volatile_Components (User_Handler);
163 -- Holds the protected procedure handler (if any) and its Static
164 -- information for each interrupt. A handler is a Static one if it is
165 -- specified through the pragma Attach_Handler. Attach_Handler. Otherwise,
166 -- not static)
168 User_Entry : array (Interrupt_ID'Range) of Entry_Assoc :=
169 (others => (T => Null_Task, E => Null_Task_Entry));
170 pragma Volatile_Components (User_Entry);
171 -- Holds the task and entry index (if any) for each interrupt
173 Blocked : array (Interrupt_ID'Range) of Boolean := (others => False);
174 pragma Atomic_Components (Blocked);
175 -- True iff the corresponding interrupt is blocked in the process level
177 Ignored : array (Interrupt_ID'Range) of Boolean := (others => False);
178 pragma Atomic_Components (Ignored);
179 -- True iff the corresponding interrupt is blocked in the process level
181 Last_Unblocker :
182 array (Interrupt_ID'Range) of Task_Id := (others => Null_Task);
183 pragma Atomic_Components (Last_Unblocker);
184 -- Holds the ID of the last Task which Unblocked this Interrupt. It
185 -- contains Null_Task if no tasks have ever requested the Unblocking
186 -- operation or the Interrupt is currently Blocked.
188 Server_ID : array (Interrupt_ID'Range) of Task_Id :=
189 (others => Null_Task);
190 pragma Atomic_Components (Server_ID);
191 -- Holds the Task_Id of the Server_Task for each interrupt. Task_Id is
192 -- needed to accomplish locking per Interrupt base. Also is needed to
193 -- decide whether to create a new Server_Task.
195 -- Type and Head, Tail of the list containing Registered Interrupt
196 -- Handlers. These definitions are used to register the handlers
197 -- specified by the pragma Interrupt_Handler.
199 type Registered_Handler;
200 type R_Link is access all Registered_Handler;
202 type Registered_Handler is record
203 H : System.Address := System.Null_Address;
204 Next : R_Link := null;
205 end record;
207 Registered_Handler_Head : R_Link := null;
208 Registered_Handler_Tail : R_Link := null;
210 Access_Hold : Server_Task_Access;
211 -- Variable used to allocate Server_Task using "new"
213 -----------------------
214 -- Local Subprograms --
215 -----------------------
217 function Is_Registered (Handler : Parameterless_Handler) return Boolean;
218 -- See if the Handler has been "pragma"ed using Interrupt_Handler. Always
219 -- consider a null handler as registered.
221 --------------------
222 -- Attach_Handler --
223 --------------------
225 -- Calling this procedure with New_Handler = null and Static = True means
226 -- we want to detach the current handler regardless of the previous
227 -- handler's binding status (i.e. do not care if it is a dynamic or static
228 -- handler).
230 -- This option is needed so that during the finalization of a PO, we can
231 -- detach handlers attached through pragma Attach_Handler.
233 procedure Attach_Handler
234 (New_Handler : Parameterless_Handler;
235 Interrupt : Interrupt_ID;
236 Static : Boolean := False)
238 begin
239 if Is_Reserved (Interrupt) then
240 raise Program_Error with
241 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
242 end if;
244 Interrupt_Manager.Attach_Handler (New_Handler, Interrupt, Static);
246 end Attach_Handler;
248 -----------------------------
249 -- Bind_Interrupt_To_Entry --
250 -----------------------------
252 -- This procedure raises a Program_Error if it tries to bind an interrupt
253 -- to which an Entry or a Procedure is already bound.
255 procedure Bind_Interrupt_To_Entry
256 (T : Task_Id;
257 E : Task_Entry_Index;
258 Int_Ref : System.Address)
260 Interrupt : constant Interrupt_ID :=
261 Interrupt_ID (Storage_Elements.To_Integer (Int_Ref));
263 begin
264 if Is_Reserved (Interrupt) then
265 raise Program_Error with
266 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
267 end if;
269 Interrupt_Manager.Bind_Interrupt_To_Entry (T, E, Interrupt);
270 end Bind_Interrupt_To_Entry;
272 ---------------------
273 -- Block_Interrupt --
274 ---------------------
276 procedure Block_Interrupt (Interrupt : Interrupt_ID) is
277 begin
278 if Is_Reserved (Interrupt) then
279 raise Program_Error with
280 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
281 end if;
283 Interrupt_Manager.Block_Interrupt (Interrupt);
284 end Block_Interrupt;
286 ---------------------
287 -- Current_Handler --
288 ---------------------
290 function Current_Handler
291 (Interrupt : Interrupt_ID) return Parameterless_Handler
293 begin
294 if Is_Reserved (Interrupt) then
295 raise Program_Error with
296 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
297 end if;
299 -- ??? Since Parameterless_Handler is not Atomic, the current
300 -- implementation is wrong. We need a new service in Interrupt_Manager
301 -- to ensure atomicity.
303 return User_Handler (Interrupt).H;
304 end Current_Handler;
306 --------------------
307 -- Detach_Handler --
308 --------------------
310 -- Calling this procedure with Static = True means we want to Detach the
311 -- current handler regardless of the previous handler's binding status
312 -- (i.e. do not care if it is a dynamic or static handler).
314 -- This option is needed so that during the finalization of a PO, we can
315 -- detach handlers attached through pragma Attach_Handler.
317 procedure Detach_Handler
318 (Interrupt : Interrupt_ID;
319 Static : Boolean := False)
321 begin
322 if Is_Reserved (Interrupt) then
323 raise Program_Error with
324 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
325 end if;
327 Interrupt_Manager.Detach_Handler (Interrupt, Static);
328 end Detach_Handler;
330 ------------------------------
331 -- Detach_Interrupt_Entries --
332 ------------------------------
334 procedure Detach_Interrupt_Entries (T : Task_Id) is
335 begin
336 Interrupt_Manager.Detach_Interrupt_Entries (T);
337 end Detach_Interrupt_Entries;
339 ----------------------
340 -- Exchange_Handler --
341 ----------------------
343 -- Calling this procedure with New_Handler = null and Static = True means
344 -- we want to detach the current handler regardless of the previous
345 -- handler's binding status (i.e. do not care if it is a dynamic or static
346 -- handler).
348 -- This option is needed so that during the finalization of a PO, we can
349 -- detach handlers attached through pragma Attach_Handler.
351 procedure Exchange_Handler
352 (Old_Handler : out Parameterless_Handler;
353 New_Handler : Parameterless_Handler;
354 Interrupt : Interrupt_ID;
355 Static : Boolean := False)
357 begin
358 if Is_Reserved (Interrupt) then
359 raise Program_Error with
360 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
361 end if;
363 Interrupt_Manager.Exchange_Handler
364 (Old_Handler, New_Handler, Interrupt, Static);
365 end Exchange_Handler;
367 --------------
368 -- Finalize --
369 --------------
371 procedure Finalize (Object : in out Static_Interrupt_Protection) is
372 begin
373 -- ??? loop to be executed only when we're not doing library level
374 -- finalization, since in this case all interrupt tasks are gone.
376 if not Interrupt_Manager'Terminated then
377 for N in reverse Object.Previous_Handlers'Range loop
378 Interrupt_Manager.Attach_Handler
379 (New_Handler => Object.Previous_Handlers (N).Handler,
380 Interrupt => Object.Previous_Handlers (N).Interrupt,
381 Static => Object.Previous_Handlers (N).Static,
382 Restoration => True);
383 end loop;
384 end if;
386 Tasking.Protected_Objects.Entries.Finalize
387 (Tasking.Protected_Objects.Entries.Protection_Entries (Object));
388 end Finalize;
390 -------------------------------------
391 -- Has_Interrupt_Or_Attach_Handler --
392 -------------------------------------
394 -- Need comments as to why these always return True ???
396 function Has_Interrupt_Or_Attach_Handler
397 (Object : access Dynamic_Interrupt_Protection) return Boolean
399 pragma Unreferenced (Object);
400 begin
401 return True;
402 end Has_Interrupt_Or_Attach_Handler;
404 function Has_Interrupt_Or_Attach_Handler
405 (Object : access Static_Interrupt_Protection) return Boolean
407 pragma Unreferenced (Object);
408 begin
409 return True;
410 end Has_Interrupt_Or_Attach_Handler;
412 ----------------------
413 -- Ignore_Interrupt --
414 ----------------------
416 procedure Ignore_Interrupt (Interrupt : Interrupt_ID) is
417 begin
418 if Is_Reserved (Interrupt) then
419 raise Program_Error with
420 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
421 end if;
423 Interrupt_Manager.Ignore_Interrupt (Interrupt);
424 end Ignore_Interrupt;
426 ----------------------
427 -- Install_Handlers --
428 ----------------------
430 procedure Install_Handlers
431 (Object : access Static_Interrupt_Protection;
432 New_Handlers : New_Handler_Array)
434 begin
435 for N in New_Handlers'Range loop
437 -- We need a lock around this ???
439 Object.Previous_Handlers (N).Interrupt := New_Handlers (N).Interrupt;
440 Object.Previous_Handlers (N).Static := User_Handler
441 (New_Handlers (N).Interrupt).Static;
443 -- We call Exchange_Handler and not directly Interrupt_Manager.
444 -- Exchange_Handler so we get the Is_Reserved check.
446 Exchange_Handler
447 (Old_Handler => Object.Previous_Handlers (N).Handler,
448 New_Handler => New_Handlers (N).Handler,
449 Interrupt => New_Handlers (N).Interrupt,
450 Static => True);
451 end loop;
452 end Install_Handlers;
454 ---------------------------------
455 -- Install_Restricted_Handlers --
456 ---------------------------------
458 procedure Install_Restricted_Handlers (Handlers : New_Handler_Array) is
459 begin
460 for N in Handlers'Range loop
461 Attach_Handler (Handlers (N).Handler, Handlers (N).Interrupt, True);
462 end loop;
463 end Install_Restricted_Handlers;
465 ----------------
466 -- Is_Blocked --
467 ----------------
469 function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
470 begin
471 if Is_Reserved (Interrupt) then
472 raise Program_Error with
473 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
474 end if;
476 return Blocked (Interrupt);
477 end Is_Blocked;
479 -----------------------
480 -- Is_Entry_Attached --
481 -----------------------
483 function Is_Entry_Attached (Interrupt : Interrupt_ID) return Boolean is
484 begin
485 if Is_Reserved (Interrupt) then
486 raise Program_Error with
487 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
488 end if;
490 return User_Entry (Interrupt).T /= Null_Task;
491 end Is_Entry_Attached;
493 -------------------------
494 -- Is_Handler_Attached --
495 -------------------------
497 function Is_Handler_Attached (Interrupt : Interrupt_ID) return Boolean is
498 begin
499 if Is_Reserved (Interrupt) then
500 raise Program_Error with
501 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
502 end if;
504 return User_Handler (Interrupt).H /= null;
505 end Is_Handler_Attached;
507 ----------------
508 -- Is_Ignored --
509 ----------------
511 function Is_Ignored (Interrupt : Interrupt_ID) return Boolean is
512 begin
513 if Is_Reserved (Interrupt) then
514 raise Program_Error with
515 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
516 end if;
518 return Ignored (Interrupt);
519 end Is_Ignored;
521 -------------------
522 -- Is_Registered --
523 -------------------
525 function Is_Registered (Handler : Parameterless_Handler) return Boolean is
527 type Fat_Ptr is record
528 Object_Addr : System.Address;
529 Handler_Addr : System.Address;
530 end record;
532 function To_Fat_Ptr is new Ada.Unchecked_Conversion
533 (Parameterless_Handler, Fat_Ptr);
535 Ptr : R_Link;
536 Fat : Fat_Ptr;
538 begin
539 if Handler = null then
540 return True;
541 end if;
543 Fat := To_Fat_Ptr (Handler);
545 Ptr := Registered_Handler_Head;
547 while Ptr /= null loop
548 if Ptr.H = Fat.Handler_Addr then
549 return True;
550 end if;
552 Ptr := Ptr.Next;
553 end loop;
555 return False;
556 end Is_Registered;
558 -----------------
559 -- Is_Reserved --
560 -----------------
562 function Is_Reserved (Interrupt : Interrupt_ID) return Boolean is
563 begin
564 return IMNG.Reserve (IMNG.Interrupt_ID (Interrupt));
565 end Is_Reserved;
567 ---------------
568 -- Reference --
569 ---------------
571 function Reference (Interrupt : Interrupt_ID) return System.Address is
572 begin
573 if Is_Reserved (Interrupt) then
574 raise Program_Error with
575 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
576 end if;
578 return Storage_Elements.To_Address
579 (Storage_Elements.Integer_Address (Interrupt));
580 end Reference;
582 ---------------------------------
583 -- Register_Interrupt_Handler --
584 ---------------------------------
586 procedure Register_Interrupt_Handler (Handler_Addr : System.Address) is
587 New_Node_Ptr : R_Link;
589 begin
590 -- This routine registers the Handler as usable for Dynamic Interrupt
591 -- Handler. Routines attaching and detaching Handler dynamically should
592 -- first consult if the Handler is registered. A Program Error should
593 -- be raised if it is not registered.
595 -- The pragma Interrupt_Handler can only appear in the library level PO
596 -- definition and instantiation. Therefore, we do not need to implement
597 -- Unregistering operation. Neither we need to protect the queue
598 -- structure using a Lock.
600 pragma Assert (Handler_Addr /= System.Null_Address);
602 New_Node_Ptr := new Registered_Handler;
603 New_Node_Ptr.H := Handler_Addr;
605 if Registered_Handler_Head = null then
606 Registered_Handler_Head := New_Node_Ptr;
607 Registered_Handler_Tail := New_Node_Ptr;
609 else
610 Registered_Handler_Tail.Next := New_Node_Ptr;
611 Registered_Handler_Tail := New_Node_Ptr;
612 end if;
613 end Register_Interrupt_Handler;
615 -----------------------
616 -- Unblock_Interrupt --
617 -----------------------
619 procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
620 begin
621 if Is_Reserved (Interrupt) then
622 raise Program_Error with
623 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
624 end if;
626 Interrupt_Manager.Unblock_Interrupt (Interrupt);
627 end Unblock_Interrupt;
629 ------------------
630 -- Unblocked_By --
631 ------------------
633 function Unblocked_By
634 (Interrupt : Interrupt_ID) return System.Tasking.Task_Id
636 begin
637 if Is_Reserved (Interrupt) then
638 raise Program_Error with
639 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
640 end if;
642 return Last_Unblocker (Interrupt);
643 end Unblocked_By;
645 ------------------------
646 -- Unignore_Interrupt --
647 ------------------------
649 procedure Unignore_Interrupt (Interrupt : Interrupt_ID) is
650 begin
651 if Is_Reserved (Interrupt) then
652 raise Program_Error with
653 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
654 end if;
656 Interrupt_Manager.Unignore_Interrupt (Interrupt);
657 end Unignore_Interrupt;
659 -----------------------
660 -- Interrupt_Manager --
661 -----------------------
663 task body Interrupt_Manager is
665 ---------------------
666 -- Local Variables --
667 ---------------------
669 Intwait_Mask : aliased IMNG.Interrupt_Mask;
670 Ret_Interrupt : Interrupt_ID;
671 Old_Mask : aliased IMNG.Interrupt_Mask;
672 Old_Handler : Parameterless_Handler;
674 --------------------
675 -- Local Routines --
676 --------------------
678 procedure Bind_Handler (Interrupt : Interrupt_ID);
679 -- This procedure does not do anything if the Interrupt is blocked.
680 -- Otherwise, we have to interrupt Server_Task for status change through
681 -- Wakeup interrupt.
683 procedure Unbind_Handler (Interrupt : Interrupt_ID);
684 -- This procedure does not do anything if the Interrupt is blocked.
685 -- Otherwise, we have to interrupt Server_Task for status change
686 -- through abort interrupt.
688 procedure Unprotected_Exchange_Handler
689 (Old_Handler : out Parameterless_Handler;
690 New_Handler : Parameterless_Handler;
691 Interrupt : Interrupt_ID;
692 Static : Boolean;
693 Restoration : Boolean := False);
695 procedure Unprotected_Detach_Handler
696 (Interrupt : Interrupt_ID;
697 Static : Boolean);
699 ------------------
700 -- Bind_Handler --
701 ------------------
703 procedure Bind_Handler (Interrupt : Interrupt_ID) is
704 begin
705 if not Blocked (Interrupt) then
707 -- Mask this task for the given Interrupt so that all tasks
708 -- are masked for the Interrupt and the actual delivery of the
709 -- Interrupt will be caught using "sigwait" by the
710 -- corresponding Server_Task.
712 IMOP.Thread_Block_Interrupt (IMNG.Interrupt_ID (Interrupt));
714 -- We have installed a Handler or an Entry before we called
715 -- this procedure. If the Handler Task is waiting to be awakened,
716 -- do it here. Otherwise, the interrupt will be discarded.
718 POP.Wakeup (Server_ID (Interrupt), Interrupt_Server_Idle_Sleep);
719 end if;
720 end Bind_Handler;
722 --------------------
723 -- Unbind_Handler --
724 --------------------
726 procedure Unbind_Handler (Interrupt : Interrupt_ID) is
727 Server : System.Tasking.Task_Id;
728 begin
729 if not Blocked (Interrupt) then
730 -- Currently, there is a Handler or an Entry attached and
731 -- corresponding Server_Task is waiting on "sigwait."
732 -- We have to wake up the Server_Task and make it
733 -- wait on condition variable by sending an
734 -- Abort_Task_Interrupt
736 Server := Server_ID (Interrupt);
738 case Server.Common.State is
739 when Interrupt_Server_Idle_Sleep |
740 Interrupt_Server_Blocked_Interrupt_Sleep
742 POP.Wakeup (Server, Server.Common.State);
744 when Interrupt_Server_Blocked_On_Event_Flag =>
745 POP.Abort_Task (Server);
747 -- Make sure corresponding Server_Task is out of its
748 -- own sigwait state.
750 Ret_Interrupt :=
751 Interrupt_ID (IMOP.Interrupt_Wait (Intwait_Mask'Access));
752 pragma Assert
753 (Ret_Interrupt = Interrupt_ID (IMNG.Abort_Task_Interrupt));
755 when Runnable =>
756 null;
758 when others =>
759 pragma Assert (False);
760 null;
761 end case;
763 IMOP.Install_Default_Action (IMNG.Interrupt_ID (Interrupt));
765 -- Unmake the Interrupt for this task in order to allow default
766 -- action again.
768 IMOP.Thread_Unblock_Interrupt (IMNG.Interrupt_ID (Interrupt));
770 else
771 IMOP.Install_Default_Action (IMNG.Interrupt_ID (Interrupt));
772 end if;
773 end Unbind_Handler;
775 --------------------------------
776 -- Unprotected_Detach_Handler --
777 --------------------------------
779 procedure Unprotected_Detach_Handler
780 (Interrupt : Interrupt_ID;
781 Static : Boolean)
783 Old_Handler : Parameterless_Handler;
785 begin
786 if User_Entry (Interrupt).T /= Null_Task then
788 -- In case we have an Interrupt Entry installed.
789 -- raise a program error. (propagate it to the caller).
791 raise Program_Error with
792 "An interrupt entry is already installed";
793 end if;
795 -- Note : Static = True will pass the following check. That is the
796 -- case when we want to detach a handler regardless of the static
797 -- status of the current_Handler.
799 if not Static and then User_Handler (Interrupt).Static then
801 -- Tries to detach a static Interrupt Handler.
802 -- raise a program error.
804 raise Program_Error with
805 "Trying to detach a static Interrupt Handler";
806 end if;
808 -- The interrupt should no longer be ignored if
809 -- it was ever ignored.
811 Ignored (Interrupt) := False;
813 Old_Handler := User_Handler (Interrupt).H;
815 -- The new handler
817 User_Handler (Interrupt).H := null;
818 User_Handler (Interrupt).Static := False;
820 if Old_Handler /= null then
821 Unbind_Handler (Interrupt);
822 end if;
823 end Unprotected_Detach_Handler;
825 ----------------------------------
826 -- Unprotected_Exchange_Handler --
827 ----------------------------------
829 procedure Unprotected_Exchange_Handler
830 (Old_Handler : out Parameterless_Handler;
831 New_Handler : Parameterless_Handler;
832 Interrupt : Interrupt_ID;
833 Static : Boolean;
834 Restoration : Boolean := False)
836 begin
837 if User_Entry (Interrupt).T /= Null_Task then
839 -- In case we have an Interrupt Entry already installed.
840 -- raise a program error. (propagate it to the caller).
842 raise Program_Error with
843 "An interrupt is already installed";
844 end if;
846 -- Note : A null handler with Static = True will pass the
847 -- following check. That is the case when we want to Detach a
848 -- handler regardless of the Static status of the current_Handler.
850 -- We don't check anything if Restoration is True, since we
851 -- may be detaching a static handler to restore a dynamic one.
853 if not Restoration and then not Static
855 -- Tries to overwrite a static Interrupt Handler with a
856 -- dynamic Handler
858 and then (User_Handler (Interrupt).Static
860 -- The new handler is not specified as an
861 -- Interrupt Handler by a pragma.
863 or else not Is_Registered (New_Handler))
864 then
865 raise Program_Error with
866 "Trying to overwrite a static Interrupt Handler with a " &
867 "dynamic Handler";
868 end if;
870 -- The interrupt should no longer be ignored if
871 -- it was ever ignored.
873 Ignored (Interrupt) := False;
875 -- Save the old handler
877 Old_Handler := User_Handler (Interrupt).H;
879 -- The new handler
881 User_Handler (Interrupt).H := New_Handler;
883 if New_Handler = null then
885 -- The null handler means we are detaching the handler
887 User_Handler (Interrupt).Static := False;
889 else
890 User_Handler (Interrupt).Static := Static;
891 end if;
893 -- Invoke a corresponding Server_Task if not yet created.
894 -- Place Task_Id info in Server_ID array.
896 if Server_ID (Interrupt) = Null_Task then
898 -- When a new Server_Task is created, it should have its
899 -- signal mask set to the All_Tasks_Mask.
901 IMOP.Set_Interrupt_Mask
902 (IMOP.All_Tasks_Mask'Access, Old_Mask'Access);
903 Access_Hold := new Server_Task (Interrupt);
904 IMOP.Set_Interrupt_Mask (Old_Mask'Access);
906 Server_ID (Interrupt) := To_System (Access_Hold.all'Identity);
907 end if;
909 if New_Handler = null then
910 if Old_Handler /= null then
911 Unbind_Handler (Interrupt);
912 end if;
914 return;
915 end if;
917 if Old_Handler = null then
918 Bind_Handler (Interrupt);
919 end if;
920 end Unprotected_Exchange_Handler;
922 -- Start of processing for Interrupt_Manager
924 begin
925 -- By making this task independent of master, when the process
926 -- goes away, the Interrupt_Manager will terminate gracefully.
928 System.Tasking.Utilities.Make_Independent;
930 -- Environment task gets its own interrupt mask, saves it,
931 -- and then masks all interrupts except the Keep_Unmasked set.
933 -- During rendezvous, the Interrupt_Manager receives the old
934 -- interrupt mask of the environment task, and sets its own
935 -- interrupt mask to that value.
937 -- The environment task will call the entry of Interrupt_Manager some
938 -- during elaboration of the body of this package.
940 accept Initialize (Mask : IMNG.Interrupt_Mask) do
941 declare
942 The_Mask : aliased IMNG.Interrupt_Mask;
944 begin
945 IMOP.Copy_Interrupt_Mask (The_Mask, Mask);
946 IMOP.Set_Interrupt_Mask (The_Mask'Access);
947 end;
948 end Initialize;
950 -- Note: All tasks in RTS will have all the Reserve Interrupts
951 -- being masked (except the Interrupt_Manager) and Keep_Unmasked
952 -- unmasked when created.
954 -- Abort_Task_Interrupt is one of the Interrupt unmasked
955 -- in all tasks. We mask the Interrupt in this particular task
956 -- so that "sigwait" is possible to catch an explicitly sent
957 -- Abort_Task_Interrupt from the Server_Tasks.
959 -- This sigwaiting is needed so that we make sure a Server_Task is
960 -- out of its own sigwait state. This extra synchronization is
961 -- necessary to prevent following scenarios.
963 -- 1) Interrupt_Manager sends an Abort_Task_Interrupt to the
964 -- Server_Task then changes its own interrupt mask (OS level).
965 -- If an interrupt (corresponding to the Server_Task) arrives
966 -- in the mean time we have the Interrupt_Manager unmasked and
967 -- the Server_Task waiting on sigwait.
969 -- 2) For unbinding handler, we install a default action in the
970 -- Interrupt_Manager. POSIX.1c states that the result of using
971 -- "sigwait" and "sigaction" simultaneously on the same interrupt
972 -- is undefined. Therefore, we need to be informed from the
973 -- Server_Task of the fact that the Server_Task is out of its
974 -- sigwait stage.
976 IMOP.Empty_Interrupt_Mask (Intwait_Mask'Access);
977 IMOP.Add_To_Interrupt_Mask
978 (Intwait_Mask'Access, IMNG.Abort_Task_Interrupt);
979 IMOP.Thread_Block_Interrupt
980 (IMNG.Abort_Task_Interrupt);
982 loop
983 -- A block is needed to absorb Program_Error exception
985 begin
986 select
987 accept Attach_Handler
988 (New_Handler : Parameterless_Handler;
989 Interrupt : Interrupt_ID;
990 Static : Boolean;
991 Restoration : Boolean := False)
993 Unprotected_Exchange_Handler
994 (Old_Handler, New_Handler, Interrupt, Static, Restoration);
995 end Attach_Handler;
998 accept Exchange_Handler
999 (Old_Handler : out Parameterless_Handler;
1000 New_Handler : Parameterless_Handler;
1001 Interrupt : Interrupt_ID;
1002 Static : Boolean)
1004 Unprotected_Exchange_Handler
1005 (Old_Handler, New_Handler, Interrupt, Static);
1006 end Exchange_Handler;
1009 accept Detach_Handler
1010 (Interrupt : Interrupt_ID;
1011 Static : Boolean)
1013 Unprotected_Detach_Handler (Interrupt, Static);
1014 end Detach_Handler;
1017 accept Bind_Interrupt_To_Entry
1018 (T : Task_Id;
1019 E : Task_Entry_Index;
1020 Interrupt : Interrupt_ID)
1022 -- if there is a binding already (either a procedure or an
1023 -- entry), raise Program_Error (propagate it to the caller).
1025 if User_Handler (Interrupt).H /= null
1026 or else User_Entry (Interrupt).T /= Null_Task
1027 then
1028 raise Program_Error with
1029 "A binding for this interrupt is already present";
1030 end if;
1032 -- The interrupt should no longer be ignored if
1033 -- it was ever ignored.
1035 Ignored (Interrupt) := False;
1036 User_Entry (Interrupt) := Entry_Assoc'(T => T, E => E);
1038 -- Indicate the attachment of Interrupt Entry in ATCB.
1039 -- This is need so that when an Interrupt Entry task
1040 -- terminates the binding can be cleaned. The call to
1041 -- unbinding must be made by the task before it terminates.
1043 T.Interrupt_Entry := True;
1045 -- Invoke a corresponding Server_Task if not yet created.
1046 -- Place Task_Id info in Server_ID array.
1048 if Server_ID (Interrupt) = Null_Task then
1050 -- When a new Server_Task is created, it should have its
1051 -- signal mask set to the All_Tasks_Mask.
1053 IMOP.Set_Interrupt_Mask
1054 (IMOP.All_Tasks_Mask'Access, Old_Mask'Access);
1055 Access_Hold := new Server_Task (Interrupt);
1056 IMOP.Set_Interrupt_Mask (Old_Mask'Access);
1057 Server_ID (Interrupt) :=
1058 To_System (Access_Hold.all'Identity);
1059 end if;
1061 Bind_Handler (Interrupt);
1062 end Bind_Interrupt_To_Entry;
1065 accept Detach_Interrupt_Entries (T : Task_Id) do
1066 for J in Interrupt_ID'Range loop
1067 if not Is_Reserved (J) then
1068 if User_Entry (J).T = T then
1070 -- The interrupt should no longer be ignored if
1071 -- it was ever ignored.
1073 Ignored (J) := False;
1074 User_Entry (J) := Entry_Assoc'
1075 (T => Null_Task, E => Null_Task_Entry);
1076 Unbind_Handler (J);
1077 end if;
1078 end if;
1079 end loop;
1081 -- Indicate in ATCB that no Interrupt Entries are attached
1083 T.Interrupt_Entry := False;
1084 end Detach_Interrupt_Entries;
1087 accept Block_Interrupt (Interrupt : Interrupt_ID) do
1088 if Blocked (Interrupt) then
1089 return;
1090 end if;
1092 Blocked (Interrupt) := True;
1093 Last_Unblocker (Interrupt) := Null_Task;
1095 -- Mask this task for the given Interrupt so that all tasks
1096 -- are masked for the Interrupt.
1098 IMOP.Thread_Block_Interrupt (IMNG.Interrupt_ID (Interrupt));
1100 if User_Handler (Interrupt).H /= null
1101 or else User_Entry (Interrupt).T /= Null_Task
1102 then
1103 -- This is the case where the Server_Task is waiting
1104 -- on "sigwait." Wake it up by sending an
1105 -- Abort_Task_Interrupt so that the Server_Task
1106 -- waits on Cond.
1108 POP.Abort_Task (Server_ID (Interrupt));
1110 -- Make sure corresponding Server_Task is out of its own
1111 -- sigwait state.
1113 Ret_Interrupt := Interrupt_ID
1114 (IMOP.Interrupt_Wait (Intwait_Mask'Access));
1115 pragma Assert
1116 (Ret_Interrupt =
1117 Interrupt_ID (IMNG.Abort_Task_Interrupt));
1118 end if;
1119 end Block_Interrupt;
1122 accept Unblock_Interrupt (Interrupt : Interrupt_ID) do
1123 if not Blocked (Interrupt) then
1124 return;
1125 end if;
1127 Blocked (Interrupt) := False;
1128 Last_Unblocker (Interrupt) :=
1129 To_System (Unblock_Interrupt'Caller);
1131 if User_Handler (Interrupt).H = null
1132 and then User_Entry (Interrupt).T = Null_Task
1133 then
1134 -- No handler is attached. Unmask the Interrupt so that
1135 -- the default action can be carried out.
1137 IMOP.Thread_Unblock_Interrupt
1138 (IMNG.Interrupt_ID (Interrupt));
1140 else
1141 -- The Server_Task must be waiting on the Cond variable
1142 -- since it was being blocked and an Interrupt Hander or
1143 -- an Entry was there. Wake it up and let it change
1144 -- it place of waiting according to its new state.
1146 POP.Wakeup (Server_ID (Interrupt),
1147 Interrupt_Server_Blocked_Interrupt_Sleep);
1148 end if;
1149 end Unblock_Interrupt;
1152 accept Ignore_Interrupt (Interrupt : Interrupt_ID) do
1153 if Ignored (Interrupt) then
1154 return;
1155 end if;
1157 Ignored (Interrupt) := True;
1159 -- If there is a handler associated with the Interrupt,
1160 -- detach it first. In this way we make sure that the
1161 -- Server_Task is not on sigwait. This is legal since
1162 -- Unignore_Interrupt is to install the default action.
1164 if User_Handler (Interrupt).H /= null then
1165 Unprotected_Detach_Handler
1166 (Interrupt => Interrupt, Static => True);
1168 elsif User_Entry (Interrupt).T /= Null_Task then
1169 User_Entry (Interrupt) := Entry_Assoc'
1170 (T => Null_Task, E => Null_Task_Entry);
1171 Unbind_Handler (Interrupt);
1172 end if;
1174 IMOP.Install_Ignore_Action (IMNG.Interrupt_ID (Interrupt));
1175 end Ignore_Interrupt;
1178 accept Unignore_Interrupt (Interrupt : Interrupt_ID) do
1179 Ignored (Interrupt) := False;
1181 -- If there is a handler associated with the Interrupt,
1182 -- detach it first. In this way we make sure that the
1183 -- Server_Task is not on sigwait. This is legal since
1184 -- Unignore_Interrupt is to install the default action.
1186 if User_Handler (Interrupt).H /= null then
1187 Unprotected_Detach_Handler
1188 (Interrupt => Interrupt, Static => True);
1190 elsif User_Entry (Interrupt).T /= Null_Task then
1191 User_Entry (Interrupt) := Entry_Assoc'
1192 (T => Null_Task, E => Null_Task_Entry);
1193 Unbind_Handler (Interrupt);
1194 end if;
1196 IMOP.Install_Default_Action (IMNG.Interrupt_ID (Interrupt));
1197 end Unignore_Interrupt;
1198 end select;
1200 exception
1201 -- If there is a program error we just want to propagate it to
1202 -- the caller and do not want to stop this task.
1204 when Program_Error =>
1205 null;
1207 when others =>
1208 pragma Assert (False);
1209 null;
1210 end;
1211 end loop;
1212 end Interrupt_Manager;
1214 -----------------
1215 -- Server_Task --
1216 -----------------
1218 task body Server_Task is
1219 Intwait_Mask : aliased IMNG.Interrupt_Mask;
1220 Ret_Interrupt : Interrupt_ID;
1221 Self_ID : constant Task_Id := Self;
1222 Tmp_Handler : Parameterless_Handler;
1223 Tmp_ID : Task_Id;
1224 Tmp_Entry_Index : Task_Entry_Index;
1226 begin
1227 -- By making this task independent of master, when the process
1228 -- goes away, the Server_Task will terminate gracefully.
1230 System.Tasking.Utilities.Make_Independent;
1232 -- Install default action in system level
1234 IMOP.Install_Default_Action (IMNG.Interrupt_ID (Interrupt));
1236 -- Note: All tasks in RTS will have all the Reserve Interrupts being
1237 -- masked (except the Interrupt_Manager) and Keep_Unmasked unmasked when
1238 -- created.
1240 -- Abort_Task_Interrupt is one of the Interrupt unmasked in all tasks.
1241 -- We mask the Interrupt in this particular task so that "sigwait" is
1242 -- possible to catch an explicitly sent Abort_Task_Interrupt from the
1243 -- Interrupt_Manager.
1245 -- There are two Interrupt interrupts that this task catch through
1246 -- "sigwait." One is the Interrupt this task is designated to catch
1247 -- in order to execute user handler or entry. The other one is the
1248 -- Abort_Task_Interrupt. This interrupt is being sent from the
1249 -- Interrupt_Manager to inform status changes (e.g: become Blocked,
1250 -- Handler or Entry is to be detached).
1252 -- Prepare a mask to used for sigwait
1254 IMOP.Empty_Interrupt_Mask (Intwait_Mask'Access);
1256 IMOP.Add_To_Interrupt_Mask
1257 (Intwait_Mask'Access, IMNG.Interrupt_ID (Interrupt));
1259 IMOP.Add_To_Interrupt_Mask
1260 (Intwait_Mask'Access, IMNG.Abort_Task_Interrupt);
1262 IMOP.Thread_Block_Interrupt
1263 (IMNG.Abort_Task_Interrupt);
1265 PIO.Set_Interrupt_ID (IMNG.Interrupt_ID (Interrupt), Self_ID);
1267 loop
1268 System.Tasking.Initialization.Defer_Abort (Self_ID);
1270 if Single_Lock then
1271 POP.Lock_RTS;
1272 end if;
1274 POP.Write_Lock (Self_ID);
1276 if User_Handler (Interrupt).H = null
1277 and then User_Entry (Interrupt).T = Null_Task
1278 then
1279 -- No Interrupt binding. If there is an interrupt,
1280 -- Interrupt_Manager will take default action.
1282 Self_ID.Common.State := Interrupt_Server_Blocked_Interrupt_Sleep;
1283 POP.Sleep (Self_ID, Interrupt_Server_Idle_Sleep);
1284 Self_ID.Common.State := Runnable;
1286 elsif Blocked (Interrupt) then
1288 -- Interrupt is blocked. Stay here, so we won't catch
1289 -- the Interrupt.
1291 Self_ID.Common.State := Interrupt_Server_Blocked_Interrupt_Sleep;
1292 POP.Sleep (Self_ID, Interrupt_Server_Blocked_Interrupt_Sleep);
1293 Self_ID.Common.State := Runnable;
1295 else
1296 -- A Handler or an Entry is installed. At this point all tasks
1297 -- mask for the Interrupt is masked. Catch the Interrupt using
1298 -- sigwait.
1300 -- This task may wake up from sigwait by receiving an interrupt
1301 -- (Abort_Task_Interrupt) from the Interrupt_Manager for unbinding
1302 -- a Procedure Handler or an Entry. Or it could be a wake up
1303 -- from status change (Unblocked -> Blocked). If that is not
1304 -- the case, we should execute the attached Procedure or Entry.
1306 Self_ID.Common.State := Interrupt_Server_Blocked_On_Event_Flag;
1307 POP.Unlock (Self_ID);
1309 if Single_Lock then
1310 POP.Unlock_RTS;
1311 end if;
1313 -- Avoid race condition when terminating application and
1314 -- System.Parameters.No_Abort is True.
1316 if Parameters.No_Abort and then Self_ID.Pending_Action then
1317 Initialization.Do_Pending_Action (Self_ID);
1318 end if;
1320 Ret_Interrupt :=
1321 Interrupt_ID (IMOP.Interrupt_Wait (Intwait_Mask'Access));
1322 Self_ID.Common.State := Runnable;
1324 if Ret_Interrupt = Interrupt_ID (IMNG.Abort_Task_Interrupt) then
1326 -- Inform the Interrupt_Manager of wakeup from above sigwait
1328 POP.Abort_Task (Interrupt_Manager_ID);
1330 if Single_Lock then
1331 POP.Lock_RTS;
1332 end if;
1334 POP.Write_Lock (Self_ID);
1336 else
1337 if Single_Lock then
1338 POP.Lock_RTS;
1339 end if;
1341 POP.Write_Lock (Self_ID);
1343 if Ret_Interrupt /= Interrupt then
1345 -- On some systems (e.g. recent linux kernels), sigwait
1346 -- may return unexpectedly (with errno set to EINTR).
1348 null;
1350 else
1351 -- Even though we have received an Interrupt the status may
1352 -- have changed already before we got the Self_ID lock above
1353 -- Therefore we make sure a Handler or an Entry is still
1354 -- there and make appropriate call.
1356 -- If there is no calls to make we need to regenerate the
1357 -- Interrupt in order not to lose it.
1359 if User_Handler (Interrupt).H /= null then
1360 Tmp_Handler := User_Handler (Interrupt).H;
1362 -- RTS calls should not be made with self being locked
1364 POP.Unlock (Self_ID);
1366 if Single_Lock then
1367 POP.Unlock_RTS;
1368 end if;
1370 Tmp_Handler.all;
1372 if Single_Lock then
1373 POP.Lock_RTS;
1374 end if;
1376 POP.Write_Lock (Self_ID);
1378 elsif User_Entry (Interrupt).T /= Null_Task then
1379 Tmp_ID := User_Entry (Interrupt).T;
1380 Tmp_Entry_Index := User_Entry (Interrupt).E;
1382 -- RTS calls should not be made with self being locked
1384 if Single_Lock then
1385 POP.Unlock_RTS;
1386 end if;
1388 POP.Unlock (Self_ID);
1390 System.Tasking.Rendezvous.Call_Simple
1391 (Tmp_ID, Tmp_Entry_Index, System.Null_Address);
1393 POP.Write_Lock (Self_ID);
1395 if Single_Lock then
1396 POP.Lock_RTS;
1397 end if;
1399 else
1400 -- This is a situation that this task wakes up receiving
1401 -- an Interrupt and before it gets the lock the Interrupt
1402 -- is blocked. We do not want to lose the interrupt in
1403 -- this case so we regenerate the Interrupt to process
1404 -- level.
1406 IMOP.Interrupt_Self_Process
1407 (IMNG.Interrupt_ID (Interrupt));
1408 end if;
1409 end if;
1410 end if;
1411 end if;
1413 POP.Unlock (Self_ID);
1415 if Single_Lock then
1416 POP.Unlock_RTS;
1417 end if;
1419 System.Tasking.Initialization.Undefer_Abort (Self_ID);
1421 if Self_ID.Pending_Action then
1422 Initialization.Do_Pending_Action (Self_ID);
1423 end if;
1425 -- Undefer abort here to allow a window for this task to be aborted
1426 -- at the time of system shutdown. We also explicitly test for
1427 -- Pending_Action in case System.Parameters.No_Abort is True.
1429 end loop;
1430 end Server_Task;
1432 -- Elaboration code for package System.Interrupts
1434 begin
1435 -- Get Interrupt_Manager's ID so that Abort_Interrupt can be sent
1437 Interrupt_Manager_ID := To_System (Interrupt_Manager'Identity);
1439 -- During the elaboration of this package body we want the RTS
1440 -- to inherit the interrupt mask from the Environment Task.
1442 IMOP.Setup_Interrupt_Mask;
1444 -- The environment task should have gotten its mask from the enclosing
1445 -- process during the RTS start up. (See processing in s-inmaop.adb). Pass
1446 -- the Interrupt_Mask of the environment task to the Interrupt_Manager.
1448 -- Note: At this point we know that all tasks are masked for non-reserved
1449 -- signals. Only the Interrupt_Manager will have masks set up differently
1450 -- inheriting the original environment task's mask.
1452 Interrupt_Manager.Initialize (IMOP.Environment_Mask);
1453 end System.Interrupts;