Alexandre Julliard | f45325e | 2003-11-06 23:05:41 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 41 | /*********************************************************************** |
| 42 | * wine_pthread_init_process |
| 43 | * |
| 44 | * Initialization for a newly created process. |
| 45 | */ |
| 46 | void wine_pthread_init_process( const struct wine_pthread_functions *functions ) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /*********************************************************************** |
| 52 | * wine_pthread_init_thread |
| 53 | * |
| 54 | * Initialization for a newly created thread. |
| 55 | */ |
| 56 | void wine_pthread_init_thread(void) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /*********************************************************************** |
| 62 | * wine_pthread_create_thread |
| 63 | */ |
| 64 | int wine_pthread_create_thread( struct wine_pthread_thread_info *info ) |
| 65 | { |
| 66 | pthread_t id; |
| 67 | pthread_attr_t attr; |
| 68 | |
| 69 | if (!info->stack_base) |
| 70 | { |
| 71 | info->stack_base = wine_anon_mmap( NULL, info->stack_size, |
| 72 | PROT_READ | PROT_WRITE | PROT_EXEC, 0 ); |
| 73 | if (info->stack_base == (void *)-1) return -1; |
| 74 | } |
| 75 | pthread_attr_init( &attr ); |
| 76 | pthread_attr_setstack( &attr, info->stack_base, info->stack_size ); |
| 77 | if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info )) return -1; |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /*********************************************************************** |
| 83 | * wine_pthread_exit_thread |
| 84 | */ |
| 85 | void wine_pthread_exit_thread( struct wine_pthread_thread_info *info ) |
| 86 | { |
| 87 | struct cleanup_info |
| 88 | { |
| 89 | pthread_t self; |
| 90 | struct wine_pthread_thread_info thread_info; |
| 91 | }; |
| 92 | |
| 93 | static struct cleanup_info *previous_info; |
| 94 | struct cleanup_info *cleanup_info, *free_info; |
| 95 | void *ptr; |
| 96 | |
| 97 | /* store it at the end of the TEB structure */ |
| 98 | cleanup_info = (struct cleanup_info *)((char *)info->teb_base + info->teb_size) - 1; |
| 99 | cleanup_info->self = pthread_self(); |
| 100 | cleanup_info->thread_info = *info; |
| 101 | |
| 102 | if ((free_info = interlocked_xchg_ptr( (void **)&previous_info, cleanup_info )) != NULL) |
| 103 | { |
| 104 | pthread_join( free_info->self, &ptr ); |
| 105 | wine_ldt_free_fs( free_info->thread_info.teb_sel ); |
| 106 | munmap( free_info->thread_info.stack_base, free_info->thread_info.stack_size ); |
| 107 | munmap( free_info->thread_info.teb_base, free_info->thread_info.teb_size ); |
| 108 | } |
| 109 | pthread_exit( (void *)info->exit_status ); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /*********************************************************************** |
| 114 | * wine_pthread_abort_thread |
| 115 | */ |
| 116 | void wine_pthread_abort_thread( int status ) |
| 117 | { |
| 118 | pthread_exit( (void *)status ); |
| 119 | } |
| 120 | |
| 121 | #endif /* HAVE_PTHREAD_H */ |