CoGetClassObject should complain about not being able to do
[wine.git] / scheduler / sysdeps.c
blobc9ea1b0e9a401c082584b3232ee182da1651cad9
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 "wine/exception.h"
32 #include "debugtools.h"
34 DEFAULT_DEBUG_CHANNEL(thread)
36 #ifdef linux
37 #define HAVE_CLONE_SYSCALL
38 #endif
40 /* Xlib critical section (FIXME: does not belong here) */
41 CRITICAL_SECTION X11DRV_CritSection = { 0, };
43 #ifdef HAVE_CLONE_SYSCALL
44 # ifdef HAVE_SCHED_H
45 # include <sched.h>
46 # endif
47 # ifndef CLONE_VM
48 # define CLONE_VM 0x00000100
49 # define CLONE_FS 0x00000200
50 # define CLONE_FILES 0x00000400
51 # define CLONE_SIGHAND 0x00000800
52 # define CLONE_PID 0x00001000
53 /* If we didn't get the flags, we probably didn't get the prototype either */
54 extern int clone( int (*fn)(void *arg), void *stack, int flags, void *arg );
55 # endif /* CLONE_VM */
56 #endif /* HAVE_CLONE_SYSCALL */
58 static int init_done;
60 #ifndef NO_REENTRANT_LIBC
62 /***********************************************************************
63 * __errno_location/__error/___errno
65 * Get the per-thread errno location.
67 #ifdef HAVE__ERRNO_LOCATION
68 int *__errno_location()
69 #endif
70 #ifdef HAVE__ERROR
71 int *__error()
72 #endif
73 #ifdef HAVE___ERRNO
74 int *___errno()
75 #endif
76 #ifdef HAVE__THR_ERRNO
77 int *__thr_errno()
78 #endif
80 if (!init_done) return perrno;
81 #ifdef NO_REENTRANT_X11
82 /* Use static libc errno while running in Xlib. */
83 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
84 return perrno;
85 #endif
86 return &NtCurrentTeb()->thread_errno;
89 /***********************************************************************
90 * __h_errno_location
92 * Get the per-thread h_errno location.
94 int *__h_errno_location()
96 if (!init_done) return ph_errno;
97 #ifdef NO_REENTRANT_X11
98 /* Use static libc h_errno while running in Xlib. */
99 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
100 return ph_errno;
101 #endif
102 return &NtCurrentTeb()->thread_h_errno;
105 #endif /* NO_REENTRANT_LIBC */
107 /***********************************************************************
108 * SYSDEPS_SetCurThread
110 * Make 'thread' the current thread.
112 void SYSDEPS_SetCurThread( TEB *teb )
114 #if defined(__i386__)
115 /* On the i386, the current thread is in the %fs register */
116 SET_FS( teb->teb_sel );
117 #elif defined(HAVE__LWP_CREATE)
118 /* On non-i386 Solaris, we use the LWP private pointer */
119 _lwp_setprivate( teb );
120 #endif
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 SIGNAL_Init();
135 __TRY
137 teb->startup();
139 __EXCEPT(UnhandledExceptionFilter)
141 TerminateThread( GetCurrentThread(), GetExceptionCode() );
143 __ENDTRY
144 SYSDEPS_ExitThread(); /* should never get here */
148 /***********************************************************************
149 * SYSDEPS_SpawnThread
151 * Start running a new thread.
152 * Return -1 on error, 0 if OK.
154 int SYSDEPS_SpawnThread( TEB *teb )
156 #ifndef NO_REENTRANT_LIBC
158 #ifdef HAVE_CLONE_SYSCALL
159 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top,
160 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, teb ) < 0)
161 return -1;
162 /* FIXME: close the child socket in the parent process */
163 /* close( thread->socket );*/
164 return 0;
165 #endif
167 #ifdef HAVE_RFORK
168 DWORD *sp = (DWORD *)teb->stack_top;
169 *--sp = (DWORD)teb;
170 *--sp = 0;
171 *--sp = (DWORD)SYSDEPS_StartThread;
172 __asm__(
173 "pushl %2;\n\t" /* RFPROC|RMEM */
174 "pushl $0;\n\t" /* 0 ? */
175 "movl %1,%%eax;\n\t" /* SYS_rfork */
176 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
177 "cmpl $0, %%edx;\n\t"
178 "je 1f;\n\t"
179 "movl %0,%%esp;\n\t" /* father -> new thread */
180 "ret;\n"
181 "1:\n\t" /* child -> caller thread */
182 "addl $8,%%esp" :
183 : "r" (sp), "g" (SYS_rfork), "g" (RFPROC|RFMEM)
184 : "eax", "edx");
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.
209 * Must not make any reference to the thread structures (THDB etc.) as
210 * they have already been deleted.
212 void SYSDEPS_ExitThread(void)
214 #ifdef HAVE__LWP_CREATE
215 _lwp_exit();
216 #endif
218 _exit( 0 );
222 /**********************************************************************
223 * NtCurrentTeb (NTDLL.89)
225 * This will crash and burn if called before threading is initialized
228 /* if it was defined as a macro, we need to do some magic */
229 #ifdef NtCurrentTeb
230 #undef NtCurrentTeb
231 #endif
233 struct _TEB * WINAPI NtCurrentTeb(void)
235 return __get_teb();