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)
124 Mono_Posix_Syscall_psignal (int sig, const char* s)
128 return errno == 0 ? 0 : -1;
131 #define NUM_SIGNALS 64
132 static signal_info signals[NUM_SIGNALS];
134 static int acquire_mutex (pthread_mutex_t *mutex)
137 while ((mr = pthread_mutex_lock (mutex)) == EAGAIN) {
138 /* try to acquire again */
140 if ((mr != 0) && (mr != EDEADLK)) {
147 static void release_mutex (pthread_mutex_t *mutex)
150 while ((mr = pthread_mutex_unlock (mutex)) == EAGAIN) {
151 /* try to release mutex again */
158 return r == -1 && errno == EINTR && !mono_runtime_is_shutting_down();
162 default_handler (int signum)
165 for (i = 0; i < NUM_SIGNALS; ++i) {
167 signal_info* h = &signals [i];
168 if (mph_int_get (&h->signum) != signum)
170 mph_int_inc (&h->count);
171 fd = mph_int_get (&h->write_fd);
175 pipecounter = mph_int_get (&h->pipecnt);
176 for (j = 0; j < pipecounter; ++j) {
178 do { r = write (fd, &c, 1); } while (keep_trying (r));
179 fsync (fd); /* force */
185 static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;
188 Mono_Unix_UnixSignal_install (int sig)
191 signal_info* h = NULL;
192 int have_handler = 0;
193 void* handler = NULL;
195 if (acquire_mutex (&signals_mutex) == -1)
198 #if defined (SIGRTMIN) && defined (SIGRTMAX)
199 /*The runtime uses some rt signals for itself so it's important to not override them.*/
200 if (sig >= SIGRTMIN && sig <= SIGRTMAX && count_handlers (sig) == 0) {
201 struct sigaction sinfo;
202 sigaction (sig, NULL, &sinfo);
203 if (sinfo.sa_handler != SIG_DFL || (void*)sinfo.sa_sigaction != (void*)SIG_DFL) {
204 pthread_mutex_unlock (&signals_mutex);
209 #endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/
211 for (i = 0; i < NUM_SIGNALS; ++i) {
212 if (h == NULL && signals [i].signum == 0) {
214 h->handler = signal (sig, default_handler);
215 if (h->handler == SIG_ERR) {
224 if (!have_handler && signals [i].signum == sig &&
225 signals [i].handler != default_handler) {
227 handler = signals [i].handler;
229 if (h && have_handler)
233 if (h && have_handler) {
235 h->handler = handler;
239 mph_int_set (&h->count, h->count, 0);
240 mph_int_set (&h->signum, h->signum, sig);
241 mph_int_set (&h->pipecnt, h->pipecnt, 0);
244 release_mutex (&signals_mutex);
250 count_handlers (int signum)
254 for (i = 0; i < NUM_SIGNALS; ++i) {
255 if (signals [i].signum == signum)
262 Mono_Unix_UnixSignal_uninstall (void* info)
267 if (acquire_mutex (&signals_mutex) == -1)
272 if (h == NULL || h < signals || h > &signals [NUM_SIGNALS])
275 /* last UnixSignal -- we can unregister */
276 if (h->have_handler && count_handlers (h->signum) == 1) {
277 mph_sighandler_t p = signal (h->signum, h->handler);
286 release_mutex (&signals_mutex);
292 setup_pipes (signal_info** signals, int count, struct pollfd *fd_structs, int *currfd)
296 for (i = 0; i < count; ++i) {
302 if (mph_int_get (&h->pipecnt) == 0) {
303 if ((r = pipe (filedes)) != 0) {
306 h->read_fd = filedes [0];
307 h->write_fd = filedes [1];
309 mph_int_inc (&h->pipecnt);
310 fd_structs[*currfd].fd = h->read_fd;
311 fd_structs[*currfd].events = POLLIN;
318 teardown_pipes (signal_info** signals, int count)
321 for (i = 0; i < count; ++i) {
322 signal_info* h = signals [i];
324 if (mph_int_dec_test (&h->pipecnt)) {
327 if (h->write_fd != 0)
336 wait_for_any (signal_info** signals, int count, int *currfd, struct pollfd* fd_structs, int timeout)
341 struct timeval *ptv = NULL;
343 tv.tv_sec = timeout / 1000;
344 tv.tv_usec = (timeout % 1000)*1000;
347 r = poll (fd_structs, count, timeout);
348 } while (keep_trying (r));
355 for (i = 0; i < count; ++i) {
356 signal_info* h = signals [i];
357 if (fd_structs[i].revents & POLLIN) {
361 r = read (h->read_fd, &c, 1);
362 } while (keep_trying (r));
373 * returns: -1 on error:
375 * index into _signals array of signal that was generated on success
378 Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */)
382 struct pollfd fd_structs[NUM_SIGNALS];
384 signal_info** signals = (signal_info**) _signals;
386 if (count > NUM_SIGNALS)
389 if (acquire_mutex (&signals_mutex) == -1)
392 r = setup_pipes (signals, count, &fd_structs[0], &currfd);
394 release_mutex (&signals_mutex);
397 r = wait_for_any (signals, count, &currfd, &fd_structs[0], timeout);
400 if (acquire_mutex (&signals_mutex) == -1)
403 teardown_pipes (signals, count);
405 release_mutex (&signals_mutex);
410 #endif /* ndef HOST_WIN32 */