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 #ifdef HAVE_PTHREAD_H
43 #include <pthread.h>
44 #endif
45 #ifdef HAVE_PTHREAD_NP_H
46 #include <pthread_np.h>
47 #endif
48
49 #include "wine/library.h"
50 #include "wine/pthread.h"
51
52 #ifdef HAVE_PTHREAD_H
53
54 static int init_done;
55 static int nb_threads = 1;
56
57 #if !defined(__i386__) && !defined(__x86_64__)
58 static pthread_key_t teb_key;
59 #endif
60
61 #if defined(__x86_64__) && defined(__linux__)
62 #include <asm/prctl.h>
63 extern int arch_prctl(int func, void *ptr);
64 #endif
65
66 /***********************************************************************
67 * init_process
68 *
69 * Initialization for a newly created process.
70 */
71 static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
72 {
73 init_done = 1;
74 }
75
76
77 /***********************************************************************
78 * init_thread
79 *
80 * Initialization for a newly created thread.
81 */
82 static void init_thread( struct wine_pthread_thread_info *info )
83 {
84 /* retrieve the stack info (except for main thread) */
85 if (init_done)
86 {
87 #ifdef HAVE_PTHREAD_GETATTR_NP
88 pthread_attr_t attr;
89 pthread_getattr_np( pthread_self(), &attr );
90 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
91 pthread_attr_destroy( &attr );
92 #elif defined(HAVE_PTHREAD_ATTR_GET_NP)
93 pthread_attr_t attr;
94 pthread_attr_init( &attr );
95 pthread_attr_get_np( pthread_self(), &attr );
96 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
97 pthread_attr_destroy( &attr );
98 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
99 char dummy;
100 info->stack_size = pthread_get_stacksize_np(pthread_self());
101 info->stack_base = pthread_get_stackaddr_np(pthread_self());
102 /* if base is too large assume it's the top of the stack instead */
103 if ((char *)info->stack_base > &dummy)
104 info->stack_base = (char *)info->stack_base - info->stack_size;
105 #else
106 /* assume that the stack allocation is page aligned */
107 char dummy;
108 size_t page_size = getpagesize();
109 char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
110 info->stack_base = stack_top - info->stack_size;
111 #endif
112 }
113 }
114
115
116 /***********************************************************************
117 * create_thread
118 */
119 static int create_thread( struct wine_pthread_thread_info *info )
120 {
121 pthread_t id;
122 pthread_attr_t attr;
123 int ret = 0;
124
125 pthread_attr_init( &attr );
126 pthread_attr_setstacksize( &attr, info->stack_size );
127 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
128 pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread on Solaris */
129 interlocked_xchg_add( &nb_threads, 1 );
130 if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info ))
131 {
132 interlocked_xchg_add( &nb_threads, -1 );
133 ret = -1;
134 }
135 pthread_attr_destroy( &attr );
136 return ret;
137 }
138
139
140 /***********************************************************************
141 * init_current_teb
142 *
143 * Set the current TEB for a new thread.
144 */
145 static void init_current_teb( struct wine_pthread_thread_info *info )
146 {
147 #ifdef __i386__
148 /* On the i386, the current thread is in the %fs register */
149 LDT_ENTRY fs_entry;
150
151 wine_ldt_set_base( &fs_entry, info->teb_base );
152 wine_ldt_set_limit( &fs_entry, info->teb_size - 1 );
153 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
154 wine_ldt_init_fs( info->teb_sel, &fs_entry );
155 #elif defined(__x86_64__)
156 /* On x86_64, it's in %gs */
157 # ifdef __linux__
158 arch_prctl(ARCH_SET_GS, info->teb_base);
159 # else
160 # error Please define setting %gs for your architecture
161 # endif
162 #else
163 if (!init_done) /* first thread */
164 pthread_key_create( &teb_key, NULL );
165 pthread_setspecific( teb_key, info->teb_base );
166 #endif
167
168 /* set pid and tid */
169 info->pid = getpid();
170 #ifdef __sun
171 info->tid = pthread_self(); /* this should return the lwp id on solaris */
172 #elif defined(__APPLE__)
173 info->tid = mach_thread_self();
174 #elif defined(__FreeBSD__)
175 {
176 long lwpid;
177 thr_self( &lwpid );
178 info->tid = (int) lwpid;
179 }
180 #else
181 info->tid = gettid();
182 #endif
183 }
184
185
186 /***********************************************************************
187 * get_current_teb
188 */
189 static void *get_current_teb(void)
190 {
191 #ifdef __i386__
192 void *ret;
193 __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
194 return ret;
195 #elif defined(__x86_64__)
196 void *ret;
197 __asm__( ".byte 0x65\n\tmovq 0x30,%0" : "=r" (ret) );
198 return ret;
199 #else
200 return pthread_getspecific( teb_key );
201 #endif
202 }
203
204
205 /***********************************************************************
206 * exit_thread
207 */
208 static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
209 {
210 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) exit( info->exit_status );
211 wine_ldt_free_fs( info->teb_sel );
212 if (info->teb_size) munmap( info->teb_base, info->teb_size );
213 pthread_exit( (void *)info->exit_status );
214 }
215
216
217 /***********************************************************************
218 * abort_thread
219 */
220 static void DECLSPEC_NORETURN abort_thread( long status )
221 {
222 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) _exit( status );
223 pthread_exit( (void *)status );
224 }
225
226 #else /* HAVE_PTHREAD_H */
227
228 static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
229 {
230 }
231
232 static void init_thread( struct wine_pthread_thread_info *info )
233 {
234 }
235
236 static int create_thread( struct wine_pthread_thread_info *info )
237 {
238 return -1;
239 }
240
241 static void init_current_teb( struct wine_pthread_thread_info *info )
242 {
243 }
244
245 static void *get_current_teb(void)
246 {
247 return NULL;
248 }
249
250 static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
251 {
252 abort();
253 }
254
255 static void DECLSPEC_NORETURN abort_thread( long status )
256 {
257 abort();
258 }
259
260 static int pthread_sigmask( int how, const sigset_t *newset, sigset_t *oldset )
261 {
262 return -1;
263 }
264
265 #endif /* HAVE_PTHREAD_H */
266
267
268 /***********************************************************************
269 * pthread_functions
270 */
271 static const struct wine_pthread_functions pthread_functions =
272 {
273 init_process,
274 init_thread,
275 create_thread,
276 init_current_teb,
277 get_current_teb,
278 exit_thread,
279 abort_thread,
280 pthread_sigmask
281 };
282
283 void init_pthread_functions(void)
284 {
285 wine_pthread_set_functions( &pthread_functions, sizeof(pthread_functions) );
286 }
287
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.