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_lookup_name, /* lookup_name */
98 no_close_handle, /* close_handle */
99 destroy_thread /* destroy */
102 static const struct fd_ops thread_fd_ops =
104 NULL, /* get_poll_events */
105 thread_poll_event, /* poll_event */
106 no_flush, /* flush */
107 no_get_file_info, /* get_file_info */
108 no_queue_async, /* queue_async */
109 no_cancel_async /* cancel_async */
112 static struct list thread_list = LIST_INIT(thread_list);
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;
122 thread->suspend_context = NULL;
124 thread->debug_ctx = NULL;
125 thread->debug_event = NULL;
126 thread->queue = NULL;
129 thread->req_data = NULL;
130 thread->req_toread = 0;
131 thread->reply_data = NULL;
132 thread->reply_towrite = 0;
133 thread->request_fd = NULL;
134 thread->reply_fd = NULL;
135 thread->wait_fd = NULL;
136 thread->state = RUNNING;
137 thread->attached = 0;
138 thread->exit_code = 0;
139 thread->priority = THREAD_PRIORITY_NORMAL;
140 thread->affinity = 1;
142 thread->creation_time = time(NULL);
143 thread->exit_time = 0;
144 thread->desktop_users = 0;
146 list_init( &thread->mutex_list );
147 list_init( &thread->system_apc );
148 list_init( &thread->user_apc );
150 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
151 thread->inflight[i].server = thread->inflight[i].client = -1;
154 /* check if address looks valid for a client-side data structure (TEB etc.) */
155 static inline int is_valid_address( void *addr )
157 return addr && !((unsigned int)addr % sizeof(int));
160 /* create a new thread */
161 struct thread *create_thread( int fd, struct process *process )
163 struct thread *thread;
165 if (!(thread = alloc_object( &thread_ops ))) return NULL;
167 init_thread_structure( thread );
169 thread->process = (struct process *)grab_object( process );
170 thread->desktop = process->desktop;
171 if (!current) current = thread;
173 list_add_head( &thread_list, &thread->entry );
175 if (!(thread->id = alloc_ptid( thread )))
177 release_object( thread );
180 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
182 release_object( thread );
186 thread->token = (struct token *) grab_object( process->token );
188 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
189 add_process_thread( thread->process, thread );
193 /* handle a client event */
194 static void thread_poll_event( struct fd *fd, int event )
196 struct thread *thread = get_fd_user( fd );
197 assert( thread->obj.ops == &thread_ops );
199 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
200 else if (event & POLLIN) read_request( thread );
201 else if (event & POLLOUT) write_reply( thread );
204 /* cleanup everything that is no longer needed by a dead thread */
205 /* used by destroy_thread and kill_thread */
206 static void cleanup_thread( struct thread *thread )
209 struct thread_apc *apc;
211 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
212 if (thread->req_data) free( thread->req_data );
213 if (thread->reply_data) free( thread->reply_data );
214 if (thread->request_fd) release_object( thread->request_fd );
215 if (thread->reply_fd) release_object( thread->reply_fd );
216 if (thread->wait_fd) release_object( thread->wait_fd );
217 if (thread->suspend_context) free( thread->suspend_context );
218 free_msg_queue( thread );
219 cleanup_clipboard_thread(thread);
220 destroy_thread_windows( thread );
221 close_thread_desktop( thread );
222 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
224 if (thread->inflight[i].client != -1)
226 close( thread->inflight[i].server );
227 thread->inflight[i].client = thread->inflight[i].server = -1;
230 thread->req_data = NULL;
231 thread->reply_data = NULL;
232 thread->request_fd = NULL;
233 thread->reply_fd = NULL;
234 thread->wait_fd = NULL;
235 thread->context = NULL;
236 thread->suspend_context = NULL;
240 /* destroy a thread when its refcount is 0 */
241 static void destroy_thread( struct object *obj )
243 struct thread *thread = (struct thread *)obj;
244 assert( obj->ops == &thread_ops );
246 assert( !thread->debug_ctx ); /* cannot still be debugging something */
247 list_remove( &thread->entry );
248 cleanup_thread( thread );
249 release_object( thread->process );
250 if (thread->id) free_ptid( thread->id );
251 if (thread->token) release_object( thread->token );
254 /* dump a thread on stdout for debugging purposes */
255 static void dump_thread( struct object *obj, int verbose )
257 struct thread *thread = (struct thread *)obj;
258 assert( obj->ops == &thread_ops );
260 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
261 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
264 static int thread_signaled( struct object *obj, struct thread *thread )
266 struct thread *mythread = (struct thread *)obj;
267 return (mythread->state == TERMINATED);
270 /* get a thread pointer from a thread id (and increment the refcount) */
271 struct thread *get_thread_from_id( thread_id_t id )
273 struct object *obj = get_ptid_entry( id );
275 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
276 set_error( STATUS_INVALID_CID );
280 /* get a thread from a handle (and increment the refcount) */
281 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
283 return (struct thread *)get_handle_obj( current->process, handle,
284 access, &thread_ops );
287 /* find a thread from a Unix pid */
288 struct thread *get_thread_from_pid( int pid )
290 struct thread *thread;
292 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
294 if (thread->unix_tid == pid) return thread;
296 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
298 if (thread->unix_pid == pid) return thread;
303 /* set all information about a thread */
304 static void set_thread_info( struct thread *thread,
305 const struct set_thread_info_request *req )
307 if (req->mask & SET_THREAD_INFO_PRIORITY)
308 thread->priority = req->priority;
309 if (req->mask & SET_THREAD_INFO_AFFINITY)
311 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
312 else thread->affinity = req->affinity;
314 if (req->mask & SET_THREAD_INFO_TOKEN)
315 security_set_thread_token( thread, req->token );
318 /* stop a thread (at the Unix level) */
319 void stop_thread( struct thread *thread )
321 if (thread->context) return; /* already inside a debug event, no need for a signal */
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, (int *)base ) == -1) goto done;
742 if (read_thread_int( thread, addr + 8192, (int *)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 wake_up( &thread->obj, 0 );
772 if (violent_death) send_thread_signal( thread, SIGTERM );
773 cleanup_thread( thread );
774 remove_process_thread( thread->process, 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 /* create a new thread */
812 DECL_HANDLER(new_thread)
814 struct thread *thread;
815 int request_fd = thread_get_inflight_fd( current, req->request_fd );
817 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
819 if (request_fd != -1) close( request_fd );
820 set_error( STATUS_INVALID_HANDLE );
824 if ((thread = create_thread( request_fd, current->process )))
826 if (req->suspend) thread->suspend++;
827 reply->tid = get_thread_id( thread );
828 if ((reply->handle = alloc_handle( current->process, thread,
829 THREAD_ALL_ACCESS, req->inherit )))
831 /* thread object will be released when the thread gets killed */
834 kill_thread( thread, 1 );
838 /* initialize a new thread */
839 DECL_HANDLER(init_thread)
841 struct process *process = current->process;
842 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
843 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
845 if (current->unix_pid != -1)
847 fatal_protocol_error( current, "init_thread: already running\n" );
850 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
852 fatal_protocol_error( current, "bad reply fd\n" );
857 fatal_protocol_error( current, "bad wait fd\n" );
860 if (!(current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, ¤t->obj )))
863 fatal_protocol_error( current, "could not allocate reply fd\n" );
866 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, ¤t->obj )))
869 if (!is_valid_address(req->teb) || !is_valid_address(req->peb) || !is_valid_address(req->ldt_copy))
871 set_error( STATUS_INVALID_PARAMETER );
875 current->unix_pid = req->unix_pid;
876 current->unix_tid = req->unix_tid;
877 current->teb = req->teb;
879 if (!process->peb) /* first thread, initialize the process too */
881 process->peb = req->peb;
882 process->ldt_copy = req->ldt_copy;
883 reply->info_size = init_process( current );
887 if (current->suspend + process->suspend > 0) stop_thread( current );
888 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
890 debug_level = max( debug_level, req->debug_level );
892 reply->pid = get_process_id( process );
893 reply->tid = get_thread_id( current );
894 reply->version = SERVER_PROTOCOL_VERSION;
895 reply->server_start = server_start_time;
899 if (reply_fd != -1) close( reply_fd );
900 if (wait_fd != -1) close( wait_fd );
903 /* terminate a thread */
904 DECL_HANDLER(terminate_thread)
906 struct thread *thread;
910 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
912 thread->exit_code = req->exit_code;
913 if (thread != current) kill_thread( thread, 1 );
917 reply->last = (thread->process->running_threads == 1);
919 release_object( thread );
923 /* open a handle to a thread */
924 DECL_HANDLER(open_thread)
926 struct thread *thread = get_thread_from_id( req->tid );
931 reply->handle = alloc_handle( current->process, thread, req->access, req->inherit );
932 release_object( thread );
936 /* fetch information about a thread */
937 DECL_HANDLER(get_thread_info)
939 struct thread *thread;
940 obj_handle_t handle = req->handle;
942 if (!handle) thread = get_thread_from_id( req->tid_in );
943 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
947 reply->pid = get_process_id( thread->process );
948 reply->tid = get_thread_id( thread );
949 reply->teb = thread->teb;
950 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
951 reply->priority = thread->priority;
952 reply->affinity = thread->affinity;
953 reply->creation_time = thread->creation_time;
954 reply->exit_time = thread->exit_time;
956 release_object( thread );
960 /* set information about a thread */
961 DECL_HANDLER(set_thread_info)
963 struct thread *thread;
965 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
967 set_thread_info( thread, req );
968 release_object( thread );
972 /* suspend a thread */
973 DECL_HANDLER(suspend_thread)
975 struct thread *thread;
977 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
979 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
980 else reply->count = suspend_thread( thread );
981 release_object( thread );
985 /* resume a thread */
986 DECL_HANDLER(resume_thread)
988 struct thread *thread;
990 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
992 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
993 else reply->count = resume_thread( thread );
994 release_object( thread );
998 /* select on a handle list */
1001 int count = get_req_data_size() / sizeof(int);
1002 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout, req->signal );
1005 /* queue an APC for a thread */
1006 DECL_HANDLER(queue_apc)
1008 struct thread *thread;
1009 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
1011 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
1012 req->arg1, req->arg2, req->arg3 );
1013 release_object( thread );
1017 /* get next APC to call */
1018 DECL_HANDLER(get_apc)
1020 struct thread_apc *apc;
1024 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
1028 reply->type = APC_NONE;
1031 /* Optimization: ignore APCs that have a NULL func; they are only used
1032 * to wake up a thread, but since we got here the thread woke up already.
1033 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1035 if (apc->func || apc->type == APC_ASYNC_IO) break;
1038 reply->func = apc->func;
1039 reply->type = apc->type;
1040 reply->arg1 = apc->arg1;
1041 reply->arg2 = apc->arg2;
1042 reply->arg3 = apc->arg3;
1046 /* retrieve the current context of a thread */
1047 DECL_HANDLER(get_thread_context)
1049 struct thread *thread;
1052 if (get_reply_max_size() < sizeof(CONTEXT))
1054 set_error( STATUS_INVALID_PARAMETER );
1057 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1061 if (thread != current || !thread->suspend_context)
1063 /* not suspended, shouldn't happen */
1064 set_error( STATUS_INVALID_PARAMETER );
1068 if (thread->context == thread->suspend_context) thread->context = NULL;
1069 set_reply_data_ptr( thread->suspend_context, sizeof(CONTEXT) );
1070 thread->suspend_context = NULL;
1073 else if (thread != current && !thread->context)
1075 /* thread is not suspended, retry (if it's still running) */
1076 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1077 else set_error( STATUS_PENDING );
1079 else if ((data = set_reply_data_size( sizeof(CONTEXT) )))
1081 memset( data, 0, sizeof(CONTEXT) );
1082 get_thread_context( thread, data, req->flags );
1084 release_object( thread );
1087 /* set the current context of a thread */
1088 DECL_HANDLER(set_thread_context)
1090 struct thread *thread;
1092 if (get_req_data_size() < sizeof(CONTEXT))
1094 set_error( STATUS_INVALID_PARAMETER );
1097 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1101 if (thread != current || thread->context)
1103 /* nested suspend or exception, shouldn't happen */
1104 set_error( STATUS_INVALID_PARAMETER );
1106 else if ((thread->suspend_context = mem_alloc( sizeof(CONTEXT) )))
1108 memcpy( thread->suspend_context, get_req_data(), sizeof(CONTEXT) );
1109 thread->context = thread->suspend_context;
1112 else if (thread != current && !thread->context)
1114 /* thread is not suspended, retry (if it's still running) */
1115 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1116 else set_error( STATUS_PENDING );
1120 set_thread_context( thread, get_req_data(), req->flags );
1122 release_object( thread );
1125 /* fetch a selector entry for a thread */
1126 DECL_HANDLER(get_selector_entry)
1128 struct thread *thread;
1129 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1131 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1132 release_object( thread );