2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
32 #include <sys/types.h>
54 struct thread_wait *next; /* next wait structure for this thread */
55 struct thread *thread; /* owner thread */
56 int count; /* count of objects */
58 void *cookie; /* magic cookie to return to client */
59 struct timeval timeout;
60 struct timeout_user *user;
61 struct wait_queue_entry queues[1];
64 /* asynchronous procedure calls */
68 struct list entry; /* queue linked list */
69 struct object *owner; /* object that queued this apc */
70 void *func; /* function to call in client */
71 enum apc_type type; /* type of apc function */
72 int nb_args; /* number of arguments */
73 void *arg1; /* function arguments */
79 /* thread operations */
81 static void dump_thread( struct object *obj, int verbose );
82 static int thread_signaled( struct object *obj, struct thread *thread );
83 static void thread_poll_event( struct fd *fd, int event );
84 static void destroy_thread( struct object *obj );
85 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
87 static const struct object_ops thread_ops =
89 sizeof(struct thread), /* size */
90 dump_thread, /* dump */
91 add_queue, /* add_queue */
92 remove_queue, /* remove_queue */
93 thread_signaled, /* signaled */
94 no_satisfied, /* satisfied */
95 no_signal, /* signal */
96 no_get_fd, /* get_fd */
97 no_close_handle, /* close_handle */
98 destroy_thread /* destroy */
101 static const struct fd_ops thread_fd_ops =
103 NULL, /* get_poll_events */
104 thread_poll_event, /* poll_event */
105 no_flush, /* flush */
106 no_get_file_info, /* get_file_info */
107 no_queue_async, /* queue_async */
108 no_cancel_async /* cancel_async */
111 static struct list thread_list = LIST_INIT(thread_list);
112 static struct thread *booting_thread;
114 /* initialize the structure for a newly allocated thread */
115 inline static void init_thread_structure( struct thread *thread )
119 thread->unix_pid = -1; /* not known yet */
120 thread->unix_tid = -1; /* not known yet */
121 thread->context = NULL;
123 thread->debug_ctx = NULL;
124 thread->debug_event = NULL;
125 thread->queue = NULL;
128 thread->req_data = NULL;
129 thread->req_toread = 0;
130 thread->reply_data = NULL;
131 thread->reply_towrite = 0;
132 thread->request_fd = NULL;
133 thread->reply_fd = NULL;
134 thread->wait_fd = NULL;
135 thread->state = RUNNING;
136 thread->attached = 0;
137 thread->exit_code = 0;
138 thread->priority = THREAD_PRIORITY_NORMAL;
139 thread->affinity = 1;
141 thread->creation_time = time(NULL);
142 thread->exit_time = 0;
144 list_init( &thread->mutex_list );
145 list_init( &thread->system_apc );
146 list_init( &thread->user_apc );
148 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
149 thread->inflight[i].server = thread->inflight[i].client = -1;
152 /* create a new thread */
153 struct thread *create_thread( int fd, struct process *process )
155 struct thread *thread;
157 if (!(thread = alloc_object( &thread_ops ))) return NULL;
159 init_thread_structure( thread );
161 thread->process = (struct process *)grab_object( process );
162 thread->desktop = process->desktop;
163 if (!current) current = thread;
165 if (!booting_thread) /* first thread ever */
167 booting_thread = thread;
168 lock_master_socket(1);
171 list_add_head( &thread_list, &thread->entry );
173 if (!(thread->id = alloc_ptid( thread )))
175 release_object( thread );
178 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
180 release_object( thread );
184 thread->token = (struct token *) grab_object( process->token );
186 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
187 add_process_thread( thread->process, thread );
191 /* handle a client event */
192 static void thread_poll_event( struct fd *fd, int event )
194 struct thread *thread = get_fd_user( fd );
195 assert( thread->obj.ops == &thread_ops );
197 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
198 else if (event & POLLIN) read_request( thread );
199 else if (event & POLLOUT) write_reply( thread );
202 /* cleanup everything that is no longer needed by a dead thread */
203 /* used by destroy_thread and kill_thread */
204 static void cleanup_thread( struct thread *thread )
207 struct thread_apc *apc;
209 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
210 if (thread->req_data) free( thread->req_data );
211 if (thread->reply_data) free( thread->reply_data );
212 if (thread->request_fd) release_object( thread->request_fd );
213 if (thread->reply_fd) release_object( thread->reply_fd );
214 if (thread->wait_fd) release_object( thread->wait_fd );
215 free_msg_queue( thread );
216 cleanup_clipboard_thread(thread);
217 destroy_thread_windows( thread );
218 close_thread_desktop( thread );
219 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
221 if (thread->inflight[i].client != -1)
223 close( thread->inflight[i].server );
224 thread->inflight[i].client = thread->inflight[i].server = -1;
227 thread->req_data = NULL;
228 thread->reply_data = NULL;
229 thread->request_fd = NULL;
230 thread->reply_fd = NULL;
231 thread->wait_fd = NULL;
234 if (thread == booting_thread) /* killing booting thread */
236 booting_thread = NULL;
237 lock_master_socket(0);
241 /* destroy a thread when its refcount is 0 */
242 static void destroy_thread( struct object *obj )
244 struct thread_apc *apc;
245 struct thread *thread = (struct thread *)obj;
246 assert( obj->ops == &thread_ops );
248 assert( !thread->debug_ctx ); /* cannot still be debugging something */
249 list_remove( &thread->entry );
250 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
251 cleanup_thread( thread );
252 release_object( thread->process );
253 if (thread->id) free_ptid( thread->id );
254 if (thread->token) release_object( thread->token );
257 /* dump a thread on stdout for debugging purposes */
258 static void dump_thread( struct object *obj, int verbose )
260 struct thread *thread = (struct thread *)obj;
261 assert( obj->ops == &thread_ops );
263 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
264 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
267 static int thread_signaled( struct object *obj, struct thread *thread )
269 struct thread *mythread = (struct thread *)obj;
270 return (mythread->state == TERMINATED);
273 /* get a thread pointer from a thread id (and increment the refcount) */
274 struct thread *get_thread_from_id( thread_id_t id )
276 struct object *obj = get_ptid_entry( id );
278 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
279 set_win32_error( ERROR_INVALID_THREAD_ID );
283 /* get a thread from a handle (and increment the refcount) */
284 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
286 return (struct thread *)get_handle_obj( current->process, handle,
287 access, &thread_ops );
290 /* find a thread from a Unix pid */
291 struct thread *get_thread_from_pid( int pid )
293 struct thread *thread;
295 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
297 if (thread->unix_tid == pid) return thread;
299 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
301 if (thread->unix_pid == pid) return thread;
306 /* set all information about a thread */
307 static void set_thread_info( struct thread *thread,
308 const struct set_thread_info_request *req )
310 if (req->mask & SET_THREAD_INFO_PRIORITY)
311 thread->priority = req->priority;
312 if (req->mask & SET_THREAD_INFO_AFFINITY)
314 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
315 else thread->affinity = req->affinity;
319 /* stop a thread (at the Unix level) */
320 void stop_thread( struct thread *thread )
322 /* can't stop a thread while initialisation is in progress */
323 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
326 /* suspend a thread */
327 static int suspend_thread( struct thread *thread )
329 int old_count = thread->suspend;
330 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
332 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
334 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
338 /* resume a thread */
339 static int resume_thread( struct thread *thread )
341 int old_count = thread->suspend;
342 if (thread->suspend > 0)
344 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
349 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
350 int add_queue( struct object *obj, struct wait_queue_entry *entry )
354 list_add_tail( &obj->wait_queue, &entry->entry );
358 /* remove a thread from an object wait queue */
359 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
361 list_remove( &entry->entry );
362 release_object( obj );
366 static void end_wait( struct thread *thread )
368 struct thread_wait *wait = thread->wait;
369 struct wait_queue_entry *entry;
373 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
374 entry->obj->ops->remove_queue( entry->obj, entry );
375 if (wait->user) remove_timeout_user( wait->user );
376 thread->wait = wait->next;
380 /* build the thread wait structure */
381 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
383 struct thread_wait *wait;
384 struct wait_queue_entry *entry;
387 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
388 wait->next = current->wait;
389 wait->thread = current;
393 current->wait = wait;
394 if (flags & SELECT_TIMEOUT)
396 wait->timeout.tv_sec = timeout->sec;
397 wait->timeout.tv_usec = timeout->usec;
400 for (i = 0, entry = wait->queues; i < count; i++, entry++)
402 struct object *obj = objects[i];
403 entry->thread = current;
404 if (!obj->ops->add_queue( obj, entry ))
414 /* check if the thread waiting condition is satisfied */
415 static int check_wait( struct thread *thread )
418 struct thread_wait *wait = thread->wait;
419 struct wait_queue_entry *entry = wait->queues;
421 /* Suspended threads may not acquire locks */
422 if (thread->process->suspend + thread->suspend > 0) return -1;
425 if (wait->flags & SELECT_ALL)
428 /* Note: we must check them all anyway, as some objects may
429 * want to do something when signaled, even if others are not */
430 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
431 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
432 if (not_ok) goto other_checks;
433 /* Wait satisfied: tell it to all objects */
435 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
436 if (entry->obj->ops->satisfied( entry->obj, thread ))
437 signaled = STATUS_ABANDONED_WAIT_0;
442 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
444 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
445 /* Wait satisfied: tell it to the object */
447 if (entry->obj->ops->satisfied( entry->obj, thread ))
448 signaled = i + STATUS_ABANDONED_WAIT_0;
454 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
455 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
456 if (wait->flags & SELECT_TIMEOUT)
459 gettimeofday( &now, NULL );
460 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
465 /* send the wakeup signal to a thread */
466 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
468 struct wake_up_reply reply;
471 reply.cookie = cookie;
472 reply.signaled = signaled;
473 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
476 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
477 else if (errno == EPIPE)
478 kill_thread( thread, 0 ); /* normal death */
480 fatal_protocol_perror( thread, "write" );
484 /* attempt to wake up a thread */
485 /* return >0 if OK, 0 if the wait condition is still not satisfied */
486 int wake_thread( struct thread *thread )
491 for (count = 0; thread->wait; count++)
493 if ((signaled = check_wait( thread )) == -1) break;
495 cookie = thread->wait->cookie;
496 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
497 thread->id, signaled, cookie );
499 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
505 /* thread wait timeout */
506 static void thread_timeout( void *ptr )
508 struct thread_wait *wait = ptr;
509 struct thread *thread = wait->thread;
510 void *cookie = wait->cookie;
513 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
514 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
516 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
517 thread->id, STATUS_TIMEOUT, cookie );
519 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
520 /* check if other objects have become signaled in the meantime */
521 wake_thread( thread );
524 /* try signaling an event flag, a semaphore or a mutex */
525 static int signal_object( obj_handle_t handle )
530 obj = get_handle_obj( current->process, handle, 0, NULL );
533 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
534 release_object( obj );
539 /* select on a list of handles */
540 static void select_on( int count, void *cookie, const obj_handle_t *handles,
541 int flags, const abs_time_t *timeout, obj_handle_t signal_obj )
544 struct object *objects[MAXIMUM_WAIT_OBJECTS];
546 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
548 set_error( STATUS_INVALID_PARAMETER );
551 for (i = 0; i < count; i++)
553 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
557 if (i < count) goto done;
558 if (!wait_on( count, objects, flags, timeout )) goto done;
560 /* signal the object */
563 if (!signal_object( signal_obj ))
568 /* check if we woke ourselves up */
569 if (!current->wait) goto done;
572 if ((ret = check_wait( current )) != -1)
574 /* condition is already satisfied */
580 /* now we need to wait */
581 if (flags & SELECT_TIMEOUT)
583 if (!(current->wait->user = add_timeout_user( ¤t->wait->timeout,
584 thread_timeout, current->wait )))
590 current->wait->cookie = cookie;
591 set_error( STATUS_PENDING );
594 while (--i >= 0) release_object( objects[i] );
597 /* attempt to wake threads sleeping on the object wait queue */
598 void wake_up( struct object *obj, int max )
600 struct list *ptr, *next;
602 LIST_FOR_EACH_SAFE( ptr, next, &obj->wait_queue )
604 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
605 if (wake_thread( entry->thread ))
607 if (max && !--max) break;
612 /* queue an async procedure call */
613 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
614 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
616 struct thread_apc *apc;
617 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
619 /* cancel a possible previous APC with the same owner */
620 if (owner) thread_cancel_apc( thread, owner, system );
621 if (thread->state == TERMINATED) return 0;
623 if (!(apc = mem_alloc( sizeof(*apc) ))) return 0;
630 list_add_tail( queue, &apc->entry );
631 if (!list_prev( queue, &apc->entry )) /* first one */
632 wake_thread( thread );
637 /* cancel the async procedure call owned by a specific object */
638 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
640 struct thread_apc *apc;
641 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
642 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
644 if (apc->owner != owner) continue;
645 list_remove( &apc->entry );
651 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
652 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
654 struct thread_apc *apc = NULL;
655 struct list *ptr = list_head( &thread->system_apc );
657 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
660 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
666 /* add an fd to the inflight list */
667 /* return list index, or -1 on error */
668 int thread_add_inflight_fd( struct thread *thread, int client, int server )
672 if (server == -1) return -1;
679 /* first check if we already have an entry for this fd */
680 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
681 if (thread->inflight[i].client == client)
683 close( thread->inflight[i].server );
684 thread->inflight[i].server = server;
688 /* now find a free spot to store it */
689 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
690 if (thread->inflight[i].client == -1)
692 thread->inflight[i].client = client;
693 thread->inflight[i].server = server;
699 /* get an inflight fd and purge it from the list */
700 /* the fd must be closed when no longer used */
701 int thread_get_inflight_fd( struct thread *thread, int client )
705 if (client == -1) return -1;
709 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
711 if (thread->inflight[i].client == client)
713 ret = thread->inflight[i].server;
714 thread->inflight[i].server = thread->inflight[i].client = -1;
718 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
722 /* retrieve an LDT selector entry */
723 static void get_selector_entry( struct thread *thread, int entry,
724 unsigned int *base, unsigned int *limit,
725 unsigned char *flags )
727 if (!thread->process->ldt_copy)
729 set_error( STATUS_ACCESS_DENIED );
734 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
737 if (suspend_for_ptrace( thread ))
739 unsigned char flags_buf[4];
740 int *addr = (int *)thread->process->ldt_copy + entry;
741 if (read_thread_int( thread, addr, base ) == -1) goto done;
742 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
743 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
744 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
745 *flags = flags_buf[entry & 3];
747 resume_after_ptrace( thread );
751 /* kill a thread on the spot */
752 void kill_thread( struct thread *thread, int violent_death )
754 if (thread->state == TERMINATED) return; /* already killed */
755 thread->state = TERMINATED;
756 thread->exit_time = time(NULL);
757 if (current == thread) current = NULL;
759 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
760 thread->id, thread->exit_code );
763 while (thread->wait) end_wait( thread );
764 send_thread_wakeup( thread, NULL, STATUS_PENDING );
765 /* if it is waiting on the socket, we don't need to send a SIGTERM */
768 kill_console_processes( thread, 0 );
769 debug_exit_thread( thread );
770 abandon_mutexes( thread );
771 remove_process_thread( thread->process, thread );
772 wake_up( &thread->obj, 0 );
773 detach_thread( thread, violent_death ? SIGTERM : 0 );
774 cleanup_thread( thread );
775 release_object( thread );
778 /* take a snapshot of currently running threads */
779 struct thread_snapshot *thread_snap( int *count )
781 struct thread_snapshot *snapshot, *ptr;
782 struct thread *thread;
785 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
786 if (thread->state != TERMINATED) total++;
787 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
789 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
791 if (thread->state == TERMINATED) continue;
792 ptr->thread = thread;
793 ptr->count = thread->obj.refcount;
794 ptr->priority = thread->priority;
795 grab_object( thread );
802 /* gets the current impersonation token */
803 struct token *thread_get_impersonation_token( struct thread *thread )
806 return thread->token;
808 return thread->process->token;
811 /* signal that we are finished booting on the client side */
812 DECL_HANDLER(boot_done)
814 debug_level = max( debug_level, req->debug_level );
815 if (current == booting_thread)
817 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
818 lock_master_socket(0); /* allow other clients now */
822 /* create a new thread */
823 DECL_HANDLER(new_thread)
825 struct thread *thread;
826 int request_fd = thread_get_inflight_fd( current, req->request_fd );
828 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
830 if (request_fd != -1) close( request_fd );
831 set_error( STATUS_INVALID_HANDLE );
835 if ((thread = create_thread( request_fd, current->process )))
837 if (req->suspend) thread->suspend++;
838 reply->tid = get_thread_id( thread );
839 if ((reply->handle = alloc_handle( current->process, thread,
840 THREAD_ALL_ACCESS, req->inherit )))
842 /* thread object will be released when the thread gets killed */
845 kill_thread( thread, 1 );
849 /* initialize a new thread */
850 DECL_HANDLER(init_thread)
852 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
853 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
855 if (current->unix_pid != -1)
857 fatal_protocol_error( current, "init_thread: already running\n" );
860 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
862 fatal_protocol_error( current, "bad reply fd\n" );
867 fatal_protocol_error( current, "bad wait fd\n" );
870 if (!(current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, ¤t->obj )))
873 fatal_protocol_error( current, "could not allocate reply fd\n" );
876 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, ¤t->obj )))
879 current->unix_pid = req->unix_pid;
880 current->unix_tid = req->unix_tid;
881 current->teb = req->teb;
883 if (current->suspend + current->process->suspend > 0) stop_thread( current );
884 if (current->process->running_threads > 1)
885 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
887 reply->pid = get_process_id( current->process );
888 reply->tid = get_thread_id( current );
889 reply->boot = (current == booting_thread);
890 reply->version = SERVER_PROTOCOL_VERSION;
894 if (reply_fd != -1) close( reply_fd );
895 if (wait_fd != -1) close( wait_fd );
898 /* terminate a thread */
899 DECL_HANDLER(terminate_thread)
901 struct thread *thread;
905 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
907 thread->exit_code = req->exit_code;
908 if (thread != current) kill_thread( thread, 1 );
912 reply->last = (thread->process->running_threads == 1);
914 release_object( thread );
918 /* open a handle to a thread */
919 DECL_HANDLER(open_thread)
921 struct thread *thread = get_thread_from_id( req->tid );
926 reply->handle = alloc_handle( current->process, thread, req->access, req->inherit );
927 release_object( thread );
931 /* fetch information about a thread */
932 DECL_HANDLER(get_thread_info)
934 struct thread *thread;
935 obj_handle_t handle = req->handle;
937 if (!handle) thread = get_thread_from_id( req->tid_in );
938 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
942 reply->pid = get_process_id( thread->process );
943 reply->tid = get_thread_id( thread );
944 reply->teb = thread->teb;
945 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
946 reply->priority = thread->priority;
947 reply->affinity = thread->affinity;
948 reply->creation_time = thread->creation_time;
949 reply->exit_time = thread->exit_time;
951 release_object( thread );
955 /* set information about a thread */
956 DECL_HANDLER(set_thread_info)
958 struct thread *thread;
960 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
962 set_thread_info( thread, req );
963 release_object( thread );
967 /* suspend a thread */
968 DECL_HANDLER(suspend_thread)
970 struct thread *thread;
972 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
974 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
975 else reply->count = suspend_thread( thread );
976 release_object( thread );
980 /* resume a thread */
981 DECL_HANDLER(resume_thread)
983 struct thread *thread;
985 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
987 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
988 else reply->count = resume_thread( thread );
989 release_object( thread );
993 /* select on a handle list */
996 int count = get_req_data_size() / sizeof(int);
997 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout, req->signal );
1000 /* queue an APC for a thread */
1001 DECL_HANDLER(queue_apc)
1003 struct thread *thread;
1004 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
1006 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
1007 req->arg1, req->arg2, req->arg3 );
1008 release_object( thread );
1012 /* get next APC to call */
1013 DECL_HANDLER(get_apc)
1015 struct thread_apc *apc;
1019 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
1023 reply->type = APC_NONE;
1026 /* Optimization: ignore APCs that have a NULL func; they are only used
1027 * to wake up a thread, but since we got here the thread woke up already.
1028 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1030 if (apc->func || apc->type == APC_ASYNC_IO) break;
1033 reply->func = apc->func;
1034 reply->type = apc->type;
1035 reply->arg1 = apc->arg1;
1036 reply->arg2 = apc->arg2;
1037 reply->arg3 = apc->arg3;
1041 /* fetch a selector entry for a thread */
1042 DECL_HANDLER(get_selector_entry)
1044 struct thread *thread;
1045 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1047 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1048 release_object( thread );