Throw exception only for Wine internal locks.
[wine/gsoc_dplay.git] / scheduler / sysdeps.c
blob06170dbb07a49d2ca979cbc1cdd62072db2b7368
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 #include <unistd.h>
27 #include <sys/time.h>
28 #ifdef HAVE_SYS_SYSCALL_H
29 # include <sys/syscall.h>
30 #endif
31 #ifdef HAVE_SYS_LWP_H
32 # include <sys/lwp.h>
33 #endif
34 #ifdef HAVE_UCONTEXT_H
35 # include <ucontext.h>
36 #endif
37 #ifdef HAVE_SYS_MMAN_H
38 #include <sys/mman.h>
39 #endif
40 #include "thread.h"
41 #include "wine/server.h"
42 #include "winbase.h"
43 #include "wine/winbase16.h"
44 #include "wine/exception.h"
45 #include "wine/library.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(thread);
50 #if defined(linux) || defined(HAVE_CLONE)
51 # ifdef HAVE_SCHED_H
52 # include <sched.h>
53 # endif
54 # ifndef CLONE_VM
55 # define CLONE_VM 0x00000100
56 # define CLONE_FS 0x00000200
57 # define CLONE_FILES 0x00000400
58 # define CLONE_SIGHAND 0x00000800
59 # define CLONE_PID 0x00001000
60 # endif /* CLONE_VM */
61 #endif /* linux || HAVE_CLONE */
63 extern void SELECTOR_FreeFs(void);
65 struct thread_cleanup_info
67 void *stack_base;
68 int stack_size;
69 int status;
72 /* temporary stacks used on thread exit */
73 #define TEMP_STACK_SIZE 1024
74 #define NB_TEMP_STACKS 8
75 static char temp_stacks[NB_TEMP_STACKS][TEMP_STACK_SIZE];
76 static LONG next_temp_stack; /* next temp stack to use */
78 /***********************************************************************
79 * SYSDEPS_SetCurThread
81 * Make 'thread' the current thread.
83 void SYSDEPS_SetCurThread( TEB *teb )
85 #if defined(__i386__)
86 /* On the i386, the current thread is in the %fs register */
87 wine_set_fs( teb->teb_sel );
88 #elif defined(__powerpc__)
89 /* On PowerPC, the current TEB is in the gpr13 register */
90 __asm__ __volatile__("mr 13, %0" : : "r" (teb));
91 #elif defined(HAVE__LWP_CREATE)
92 /* On non-i386 Solaris, we use the LWP private pointer */
93 _lwp_setprivate( teb );
94 #endif
98 /***********************************************************************
99 * call_on_thread_stack
101 * Call a function once we switched to the thread stack.
103 static void call_on_thread_stack( void *func )
105 __TRY
107 void (*funcptr)(void) = func;
108 funcptr();
110 __EXCEPT(UnhandledExceptionFilter)
112 TerminateThread( GetCurrentThread(), GetExceptionCode() );
114 __ENDTRY
115 SYSDEPS_ExitThread(0); /* should never get here */
119 /***********************************************************************
120 * get_temp_stack
122 * Get a temporary stack address to run the thread exit code on.
124 inline static char *get_temp_stack(void)
126 unsigned int next = InterlockedExchangeAdd( &next_temp_stack, 1 );
127 return temp_stacks[next % NB_TEMP_STACKS];
131 /***********************************************************************
132 * cleanup_thread
134 * Cleanup the remains of a thread. Runs on a temporary stack.
136 static void cleanup_thread( void *ptr )
138 /* copy the info structure since it is on the stack we will free */
139 struct thread_cleanup_info info = *(struct thread_cleanup_info *)ptr;
140 munmap( info.stack_base, info.stack_size );
141 SELECTOR_FreeFs();
142 #ifdef HAVE__LWP_CREATE
143 _lwp_exit();
144 #endif
145 _exit( info.status );
149 /***********************************************************************
150 * SYSDEPS_StartThread
152 * Startup routine for a new thread.
154 static void SYSDEPS_StartThread( TEB *teb )
156 SYSDEPS_SetCurThread( teb );
157 CLIENT_InitThread();
158 SIGNAL_Init();
159 __TRY
161 teb->startup();
163 __EXCEPT(UnhandledExceptionFilter)
165 TerminateThread( GetCurrentThread(), GetExceptionCode() );
167 __ENDTRY
168 SYSDEPS_ExitThread(0); /* should never get here */
172 /***********************************************************************
173 * SYSDEPS_SpawnThread
175 * Start running a new thread.
176 * Return -1 on error, 0 if OK.
178 int SYSDEPS_SpawnThread( TEB *teb )
180 #ifdef ERRNO_LOCATION
182 #if defined(linux) || defined(HAVE_CLONE)
183 const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
184 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
185 return -1;
186 if (!(flags & CLONE_FILES)) close( teb->request_fd ); /* close the child socket in the parent */
187 return 0;
188 #endif
190 #ifdef HAVE_RFORK
191 const int flags = RFPROC | RFMEM; /*|RFFDG*/
192 void **sp = (void **)teb->stack_top;
193 *--sp = teb;
194 *--sp = 0;
195 *--sp = SYSDEPS_StartThread;
196 __asm__ __volatile__(
197 "pushl %2;\n\t" /* flags */
198 "pushl $0;\n\t" /* 0 ? */
199 "movl %1,%%eax;\n\t" /* SYS_rfork */
200 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
201 "cmpl $0, %%edx;\n\t"
202 "je 1f;\n\t"
203 "movl %0,%%esp;\n\t" /* child -> new thread */
204 "ret;\n"
205 "1:\n\t" /* parent -> caller thread */
206 "addl $8,%%esp" :
207 : "r" (sp), "g" (SYS_rfork), "g" (flags)
208 : "eax", "edx");
209 if (flags & RFFDG) close( teb->request_fd ); /* close the child socket in the parent */
210 return 0;
211 #endif
213 #ifdef HAVE__LWP_CREATE
214 ucontext_t context;
215 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
216 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
217 if ( _lwp_create( &context, 0, NULL ) )
218 return -1;
219 return 0;
220 #endif
222 #endif /* ERRNO_LOCATION */
224 FIXME("CreateThread: stub\n" );
225 return 0;
229 /***********************************************************************
230 * SYSDEPS_CallOnStack
232 void SYSDEPS_CallOnStack( void (*func)(LPVOID), LPVOID arg ) WINE_NORETURN;
233 #ifdef __i386__
234 #ifdef __GNUC__
235 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
236 "movl 4(%esp),%ecx\n\t" /* func */
237 "movl 8(%esp),%edx\n\t" /* arg */
238 ".byte 0x64\n\tmovl 0x04,%esp\n\t" /* teb->stack_top */
239 "pushl %edx\n\t"
240 "xorl %ebp,%ebp\n\t"
241 "call *%ecx\n\t"
242 "int $3" /* we never return here */ );
243 #elif defined(_MSC_VER)
244 __declspec(naked) void SYSDEPS_CallOnStack( void (*func)(LPVOID), LPVOID arg )
246 __asm mov ecx, 4[esp];
247 __asm mov edx, 8[esp];
248 __asm mov fs:[0x04], esp;
249 __asm push edx;
250 __asm xor ebp, ebp;
251 __asm call [ecx];
252 __asm int 3;
254 #endif /* defined(__GNUC__) || defined(_MSC_VER) */
255 #else /* !defined(__i386__) */
256 void SYSDEPS_CallOnStack( void (*func)(LPVOID), LPVOID arg )
258 func( arg );
259 while(1); /* avoid warning */
261 #endif /* !defined(__i386__) */
264 /***********************************************************************
265 * SYSDEPS_SwitchToThreadStack
267 void SYSDEPS_SwitchToThreadStack( void (*func)(void) )
269 SYSDEPS_CallOnStack( call_on_thread_stack, func );
273 /***********************************************************************
274 * SYSDEPS_ExitThread
276 * Exit a running thread; must not return.
278 void SYSDEPS_ExitThread( int status )
280 TEB *teb = NtCurrentTeb();
281 struct thread_cleanup_info info;
282 MEMORY_BASIC_INFORMATION meminfo;
284 FreeSelector16( teb->stack_sel );
285 VirtualQuery( teb->stack_top, &meminfo, sizeof(meminfo) );
286 info.stack_base = meminfo.AllocationBase;
287 info.stack_size = meminfo.RegionSize + ((char *)teb->stack_top - (char *)meminfo.AllocationBase);
288 info.status = status;
290 SIGNAL_Reset();
292 VirtualFree( teb->stack_base, 0, MEM_RELEASE | MEM_SYSTEM );
293 close( teb->wait_fd[0] );
294 close( teb->wait_fd[1] );
295 close( teb->reply_fd );
296 close( teb->request_fd );
297 teb->stack_low = get_temp_stack();
298 teb->stack_top = (char *) teb->stack_low + TEMP_STACK_SIZE;
299 SYSDEPS_CallOnStack( cleanup_thread, &info );
303 /***********************************************************************
304 * SYSDEPS_AbortThread
306 * Same as SYSDEPS_ExitThread, but must not do anything that requires a server call.
308 void SYSDEPS_AbortThread( int status )
310 SIGNAL_Reset();
311 close( NtCurrentTeb()->wait_fd[0] );
312 close( NtCurrentTeb()->wait_fd[1] );
313 close( NtCurrentTeb()->reply_fd );
314 close( NtCurrentTeb()->request_fd );
315 #ifdef HAVE__LWP_CREATE
316 _lwp_exit();
317 #endif
318 for (;;) /* avoid warning */
319 _exit( status );
323 /**********************************************************************
324 * NtCurrentTeb (NTDLL.@)
326 * This will crash and burn if called before threading is initialized
328 #if defined(__i386__) && defined(__GNUC__)
329 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
330 #elif defined(__i386__) && defined(_MSC_VER)
331 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
332 #elif defined(HAVE__LWP_CREATE)
333 /***********************************************************************
334 * NtCurrentTeb (NTDLL.@)
336 struct _TEB * WINAPI NtCurrentTeb(void)
338 extern void *_lwp_getprivate(void);
339 return (struct _TEB *)_lwp_getprivate();
341 #elif defined(__powerpc__)
342 __ASM_GLOBAL_FUNC( NtCurrentTeb, "\n\tmr 3,13\n\tblr" );
343 #else
344 # error NtCurrentTeb not defined for this architecture
345 #endif /* __i386__ */