Release 980712
[wine/multimedia.git] / scheduler / sysdeps.c
blob390c68c458447e74e443f5656d65773c7951926b
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 "server.h"
19 #include "winbase.h"
20 #include "debug.h"
22 /* Xlib critical section (FIXME: does not belong here) */
23 CRITICAL_SECTION X11DRV_CritSection = { 0, };
25 #ifdef __linux__
26 # ifdef HAVE_SCHED_H
27 # include <sched.h>
28 # endif
29 # ifndef CLONE_VM
30 # define CLONE_VM 0x00000100
31 # define CLONE_FS 0x00000200
32 # define CLONE_FILES 0x00000400
33 # define CLONE_SIGHAND 0x00000800
34 # define CLONE_PID 0x00001000
35 /* If we didn't get the flags, we probably didn't get the prototype either */
36 extern int clone( int (*fn)(void *arg), void *stack, int flags, void *arg );
37 # endif /* CLONE_VM */
38 #endif /* __linux__ */
41 #ifdef __linux__
42 /***********************************************************************
43 * __errno_location
45 * Get the per-thread errno location.
47 int *__errno_location()
49 THDB *thdb = THREAD_Current();
50 if (!thdb) return perrno;
51 #ifdef NO_REENTRANT_X11
52 /* Use static libc errno while running in Xlib. */
53 if (X11DRV_CritSection.OwningThread == THDB_TO_THREAD_ID(thdb))
54 return perrno;
55 #endif
56 return &thdb->thread_errno;
59 /***********************************************************************
60 * __h_errno_location
62 * Get the per-thread h_errno location.
64 int *__h_errno_location()
66 THDB *thdb = THREAD_Current();
67 if (!thdb) return ph_errno;
68 #ifdef NO_REENTRANT_X11
69 /* Use static libc h_errno while running in Xlib. */
70 if (X11DRV_CritSection.OwningThread == THDB_TO_THREAD_ID(thdb))
71 return ph_errno;
72 #endif
73 return &thdb->thread_h_errno;
76 /***********************************************************************
77 * SYSDEPS_StartThread
79 * Startup routine for a new thread.
81 static void SYSDEPS_StartThread( THDB *thdb )
83 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)thdb->entry_point;
84 thdb->unix_pid = getpid();
85 SET_FS( thdb->teb_sel );
86 CLIENT_InitThread();
87 ExitThread( func( thdb->entry_arg ) );
89 #endif /* __linux__ */
92 /***********************************************************************
93 * SYSDEPS_SpawnThread
95 * Start running a new thread.
96 * Return -1 on error, 0 if OK.
98 int SYSDEPS_SpawnThread( THDB *thread )
100 #ifdef __linux__
101 if (clone( (int (*)(void *))SYSDEPS_StartThread, thread->teb.stack_top,
102 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, thread ) < 0)
103 return -1;
104 /* FIXME: close the child socket in the parent process */
105 /* close( thread->socket );*/
106 #else
107 FIXME(thread, "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 * This will crash and burn if called before threading is initialized
129 TEB * WINAPI NtCurrentTeb(void)
131 #ifdef __i386__
132 TEB *teb;
134 /* Get the TEB self-pointer */
135 __asm__( ".byte 0x64\n\tmovl (%1),%0"
136 : "=r" (teb) : "r" (&((TEB *)0)->self) );
137 return teb;
138 #else
139 return &pCurrentThread->teb;
140 #endif /* __i386__ */