Fixed CreateDIBSection() called with negative height to set the height
[wine.git] / scheduler / sysdeps.c
blob17161c0729939b63ddf5d2f20db04313355860cb
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 "thread.h"
29 #include "server.h"
30 #include "winbase.h"
31 #include "debugtools.h"
33 DEFAULT_DEBUG_CHANNEL(thread)
35 #ifdef linux
36 #define HAVE_CLONE_SYSCALL
37 #endif
39 /* Xlib critical section (FIXME: does not belong here) */
40 CRITICAL_SECTION X11DRV_CritSection = { 0, };
42 #ifdef HAVE_CLONE_SYSCALL
43 # ifdef HAVE_SCHED_H
44 # include <sched.h>
45 # endif
46 # ifndef CLONE_VM
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 */
57 static int init_done;
59 #ifndef __i386__
60 static TEB *pCurrentTeb;
61 #endif
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()
73 #endif
74 #ifdef HAVE__ERROR
75 int *__error()
76 #endif
77 #ifdef HAVE___ERRNO
78 int *___errno()
79 #endif
81 if (!init_done) return perrno;
82 #ifdef NO_REENTRANT_X11
83 /* Use static libc errno while running in Xlib. */
84 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
85 return perrno;
86 #endif
87 return &NtCurrentTeb()->thread_errno;
90 /***********************************************************************
91 * __h_errno_location
93 * Get the per-thread h_errno location.
95 int *__h_errno_location()
97 if (!init_done) return ph_errno;
98 #ifdef NO_REENTRANT_X11
99 /* Use static libc h_errno while running in Xlib. */
100 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
101 return ph_errno;
102 #endif
103 return &NtCurrentTeb()->thread_h_errno;
106 #endif /* NO_REENTRANT_LIBC */
108 /***********************************************************************
109 * SYSDEPS_SetCurThread
111 * Make 'thread' the current thread.
113 void SYSDEPS_SetCurThread( TEB *teb )
115 #ifdef __i386__
116 /* On the i386, the current thread is in the %fs register */
117 SET_FS( teb->teb_sel );
118 #else
119 /* FIXME: only works if there is no preemptive task-switching going on... */
120 pCurrentTeb = teb;
121 #endif /* __i386__ */
122 init_done = 1; /* now we can use threading routines */
125 /***********************************************************************
126 * SYSDEPS_StartThread
128 * Startup routine for a new thread.
130 static void SYSDEPS_StartThread( TEB *teb )
132 SYSDEPS_SetCurThread( teb );
133 CLIENT_InitThread();
134 teb->startup();
135 SYSDEPS_ExitThread(); /* should never get here */
139 /***********************************************************************
140 * SYSDEPS_SpawnThread
142 * Start running a new thread.
143 * Return -1 on error, 0 if OK.
145 int SYSDEPS_SpawnThread( TEB *teb )
147 #ifndef NO_REENTRANT_LIBC
149 #ifdef HAVE_CLONE_SYSCALL
150 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top,
151 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, teb ) < 0)
152 return -1;
153 /* FIXME: close the child socket in the parent process */
154 /* close( thread->socket );*/
155 return 0;
156 #endif
158 #ifdef HAVE_RFORK
159 DWORD *sp = (DWORD *)teb->stack_top;
160 *--sp = (DWORD)teb;
161 *--sp = 0;
162 *--sp = (DWORD)SYSDEPS_StartThread;
163 __asm__(
164 "pushl %2;\n\t" /* RFPROC|RMEM */
165 "pushl $0;\n\t" /* 0 ? */
166 "movl %1,%%eax;\n\t" /* SYS_rfork */
167 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
168 "cmpl $0, %%edx;\n\t"
169 "je 1f;\n\t"
170 "movl %0,%%esp;\n\t" /* father -> new thread */
171 "ret;\n"
172 "1:\n\t" /* child -> caller thread */
173 "addl $8,%%esp" :
174 : "r" (sp), "g" (SYS_rfork), "g" (RFPROC|RFMEM)
175 : "eax", "edx");
176 return 0;
177 #endif
179 #ifdef HAVE__LWP_CREATE
180 ucontext_t context;
181 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
182 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
183 if ( _lwp_create( &context, 0, NULL ) )
184 return -1;
185 return 0;
186 #endif
188 #endif /* NO_REENTRANT_LIBC */
190 FIXME("CreateThread: stub\n" );
191 return 0;
196 /***********************************************************************
197 * SYSDEPS_ExitThread
199 * Exit a running thread; must not return.
200 * Must not make any reference to the thread structures (THDB etc.) as
201 * they have already been deleted.
203 void SYSDEPS_ExitThread(void)
205 #ifdef HAVE__LWP_CREATE
206 _lwp_exit();
207 #endif
209 _exit( 0 );
213 /**********************************************************************
214 * NtCurrentTeb (NTDLL.89)
216 * This will crash and burn if called before threading is initialized
219 #ifdef NtCurrentTeb
221 /* if it was defined as a macro, we need to do some magic */
222 #undef NtCurrentTeb
223 struct _TEB * WINAPI NtCurrentTeb(void)
225 return __get_teb();
228 #else /* NtCurrentTeb */
230 struct _TEB * WINAPI NtCurrentTeb(void)
232 return pCurrentTeb;
235 #endif /* NtCurrentTeb */