* Authors:
* Jonathan Pryor (jonpryor@vt.edu)
* Jonathan Pryor (jpryor@novell.com)
+ * Tim Jenks (tim.jenks@realtimeworlds.com)
*
* Copyright (C) 2004-2005 Jonathan Pryor
* Copyright (C) 2008 Novell, Inc.
#include <signal.h>
-#ifndef PLATFORM_WIN32
+#include "map.h"
+#include "mph.h"
+
+#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/utils/atomic.h>
+#include <mono/metadata/appdomain.h>
#endif
-#include "map.h"
-#include "mph.h"
-
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)
{
_h (signum);
}
-#ifndef PLATFORM_WIN32
+int Mono_Posix_SIGRTMIN (void)
+{
+#ifdef SIGRTMIN
+ return SIGRTMIN;
+#else /* def SIGRTMIN */
+ return -1;
+#endif /* ndef SIGRTMIN */
+}
+
+int Mono_Posix_SIGRTMAX (void)
+{
+#ifdef SIGRTMAX
+ return SIGRTMAX;
+#else /* def SIGRTMAX */
+ return -1;
+#endif /* ndef SIGRTMAX */
+}
+
+int Mono_Posix_FromRealTimeSignum (int offset, int *r)
+{
+ if (NULL == r) {
+ errno = EINVAL;
+ return -1;
+ }
+ *r = 0;
+#if defined (SIGRTMIN) && defined (SIGRTMAX)
+ if ((offset < 0) || (SIGRTMIN > SIGRTMAX - offset)) {
+ errno = EINVAL;
+ return -1;
+ }
+ *r = SIGRTMIN+offset;
+ return 0;
+#else /* defined (SIGRTMIN) && defined (SIGRTMAX) */
+# ifdef ENOSYS
+ errno = ENOSYS;
+# endif /* ENOSYS */
+ return -1;
+#endif /* defined (SIGRTMIN) && defined (SIGRTMAX) */
+}
+
+#ifndef HOST_WIN32
+
+#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)
+ #define mph_int_set(p,o,n) InterlockedExchange ((p), (n))
+#elif GLIB_CHECK_VERSION(2,4,0)
+ #define mph_int_get(p) g_atomic_int_get ((p))
+ #define mph_int_inc(p) do {g_atomic_int_inc ((p));} while (0)
+ #define mph_int_dec_test(p) g_atomic_int_dec_and_test ((p))
+ #define mph_int_set(p,o,n) do { \
+ while (!g_atomic_int_compare_and_exchange ((p), (o), (n))) {} \
+ } while (0)
+#else
+ #define mph_int_get(p) (*(p))
+ #define mph_int_inc(p) do { (*(p))++; } while (0)
+ #define mph_int_dec_test(p) (--(*(p)) == 0)
+ #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)
{
psignal (sig, s);
return errno == 0 ? 0 : -1;
}
+#endif /* def HAVE_PSIGNAL */
#define NUM_SIGNALS 64
static signal_info signals[NUM_SIGNALS];
+static int acquire_mutex (pthread_mutex_t *mutex)
+{
+ int mr;
+ while ((mr = pthread_mutex_lock (mutex)) == EAGAIN) {
+ /* try to acquire again */
+ }
+ if ((mr != 0) && (mr != EDEADLK)) {
+ errno = mr;
+ return -1;
+ }
+ return 0;
+}
+
+static void release_mutex (pthread_mutex_t *mutex)
+{
+ int mr;
+ while ((mr = pthread_mutex_unlock (mutex)) == EAGAIN) {
+ /* try to release mutex again */
+ }
+}
+
+static inline int
+keep_trying (int r)
+{
+ return r == -1 && errno == EINTR;
+}
+
static void
default_handler (int signum)
{
int i;
for (i = 0; i < NUM_SIGNALS; ++i) {
+ int fd;
signal_info* h = &signals [i];
- if (h->signum != signum)
+ if (mph_int_get (&h->signum) != signum)
continue;
- ++h->count;
- if (h->write_fd > 0) {
+ mph_int_inc (&h->count);
+ fd = mph_int_get (&h->write_fd);
+ if (fd > 0) {
+ int j,pipecounter;
char c = signum;
- write (h->write_fd, &c, 1);
+ pipecounter = mph_int_get (&h->pipecnt);
+ for (j = 0; j < pipecounter; ++j) {
+ int r;
+ do { r = write (fd, &c, 1); } while (keep_trying (r));
+ fsync (fd); /* force */
+ }
}
}
}
void*
Mono_Unix_UnixSignal_install (int sig)
{
- int i, mr;
+#if defined(HAVE_SIGNAL)
+ int i;
signal_info* h = NULL;
int have_handler = 0;
- void* handler;
+ void* handler = NULL;
- mr = pthread_mutex_lock (&signals_mutex);
- if (mr != 0) {
- errno = mr;
+ if (acquire_mutex (&signals_mutex) == -1)
return NULL;
+
+#if defined (SIGRTMIN) && defined (SIGRTMAX)
+ /*The runtime uses some rt signals for itself so it's important to not override them.*/
+ if (sig >= SIGRTMIN && sig <= SIGRTMAX && count_handlers (sig) == 0) {
+ struct sigaction sinfo;
+ sigaction (sig, NULL, &sinfo);
+ if (sinfo.sa_handler != SIG_DFL || (void*)sinfo.sa_sigaction != (void*)SIG_DFL) {
+ pthread_mutex_unlock (&signals_mutex);
+ errno = EADDRINUSE;
+ return NULL;
+ }
}
+#endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/
for (i = 0; i < NUM_SIGNALS; ++i) {
if (h == NULL && signals [i].signum == 0) {
break;
}
else {
- h->signum = sig;
- h->count = 0;
h->have_handler = 1;
}
}
h->handler = handler;
}
- pthread_mutex_unlock (&signals_mutex);
+ if (h) {
+ mph_int_set (&h->count, h->count, 0);
+ mph_int_set (&h->signum, h->signum, sig);
+ mph_int_set (&h->pipecnt, h->pipecnt, 0);
+ }
+
+ release_mutex (&signals_mutex);
return h;
+#else
+ g_error ("signal() is not supported by this platform");
+ return 0;
+#endif
}
static int
int
Mono_Unix_UnixSignal_uninstall (void* info)
{
+#if defined(HAVE_SIGNAL)
signal_info* h;
- int mr, r = -1;
+ int r = -1;
- mr = pthread_mutex_lock (&signals_mutex);
- if (mr != 0) {
- errno = mr;
+ if (acquire_mutex (&signals_mutex) == -1)
return -1;
- }
h = info;
h->signum = 0;
}
- pthread_mutex_unlock (&signals_mutex);
+ release_mutex (&signals_mutex);
return r;
+#else
+ g_error ("signal() is not supported by this platform");
+ return 0;
+#endif
}
static int
-setup_pipes (signal_info** signals, int count, fd_set *read_fds, int *max_fd)
+setup_pipes (signal_info** signals, int count, struct pollfd *fd_structs, int *currfd)
{
- int i, r;
+ int i;
+ int r = 0;
for (i = 0; i < count; ++i) {
signal_info* h;
int filedes[2];
- if ((r = pipe (filedes)) != 0) {
- break;
- }
h = signals [i];
- h->read_fd = filedes [0];
- h->write_fd = filedes [1];
- if (h->read_fd > *max_fd)
- *max_fd = h->read_fd;
- FD_SET (h->read_fd, read_fds);
+
+ if (mph_int_get (&h->pipecnt) == 0) {
+ if ((r = pipe (filedes)) != 0) {
+ break;
+ }
+ h->read_fd = filedes [0];
+ h->write_fd = filedes [1];
+ }
+ mph_int_inc (&h->pipecnt);
+ fd_structs[*currfd].fd = h->read_fd;
+ fd_structs[*currfd].events = POLLIN;
+ ++(*currfd);
}
return r;
}
int i;
for (i = 0; i < count; ++i) {
signal_info* h = signals [i];
- if (h->read_fd != 0)
- close (h->read_fd);
- if (h->write_fd != 0)
- close (h->write_fd);
- h->read_fd = 0;
- h->write_fd = 0;
+
+ if (mph_int_dec_test (&h->pipecnt)) {
+ if (h->read_fd != 0)
+ close (h->read_fd);
+ if (h->write_fd != 0)
+ close (h->write_fd);
+ h->read_fd = 0;
+ h->write_fd = 0;
+ }
}
}
static int
-wait_for_any (signal_info** signals, int count, int max_fd, fd_set* read_fds, 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;
+ int r, idx;
do {
struct timeval tv;
struct timeval *ptv = NULL;
tv.tv_usec = (timeout % 1000)*1000;
ptv = &tv;
}
+ r = poll (fd_structs, count, timeout);
+ } while (keep_trying (r) && !shutting_down ());
- r = select (max_fd + 1, read_fds, NULL, NULL, ptv);
- } while (r == -1 && errno == EINTR);
-
- if (r > 0) {
+ idx = -1;
+ if (r == 0)
+ idx = timeout;
+ else if (r > 0) {
int i;
for (i = 0; i < count; ++i) {
signal_info* h = signals [i];
- if (FD_ISSET (h->read_fd, read_fds)) {
+ if (fd_structs[i].revents & POLLIN) {
+ int r;
char c;
- read (h->read_fd, &c, 1); /* ignore error */
+ do {
+ r = read (h->read_fd, &c, 1);
+ } while (keep_trying (r) && !shutting_down ());
+ if (idx == -1)
+ idx = i;
}
}
}
- return r;
+ return idx;
}
/*
* returns: -1 on error:
- * 0 on timeout
- * > 0 on success
+ * timeout on timeout
+ * 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)
{
- fd_set read_fds;
- int mr, r;
- int max_fd = 0;
+ int r;
+ int currfd = 0;
+ struct pollfd fd_structs[NUM_SIGNALS];
signal_info** signals = (signal_info**) _signals;
- mr = pthread_mutex_lock (&signals_mutex);
- if (mr != 0) {
- errno = mr;
+ if (count > NUM_SIGNALS)
+ return -1;
+
+ if (acquire_mutex (&signals_mutex) == -1)
return -1;
- }
- FD_ZERO (&read_fds);
+ r = setup_pipes (signals, count, &fd_structs[0], &currfd);
+
+ release_mutex (&signals_mutex);
- r = setup_pipes (signals, count, &read_fds, &max_fd);
if (r == 0) {
- r = wait_for_any (signals, count, max_fd, &read_fds, timeout);
+ r = wait_for_any (signals, count, &currfd, &fd_structs[0], timeout, shutting_down);
}
+
+ if (acquire_mutex (&signals_mutex) == -1)
+ return -1;
+
teardown_pipes (signals, count);
- pthread_mutex_unlock (&signals_mutex);
+ release_mutex (&signals_mutex);
return r;
}
-#endif /* ndef PLATFORM_WIN32 */
+#endif /* ndef HOST_WIN32 */
G_END_DECLS