Release 980215
[wine/multimedia.git] / scheduler / sysdeps.c
blobe020953e00280bbc591a212f0325ef5535e708e3
1 /*
2 * System-dependent scheduler support
4 * Copyright 1998 Alexandre Julliard
5 */
7 /* Get pointers to the static errno and h_errno variables used by Xlib. This
8 must be done before including <errno.h> makes the variables invisible. */
9 extern int errno;
10 static int *perrno = &errno;
11 extern int h_errno;
12 static int *ph_errno = &h_errno;
14 #include <signal.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include "thread.h"
18 #include "winbase.h"
20 /* FIXME: X libs compiled w/o -D_REENTRANT should be detected by autoconf. */
21 #define NO_REENTRANT_X11
23 #ifdef NO_REENTRANT_X11
24 /* Xlib critical section (FIXME: does not belong here) */
25 CRITICAL_SECTION X11DRV_CritSection = { 0, };
26 #endif
28 #ifdef __linux__
29 # ifdef HAVE_SCHED_H
30 # include <sched.h>
31 # endif
32 # ifndef CLONE_VM
33 # define CLONE_VM 0x00000100
34 # define CLONE_FS 0x00000200
35 # define CLONE_FILES 0x00000400
36 # define CLONE_SIGHAND 0x00000800
37 # define CLONE_PID 0x00001000
38 /* If we didn't get the flags, we probably didn't get the prototype either */
39 extern int clone( int (*fn)(void *arg), void *stack, int flags, void *arg );
40 # endif /* CLONE_VM */
41 #endif /* __linux__ */
44 #ifdef __linux__
45 /***********************************************************************
46 * __errno_location
48 * Get the per-thread errno location.
50 int *__errno_location()
52 THDB *thdb = THREAD_Current();
53 if (!thdb) return perrno;
54 #ifdef NO_REENTRANT_X11
55 /* Use static libc errno while running in Xlib. */
56 if (X11DRV_CritSection.OwningThread == THDB_TO_THREAD_ID(thdb))
57 return perrno;
58 #endif
59 return &thdb->thread_errno;
62 /***********************************************************************
63 * __h_errno_location
65 * Get the per-thread h_errno location.
67 int *__h_errno_location()
69 THDB *thdb = THREAD_Current();
70 if (!thdb) return ph_errno;
71 #ifdef NO_REENTRANT_X11
72 /* Use static libc h_errno while running in Xlib. */
73 if (X11DRV_CritSection.OwningThread == THDB_TO_THREAD_ID(thdb))
74 return ph_errno;
75 #endif
76 return &thdb->thread_h_errno;
79 /***********************************************************************
80 * SYSDEPS_StartThread
82 * Startup routine for a new thread.
84 static void SYSDEPS_StartThread( THDB *thdb )
86 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)thdb->entry_point;
87 thdb->unix_pid = getpid();
88 SET_FS( thdb->teb_sel );
89 ExitThread( func( thdb->entry_arg ) );
91 #endif /* __linux__ */
94 /***********************************************************************
95 * SYSDEPS_SpawnThread
97 * Start running a new thread.
98 * Return -1 on error, 0 if OK.
100 int SYSDEPS_SpawnThread( THDB *thread )
102 #ifdef __linux__
103 if (clone( (int (*)(void *))SYSDEPS_StartThread, thread->teb.stack_top,
104 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, thread ) < 0)
105 return -1;
106 #else
107 fprintf( stderr, "CreateThread: stub\n" );
108 #endif /* __linux__ */
109 return 0;
113 /***********************************************************************
114 * SYSDEPS_ExitThread
116 * Exit a running thread; must not return.
118 void SYSDEPS_ExitThread(void)
120 _exit( 0 );
124 /**********************************************************************
125 * NtCurrentTeb (NTDLL.89)
127 TEB * WINAPI NtCurrentTeb(void)
129 #ifdef __i386__
130 TEB *teb;
131 WORD ds, fs;
133 /* Check if we have a current thread */
134 GET_FS( fs );
135 if (!fs) return NULL;
136 GET_DS( ds );
137 if (fs == ds) return NULL; /* FIXME: should be an assert */
138 /* Get the TEB self-pointer */
139 __asm__( ".byte 0x64\n\tmovl (%1),%0"
140 : "=r" (teb) : "r" (&((TEB *)0)->self) );
141 return teb;
142 #else
143 if (!pCurrentThread) return NULL;
144 return &pCurrentThread->teb;
145 #endif /* __i386__ */