Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side thread management |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 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 |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 21 | #include "config.h" |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 22 | #include "wine/port.h" |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 23 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 24 | #include <assert.h> |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 25 | #include <errno.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 26 | #include <fcntl.h> |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 27 | #include <signal.h> |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 28 | #include <stdarg.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 32 | #include <sys/types.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
Ryan Cumming | 24f4ece | 2002-11-25 01:33:38 +0000 | [diff] [blame] | 34 | #include <time.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 35 | |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 36 | #include "windef.h" |
Michael Veksler | f935c59 | 1999-02-09 15:49:39 +0000 | [diff] [blame] | 37 | #include "winbase.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 38 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 39 | #include "file.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 40 | #include "handle.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 41 | #include "process.h" |
| 42 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 43 | #include "request.h" |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 44 | #include "user.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 45 | |
| 46 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 47 | /* thread queues */ |
| 48 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 49 | struct thread_wait |
| 50 | { |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 51 | struct thread_wait *next; /* next wait structure for this thread */ |
| 52 | struct thread *thread; /* owner thread */ |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 53 | int count; /* count of objects */ |
| 54 | int flags; |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 55 | void *cookie; /* magic cookie to return to client */ |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 56 | struct timeval timeout; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 57 | struct timeout_user *user; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 58 | struct wait_queue_entry queues[1]; |
| 59 | }; |
| 60 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 61 | /* asynchronous procedure calls */ |
| 62 | |
| 63 | struct thread_apc |
| 64 | { |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 65 | struct thread_apc *next; /* queue linked list */ |
| 66 | struct thread_apc *prev; |
| 67 | struct object *owner; /* object that queued this apc */ |
| 68 | void *func; /* function to call in client */ |
| 69 | enum apc_type type; /* type of apc function */ |
| 70 | int nb_args; /* number of arguments */ |
Alexandre Julliard | 088bcf9 | 2003-04-04 22:26:34 +0000 | [diff] [blame] | 71 | void *arg1; /* function arguments */ |
| 72 | void *arg2; |
| 73 | void *arg3; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 74 | }; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 75 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 76 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 77 | /* thread operations */ |
| 78 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 79 | static void dump_thread( struct object *obj, int verbose ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 80 | static int thread_signaled( struct object *obj, struct thread *thread ); |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 81 | static void thread_poll_event( struct fd *fd, int event ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 82 | static void destroy_thread( struct object *obj ); |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 83 | static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 84 | |
| 85 | static const struct object_ops thread_ops = |
| 86 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 87 | sizeof(struct thread), /* size */ |
| 88 | dump_thread, /* dump */ |
| 89 | add_queue, /* add_queue */ |
| 90 | remove_queue, /* remove_queue */ |
| 91 | thread_signaled, /* signaled */ |
| 92 | no_satisfied, /* satisfied */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 93 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 94 | destroy_thread /* destroy */ |
| 95 | }; |
| 96 | |
| 97 | static const struct fd_ops thread_fd_ops = |
| 98 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 99 | NULL, /* get_poll_events */ |
| 100 | thread_poll_event, /* poll_event */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 101 | no_flush, /* flush */ |
| 102 | no_get_file_info, /* get_file_info */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 103 | no_queue_async /* queue_async */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 106 | static struct thread *first_thread; |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 107 | static struct thread *booting_thread; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 108 | |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 109 | /* initialize the structure for a newly allocated thread */ |
| 110 | inline static void init_thread_structure( struct thread *thread ) |
| 111 | { |
| 112 | int i; |
| 113 | |
Alexandre Julliard | 02a53c1 | 2003-02-25 04:17:22 +0000 | [diff] [blame] | 114 | thread->unix_pid = -1; /* not known yet */ |
Alexandre Julliard | a8497bd | 2003-03-22 21:00:09 +0000 | [diff] [blame] | 115 | thread->unix_tid = -1; /* not known yet */ |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 116 | thread->context = NULL; |
| 117 | thread->teb = NULL; |
| 118 | thread->mutex = NULL; |
| 119 | thread->debug_ctx = NULL; |
| 120 | thread->debug_event = NULL; |
| 121 | thread->queue = NULL; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 122 | thread->wait = NULL; |
| 123 | thread->system_apc.head = NULL; |
| 124 | thread->system_apc.tail = NULL; |
| 125 | thread->user_apc.head = NULL; |
| 126 | thread->user_apc.tail = NULL; |
| 127 | thread->error = 0; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 128 | thread->req_data = NULL; |
| 129 | thread->req_toread = 0; |
| 130 | thread->reply_data = NULL; |
| 131 | thread->reply_towrite = 0; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 132 | thread->request_fd = NULL; |
| 133 | thread->reply_fd = NULL; |
| 134 | thread->wait_fd = NULL; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 135 | thread->state = RUNNING; |
| 136 | thread->attached = 0; |
| 137 | thread->exit_code = 0; |
| 138 | thread->next = NULL; |
| 139 | thread->prev = NULL; |
| 140 | thread->priority = THREAD_PRIORITY_NORMAL; |
| 141 | thread->affinity = 1; |
| 142 | thread->suspend = 0; |
Ryan Cumming | 24f4ece | 2002-11-25 01:33:38 +0000 | [diff] [blame] | 143 | thread->creation_time = time(NULL); |
| 144 | thread->exit_time = 0; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 145 | |
| 146 | for (i = 0; i < MAX_INFLIGHT_FDS; i++) |
| 147 | thread->inflight[i].server = thread->inflight[i].client = -1; |
| 148 | } |
| 149 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 150 | /* create a new thread */ |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 151 | struct thread *create_thread( int fd, struct process *process ) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 152 | { |
| 153 | struct thread *thread; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 154 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 155 | if (!(thread = alloc_object( &thread_ops ))) return NULL; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 156 | |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 157 | init_thread_structure( thread ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 158 | |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 159 | thread->process = (struct process *)grab_object( process ); |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 160 | if (!current) current = thread; |
| 161 | |
| 162 | if (!booting_thread) /* first thread ever */ |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 163 | { |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 164 | booting_thread = thread; |
| 165 | lock_master_socket(1); |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 166 | } |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 167 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 168 | if ((thread->next = first_thread) != NULL) thread->next->prev = thread; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 169 | first_thread = thread; |
| 170 | |
Alexandre Julliard | 91befe1 | 2003-02-01 01:38:40 +0000 | [diff] [blame] | 171 | if (!(thread->id = alloc_ptid( thread ))) |
| 172 | { |
| 173 | release_object( thread ); |
| 174 | return NULL; |
| 175 | } |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 176 | if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj ))) |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 177 | { |
| 178 | release_object( thread ); |
| 179 | return NULL; |
| 180 | } |
Alexandre Julliard | 91befe1 | 2003-02-01 01:38:40 +0000 | [diff] [blame] | 181 | |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 182 | thread->token = (struct token *) grab_object( process->token ); |
| 183 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 184 | set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */ |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 185 | add_process_thread( thread->process, thread ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 186 | return thread; |
| 187 | } |
| 188 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 189 | /* handle a client event */ |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 190 | static void thread_poll_event( struct fd *fd, int event ) |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 191 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 192 | struct thread *thread = get_fd_user( fd ); |
| 193 | assert( thread->obj.ops == &thread_ops ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 194 | |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 195 | if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 ); |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 196 | else if (event & POLLIN) read_request( thread ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 197 | else if (event & POLLOUT) write_reply( thread ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* cleanup everything that is no longer needed by a dead thread */ |
| 201 | /* used by destroy_thread and kill_thread */ |
| 202 | static void cleanup_thread( struct thread *thread ) |
| 203 | { |
| 204 | int i; |
| 205 | struct thread_apc *apc; |
| 206 | |
| 207 | while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 208 | if (thread->req_data) free( thread->req_data ); |
| 209 | if (thread->reply_data) free( thread->reply_data ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 210 | if (thread->request_fd) release_object( thread->request_fd ); |
| 211 | if (thread->reply_fd) release_object( thread->reply_fd ); |
| 212 | if (thread->wait_fd) release_object( thread->wait_fd ); |
Alexandre Julliard | 31022d6 | 2002-08-16 23:30:41 +0000 | [diff] [blame] | 213 | free_msg_queue( thread ); |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 214 | cleanup_clipboard_thread(thread); |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 215 | destroy_thread_windows( thread ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 216 | for (i = 0; i < MAX_INFLIGHT_FDS; i++) |
| 217 | { |
| 218 | if (thread->inflight[i].client != -1) |
| 219 | { |
| 220 | close( thread->inflight[i].server ); |
| 221 | thread->inflight[i].client = thread->inflight[i].server = -1; |
| 222 | } |
| 223 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 224 | thread->req_data = NULL; |
| 225 | thread->reply_data = NULL; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 226 | thread->request_fd = NULL; |
| 227 | thread->reply_fd = NULL; |
| 228 | thread->wait_fd = NULL; |
Alexandre Julliard | ff73250 | 2002-06-22 01:20:36 +0000 | [diff] [blame] | 229 | |
| 230 | if (thread == booting_thread) /* killing booting thread */ |
| 231 | { |
| 232 | booting_thread = NULL; |
| 233 | lock_master_socket(0); |
| 234 | } |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 237 | /* destroy a thread when its refcount is 0 */ |
| 238 | static void destroy_thread( struct object *obj ) |
| 239 | { |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 240 | struct thread_apc *apc; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 241 | struct thread *thread = (struct thread *)obj; |
| 242 | assert( obj->ops == &thread_ops ); |
| 243 | |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 244 | assert( !thread->debug_ctx ); /* cannot still be debugging something */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 245 | if (thread->next) thread->next->prev = thread->prev; |
| 246 | if (thread->prev) thread->prev->next = thread->next; |
| 247 | else first_thread = thread->next; |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 248 | while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 249 | cleanup_thread( thread ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 250 | release_object( thread->process ); |
Alexandre Julliard | 91befe1 | 2003-02-01 01:38:40 +0000 | [diff] [blame] | 251 | if (thread->id) free_ptid( thread->id ); |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 252 | if (thread->token) release_object( thread->token ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 255 | /* dump a thread on stdout for debugging purposes */ |
| 256 | static void dump_thread( struct object *obj, int verbose ) |
| 257 | { |
| 258 | struct thread *thread = (struct thread *)obj; |
| 259 | assert( obj->ops == &thread_ops ); |
| 260 | |
Alexandre Julliard | a8497bd | 2003-03-22 21:00:09 +0000 | [diff] [blame] | 261 | fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n", |
| 262 | thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 265 | static int thread_signaled( struct object *obj, struct thread *thread ) |
| 266 | { |
| 267 | struct thread *mythread = (struct thread *)obj; |
| 268 | return (mythread->state == TERMINATED); |
| 269 | } |
| 270 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 271 | /* get a thread pointer from a thread id (and increment the refcount) */ |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 272 | struct thread *get_thread_from_id( thread_id_t id ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 273 | { |
Alexandre Julliard | 91befe1 | 2003-02-01 01:38:40 +0000 | [diff] [blame] | 274 | struct object *obj = get_ptid_entry( id ); |
| 275 | |
| 276 | if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj ); |
| 277 | set_error( STATUS_INVALID_PARAMETER ); |
| 278 | return NULL; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 281 | /* get a thread from a handle (and increment the refcount) */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 282 | struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 283 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 284 | return (struct thread *)get_handle_obj( current->process, handle, |
| 285 | access, &thread_ops ); |
| 286 | } |
| 287 | |
Alexandre Julliard | 578c100 | 1999-11-14 21:23:21 +0000 | [diff] [blame] | 288 | /* find a thread from a Unix pid */ |
| 289 | struct thread *get_thread_from_pid( int pid ) |
| 290 | { |
Alexandre Julliard | a8497bd | 2003-03-22 21:00:09 +0000 | [diff] [blame] | 291 | struct thread *t; |
| 292 | |
| 293 | for (t = first_thread; t; t = t->next) if (t->unix_tid == pid) return t; |
| 294 | for (t = first_thread; t; t = t->next) if (t->unix_pid == pid) return t; |
| 295 | return NULL; |
Alexandre Julliard | 578c100 | 1999-11-14 21:23:21 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 298 | /* set all information about a thread */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 299 | static void set_thread_info( struct thread *thread, |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 300 | const struct set_thread_info_request *req ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 301 | { |
| 302 | if (req->mask & SET_THREAD_INFO_PRIORITY) |
| 303 | thread->priority = req->priority; |
| 304 | if (req->mask & SET_THREAD_INFO_AFFINITY) |
| 305 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 306 | if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 307 | else thread->affinity = req->affinity; |
| 308 | } |
| 309 | } |
| 310 | |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 311 | /* stop a thread (at the Unix level) */ |
| 312 | void stop_thread( struct thread *thread ) |
| 313 | { |
| 314 | /* can't stop a thread while initialisation is in progress */ |
| 315 | if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 ); |
| 316 | } |
| 317 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 318 | /* suspend a thread */ |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 319 | static int suspend_thread( struct thread *thread ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 320 | { |
| 321 | int old_count = thread->suspend; |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 322 | if (thread->suspend < MAXIMUM_SUSPEND_COUNT) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 323 | { |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 324 | if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 325 | } |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 326 | else set_error( STATUS_SUSPEND_COUNT_EXCEEDED ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 327 | return old_count; |
| 328 | } |
| 329 | |
| 330 | /* resume a thread */ |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 331 | static int resume_thread( struct thread *thread ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 332 | { |
| 333 | int old_count = thread->suspend; |
| 334 | if (thread->suspend > 0) |
| 335 | { |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 336 | if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 337 | } |
| 338 | return old_count; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 341 | /* add a thread to an object wait queue; return 1 if OK, 0 on error */ |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 342 | int add_queue( struct object *obj, struct wait_queue_entry *entry ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 343 | { |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 344 | grab_object( obj ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 345 | entry->obj = obj; |
| 346 | entry->prev = obj->tail; |
| 347 | entry->next = NULL; |
| 348 | if (obj->tail) obj->tail->next = entry; |
| 349 | else obj->head = entry; |
| 350 | obj->tail = entry; |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 351 | return 1; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* remove a thread from an object wait queue */ |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 355 | void remove_queue( struct object *obj, struct wait_queue_entry *entry ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 356 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 357 | if (entry->next) entry->next->prev = entry->prev; |
| 358 | else obj->tail = entry->prev; |
| 359 | if (entry->prev) entry->prev->next = entry->next; |
| 360 | else obj->head = entry->next; |
| 361 | release_object( obj ); |
| 362 | } |
| 363 | |
| 364 | /* finish waiting */ |
| 365 | static void end_wait( struct thread *thread ) |
| 366 | { |
| 367 | struct thread_wait *wait = thread->wait; |
| 368 | struct wait_queue_entry *entry; |
| 369 | int i; |
| 370 | |
| 371 | assert( wait ); |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 372 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 373 | entry->obj->ops->remove_queue( entry->obj, entry ); |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 374 | if (wait->user) remove_timeout_user( wait->user ); |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 375 | thread->wait = wait->next; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 376 | free( wait ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* build the thread wait structure */ |
Alexandre Julliard | 462172a | 2003-04-02 22:48:59 +0000 | [diff] [blame] | 380 | static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 381 | { |
| 382 | struct thread_wait *wait; |
| 383 | struct wait_queue_entry *entry; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 384 | int i; |
| 385 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 386 | if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0; |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 387 | wait->next = current->wait; |
| 388 | wait->thread = current; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 389 | wait->count = count; |
| 390 | wait->flags = flags; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 391 | wait->user = NULL; |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 392 | current->wait = wait; |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 393 | if (flags & SELECT_TIMEOUT) |
| 394 | { |
Alexandre Julliard | 462172a | 2003-04-02 22:48:59 +0000 | [diff] [blame] | 395 | wait->timeout.tv_sec = timeout->sec; |
| 396 | wait->timeout.tv_usec = timeout->usec; |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 397 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 398 | |
| 399 | for (i = 0, entry = wait->queues; i < count; i++, entry++) |
| 400 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 401 | struct object *obj = objects[i]; |
| 402 | entry->thread = current; |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 403 | if (!obj->ops->add_queue( obj, entry )) |
| 404 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 405 | wait->count = i; |
| 406 | end_wait( current ); |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 407 | return 0; |
| 408 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 409 | } |
| 410 | return 1; |
| 411 | } |
| 412 | |
| 413 | /* check if the thread waiting condition is satisfied */ |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 414 | static int check_wait( struct thread *thread ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 415 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 416 | int i, signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 417 | struct thread_wait *wait = thread->wait; |
| 418 | struct wait_queue_entry *entry = wait->queues; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 419 | |
Peter Hunnisett | a3c5ad4 | 2003-02-28 21:50:47 +0000 | [diff] [blame] | 420 | /* Suspended threads may not acquire locks */ |
| 421 | if( thread->process->suspend + thread->suspend > 0 ) return -1; |
| 422 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 423 | assert( wait ); |
| 424 | if (wait->flags & SELECT_ALL) |
| 425 | { |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 426 | int not_ok = 0; |
| 427 | /* Note: we must check them all anyway, as some objects may |
| 428 | * want to do something when signaled, even if others are not */ |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 429 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 430 | not_ok |= !entry->obj->ops->signaled( entry->obj, thread ); |
| 431 | if (not_ok) goto other_checks; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 432 | /* Wait satisfied: tell it to all objects */ |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 433 | signaled = 0; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 434 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 435 | if (entry->obj->ops->satisfied( entry->obj, thread )) |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 436 | signaled = STATUS_ABANDONED_WAIT_0; |
| 437 | return signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 438 | } |
| 439 | else |
| 440 | { |
| 441 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 442 | { |
| 443 | if (!entry->obj->ops->signaled( entry->obj, thread )) continue; |
| 444 | /* Wait satisfied: tell it to the object */ |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 445 | signaled = i; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 446 | if (entry->obj->ops->satisfied( entry->obj, thread )) |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 447 | signaled = i + STATUS_ABANDONED_WAIT_0; |
| 448 | return signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 451 | |
| 452 | other_checks: |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 453 | if ((wait->flags & SELECT_INTERRUPTIBLE) && thread->system_apc.head) return STATUS_USER_APC; |
| 454 | if ((wait->flags & SELECT_ALERTABLE) && thread->user_apc.head) return STATUS_USER_APC; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 455 | if (wait->flags & SELECT_TIMEOUT) |
| 456 | { |
| 457 | struct timeval now; |
| 458 | gettimeofday( &now, NULL ); |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 459 | if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 460 | } |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 461 | return -1; |
| 462 | } |
| 463 | |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 464 | /* send the wakeup signal to a thread */ |
| 465 | static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled ) |
| 466 | { |
| 467 | struct wake_up_reply reply; |
| 468 | int ret; |
| 469 | |
| 470 | reply.cookie = cookie; |
| 471 | reply.signaled = signaled; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 472 | if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply)) |
| 473 | return 0; |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 474 | if (ret >= 0) |
| 475 | fatal_protocol_error( thread, "partial wakeup write %d\n", ret ); |
| 476 | else if (errno == EPIPE) |
| 477 | kill_thread( thread, 0 ); /* normal death */ |
| 478 | else |
| 479 | fatal_protocol_perror( thread, "write" ); |
| 480 | return -1; |
| 481 | } |
| 482 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 483 | /* attempt to wake up a thread */ |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 484 | /* return >0 if OK, 0 if the wait condition is still not satisfied */ |
Peter Hunnisett | a3c5ad4 | 2003-02-28 21:50:47 +0000 | [diff] [blame] | 485 | int wake_thread( struct thread *thread ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 486 | { |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 487 | int signaled, count; |
| 488 | void *cookie; |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 489 | |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 490 | for (count = 0; thread->wait; count++) |
| 491 | { |
| 492 | if ((signaled = check_wait( thread )) == -1) break; |
| 493 | |
| 494 | cookie = thread->wait->cookie; |
Alexandre Julliard | 91befe1 | 2003-02-01 01:38:40 +0000 | [diff] [blame] | 495 | if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n", |
| 496 | thread->id, signaled, cookie ); |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 497 | end_wait( thread ); |
Andreas Mohr | d66130a | 2001-08-07 19:31:43 +0000 | [diff] [blame] | 498 | if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */ |
| 499 | break; |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 500 | } |
| 501 | return count; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 504 | /* thread wait timeout */ |
| 505 | static void thread_timeout( void *ptr ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 506 | { |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 507 | struct thread_wait *wait = ptr; |
| 508 | struct thread *thread = wait->thread; |
| 509 | void *cookie = wait->cookie; |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 510 | |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 511 | wait->user = NULL; |
| 512 | if (thread->wait != wait) return; /* not the top-level wait, ignore it */ |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 513 | if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */ |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 514 | |
Alexandre Julliard | 91befe1 | 2003-02-01 01:38:40 +0000 | [diff] [blame] | 515 | if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n", |
| 516 | thread->id, STATUS_TIMEOUT, cookie ); |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 517 | end_wait( thread ); |
Alexandre Julliard | ff73250 | 2002-06-22 01:20:36 +0000 | [diff] [blame] | 518 | if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return; |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 519 | /* check if other objects have become signaled in the meantime */ |
| 520 | wake_thread( thread ); |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 523 | /* select on a list of handles */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 524 | static void select_on( int count, void *cookie, const obj_handle_t *handles, |
Alexandre Julliard | 462172a | 2003-04-02 22:48:59 +0000 | [diff] [blame] | 525 | int flags, const abs_time_t *timeout ) |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 526 | { |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 527 | int ret, i; |
| 528 | struct object *objects[MAXIMUM_WAIT_OBJECTS]; |
| 529 | |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 530 | if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS)) |
| 531 | { |
| 532 | set_error( STATUS_INVALID_PARAMETER ); |
| 533 | return; |
| 534 | } |
| 535 | for (i = 0; i < count; i++) |
| 536 | { |
| 537 | if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL ))) |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | if (i < count) goto done; |
Alexandre Julliard | 462172a | 2003-04-02 22:48:59 +0000 | [diff] [blame] | 542 | if (!wait_on( count, objects, flags, timeout )) goto done; |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 543 | |
| 544 | if ((ret = check_wait( current )) != -1) |
| 545 | { |
| 546 | /* condition is already satisfied */ |
| 547 | end_wait( current ); |
| 548 | set_error( ret ); |
| 549 | goto done; |
| 550 | } |
| 551 | |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 552 | /* now we need to wait */ |
| 553 | if (flags & SELECT_TIMEOUT) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 554 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 555 | if (!(current->wait->user = add_timeout_user( ¤t->wait->timeout, |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 556 | thread_timeout, current->wait ))) |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 557 | { |
| 558 | end_wait( current ); |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 559 | goto done; |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 560 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 561 | } |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 562 | current->wait->cookie = cookie; |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 563 | set_error( STATUS_PENDING ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 564 | |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 565 | done: |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 566 | while (--i >= 0) release_object( objects[i] ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* attempt to wake threads sleeping on the object wait queue */ |
| 570 | void wake_up( struct object *obj, int max ) |
| 571 | { |
| 572 | struct wait_queue_entry *entry = obj->head; |
| 573 | |
| 574 | while (entry) |
| 575 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 576 | struct thread *thread = entry->thread; |
| 577 | entry = entry->next; |
| 578 | if (wake_thread( thread )) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 579 | { |
| 580 | if (max && !--max) break; |
| 581 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 585 | /* queue an async procedure call */ |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 586 | int thread_queue_apc( struct thread *thread, struct object *owner, void *func, |
Alexandre Julliard | 088bcf9 | 2003-04-04 22:26:34 +0000 | [diff] [blame] | 587 | enum apc_type type, int system, void *arg1, void *arg2, void *arg3 ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 588 | { |
| 589 | struct thread_apc *apc; |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 590 | struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc; |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 591 | |
| 592 | /* cancel a possible previous APC with the same owner */ |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 593 | if (owner) thread_cancel_apc( thread, owner, system ); |
Alexandre Julliard | 15979fd | 2002-03-23 18:50:04 +0000 | [diff] [blame] | 594 | if (thread->state == TERMINATED) return 0; |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 595 | |
Alexandre Julliard | 088bcf9 | 2003-04-04 22:26:34 +0000 | [diff] [blame] | 596 | if (!(apc = mem_alloc( sizeof(*apc) ))) return 0; |
| 597 | apc->prev = queue->tail; |
| 598 | apc->next = NULL; |
| 599 | apc->owner = owner; |
| 600 | apc->func = func; |
| 601 | apc->type = type; |
| 602 | apc->arg1 = arg1; |
| 603 | apc->arg2 = arg2; |
| 604 | apc->arg3 = arg3; |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 605 | queue->tail = apc; |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 606 | if (!apc->prev) /* first one */ |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 607 | { |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 608 | queue->head = apc; |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 609 | wake_thread( thread ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 610 | } |
Martin Wilck | b1c45b9 | 2002-01-09 19:09:57 +0000 | [diff] [blame] | 611 | else apc->prev->next = apc; |
| 612 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 613 | return 1; |
| 614 | } |
| 615 | |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 616 | /* cancel the async procedure call owned by a specific object */ |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 617 | void thread_cancel_apc( struct thread *thread, struct object *owner, int system ) |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 618 | { |
| 619 | struct thread_apc *apc; |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 620 | struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc; |
| 621 | for (apc = queue->head; apc; apc = apc->next) |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 622 | { |
| 623 | if (apc->owner != owner) continue; |
| 624 | if (apc->next) apc->next->prev = apc->prev; |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 625 | else queue->tail = apc->prev; |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 626 | if (apc->prev) apc->prev->next = apc->next; |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 627 | else queue->head = apc->next; |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 628 | free( apc ); |
| 629 | return; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /* remove the head apc from the queue; the returned pointer must be freed by the caller */ |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 634 | static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only ) |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 635 | { |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 636 | struct thread_apc *apc; |
| 637 | struct apc_queue *queue = &thread->system_apc; |
| 638 | |
| 639 | if (!queue->head && !system_only) queue = &thread->user_apc; |
| 640 | if ((apc = queue->head)) |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 641 | { |
| 642 | if (apc->next) apc->next->prev = NULL; |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 643 | else queue->tail = NULL; |
| 644 | queue->head = apc->next; |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 645 | } |
| 646 | return apc; |
| 647 | } |
| 648 | |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 649 | /* add an fd to the inflight list */ |
| 650 | /* return list index, or -1 on error */ |
| 651 | int thread_add_inflight_fd( struct thread *thread, int client, int server ) |
| 652 | { |
| 653 | int i; |
| 654 | |
| 655 | if (server == -1) return -1; |
| 656 | if (client == -1) |
| 657 | { |
| 658 | close( server ); |
| 659 | return -1; |
| 660 | } |
| 661 | |
| 662 | /* first check if we already have an entry for this fd */ |
| 663 | for (i = 0; i < MAX_INFLIGHT_FDS; i++) |
| 664 | if (thread->inflight[i].client == client) |
| 665 | { |
| 666 | close( thread->inflight[i].server ); |
| 667 | thread->inflight[i].server = server; |
| 668 | return i; |
| 669 | } |
| 670 | |
| 671 | /* now find a free spot to store it */ |
| 672 | for (i = 0; i < MAX_INFLIGHT_FDS; i++) |
| 673 | if (thread->inflight[i].client == -1) |
| 674 | { |
| 675 | thread->inflight[i].client = client; |
| 676 | thread->inflight[i].server = server; |
| 677 | return i; |
| 678 | } |
| 679 | return -1; |
| 680 | } |
| 681 | |
| 682 | /* get an inflight fd and purge it from the list */ |
| 683 | /* the fd must be closed when no longer used */ |
| 684 | int thread_get_inflight_fd( struct thread *thread, int client ) |
| 685 | { |
| 686 | int i, ret; |
| 687 | |
| 688 | if (client == -1) return -1; |
| 689 | |
| 690 | do |
| 691 | { |
| 692 | for (i = 0; i < MAX_INFLIGHT_FDS; i++) |
| 693 | { |
| 694 | if (thread->inflight[i].client == client) |
| 695 | { |
| 696 | ret = thread->inflight[i].server; |
| 697 | thread->inflight[i].server = thread->inflight[i].client = -1; |
| 698 | return ret; |
| 699 | } |
| 700 | } |
| 701 | } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */ |
| 702 | return -1; |
| 703 | } |
| 704 | |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 705 | /* retrieve an LDT selector entry */ |
| 706 | static void get_selector_entry( struct thread *thread, int entry, |
| 707 | unsigned int *base, unsigned int *limit, |
| 708 | unsigned char *flags ) |
| 709 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 710 | if (!thread->process->ldt_copy) |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 711 | { |
| 712 | set_error( STATUS_ACCESS_DENIED ); |
| 713 | return; |
| 714 | } |
| 715 | if (entry >= 8192) |
| 716 | { |
| 717 | set_error( STATUS_INVALID_PARAMETER ); /* FIXME */ |
| 718 | return; |
| 719 | } |
Alexandre Julliard | 98aacc7 | 2000-03-15 19:46:14 +0000 | [diff] [blame] | 720 | if (suspend_for_ptrace( thread )) |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 721 | { |
| 722 | unsigned char flags_buf[4]; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 723 | int *addr = (int *)thread->process->ldt_copy + entry; |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 724 | if (read_thread_int( thread, addr, base ) == -1) goto done; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 725 | if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done; |
| 726 | addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2); |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 727 | if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done; |
| 728 | *flags = flags_buf[entry & 3]; |
Alexandre Julliard | 98aacc7 | 2000-03-15 19:46:14 +0000 | [diff] [blame] | 729 | done: |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 730 | resume_after_ptrace( thread ); |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 731 | } |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 734 | /* kill a thread on the spot */ |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 735 | void kill_thread( struct thread *thread, int violent_death ) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 736 | { |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 737 | if (thread->state == TERMINATED) return; /* already killed */ |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 738 | thread->state = TERMINATED; |
Ryan Cumming | 24f4ece | 2002-11-25 01:33:38 +0000 | [diff] [blame] | 739 | thread->exit_time = time(NULL); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 740 | if (current == thread) current = NULL; |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 741 | if (debug_level) |
Alexandre Julliard | 91befe1 | 2003-02-01 01:38:40 +0000 | [diff] [blame] | 742 | fprintf( stderr,"%04x: *killed* exit_code=%d\n", |
| 743 | thread->id, thread->exit_code ); |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 744 | if (thread->wait) |
| 745 | { |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 746 | while (thread->wait) end_wait( thread ); |
| 747 | send_thread_wakeup( thread, NULL, STATUS_PENDING ); |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 748 | /* if it is waiting on the socket, we don't need to send a SIGTERM */ |
| 749 | violent_death = 0; |
| 750 | } |
Eric Pouech | 3940d8a | 2001-12-04 20:17:43 +0000 | [diff] [blame] | 751 | kill_console_processes( thread, 0 ); |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 752 | debug_exit_thread( thread ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 753 | abandon_mutexes( thread ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 754 | remove_process_thread( thread->process, thread ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 755 | wake_up( &thread->obj, 0 ); |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 756 | detach_thread( thread, violent_death ? SIGTERM : 0 ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 757 | cleanup_thread( thread ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 758 | release_object( thread ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 759 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 760 | |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 761 | /* take a snapshot of currently running threads */ |
| 762 | struct thread_snapshot *thread_snap( int *count ) |
| 763 | { |
| 764 | struct thread_snapshot *snapshot, *ptr; |
| 765 | struct thread *thread; |
| 766 | int total = 0; |
| 767 | |
| 768 | for (thread = first_thread; thread; thread = thread->next) |
| 769 | if (thread->state != TERMINATED) total++; |
| 770 | if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL; |
| 771 | ptr = snapshot; |
| 772 | for (thread = first_thread; thread; thread = thread->next) |
| 773 | { |
| 774 | if (thread->state == TERMINATED) continue; |
| 775 | ptr->thread = thread; |
| 776 | ptr->count = thread->obj.refcount; |
| 777 | ptr->priority = thread->priority; |
| 778 | grab_object( thread ); |
| 779 | ptr++; |
| 780 | } |
| 781 | *count = total; |
| 782 | return snapshot; |
| 783 | } |
| 784 | |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 785 | /* signal that we are finished booting on the client side */ |
| 786 | DECL_HANDLER(boot_done) |
| 787 | { |
Alexandre Julliard | f6507ed | 2000-04-06 22:05:16 +0000 | [diff] [blame] | 788 | debug_level = max( debug_level, req->debug_level ); |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 789 | if (current == booting_thread) |
| 790 | { |
| 791 | booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */ |
| 792 | lock_master_socket(0); /* allow other clients now */ |
| 793 | } |
| 794 | } |
| 795 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 796 | /* create a new thread */ |
| 797 | DECL_HANDLER(new_thread) |
| 798 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 799 | struct thread *thread; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 800 | int request_fd = thread_get_inflight_fd( current, req->request_fd ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 801 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 802 | if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 803 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 804 | if (request_fd != -1) close( request_fd ); |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 805 | set_error( STATUS_INVALID_HANDLE ); |
| 806 | return; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 807 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 808 | |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 809 | if ((thread = create_thread( request_fd, current->process ))) |
| 810 | { |
| 811 | if (req->suspend) thread->suspend++; |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 812 | reply->tid = get_thread_id( thread ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 813 | if ((reply->handle = alloc_handle( current->process, thread, |
| 814 | THREAD_ALL_ACCESS, req->inherit ))) |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 815 | { |
| 816 | /* thread object will be released when the thread gets killed */ |
| 817 | return; |
| 818 | } |
| 819 | kill_thread( thread, 1 ); |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 820 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /* initialize a new thread */ |
| 824 | DECL_HANDLER(init_thread) |
| 825 | { |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 826 | int reply_fd = thread_get_inflight_fd( current, req->reply_fd ); |
| 827 | int wait_fd = thread_get_inflight_fd( current, req->wait_fd ); |
| 828 | |
Alexandre Julliard | 02a53c1 | 2003-02-25 04:17:22 +0000 | [diff] [blame] | 829 | if (current->unix_pid != -1) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 830 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 831 | fatal_protocol_error( current, "init_thread: already running\n" ); |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 832 | goto error; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 833 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 834 | if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 835 | { |
| 836 | fatal_protocol_error( current, "bad reply fd\n" ); |
| 837 | goto error; |
| 838 | } |
| 839 | if (wait_fd == -1) |
| 840 | { |
| 841 | fatal_protocol_error( current, "bad wait fd\n" ); |
| 842 | goto error; |
| 843 | } |
Alexandre Julliard | 3c68ab0 | 2003-10-27 22:10:22 +0000 | [diff] [blame] | 844 | if (!(current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, ¤t->obj ))) |
| 845 | { |
| 846 | reply_fd = -1; |
| 847 | fatal_protocol_error( current, "could not allocate reply fd\n" ); |
| 848 | goto error; |
| 849 | } |
| 850 | if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, ¤t->obj ))) |
| 851 | return; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 852 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 853 | current->unix_pid = req->unix_pid; |
Alexandre Julliard | a8497bd | 2003-03-22 21:00:09 +0000 | [diff] [blame] | 854 | current->unix_tid = req->unix_tid; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 855 | current->teb = req->teb; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 856 | |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 857 | if (current->suspend + current->process->suspend > 0) stop_thread( current ); |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 858 | if (current->process->running_threads > 1) |
Alexandre Julliard | b73421d | 2000-03-30 19:30:24 +0000 | [diff] [blame] | 859 | generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry ); |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 860 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 861 | reply->pid = get_process_id( current->process ); |
| 862 | reply->tid = get_thread_id( current ); |
| 863 | reply->boot = (current == booting_thread); |
| 864 | reply->version = SERVER_PROTOCOL_VERSION; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 865 | return; |
| 866 | |
| 867 | error: |
| 868 | if (reply_fd != -1) close( reply_fd ); |
| 869 | if (wait_fd != -1) close( wait_fd ); |
| 870 | } |
| 871 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 872 | /* terminate a thread */ |
| 873 | DECL_HANDLER(terminate_thread) |
| 874 | { |
| 875 | struct thread *thread; |
| 876 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 877 | reply->self = 0; |
| 878 | reply->last = 0; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 879 | if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE ))) |
| 880 | { |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 881 | thread->exit_code = req->exit_code; |
| 882 | if (thread != current) kill_thread( thread, 1 ); |
| 883 | else |
| 884 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 885 | reply->self = 1; |
| 886 | reply->last = (thread->process->running_threads == 1); |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 887 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 888 | release_object( thread ); |
| 889 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Chris Morgan | 417296c | 2002-04-02 00:49:05 +0000 | [diff] [blame] | 892 | /* open a handle to a thread */ |
| 893 | DECL_HANDLER(open_thread) |
| 894 | { |
| 895 | struct thread *thread = get_thread_from_id( req->tid ); |
| 896 | |
| 897 | reply->handle = 0; |
| 898 | if (thread) |
| 899 | { |
| 900 | reply->handle = alloc_handle( current->process, thread, req->access, req->inherit ); |
| 901 | release_object( thread ); |
| 902 | } |
| 903 | } |
| 904 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 905 | /* fetch information about a thread */ |
| 906 | DECL_HANDLER(get_thread_info) |
| 907 | { |
| 908 | struct thread *thread; |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 909 | obj_handle_t handle = req->handle; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 910 | |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 911 | if (!handle) thread = get_thread_from_id( req->tid_in ); |
Alexandre Julliard | 9a0e28f | 2000-03-25 19:14:37 +0000 | [diff] [blame] | 912 | else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION ); |
| 913 | |
| 914 | if (thread) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 915 | { |
Alexandre Julliard | 4f196ea | 2003-07-09 02:57:57 +0000 | [diff] [blame] | 916 | reply->pid = get_process_id( thread->process ); |
Ryan Cumming | 24f4ece | 2002-11-25 01:33:38 +0000 | [diff] [blame] | 917 | reply->tid = get_thread_id( thread ); |
| 918 | reply->teb = thread->teb; |
| 919 | reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE; |
| 920 | reply->priority = thread->priority; |
Alexandre Julliard | 4f196ea | 2003-07-09 02:57:57 +0000 | [diff] [blame] | 921 | reply->affinity = thread->affinity; |
Ryan Cumming | 24f4ece | 2002-11-25 01:33:38 +0000 | [diff] [blame] | 922 | reply->creation_time = thread->creation_time; |
| 923 | reply->exit_time = thread->exit_time; |
| 924 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 925 | release_object( thread ); |
| 926 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | /* set information about a thread */ |
| 930 | DECL_HANDLER(set_thread_info) |
| 931 | { |
| 932 | struct thread *thread; |
| 933 | |
| 934 | if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION ))) |
| 935 | { |
| 936 | set_thread_info( thread, req ); |
| 937 | release_object( thread ); |
| 938 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* suspend a thread */ |
| 942 | DECL_HANDLER(suspend_thread) |
| 943 | { |
| 944 | struct thread *thread; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 945 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 946 | if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) |
| 947 | { |
François Gouget | f666257 | 2002-04-02 02:53:45 +0000 | [diff] [blame] | 948 | if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 949 | else reply->count = suspend_thread( thread ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 950 | release_object( thread ); |
| 951 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* resume a thread */ |
| 955 | DECL_HANDLER(resume_thread) |
| 956 | { |
| 957 | struct thread *thread; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 958 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 959 | if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) |
| 960 | { |
François Gouget | f666257 | 2002-04-02 02:53:45 +0000 | [diff] [blame] | 961 | if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED ); |
| 962 | else reply->count = resume_thread( thread ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 963 | release_object( thread ); |
| 964 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /* select on a handle list */ |
| 968 | DECL_HANDLER(select) |
| 969 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 970 | int count = get_req_data_size() / sizeof(int); |
Alexandre Julliard | 462172a | 2003-04-02 22:48:59 +0000 | [diff] [blame] | 971 | select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /* queue an APC for a thread */ |
| 975 | DECL_HANDLER(queue_apc) |
| 976 | { |
| 977 | struct thread *thread; |
| 978 | if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) |
| 979 | { |
Alexandre Julliard | 088bcf9 | 2003-04-04 22:26:34 +0000 | [diff] [blame] | 980 | thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user, |
| 981 | req->arg1, req->arg2, req->arg3 ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 982 | release_object( thread ); |
| 983 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 984 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 985 | |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 986 | /* get next APC to call */ |
| 987 | DECL_HANDLER(get_apc) |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 988 | { |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 989 | struct thread_apc *apc; |
| 990 | |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 991 | for (;;) |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 992 | { |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 993 | if (!(apc = thread_dequeue_apc( current, !req->alertable ))) |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 994 | { |
| 995 | /* no more APCs */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 996 | reply->func = NULL; |
| 997 | reply->type = APC_NONE; |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 998 | return; |
| 999 | } |
| 1000 | /* Optimization: ignore APCs that have a NULL func; they are only used |
| 1001 | * to wake up a thread, but since we got here the thread woke up already. |
Martin Wilck | 2b47fb3 | 2002-04-05 22:53:57 +0000 | [diff] [blame] | 1002 | * Exception: for APC_ASYNC_IO, func == NULL is legal. |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 1003 | */ |
Martin Wilck | 2b47fb3 | 2002-04-05 22:53:57 +0000 | [diff] [blame] | 1004 | if (apc->func || apc->type == APC_ASYNC_IO) break; |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 1005 | free( apc ); |
| 1006 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1007 | reply->func = apc->func; |
| 1008 | reply->type = apc->type; |
Alexandre Julliard | 088bcf9 | 2003-04-04 22:26:34 +0000 | [diff] [blame] | 1009 | reply->arg1 = apc->arg1; |
| 1010 | reply->arg2 = apc->arg2; |
| 1011 | reply->arg3 = apc->arg3; |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 1012 | free( apc ); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 1013 | } |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 1014 | |
| 1015 | /* fetch a selector entry for a thread */ |
| 1016 | DECL_HANDLER(get_selector_entry) |
| 1017 | { |
| 1018 | struct thread *thread; |
| 1019 | if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION ))) |
| 1020 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1021 | get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags ); |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 1022 | release_object( thread ); |
| 1023 | } |
| 1024 | } |