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
;
19 #include <sys/resource.h>
20 #ifdef HAVE_SYS_SYSCALL_H
21 # include <sys/syscall.h>
26 #ifdef HAVE_UCONTEXT_H
27 # include <ucontext.h>
29 #include "wine/port.h"
31 #include "selectors.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 static int SYSDEPS_DoCallOnStack( int (*func
)(LPVOID
), LPVOID arg
) WINE_UNUSED
;
229 static int SYSDEPS_DoCallOnStack( int (*func
)(LPVOID
), LPVOID arg
)
237 __EXCEPT(UnhandledExceptionFilter
)
239 TerminateThread( GetCurrentThread(), GetExceptionCode() );
248 int SYSDEPS_CallOnStack( LPVOID stackTop
, LPVOID stackLow
,
249 int (*func
)(LPVOID
), LPVOID arg
);
250 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack
,
252 "movl %esp, %ebp\n\t"
253 ".byte 0x64; pushl 0x04\n\t"
254 ".byte 0x64; pushl 0x08\n\t"
255 "movl 8(%ebp), %esp\n\t"
256 "movl 12(%ebp), %eax\n\t"
257 ".byte 0x64; movl %esp, 0x04\n\t"
258 ".byte 0x64; movl %eax, 0x08\n\t"
261 "call " __ASM_NAME("SYSDEPS_DoCallOnStack") "\n\t"
262 "leal -8(%ebp), %esp\n\t"
263 ".byte 0x64; popl 0x08\n\t"
264 ".byte 0x64; popl 0x04\n\t"
268 int SYSDEPS_CallOnStack( LPVOID stackTop
, LPVOID stackLow
,
269 int (*func
)(LPVOID
), LPVOID arg
)
271 return SYSDEPS_DoCallOnStack( func
, arg
);
275 /***********************************************************************
276 * SYSDEPS_SwitchToThreadStack
279 static LPVOID SYSDEPS_LargeStackTop
= NULL
;
280 static LPVOID SYSDEPS_LargeStackLow
= NULL
;
282 void SYSDEPS_SwitchToThreadStack( void (*func
)(void) )
284 TEB
*teb
= NtCurrentTeb();
285 LPVOID stackTop
= teb
->stack_top
;
286 LPVOID stackLow
= teb
->stack_low
;
290 if ( getrlimit(RLIMIT_STACK
, &rl
) < 0 )
292 WARN("Can't get rlimit\n");
293 rl
.rlim_cur
= 8*1024*1024;
296 SYSDEPS_LargeStackTop
= teb
->stack_top
= &func
- 128;
297 SYSDEPS_LargeStackLow
= teb
->stack_low
= teb
->stack_top
- rl
.rlim_cur
;
299 SYSDEPS_CallOnStack( stackTop
, stackLow
,
300 (int (*)(void *))func
, NULL
);
303 /***********************************************************************
304 * SYSDEPS_CallOnLargeStack
306 int SYSDEPS_CallOnLargeStack( int (*func
)(LPVOID
), LPVOID arg
)
308 static int recurse
= 0;
311 if ( recurse
++ == 0 && SYSDEPS_LargeStackTop
)
312 retv
= SYSDEPS_CallOnStack( SYSDEPS_LargeStackTop
,
313 SYSDEPS_LargeStackLow
, func
, arg
);
322 /**********************************************************************
323 * NtCurrentTeb (NTDLL.89)
325 * This will crash and burn if called before threading is initialized
328 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
329 #elif defined(HAVE__LWP_CREATE)
330 struct _TEB
* WINAPI
NtCurrentTeb(void)
332 extern void *_lwp_getprivate(void);
333 return (struct _TEB
*)_lwp_getprivate();
336 # error NtCurrentTeb not defined for this architecture
337 #endif /* __i386__ */