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 */
60 static THDB
*pCurrentThread
;
64 #ifndef NO_REENTRANT_LIBC
66 /***********************************************************************
67 * __errno_location/__error/___errno
69 * Get the per-thread errno location.
71 #ifdef HAVE__ERRNO_LOCATION
72 int *__errno_location()
82 if (!init_done
) return perrno
;
83 thdb
= THREAD_Current();
84 #ifdef NO_REENTRANT_X11
85 /* Use static libc errno while running in Xlib. */
86 if (X11DRV_CritSection
.OwningThread
== (HANDLE
)thdb
->teb
.tid
)
89 return &thdb
->thread_errno
;
92 /***********************************************************************
95 * Get the per-thread h_errno location.
97 int *__h_errno_location()
100 if (!init_done
) return ph_errno
;
101 thdb
= THREAD_Current();
102 #ifdef NO_REENTRANT_X11
103 /* Use static libc h_errno while running in Xlib. */
104 if (X11DRV_CritSection
.OwningThread
== (HANDLE
)thdb
->teb
.tid
)
107 return &thdb
->thread_h_errno
;
110 #endif /* NO_REENTRANT_LIBC */
112 /***********************************************************************
113 * SYSDEPS_SetCurThread
115 * Make 'thread' the current thread.
117 void SYSDEPS_SetCurThread( THDB
*thread
)
120 /* On the i386, the current thread is in the %fs register */
121 SET_FS( thread
->teb_sel
);
123 /* FIXME: only works if there is no preemptive task-switching going on... */
124 pCurrentThread
= thread
;
125 #endif /* __i386__ */
126 init_done
= 1; /* now we can use threading routines */
129 /***********************************************************************
130 * SYSDEPS_StartThread
132 * Startup routine for a new thread.
134 static void SYSDEPS_StartThread( THDB
*thdb
)
136 SYSDEPS_SetCurThread( thdb
);
139 SYSDEPS_ExitThread(); /* should never get here */
143 /***********************************************************************
144 * SYSDEPS_SpawnThread
146 * Start running a new thread.
147 * Return -1 on error, 0 if OK.
149 int SYSDEPS_SpawnThread( THDB
*thread
)
151 #ifndef NO_REENTRANT_LIBC
153 #ifdef HAVE_CLONE_SYSCALL
154 if (clone( (int (*)(void *))SYSDEPS_StartThread
, thread
->teb
.stack_top
,
155 CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
, thread
) < 0)
157 /* FIXME: close the child socket in the parent process */
158 /* close( thread->socket );*/
163 DWORD
*sp
= (DWORD
*)thread
->teb
.stack_top
;
164 *--sp
= (DWORD
)thread
;
166 *--sp
= (DWORD
)SYSDEPS_StartThread
;
168 "pushl %2;\n\t" /* RFPROC|RMEM */
169 "pushl $0;\n\t" /* 0 ? */
170 "movl %1,%%eax;\n\t" /* SYS_rfork */
171 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
172 "cmpl $0, %%edx;\n\t"
174 "movl %0,%%esp;\n\t" /* father -> new thread */
176 "1:\n\t" /* child -> caller thread */
178 : "r" (sp
), "g" (SYS_rfork
), "g" (RFPROC
|RFMEM
)
183 #ifdef HAVE__LWP_CREATE
185 _lwp_makecontext( &context
, (void(*)(void *))SYSDEPS_StartThread
, thread
,
186 NULL
, thread
->stack_base
, (char *) thread
->teb
.stack_top
- (char *) thread
->stack_base
);
187 if ( _lwp_create( &context
, 0, NULL
) )
192 #endif /* NO_REENTRANT_LIBC */
194 FIXME("CreateThread: stub\n" );
200 /***********************************************************************
203 * Exit a running thread; must not return.
204 * Must not make any reference to the thread structures (THDB etc.) as
205 * they have already been deleted.
207 void SYSDEPS_ExitThread(void)
209 #ifdef HAVE__LWP_CREATE
217 /**********************************************************************
218 * NtCurrentTeb (NTDLL.89)
220 * This will crash and burn if called before threading is initialized
222 TEB
* WINAPI
NtCurrentTeb(void)
227 return &pCurrentThread
->teb
;
228 #endif /* __i386__ */