comdlg32: Move GetFileTitle(A/W/16) to filedlg.c, filedlg16.c.
[wine/wine-kai.git] / loader / pthread.c
blob17be9e43fa790c1e17545847e00d5cfb64c850dd
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #ifdef HAVE_PTHREAD_H
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_MMAN_H
35 #include <sys/mman.h>
36 #endif
37 #ifdef HAVE_MACH_MACH_H
38 #include <mach/mach.h>
39 #endif
41 #include "wine/library.h"
42 #include "wine/pthread.h"
44 static int init_done;
45 static int nb_threads = 1;
47 #ifndef __i386__
48 static pthread_key_t teb_key;
49 #endif
51 /***********************************************************************
52 * init_process
54 * Initialization for a newly created process.
56 static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
58 init_done = 1;
62 /***********************************************************************
63 * init_thread
65 * Initialization for a newly created thread.
67 static void init_thread( struct wine_pthread_thread_info *info )
69 /* retrieve the stack info (except for main thread) */
70 if (init_done)
72 #ifdef HAVE_PTHREAD_GETATTR_NP
73 pthread_attr_t attr;
74 pthread_getattr_np( pthread_self(), &attr );
75 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
76 pthread_attr_destroy( &attr );
77 #elif defined(HAVE_PTHREAD_ATTR_GET_NP)
78 pthread_attr_t attr;
79 pthread_attr_init( &attr );
80 pthread_attr_get_np( pthread_self(), &attr );
81 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
82 pthread_attr_destroy( &attr );
83 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
84 char dummy;
85 info->stack_size = pthread_get_stacksize_np(pthread_self());
86 info->stack_base = pthread_get_stackaddr_np(pthread_self());
87 /* if base is too large assume it's the top of the stack instead */
88 if ((char *)info->stack_base > &dummy)
89 info->stack_base = (char *)info->stack_base - info->stack_size;
90 #else
91 /* assume that the stack allocation is page aligned */
92 char dummy;
93 size_t page_size = getpagesize();
94 char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
95 info->stack_base = stack_top - info->stack_size;
96 #endif
101 /***********************************************************************
102 * create_thread
104 static int create_thread( struct wine_pthread_thread_info *info )
106 pthread_t id;
107 pthread_attr_t attr;
108 int ret = 0;
110 pthread_attr_init( &attr );
111 pthread_attr_setstacksize( &attr, info->stack_size );
112 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
113 pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread on Solaris */
114 interlocked_xchg_add( &nb_threads, 1 );
115 if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info ))
117 interlocked_xchg_add( &nb_threads, -1 );
118 ret = -1;
120 pthread_attr_destroy( &attr );
121 return ret;
125 /***********************************************************************
126 * init_current_teb
128 * Set the current TEB for a new thread.
130 static void init_current_teb( struct wine_pthread_thread_info *info )
132 #ifdef __i386__
133 /* On the i386, the current thread is in the %fs register */
134 LDT_ENTRY fs_entry;
136 wine_ldt_set_base( &fs_entry, info->teb_base );
137 wine_ldt_set_limit( &fs_entry, info->teb_size - 1 );
138 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
139 wine_ldt_init_fs( info->teb_sel, &fs_entry );
140 #else
141 if (!init_done) /* first thread */
142 pthread_key_create( &teb_key, NULL );
143 pthread_setspecific( teb_key, info->teb_base );
144 #endif
146 /* set pid and tid */
147 info->pid = getpid();
148 #ifdef __sun
149 info->tid = pthread_self(); /* this should return the lwp id on solaris */
150 #elif defined(__APPLE__)
151 info->tid = mach_thread_self();
152 #else
153 info->tid = gettid();
154 #endif
158 /***********************************************************************
159 * get_current_teb
161 static void *get_current_teb(void)
163 #ifdef __i386__
164 void *ret;
165 __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
166 return ret;
167 #else
168 return pthread_getspecific( teb_key );
169 #endif
173 /***********************************************************************
174 * exit_thread
176 static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
178 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) exit( info->exit_status );
179 wine_ldt_free_fs( info->teb_sel );
180 if (info->teb_size) munmap( info->teb_base, info->teb_size );
181 pthread_exit( (void *)info->exit_status );
185 /***********************************************************************
186 * abort_thread
188 static void DECLSPEC_NORETURN abort_thread( long status )
190 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) _exit( status );
191 pthread_exit( (void *)status );
195 /***********************************************************************
196 * pthread_functions
198 const struct wine_pthread_functions pthread_functions =
200 init_process,
201 init_thread,
202 create_thread,
203 init_current_teb,
204 get_current_teb,
205 exit_thread,
206 abort_thread,
207 pthread_sigmask
210 #endif /* HAVE_PTHREAD_H */