Revert "[runtime] Disable finally_block_ending_in_dead_bb.exe on wrench, it fails...
[mono.git] / support / signal.c
index 8a2e761c38f2e079a643b45a8fd7245be0828899..6adf5463878ef2693b8d3ecc101ea5809b88cb28 100644 (file)
 #include "map.h"
 #include "mph.h"
 
-#ifndef PLATFORM_WIN32
+#ifndef HOST_WIN32
 #include <sys/time.h>
 #include <sys/types.h>
+#if defined(HAVE_POLL_H)
 #include <poll.h>
+#elif defined(HAVE_SYS_POLL_H)
+#include <sys/poll.h>
+#endif
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
-#include <mono/io-layer/atomic.h>
+#include <mono/utils/atomic.h>
+#include <mono/metadata/appdomain.h>
 #endif
 
 G_BEGIN_DECLS
@@ -31,7 +36,9 @@ G_BEGIN_DECLS
 typedef void (*mph_sighandler_t)(int);
 typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info;
 
+#ifndef HOST_WIN32
 static int count_handlers (int signum);
+#endif
 
 void*
 Mono_Posix_Stdlib_SIG_DFL (void)
@@ -98,9 +105,9 @@ int Mono_Posix_FromRealTimeSignum (int offset, int *r)
 #endif /* defined (SIGRTMIN) && defined (SIGRTMAX) */
 }
 
-#ifndef PLATFORM_WIN32
+#ifndef HOST_WIN32
 
-#ifdef WAPI_ATOMIC_ASM
+#ifndef WAPI_NO_ATOMIC_ASM
        #define mph_int_get(p)     InterlockedExchangeAdd ((p), 0)
        #define mph_int_inc(p)     InterlockedIncrement ((p))
        #define mph_int_dec_test(p)     (InterlockedDecrement ((p)) == 0)
@@ -119,6 +126,7 @@ int Mono_Posix_FromRealTimeSignum (int offset, int *r)
        #define mph_int_set(p,o,n) do { *(p) = n; } while (0)
 #endif
 
+#if HAVE_PSIGNAL
 int
 Mono_Posix_Syscall_psignal (int sig, const char* s)
 {
@@ -126,6 +134,7 @@ Mono_Posix_Syscall_psignal (int sig, const char* s)
        psignal (sig, s);
        return errno == 0 ? 0 : -1;
 }
+#endif  /* def HAVE_PSIGNAL */
 
 #define NUM_SIGNALS 64
 static signal_info signals[NUM_SIGNALS];
@@ -151,6 +160,12 @@ static void release_mutex (pthread_mutex_t *mutex)
        }
 }
 
+static inline int
+keep_trying (int r)
+{
+       return r == -1 && errno == EINTR;
+}
+
 static void
 default_handler (int signum)
 {
@@ -168,7 +183,7 @@ default_handler (int signum)
                        pipecounter = mph_int_get (&h->pipecnt);
                        for (j = 0; j < pipecounter; ++j) {
                                int r;
-                               do { r = write (fd, &c, 1); } while (r == -1 && errno == EINTR);
+                               do { r = write (fd, &c, 1); } while (keep_trying (r));
                                fsync (fd); /* force */
                        }
                }
@@ -180,6 +195,7 @@ static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;
 void*
 Mono_Unix_UnixSignal_install (int sig)
 {
+#if defined(HAVE_SIGNAL)
        int i;
        signal_info* h = NULL; 
        int have_handler = 0;
@@ -237,6 +253,10 @@ Mono_Unix_UnixSignal_install (int sig)
        release_mutex (&signals_mutex);
 
        return h;
+#else
+       g_error ("signal() is not supported by this platform");
+       return 0;
+#endif
 }
 
 static int
@@ -254,6 +274,7 @@ count_handlers (int signum)
 int
 Mono_Unix_UnixSignal_uninstall (void* info)
 {
+#if defined(HAVE_SIGNAL)       
        signal_info* h;
        int r = -1;
 
@@ -279,6 +300,10 @@ Mono_Unix_UnixSignal_uninstall (void* info)
        release_mutex (&signals_mutex);
 
        return r;
+#else
+       g_error ("signal() is not supported by this platform");
+       return 0;
+#endif
 }
 
 static int
@@ -326,7 +351,7 @@ teardown_pipes (signal_info** signals, int count)
 }
 
 static int
-wait_for_any (signal_info** signals, int count, int *currfd, struct pollfd* fd_structs, int timeout)
+wait_for_any (signal_info** signals, int count, int *currfd, struct pollfd* fd_structs, int timeout, Mono_Posix_RuntimeIsShuttingDown shutting_down)
 {
        int r, idx;
        do {
@@ -338,7 +363,7 @@ wait_for_any (signal_info** signals, int count, int *currfd, struct pollfd* fd_s
                        ptv = &tv;
                }
                r = poll (fd_structs, count, timeout);
-       } while (r == -1 && errno == EINTR);
+       } while (keep_trying (r) && !shutting_down ());
 
        idx = -1;
        if (r == 0)
@@ -352,7 +377,7 @@ wait_for_any (signal_info** signals, int count, int *currfd, struct pollfd* fd_s
                                char c;
                                do {
                                        r = read (h->read_fd, &c, 1);
-                               } while (r == -1 && errno == EINTR);
+                               } while (keep_trying (r) && !shutting_down ());
                                if (idx == -1)
                                        idx = i;
                        }
@@ -368,7 +393,7 @@ wait_for_any (signal_info** signals, int count, int *currfd, struct pollfd* fd_s
  *          index into _signals array of signal that was generated on success
  */
 int
-Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */)
+Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */, Mono_Posix_RuntimeIsShuttingDown shutting_down)
 {
        int r;
        int currfd = 0;
@@ -387,7 +412,7 @@ Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* millise
        release_mutex (&signals_mutex);
 
        if (r == 0) {
-               r = wait_for_any (signals, count, &currfd, &fd_structs[0], timeout);
+               r = wait_for_any (signals, count, &currfd, &fd_structs[0], timeout, shutting_down);
        }
 
        if (acquire_mutex (&signals_mutex) == -1)
@@ -400,7 +425,7 @@ Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* millise
        return r;
 }
 
-#endif /* ndef PLATFORM_WIN32 */
+#endif /* ndef HOST_WIN32 */
 
 
 G_END_DECLS