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 tid */
301 struct thread *get_thread_from_tid( int tid )
303 struct thread *thread;
305 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
307 if (thread->unix_tid == tid) return thread;
312 /* find a thread from a Unix pid */
313 struct thread *get_thread_from_pid( int pid )
315 struct thread *thread;
317 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
319 if (thread->unix_pid == pid) return thread;
324 /* set all information about a thread */
325 static void set_thread_info( struct thread *thread,
326 const struct set_thread_info_request *req )
328 if (req->mask & SET_THREAD_INFO_PRIORITY)
329 thread->priority = req->priority;
330 if (req->mask & SET_THREAD_INFO_AFFINITY)
332 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
333 else thread->affinity = req->affinity;
335 if (req->mask & SET_THREAD_INFO_TOKEN)
336 security_set_thread_token( thread, req->token );
339 /* stop a thread (at the Unix level) */
340 void stop_thread( struct thread *thread )
342 if (thread->context) return; /* already inside a debug event, no need for a signal */
343 /* can't stop a thread while initialisation is in progress */
344 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
347 /* suspend a thread */
348 static int suspend_thread( struct thread *thread )
350 int old_count = thread->suspend;
351 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
353 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
355 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
359 /* resume a thread */
360 static int resume_thread( struct thread *thread )
362 int old_count = thread->suspend;
363 if (thread->suspend > 0)
365 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
370 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
371 int add_queue( struct object *obj, struct wait_queue_entry *entry )
375 list_add_tail( &obj->wait_queue, &entry->entry );
379 /* remove a thread from an object wait queue */
380 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
382 list_remove( &entry->entry );
383 release_object( obj );
387 static void end_wait( struct thread *thread )
389 struct thread_wait *wait = thread->wait;
390 struct wait_queue_entry *entry;
394 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
395 entry->obj->ops->remove_queue( entry->obj, entry );
396 if (wait->user) remove_timeout_user( wait->user );
397 thread->wait = wait->next;
401 /* build the thread wait structure */
402 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
404 struct thread_wait *wait;
405 struct wait_queue_entry *entry;
408 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
409 wait->next = current->wait;
410 wait->thread = current;
414 current->wait = wait;
415 if (flags & SELECT_TIMEOUT)
417 wait->timeout.tv_sec = timeout->sec;
418 wait->timeout.tv_usec = timeout->usec;
421 for (i = 0, entry = wait->queues; i < count; i++, entry++)
423 struct object *obj = objects[i];
424 entry->thread = current;
425 if (!obj->ops->add_queue( obj, entry ))
435 /* check if the thread waiting condition is satisfied */
436 static int check_wait( struct thread *thread )
439 struct thread_wait *wait = thread->wait;
440 struct wait_queue_entry *entry = wait->queues;
442 /* Suspended threads may not acquire locks */
443 if (thread->process->suspend + thread->suspend > 0) return -1;
446 if (wait->flags & SELECT_ALL)
449 /* Note: we must check them all anyway, as some objects may
450 * want to do something when signaled, even if others are not */
451 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
452 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
453 if (not_ok) goto other_checks;
454 /* Wait satisfied: tell it to all objects */
456 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
457 if (entry->obj->ops->satisfied( entry->obj, thread ))
458 signaled = STATUS_ABANDONED_WAIT_0;
463 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
465 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
466 /* Wait satisfied: tell it to the object */
468 if (entry->obj->ops->satisfied( entry->obj, thread ))
469 signaled = i + STATUS_ABANDONED_WAIT_0;
475 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
476 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
477 if (wait->flags & SELECT_TIMEOUT)
479 if (!time_before( ¤t_time, &wait->timeout )) return STATUS_TIMEOUT;
484 /* send the wakeup signal to a thread */
485 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
487 struct wake_up_reply reply;
490 reply.cookie = cookie;
491 reply.signaled = signaled;
492 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
495 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
496 else if (errno == EPIPE)
497 kill_thread( thread, 0 ); /* normal death */
499 fatal_protocol_perror( thread, "write" );
503 /* attempt to wake up a thread */
504 /* return >0 if OK, 0 if the wait condition is still not satisfied */
505 int wake_thread( struct thread *thread )
510 for (count = 0; thread->wait; count++)
512 if ((signaled = check_wait( thread )) == -1) break;
514 cookie = thread->wait->cookie;
515 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
516 thread->id, signaled, cookie );
518 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
524 /* thread wait timeout */
525 static void thread_timeout( void *ptr )
527 struct thread_wait *wait = ptr;
528 struct thread *thread = wait->thread;
529 void *cookie = wait->cookie;
532 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
533 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
535 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
536 thread->id, (int)STATUS_TIMEOUT, cookie );
538 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
539 /* check if other objects have become signaled in the meantime */
540 wake_thread( thread );
543 /* try signaling an event flag, a semaphore or a mutex */
544 static int signal_object( obj_handle_t handle )
549 obj = get_handle_obj( current->process, handle, 0, NULL );
552 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
553 release_object( obj );
558 /* select on a list of handles */
559 static void select_on( int count, void *cookie, const obj_handle_t *handles,
560 int flags, const abs_time_t *timeout, obj_handle_t signal_obj )
563 struct object *objects[MAXIMUM_WAIT_OBJECTS];
565 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
567 set_error( STATUS_INVALID_PARAMETER );
570 for (i = 0; i < count; i++)
572 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
576 if (i < count) goto done;
577 if (!wait_on( count, objects, flags, timeout )) goto done;
579 /* signal the object */
582 if (!signal_object( signal_obj ))
587 /* check if we woke ourselves up */
588 if (!current->wait) goto done;
591 if ((ret = check_wait( current )) != -1)
593 /* condition is already satisfied */
599 /* now we need to wait */
600 if (flags & SELECT_TIMEOUT)
602 if (!(current->wait->user = add_timeout_user( ¤t->wait->timeout,
603 thread_timeout, current->wait )))
609 current->wait->cookie = cookie;
610 set_error( STATUS_PENDING );
613 while (--i >= 0) release_object( objects[i] );
616 /* attempt to wake threads sleeping on the object wait queue */
617 void wake_up( struct object *obj, int max )
619 struct list *ptr, *next;
621 LIST_FOR_EACH_SAFE( ptr, next, &obj->wait_queue )
623 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
624 if (wake_thread( entry->thread ))
626 if (max && !--max) break;
631 /* queue an async procedure call */
632 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
633 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
635 struct thread_apc *apc;
636 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
638 /* cancel a possible previous APC with the same owner */
639 if (owner) thread_cancel_apc( thread, owner, system );
640 if (thread->state == TERMINATED) return 0;
642 if (!(apc = mem_alloc( sizeof(*apc) ))) return 0;
649 list_add_tail( queue, &apc->entry );
650 if (!list_prev( queue, &apc->entry )) /* first one */
651 wake_thread( thread );
656 /* cancel the async procedure call owned by a specific object */
657 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
659 struct thread_apc *apc;
660 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
661 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
663 if (apc->owner != owner) continue;
664 list_remove( &apc->entry );
670 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
671 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
673 struct thread_apc *apc = NULL;
674 struct list *ptr = list_head( &thread->system_apc );
676 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
679 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
685 /* add an fd to the inflight list */
686 /* return list index, or -1 on error */
687 int thread_add_inflight_fd( struct thread *thread, int client, int server )
691 if (server == -1) return -1;
698 /* first check if we already have an entry for this fd */
699 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
700 if (thread->inflight[i].client == client)
702 close( thread->inflight[i].server );
703 thread->inflight[i].server = server;
707 /* now find a free spot to store it */
708 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
709 if (thread->inflight[i].client == -1)
711 thread->inflight[i].client = client;
712 thread->inflight[i].server = server;
718 /* get an inflight fd and purge it from the list */
719 /* the fd must be closed when no longer used */
720 int thread_get_inflight_fd( struct thread *thread, int client )
724 if (client == -1) return -1;
728 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
730 if (thread->inflight[i].client == client)
732 ret = thread->inflight[i].server;
733 thread->inflight[i].server = thread->inflight[i].client = -1;
737 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
741 /* kill a thread on the spot */
742 void kill_thread( struct thread *thread, int violent_death )
744 if (thread->state == TERMINATED) return; /* already killed */
745 thread->state = TERMINATED;
746 thread->exit_time = current_time;
747 if (current == thread) current = NULL;
749 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
750 thread->id, thread->exit_code );
753 while (thread->wait) end_wait( thread );
754 send_thread_wakeup( thread, NULL, STATUS_PENDING );
755 /* if it is waiting on the socket, we don't need to send a SIGTERM */
758 kill_console_processes( thread, 0 );
759 debug_exit_thread( thread );
760 abandon_mutexes( thread );
761 wake_up( &thread->obj, 0 );
762 if (violent_death) send_thread_signal( thread, SIGTERM );
763 cleanup_thread( thread );
764 remove_process_thread( thread->process, thread );
765 release_object( thread );
768 /* trigger a breakpoint event in a given thread */
769 void break_thread( struct thread *thread )
771 struct debug_event_exception data;
773 assert( thread->context );
775 data.record.ExceptionCode = STATUS_BREAKPOINT;
776 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
777 data.record.ExceptionRecord = NULL;
778 data.record.ExceptionAddress = get_context_ip( thread->context );
779 data.record.NumberParameters = 0;
781 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
782 thread->debug_break = 0;
785 /* take a snapshot of currently running threads */
786 struct thread_snapshot *thread_snap( int *count )
788 struct thread_snapshot *snapshot, *ptr;
789 struct thread *thread;
792 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
793 if (thread->state != TERMINATED) total++;
794 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
796 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
798 if (thread->state == TERMINATED) continue;
799 ptr->thread = thread;
800 ptr->count = thread->obj.refcount;
801 ptr->priority = thread->priority;
802 grab_object( thread );
809 /* gets the current impersonation token */
810 struct token *thread_get_impersonation_token( struct thread *thread )
813 return thread->token;
815 return thread->process->token;
818 /* create a new thread */
819 DECL_HANDLER(new_thread)
821 struct thread *thread;
822 int request_fd = thread_get_inflight_fd( current, req->request_fd );
824 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
826 if (request_fd != -1) close( request_fd );
827 set_error( STATUS_INVALID_HANDLE );
831 if ((thread = create_thread( request_fd, current->process )))
833 if (req->suspend) thread->suspend++;
834 reply->tid = get_thread_id( thread );
835 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
837 /* thread object will be released when the thread gets killed */
840 kill_thread( thread, 1 );
844 /* initialize a new thread */
845 DECL_HANDLER(init_thread)
847 struct process *process = current->process;
848 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
849 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
851 if (current->reply_fd) /* already initialised */
853 set_error( STATUS_INVALID_PARAMETER );
857 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
859 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, ¤t->obj );
861 if (!current->reply_fd) goto error;
865 set_error( STATUS_TOO_MANY_OPENED_FILES ); /* most likely reason */
868 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, ¤t->obj )))
871 if (!is_valid_address(req->teb) || !is_valid_address(req->peb) || !is_valid_address(req->ldt_copy))
873 set_error( STATUS_INVALID_PARAMETER );
877 current->unix_pid = req->unix_pid;
878 current->unix_tid = req->unix_tid;
879 current->teb = req->teb;
881 if (!process->peb) /* first thread, initialize the process too */
883 process->unix_pid = current->unix_pid;
884 process->peb = req->peb;
885 process->ldt_copy = req->ldt_copy;
886 reply->info_size = init_process( current );
890 if (process->unix_pid != current->unix_pid)
891 process->unix_pid = -1; /* can happen with linuxthreads */
892 if (current->suspend + process->suspend > 0) stop_thread( current );
893 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
895 debug_level = max( debug_level, req->debug_level );
897 reply->pid = get_process_id( process );
898 reply->tid = get_thread_id( current );
899 reply->version = SERVER_PROTOCOL_VERSION;
900 reply->server_start.sec = server_start_time.tv_sec;
901 reply->server_start.usec = server_start_time.tv_usec;
905 if (reply_fd != -1) close( reply_fd );
906 if (wait_fd != -1) close( wait_fd );
909 /* terminate a thread */
910 DECL_HANDLER(terminate_thread)
912 struct thread *thread;
916 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
918 thread->exit_code = req->exit_code;
919 if (thread != current) kill_thread( thread, 1 );
923 reply->last = (thread->process->running_threads == 1);
925 release_object( thread );
929 /* open a handle to a thread */
930 DECL_HANDLER(open_thread)
932 struct thread *thread = get_thread_from_id( req->tid );
937 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
938 release_object( thread );
942 /* fetch information about a thread */
943 DECL_HANDLER(get_thread_info)
945 struct thread *thread;
946 obj_handle_t handle = req->handle;
948 if (!handle) thread = get_thread_from_id( req->tid_in );
949 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
953 reply->pid = get_process_id( thread->process );
954 reply->tid = get_thread_id( thread );
955 reply->teb = thread->teb;
956 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
957 reply->priority = thread->priority;
958 reply->affinity = thread->affinity;
959 reply->creation_time.sec = thread->creation_time.tv_sec;
960 reply->creation_time.usec = thread->creation_time.tv_usec;
961 reply->exit_time.sec = thread->exit_time.tv_sec;
962 reply->exit_time.usec = thread->exit_time.tv_usec;
963 reply->last = thread->process->running_threads == 1;
965 release_object( thread );
969 /* set information about a thread */
970 DECL_HANDLER(set_thread_info)
972 struct thread *thread;
974 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
976 set_thread_info( thread, req );
977 release_object( thread );
981 /* suspend a thread */
982 DECL_HANDLER(suspend_thread)
984 struct thread *thread;
986 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
988 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
989 else reply->count = suspend_thread( thread );
990 release_object( thread );
994 /* resume a thread */
995 DECL_HANDLER(resume_thread)
997 struct thread *thread;
999 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1001 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1002 else reply->count = resume_thread( thread );
1003 release_object( thread );
1007 /* select on a handle list */
1008 DECL_HANDLER(select)
1010 int count = get_req_data_size() / sizeof(obj_handle_t);
1011 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout, req->signal );
1014 /* queue an APC for a thread */
1015 DECL_HANDLER(queue_apc)
1017 struct thread *thread;
1018 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
1020 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
1021 req->arg1, req->arg2, req->arg3 );
1022 release_object( thread );
1026 /* get next APC to call */
1027 DECL_HANDLER(get_apc)
1029 struct thread_apc *apc;
1033 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
1037 reply->type = APC_NONE;
1040 /* Optimization: ignore APCs that have a NULL func; they are only used
1041 * to wake up a thread, but since we got here the thread woke up already.
1042 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1044 if (apc->func || apc->type == APC_ASYNC_IO) break;
1047 reply->func = apc->func;
1048 reply->type = apc->type;
1049 reply->arg1 = apc->arg1;
1050 reply->arg2 = apc->arg2;
1051 reply->arg3 = apc->arg3;
1055 /* retrieve the current context of a thread */
1056 DECL_HANDLER(get_thread_context)
1058 struct thread *thread;
1061 if (get_reply_max_size() < sizeof(CONTEXT))
1063 set_error( STATUS_INVALID_PARAMETER );
1066 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1070 if (thread != current || !thread->suspend_context)
1072 /* not suspended, shouldn't happen */
1073 set_error( STATUS_INVALID_PARAMETER );
1077 if (thread->context == thread->suspend_context) thread->context = NULL;
1078 set_reply_data_ptr( thread->suspend_context, sizeof(CONTEXT) );
1079 thread->suspend_context = NULL;
1082 else if (thread != current && !thread->context)
1084 /* thread is not suspended, retry (if it's still running) */
1085 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1086 else set_error( STATUS_PENDING );
1088 else if ((context = set_reply_data_size( sizeof(CONTEXT) )))
1090 unsigned int flags = get_context_system_regs( req->flags );
1092 memset( context, 0, sizeof(CONTEXT) );
1093 context->ContextFlags = get_context_cpu_flag();
1094 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1095 if (flags) get_thread_context( thread, context, flags );
1097 reply->self = (thread == current);
1098 release_object( thread );
1101 /* set the current context of a thread */
1102 DECL_HANDLER(set_thread_context)
1104 struct thread *thread;
1106 if (get_req_data_size() < sizeof(CONTEXT))
1108 set_error( STATUS_INVALID_PARAMETER );
1111 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1115 if (thread != current || thread->context)
1117 /* nested suspend or exception, shouldn't happen */
1118 set_error( STATUS_INVALID_PARAMETER );
1120 else if ((thread->suspend_context = mem_alloc( sizeof(CONTEXT) )))
1122 memcpy( thread->suspend_context, get_req_data(), sizeof(CONTEXT) );
1123 thread->context = thread->suspend_context;
1124 if (thread->debug_break) break_thread( thread );
1127 else if (thread != current && !thread->context)
1129 /* thread is not suspended, retry (if it's still running) */
1130 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1131 else set_error( STATUS_PENDING );
1135 const CONTEXT *context = get_req_data();
1136 unsigned int flags = get_context_system_regs( req->flags );
1138 if (flags) set_thread_context( thread, context, flags );
1139 if (thread->context && !get_error())
1140 copy_context( thread->context, context, req->flags & ~flags );
1142 reply->self = (thread == current);
1143 release_object( thread );
1146 /* fetch a selector entry for a thread */
1147 DECL_HANDLER(get_selector_entry)
1149 struct thread *thread;
1150 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1152 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1153 release_object( thread );