* config/avr/avr.md ("mcu_enhanced"): New attribute.
[official-gcc.git] / boehm-gc / win32_threads.c
blob469fd232003997f2e3ea888740d71f3bf9443a6a
1 #ifdef WIN32_THREADS
3 #include "gc_priv.h"
5 #if 0
6 #define STRICT
7 #include <windows.h>
8 #endif
10 #define MAX_THREADS 64
12 struct thread_entry {
13 LONG in_use;
14 DWORD id;
15 HANDLE handle;
16 void *stack; /* The cold end of the stack. */
17 /* 0 ==> entry not valid. */
18 /* !in_use ==> stack == 0 */
19 CONTEXT context;
20 GC_bool suspended;
23 volatile GC_bool GC_please_stop = FALSE;
25 volatile struct thread_entry thread_table[MAX_THREADS];
27 void GC_stop_world()
29 DWORD thread_id = GetCurrentThreadId();
30 int i;
32 GC_please_stop = TRUE;
33 for (i = 0; i < MAX_THREADS; i++)
34 if (thread_table[i].stack != 0
35 && thread_table[i].id != thread_id) {
36 if (SuspendThread(thread_table[i].handle) == (DWORD)-1)
37 ABORT("SuspendThread failed");
38 thread_table[i].suspended = TRUE;
42 void GC_start_world()
44 DWORD thread_id = GetCurrentThreadId();
45 int i;
46 for (i = 0; i < MAX_THREADS; i++)
47 if (thread_table[i].stack != 0 && thread_table[i].suspended
48 && thread_table[i].id != thread_id) {
49 if (ResumeThread(thread_table[i].handle) == (DWORD)-1)
50 ABORT("ResumeThread failed");
51 thread_table[i].suspended = FALSE;
53 GC_please_stop = FALSE;
56 ptr_t GC_current_stackbottom()
58 DWORD thread_id = GetCurrentThreadId();
59 int i;
60 for (i = 0; i < MAX_THREADS; i++)
61 if (thread_table[i].stack && thread_table[i].id == thread_id)
62 return thread_table[i].stack;
63 ABORT("no thread table entry for current thread");
66 static ptr_t GC_get_lo_stack_addr(ptr_t s)
68 ptr_t bottom;
69 MEMORY_BASIC_INFORMATION info;
70 VirtualQuery(s, &info, sizeof(info));
71 do {
72 bottom = info.BaseAddress;
73 VirtualQuery(bottom - 1, &info, sizeof(info));
74 } while ((info.Protect & PAGE_READWRITE) && !(info.Protect & PAGE_GUARD));
75 return(bottom);
78 void GC_push_all_stacks()
80 DWORD thread_id = GetCurrentThreadId();
81 int i;
82 for (i = 0; i < MAX_THREADS; i++)
83 if (thread_table[i].stack) {
84 ptr_t bottom = GC_get_lo_stack_addr(thread_table[i].stack);
85 if (thread_table[i].id == thread_id)
86 GC_push_all_stack(&i, thread_table[i].stack);
87 else {
88 thread_table[i].context.ContextFlags
89 = (CONTEXT_INTEGER|CONTEXT_CONTROL);
90 if (!GetThreadContext(thread_table[i].handle,
91 &thread_table[i].context))
92 ABORT("GetThreadContext failed");
93 if (thread_table[i].context.Esp >= (DWORD)thread_table[i].stack
94 || thread_table[i].context.Esp < (DWORD)bottom)
95 ABORT("Thread stack pointer out of range");
96 GC_push_one ((word) thread_table[i].context.Edi);
97 GC_push_one ((word) thread_table[i].context.Esi);
98 GC_push_one ((word) thread_table[i].context.Ebx);
99 GC_push_one ((word) thread_table[i].context.Edx);
100 GC_push_one ((word) thread_table[i].context.Ecx);
101 GC_push_one ((word) thread_table[i].context.Eax);
102 GC_push_all_stack(thread_table[i].context.Esp, thread_table[i].stack);
107 void GC_get_next_stack(char *start, char **lo, char **hi)
109 int i;
110 # define ADDR_LIMIT (char *)(-1L)
111 char * current_min = ADDR_LIMIT;
113 for (i = 0; i < MAX_THREADS; i++) {
114 char * s = (char *)thread_table[i].stack;
116 if (0 != s && s > start && s < current_min) {
117 current_min = s;
120 *hi = current_min;
121 if (current_min == ADDR_LIMIT) {
122 *lo = ADDR_LIMIT;
123 return;
125 *lo = GC_get_lo_stack_addr(current_min);
126 if (*lo < start) *lo = start;
129 LONG WINAPI GC_write_fault_handler(struct _EXCEPTION_POINTERS *exc_info);
132 * This isn't generally safe, since DllMain is not premptible.
133 * If another thread holds the lock while this runs we're in trouble.
134 * Pontus Rydin suggests wrapping the thread start routine instead.
136 BOOL WINAPI DllMain(HINSTANCE inst, ULONG reason, LPVOID reserved)
138 switch (reason) {
139 case DLL_PROCESS_ATTACH:
140 InitializeCriticalSection(&GC_allocate_ml);
141 GC_init(); /* Force initialization before thread attach. */
142 /* fall through */
143 case DLL_THREAD_ATTACH:
145 int i;
146 /* It appears to be unsafe to acquire a lock here, since this */
147 /* code is apparently not preeemptible on some systems. */
148 /* (This is based on complaints, not on Microsoft's official */
149 /* documentation, which says this should perform "only simple */
150 /* inititalization tasks".) */
151 /* Hence we make do with nonblocking synchronization. */
153 /* The following should be a noop according to the win32 */
154 /* documentation. There is empirical evidence that it */
155 /* isn't. - HB */
156 # ifndef SMALL_CONFIG
157 if (GC_incremental) SetUnhandledExceptionFilter(GC_write_fault_handler);
158 # endif
160 for (i = 0; InterlockedExchange(&thread_table[i].in_use,1) != 0; i++) {
161 /* Compare-and-swap would make this cleaner, but that's not */
162 /* supported before Windows 98 and NT 4.0. In Windows 2000, */
163 /* InterlockedExchange is supposed to be replaced by */
164 /* InterlockedExchangePointer, but that's not really what I */
165 /* want here. */
166 if (i == MAX_THREADS - 1)
167 ABORT("too many threads");
169 thread_table[i].id = GetCurrentThreadId();
170 if (!DuplicateHandle(GetCurrentProcess(),
171 GetCurrentThread(),
172 GetCurrentProcess(),
173 &thread_table[i].handle,
176 DUPLICATE_SAME_ACCESS)) {
177 DWORD last_error = GetLastError();
178 GC_printf1("Last error code: %lx\n", last_error);
179 ABORT("DuplicateHandle failed");
181 thread_table[i].stack = GC_get_stack_base();
182 /* If this thread is being created while we are trying to stop */
183 /* the world, wait here. Hopefully this can't happen on any */
184 /* systems that don't allow us to block here. */
185 while (GC_please_stop) Sleep(20);
187 break;
188 case DLL_PROCESS_DETACH:
189 case DLL_THREAD_DETACH:
191 int i;
192 DWORD thread_id = GetCurrentThreadId();
193 LOCK();
194 for (i = 0;
195 thread_table[i].stack == 0 || thread_table[i].id != thread_id;
196 i++) {
197 if (i == MAX_THREADS - 1)
198 ABORT("thread not found on detach");
200 thread_table[i].stack = 0;
201 thread_table[i].in_use = FALSE;
202 CloseHandle(thread_table[i].handle);
203 BZERO(&thread_table[i].context, sizeof(CONTEXT));
204 UNLOCK();
206 break;
208 return TRUE;
211 #endif /* WIN32_THREADS */