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
50 #include "wine/pthread.h"
51 #include "wine/server.h"
53 #include "wine/library.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
58 struct thread_cleanup_info
65 /* temporary stacks used on thread exit */
66 #define TEMP_STACK_SIZE 1024
67 #define NB_TEMP_STACKS 8
68 static char temp_stacks
[NB_TEMP_STACKS
][TEMP_STACK_SIZE
];
69 static LONG next_temp_stack
; /* next temp stack to use */
72 /***********************************************************************
73 * SYSDEPS_SetCurThread
75 * Make 'thread' the current thread.
77 void SYSDEPS_SetCurThread( TEB
*teb
)
80 /* On the i386, the current thread is in the %fs register */
83 wine_ldt_set_base( &fs_entry
, teb
);
84 wine_ldt_set_limit( &fs_entry
, 0xfff );
85 wine_ldt_set_flags( &fs_entry
, WINE_LDT_FLAGS_DATA
|WINE_LDT_FLAGS_32BIT
);
86 wine_ldt_init_fs( teb
->teb_sel
, &fs_entry
);
87 #elif defined(__powerpc__)
88 /* On PowerPC, the current TEB is in the gpr13 register */
90 __asm__
__volatile__("mr r13, %0" : : "r" (teb
));
92 __asm__
__volatile__("mr 2, %0" : : "r" (teb
));
94 #elif defined(HAVE__LWP_CREATE)
95 /* On non-i386 Solaris, we use the LWP private pointer */
96 _lwp_setprivate( teb
);
100 teb
->pthread_data
= (void *)pthread_self();
102 wine_pthread_init_thread();
107 /***********************************************************************
110 * Get a temporary stack address to run the thread exit code on.
112 inline static char *get_temp_stack(void)
114 unsigned int next
= interlocked_xchg_add( &next_temp_stack
, 1 );
115 return temp_stacks
[next
% NB_TEMP_STACKS
] + TEMP_STACK_SIZE
;
119 /***********************************************************************
122 * Cleanup the remains of a thread. Runs on a temporary stack.
124 static void cleanup_thread( void *ptr
)
126 /* copy the info structure since it is on the stack we will free */
127 struct thread_cleanup_info info
= *(struct thread_cleanup_info
*)ptr
;
128 munmap( info
.stack_base
, info
.stack_size
);
129 wine_ldt_free_fs( wine_get_fs() );
130 #ifdef HAVE__LWP_CREATE
133 _exit( info
.status
);
137 /***********************************************************************
138 * SYSDEPS_SpawnThread
140 * Start running a new thread.
141 * Return -1 on error, 0 if OK.
143 int SYSDEPS_SpawnThread( void (*func
)(TEB
*), TEB
*teb
)
149 pthread_attr_init( &attr
);
150 pthread_attr_setstack( &attr
, teb
->DeallocationStack
,
151 (char *)teb
->Tib
.StackBase
- (char *)teb
->DeallocationStack
);
152 if (pthread_create( &id
, &attr
, (void * (*)(void *))func
, teb
)) return -1;
154 #elif defined(HAVE_CLONE)
155 if (clone( (int (*)(void *))func
, teb
->Tib
.StackBase
,
156 CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
, teb
) < 0)
159 #elif defined(HAVE_RFORK)
160 void **sp
= (void **)teb
->Tib
.StackBase
;
164 __asm__
__volatile__(
165 "pushl %2;\n\t" /* flags */
166 "pushl $0;\n\t" /* 0 ? */
167 "movl %1,%%eax;\n\t" /* SYS_rfork */
168 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
169 "cmpl $0, %%edx;\n\t"
171 "movl %0,%%esp;\n\t" /* child -> new thread */
173 "1:\n\t" /* parent -> caller thread */
175 : "r" (sp
), "g" (SYS_rfork
), "g" (RFPROC
| RFMEM
)
178 #elif defined(HAVE__LWP_CREATE)
180 _lwp_makecontext( &context
, (void(*)(void *))func
, teb
,
181 NULL
, teb
->DeallocationStack
, (char *)teb
->Tib
.StackBase
- (char *)teb
->DeallocationStack
);
182 if ( _lwp_create( &context
, 0, NULL
) )
187 FIXME("CreateThread: stub\n" );
192 /***********************************************************************
195 * Exit a running thread; must not return.
197 void SYSDEPS_ExitThread( int status
)
199 TEB
*teb
= NtCurrentTeb();
203 static TEB
*teb_to_free
;
206 if ((free_teb
= interlocked_xchg_ptr( (void **)&teb_to_free
, teb
)) != NULL
)
210 TRACE("freeing prev teb %p stack %p fs %04x\n",
211 free_teb
, free_teb
->DeallocationStack
, free_teb
->teb_sel
);
213 pthread_join( (pthread_t
)free_teb
->pthread_data
, &ptr
);
214 wine_ldt_free_fs( free_teb
->teb_sel
);
215 ptr
= free_teb
->DeallocationStack
;
216 NtFreeVirtualMemory( GetCurrentProcess(), &ptr
, &size
, MEM_RELEASE
);
218 SYSDEPS_AbortThread( status
);
220 struct thread_cleanup_info info
;
221 MEMORY_BASIC_INFORMATION meminfo
;
223 NtQueryVirtualMemory( GetCurrentProcess(), teb
->Tib
.StackBase
, MemoryBasicInformation
,
224 &meminfo
, sizeof(meminfo
), NULL
);
225 info
.stack_base
= meminfo
.AllocationBase
;
226 info
.stack_size
= meminfo
.RegionSize
+ ((char *)teb
->Tib
.StackBase
- (char *)meminfo
.AllocationBase
);
227 info
.status
= status
;
231 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
| MEM_SYSTEM
);
232 close( teb
->wait_fd
[0] );
233 close( teb
->wait_fd
[1] );
234 close( teb
->reply_fd
);
235 close( teb
->request_fd
);
237 wine_switch_to_stack( cleanup_thread
, &info
, get_temp_stack() );
242 /***********************************************************************
243 * SYSDEPS_AbortThread
245 * Same as SYSDEPS_ExitThread, but must not do anything that requires a server call.
247 void SYSDEPS_AbortThread( int status
)
250 close( NtCurrentTeb()->wait_fd
[0] );
251 close( NtCurrentTeb()->wait_fd
[1] );
252 close( NtCurrentTeb()->reply_fd
);
253 close( NtCurrentTeb()->request_fd
);
255 pthread_exit( (void *)status
);
258 #ifdef HAVE__LWP_CREATE
261 for (;;) /* avoid warning */
265 /***********************************************************************
268 * Get the Unix tid of the current thread.
270 int SYSDEPS_GetUnixTid(void)
272 #ifdef HAVE__LWP_SELF
274 #elif defined(__linux__) && defined(__i386__)
276 __asm__("int $0x80" : "=a" (ret
) : "0" (224) /* SYS_gettid */);
277 if (ret
< 0) ret
= -1;
284 /**********************************************************************
285 * NtCurrentTeb (NTDLL.@)
287 * This will crash and burn if called before threading is initialized
289 #if defined(__i386__) && defined(__GNUC__)
290 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
291 #elif defined(__i386__) && defined(_MSC_VER)
292 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
293 #elif defined(HAVE__LWP_CREATE)
294 /***********************************************************************
295 * NtCurrentTeb (NTDLL.@)
297 struct _TEB
* WINAPI
NtCurrentTeb(void)
299 extern void *_lwp_getprivate(void);
300 return (struct _TEB
*)_lwp_getprivate();
302 #elif defined(__powerpc__)
304 __ASM_GLOBAL_FUNC( NtCurrentTeb
, "\n\tmr r3,r13\n\tblr" );
306 __ASM_GLOBAL_FUNC( NtCurrentTeb
, "\n\tmr 3,2\n\tblr" );
309 # error NtCurrentTeb not defined for this architecture
310 #endif /* __i386__ */