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] + TEMP_STACK_SIZE;
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 /***********************************************************************
198 * Exit a running thread; must not return.
200 void SYSDEPS_ExitThread( int status )
202 TEB *teb = NtCurrentTeb();
206 static TEB *teb_to_free;
209 if ((free_teb = interlocked_xchg_ptr( (void **)&teb_to_free, teb )) != NULL)
213 TRACE("freeing prev teb %p stack %p fs %04x\n",
214 free_teb, free_teb->DeallocationStack, free_teb->teb_sel );
216 pthread_join( (pthread_t)free_teb->pthread_data, &ptr );
217 wine_ldt_free_fs( free_teb->teb_sel );
218 ptr = free_teb->DeallocationStack;
219 NtFreeVirtualMemory( GetCurrentProcess(), &ptr, &size, MEM_RELEASE );
221 SYSDEPS_AbortThread( status );
223 struct thread_cleanup_info info;
224 MEMORY_BASIC_INFORMATION meminfo;
226 NtQueryVirtualMemory( GetCurrentProcess(), teb->Tib.StackBase, MemoryBasicInformation,
227 &meminfo, sizeof(meminfo), NULL );
228 info.stack_base = meminfo.AllocationBase;
229 info.stack_size = meminfo.RegionSize + ((char *)teb->Tib.StackBase - (char *)meminfo.AllocationBase);
230 info.status = status;
234 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE | MEM_SYSTEM );
235 close( teb->wait_fd[0] );
236 close( teb->wait_fd[1] );
237 close( teb->reply_fd );
238 close( teb->request_fd );
240 wine_switch_to_stack( cleanup_thread, &info, get_temp_stack() );
245 /***********************************************************************
246 * SYSDEPS_AbortThread
248 * Same as SYSDEPS_ExitThread, but must not do anything that requires a server call.
250 void SYSDEPS_AbortThread( int status )
253 close( NtCurrentTeb()->wait_fd[0] );
254 close( NtCurrentTeb()->wait_fd[1] );
255 close( NtCurrentTeb()->reply_fd );
256 close( NtCurrentTeb()->request_fd );
258 pthread_exit( (void *)status );
261 #ifdef HAVE__LWP_CREATE
264 for (;;) /* avoid warning */
268 /***********************************************************************
271 * Get the Unix tid of the current thread.
273 int SYSDEPS_GetUnixTid(void)
275 #ifdef HAVE__LWP_SELF
277 #elif defined(__linux__) && defined(__i386__)
279 __asm__("int $0x80" : "=a" (ret) : "0" (224) /* SYS_gettid */);
280 if (ret < 0) ret = -1;
287 /**********************************************************************
288 * NtCurrentTeb (NTDLL.@)
290 * This will crash and burn if called before threading is initialized
292 #if defined(__i386__) && defined(__GNUC__)
293 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
294 #elif defined(__i386__) && defined(_MSC_VER)
295 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
296 #elif defined(HAVE__LWP_CREATE)
297 /***********************************************************************
298 * NtCurrentTeb (NTDLL.@)
300 struct _TEB * WINAPI NtCurrentTeb(void)
302 extern void *_lwp_getprivate(void);
303 return (struct _TEB *)_lwp_getprivate();
305 #elif defined(__powerpc__)
307 __ASM_GLOBAL_FUNC( NtCurrentTeb, "\n\tmr r3,r13\n\tblr" );
309 __ASM_GLOBAL_FUNC( NtCurrentTeb, "\n\tmr 3,2\n\tblr" );
312 # error NtCurrentTeb not defined for this architecture
313 #endif /* __i386__ */