Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / metadata / threads-types.h
1 /**
2  * \file
3  * Generic thread typedef support (includes system-specific files)
4  *
5  * Author:
6  *      Dick Porter (dick@ximian.com)
7  *
8  * (C) 2001 Ximian, Inc
9  * (C) Copyright 2002-2006 Novell, Inc
10  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11  */
12
13 #ifndef _MONO_METADATA_THREADS_TYPES_H_
14 #define _MONO_METADATA_THREADS_TYPES_H_
15
16 #include <glib.h>
17
18 #include <mono/metadata/object.h>
19 #include "mono/metadata/handle.h"
20 #include "mono/utils/mono-compiler.h"
21 #include "mono/utils/mono-membar.h"
22 #include "mono/utils/mono-threads.h"
23
24 /* This is a copy of System.Threading.ThreadState */
25 typedef enum {
26         ThreadState_Running = 0x00000000,
27         ThreadState_SuspendRequested = 0x00000002,
28         ThreadState_Background = 0x00000004,
29         ThreadState_Unstarted = 0x00000008,
30         ThreadState_Stopped = 0x00000010,
31         ThreadState_WaitSleepJoin = 0x00000020,
32         ThreadState_Suspended = 0x00000040,
33         ThreadState_AbortRequested = 0x00000080,
34         ThreadState_Aborted = 0x00000100
35 } MonoThreadState; 
36
37 /* This is a copy of System.Threading.ApartmentState */
38 typedef enum {
39         ThreadApartmentState_STA = 0x00000000,
40         ThreadApartmentState_MTA = 0x00000001,
41         ThreadApartmentState_Unknown = 0x00000002
42 } MonoThreadApartmentState;
43
44 typedef enum {
45         MONO_THREAD_PRIORITY_LOWEST       = 0,
46         MONO_THREAD_PRIORITY_BELOW_NORMAL = 1,
47         MONO_THREAD_PRIORITY_NORMAL       = 2,
48         MONO_THREAD_PRIORITY_ABOVE_NORMAL = 3,
49         MONO_THREAD_PRIORITY_HIGHEST      = 4,
50 } MonoThreadPriority;
51
52 #define SPECIAL_STATIC_NONE 0
53 #define SPECIAL_STATIC_THREAD 1
54 #define SPECIAL_STATIC_CONTEXT 2
55
56 typedef struct _MonoInternalThread MonoInternalThread;
57
58 typedef void (*MonoThreadCleanupFunc) (MonoNativeThreadId tid);
59 /* INFO has type MonoThreadInfo* */
60 typedef void (*MonoThreadNotifyPendingExcFunc) (gpointer info);
61
62 void
63 mono_thread_callbacks_init (void);
64
65 typedef enum {
66         MONO_THREAD_CREATE_FLAGS_NONE         = 0x0,
67         MONO_THREAD_CREATE_FLAGS_THREADPOOL   = 0x1,
68         MONO_THREAD_CREATE_FLAGS_DEBUGGER     = 0x2,
69         MONO_THREAD_CREATE_FLAGS_FORCE_CREATE = 0x4,
70         MONO_THREAD_CREATE_FLAGS_SMALL_STACK  = 0x8,
71 } MonoThreadCreateFlags;
72
73 MonoInternalThread*
74 mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, MonoThreadCreateFlags flags, MonoError *error);
75
76 void mono_threads_install_cleanup (MonoThreadCleanupFunc func);
77
78 void ves_icall_System_Threading_Thread_ConstructInternalThread (MonoThread *this_obj);
79 gpointer ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this_obj, MonoObject *start);
80 void ves_icall_System_Threading_InternalThread_Thread_free_internal(MonoInternalThread *this_obj);
81 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms);
82 gboolean ves_icall_System_Threading_Thread_Join_internal(MonoThread *this_obj, int ms);
83 gint32 ves_icall_System_Threading_Thread_GetDomainID (void);
84 gboolean ves_icall_System_Threading_Thread_Yield (void);
85 MonoString* ves_icall_System_Threading_Thread_GetName_internal (MonoInternalThread *this_obj);
86 void ves_icall_System_Threading_Thread_SetName_internal (MonoInternalThread *this_obj, MonoString *name);
87 int ves_icall_System_Threading_Thread_GetPriority (MonoThread *this_obj);
88 void ves_icall_System_Threading_Thread_SetPriority (MonoThread *this_obj, int priority);
89 MonoObject* ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoInternalThread *this_obj);
90 void ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread *this_obj, MonoObject *culture);
91 MonoObject* ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoInternalThread *this_obj);
92 void ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread *this_obj, MonoObject *culture);
93 MonoThread *ves_icall_System_Threading_Thread_GetCurrentThread (void);
94
95 gint32 ves_icall_System_Threading_WaitHandle_Wait_internal(gpointer *handles, gint32 numhandles, MonoBoolean waitall, gint32 ms, MonoError *error);
96 gint32 ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal (gpointer toSignal, gpointer toWait, gint32 ms, MonoError *error);
97
98 MonoArray* ves_icall_System_Threading_Thread_ByteArrayToRootDomain (MonoArray *arr);
99 MonoArray* ves_icall_System_Threading_Thread_ByteArrayToCurrentDomain (MonoArray *arr);
100
101 gint32 ves_icall_System_Threading_Interlocked_Increment_Int(gint32 *location);
102 gint64 ves_icall_System_Threading_Interlocked_Increment_Long(gint64 *location);
103 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int(gint32 *location);
104 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long(gint64 * location);
105
106 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int(gint32 *location, gint32 value);
107 gint64 ves_icall_System_Threading_Interlocked_Exchange_Long(gint64 *location, gint64 value);
108 MonoObject *ves_icall_System_Threading_Interlocked_Exchange_Object(MonoObject **location, MonoObject *value);
109 gpointer ves_icall_System_Threading_Interlocked_Exchange_IntPtr(gpointer *location, gpointer value);
110 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single(gfloat *location, gfloat value);
111 gdouble ves_icall_System_Threading_Interlocked_Exchange_Double(gdouble *location, gdouble value);
112
113 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location, gint32 value, gint32 comparand);
114 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int_Success(gint32 *location, gint32 value, gint32 comparand, MonoBoolean *success);
115 gint64 ves_icall_System_Threading_Interlocked_CompareExchange_Long(gint64 *location, gint64 value, gint64 comparand);
116 MonoObject *ves_icall_System_Threading_Interlocked_CompareExchange_Object(MonoObject **location, MonoObject *value, MonoObject *comparand);
117 gpointer ves_icall_System_Threading_Interlocked_CompareExchange_IntPtr(gpointer *location, gpointer value, gpointer comparand);
118 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single(gfloat *location, gfloat value, gfloat comparand);
119 gdouble ves_icall_System_Threading_Interlocked_CompareExchange_Double(gdouble *location, gdouble value, gdouble comparand);
120 MonoObject* ves_icall_System_Threading_Interlocked_CompareExchange_T(MonoObject **location, MonoObject *value, MonoObject *comparand);
121 MonoObject* ves_icall_System_Threading_Interlocked_Exchange_T(MonoObject **location, MonoObject *value);
122
123 gint32 ves_icall_System_Threading_Interlocked_Add_Int(gint32 *location, gint32 value);
124 gint64 ves_icall_System_Threading_Interlocked_Add_Long(gint64 *location, gint64 value);
125 gint64 ves_icall_System_Threading_Interlocked_Read_Long(gint64 *location);
126
127 gint32 ves_icall_System_Threading_Interlocked_Increment_Int(gint32 *location);
128 gint64 ves_icall_System_Threading_Interlocked_Increment_Long(gint64 *location);
129
130 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int(gint32 *location);
131 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long(gint64 * location);
132
133 void ves_icall_System_Threading_Thread_Abort (MonoInternalThread *thread, MonoObject *state);
134 void ves_icall_System_Threading_Thread_ResetAbort (MonoThread *this_obj);
135 MonoObject* ves_icall_System_Threading_Thread_GetAbortExceptionState (MonoThread *thread);
136 void ves_icall_System_Threading_Thread_Suspend (MonoThread *this_obj);
137 void ves_icall_System_Threading_Thread_Resume (MonoThread *thread);
138 void ves_icall_System_Threading_Thread_ClrState (MonoInternalThread *thread, guint32 state);
139 void ves_icall_System_Threading_Thread_SetState (MonoInternalThread *thread, guint32 state);
140 guint32 ves_icall_System_Threading_Thread_GetState (MonoInternalThread *thread);
141
142 gint8 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr);
143 gint16 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr);
144 gint32 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr);
145 gint64 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr);
146 void * ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr);
147 void * ves_icall_System_Threading_Thread_VolatileReadObject (void *ptr);
148 double ves_icall_System_Threading_Thread_VolatileReadDouble (void *ptr);
149 float ves_icall_System_Threading_Thread_VolatileReadFloat (void *ptr);
150
151 void ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8);
152 void ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16);
153 void ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32);
154 void ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64);
155 void ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *);
156 void ves_icall_System_Threading_Thread_VolatileWriteObject (void *ptr, MonoObject *);
157 void ves_icall_System_Threading_Thread_VolatileWriteFloat (void *ptr, float);
158 void ves_icall_System_Threading_Thread_VolatileWriteDouble (void *ptr, double);
159
160 gint8 ves_icall_System_Threading_Volatile_Read1 (void *ptr);
161 gint16 ves_icall_System_Threading_Volatile_Read2 (void *ptr);
162 gint32 ves_icall_System_Threading_Volatile_Read4 (void *ptr);
163 gint64 ves_icall_System_Threading_Volatile_Read8 (void *ptr);
164 void * ves_icall_System_Threading_Volatile_ReadIntPtr (void *ptr);
165 double ves_icall_System_Threading_Volatile_ReadDouble (void *ptr);
166 float ves_icall_System_Threading_Volatile_ReadFloat (void *ptr);
167 MonoObject* ves_icall_System_Threading_Volatile_Read_T (void *ptr);
168
169 void ves_icall_System_Threading_Volatile_Write1 (void *ptr, gint8);
170 void ves_icall_System_Threading_Volatile_Write2 (void *ptr, gint16);
171 void ves_icall_System_Threading_Volatile_Write4 (void *ptr, gint32);
172 void ves_icall_System_Threading_Volatile_Write8 (void *ptr, gint64);
173 void ves_icall_System_Threading_Volatile_WriteIntPtr (void *ptr, void *);
174 void ves_icall_System_Threading_Volatile_WriteFloat (void *ptr, float);
175 void ves_icall_System_Threading_Volatile_WriteDouble (void *ptr, double);
176 void ves_icall_System_Threading_Volatile_Write_T (void *ptr, MonoObject *value);
177
178 void ves_icall_System_Threading_Thread_MemoryBarrier (void);
179 void ves_icall_System_Threading_Thread_Interrupt_internal (MonoThread *this_obj);
180 void ves_icall_System_Threading_Thread_SpinWait_nop (void);
181
182 void
183 mono_threads_register_app_context (MonoAppContext* ctx, MonoError *error);
184 void
185 mono_threads_release_app_context (MonoAppContext* ctx, MonoError *error);
186
187 void ves_icall_System_Runtime_Remoting_Contexts_Context_RegisterContext (MonoAppContextHandle ctx, MonoError *error);
188 void ves_icall_System_Runtime_Remoting_Contexts_Context_ReleaseContext (MonoAppContextHandle ctx, MonoError *error);
189
190 MonoInternalThread *mono_thread_internal_current (void);
191
192 void mono_thread_internal_abort (MonoInternalThread *thread, gboolean appdomain_unload);
193 void mono_thread_internal_suspend_for_shutdown (MonoInternalThread *thread);
194
195 gboolean mono_thread_internal_has_appdomain_ref (MonoInternalThread *thread, MonoDomain *domain);
196
197 void mono_thread_internal_reset_abort (MonoInternalThread *thread);
198
199 void mono_thread_internal_unhandled_exception (MonoObject* exc);
200
201 void mono_alloc_special_static_data_free (GHashTable *special_static_fields);
202 gboolean mono_thread_current_check_pending_interrupt (void);
203
204 void mono_thread_set_state (MonoInternalThread *thread, MonoThreadState state);
205 void mono_thread_clr_state (MonoInternalThread *thread, MonoThreadState state);
206 gboolean mono_thread_test_state (MonoInternalThread *thread, MonoThreadState test);
207 gboolean mono_thread_test_and_set_state (MonoInternalThread *thread, MonoThreadState test, MonoThreadState set);
208
209 void mono_thread_init_apartment_state (void);
210 void mono_thread_cleanup_apartment_state (void);
211
212 void mono_threads_set_shutting_down (void);
213
214 gunichar2* mono_thread_get_name (MonoInternalThread *this_obj, guint32 *name_len);
215
216 MONO_API MonoException* mono_thread_get_undeniable_exception (void);
217 void mono_thread_self_abort (void);
218
219 void mono_thread_set_name_internal (MonoInternalThread *this_obj, MonoString *name, gboolean permanent, gboolean reset, MonoError *error);
220
221 void mono_thread_suspend_all_other_threads (void);
222 gboolean mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout);
223
224 void mono_thread_push_appdomain_ref (MonoDomain *domain);
225 void mono_thread_pop_appdomain_ref (void);
226 gboolean mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain);
227
228 MonoException* mono_thread_request_interruption (mono_bool running_managed);
229 gboolean mono_thread_interruption_requested (void);
230 MonoException* mono_thread_interruption_checkpoint (void);
231 MonoException* mono_thread_force_interruption_checkpoint_noraise (void);
232 gint32* mono_thread_interruption_request_flag (void);
233
234 uint32_t mono_alloc_special_static_data (uint32_t static_type, uint32_t size, uint32_t align, uintptr_t *bitmap, int numbits);
235 void*    mono_get_special_static_data   (uint32_t offset);
236 gpointer mono_get_special_static_data_for_thread (MonoInternalThread *thread, guint32 offset);
237
238 MonoException* mono_thread_resume_interruption (gboolean exec);
239 void mono_threads_perform_thread_dump (void);
240
241 gboolean
242 mono_thread_create_checked (MonoDomain *domain, gpointer func, gpointer arg, MonoError *error);
243
244 /* Can't include utils/mono-threads.h because of the THREAD_INFO_TYPE wizardry */
245 void mono_threads_add_joinable_runtime_thread (gpointer thread_info);
246 void mono_threads_add_joinable_thread (gpointer tid);
247 void mono_threads_join_threads (void);
248 void mono_thread_join (gpointer tid);
249
250 void ves_icall_System_Threading_Thread_GetStackTraces (MonoArray **out_threads, MonoArray **out_stack_traces);
251
252 MONO_API gpointer
253 mono_threads_attach_coop (MonoDomain *domain, gpointer *dummy);
254
255 MONO_API void
256 mono_threads_detach_coop (gpointer cookie, gpointer *dummy);
257
258 void mono_threads_begin_abort_protected_block (void);
259 gboolean mono_threads_end_abort_protected_block (void);
260
261 gboolean
262 mono_thread_internal_current_is_attached (void);
263
264 void
265 mono_thread_internal_describe (MonoInternalThread *internal, GString *str);
266
267 gboolean
268 mono_thread_internal_is_current (MonoInternalThread *internal);
269
270 #endif /* _MONO_METADATA_THREADS_TYPES_H_ */