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>
26 #include <mono/io-layer/atomic.h>
27 #include <mono/metadata/appdomain.h>
32 typedef void (*mph_sighandler_t)(int);
33 typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info;
35 static int count_handlers (int signum);
38 Mono_Posix_Stdlib_SIG_DFL (void)
44 Mono_Posix_Stdlib_SIG_ERR (void)
50 Mono_Posix_Stdlib_SIG_IGN (void)
56 Mono_Posix_Stdlib_InvokeSignalHandler (int signum, void *handler)
58 mph_sighandler_t _h = (mph_sighandler_t) handler;
62 int Mono_Posix_SIGRTMIN (void)
66 #else /* def SIGRTMIN */
68 #endif /* ndef SIGRTMIN */
71 int Mono_Posix_SIGRTMAX (void)
75 #else /* def SIGRTMAX */
77 #endif /* ndef SIGRTMAX */
80 int Mono_Posix_FromRealTimeSignum (int offset, int *r)
87 #if defined (SIGRTMIN) && defined (SIGRTMAX)
88 if ((offset < 0) || (SIGRTMIN > SIGRTMAX - offset)) {
94 #else /* defined (SIGRTMIN) && defined (SIGRTMAX) */
99 #endif /* defined (SIGRTMIN) && defined (SIGRTMAX) */
104 #ifdef WAPI_ATOMIC_ASM
105 #define mph_int_get(p) InterlockedExchangeAdd ((p), 0)
106 #define mph_int_inc(p) InterlockedIncrement ((p))
107 #define mph_int_dec_test(p) (InterlockedDecrement ((p)) == 0)
108 #define mph_int_set(p,o,n) InterlockedExchange ((p), (n))
109 #elif GLIB_CHECK_VERSION(2,4,0)
110 #define mph_int_get(p) g_atomic_int_get ((p))
111 #define mph_int_inc(p) do {g_atomic_int_inc ((p));} while (0)
112 #define mph_int_dec_test(p) g_atomic_int_dec_and_test ((p))
113 #define mph_int_set(p,o,n) do { \
114 while (!g_atomic_int_compare_and_exchange ((p), (o), (n))) {} \
117 #define mph_int_get(p) (*(p))
118 #define mph_int_inc(p) do { (*(p))++; } while (0)
119 #define mph_int_dec_test(p) (--(*(p)) == 0)
120 #define mph_int_set(p,o,n) do { *(p) = n; } while (0)
125 Mono_Posix_Syscall_psignal (int sig, const char* s)
129 return errno == 0 ? 0 : -1;
131 #endif /* def HAVE_PSIGNAL */
133 #define NUM_SIGNALS 64
134 static signal_info signals[NUM_SIGNALS];
136 static int acquire_mutex (pthread_mutex_t *mutex)
139 while ((mr = pthread_mutex_lock (mutex)) == EAGAIN) {
140 /* try to acquire again */
142 if ((mr != 0) && (mr != EDEADLK)) {
149 static void release_mutex (pthread_mutex_t *mutex)
152 while ((mr = pthread_mutex_unlock (mutex)) == EAGAIN) {
153 /* try to release mutex again */
160 return r == -1 && errno == EINTR;
164 default_handler (int signum)
167 for (i = 0; i < NUM_SIGNALS; ++i) {
169 signal_info* h = &signals [i];
170 if (mph_int_get (&h->signum) != signum)
172 mph_int_inc (&h->count);
173 fd = mph_int_get (&h->write_fd);
177 pipecounter = mph_int_get (&h->pipecnt);
178 for (j = 0; j < pipecounter; ++j) {
180 do { r = write (fd, &c, 1); } while (keep_trying (r));
181 fsync (fd); /* force */
187 static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;
190 Mono_Unix_UnixSignal_install (int sig)
193 signal_info* h = NULL;
194 int have_handler = 0;
195 void* handler = NULL;
197 if (acquire_mutex (&signals_mutex) == -1)
200 #if defined (SIGRTMIN) && defined (SIGRTMAX)
201 /*The runtime uses some rt signals for itself so it's important to not override them.*/
202 if (sig >= SIGRTMIN && sig <= SIGRTMAX && count_handlers (sig) == 0) {
203 struct sigaction sinfo;
204 sigaction (sig, NULL, &sinfo);
205 if (sinfo.sa_handler != SIG_DFL || (void*)sinfo.sa_sigaction != (void*)SIG_DFL) {
206 pthread_mutex_unlock (&signals_mutex);
211 #endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/
213 for (i = 0; i < NUM_SIGNALS; ++i) {
214 if (h == NULL && signals [i].signum == 0) {
216 h->handler = signal (sig, default_handler);
217 if (h->handler == SIG_ERR) {
226 if (!have_handler && signals [i].signum == sig &&
227 signals [i].handler != default_handler) {
229 handler = signals [i].handler;
231 if (h && have_handler)
235 if (h && have_handler) {
237 h->handler = handler;
241 mph_int_set (&h->count, h->count, 0);
242 mph_int_set (&h->signum, h->signum, sig);
243 mph_int_set (&h->pipecnt, h->pipecnt, 0);
246 release_mutex (&signals_mutex);
252 count_handlers (int signum)
256 for (i = 0; i < NUM_SIGNALS; ++i) {
257 if (signals [i].signum == signum)
264 Mono_Unix_UnixSignal_uninstall (void* info)
269 if (acquire_mutex (&signals_mutex) == -1)
274 if (h == NULL || h < signals || h > &signals [NUM_SIGNALS])
277 /* last UnixSignal -- we can unregister */
278 if (h->have_handler && count_handlers (h->signum) == 1) {
279 mph_sighandler_t p = signal (h->signum, h->handler);
288 release_mutex (&signals_mutex);
294 setup_pipes (signal_info** signals, int count, struct pollfd *fd_structs, int *currfd)
298 for (i = 0; i < count; ++i) {
304 if (mph_int_get (&h->pipecnt) == 0) {
305 if ((r = pipe (filedes)) != 0) {
308 h->read_fd = filedes [0];
309 h->write_fd = filedes [1];
311 mph_int_inc (&h->pipecnt);
312 fd_structs[*currfd].fd = h->read_fd;
313 fd_structs[*currfd].events = POLLIN;
320 teardown_pipes (signal_info** signals, int count)
323 for (i = 0; i < count; ++i) {
324 signal_info* h = signals [i];
326 if (mph_int_dec_test (&h->pipecnt)) {
329 if (h->write_fd != 0)
338 wait_for_any (signal_info** signals, int count, int *currfd, struct pollfd* fd_structs, int timeout, Mono_Posix_RuntimeIsShuttingDown shutting_down)
343 struct timeval *ptv = NULL;
345 tv.tv_sec = timeout / 1000;
346 tv.tv_usec = (timeout % 1000)*1000;
349 r = poll (fd_structs, count, timeout);
350 } while (keep_trying (r) && !shutting_down ());
357 for (i = 0; i < count; ++i) {
358 signal_info* h = signals [i];
359 if (fd_structs[i].revents & POLLIN) {
363 r = read (h->read_fd, &c, 1);
364 } while (keep_trying (r) && !shutting_down ());
375 * returns: -1 on error:
377 * index into _signals array of signal that was generated on success
380 Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */, Mono_Posix_RuntimeIsShuttingDown shutting_down)
384 struct pollfd fd_structs[NUM_SIGNALS];
386 signal_info** signals = (signal_info**) _signals;
388 if (count > NUM_SIGNALS)
391 if (acquire_mutex (&signals_mutex) == -1)
394 r = setup_pipes (signals, count, &fd_structs[0], &currfd);
396 release_mutex (&signals_mutex);
399 r = wait_for_any (signals, count, &currfd, &fd_structs[0], timeout, shutting_down);
402 if (acquire_mutex (&signals_mutex) == -1)
405 teardown_pipes (signals, count);
407 release_mutex (&signals_mutex);
412 #endif /* ndef HOST_WIN32 */