Daily bump.
[official-gcc.git] / gcc / ada / 5zinterr.adb
blobd1c0fd0e11968e677bd270932f259fc5a2947ba0
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA 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 -- $Revision: 1.2 $
10 -- --
11 -- Copyright (C) 1991-2001 Free Software Foundation, Inc. --
12 -- --
13 -- GNARL is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNARL; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
30 -- --
31 -- GNARL was developed by the GNARL team at Florida State University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 -- --
34 ------------------------------------------------------------------------------
36 -- Invariants:
38 -- All user-handleable signals are masked at all times in all
39 -- tasks/threads except possibly for the Interrupt_Manager task.
41 -- When a user task wants to have the effect of masking/unmasking an
42 -- signal, it must call Block_Interrupt/Unblock_Interrupt, which
43 -- will have the effect of unmasking/masking the signal in the
44 -- Interrupt_Manager task. These comments do not apply to vectored
45 -- hardware interrupts, which may be masked or unmasked using routined
46 -- interfaced to the relevant VxWorks system calls.
48 -- Note : Direct calls to sigaction, sigprocmask, pthread_sigsetmask or any
49 -- other low-level interface that changes the signal action or
50 -- signal mask needs careful consideration.
51 -- One may achieve the effect of system calls first masking RTS blocked
52 -- (by calling Block_Interrupt) for the signal under consideration.
53 -- This will make all the tasks in RTS blocked for the signal.
55 -- Once we associate a Signal_Server_Task with an signal, the task never
56 -- goes away, and we never remove the association. On the other hand, it
57 -- is more convenient to terminate an associated Interrupt_Server_Task
58 -- for a vectored hardware interrupt (since we use a binary semaphore
59 -- for synchronization with the umbrella handler).
61 -- There is no more than one signal per Signal_Server_Task and no more than
62 -- one Signal_Server_Task per signal. The same relation holds for hardware
63 -- interrupts and Interrupt_Server_Task's at any given time. That is,
64 -- only one non-terminated Interrupt_Server_Task exists for a give
65 -- interrupt at any time.
67 -- Within this package, the lock L is used to protect the various status
68 -- tables. If there is a Server_Task associated with a signal or interrupt,
69 -- we use the per-task lock of the Server_Task instead so that we protect the
70 -- status between Interrupt_Manager and Server_Task. Protection among
71 -- service requests are ensured via user calls to the Interrupt_Manager
72 -- entries.
74 -- This is the VxWorks version of this package, supporting both signals
75 -- and vectored hardware interrupts.
77 with Unchecked_Conversion;
79 with System.OS_Interface; use System.OS_Interface;
81 with System.VxWorks;
83 with Interfaces.VxWorks;
85 with Ada.Task_Identification;
86 -- used for Task_ID type
88 with Ada.Exceptions;
89 -- used for Raise_Exception
91 with System.Task_Primitives;
92 -- used for RTS_Lock
93 -- Self
95 with System.Interrupt_Management;
96 -- used for Reserve
97 -- Interrupt_ID
98 -- Interrupt_Mask
99 -- Abort_Task_Interrupt
101 with System.Interrupt_Management.Operations;
102 -- used for Thread_Block_Interrupt
103 -- Thread_Unblock_Interrupt
104 -- Install_Default_Action
105 -- Install_Ignore_Action
106 -- Copy_Interrupt_Mask
107 -- Set_Interrupt_Mask
108 -- Empty_Interrupt_Mask
109 -- Fill_Interrupt_Mask
110 -- Add_To_Interrupt_Mask
111 -- Delete_From_Interrupt_Mask
112 -- Interrupt_Wait
113 -- Interrupt_Self_Process
114 -- Get_Interrupt_Mask
115 -- Set_Interrupt_Mask
116 -- IS_Member
117 -- Environment_Mask
118 -- All_Tasks_Mask
119 pragma Elaborate_All (System.Interrupt_Management.Operations);
121 with System.Error_Reporting;
122 -- used for Shutdown
124 with System.Task_Primitives.Operations;
125 -- used for Write_Lock
126 -- Unlock
127 -- Abort
128 -- Wakeup_Task
129 -- Sleep
130 -- Initialize_Lock
132 with System.Task_Primitives.Interrupt_Operations;
133 -- used for Set_Interrupt_ID
135 with System.Storage_Elements;
136 -- used for To_Address
137 -- To_Integer
138 -- Integer_Address
140 with System.Tasking;
141 -- used for Task_ID
142 -- Task_Entry_Index
143 -- Null_Task
144 -- Self
145 -- Interrupt_Manager_ID
147 with System.Tasking.Utilities;
148 -- used for Make_Independent
150 with System.Tasking.Rendezvous;
151 -- used for Call_Simple
152 pragma Elaborate_All (System.Tasking.Rendezvous);
154 with System.Tasking.Initialization;
155 -- used for Defer_Abort
156 -- Undefer_Abort
158 package body System.Interrupts is
160 use Tasking;
161 use System.Error_Reporting;
162 use Ada.Exceptions;
164 package PRI renames System.Task_Primitives;
165 package POP renames System.Task_Primitives.Operations;
166 package PIO renames System.Task_Primitives.Interrupt_Operations;
167 package IMNG renames System.Interrupt_Management;
168 package IMOP renames System.Interrupt_Management.Operations;
170 function To_Ada is new Unchecked_Conversion
171 (System.Tasking.Task_ID, Ada.Task_Identification.Task_Id);
173 function To_System is new Unchecked_Conversion
174 (Ada.Task_Identification.Task_Id, Task_ID);
176 -----------------
177 -- Local Tasks --
178 -----------------
180 -- WARNING: System.Tasking.Utilities performs calls to this task
181 -- with low-level constructs. Do not change this spec without synchro-
182 -- nizing it.
184 task Interrupt_Manager is
185 entry Initialize (Mask : IMNG.Interrupt_Mask);
187 entry Attach_Handler
188 (New_Handler : Parameterless_Handler;
189 Interrupt : Interrupt_ID;
190 Static : Boolean;
191 Restoration : Boolean := False);
193 entry Exchange_Handler
194 (Old_Handler : out Parameterless_Handler;
195 New_Handler : Parameterless_Handler;
196 Interrupt : Interrupt_ID;
197 Static : Boolean);
199 entry Detach_Handler
200 (Interrupt : Interrupt_ID;
201 Static : Boolean);
203 entry Bind_Interrupt_To_Entry
204 (T : Task_ID;
205 E : Task_Entry_Index;
206 Interrupt : Interrupt_ID);
208 entry Detach_Interrupt_Entries (T : Task_ID);
210 pragma Interrupt_Priority (System.Interrupt_Priority'First);
211 end Interrupt_Manager;
213 task type Signal_Server_Task (Interrupt : Interrupt_ID) is
214 pragma Interrupt_Priority (System.Interrupt_Priority'First + 1);
215 end Signal_Server_Task;
216 -- Server task for signal handling
218 type Signal_Task_Access is access Signal_Server_Task;
220 task type Interrupt_Server_Task
221 (Interrupt : Interrupt_ID; Int_Sema : SEM_ID) is
222 -- Server task for vectored hardware interrupt handling
223 pragma Interrupt_Priority (System.Interrupt_Priority'First + 2);
224 end Interrupt_Server_Task;
226 type Interrupt_Task_Access is access Interrupt_Server_Task;
228 -------------------------------
229 -- Local Types and Variables --
230 -------------------------------
232 type Entry_Assoc is record
233 T : Task_ID;
234 E : Task_Entry_Index;
235 end record;
237 type Handler_Assoc is record
238 H : Parameterless_Handler;
239 Static : Boolean; -- Indicates static binding;
240 end record;
242 User_Handler : array (Interrupt_ID) of Handler_Assoc :=
243 (others => (null, Static => False));
244 pragma Volatile_Components (User_Handler);
245 -- Holds the protected procedure handler (if any) and its Static
246 -- information for each interrupt or signal. A handler is static
247 -- iff it is specified through the pragma Attach_Handler.
249 User_Entry : array (Interrupt_ID) of Entry_Assoc :=
250 (others => (T => Null_Task, E => Null_Task_Entry));
251 pragma Volatile_Components (User_Entry);
252 -- Holds the task and entry index (if any) for each interrupt / signal
254 -- Type and Head, Tail of the list containing Registered Interrupt
255 -- Handlers. These definitions are used to register the handlers
256 -- specified by the pragma Interrupt_Handler.
258 type Registered_Handler;
259 type R_Link is access all Registered_Handler;
261 type Registered_Handler is record
262 H : System.Address := System.Null_Address;
263 Next : R_Link := null;
264 end record;
266 Registered_Handler_Head : R_Link := null;
267 Registered_Handler_Tail : R_Link := null;
269 Server_ID : array (Interrupt_ID) of System.Tasking.Task_ID :=
270 (others => System.Tasking.Null_Task);
271 pragma Atomic_Components (Server_ID);
272 -- Holds the Task_ID of the Server_Task for each interrupt / signal.
273 -- Task_ID is needed to accomplish locking per interrupt base. Also
274 -- is needed to determine whether to create a new Server_Task.
276 Semaphore_ID_Map : array
277 (Interrupt_ID range 0 .. System.VxWorks.Num_HW_Interrupts) of SEM_ID :=
278 (others => 0);
279 -- Array of binary semaphores associated with vectored interrupts
280 -- Note that the last bound should be Max_HW_Interrupt, but this will raise
281 -- Storage_Error if Num_HW_Interrupts is null, so use an extra 4 bytes
282 -- instead.
284 Signal_Access_Hold : Signal_Task_Access;
285 -- Variable for allocating a Signal_Server_Task
287 Interrupt_Access_Hold : Interrupt_Task_Access;
288 -- Variable for allocating an Interrupt_Server_Task
290 L : aliased PRI.RTS_Lock;
291 -- L protects the contents of the above tables for interrupts / signals
292 -- for which Server_ID (I) = Null_Task.
294 -- If Server_ID (I) /= Null_Task then protection is via the
295 -- per-task (TCB) lock of Server_ID (I).
297 -- For deadlock prevention, L should not be locked after
298 -- any other lock is held, hence we use PO_Level which is the highest
299 -- lock level for error checking.
301 Task_Lock : array (Interrupt_ID) of Boolean := (others => False);
302 -- Booleans indicating whether the per task lock is used
304 Default_Handler : array (HW_Interrupt) of Interfaces.VxWorks.VOIDFUNCPTR;
305 -- Vectored interrupt handlers installed prior to program startup.
306 -- These are saved only when the umbrella handler is installed for
307 -- a given interrupt number.
309 -----------------------
310 -- Local Subprograms --
311 -----------------------
313 procedure Check_Reserved_Interrupt (Interrupt : Interrupt_ID);
314 -- Check if Id is a reserved interrupt, and if so raise Program_Error
315 -- with an appropriate message, otherwise return.
317 procedure Finalize_Interrupt_Servers;
318 -- Unbind the handlers for hardware interrupt server tasks at program
319 -- termination.
321 procedure Lock_Interrupt
322 (Self_ID : Task_ID;
323 Interrupt : Interrupt_ID);
324 -- Protect the tables using L or the per-task lock. Set the Boolean
325 -- value Task_Lock if the lock is made using per-task lock.
326 -- This information is needed so that Unlock_Interrupt
327 -- performs unlocking on the same lock. The situation we are preventing
328 -- is, for example, when Attach_Handler is called for the first time
329 -- we lock L and create an Server_Task. For a matching unlocking, if we
330 -- rely on the fact that there is a Server_Task, we will unlock the
331 -- per-task lock.
333 procedure Unlock_Interrupt
334 (Self_ID : Task_ID;
335 Interrupt : Interrupt_ID);
336 -- Unlock interrupt previously locked by Lock_Interrupt
338 function Is_Registered (Handler : Parameterless_Handler) return Boolean;
339 -- Needs comment ???
341 procedure Notify_Interrupt (Param : System.Address);
342 -- Umbrella handler for vectored interrupts (not signals)
344 procedure Install_Default_Action (Interrupt : HW_Interrupt);
345 -- Restore a handler that was in place prior to program execution
347 procedure Install_Umbrella_Handler
348 (Interrupt : HW_Interrupt;
349 Handler : Interfaces.VxWorks.VOIDFUNCPTR);
350 -- Install the runtime umbrella handler for a vectored hardware
351 -- interrupt
353 function To_Signal (S : Interrupt_ID) return IMNG.Interrupt_ID;
354 -- Convert interrupt ID to signal number.
356 procedure Unimplemented (Feature : String);
357 pragma No_Return (Unimplemented);
358 -- Used to mark a call to an unimplemented function. Raises Program_Error
359 -- with an appropriate message noting that Feature is unimplemented.
361 --------------------
362 -- Attach_Handler --
363 --------------------
365 -- Calling this procedure with New_Handler = null and Static = True
366 -- means we want to detach the current handler regardless of the
367 -- previous handler's binding status (ie. do not care if it is a
368 -- dynamic or static handler).
370 -- This option is needed so that during the finalization of a PO, we
371 -- can detach handlers attached through pragma Attach_Handler.
373 procedure Attach_Handler
374 (New_Handler : Parameterless_Handler;
375 Interrupt : Interrupt_ID;
376 Static : Boolean := False)
378 begin
379 Check_Reserved_Interrupt (Interrupt);
380 Interrupt_Manager.Attach_Handler (New_Handler, Interrupt, Static);
381 end Attach_Handler;
383 -----------------------------
384 -- Bind_Interrupt_To_Entry --
385 -----------------------------
387 -- This procedure raises a Program_Error if it tries to
388 -- bind an interrupt to which an Entry or a Procedure is
389 -- already bound.
391 procedure Bind_Interrupt_To_Entry
392 (T : Task_ID;
393 E : Task_Entry_Index;
394 Int_Ref : System.Address)
396 Interrupt : constant Interrupt_ID :=
397 Interrupt_ID (Storage_Elements.To_Integer (Int_Ref));
399 begin
400 Check_Reserved_Interrupt (Interrupt);
401 Interrupt_Manager.Bind_Interrupt_To_Entry (T, E, Interrupt);
402 end Bind_Interrupt_To_Entry;
404 ---------------------
405 -- Block_Interrupt --
406 ---------------------
408 procedure Block_Interrupt (Interrupt : Interrupt_ID) is
409 begin
410 Unimplemented ("Block_Interrupt");
411 end Block_Interrupt;
413 ------------------------------
414 -- Check_Reserved_Interrupt --
415 ------------------------------
417 procedure Check_Reserved_Interrupt (Interrupt : Interrupt_ID) is
418 begin
419 if Is_Reserved (Interrupt) then
420 Raise_Exception
421 (Program_Error'Identity,
422 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved");
423 else
424 return;
425 end if;
426 end Check_Reserved_Interrupt;
428 ---------------------
429 -- Current_Handler --
430 ---------------------
432 function Current_Handler
433 (Interrupt : Interrupt_ID)
434 return Parameterless_Handler
436 begin
437 Check_Reserved_Interrupt (Interrupt);
439 -- ??? Since Parameterless_Handler is not Atomic, the
440 -- current implementation is wrong. We need a new service in
441 -- Interrupt_Manager to ensure atomicity.
443 return User_Handler (Interrupt).H;
444 end Current_Handler;
446 --------------------
447 -- Detach_Handler --
448 --------------------
450 -- Calling this procedure with Static = True means we want to Detach the
451 -- current handler regardless of the previous handler's binding status
452 -- (i.e. do not care if it is a dynamic or static handler).
454 -- This option is needed so that during the finalization of a PO, we can
455 -- detach handlers attached through pragma Attach_Handler.
457 procedure Detach_Handler
458 (Interrupt : Interrupt_ID;
459 Static : Boolean := False)
461 begin
462 Check_Reserved_Interrupt (Interrupt);
463 Interrupt_Manager.Detach_Handler (Interrupt, Static);
464 end Detach_Handler;
466 ------------------------------
467 -- Detach_Interrupt_Entries --
468 ------------------------------
470 procedure Detach_Interrupt_Entries (T : Task_ID) is
471 begin
472 Interrupt_Manager.Detach_Interrupt_Entries (T);
473 end Detach_Interrupt_Entries;
475 ----------------------
476 -- Exchange_Handler --
477 ----------------------
479 -- Calling this procedure with New_Handler = null and Static = True
480 -- means we want to detach the current handler regardless of the
481 -- previous handler's binding status (ie. do not care if it is a
482 -- dynamic or static handler).
484 -- This option is needed so that during the finalization of a PO, we
485 -- can detach handlers attached through pragma Attach_Handler.
487 procedure Exchange_Handler
488 (Old_Handler : out Parameterless_Handler;
489 New_Handler : Parameterless_Handler;
490 Interrupt : Interrupt_ID;
491 Static : Boolean := False)
493 begin
494 Check_Reserved_Interrupt (Interrupt);
495 Interrupt_Manager.Exchange_Handler
496 (Old_Handler, New_Handler, Interrupt, Static);
497 end Exchange_Handler;
499 --------------
500 -- Finalize --
501 --------------
503 procedure Finalize (Object : in out Static_Interrupt_Protection) is
504 begin
505 -- ??? loop to be executed only when we're not doing library level
506 -- finalization, since in this case all interrupt / signal tasks are
507 -- gone.
509 if not Interrupt_Manager'Terminated then
510 for N in reverse Object.Previous_Handlers'Range loop
511 Interrupt_Manager.Attach_Handler
512 (New_Handler => Object.Previous_Handlers (N).Handler,
513 Interrupt => Object.Previous_Handlers (N).Interrupt,
514 Static => Object.Previous_Handlers (N).Static,
515 Restoration => True);
516 end loop;
517 end if;
519 Tasking.Protected_Objects.Entries.Finalize
520 (Tasking.Protected_Objects.Entries.Protection_Entries (Object));
521 end Finalize;
523 --------------------------------
524 -- Finalize_Interrupt_Servers --
525 --------------------------------
527 -- Restore default handlers for interrupt servers. Signal servers
528 -- restore the default handlers when they're aborted. This is called
529 -- by the Interrupt_Manager task when it receives the abort signal
530 -- during program finalization.
532 procedure Finalize_Interrupt_Servers is
533 begin
534 if HW_Interrupt'Last >= 0 then
535 for Int in HW_Interrupt loop
536 if Server_ID (Interrupt_ID (Int)) /= null
537 and then
538 not Ada.Task_Identification.Is_Terminated
539 (To_Ada (Server_ID (Interrupt_ID (Int))))
540 then
541 Interrupt_Manager.Attach_Handler
542 (New_Handler => null,
543 Interrupt => Interrupt_ID (Int),
544 Static => True,
545 Restoration => True);
546 end if;
547 end loop;
548 end if;
549 end Finalize_Interrupt_Servers;
551 -------------------------------------
552 -- Has_Interrupt_Or_Attach_Handler --
553 -------------------------------------
555 function Has_Interrupt_Or_Attach_Handler
556 (Object : access Dynamic_Interrupt_Protection)
557 return Boolean
559 begin
560 return True;
561 end Has_Interrupt_Or_Attach_Handler;
563 function Has_Interrupt_Or_Attach_Handler
564 (Object : access Static_Interrupt_Protection)
565 return Boolean
567 begin
568 return True;
569 end Has_Interrupt_Or_Attach_Handler;
571 ----------------------
572 -- Ignore_Interrupt --
573 ----------------------
575 procedure Ignore_Interrupt (Interrupt : Interrupt_ID) is
576 begin
577 Unimplemented ("Ignore_Interrupt");
578 end Ignore_Interrupt;
580 ----------------------------
581 -- Install_Default_Action --
582 ----------------------------
584 procedure Install_Default_Action (Interrupt : HW_Interrupt) is
585 begin
586 -- Restore original interrupt handler
588 Interfaces.VxWorks.intVecSet
589 (Interfaces.VxWorks.INUM_TO_IVEC (Integer (Interrupt)),
590 Default_Handler (Interrupt));
591 Default_Handler (Interrupt) := null;
592 end Install_Default_Action;
594 ----------------------
595 -- Install_Handlers --
596 ----------------------
598 procedure Install_Handlers
599 (Object : access Static_Interrupt_Protection;
600 New_Handlers : New_Handler_Array) is
601 begin
602 for N in New_Handlers'Range loop
603 -- We need a lock around this ???
605 Object.Previous_Handlers (N).Interrupt := New_Handlers (N).Interrupt;
606 Object.Previous_Handlers (N).Static := User_Handler
607 (New_Handlers (N).Interrupt).Static;
609 -- We call Exchange_Handler and not directly Interrupt_Manager.
610 -- Exchange_Handler so we get the Is_Reserved check.
612 Exchange_Handler
613 (Old_Handler => Object.Previous_Handlers (N).Handler,
614 New_Handler => New_Handlers (N).Handler,
615 Interrupt => New_Handlers (N).Interrupt,
616 Static => True);
617 end loop;
618 end Install_Handlers;
620 ------------------------------
621 -- Install_Umbrella_Handler --
622 ------------------------------
624 procedure Install_Umbrella_Handler
625 (Interrupt : HW_Interrupt;
626 Handler : Interfaces.VxWorks.VOIDFUNCPTR)
628 use Interfaces.VxWorks;
630 Vec : constant Interrupt_Vector :=
631 INUM_TO_IVEC (Interfaces.VxWorks.int (Interrupt));
632 Old_Handler : constant VOIDFUNCPTR :=
633 intVecGet
634 (INUM_TO_IVEC (Interfaces.VxWorks.int (Interrupt)));
635 Stat : Interfaces.VxWorks.STATUS;
637 begin
638 -- Only install umbrella handler when no Ada handler has already been
639 -- installed. Note that the interrupt number is passed as a parameter
640 -- when an interrupt occurs, so the umbrella handler has a different
641 -- wrapper generated by intConnect for each interrupt number.
643 if Default_Handler (Interrupt) = null then
644 Stat :=
645 intConnect (Vec, VOIDFUNCPTR (Handler), System.Address (Interrupt));
646 Default_Handler (Interrupt) := Old_Handler;
647 end if;
648 end Install_Umbrella_Handler;
650 ----------------
651 -- Is_Blocked --
652 ----------------
654 function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
655 begin
656 Unimplemented ("Is_Blocked");
657 return False;
658 end Is_Blocked;
660 -----------------------
661 -- Is_Entry_Attached --
662 -----------------------
664 function Is_Entry_Attached (Interrupt : Interrupt_ID) return Boolean is
665 begin
666 Check_Reserved_Interrupt (Interrupt);
667 return User_Entry (Interrupt).T /= Null_Task;
668 end Is_Entry_Attached;
670 -------------------------
671 -- Is_Handler_Attached --
672 -------------------------
674 function Is_Handler_Attached (Interrupt : Interrupt_ID) return Boolean is
675 begin
676 Check_Reserved_Interrupt (Interrupt);
677 return User_Handler (Interrupt).H /= null;
678 end Is_Handler_Attached;
680 ----------------
681 -- Is_Ignored --
682 ----------------
684 function Is_Ignored (Interrupt : Interrupt_ID) return Boolean is
685 begin
686 Unimplemented ("Is_Ignored");
687 return False;
688 end Is_Ignored;
690 -------------------
691 -- Is_Registered --
692 -------------------
694 -- See if Handler has been "pragma"ed using Interrupt_Handler.
695 -- Always consider a null handler as registered.
697 function Is_Registered (Handler : Parameterless_Handler) return Boolean is
698 type Fat_Ptr is record
699 Object_Addr : System.Address;
700 Handler_Addr : System.Address;
701 end record;
703 function To_Fat_Ptr is new Unchecked_Conversion
704 (Parameterless_Handler, Fat_Ptr);
706 Ptr : R_Link;
707 Fat : Fat_Ptr;
709 begin
710 if Handler = null then
711 return True;
712 end if;
714 Fat := To_Fat_Ptr (Handler);
716 Ptr := Registered_Handler_Head;
718 while (Ptr /= null) loop
719 if Ptr.H = Fat.Handler_Addr then
720 return True;
721 end if;
723 Ptr := Ptr.Next;
724 end loop;
726 return False;
728 end Is_Registered;
730 -----------------
731 -- Is_Reserved --
732 -----------------
734 function Is_Reserved (Interrupt : Interrupt_ID) return Boolean is
735 begin
736 if Interrupt < System.VxWorks.Num_HW_Interrupts then
737 return False;
738 else
739 return IMNG.Reserve (To_Signal (Interrupt));
740 end if;
741 end Is_Reserved;
743 --------------------
744 -- Lock_Interrupt --
745 --------------------
747 -- ?????
748 -- This package has been modified several times.
749 -- Do we still need this fancy locking scheme, now that more operations
750 -- are entries of the interrupt manager task?
751 -- ?????
752 -- More likely, we will need to convert one or more entry calls to
753 -- protected operations, because presently we are violating locking order
754 -- rules by calling a task entry from within the runtime system.
756 procedure Lock_Interrupt
757 (Self_ID : Task_ID;
758 Interrupt : Interrupt_ID) is
759 begin
760 Initialization.Defer_Abort (Self_ID);
762 POP.Write_Lock (L'Access);
764 if Task_Lock (Interrupt) then
765 pragma Assert (Server_ID (Interrupt) /= null,
766 "Task_Lock is true for null server task");
767 pragma Assert
768 (not Ada.Task_Identification.Is_Terminated
769 (To_Ada (Server_ID (Interrupt))),
770 "Attempt to lock per task lock of terminated server: " &
771 "Task_Lock => True");
773 POP.Unlock (L'Access);
774 POP.Write_Lock (Server_ID (Interrupt));
776 elsif Server_ID (Interrupt) /= Null_Task then
777 pragma Assert
778 (not Ada.Task_Identification.Is_Terminated
779 (To_Ada (Server_ID (Interrupt))),
780 "Attempt to lock per task lock of terminated server: " &
781 "Task_Lock => False");
783 Task_Lock (Interrupt) := True;
784 POP.Unlock (L'Access);
785 POP.Write_Lock (Server_ID (Interrupt));
786 end if;
788 end Lock_Interrupt;
790 ------------------------
791 -- Notify_Interrupt --
792 ------------------------
794 -- Umbrella handler for vectored hardware interrupts (as opposed to
795 -- signals and exceptions). As opposed to the signal implementation,
796 -- this handler is only installed in the vector table while there is
797 -- an active association of an Ada handler to the interrupt.
799 -- Otherwise, the handler that existed prior to program startup is
800 -- in the vector table. This ensures that handlers installed by
801 -- the BSP are active unless explicitly replaced in the program text.
803 -- Each Interrupt_Server_Task has an associated binary semaphore
804 -- on which it pends once it's been started. This routine determines
805 -- The appropriate semaphore and and issues a semGive call, waking
806 -- the server task. When a handler is unbound,
807 -- System.Interrupts.Unbind_Handler issues a semFlush, and the
808 -- server task deletes its semaphore and terminates.
810 procedure Notify_Interrupt (Param : System.Address) is
811 Interrupt : Interrupt_ID := Interrupt_ID (Param);
812 Discard_Result : STATUS;
814 begin
815 Discard_Result := semGive (Semaphore_ID_Map (Interrupt));
816 end Notify_Interrupt;
818 ---------------
819 -- Reference --
820 ---------------
822 function Reference (Interrupt : Interrupt_ID) return System.Address is
823 begin
824 Check_Reserved_Interrupt (Interrupt);
825 return Storage_Elements.To_Address
826 (Storage_Elements.Integer_Address (Interrupt));
827 end Reference;
829 --------------------------------
830 -- Register_Interrupt_Handler --
831 --------------------------------
833 procedure Register_Interrupt_Handler (Handler_Addr : System.Address) is
834 New_Node_Ptr : R_Link;
835 begin
836 -- This routine registers a handler as usable for dynamic
837 -- interrupt handler association. Routines attaching and detaching
838 -- handlers dynamically should determine whether the handler is
839 -- registered. Program_Error should be raised if it is not registered.
841 -- Pragma Interrupt_Handler can only appear in a library
842 -- level PO definition and instantiation. Therefore, we do not need
843 -- to implement an unregister operation. Nor do we need to
844 -- protect the queue structure with a lock.
846 pragma Assert (Handler_Addr /= System.Null_Address);
848 New_Node_Ptr := new Registered_Handler;
849 New_Node_Ptr.H := Handler_Addr;
851 if Registered_Handler_Head = null then
852 Registered_Handler_Head := New_Node_Ptr;
853 Registered_Handler_Tail := New_Node_Ptr;
855 else
856 Registered_Handler_Tail.Next := New_Node_Ptr;
857 Registered_Handler_Tail := New_Node_Ptr;
858 end if;
859 end Register_Interrupt_Handler;
861 ---------------
862 -- To_Signal --
863 ---------------
865 function To_Signal (S : Interrupt_ID) return IMNG.Interrupt_ID is
866 begin
867 return IMNG.Interrupt_ID (S - System.VxWorks.Num_HW_Interrupts);
868 end To_Signal;
870 -----------------------
871 -- Unblock_Interrupt --
872 -----------------------
874 procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
875 begin
876 Unimplemented ("Unblock_Interrupt");
877 end Unblock_Interrupt;
879 ------------------
880 -- Unblocked_By --
881 ------------------
883 function Unblocked_By
884 (Interrupt : Interrupt_ID) return System.Tasking.Task_ID is
885 begin
886 Unimplemented ("Unblocked_By");
887 return Null_Task;
888 end Unblocked_By;
890 ------------------------
891 -- Unignore_Interrupt --
892 ------------------------
894 procedure Unignore_Interrupt (Interrupt : Interrupt_ID) is
895 begin
896 Unimplemented ("Unignore_Interrupt");
897 end Unignore_Interrupt;
899 -------------------
900 -- Unimplemented --
901 -------------------
903 procedure Unimplemented (Feature : String) is
904 begin
905 Raise_Exception
906 (Program_Error'Identity,
907 Feature & " not implemented on VxWorks");
908 end Unimplemented;
910 ----------------------
911 -- Unlock_Interrupt --
912 ----------------------
914 procedure Unlock_Interrupt
915 (Self_ID : Task_ID;
916 Interrupt : Interrupt_ID) is
917 begin
918 if Task_Lock (Interrupt) then
919 pragma Assert
920 (not Ada.Task_Identification.Is_Terminated
921 (To_Ada (Server_ID (Interrupt))),
922 "Attempt to unlock per task lock of terminated server");
924 POP.Unlock (Server_ID (Interrupt));
925 else
926 POP.Unlock (L'Access);
927 end if;
929 Initialization.Undefer_Abort (Self_ID);
930 end Unlock_Interrupt;
932 -----------------------
933 -- Interrupt_Manager --
934 -----------------------
936 task body Interrupt_Manager is
937 ---------------------
938 -- Local Variables --
939 ---------------------
941 Intwait_Mask : aliased IMNG.Interrupt_Mask;
942 Old_Mask : aliased IMNG.Interrupt_Mask;
943 Self_ID : Task_ID := POP.Self;
945 --------------------
946 -- Local Routines --
947 --------------------
949 procedure Bind_Handler (Interrupt : Interrupt_ID);
950 -- This procedure does not do anything if a signal is blocked.
951 -- Otherwise, we have to interrupt Server_Task for status change through
952 -- a wakeup signal.
954 procedure Unbind_Handler (Interrupt : Interrupt_ID);
955 -- This procedure does not do anything if a signal is blocked.
956 -- Otherwise, we have to interrupt Server_Task for status change
957 -- through an abort signal.
959 -- The following two procedures are labelled Unprotected... in order to
960 -- indicate that Lock/Unlock_Interrupt operations are needed around
961 -- around calls to them.
963 procedure Unprotected_Exchange_Handler
964 (Old_Handler : out Parameterless_Handler;
965 New_Handler : Parameterless_Handler;
966 Interrupt : Interrupt_ID;
967 Static : Boolean;
968 Restoration : Boolean := False);
970 procedure Unprotected_Detach_Handler
971 (Interrupt : Interrupt_ID;
972 Static : Boolean);
974 ------------------
975 -- Bind_Handler --
976 ------------------
978 procedure Bind_Handler (Interrupt : Interrupt_ID) is
979 begin
980 if Interrupt < System.VxWorks.Num_HW_Interrupts then
981 Install_Umbrella_Handler
982 (HW_Interrupt (Interrupt), Notify_Interrupt'Access);
984 else
985 -- Mask this task for the given signal so that all tasks
986 -- are masked for the signal and the actual delivery of the
987 -- signal will be caught using "sigwait" by the
988 -- corresponding Server_Task.
990 IMOP.Thread_Block_Interrupt (To_Signal (Interrupt));
991 -- We have installed a handler or an entry before we called
992 -- this procedure. If the handler task is waiting to be
993 -- awakened, do it here. Otherwise, the signal will be
994 -- discarded.
996 POP.Wakeup (Server_ID (Interrupt), Interrupt_Server_Idle_Sleep);
997 end if;
998 end Bind_Handler;
1000 --------------------
1001 -- Unbind_Handler --
1002 --------------------
1004 procedure Unbind_Handler (Interrupt : Interrupt_ID) is
1005 S : STATUS;
1006 Ret_Interrupt : IMNG.Interrupt_ID;
1008 use type IMNG.Interrupt_ID;
1009 use type STATUS;
1011 begin
1012 if Interrupt < System.VxWorks.Num_HW_Interrupts then
1014 -- Hardware interrupt
1016 Install_Default_Action (HW_Interrupt (Interrupt));
1018 -- Flush server task off semaphore, allowing it to terminate
1020 S := semFlush (Semaphore_ID_Map (Interrupt));
1021 pragma Assert (S = 0);
1023 else
1024 -- Currently, there is a handler or an entry attached and
1025 -- the corresponding Server_Task is waiting on "sigwait."
1026 -- We have to wake up the Server_Task and make it
1027 -- wait on a condition variable by sending an
1028 -- Abort_Task_Interrupt
1030 -- Make sure corresponding Server_Task is out of its own
1031 -- sigwait state.
1033 POP.Abort_Task (Server_ID (Interrupt));
1034 Ret_Interrupt := IMOP.Interrupt_Wait (Intwait_Mask'Access);
1035 pragma Assert (Ret_Interrupt = IMNG.Abort_Task_Interrupt);
1037 IMOP.Install_Default_Action (To_Signal (Interrupt));
1039 -- Unmake the Interrupt for this task in order to allow default
1040 -- action again.
1042 IMOP.Thread_Unblock_Interrupt (To_Signal (Interrupt));
1043 end if;
1044 end Unbind_Handler;
1046 --------------------------------
1047 -- Unprotected_Detach_Handler --
1048 --------------------------------
1050 procedure Unprotected_Detach_Handler
1051 (Interrupt : Interrupt_ID;
1052 Static : Boolean)
1054 Old_Handler : Parameterless_Handler;
1055 begin
1056 if User_Entry (Interrupt).T /= Null_Task then
1058 -- If an interrupt entry is installed raise
1059 -- Program_Error. (propagate it to the caller).
1061 Unlock_Interrupt (Self_ID, Interrupt);
1062 Raise_Exception (Program_Error'Identity,
1063 "An interrupt entry is already installed");
1064 end if;
1066 -- Note : Static = True will pass the following check. This is the
1067 -- case when we want to detach a handler regardless of the static
1068 -- status of the Current_Handler.
1070 if not Static and then User_Handler (Interrupt).Static then
1072 -- Trying to detach a static Interrupt Handler.
1073 -- raise Program_Error.
1075 Unlock_Interrupt (Self_ID, Interrupt);
1076 Raise_Exception (Program_Error'Identity,
1077 "Trying to detach a static Interrupt Handler");
1078 end if;
1080 Old_Handler := User_Handler (Interrupt).H;
1082 -- The new handler
1084 User_Handler (Interrupt).H := null;
1085 User_Handler (Interrupt).Static := False;
1087 if Old_Handler /= null then
1088 Unbind_Handler (Interrupt);
1089 end if;
1091 end Unprotected_Detach_Handler;
1093 ----------------------------------
1094 -- Unprotected_Exchange_Handler --
1095 ----------------------------------
1097 procedure Unprotected_Exchange_Handler
1098 (Old_Handler : out Parameterless_Handler;
1099 New_Handler : Parameterless_Handler;
1100 Interrupt : Interrupt_ID;
1101 Static : Boolean;
1102 Restoration : Boolean := False) is
1103 begin
1104 if User_Entry (Interrupt).T /= Null_Task then
1106 -- If an interrupt entry is already installed, raise
1107 -- Program_Error. (propagate it to the caller).
1109 Unlock_Interrupt (Self_ID, Interrupt);
1110 Raise_Exception (Program_Error'Identity,
1111 "An interrupt is already installed");
1112 end if;
1114 -- Note : A null handler with Static = True will
1115 -- pass the following check. This is the case when we want to
1116 -- detach a handler regardless of the Static status
1117 -- of Current_Handler.
1118 -- We don't check anything if Restoration is True, since we
1119 -- may be detaching a static handler to restore a dynamic one.
1121 if not Restoration and then not Static
1122 and then (User_Handler (Interrupt).Static
1124 -- Trying to overwrite a static Interrupt Handler with a
1125 -- dynamic Handler
1127 -- The new handler is not specified as an
1128 -- Interrupt Handler by a pragma.
1130 or else not Is_Registered (New_Handler))
1131 then
1132 Unlock_Interrupt (Self_ID, Interrupt);
1133 Raise_Exception
1134 (Program_Error'Identity,
1135 "Trying to overwrite a static Interrupt Handler with a " &
1136 "dynamic Handler");
1137 end if;
1139 -- Save the old handler
1141 Old_Handler := User_Handler (Interrupt).H;
1143 -- The new handler
1145 User_Handler (Interrupt).H := New_Handler;
1147 if New_Handler = null then
1149 -- The null handler means we are detaching the handler.
1151 User_Handler (Interrupt).Static := False;
1153 else
1154 User_Handler (Interrupt).Static := Static;
1155 end if;
1157 -- Invoke a corresponding Server_Task if not yet created.
1158 -- Place Task_ID info in Server_ID array.
1160 if New_Handler /= null
1161 and then
1162 (Server_ID (Interrupt) = Null_Task
1163 or else
1164 Ada.Task_Identification.Is_Terminated
1165 (To_Ada (Server_ID (Interrupt))))
1166 then
1167 -- When a new Server_Task is created, it should have its
1168 -- signal mask set to the All_Tasks_Mask.
1170 IMOP.Set_Interrupt_Mask
1171 (IMOP.All_Tasks_Mask'Access, Old_Mask'Access);
1173 if Interrupt < System.VxWorks.Num_HW_Interrupts then
1175 -- Vectored hardware interrupt
1177 Interrupt_Access_Hold :=
1178 new Interrupt_Server_Task
1179 (Interrupt, semBCreate (SEM_Q_FIFO, SEM_EMPTY));
1180 Server_ID (Interrupt) :=
1181 To_System (Interrupt_Access_Hold.all'Identity);
1183 else
1184 -- Signal
1186 Signal_Access_Hold := new Signal_Server_Task (Interrupt);
1187 Server_ID (Interrupt) :=
1188 To_System (Signal_Access_Hold.all'Identity);
1189 end if;
1191 IMOP.Set_Interrupt_Mask (Old_Mask'Access);
1192 end if;
1194 if (New_Handler = null) and then Old_Handler /= null then
1196 -- Restore default handler
1198 Unbind_Handler (Interrupt);
1200 elsif Old_Handler = null then
1202 -- Save default handler
1204 Bind_Handler (Interrupt);
1205 end if;
1207 end Unprotected_Exchange_Handler;
1209 -- Start of processing for Interrupt_Manager
1211 begin
1212 -- By making this task independent of any master, when the process
1213 -- goes away, the Interrupt_Manager will terminate gracefully.
1215 System.Tasking.Utilities.Make_Independent;
1217 -- Environment task gets its own interrupt mask, saves it,
1218 -- and then masks all signals except the Keep_Unmasked set.
1220 -- During rendezvous, the Interrupt_Manager receives the old
1221 -- signal mask of the environment task, and sets its own
1222 -- signal mask to that value.
1224 -- The environment task will call this entry of Interrupt_Manager
1225 -- during elaboration of the body of this package.
1227 accept Initialize (Mask : IMNG.Interrupt_Mask) do
1228 declare
1229 The_Mask : aliased IMNG.Interrupt_Mask;
1231 begin
1232 IMOP.Copy_Interrupt_Mask (The_Mask, Mask);
1233 IMOP.Set_Interrupt_Mask (The_Mask'Access);
1234 end;
1235 end Initialize;
1237 -- Note: All tasks in RTS will have all reserved signals
1238 -- being masked (except the Interrupt_Manager) and Keep_Unmasked
1239 -- signals unmasked when created.
1241 -- Abort_Task_Interrupt is one of the signals unmasked
1242 -- in all tasks. We mask the signal in this particular task
1243 -- so that "sigwait" is can catch an explicit
1244 -- Abort_Task_Interrupt from a Server_Task.
1246 -- This sigwaiting is needed to ensure that a Signal_Server_Task is
1247 -- out of its own sigwait state. This extra synchronization is
1248 -- necessary to prevent following scenarios:
1250 -- 1) Interrupt_Manager sends an Abort_Task_Interrupt to a
1251 -- Signal_Server_Task then changes its own signal mask (OS level).
1252 -- If a signal (corresponding to the Signal_Server_Task) arrives
1253 -- in the meantime, we have the Interrupt_Manager umnasked and
1254 -- the Signal_Server_Task waiting on sigwait.
1256 -- 2) For unbinding a handler, we install a default action in the
1257 -- Interrupt_Manager. POSIX.1c states that the result of using
1258 -- "sigwait" and "sigaction" simultaneously on the same signal
1259 -- is undefined. Therefore, we need to be informed from the
1260 -- Signal_Server_Task that it is out of its sigwait stage.
1262 IMOP.Empty_Interrupt_Mask (Intwait_Mask'Access);
1263 IMOP.Add_To_Interrupt_Mask
1264 (Intwait_Mask'Access, IMNG.Abort_Task_Interrupt);
1265 IMOP.Thread_Block_Interrupt (IMNG.Abort_Task_Interrupt);
1267 loop
1268 -- A block is needed to absorb Program_Error exception
1270 declare
1271 Old_Handler : Parameterless_Handler;
1273 begin
1274 select
1276 accept Attach_Handler
1277 (New_Handler : Parameterless_Handler;
1278 Interrupt : Interrupt_ID;
1279 Static : Boolean;
1280 Restoration : Boolean := False)
1282 Lock_Interrupt (Self_ID, Interrupt);
1283 Unprotected_Exchange_Handler
1284 (Old_Handler, New_Handler, Interrupt, Static, Restoration);
1285 Unlock_Interrupt (Self_ID, Interrupt);
1286 end Attach_Handler;
1288 or accept Exchange_Handler
1289 (Old_Handler : out Parameterless_Handler;
1290 New_Handler : Parameterless_Handler;
1291 Interrupt : Interrupt_ID;
1292 Static : Boolean)
1294 Lock_Interrupt (Self_ID, Interrupt);
1295 Unprotected_Exchange_Handler
1296 (Old_Handler, New_Handler, Interrupt, Static);
1297 Unlock_Interrupt (Self_ID, Interrupt);
1298 end Exchange_Handler;
1300 or accept Detach_Handler
1301 (Interrupt : Interrupt_ID;
1302 Static : Boolean)
1304 Lock_Interrupt (Self_ID, Interrupt);
1305 Unprotected_Detach_Handler (Interrupt, Static);
1306 Unlock_Interrupt (Self_ID, Interrupt);
1307 end Detach_Handler;
1309 or accept Bind_Interrupt_To_Entry
1310 (T : Task_ID;
1311 E : Task_Entry_Index;
1312 Interrupt : Interrupt_ID)
1314 Lock_Interrupt (Self_ID, Interrupt);
1316 -- If there is a binding already (either a procedure or an
1317 -- entry), raise Program_Error (propagate it to the caller).
1319 if User_Handler (Interrupt).H /= null
1320 or else User_Entry (Interrupt).T /= Null_Task
1321 then
1322 Unlock_Interrupt (Self_ID, Interrupt);
1323 Raise_Exception
1324 (Program_Error'Identity,
1325 "A binding for this interrupt is already present");
1326 end if;
1328 User_Entry (Interrupt) := Entry_Assoc' (T => T, E => E);
1330 -- Indicate the attachment of interrupt entry in the ATCB.
1331 -- This is needed so when an interrupt entry task terminates
1332 -- the binding can be cleaned. The call to unbinding must be
1333 -- make by the task before it terminates.
1335 T.Interrupt_Entry := True;
1337 -- Invoke a corresponding Server_Task if not yet created.
1338 -- Place Task_ID info in Server_ID array.
1340 if Server_ID (Interrupt) = Null_Task or else
1341 Ada.Task_Identification.Is_Terminated
1342 (To_Ada (Server_ID (Interrupt))) then
1344 -- When a new Server_Task is created, it should have its
1345 -- signal mask set to the All_Tasks_Mask.
1347 IMOP.Set_Interrupt_Mask
1348 (IMOP.All_Tasks_Mask'Access, Old_Mask'Access);
1350 if Interrupt < System.VxWorks.Num_HW_Interrupts then
1351 Interrupt_Access_Hold := new Interrupt_Server_Task
1352 (Interrupt, semBCreate (SEM_Q_FIFO, SEM_EMPTY));
1353 Server_ID (Interrupt) :=
1354 To_System (Interrupt_Access_Hold.all'Identity);
1356 else
1357 Signal_Access_Hold := new Signal_Server_Task (Interrupt);
1358 Server_ID (Interrupt) :=
1359 To_System (Signal_Access_Hold.all'Identity);
1360 end if;
1362 IMOP.Set_Interrupt_Mask (Old_Mask'Access);
1363 end if;
1365 Bind_Handler (Interrupt);
1366 Unlock_Interrupt (Self_ID, Interrupt);
1367 end Bind_Interrupt_To_Entry;
1369 or accept Detach_Interrupt_Entries (T : Task_ID)
1371 for Int in Interrupt_ID'Range loop
1372 if not Is_Reserved (Int) then
1373 Lock_Interrupt (Self_ID, Int);
1375 if User_Entry (Int).T = T then
1377 User_Entry (Int) := Entry_Assoc'
1378 (T => Null_Task, E => Null_Task_Entry);
1379 Unbind_Handler (Int);
1380 end if;
1382 Unlock_Interrupt (Self_ID, Int);
1383 end if;
1384 end loop;
1386 -- Indicate in ATCB that no interrupt entries are attached.
1388 T.Interrupt_Entry := False;
1389 end Detach_Interrupt_Entries;
1391 end select;
1393 exception
1395 -- If there is a Program_Error we just want to propagate it to
1396 -- the caller and do not want to stop this task.
1398 when Program_Error =>
1399 null;
1401 when E : others =>
1402 pragma Assert
1403 (Shutdown ("Interrupt_Manager---exception not expected" &
1404 ASCII.LF &
1405 Exception_Information (E)));
1406 null;
1407 end;
1408 end loop;
1410 pragma Assert (Shutdown ("Interrupt_Manager---should not get here"));
1411 exception
1412 when Standard'Abort_Signal =>
1413 -- Flush interrupt server semaphores, so they can terminate
1414 Finalize_Interrupt_Servers;
1415 raise;
1416 end Interrupt_Manager;
1418 ------------------------
1419 -- Signal_Server_Task --
1420 ------------------------
1422 task body Signal_Server_Task is
1423 Intwait_Mask : aliased IMNG.Interrupt_Mask;
1424 Ret_Interrupt : IMNG.Interrupt_ID;
1425 Self_ID : Task_ID := Self;
1426 Tmp_Handler : Parameterless_Handler;
1427 Tmp_ID : Task_ID;
1428 Tmp_Entry_Index : Task_Entry_Index;
1430 use type IMNG.Interrupt_ID;
1432 begin
1433 -- By making this task independent of master, when the process
1434 -- goes away, the Server_Task will terminate gracefully.
1436 System.Tasking.Utilities.Make_Independent;
1438 -- Install default action in system level.
1440 IMOP.Install_Default_Action (To_Signal (Interrupt));
1442 -- Note: All tasks in RTS will have all reserved signals
1443 -- masked (except the Interrupt_Manager) and Keep_Unmasked
1444 -- unmasked when created.
1446 -- Abort_Task_Interrupt is one of the signals unmasked
1447 -- in all tasks. We mask it in this particular task
1448 -- so that "sigwait" can catch an explicit
1449 -- Abort_Task_Interrupt from the Interrupt_Manager.
1451 -- There are two signals that this task catches through
1452 -- "sigwait." One is the signal it is designated to catch
1453 -- in order to execute an user handler or entry. The other is
1454 -- Abort_Task_Interrupt. This signal is sent from the
1455 -- Interrupt_Manager to inform of status changes (e.g: become Blocked,
1456 -- or a handler or entry is to be detached).
1458 -- Prepare the mask to be used for sigwait.
1460 IMOP.Empty_Interrupt_Mask (Intwait_Mask'Access);
1462 IMOP.Add_To_Interrupt_Mask
1463 (Intwait_Mask'Access, To_Signal (Interrupt));
1465 IMOP.Add_To_Interrupt_Mask
1466 (Intwait_Mask'Access, IMNG.Abort_Task_Interrupt);
1468 IMOP.Thread_Block_Interrupt (IMNG.Abort_Task_Interrupt);
1470 PIO.Set_Interrupt_ID (To_Signal (Interrupt), Self_ID);
1472 loop
1473 System.Tasking.Initialization.Defer_Abort (Self_ID);
1474 POP.Write_Lock (Self_ID);
1476 if User_Handler (Interrupt).H = null
1477 and then User_Entry (Interrupt).T = Null_Task
1478 then
1480 -- No signal binding. If a signal is received,
1481 -- Interrupt_Manager will take the default action.
1483 Self_ID.Common.State := Interrupt_Server_Blocked_Interrupt_Sleep;
1484 POP.Sleep (Self_ID, Interrupt_Server_Idle_Sleep);
1485 Self_ID.Common.State := Runnable;
1487 else
1488 -- A handler or an entry is installed. At this point all tasks
1489 -- mask for the signal is masked. Catch it using
1490 -- sigwait.
1492 -- This task may wake up from sigwait by receiving a signal
1493 -- (Abort_Task_Interrupt) from the Interrupt_Manager for unbinding
1494 -- a procedure handler or an entry. Or it could be a wake up
1495 -- from status change (Unblocked -> Blocked). If that is not
1496 -- the case, we should execute the attached procedure or entry.
1498 POP.Unlock (Self_ID);
1500 Ret_Interrupt := IMOP.Interrupt_Wait (Intwait_Mask'Access);
1502 if Ret_Interrupt = IMNG.Abort_Task_Interrupt then
1503 -- Inform the Interrupt_Manager of wakeup from above sigwait.
1505 POP.Abort_Task (Interrupt_Manager_ID);
1506 POP.Write_Lock (Self_ID);
1508 else
1509 POP.Write_Lock (Self_ID);
1511 -- Even though we have received a signal, the status may
1512 -- have changed before we got the Self_ID lock above.
1513 -- Therefore we make sure a handler or an entry is still
1514 -- bound and make appropriate call.
1515 -- If there is no call to make we need to regenerate the
1516 -- signal in order not to lose it.
1518 if User_Handler (Interrupt).H /= null then
1520 Tmp_Handler := User_Handler (Interrupt).H;
1522 -- RTS calls should not be made with self being locked.
1524 POP.Unlock (Self_ID);
1526 Tmp_Handler.all;
1527 POP.Write_Lock (Self_ID);
1529 elsif User_Entry (Interrupt).T /= Null_Task then
1531 Tmp_ID := User_Entry (Interrupt).T;
1532 Tmp_Entry_Index := User_Entry (Interrupt).E;
1534 -- RTS calls should not be made with self being locked.
1536 POP.Unlock (Self_ID);
1538 System.Tasking.Rendezvous.Call_Simple
1539 (Tmp_ID, Tmp_Entry_Index, System.Null_Address);
1541 POP.Write_Lock (Self_ID);
1542 else
1543 -- This is a situation where this task woke up receiving a
1544 -- signal and before it got the lock the signal was blocked.
1545 -- We do not want to lose the signal so we regenerate it at
1546 -- the process level.
1548 IMOP.Interrupt_Self_Process (Ret_Interrupt);
1549 end if;
1550 end if;
1551 end if;
1553 POP.Unlock (Self_ID);
1554 System.Tasking.Initialization.Undefer_Abort (Self_ID);
1556 -- Undefer abort here to allow a window for this task
1557 -- to be aborted at the time of system shutdown.
1558 end loop;
1559 end Signal_Server_Task;
1561 ---------------------------
1562 -- Interrupt_Server_Task --
1563 ---------------------------
1565 -- Server task for vectored hardware interrupt handling
1567 task body Interrupt_Server_Task is
1568 Self_ID : Task_ID := Self;
1569 Tmp_Handler : Parameterless_Handler;
1570 Tmp_ID : Task_ID;
1571 Tmp_Entry_Index : Task_Entry_Index;
1572 S : STATUS;
1574 use type STATUS;
1576 begin
1577 System.Tasking.Utilities.Make_Independent;
1578 Semaphore_ID_Map (Interrupt) := Int_Sema;
1580 loop
1581 -- Pend on semaphore that will be triggered by the
1582 -- umbrella handler when the associated interrupt comes in
1584 S := semTake (Int_Sema, WAIT_FOREVER);
1585 pragma Assert (S = 0);
1587 if User_Handler (Interrupt).H /= null then
1589 -- Protected procedure handler
1591 Tmp_Handler := User_Handler (Interrupt).H;
1592 Tmp_Handler.all;
1594 elsif User_Entry (Interrupt).T /= Null_Task then
1596 -- Interrupt entry handler
1598 Tmp_ID := User_Entry (Interrupt).T;
1599 Tmp_Entry_Index := User_Entry (Interrupt).E;
1600 System.Tasking.Rendezvous.Call_Simple
1601 (Tmp_ID, Tmp_Entry_Index, System.Null_Address);
1603 else
1604 -- Semaphore has been flushed by an unbind operation in
1605 -- the Interrupt_Manager. Terminate the server task.
1607 -- Wait for the Interrupt_Manager to complete its work
1609 POP.Write_Lock (Self_ID);
1611 -- Delete the associated semaphore
1613 S := semDelete (Int_Sema);
1615 pragma Assert (S = 0);
1617 -- Set status for the Interrupt_Manager
1619 Semaphore_ID_Map (Interrupt) := 0;
1620 Task_Lock (Interrupt) := False;
1621 Server_ID (Interrupt) := Null_Task;
1622 POP.Unlock (Self_ID);
1624 exit;
1625 end if;
1626 end loop;
1627 end Interrupt_Server_Task;
1629 begin
1630 -- Elaboration code for package System.Interrupts
1632 -- Get Interrupt_Manager's ID so that Abort_Interrupt can be sent.
1634 Interrupt_Manager_ID := To_System (Interrupt_Manager'Identity);
1636 -- Initialize the lock L.
1638 Initialization.Defer_Abort (Self);
1639 POP.Initialize_Lock (L'Access, POP.PO_Level);
1640 Initialization.Undefer_Abort (Self);
1642 -- During the elaboration of this package body we want the RTS to
1643 -- inherit its signal mask from the Environment Task.
1645 -- The Environment Task should have gotten its mask from
1646 -- the enclosing process during the RTS start up. (See
1647 -- in s-inmaop.adb). Pass the Interrupt_Mask of the Environment
1648 -- task to the Interrupt_Manager.
1650 -- Note : At this point we know that all tasks (including
1651 -- RTS internal servers) are masked for non-reserved signals
1652 -- (see s-taprop.adb). Only the Interrupt_Manager will have
1653 -- masks set up differently, inheriting the original Environment
1654 -- Task's mask.
1656 Interrupt_Manager.Initialize (IMOP.Environment_Mask);
1657 end System.Interrupts;