Xiang Li
[wine/multimedia.git] / scheduler / sysdeps.c
blobab3f6d97a52a8752fdd854c8a2c45c00e5d7f7e2
1 /*
2 * System-dependent scheduler support
4 * Copyright 1998 Alexandre Julliard
5 */
7 #include "config.h"
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. */
11 extern int errno;
12 static int *perrno = &errno;
13 extern int h_errno;
14 static int *ph_errno = &h_errno;
16 #include <signal.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #ifdef HAVE_SYS_SYSCALL_H
20 # include <sys/syscall.h>
21 #endif
22 #ifdef HAVE_SYS_LWP_H
23 # include <sys/lwp.h>
24 #endif
25 #ifdef HAVE_UCONTEXT_H
26 # include <ucontext.h>
27 #endif
28 #include "wine/port.h"
29 #include "thread.h"
30 #include "server.h"
31 #include "winbase.h"
32 #include "wine/exception.h"
33 #include "debugtools.h"
35 DEFAULT_DEBUG_CHANNEL(thread)
37 /* Xlib critical section (FIXME: does not belong here) */
38 CRITICAL_SECTION X11DRV_CritSection = { 0, };
40 #ifdef linux
41 # ifdef HAVE_SCHED_H
42 # include <sched.h>
43 # endif
44 # ifndef CLONE_VM
45 # define CLONE_VM 0x00000100
46 # define CLONE_FS 0x00000200
47 # define CLONE_FILES 0x00000400
48 # define CLONE_SIGHAND 0x00000800
49 # define CLONE_PID 0x00001000
50 # endif /* CLONE_VM */
51 #endif /* linux */
53 static int init_done;
55 #ifndef NO_REENTRANT_LIBC
57 /***********************************************************************
58 * __errno_location/__error/___errno
60 * Get the per-thread errno location.
62 #ifdef HAVE__ERRNO_LOCATION
63 int *__errno_location()
64 #endif
65 #ifdef HAVE__ERROR
66 int *__error()
67 #endif
68 #ifdef HAVE___ERRNO
69 int *___errno()
70 #endif
71 #ifdef HAVE__THR_ERRNO
72 int *__thr_errno()
73 #endif
75 if (!init_done) return perrno;
76 #ifdef NO_REENTRANT_X11
77 /* Use static libc errno while running in Xlib. */
78 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
79 return perrno;
80 #endif
81 return &NtCurrentTeb()->thread_errno;
84 /***********************************************************************
85 * __h_errno_location
87 * Get the per-thread h_errno location.
89 int *__h_errno_location()
91 if (!init_done) return ph_errno;
92 #ifdef NO_REENTRANT_X11
93 /* Use static libc h_errno while running in Xlib. */
94 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
95 return ph_errno;
96 #endif
97 return &NtCurrentTeb()->thread_h_errno;
100 #endif /* NO_REENTRANT_LIBC */
102 /***********************************************************************
103 * SYSDEPS_SetCurThread
105 * Make 'thread' the current thread.
107 void SYSDEPS_SetCurThread( TEB *teb )
109 #if defined(__i386__)
110 /* On the i386, the current thread is in the %fs register */
111 SET_FS( teb->teb_sel );
112 #elif defined(HAVE__LWP_CREATE)
113 /* On non-i386 Solaris, we use the LWP private pointer */
114 _lwp_setprivate( teb );
115 #endif
117 init_done = 1; /* now we can use threading routines */
120 /***********************************************************************
121 * SYSDEPS_StartThread
123 * Startup routine for a new thread.
125 static void SYSDEPS_StartThread( TEB *teb )
127 SYSDEPS_SetCurThread( teb );
128 CLIENT_InitThread();
129 SIGNAL_Init();
130 __TRY
132 teb->startup();
134 __EXCEPT(UnhandledExceptionFilter)
136 TerminateThread( GetCurrentThread(), GetExceptionCode() );
138 __ENDTRY
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( TEB *teb )
151 #ifndef NO_REENTRANT_LIBC
153 #ifdef linux
154 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top,
155 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, teb ) < 0)
156 return -1;
157 /* FIXME: close the child socket in the parent process */
158 /* close( thread->socket );*/
159 return 0;
160 #endif
162 #ifdef HAVE_RFORK
163 DWORD *sp = (DWORD *)teb->stack_top;
164 *--sp = (DWORD)teb;
165 *--sp = 0;
166 *--sp = (DWORD)SYSDEPS_StartThread;
167 __asm__(
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"
173 "je 1f;\n\t"
174 "movl %0,%%esp;\n\t" /* father -> new thread */
175 "ret;\n"
176 "1:\n\t" /* child -> caller thread */
177 "addl $8,%%esp" :
178 : "r" (sp), "g" (SYS_rfork), "g" (RFPROC|RFMEM)
179 : "eax", "edx");
180 return 0;
181 #endif
183 #ifdef HAVE__LWP_CREATE
184 ucontext_t context;
185 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
186 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
187 if ( _lwp_create( &context, 0, NULL ) )
188 return -1;
189 return 0;
190 #endif
192 #endif /* NO_REENTRANT_LIBC */
194 FIXME("CreateThread: stub\n" );
195 return 0;
200 /***********************************************************************
201 * SYSDEPS_ExitThread
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
210 _lwp_exit();
211 #endif
213 _exit( 0 );
217 /**********************************************************************
218 * NtCurrentTeb (NTDLL.89)
220 * This will crash and burn if called before threading is initialized
223 /* if it was defined as a macro, we need to do some magic */
224 #ifdef NtCurrentTeb
225 #undef NtCurrentTeb
226 #endif
228 struct _TEB * WINAPI NtCurrentTeb(void)
230 return __get_teb();