X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Futils%2Fmono-threads-posix.c;h=80f283f8878725d6fbb4635ce2b67f434a3d94a3;hb=d4b4c2dbbc019bc3b25e197f1faa0ba4506df05d;hp=dcbde725bd50a6cef22f4e126843fb9c2eb62610;hpb=7eb1f72f17ab66615499e560759b5c47d9372101;p=mono.git diff --git a/mono/utils/mono-threads-posix.c b/mono/utils/mono-threads-posix.c index dcbde725bd5..80f283f8878 100644 --- a/mono/utils/mono-threads-posix.c +++ b/mono/utils/mono-threads-posix.c @@ -9,18 +9,13 @@ #include -#if defined(__OpenBSD__) || defined(__FreeBSD__) -#include -#include -#endif - #include #include #include #include -#include #include #include +#include #include @@ -28,12 +23,8 @@ extern int tkill (pid_t tid, int signal); #endif -#if defined(PLATFORM_MACOSX) && defined(HAVE_PTHREAD_GET_STACKADDR_NP) -void *pthread_get_stackaddr_np(pthread_t); -size_t pthread_get_stacksize_np(pthread_t); -#endif - #if defined(_POSIX_VERSION) || defined(__native_client__) +#include #include #if defined(__native_client__) @@ -71,6 +62,7 @@ inner_start_thread (void *arg) info = mono_thread_info_attach (&result); info->runtime_thread = TRUE; + info->handle = handle; if (flags & CREATE_SUSPENDED) { info->create_suspended = TRUE; @@ -92,19 +84,19 @@ inner_start_thread (void *arg) result = start_func (t_arg); /* - g_assert (!mono_domain_get ()); - mono_thread_info_dettach (); + mono_thread_info_detach (); */ #if defined(__native_client__) nacl_shutdown_gc_thread(); #endif - wapi_thread_set_exit_code (GPOINTER_TO_UINT (result), handle); - - // FIXME: Why is this needed ? - mono_gc_pthread_exit (NULL); + wapi_thread_handle_set_exited (handle, GPOINTER_TO_UINT (result)); + /* This is needed by mono_threads_core_unregister () which is called later */ + info->handle = NULL; + g_assert (mono_threads_get_callbacks ()->thread_exit); + mono_threads_get_callbacks ()->thread_exit (NULL); g_assert_not_reached (); return result; } @@ -150,8 +142,8 @@ mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer /* Actually start the thread */ res = mono_threads_get_callbacks ()->mono_gc_pthread_create (&thread, &attr, inner_start_thread, &start_info); if (res) { - // FIXME: - g_assert_not_reached (); + MONO_SEM_DESTROY (&(start_info.registered)); + return NULL; } /* Wait until the thread register itself in various places */ @@ -177,118 +169,95 @@ mono_threads_core_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid) MONO_SEM_POST (&info->create_suspended_sem); } -void -mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize) +gboolean +mono_threads_core_yield (void) { -#if defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP) - /* Mac OS X */ - *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self()); - *stsize = pthread_get_stacksize_np (pthread_self()); - - -#ifdef TARGET_OSX - /* - * Mavericks reports stack sizes as 512kb: - * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590 - * https://bugs.openjdk.java.net/browse/JDK-8020753 - */ - if (*stsize == 512 * 1024) - *stsize = 2048 * mono_pagesize (); -#endif - - /* staddr points to the start of the stack, not the end */ - *staddr -= *stsize; - - /* When running under emacs, sometimes staddr is not aligned to a page size */ - *staddr = (guint8*)((gssize)*staddr & ~(mono_pagesize() - 1)); - return; - -#elif (defined(HAVE_PTHREAD_GETATTR_NP) || defined(HAVE_PTHREAD_ATTR_GET_NP)) && defined(HAVE_PTHREAD_ATTR_GETSTACK) - /* Linux, BSD */ - - pthread_attr_t attr; - guint8 *current = (guint8*)&attr; - - *staddr = NULL; - *stsize = (size_t)-1; - - pthread_attr_init (&attr); - -#if defined(HAVE_PTHREAD_GETATTR_NP) - /* Linux */ - pthread_getattr_np (pthread_self(), &attr); + return sched_yield () == 0; +} -#elif defined(HAVE_PTHREAD_ATTR_GET_NP) - /* BSD */ - pthread_attr_get_np (pthread_self(), &attr); +void +mono_threads_core_exit (int exit_code) +{ + MonoThreadInfo *current = mono_thread_info_current (); -#else -#error Cannot determine which API is needed to retrieve pthread attributes. +#if defined(__native_client__) + nacl_shutdown_gc_thread(); #endif - pthread_attr_getstack (&attr, (void**)staddr, stsize); - pthread_attr_destroy (&attr); - - if (*staddr) - g_assert ((current > *staddr) && (current < *staddr + *stsize)); + wapi_thread_handle_set_exited (current->handle, exit_code); - /* When running under emacs, sometimes staddr is not aligned to a page size */ - *staddr = (guint8*)((gssize)*staddr & ~(mono_pagesize () - 1)); - return; - -#elif defined(__OpenBSD__) - /* OpenBSD */ - /* TODO : Determine if this code is actually still needed. It may already be covered by the case above. */ - - pthread_attr_t attr; - guint8 *current = (guint8*)&attr; + g_assert (mono_threads_get_callbacks ()->thread_exit); + mono_threads_get_callbacks ()->thread_exit (NULL); +} - *staddr = NULL; - *stsize = (size_t)-1; +void +mono_threads_core_unregister (MonoThreadInfo *info) +{ + if (info->handle) { + wapi_thread_handle_set_exited (info->handle, 0); + info->handle = NULL; + } +} - pthread_attr_init (&attr); +HANDLE +mono_threads_core_open_handle (void) +{ + MonoThreadInfo *info; - stack_t ss; - int rslt; + info = mono_thread_info_current (); + g_assert (info); - rslt = pthread_stackseg_np(pthread_self(), &ss); - g_assert (rslt == 0); + if (!info->handle) + info->handle = wapi_create_thread_handle (); + else + wapi_ref_thread_handle (info->handle); + return info->handle; +} - *staddr = (guint8*)((size_t)ss.ss_sp - ss.ss_size); - *stsize = ss.ss_size; +int +mono_threads_get_max_stack_size (void) +{ + struct rlimit lim; + + /* If getrlimit fails, we don't enforce any limits. */ + if (getrlimit (RLIMIT_STACK, &lim)) + return INT_MAX; + /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */ + if (lim.rlim_max > (rlim_t)INT_MAX) + return INT_MAX; + return (int)lim.rlim_max; +} - pthread_attr_destroy (&attr); +HANDLE +mono_threads_core_open_thread_handle (HANDLE handle, MonoNativeThreadId tid) +{ + wapi_ref_thread_handle (handle); - if (*staddr) - g_assert ((current > *staddr) && (current < *staddr + *stsize)); + return handle; +} - /* When running under emacs, sometimes staddr is not aligned to a page size */ - *staddr = (guint8*)((gssize)*staddr & ~(mono_pagesize () - 1)); - return; +gpointer +mono_threads_core_prepare_interrupt (HANDLE thread_handle) +{ + return wapi_prepare_interrupt_thread (thread_handle); +} -#elif defined(sun) || defined(__native_client__) - /* Solaris/Illumos, NaCl */ - pthread_attr_t attr; - pthread_attr_init (&attr); - pthread_attr_getstacksize (&attr, &stsize); - pthread_attr_destroy (&attr); - *staddr = NULL; - return; +void +mono_threads_core_finish_interrupt (gpointer wait_handle) +{ + wapi_finish_interrupt_thread (wait_handle); +} -#else - /* FIXME: It'd be better to use the 'error' preprocessor macro here so we know - at compile-time if the target platform isn't supported. */ -#warning "Unable to determine how to retrieve a thread's stack-bounds for this platform in 'mono_thread_get_stack_bounds()'." - *staddr = NULL; - *stsize = 0; - return; -#endif +void +mono_threads_core_self_interrupt (void) +{ + wapi_self_interrupt (); } -gboolean -mono_threads_core_yield (void) +void +mono_threads_core_clear_interruption (void) { - return sched_yield () == 0; + wapi_clear_interruption (); } #if !defined (__MACH__) @@ -470,6 +439,22 @@ mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg) return pthread_create (tid, NULL, func, arg) == 0; } +void +mono_threads_core_set_name (MonoNativeThreadId tid, const char *name) +{ +#ifdef HAVE_PTHREAD_SETNAME_NP + if (!name) { + pthread_setname_np (tid, ""); + } else { + char n [16]; + + strncpy (n, name, 16); + n [15] = '\0'; + pthread_setname_np (tid, n); + } +#endif +} + #endif /*!defined (__MACH__)*/ #endif