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 |
| 5 | */ |
| 6 | |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 7 | #include "config.h" |
| 8 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <fcntl.h> |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 11 | #include <signal.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 15 | #ifdef HAVE_SYS_MMAN_H |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 16 | #include <sys/mman.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 17 | #endif |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 18 | #include <sys/types.h> |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 19 | #ifdef HAVE_SYS_SOCKET_H |
| 20 | # include <sys/socket.h> |
| 21 | #endif |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 22 | #include <sys/uio.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
Michael Veksler | f935c59 | 1999-02-09 15:49:39 +0000 | [diff] [blame] | 24 | #include <stdarg.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 25 | |
Michael Veksler | f935c59 | 1999-02-09 15:49:39 +0000 | [diff] [blame] | 26 | |
| 27 | #include "winbase.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 28 | #include "winerror.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 29 | |
| 30 | #include "handle.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 31 | #include "process.h" |
| 32 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 33 | #include "request.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 34 | |
| 35 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 36 | /* thread queues */ |
| 37 | |
| 38 | struct wait_queue_entry |
| 39 | { |
| 40 | struct wait_queue_entry *next; |
| 41 | struct wait_queue_entry *prev; |
| 42 | struct object *obj; |
| 43 | struct thread *thread; |
| 44 | }; |
| 45 | |
| 46 | struct thread_wait |
| 47 | { |
| 48 | int count; /* count of objects */ |
| 49 | int flags; |
| 50 | struct timeval timeout; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 51 | struct timeout_user *user; |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 52 | sleep_reply reply; /* function to build the reply */ |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 53 | struct wait_queue_entry queues[1]; |
| 54 | }; |
| 55 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 56 | /* asynchronous procedure calls */ |
| 57 | |
| 58 | struct thread_apc |
| 59 | { |
| 60 | void *func; /* function to call in client */ |
| 61 | void *param; /* function param */ |
| 62 | }; |
| 63 | #define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */ |
| 64 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 65 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 66 | /* thread operations */ |
| 67 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 68 | static void dump_thread( struct object *obj, int verbose ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 69 | static int thread_signaled( struct object *obj, struct thread *thread ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 70 | extern void thread_poll_event( struct object *obj, int event ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 71 | static void destroy_thread( struct object *obj ); |
| 72 | |
| 73 | static const struct object_ops thread_ops = |
| 74 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 75 | sizeof(struct thread), /* size */ |
| 76 | dump_thread, /* dump */ |
| 77 | add_queue, /* add_queue */ |
| 78 | remove_queue, /* remove_queue */ |
| 79 | thread_signaled, /* signaled */ |
| 80 | no_satisfied, /* satisfied */ |
| 81 | NULL, /* get_poll_events */ |
| 82 | thread_poll_event, /* poll_event */ |
| 83 | no_read_fd, /* get_read_fd */ |
| 84 | no_write_fd, /* get_write_fd */ |
| 85 | no_flush, /* flush */ |
| 86 | no_get_file_info, /* get_file_info */ |
| 87 | destroy_thread /* destroy */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 90 | static struct thread *first_thread; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 91 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 92 | /* allocate the buffer for the communication with the client */ |
| 93 | static int alloc_client_buffer( struct thread *thread ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 94 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 95 | int fd; |
| 96 | |
| 97 | if ((fd = create_anonymous_file()) == -1) return -1; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 98 | if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error; |
| 99 | if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE, |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 100 | MAP_SHARED, fd, 0 )) == (void*)-1) goto error; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 101 | return fd; |
| 102 | |
| 103 | error: |
| 104 | file_set_error(); |
| 105 | if (fd != -1) close( fd ); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | /* create a new thread */ |
| 110 | static struct thread *create_thread( int fd, struct process *process, int suspend ) |
| 111 | { |
| 112 | struct thread *thread; |
| 113 | int buf_fd; |
| 114 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 115 | int flags = fcntl( fd, F_GETFL, 0 ); |
| 116 | fcntl( fd, F_SETFL, flags | O_NONBLOCK ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 117 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 118 | if (!(thread = alloc_object( &thread_ops, fd ))) return NULL; |
| 119 | |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 120 | thread->unix_pid = 0; /* not known yet */ |
| 121 | thread->teb = NULL; |
| 122 | thread->mutex = NULL; |
| 123 | thread->debug_ctx = NULL; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 124 | thread->wait = NULL; |
| 125 | thread->apc = NULL; |
| 126 | thread->apc_count = 0; |
| 127 | thread->error = 0; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 128 | thread->pass_fd = -1; |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 129 | thread->state = RUNNING; |
| 130 | thread->attached = 0; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 131 | thread->exit_code = 0x103; /* STILL_ACTIVE */ |
| 132 | thread->next = NULL; |
| 133 | thread->prev = NULL; |
| 134 | thread->priority = THREAD_PRIORITY_NORMAL; |
| 135 | thread->affinity = 1; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 136 | thread->suspend = (suspend != 0); |
| 137 | thread->buffer = (void *)-1; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 138 | thread->last_req = REQ_GET_THREAD_BUFFER; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 139 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 140 | if (!first_thread) /* creating the first thread */ |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 141 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 142 | current = thread; |
| 143 | thread->process = process = create_initial_process(); |
| 144 | assert( process ); |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 145 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 146 | else thread->process = (struct process *)grab_object( process ); |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 147 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 148 | if ((thread->next = first_thread) != NULL) thread->next->prev = thread; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 149 | first_thread = thread; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 150 | add_process_thread( process, thread ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 151 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 152 | if ((buf_fd = alloc_client_buffer( thread )) == -1) goto error; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 153 | |
| 154 | set_select_events( &thread->obj, POLLIN ); /* start listening to events */ |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 155 | set_reply_fd( thread, buf_fd ); /* send the fd to the client */ |
| 156 | send_reply( thread ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 157 | return thread; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 158 | |
| 159 | error: |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 160 | remove_process_thread( process, thread ); |
| 161 | release_object( thread ); |
| 162 | return NULL; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 165 | /* create the initial thread and start the main server loop */ |
| 166 | void create_initial_thread( int fd ) |
| 167 | { |
| 168 | create_thread( fd, NULL, 0 ); |
| 169 | select_loop(); |
| 170 | } |
| 171 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 172 | /* handle a client event */ |
| 173 | void thread_poll_event( struct object *obj, int event ) |
| 174 | { |
| 175 | struct thread *thread = (struct thread *)obj; |
| 176 | assert( obj->ops == &thread_ops ); |
| 177 | |
| 178 | if (event & (POLLERR | POLLHUP)) kill_thread( thread, BROKEN_PIPE ); |
| 179 | else |
| 180 | { |
| 181 | if (event & POLLOUT) write_request( thread ); |
| 182 | if (event & POLLIN) read_request( thread ); |
| 183 | } |
| 184 | } |
| 185 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 186 | /* destroy a thread when its refcount is 0 */ |
| 187 | static void destroy_thread( struct object *obj ) |
| 188 | { |
| 189 | struct thread *thread = (struct thread *)obj; |
| 190 | assert( obj->ops == &thread_ops ); |
| 191 | |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 192 | assert( !thread->debug_ctx ); /* cannot still be debugging something */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 193 | release_object( thread->process ); |
| 194 | if (thread->next) thread->next->prev = thread->prev; |
| 195 | if (thread->prev) thread->prev->next = thread->next; |
| 196 | else first_thread = thread->next; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 197 | if (thread->apc) free( thread->apc ); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 198 | if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 199 | if (thread->pass_fd != -1) close( thread->pass_fd ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 202 | /* dump a thread on stdout for debugging purposes */ |
| 203 | static void dump_thread( struct object *obj, int verbose ) |
| 204 | { |
| 205 | struct thread *thread = (struct thread *)obj; |
| 206 | assert( obj->ops == &thread_ops ); |
| 207 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 208 | fprintf( stderr, "Thread pid=%d teb=%p state=%d\n", |
| 209 | thread->unix_pid, thread->teb, thread->state ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 212 | static int thread_signaled( struct object *obj, struct thread *thread ) |
| 213 | { |
| 214 | struct thread *mythread = (struct thread *)obj; |
| 215 | return (mythread->state == TERMINATED); |
| 216 | } |
| 217 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 218 | /* get a thread pointer from a thread id (and increment the refcount) */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 219 | struct thread *get_thread_from_id( void *id ) |
| 220 | { |
| 221 | struct thread *t = first_thread; |
| 222 | while (t && (t != id)) t = t->next; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 223 | if (t) grab_object( t ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 224 | return t; |
| 225 | } |
| 226 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 227 | /* get a thread from a handle (and increment the refcount) */ |
| 228 | struct thread *get_thread_from_handle( int handle, unsigned int access ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 229 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 230 | return (struct thread *)get_handle_obj( current->process, handle, |
| 231 | access, &thread_ops ); |
| 232 | } |
| 233 | |
Alexandre Julliard | 578c100 | 1999-11-14 21:23:21 +0000 | [diff] [blame] | 234 | /* find a thread from a Unix pid */ |
| 235 | struct thread *get_thread_from_pid( int pid ) |
| 236 | { |
| 237 | struct thread *t = first_thread; |
| 238 | while (t && (t->unix_pid != pid)) t = t->next; |
| 239 | return t; |
| 240 | } |
| 241 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 242 | /* set all information about a thread */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 243 | static void set_thread_info( struct thread *thread, |
| 244 | struct set_thread_info_request *req ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 245 | { |
| 246 | if (req->mask & SET_THREAD_INFO_PRIORITY) |
| 247 | thread->priority = req->priority; |
| 248 | if (req->mask & SET_THREAD_INFO_AFFINITY) |
| 249 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 250 | if (req->affinity != 1) set_error( ERROR_INVALID_PARAMETER ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 251 | else thread->affinity = req->affinity; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* suspend a thread */ |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 256 | int suspend_thread( struct thread *thread, int check_limit ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 257 | { |
| 258 | int old_count = thread->suspend; |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 259 | if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 260 | { |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 261 | if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 262 | } |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 263 | else set_error( ERROR_SIGNAL_REFUSED ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 264 | return old_count; |
| 265 | } |
| 266 | |
| 267 | /* resume a thread */ |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 268 | int resume_thread( struct thread *thread ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 269 | { |
| 270 | int old_count = thread->suspend; |
| 271 | if (thread->suspend > 0) |
| 272 | { |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 273 | if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 274 | } |
| 275 | return old_count; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Ulrich Weigand | 371fd75 | 1999-04-11 17:13:03 +0000 | [diff] [blame] | 278 | /* suspend all threads but the current */ |
| 279 | void suspend_all_threads( void ) |
| 280 | { |
| 281 | struct thread *thread; |
| 282 | for ( thread = first_thread; thread; thread = thread->next ) |
| 283 | if ( thread != current ) |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 284 | suspend_thread( thread, 0 ); |
Ulrich Weigand | 371fd75 | 1999-04-11 17:13:03 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* resume all threads but the current */ |
| 288 | void resume_all_threads( void ) |
| 289 | { |
| 290 | struct thread *thread; |
| 291 | for ( thread = first_thread; thread; thread = thread->next ) |
| 292 | if ( thread != current ) |
| 293 | resume_thread( thread ); |
| 294 | } |
| 295 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 296 | /* 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] | 297 | int add_queue( struct object *obj, struct wait_queue_entry *entry ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 298 | { |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 299 | grab_object( obj ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 300 | entry->obj = obj; |
| 301 | entry->prev = obj->tail; |
| 302 | entry->next = NULL; |
| 303 | if (obj->tail) obj->tail->next = entry; |
| 304 | else obj->head = entry; |
| 305 | obj->tail = entry; |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 306 | return 1; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* remove a thread from an object wait queue */ |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 310 | void remove_queue( struct object *obj, struct wait_queue_entry *entry ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 311 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 312 | if (entry->next) entry->next->prev = entry->prev; |
| 313 | else obj->tail = entry->prev; |
| 314 | if (entry->prev) entry->prev->next = entry->next; |
| 315 | else obj->head = entry->next; |
| 316 | release_object( obj ); |
| 317 | } |
| 318 | |
| 319 | /* finish waiting */ |
| 320 | static void end_wait( struct thread *thread ) |
| 321 | { |
| 322 | struct thread_wait *wait = thread->wait; |
| 323 | struct wait_queue_entry *entry; |
| 324 | int i; |
| 325 | |
| 326 | assert( wait ); |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 327 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 328 | entry->obj->ops->remove_queue( entry->obj, entry ); |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 329 | if (wait->user) remove_timeout_user( wait->user ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 330 | free( wait ); |
| 331 | thread->wait = NULL; |
| 332 | } |
| 333 | |
| 334 | /* build the thread wait structure */ |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 335 | static int wait_on( int count, struct object *objects[], int flags, |
| 336 | int timeout, sleep_reply func ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 337 | { |
| 338 | struct thread_wait *wait; |
| 339 | struct wait_queue_entry *entry; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 340 | int i; |
| 341 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 342 | if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0; |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 343 | current->wait = wait; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 344 | wait->count = count; |
| 345 | wait->flags = flags; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 346 | wait->user = NULL; |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 347 | wait->reply = func; |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 348 | if (flags & SELECT_TIMEOUT) |
| 349 | { |
| 350 | gettimeofday( &wait->timeout, 0 ); |
| 351 | add_timeout( &wait->timeout, timeout ); |
| 352 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 353 | |
| 354 | for (i = 0, entry = wait->queues; i < count; i++, entry++) |
| 355 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 356 | struct object *obj = objects[i]; |
| 357 | entry->thread = current; |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 358 | if (!obj->ops->add_queue( obj, entry )) |
| 359 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 360 | wait->count = i; |
| 361 | end_wait( current ); |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 362 | return 0; |
| 363 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 364 | } |
| 365 | return 1; |
| 366 | } |
| 367 | |
| 368 | /* check if the thread waiting condition is satisfied */ |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 369 | static int check_wait( struct thread *thread, struct object **object ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 370 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 371 | int i, signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 372 | struct thread_wait *wait = thread->wait; |
| 373 | struct wait_queue_entry *entry = wait->queues; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 374 | |
| 375 | assert( wait ); |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 376 | *object = NULL; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 377 | if (wait->flags & SELECT_ALL) |
| 378 | { |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 379 | int not_ok = 0; |
| 380 | /* Note: we must check them all anyway, as some objects may |
| 381 | * want to do something when signaled, even if others are not */ |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 382 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 383 | not_ok |= !entry->obj->ops->signaled( entry->obj, thread ); |
| 384 | if (not_ok) goto other_checks; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 385 | /* Wait satisfied: tell it to all objects */ |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 386 | signaled = 0; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 387 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 388 | if (entry->obj->ops->satisfied( entry->obj, thread )) |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 389 | signaled = STATUS_ABANDONED_WAIT_0; |
| 390 | return signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 391 | } |
| 392 | else |
| 393 | { |
| 394 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 395 | { |
| 396 | if (!entry->obj->ops->signaled( entry->obj, thread )) continue; |
| 397 | /* Wait satisfied: tell it to the object */ |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 398 | signaled = i; |
| 399 | *object = entry->obj; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 400 | if (entry->obj->ops->satisfied( entry->obj, thread )) |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 401 | signaled = i + STATUS_ABANDONED_WAIT_0; |
| 402 | return signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 405 | |
| 406 | other_checks: |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 407 | if ((wait->flags & SELECT_ALERTABLE) && thread->apc) return STATUS_USER_APC; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 408 | if (wait->flags & SELECT_TIMEOUT) |
| 409 | { |
| 410 | struct timeval now; |
| 411 | gettimeofday( &now, NULL ); |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 412 | if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 413 | } |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | /* build a reply to the select request */ |
| 418 | static void build_select_reply( struct thread *thread, struct object *obj, int signaled ) |
| 419 | { |
| 420 | struct select_request *req = get_req_ptr( thread ); |
| 421 | req->signaled = signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 424 | /* attempt to wake up a thread */ |
| 425 | /* return 1 if OK, 0 if the wait condition is still not satisfied */ |
| 426 | static int wake_thread( struct thread *thread ) |
| 427 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 428 | int signaled; |
| 429 | struct object *object; |
| 430 | if ((signaled = check_wait( thread, &object )) == -1) return 0; |
| 431 | thread->error = 0; |
| 432 | thread->wait->reply( thread, object, signaled ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 433 | end_wait( thread ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 434 | return 1; |
| 435 | } |
| 436 | |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 437 | /* thread wait timeout */ |
| 438 | static void thread_timeout( void *ptr ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 439 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 440 | struct thread *thread = ptr; |
| 441 | if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread ); |
| 442 | assert( thread->wait ); |
| 443 | thread->error = 0; |
| 444 | thread->wait->user = NULL; |
| 445 | thread->wait->reply( thread, NULL, STATUS_TIMEOUT ); |
| 446 | end_wait( thread ); |
| 447 | send_reply( thread ); |
| 448 | } |
| 449 | |
| 450 | /* sleep on a list of objects */ |
| 451 | int sleep_on( int count, struct object *objects[], int flags, int timeout, sleep_reply func ) |
| 452 | { |
| 453 | assert( !current->wait ); |
| 454 | if (!wait_on( count, objects, flags, timeout, func )) return 0; |
| 455 | if (wake_thread( current )) return 1; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 456 | /* now we need to wait */ |
| 457 | if (flags & SELECT_TIMEOUT) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 458 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 459 | if (!(current->wait->user = add_timeout_user( ¤t->wait->timeout, |
| 460 | thread_timeout, current ))) |
| 461 | { |
| 462 | end_wait( current ); |
| 463 | return 0; |
| 464 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 465 | } |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 466 | return 1; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 469 | /* select on a list of handles */ |
| 470 | static int select_on( int count, int *handles, int flags, int timeout ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 471 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 472 | int ret = 0; |
| 473 | int i; |
| 474 | struct object *objects[MAXIMUM_WAIT_OBJECTS]; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 475 | |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 476 | if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS)) |
| 477 | { |
| 478 | set_error( ERROR_INVALID_PARAMETER ); |
| 479 | return 0; |
| 480 | } |
| 481 | for (i = 0; i < count; i++) |
| 482 | { |
| 483 | if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL ))) |
| 484 | break; |
| 485 | } |
| 486 | if (i == count) ret = sleep_on( count, objects, flags, timeout, build_select_reply ); |
| 487 | while (--i >= 0) release_object( objects[i] ); |
| 488 | return ret; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /* attempt to wake threads sleeping on the object wait queue */ |
| 492 | void wake_up( struct object *obj, int max ) |
| 493 | { |
| 494 | struct wait_queue_entry *entry = obj->head; |
| 495 | |
| 496 | while (entry) |
| 497 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 498 | struct thread *thread = entry->thread; |
| 499 | entry = entry->next; |
| 500 | if (wake_thread( thread )) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 501 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 502 | send_reply( thread ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 503 | if (max && !--max) break; |
| 504 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 508 | /* queue an async procedure call */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 509 | static int thread_queue_apc( struct thread *thread, void *func, void *param ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 510 | { |
| 511 | struct thread_apc *apc; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 512 | if (!thread->apc) |
| 513 | { |
| 514 | if (!(thread->apc = mem_alloc( MAX_THREAD_APC * sizeof(*apc) ))) |
| 515 | return 0; |
| 516 | thread->apc_count = 0; |
| 517 | } |
| 518 | else if (thread->apc_count >= MAX_THREAD_APC) return 0; |
| 519 | thread->apc[thread->apc_count].func = func; |
| 520 | thread->apc[thread->apc_count].param = param; |
| 521 | thread->apc_count++; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 522 | if (thread->wait) |
| 523 | { |
| 524 | if (wake_thread( thread )) send_reply( thread ); |
| 525 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 526 | return 1; |
| 527 | } |
| 528 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 529 | /* kill a thread on the spot */ |
| 530 | void kill_thread( struct thread *thread, int exit_code ) |
| 531 | { |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 532 | if (thread->state == TERMINATED) return; /* already killed */ |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 533 | thread->state = TERMINATED; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 534 | thread->exit_code = exit_code; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 535 | if (current == thread) current = NULL; |
| 536 | if (debug_level) trace_kill( thread ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 537 | if (thread->wait) end_wait( thread ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 538 | debug_exit_thread( thread, exit_code ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 539 | abandon_mutexes( thread ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 540 | remove_process_thread( thread->process, thread ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 541 | wake_up( &thread->obj, 0 ); |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 542 | detach_thread( thread ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 543 | remove_select_user( &thread->obj ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 544 | release_object( thread ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 545 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 546 | |
| 547 | /* create a new thread */ |
| 548 | DECL_HANDLER(new_thread) |
| 549 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 550 | struct thread *thread; |
| 551 | struct process *process; |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 552 | int sock[2]; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 553 | |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 554 | if (!(process = get_process_from_id( req->pid ))) return; |
| 555 | |
| 556 | if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 557 | { |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 558 | if ((thread = create_thread( sock[0], process, req->suspend ))) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 559 | { |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 560 | req->tid = thread; |
| 561 | if ((req->handle = alloc_handle( current->process, thread, |
| 562 | THREAD_ALL_ACCESS, req->inherit )) != -1) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 563 | { |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 564 | set_reply_fd( current, sock[1] ); |
| 565 | release_object( process ); |
| 566 | /* thread object will be released when the thread gets killed */ |
| 567 | return; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 568 | } |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 569 | release_object( thread ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 570 | } |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 571 | close( sock[1] ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 572 | } |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 573 | else file_set_error(); |
| 574 | release_object( process ); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* retrieve the thread buffer file descriptor */ |
| 578 | DECL_HANDLER(get_thread_buffer) |
| 579 | { |
| 580 | fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* initialize a new thread */ |
| 584 | DECL_HANDLER(init_thread) |
| 585 | { |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 586 | if (current->unix_pid) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 587 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 588 | fatal_protocol_error( current, "init_thread: already running\n" ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 589 | return; |
| 590 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 591 | current->unix_pid = req->unix_pid; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 592 | current->teb = req->teb; |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 593 | if (current->suspend + current->process->suspend > 0) stop_thread( current ); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 594 | req->pid = current->process; |
| 595 | req->tid = current; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* terminate a thread */ |
| 599 | DECL_HANDLER(terminate_thread) |
| 600 | { |
| 601 | struct thread *thread; |
| 602 | |
| 603 | if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE ))) |
| 604 | { |
| 605 | kill_thread( thread, req->exit_code ); |
| 606 | release_object( thread ); |
| 607 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* fetch information about a thread */ |
| 611 | DECL_HANDLER(get_thread_info) |
| 612 | { |
| 613 | struct thread *thread; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 614 | |
| 615 | if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION ))) |
| 616 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 617 | req->tid = thread; |
| 618 | req->exit_code = thread->exit_code; |
| 619 | req->priority = thread->priority; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 620 | release_object( thread ); |
| 621 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* set information about a thread */ |
| 625 | DECL_HANDLER(set_thread_info) |
| 626 | { |
| 627 | struct thread *thread; |
| 628 | |
| 629 | if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION ))) |
| 630 | { |
| 631 | set_thread_info( thread, req ); |
| 632 | release_object( thread ); |
| 633 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /* suspend a thread */ |
| 637 | DECL_HANDLER(suspend_thread) |
| 638 | { |
| 639 | struct thread *thread; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 640 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 641 | if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) |
| 642 | { |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 643 | req->count = suspend_thread( thread, 1 ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 644 | release_object( thread ); |
| 645 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* resume a thread */ |
| 649 | DECL_HANDLER(resume_thread) |
| 650 | { |
| 651 | struct thread *thread; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 652 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 653 | if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) |
| 654 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 655 | req->count = resume_thread( thread ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 656 | release_object( thread ); |
| 657 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /* select on a handle list */ |
| 661 | DECL_HANDLER(select) |
| 662 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 663 | if (!select_on( req->count, req->handles, req->flags, req->timeout )) |
| 664 | req->signaled = -1; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* queue an APC for a thread */ |
| 668 | DECL_HANDLER(queue_apc) |
| 669 | { |
| 670 | struct thread *thread; |
| 671 | if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) |
| 672 | { |
| 673 | thread_queue_apc( thread, req->func, req->param ); |
| 674 | release_object( thread ); |
| 675 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 676 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 677 | |
| 678 | /* get list of APC to call */ |
| 679 | DECL_HANDLER(get_apcs) |
| 680 | { |
| 681 | if ((req->count = current->apc_count)) |
| 682 | { |
| 683 | memcpy( req->apcs, current->apc, current->apc_count * sizeof(*current->apc) ); |
| 684 | free( current->apc ); |
| 685 | current->apc = NULL; |
| 686 | current->apc_count = 0; |
| 687 | } |
| 688 | } |