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
22 #include "wine/port.h"
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
32 #ifdef HAVE_SYS_SYSCALL_H
33 # include <sys/syscall.h>
38 #ifdef HAVE_UCONTEXT_H
39 # include <ucontext.h>
41 #ifdef HAVE_SYS_MMAN_H
53 #include "wine/server.h"
55 #include "wine/library.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(thread);
60 struct thread_cleanup_info
67 /* temporary stacks used on thread exit */
68 #define TEMP_STACK_SIZE 1024
69 #define NB_TEMP_STACKS 8
70 static char temp_stacks[NB_TEMP_STACKS][TEMP_STACK_SIZE];
71 static LONG next_temp_stack; /* next temp stack to use */
73 extern void PTHREAD_init_thread(void);
75 /***********************************************************************
76 * SYSDEPS_SetCurThread
78 * Make 'thread' the current thread.
80 void SYSDEPS_SetCurThread( TEB *teb )
83 /* On the i386, the current thread is in the %fs register */
86 wine_ldt_set_base( &fs_entry, teb );
87 wine_ldt_set_limit( &fs_entry, 0xfff );
88 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
89 wine_ldt_init_fs( teb->teb_sel, &fs_entry );
90 #elif defined(__powerpc__)
91 /* On PowerPC, the current TEB is in the gpr13 register */
93 __asm__ __volatile__("mr r13, %0" : : "r" (teb));
95 __asm__ __volatile__("mr 2, %0" : : "r" (teb));
97 #elif defined(HAVE__LWP_CREATE)
98 /* On non-i386 Solaris, we use the LWP private pointer */
99 _lwp_setprivate( teb );
103 teb->pthread_data = (void *)pthread_self();
105 PTHREAD_init_thread();
110 /***********************************************************************
113 * Get a temporary stack address to run the thread exit code on.
115 inline static char *get_temp_stack(void)
117 unsigned int next = interlocked_xchg_add( &next_temp_stack, 1 );
118 return temp_stacks[next % NB_TEMP_STACKS];
122 /***********************************************************************
125 * Cleanup the remains of a thread. Runs on a temporary stack.
127 static void cleanup_thread( void *ptr )
129 /* copy the info structure since it is on the stack we will free */
130 struct thread_cleanup_info info = *(struct thread_cleanup_info *)ptr;
131 munmap( info.stack_base, info.stack_size );
132 wine_ldt_free_fs( wine_get_fs() );
133 #ifdef HAVE__LWP_CREATE
136 _exit( info.status );
140 /***********************************************************************
141 * SYSDEPS_SpawnThread
143 * Start running a new thread.
144 * Return -1 on error, 0 if OK.
146 int SYSDEPS_SpawnThread( void (*func)(TEB *), TEB *teb )
152 pthread_attr_init( &attr );
153 pthread_attr_setstack( &attr, teb->DeallocationStack,
154 (char *)teb->Tib.StackBase - (char *)teb->DeallocationStack );
155 if (pthread_create( &id, &attr, (void * (*)(void *))func, teb )) return -1;
157 #elif defined(HAVE_CLONE)
158 if (clone( (int (*)(void *))func, teb->Tib.StackBase,
159 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, teb ) < 0)
162 #elif defined(HAVE_RFORK)
163 void **sp = (void **)teb->Tib.StackBase;
167 __asm__ __volatile__(
168 "pushl %2;\n\t" /* flags */
169 "pushl $0;\n\t" /* 0 ? */
170 "movl %1,%%eax;\n\t" /* SYS_rfork */
171 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
172 "cmpl $0, %%edx;\n\t"
174 "movl %0,%%esp;\n\t" /* child -> new thread */
176 "1:\n\t" /* parent -> caller thread */
178 : "r" (sp), "g" (SYS_rfork), "g" (RFPROC | RFMEM)
181 #elif defined(HAVE__LWP_CREATE)
183 _lwp_makecontext( &context, (void(*)(void *))func, teb,
184 NULL, teb->DeallocationStack, (char *)teb->Tib.StackBase - (char *)teb->DeallocationStack );
185 if ( _lwp_create( &context, 0, NULL ) )
190 FIXME("CreateThread: stub\n" );
195 /***********************************************************************
196 * SYSDEPS_SwitchToThreadStack
198 * Switch to the stack specified in the current thread TEB
199 * and call the specified function.
201 void DECLSPEC_NORETURN SYSDEPS_SwitchToThreadStack( void (*func)(void *), void *arg );
204 __ASM_GLOBAL_FUNC( SYSDEPS_SwitchToThreadStack,
205 "movl 4(%esp),%ecx\n\t" /* func */
206 "movl 8(%esp),%edx\n\t" /* arg */
207 ".byte 0x64\n\tmovl 0x04,%esp\n\t" /* teb->Tib.StackBase */
211 "int $3" /* we never return here */ );
212 # elif defined(_MSC_VER)
213 __declspec(naked) void SYSDEPS_SwitchToThreadStack( void (*func)(void *), void *arg )
215 __asm mov ecx, 4[esp];
216 __asm mov edx, 8[esp];
217 __asm mov fs:[0x04], esp;
223 # endif /* defined(__GNUC__) || defined(_MSC_VER) */
224 #elif defined(__sparc__) && defined(__GNUC__)
225 __ASM_GLOBAL_FUNC( SYSDEPS_SwitchToThreadStack,
226 "mov %o0, %l0\n\t" /* store first argument */
227 "call " __ASM_NAME("NtCurrentTeb") ", 0\n\t"
228 "mov %o1, %l1\n\t" /* delay slot: store second argument */
229 "ld [%o0+4], %sp\n\t" /* teb->Tib.StackBase */
230 "call %l0, 0\n\t" /* call func */
231 "mov %l1, %o0\n\t" /* delay slot: arg for func */
232 "ta 0x01\n\t"); /* breakpoint - we never get here */
233 #elif defined(__powerpc__) && defined(__APPLE__)
234 /* Darwin SYSDEPS_SwitchToThreadStack
235 Function Pointer to call is on r3, Args to pass on r4 and stack on r1 */
236 __ASM_GLOBAL_FUNC( SYSDEPS_SwitchToThreadStack,
237 "stw r1, 0x4(r13)\n\t" /* teb->Tib.StackBase */
239 "mtctr r12\n\t" /* func->ctr */
240 "mr r3,r4\n\t" /* args->function param 1 (r3) */
241 "bctr\n\t" /* call ctr */
242 "b _SYSDEPS_SwitchToThreadStack+24\n\t"); /* loop */
243 #elif defined(__powerpc__) && defined(__GNUC__)
244 /* Linux SYSDEPS_SwitchToThreadStack
245 Function Pointer to call is on r3, Args to pass on r4 and stack on r1 */
246 __ASM_GLOBAL_FUNC( SYSDEPS_SwitchToThreadStack,
247 "stw 1, 0x4(13)\n\t" /* teb->Tib.StackBase */
249 "mtctr 12\n\t" /* func->ctr */
250 "mr 3,4\n\t" /* args->function param 1 (r3) */
251 "bctr\n\t" /* call ctr */
252 "b _SYSDEPS_SwitchToThreadStack+24\n\t"); /* loop */
253 #else /* !powerpc, !sparc, !i386 */
254 void SYSDEPS_SwitchToThreadStack( void (*func)(void *), void *arg )
257 while(1); /* avoid warning */
259 #endif /* !defined(__i386__) && !defined(__sparc__) */
262 /***********************************************************************
265 * Exit a running thread; must not return.
267 void SYSDEPS_ExitThread( int status )
269 TEB *teb = NtCurrentTeb();
273 static TEB *teb_to_free;
276 if ((free_teb = interlocked_xchg_ptr( (void **)&teb_to_free, teb )) != NULL)
280 TRACE("freeing prev teb %p stack %p fs %04x\n",
281 free_teb, free_teb->DeallocationStack, free_teb->teb_sel );
283 pthread_join( (pthread_t)free_teb->pthread_data, &ptr );
284 wine_ldt_free_fs( free_teb->teb_sel );
285 ptr = free_teb->DeallocationStack;
286 NtFreeVirtualMemory( GetCurrentProcess(), &ptr, &size, MEM_RELEASE );
289 SYSDEPS_AbortThread( status );
291 struct thread_cleanup_info info;
292 MEMORY_BASIC_INFORMATION meminfo;
294 NtQueryVirtualMemory( GetCurrentProcess(), teb->Tib.StackBase, MemoryBasicInformation,
295 &meminfo, sizeof(meminfo), NULL );
296 info.stack_base = meminfo.AllocationBase;
297 info.stack_size = meminfo.RegionSize + ((char *)teb->Tib.StackBase - (char *)meminfo.AllocationBase);
298 info.status = status;
302 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE | MEM_SYSTEM );
303 close( teb->wait_fd[0] );
304 close( teb->wait_fd[1] );
305 close( teb->reply_fd );
306 close( teb->request_fd );
308 teb->Tib.StackLimit = get_temp_stack();
309 teb->Tib.StackBase = (char *)teb->Tib.StackLimit + TEMP_STACK_SIZE;
310 SYSDEPS_SwitchToThreadStack( cleanup_thread, &info );
315 /***********************************************************************
316 * SYSDEPS_AbortThread
318 * Same as SYSDEPS_ExitThread, but must not do anything that requires a server call.
320 void SYSDEPS_AbortThread( int status )
323 close( NtCurrentTeb()->wait_fd[0] );
324 close( NtCurrentTeb()->wait_fd[1] );
325 close( NtCurrentTeb()->reply_fd );
326 close( NtCurrentTeb()->request_fd );
328 pthread_exit( (void *)status );
331 #ifdef HAVE__LWP_CREATE
334 for (;;) /* avoid warning */
338 /***********************************************************************
341 * Get the Unix tid of the current thread.
343 int SYSDEPS_GetUnixTid(void)
345 #ifdef HAVE__LWP_SELF
347 #elif defined(__linux__) && defined(__i386__)
349 __asm__("int $0x80" : "=a" (ret) : "0" (224) /* SYS_gettid */);
350 if (ret < 0) ret = -1;
357 /**********************************************************************
358 * NtCurrentTeb (NTDLL.@)
360 * This will crash and burn if called before threading is initialized
362 #if defined(__i386__) && defined(__GNUC__)
363 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
364 #elif defined(__i386__) && defined(_MSC_VER)
365 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
366 #elif defined(HAVE__LWP_CREATE)
367 /***********************************************************************
368 * NtCurrentTeb (NTDLL.@)
370 struct _TEB * WINAPI NtCurrentTeb(void)
372 extern void *_lwp_getprivate(void);
373 return (struct _TEB *)_lwp_getprivate();
375 #elif defined(__powerpc__)
377 __ASM_GLOBAL_FUNC( NtCurrentTeb, "\n\tmr r3,r13\n\tblr" );
379 __ASM_GLOBAL_FUNC( NtCurrentTeb, "\n\tmr 3,2\n\tblr" );
382 # error NtCurrentTeb not defined for this architecture
383 #endif /* __i386__ */