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>
28 #include "wine/port.h"
30 #include "selectors.h"
33 #include "wine/exception.h"
34 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(thread
)
38 /* Xlib critical section (FIXME: does not belong here) */
39 CRITICAL_SECTION X11DRV_CritSection
= { 0, };
46 # define CLONE_VM 0x00000100
47 # define CLONE_FS 0x00000200
48 # define CLONE_FILES 0x00000400
49 # define CLONE_SIGHAND 0x00000800
50 # define CLONE_PID 0x00001000
51 # endif /* CLONE_VM */
56 #ifndef NO_REENTRANT_LIBC
58 /***********************************************************************
59 * __errno_location/__error/___errno
61 * Get the per-thread errno location.
63 #ifdef HAVE__ERRNO_LOCATION
64 int *__errno_location()
72 #ifdef HAVE__THR_ERRNO
76 if (!init_done
) return perrno
;
77 #ifdef NO_REENTRANT_X11
78 /* Use static libc errno while running in Xlib. */
79 if (X11DRV_CritSection
.OwningThread
== GetCurrentThreadId())
82 return &NtCurrentTeb()->thread_errno
;
85 /***********************************************************************
88 * Get the per-thread h_errno location.
90 int *__h_errno_location()
92 if (!init_done
) return ph_errno
;
93 #ifdef NO_REENTRANT_X11
94 /* Use static libc h_errno while running in Xlib. */
95 if (X11DRV_CritSection
.OwningThread
== GetCurrentThreadId())
98 return &NtCurrentTeb()->thread_h_errno
;
101 #endif /* NO_REENTRANT_LIBC */
103 /***********************************************************************
104 * SYSDEPS_SetCurThread
106 * Make 'thread' the current thread.
108 void SYSDEPS_SetCurThread( TEB
*teb
)
110 #if defined(__i386__)
111 /* On the i386, the current thread is in the %fs register */
112 __set_fs( teb
->teb_sel
);
113 #elif defined(HAVE__LWP_CREATE)
114 /* On non-i386 Solaris, we use the LWP private pointer */
115 _lwp_setprivate( teb
);
118 init_done
= 1; /* now we can use threading routines */
121 /***********************************************************************
122 * SYSDEPS_StartThread
124 * Startup routine for a new thread.
126 static void SYSDEPS_StartThread( TEB
*teb
)
128 SYSDEPS_SetCurThread( teb
);
135 __EXCEPT(UnhandledExceptionFilter
)
137 TerminateThread( GetCurrentThread(), GetExceptionCode() );
140 SYSDEPS_ExitThread(); /* should never get here */
144 /***********************************************************************
145 * SYSDEPS_SpawnThread
147 * Start running a new thread.
148 * Return -1 on error, 0 if OK.
150 int SYSDEPS_SpawnThread( TEB
*teb
)
152 #ifndef NO_REENTRANT_LIBC
155 if (clone( (int (*)(void *))SYSDEPS_StartThread
, teb
->stack_top
,
156 CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
, teb
) < 0)
158 /* FIXME: close the child socket in the parent process */
159 /* close( thread->socket );*/
164 DWORD
*sp
= (DWORD
*)teb
->stack_top
;
167 *--sp
= (DWORD
)SYSDEPS_StartThread
;
169 "pushl %2;\n\t" /* RFPROC|RMEM */
170 "pushl $0;\n\t" /* 0 ? */
171 "movl %1,%%eax;\n\t" /* SYS_rfork */
172 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
173 "cmpl $0, %%edx;\n\t"
175 "movl %0,%%esp;\n\t" /* father -> new thread */
177 "1:\n\t" /* child -> caller thread */
179 : "r" (sp
), "g" (SYS_rfork
), "g" (RFPROC
|RFMEM
)
184 #ifdef HAVE__LWP_CREATE
186 _lwp_makecontext( &context
, (void(*)(void *))SYSDEPS_StartThread
, teb
,
187 NULL
, teb
->stack_base
, (char *)teb
->stack_top
- (char *)teb
->stack_base
);
188 if ( _lwp_create( &context
, 0, NULL
) )
193 #endif /* NO_REENTRANT_LIBC */
195 FIXME("CreateThread: stub\n" );
201 /***********************************************************************
204 * Exit a running thread; must not return.
205 * Must not make any reference to the thread structures (THDB etc.) as
206 * they have already been deleted.
208 void SYSDEPS_ExitThread(void)
210 #if !defined(__i386__) && defined(HAVE__LWP_CREATE)
217 /**********************************************************************
218 * NtCurrentTeb (NTDLL.89)
220 * This will crash and burn if called before threading is initialized
223 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
224 #elif defined(HAVE__LWP_CREATE)
225 struct _TEB
* WINAPI
NtCurrentTeb(void)
227 extern void *_lwp_getprivate(void);
228 return (struct _TEB
*)_lwp_getprivate();
231 # error NtCurrentTeb not defined for this architecture
232 #endif /* __i386__ */