Unblock the dialog message loop with a WM_NULL message.
[wine.git] / server / thread.c
blob60363c7f15e7049fdc0b6b59443d77e34bb3ec3f
1 /*
2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #ifdef HAVE_SYS_MMAN_H
17 #include <sys/mman.h>
18 #endif
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <stdarg.h>
23 #include "winbase.h"
25 #include "handle.h"
26 #include "process.h"
27 #include "thread.h"
28 #include "request.h"
31 /* thread queues */
33 struct thread_wait
35 int count; /* count of objects */
36 int flags;
37 struct timeval timeout;
38 struct timeout_user *user;
39 struct wait_queue_entry queues[1];
42 /* asynchronous procedure calls */
44 struct thread_apc
46 struct thread_apc *next; /* queue linked list */
47 struct thread_apc *prev;
48 struct object *owner; /* object that queued this apc */
49 void *func; /* function to call in client */
50 enum apc_type type; /* type of apc function */
51 int nb_args; /* number of arguments */
52 void *args[1]; /* function arguments */
56 /* thread operations */
58 static void dump_thread( struct object *obj, int verbose );
59 static int thread_signaled( struct object *obj, struct thread *thread );
60 static void thread_poll_event( struct object *obj, int event );
61 static void destroy_thread( struct object *obj );
62 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
64 static const struct object_ops thread_ops =
66 sizeof(struct thread), /* size */
67 dump_thread, /* dump */
68 add_queue, /* add_queue */
69 remove_queue, /* remove_queue */
70 thread_signaled, /* signaled */
71 no_satisfied, /* satisfied */
72 NULL, /* get_poll_events */
73 thread_poll_event, /* poll_event */
74 no_get_fd, /* get_fd */
75 no_flush, /* flush */
76 no_get_file_info, /* get_file_info */
77 destroy_thread /* destroy */
80 static struct thread *first_thread;
81 static struct thread *booting_thread;
83 /* initialize the structure for a newly allocated thread */
84 inline static void init_thread_structure( struct thread *thread )
86 int i;
88 thread->unix_pid = 0; /* not known yet */
89 thread->context = NULL;
90 thread->teb = NULL;
91 thread->mutex = NULL;
92 thread->debug_ctx = NULL;
93 thread->debug_event = NULL;
94 thread->queue = NULL;
95 thread->info = NULL;
96 thread->wait = NULL;
97 thread->system_apc.head = NULL;
98 thread->system_apc.tail = NULL;
99 thread->user_apc.head = NULL;
100 thread->user_apc.tail = NULL;
101 thread->error = 0;
102 thread->request_fd = NULL;
103 thread->reply_fd = -1;
104 thread->wait_fd = -1;
105 thread->state = RUNNING;
106 thread->attached = 0;
107 thread->exit_code = 0;
108 thread->next = NULL;
109 thread->prev = NULL;
110 thread->priority = THREAD_PRIORITY_NORMAL;
111 thread->affinity = 1;
112 thread->suspend = 0;
113 thread->buffer = (void *)-1;
115 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
116 thread->inflight[i].server = thread->inflight[i].client = -1;
119 /* create a new thread */
120 struct thread *create_thread( int fd, struct process *process )
122 struct thread *thread;
124 if (!(thread = alloc_object( &thread_ops, fd ))) return NULL;
126 init_thread_structure( thread );
128 thread->process = (struct process *)grab_object( process );
129 if (!current) current = thread;
131 if (!booting_thread) /* first thread ever */
133 booting_thread = thread;
134 lock_master_socket(1);
137 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
138 first_thread = thread;
140 fcntl( fd, F_SETFL, O_NONBLOCK );
141 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
142 add_process_thread( thread->process, thread );
143 return thread;
146 /* handle a client event */
147 static void thread_poll_event( struct object *obj, int event )
149 struct thread *thread = (struct thread *)obj;
150 assert( obj->ops == &thread_ops );
152 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
153 else if (event & POLLIN) read_request( thread );
156 /* cleanup everything that is no longer needed by a dead thread */
157 /* used by destroy_thread and kill_thread */
158 static void cleanup_thread( struct thread *thread )
160 int i;
161 struct thread_apc *apc;
163 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
164 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
165 if (thread->reply_fd != -1) close( thread->reply_fd );
166 if (thread->wait_fd != -1) close( thread->wait_fd );
167 if (thread->request_fd) release_object( thread->request_fd );
168 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
170 if (thread->inflight[i].client != -1)
172 close( thread->inflight[i].server );
173 thread->inflight[i].client = thread->inflight[i].server = -1;
176 thread->buffer = (void *)-1;
177 thread->reply_fd = -1;
178 thread->wait_fd = -1;
179 thread->request_fd = NULL;
182 /* destroy a thread when its refcount is 0 */
183 static void destroy_thread( struct object *obj )
185 struct thread_apc *apc;
186 struct thread *thread = (struct thread *)obj;
187 assert( obj->ops == &thread_ops );
189 assert( !thread->debug_ctx ); /* cannot still be debugging something */
190 release_object( thread->process );
191 if (thread->next) thread->next->prev = thread->prev;
192 if (thread->prev) thread->prev->next = thread->next;
193 else first_thread = thread->next;
194 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
195 if (thread->info) release_object( thread->info );
196 if (thread->queue) release_object( thread->queue );
197 cleanup_thread( thread );
200 /* dump a thread on stdout for debugging purposes */
201 static void dump_thread( struct object *obj, int verbose )
203 struct thread *thread = (struct thread *)obj;
204 assert( obj->ops == &thread_ops );
206 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
207 thread->unix_pid, thread->teb, thread->state );
210 static int thread_signaled( struct object *obj, struct thread *thread )
212 struct thread *mythread = (struct thread *)obj;
213 return (mythread->state == TERMINATED);
216 /* get a thread pointer from a thread id (and increment the refcount) */
217 struct thread *get_thread_from_id( void *id )
219 struct thread *t = first_thread;
220 while (t && (t != id)) t = t->next;
221 if (t) grab_object( t );
222 return t;
225 /* get a thread from a handle (and increment the refcount) */
226 struct thread *get_thread_from_handle( handle_t handle, unsigned int access )
228 return (struct thread *)get_handle_obj( current->process, handle,
229 access, &thread_ops );
232 /* find a thread from a Unix pid */
233 struct thread *get_thread_from_pid( int pid )
235 struct thread *t = first_thread;
236 while (t && (t->unix_pid != pid)) t = t->next;
237 return t;
240 /* set all information about a thread */
241 static void set_thread_info( struct thread *thread,
242 struct set_thread_info_request *req )
244 if (req->mask & SET_THREAD_INFO_PRIORITY)
245 thread->priority = req->priority;
246 if (req->mask & SET_THREAD_INFO_AFFINITY)
248 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
249 else thread->affinity = req->affinity;
253 /* suspend a thread */
254 int suspend_thread( struct thread *thread, int check_limit )
256 int old_count = thread->suspend;
257 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
259 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
261 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
262 return old_count;
265 /* resume a thread */
266 int resume_thread( struct thread *thread )
268 int old_count = thread->suspend;
269 if (thread->suspend > 0)
271 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
273 return old_count;
276 /* suspend all threads but the current */
277 void suspend_all_threads( void )
279 struct thread *thread;
280 for ( thread = first_thread; thread; thread = thread->next )
281 if ( thread != current )
282 suspend_thread( thread, 0 );
285 /* resume all threads but the current */
286 void resume_all_threads( void )
288 struct thread *thread;
289 for ( thread = first_thread; thread; thread = thread->next )
290 if ( thread != current )
291 resume_thread( thread );
294 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
295 int add_queue( struct object *obj, struct wait_queue_entry *entry )
297 grab_object( obj );
298 entry->obj = obj;
299 entry->prev = obj->tail;
300 entry->next = NULL;
301 if (obj->tail) obj->tail->next = entry;
302 else obj->head = entry;
303 obj->tail = entry;
304 return 1;
307 /* remove a thread from an object wait queue */
308 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
310 if (entry->next) entry->next->prev = entry->prev;
311 else obj->tail = entry->prev;
312 if (entry->prev) entry->prev->next = entry->next;
313 else obj->head = entry->next;
314 release_object( obj );
317 /* finish waiting */
318 static void end_wait( struct thread *thread )
320 struct thread_wait *wait = thread->wait;
321 struct wait_queue_entry *entry;
322 int i;
324 assert( wait );
325 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
326 entry->obj->ops->remove_queue( entry->obj, entry );
327 if (wait->user) remove_timeout_user( wait->user );
328 free( wait );
329 thread->wait = NULL;
332 /* build the thread wait structure */
333 static int wait_on( int count, struct object *objects[], int flags, int sec, int usec )
335 struct thread_wait *wait;
336 struct wait_queue_entry *entry;
337 int i;
339 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
340 current->wait = wait;
341 wait->count = count;
342 wait->flags = flags;
343 wait->user = NULL;
344 if (flags & SELECT_TIMEOUT)
346 wait->timeout.tv_sec = sec;
347 wait->timeout.tv_usec = usec;
350 for (i = 0, entry = wait->queues; i < count; i++, entry++)
352 struct object *obj = objects[i];
353 entry->thread = current;
354 if (!obj->ops->add_queue( obj, entry ))
356 wait->count = i;
357 end_wait( current );
358 return 0;
361 return 1;
364 /* check if the thread waiting condition is satisfied */
365 static int check_wait( struct thread *thread )
367 int i, signaled;
368 struct thread_wait *wait = thread->wait;
369 struct wait_queue_entry *entry = wait->queues;
371 assert( wait );
372 if (wait->flags & SELECT_ALL)
374 int not_ok = 0;
375 /* Note: we must check them all anyway, as some objects may
376 * want to do something when signaled, even if others are not */
377 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
378 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
379 if (not_ok) goto other_checks;
380 /* Wait satisfied: tell it to all objects */
381 signaled = 0;
382 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
383 if (entry->obj->ops->satisfied( entry->obj, thread ))
384 signaled = STATUS_ABANDONED_WAIT_0;
385 return signaled;
387 else
389 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
391 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
392 /* Wait satisfied: tell it to the object */
393 signaled = i;
394 if (entry->obj->ops->satisfied( entry->obj, thread ))
395 signaled = i + STATUS_ABANDONED_WAIT_0;
396 return signaled;
400 other_checks:
401 if ((wait->flags & SELECT_INTERRUPTIBLE) && thread->system_apc.head) return STATUS_USER_APC;
402 if ((wait->flags & SELECT_ALERTABLE) && thread->user_apc.head) return STATUS_USER_APC;
403 if (wait->flags & SELECT_TIMEOUT)
405 struct timeval now;
406 gettimeofday( &now, NULL );
407 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
409 return -1;
412 /* attempt to wake up a thread */
413 /* return 1 if OK, 0 if the wait condition is still not satisfied */
414 static int wake_thread( struct thread *thread )
416 int signaled;
417 if ((signaled = check_wait( thread )) == -1) return 0;
419 if (debug_level) fprintf( stderr, "%08x: *wakeup* object=%d\n",
420 (unsigned int)thread, signaled );
421 end_wait( thread );
422 send_thread_wakeup( thread, signaled );
423 return 1;
426 /* thread wait timeout */
427 static void thread_timeout( void *ptr )
429 struct thread *thread = ptr;
431 if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread );
433 assert( thread->wait );
434 thread->wait->user = NULL;
435 end_wait( thread );
436 send_thread_wakeup( thread, STATUS_TIMEOUT );
439 /* select on a list of handles */
440 static void select_on( int count, handle_t *handles, int flags, int sec, int usec )
442 int ret, i;
443 struct object *objects[MAXIMUM_WAIT_OBJECTS];
445 assert( !current->wait );
447 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
449 set_error( STATUS_INVALID_PARAMETER );
450 return;
452 for (i = 0; i < count; i++)
454 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
455 break;
458 if (i < count) goto done;
459 if (!wait_on( count, objects, flags, sec, usec )) goto done;
461 if ((ret = check_wait( current )) != -1)
463 /* condition is already satisfied */
464 end_wait( current );
465 set_error( ret );
466 goto done;
469 /* now we need to wait */
470 if (flags & SELECT_TIMEOUT)
472 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
473 thread_timeout, current )))
475 end_wait( current );
476 goto done;
479 set_error( STATUS_PENDING );
481 done:
482 while (--i >= 0) release_object( objects[i] );
485 /* attempt to wake threads sleeping on the object wait queue */
486 void wake_up( struct object *obj, int max )
488 struct wait_queue_entry *entry = obj->head;
490 while (entry)
492 struct thread *thread = entry->thread;
493 entry = entry->next;
494 if (wake_thread( thread ))
496 if (max && !--max) break;
501 /* queue an async procedure call */
502 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
503 enum apc_type type, int system, int nb_args, ... )
505 struct thread_apc *apc;
506 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
508 /* cancel a possible previous APC with the same owner */
509 if (owner) thread_cancel_apc( thread, owner, system );
511 if (!(apc = mem_alloc( sizeof(*apc) + (nb_args-1)*sizeof(apc->args[0]) ))) return 0;
512 apc->prev = queue->tail;
513 apc->next = NULL;
514 apc->owner = owner;
515 apc->func = func;
516 apc->type = type;
517 apc->nb_args = nb_args;
518 if (nb_args)
520 int i;
521 va_list args;
522 va_start( args, nb_args );
523 for (i = 0; i < nb_args; i++) apc->args[i] = va_arg( args, void * );
524 va_end( args );
526 queue->tail = apc;
527 if (!apc->prev) /* first one */
529 queue->head = apc;
530 if (thread->wait) wake_thread( thread );
532 return 1;
535 /* cancel the async procedure call owned by a specific object */
536 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
538 struct thread_apc *apc;
539 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
540 for (apc = queue->head; apc; apc = apc->next)
542 if (apc->owner != owner) continue;
543 if (apc->next) apc->next->prev = apc->prev;
544 else queue->tail = apc->prev;
545 if (apc->prev) apc->prev->next = apc->next;
546 else queue->head = apc->next;
547 free( apc );
548 return;
552 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
553 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
555 struct thread_apc *apc;
556 struct apc_queue *queue = &thread->system_apc;
558 if (!queue->head && !system_only) queue = &thread->user_apc;
559 if ((apc = queue->head))
561 if (apc->next) apc->next->prev = NULL;
562 else queue->tail = NULL;
563 queue->head = apc->next;
565 return apc;
568 /* add an fd to the inflight list */
569 /* return list index, or -1 on error */
570 int thread_add_inflight_fd( struct thread *thread, int client, int server )
572 int i;
574 if (server == -1) return -1;
575 if (client == -1)
577 close( server );
578 return -1;
581 /* first check if we already have an entry for this fd */
582 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
583 if (thread->inflight[i].client == client)
585 close( thread->inflight[i].server );
586 thread->inflight[i].server = server;
587 return i;
590 /* now find a free spot to store it */
591 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
592 if (thread->inflight[i].client == -1)
594 thread->inflight[i].client = client;
595 thread->inflight[i].server = server;
596 return i;
598 return -1;
601 /* get an inflight fd and purge it from the list */
602 /* the fd must be closed when no longer used */
603 int thread_get_inflight_fd( struct thread *thread, int client )
605 int i, ret;
607 if (client == -1) return -1;
611 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
613 if (thread->inflight[i].client == client)
615 ret = thread->inflight[i].server;
616 thread->inflight[i].server = thread->inflight[i].client = -1;
617 return ret;
620 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
621 return -1;
624 /* retrieve an LDT selector entry */
625 static void get_selector_entry( struct thread *thread, int entry,
626 unsigned int *base, unsigned int *limit,
627 unsigned char *flags )
629 if (!thread->process->ldt_copy)
631 set_error( STATUS_ACCESS_DENIED );
632 return;
634 if (entry >= 8192)
636 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
637 return;
639 if (suspend_for_ptrace( thread ))
641 unsigned char flags_buf[4];
642 int *addr = (int *)thread->process->ldt_copy + entry;
643 if (read_thread_int( thread, addr, base ) == -1) goto done;
644 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
645 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
646 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
647 *flags = flags_buf[entry & 3];
648 done:
649 resume_thread( thread );
653 /* kill a thread on the spot */
654 void kill_thread( struct thread *thread, int violent_death )
656 if (thread->state == TERMINATED) return; /* already killed */
657 thread->state = TERMINATED;
658 if (current == thread) current = NULL;
659 if (debug_level)
660 fprintf( stderr,"%08x: *killed* exit_code=%d\n",
661 (unsigned int)thread, thread->exit_code );
662 if (thread->wait)
664 end_wait( thread );
665 /* if it is waiting on the socket, we don't need to send a SIGTERM */
666 violent_death = 0;
668 debug_exit_thread( thread );
669 abandon_mutexes( thread );
670 remove_process_thread( thread->process, thread );
671 wake_up( &thread->obj, 0 );
672 detach_thread( thread, violent_death ? SIGTERM : 0 );
673 remove_select_user( &thread->obj );
674 cleanup_thread( thread );
675 release_object( thread );
678 /* take a snapshot of currently running threads */
679 struct thread_snapshot *thread_snap( int *count )
681 struct thread_snapshot *snapshot, *ptr;
682 struct thread *thread;
683 int total = 0;
685 for (thread = first_thread; thread; thread = thread->next)
686 if (thread->state != TERMINATED) total++;
687 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
688 ptr = snapshot;
689 for (thread = first_thread; thread; thread = thread->next)
691 if (thread->state == TERMINATED) continue;
692 ptr->thread = thread;
693 ptr->count = thread->obj.refcount;
694 ptr->priority = thread->priority;
695 grab_object( thread );
696 ptr++;
698 *count = total;
699 return snapshot;
702 /* signal that we are finished booting on the client side */
703 DECL_HANDLER(boot_done)
705 debug_level = max( debug_level, req->debug_level );
706 if (current == booting_thread)
708 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
709 lock_master_socket(0); /* allow other clients now */
713 /* create a new thread */
714 DECL_HANDLER(new_thread)
716 struct thread *thread;
717 int request_fd = thread_get_inflight_fd( current, req->request_fd );
719 if (request_fd == -1)
721 set_error( STATUS_INVALID_HANDLE );
722 return;
725 if ((thread = create_thread( request_fd, current->process )))
727 if (req->suspend) thread->suspend++;
728 req->tid = thread;
729 if ((req->handle = alloc_handle( current->process, thread,
730 THREAD_ALL_ACCESS, req->inherit )))
732 /* thread object will be released when the thread gets killed */
733 return;
735 kill_thread( thread, 1 );
736 request_fd = -1;
740 /* initialize a new thread */
741 DECL_HANDLER(init_thread)
743 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
744 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
746 if (current->unix_pid)
748 fatal_protocol_error( current, "init_thread: already running\n" );
749 goto error;
751 if (reply_fd == -1)
753 fatal_protocol_error( current, "bad reply fd\n" );
754 goto error;
756 if (wait_fd == -1)
758 fatal_protocol_error( current, "bad wait fd\n" );
759 goto error;
762 current->unix_pid = req->unix_pid;
763 current->teb = req->teb;
764 current->reply_fd = reply_fd;
765 current->wait_fd = wait_fd;
767 if (current->suspend + current->process->suspend > 0) stop_thread( current );
768 if (current->process->running_threads > 1)
769 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
771 req->pid = get_process_id( current->process );
772 req->tid = get_thread_id( current );
773 req->boot = (current == booting_thread);
774 req->version = SERVER_PROTOCOL_VERSION;
775 return;
777 error:
778 if (reply_fd != -1) close( reply_fd );
779 if (wait_fd != -1) close( wait_fd );
782 /* set the shared buffer for a thread */
783 DECL_HANDLER(set_thread_buffer)
785 unsigned int size = MAX_REQUEST_LENGTH;
786 unsigned int offset = 0;
787 int fd = thread_get_inflight_fd( current, req->fd );
789 req->size = size;
790 req->offset = offset;
792 if (fd != -1)
794 if (ftruncate( fd, size ) == -1) file_set_error();
795 else
797 void *buffer = mmap( 0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset );
798 if (buffer == (void *)-1) file_set_error();
799 else
801 if (current->buffer != (void *)-1) munmap( current->buffer, size );
802 current->buffer = buffer;
805 close( fd );
807 else set_error( STATUS_INVALID_HANDLE );
810 /* terminate a thread */
811 DECL_HANDLER(terminate_thread)
813 struct thread *thread;
815 req->self = 0;
816 req->last = 0;
817 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
819 thread->exit_code = req->exit_code;
820 if (thread != current) kill_thread( thread, 1 );
821 else
823 req->self = 1;
824 req->last = (thread->process->running_threads == 1);
826 release_object( thread );
830 /* fetch information about a thread */
831 DECL_HANDLER(get_thread_info)
833 struct thread *thread;
834 handle_t handle = req->handle;
836 if (!handle) thread = get_thread_from_id( req->tid_in );
837 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
839 if (thread)
841 req->tid = get_thread_id( thread );
842 req->teb = thread->teb;
843 req->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
844 req->priority = thread->priority;
845 release_object( thread );
849 /* set information about a thread */
850 DECL_HANDLER(set_thread_info)
852 struct thread *thread;
854 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
856 set_thread_info( thread, req );
857 release_object( thread );
861 /* suspend a thread */
862 DECL_HANDLER(suspend_thread)
864 struct thread *thread;
866 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
868 req->count = suspend_thread( thread, 1 );
869 release_object( thread );
873 /* resume a thread */
874 DECL_HANDLER(resume_thread)
876 struct thread *thread;
878 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
880 req->count = resume_thread( thread );
881 release_object( thread );
885 /* select on a handle list */
886 DECL_HANDLER(select)
888 int count = get_req_data_size(req) / sizeof(int);
889 select_on( count, get_req_data(req), req->flags, req->sec, req->usec );
892 /* queue an APC for a thread */
893 DECL_HANDLER(queue_apc)
895 struct thread *thread;
896 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
898 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user, 1, req->param );
899 release_object( thread );
903 /* get next APC to call */
904 DECL_HANDLER(get_apc)
906 struct thread_apc *apc;
907 size_t size;
909 for (;;)
911 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
913 /* no more APCs */
914 req->func = NULL;
915 req->type = APC_NONE;
916 set_req_data_size( req, 0 );
917 return;
919 /* Optimization: ignore APCs that have a NULL func; they are only used
920 * to wake up a thread, but since we got here the thread woke up already.
922 if (apc->func) break;
923 free( apc );
925 size = apc->nb_args * sizeof(apc->args[0]);
926 if (size > get_req_data_size(req)) size = get_req_data_size(req);
927 req->func = apc->func;
928 req->type = apc->type;
929 memcpy( get_req_data(req), apc->args, size );
930 set_req_data_size( req, size );
931 free( apc );
934 /* fetch a selector entry for a thread */
935 DECL_HANDLER(get_selector_entry)
937 struct thread *thread;
938 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
940 get_selector_entry( thread, req->entry, &req->base, &req->limit, &req->flags );
941 release_object( thread );