New files to support load order and partial stubs for elfdlls.
[wine/gsoc_dplay.git] / scheduler / sysdeps.c
bloba579aefc2671311bd5eaf36175aa6ecae3c0d806
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 #include "thread.h"
23 #include "server.h"
24 #include "winbase.h"
25 #include "debug.h"
28 /* Xlib critical section (FIXME: does not belong here) */
29 CRITICAL_SECTION X11DRV_CritSection = { 0, };
31 #ifdef HAVE_CLONE_SYSCALL
32 # ifdef HAVE_SCHED_H
33 # include <sched.h>
34 # endif
35 # ifndef CLONE_VM
36 # define CLONE_VM 0x00000100
37 # define CLONE_FS 0x00000200
38 # define CLONE_FILES 0x00000400
39 # define CLONE_SIGHAND 0x00000800
40 # define CLONE_PID 0x00001000
41 /* If we didn't get the flags, we probably didn't get the prototype either */
42 extern int clone( int (*fn)(void *arg), void *stack, int flags, void *arg );
43 # endif /* CLONE_VM */
44 #endif /* HAVE_CLONE_SYSCALL */
47 #ifdef USE_THREADS
48 #ifdef linux
49 /***********************************************************************
50 * __errno_location
52 * Get the per-thread errno location.
54 int *__errno_location()
56 THDB *thdb = THREAD_Current();
57 if (!thdb) return perrno;
58 #ifdef NO_REENTRANT_X11
59 /* Use static libc errno while running in Xlib. */
60 if (X11DRV_CritSection.OwningThread == (HANDLE)thdb->server_tid)
61 return perrno;
62 #endif
63 return &thdb->thread_errno;
66 /***********************************************************************
67 * __h_errno_location
69 * Get the per-thread h_errno location.
71 int *__h_errno_location()
73 THDB *thdb = THREAD_Current();
74 if (!thdb) return ph_errno;
75 #ifdef NO_REENTRANT_X11
76 /* Use static libc h_errno while running in Xlib. */
77 if (X11DRV_CritSection.OwningThread == (HANDLE)thdb->server_tid)
78 return ph_errno;
79 #endif
80 return &thdb->thread_h_errno;
82 #endif
84 #ifdef __FreeBSD__
85 int *__error() {
86 THDB *thdb = THREAD_Current();
87 if (!thdb) return perrno;
88 #ifdef NO_REENTRANT_X11
89 /* Use static libc errno while running in Xlib. */
90 if (X11DRV_CritSection.OwningThread == (HANDLE)thdb->server_tid)
91 return perrno;
92 #endif
93 return &thdb->thread_errno;
95 #endif
97 /***********************************************************************
98 * SYSDEPS_StartThread
100 * Startup routine for a new thread.
102 static void SYSDEPS_StartThread( THDB *thdb )
104 SET_CUR_THREAD( thdb );
105 CLIENT_InitThread();
106 thdb->startup();
107 _exit(0); /* should never get here */
109 #endif /* USE_THREADS */
112 /***********************************************************************
113 * SYSDEPS_SpawnThread
115 * Start running a new thread.
116 * Return -1 on error, 0 if OK.
118 int SYSDEPS_SpawnThread( THDB *thread )
120 #ifdef USE_THREADS
122 #ifdef HAVE_CLONE_SYSCALL
123 if (clone( (int (*)(void *))SYSDEPS_StartThread, thread->teb.stack_top,
124 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, thread ) < 0)
125 return -1;
126 /* FIXME: close the child socket in the parent process */
127 /* close( thread->socket );*/
128 #endif
130 #ifdef HAVE_RFORK
131 DWORD *sp = (DWORD *)thread->teb.stack_top;
132 *--sp = (DWORD)thread;
133 *--sp = 0;
134 *--sp = (DWORD)SYSDEPS_StartThread;
135 __asm__(
136 "pushl %2;\n\t" /* RFPROC|RMEM */
137 "pushl $0;\n\t" /* 0 ? */
138 "movl %1,%%eax;\n\t" /* SYS_rfork */
139 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
140 "cmpl $0, %%edx;\n\t"
141 "je 1f;\n\t"
142 "movl %0,%%esp;\n\t" /* father -> new thread */
143 "ret;\n"
144 "1:\n\t" /* child -> caller thread */
145 "addl $8,%%esp" :
146 : "r" (sp), "g" (SYS_rfork), "g" (RFPROC|RFMEM)
147 : "eax", "edx");
148 #endif
150 #else /* !USE_THREADS */
151 FIXME(thread, "CreateThread: stub\n" );
152 #endif /* USE_THREADS */
153 return 0;
157 /***********************************************************************
158 * SYSDEPS_ExitThread
160 * Exit a running thread; must not return.
161 * Must not make any reference to the thread structures (THDB etc.) as
162 * they have already been deleted.
164 void SYSDEPS_ExitThread(void)
166 _exit( 0 );
170 /**********************************************************************
171 * NtCurrentTeb (NTDLL.89)
173 * This will crash and burn if called before threading is initialized
175 TEB * WINAPI NtCurrentTeb(void)
177 #ifdef __i386__
178 TEB *teb;
180 /* Get the TEB self-pointer */
181 __asm__( ".byte 0x64\n\tmovl (%1),%0"
182 : "=r" (teb) : "r" (&((TEB *)0)->self) );
183 return teb;
184 #else
185 return &pCurrentThread->teb;
186 #endif /* __i386__ */