2 * System-dependent scheduler support
4 * Copyright 1998 Alexandre Julliard
9 /* Get pointers to the static errno and h_errno variables used by Xlib. This
10 must be done before including <errno.h> makes the variables invisible. */
12 static int *perrno = &errno;
14 static int *ph_errno = &h_errno;
20 #include <sys/resource.h>
21 #ifdef HAVE_SYS_SYSCALL_H
22 # include <sys/syscall.h>
27 #ifdef HAVE_UCONTEXT_H
28 # include <ucontext.h>
30 #include "wine/port.h"
34 #include "wine/exception.h"
35 #include "debugtools.h"
37 DEFAULT_DEBUG_CHANNEL(thread);
39 /* Xlib critical section (FIXME: does not belong here) */
40 CRITICAL_SECTION X11DRV_CritSection = CRITICAL_SECTION_INIT;
47 # define CLONE_VM 0x00000100
48 # define CLONE_FS 0x00000200
49 # define CLONE_FILES 0x00000400
50 # define CLONE_SIGHAND 0x00000800
51 # define CLONE_PID 0x00001000
52 # endif /* CLONE_VM */
57 #ifndef NO_REENTRANT_LIBC
59 /***********************************************************************
60 * __errno_location/__error/___errno
62 * Get the per-thread errno location.
64 #ifdef HAVE__ERRNO_LOCATION
65 int *__errno_location()
73 #ifdef HAVE__THR_ERRNO
77 if (!init_done) return perrno;
78 #ifdef NO_REENTRANT_X11
79 /* Use static libc errno while running in Xlib. */
80 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
83 return &NtCurrentTeb()->thread_errno;
86 /***********************************************************************
89 * Get the per-thread h_errno location.
91 int *__h_errno_location()
93 if (!init_done) return ph_errno;
94 #ifdef NO_REENTRANT_X11
95 /* Use static libc h_errno while running in Xlib. */
96 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
99 return &NtCurrentTeb()->thread_h_errno;
102 #endif /* NO_REENTRANT_LIBC */
104 /***********************************************************************
105 * SYSDEPS_SetCurThread
107 * Make 'thread' the current thread.
109 void SYSDEPS_SetCurThread( TEB *teb )
111 #if defined(__i386__)
112 /* On the i386, the current thread is in the %fs register */
113 __set_fs( teb->teb_sel );
114 #elif defined(HAVE__LWP_CREATE)
115 /* On non-i386 Solaris, we use the LWP private pointer */
116 _lwp_setprivate( teb );
119 init_done = 1; /* now we can use threading routines */
122 /***********************************************************************
123 * SYSDEPS_StartThread
125 * Startup routine for a new thread.
127 static void SYSDEPS_StartThread( TEB *teb )
129 SYSDEPS_SetCurThread( teb );
136 __EXCEPT(UnhandledExceptionFilter)
138 TerminateThread( GetCurrentThread(), GetExceptionCode() );
141 SYSDEPS_ExitThread(0); /* should never get here */
145 /***********************************************************************
146 * SYSDEPS_SpawnThread
148 * Start running a new thread.
149 * Return -1 on error, 0 if OK.
151 int SYSDEPS_SpawnThread( TEB *teb )
153 #ifndef NO_REENTRANT_LIBC
156 const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
157 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
159 if (!(flags & CLONE_FILES)) close( teb->socket ); /* close the child socket in the parent */
164 const int flags = RFPROC | RFMEM; /*|RFFDG*/
165 void **sp = (void **)teb->stack_top;
168 *--sp = SYSDEPS_StartThread;
169 __asm__ __volatile__(
170 "pushl %2;\n\t" /* flags */
171 "pushl $0;\n\t" /* 0 ? */
172 "movl %1,%%eax;\n\t" /* SYS_rfork */
173 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
174 "cmpl $0, %%edx;\n\t"
176 "movl %0,%%esp;\n\t" /* child -> new thread */
178 "1:\n\t" /* parent -> caller thread */
180 : "r" (sp), "g" (SYS_rfork), "g" (flags)
182 if (flags & RFFDG) close( teb->socket ); /* close the child socket in the parent */
186 #ifdef HAVE__LWP_CREATE
188 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
189 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
190 if ( _lwp_create( &context, 0, NULL ) )
195 #endif /* NO_REENTRANT_LIBC */
197 FIXME("CreateThread: stub\n" );
203 /***********************************************************************
206 * Exit a running thread; must not return.
208 void SYSDEPS_ExitThread( int status )
210 int socket = NtCurrentTeb()->socket;
211 NtCurrentTeb()->socket = -1;
213 #ifdef HAVE__LWP_CREATE
218 * It is of course impossible to come here,
219 * but it eliminates a compiler warning.
225 /***********************************************************************
226 * SYSDEPS_CallOnStack
228 int SYSDEPS_DoCallOnStack( int (*func)(LPVOID), LPVOID arg )
236 __EXCEPT(UnhandledExceptionFilter)
238 TerminateThread( GetCurrentThread(), GetExceptionCode() );
247 int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
248 int (*func)(LPVOID), LPVOID arg );
249 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
251 "movl %esp, %ebp\n\t"
252 ".byte 0x64; pushl 0x04\n\t"
253 ".byte 0x64; pushl 0x08\n\t"
254 "movl 8(%ebp), %esp\n\t"
255 "movl 12(%ebp), %eax\n\t"
256 ".byte 0x64; movl %esp, 0x04\n\t"
257 ".byte 0x64; movl %eax, 0x08\n\t"
260 "call " __ASM_NAME("SYSDEPS_DoCallOnStack") "\n\t"
261 "leal -8(%ebp), %esp\n\t"
262 ".byte 0x64; popl 0x08\n\t"
263 ".byte 0x64; popl 0x04\n\t"
267 int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
268 int (*func)(LPVOID), LPVOID arg )
270 return SYSDEPS_DoCallOnStack( func, arg );
274 /***********************************************************************
275 * SYSDEPS_SwitchToThreadStack
277 void SYSDEPS_SwitchToThreadStack( void (*func)(void) )
279 DWORD page_size = getpagesize();
280 DWORD cur_stack = (((DWORD)&func) + (page_size-1)) & ~(page_size-1);
282 TEB *teb = NtCurrentTeb();
283 LPVOID stackTop = teb->stack_top;
284 LPVOID stackLow = teb->stack_low;
288 if ( getrlimit(RLIMIT_STACK, &rl) < 0 )
290 WARN("Can't get rlimit\n");
291 rl.rlim_cur = 8*1024*1024;
294 teb->stack_top = (LPVOID) cur_stack;
295 teb->stack_low = (LPVOID)(cur_stack - rl.rlim_cur);
297 SYSDEPS_CallOnStack( stackTop, stackLow,
298 (int (*)(void *))func, NULL );
301 /**********************************************************************
302 * NtCurrentTeb (NTDLL.89)
304 * This will crash and burn if called before threading is initialized
307 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
308 #elif defined(HAVE__LWP_CREATE)
309 struct _TEB * WINAPI NtCurrentTeb(void)
311 extern void *_lwp_getprivate(void);
312 return (struct _TEB *)_lwp_getprivate();
315 # error NtCurrentTeb not defined for this architecture
316 #endif /* __i386__ */