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>
31 typedef void (*mph_sighandler_t)(int);
32 typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info;
34 static int count_handlers (int signum);
37 Mono_Posix_Stdlib_SIG_DFL (void)
43 Mono_Posix_Stdlib_SIG_ERR (void)
49 Mono_Posix_Stdlib_SIG_IGN (void)
55 Mono_Posix_Stdlib_InvokeSignalHandler (int signum, void *handler)
57 mph_sighandler_t _h = (mph_sighandler_t) handler;
61 int Mono_Posix_SIGRTMIN (void)
65 #else /* def SIGRTMIN */
67 #endif /* ndef SIGRTMIN */
70 int Mono_Posix_SIGRTMAX (void)
74 #else /* def SIGRTMAX */
76 #endif /* ndef SIGRTMAX */
79 int Mono_Posix_FromRealTimeSignum (int offset, int *r)
86 #if defined (SIGRTMIN) && defined (SIGRTMAX)
87 if ((offset < 0) || (SIGRTMIN > SIGRTMAX - offset)) {
93 #else /* defined (SIGRTMIN) && defined (SIGRTMAX) */
98 #endif /* defined (SIGRTMIN) && defined (SIGRTMAX) */
103 #ifdef WAPI_ATOMIC_ASM
104 #define mph_int_get(p) InterlockedExchangeAdd ((p), 0)
105 #define mph_int_inc(p) InterlockedIncrement ((p))
106 #define mph_int_dec_test(p) (InterlockedDecrement ((p)) == 0)
107 #define mph_int_set(p,o,n) InterlockedExchange ((p), (n))
108 #elif GLIB_CHECK_VERSION(2,4,0)
109 #define mph_int_get(p) g_atomic_int_get ((p))
110 #define mph_int_inc(p) do {g_atomic_int_inc ((p));} while (0)
111 #define mph_int_dec_test(p) g_atomic_int_dec_and_test ((p))
112 #define mph_int_set(p,o,n) do { \
113 while (!g_atomic_int_compare_and_exchange ((p), (o), (n))) {} \
116 #define mph_int_get(p) (*(p))
117 #define mph_int_inc(p) do { (*(p))++; } while (0)
118 #define mph_int_dec_test(p) (--(*(p)) == 0)
119 #define mph_int_set(p,o,n) do { *(p) = n; } while (0)
123 Mono_Posix_Syscall_psignal (int sig, const char* s)
127 return errno == 0 ? 0 : -1;
130 #define NUM_SIGNALS 64
131 static signal_info signals[NUM_SIGNALS];
133 static int acquire_mutex (pthread_mutex_t *mutex)
136 while ((mr = pthread_mutex_lock (mutex)) == EAGAIN) {
137 /* try to acquire again */
139 if ((mr != 0) && (mr != EDEADLK)) {
146 static void release_mutex (pthread_mutex_t *mutex)
149 while ((mr = pthread_mutex_unlock (mutex)) == EAGAIN) {
150 /* try to release mutex again */
155 default_handler (int signum)
158 for (i = 0; i < NUM_SIGNALS; ++i) {
160 signal_info* h = &signals [i];
161 if (mph_int_get (&h->signum) != signum)
163 mph_int_inc (&h->count);
164 fd = mph_int_get (&h->write_fd);
168 pipecounter = mph_int_get (&h->pipecnt);
169 for (j = 0; j < pipecounter; ++j) {
171 do { r = write (fd, &c, 1); } while (r == -1 && errno == EINTR);
172 fsync (fd); /* force */
178 static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;
181 Mono_Unix_UnixSignal_install (int sig)
184 signal_info* h = NULL;
185 int have_handler = 0;
186 void* handler = NULL;
188 if (acquire_mutex (&signals_mutex) == -1)
191 #if defined (SIGRTMIN) && defined (SIGRTMAX)
192 /*The runtime uses some rt signals for itself so it's important to not override them.*/
193 if (sig >= SIGRTMIN && sig <= SIGRTMAX && count_handlers (sig) == 0) {
194 struct sigaction sinfo;
195 sigaction (sig, NULL, &sinfo);
196 if (sinfo.sa_handler != SIG_DFL || (void*)sinfo.sa_sigaction != (void*)SIG_DFL) {
197 pthread_mutex_unlock (&signals_mutex);
202 #endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/
204 for (i = 0; i < NUM_SIGNALS; ++i) {
205 if (h == NULL && signals [i].signum == 0) {
207 h->handler = signal (sig, default_handler);
208 if (h->handler == SIG_ERR) {
217 if (!have_handler && signals [i].signum == sig &&
218 signals [i].handler != default_handler) {
220 handler = signals [i].handler;
222 if (h && have_handler)
226 if (h && have_handler) {
228 h->handler = handler;
232 mph_int_set (&h->count, h->count, 0);
233 mph_int_set (&h->signum, h->signum, sig);
234 mph_int_set (&h->pipecnt, h->pipecnt, 0);
237 release_mutex (&signals_mutex);
243 count_handlers (int signum)
247 for (i = 0; i < NUM_SIGNALS; ++i) {
248 if (signals [i].signum == signum)
255 Mono_Unix_UnixSignal_uninstall (void* info)
260 if (acquire_mutex (&signals_mutex) == -1)
265 if (h == NULL || h < signals || h > &signals [NUM_SIGNALS])
268 /* last UnixSignal -- we can unregister */
269 if (h->have_handler && count_handlers (h->signum) == 1) {
270 mph_sighandler_t p = signal (h->signum, h->handler);
279 release_mutex (&signals_mutex);
285 setup_pipes (signal_info** signals, int count, struct pollfd *fd_structs, int *currfd)
289 for (i = 0; i < count; ++i) {
295 if (mph_int_get (&h->pipecnt) == 0) {
296 if ((r = pipe (filedes)) != 0) {
299 h->read_fd = filedes [0];
300 h->write_fd = filedes [1];
302 mph_int_inc (&h->pipecnt);
303 fd_structs[*currfd].fd = h->read_fd;
304 fd_structs[*currfd].events = POLLIN;
311 teardown_pipes (signal_info** signals, int count)
314 for (i = 0; i < count; ++i) {
315 signal_info* h = signals [i];
317 if (mph_int_dec_test (&h->pipecnt)) {
320 if (h->write_fd != 0)
329 wait_for_any (signal_info** signals, int count, int *currfd, struct pollfd* fd_structs, int timeout)
334 struct timeval *ptv = NULL;
336 tv.tv_sec = timeout / 1000;
337 tv.tv_usec = (timeout % 1000)*1000;
340 r = poll (fd_structs, count, timeout);
341 } while (r == -1 && errno == EINTR);
348 for (i = 0; i < count; ++i) {
349 signal_info* h = signals [i];
350 if (fd_structs[i].revents & POLLIN) {
354 r = read (h->read_fd, &c, 1);
355 } while (r == -1 && errno == EINTR);
366 * returns: -1 on error:
368 * index into _signals array of signal that was generated on success
371 Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */)
375 struct pollfd fd_structs[NUM_SIGNALS];
377 signal_info** signals = (signal_info**) _signals;
379 if (count > NUM_SIGNALS)
382 if (acquire_mutex (&signals_mutex) == -1)
385 r = setup_pipes (signals, count, &fd_structs[0], &currfd);
387 release_mutex (&signals_mutex);
390 r = wait_for_any (signals, count, &currfd, &fd_structs[0], timeout);
393 if (acquire_mutex (&signals_mutex) == -1)
396 teardown_pipes (signals, count);
398 release_mutex (&signals_mutex);
403 #endif /* ndef HOST_WIN32 */