OSDN Git Service

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