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