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 #ifdef HAVE_SYS_SYSCALL_H
20 # include <sys/syscall.h>
25 #ifdef HAVE_UCONTEXT_H
26 # include <ucontext.h>
31 #include "debugtools.h"
33 DEFAULT_DEBUG_CHANNEL(thread
)
36 #define HAVE_CLONE_SYSCALL
39 /* Xlib critical section (FIXME: does not belong here) */
40 CRITICAL_SECTION X11DRV_CritSection
= { 0, };
42 #ifdef HAVE_CLONE_SYSCALL
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 /* If we didn't get the flags, we probably didn't get the prototype either */
53 extern int clone( int (*fn
)(void *arg
), void *stack
, int flags
, void *arg
);
54 # endif /* CLONE_VM */
55 #endif /* HAVE_CLONE_SYSCALL */
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
);
134 SYSDEPS_ExitThread(); /* should never get here */
138 /***********************************************************************
139 * SYSDEPS_SpawnThread
141 * Start running a new thread.
142 * Return -1 on error, 0 if OK.
144 int SYSDEPS_SpawnThread( TEB
*teb
)
146 #ifndef NO_REENTRANT_LIBC
148 #ifdef HAVE_CLONE_SYSCALL
149 if (clone( (int (*)(void *))SYSDEPS_StartThread
, teb
->stack_top
,
150 CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
, teb
) < 0)
152 /* FIXME: close the child socket in the parent process */
153 /* close( thread->socket );*/
158 DWORD
*sp
= (DWORD
*)teb
->stack_top
;
161 *--sp
= (DWORD
)SYSDEPS_StartThread
;
163 "pushl %2;\n\t" /* RFPROC|RMEM */
164 "pushl $0;\n\t" /* 0 ? */
165 "movl %1,%%eax;\n\t" /* SYS_rfork */
166 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
167 "cmpl $0, %%edx;\n\t"
169 "movl %0,%%esp;\n\t" /* father -> new thread */
171 "1:\n\t" /* child -> caller thread */
173 : "r" (sp
), "g" (SYS_rfork
), "g" (RFPROC
|RFMEM
)
178 #ifdef HAVE__LWP_CREATE
180 _lwp_makecontext( &context
, (void(*)(void *))SYSDEPS_StartThread
, teb
,
181 NULL
, teb
->stack_base
, (char *)teb
->stack_top
- (char *)teb
->stack_base
);
182 if ( _lwp_create( &context
, 0, NULL
) )
187 #endif /* NO_REENTRANT_LIBC */
189 FIXME("CreateThread: stub\n" );
195 /***********************************************************************
198 * Exit a running thread; must not return.
199 * Must not make any reference to the thread structures (THDB etc.) as
200 * they have already been deleted.
202 void SYSDEPS_ExitThread(void)
204 #ifdef HAVE__LWP_CREATE
212 /**********************************************************************
213 * NtCurrentTeb (NTDLL.89)
215 * This will crash and burn if called before threading is initialized
218 /* if it was defined as a macro, we need to do some magic */
223 struct _TEB
* WINAPI
NtCurrentTeb(void)