Add few SFGAO_CAN* attributes to supported list.
[wine/hacks.git] / scheduler / sysdeps.c
blobfd88451b5d5bf7b37b5592aa3f8bf3c22e8fbb00
1 /*
2 * System-dependent scheduler support
4 * Copyright 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <signal.h>
25 #include <stdio.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
31 #endif
32 #ifdef HAVE_SYS_SYSCALL_H
33 # include <sys/syscall.h>
34 #endif
35 #ifdef HAVE_SYS_LWP_H
36 # include <sys/lwp.h>
37 #endif
38 #ifdef HAVE_UCONTEXT_H
39 # include <ucontext.h>
40 #endif
41 #ifdef HAVE_SYS_MMAN_H
42 #include <sys/mman.h>
43 #endif
44 #include "thread.h"
45 #include "wine/server.h"
46 #include "winbase.h"
47 #include "wine/winbase16.h"
48 #include "wine/exception.h"
49 #include "wine/library.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(thread);
54 #if defined(linux) || defined(HAVE_CLONE)
55 # ifdef HAVE_SCHED_H
56 # include <sched.h>
57 # endif
58 # ifndef CLONE_VM
59 # define CLONE_VM 0x00000100
60 # define CLONE_FS 0x00000200
61 # define CLONE_FILES 0x00000400
62 # define CLONE_SIGHAND 0x00000800
63 # define CLONE_PID 0x00001000
64 # endif /* CLONE_VM */
65 #endif /* linux || HAVE_CLONE */
67 struct thread_cleanup_info
69 void *stack_base;
70 int stack_size;
71 int status;
74 /* temporary stacks used on thread exit */
75 #define TEMP_STACK_SIZE 1024
76 #define NB_TEMP_STACKS 8
77 static char temp_stacks[NB_TEMP_STACKS][TEMP_STACK_SIZE];
78 static LONG next_temp_stack; /* next temp stack to use */
80 /***********************************************************************
81 * SYSDEPS_SetCurThread
83 * Make 'thread' the current thread.
85 void SYSDEPS_SetCurThread( TEB *teb )
87 #if defined(__i386__)
88 /* On the i386, the current thread is in the %fs register */
89 LDT_ENTRY fs_entry;
91 wine_ldt_set_base( &fs_entry, teb );
92 wine_ldt_set_limit( &fs_entry, 0xfff );
93 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
94 wine_ldt_init_fs( teb->teb_sel, &fs_entry );
95 teb->gs_sel = wine_get_gs();
96 #elif defined(__powerpc__)
97 /* On PowerPC, the current TEB is in the gpr13 register */
98 __asm__ __volatile__("mr 2, %0" : : "r" (teb));
99 #elif defined(HAVE__LWP_CREATE)
100 /* On non-i386 Solaris, we use the LWP private pointer */
101 _lwp_setprivate( teb );
102 #endif
106 /***********************************************************************
107 * call_on_thread_stack
109 * Call a function once we switched to the thread stack.
111 static void call_on_thread_stack( void *func )
113 __TRY
115 void (*funcptr)(void) = func;
116 funcptr();
118 __EXCEPT(UnhandledExceptionFilter)
120 TerminateThread( GetCurrentThread(), GetExceptionCode() );
122 __ENDTRY
123 SYSDEPS_ExitThread(0); /* should never get here */
127 /***********************************************************************
128 * get_temp_stack
130 * Get a temporary stack address to run the thread exit code on.
132 inline static char *get_temp_stack(void)
134 unsigned int next = InterlockedExchangeAdd( &next_temp_stack, 1 );
135 return temp_stacks[next % NB_TEMP_STACKS];
139 /***********************************************************************
140 * cleanup_thread
142 * Cleanup the remains of a thread. Runs on a temporary stack.
144 static void cleanup_thread( void *ptr )
146 /* copy the info structure since it is on the stack we will free */
147 struct thread_cleanup_info info = *(struct thread_cleanup_info *)ptr;
148 munmap( info.stack_base, info.stack_size );
149 wine_ldt_free_fs( wine_get_fs() );
150 #ifdef HAVE__LWP_CREATE
151 _lwp_exit();
152 #endif
153 _exit( info.status );
157 /***********************************************************************
158 * SYSDEPS_StartThread
160 * Startup routine for a new thread.
162 static void SYSDEPS_StartThread( TEB *teb )
164 SYSDEPS_SetCurThread( teb );
165 CLIENT_InitThread();
166 SIGNAL_Init();
167 __TRY
169 teb->startup();
171 __EXCEPT(UnhandledExceptionFilter)
173 TerminateThread( GetCurrentThread(), GetExceptionCode() );
175 __ENDTRY
176 SYSDEPS_ExitThread(0); /* should never get here */
180 /***********************************************************************
181 * SYSDEPS_SpawnThread
183 * Start running a new thread.
184 * Return -1 on error, 0 if OK.
186 int SYSDEPS_SpawnThread( TEB *teb )
188 #ifdef ERRNO_LOCATION
190 #if defined(linux) || defined(HAVE_CLONE)
191 const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
192 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
193 return -1;
194 if (!(flags & CLONE_FILES)) close( teb->request_fd ); /* close the child socket in the parent */
195 return 0;
196 #endif
198 #ifdef HAVE_RFORK
199 const int flags = RFPROC | RFMEM; /*|RFFDG*/
200 void **sp = (void **)teb->stack_top;
201 *--sp = teb;
202 *--sp = 0;
203 *--sp = SYSDEPS_StartThread;
204 __asm__ __volatile__(
205 "pushl %2;\n\t" /* flags */
206 "pushl $0;\n\t" /* 0 ? */
207 "movl %1,%%eax;\n\t" /* SYS_rfork */
208 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
209 "cmpl $0, %%edx;\n\t"
210 "je 1f;\n\t"
211 "movl %0,%%esp;\n\t" /* child -> new thread */
212 "ret;\n"
213 "1:\n\t" /* parent -> caller thread */
214 "addl $8,%%esp" :
215 : "r" (sp), "g" (SYS_rfork), "g" (flags)
216 : "eax", "edx");
217 if (flags & RFFDG) close( teb->request_fd ); /* close the child socket in the parent */
218 return 0;
219 #endif
221 #ifdef HAVE__LWP_CREATE
222 ucontext_t context;
223 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
224 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
225 if ( _lwp_create( &context, 0, NULL ) )
226 return -1;
227 return 0;
228 #endif
230 #endif /* ERRNO_LOCATION */
232 FIXME("CreateThread: stub\n" );
233 return 0;
237 /***********************************************************************
238 * SYSDEPS_CallOnStack
240 void DECLSPEC_NORETURN SYSDEPS_CallOnStack( void (*func)(LPVOID), LPVOID arg );
241 #ifdef __i386__
242 # ifdef __GNUC__
243 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
244 "movl 4(%esp),%ecx\n\t" /* func */
245 "movl 8(%esp),%edx\n\t" /* arg */
246 ".byte 0x64\n\tmovl 0x04,%esp\n\t" /* teb->stack_top */
247 "pushl %edx\n\t"
248 "xorl %ebp,%ebp\n\t"
249 "call *%ecx\n\t"
250 "int $3" /* we never return here */ );
251 # elif defined(_MSC_VER)
252 __declspec(naked) void SYSDEPS_CallOnStack( void (*func)(LPVOID), LPVOID arg )
254 __asm mov ecx, 4[esp];
255 __asm mov edx, 8[esp];
256 __asm mov fs:[0x04], esp;
257 __asm push edx;
258 __asm xor ebp, ebp;
259 __asm call [ecx];
260 __asm int 3;
262 # endif /* defined(__GNUC__) || defined(_MSC_VER) */
263 #elif defined(__sparc__) && defined(__GNUC__)
264 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
265 "mov %o0, %l0\n\t" /* store first argument */
266 "call " __ASM_NAME("NtCurrentTeb") ", 0\n\t"
267 "mov %o1, %l1\n\t" /* delay slot: store second argument */
268 "ld [%o0+4], %sp\n\t" /* teb->stack_top */
269 "call %l0, 0\n\t" /* call func */
270 "mov %l1, %o0\n\t" /* delay slot: arg for func */
271 "ta 0x01\n\t"); /* breakpoint - we never get here */
272 #else /* !sparc, !i386 */
273 void SYSDEPS_CallOnStack( void (*func)(LPVOID), LPVOID arg )
275 func( arg );
276 while(1); /* avoid warning */
278 #endif /* !defined(__i386__) && !defined(__sparc__) */
281 /***********************************************************************
282 * SYSDEPS_SwitchToThreadStack
284 void SYSDEPS_SwitchToThreadStack( void (*func)(void) )
286 SYSDEPS_CallOnStack( call_on_thread_stack, func );
290 /***********************************************************************
291 * SYSDEPS_ExitThread
293 * Exit a running thread; must not return.
295 void SYSDEPS_ExitThread( int status )
297 TEB *teb = NtCurrentTeb();
298 struct thread_cleanup_info info;
299 MEMORY_BASIC_INFORMATION meminfo;
301 wine_ldt_free_entries( teb->stack_sel, 1 );
302 VirtualQuery( teb->stack_top, &meminfo, sizeof(meminfo) );
303 info.stack_base = meminfo.AllocationBase;
304 info.stack_size = meminfo.RegionSize + ((char *)teb->stack_top - (char *)meminfo.AllocationBase);
305 info.status = status;
307 SIGNAL_Reset();
309 VirtualFree( teb->stack_base, 0, MEM_RELEASE | MEM_SYSTEM );
310 close( teb->wait_fd[0] );
311 close( teb->wait_fd[1] );
312 close( teb->reply_fd );
313 close( teb->request_fd );
314 teb->stack_low = get_temp_stack();
315 teb->stack_top = (char *) teb->stack_low + TEMP_STACK_SIZE;
316 SYSDEPS_CallOnStack( cleanup_thread, &info );
320 /***********************************************************************
321 * SYSDEPS_AbortThread
323 * Same as SYSDEPS_ExitThread, but must not do anything that requires a server call.
325 void SYSDEPS_AbortThread( int status )
327 SIGNAL_Reset();
328 close( NtCurrentTeb()->wait_fd[0] );
329 close( NtCurrentTeb()->wait_fd[1] );
330 close( NtCurrentTeb()->reply_fd );
331 close( NtCurrentTeb()->request_fd );
332 #ifdef HAVE__LWP_CREATE
333 _lwp_exit();
334 #endif
335 for (;;) /* avoid warning */
336 _exit( status );
340 /**********************************************************************
341 * NtCurrentTeb (NTDLL.@)
343 * This will crash and burn if called before threading is initialized
345 #if defined(__i386__) && defined(__GNUC__)
346 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
347 #elif defined(__i386__) && defined(_MSC_VER)
348 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
349 #elif defined(HAVE__LWP_CREATE)
350 /***********************************************************************
351 * NtCurrentTeb (NTDLL.@)
353 struct _TEB * WINAPI NtCurrentTeb(void)
355 extern void *_lwp_getprivate(void);
356 return (struct _TEB *)_lwp_getprivate();
358 #elif defined(__powerpc__)
359 __ASM_GLOBAL_FUNC( NtCurrentTeb, "\n\tmr 3,2\n\tblr" );
360 #else
361 # error NtCurrentTeb not defined for this architecture
362 #endif /* __i386__ */