2 * <signal.h> wrapper functions.
5 * Jonathan Pryor (jonpryor@vt.edu)
6 * Jonathan Pryor (jpryor@novell.com)
8 * Copyright (C) 2004-2005 Jonathan Pryor
9 * Copyright (C) 2008 Novell, Inc.
17 #ifndef PLATFORM_WIN32
19 #include <sys/types.h>
24 #include <mono/io-layer/atomic.h>
29 typedef void (*mph_sighandler_t)(int);
30 typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info;
33 Mono_Posix_Stdlib_SIG_DFL (void)
39 Mono_Posix_Stdlib_SIG_ERR (void)
45 Mono_Posix_Stdlib_SIG_IGN (void)
51 Mono_Posix_Stdlib_InvokeSignalHandler (int signum, void *handler)
53 mph_sighandler_t _h = (mph_sighandler_t) handler;
57 int Mono_Posix_SIGRTMIN (void)
61 #else /* def SIGRTMIN */
63 #endif /* ndef SIGRTMIN */
66 int Mono_Posix_SIGRTMAX (void)
70 #else /* def SIGRTMAX */
72 #endif /* ndef SIGRTMAX */
75 int Mono_Posix_FromRealTimeSignum (int offset, int *r)
78 #if defined (SIGRTMIN) && defined (SIGRTMAX)
79 if ((offset < 0) || (SIGRTMIN > SIGRTMAX - offset))
88 #ifndef PLATFORM_WIN32
90 #ifdef WAPI_ATOMIC_ASM
91 #define mph_int_get(p) InterlockedExchangeAdd ((p), 0)
92 #define mph_int_inc(p) InterlockedIncrement ((p))
93 #define mph_int_set(p,o,n) InterlockedExchange ((p), (n))
94 #elif GLIB_CHECK_VERSION(2,4,0)
95 #define mph_int_get(p) g_atomic_int_get ((p))
96 #define mph_int_inc(p) do {g_atomic_int_inc ((p));} while (0)
97 #define mph_int_set(p,o,n) do { \
98 while (!g_atomic_int_compare_and_exchange ((p), (o), (n))) {} \
101 #define mph_int_get(p) (*(p))
102 #define mph_int_inc(p) do { (*(p))++; } while (0)
103 #define mph_int_set(p,o,n) do { *(p) = n; } while (0)
107 Mono_Posix_Syscall_psignal (int sig, const char* s)
111 return errno == 0 ? 0 : -1;
114 #define NUM_SIGNALS 64
115 static signal_info signals[NUM_SIGNALS];
118 default_handler (int signum)
121 for (i = 0; i < NUM_SIGNALS; ++i) {
123 signal_info* h = &signals [i];
124 if (mph_int_get (&h->signum) != signum)
126 mph_int_inc (&h->count);
127 fd = mph_int_get (&h->write_fd);
135 static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;
138 Mono_Unix_UnixSignal_install (int sig)
141 signal_info* h = NULL;
142 int have_handler = 0;
143 void* handler = NULL;
145 mr = pthread_mutex_lock (&signals_mutex);
151 for (i = 0; i < NUM_SIGNALS; ++i) {
152 if (h == NULL && signals [i].signum == 0) {
154 h->handler = signal (sig, default_handler);
155 if (h->handler == SIG_ERR) {
164 if (!have_handler && signals [i].signum == sig &&
165 signals [i].handler != default_handler) {
167 handler = signals [i].handler;
169 if (h && have_handler)
173 if (h && have_handler) {
175 h->handler = handler;
179 mph_int_set (&h->count, h->count, 0);
180 mph_int_set (&h->signum, h->signum, sig);
183 pthread_mutex_unlock (&signals_mutex);
189 count_handlers (int signum)
193 for (i = 0; i < NUM_SIGNALS; ++i) {
194 if (signals [i].signum == signum)
201 Mono_Unix_UnixSignal_uninstall (void* info)
206 mr = pthread_mutex_lock (&signals_mutex);
214 if (h == NULL || h < signals || h > &signals [NUM_SIGNALS])
217 /* last UnixSignal -- we can unregister */
218 if (h->have_handler && count_handlers (h->signum) == 1) {
219 mph_sighandler_t p = signal (h->signum, h->handler);
228 pthread_mutex_unlock (&signals_mutex);
234 setup_pipes (signal_info** signals, int count, fd_set *read_fds, int *max_fd)
237 for (i = 0; i < count; ++i) {
241 if ((r = pipe (filedes)) != 0) {
245 h->read_fd = filedes [0];
246 h->write_fd = filedes [1];
247 if (h->read_fd > *max_fd)
248 *max_fd = h->read_fd;
249 FD_SET (h->read_fd, read_fds);
255 teardown_pipes (signal_info** signals, int count)
258 for (i = 0; i < count; ++i) {
259 signal_info* h = signals [i];
262 if (h->write_fd != 0)
270 wait_for_any (signal_info** signals, int count, int max_fd, fd_set* read_fds, int timeout)
275 struct timeval *ptv = NULL;
277 tv.tv_sec = timeout / 1000;
278 tv.tv_usec = (timeout % 1000)*1000;
282 r = select (max_fd + 1, read_fds, NULL, NULL, ptv);
283 } while (r == -1 && errno == EINTR);
290 for (i = 0; i < count; ++i) {
291 signal_info* h = signals [i];
292 if (FD_ISSET (h->read_fd, read_fds)) {
294 read (h->read_fd, &c, 1); /* ignore any error */
305 * returns: -1 on error:
307 * index into _signals array of signal that was generated on success
310 Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */)
316 signal_info** signals = (signal_info**) _signals;
318 mr = pthread_mutex_lock (&signals_mutex);
326 r = setup_pipes (signals, count, &read_fds, &max_fd);
328 r = wait_for_any (signals, count, max_fd, &read_fds, timeout);
330 teardown_pipes (signals, count);
332 pthread_mutex_unlock (&signals_mutex);
337 #endif /* ndef PLATFORM_WIN32 */