Merge pull request #708 from spicypixel/hotfix/mono-object-to-string
[mono.git] / mono / metadata / sgen-os-win32.c
1 #include "config.h"
2
3 #if defined(HAVE_SGEN_GC) && defined(HOST_WIN32)
4
5 #include "io-layer/io-layer.h"
6
7 #include "metadata/sgen-gc.h"
8 #include "metadata/gc-internal.h"
9
10 gboolean
11 sgen_resume_thread (SgenThreadInfo *info)
12 {
13         DWORD id = mono_thread_info_get_tid (info);
14         HANDLE handle = OpenThread (THREAD_ALL_ACCESS, FALSE, id);
15         DWORD result;
16
17         g_assert (handle);
18
19         result = ResumeThread (handle);
20         g_assert (result != (DWORD)-1);
21
22         CloseHandle (handle);
23
24         return result != (DWORD)-1;
25 }
26
27 gboolean
28 sgen_suspend_thread (SgenThreadInfo *info)
29 {
30         DWORD id = mono_thread_info_get_tid (info);
31         HANDLE handle = OpenThread (THREAD_ALL_ACCESS, FALSE, id);
32         CONTEXT context;
33         DWORD result;
34
35         g_assert (id != GetCurrentThreadId ());
36
37         g_assert (handle);
38
39         result = SuspendThread (handle);
40         if (result == (DWORD)-1) {
41                 fprintf (stderr, "could not suspend thread %x (handle %p): %d\n", id, handle, GetLastError ()); fflush (stderr);
42                 CloseHandle (handle);
43                 return FALSE;
44         }
45
46         context.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
47
48         if (!GetThreadContext (handle, &context)) {
49                 g_assert_not_reached ();
50                 ResumeThread (handle);
51                 CloseHandle (handle);
52                 return FALSE;
53         }
54
55         g_assert (context.ContextFlags & CONTEXT_INTEGER);
56         g_assert (context.ContextFlags & CONTEXT_CONTROL);
57
58         CloseHandle (handle);
59
60         info->stopped_ip = (gpointer)context.Eip;
61         info->stack_start = (char*)context.Esp - REDZONE_SIZE;
62
63 #ifdef USE_MONO_CTX
64         memset (&info->ctx, 0, sizeof (MonoContext));
65         info->ctx.edi = context.Edi;
66         info->ctx.esi = context.Esi;
67         info->ctx.ebx = context.Ebx;
68         info->ctx.edx = context.Edx;
69         info->ctx.ecx = context.Ecx;
70         info->ctx.eax = context.Eax;
71         info->ctx.ebp = context.Ebp;
72         info->ctx.esp = context.Esp;
73 #else
74         info->regs [0] = context.Edi;
75         info->regs [1] = context.Esi;
76         info->regs [2] = context.Ebx;
77         info->regs [3] = context.Edx;
78         info->regs [4] = context.Ecx;
79         info->regs [5] = context.Eax;
80         info->regs [6] = context.Ebp;
81         info->regs [7] = context.Esp;
82 #endif
83
84         /* Notify the JIT */
85         if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
86                 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, NULL, NULL);
87
88         return TRUE;
89 }
90
91 void
92 sgen_wait_for_suspend_ack (int count)
93 {
94         /* Win32 suspend/resume is synchronous, so we don't need to wait for anything */
95 }
96
97 int
98 sgen_thread_handshake (BOOL suspend)
99 {
100         SgenThreadInfo *info;
101         SgenThreadInfo *current = mono_thread_info_current ();
102         int count = 0;
103
104         FOREACH_THREAD_SAFE (info) {
105                 if (info->joined_stw == suspend)
106                         continue;
107                 info->joined_stw = suspend;
108                 if (info == current)
109                         continue;
110                 if (info->gc_disabled)
111                         continue;
112                 if (suspend) {
113                         g_assert (!info->doing_handshake);
114                         info->doing_handshake = TRUE;
115
116                         if (!sgen_suspend_thread (info))
117                                 continue;
118                 } else {
119                         g_assert (info->doing_handshake);
120                         info->doing_handshake = FALSE;
121
122                         if (!sgen_resume_thread (info))
123                                 continue;
124                 }
125                 ++count;
126         } END_FOREACH_THREAD_SAFE
127         return count;
128 }
129
130 void
131 sgen_os_init (void)
132 {
133 }
134
135 int
136 mono_gc_get_suspend_signal (void)
137 {
138         return -1;
139 }
140
141 #endif