[runtime] Add a mono_threads_close_thread_handle () function and use it to close...
[mono.git] / mono / utils / mono-threads.h
index 5f2a6e6aca6e65b44f2bb95b8d31b4235a0ad228..778deb5b51c584fa6a80b7f99ffd1b806078eda7 100644 (file)
@@ -14,8 +14,6 @@
 #include <mono/utils/mono-stack-unwinding.h>
 #include <mono/utils/mono-linked-list-set.h>
 #include <mono/utils/mono-tls.h>
-#include <mono/utils/mono-threads-coop.h>
-#include <mono/utils/mono-threads-api.h>
 #include <mono/utils/mono-coop-semaphore.h>
 
 #include <mono/io-layer/io-layer.h>
@@ -34,6 +32,8 @@ typedef DWORD mono_native_thread_return_t;
 #define MONO_NATIVE_THREAD_ID_TO_UINT(tid) (tid)
 #define MONO_UINT_TO_NATIVE_THREAD_ID(tid) ((MonoNativeThreadId)(tid))
 
+typedef LPTHREAD_START_ROUTINE MonoThreadStart;
+
 #else
 
 #include <pthread.h>
@@ -58,6 +58,8 @@ typedef void* mono_native_thread_return_t;
 #define MONO_NATIVE_THREAD_ID_TO_UINT(tid) (gsize)(tid)
 #define MONO_UINT_TO_NATIVE_THREAD_ID(tid) (MonoNativeThreadId)(gsize)(tid)
 
+typedef gsize (*MonoThreadStart)(gpointer);
+
 #endif /* #ifdef HOST_WIN32 */
 
 /*
@@ -191,10 +193,11 @@ typedef struct {
 #if defined(USE_POSIX_BACKEND)
        MonoSemType finish_resume_semaphore;
        gboolean syscall_break_signal;
-       gboolean suspend_can_continue;
        int signal;
 #endif
 
+       gboolean suspend_can_continue;
+
        /* This memory pool is used by coop GC to save stack data roots between GC unsafe regions */
        GByteArray *stackdata;
 
@@ -238,8 +241,11 @@ typedef struct {
 
        MonoThreadInfoInterruptToken *interrupt_token;
 
-       /* MonoHandleArena for coop handles */
-       gpointer handle_arena;
+       /* HandleStack for coop handles */
+       gpointer handle_stack;
+
+       /* Stack mark for targets that explicitly require one */
+       gpointer stack_mark;
 } MonoThreadInfo;
 
 typedef struct {
@@ -278,6 +284,23 @@ typedef enum {
 
 typedef SuspendThreadResult (*MonoSuspendThreadCallback) (THREAD_INFO_TYPE *info, gpointer user_data);
 
+/*
+ * Parameters to pass for thread creation
+ */
+typedef struct {
+       int priority;
+       guint32 creation_flags; 
+       guint32 stack_size;             
+} MonoThreadParm;
+
+typedef enum {
+       MONO_THREAD_PRIORITY_LOWEST       = 0,
+       MONO_THREAD_PRIORITY_BELOW_NORMAL = 1,
+       MONO_THREAD_PRIORITY_NORMAL       = 2,
+       MONO_THREAD_PRIORITY_ABOVE_NORMAL = 3,
+       MONO_THREAD_PRIORITY_HIGHEST      = 4,
+} MonoThreadPriority;
+
 static inline gboolean
 mono_threads_filter_tools_threads (THREAD_INFO_TYPE *info)
 {
@@ -357,9 +380,6 @@ mono_thread_info_lookup (MonoNativeThreadId id);
 gboolean
 mono_thread_info_resume (MonoNativeThreadId tid);
 
-MONO_API void
-mono_thread_info_set_name (MonoNativeThreadId tid, const char *name);
-
 void
 mono_thread_info_safe_suspend_and_run (MonoNativeThreadId id, gboolean interrupt_kernel, MonoSuspendThreadCallback callback, gpointer user_data);
 
@@ -396,7 +416,7 @@ mono_thread_info_is_async_context (void);
 void
 mono_thread_info_get_stack_bounds (guint8 **staddr, size_t *stsize);
 
-gboolean
+MONO_API gboolean
 mono_thread_info_yield (void);
 
 gint
@@ -414,8 +434,8 @@ mono_thread_info_tls_set (THREAD_INFO_TYPE *info, MonoTlsKey key, gpointer value
 void
 mono_thread_info_exit (void);
 
-HANDLE
-mono_thread_info_open_handle (void);
+void
+mono_thread_info_set_exited (THREAD_INFO_TYPE *info);
 
 void
 mono_thread_info_install_interrupt (void (*callback) (gpointer data), gpointer data, gboolean *interrupted);
@@ -445,7 +465,7 @@ gboolean
 mono_thread_info_is_live (THREAD_INFO_TYPE *info);
 
 HANDLE
-mono_threads_create_thread (LPTHREAD_START_ROUTINE start, gpointer arg, guint32 stack_size, guint32 creation_flags, MonoNativeThreadId *out_tid);
+mono_threads_create_thread (MonoThreadStart start, gpointer arg, MonoThreadParm *tp, MonoNativeThreadId *out_tid);
 
 int
 mono_threads_get_max_stack_size (void);
@@ -453,6 +473,9 @@ mono_threads_get_max_stack_size (void);
 HANDLE
 mono_threads_open_thread_handle (HANDLE handle, MonoNativeThreadId tid);
 
+void
+mono_threads_close_thread_handle (HANDLE handle);
+
 MONO_API void
 mono_threads_attach_tools_thread (void);
 
@@ -472,11 +495,13 @@ mono_threads_pthread_kill (THREAD_INFO_TYPE *info, int signum);
 This is called very early in the runtime, it cannot access any runtime facilities.
 
 */
-void mono_threads_init_platform (void); //ok
+void mono_threads_suspend_init (void); //ok
 
-void mono_threads_init_coop (void);
+void mono_threads_platform_init (void);
 
-void mono_threads_init_abort_syscall (void);
+void mono_threads_coop_init (void);
+
+void mono_threads_abort_syscall_init (void);
 
 /*
 This begins async suspend. This function must do the following:
@@ -487,7 +512,7 @@ This begins async suspend. This function must do the following:
 
 If begin suspend fails the thread must be left uninterrupted and resumed.
 */
-gboolean mono_threads_core_begin_async_suspend (THREAD_INFO_TYPE *info, gboolean interrupt_kernel);
+gboolean mono_threads_suspend_begin_async_suspend (THREAD_INFO_TYPE *info, gboolean interrupt_kernel);
 
 /*
 This verifies the outcome of an async suspend operation.
@@ -495,7 +520,7 @@ This verifies the outcome of an async suspend operation.
 Some targets, such as posix, verify suspend results assynchronously. Suspend results must be
 available (in a non blocking way) after mono_threads_wait_pending_operations completes.
 */
-gboolean mono_threads_core_check_suspend_result (THREAD_INFO_TYPE *info);
+gboolean mono_threads_suspend_check_suspend_result (THREAD_INFO_TYPE *info);
 
 /*
 This begins async resume. This function must do the following:
@@ -504,21 +529,27 @@ This begins async resume. This function must do the following:
 - Notify the target to resume.
 - Register the thread for pending ack with mono_threads_add_to_pending_operation_set if needed.
 */
-gboolean mono_threads_core_begin_async_resume (THREAD_INFO_TYPE *info);
-
-void mono_threads_platform_register (THREAD_INFO_TYPE *info); //ok
-void mono_threads_platform_free (THREAD_INFO_TYPE *info);
-void mono_threads_core_abort_syscall (THREAD_INFO_TYPE *info);
-gboolean mono_threads_core_needs_abort_syscall (void);
-HANDLE mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start, gpointer arg, guint32 stack_size, guint32 creation_flags, MonoNativeThreadId *out_tid);
-void mono_threads_core_resume_created (THREAD_INFO_TYPE *info, MonoNativeThreadId tid);
-void mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize);
-gboolean mono_threads_core_yield (void);
-void mono_threads_core_exit (int exit_code);
-void mono_threads_core_unregister (THREAD_INFO_TYPE *info);
-HANDLE mono_threads_core_open_handle (void);
-HANDLE mono_threads_core_open_thread_handle (HANDLE handle, MonoNativeThreadId tid);
-void mono_threads_core_set_name (MonoNativeThreadId tid, const char *name);
+gboolean mono_threads_suspend_begin_async_resume (THREAD_INFO_TYPE *info);
+
+void mono_threads_suspend_register (THREAD_INFO_TYPE *info); //ok
+void mono_threads_suspend_free (THREAD_INFO_TYPE *info);
+void mono_threads_suspend_abort_syscall (THREAD_INFO_TYPE *info);
+gboolean mono_threads_suspend_needs_abort_syscall (void);
+
+void mono_threads_platform_register (THREAD_INFO_TYPE *info);
+void mono_threads_platform_unregister (THREAD_INFO_TYPE *info);
+HANDLE mono_threads_platform_create_thread (MonoThreadStart start, gpointer arg, MonoThreadParm *, MonoNativeThreadId *out_tid);
+void mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize);
+gboolean mono_threads_platform_yield (void);
+void mono_threads_platform_exit (int exit_code);
+HANDLE mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid);
+void mono_threads_platform_close_thread_handle (HANDLE handle);
+void mono_threads_platform_set_exited (THREAD_INFO_TYPE *info);
+void mono_threads_platform_describe (THREAD_INFO_TYPE *info, GString *text);
+void mono_threads_platform_own_mutex (THREAD_INFO_TYPE *info, gpointer mutex_handle);
+void mono_threads_platform_disown_mutex (THREAD_INFO_TYPE *info, gpointer mutex_handle);
+MonoThreadPriority mono_threads_platform_get_priority (THREAD_INFO_TYPE *info);
+gboolean mono_threads_platform_set_priority (THREAD_INFO_TYPE *info, MonoThreadPriority priority);
 
 void mono_threads_coop_begin_global_suspend (void);
 void mono_threads_coop_end_global_suspend (void);
@@ -532,6 +563,9 @@ mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2);
 gboolean
 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg);
 
+MONO_API void
+mono_native_thread_set_name (MonoNativeThreadId tid, const char *name);
+
 /*Mach specific internals */
 void mono_threads_init_dead_letter (void);
 void mono_threads_install_dead_letter (void);
@@ -580,7 +614,6 @@ typedef enum {
 } MonoDoBlockingResult;
 
 typedef enum {
-       DoneBlockingAborted, //blocking was aborted and not properly restored, poll the state
        DoneBlockingOk, //exited blocking fine
        DoneBlockingWait, //thread should end suspended
 } MonoDoneBlockingResult;
@@ -588,9 +621,9 @@ typedef enum {
 
 typedef enum {
        AbortBlockingIgnore, //Ignore
-       AbortBlockingIgnoreAndPoll, //Ignore and pool
+       AbortBlockingIgnoreAndPoll, //Ignore and poll
        AbortBlockingOk, //Abort worked
-       AbortBlockingOkAndPool, //Abort worked, but pool before
+       AbortBlockingWait, //Abort worked, but should wait for resume
 } MonoAbortBlockingResult;
 
 
@@ -601,7 +634,6 @@ MonoRequestAsyncSuspendResult mono_threads_transition_request_async_suspension (
 MonoSelfSupendResult mono_threads_transition_state_poll (THREAD_INFO_TYPE *info);
 MonoResumeResult mono_threads_transition_request_resume (THREAD_INFO_TYPE* info);
 gboolean mono_threads_transition_finish_async_suspend (THREAD_INFO_TYPE* info);
-void mono_threads_transition_async_suspend_compensation (THREAD_INFO_TYPE* info);
 MonoDoBlockingResult mono_threads_transition_do_blocking (THREAD_INFO_TYPE* info);
 MonoDoneBlockingResult mono_threads_transition_done_blocking (THREAD_INFO_TYPE* info);
 MonoAbortBlockingResult mono_threads_transition_abort_blocking (THREAD_INFO_TYPE* info);
@@ -609,7 +641,7 @@ MonoAbortBlockingResult mono_threads_transition_abort_blocking (THREAD_INFO_TYPE
 MonoThreadUnwindState* mono_thread_info_get_suspend_state (THREAD_INFO_TYPE *info);
 
 gpointer
-mono_threads_cookie_for_reset_blocking_start (THREAD_INFO_TYPE *info, int reset_blocking_count);
+mono_threads_enter_gc_unsafe_region_cookie (void);
 
 
 void mono_thread_info_wait_for_resume (THREAD_INFO_TYPE *info);
@@ -624,12 +656,30 @@ gboolean mono_thread_info_in_critical_location (THREAD_INFO_TYPE *info);
 gboolean mono_thread_info_begin_suspend (THREAD_INFO_TYPE *info);
 gboolean mono_thread_info_begin_resume (THREAD_INFO_TYPE *info);
 
-gboolean
-mono_thread_info_check_suspend_result (THREAD_INFO_TYPE *info);
-
 void mono_threads_add_to_pending_operation_set (THREAD_INFO_TYPE* info); //XXX rename to something to reflect the fact that this is used for both suspend and resume
 gboolean mono_threads_wait_pending_operations (void);
 void mono_threads_begin_global_suspend (void);
 void mono_threads_end_global_suspend (void);
 
+gboolean
+mono_thread_info_is_current (THREAD_INFO_TYPE *info);
+
+gpointer
+mono_thread_info_get_handle (THREAD_INFO_TYPE *info);
+
+void
+mono_thread_info_describe (THREAD_INFO_TYPE *info, GString *text);
+
+void
+mono_thread_info_own_mutex (THREAD_INFO_TYPE *info, gpointer mutex_handle);
+
+void
+mono_thread_info_disown_mutex (THREAD_INFO_TYPE *info, gpointer mutex_handle);
+
+MonoThreadPriority
+mono_thread_info_get_priority (THREAD_INFO_TYPE *info);
+
+gboolean
+mono_thread_info_set_priority (THREAD_INFO_TYPE *info, MonoThreadPriority priority);
+
 #endif /* __MONO_THREADS_H__ */