2 * <signal.h> wrapper functions.
5 * Jonathan Pryor (jonpryor@vt.edu)
6 * Jonathan Pryor (jpryor@novell.com)
7 * Tim Jenks (tim.jenks@realtimeworlds.com)
9 * Copyright (C) 2004-2005 Jonathan Pryor
10 * Copyright (C) 2008 Novell, Inc.
20 #include <sys/types.h>
21 #if defined(__APPLE__)
30 #include <mono/utils/atomic.h>
31 #include <mono/metadata/appdomain.h>
36 typedef void (*mph_sighandler_t
)(int);
37 typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info
;
40 static int count_handlers (int signum
);
44 Mono_Posix_Stdlib_SIG_DFL (void)
50 Mono_Posix_Stdlib_SIG_ERR (void)
56 Mono_Posix_Stdlib_SIG_IGN (void)
62 Mono_Posix_Stdlib_InvokeSignalHandler (int signum
, void *handler
)
64 mph_sighandler_t _h
= (mph_sighandler_t
) handler
;
68 int Mono_Posix_SIGRTMIN (void)
72 #else /* def SIGRTMIN */
74 #endif /* ndef SIGRTMIN */
77 int Mono_Posix_SIGRTMAX (void)
81 #else /* def SIGRTMAX */
83 #endif /* ndef SIGRTMAX */
86 int Mono_Posix_FromRealTimeSignum (int offset
, int *r
)
93 #if defined (SIGRTMIN) && defined (SIGRTMAX)
94 if ((offset
< 0) || (SIGRTMIN
> SIGRTMAX
- offset
)) {
100 #else /* defined (SIGRTMIN) && defined (SIGRTMAX) */
105 #endif /* defined (SIGRTMIN) && defined (SIGRTMAX) */
110 #ifndef WAPI_NO_ATOMIC_ASM
111 #define mph_int_get(p) InterlockedExchangeAdd ((p), 0)
112 #define mph_int_inc(p) InterlockedIncrement ((p))
113 #define mph_int_dec_test(p) (InterlockedDecrement ((p)) == 0)
114 #define mph_int_set(p,o,n) InterlockedExchange ((p), (n))
115 #elif GLIB_CHECK_VERSION(2,4,0)
116 #define mph_int_get(p) g_atomic_int_get ((p))
117 #define mph_int_inc(p) do {g_atomic_int_inc ((p));} while (0)
118 #define mph_int_dec_test(p) g_atomic_int_dec_and_test ((p))
119 #define mph_int_set(p,o,n) do { \
120 while (!g_atomic_int_compare_and_exchange ((p), (o), (n))) {} \
123 #define mph_int_get(p) (*(p))
124 #define mph_int_inc(p) do { (*(p))++; } while (0)
125 #define mph_int_dec_test(p) (--(*(p)) == 0)
126 #define mph_int_set(p,o,n) do { *(p) = n; } while (0)
131 Mono_Posix_Syscall_psignal (int sig
, const char* s
)
135 return errno
== 0 ? 0 : -1;
137 #endif /* def HAVE_PSIGNAL */
139 #define NUM_SIGNALS 64
140 static signal_info signals
[NUM_SIGNALS
];
142 static int acquire_mutex (pthread_mutex_t
*mutex
)
145 while ((mr
= pthread_mutex_lock (mutex
)) == EAGAIN
) {
146 /* try to acquire again */
148 if ((mr
!= 0) && (mr
!= EDEADLK
)) {
155 static void release_mutex (pthread_mutex_t
*mutex
)
158 while ((mr
= pthread_mutex_unlock (mutex
)) == EAGAIN
) {
159 /* try to release mutex again */
166 return r
== -1 && errno
== EINTR
;
170 default_handler (int signum
)
173 for (i
= 0; i
< NUM_SIGNALS
; ++i
) {
175 signal_info
* h
= &signals
[i
];
176 if (mph_int_get (&h
->signum
) != signum
)
178 mph_int_inc (&h
->count
);
179 fd
= mph_int_get (&h
->write_fd
);
183 pipecounter
= mph_int_get (&h
->pipecnt
);
184 for (j
= 0; j
< pipecounter
; ++j
) {
186 do { r
= write (fd
, &c
, 1); } while (keep_trying (r
));
187 fsync (fd
); /* force */
193 static pthread_mutex_t signals_mutex
= PTHREAD_MUTEX_INITIALIZER
;
196 Mono_Unix_UnixSignal_install (int sig
)
199 signal_info
* h
= NULL
;
200 int have_handler
= 0;
201 void* handler
= NULL
;
203 if (acquire_mutex (&signals_mutex
) == -1)
206 #if defined (SIGRTMIN) && defined (SIGRTMAX)
207 /*The runtime uses some rt signals for itself so it's important to not override them.*/
208 if (sig
>= SIGRTMIN
&& sig
<= SIGRTMAX
&& count_handlers (sig
) == 0) {
209 struct sigaction sinfo
;
210 sigaction (sig
, NULL
, &sinfo
);
211 if (sinfo
.sa_handler
!= SIG_DFL
|| (void*)sinfo
.sa_sigaction
!= (void*)SIG_DFL
) {
212 pthread_mutex_unlock (&signals_mutex
);
217 #endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/
219 for (i
= 0; i
< NUM_SIGNALS
; ++i
) {
220 if (h
== NULL
&& signals
[i
].signum
== 0) {
222 h
->handler
= signal (sig
, default_handler
);
223 if (h
->handler
== SIG_ERR
) {
232 if (!have_handler
&& signals
[i
].signum
== sig
&&
233 signals
[i
].handler
!= default_handler
) {
235 handler
= signals
[i
].handler
;
237 if (h
&& have_handler
)
241 if (h
&& have_handler
) {
243 h
->handler
= handler
;
247 mph_int_set (&h
->count
, h
->count
, 0);
248 mph_int_set (&h
->signum
, h
->signum
, sig
);
249 mph_int_set (&h
->pipecnt
, h
->pipecnt
, 0);
252 release_mutex (&signals_mutex
);
258 count_handlers (int signum
)
262 for (i
= 0; i
< NUM_SIGNALS
; ++i
) {
263 if (signals
[i
].signum
== signum
)
270 Mono_Unix_UnixSignal_uninstall (void* info
)
275 if (acquire_mutex (&signals_mutex
) == -1)
280 if (h
== NULL
|| h
< signals
|| h
> &signals
[NUM_SIGNALS
])
283 /* last UnixSignal -- we can unregister */
284 if (h
->have_handler
&& count_handlers (h
->signum
) == 1) {
285 mph_sighandler_t p
= signal (h
->signum
, h
->handler
);
294 release_mutex (&signals_mutex
);
300 setup_pipes (signal_info
** signals
, int count
, struct pollfd
*fd_structs
, int *currfd
)
304 for (i
= 0; i
< count
; ++i
) {
310 if (mph_int_get (&h
->pipecnt
) == 0) {
311 if ((r
= pipe (filedes
)) != 0) {
314 h
->read_fd
= filedes
[0];
315 h
->write_fd
= filedes
[1];
317 mph_int_inc (&h
->pipecnt
);
318 fd_structs
[*currfd
].fd
= h
->read_fd
;
319 fd_structs
[*currfd
].events
= POLLIN
;
326 teardown_pipes (signal_info
** signals
, int count
)
329 for (i
= 0; i
< count
; ++i
) {
330 signal_info
* h
= signals
[i
];
332 if (mph_int_dec_test (&h
->pipecnt
)) {
335 if (h
->write_fd
!= 0)
344 wait_for_any (signal_info
** signals
, int count
, int *currfd
, struct pollfd
* fd_structs
, int timeout
, Mono_Posix_RuntimeIsShuttingDown shutting_down
)
349 struct timeval
*ptv
= NULL
;
351 tv
.tv_sec
= timeout
/ 1000;
352 tv
.tv_usec
= (timeout
% 1000)*1000;
355 r
= poll (fd_structs
, count
, timeout
);
356 } while (keep_trying (r
) && !shutting_down ());
363 for (i
= 0; i
< count
; ++i
) {
364 signal_info
* h
= signals
[i
];
365 if (fd_structs
[i
].revents
& POLLIN
) {
369 r
= read (h
->read_fd
, &c
, 1);
370 } while (keep_trying (r
) && !shutting_down ());
381 * returns: -1 on error:
383 * index into _signals array of signal that was generated on success
386 Mono_Unix_UnixSignal_WaitAny (void** _signals
, int count
, int timeout
/* milliseconds */, Mono_Posix_RuntimeIsShuttingDown shutting_down
)
390 struct pollfd fd_structs
[NUM_SIGNALS
];
392 signal_info
** signals
= (signal_info
**) _signals
;
394 if (count
> NUM_SIGNALS
)
397 if (acquire_mutex (&signals_mutex
) == -1)
400 r
= setup_pipes (signals
, count
, &fd_structs
[0], &currfd
);
402 release_mutex (&signals_mutex
);
405 r
= wait_for_any (signals
, count
, &currfd
, &fd_structs
[0], timeout
, shutting_down
);
408 if (acquire_mutex (&signals_mutex
) == -1)
411 teardown_pipes (signals
, count
);
413 release_mutex (&signals_mutex
);
418 #endif /* ndef HOST_WIN32 */