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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_MMAN_H
38 #include "wine/library.h"
39 #include "wine/pthread.h"
41 static struct wine_pthread_functions funcs
;
44 static pthread_key_t teb_key
;
47 static inline int gettid(void)
49 #if defined(__linux__) && defined(__i386__)
51 __asm__("int $0x80" : "=a" (ret
) : "0" (224) /* SYS_gettid */);
52 if (ret
< 0) ret
= -1;
55 return -1; /* FIXME */
60 /***********************************************************************
61 * wine_pthread_init_process
63 * Initialization for a newly created process.
65 void wine_pthread_init_process( const struct wine_pthread_functions
*functions
)
67 memcpy( &funcs
, functions
, min(functions
->size
,sizeof(funcs
)) );
71 /***********************************************************************
72 * wine_pthread_init_thread
74 * Initialization for a newly created thread.
76 void wine_pthread_init_thread( struct wine_pthread_thread_info
*info
)
79 /* On the i386, the current thread is in the %fs register */
82 wine_ldt_set_base( &fs_entry
, info
->teb_base
);
83 wine_ldt_set_limit( &fs_entry
, info
->teb_size
- 1 );
84 wine_ldt_set_flags( &fs_entry
, WINE_LDT_FLAGS_DATA
|WINE_LDT_FLAGS_32BIT
);
85 wine_ldt_init_fs( info
->teb_sel
, &fs_entry
);
87 if (!funcs
.ptr_set_thread_data
) /* first thread */
88 pthread_key_create( &teb_key
, NULL
);
89 pthread_setspecific( teb_key
, info
->teb_base
);
92 /* retrieve the stack info (except for main thread) */
93 if (funcs
.ptr_set_thread_data
)
96 pthread_getattr_np( pthread_self(), &attr
);
97 pthread_attr_getstack( &attr
, &info
->stack_base
, &info
->stack_size
);
100 /* set pid and tid */
101 info
->pid
= getpid();
102 info
->tid
= gettid();
106 /***********************************************************************
107 * wine_pthread_create_thread
109 int wine_pthread_create_thread( struct wine_pthread_thread_info
*info
)
115 pthread_attr_init( &attr
);
116 pthread_attr_setstacksize( &attr
, info
->stack_size
);
117 if (pthread_create( &id
, &attr
, (void * (*)(void *))info
->entry
, info
)) ret
= -1;
118 pthread_attr_destroy( &attr
);
123 /***********************************************************************
124 * wine_pthread_get_current_teb
126 void *wine_pthread_get_current_teb(void)
130 __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret
) );
133 return pthread_getspecific( teb_key
);
138 /***********************************************************************
139 * wine_pthread_exit_thread
141 void wine_pthread_exit_thread( struct wine_pthread_thread_info
*info
)
146 struct wine_pthread_thread_info thread_info
;
149 static struct cleanup_info
*previous_info
;
150 struct cleanup_info
*cleanup_info
, *free_info
;
153 /* store it at the end of the TEB structure */
154 cleanup_info
= (struct cleanup_info
*)((char *)info
->teb_base
+ info
->teb_size
) - 1;
155 cleanup_info
->self
= pthread_self();
156 cleanup_info
->thread_info
= *info
;
158 if ((free_info
= interlocked_xchg_ptr( (void **)&previous_info
, cleanup_info
)) != NULL
)
160 pthread_join( free_info
->self
, &ptr
);
161 wine_ldt_free_fs( free_info
->thread_info
.teb_sel
);
162 munmap( free_info
->thread_info
.teb_base
, free_info
->thread_info
.teb_size
);
164 pthread_exit( (void *)info
->exit_status
);
168 /***********************************************************************
169 * wine_pthread_abort_thread
171 void wine_pthread_abort_thread( int status
)
173 pthread_detach( pthread_self() ); /* no one will be joining with us */
174 pthread_exit( (void *)status
);
177 #endif /* HAVE_PTHREAD_H */