Added offsetof (for the MFC).
[wine/wine-kai.git] / scheduler / sysdeps.c
blob86ac9b953364c8dc9c2538a44bfff3fbc710ac03
1 /*
2 * System-dependent scheduler support
4 * Copyright 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <signal.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <sys/time.h>
13 #include <sys/resource.h>
14 #ifdef HAVE_SYS_SYSCALL_H
15 # include <sys/syscall.h>
16 #endif
17 #ifdef HAVE_SYS_LWP_H
18 # include <sys/lwp.h>
19 #endif
20 #ifdef HAVE_UCONTEXT_H
21 # include <ucontext.h>
22 #endif
23 #include "wine/port.h"
24 #include "thread.h"
25 #include "server.h"
26 #include "winbase.h"
27 #include "wine/exception.h"
28 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(thread);
32 #ifdef linux
33 # ifdef HAVE_SCHED_H
34 # include <sched.h>
35 # endif
36 # ifndef CLONE_VM
37 # define CLONE_VM 0x00000100
38 # define CLONE_FS 0x00000200
39 # define CLONE_FILES 0x00000400
40 # define CLONE_SIGHAND 0x00000800
41 # define CLONE_PID 0x00001000
42 # endif /* CLONE_VM */
43 #endif /* linux */
45 /***********************************************************************
46 * SYSDEPS_SetCurThread
48 * Make 'thread' the current thread.
50 void SYSDEPS_SetCurThread( TEB *teb )
52 #if defined(__i386__)
53 /* On the i386, the current thread is in the %fs register */
54 __set_fs( teb->teb_sel );
55 #elif defined(HAVE__LWP_CREATE)
56 /* On non-i386 Solaris, we use the LWP private pointer */
57 _lwp_setprivate( teb );
58 #endif
61 /***********************************************************************
62 * SYSDEPS_StartThread
64 * Startup routine for a new thread.
66 static void SYSDEPS_StartThread( TEB *teb )
68 SYSDEPS_SetCurThread( teb );
69 CLIENT_InitThread();
70 SIGNAL_Init();
71 __TRY
73 teb->startup();
75 __EXCEPT(UnhandledExceptionFilter)
77 TerminateThread( GetCurrentThread(), GetExceptionCode() );
79 __ENDTRY
80 SYSDEPS_ExitThread(0); /* should never get here */
84 /***********************************************************************
85 * SYSDEPS_SpawnThread
87 * Start running a new thread.
88 * Return -1 on error, 0 if OK.
90 int SYSDEPS_SpawnThread( TEB *teb )
92 #ifdef ERRNO_LOCATION
94 #ifdef linux
95 const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
96 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
97 return -1;
98 if (!(flags & CLONE_FILES)) close( teb->request_fd ); /* close the child socket in the parent */
99 return 0;
100 #endif
102 #ifdef HAVE_RFORK
103 const int flags = RFPROC | RFMEM; /*|RFFDG*/
104 void **sp = (void **)teb->stack_top;
105 *--sp = teb;
106 *--sp = 0;
107 *--sp = SYSDEPS_StartThread;
108 __asm__ __volatile__(
109 "pushl %2;\n\t" /* flags */
110 "pushl $0;\n\t" /* 0 ? */
111 "movl %1,%%eax;\n\t" /* SYS_rfork */
112 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
113 "cmpl $0, %%edx;\n\t"
114 "je 1f;\n\t"
115 "movl %0,%%esp;\n\t" /* child -> new thread */
116 "ret;\n"
117 "1:\n\t" /* parent -> caller thread */
118 "addl $8,%%esp" :
119 : "r" (sp), "g" (SYS_rfork), "g" (flags)
120 : "eax", "edx");
121 if (flags & RFFDG) close( teb->request_fd ); /* close the child socket in the parent */
122 return 0;
123 #endif
125 #ifdef HAVE__LWP_CREATE
126 ucontext_t context;
127 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
128 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
129 if ( _lwp_create( &context, 0, NULL ) )
130 return -1;
131 return 0;
132 #endif
134 #endif /* ERRNO_LOCATION */
136 FIXME("CreateThread: stub\n" );
137 return 0;
142 /***********************************************************************
143 * SYSDEPS_ExitThread
145 * Exit a running thread; must not return.
147 void SYSDEPS_ExitThread( int status )
149 int fd = NtCurrentTeb()->request_fd;
150 NtCurrentTeb()->request_fd = -1;
151 close( fd );
152 #ifdef HAVE__LWP_CREATE
153 _lwp_exit();
154 #endif
155 _exit( status );
157 * It is of course impossible to come here,
158 * but it eliminates a compiler warning.
160 exit( status );
164 /***********************************************************************
165 * SYSDEPS_CallOnStack
167 int SYSDEPS_DoCallOnStack( int (*func)(LPVOID), LPVOID arg )
169 int retv = 0;
171 __TRY
173 retv = func( arg );
175 __EXCEPT(UnhandledExceptionFilter)
177 TerminateThread( GetCurrentThread(), GetExceptionCode() );
178 return 0;
180 __ENDTRY
182 return retv;
185 #ifdef __i386__
186 int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
187 int (*func)(LPVOID), LPVOID arg );
188 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
189 "pushl %ebp\n\t"
190 "movl %esp, %ebp\n\t"
191 ".byte 0x64; pushl 0x04\n\t"
192 ".byte 0x64; pushl 0x08\n\t"
193 "movl 8(%ebp), %esp\n\t"
194 "movl 12(%ebp), %eax\n\t"
195 ".byte 0x64; movl %esp, 0x04\n\t"
196 ".byte 0x64; movl %eax, 0x08\n\t"
197 "pushl 20(%ebp)\n\t"
198 "pushl 16(%ebp)\n\t"
199 "call " __ASM_NAME("SYSDEPS_DoCallOnStack") "\n\t"
200 "leal -8(%ebp), %esp\n\t"
201 ".byte 0x64; popl 0x08\n\t"
202 ".byte 0x64; popl 0x04\n\t"
203 "popl %ebp\n\t"
204 "ret" );
205 #else
206 int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
207 int (*func)(LPVOID), LPVOID arg )
209 return SYSDEPS_DoCallOnStack( func, arg );
211 #endif
213 /***********************************************************************
214 * SYSDEPS_SwitchToThreadStack
216 void SYSDEPS_SwitchToThreadStack( void (*func)(void) )
218 DWORD page_size = getpagesize();
219 DWORD cur_stack = (((DWORD)&func) + (page_size-1)) & ~(page_size-1);
221 TEB *teb = NtCurrentTeb();
222 LPVOID stackTop = teb->stack_top;
223 LPVOID stackLow = teb->stack_low;
225 struct rlimit rl;
227 if ( getrlimit(RLIMIT_STACK, &rl) < 0 )
229 WARN("Can't get rlimit\n");
230 rl.rlim_cur = 8*1024*1024;
233 teb->stack_top = (LPVOID) cur_stack;
234 teb->stack_low = (LPVOID)(cur_stack - rl.rlim_cur);
236 SYSDEPS_CallOnStack( stackTop, stackLow,
237 (int (*)(void *))func, NULL );
240 /**********************************************************************
241 * NtCurrentTeb (NTDLL.@)
243 * This will crash and burn if called before threading is initialized
245 #ifdef __i386__
246 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
247 #elif defined(HAVE__LWP_CREATE)
248 struct _TEB * WINAPI NtCurrentTeb(void)
250 extern void *_lwp_getprivate(void);
251 return (struct _TEB *)_lwp_getprivate();
253 #else
254 # error NtCurrentTeb not defined for this architecture
255 #endif /* __i386__ */