2003-11-07 00:05:41 +01:00
|
|
|
/*
|
|
|
|
* Wine threading routines using the pthread library
|
|
|
|
*
|
|
|
|
* Copyright 2003 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-11-07 00:05:41 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_PTHREAD_H
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wine/library.h"
|
|
|
|
#include "wine/pthread.h"
|
|
|
|
|
2005-09-03 17:11:29 +02:00
|
|
|
static int init_done;
|
2003-11-09 02:25:23 +01:00
|
|
|
|
|
|
|
#ifndef __i386__
|
|
|
|
static pthread_key_t teb_key;
|
|
|
|
#endif
|
|
|
|
|
2003-11-07 00:05:41 +01:00
|
|
|
/***********************************************************************
|
2005-09-03 17:11:29 +02:00
|
|
|
* init_process
|
2003-11-07 00:05:41 +01:00
|
|
|
*
|
|
|
|
* Initialization for a newly created process.
|
|
|
|
*/
|
2005-09-03 17:11:29 +02:00
|
|
|
static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
|
2003-11-07 00:05:41 +01:00
|
|
|
{
|
2005-09-03 17:11:29 +02:00
|
|
|
init_done = 1;
|
2003-11-07 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2005-09-03 17:11:29 +02:00
|
|
|
* init_thread
|
2003-11-07 00:05:41 +01:00
|
|
|
*
|
|
|
|
* Initialization for a newly created thread.
|
|
|
|
*/
|
2005-09-03 17:11:29 +02:00
|
|
|
static void init_thread( struct wine_pthread_thread_info *info )
|
2003-11-07 00:05:41 +01:00
|
|
|
{
|
2003-11-09 02:25:23 +01:00
|
|
|
/* retrieve the stack info (except for main thread) */
|
2005-09-03 17:11:29 +02:00
|
|
|
if (init_done)
|
2003-11-09 02:25:23 +01:00
|
|
|
{
|
2003-11-12 23:44:56 +01:00
|
|
|
#ifdef HAVE_PTHREAD_GETATTR_NP
|
2003-11-09 02:25:23 +01:00
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_getattr_np( pthread_self(), &attr );
|
|
|
|
pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
|
2006-02-14 11:34:58 +01:00
|
|
|
pthread_attr_destroy( &attr );
|
2004-07-21 05:06:03 +02:00
|
|
|
#elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
|
2006-01-24 15:11:58 +01:00
|
|
|
char dummy;
|
2004-07-21 05:06:03 +02:00
|
|
|
info->stack_size = pthread_get_stacksize_np(pthread_self());
|
|
|
|
info->stack_base = pthread_get_stackaddr_np(pthread_self());
|
2006-01-24 15:11:58 +01:00
|
|
|
/* if base is too large assume it's the top of the stack instead */
|
|
|
|
if ((char *)info->stack_base > &dummy)
|
|
|
|
info->stack_base = (char *)info->stack_base - info->stack_size;
|
2003-11-12 23:44:56 +01:00
|
|
|
#else
|
|
|
|
/* assume that the stack allocation is page aligned */
|
|
|
|
char dummy;
|
2004-03-30 07:13:35 +02:00
|
|
|
size_t page_size = getpagesize();
|
|
|
|
char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
|
2003-11-12 23:44:56 +01:00
|
|
|
info->stack_base = stack_top - info->stack_size;
|
|
|
|
#endif
|
2003-11-09 02:25:23 +01:00
|
|
|
}
|
2003-11-07 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2005-09-03 17:11:29 +02:00
|
|
|
* create_thread
|
2003-11-07 00:05:41 +01:00
|
|
|
*/
|
2005-09-03 17:11:29 +02:00
|
|
|
static int create_thread( struct wine_pthread_thread_info *info )
|
2003-11-07 00:05:41 +01:00
|
|
|
{
|
|
|
|
pthread_t id;
|
|
|
|
pthread_attr_t attr;
|
2003-11-09 02:25:23 +01:00
|
|
|
int ret = 0;
|
2003-11-07 00:05:41 +01:00
|
|
|
|
|
|
|
pthread_attr_init( &attr );
|
2003-11-09 02:25:23 +01:00
|
|
|
pthread_attr_setstacksize( &attr, info->stack_size );
|
2004-02-12 00:58:46 +01:00
|
|
|
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
|
2006-06-23 12:26:23 +02:00
|
|
|
pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread on Solaris */
|
2003-11-09 02:25:23 +01:00
|
|
|
if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info )) ret = -1;
|
|
|
|
pthread_attr_destroy( &attr );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-05 03:01:35 +01:00
|
|
|
/***********************************************************************
|
2005-09-03 17:11:29 +02:00
|
|
|
* init_current_teb
|
2004-02-05 03:01:35 +01:00
|
|
|
*
|
|
|
|
* Set the current TEB for a new thread.
|
|
|
|
*/
|
2005-09-03 17:11:29 +02:00
|
|
|
static void init_current_teb( struct wine_pthread_thread_info *info )
|
2004-02-05 03:01:35 +01:00
|
|
|
{
|
|
|
|
#ifdef __i386__
|
|
|
|
/* On the i386, the current thread is in the %fs register */
|
|
|
|
LDT_ENTRY fs_entry;
|
|
|
|
|
|
|
|
wine_ldt_set_base( &fs_entry, info->teb_base );
|
|
|
|
wine_ldt_set_limit( &fs_entry, info->teb_size - 1 );
|
|
|
|
wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
|
|
|
|
wine_ldt_init_fs( info->teb_sel, &fs_entry );
|
|
|
|
#else
|
2005-09-03 17:11:29 +02:00
|
|
|
if (!init_done) /* first thread */
|
2004-02-05 03:01:35 +01:00
|
|
|
pthread_key_create( &teb_key, NULL );
|
|
|
|
pthread_setspecific( teb_key, info->teb_base );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* set pid and tid */
|
|
|
|
info->pid = getpid();
|
2006-06-23 12:26:23 +02:00
|
|
|
#ifdef __sun
|
|
|
|
info->tid = pthread_self(); /* this should return the lwp id on solaris */
|
|
|
|
#else
|
2004-02-05 03:01:35 +01:00
|
|
|
info->tid = gettid();
|
2006-06-23 12:26:23 +02:00
|
|
|
#endif
|
2004-02-05 03:01:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-09 02:25:23 +01:00
|
|
|
/***********************************************************************
|
2005-09-03 17:11:29 +02:00
|
|
|
* get_current_teb
|
2003-11-09 02:25:23 +01:00
|
|
|
*/
|
2005-09-03 17:11:29 +02:00
|
|
|
static void *get_current_teb(void)
|
2003-11-09 02:25:23 +01:00
|
|
|
{
|
|
|
|
#ifdef __i386__
|
|
|
|
void *ret;
|
|
|
|
__asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
return pthread_getspecific( teb_key );
|
|
|
|
#endif
|
2003-11-07 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2005-09-03 17:11:29 +02:00
|
|
|
* exit_thread
|
2003-11-07 00:05:41 +01:00
|
|
|
*/
|
2005-09-03 17:11:29 +02:00
|
|
|
static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
|
2003-11-07 00:05:41 +01:00
|
|
|
{
|
2004-02-12 00:58:46 +01:00
|
|
|
wine_ldt_free_fs( info->teb_sel );
|
|
|
|
munmap( info->teb_base, info->teb_size );
|
2003-11-07 00:05:41 +01:00
|
|
|
pthread_exit( (void *)info->exit_status );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2005-09-03 17:11:29 +02:00
|
|
|
* abort_thread
|
2003-11-07 00:05:41 +01:00
|
|
|
*/
|
2005-09-13 13:07:14 +02:00
|
|
|
static void DECLSPEC_NORETURN abort_thread( long status )
|
2003-11-07 00:05:41 +01:00
|
|
|
{
|
|
|
|
pthread_exit( (void *)status );
|
|
|
|
}
|
|
|
|
|
2005-09-03 17:11:29 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* pthread_functions
|
|
|
|
*/
|
|
|
|
const struct wine_pthread_functions pthread_functions =
|
|
|
|
{
|
|
|
|
init_process,
|
|
|
|
init_thread,
|
|
|
|
create_thread,
|
|
|
|
init_current_teb,
|
|
|
|
get_current_teb,
|
|
|
|
exit_thread,
|
2006-06-23 12:27:45 +02:00
|
|
|
abort_thread,
|
|
|
|
pthread_sigmask
|
2005-09-03 17:11:29 +02:00
|
|
|
};
|
|
|
|
|
2003-11-07 00:05:41 +01:00
|
|
|
#endif /* HAVE_PTHREAD_H */
|