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