Stubbed GetDeviceGammaRamp.
[wine.git] / scheduler / sysdeps.c
blobed530de6f990f653ae80a794eda5e375159cf61d
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 #include <sys/time.h>
20 #include <sys/resource.h>
21 #ifdef HAVE_SYS_SYSCALL_H
22 # include <sys/syscall.h>
23 #endif
24 #ifdef HAVE_SYS_LWP_H
25 # include <sys/lwp.h>
26 #endif
27 #ifdef HAVE_UCONTEXT_H
28 # include <ucontext.h>
29 #endif
30 #include "wine/port.h"
31 #include "thread.h"
32 #include "selectors.h"
33 #include "server.h"
34 #include "winbase.h"
35 #include "global.h"
36 #include "wine/exception.h"
37 #include "debugtools.h"
39 DEFAULT_DEBUG_CHANNEL(thread)
41 /* Xlib critical section (FIXME: does not belong here) */
42 CRITICAL_SECTION X11DRV_CritSection = CRITICAL_SECTION_INIT;
44 #ifdef linux
45 # ifdef HAVE_SCHED_H
46 # include <sched.h>
47 # endif
48 # ifndef CLONE_VM
49 # define CLONE_VM 0x00000100
50 # define CLONE_FS 0x00000200
51 # define CLONE_FILES 0x00000400
52 # define CLONE_SIGHAND 0x00000800
53 # define CLONE_PID 0x00001000
54 # endif /* CLONE_VM */
55 #endif /* linux */
57 static int init_done;
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()
68 #endif
69 #ifdef HAVE__ERROR
70 int *__error()
71 #endif
72 #ifdef HAVE___ERRNO
73 int *___errno()
74 #endif
75 #ifdef HAVE__THR_ERRNO
76 int *__thr_errno()
77 #endif
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())
83 return perrno;
84 #endif
85 return &NtCurrentTeb()->thread_errno;
88 /***********************************************************************
89 * __h_errno_location
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())
99 return ph_errno;
100 #endif
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 );
119 #endif
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 );
132 CLIENT_InitThread();
133 SIGNAL_Init();
134 __TRY
136 teb->startup();
138 __EXCEPT(UnhandledExceptionFilter)
140 TerminateThread( GetCurrentThread(), GetExceptionCode() );
142 __ENDTRY
143 SYSDEPS_ExitThread(0); /* should never get here */
147 /***********************************************************************
148 * SYSDEPS_SpawnThread
150 * Start running a new thread.
151 * Return -1 on error, 0 if OK.
153 int SYSDEPS_SpawnThread( TEB *teb )
155 #ifndef NO_REENTRANT_LIBC
157 #ifdef linux
158 const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
159 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
160 return -1;
161 if (!(flags & CLONE_FILES)) close( teb->socket ); /* close the child socket in the parent */
162 return 0;
163 #endif
165 #ifdef HAVE_RFORK
166 const int flags = RFPROC | RFMEM; /*|RFFDG*/
167 void **sp = (void **)teb->stack_top;
168 *--sp = teb;
169 *--sp = 0;
170 *--sp = SYSDEPS_StartThread;
171 __asm__ __volatile__(
172 "pushl %2;\n\t" /* flags */
173 "pushl $0;\n\t" /* 0 ? */
174 "movl %1,%%eax;\n\t" /* SYS_rfork */
175 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
176 "cmpl $0, %%edx;\n\t"
177 "je 1f;\n\t"
178 "movl %0,%%esp;\n\t" /* child -> new thread */
179 "ret;\n"
180 "1:\n\t" /* parent -> caller thread */
181 "addl $8,%%esp" :
182 : "r" (sp), "g" (SYS_rfork), "g" (flags)
183 : "eax", "edx");
184 if (flags & RFFDG) close( teb->socket ); /* close the child socket in the parent */
185 return 0;
186 #endif
188 #ifdef HAVE__LWP_CREATE
189 ucontext_t context;
190 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
191 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
192 if ( _lwp_create( &context, 0, NULL ) )
193 return -1;
194 return 0;
195 #endif
197 #endif /* NO_REENTRANT_LIBC */
199 FIXME("CreateThread: stub\n" );
200 return 0;
205 /***********************************************************************
206 * SYSDEPS_ExitThread
208 * Exit a running thread; must not return.
210 void SYSDEPS_ExitThread( int status )
212 int socket = NtCurrentTeb()->socket;
213 NtCurrentTeb()->socket = -1;
214 close( socket );
215 #ifdef HAVE__LWP_CREATE
216 _lwp_exit();
217 #endif
218 _exit( status );
220 * It is of course impossible to come here,
221 * but it eliminates a compiler warning.
223 exit( status );
227 /***********************************************************************
228 * SYSDEPS_CallOnStack
230 int SYSDEPS_DoCallOnStack( int (*func)(LPVOID), LPVOID arg )
232 int retv = 0;
234 __TRY
236 retv = func( arg );
238 __EXCEPT(UnhandledExceptionFilter)
240 TerminateThread( GetCurrentThread(), GetExceptionCode() );
241 return 0;
243 __ENDTRY
245 return retv;
248 #ifdef __i386__
249 int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
250 int (*func)(LPVOID), LPVOID arg );
251 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
252 "pushl %ebp\n\t"
253 "movl %esp, %ebp\n\t"
254 ".byte 0x64; pushl 0x04\n\t"
255 ".byte 0x64; pushl 0x08\n\t"
256 "movl 8(%ebp), %esp\n\t"
257 "movl 12(%ebp), %eax\n\t"
258 ".byte 0x64; movl %esp, 0x04\n\t"
259 ".byte 0x64; movl %eax, 0x08\n\t"
260 "pushl 20(%ebp)\n\t"
261 "pushl 16(%ebp)\n\t"
262 "call " __ASM_NAME("SYSDEPS_DoCallOnStack") "\n\t"
263 "leal -8(%ebp), %esp\n\t"
264 ".byte 0x64; popl 0x08\n\t"
265 ".byte 0x64; popl 0x04\n\t"
266 "popl %ebp\n\t"
267 "ret" );
268 #else
269 int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
270 int (*func)(LPVOID), LPVOID arg )
272 return SYSDEPS_DoCallOnStack( func, arg );
274 #endif
276 /***********************************************************************
277 * SYSDEPS_SwitchToThreadStack
280 static LPVOID SYSDEPS_LargeStackTop = NULL;
281 static LPVOID SYSDEPS_LargeStackLow = NULL;
283 void SYSDEPS_SwitchToThreadStack( void (*func)(void) )
285 DWORD page_size = VIRTUAL_GetPageSize();
286 DWORD cur_stack = (((DWORD)&func) + (page_size-1)) & ~(page_size-1);
288 TEB *teb = NtCurrentTeb();
289 LPVOID stackTop = teb->stack_top;
290 LPVOID stackLow = teb->stack_low;
292 struct rlimit rl;
294 if ( getrlimit(RLIMIT_STACK, &rl) < 0 )
296 WARN("Can't get rlimit\n");
297 rl.rlim_cur = 8*1024*1024;
300 teb->stack_top = (LPVOID) cur_stack;
301 teb->stack_low = (LPVOID)(cur_stack - rl.rlim_cur);
303 SYSDEPS_LargeStackTop = (LPVOID)(cur_stack - 2*page_size);
304 SYSDEPS_LargeStackLow = (LPVOID)(cur_stack - rl.rlim_cur);
306 SYSDEPS_CallOnStack( stackTop, stackLow,
307 (int (*)(void *))func, NULL );
310 /***********************************************************************
311 * SYSDEPS_CallOnLargeStack
313 int SYSDEPS_CallOnLargeStack( int (*func)(LPVOID), LPVOID arg )
315 static int recurse = 0;
316 int retv;
318 if ( recurse++ == 0 && SYSDEPS_LargeStackTop )
319 retv = SYSDEPS_CallOnStack( SYSDEPS_LargeStackTop,
320 SYSDEPS_LargeStackLow, func, arg );
321 else
322 retv = func( arg );
324 recurse--;
325 return retv;
329 /**********************************************************************
330 * NtCurrentTeb (NTDLL.89)
332 * This will crash and burn if called before threading is initialized
334 #ifdef __i386__
335 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
336 #elif defined(HAVE__LWP_CREATE)
337 struct _TEB * WINAPI NtCurrentTeb(void)
339 extern void *_lwp_getprivate(void);
340 return (struct _TEB *)_lwp_getprivate();
342 #else
343 # error NtCurrentTeb not defined for this architecture
344 #endif /* __i386__ */