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>
40 #define WIN32_NO_STATUS
57 struct thread_wait *next; /* next wait structure for this thread */
58 struct thread *thread; /* owner thread */
59 int count; /* count of objects */
61 void *cookie; /* magic cookie to return to client */
62 struct timeval timeout;
63 struct timeout_user *user;
64 struct wait_queue_entry queues[1];
67 /* asynchronous procedure calls */
71 struct list entry; /* queue linked list */
72 struct object *owner; /* object that queued this apc */
73 void *func; /* function to call in client */
74 enum apc_type type; /* type of apc function */
75 int nb_args; /* number of arguments */
76 void *arg1; /* function arguments */
82 /* thread operations */
84 static void dump_thread( struct object *obj, int verbose );
85 static int thread_signaled( struct object *obj, struct thread *thread );
86 static unsigned int thread_map_access( struct object *obj, unsigned int access );
87 static void thread_poll_event( struct fd *fd, int event );
88 static void destroy_thread( struct object *obj );
89 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
91 static const struct object_ops thread_ops =
93 sizeof(struct thread), /* size */
94 dump_thread, /* dump */
95 add_queue, /* add_queue */
96 remove_queue, /* remove_queue */
97 thread_signaled, /* signaled */
98 no_satisfied, /* satisfied */
99 no_signal, /* signal */
100 no_get_fd, /* get_fd */
101 thread_map_access, /* map_access */
102 no_lookup_name, /* lookup_name */
103 no_close_handle, /* close_handle */
104 destroy_thread /* destroy */
107 static const struct fd_ops thread_fd_ops =
109 NULL, /* get_poll_events */
110 thread_poll_event, /* poll_event */
111 no_flush, /* flush */
112 no_get_file_info, /* get_file_info */
113 no_queue_async, /* queue_async */
114 no_cancel_async /* cancel_async */
117 static struct list thread_list = LIST_INIT(thread_list);
119 /* initialize the structure for a newly allocated thread */
120 inline static void init_thread_structure( struct thread *thread )
124 thread->unix_pid = -1; /* not known yet */
125 thread->unix_tid = -1; /* not known yet */
126 thread->context = NULL;
127 thread->suspend_context = NULL;
129 thread->debug_ctx = NULL;
130 thread->debug_event = NULL;
131 thread->debug_break = 0;
132 thread->queue = NULL;
135 thread->req_data = NULL;
136 thread->req_toread = 0;
137 thread->reply_data = NULL;
138 thread->reply_towrite = 0;
139 thread->request_fd = NULL;
140 thread->reply_fd = NULL;
141 thread->wait_fd = NULL;
142 thread->state = RUNNING;
143 thread->exit_code = 0;
144 thread->priority = THREAD_PRIORITY_NORMAL;
145 thread->affinity = 1;
147 thread->creation_time = time(NULL);
148 thread->exit_time = 0;
149 thread->desktop_users = 0;
151 list_init( &thread->mutex_list );
152 list_init( &thread->system_apc );
153 list_init( &thread->user_apc );
155 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
156 thread->inflight[i].server = thread->inflight[i].client = -1;
159 /* check if address looks valid for a client-side data structure (TEB etc.) */
160 static inline int is_valid_address( void *addr )
162 return addr && !((unsigned int)addr % sizeof(int));
165 /* create a new thread */
166 struct thread *create_thread( int fd, struct process *process )
168 struct thread *thread;
170 if (!(thread = alloc_object( &thread_ops ))) return NULL;
172 init_thread_structure( thread );
174 thread->process = (struct process *)grab_object( process );
175 thread->desktop = process->desktop;
176 if (!current) current = thread;
178 list_add_head( &thread_list, &thread->entry );
180 if (!(thread->id = alloc_ptid( thread )))
182 release_object( thread );
185 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
187 release_object( thread );
191 thread->token = (struct token *) grab_object( process->token );
193 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
194 add_process_thread( thread->process, thread );
198 /* handle a client event */
199 static void thread_poll_event( struct fd *fd, int event )
201 struct thread *thread = get_fd_user( fd );
202 assert( thread->obj.ops == &thread_ops );
204 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
205 else if (event & POLLIN) read_request( thread );
206 else if (event & POLLOUT) write_reply( thread );
209 /* cleanup everything that is no longer needed by a dead thread */
210 /* used by destroy_thread and kill_thread */
211 static void cleanup_thread( struct thread *thread )
214 struct thread_apc *apc;
216 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
217 if (thread->req_data) free( thread->req_data );
218 if (thread->reply_data) free( thread->reply_data );
219 if (thread->request_fd) release_object( thread->request_fd );
220 if (thread->reply_fd) release_object( thread->reply_fd );
221 if (thread->wait_fd) release_object( thread->wait_fd );
222 if (thread->suspend_context) free( thread->suspend_context );
223 free_msg_queue( thread );
224 cleanup_clipboard_thread(thread);
225 destroy_thread_windows( thread );
226 close_thread_desktop( thread );
227 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
229 if (thread->inflight[i].client != -1)
231 close( thread->inflight[i].server );
232 thread->inflight[i].client = thread->inflight[i].server = -1;
235 thread->req_data = NULL;
236 thread->reply_data = NULL;
237 thread->request_fd = NULL;
238 thread->reply_fd = NULL;
239 thread->wait_fd = NULL;
240 thread->context = NULL;
241 thread->suspend_context = NULL;
245 /* destroy a thread when its refcount is 0 */
246 static void destroy_thread( struct object *obj )
248 struct thread *thread = (struct thread *)obj;
249 assert( obj->ops == &thread_ops );
251 assert( !thread->debug_ctx ); /* cannot still be debugging something */
252 list_remove( &thread->entry );
253 cleanup_thread( thread );
254 release_object( thread->process );
255 if (thread->id) free_ptid( thread->id );
256 if (thread->token) release_object( thread->token );
259 /* dump a thread on stdout for debugging purposes */
260 static void dump_thread( struct object *obj, int verbose )
262 struct thread *thread = (struct thread *)obj;
263 assert( obj->ops == &thread_ops );
265 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
266 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
269 static int thread_signaled( struct object *obj, struct thread *thread )
271 struct thread *mythread = (struct thread *)obj;
272 return (mythread->state == TERMINATED);
275 static unsigned int thread_map_access( struct object *obj, unsigned int access )
277 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
278 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
279 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
280 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
281 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
284 /* get a thread pointer from a thread id (and increment the refcount) */
285 struct thread *get_thread_from_id( thread_id_t id )
287 struct object *obj = get_ptid_entry( id );
289 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
290 set_error( STATUS_INVALID_CID );
294 /* get a thread from a handle (and increment the refcount) */
295 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
297 return (struct thread *)get_handle_obj( current->process, handle,
298 access, &thread_ops );
301 /* find a thread from a Unix pid */
302 struct thread *get_thread_from_pid( int pid )
304 struct thread *thread;
306 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
308 if (thread->unix_tid == pid) return thread;
310 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
312 if (thread->unix_pid == pid) return thread;
317 /* set all information about a thread */
318 static void set_thread_info( struct thread *thread,
319 const struct set_thread_info_request *req )
321 if (req->mask & SET_THREAD_INFO_PRIORITY)
322 thread->priority = req->priority;
323 if (req->mask & SET_THREAD_INFO_AFFINITY)
325 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
326 else thread->affinity = req->affinity;
328 if (req->mask & SET_THREAD_INFO_TOKEN)
329 security_set_thread_token( thread, req->token );
332 /* stop a thread (at the Unix level) */
333 void stop_thread( struct thread *thread )
335 if (thread->context) return; /* already inside a debug event, no need for a signal */
336 /* can't stop a thread while initialisation is in progress */
337 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
340 /* suspend a thread */
341 static int suspend_thread( struct thread *thread )
343 int old_count = thread->suspend;
344 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
346 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
348 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
352 /* resume a thread */
353 static int resume_thread( struct thread *thread )
355 int old_count = thread->suspend;
356 if (thread->suspend > 0)
358 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
363 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
364 int add_queue( struct object *obj, struct wait_queue_entry *entry )
368 list_add_tail( &obj->wait_queue, &entry->entry );
372 /* remove a thread from an object wait queue */
373 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
375 list_remove( &entry->entry );
376 release_object( obj );
380 static void end_wait( struct thread *thread )
382 struct thread_wait *wait = thread->wait;
383 struct wait_queue_entry *entry;
387 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
388 entry->obj->ops->remove_queue( entry->obj, entry );
389 if (wait->user) remove_timeout_user( wait->user );
390 thread->wait = wait->next;
394 /* build the thread wait structure */
395 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
397 struct thread_wait *wait;
398 struct wait_queue_entry *entry;
401 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
402 wait->next = current->wait;
403 wait->thread = current;
407 current->wait = wait;
408 if (flags & SELECT_TIMEOUT)
410 wait->timeout.tv_sec = timeout->sec;
411 wait->timeout.tv_usec = timeout->usec;
414 for (i = 0, entry = wait->queues; i < count; i++, entry++)
416 struct object *obj = objects[i];
417 entry->thread = current;
418 if (!obj->ops->add_queue( obj, entry ))
428 /* check if the thread waiting condition is satisfied */
429 static int check_wait( struct thread *thread )
432 struct thread_wait *wait = thread->wait;
433 struct wait_queue_entry *entry = wait->queues;
435 /* Suspended threads may not acquire locks */
436 if (thread->process->suspend + thread->suspend > 0) return -1;
439 if (wait->flags & SELECT_ALL)
442 /* Note: we must check them all anyway, as some objects may
443 * want to do something when signaled, even if others are not */
444 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
445 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
446 if (not_ok) goto other_checks;
447 /* Wait satisfied: tell it to all objects */
449 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
450 if (entry->obj->ops->satisfied( entry->obj, thread ))
451 signaled = STATUS_ABANDONED_WAIT_0;
456 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
458 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
459 /* Wait satisfied: tell it to the object */
461 if (entry->obj->ops->satisfied( entry->obj, thread ))
462 signaled = i + STATUS_ABANDONED_WAIT_0;
468 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
469 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
470 if (wait->flags & SELECT_TIMEOUT)
473 gettimeofday( &now, NULL );
474 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
479 /* send the wakeup signal to a thread */
480 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
482 struct wake_up_reply reply;
485 reply.cookie = cookie;
486 reply.signaled = signaled;
487 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
490 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
491 else if (errno == EPIPE)
492 kill_thread( thread, 0 ); /* normal death */
494 fatal_protocol_perror( thread, "write" );
498 /* attempt to wake up a thread */
499 /* return >0 if OK, 0 if the wait condition is still not satisfied */
500 int wake_thread( struct thread *thread )
505 for (count = 0; thread->wait; count++)
507 if ((signaled = check_wait( thread )) == -1) break;
509 cookie = thread->wait->cookie;
510 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
511 thread->id, signaled, cookie );
513 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
519 /* thread wait timeout */
520 static void thread_timeout( void *ptr )
522 struct thread_wait *wait = ptr;
523 struct thread *thread = wait->thread;
524 void *cookie = wait->cookie;
527 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
528 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
530 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
531 thread->id, (int)STATUS_TIMEOUT, cookie );
533 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
534 /* check if other objects have become signaled in the meantime */
535 wake_thread( thread );
538 /* try signaling an event flag, a semaphore or a mutex */
539 static int signal_object( obj_handle_t handle )
544 obj = get_handle_obj( current->process, handle, 0, NULL );
547 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
548 release_object( obj );
553 /* select on a list of handles */
554 static void select_on( int count, void *cookie, const obj_handle_t *handles,
555 int flags, const abs_time_t *timeout, obj_handle_t signal_obj )
558 struct object *objects[MAXIMUM_WAIT_OBJECTS];
560 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
562 set_error( STATUS_INVALID_PARAMETER );
565 for (i = 0; i < count; i++)
567 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
571 if (i < count) goto done;
572 if (!wait_on( count, objects, flags, timeout )) goto done;
574 /* signal the object */
577 if (!signal_object( signal_obj ))
582 /* check if we woke ourselves up */
583 if (!current->wait) goto done;
586 if ((ret = check_wait( current )) != -1)
588 /* condition is already satisfied */
594 /* now we need to wait */
595 if (flags & SELECT_TIMEOUT)
597 if (!(current->wait->user = add_timeout_user( ¤t->wait->timeout,
598 thread_timeout, current->wait )))
604 current->wait->cookie = cookie;
605 set_error( STATUS_PENDING );
608 while (--i >= 0) release_object( objects[i] );
611 /* attempt to wake threads sleeping on the object wait queue */
612 void wake_up( struct object *obj, int max )
614 struct list *ptr, *next;
616 LIST_FOR_EACH_SAFE( ptr, next, &obj->wait_queue )
618 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
619 if (wake_thread( entry->thread ))
621 if (max && !--max) break;
626 /* queue an async procedure call */
627 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
628 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
630 struct thread_apc *apc;
631 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
633 /* cancel a possible previous APC with the same owner */
634 if (owner) thread_cancel_apc( thread, owner, system );
635 if (thread->state == TERMINATED) return 0;
637 if (!(apc = mem_alloc( sizeof(*apc) ))) return 0;
644 list_add_tail( queue, &apc->entry );
645 if (!list_prev( queue, &apc->entry )) /* first one */
646 wake_thread( thread );
651 /* cancel the async procedure call owned by a specific object */
652 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
654 struct thread_apc *apc;
655 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
656 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
658 if (apc->owner != owner) continue;
659 list_remove( &apc->entry );
665 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
666 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
668 struct thread_apc *apc = NULL;
669 struct list *ptr = list_head( &thread->system_apc );
671 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
674 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
680 /* add an fd to the inflight list */
681 /* return list index, or -1 on error */
682 int thread_add_inflight_fd( struct thread *thread, int client, int server )
686 if (server == -1) return -1;
693 /* first check if we already have an entry for this fd */
694 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
695 if (thread->inflight[i].client == client)
697 close( thread->inflight[i].server );
698 thread->inflight[i].server = server;
702 /* now find a free spot to store it */
703 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
704 if (thread->inflight[i].client == -1)
706 thread->inflight[i].client = client;
707 thread->inflight[i].server = server;
713 /* get an inflight fd and purge it from the list */
714 /* the fd must be closed when no longer used */
715 int thread_get_inflight_fd( struct thread *thread, int client )
719 if (client == -1) return -1;
723 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
725 if (thread->inflight[i].client == client)
727 ret = thread->inflight[i].server;
728 thread->inflight[i].server = thread->inflight[i].client = -1;
732 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
736 /* kill a thread on the spot */
737 void kill_thread( struct thread *thread, int violent_death )
739 if (thread->state == TERMINATED) return; /* already killed */
740 thread->state = TERMINATED;
741 thread->exit_time = time(NULL);
742 if (current == thread) current = NULL;
744 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
745 thread->id, thread->exit_code );
748 while (thread->wait) end_wait( thread );
749 send_thread_wakeup( thread, NULL, STATUS_PENDING );
750 /* if it is waiting on the socket, we don't need to send a SIGTERM */
753 kill_console_processes( thread, 0 );
754 debug_exit_thread( thread );
755 abandon_mutexes( thread );
756 wake_up( &thread->obj, 0 );
757 if (violent_death) send_thread_signal( thread, SIGTERM );
758 cleanup_thread( thread );
759 remove_process_thread( thread->process, thread );
760 release_object( thread );
763 /* trigger a breakpoint event in a given thread */
764 void break_thread( struct thread *thread )
766 struct debug_event_exception data;
768 assert( thread->context );
770 data.record.ExceptionCode = EXCEPTION_BREAKPOINT;
771 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
772 data.record.ExceptionRecord = NULL;
773 data.record.ExceptionAddress = get_context_ip( thread->context );
774 data.record.NumberParameters = 0;
776 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
777 thread->debug_break = 0;
780 /* take a snapshot of currently running threads */
781 struct thread_snapshot *thread_snap( int *count )
783 struct thread_snapshot *snapshot, *ptr;
784 struct thread *thread;
787 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
788 if (thread->state != TERMINATED) total++;
789 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
791 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
793 if (thread->state == TERMINATED) continue;
794 ptr->thread = thread;
795 ptr->count = thread->obj.refcount;
796 ptr->priority = thread->priority;
797 grab_object( thread );
804 /* gets the current impersonation token */
805 struct token *thread_get_impersonation_token( struct thread *thread )
808 return thread->token;
810 return thread->process->token;
813 /* create a new thread */
814 DECL_HANDLER(new_thread)
816 struct thread *thread;
817 int request_fd = thread_get_inflight_fd( current, req->request_fd );
819 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
821 if (request_fd != -1) close( request_fd );
822 set_error( STATUS_INVALID_HANDLE );
826 if ((thread = create_thread( request_fd, current->process )))
828 if (req->suspend) thread->suspend++;
829 reply->tid = get_thread_id( thread );
830 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
832 /* thread object will be released when the thread gets killed */
835 kill_thread( thread, 1 );
839 /* initialize a new thread */
840 DECL_HANDLER(init_thread)
842 struct process *process = current->process;
843 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
844 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
846 if (current->unix_pid != -1)
848 fatal_protocol_error( current, "init_thread: already running\n" );
851 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
853 fatal_protocol_error( current, "bad reply fd\n" );
858 fatal_protocol_error( current, "bad wait fd\n" );
861 if (!(current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, ¤t->obj )))
864 fatal_protocol_error( current, "could not allocate reply fd\n" );
867 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, ¤t->obj )))
870 if (!is_valid_address(req->teb) || !is_valid_address(req->peb) || !is_valid_address(req->ldt_copy))
872 set_error( STATUS_INVALID_PARAMETER );
876 current->unix_pid = req->unix_pid;
877 current->unix_tid = req->unix_tid;
878 current->teb = req->teb;
880 if (!process->peb) /* first thread, initialize the process too */
882 process->peb = req->peb;
883 process->ldt_copy = req->ldt_copy;
884 reply->info_size = init_process( current );
888 if (current->suspend + process->suspend > 0) stop_thread( current );
889 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
891 debug_level = max( debug_level, req->debug_level );
893 reply->pid = get_process_id( process );
894 reply->tid = get_thread_id( current );
895 reply->version = SERVER_PROTOCOL_VERSION;
896 reply->server_start = server_start_time;
900 if (reply_fd != -1) close( reply_fd );
901 if (wait_fd != -1) close( wait_fd );
904 /* terminate a thread */
905 DECL_HANDLER(terminate_thread)
907 struct thread *thread;
911 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
913 thread->exit_code = req->exit_code;
914 if (thread != current) kill_thread( thread, 1 );
918 reply->last = (thread->process->running_threads == 1);
920 release_object( thread );
924 /* open a handle to a thread */
925 DECL_HANDLER(open_thread)
927 struct thread *thread = get_thread_from_id( req->tid );
932 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
933 release_object( thread );
937 /* fetch information about a thread */
938 DECL_HANDLER(get_thread_info)
940 struct thread *thread;
941 obj_handle_t handle = req->handle;
943 if (!handle) thread = get_thread_from_id( req->tid_in );
944 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
948 reply->pid = get_process_id( thread->process );
949 reply->tid = get_thread_id( thread );
950 reply->teb = thread->teb;
951 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
952 reply->priority = thread->priority;
953 reply->affinity = thread->affinity;
954 reply->creation_time = thread->creation_time;
955 reply->exit_time = thread->exit_time;
957 release_object( thread );
961 /* set information about a thread */
962 DECL_HANDLER(set_thread_info)
964 struct thread *thread;
966 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
968 set_thread_info( thread, req );
969 release_object( thread );
973 /* suspend a thread */
974 DECL_HANDLER(suspend_thread)
976 struct thread *thread;
978 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
980 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
981 else reply->count = suspend_thread( thread );
982 release_object( thread );
986 /* resume a thread */
987 DECL_HANDLER(resume_thread)
989 struct thread *thread;
991 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
993 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
994 else reply->count = resume_thread( thread );
995 release_object( thread );
999 /* select on a handle list */
1000 DECL_HANDLER(select)
1002 int count = get_req_data_size() / sizeof(int);
1003 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout, req->signal );
1006 /* queue an APC for a thread */
1007 DECL_HANDLER(queue_apc)
1009 struct thread *thread;
1010 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
1012 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
1013 req->arg1, req->arg2, req->arg3 );
1014 release_object( thread );
1018 /* get next APC to call */
1019 DECL_HANDLER(get_apc)
1021 struct thread_apc *apc;
1025 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
1029 reply->type = APC_NONE;
1032 /* Optimization: ignore APCs that have a NULL func; they are only used
1033 * to wake up a thread, but since we got here the thread woke up already.
1034 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1036 if (apc->func || apc->type == APC_ASYNC_IO) break;
1039 reply->func = apc->func;
1040 reply->type = apc->type;
1041 reply->arg1 = apc->arg1;
1042 reply->arg2 = apc->arg2;
1043 reply->arg3 = apc->arg3;
1047 /* retrieve the current context of a thread */
1048 DECL_HANDLER(get_thread_context)
1050 struct thread *thread;
1053 if (get_reply_max_size() < sizeof(CONTEXT))
1055 set_error( STATUS_INVALID_PARAMETER );
1058 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1062 if (thread != current || !thread->suspend_context)
1064 /* not suspended, shouldn't happen */
1065 set_error( STATUS_INVALID_PARAMETER );
1069 if (thread->context == thread->suspend_context) thread->context = NULL;
1070 set_reply_data_ptr( thread->suspend_context, sizeof(CONTEXT) );
1071 thread->suspend_context = NULL;
1074 else if (thread != current && !thread->context)
1076 /* thread is not suspended, retry (if it's still running) */
1077 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1078 else set_error( STATUS_PENDING );
1080 else if ((data = set_reply_data_size( sizeof(CONTEXT) )))
1082 memset( data, 0, sizeof(CONTEXT) );
1083 get_thread_context( thread, data, req->flags );
1085 reply->self = (thread == current);
1086 release_object( thread );
1089 /* set the current context of a thread */
1090 DECL_HANDLER(set_thread_context)
1092 struct thread *thread;
1094 if (get_req_data_size() < sizeof(CONTEXT))
1096 set_error( STATUS_INVALID_PARAMETER );
1099 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1103 if (thread != current || thread->context)
1105 /* nested suspend or exception, shouldn't happen */
1106 set_error( STATUS_INVALID_PARAMETER );
1108 else if ((thread->suspend_context = mem_alloc( sizeof(CONTEXT) )))
1110 memcpy( thread->suspend_context, get_req_data(), sizeof(CONTEXT) );
1111 thread->context = thread->suspend_context;
1112 if (thread->debug_break) break_thread( thread );
1115 else if (thread != current && !thread->context)
1117 /* thread is not suspended, retry (if it's still running) */
1118 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1119 else set_error( STATUS_PENDING );
1123 set_thread_context( thread, get_req_data(), req->flags );
1125 reply->self = (thread == current);
1126 release_object( thread );
1129 /* fetch a selector entry for a thread */
1130 DECL_HANDLER(get_selector_entry)
1132 struct thread *thread;
1133 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1135 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1136 release_object( thread );