OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / 7sintman.adb
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 _ M A N A G E M E N T          --
6 --                                                                          --
7 --                                  B o d y                                 --
8 --                                                                          --
9 --                                                                          --
10 --             Copyright (C) 1991-2002, Florida State University            --
11 --                                                                          --
12 -- GNARL is free software; you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNARL was developed by the GNARL team at Florida State University. It is --
31 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
32 -- State University (http://www.gnat.com).                                  --
33 --                                                                          --
34 ------------------------------------------------------------------------------
35
36 --  This is the default version of this package
37
38 --  This is a Sun OS (FSU THREADS) version of this package
39
40 --  PLEASE DO NOT add any dependences on other packages. ??? why not ???
41 --  This package is designed to work with or without tasking support.
42
43 --  See the other warnings in the package specification before making
44 --  any modifications to this file.
45
46 --  Make a careful study of all signals available under the OS, to see which
47 --  need to be reserved, kept always unmasked, or kept always unmasked. Be on
48 --  the lookout for special signals that may be used by the thread library.
49
50 --  Since this is a multi target file, the signal <-> exception mapping
51 --  is simple minded. If you need a more precise and target specific
52 --  signal handling, create a new s-intman.adb that will fit your needs.
53
54 --  This file assumes that:
55 --
56 --    SIGFPE, SIGILL, SIGSEGV and SIGBUS exist. They are mapped as follows:
57 --      SIGPFE  => Constraint_Error
58 --      SIGILL  => Program_Error
59 --      SIGSEGV => Storage_Error
60 --      SIGBUS  => Storage_Error
61 --
62 --    SIGINT exists and will be kept unmasked unless the pragma
63 --     Unreserve_All_Interrupts is specified anywhere in the application.
64 --
65 --    System.OS_Interface contains the following:
66 --      SIGADAABORT: the signal that will be used to abort tasks.
67 --      Unmasked: the OS specific set of signals that should be unmasked in
68 --                all the threads. SIGADAABORT is unmasked by
69 --                default
70 --      Reserved: the OS specific set of signals that are reserved.
71
72 with Interfaces.C;
73 --  used for int and other types
74
75 with System.OS_Interface;
76 --  used for various Constants, Signal and types
77
78 package body System.Interrupt_Management is
79
80    use Interfaces.C;
81    use System.OS_Interface;
82
83    type Interrupt_List is array (Interrupt_ID range <>) of Interrupt_ID;
84    Exception_Interrupts : constant Interrupt_List :=
85      (SIGFPE, SIGILL, SIGSEGV, SIGBUS);
86
87    Unreserve_All_Interrupts : Interfaces.C.int;
88    pragma Import
89      (C, Unreserve_All_Interrupts, "__gl_unreserve_all_interrupts");
90
91    -----------------------
92    -- Local Subprograms --
93    -----------------------
94
95    procedure Notify_Exception (signo : Signal);
96    --  This function identifies the Ada exception to be raised using
97    --  the information when the system received a synchronous signal.
98    --  Since this function is machine and OS dependent, different code
99    --  has to be provided for different target.
100
101    ----------------------
102    -- Notify_Exception --
103    ----------------------
104
105    Signal_Mask : aliased sigset_t;
106    --  The set of signals handled by Notify_Exception
107
108    procedure Notify_Exception (signo : Signal) is
109       Result  : Interfaces.C.int;
110
111    begin
112       --  With the __builtin_longjmp, the signal mask is not restored, so we
113       --  need to restore it explicitly.
114
115       Result := pthread_sigmask (SIG_UNBLOCK, Signal_Mask'Access, null);
116       pragma Assert (Result = 0);
117
118       --  Check that treatment of exception propagation here
119       --  is consistent with treatment of the abort signal in
120       --  System.Task_Primitives.Operations.
121
122       case signo is
123          when SIGFPE =>
124             raise Constraint_Error;
125          when SIGILL =>
126             raise Program_Error;
127          when SIGSEGV =>
128             raise Storage_Error;
129          when SIGBUS =>
130             raise Storage_Error;
131          when others =>
132             null;
133       end case;
134    end Notify_Exception;
135
136    ---------------------------
137    -- Initialize_Interrupts --
138    ---------------------------
139
140    --  Nothing needs to be done on this platform.
141
142    procedure Initialize_Interrupts is
143    begin
144       null;
145    end Initialize_Interrupts;
146
147 -------------------------
148 -- Package Elaboration --
149 -------------------------
150
151 begin
152    declare
153       act     : aliased struct_sigaction;
154       old_act : aliased struct_sigaction;
155       Result  : Interfaces.C.int;
156
157    begin
158       --  Need to call pthread_init very early because it is doing signal
159       --  initializations.
160
161       pthread_init;
162
163       Abort_Task_Interrupt := SIGADAABORT;
164
165       act.sa_handler := Notify_Exception'Address;
166
167       act.sa_flags := 0;
168
169       --  On some targets, we set sa_flags to SA_NODEFER so that during the
170       --  handler execution we do not change the Signal_Mask to be masked for
171       --  the Signal.
172
173       --  This is a temporary fix to the problem that the Signal_Mask is
174       --  not restored after the exception (longjmp) from the handler.
175       --  The right fix should be made in sigsetjmp so that we save
176       --  the Signal_Set and restore it after a longjmp.
177
178       --  Since SA_NODEFER is obsolete, instead we reset explicitly
179       --  the mask in the exception handler.
180
181       Result := sigemptyset (Signal_Mask'Access);
182       pragma Assert (Result = 0);
183
184       --  ??? For the same reason explained above, we can't mask these
185       --  signals because otherwise we won't be able to catch more than
186       --  one signal.
187
188       act.sa_mask := Signal_Mask;
189
190       Keep_Unmasked (Abort_Task_Interrupt) := True;
191
192       --  By keeping SIGINT unmasked, allow the user to do a Ctrl-C, but at
193       --  the same time, disable the ability of handling this signal via
194       --  package Ada.Interrupts.
195
196       --  The pragma Unreserve_All_Interrupts let the user the ability to
197       --  change this behavior.
198
199       if Unreserve_All_Interrupts = 0 then
200          Keep_Unmasked (SIGINT) := True;
201       end if;
202
203       for J in Exception_Interrupts'Range loop
204          Keep_Unmasked (Exception_Interrupts (J)) := True;
205
206          Result :=
207            sigaction
208            (Signal (Exception_Interrupts (J)), act'Unchecked_Access,
209             old_act'Unchecked_Access);
210          pragma Assert (Result = 0);
211       end loop;
212
213       for J in Unmasked'Range loop
214          Keep_Unmasked (Interrupt_ID (Unmasked (J))) := True;
215       end loop;
216
217       Reserve := Keep_Unmasked or Keep_Masked;
218
219       for J in Reserved'Range loop
220          Reserve (Interrupt_ID (Reserved (J))) := True;
221       end loop;
222
223       --  We do not have Signal 0 in reality. We just use this value
224       --  to identify non-existent signals (see s-intnam.ads). Therefore,
225       --  Signal 0 should not be used in all signal related operations hence
226       --  mark it as reserved.
227
228       Reserve (0) := True;
229    end;
230 end System.Interrupt_Management;