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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 void *arg1; /* function arguments */
81 /* thread operations */
83 static void dump_thread( struct object *obj, int verbose );
84 static int thread_signaled( struct object *obj, struct thread *thread );
85 static unsigned int thread_map_access( struct object *obj, unsigned int access );
86 static void thread_poll_event( struct fd *fd, int event );
87 static void destroy_thread( struct object *obj );
88 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
90 static const struct object_ops thread_ops =
92 sizeof(struct thread), /* size */
93 dump_thread, /* dump */
94 add_queue, /* add_queue */
95 remove_queue, /* remove_queue */
96 thread_signaled, /* signaled */
97 no_satisfied, /* satisfied */
98 no_signal, /* signal */
99 no_get_fd, /* get_fd */
100 thread_map_access, /* map_access */
101 no_lookup_name, /* lookup_name */
102 no_close_handle, /* close_handle */
103 destroy_thread /* destroy */
106 static const struct fd_ops thread_fd_ops =
108 NULL, /* get_poll_events */
109 thread_poll_event, /* poll_event */
110 no_flush, /* flush */
111 no_get_file_info, /* get_file_info */
112 no_queue_async, /* queue_async */
113 no_cancel_async /* cancel_async */
116 static struct list thread_list = LIST_INIT(thread_list);
118 /* initialize the structure for a newly allocated thread */
119 inline static void init_thread_structure( struct thread *thread )
123 thread->unix_pid = -1; /* not known yet */
124 thread->unix_tid = -1; /* not known yet */
125 thread->context = NULL;
126 thread->suspend_context = NULL;
128 thread->debug_ctx = NULL;
129 thread->debug_event = NULL;
130 thread->debug_break = 0;
131 thread->queue = NULL;
134 thread->req_data = NULL;
135 thread->req_toread = 0;
136 thread->reply_data = NULL;
137 thread->reply_towrite = 0;
138 thread->request_fd = NULL;
139 thread->reply_fd = NULL;
140 thread->wait_fd = NULL;
141 thread->state = RUNNING;
142 thread->exit_code = 0;
143 thread->priority = 0;
144 thread->affinity = 1;
146 thread->desktop_users = 0;
147 thread->token = NULL;
149 thread->creation_time = current_time;
150 thread->exit_time.tv_sec = thread->exit_time.tv_usec = 0;
152 list_init( &thread->mutex_list );
153 list_init( &thread->system_apc );
154 list_init( &thread->user_apc );
156 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
157 thread->inflight[i].server = thread->inflight[i].client = -1;
160 /* check if address looks valid for a client-side data structure (TEB etc.) */
161 static inline int is_valid_address( void *addr )
163 return addr && !((unsigned long)addr % sizeof(int));
166 /* create a new thread */
167 struct thread *create_thread( int fd, struct process *process )
169 struct thread *thread;
171 if (!(thread = alloc_object( &thread_ops ))) return NULL;
173 init_thread_structure( thread );
175 thread->process = (struct process *)grab_object( process );
176 thread->desktop = process->desktop;
177 if (!current) current = thread;
179 list_add_head( &thread_list, &thread->entry );
181 if (!(thread->id = alloc_ptid( thread )))
183 release_object( thread );
186 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
188 release_object( thread );
192 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
193 add_process_thread( thread->process, thread );
197 /* handle a client event */
198 static void thread_poll_event( struct fd *fd, int event )
200 struct thread *thread = get_fd_user( fd );
201 assert( thread->obj.ops == &thread_ops );
203 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
204 else if (event & POLLIN) read_request( thread );
205 else if (event & POLLOUT) write_reply( thread );
208 /* cleanup everything that is no longer needed by a dead thread */
209 /* used by destroy_thread and kill_thread */
210 static void cleanup_thread( struct thread *thread )
213 struct thread_apc *apc;
215 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
216 free( thread->req_data );
217 free( thread->reply_data );
218 if (thread->request_fd) release_object( thread->request_fd );
219 if (thread->reply_fd) release_object( thread->reply_fd );
220 if (thread->wait_fd) release_object( thread->wait_fd );
221 free( thread->suspend_context );
222 free_msg_queue( thread );
223 cleanup_clipboard_thread(thread);
224 destroy_thread_windows( thread );
225 close_thread_desktop( thread );
226 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
228 if (thread->inflight[i].client != -1)
230 close( thread->inflight[i].server );
231 thread->inflight[i].client = thread->inflight[i].server = -1;
234 thread->req_data = NULL;
235 thread->reply_data = NULL;
236 thread->request_fd = NULL;
237 thread->reply_fd = NULL;
238 thread->wait_fd = NULL;
239 thread->context = NULL;
240 thread->suspend_context = NULL;
244 /* destroy a thread when its refcount is 0 */
245 static void destroy_thread( struct object *obj )
247 struct thread *thread = (struct thread *)obj;
248 assert( obj->ops == &thread_ops );
250 assert( !thread->debug_ctx ); /* cannot still be debugging something */
251 list_remove( &thread->entry );
252 cleanup_thread( thread );
253 release_object( thread->process );
254 if (thread->id) free_ptid( thread->id );
255 if (thread->token) release_object( thread->token );
258 /* dump a thread on stdout for debugging purposes */
259 static void dump_thread( struct object *obj, int verbose )
261 struct thread *thread = (struct thread *)obj;
262 assert( obj->ops == &thread_ops );
264 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
265 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
268 static int thread_signaled( struct object *obj, struct thread *thread )
270 struct thread *mythread = (struct thread *)obj;
271 return (mythread->state == TERMINATED);
274 static unsigned int thread_map_access( struct object *obj, unsigned int access )
276 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
277 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
278 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
279 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
280 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
283 /* get a thread pointer from a thread id (and increment the refcount) */
284 struct thread *get_thread_from_id( thread_id_t id )
286 struct object *obj = get_ptid_entry( id );
288 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
289 set_error( STATUS_INVALID_CID );
293 /* get a thread from a handle (and increment the refcount) */
294 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
296 return (struct thread *)get_handle_obj( current->process, handle,
297 access, &thread_ops );
300 /* find a thread from a Unix pid */
301 struct thread *get_thread_from_pid( int pid )
303 struct thread *thread;
305 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
307 if (thread->unix_tid == pid) return thread;
309 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
311 if (thread->unix_pid == pid) return thread;
316 /* set all information about a thread */
317 static void set_thread_info( struct thread *thread,
318 const struct set_thread_info_request *req )
320 if (req->mask & SET_THREAD_INFO_PRIORITY)
321 thread->priority = req->priority;
322 if (req->mask & SET_THREAD_INFO_AFFINITY)
324 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
325 else thread->affinity = req->affinity;
327 if (req->mask & SET_THREAD_INFO_TOKEN)
328 security_set_thread_token( thread, req->token );
331 /* stop a thread (at the Unix level) */
332 void stop_thread( struct thread *thread )
334 if (thread->context) return; /* already inside a debug event, no need for a signal */
335 /* can't stop a thread while initialisation is in progress */
336 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
339 /* suspend a thread */
340 static int suspend_thread( struct thread *thread )
342 int old_count = thread->suspend;
343 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
345 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
347 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
351 /* resume a thread */
352 static int resume_thread( struct thread *thread )
354 int old_count = thread->suspend;
355 if (thread->suspend > 0)
357 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
362 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
363 int add_queue( struct object *obj, struct wait_queue_entry *entry )
367 list_add_tail( &obj->wait_queue, &entry->entry );
371 /* remove a thread from an object wait queue */
372 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
374 list_remove( &entry->entry );
375 release_object( obj );
379 static void end_wait( struct thread *thread )
381 struct thread_wait *wait = thread->wait;
382 struct wait_queue_entry *entry;
386 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
387 entry->obj->ops->remove_queue( entry->obj, entry );
388 if (wait->user) remove_timeout_user( wait->user );
389 thread->wait = wait->next;
393 /* build the thread wait structure */
394 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
396 struct thread_wait *wait;
397 struct wait_queue_entry *entry;
400 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
401 wait->next = current->wait;
402 wait->thread = current;
406 current->wait = wait;
407 if (flags & SELECT_TIMEOUT)
409 wait->timeout.tv_sec = timeout->sec;
410 wait->timeout.tv_usec = timeout->usec;
413 for (i = 0, entry = wait->queues; i < count; i++, entry++)
415 struct object *obj = objects[i];
416 entry->thread = current;
417 if (!obj->ops->add_queue( obj, entry ))
427 /* check if the thread waiting condition is satisfied */
428 static int check_wait( struct thread *thread )
431 struct thread_wait *wait = thread->wait;
432 struct wait_queue_entry *entry = wait->queues;
434 /* Suspended threads may not acquire locks */
435 if (thread->process->suspend + thread->suspend > 0) return -1;
438 if (wait->flags & SELECT_ALL)
441 /* Note: we must check them all anyway, as some objects may
442 * want to do something when signaled, even if others are not */
443 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
444 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
445 if (not_ok) goto other_checks;
446 /* Wait satisfied: tell it to all objects */
448 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
449 if (entry->obj->ops->satisfied( entry->obj, thread ))
450 signaled = STATUS_ABANDONED_WAIT_0;
455 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
457 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
458 /* Wait satisfied: tell it to the object */
460 if (entry->obj->ops->satisfied( entry->obj, thread ))
461 signaled = i + STATUS_ABANDONED_WAIT_0;
467 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
468 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
469 if (wait->flags & SELECT_TIMEOUT)
471 if (!time_before( ¤t_time, &wait->timeout )) return STATUS_TIMEOUT;
476 /* send the wakeup signal to a thread */
477 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
479 struct wake_up_reply reply;
482 reply.cookie = cookie;
483 reply.signaled = signaled;
484 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
487 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
488 else if (errno == EPIPE)
489 kill_thread( thread, 0 ); /* normal death */
491 fatal_protocol_perror( thread, "write" );
495 /* attempt to wake up a thread */
496 /* return >0 if OK, 0 if the wait condition is still not satisfied */
497 int wake_thread( struct thread *thread )
502 for (count = 0; thread->wait; count++)
504 if ((signaled = check_wait( thread )) == -1) break;
506 cookie = thread->wait->cookie;
507 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
508 thread->id, signaled, cookie );
510 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
516 /* thread wait timeout */
517 static void thread_timeout( void *ptr )
519 struct thread_wait *wait = ptr;
520 struct thread *thread = wait->thread;
521 void *cookie = wait->cookie;
524 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
525 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
527 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
528 thread->id, (int)STATUS_TIMEOUT, cookie );
530 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
531 /* check if other objects have become signaled in the meantime */
532 wake_thread( thread );
535 /* try signaling an event flag, a semaphore or a mutex */
536 static int signal_object( obj_handle_t handle )
541 obj = get_handle_obj( current->process, handle, 0, NULL );
544 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
545 release_object( obj );
550 /* select on a list of handles */
551 static void select_on( int count, void *cookie, const obj_handle_t *handles,
552 int flags, const abs_time_t *timeout, obj_handle_t signal_obj )
555 struct object *objects[MAXIMUM_WAIT_OBJECTS];
557 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
559 set_error( STATUS_INVALID_PARAMETER );
562 for (i = 0; i < count; i++)
564 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
568 if (i < count) goto done;
569 if (!wait_on( count, objects, flags, timeout )) goto done;
571 /* signal the object */
574 if (!signal_object( signal_obj ))
579 /* check if we woke ourselves up */
580 if (!current->wait) goto done;
583 if ((ret = check_wait( current )) != -1)
585 /* condition is already satisfied */
591 /* now we need to wait */
592 if (flags & SELECT_TIMEOUT)
594 if (!(current->wait->user = add_timeout_user( ¤t->wait->timeout,
595 thread_timeout, current->wait )))
601 current->wait->cookie = cookie;
602 set_error( STATUS_PENDING );
605 while (--i >= 0) release_object( objects[i] );
608 /* attempt to wake threads sleeping on the object wait queue */
609 void wake_up( struct object *obj, int max )
611 struct list *ptr, *next;
613 LIST_FOR_EACH_SAFE( ptr, next, &obj->wait_queue )
615 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
616 if (wake_thread( entry->thread ))
618 if (max && !--max) break;
623 /* queue an async procedure call */
624 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
625 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
627 struct thread_apc *apc;
628 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
630 /* cancel a possible previous APC with the same owner */
631 if (owner) thread_cancel_apc( thread, owner, system );
632 if (thread->state == TERMINATED) return 0;
634 if (!(apc = mem_alloc( sizeof(*apc) ))) return 0;
641 list_add_tail( queue, &apc->entry );
642 if (!list_prev( queue, &apc->entry )) /* first one */
643 wake_thread( thread );
648 /* cancel the async procedure call owned by a specific object */
649 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
651 struct thread_apc *apc;
652 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
653 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
655 if (apc->owner != owner) continue;
656 list_remove( &apc->entry );
662 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
663 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
665 struct thread_apc *apc = NULL;
666 struct list *ptr = list_head( &thread->system_apc );
668 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
671 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
677 /* add an fd to the inflight list */
678 /* return list index, or -1 on error */
679 int thread_add_inflight_fd( struct thread *thread, int client, int server )
683 if (server == -1) return -1;
690 /* first check if we already have an entry for this fd */
691 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
692 if (thread->inflight[i].client == client)
694 close( thread->inflight[i].server );
695 thread->inflight[i].server = server;
699 /* now find a free spot to store it */
700 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
701 if (thread->inflight[i].client == -1)
703 thread->inflight[i].client = client;
704 thread->inflight[i].server = server;
710 /* get an inflight fd and purge it from the list */
711 /* the fd must be closed when no longer used */
712 int thread_get_inflight_fd( struct thread *thread, int client )
716 if (client == -1) return -1;
720 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
722 if (thread->inflight[i].client == client)
724 ret = thread->inflight[i].server;
725 thread->inflight[i].server = thread->inflight[i].client = -1;
729 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
733 /* kill a thread on the spot */
734 void kill_thread( struct thread *thread, int violent_death )
736 if (thread->state == TERMINATED) return; /* already killed */
737 thread->state = TERMINATED;
738 thread->exit_time = current_time;
739 if (current == thread) current = NULL;
741 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
742 thread->id, thread->exit_code );
745 while (thread->wait) end_wait( thread );
746 send_thread_wakeup( thread, NULL, STATUS_PENDING );
747 /* if it is waiting on the socket, we don't need to send a SIGTERM */
750 kill_console_processes( thread, 0 );
751 debug_exit_thread( thread );
752 abandon_mutexes( thread );
753 wake_up( &thread->obj, 0 );
754 if (violent_death) send_thread_signal( thread, SIGTERM );
755 cleanup_thread( thread );
756 remove_process_thread( thread->process, thread );
757 release_object( thread );
760 /* trigger a breakpoint event in a given thread */
761 void break_thread( struct thread *thread )
763 struct debug_event_exception data;
765 assert( thread->context );
767 data.record.ExceptionCode = STATUS_BREAKPOINT;
768 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
769 data.record.ExceptionRecord = NULL;
770 data.record.ExceptionAddress = get_context_ip( thread->context );
771 data.record.NumberParameters = 0;
773 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
774 thread->debug_break = 0;
777 /* take a snapshot of currently running threads */
778 struct thread_snapshot *thread_snap( int *count )
780 struct thread_snapshot *snapshot, *ptr;
781 struct thread *thread;
784 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
785 if (thread->state != TERMINATED) total++;
786 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
788 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
790 if (thread->state == TERMINATED) continue;
791 ptr->thread = thread;
792 ptr->count = thread->obj.refcount;
793 ptr->priority = thread->priority;
794 grab_object( thread );
801 /* gets the current impersonation token */
802 struct token *thread_get_impersonation_token( struct thread *thread )
805 return thread->token;
807 return thread->process->token;
810 /* create a new thread */
811 DECL_HANDLER(new_thread)
813 struct thread *thread;
814 int request_fd = thread_get_inflight_fd( current, req->request_fd );
816 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
818 if (request_fd != -1) close( request_fd );
819 set_error( STATUS_INVALID_HANDLE );
823 if ((thread = create_thread( request_fd, current->process )))
825 if (req->suspend) thread->suspend++;
826 reply->tid = get_thread_id( thread );
827 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
829 /* thread object will be released when the thread gets killed */
832 kill_thread( thread, 1 );
836 /* initialize a new thread */
837 DECL_HANDLER(init_thread)
839 struct process *process = current->process;
840 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
841 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
843 if (current->reply_fd) /* already initialised */
845 set_error( STATUS_INVALID_PARAMETER );
849 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
851 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, ¤t->obj );
853 if (!current->reply_fd) goto error;
857 set_error( STATUS_TOO_MANY_OPENED_FILES ); /* most likely reason */
860 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, ¤t->obj )))
863 if (!is_valid_address(req->teb) || !is_valid_address(req->peb) || !is_valid_address(req->ldt_copy))
865 set_error( STATUS_INVALID_PARAMETER );
869 current->unix_pid = req->unix_pid;
870 current->unix_tid = req->unix_tid;
871 current->teb = req->teb;
873 if (!process->peb) /* first thread, initialize the process too */
875 process->unix_pid = current->unix_pid;
876 process->peb = req->peb;
877 process->ldt_copy = req->ldt_copy;
878 reply->info_size = init_process( current );
882 if (process->unix_pid != current->unix_pid)
883 process->unix_pid = -1; /* can happen with linuxthreads */
884 if (current->suspend + process->suspend > 0) stop_thread( current );
885 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
887 debug_level = max( debug_level, req->debug_level );
889 reply->pid = get_process_id( process );
890 reply->tid = get_thread_id( current );
891 reply->version = SERVER_PROTOCOL_VERSION;
892 reply->server_start.sec = server_start_time.tv_sec;
893 reply->server_start.usec = server_start_time.tv_usec;
897 if (reply_fd != -1) close( reply_fd );
898 if (wait_fd != -1) close( wait_fd );
901 /* terminate a thread */
902 DECL_HANDLER(terminate_thread)
904 struct thread *thread;
908 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
910 thread->exit_code = req->exit_code;
911 if (thread != current) kill_thread( thread, 1 );
915 reply->last = (thread->process->running_threads == 1);
917 release_object( thread );
921 /* open a handle to a thread */
922 DECL_HANDLER(open_thread)
924 struct thread *thread = get_thread_from_id( req->tid );
929 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
930 release_object( thread );
934 /* fetch information about a thread */
935 DECL_HANDLER(get_thread_info)
937 struct thread *thread;
938 obj_handle_t handle = req->handle;
940 if (!handle) thread = get_thread_from_id( req->tid_in );
941 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
945 reply->pid = get_process_id( thread->process );
946 reply->tid = get_thread_id( thread );
947 reply->teb = thread->teb;
948 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
949 reply->priority = thread->priority;
950 reply->affinity = thread->affinity;
951 reply->creation_time.sec = thread->creation_time.tv_sec;
952 reply->creation_time.usec = thread->creation_time.tv_usec;
953 reply->exit_time.sec = thread->exit_time.tv_sec;
954 reply->exit_time.usec = thread->exit_time.tv_usec;
955 reply->last = thread->process->running_threads == 1;
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(obj_handle_t);
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 ((context = set_reply_data_size( sizeof(CONTEXT) )))
1082 unsigned int flags = get_context_system_regs( req->flags );
1084 memset( context, 0, sizeof(CONTEXT) );
1085 context->ContextFlags = get_context_cpu_flag();
1086 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1087 if (flags) get_thread_context( thread, context, flags );
1089 reply->self = (thread == current);
1090 release_object( thread );
1093 /* set the current context of a thread */
1094 DECL_HANDLER(set_thread_context)
1096 struct thread *thread;
1098 if (get_req_data_size() < sizeof(CONTEXT))
1100 set_error( STATUS_INVALID_PARAMETER );
1103 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1107 if (thread != current || thread->context)
1109 /* nested suspend or exception, shouldn't happen */
1110 set_error( STATUS_INVALID_PARAMETER );
1112 else if ((thread->suspend_context = mem_alloc( sizeof(CONTEXT) )))
1114 memcpy( thread->suspend_context, get_req_data(), sizeof(CONTEXT) );
1115 thread->context = thread->suspend_context;
1116 if (thread->debug_break) break_thread( thread );
1119 else if (thread != current && !thread->context)
1121 /* thread is not suspended, retry (if it's still running) */
1122 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1123 else set_error( STATUS_PENDING );
1127 const CONTEXT *context = get_req_data();
1128 unsigned int flags = get_context_system_regs( req->flags );
1130 if (flags) set_thread_context( thread, context, flags );
1131 if (thread->context && !get_error())
1132 copy_context( thread->context, context, req->flags & ~flags );
1134 reply->self = (thread == current);
1135 release_object( thread );
1138 /* fetch a selector entry for a thread */
1139 DECL_HANDLER(get_selector_entry)
1141 struct thread *thread;
1142 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1144 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1145 release_object( thread );