Add some null checking in the Get/SetBitmapBits functions.
[wine/wine64.git] / scheduler / sysdeps.c
blob08912177180381cd157e33fa532c225d2081fe8e
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 "debug.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 */
58 #ifndef NO_REENTRANT_LIBC
60 /***********************************************************************
61 * __errno_location/__error/___errno
63 * Get the per-thread errno location.
65 #ifdef HAVE__ERRNO_LOCATION
66 int *__errno_location()
67 #endif
68 #ifdef HAVE__ERROR
69 int *__error()
70 #endif
71 #ifdef HAVE___ERRNO
72 int *___errno()
73 #endif
75 THDB *thdb = THREAD_Current();
76 if (!thdb) return perrno;
77 #ifdef NO_REENTRANT_X11
78 /* Use static libc errno while running in Xlib. */
79 if (X11DRV_CritSection.OwningThread == (HANDLE)thdb->server_tid)
80 return perrno;
81 #endif
82 return &thdb->thread_errno;
85 /***********************************************************************
86 * __h_errno_location
88 * Get the per-thread h_errno location.
90 int *__h_errno_location()
92 THDB *thdb = THREAD_Current();
93 if (!thdb) return ph_errno;
94 #ifdef NO_REENTRANT_X11
95 /* Use static libc h_errno while running in Xlib. */
96 if (X11DRV_CritSection.OwningThread == (HANDLE)thdb->server_tid)
97 return ph_errno;
98 #endif
99 return &thdb->thread_h_errno;
102 #endif /* NO_REENTRANT_LIBC */
104 /***********************************************************************
105 * SYSDEPS_StartThread
107 * Startup routine for a new thread.
109 static void SYSDEPS_StartThread( THDB *thdb )
111 SET_CUR_THREAD( thdb );
112 CLIENT_InitThread();
113 thdb->startup();
114 _exit(0); /* should never get here */
118 /***********************************************************************
119 * SYSDEPS_SpawnThread
121 * Start running a new thread.
122 * Return -1 on error, 0 if OK.
124 int SYSDEPS_SpawnThread( THDB *thread )
126 #ifndef NO_REENTRANT_LIBC
128 #ifdef HAVE_CLONE_SYSCALL
129 if (clone( (int (*)(void *))SYSDEPS_StartThread, thread->teb.stack_top,
130 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, thread ) < 0)
131 return -1;
132 /* FIXME: close the child socket in the parent process */
133 /* close( thread->socket );*/
134 return 0;
135 #endif
137 #ifdef HAVE_RFORK
138 DWORD *sp = (DWORD *)thread->teb.stack_top;
139 *--sp = (DWORD)thread;
140 *--sp = 0;
141 *--sp = (DWORD)SYSDEPS_StartThread;
142 __asm__(
143 "pushl %2;\n\t" /* RFPROC|RMEM */
144 "pushl $0;\n\t" /* 0 ? */
145 "movl %1,%%eax;\n\t" /* SYS_rfork */
146 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
147 "cmpl $0, %%edx;\n\t"
148 "je 1f;\n\t"
149 "movl %0,%%esp;\n\t" /* father -> new thread */
150 "ret;\n"
151 "1:\n\t" /* child -> caller thread */
152 "addl $8,%%esp" :
153 : "r" (sp), "g" (SYS_rfork), "g" (RFPROC|RFMEM)
154 : "eax", "edx");
155 return 0;
156 #endif
158 #ifdef HAVE__LWP_CREATE
159 ucontext_t context;
160 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, thread,
161 NULL, thread->stack_base, (char *) thread->teb.stack_top - (char *) thread->stack_base );
162 if ( _lwp_create( &context, 0, NULL ) )
163 return -1;
164 return 0;
165 #endif
167 #endif /* NO_REENTRANT_LIBC */
169 FIXME(thread, "CreateThread: stub\n" );
170 return 0;
175 /***********************************************************************
176 * SYSDEPS_ExitThread
178 * Exit a running thread; must not return.
179 * Must not make any reference to the thread structures (THDB etc.) as
180 * they have already been deleted.
182 void SYSDEPS_ExitThread(void)
184 #ifdef HAVE__LWP_CREATE
185 _lwp_exit();
186 #endif
188 _exit( 0 );
192 /**********************************************************************
193 * NtCurrentTeb (NTDLL.89)
195 * This will crash and burn if called before threading is initialized
197 TEB * WINAPI NtCurrentTeb(void)
199 #ifdef __i386__
200 TEB *teb;
202 /* Get the TEB self-pointer */
203 __asm__( ".byte 0x64\n\tmovl (%1),%0"
204 : "=r" (teb) : "r" (&((TEB *)0)->self) );
205 return teb;
206 #else
207 return &pCurrentThread->teb;
208 #endif /* __i386__ */