2 * Wine threading routines using the pthread library
4 * Copyright 2003 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_MMAN_H
35 #ifdef HAVE_MACH_MACH_H
36 #include <mach/mach.h>
39 #include <sys/ucontext.h>
43 #include "wine/library.h"
44 #include "wine/pthread.h"
49 static int nb_threads = 1;
51 #if !defined(__i386__) && !defined(__x86_64__)
52 static pthread_key_t teb_key;
55 #if defined(__x86_64__) && defined(__linux__)
56 #include <asm/prctl.h>
57 extern int arch_prctl(int func, void *ptr);
60 /***********************************************************************
63 * Initialization for a newly created process.
65 static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
71 /***********************************************************************
74 * Initialization for a newly created thread.
76 static void init_thread( struct wine_pthread_thread_info *info )
78 /* retrieve the stack info (except for main thread) */
81 #ifdef HAVE_PTHREAD_GETATTR_NP
83 pthread_getattr_np( pthread_self(), &attr );
84 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
85 pthread_attr_destroy( &attr );
86 #elif defined(HAVE_PTHREAD_ATTR_GET_NP)
88 pthread_attr_init( &attr );
89 pthread_attr_get_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_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
94 info->stack_size = pthread_get_stacksize_np(pthread_self());
95 info->stack_base = pthread_get_stackaddr_np(pthread_self());
96 /* if base is too large assume it's the top of the stack instead */
97 if ((char *)info->stack_base > &dummy)
98 info->stack_base = (char *)info->stack_base - info->stack_size;
100 /* assume that the stack allocation is page aligned */
102 size_t page_size = getpagesize();
103 char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
104 info->stack_base = stack_top - info->stack_size;
110 /***********************************************************************
113 static int create_thread( struct wine_pthread_thread_info *info )
119 pthread_attr_init( &attr );
120 pthread_attr_setstacksize( &attr, info->stack_size );
121 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
122 pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread on Solaris */
123 interlocked_xchg_add( &nb_threads, 1 );
124 if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info ))
126 interlocked_xchg_add( &nb_threads, -1 );
129 pthread_attr_destroy( &attr );
134 /***********************************************************************
137 * Set the current TEB for a new thread.
139 static void init_current_teb( struct wine_pthread_thread_info *info )
142 /* On the i386, the current thread is in the %fs register */
145 wine_ldt_set_base( &fs_entry, info->teb_base );
146 wine_ldt_set_limit( &fs_entry, info->teb_size - 1 );
147 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
148 wine_ldt_init_fs( info->teb_sel, &fs_entry );
149 #elif defined(__x86_64__)
150 /* On x86_64, it's in %gs */
152 arch_prctl(ARCH_SET_GS, info->teb_base);
154 # error Please define setting %gs for your architecture
157 if (!init_done) /* first thread */
158 pthread_key_create( &teb_key, NULL );
159 pthread_setspecific( teb_key, info->teb_base );
162 /* set pid and tid */
163 info->pid = getpid();
165 info->tid = pthread_self(); /* this should return the lwp id on solaris */
166 #elif defined(__APPLE__)
167 info->tid = mach_thread_self();
168 #elif defined(__FreeBSD__)
172 info->tid = (int) lwpid;
175 info->tid = gettid();
180 /***********************************************************************
183 static void *get_current_teb(void)
187 __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
189 #elif defined(__x86_64__)
191 __asm__( ".byte 0x65\n\tmovq 0x30,%0" : "=r" (ret) );
194 return pthread_getspecific( teb_key );
199 /***********************************************************************
202 static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
204 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) exit( info->exit_status );
205 wine_ldt_free_fs( info->teb_sel );
206 if (info->teb_size) munmap( info->teb_base, info->teb_size );
207 pthread_exit( (void *)info->exit_status );
211 /***********************************************************************
214 static void DECLSPEC_NORETURN abort_thread( long status )
216 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) _exit( status );
217 pthread_exit( (void *)status );
220 #else /* HAVE_PTHREAD_H */
222 static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
226 static void init_thread( struct wine_pthread_thread_info *info )
230 static int create_thread( struct wine_pthread_thread_info *info )
235 static void init_current_teb( struct wine_pthread_thread_info *info )
239 static void *get_current_teb(void)
244 static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
249 static void DECLSPEC_NORETURN abort_thread( long status )
254 static int pthread_sigmask( int how, const sigset_t *newset, sigset_t *oldset )
259 #endif /* HAVE_PTHREAD_H */
262 /***********************************************************************
265 static const struct wine_pthread_functions pthread_functions =
277 void init_pthread_functions(void)
279 wine_pthread_set_functions( &pthread_functions, sizeof(pthread_functions) );