9cc43855c299a0f3142330d6a7e264a171f84be8
[mono.git] / mono / metadata / threads-types.h
1 /*
2  * threads-types.h: Generic thread typedef support (includes
3  * 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/io-layer/io-layer.h>
19 #include <mono/metadata/object.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_StopRequested = 0x00000001,
28         ThreadState_SuspendRequested = 0x00000002,
29         ThreadState_Background = 0x00000004,
30         ThreadState_Unstarted = 0x00000008,
31         ThreadState_Stopped = 0x00000010,
32         ThreadState_WaitSleepJoin = 0x00000020,
33         ThreadState_Suspended = 0x00000040,
34         ThreadState_AbortRequested = 0x00000080,
35         ThreadState_Aborted = 0x00000100
36 } MonoThreadState; 
37
38 /* This is a copy of System.Threading.ApartmentState */
39 typedef enum {
40         ThreadApartmentState_STA = 0x00000000,
41         ThreadApartmentState_MTA = 0x00000001,
42         ThreadApartmentState_Unknown = 0x00000002
43 } MonoThreadApartmentState;
44
45 typedef enum {
46         ThreadPriority_Lowest = 0,
47         ThreadPriority_BelowNormal = 1,
48         ThreadPriority_Normal = 2,
49         ThreadPriority_AboveNormal = 3,
50         ThreadPriority_Highest = 4
51 } MonoThreadPriority;
52
53 #define SPECIAL_STATIC_NONE 0
54 #define SPECIAL_STATIC_THREAD 1
55 #define SPECIAL_STATIC_CONTEXT 2
56
57 #ifdef HOST_WIN32
58 typedef SECURITY_ATTRIBUTES WapiSecurityAttributes;
59 typedef LPTHREAD_START_ROUTINE WapiThreadStart;
60 #endif
61
62 typedef struct _MonoInternalThread MonoInternalThread;
63
64 typedef void (*MonoThreadCleanupFunc) (MonoNativeThreadId tid);
65 /* INFO has type MonoThreadInfo* */
66 typedef void (*MonoThreadNotifyPendingExcFunc) (gpointer info);
67
68 MonoInternalThread* mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, gboolean threadpool_thread, guint32 stack_size, MonoError *error);
69
70 void mono_threads_install_cleanup (MonoThreadCleanupFunc func);
71
72 void ves_icall_System_Threading_Thread_ConstructInternalThread (MonoThread *this_obj);
73 HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this_obj, MonoObject *start);
74 void ves_icall_System_Threading_InternalThread_Thread_free_internal(MonoInternalThread *this_obj, HANDLE thread);
75 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms);
76 gboolean ves_icall_System_Threading_Thread_Join_internal(MonoThread *this_obj, int ms);
77 gint32 ves_icall_System_Threading_Thread_GetDomainID (void);
78 gboolean ves_icall_System_Threading_Thread_Yield (void);
79 MonoString* ves_icall_System_Threading_Thread_GetName_internal (MonoInternalThread *this_obj);
80 void ves_icall_System_Threading_Thread_SetName_internal (MonoInternalThread *this_obj, MonoString *name);
81 int ves_icall_System_Threading_Thread_GetPriority (MonoThread *this_obj);
82 void ves_icall_System_Threading_Thread_SetPriority (MonoThread *this_obj, int priority);
83 MonoObject* ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoInternalThread *this_obj);
84 void ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread *this_obj, MonoObject *culture);
85 MonoObject* ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoInternalThread *this_obj);
86 void ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread *this_obj, MonoObject *culture);
87 HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal(MonoBoolean owned, MonoString *name, MonoBoolean *created);
88 MonoBoolean ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle );
89 HANDLE ves_icall_System_Threading_Mutex_OpenMutex_internal (MonoString *name, gint32 rights, gint32 *error);
90 HANDLE ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, MonoBoolean *created);
91 gint32 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (HANDLE handle, gint32 releaseCount, MonoBoolean *fail);
92 HANDLE ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error);
93 HANDLE ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, MonoBoolean *created);
94 gboolean ves_icall_System_Threading_Events_SetEvent_internal (HANDLE handle);
95 gboolean ves_icall_System_Threading_Events_ResetEvent_internal (HANDLE handle);
96 void ves_icall_System_Threading_Events_CloseEvent_internal (HANDLE handle);
97 HANDLE ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name, gint32 rights, gint32 *error);
98
99 gint32 ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_handles, gint32 ms);
100 gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_handles, gint32 ms);
101 gint32 ves_icall_System_Threading_WaitHandle_WaitOne_internal(HANDLE handle, gint32 ms);
102 gint32 ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal (HANDLE toSignal, HANDLE toWait, gint32 ms);
103
104 MonoArray* ves_icall_System_Threading_Thread_ByteArrayToRootDomain (MonoArray *arr);
105 MonoArray* ves_icall_System_Threading_Thread_ByteArrayToCurrentDomain (MonoArray *arr);
106
107 gint32 ves_icall_System_Threading_Interlocked_Increment_Int(gint32 *location);
108 gint64 ves_icall_System_Threading_Interlocked_Increment_Long(gint64 *location);
109 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int(gint32 *location);
110 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long(gint64 * location);
111
112 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int(gint32 *location, gint32 value);
113 gint64 ves_icall_System_Threading_Interlocked_Exchange_Long(gint64 *location, gint64 value);
114 MonoObject *ves_icall_System_Threading_Interlocked_Exchange_Object(MonoObject **location, MonoObject *value);
115 gpointer ves_icall_System_Threading_Interlocked_Exchange_IntPtr(gpointer *location, gpointer value);
116 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single(gfloat *location, gfloat value);
117 gdouble ves_icall_System_Threading_Interlocked_Exchange_Double(gdouble *location, gdouble value);
118
119 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location, gint32 value, gint32 comparand);
120 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int_Success(gint32 *location, gint32 value, gint32 comparand, MonoBoolean *success);
121 gint64 ves_icall_System_Threading_Interlocked_CompareExchange_Long(gint64 *location, gint64 value, gint64 comparand);
122 MonoObject *ves_icall_System_Threading_Interlocked_CompareExchange_Object(MonoObject **location, MonoObject *value, MonoObject *comparand);
123 gpointer ves_icall_System_Threading_Interlocked_CompareExchange_IntPtr(gpointer *location, gpointer value, gpointer comparand);
124 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single(gfloat *location, gfloat value, gfloat comparand);
125 gdouble ves_icall_System_Threading_Interlocked_CompareExchange_Double(gdouble *location, gdouble value, gdouble comparand);
126 MonoObject* ves_icall_System_Threading_Interlocked_CompareExchange_T(MonoObject **location, MonoObject *value, MonoObject *comparand);
127 MonoObject* ves_icall_System_Threading_Interlocked_Exchange_T(MonoObject **location, MonoObject *value);
128
129 gint32 ves_icall_System_Threading_Interlocked_Add_Int(gint32 *location, gint32 value);
130 gint64 ves_icall_System_Threading_Interlocked_Add_Long(gint64 *location, gint64 value);
131 gint64 ves_icall_System_Threading_Interlocked_Read_Long(gint64 *location);
132
133 gint32 ves_icall_System_Threading_Interlocked_Increment_Int(gint32 *location);
134 gint64 ves_icall_System_Threading_Interlocked_Increment_Long(gint64 *location);
135
136 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int(gint32 *location);
137 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long(gint64 * location);
138
139 void ves_icall_System_Threading_Thread_Abort (MonoInternalThread *thread, MonoObject *state);
140 void ves_icall_System_Threading_Thread_ResetAbort (MonoThread *this_obj);
141 MonoObject* ves_icall_System_Threading_Thread_GetAbortExceptionState (MonoThread *thread);
142 void ves_icall_System_Threading_Thread_Suspend (MonoThread *this_obj);
143 void ves_icall_System_Threading_Thread_Resume (MonoThread *thread);
144 void ves_icall_System_Threading_Thread_ClrState (MonoInternalThread *thread, guint32 state);
145 void ves_icall_System_Threading_Thread_SetState (MonoInternalThread *thread, guint32 state);
146 guint32 ves_icall_System_Threading_Thread_GetState (MonoInternalThread *thread);
147
148 gint8 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr);
149 gint16 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr);
150 gint32 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr);
151 gint64 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr);
152 void * ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr);
153 void * ves_icall_System_Threading_Thread_VolatileReadObject (void *ptr);
154 double ves_icall_System_Threading_Thread_VolatileReadDouble (void *ptr);
155 float ves_icall_System_Threading_Thread_VolatileReadFloat (void *ptr);
156
157 void ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8);
158 void ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16);
159 void ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32);
160 void ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64);
161 void ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *);
162 void ves_icall_System_Threading_Thread_VolatileWriteObject (void *ptr, MonoObject *);
163 void ves_icall_System_Threading_Thread_VolatileWriteFloat (void *ptr, float);
164 void ves_icall_System_Threading_Thread_VolatileWriteDouble (void *ptr, double);
165
166 gint8 ves_icall_System_Threading_Volatile_Read1 (void *ptr);
167 gint16 ves_icall_System_Threading_Volatile_Read2 (void *ptr);
168 gint32 ves_icall_System_Threading_Volatile_Read4 (void *ptr);
169 gint64 ves_icall_System_Threading_Volatile_Read8 (void *ptr);
170 void * ves_icall_System_Threading_Volatile_ReadIntPtr (void *ptr);
171 double ves_icall_System_Threading_Volatile_ReadDouble (void *ptr);
172 float ves_icall_System_Threading_Volatile_ReadFloat (void *ptr);
173 MonoObject* ves_icall_System_Threading_Volatile_Read_T (void *ptr);
174
175 void ves_icall_System_Threading_Volatile_Write1 (void *ptr, gint8);
176 void ves_icall_System_Threading_Volatile_Write2 (void *ptr, gint16);
177 void ves_icall_System_Threading_Volatile_Write4 (void *ptr, gint32);
178 void ves_icall_System_Threading_Volatile_Write8 (void *ptr, gint64);
179 void ves_icall_System_Threading_Volatile_WriteIntPtr (void *ptr, void *);
180 void ves_icall_System_Threading_Volatile_WriteFloat (void *ptr, float);
181 void ves_icall_System_Threading_Volatile_WriteDouble (void *ptr, double);
182 void ves_icall_System_Threading_Volatile_Write_T (void *ptr, MonoObject *value);
183
184 void ves_icall_System_Threading_Thread_MemoryBarrier (void);
185 void ves_icall_System_Threading_Thread_Interrupt_internal (MonoThread *this_obj);
186 void ves_icall_System_Threading_Thread_SpinWait_nop (void);
187
188 void ves_icall_System_Runtime_Remoting_Contexts_Context_RegisterContext (MonoAppContext *ctx);
189 void ves_icall_System_Runtime_Remoting_Contexts_Context_ReleaseContext (MonoAppContext *ctx);
190
191 MonoInternalThread *mono_thread_internal_current (void);
192
193 void mono_thread_internal_check_for_interruption_critical (MonoInternalThread *thread);
194
195 void mono_thread_internal_stop (MonoInternalThread *thread);
196
197 gboolean mono_thread_internal_has_appdomain_ref (MonoInternalThread *thread, MonoDomain *domain);
198
199 void mono_thread_internal_reset_abort (MonoInternalThread *thread);
200
201 void mono_thread_internal_unhandled_exception (MonoObject* exc);
202
203 void mono_alloc_special_static_data_free (GHashTable *special_static_fields);
204 void mono_thread_current_check_pending_interrupt (void);
205
206 void mono_thread_set_state (MonoInternalThread *thread, MonoThreadState state);
207 void mono_thread_clr_state (MonoInternalThread *thread, MonoThreadState state);
208 gboolean mono_thread_test_state (MonoInternalThread *thread, MonoThreadState test);
209
210 void mono_thread_init_apartment_state (void);
211 void mono_thread_cleanup_apartment_state (void);
212
213 void mono_threads_set_shutting_down (void);
214
215 gunichar2* mono_thread_get_name (MonoInternalThread *this_obj, guint32 *name_len);
216
217 MONO_API MonoException* mono_thread_get_undeniable_exception (void);
218
219 void mono_thread_set_name_internal (MonoInternalThread *this_obj, MonoString *name, gboolean permanent, MonoError *error);
220
221 void mono_runtime_set_has_tls_get (gboolean val);
222 gboolean mono_runtime_has_tls_get (void);
223
224 void mono_thread_suspend_all_other_threads (void);
225 gboolean mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout);
226
227 void mono_thread_push_appdomain_ref (MonoDomain *domain);
228 void mono_thread_pop_appdomain_ref (void);
229 gboolean mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain);
230
231 void mono_threads_clear_cached_culture (MonoDomain *domain);
232
233 MonoException* mono_thread_request_interruption (mono_bool running_managed);
234 gboolean mono_thread_interruption_requested (void);
235 MonoException* mono_thread_interruption_checkpoint (void);
236 MonoException* mono_thread_force_interruption_checkpoint_noraise (void);
237 gint32* mono_thread_interruption_request_flag (void);
238
239 uint32_t mono_alloc_special_static_data (uint32_t static_type, uint32_t size, uint32_t align, uintptr_t *bitmap, int numbits);
240 void*    mono_get_special_static_data   (uint32_t offset);
241 gpointer mono_get_special_static_data_for_thread (MonoInternalThread *thread, guint32 offset);
242
243 MonoException* mono_thread_resume_interruption (void);
244 void mono_threads_perform_thread_dump (void);
245
246 gboolean
247 mono_thread_create_checked (MonoDomain *domain, gpointer func, gpointer arg, MonoError *error);
248
249 MonoThread *
250 mono_thread_attach_full (MonoDomain *domain, gboolean force_attach, MonoError *error);
251
252 void mono_thread_init_tls (void);
253
254 /* Can't include utils/mono-threads.h because of the THREAD_INFO_TYPE wizardry */
255 void mono_threads_add_joinable_thread (gpointer tid);
256 void mono_threads_join_threads (void);
257 void mono_thread_join (gpointer tid);
258
259 void mono_thread_detach_internal (MonoInternalThread *thread);
260
261 void ves_icall_System_Threading_Thread_GetStackTraces (MonoArray **out_threads, MonoArray **out_stack_traces);
262
263 #endif /* _MONO_METADATA_THREADS_TYPES_H_ */