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 | |
| 7 | #include <assert.h> |
| 8 | #include <fcntl.h> |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 9 | #include <signal.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 13 | #include <sys/types.h> |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 14 | #include <sys/uio.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
| 16 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 17 | #include "winnt.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 18 | #include "winerror.h" |
| 19 | #include "server.h" |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 20 | #include "server/thread.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 21 | |
| 22 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 23 | /* thread queues */ |
| 24 | |
| 25 | struct wait_queue_entry |
| 26 | { |
| 27 | struct wait_queue_entry *next; |
| 28 | struct wait_queue_entry *prev; |
| 29 | struct object *obj; |
| 30 | struct thread *thread; |
| 31 | }; |
| 32 | |
| 33 | struct thread_wait |
| 34 | { |
| 35 | int count; /* count of objects */ |
| 36 | int flags; |
| 37 | struct timeval timeout; |
| 38 | struct wait_queue_entry queues[1]; |
| 39 | }; |
| 40 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 41 | /* asynchronous procedure calls */ |
| 42 | |
| 43 | struct thread_apc |
| 44 | { |
| 45 | void *func; /* function to call in client */ |
| 46 | void *param; /* function param */ |
| 47 | }; |
| 48 | #define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */ |
| 49 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 50 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 51 | /* thread operations */ |
| 52 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 53 | static void dump_thread( struct object *obj, int verbose ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 54 | static int thread_signaled( struct object *obj, struct thread *thread ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 55 | static void destroy_thread( struct object *obj ); |
| 56 | |
| 57 | static const struct object_ops thread_ops = |
| 58 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 59 | dump_thread, |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 60 | add_queue, |
| 61 | remove_queue, |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 62 | thread_signaled, |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 63 | no_satisfied, |
| 64 | no_read_fd, |
| 65 | no_write_fd, |
| 66 | no_flush, |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 67 | no_get_file_info, |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 68 | destroy_thread |
| 69 | }; |
| 70 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 71 | static struct thread *first_thread; |
| 72 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 73 | |
| 74 | /* create a new thread */ |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 75 | struct thread *create_thread( int fd, void *pid, int *thread_handle, |
| 76 | int *process_handle ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 77 | { |
| 78 | struct thread *thread; |
| 79 | struct process *process; |
| 80 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 81 | if (!(thread = mem_alloc( sizeof(*thread) ))) return NULL; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 82 | |
| 83 | if (pid) process = get_process_from_id( pid ); |
| 84 | else process = create_process(); |
| 85 | if (!process) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 86 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 87 | free( thread ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 88 | return NULL; |
| 89 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 90 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 91 | init_object( &thread->obj, &thread_ops, NULL ); |
| 92 | thread->client_fd = fd; |
| 93 | thread->process = process; |
| 94 | thread->unix_pid = 0; /* not known yet */ |
| 95 | thread->name = NULL; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 96 | thread->mutex = NULL; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 97 | thread->wait = NULL; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 98 | thread->apc = NULL; |
| 99 | thread->apc_count = 0; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 100 | thread->error = 0; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 101 | thread->state = STARTING; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 102 | thread->exit_code = 0x103; /* STILL_ACTIVE */ |
| 103 | thread->next = first_thread; |
| 104 | thread->prev = NULL; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 105 | thread->priority = THREAD_PRIORITY_NORMAL; |
| 106 | thread->affinity = 1; |
| 107 | thread->suspend = 0; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 108 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 109 | if (first_thread) first_thread->prev = thread; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 110 | first_thread = thread; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 111 | add_process_thread( process, thread ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 112 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 113 | *thread_handle = *process_handle = -1; |
| 114 | if (current) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 115 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 116 | if ((*thread_handle = alloc_handle( current->process, thread, |
| 117 | THREAD_ALL_ACCESS, 0 )) == -1) |
| 118 | goto error; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 119 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 120 | if (current && !pid) |
| 121 | { |
| 122 | if ((*process_handle = alloc_handle( current->process, process, |
| 123 | PROCESS_ALL_ACCESS, 0 )) == -1) |
| 124 | goto error; |
| 125 | } |
| 126 | |
| 127 | if (add_client( fd, thread ) == -1) goto error; |
| 128 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 129 | return thread; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 130 | |
| 131 | error: |
| 132 | if (current) |
| 133 | { |
| 134 | close_handle( current->process, *thread_handle ); |
| 135 | close_handle( current->process, *process_handle ); |
| 136 | } |
| 137 | remove_process_thread( process, thread ); |
| 138 | release_object( thread ); |
| 139 | return NULL; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* destroy a thread when its refcount is 0 */ |
| 143 | static void destroy_thread( struct object *obj ) |
| 144 | { |
| 145 | struct thread *thread = (struct thread *)obj; |
| 146 | assert( obj->ops == &thread_ops ); |
| 147 | |
| 148 | release_object( thread->process ); |
| 149 | if (thread->next) thread->next->prev = thread->prev; |
| 150 | if (thread->prev) thread->prev->next = thread->next; |
| 151 | else first_thread = thread->next; |
| 152 | if (thread->name) free( thread->name ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 153 | if (thread->apc) free( thread->apc ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 154 | if (debug_level) memset( thread, 0xaa, sizeof(thread) ); /* catch errors */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 155 | free( thread ); |
| 156 | } |
| 157 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 158 | /* dump a thread on stdout for debugging purposes */ |
| 159 | static void dump_thread( struct object *obj, int verbose ) |
| 160 | { |
| 161 | struct thread *thread = (struct thread *)obj; |
| 162 | assert( obj->ops == &thread_ops ); |
| 163 | |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 164 | fprintf( stderr, "Thread pid=%d fd=%d name='%s'\n", |
| 165 | thread->unix_pid, thread->client_fd, thread->name ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 168 | static int thread_signaled( struct object *obj, struct thread *thread ) |
| 169 | { |
| 170 | struct thread *mythread = (struct thread *)obj; |
| 171 | return (mythread->state == TERMINATED); |
| 172 | } |
| 173 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 174 | /* get a thread pointer from a thread id (and increment the refcount) */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 175 | struct thread *get_thread_from_id( void *id ) |
| 176 | { |
| 177 | struct thread *t = first_thread; |
| 178 | while (t && (t != id)) t = t->next; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 179 | if (t) grab_object( t ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 180 | return t; |
| 181 | } |
| 182 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 183 | /* get a thread from a handle (and increment the refcount) */ |
| 184 | struct thread *get_thread_from_handle( int handle, unsigned int access ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 185 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 186 | return (struct thread *)get_handle_obj( current->process, handle, |
| 187 | access, &thread_ops ); |
| 188 | } |
| 189 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 190 | /* get all information about a thread */ |
| 191 | void get_thread_info( struct thread *thread, |
| 192 | struct get_thread_info_reply *reply ) |
| 193 | { |
| 194 | reply->pid = thread; |
| 195 | reply->exit_code = thread->exit_code; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 196 | reply->priority = thread->priority; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /* set all information about a thread */ |
| 201 | void set_thread_info( struct thread *thread, |
| 202 | struct set_thread_info_request *req ) |
| 203 | { |
| 204 | if (req->mask & SET_THREAD_INFO_PRIORITY) |
| 205 | thread->priority = req->priority; |
| 206 | if (req->mask & SET_THREAD_INFO_AFFINITY) |
| 207 | { |
| 208 | if (req->affinity != 1) SET_ERROR( ERROR_INVALID_PARAMETER ); |
| 209 | else thread->affinity = req->affinity; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* suspend a thread */ |
| 214 | int suspend_thread( struct thread *thread ) |
| 215 | { |
| 216 | int old_count = thread->suspend; |
| 217 | if (thread->suspend < MAXIMUM_SUSPEND_COUNT) |
| 218 | { |
| 219 | if (!thread->suspend++) |
| 220 | { |
| 221 | if (thread->unix_pid) kill( thread->unix_pid, SIGSTOP ); |
| 222 | } |
| 223 | } |
| 224 | return old_count; |
| 225 | } |
| 226 | |
| 227 | /* resume a thread */ |
| 228 | int resume_thread( struct thread *thread ) |
| 229 | { |
| 230 | int old_count = thread->suspend; |
| 231 | if (thread->suspend > 0) |
| 232 | { |
| 233 | if (!--thread->suspend) |
| 234 | { |
| 235 | if (thread->unix_pid) kill( thread->unix_pid, SIGCONT ); |
| 236 | } |
| 237 | } |
| 238 | return old_count; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 241 | /* send a reply to a thread */ |
| 242 | int send_reply( struct thread *thread, int pass_fd, int n, |
| 243 | ... /* arg_1, len_1, ..., arg_n, len_n */ ) |
| 244 | { |
| 245 | struct iovec vec[16]; |
| 246 | va_list args; |
| 247 | int i; |
| 248 | |
| 249 | assert( n < 16 ); |
| 250 | va_start( args, n ); |
| 251 | for (i = 0; i < n; i++) |
| 252 | { |
| 253 | vec[i].iov_base = va_arg( args, void * ); |
| 254 | vec[i].iov_len = va_arg( args, int ); |
| 255 | } |
| 256 | va_end( args ); |
| 257 | return send_reply_v( thread->client_fd, thread->error, pass_fd, vec, n ); |
| 258 | } |
| 259 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 260 | /* 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] | 261 | int add_queue( struct object *obj, struct wait_queue_entry *entry ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 262 | { |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 263 | grab_object( obj ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 264 | entry->obj = obj; |
| 265 | entry->prev = obj->tail; |
| 266 | entry->next = NULL; |
| 267 | if (obj->tail) obj->tail->next = entry; |
| 268 | else obj->head = entry; |
| 269 | obj->tail = entry; |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 270 | return 1; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* remove a thread from an object wait queue */ |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 274 | void remove_queue( struct object *obj, struct wait_queue_entry *entry ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 275 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 276 | if (entry->next) entry->next->prev = entry->prev; |
| 277 | else obj->tail = entry->prev; |
| 278 | if (entry->prev) entry->prev->next = entry->next; |
| 279 | else obj->head = entry->next; |
| 280 | release_object( obj ); |
| 281 | } |
| 282 | |
| 283 | /* finish waiting */ |
| 284 | static void end_wait( struct thread *thread ) |
| 285 | { |
| 286 | struct thread_wait *wait = thread->wait; |
| 287 | struct wait_queue_entry *entry; |
| 288 | int i; |
| 289 | |
| 290 | assert( wait ); |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 291 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 292 | entry->obj->ops->remove_queue( entry->obj, entry ); |
| 293 | if (wait->flags & SELECT_TIMEOUT) set_select_timeout( thread->client_fd, NULL ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 294 | free( wait ); |
| 295 | thread->wait = NULL; |
| 296 | } |
| 297 | |
| 298 | /* build the thread wait structure */ |
| 299 | static int wait_on( struct thread *thread, int count, |
| 300 | int *handles, int flags, int timeout ) |
| 301 | { |
| 302 | struct thread_wait *wait; |
| 303 | struct wait_queue_entry *entry; |
| 304 | struct object *obj; |
| 305 | int i; |
| 306 | |
| 307 | if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS)) |
| 308 | { |
| 309 | SET_ERROR( ERROR_INVALID_PARAMETER ); |
| 310 | return 0; |
| 311 | } |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 312 | if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 313 | thread->wait = wait; |
| 314 | wait->count = count; |
| 315 | wait->flags = flags; |
| 316 | if (flags & SELECT_TIMEOUT) |
| 317 | { |
| 318 | gettimeofday( &wait->timeout, 0 ); |
| 319 | if (timeout) |
| 320 | { |
| 321 | wait->timeout.tv_usec += (timeout % 1000) * 1000; |
| 322 | if (wait->timeout.tv_usec >= 1000000) |
| 323 | { |
| 324 | wait->timeout.tv_usec -= 1000000; |
| 325 | wait->timeout.tv_sec++; |
| 326 | } |
| 327 | wait->timeout.tv_sec += timeout / 1000; |
| 328 | } |
| 329 | } |
| 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 | { |
| 362 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 363 | if (!entry->obj->ops->signaled( entry->obj, thread )) goto other_checks; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 364 | /* Wait satisfied: tell it to all objects */ |
| 365 | *signaled = 0; |
| 366 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 367 | if (entry->obj->ops->satisfied( entry->obj, thread )) |
| 368 | *signaled = STATUS_ABANDONED_WAIT_0; |
| 369 | return 1; |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 374 | { |
| 375 | if (!entry->obj->ops->signaled( entry->obj, thread )) continue; |
| 376 | /* Wait satisfied: tell it to the object */ |
| 377 | *signaled = i; |
| 378 | if (entry->obj->ops->satisfied( entry->obj, thread )) |
| 379 | *signaled += STATUS_ABANDONED_WAIT_0; |
| 380 | return 1; |
| 381 | } |
| 382 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 383 | |
| 384 | other_checks: |
| 385 | if ((wait->flags & SELECT_ALERTABLE) && thread->apc) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 386 | { |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 387 | *signaled = STATUS_USER_APC; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 388 | return 1; |
| 389 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 390 | if (wait->flags & SELECT_TIMEOUT) |
| 391 | { |
| 392 | struct timeval now; |
| 393 | gettimeofday( &now, NULL ); |
| 394 | if ((now.tv_sec > wait->timeout.tv_sec) || |
| 395 | ((now.tv_sec == wait->timeout.tv_sec) && |
| 396 | (now.tv_usec >= wait->timeout.tv_usec))) |
| 397 | { |
| 398 | *signaled = STATUS_TIMEOUT; |
| 399 | return 1; |
| 400 | } |
| 401 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 405 | /* send the select reply to wake up the client */ |
| 406 | static void send_select_reply( struct thread *thread, int signaled ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 407 | { |
| 408 | struct select_reply reply; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 409 | reply.signaled = signaled; |
| 410 | if ((signaled == STATUS_USER_APC) && thread->apc) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 411 | { |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 412 | struct thread_apc *apc = thread->apc; |
| 413 | int len = thread->apc_count * sizeof(*apc); |
| 414 | thread->apc = NULL; |
| 415 | thread->apc_count = 0; |
| 416 | send_reply( thread, -1, 2, &reply, sizeof(reply), |
| 417 | apc, len ); |
| 418 | free( apc ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 419 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 420 | else send_reply( thread, -1, 1, &reply, sizeof(reply) ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* attempt to wake up a thread */ |
| 424 | /* return 1 if OK, 0 if the wait condition is still not satisfied */ |
| 425 | static int wake_thread( struct thread *thread ) |
| 426 | { |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 427 | int signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 428 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 429 | if (!check_wait( thread, &signaled )) return 0; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 430 | end_wait( thread ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 431 | send_select_reply( thread, signaled ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 432 | return 1; |
| 433 | } |
| 434 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 435 | /* sleep on a list of objects */ |
| 436 | void sleep_on( struct thread *thread, int count, int *handles, int flags, int timeout ) |
| 437 | { |
| 438 | assert( !thread->wait ); |
| 439 | if (!wait_on( thread, count, handles, flags, timeout )) |
| 440 | { |
| 441 | /* return an error */ |
| 442 | send_select_reply( thread, -1 ); |
| 443 | return; |
| 444 | } |
| 445 | if (!wake_thread( thread )) |
| 446 | { |
| 447 | /* we need to wait */ |
| 448 | if (flags & SELECT_TIMEOUT) |
| 449 | set_select_timeout( thread->client_fd, &thread->wait->timeout ); |
| 450 | } |
| 451 | } |
| 452 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 453 | /* timeout for the current thread */ |
| 454 | void thread_timeout(void) |
| 455 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 456 | assert( current->wait ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 457 | end_wait( current ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 458 | send_select_reply( current, STATUS_TIMEOUT ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* attempt to wake threads sleeping on the object wait queue */ |
| 462 | void wake_up( struct object *obj, int max ) |
| 463 | { |
| 464 | struct wait_queue_entry *entry = obj->head; |
| 465 | |
| 466 | while (entry) |
| 467 | { |
| 468 | struct wait_queue_entry *next = entry->next; |
| 469 | if (wake_thread( entry->thread )) |
| 470 | { |
| 471 | if (max && !--max) break; |
| 472 | } |
| 473 | entry = next; |
| 474 | } |
| 475 | } |
| 476 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame^] | 477 | /* queue an async procedure call */ |
| 478 | int thread_queue_apc( struct thread *thread, void *func, void *param ) |
| 479 | { |
| 480 | struct thread_apc *apc; |
| 481 | if (!func) |
| 482 | { |
| 483 | SET_ERROR( ERROR_INVALID_PARAMETER ); |
| 484 | return 0; |
| 485 | } |
| 486 | if (!thread->apc) |
| 487 | { |
| 488 | if (!(thread->apc = mem_alloc( MAX_THREAD_APC * sizeof(*apc) ))) |
| 489 | return 0; |
| 490 | thread->apc_count = 0; |
| 491 | } |
| 492 | else if (thread->apc_count >= MAX_THREAD_APC) return 0; |
| 493 | thread->apc[thread->apc_count].func = func; |
| 494 | thread->apc[thread->apc_count].param = param; |
| 495 | thread->apc_count++; |
| 496 | wake_thread( thread ); |
| 497 | return 1; |
| 498 | } |
| 499 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 500 | /* kill a thread on the spot */ |
| 501 | void kill_thread( struct thread *thread, int exit_code ) |
| 502 | { |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 503 | if (thread->state == TERMINATED) return; /* already killed */ |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 504 | if (thread->unix_pid) kill( thread->unix_pid, SIGTERM ); |
| 505 | remove_client( thread->client_fd, exit_code ); /* this will call thread_killed */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* a thread has been killed */ |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 509 | void thread_killed( struct thread *thread, int exit_code ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 510 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 511 | thread->state = TERMINATED; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 512 | thread->exit_code = exit_code; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 513 | if (thread->wait) end_wait( thread ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 514 | abandon_mutexes( thread ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 515 | remove_process_thread( thread->process, thread ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 516 | wake_up( &thread->obj, 0 ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 517 | release_object( thread ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 518 | } |