comctl32: Add missing '\n' to ok() call.
[wine/dibdrv.git] / loader / pthread.c
blob9a7834e5ba1ca5bcdaf138c9e5b4ec72c603ec27
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
38 #include "wine/library.h"
39 #include "wine/pthread.h"
41 static int init_done;
42 static int nb_threads = 1;
44 #ifndef __i386__
45 static pthread_key_t teb_key;
46 #endif
48 /***********************************************************************
49 * init_process
51 * Initialization for a newly created process.
53 static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
55 init_done = 1;
59 /***********************************************************************
60 * init_thread
62 * Initialization for a newly created thread.
64 static void init_thread( struct wine_pthread_thread_info *info )
66 /* retrieve the stack info (except for main thread) */
67 if (init_done)
69 #ifdef HAVE_PTHREAD_GETATTR_NP
70 pthread_attr_t attr;
71 pthread_getattr_np( pthread_self(), &attr );
72 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
73 pthread_attr_destroy( &attr );
74 #elif defined(HAVE_PTHREAD_ATTR_GET_NP)
75 pthread_attr_t attr;
76 pthread_attr_init( &attr );
77 pthread_attr_get_np( pthread_self(), &attr );
78 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
79 pthread_attr_destroy( &attr );
80 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
81 char dummy;
82 info->stack_size = pthread_get_stacksize_np(pthread_self());
83 info->stack_base = pthread_get_stackaddr_np(pthread_self());
84 /* if base is too large assume it's the top of the stack instead */
85 if ((char *)info->stack_base > &dummy)
86 info->stack_base = (char *)info->stack_base - info->stack_size;
87 #else
88 /* assume that the stack allocation is page aligned */
89 char dummy;
90 size_t page_size = getpagesize();
91 char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
92 info->stack_base = stack_top - info->stack_size;
93 #endif
98 /***********************************************************************
99 * create_thread
101 static int create_thread( struct wine_pthread_thread_info *info )
103 pthread_t id;
104 pthread_attr_t attr;
105 int ret = 0;
107 pthread_attr_init( &attr );
108 pthread_attr_setstacksize( &attr, info->stack_size );
109 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
110 pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread on Solaris */
111 interlocked_xchg_add( &nb_threads, 1 );
112 if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info ))
114 interlocked_xchg_add( &nb_threads, -1 );
115 ret = -1;
117 pthread_attr_destroy( &attr );
118 return ret;
122 /***********************************************************************
123 * init_current_teb
125 * Set the current TEB for a new thread.
127 static void init_current_teb( struct wine_pthread_thread_info *info )
129 #ifdef __i386__
130 /* On the i386, the current thread is in the %fs register */
131 LDT_ENTRY fs_entry;
133 wine_ldt_set_base( &fs_entry, info->teb_base );
134 wine_ldt_set_limit( &fs_entry, info->teb_size - 1 );
135 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
136 wine_ldt_init_fs( info->teb_sel, &fs_entry );
137 #else
138 if (!init_done) /* first thread */
139 pthread_key_create( &teb_key, NULL );
140 pthread_setspecific( teb_key, info->teb_base );
141 #endif
143 /* set pid and tid */
144 info->pid = getpid();
145 #ifdef __sun
146 info->tid = pthread_self(); /* this should return the lwp id on solaris */
147 #else
148 info->tid = gettid();
149 #endif
153 /***********************************************************************
154 * get_current_teb
156 static void *get_current_teb(void)
158 #ifdef __i386__
159 void *ret;
160 __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
161 return ret;
162 #else
163 return pthread_getspecific( teb_key );
164 #endif
168 /***********************************************************************
169 * exit_thread
171 static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
173 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) exit( info->exit_status );
174 wine_ldt_free_fs( info->teb_sel );
175 if (info->teb_size) munmap( info->teb_base, info->teb_size );
176 pthread_exit( (void *)info->exit_status );
180 /***********************************************************************
181 * abort_thread
183 static void DECLSPEC_NORETURN abort_thread( long status )
185 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) _exit( status );
186 pthread_exit( (void *)status );
190 /***********************************************************************
191 * pthread_functions
193 const struct wine_pthread_functions pthread_functions =
195 init_process,
196 init_thread,
197 create_thread,
198 init_current_teb,
199 get_current_teb,
200 exit_thread,
201 abort_thread,
202 pthread_sigmask
205 #endif /* HAVE_PTHREAD_H */