widl: Pass a state into marshall_arguments and unmarshall_arguments to
[wine.git] / loader / pthread.c
blobf2f87d73ba11775f2a382a42b09d92b3caf619ab
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #ifdef HAVE_PTHREAD_H
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
38 #include "wine/library.h"
39 #include "wine/pthread.h"
41 static int init_done;
43 #ifndef __i386__
44 static pthread_key_t teb_key;
45 #endif
47 /***********************************************************************
48 * init_process
50 * Initialization for a newly created process.
52 static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
54 init_done = 1;
58 /***********************************************************************
59 * init_thread
61 * Initialization for a newly created thread.
63 static void init_thread( struct wine_pthread_thread_info *info )
65 /* retrieve the stack info (except for main thread) */
66 if (init_done)
68 #ifdef HAVE_PTHREAD_GETATTR_NP
69 pthread_attr_t attr;
70 pthread_getattr_np( pthread_self(), &attr );
71 pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
72 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
73 info->stack_size = pthread_get_stacksize_np(pthread_self());
74 info->stack_base = pthread_get_stackaddr_np(pthread_self());
75 #else
76 /* assume that the stack allocation is page aligned */
77 char dummy;
78 size_t page_size = getpagesize();
79 char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
80 info->stack_base = stack_top - info->stack_size;
81 #endif
86 /***********************************************************************
87 * create_thread
89 static int create_thread( struct wine_pthread_thread_info *info )
91 pthread_t id;
92 pthread_attr_t attr;
93 int ret = 0;
95 pthread_attr_init( &attr );
96 pthread_attr_setstacksize( &attr, info->stack_size );
97 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
98 if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info )) ret = -1;
99 pthread_attr_destroy( &attr );
100 return ret;
104 /***********************************************************************
105 * init_current_teb
107 * Set the current TEB for a new thread.
109 static void init_current_teb( struct wine_pthread_thread_info *info )
111 #ifdef __i386__
112 /* On the i386, the current thread is in the %fs register */
113 LDT_ENTRY fs_entry;
115 wine_ldt_set_base( &fs_entry, info->teb_base );
116 wine_ldt_set_limit( &fs_entry, info->teb_size - 1 );
117 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
118 wine_ldt_init_fs( info->teb_sel, &fs_entry );
119 #else
120 if (!init_done) /* first thread */
121 pthread_key_create( &teb_key, NULL );
122 pthread_setspecific( teb_key, info->teb_base );
123 #endif
125 /* set pid and tid */
126 info->pid = getpid();
127 info->tid = gettid();
131 /***********************************************************************
132 * get_current_teb
134 static void *get_current_teb(void)
136 #ifdef __i386__
137 void *ret;
138 __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
139 return ret;
140 #else
141 return pthread_getspecific( teb_key );
142 #endif
146 /***********************************************************************
147 * exit_thread
149 static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
151 wine_ldt_free_fs( info->teb_sel );
152 munmap( info->teb_base, info->teb_size );
153 pthread_exit( (void *)info->exit_status );
157 /***********************************************************************
158 * abort_thread
160 static void DECLSPEC_NORETURN abort_thread( long status )
162 pthread_exit( (void *)status );
166 /***********************************************************************
167 * pthread_functions
169 const struct wine_pthread_functions pthread_functions =
171 init_process,
172 init_thread,
173 create_thread,
174 init_current_teb,
175 get_current_teb,
176 exit_thread,
177 abort_thread
180 #endif /* HAVE_PTHREAD_H */