pthread.c 6.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 */

#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
37 38 39
#ifdef HAVE_MACH_MACH_H
#include <mach/mach.h>
#endif
40 41 42 43

#include "wine/library.h"
#include "wine/pthread.h"

44
static int init_done;
45
static int nb_threads = 1;
46 47 48 49 50

#ifndef __i386__
static pthread_key_t teb_key;
#endif

51
/***********************************************************************
52
 *           init_process
53 54 55
 *
 * Initialization for a newly created process.
 */
56
static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
57
{
58
    init_done = 1;
59 60 61 62
}


/***********************************************************************
63
 *           init_thread
64 65 66
 *
 * Initialization for a newly created thread.
 */
67
static void init_thread( struct wine_pthread_thread_info *info )
68
{
69
    /* retrieve the stack info (except for main thread) */
70
    if (init_done)
71
    {
72
#ifdef HAVE_PTHREAD_GETATTR_NP
73 74 75
        pthread_attr_t attr;
        pthread_getattr_np( pthread_self(), &attr );
        pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
76
        pthread_attr_destroy( &attr );
77 78 79 80 81 82
#elif defined(HAVE_PTHREAD_ATTR_GET_NP)
        pthread_attr_t attr;
        pthread_attr_init( &attr );
        pthread_attr_get_np( pthread_self(), &attr );
        pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
        pthread_attr_destroy( &attr );
83
#elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
84
        char dummy;
85 86
        info->stack_size = pthread_get_stacksize_np(pthread_self());
        info->stack_base = pthread_get_stackaddr_np(pthread_self());
87 88 89
        /* 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;
90 91 92
#else
        /* assume that the stack allocation is page aligned */
        char dummy;
93 94
        size_t page_size = getpagesize();
        char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
95 96
        info->stack_base = stack_top - info->stack_size;
#endif
97
    }
98 99 100 101
}


/***********************************************************************
102
 *           create_thread
103
 */
104
static int create_thread( struct wine_pthread_thread_info *info )
105 106 107
{
    pthread_t id;
    pthread_attr_t attr;
108
    int ret = 0;
109 110

    pthread_attr_init( &attr );
111
    pthread_attr_setstacksize( &attr, info->stack_size );
112
    pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
113
    pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread on Solaris */
114 115 116 117 118 119
    interlocked_xchg_add( &nb_threads, 1 );
    if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info ))
    {
        interlocked_xchg_add( &nb_threads, -1 );
        ret = -1;
    }
120 121 122 123 124
    pthread_attr_destroy( &attr );
    return ret;
}


125
/***********************************************************************
126
 *           init_current_teb
127 128 129
 *
 * Set the current TEB for a new thread.
 */
130
static void init_current_teb( struct wine_pthread_thread_info *info )
131 132 133 134 135 136 137 138 139 140
{
#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
141
    if (!init_done)  /* first thread */
142 143 144 145 146 147
        pthread_key_create( &teb_key, NULL );
    pthread_setspecific( teb_key, info->teb_base );
#endif

    /* set pid and tid */
    info->pid = getpid();
148 149
#ifdef __sun
    info->tid = pthread_self();  /* this should return the lwp id on solaris */
150 151
#elif defined(__APPLE__)
    info->tid = mach_thread_self();
152
#else
153
    info->tid = gettid();
154
#endif
155 156 157
}


158
/***********************************************************************
159
 *           get_current_teb
160
 */
161
static void *get_current_teb(void)
162 163 164 165 166 167 168 169
{
#ifdef __i386__
    void *ret;
    __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
    return ret;
#else
    return pthread_getspecific( teb_key );
#endif
170 171 172 173
}


/***********************************************************************
174
 *           exit_thread
175
 */
176
static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
177
{
178
    if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) exit( info->exit_status );
179
    wine_ldt_free_fs( info->teb_sel );
180
    if (info->teb_size) munmap( info->teb_base, info->teb_size );
181 182 183 184 185
    pthread_exit( (void *)info->exit_status );
}


/***********************************************************************
186
 *           abort_thread
187
 */
188
static void DECLSPEC_NORETURN abort_thread( long status )
189
{
190
    if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) _exit( status );
191 192 193
    pthread_exit( (void *)status );
}

194 195 196 197 198 199 200 201 202 203 204 205

/***********************************************************************
 *           pthread_functions
 */
const struct wine_pthread_functions pthread_functions =
{
    init_process,
    init_thread,
    create_thread,
    init_current_teb,
    get_current_teb,
    exit_thread,
206 207
    abort_thread,
    pthread_sigmask
208 209
};

210
#endif  /* HAVE_PTHREAD_H */