OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / 5sintman.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) 1992-2002 Free Software Foundation, Inc.          --
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 a Solaris version of this package.
37
38 --  PLEASE DO NOT add any dependences on other packages.
39 --  This package is designed to work with or without tasking support.
40
41 --  Make a careful study of all signals available under the OS,
42 --  to see which need to be reserved, kept always unmasked,
43 --  or kept always unmasked.
44
45 --  Be on the lookout for special signals that
46 --  may be used by the thread library.
47
48 with Interfaces.C;
49 --  used for int
50
51 with System.OS_Interface;
52 --  used for various Constants, Signal and types
53
54 package body System.Interrupt_Management is
55
56    use Interfaces.C;
57    use System.OS_Interface;
58
59    type Interrupt_List is array (Interrupt_ID range <>) of Interrupt_ID;
60
61    Exception_Interrupts : constant Interrupt_List :=
62      (SIGFPE, SIGILL, SIGSEGV, SIGBUS);
63
64    Unreserve_All_Interrupts : Interfaces.C.int;
65    pragma Import
66      (C, Unreserve_All_Interrupts, "__gl_unreserve_all_interrupts");
67
68    ----------------------
69    -- Notify_Exception --
70    ----------------------
71
72    --  This function identifies the Ada exception to be raised using
73    --  the information when the system received a synchronous signal.
74    --  Since this function is machine and OS dependent, different code
75    --  has to be provided for different target.
76
77    procedure Notify_Exception
78      (signo   : Signal;
79       info    : access siginfo_t;
80       context : access ucontext_t);
81
82    procedure Notify_Exception
83      (signo   : Signal;
84       info    : access siginfo_t;
85       context : access ucontext_t) is
86    begin
87       --  Check that treatment of exception propagation here
88       --  is consistent with treatment of the abort signal in
89       --  System.Task_Primitives.Operations.
90
91       case signo is
92          when SIGFPE =>
93             case info.si_code is
94                when  FPE_INTDIV |
95                      FPE_INTOVF |
96                      FPE_FLTDIV |
97                      FPE_FLTOVF |
98                      FPE_FLTUND |
99                      FPE_FLTRES |
100                      FPE_FLTINV |
101                      FPE_FLTSUB =>
102
103                   raise Constraint_Error;
104
105                when others =>
106                   pragma Assert (False);
107                   null;
108             end case;
109
110          when SIGILL | SIGSEGV | SIGBUS  =>
111             raise Storage_Error;
112
113          when others =>
114             pragma Assert (False);
115             null;
116       end case;
117    end Notify_Exception;
118
119    ---------------------------
120    -- Initialize_Interrupts --
121    ---------------------------
122
123    --  Nothing needs to be done on this platform.
124
125    procedure Initialize_Interrupts is
126    begin
127       null;
128    end Initialize_Interrupts;
129
130 ----------------------------
131 -- Package Initialization --
132 ----------------------------
133
134 begin
135    declare
136       act     : aliased struct_sigaction;
137       old_act : aliased struct_sigaction;
138       mask    : aliased sigset_t;
139       Result  : Interfaces.C.int;
140
141    begin
142       --  Need to call pthread_init very early because it is doing signal
143       --  initializations.
144
145       pthread_init;
146
147       --  Change this if you want to use another signal for task abort.
148       --  SIGTERM might be a good one.
149
150       Abort_Task_Interrupt := SIGABRT;
151
152       act.sa_handler := Notify_Exception'Address;
153
154       --  Set sa_flags to SA_NODEFER so that during the handler execution
155       --  we do not change the Signal_Mask to be masked for the Signal.
156       --  This is a temporary fix to the problem that the Signal_Mask is
157       --  not restored after the exception (longjmp) from the handler.
158       --  The right fix should be made in sigsetjmp so that we save
159       --  the Signal_Set and restore it after a longjmp.
160
161       --  In that case, this field should be changed back to 0. ??? (Dong-Ik)
162
163       act.sa_flags := 16;
164
165       Result := sigemptyset (mask'Access);
166       pragma Assert (Result = 0);
167
168       --  ??? For the same reason explained above, we can't mask these
169       --  signals because otherwise we won't be able to catch more than
170       --  one signal.
171
172       act.sa_mask := mask;
173
174       Keep_Unmasked (Abort_Task_Interrupt) := True;
175
176       --  By keeping SIGINT unmasked, allow the user to do a Ctrl-C, but in the
177       --  same time, disable the ability of handling this signal
178       --  via Ada.Interrupts.
179       --  The pragma Unreserve_All_Interrupts let the user the ability to
180       --  change this behavior.
181
182       if Unreserve_All_Interrupts = 0 then
183          Keep_Unmasked (SIGINT) := True;
184       end if;
185
186       for J in Exception_Interrupts'Range loop
187          Keep_Unmasked (Exception_Interrupts (J)) := True;
188          Result :=
189            sigaction
190            (Signal (Exception_Interrupts (J)), act'Unchecked_Access,
191             old_act'Unchecked_Access);
192          pragma Assert (Result = 0);
193       end loop;
194
195       for J in Unmasked'Range loop
196          Keep_Unmasked (Interrupt_ID (Unmasked (J))) := True;
197       end loop;
198
199       Reserve := Keep_Unmasked or Keep_Masked;
200
201       for J in Reserved'Range loop
202          Reserve (Interrupt_ID (Reserved (J))) := True;
203       end loop;
204
205       --  We do not have Signal 0 in reality. We just use this value
206       --  to identify not existing signals (see s-intnam.ads). Therefore,
207       --  Signal 0 should not be used in all signal related operations hence
208       --  mark it as reserved.
209
210       Reserve (0) := True;
211    end;
212 end System.Interrupt_Management;