blob: 87ef2e839ecf84d964e9942c23cc19d6195cb732 [file] [log] [blame]
Alexandre Julliardf45325e2003-11-06 23:05:41 +00001/*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "config.h"
22#include "wine/port.h"
23
24#ifdef HAVE_PTHREAD_H
25
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
38#include "wine/library.h"
39#include "wine/pthread.h"
40
Alexandre Julliard883d3c52005-09-03 15:11:29 +000041static int init_done;
Alexandre Julliard50fba7f2003-11-09 01:25:23 +000042
43#ifndef __i386__
44static pthread_key_t teb_key;
45#endif
46
Alexandre Julliardf45325e2003-11-06 23:05:41 +000047/***********************************************************************
Alexandre Julliard883d3c52005-09-03 15:11:29 +000048 * init_process
Alexandre Julliardf45325e2003-11-06 23:05:41 +000049 *
50 * Initialization for a newly created process.
51 */
Alexandre Julliard883d3c52005-09-03 15:11:29 +000052static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
Alexandre Julliardf45325e2003-11-06 23:05:41 +000053{
Alexandre Julliard883d3c52005-09-03 15:11:29 +000054 init_done = 1;
Alexandre Julliardf45325e2003-11-06 23:05:41 +000055}
56
57
58/***********************************************************************
Alexandre Julliard883d3c52005-09-03 15:11:29 +000059 * init_thread
Alexandre Julliardf45325e2003-11-06 23:05:41 +000060 *
61 * Initialization for a newly created thread.
62 */
Alexandre Julliard883d3c52005-09-03 15:11:29 +000063static void init_thread( struct wine_pthread_thread_info *info )
Alexandre Julliardf45325e2003-11-06 23:05:41 +000064{
Alexandre Julliard50fba7f2003-11-09 01:25:23 +000065 /* retrieve the stack info (except for main thread) */
Alexandre Julliard883d3c52005-09-03 15:11:29 +000066 if (init_done)
Alexandre Julliard50fba7f2003-11-09 01:25:23 +000067 {
Alexandre Julliard821ab862003-11-12 22:44:56 +000068#ifdef HAVE_PTHREAD_GETATTR_NP
Alexandre Julliard50fba7f2003-11-09 01:25:23 +000069 pthread_attr_t attr;
70 pthread_getattr_np( pthread_self(), &attr );
71 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
Eric Pouech4642e1e2006-02-14 11:34:58 +010072 pthread_attr_destroy( &attr );
Emmanuel Maillardd110e1f2004-07-21 03:06:03 +000073#elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
Alexandre Julliard538cd172006-01-24 15:11:58 +010074 char dummy;
Emmanuel Maillardd110e1f2004-07-21 03:06:03 +000075 info->stack_size = pthread_get_stacksize_np(pthread_self());
76 info->stack_base = pthread_get_stackaddr_np(pthread_self());
Alexandre Julliard538cd172006-01-24 15:11:58 +010077 /* if base is too large assume it's the top of the stack instead */
78 if ((char *)info->stack_base > &dummy)
79 info->stack_base = (char *)info->stack_base - info->stack_size;
Alexandre Julliard821ab862003-11-12 22:44:56 +000080#else
81 /* assume that the stack allocation is page aligned */
82 char dummy;
Alexandre Julliardb64c6272004-03-30 05:13:35 +000083 size_t page_size = getpagesize();
84 char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
Alexandre Julliard821ab862003-11-12 22:44:56 +000085 info->stack_base = stack_top - info->stack_size;
86#endif
Alexandre Julliard50fba7f2003-11-09 01:25:23 +000087 }
Alexandre Julliardf45325e2003-11-06 23:05:41 +000088}
89
90
91/***********************************************************************
Alexandre Julliard883d3c52005-09-03 15:11:29 +000092 * create_thread
Alexandre Julliardf45325e2003-11-06 23:05:41 +000093 */
Alexandre Julliard883d3c52005-09-03 15:11:29 +000094static int create_thread( struct wine_pthread_thread_info *info )
Alexandre Julliardf45325e2003-11-06 23:05:41 +000095{
96 pthread_t id;
97 pthread_attr_t attr;
Alexandre Julliard50fba7f2003-11-09 01:25:23 +000098 int ret = 0;
Alexandre Julliardf45325e2003-11-06 23:05:41 +000099
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000100 pthread_attr_init( &attr );
Alexandre Julliard50fba7f2003-11-09 01:25:23 +0000101 pthread_attr_setstacksize( &attr, info->stack_size );
Alexandre Julliarda4d18262004-02-11 23:58:46 +0000102 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
Alexandre Julliard50fba7f2003-11-09 01:25:23 +0000103 if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info )) ret = -1;
104 pthread_attr_destroy( &attr );
105 return ret;
106}
107
108
109/***********************************************************************
Alexandre Julliard883d3c52005-09-03 15:11:29 +0000110 * init_current_teb
Alexandre Julliard4fac95d2004-02-05 02:01:35 +0000111 *
112 * Set the current TEB for a new thread.
113 */
Alexandre Julliard883d3c52005-09-03 15:11:29 +0000114static void init_current_teb( struct wine_pthread_thread_info *info )
Alexandre Julliard4fac95d2004-02-05 02:01:35 +0000115{
116#ifdef __i386__
117 /* On the i386, the current thread is in the %fs register */
118 LDT_ENTRY fs_entry;
119
120 wine_ldt_set_base( &fs_entry, info->teb_base );
121 wine_ldt_set_limit( &fs_entry, info->teb_size - 1 );
122 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
123 wine_ldt_init_fs( info->teb_sel, &fs_entry );
124#else
Alexandre Julliard883d3c52005-09-03 15:11:29 +0000125 if (!init_done) /* first thread */
Alexandre Julliard4fac95d2004-02-05 02:01:35 +0000126 pthread_key_create( &teb_key, NULL );
127 pthread_setspecific( teb_key, info->teb_base );
128#endif
129
130 /* set pid and tid */
131 info->pid = getpid();
132 info->tid = gettid();
133}
134
135
136/***********************************************************************
Alexandre Julliard883d3c52005-09-03 15:11:29 +0000137 * get_current_teb
Alexandre Julliard50fba7f2003-11-09 01:25:23 +0000138 */
Alexandre Julliard883d3c52005-09-03 15:11:29 +0000139static void *get_current_teb(void)
Alexandre Julliard50fba7f2003-11-09 01:25:23 +0000140{
141#ifdef __i386__
142 void *ret;
143 __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
144 return ret;
145#else
146 return pthread_getspecific( teb_key );
147#endif
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000148}
149
150
151/***********************************************************************
Alexandre Julliard883d3c52005-09-03 15:11:29 +0000152 * exit_thread
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000153 */
Alexandre Julliard883d3c52005-09-03 15:11:29 +0000154static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000155{
Alexandre Julliarda4d18262004-02-11 23:58:46 +0000156 wine_ldt_free_fs( info->teb_sel );
157 munmap( info->teb_base, info->teb_size );
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000158 pthread_exit( (void *)info->exit_status );
159}
160
161
162/***********************************************************************
Alexandre Julliard883d3c52005-09-03 15:11:29 +0000163 * abort_thread
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000164 */
Alexandre Julliard7e4c88c2005-09-13 11:07:14 +0000165static void DECLSPEC_NORETURN abort_thread( long status )
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000166{
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000167 pthread_exit( (void *)status );
168}
169
Alexandre Julliard883d3c52005-09-03 15:11:29 +0000170
171/***********************************************************************
172 * pthread_functions
173 */
174const struct wine_pthread_functions pthread_functions =
175{
176 init_process,
177 init_thread,
178 create_thread,
179 init_current_teb,
180 get_current_teb,
181 exit_thread,
182 abort_thread
183};
184
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000185#endif /* HAVE_PTHREAD_H */