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
45 #include "wine/server.h"
47 #include "wine/winbase16.h"
48 #include "wine/exception.h"
49 #include "wine/library.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
54 #if defined(linux) || defined(HAVE_CLONE)
59 # define CLONE_VM 0x00000100
60 # define CLONE_FS 0x00000200
61 # define CLONE_FILES 0x00000400
62 # define CLONE_SIGHAND 0x00000800
63 # define CLONE_PID 0x00001000
64 # endif /* CLONE_VM */
65 #endif /* linux || HAVE_CLONE */
67 extern void SELECTOR_FreeFs(void);
69 struct thread_cleanup_info
76 /* temporary stacks used on thread exit */
77 #define TEMP_STACK_SIZE 1024
78 #define NB_TEMP_STACKS 8
79 static char temp_stacks
[NB_TEMP_STACKS
][TEMP_STACK_SIZE
];
80 static LONG next_temp_stack
; /* next temp stack to use */
82 /***********************************************************************
83 * SYSDEPS_SetCurThread
85 * Make 'thread' the current thread.
87 void SYSDEPS_SetCurThread( TEB
*teb
)
90 /* On the i386, the current thread is in the %fs register */
91 wine_set_fs( teb
->teb_sel
);
92 #elif defined(__powerpc__)
93 /* On PowerPC, the current TEB is in the gpr13 register */
94 __asm__
__volatile__("mr 2, %0" : : "r" (teb
));
95 #elif defined(HAVE__LWP_CREATE)
96 /* On non-i386 Solaris, we use the LWP private pointer */
97 _lwp_setprivate( teb
);
102 /***********************************************************************
103 * call_on_thread_stack
105 * Call a function once we switched to the thread stack.
107 static void call_on_thread_stack( void *func
)
111 void (*funcptr
)(void) = func
;
114 __EXCEPT(UnhandledExceptionFilter
)
116 TerminateThread( GetCurrentThread(), GetExceptionCode() );
119 SYSDEPS_ExitThread(0); /* should never get here */
123 /***********************************************************************
126 * Get a temporary stack address to run the thread exit code on.
128 inline static char *get_temp_stack(void)
130 unsigned int next
= InterlockedExchangeAdd( &next_temp_stack
, 1 );
131 return temp_stacks
[next
% NB_TEMP_STACKS
];
135 /***********************************************************************
138 * Cleanup the remains of a thread. Runs on a temporary stack.
140 static void cleanup_thread( void *ptr
)
142 /* copy the info structure since it is on the stack we will free */
143 struct thread_cleanup_info info
= *(struct thread_cleanup_info
*)ptr
;
144 munmap( info
.stack_base
, info
.stack_size
);
146 #ifdef HAVE__LWP_CREATE
149 _exit( info
.status
);
153 /***********************************************************************
154 * SYSDEPS_StartThread
156 * Startup routine for a new thread.
158 static void SYSDEPS_StartThread( TEB
*teb
)
160 SYSDEPS_SetCurThread( teb
);
167 __EXCEPT(UnhandledExceptionFilter
)
169 TerminateThread( GetCurrentThread(), GetExceptionCode() );
172 SYSDEPS_ExitThread(0); /* should never get here */
176 /***********************************************************************
177 * SYSDEPS_SpawnThread
179 * Start running a new thread.
180 * Return -1 on error, 0 if OK.
182 int SYSDEPS_SpawnThread( TEB
*teb
)
184 #ifdef ERRNO_LOCATION
186 #if defined(linux) || defined(HAVE_CLONE)
187 const int flags
= CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
;
188 if (clone( (int (*)(void *))SYSDEPS_StartThread
, teb
->stack_top
, flags
, teb
) < 0)
190 if (!(flags
& CLONE_FILES
)) close( teb
->request_fd
); /* close the child socket in the parent */
195 const int flags
= RFPROC
| RFMEM
; /*|RFFDG*/
196 void **sp
= (void **)teb
->stack_top
;
199 *--sp
= SYSDEPS_StartThread
;
200 __asm__
__volatile__(
201 "pushl %2;\n\t" /* flags */
202 "pushl $0;\n\t" /* 0 ? */
203 "movl %1,%%eax;\n\t" /* SYS_rfork */
204 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
205 "cmpl $0, %%edx;\n\t"
207 "movl %0,%%esp;\n\t" /* child -> new thread */
209 "1:\n\t" /* parent -> caller thread */
211 : "r" (sp
), "g" (SYS_rfork
), "g" (flags
)
213 if (flags
& RFFDG
) close( teb
->request_fd
); /* close the child socket in the parent */
217 #ifdef HAVE__LWP_CREATE
219 _lwp_makecontext( &context
, (void(*)(void *))SYSDEPS_StartThread
, teb
,
220 NULL
, teb
->stack_base
, (char *)teb
->stack_top
- (char *)teb
->stack_base
);
221 if ( _lwp_create( &context
, 0, NULL
) )
226 #endif /* ERRNO_LOCATION */
228 FIXME("CreateThread: stub\n" );
233 /***********************************************************************
234 * SYSDEPS_CallOnStack
236 void SYSDEPS_CallOnStack( void (*func
)(LPVOID
), LPVOID arg
) WINE_NORETURN
;
239 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack
,
240 "movl 4(%esp),%ecx\n\t" /* func */
241 "movl 8(%esp),%edx\n\t" /* arg */
242 ".byte 0x64\n\tmovl 0x04,%esp\n\t" /* teb->stack_top */
246 "int $3" /* we never return here */ );
247 #elif defined(_MSC_VER)
248 __declspec(naked
) void SYSDEPS_CallOnStack( void (*func
)(LPVOID
), LPVOID arg
)
250 __asm mov ecx
, 4[esp
];
251 __asm mov edx
, 8[esp
];
252 __asm mov fs
:[0x04], esp
;
258 #endif /* defined(__GNUC__) || defined(_MSC_VER) */
259 #else /* !defined(__i386__) */
260 void SYSDEPS_CallOnStack( void (*func
)(LPVOID
), LPVOID arg
)
263 while(1); /* avoid warning */
265 #endif /* !defined(__i386__) */
268 /***********************************************************************
269 * SYSDEPS_SwitchToThreadStack
271 void SYSDEPS_SwitchToThreadStack( void (*func
)(void) )
273 SYSDEPS_CallOnStack( call_on_thread_stack
, func
);
277 /***********************************************************************
280 * Exit a running thread; must not return.
282 void SYSDEPS_ExitThread( int status
)
284 TEB
*teb
= NtCurrentTeb();
285 struct thread_cleanup_info info
;
286 MEMORY_BASIC_INFORMATION meminfo
;
288 FreeSelector16( teb
->stack_sel
);
289 VirtualQuery( teb
->stack_top
, &meminfo
, sizeof(meminfo
) );
290 info
.stack_base
= meminfo
.AllocationBase
;
291 info
.stack_size
= meminfo
.RegionSize
+ ((char *)teb
->stack_top
- (char *)meminfo
.AllocationBase
);
292 info
.status
= status
;
296 VirtualFree( teb
->stack_base
, 0, MEM_RELEASE
| MEM_SYSTEM
);
297 close( teb
->wait_fd
[0] );
298 close( teb
->wait_fd
[1] );
299 close( teb
->reply_fd
);
300 close( teb
->request_fd
);
301 teb
->stack_low
= get_temp_stack();
302 teb
->stack_top
= (char *) teb
->stack_low
+ TEMP_STACK_SIZE
;
303 SYSDEPS_CallOnStack( cleanup_thread
, &info
);
307 /***********************************************************************
308 * SYSDEPS_AbortThread
310 * Same as SYSDEPS_ExitThread, but must not do anything that requires a server call.
312 void SYSDEPS_AbortThread( int status
)
315 close( NtCurrentTeb()->wait_fd
[0] );
316 close( NtCurrentTeb()->wait_fd
[1] );
317 close( NtCurrentTeb()->reply_fd
);
318 close( NtCurrentTeb()->request_fd
);
319 #ifdef HAVE__LWP_CREATE
322 for (;;) /* avoid warning */
327 /**********************************************************************
328 * NtCurrentTeb (NTDLL.@)
330 * This will crash and burn if called before threading is initialized
332 #if defined(__i386__) && defined(__GNUC__)
333 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
334 #elif defined(__i386__) && defined(_MSC_VER)
335 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
336 #elif defined(HAVE__LWP_CREATE)
337 /***********************************************************************
338 * NtCurrentTeb (NTDLL.@)
340 struct _TEB
* WINAPI
NtCurrentTeb(void)
342 extern void *_lwp_getprivate(void);
343 return (struct _TEB
*)_lwp_getprivate();
345 #elif defined(__powerpc__)
346 __ASM_GLOBAL_FUNC( NtCurrentTeb
, "\n\tmr 3,2\n\tblr" );
348 # error NtCurrentTeb not defined for this architecture
349 #endif /* __i386__ */