OSDN Git Service

* a-calend-mingw.adb: Add call to OS_Primitives.Initialize;
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-intman-vxworks.adb
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 _ M A N A G E M E N T          --
6 --                                                                          --
7 --                                  B o d y                                 --
8 --                                                                          --
9 --          Copyright (C) 1992-2005 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 ------------------------------------------------------------------------------
33
34 --  This is the VxWorks version of this package.
35
36 --  Make a careful study of all signals available under the OS,
37 --  to see which need to be reserved, kept always unmasked,
38 --  or kept always unmasked.
39 --  Be on the lookout for special signals that
40 --  may be used by the thread library.
41
42 with Interfaces.C;
43
44 with System.OS_Interface;
45 --  used for various Constants, Signal and types
46
47 package body System.Interrupt_Management is
48
49    use System.OS_Interface;
50    use type Interfaces.C.int;
51
52    type Signal_List is array (Signal_ID range <>) of Signal_ID;
53    Exception_Signals : constant Signal_List (1 .. 4) :=
54                          (SIGFPE, SIGILL, SIGSEGV, SIGBUS);
55
56    Exception_Action : aliased struct_sigaction;
57    --  Keep this variable global so that it is initialized only once
58
59    procedure Map_And_Raise_Exception (signo : Signal);
60    pragma Import (C, Map_And_Raise_Exception, "__gnat_map_signal");
61    --  Map signal to Ada exception and raise it.  Different versions
62    --  of VxWorks need different mappings.
63
64    -----------------------
65    -- Local Subprograms --
66    -----------------------
67
68    function State (Int : Interrupt_ID) return Character;
69    pragma Import (C, State, "__gnat_get_interrupt_state");
70    --  Get interrupt state.  Defined in init.c
71    --  The input argument is the interrupt number,
72    --  and the result is one of the following:
73
74    Runtime : constant Character := 'r';
75    Default : constant Character := 's';
76    --    'n'   this interrupt not set by any Interrupt_State pragma
77    --    'u'   Interrupt_State pragma set state to User
78    --    'r'   Interrupt_State pragma set state to Runtime
79    --    's'   Interrupt_State pragma set state to System (use "default"
80    --           system handler)
81
82    procedure Notify_Exception (signo : Signal);
83    --  Identify the Ada exception to be raised using
84    --  the information when the system received a synchronous signal.
85
86    ----------------------
87    -- Notify_Exception --
88    ----------------------
89
90    procedure Notify_Exception (signo : Signal) is
91       Mask   : aliased sigset_t;
92
93       Result : int;
94       pragma Unreferenced (Result);
95
96    begin
97       Result := pthread_sigmask (SIG_SETMASK, null, Mask'Unchecked_Access);
98       Result := sigdelset (Mask'Access, signo);
99       Result := pthread_sigmask (SIG_SETMASK, Mask'Unchecked_Access, null);
100
101       Map_And_Raise_Exception (signo);
102    end Notify_Exception;
103
104    ---------------------------
105    -- Initialize_Interrupts --
106    ---------------------------
107
108    --  Since there is no signal inheritance between VxWorks tasks, we need
109    --  to initialize signal handling in each task.
110
111    procedure Initialize_Interrupts is
112       Result  : int;
113       old_act : aliased struct_sigaction;
114    begin
115       for J in Exception_Signals'Range loop
116          Result :=
117            sigaction
118              (Signal (Exception_Signals (J)), Exception_Action'Access,
119               old_act'Unchecked_Access);
120          pragma Assert (Result = 0);
121       end loop;
122    end Initialize_Interrupts;
123
124    ----------------
125    -- Initialize --
126    ----------------
127
128    Initialized : Boolean := False;
129
130    procedure Initialize is
131       mask   : aliased sigset_t;
132       Result : int;
133    begin
134       if Initialized then
135          return;
136       end if;
137
138       Initialized := True;
139
140       --  Change this if you want to use another signal for task abort.
141       --  SIGTERM might be a good one.
142
143       Abort_Task_Signal := SIGABRT;
144
145       Exception_Action.sa_handler := Notify_Exception'Address;
146       Exception_Action.sa_flags := SA_ONSTACK;
147       Result := sigemptyset (mask'Access);
148       pragma Assert (Result = 0);
149
150       for J in Exception_Signals'Range loop
151          Result := sigaddset (mask'Access, Signal (Exception_Signals (J)));
152          pragma Assert (Result = 0);
153       end loop;
154
155       Exception_Action.sa_mask := mask;
156
157       --  Initialize hardware interrupt handling
158
159       pragma Assert (Reserve = (Interrupt_ID'Range => False));
160
161       --  Check all interrupts for state that requires keeping them reserved
162
163       for J in Interrupt_ID'Range loop
164          if State (J) = Default or else State (J) = Runtime then
165             Reserve (J) := True;
166          end if;
167       end loop;
168
169       --  Add exception signals to the set of unmasked signals
170
171       for J in Exception_Signals'Range loop
172          Keep_Unmasked (Exception_Signals (J)) := True;
173       end loop;
174
175       --  The abort signal must also be unmasked
176
177       Keep_Unmasked (Abort_Task_Signal) := True;
178    end Initialize;
179
180 end System.Interrupt_Management;