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