6 * Rodrigo Kumpera (kumpera@gmail.com)
11 #ifndef __MONO_THREADS_H__
12 #define __MONO_THREADS_H__
14 #include <mono/utils/mono-forward-internal.h>
15 #include <mono/utils/mono-os-semaphore.h>
16 #include <mono/utils/mono-stack-unwinding.h>
17 #include <mono/utils/mono-linked-list-set.h>
18 #include <mono/utils/lock-free-alloc.h>
19 #include <mono/utils/lock-free-queue.h>
20 #include <mono/utils/mono-tls.h>
21 #include <mono/utils/mono-coop-semaphore.h>
22 #include <mono/utils/os-event.h>
23 #include <mono/utils/refcount.h>
24 #include <mono/utils/mono-error-internals.h>
32 typedef DWORD MonoNativeThreadId
;
33 typedef HANDLE MonoNativeThreadHandle
;
35 typedef DWORD mono_native_thread_return_t
;
36 typedef DWORD mono_thread_start_return_t
;
38 #define MONO_NATIVE_THREAD_ID_TO_UINT(tid) (tid)
39 #define MONO_UINT_TO_NATIVE_THREAD_ID(tid) ((MonoNativeThreadId)(tid))
41 typedef LPTHREAD_START_ROUTINE MonoThreadStart
;
48 #include <mono/utils/mach-support.h>
50 typedef thread_port_t MonoNativeThreadHandle
;
56 typedef pid_t MonoNativeThreadHandle
;
58 #endif /* defined(__MACH__) */
60 typedef pthread_t MonoNativeThreadId
;
62 typedef void* mono_native_thread_return_t
;
63 typedef gsize mono_thread_start_return_t
;
65 #define MONO_NATIVE_THREAD_ID_TO_UINT(tid) (gsize)(tid)
66 #define MONO_UINT_TO_NATIVE_THREAD_ID(tid) (MonoNativeThreadId)(gsize)(tid)
68 typedef gsize (*MonoThreadStart
)(gpointer
);
70 #if !defined(__HAIKU__)
71 #define MONO_THREADS_PLATFORM_HAS_ATTR_SETSCHED
72 #endif /* !defined(__HAIKU__) */
74 #endif /* #ifdef HOST_WIN32 */
76 #define MONO_NATIVE_THREAD_HANDLE_TO_GPOINTER(handle) ((gpointer)(gsize)(handle))
77 #define MONO_GPOINTER_TO_NATIVE_THREAD_HANDLE(handle) ((MonoNativeThreadHandle)(gsize)(handle))
79 #define MONO_INFINITE_WAIT ((guint32) 0xFFFFFFFF)
87 THREAD_INFO_TYPE is a way to make the mono-threads module parametric - or sort of.
88 The GC using mono-threads might extend the MonoThreadInfo struct to add its own
89 data, this avoid a pointer indirection on what is on a lot of hot paths.
91 But extending MonoThreadInfo has de disavantage that all functions here return type
92 would require a cast, something like the following:
100 ((MyThreadInfo*)mono_thread_info_current ())->stuff = 1;
102 While porting sgen to use mono-threads, the number of casts required was too much and
103 code ended up looking horrible. So we use this cute little hack. The idea is that
104 whomever is including this header can set the expected type to be used by functions here
105 and reduce the number of casts drastically.
108 #ifndef THREAD_INFO_TYPE
109 #define THREAD_INFO_TYPE MonoThreadInfo
112 /* Mono Threads internal configuration knows*/
114 /* If this is defined, use the signals backed on Mach. Debug only as signals can't be made usable on OSX. */
115 // #define USE_SIGNALS_ON_MACH
118 #define USE_WASM_BACKEND
119 #elif defined (_POSIX_VERSION)
120 #if defined (__MACH__) && !defined (USE_SIGNALS_ON_MACH)
121 #define USE_MACH_BACKEND
123 #define USE_POSIX_BACKEND
126 #define USE_WINDOWS_BACKEND
128 #error "no backend support for current platform"
129 #endif /* defined (_POSIX_VERSION) */
132 STATE_STARTING
= 0x00,
133 STATE_DETACHED
= 0x01,
135 STATE_RUNNING
= 0x02,
136 STATE_ASYNC_SUSPENDED
= 0x03,
137 STATE_SELF_SUSPENDED
= 0x04,
138 STATE_ASYNC_SUSPEND_REQUESTED
= 0x05,
140 STATE_BLOCKING
= 0x06,
141 STATE_BLOCKING_ASYNC_SUSPENDED
= 0x07,
142 /* FIXME: All the transitions from STATE_SELF_SUSPENDED and
143 * STATE_BLOCKING_SELF_SUSPENDED are the same - they should be the same
145 STATE_BLOCKING_SELF_SUSPENDED
= 0x08,
146 STATE_BLOCKING_SUSPEND_REQUESTED
= 0x09,
150 THREAD_STATE_MASK
= 0x007F,
151 THREAD_SUSPEND_COUNT_MASK
= 0xFF00,
152 THREAD_SUSPEND_COUNT_SHIFT
= 8,
153 THREAD_SUSPEND_COUNT_MAX
= 0xFF,
155 THREAD_SUSPEND_NO_SAFEPOINTS_MASK
= 0x0080,
156 THREAD_SUSPEND_NO_SAFEPOINTS_SHIFT
= 7,
158 SELF_SUSPEND_STATE_INDEX
= 0,
159 ASYNC_SUSPEND_STATE_INDEX
= 1,
163 * These flags control how the rest of the runtime will see and interact with
168 * No flags means it's a normal thread that takes part in all runtime
171 MONO_THREAD_INFO_FLAGS_NONE
= 0,
173 * The thread will not be suspended by the STW machinery. The thread is not
174 * allowed to allocate or access managed memory at all, nor execute managed
177 MONO_THREAD_INFO_FLAGS_NO_GC
= 1,
179 * The thread will not be subject to profiler sampling signals.
181 MONO_THREAD_INFO_FLAGS_NO_SAMPLE
= 2,
182 } MonoThreadInfoFlags
;
184 G_ENUM_FUNCTIONS (MonoThreadInfoFlags
)
186 typedef struct _MonoThreadInfoInterruptToken MonoThreadInfoInterruptToken
;
188 typedef struct _MonoThreadInfo
{
189 MonoLinkedListSetNode node
;
190 guint32 small_id
; /*Used by hazard pointers */
191 MonoNativeThreadHandle native_handle
; /* Valid on mach, android and Windows */
195 * Must not be changed directly, and especially not by other threads. Use
196 * mono_thread_info_get/set_flags () to manipulate this.
198 volatile gint32 flags
;
200 /*Tells if this thread was created by the runtime or not.*/
201 gboolean runtime_thread
;
203 /* Max stack bounds, all valid addresses must be between [stack_start_limit, stack_end[ */
204 void *stack_start_limit
, *stack_end
;
206 /* suspend machinery, fields protected by suspend_semaphore */
207 MonoSemType suspend_semaphore
;
210 MonoSemType resume_semaphore
;
212 /* only needed by the posix backend */
213 #if defined(USE_POSIX_BACKEND)
214 MonoSemType finish_resume_semaphore
;
215 gboolean syscall_break_signal
;
219 gboolean suspend_can_continue
;
221 /* This memory pool is used by coop GC to save stack data roots between GC unsafe regions */
222 GByteArray
*stackdata
;
224 /*In theory, only the posix backend needs this, but having it on mach/win32 simplifies things a lot.*/
225 MonoThreadUnwindState thread_saved_state
[2]; //0 is self suspend, 1 is async suspend.
227 /*async call machinery, thread MUST be suspended before accessing those fields*/
228 void (*async_target
)(void*);
232 If true, this thread is running a critical region of code and cannot be suspended.
233 A critical session is implicitly started when you call mono_thread_info_safe_suspend_sync
234 and is ended when you call either mono_thread_info_resume or mono_thread_info_finish_suspend.
236 gboolean inside_critical_region
;
239 * If TRUE, the thread is in async context. Code can use this information to avoid async-unsafe
240 * operations like locking without having to pass an 'async' parameter around.
242 gboolean is_async_context
;
245 * Values of TLS variables for this thread.
246 * This can be used to obtain the values of TLS variable for threads
247 * other than the current one.
249 gpointer tls
[TLS_KEY_NUM
];
251 /* IO layer handle for this thread */
252 /* Set when the thread is started, or in _wapi_thread_duplicate () */
253 MonoThreadHandle
*handle
;
255 MonoJitTlsData
*jit_data
;
257 MonoThreadInfoInterruptToken
*interrupt_token
;
259 /* HandleStack for coop handles */
260 MonoHandleStack
*handle_stack
;
262 /* Stack mark for targets that explicitly require one */
265 /* GCHandle to MonoInternalThread */
266 guint32 internal_thread_gchandle
;
269 * Used by the sampling code in mini-posix.c to ensure that a thread has
270 * handled a sampling signal before sending another one.
272 gint32 profiler_signal_ack
;
274 #ifdef USE_WINDOWS_BACKEND
275 gint32 win32_apc_info
;
276 gpointer win32_apc_info_io_handle
;
279 /* When running hybrid suspend mode, a thread can explicitly flag */
280 /* itself to be cooperative suspend aware. If it does, it won't */
281 /* be preemptive suspended, so will behave as a cooperative suspended */
282 /* thread, continue to run when in safe mode. NOTE, threads setting */
283 /* this flag needs to correctly follow cooperative suspend rules. */
284 /* Flag can only be toggled for hybrid/cooperative suspend mode */
285 /* and is a onetime switch, FALSE -> TRUE. */
286 gboolean coop_aware_thread
;
289 * This is where we store tools tls data so it follows our lifecycle and doesn't depends on posix tls cleanup ordering
291 * TODO support multiple values by multiple tools
297 void* (*thread_attach
)(THREAD_INFO_TYPE
*info
);
299 This callback is called right before thread_detach_with_lock. This is called
300 without any locks held so it's the place for complicated cleanup.
302 The thread must remain operational between this call and thread_detach_with_lock.
303 It must be possible to successfully suspend it after thread_detach completes.
305 void (*thread_detach
)(THREAD_INFO_TYPE
*info
);
307 This callback is called with @info still on the thread list.
308 This call is made while holding the suspend lock, so don't do callbacks.
309 SMR remains functional as its small_id has not been reclaimed.
311 void (*thread_detach_with_lock
)(THREAD_INFO_TYPE
*info
);
312 gboolean (*ip_in_critical_region
) (MonoDomain
*domain
, gpointer ip
);
313 gboolean (*thread_in_critical_region
) (THREAD_INFO_TYPE
*info
);
315 // Called on the affected thread.
316 void (*thread_flags_changing
) (MonoThreadInfoFlags old
, MonoThreadInfoFlags new_
);
317 void (*thread_flags_changed
) (MonoThreadInfoFlags old
, MonoThreadInfoFlags new_
);
318 } MonoThreadInfoCallbacks
;
321 void (*setup_async_callback
) (MonoContext
*ctx
, void (*async_cb
)(void *fun
), gpointer user_data
);
322 gboolean (*thread_state_init_from_sigctx
) (MonoThreadUnwindState
*state
, void *sigctx
);
323 gboolean (*thread_state_init_from_handle
) (MonoThreadUnwindState
*tctx
, MonoThreadInfo
*info
, /*optional*/ void *sigctx
);
324 void (*thread_state_init
) (MonoThreadUnwindState
*tctx
);
325 } MonoThreadInfoRuntimeCallbacks
;
327 //Not using 0 and 1 to ensure callbacks are not returning bad data
329 MonoResumeThread
= 0x1234,
330 KeepSuspended
= 0x4321,
331 } SuspendThreadResult
;
333 typedef SuspendThreadResult (*MonoSuspendThreadCallback
) (THREAD_INFO_TYPE
*info
, gpointer user_data
);
335 MONO_API MonoThreadInfoFlags
336 mono_thread_info_get_flags (THREAD_INFO_TYPE
*info
);
339 * Sets the thread info flags for the current thread. This function may invoke
340 * callbacks containing arbitrary code (e.g. locks) so it must be assumed to be
344 mono_thread_info_set_flags (MonoThreadInfoFlags flags
);
346 static inline gboolean
347 mono_threads_filter_exclude_flags (THREAD_INFO_TYPE
*info
, MonoThreadInfoFlags flags
)
349 return !(mono_thread_info_get_flags (info
) & flags
);
352 /* Normal iteration; requires the world to be stopped. */
354 #define FOREACH_THREAD_ALL(thread) \
355 MONO_LLS_FOREACH_FILTERED (mono_thread_info_list_head (), THREAD_INFO_TYPE, thread, mono_lls_filter_accept_all, NULL)
357 #define FOREACH_THREAD_EXCLUDE(thread, not_flags) \
358 MONO_LLS_FOREACH_FILTERED (mono_thread_info_list_head (), THREAD_INFO_TYPE, thread, mono_threads_filter_exclude_flags, not_flags)
360 #define FOREACH_THREAD_END \
363 /* Snapshot iteration; can be done anytime but is slower. */
365 #define FOREACH_THREAD_SAFE_ALL(thread) \
366 MONO_LLS_FOREACH_FILTERED_SAFE (mono_thread_info_list_head (), THREAD_INFO_TYPE, thread, mono_lls_filter_accept_all, NULL)
368 #define FOREACH_THREAD_SAFE_EXCLUDE(thread, not_flags) \
369 MONO_LLS_FOREACH_FILTERED_SAFE (mono_thread_info_list_head (), THREAD_INFO_TYPE, thread, mono_threads_filter_exclude_flags, not_flags)
371 #define FOREACH_THREAD_SAFE_END \
372 MONO_LLS_FOREACH_SAFE_END
374 static inline MonoNativeThreadId
375 mono_thread_info_get_tid (THREAD_INFO_TYPE
*info
)
377 return MONO_UINT_TO_NATIVE_THREAD_ID (((MonoThreadInfo
*) info
)->node
.key
);
381 mono_thread_info_set_tid (THREAD_INFO_TYPE
*info
, MonoNativeThreadId tid
)
383 ((MonoThreadInfo
*) info
)->node
.key
= (uintptr_t) MONO_NATIVE_THREAD_ID_TO_UINT (tid
);
387 mono_thread_info_cleanup (void);
390 * @thread_info_size is sizeof (GcThreadInfo), a struct the GC defines to make it possible to have
391 * a single block with info from both camps.
394 mono_thread_info_init (size_t thread_info_size
);
397 * Wait for the above mono_thread_info_init to be called
400 mono_thread_info_wait_inited (void);
403 mono_thread_info_callbacks_init (MonoThreadInfoCallbacks
*callbacks
);
406 mono_thread_info_signals_init (void);
409 mono_thread_info_runtime_init (MonoThreadInfoRuntimeCallbacks
*callbacks
);
411 MonoThreadInfoRuntimeCallbacks
*
412 mono_threads_get_runtime_callbacks (void);
415 mono_thread_info_register_small_id (void);
417 MONO_API THREAD_INFO_TYPE
*
418 mono_thread_info_attach (void);
421 mono_thread_info_detach (void);
424 mono_thread_info_try_get_internal_thread_gchandle (THREAD_INFO_TYPE
*info
, guint32
*gchandle
);
427 mono_thread_info_set_internal_thread_gchandle (THREAD_INFO_TYPE
*info
, guint32 gchandle
);
430 mono_thread_info_unset_internal_thread_gchandle (THREAD_INFO_TYPE
*info
);
433 mono_thread_info_is_exiting (void);
436 G_EXTERN_C
// due to THREAD_INFO_TYPE varying
439 mono_thread_info_current (void);
442 mono_thread_info_set_tools_data (void *data
);
445 mono_thread_info_get_tools_data (void);
448 MONO_PROFILER_API THREAD_INFO_TYPE
*
449 mono_thread_info_current_unchecked (void);
452 mono_thread_info_get_small_id (void);
455 mono_thread_info_list_head (void);
458 mono_thread_info_lookup (MonoNativeThreadId id
);
461 mono_thread_info_resume (MonoNativeThreadId tid
);
464 mono_thread_info_safe_suspend_and_run (MonoNativeThreadId id
, gboolean interrupt_kernel
, MonoSuspendThreadCallback callback
, gpointer user_data
);
467 mono_thread_info_setup_async_call (THREAD_INFO_TYPE
*info
, void (*target_func
)(void*), void *user_data
);
470 mono_thread_info_suspend_lock (void);
473 mono_thread_info_suspend_unlock (void);
476 mono_thread_info_abort_socket_syscall_for_close (MonoNativeThreadId tid
);
479 mono_thread_info_set_is_async_context (gboolean async_context
);
482 mono_thread_info_is_async_context (void);
485 mono_thread_info_get_stack_bounds (guint8
**staddr
, size_t *stsize
);
488 mono_thread_info_yield (void);
491 mono_thread_info_sleep (guint32 ms
, gboolean
*alerted
);
494 mono_thread_info_usleep (guint64 us
);
497 mono_thread_info_tls_get (THREAD_INFO_TYPE
*info
, MonoTlsKey key
);
500 mono_thread_info_tls_set (THREAD_INFO_TYPE
*info
, MonoTlsKey key
, gpointer value
);
503 mono_thread_info_exit (gsize exit_code
);
506 mono_thread_info_install_interrupt (void (*callback
) (gpointer data
), gpointer data
, gboolean
*interrupted
);
509 mono_thread_info_uninstall_interrupt (gboolean
*interrupted
);
511 MonoThreadInfoInterruptToken
*
512 mono_thread_info_prepare_interrupt (THREAD_INFO_TYPE
*info
);
515 mono_thread_info_finish_interrupt (MonoThreadInfoInterruptToken
*token
);
518 mono_thread_info_self_interrupt (void);
521 mono_thread_info_clear_self_interrupt (void);
524 mono_thread_info_is_interrupt_state (THREAD_INFO_TYPE
*info
);
527 mono_thread_info_describe_interrupt_token (THREAD_INFO_TYPE
*info
, GString
*text
);
529 G_EXTERN_C
// due to THREAD_INFO_TYPE varying
531 mono_thread_info_is_live (THREAD_INFO_TYPE
*info
);
534 mono_thread_info_get_system_max_stack_size (void);
537 mono_threads_open_thread_handle (MonoThreadHandle
*handle
);
540 mono_threads_close_thread_handle (MonoThreadHandle
*handle
);
542 MonoNativeThreadHandle
543 mono_threads_open_native_thread_handle (MonoNativeThreadHandle handle
);
546 mono_threads_close_native_thread_handle (MonoNativeThreadHandle handle
);
548 #if !defined(HOST_WIN32)
550 /*Use this instead of pthread_kill */
552 mono_threads_pthread_kill (THREAD_INFO_TYPE
*info
, int signum
);
554 #endif /* !defined(HOST_WIN32) */
556 /* Internal API between mono-threads and its backends. */
558 /* Backend functions - a backend must implement all of the following */
560 This is called very early in the runtime, it cannot access any runtime facilities.
563 void mono_threads_suspend_init (void); //ok
565 void mono_threads_suspend_init_signals (void);
567 void mono_threads_coop_init (void);
570 This begins async suspend. This function must do the following:
572 -Ensure the target will EINTR any syscalls if @interrupt_kernel is true
573 -Call mono_threads_transition_finish_async_suspend as part of its async suspend.
574 -Register the thread for pending suspend with mono_threads_add_to_pending_operation_set if needed.
576 If begin suspend fails the following should be done:
577 -The thread must be left uninterrupted and resumed.
578 -Call mono_threads_transition_abort_async_suspend to make sure thread has correct state.
579 -No pending suspend should be registered with mono_threads_add_to_pending_operation_set for this operation.
581 gboolean
mono_threads_suspend_begin_async_suspend (THREAD_INFO_TYPE
*info
, gboolean interrupt_kernel
);
584 This verifies the outcome of an async suspend operation.
586 Some targets, such as posix, verify suspend results assynchronously. Suspend results must be
587 available (in a non blocking way) after mono_threads_wait_pending_operations completes.
589 gboolean
mono_threads_suspend_check_suspend_result (THREAD_INFO_TYPE
*info
);
592 This begins async resume. This function must do the following:
594 - Install an async target if one was requested.
595 - Notify the target to resume.
596 - Register the thread for pending ack with mono_threads_add_to_pending_operation_set if needed.
598 gboolean
mono_threads_suspend_begin_async_resume (THREAD_INFO_TYPE
*info
);
600 void mono_threads_suspend_register (THREAD_INFO_TYPE
*info
); //ok
601 void mono_threads_suspend_free (THREAD_INFO_TYPE
*info
);
602 void mono_threads_suspend_abort_syscall (THREAD_INFO_TYPE
*info
);
603 gint
mono_threads_suspend_search_alternative_signal (void);
604 gint
mono_threads_suspend_get_suspend_signal (void);
605 gint
mono_threads_suspend_get_restart_signal (void);
606 gint
mono_threads_suspend_get_abort_signal (void);
609 mono_thread_platform_create_thread (MonoThreadStart thread_fn
, gpointer thread_data
,
610 gsize
* const stack_size
, MonoNativeThreadId
*tid
);
612 void mono_threads_platform_get_stack_bounds (guint8
**staddr
, size_t *stsize
);
613 gboolean
mono_threads_platform_is_main_thread (void);
614 void mono_threads_platform_init (void);
615 gboolean
mono_threads_platform_in_critical_region (THREAD_INFO_TYPE
*info
);
616 gboolean
mono_threads_platform_yield (void);
617 void mono_threads_platform_exit (gsize exit_code
);
619 void mono_threads_coop_begin_global_suspend (void);
620 void mono_threads_coop_end_global_suspend (void);
622 MONO_API MonoNativeThreadId
623 mono_native_thread_id_get (void);
626 * This does _not_ return the same value as mono_native_thread_id_get, except on Windows.
627 * On POSIX, mono_native_thread_id_get returns the value from pthread_self, which is then
628 * passed around as an identifier to other pthread functions. However this function, where
629 * possible, returns the OS-unique thread id value, fetched in a platform-specific manner.
630 * It will not work with the various pthread functions, should never be used as a
631 * MonoNativeThreadId, and is intended solely to match the output of various diagonistic tools.
633 guint64
mono_native_thread_os_id_get (void);
636 mono_native_thread_id_equals (MonoNativeThreadId id1
, MonoNativeThreadId id2
);
638 //FIXMEcxx typedef mono_native_thread_return_t (MONO_STDCALL * MonoNativeThreadStart)(void*);
639 // mono_native_thread_return_t func
640 // and remove the template
643 mono_native_thread_create (MonoNativeThreadId
*tid
, gpointer func
, gpointer arg
);
646 template <typename T
>
648 mono_native_thread_create (MonoNativeThreadId
*tid
, T func
, gpointer arg
)
650 return mono_native_thread_create (tid
, (gpointer
)func
, arg
);
655 mono_native_thread_set_name (MonoNativeThreadId tid
, const char *name
);
658 mono_native_thread_get_name (MonoNativeThreadId tid
, char *name_out
, size_t max_len
);
661 mono_native_thread_join (MonoNativeThreadId tid
);
663 /*Mach specific internals */
664 void mono_threads_init_dead_letter (void);
665 void mono_threads_install_dead_letter (void);
667 /* mono-threads internal API used by the backends. */
669 This tells the suspend initiator that we completed suspend and will now be waiting for resume.
671 void mono_threads_notify_initiator_of_suspend (THREAD_INFO_TYPE
* info
);
673 This tells the resume initiator that we completed resume duties and will return to runnable state.
675 void mono_threads_notify_initiator_of_resume (THREAD_INFO_TYPE
* info
);
678 This tells the resume initiator that we completed abort duties and will return to previous state.
680 void mono_threads_notify_initiator_of_abort (THREAD_INFO_TYPE
* info
);
682 /* Thread state machine functions */
687 ResumeInitSelfResume
,
688 ResumeInitAsyncResume
,
689 ResumeInitBlockingResume
,
698 SelfSuspendNotifyAndWait
,
699 } MonoSelfSupendResult
;
702 ReqSuspendAlreadySuspended
,
703 ReqSuspendAlreadySuspendedBlocking
,
704 ReqSuspendInitSuspendRunning
,
705 ReqSuspendInitSuspendBlocking
,
706 } MonoRequestSuspendResult
;
709 DoBlockingContinue
, //in blocking mode, continue
710 DoBlockingPollAndRetry
, //async suspend raced blocking and won, pool and retry
711 } MonoDoBlockingResult
;
714 DoneBlockingOk
, //exited blocking fine
715 DoneBlockingWait
, //thread should end suspended and wait for resume
716 } MonoDoneBlockingResult
;
720 AbortBlockingIgnore
, //Ignore
721 AbortBlockingIgnoreAndPoll
, //Ignore and poll
722 AbortBlockingOk
, //Abort worked
723 AbortBlockingWait
, //Abort worked, but should wait for resume
724 } MonoAbortBlockingResult
;
727 void mono_threads_transition_attach (THREAD_INFO_TYPE
* info
);
728 gboolean
mono_threads_transition_detach (THREAD_INFO_TYPE
*info
);
729 MonoRequestSuspendResult
mono_threads_transition_request_suspension (THREAD_INFO_TYPE
*info
);
730 MonoSelfSupendResult
mono_threads_transition_state_poll (THREAD_INFO_TYPE
*info
);
731 MonoResumeResult
mono_threads_transition_request_resume (THREAD_INFO_TYPE
* info
);
732 MonoPulseResult
mono_threads_transition_request_pulse (THREAD_INFO_TYPE
* info
);
733 gboolean
mono_threads_transition_abort_async_suspend (THREAD_INFO_TYPE
* info
);
734 gboolean
mono_threads_transition_finish_async_suspend (THREAD_INFO_TYPE
* info
);
735 MonoDoBlockingResult
mono_threads_transition_do_blocking (THREAD_INFO_TYPE
* info
, const char* func
);
736 MonoDoneBlockingResult
mono_threads_transition_done_blocking (THREAD_INFO_TYPE
* info
, const char* func
);
737 MonoAbortBlockingResult
mono_threads_transition_abort_blocking (THREAD_INFO_TYPE
* info
, const char* func
);
738 gboolean
mono_threads_transition_peek_blocking_suspend_requested (THREAD_INFO_TYPE
* info
);
739 void mono_threads_transition_begin_no_safepoints (THREAD_INFO_TYPE
* info
, const char *func
);
740 void mono_threads_transition_end_no_safepoints (THREAD_INFO_TYPE
* info
, const char *func
);
742 G_EXTERN_C
// due to THREAD_INFO_TYPE varying
743 MonoThreadUnwindState
* mono_thread_info_get_suspend_state (THREAD_INFO_TYPE
*info
);
746 mono_threads_enter_gc_unsafe_region_cookie (void);
749 void mono_thread_info_wait_for_resume (THREAD_INFO_TYPE
*info
);
750 /* Advanced suspend API, used for suspending multiple threads as once. */
751 G_EXTERN_C
// due to THREAD_INFO_TYPE varying
752 gboolean
mono_thread_info_is_running (THREAD_INFO_TYPE
*info
);
753 gboolean
mono_thread_info_is_live (THREAD_INFO_TYPE
*info
);
754 G_EXTERN_C
// due to THREAD_INFO_TYPE varying
755 int mono_thread_info_suspend_count (THREAD_INFO_TYPE
*info
);
756 int mono_thread_info_current_state (THREAD_INFO_TYPE
*info
);
757 const char* mono_thread_state_name (int state
);
758 gboolean
mono_thread_is_gc_unsafe_mode (void);
759 G_EXTERN_C
// due to THREAD_INFO_TYPE varying
760 gboolean
mono_thread_info_will_not_safepoint (THREAD_INFO_TYPE
*info
);
764 * In a full coop or full preemptive suspend, there is only a single phase. In
765 * the initial phase, all threads are either cooperatively or preemptively
766 * suspended, respectively.
768 * In hybrid suspend, there may be two phases. In the initial phase, threads
769 * are invited to cooperatively suspend. Running threads are expected to
770 * finish cooperatively suspending (the phase waits for them), but blocking
773 * If any blocking thread was encountered in the initial phase, a second
774 * "mop-up" phase runs which checks whether the blocking threads self-suspended
775 * (in which case nothing more needs to be done) or if they're still in the
776 * BLOCKING_SUSPEND_REQUESTED state, in which case they are preemptively
779 #define MONO_THREAD_SUSPEND_PHASE_INITIAL (0)
780 #define MONO_THREAD_SUSPEND_PHASE_MOPUP (1)
782 #define MONO_THREAD_SUSPEND_PHASE_COUNT (2)
783 typedef int MonoThreadSuspendPhase
;
786 MONO_THREAD_BEGIN_SUSPEND_SKIP
= 0,
787 MONO_THREAD_BEGIN_SUSPEND_SUSPENDED
= 1,
788 MONO_THREAD_BEGIN_SUSPEND_NEXT_PHASE
= 2,
789 } MonoThreadBeginSuspendResult
;
791 G_EXTERN_C
// due to THREAD_INFO_TYPE varying
792 gboolean
mono_thread_info_in_critical_location (THREAD_INFO_TYPE
*info
);
793 G_EXTERN_C
// due to THREAD_INFO_TYPE varying
794 MonoThreadBeginSuspendResult
mono_thread_info_begin_suspend (THREAD_INFO_TYPE
*info
, MonoThreadSuspendPhase phase
);
795 G_EXTERN_C
// due to THREAD_INFO_TYPE varying
796 gboolean
mono_thread_info_begin_resume (THREAD_INFO_TYPE
*info
);
797 G_EXTERN_C
// due to THREAD_INFO_TYPE varying
798 gboolean
mono_thread_info_begin_pulse_resume_and_request_suspension (THREAD_INFO_TYPE
*info
);
801 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
802 gboolean
mono_threads_wait_pending_operations (void);
803 void mono_threads_begin_global_suspend (void);
804 void mono_threads_end_global_suspend (void);
807 mono_thread_info_is_current (THREAD_INFO_TYPE
*info
);
810 MONO_THREAD_INFO_WAIT_RET_SUCCESS_0
= 0,
811 MONO_THREAD_INFO_WAIT_RET_ALERTED
= -1,
812 MONO_THREAD_INFO_WAIT_RET_TIMEOUT
= -2,
813 MONO_THREAD_INFO_WAIT_RET_FAILED
= -3,
814 } MonoThreadInfoWaitRet
;
816 MonoThreadInfoWaitRet
817 mono_thread_info_wait_one_handle (MonoThreadHandle
*handle
, guint32 timeout
, gboolean alertable
);
819 MonoThreadInfoWaitRet
820 mono_thread_info_wait_multiple_handle (MonoThreadHandle
**thread_handles
, gsize nhandles
, MonoOSEvent
*background_change_event
, gboolean waitall
, guint32 timeout
, gboolean alertable
);
822 void mono_threads_join_lock (void);
823 void mono_threads_join_unlock (void);
827 typedef void (*background_job_cb
)(void);
828 void mono_threads_schedule_background_job (background_job_cb cb
);
831 #ifdef USE_WINDOWS_BACKEND
834 mono_win32_enter_alertable_wait (THREAD_INFO_TYPE
*info
);
837 mono_win32_leave_alertable_wait (THREAD_INFO_TYPE
*info
);
840 mono_win32_enter_blocking_io_call (THREAD_INFO_TYPE
*info
, HANDLE io_handle
);
843 mono_win32_leave_blocking_io_call (THREAD_INFO_TYPE
*info
, HANDLE io_handle
);
846 mono_win32_interrupt_wait (PVOID thread_info
, HANDLE native_thread_handle
, DWORD tid
);
849 mono_win32_abort_blocking_io_call (THREAD_INFO_TYPE
*info
);
851 #define W32_DEFINE_LAST_ERROR_RESTORE_POINT \
852 const DWORD _last_error_restore_point = GetLastError ();
854 #define W32_RESTORE_LAST_ERROR_FROM_RESTORE_POINT \
855 /* Only restore if changed to prevent unecessary writes. */ \
856 if (GetLastError () != _last_error_restore_point) \
857 mono_SetLastError (_last_error_restore_point);
861 #define W32_DEFINE_LAST_ERROR_RESTORE_POINT /* nothing */
862 #define W32_RESTORE_LAST_ERROR_FROM_RESTORE_POINT /* nothing */
866 #endif /* __MONO_THREADS_H__ */