blob: 835786d2c519dc3290e84630a999a3cedd2bf6e8 [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 Julliard50fba7f2003-11-09 01:25:23 +000041static struct wine_pthread_functions funcs;
42
43#ifndef __i386__
44static pthread_key_t teb_key;
45#endif
46
Alexandre Julliardf45325e2003-11-06 23:05:41 +000047/***********************************************************************
48 * wine_pthread_init_process
49 *
50 * Initialization for a newly created process.
51 */
52void wine_pthread_init_process( const struct wine_pthread_functions *functions )
53{
Alexandre Julliard50fba7f2003-11-09 01:25:23 +000054 memcpy( &funcs, functions, min(functions->size,sizeof(funcs)) );
Alexandre Julliardf45325e2003-11-06 23:05:41 +000055}
56
57
58/***********************************************************************
59 * wine_pthread_init_thread
60 *
61 * Initialization for a newly created thread.
62 */
Alexandre Julliard50fba7f2003-11-09 01:25:23 +000063void wine_pthread_init_thread( struct wine_pthread_thread_info *info )
Alexandre Julliardf45325e2003-11-06 23:05:41 +000064{
Alexandre Julliard50fba7f2003-11-09 01:25:23 +000065#ifdef __i386__
66 /* On the i386, the current thread is in the %fs register */
67 LDT_ENTRY fs_entry;
68
69 wine_ldt_set_base( &fs_entry, info->teb_base );
70 wine_ldt_set_limit( &fs_entry, info->teb_size - 1 );
71 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
72 wine_ldt_init_fs( info->teb_sel, &fs_entry );
73#else
74 if (!funcs.ptr_set_thread_data) /* first thread */
75 pthread_key_create( &teb_key, NULL );
76 pthread_setspecific( teb_key, info->teb_base );
77#endif
78
79 /* retrieve the stack info (except for main thread) */
80 if (funcs.ptr_set_thread_data)
81 {
Alexandre Julliard821ab862003-11-12 22:44:56 +000082#ifdef HAVE_PTHREAD_GETATTR_NP
Alexandre Julliard50fba7f2003-11-09 01:25:23 +000083 pthread_attr_t attr;
84 pthread_getattr_np( pthread_self(), &attr );
85 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
Alexandre Julliard821ab862003-11-12 22:44:56 +000086#else
87 /* assume that the stack allocation is page aligned */
88 char dummy;
89 size_t page_mask = getpagesize() - 1;
90 char *stack_top = (char *)((unsigned long)(&dummy + page_mask) & ~page_mask);
91 info->stack_base = stack_top - info->stack_size;
92#endif
Alexandre Julliard50fba7f2003-11-09 01:25:23 +000093 }
94
95 /* set pid and tid */
96 info->pid = getpid();
97 info->tid = gettid();
Alexandre Julliardf45325e2003-11-06 23:05:41 +000098}
99
100
101/***********************************************************************
102 * wine_pthread_create_thread
103 */
104int wine_pthread_create_thread( struct wine_pthread_thread_info *info )
105{
106 pthread_t id;
107 pthread_attr_t attr;
Alexandre Julliard50fba7f2003-11-09 01:25:23 +0000108 int ret = 0;
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000109
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000110 pthread_attr_init( &attr );
Alexandre Julliard50fba7f2003-11-09 01:25:23 +0000111 pthread_attr_setstacksize( &attr, info->stack_size );
112 if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info )) ret = -1;
113 pthread_attr_destroy( &attr );
114 return ret;
115}
116
117
118/***********************************************************************
119 * wine_pthread_get_current_teb
120 */
121void *wine_pthread_get_current_teb(void)
122{
123#ifdef __i386__
124 void *ret;
125 __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
126 return ret;
127#else
128 return pthread_getspecific( teb_key );
129#endif
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000130}
131
132
133/***********************************************************************
134 * wine_pthread_exit_thread
135 */
136void wine_pthread_exit_thread( struct wine_pthread_thread_info *info )
137{
138 struct cleanup_info
139 {
140 pthread_t self;
141 struct wine_pthread_thread_info thread_info;
142 };
143
144 static struct cleanup_info *previous_info;
145 struct cleanup_info *cleanup_info, *free_info;
146 void *ptr;
147
148 /* store it at the end of the TEB structure */
149 cleanup_info = (struct cleanup_info *)((char *)info->teb_base + info->teb_size) - 1;
150 cleanup_info->self = pthread_self();
151 cleanup_info->thread_info = *info;
152
153 if ((free_info = interlocked_xchg_ptr( (void **)&previous_info, cleanup_info )) != NULL)
154 {
155 pthread_join( free_info->self, &ptr );
156 wine_ldt_free_fs( free_info->thread_info.teb_sel );
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000157 munmap( free_info->thread_info.teb_base, free_info->thread_info.teb_size );
158 }
159 pthread_exit( (void *)info->exit_status );
160}
161
162
163/***********************************************************************
164 * wine_pthread_abort_thread
165 */
166void wine_pthread_abort_thread( int status )
167{
Alexandre Julliard50fba7f2003-11-09 01:25:23 +0000168 pthread_detach( pthread_self() ); /* no one will be joining with us */
Alexandre Julliardf45325e2003-11-06 23:05:41 +0000169 pthread_exit( (void *)status );
170}
171
172#endif /* HAVE_PTHREAD_H */