1 /*
2 * Wine threading routines using the pthread library
3 *
4 * Copyright 2003 Alexandre Julliard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <signal.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_MMAN_H
33 #include <sys/mman.h>
34 #endif
35 #ifdef HAVE_MACH_MACH_H
36 #include <mach/mach.h>
37 #endif
38 #ifdef HAVE_SYS_THR_H
39 #include <sys/ucontext.h>
40 #include <sys/thr.h>
41 #endif
42
43 #include "wine/library.h"
44 #include "wine/pthread.h"
45
46 #ifdef HAVE_PTHREAD_H
47
48 static int init_done;
49 static int nb_threads = 1;
50
51 #ifndef __i386__
52 static pthread_key_t teb_key;
53 #endif
54
55 /***********************************************************************
56 * init_process
57 *
58 * Initialization for a newly created process.
59 */
60 static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
61 {
62 init_done = 1;
63 }
64
65
66 /***********************************************************************
67 * init_thread
68 *
69 * Initialization for a newly created thread.
70 */
71 static void init_thread( struct wine_pthread_thread_info *info )
72 {
73 /* retrieve the stack info (except for main thread) */
74 if (init_done)
75 {
76 #ifdef HAVE_PTHREAD_GETATTR_NP
77 pthread_attr_t attr;
78 pthread_getattr_np( pthread_self(), &attr );
79 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
80 pthread_attr_destroy( &attr );
81 #elif defined(HAVE_PTHREAD_ATTR_GET_NP)
82 pthread_attr_t attr;
83 pthread_attr_init( &attr );
84 pthread_attr_get_np( pthread_self(), &attr );
85 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
86 pthread_attr_destroy( &attr );
87 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
88 char dummy;
89 info->stack_size = pthread_get_stacksize_np(pthread_self());
90 info->stack_base = pthread_get_stackaddr_np(pthread_self());
91 /* if base is too large assume it's the top of the stack instead */
92 if ((char *)info->stack_base > &dummy)
93 info->stack_base = (char *)info->stack_base - info->stack_size;
94 #else
95 /* assume that the stack allocation is page aligned */
96 char dummy;
97 size_t page_size = getpagesize();
98 char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
99 info->stack_base = stack_top - info->stack_size;
100 #endif
101 }
102 }
103
104
105 /***********************************************************************
106 * create_thread
107 */
108 static int create_thread( struct wine_pthread_thread_info *info )
109 {
110 pthread_t id;
111 pthread_attr_t attr;
112 int ret = 0;
113
114 pthread_attr_init( &attr );
115 pthread_attr_setstacksize( &attr, info->stack_size );
116 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
117 pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread on Solaris */
118 interlocked_xchg_add( &nb_threads, 1 );
119 if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info ))
120 {
121 interlocked_xchg_add( &nb_threads, -1 );
122 ret = -1;
123 }
124 pthread_attr_destroy( &attr );
125 return ret;
126 }
127
128
129 /***********************************************************************
130 * init_current_teb
131 *
132 * Set the current TEB for a new thread.
133 */
134 static void init_current_teb( struct wine_pthread_thread_info *info )
135 {
136 #ifdef __i386__
137 /* On the i386, the current thread is in the %fs register */
138 LDT_ENTRY fs_entry;
139
140 wine_ldt_set_base( &fs_entry, info->teb_base );
141 wine_ldt_set_limit( &fs_entry, info->teb_size - 1 );
142 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
143 wine_ldt_init_fs( info->teb_sel, &fs_entry );
144 #else
145 if (!init_done) /* first thread */
146 pthread_key_create( &teb_key, NULL );
147 pthread_setspecific( teb_key, info->teb_base );
148 #endif
149
150 /* set pid and tid */
151 info->pid = getpid();
152 #ifdef __sun
153 info->tid = pthread_self(); /* this should return the lwp id on solaris */
154 #elif defined(__APPLE__)
155 info->tid = mach_thread_self();
156 #elif defined(__FreeBSD__)
157 {
158 long lwpid;
159 thr_self( &lwpid );
160 info->tid = (int) lwpid;
161 }
162 #else
163 info->tid = gettid();
164 #endif
165 }
166
167
168 /***********************************************************************
169 * get_current_teb
170 */
171 static void *get_current_teb(void)
172 {
173 #ifdef __i386__
174 void *ret;
175 __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
176 return ret;
177 #else
178 return pthread_getspecific( teb_key );
179 #endif
180 }
181
182
183 /***********************************************************************
184 * exit_thread
185 */
186 static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
187 {
188 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) exit( info->exit_status );
189 wine_ldt_free_fs( info->teb_sel );
190 if (info->teb_size) munmap( info->teb_base, info->teb_size );
191 pthread_exit( (void *)info->exit_status );
192 }
193
194
195 /***********************************************************************
196 * abort_thread
197 */
198 static void DECLSPEC_NORETURN abort_thread( long status )
199 {
200 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) _exit( status );
201 pthread_exit( (void *)status );
202 }
203
204 #else /* HAVE_PTHREAD_H */
205
206 static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
207 {
208 }
209
210 static void init_thread( struct wine_pthread_thread_info *info )
211 {
212 }
213
214 static int create_thread( struct wine_pthread_thread_info *info )
215 {
216 return -1;
217 }
218
219 static void init_current_teb( struct wine_pthread_thread_info *info )
220 {
221 }
222
223 static void *get_current_teb(void)
224 {
225 return NULL;
226 }
227
228 static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
229 {
230 abort();
231 }
232
233 static void DECLSPEC_NORETURN abort_thread( long status )
234 {
235 abort();
236 }
237
238 static int pthread_sigmask( int how, const sigset_t *newset, sigset_t *oldset )
239 {
240 return -1;
241 }
242
243 #endif /* HAVE_PTHREAD_H */
244
245
246 /***********************************************************************
247 * pthread_functions
248 */
249 const struct wine_pthread_functions pthread_functions =
250 {
251 init_process,
252 init_thread,
253 create_thread,
254 init_current_teb,
255 get_current_teb,
256 exit_thread,
257 abort_thread,
258 pthread_sigmask
259 };
260
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.