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