2 * System-dependent scheduler support
4 * Copyright 1998 Alexandre Julliard
14 #include <sys/resource.h>
15 #ifdef HAVE_SYS_SYSCALL_H
16 # include <sys/syscall.h>
21 #ifdef HAVE_UCONTEXT_H
22 # include <ucontext.h>
24 #ifdef HAVE_SYS_MMAN_H
28 #include "wine/server.h"
30 #include "wine/winbase16.h"
31 #include "wine/exception.h"
32 #include "debugtools.h"
34 DEFAULT_DEBUG_CHANNEL(thread);
36 #if defined(linux) || defined(HAVE_CLONE)
41 # define CLONE_VM 0x00000100
42 # define CLONE_FS 0x00000200
43 # define CLONE_FILES 0x00000400
44 # define CLONE_SIGHAND 0x00000800
45 # define CLONE_PID 0x00001000
46 # endif /* CLONE_VM */
47 #endif /* linux || HAVE_CLONE */
49 extern void SELECTOR_FreeFs(void);
51 struct thread_cleanup_info
58 /* temporary stacks used on thread exit */
59 #define TEMP_STACK_SIZE 1024
60 #define NB_TEMP_STACKS 8
61 static char temp_stacks[NB_TEMP_STACKS][TEMP_STACK_SIZE];
62 static LONG next_temp_stack; /* next temp stack to use */
64 /***********************************************************************
65 * SYSDEPS_SetCurThread
67 * Make 'thread' the current thread.
69 void SYSDEPS_SetCurThread( TEB *teb )
72 /* On the i386, the current thread is in the %fs register */
73 __set_fs( teb->teb_sel );
74 #elif defined(HAVE__LWP_CREATE)
75 /* On non-i386 Solaris, we use the LWP private pointer */
76 _lwp_setprivate( teb );
81 /***********************************************************************
82 * call_on_thread_stack
84 * Call a function once we switched to the thread stack.
86 static void call_on_thread_stack( void *func )
90 void (*funcptr)(void) = func;
93 __EXCEPT(UnhandledExceptionFilter)
95 TerminateThread( GetCurrentThread(), GetExceptionCode() );
98 SYSDEPS_ExitThread(0); /* should never get here */
102 /***********************************************************************
105 * Get a temporary stack address to run the thread exit code on.
107 inline static char *get_temp_stack(void)
109 unsigned int next = InterlockedExchangeAdd( &next_temp_stack, 1 );
110 return temp_stacks[next % NB_TEMP_STACKS];
114 /***********************************************************************
117 * Cleanup the remains of a thread. Runs on a temporary stack.
119 static void cleanup_thread( void *ptr )
121 /* copy the info structure since it is on the stack we will free */
122 struct thread_cleanup_info info = *(struct thread_cleanup_info *)ptr;
123 munmap( info.stack_base, info.stack_size );
125 #ifdef HAVE__LWP_CREATE
128 _exit( info.status );
132 /***********************************************************************
133 * SYSDEPS_StartThread
135 * Startup routine for a new thread.
137 static void SYSDEPS_StartThread( TEB *teb )
139 SYSDEPS_SetCurThread( teb );
146 __EXCEPT(UnhandledExceptionFilter)
148 TerminateThread( GetCurrentThread(), GetExceptionCode() );
151 SYSDEPS_ExitThread(0); /* should never get here */
155 /***********************************************************************
156 * SYSDEPS_SpawnThread
158 * Start running a new thread.
159 * Return -1 on error, 0 if OK.
161 int SYSDEPS_SpawnThread( TEB *teb )
163 #ifdef ERRNO_LOCATION
165 #if defined(linux) || defined(HAVE_CLONE)
166 const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
167 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
169 if (!(flags & CLONE_FILES)) close( teb->request_fd ); /* close the child socket in the parent */
174 const int flags = RFPROC | RFMEM; /*|RFFDG*/
175 void **sp = (void **)teb->stack_top;
178 *--sp = SYSDEPS_StartThread;
179 __asm__ __volatile__(
180 "pushl %2;\n\t" /* flags */
181 "pushl $0;\n\t" /* 0 ? */
182 "movl %1,%%eax;\n\t" /* SYS_rfork */
183 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
184 "cmpl $0, %%edx;\n\t"
186 "movl %0,%%esp;\n\t" /* child -> new thread */
188 "1:\n\t" /* parent -> caller thread */
190 : "r" (sp), "g" (SYS_rfork), "g" (flags)
192 if (flags & RFFDG) close( teb->request_fd ); /* close the child socket in the parent */
196 #ifdef HAVE__LWP_CREATE
198 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
199 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
200 if ( _lwp_create( &context, 0, NULL ) )
205 #endif /* ERRNO_LOCATION */
207 FIXME("CreateThread: stub\n" );
212 /***********************************************************************
213 * SYSDEPS_CallOnStack
215 void SYSDEPS_CallOnStack( void (*func)(LPVOID), LPVOID arg ) WINE_NORETURN;
217 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
218 "movl 4(%esp),%ecx\n\t" /* func */
219 "movl 8(%esp),%edx\n\t" /* arg */
220 ".byte 0x64\n\tmovl 0x04,%esp\n\t" /* teb->stack_top */
224 "int $3" /* we never return here */ );
226 void SYSDEPS_CallOnStack( void (*func)(LPVOID), LPVOID arg )
229 while(1); /* avoid warning */
234 /***********************************************************************
235 * SYSDEPS_SwitchToThreadStack
237 void SYSDEPS_SwitchToThreadStack( void (*func)(void) )
239 SYSDEPS_CallOnStack( call_on_thread_stack, func );
243 /***********************************************************************
246 * Exit a running thread; must not return.
248 void SYSDEPS_ExitThread( int status )
250 TEB *teb = NtCurrentTeb();
251 struct thread_cleanup_info info;
252 MEMORY_BASIC_INFORMATION meminfo;
254 FreeSelector16( teb->stack_sel );
255 VirtualQuery( teb->stack_top, &meminfo, sizeof(meminfo) );
256 info.stack_base = meminfo.AllocationBase;
257 info.stack_size = meminfo.RegionSize + ((char *)teb->stack_top - (char *)meminfo.AllocationBase);
258 info.status = status;
262 VirtualFree( teb->stack_base, 0, MEM_RELEASE | MEM_SYSTEM );
263 close( teb->wait_fd[0] );
264 close( teb->wait_fd[1] );
265 close( teb->reply_fd );
266 close( teb->request_fd );
267 teb->stack_low = get_temp_stack();
268 teb->stack_top = teb->stack_low + TEMP_STACK_SIZE;
269 SYSDEPS_CallOnStack( cleanup_thread, &info );
273 /***********************************************************************
274 * SYSDEPS_AbortThread
276 * Same as SYSDEPS_ExitThread, but must not do anything that requires a server call.
278 void SYSDEPS_AbortThread( int status )
281 close( NtCurrentTeb()->wait_fd[0] );
282 close( NtCurrentTeb()->wait_fd[1] );
283 close( NtCurrentTeb()->reply_fd );
284 close( NtCurrentTeb()->request_fd );
285 #ifdef HAVE__LWP_CREATE
288 for (;;) /* avoid warning */
293 /**********************************************************************
294 * NtCurrentTeb (NTDLL.@)
296 * This will crash and burn if called before threading is initialized
299 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
300 #elif defined(HAVE__LWP_CREATE)
301 /***********************************************************************
302 * NtCurrentTeb (NTDLL.@)
304 struct _TEB * WINAPI NtCurrentTeb(void)
306 extern void *_lwp_getprivate(void);
307 return (struct _TEB *)_lwp_getprivate();
310 # error NtCurrentTeb not defined for this architecture
311 #endif /* __i386__ */