7d38808ea6cab4bca7dc074cdb1ac497d74f45f6
[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 <windows.h>
6
7 #include "metadata/sgen-gc.h"
8 #include "metadata/gc-internal.h"
9
10 gboolean
11 mono_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 mono_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 (handle);
36
37         result = SuspendThread (handle);
38         g_assert (result != (DWORD)-1);
39         if (result == (DWORD)-1) {
40                 CloseHandle (handle);
41                 return FALSE;
42         }
43
44         context.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
45
46         if (!GetThreadContext (handle, &context)) {
47                 g_assert_not_reached ();
48                 ResumeThread (handle);
49                 CloseHandle (handle);
50                 return FALSE;
51         }
52
53         g_assert (context.ContextFlags & CONTEXT_INTEGER);
54         g_assert (context.ContextFlags & CONTEXT_CONTROL);
55
56         CloseHandle (handle);
57
58         info->stopped_domain = NULL; /* FIXME: implement! */
59         info->stopped_ip = (gpointer)context.Eip;
60         info->stack_start = (char*)context.Esp - REDZONE_SIZE;
61
62         info->regs [0] = context.Edi;
63         info->regs [1] = context.Esi;
64         info->regs [2] = context.Ebx;
65         info->regs [3] = context.Edx;
66         info->regs [4] = context.Ecx;
67         info->regs [5] = context.Eax;
68         info->regs [6] = context.Ebp;
69         info->regs [7] = context.Esp;
70         info->stopped_regs = &info->regs;
71
72         /* Notify the JIT */
73         if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
74                 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, NULL);
75
76         return TRUE;
77 }
78
79 void
80 mono_sgen_wait_for_suspend_ack (int count)
81 {
82         /* Win32 suspend/resume is synchronous, so we don't need to wait for anything */
83 }
84
85 int
86 mono_sgen_thread_handshake (BOOL suspend)
87 {
88         SgenThreadInfo *info;
89         SgenThreadInfo *current = mono_thread_info_current ();
90         int count = 0;
91
92         FOREACH_THREAD_SAFE (info) {
93                 if (info == current)
94                         continue;
95                 if (suspend) {
96                         g_assert (!info->doing_handshake);
97                         info->doing_handshake = TRUE;
98
99                         if (!mono_sgen_suspend_thread (info))
100                                 continue;
101                 } else {
102                         g_assert (info->doing_handshake);
103                         info->doing_handshake = FALSE;
104
105                         if (!mono_sgen_resume_thread (info))
106                                 continue;
107                 }
108
109                 ++count;
110         } END_FOREACH_THREAD_SAFE
111         return count;
112 }
113
114 void
115 mono_sgen_os_init (void)
116 {
117 }
118
119 int
120 mono_gc_get_suspend_signal (void)
121 {
122         return -1;
123 }
124
125 #endif