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"
32 #include "selectors.h"
36 #include "wine/exception.h"
37 #include "debugtools.h"
39 DEFAULT_DEBUG_CHANNEL(thread
);
41 /* Xlib critical section (FIXME: does not belong here) */
42 CRITICAL_SECTION X11DRV_CritSection
= CRITICAL_SECTION_INIT
;
49 # define CLONE_VM 0x00000100
50 # define CLONE_FS 0x00000200
51 # define CLONE_FILES 0x00000400
52 # define CLONE_SIGHAND 0x00000800
53 # define CLONE_PID 0x00001000
54 # endif /* CLONE_VM */
59 #ifndef NO_REENTRANT_LIBC
61 /***********************************************************************
62 * __errno_location/__error/___errno
64 * Get the per-thread errno location.
66 #ifdef HAVE__ERRNO_LOCATION
67 int *__errno_location()
75 #ifdef HAVE__THR_ERRNO
79 if (!init_done
) return perrno
;
80 #ifdef NO_REENTRANT_X11
81 /* Use static libc errno while running in Xlib. */
82 if (X11DRV_CritSection
.OwningThread
== GetCurrentThreadId())
85 return &NtCurrentTeb()->thread_errno
;
88 /***********************************************************************
91 * Get the per-thread h_errno location.
93 int *__h_errno_location()
95 if (!init_done
) return ph_errno
;
96 #ifdef NO_REENTRANT_X11
97 /* Use static libc h_errno while running in Xlib. */
98 if (X11DRV_CritSection
.OwningThread
== GetCurrentThreadId())
101 return &NtCurrentTeb()->thread_h_errno
;
104 #endif /* NO_REENTRANT_LIBC */
106 /***********************************************************************
107 * SYSDEPS_SetCurThread
109 * Make 'thread' the current thread.
111 void SYSDEPS_SetCurThread( TEB
*teb
)
113 #if defined(__i386__)
114 /* On the i386, the current thread is in the %fs register */
115 __set_fs( teb
->teb_sel
);
116 #elif defined(HAVE__LWP_CREATE)
117 /* On non-i386 Solaris, we use the LWP private pointer */
118 _lwp_setprivate( teb
);
121 init_done
= 1; /* now we can use threading routines */
124 /***********************************************************************
125 * SYSDEPS_StartThread
127 * Startup routine for a new thread.
129 static void SYSDEPS_StartThread( TEB
*teb
)
131 SYSDEPS_SetCurThread( teb
);
138 __EXCEPT(UnhandledExceptionFilter
)
140 TerminateThread( GetCurrentThread(), GetExceptionCode() );
143 SYSDEPS_ExitThread(0); /* should never get here */
147 /***********************************************************************
148 * SYSDEPS_SpawnThread
150 * Start running a new thread.
151 * Return -1 on error, 0 if OK.
153 int SYSDEPS_SpawnThread( TEB
*teb
)
155 #ifndef NO_REENTRANT_LIBC
158 const int flags
= CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
;
159 if (clone( (int (*)(void *))SYSDEPS_StartThread
, teb
->stack_top
, flags
, teb
) < 0)
161 if (!(flags
& CLONE_FILES
)) close( teb
->socket
); /* close the child socket in the parent */
166 const int flags
= RFPROC
| RFMEM
; /*|RFFDG*/
167 void **sp
= (void **)teb
->stack_top
;
170 *--sp
= SYSDEPS_StartThread
;
171 __asm__
__volatile__(
172 "pushl %2;\n\t" /* flags */
173 "pushl $0;\n\t" /* 0 ? */
174 "movl %1,%%eax;\n\t" /* SYS_rfork */
175 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
176 "cmpl $0, %%edx;\n\t"
178 "movl %0,%%esp;\n\t" /* child -> new thread */
180 "1:\n\t" /* parent -> caller thread */
182 : "r" (sp
), "g" (SYS_rfork
), "g" (flags
)
184 if (flags
& RFFDG
) close( teb
->socket
); /* close the child socket in the parent */
188 #ifdef HAVE__LWP_CREATE
190 _lwp_makecontext( &context
, (void(*)(void *))SYSDEPS_StartThread
, teb
,
191 NULL
, teb
->stack_base
, (char *)teb
->stack_top
- (char *)teb
->stack_base
);
192 if ( _lwp_create( &context
, 0, NULL
) )
197 #endif /* NO_REENTRANT_LIBC */
199 FIXME("CreateThread: stub\n" );
205 /***********************************************************************
208 * Exit a running thread; must not return.
210 void SYSDEPS_ExitThread( int status
)
212 int socket
= NtCurrentTeb()->socket
;
213 NtCurrentTeb()->socket
= -1;
215 #ifdef HAVE__LWP_CREATE
220 * It is of course impossible to come here,
221 * but it eliminates a compiler warning.
227 /***********************************************************************
228 * SYSDEPS_CallOnStack
230 int SYSDEPS_DoCallOnStack( int (*func
)(LPVOID
), LPVOID arg
)
238 __EXCEPT(UnhandledExceptionFilter
)
240 TerminateThread( GetCurrentThread(), GetExceptionCode() );
249 int SYSDEPS_CallOnStack( LPVOID stackTop
, LPVOID stackLow
,
250 int (*func
)(LPVOID
), LPVOID arg
);
251 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack
,
253 "movl %esp, %ebp\n\t"
254 ".byte 0x64; pushl 0x04\n\t"
255 ".byte 0x64; pushl 0x08\n\t"
256 "movl 8(%ebp), %esp\n\t"
257 "movl 12(%ebp), %eax\n\t"
258 ".byte 0x64; movl %esp, 0x04\n\t"
259 ".byte 0x64; movl %eax, 0x08\n\t"
262 "call " __ASM_NAME("SYSDEPS_DoCallOnStack") "\n\t"
263 "leal -8(%ebp), %esp\n\t"
264 ".byte 0x64; popl 0x08\n\t"
265 ".byte 0x64; popl 0x04\n\t"
269 int SYSDEPS_CallOnStack( LPVOID stackTop
, LPVOID stackLow
,
270 int (*func
)(LPVOID
), LPVOID arg
)
272 return SYSDEPS_DoCallOnStack( func
, arg
);
276 /***********************************************************************
277 * SYSDEPS_SwitchToThreadStack
280 static LPVOID SYSDEPS_LargeStackTop
= NULL
;
281 static LPVOID SYSDEPS_LargeStackLow
= NULL
;
283 void SYSDEPS_SwitchToThreadStack( void (*func
)(void) )
285 DWORD page_size
= VIRTUAL_GetPageSize();
286 DWORD cur_stack
= (((DWORD
)&func
) + (page_size
-1)) & ~(page_size
-1);
288 TEB
*teb
= NtCurrentTeb();
289 LPVOID stackTop
= teb
->stack_top
;
290 LPVOID stackLow
= teb
->stack_low
;
294 if ( getrlimit(RLIMIT_STACK
, &rl
) < 0 )
296 WARN("Can't get rlimit\n");
297 rl
.rlim_cur
= 8*1024*1024;
300 teb
->stack_top
= (LPVOID
) cur_stack
;
301 teb
->stack_low
= (LPVOID
)(cur_stack
- rl
.rlim_cur
);
303 SYSDEPS_LargeStackTop
= (LPVOID
)(cur_stack
- 2*page_size
);
304 SYSDEPS_LargeStackLow
= (LPVOID
)(cur_stack
- rl
.rlim_cur
);
306 SYSDEPS_CallOnStack( stackTop
, stackLow
,
307 (int (*)(void *))func
, NULL
);
310 /***********************************************************************
311 * SYSDEPS_CallOnLargeStack
313 int SYSDEPS_CallOnLargeStack( int (*func
)(LPVOID
), LPVOID arg
)
315 static int recurse
= 0;
318 if ( recurse
++ == 0 && SYSDEPS_LargeStackTop
)
319 retv
= SYSDEPS_CallOnStack( SYSDEPS_LargeStackTop
,
320 SYSDEPS_LargeStackLow
, func
, arg
);
329 /**********************************************************************
330 * NtCurrentTeb (NTDLL.89)
332 * This will crash and burn if called before threading is initialized
335 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
336 #elif defined(HAVE__LWP_CREATE)
337 struct _TEB
* WINAPI
NtCurrentTeb(void)
339 extern void *_lwp_getprivate(void);
340 return (struct _TEB
*)_lwp_getprivate();
343 # error NtCurrentTeb not defined for this architecture
344 #endif /* __i386__ */