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/io-layer/atomic.h>
31 #include <mono/metadata/appdomain.h>
36 typedef void (*mph_sighandler_t
)(int);
37 typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info
;
39 static int count_handlers (int signum
);
42 Mono_Posix_Stdlib_SIG_DFL (void)
48 Mono_Posix_Stdlib_SIG_ERR (void)
54 Mono_Posix_Stdlib_SIG_IGN (void)
60 Mono_Posix_Stdlib_InvokeSignalHandler (int signum
, void *handler
)
62 mph_sighandler_t _h
= (mph_sighandler_t
) handler
;
66 int Mono_Posix_SIGRTMIN (void)
70 #else /* def SIGRTMIN */
72 #endif /* ndef SIGRTMIN */
75 int Mono_Posix_SIGRTMAX (void)
79 #else /* def SIGRTMAX */
81 #endif /* ndef SIGRTMAX */
84 int Mono_Posix_FromRealTimeSignum (int offset
, int *r
)
91 #if defined (SIGRTMIN) && defined (SIGRTMAX)
92 if ((offset
< 0) || (SIGRTMIN
> SIGRTMAX
- offset
)) {
98 #else /* defined (SIGRTMIN) && defined (SIGRTMAX) */
103 #endif /* defined (SIGRTMIN) && defined (SIGRTMAX) */
108 #ifdef WAPI_ATOMIC_ASM
109 #define mph_int_get(p) InterlockedExchangeAdd ((p), 0)
110 #define mph_int_inc(p) InterlockedIncrement ((p))
111 #define mph_int_dec_test(p) (InterlockedDecrement ((p)) == 0)
112 #define mph_int_set(p,o,n) InterlockedExchange ((p), (n))
113 #elif GLIB_CHECK_VERSION(2,4,0)
114 #define mph_int_get(p) g_atomic_int_get ((p))
115 #define mph_int_inc(p) do {g_atomic_int_inc ((p));} while (0)
116 #define mph_int_dec_test(p) g_atomic_int_dec_and_test ((p))
117 #define mph_int_set(p,o,n) do { \
118 while (!g_atomic_int_compare_and_exchange ((p), (o), (n))) {} \
121 #define mph_int_get(p) (*(p))
122 #define mph_int_inc(p) do { (*(p))++; } while (0)
123 #define mph_int_dec_test(p) (--(*(p)) == 0)
124 #define mph_int_set(p,o,n) do { *(p) = n; } while (0)
129 Mono_Posix_Syscall_psignal (int sig
, const char* s
)
133 return errno
== 0 ? 0 : -1;
135 #endif /* def HAVE_PSIGNAL */
137 #define NUM_SIGNALS 64
138 static signal_info signals
[NUM_SIGNALS
];
140 static int acquire_mutex (pthread_mutex_t
*mutex
)
143 while ((mr
= pthread_mutex_lock (mutex
)) == EAGAIN
) {
144 /* try to acquire again */
146 if ((mr
!= 0) && (mr
!= EDEADLK
)) {
153 static void release_mutex (pthread_mutex_t
*mutex
)
156 while ((mr
= pthread_mutex_unlock (mutex
)) == EAGAIN
) {
157 /* try to release mutex again */
164 return r
== -1 && errno
== EINTR
;
168 default_handler (int signum
)
171 for (i
= 0; i
< NUM_SIGNALS
; ++i
) {
173 signal_info
* h
= &signals
[i
];
174 if (mph_int_get (&h
->signum
) != signum
)
176 mph_int_inc (&h
->count
);
177 fd
= mph_int_get (&h
->write_fd
);
181 pipecounter
= mph_int_get (&h
->pipecnt
);
182 for (j
= 0; j
< pipecounter
; ++j
) {
184 do { r
= write (fd
, &c
, 1); } while (keep_trying (r
));
185 fsync (fd
); /* force */
191 static pthread_mutex_t signals_mutex
= PTHREAD_MUTEX_INITIALIZER
;
194 Mono_Unix_UnixSignal_install (int sig
)
197 signal_info
* h
= NULL
;
198 int have_handler
= 0;
199 void* handler
= NULL
;
201 if (acquire_mutex (&signals_mutex
) == -1)
204 #if defined (SIGRTMIN) && defined (SIGRTMAX)
205 /*The runtime uses some rt signals for itself so it's important to not override them.*/
206 if (sig
>= SIGRTMIN
&& sig
<= SIGRTMAX
&& count_handlers (sig
) == 0) {
207 struct sigaction sinfo
;
208 sigaction (sig
, NULL
, &sinfo
);
209 if (sinfo
.sa_handler
!= SIG_DFL
|| (void*)sinfo
.sa_sigaction
!= (void*)SIG_DFL
) {
210 pthread_mutex_unlock (&signals_mutex
);
215 #endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/
217 for (i
= 0; i
< NUM_SIGNALS
; ++i
) {
218 if (h
== NULL
&& signals
[i
].signum
== 0) {
220 h
->handler
= signal (sig
, default_handler
);
221 if (h
->handler
== SIG_ERR
) {
230 if (!have_handler
&& signals
[i
].signum
== sig
&&
231 signals
[i
].handler
!= default_handler
) {
233 handler
= signals
[i
].handler
;
235 if (h
&& have_handler
)
239 if (h
&& have_handler
) {
241 h
->handler
= handler
;
245 mph_int_set (&h
->count
, h
->count
, 0);
246 mph_int_set (&h
->signum
, h
->signum
, sig
);
247 mph_int_set (&h
->pipecnt
, h
->pipecnt
, 0);
250 release_mutex (&signals_mutex
);
256 count_handlers (int signum
)
260 for (i
= 0; i
< NUM_SIGNALS
; ++i
) {
261 if (signals
[i
].signum
== signum
)
268 Mono_Unix_UnixSignal_uninstall (void* info
)
273 if (acquire_mutex (&signals_mutex
) == -1)
278 if (h
== NULL
|| h
< signals
|| h
> &signals
[NUM_SIGNALS
])
281 /* last UnixSignal -- we can unregister */
282 if (h
->have_handler
&& count_handlers (h
->signum
) == 1) {
283 mph_sighandler_t p
= signal (h
->signum
, h
->handler
);
292 release_mutex (&signals_mutex
);
298 setup_pipes (signal_info
** signals
, int count
, struct pollfd
*fd_structs
, int *currfd
)
302 for (i
= 0; i
< count
; ++i
) {
308 if (mph_int_get (&h
->pipecnt
) == 0) {
309 if ((r
= pipe (filedes
)) != 0) {
312 h
->read_fd
= filedes
[0];
313 h
->write_fd
= filedes
[1];
315 mph_int_inc (&h
->pipecnt
);
316 fd_structs
[*currfd
].fd
= h
->read_fd
;
317 fd_structs
[*currfd
].events
= POLLIN
;
324 teardown_pipes (signal_info
** signals
, int count
)
327 for (i
= 0; i
< count
; ++i
) {
328 signal_info
* h
= signals
[i
];
330 if (mph_int_dec_test (&h
->pipecnt
)) {
333 if (h
->write_fd
!= 0)
342 wait_for_any (signal_info
** signals
, int count
, int *currfd
, struct pollfd
* fd_structs
, int timeout
, Mono_Posix_RuntimeIsShuttingDown shutting_down
)
347 struct timeval
*ptv
= NULL
;
349 tv
.tv_sec
= timeout
/ 1000;
350 tv
.tv_usec
= (timeout
% 1000)*1000;
353 r
= poll (fd_structs
, count
, timeout
);
354 } while (keep_trying (r
) && !shutting_down ());
361 for (i
= 0; i
< count
; ++i
) {
362 signal_info
* h
= signals
[i
];
363 if (fd_structs
[i
].revents
& POLLIN
) {
367 r
= read (h
->read_fd
, &c
, 1);
368 } while (keep_trying (r
) && !shutting_down ());
379 * returns: -1 on error:
381 * index into _signals array of signal that was generated on success
384 Mono_Unix_UnixSignal_WaitAny (void** _signals
, int count
, int timeout
/* milliseconds */, Mono_Posix_RuntimeIsShuttingDown shutting_down
)
388 struct pollfd fd_structs
[NUM_SIGNALS
];
390 signal_info
** signals
= (signal_info
**) _signals
;
392 if (count
> NUM_SIGNALS
)
395 if (acquire_mutex (&signals_mutex
) == -1)
398 r
= setup_pipes (signals
, count
, &fd_structs
[0], &currfd
);
400 release_mutex (&signals_mutex
);
403 r
= wait_for_any (signals
, count
, &currfd
, &fd_structs
[0], timeout
, shutting_down
);
406 if (acquire_mutex (&signals_mutex
) == -1)
409 teardown_pipes (signals
, count
);
411 release_mutex (&signals_mutex
);
416 #endif /* ndef HOST_WIN32 */