1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- SYSTEM.INTERRUPT_MANAGEMENT.OPERATIONS --
9 -- Copyright (C) 1991-1994, Florida State University --
10 -- Copyright (C) 1995-2010, AdaCore --
12 -- GNAT 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 3, or (at your option) any later ver- --
15 -- sion. GNAT 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. --
19 -- As a special exception under Section 7 of GPL version 3, you are granted --
20 -- additional permissions described in the GCC Runtime Library Exception, --
21 -- version 3.1, as published by the Free Software Foundation. --
23 -- You should have received a copy of the GNU General Public License and --
24 -- a copy of the GCC Runtime Library Exception along with this program; --
25 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
26 -- <http://www.gnu.org/licenses/>. --
28 -- GNARL was developed by the GNARL team at Florida State University. --
29 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 ------------------------------------------------------------------------------
33 -- This is a POSIX-like version of this package
35 -- Note: this file can only be used for POSIX compliant systems
39 with System
.OS_Interface
;
40 with System
.Storage_Elements
;
42 package body System
.Interrupt_Management
.Operations
is
45 use System
.OS_Interface
;
51 Initial_Action
: array (Signal
) of aliased struct_sigaction
;
53 Default_Action
: aliased struct_sigaction
;
54 pragma Warnings
(Off
, Default_Action
);
56 Ignore_Action
: aliased struct_sigaction
;
58 ----------------------------
59 -- Thread_Block_Interrupt --
60 ----------------------------
62 procedure Thread_Block_Interrupt
63 (Interrupt
: Interrupt_ID
)
65 Result
: Interfaces
.C
.int
;
66 Mask
: aliased sigset_t
;
68 Result
:= sigemptyset
(Mask
'Access);
69 pragma Assert
(Result
= 0);
70 Result
:= sigaddset
(Mask
'Access, Signal
(Interrupt
));
71 pragma Assert
(Result
= 0);
72 Result
:= pthread_sigmask
(SIG_BLOCK
, Mask
'Access, null);
73 pragma Assert
(Result
= 0);
74 end Thread_Block_Interrupt
;
76 ------------------------------
77 -- Thread_Unblock_Interrupt --
78 ------------------------------
80 procedure Thread_Unblock_Interrupt
81 (Interrupt
: Interrupt_ID
)
83 Mask
: aliased sigset_t
;
84 Result
: Interfaces
.C
.int
;
86 Result
:= sigemptyset
(Mask
'Access);
87 pragma Assert
(Result
= 0);
88 Result
:= sigaddset
(Mask
'Access, Signal
(Interrupt
));
89 pragma Assert
(Result
= 0);
90 Result
:= pthread_sigmask
(SIG_UNBLOCK
, Mask
'Access, null);
91 pragma Assert
(Result
= 0);
92 end Thread_Unblock_Interrupt
;
94 ------------------------
95 -- Set_Interrupt_Mask --
96 ------------------------
98 procedure Set_Interrupt_Mask
(Mask
: access Interrupt_Mask
) is
99 Result
: Interfaces
.C
.int
;
101 Result
:= pthread_sigmask
(SIG_SETMASK
, Mask
, null);
102 pragma Assert
(Result
= 0);
103 end Set_Interrupt_Mask
;
105 procedure Set_Interrupt_Mask
106 (Mask
: access Interrupt_Mask
;
107 OMask
: access Interrupt_Mask
)
109 Result
: Interfaces
.C
.int
;
111 Result
:= pthread_sigmask
(SIG_SETMASK
, Mask
, OMask
);
112 pragma Assert
(Result
= 0);
113 end Set_Interrupt_Mask
;
115 ------------------------
116 -- Get_Interrupt_Mask --
117 ------------------------
119 procedure Get_Interrupt_Mask
(Mask
: access Interrupt_Mask
) is
120 Result
: Interfaces
.C
.int
;
122 Result
:= pthread_sigmask
(SIG_SETMASK
, null, Mask
);
123 pragma Assert
(Result
= 0);
124 end Get_Interrupt_Mask
;
130 function Interrupt_Wait
131 (Mask
: access Interrupt_Mask
) return Interrupt_ID
133 Result
: Interfaces
.C
.int
;
134 Sig
: aliased Signal
;
137 Result
:= sigwait
(Mask
, Sig
'Access);
143 return Interrupt_ID
(Sig
);
146 ----------------------------
147 -- Install_Default_Action --
148 ----------------------------
150 procedure Install_Default_Action
(Interrupt
: Interrupt_ID
) is
151 Result
: Interfaces
.C
.int
;
155 Initial_Action
(Signal
(Interrupt
))'Access, null);
156 pragma Assert
(Result
= 0);
157 end Install_Default_Action
;
159 ---------------------------
160 -- Install_Ignore_Action --
161 ---------------------------
163 procedure Install_Ignore_Action
(Interrupt
: Interrupt_ID
) is
164 Result
: Interfaces
.C
.int
;
166 Result
:= sigaction
(Signal
(Interrupt
), Ignore_Action
'Access, null);
167 pragma Assert
(Result
= 0);
168 end Install_Ignore_Action
;
170 -------------------------
171 -- Fill_Interrupt_Mask --
172 -------------------------
174 procedure Fill_Interrupt_Mask
(Mask
: access Interrupt_Mask
) is
175 Result
: Interfaces
.C
.int
;
177 Result
:= sigfillset
(Mask
);
178 pragma Assert
(Result
= 0);
179 end Fill_Interrupt_Mask
;
181 --------------------------
182 -- Empty_Interrupt_Mask --
183 --------------------------
185 procedure Empty_Interrupt_Mask
(Mask
: access Interrupt_Mask
) is
186 Result
: Interfaces
.C
.int
;
188 Result
:= sigemptyset
(Mask
);
189 pragma Assert
(Result
= 0);
190 end Empty_Interrupt_Mask
;
192 ---------------------------
193 -- Add_To_Interrupt_Mask --
194 ---------------------------
196 procedure Add_To_Interrupt_Mask
197 (Mask
: access Interrupt_Mask
;
198 Interrupt
: Interrupt_ID
)
200 Result
: Interfaces
.C
.int
;
202 Result
:= sigaddset
(Mask
, Signal
(Interrupt
));
203 pragma Assert
(Result
= 0);
204 end Add_To_Interrupt_Mask
;
206 --------------------------------
207 -- Delete_From_Interrupt_Mask --
208 --------------------------------
210 procedure Delete_From_Interrupt_Mask
211 (Mask
: access Interrupt_Mask
;
212 Interrupt
: Interrupt_ID
)
214 Result
: Interfaces
.C
.int
;
216 Result
:= sigdelset
(Mask
, Signal
(Interrupt
));
217 pragma Assert
(Result
= 0);
218 end Delete_From_Interrupt_Mask
;
225 (Mask
: access Interrupt_Mask
;
226 Interrupt
: Interrupt_ID
) return Boolean
228 Result
: Interfaces
.C
.int
;
230 Result
:= sigismember
(Mask
, Signal
(Interrupt
));
231 pragma Assert
(Result
= 0 or else Result
= 1);
235 -------------------------
236 -- Copy_Interrupt_Mask --
237 -------------------------
239 procedure Copy_Interrupt_Mask
240 (X
: out Interrupt_Mask
;
241 Y
: Interrupt_Mask
) is
244 end Copy_Interrupt_Mask
;
246 ----------------------------
247 -- Interrupt_Self_Process --
248 ----------------------------
250 procedure Interrupt_Self_Process
(Interrupt
: Interrupt_ID
) is
251 Result
: Interfaces
.C
.int
;
253 Result
:= kill
(getpid
, Signal
(Interrupt
));
254 pragma Assert
(Result
= 0);
255 end Interrupt_Self_Process
;
257 --------------------------
258 -- Setup_Interrupt_Mask --
259 --------------------------
261 procedure Setup_Interrupt_Mask
is
263 -- Mask task for all signals. The original mask of the Environment task
264 -- will be recovered by Interrupt_Manager task during the elaboration
267 Set_Interrupt_Mask
(All_Tasks_Mask
'Access);
268 end Setup_Interrupt_Mask
;
272 mask
: aliased sigset_t
;
273 allmask
: aliased sigset_t
;
274 Result
: Interfaces
.C
.int
;
277 Interrupt_Management
.Initialize
;
279 for Sig
in 1 .. Signal
'Last loop
281 (Sig
, null, Initial_Action
(Sig
)'Access);
284 -- we can't check Result here since sigaction will fail on
285 -- SIGKILL, SIGSTOP, and possibly other signals
286 -- pragma Assert (Result = 0);
290 -- Setup the masks to be exported
292 Result
:= sigemptyset
(mask
'Access);
293 pragma Assert
(Result
= 0);
295 Result
:= sigfillset
(allmask
'Access);
296 pragma Assert
(Result
= 0);
298 Default_Action
.sa_flags
:= 0;
299 Default_Action
.sa_mask
:= mask
;
300 Default_Action
.sa_handler
:=
301 Storage_Elements
.To_Address
302 (Storage_Elements
.Integer_Address
(SIG_DFL
));
304 Ignore_Action
.sa_flags
:= 0;
305 Ignore_Action
.sa_mask
:= mask
;
306 Ignore_Action
.sa_handler
:=
307 Storage_Elements
.To_Address
308 (Storage_Elements
.Integer_Address
(SIG_IGN
));
310 for J
in Interrupt_ID
loop
311 if Keep_Unmasked
(J
) then
312 Result
:= sigaddset
(mask
'Access, Signal
(J
));
313 pragma Assert
(Result
= 0);
314 Result
:= sigdelset
(allmask
'Access, Signal
(J
));
315 pragma Assert
(Result
= 0);
319 -- The Keep_Unmasked signals should be unmasked for Environment task
321 Result
:= pthread_sigmask
(SIG_UNBLOCK
, mask
'Access, null);
322 pragma Assert
(Result
= 0);
324 -- Get the signal mask of the Environment Task
326 Result
:= pthread_sigmask
(SIG_SETMASK
, null, mask
'Access);
327 pragma Assert
(Result
= 0);
329 -- Setup the constants exported
331 Environment_Mask
:= Interrupt_Mask
(mask
);
333 All_Tasks_Mask
:= Interrupt_Mask
(allmask
);
336 end System
.Interrupt_Management
.Operations
;