Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "wine/port.h" |
| 23 | |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 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 |
Alexandre Julliard | 150b5dc | 2006-12-29 20:02:02 +0100 | [diff] [blame] | 35 | #ifdef HAVE_MACH_MACH_H |
| 36 | #include <mach/mach.h> |
| 37 | #endif |
Tijl Coosemans | 60833da | 2007-08-08 00:25:24 +0200 | [diff] [blame] | 38 | #ifdef HAVE_SYS_THR_H |
| 39 | #include <sys/ucontext.h> |
| 40 | #include <sys/thr.h> |
| 41 | #endif |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 42 | |
| 43 | #include "wine/library.h" |
| 44 | #include "wine/pthread.h" |
| 45 | |
Alexandre Julliard | 679aab8 | 2008-04-29 20:19:02 +0200 | [diff] [blame] | 46 | #ifdef HAVE_PTHREAD_H |
| 47 | |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 48 | static int init_done; |
Alexandre Julliard | 5da3e55 | 2006-08-11 19:27:32 +0200 | [diff] [blame] | 49 | static int nb_threads = 1; |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 50 | |
Maarten Lankhorst | 0a393d7 | 2008-11-25 08:50:37 +0100 | [diff] [blame] | 51 | #if !defined(__i386__) && !defined(__x86_64__) |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 52 | static pthread_key_t teb_key; |
| 53 | #endif |
| 54 | |
Maarten Lankhorst | 0a393d7 | 2008-11-25 08:50:37 +0100 | [diff] [blame] | 55 | #if defined(__x86_64__) && defined(__linux__) |
| 56 | #include <asm/prctl.h> |
| 57 | extern int arch_prctl(int func, void *ptr); |
| 58 | #endif |
| 59 | |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 60 | /*********************************************************************** |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 61 | * init_process |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 62 | * |
| 63 | * Initialization for a newly created process. |
| 64 | */ |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 65 | static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size ) |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 66 | { |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 67 | init_done = 1; |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | |
| 71 | /*********************************************************************** |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 72 | * init_thread |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 73 | * |
| 74 | * Initialization for a newly created thread. |
| 75 | */ |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 76 | static void init_thread( struct wine_pthread_thread_info *info ) |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 77 | { |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 78 | /* retrieve the stack info (except for main thread) */ |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 79 | if (init_done) |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 80 | { |
Alexandre Julliard | 821ab86 | 2003-11-12 22:44:56 +0000 | [diff] [blame] | 81 | #ifdef HAVE_PTHREAD_GETATTR_NP |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 82 | pthread_attr_t attr; |
| 83 | pthread_getattr_np( pthread_self(), &attr ); |
| 84 | pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size ); |
Eric Pouech | 4642e1e | 2006-02-14 11:34:58 +0100 | [diff] [blame] | 85 | pthread_attr_destroy( &attr ); |
Tijl Coosemans | fc8172f | 2006-08-05 23:31:07 +0200 | [diff] [blame] | 86 | #elif defined(HAVE_PTHREAD_ATTR_GET_NP) |
| 87 | pthread_attr_t attr; |
| 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 ); |
Emmanuel Maillard | d110e1f | 2004-07-21 03:06:03 +0000 | [diff] [blame] | 92 | #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP) |
Alexandre Julliard | 538cd17 | 2006-01-24 15:11:58 +0100 | [diff] [blame] | 93 | char dummy; |
Emmanuel Maillard | d110e1f | 2004-07-21 03:06:03 +0000 | [diff] [blame] | 94 | info->stack_size = pthread_get_stacksize_np(pthread_self()); |
| 95 | info->stack_base = pthread_get_stackaddr_np(pthread_self()); |
Alexandre Julliard | 538cd17 | 2006-01-24 15:11:58 +0100 | [diff] [blame] | 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; |
Alexandre Julliard | 821ab86 | 2003-11-12 22:44:56 +0000 | [diff] [blame] | 99 | #else |
| 100 | /* assume that the stack allocation is page aligned */ |
| 101 | char dummy; |
Alexandre Julliard | b64c627 | 2004-03-30 05:13:35 +0000 | [diff] [blame] | 102 | size_t page_size = getpagesize(); |
| 103 | char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size; |
Alexandre Julliard | 821ab86 | 2003-11-12 22:44:56 +0000 | [diff] [blame] | 104 | info->stack_base = stack_top - info->stack_size; |
| 105 | #endif |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 106 | } |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | /*********************************************************************** |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 111 | * create_thread |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 112 | */ |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 113 | static int create_thread( struct wine_pthread_thread_info *info ) |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 114 | { |
| 115 | pthread_t id; |
| 116 | pthread_attr_t attr; |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 117 | int ret = 0; |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 118 | |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 119 | pthread_attr_init( &attr ); |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 120 | pthread_attr_setstacksize( &attr, info->stack_size ); |
Alexandre Julliard | a4d1826 | 2004-02-11 23:58:46 +0000 | [diff] [blame] | 121 | pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED ); |
Alexandre Julliard | c6ad020 | 2006-06-23 12:26:23 +0200 | [diff] [blame] | 122 | pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread on Solaris */ |
Alexandre Julliard | 5da3e55 | 2006-08-11 19:27:32 +0200 | [diff] [blame] | 123 | interlocked_xchg_add( &nb_threads, 1 ); |
| 124 | if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info )) |
| 125 | { |
| 126 | interlocked_xchg_add( &nb_threads, -1 ); |
| 127 | ret = -1; |
| 128 | } |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 129 | pthread_attr_destroy( &attr ); |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /*********************************************************************** |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 135 | * init_current_teb |
Alexandre Julliard | 4fac95d | 2004-02-05 02:01:35 +0000 | [diff] [blame] | 136 | * |
| 137 | * Set the current TEB for a new thread. |
| 138 | */ |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 139 | static void init_current_teb( struct wine_pthread_thread_info *info ) |
Alexandre Julliard | 4fac95d | 2004-02-05 02:01:35 +0000 | [diff] [blame] | 140 | { |
| 141 | #ifdef __i386__ |
| 142 | /* On the i386, the current thread is in the %fs register */ |
| 143 | LDT_ENTRY fs_entry; |
| 144 | |
| 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 ); |
Maarten Lankhorst | 0a393d7 | 2008-11-25 08:50:37 +0100 | [diff] [blame] | 149 | #elif defined(__x86_64__) |
| 150 | /* On x86_64, it's in %gs */ |
| 151 | # ifdef __linux__ |
| 152 | arch_prctl(ARCH_SET_GS, info->teb_base); |
| 153 | # else |
| 154 | # error Please define setting %gs for your architecture |
| 155 | # endif |
Alexandre Julliard | 4fac95d | 2004-02-05 02:01:35 +0000 | [diff] [blame] | 156 | #else |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 157 | if (!init_done) /* first thread */ |
Alexandre Julliard | 4fac95d | 2004-02-05 02:01:35 +0000 | [diff] [blame] | 158 | pthread_key_create( &teb_key, NULL ); |
| 159 | pthread_setspecific( teb_key, info->teb_base ); |
| 160 | #endif |
| 161 | |
| 162 | /* set pid and tid */ |
| 163 | info->pid = getpid(); |
Alexandre Julliard | c6ad020 | 2006-06-23 12:26:23 +0200 | [diff] [blame] | 164 | #ifdef __sun |
| 165 | info->tid = pthread_self(); /* this should return the lwp id on solaris */ |
Alexandre Julliard | 150b5dc | 2006-12-29 20:02:02 +0100 | [diff] [blame] | 166 | #elif defined(__APPLE__) |
| 167 | info->tid = mach_thread_self(); |
Tijl Coosemans | 60833da | 2007-08-08 00:25:24 +0200 | [diff] [blame] | 168 | #elif defined(__FreeBSD__) |
| 169 | { |
| 170 | long lwpid; |
| 171 | thr_self( &lwpid ); |
| 172 | info->tid = (int) lwpid; |
| 173 | } |
Alexandre Julliard | c6ad020 | 2006-06-23 12:26:23 +0200 | [diff] [blame] | 174 | #else |
Alexandre Julliard | 4fac95d | 2004-02-05 02:01:35 +0000 | [diff] [blame] | 175 | info->tid = gettid(); |
Alexandre Julliard | c6ad020 | 2006-06-23 12:26:23 +0200 | [diff] [blame] | 176 | #endif |
Alexandre Julliard | 4fac95d | 2004-02-05 02:01:35 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | |
| 180 | /*********************************************************************** |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 181 | * get_current_teb |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 182 | */ |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 183 | static void *get_current_teb(void) |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 184 | { |
| 185 | #ifdef __i386__ |
| 186 | void *ret; |
| 187 | __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) ); |
| 188 | return ret; |
Maarten Lankhorst | 0a393d7 | 2008-11-25 08:50:37 +0100 | [diff] [blame] | 189 | #elif defined(__x86_64__) |
| 190 | void *ret; |
| 191 | __asm__( ".byte 0x65\n\tmovq 0x30,%0" : "=r" (ret) ); |
| 192 | return ret; |
Alexandre Julliard | 50fba7f | 2003-11-09 01:25:23 +0000 | [diff] [blame] | 193 | #else |
| 194 | return pthread_getspecific( teb_key ); |
| 195 | #endif |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | /*********************************************************************** |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 200 | * exit_thread |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 201 | */ |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 202 | static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info ) |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 203 | { |
Alexandre Julliard | 5da3e55 | 2006-08-11 19:27:32 +0200 | [diff] [blame] | 204 | if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) exit( info->exit_status ); |
Alexandre Julliard | a4d1826 | 2004-02-11 23:58:46 +0000 | [diff] [blame] | 205 | wine_ldt_free_fs( info->teb_sel ); |
Alexandre Julliard | c79cf02 | 2006-07-13 20:47:07 +0200 | [diff] [blame] | 206 | if (info->teb_size) munmap( info->teb_base, info->teb_size ); |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 207 | pthread_exit( (void *)info->exit_status ); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /*********************************************************************** |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 212 | * abort_thread |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 213 | */ |
Alexandre Julliard | 7e4c88c | 2005-09-13 11:07:14 +0000 | [diff] [blame] | 214 | static void DECLSPEC_NORETURN abort_thread( long status ) |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 215 | { |
Alexandre Julliard | 5da3e55 | 2006-08-11 19:27:32 +0200 | [diff] [blame] | 216 | if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) _exit( status ); |
Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 217 | pthread_exit( (void *)status ); |
| 218 | } |
| 219 | |
Alexandre Julliard | 679aab8 | 2008-04-29 20:19:02 +0200 | [diff] [blame] | 220 | #else /* HAVE_PTHREAD_H */ |
| 221 | |
| 222 | static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size ) |
| 223 | { |
| 224 | } |
| 225 | |
| 226 | static void init_thread( struct wine_pthread_thread_info *info ) |
| 227 | { |
| 228 | } |
| 229 | |
| 230 | static int create_thread( struct wine_pthread_thread_info *info ) |
| 231 | { |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | static void init_current_teb( struct wine_pthread_thread_info *info ) |
| 236 | { |
| 237 | } |
| 238 | |
| 239 | static void *get_current_teb(void) |
| 240 | { |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info ) |
| 245 | { |
| 246 | abort(); |
| 247 | } |
| 248 | |
| 249 | static void DECLSPEC_NORETURN abort_thread( long status ) |
| 250 | { |
| 251 | abort(); |
| 252 | } |
| 253 | |
| 254 | static int pthread_sigmask( int how, const sigset_t *newset, sigset_t *oldset ) |
| 255 | { |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | #endif /* HAVE_PTHREAD_H */ |
| 260 | |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 261 | |
| 262 | /*********************************************************************** |
| 263 | * pthread_functions |
| 264 | */ |
Alexandre Julliard | 17ab587 | 2008-11-07 11:11:21 +0100 | [diff] [blame] | 265 | static const struct wine_pthread_functions pthread_functions = |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 266 | { |
| 267 | init_process, |
| 268 | init_thread, |
| 269 | create_thread, |
| 270 | init_current_teb, |
| 271 | get_current_teb, |
| 272 | exit_thread, |
Alexandre Julliard | e040e6c | 2006-06-23 12:27:45 +0200 | [diff] [blame] | 273 | abort_thread, |
| 274 | pthread_sigmask |
Alexandre Julliard | 883d3c5 | 2005-09-03 15:11:29 +0000 | [diff] [blame] | 275 | }; |
Alexandre Julliard | 17ab587 | 2008-11-07 11:11:21 +0100 | [diff] [blame] | 276 | |
| 277 | void init_pthread_functions(void) |
| 278 | { |
| 279 | wine_pthread_set_functions( &pthread_functions, sizeof(pthread_functions) ); |
| 280 | } |