Skip to content

Commit 6ac1197

Browse files
monojenkinslambdageek
authored andcommitted
[2019-10] [threads] Add back mono_threads_attach_tools_thread as a public API (#18075)
* [utils] Add back mono_threads_attach_tools_thread In a5da7b2 we got rid of "tools" threads internally to the runtime. However since the API was previously marked with MONO_API it was an internal API that some embedders depended on. This PR adds back an (external-only) limited form of tools thread. The runtime is aware of the Tools thread in that FOREACH_THREAD_* macros will iterate over them, and the thread has a coop thread state machine. (That is, mono_thread_info_current() and GC Safe and GC Unsafe transitions all work.) However the thread is: 1. Not stopped by the GC 2. Is not interrupted by profiler sampling. 3. Does not have a "current domain" 4. (As a consequence of the above) cannot call managed methods or touch managed objects. Such threads are useful for low-level interaction with the runtime such as querying metadata, the JIT state and other coordination. mono_threads_attach_tools_thread should be called no more than once. It should not be called on a thread that is already attached with mono_thread_atach, and vice versa. Addresses #18011 * [threads] Make mono_threads_attach_tools_thread into a public API
1 parent b687667 commit 6ac1197

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

mono/metadata/threads.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,34 @@ mono_thread_attach (MonoDomain *domain)
15491549
return thread;
15501550
}
15511551

1552+
/**
1553+
* mono_threads_attach_tools_thread:
1554+
*
1555+
* Attach the current thread as a tool thread. DON'T USE THIS FUNCTION WITHOUT READING ALL DISCLAIMERS.
1556+
*
1557+
* A tools thread is a very special kind of thread that needs access to core
1558+
* runtime facilities but should not be counted as a regular thread for high
1559+
* order facilities such as executing managed code or accessing the managed
1560+
* heap.
1561+
*
1562+
* This is intended only for low-level utilities than need to be able to use
1563+
* some low-level runtime facilities when doing things like resolving
1564+
* backtraces in their background processing thread.
1565+
*
1566+
* Note in particular that the thread is not fully attached - it does not have
1567+
* a "current domain" because it cannot run managed code or interact with
1568+
* managed objects. However it can act on some metadata, and use our low-level
1569+
* locks and the coop thread state machine (ie GC Safe and GC Unsafe
1570+
* transitions make sense).
1571+
*/
1572+
void
1573+
mono_threads_attach_tools_thread (void)
1574+
{
1575+
MonoThreadInfo *info = mono_thread_info_attach ();
1576+
g_assert (info);
1577+
mono_thread_info_set_flags (MONO_THREAD_INFO_FLAGS_NO_GC | MONO_THREAD_INFO_FLAGS_NO_SAMPLE);
1578+
}
1579+
15521580
/**
15531581
* mono_thread_detach:
15541582
*/
@@ -1559,6 +1587,8 @@ mono_thread_detach (MonoThread *thread)
15591587
mono_thread_detach_internal (thread->internal_thread);
15601588
}
15611589

1590+
1591+
15621592
/**
15631593
* mono_thread_detach_if_exiting:
15641594
*

mono/metadata/threads.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ MONO_API MonoThread *mono_thread_attach (MonoDomain *domain);
4444
MONO_API void mono_thread_detach (MonoThread *thread);
4545
MONO_API void mono_thread_exit (void);
4646

47+
MONO_API MONO_RT_EXTERNAL_ONLY void
48+
mono_threads_attach_tools_thread (void);
49+
4750
MONO_API char *mono_thread_get_name_utf8 (MonoThread *thread);
4851
MONO_API int32_t mono_thread_get_managed_id (MonoThread *thread);
4952

0 commit comments

Comments
 (0)