#include <signal.h>
#include <string.h>
-#if defined(__OpenBSD__)
+#if defined(__OpenBSD__) || defined(__FreeBSD__)
#include <pthread.h>
#include <pthread_np.h>
#endif
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
+
/*#define THREAD_DEBUG(a) do { a; } while (0)*/
#define THREAD_DEBUG(a)
/*#define THREAD_WAIT_DEBUG(a) do { a; } while (0)*/
mono_array_set (thread->cached_culture_info, MonoObject*, i, NULL);
}
- /* if the thread is not in the hash it has been removed already */
- if (!handle_remove (thread)) {
- /* This needs to be called even if handle_remove () fails */
- if (mono_thread_cleanup_fn)
- mono_thread_cleanup_fn (thread);
- return;
- }
- mono_release_type_locks (thread);
-
ensure_synch_cs_set (thread);
EnterCriticalSection (thread->synch_cs);
thread->state &= ~ThreadState_Background;
LeaveCriticalSection (thread->synch_cs);
+
+ /*
+ An interruption request has leaked to cleanup. Adjust the global counter.
+
+ This can happen is the abort source thread finds the abortee (this) thread
+ in unmanaged code. If this thread never trips back to managed code or check
+ the local flag it will be left set and positively unbalance the global counter.
+ Leaving the counter unbalanced will cause a performance degradation since all threads
+ will now keep checking their local flags all the time.
+ */
+ if (InterlockedExchange (&thread->interruption_requested, 0))
+ InterlockedDecrement (&thread_interruption_requested);
+
+ /* if the thread is not in the hash it has been removed already */
+ if (!handle_remove (thread)) {
+ /* This needs to be called even if handle_remove () fails */
+ if (mono_thread_cleanup_fn)
+ mono_thread_cleanup_fn (thread);
+ return;
+ }
+ mono_release_type_locks (thread);
+
mono_profiler_thread_end (thread->tid);
if (thread == mono_thread_internal_current ())
static guint32 WINAPI start_wrapper(void *data)
{
-#ifdef HAVE_SGEN_GC
volatile int dummy;
/* Avoid scanning the frames above this frame during a GC */
mono_gc_set_stack_end ((void*)&dummy);
-#endif
return start_wrapper_internal (data);
}
THREAD_DEBUG (g_message ("%s: mono_thread_detach for %p (%"G_GSIZE_FORMAT")", __func__, thread, (gsize)thread->internal_thread->tid));
+ mono_profiler_thread_end (thread->internal_thread->tid);
+
thread_cleanup (thread->internal_thread);
SET_CURRENT_OBJECT (NULL);
}
void
-ves_icall_System_Threading_Thread_SetName_internal (MonoInternalThread *this_obj, MonoString *name)
+mono_thread_set_name_internal (MonoInternalThread *this_obj, MonoString *name, gboolean managed)
{
ensure_synch_cs_set (this_obj);
EnterCriticalSection (this_obj->synch_cs);
-
- if (this_obj->name) {
+
+ if (this_obj->flags & MONO_THREAD_FLAG_NAME_SET) {
LeaveCriticalSection (this_obj->synch_cs);
mono_raise_exception (mono_get_exception_invalid_operation ("Thread.Name can only be set once."));
}
else
this_obj->name = NULL;
+
+ if (managed)
+ this_obj->flags |= MONO_THREAD_FLAG_NAME_SET;
LeaveCriticalSection (this_obj->synch_cs);
if (this_obj->name) {
}
}
+void
+ves_icall_System_Threading_Thread_SetName_internal (MonoInternalThread *this_obj, MonoString *name)
+{
+ mono_thread_set_name_internal (this_obj, name, TRUE);
+}
+
/* If the array is already in the requested domain, we just return it,
otherwise we return a copy in that domain. */
static MonoArray*
static uintptr_t* static_reference_bitmaps [NUM_STATIC_DATA_IDX];
-#ifdef HAVE_SGEN_GC
static void
mark_tls_slots (void *addr, MonoGCMarkFunc mark_func)
{
}
}
}
-#endif
/*
* mono_alloc_static_data
gpointer* static_data = *static_data_ptr;
if (!static_data) {
static void* tls_desc = NULL;
-#ifdef HAVE_SGEN_GC
- if (!tls_desc)
+ if (mono_gc_user_markers_supported () && !tls_desc)
tls_desc = mono_gc_make_root_descr_user (mark_tls_slots);
-#endif
static_data = mono_gc_alloc_fixed (static_data_size [0], threadlocal?tls_desc:NULL);
*static_data_ptr = static_data;
static_data [0] = static_data;
for (i = 1; i <= idx; ++i) {
if (static_data [i])
continue;
-#ifdef HAVE_SGEN_GC
- static_data [i] = threadlocal?g_malloc0 (static_data_size [i]):mono_gc_alloc_fixed (static_data_size [i], NULL);
-#else
- static_data [i] = mono_gc_alloc_fixed (static_data_size [i], NULL);
-#endif
+ if (mono_gc_user_markers_supported () && threadlocal)
+ static_data [i] = g_malloc0 (static_data_size [i]);
+ else
+ static_data [i] = mono_gc_alloc_fixed (static_data_size [i], NULL);
}
}
for (i = 1; i < NUM_STATIC_DATA_IDX; ++i) {
if (!static_data [i])
continue;
-#ifdef HAVE_SGEN_GC
- if (threadlocal)
+ if (mono_gc_user_markers_supported () && threadlocal)
g_free (static_data [i]);
else
mono_gc_free_fixed (static_data [i]);
-#else
- mono_gc_free_fixed (static_data [i]);
-#endif
}
mono_gc_free_fixed (static_data);
}
/* TlsDatum is a struct, so we subtract the object header size offset */
bitmap = mono_class_compute_bitmap (klass, default_bitmap, sizeof (default_bitmap) * 8, - (int)(sizeof (MonoObject) / sizeof (gpointer)), &max_set, FALSE);
size = mono_type_size (type->type, &align);
- tls_offset = mono_alloc_special_static_data (SPECIAL_STATIC_THREAD, size, align, bitmap, max_set);
+ tls_offset = mono_alloc_special_static_data (SPECIAL_STATIC_THREAD, size, align, (uintptr_t*)bitmap, max_set);
if (bitmap != default_bitmap)
g_free (bitmap);
tlsrec = g_new0 (MonoTlsDataRecord, 1);
mono_thread_exit ();
return NULL;
+ } else if (thread->pending_exception) {
+ MonoException *exc;
+
+ exc = thread->pending_exception;
+ thread->pending_exception = NULL;
+
+ LeaveCriticalSection (thread->synch_cs);
+ return exc;
} else if (thread->thread_interrupt_requested) {
thread->thread_interrupt_requested = FALSE;
if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 1)
return NULL;
+ InterlockedIncrement (&thread_interruption_requested);
if (!running_managed || is_running_protected_wrapper ()) {
/* Can't stop while in unmanaged code. Increase the global interruption
request count. When exiting the unmanaged method the count will be
checked and the thread will be interrupted. */
- InterlockedIncrement (&thread_interruption_requested);
if (mono_thread_notify_pending_exc_fn && !running_managed)
/* The JIT will notify the thread about the interruption */
/*
* mono_set_pending_exception:
*
- * Set the pending exception of the current thread to EXC. On platforms which
- * support it, the exception will be thrown when execution returns to managed code.
- * On other platforms, this function is equivalent to mono_raise_exception ().
- * Internal calls which report exceptions using this function instead of
- * raise_exception () might be called by JITted code using a more efficient calling
- * convention.
+ * Set the pending exception of the current thread to EXC.
+ * The exception will be thrown when execution returns to managed code.
*/
void
mono_set_pending_exception (MonoException *exc)
if (thread == NULL)
return;
- if (mono_thread_notify_pending_exc_fn) {
- MONO_OBJECT_SETREF (thread, pending_exception, exc);
+ MONO_OBJECT_SETREF (thread, pending_exception, exc);
- mono_thread_notify_pending_exc_fn ();
- } else {
- /* No way to notify the JIT about the exception, have to throw it now */
- mono_raise_exception (exc);
- }
+ mono_thread_request_interruption (FALSE);
}
/**
return ret;
}
-static MonoClassField *execution_context_field;
+//static MonoClassField *execution_context_field;
static MonoObject**
get_execution_context_addr (void)
{
MonoDomain *domain = mono_domain_get ();
- guint32 offset;
+ guint32 offset = domain->execution_context_field_offset;
- if (!execution_context_field) {
- execution_context_field = mono_class_get_field_from_name (mono_defaults.thread_class,
- "_ec");
- g_assert (execution_context_field);
- }
+ if (!offset) {
+ MonoClassField *field = mono_class_get_field_from_name (mono_defaults.thread_class, "_ec");
+ g_assert (field);
- g_assert (mono_class_try_get_vtable (domain, mono_defaults.appdomain_class));
+ g_assert (mono_class_try_get_vtable (domain, mono_defaults.appdomain_class));
- mono_domain_lock (domain);
- offset = GPOINTER_TO_UINT (g_hash_table_lookup (domain->special_static_fields, execution_context_field));
- mono_domain_unlock (domain);
- g_assert (offset);
+ mono_domain_lock (domain);
+ offset = GPOINTER_TO_UINT (g_hash_table_lookup (domain->special_static_fields, field));
+ mono_domain_unlock (domain);
+ g_assert (offset);
+
+ domain->execution_context_field_offset = offset;
+ }
return (MonoObject**) mono_get_special_static_data (offset);
}
mono_thread_info_resume (mono_thread_info_get_tid (info));
return;
}
+ InterlockedIncrement (&thread_interruption_requested);
ji = mono_thread_info_get_last_managed (info);
protected_wrapper = ji && mono_threads_is_critical_method (ji->method);
mono_thread_info_setup_async_call (info, self_interrupt_thread, NULL);
mono_thread_info_resume (mono_thread_info_get_tid (info));
} else {
+ gpointer interrupt_handle;
/*
* This will cause waits to be broken.
* It will also prevent the thread from entering a wait, so if the thread returns
* functions in the io-layer until the signal handler calls QueueUserAPC which will
* make it return.
*/
- InterlockedIncrement (&thread_interruption_requested);
+#ifndef HOST_WIN32
+ interrupt_handle = wapi_prepare_interrupt_thread (thread->handle);
+#endif
mono_thread_info_resume (mono_thread_info_get_tid (info));
#ifndef HOST_WIN32
- wapi_interrupt_thread (thread->handle);
+ wapi_finish_interrupt_thread (interrupt_handle);
#endif
}
/*FIXME we need to wait for interruption to complete -- figure out how much into interruption we should wait for here*/
if (running_managed && !protected_wrapper) {
transition_to_suspended (thread);
} else {
+ gpointer interrupt_handle;
+
if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 0)
InterlockedIncrement (&thread_interruption_requested);
#ifndef HOST_WIN32
if (interrupt)
- wapi_interrupt_thread (thread->handle);
+ interrupt_handle = wapi_prepare_interrupt_thread (thread->handle);
#endif
mono_thread_info_resume (mono_thread_info_get_tid (info));
+#ifndef HOST_WIN32
+ if (interrupt)
+ wapi_finish_interrupt_thread (interrupt_handle);
+#endif
LeaveCriticalSection (thread->synch_cs);
}
}