Merge pull request #819 from brendanzagaeski/patch-1
[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 #ifdef USE_MONO_CTX
61         memset (&info->ctx, 0, sizeof (MonoContext));
62 #ifdef TARGET_AMD64
63         info->ctx.rip = context.Rip;
64         info->ctx.rax = context.Rax;
65         info->ctx.rcx = context.Rcx;
66         info->ctx.rdx = context.Rdx;
67         info->ctx.rbx = context.Rbx;
68         info->ctx.rsp = context.Rsp;
69         info->ctx.rbp = context.Rbp;
70         info->ctx.rsi = context.Rsi;
71         info->ctx.rdi = context.Rdi;
72         info->ctx.r8 = context.R8;
73         info->ctx.r9 = context.R9;
74         info->ctx.r10 = context.R10;
75         info->ctx.r11 = context.R11;
76         info->ctx.r12 = context.R12;
77         info->ctx.r13 = context.R13;
78         info->ctx.r14 = context.R14;
79         info->ctx.r15 = context.R15;
80         info->stopped_ip = info->ctx.rip;
81         info->stack_start = (char*)info->ctx.rsp - REDZONE_SIZE;
82 #else
83         info->ctx.edi = context.Edi;
84         info->ctx.esi = context.Esi;
85         info->ctx.ebx = context.Ebx;
86         info->ctx.edx = context.Edx;
87         info->ctx.ecx = context.Ecx;
88         info->ctx.eax = context.Eax;
89         info->ctx.ebp = context.Ebp;
90         info->ctx.esp = context.Esp;
91         info->stopped_ip = (gpointer)context.Eip;
92         info->stack_start = (char*)context.Esp - REDZONE_SIZE;
93 #endif
94
95 #else
96         info->regs [0] = context.Edi;
97         info->regs [1] = context.Esi;
98         info->regs [2] = context.Ebx;
99         info->regs [3] = context.Edx;
100         info->regs [4] = context.Ecx;
101         info->regs [5] = context.Eax;
102         info->regs [6] = context.Ebp;
103         info->regs [7] = context.Esp;
104         info->stopped_ip = (gpointer)context.Eip;
105         info->stack_start = (char*)context.Esp - REDZONE_SIZE;
106 #endif
107
108         /* Notify the JIT */
109         if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
110                 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, NULL, NULL);
111
112         return TRUE;
113 }
114
115 void
116 sgen_wait_for_suspend_ack (int count)
117 {
118         /* Win32 suspend/resume is synchronous, so we don't need to wait for anything */
119 }
120
121 int
122 sgen_thread_handshake (BOOL suspend)
123 {
124         SgenThreadInfo *info;
125         SgenThreadInfo *current = mono_thread_info_current ();
126         int count = 0;
127
128         FOREACH_THREAD_SAFE (info) {
129                 if (info == current)
130                         continue;
131                 if (info->gc_disabled)
132                         continue;
133                 if (suspend) {
134                         if (!sgen_suspend_thread (info))
135                                 continue;
136                 } else {
137                         if (!sgen_resume_thread (info))
138                                 continue;
139                 }
140                 ++count;
141         } END_FOREACH_THREAD_SAFE
142         return count;
143 }
144
145 void
146 sgen_os_init (void)
147 {
148 }
149
150 int
151 mono_gc_get_suspend_signal (void)
152 {
153         return -1;
154 }
155
156 #endif