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 object obj; /* object header */
72 struct list entry; /* queue linked list */
73 struct thread *caller; /* thread that queued this apc */
74 struct object *owner; /* object that queued this apc */
75 int executed; /* has it been executed by the client? */
76 apc_call_t call; /* call arguments */
77 apc_result_t result; /* call results once executed */
80 static void dump_thread_apc( struct object *obj, int verbose );
81 static int thread_apc_signaled( struct object *obj, struct thread *thread );
82 static void thread_apc_destroy( struct object *obj );
83 static void clear_apc_queue( struct list *queue );
85 static const struct object_ops thread_apc_ops =
87 sizeof(struct thread_apc), /* size */
88 dump_thread_apc, /* dump */
89 add_queue, /* add_queue */
90 remove_queue, /* remove_queue */
91 thread_apc_signaled, /* signaled */
92 no_satisfied, /* satisfied */
93 no_signal, /* signal */
94 no_get_fd, /* get_fd */
95 no_map_access, /* map_access */
96 no_lookup_name, /* lookup_name */
97 no_close_handle, /* close_handle */
98 thread_apc_destroy /* destroy */
102 /* thread operations */
104 static void dump_thread( struct object *obj, int verbose );
105 static int thread_signaled( struct object *obj, struct thread *thread );
106 static unsigned int thread_map_access( struct object *obj, unsigned int access );
107 static void thread_poll_event( struct fd *fd, int event );
108 static void destroy_thread( struct object *obj );
110 static const struct object_ops thread_ops =
112 sizeof(struct thread), /* size */
113 dump_thread, /* dump */
114 add_queue, /* add_queue */
115 remove_queue, /* remove_queue */
116 thread_signaled, /* signaled */
117 no_satisfied, /* satisfied */
118 no_signal, /* signal */
119 no_get_fd, /* get_fd */
120 thread_map_access, /* map_access */
121 no_lookup_name, /* lookup_name */
122 no_close_handle, /* close_handle */
123 destroy_thread /* destroy */
126 static const struct fd_ops thread_fd_ops =
128 NULL, /* get_poll_events */
129 thread_poll_event, /* poll_event */
130 no_flush, /* flush */
131 no_get_file_info, /* get_file_info */
132 no_queue_async, /* queue_async */
133 no_cancel_async /* cancel_async */
136 static struct list thread_list = LIST_INIT(thread_list);
138 /* initialize the structure for a newly allocated thread */
139 inline static void init_thread_structure( struct thread *thread )
143 thread->unix_pid = -1; /* not known yet */
144 thread->unix_tid = -1; /* not known yet */
145 thread->context = NULL;
146 thread->suspend_context = NULL;
148 thread->debug_ctx = NULL;
149 thread->debug_event = NULL;
150 thread->debug_break = 0;
151 thread->queue = NULL;
154 thread->req_data = NULL;
155 thread->req_toread = 0;
156 thread->reply_data = NULL;
157 thread->reply_towrite = 0;
158 thread->request_fd = NULL;
159 thread->reply_fd = NULL;
160 thread->wait_fd = NULL;
161 thread->state = RUNNING;
162 thread->exit_code = 0;
163 thread->priority = 0;
164 thread->affinity = 1;
166 thread->desktop_users = 0;
167 thread->token = NULL;
169 thread->creation_time = current_time;
170 thread->exit_time.tv_sec = thread->exit_time.tv_usec = 0;
172 list_init( &thread->mutex_list );
173 list_init( &thread->system_apc );
174 list_init( &thread->user_apc );
176 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
177 thread->inflight[i].server = thread->inflight[i].client = -1;
180 /* check if address looks valid for a client-side data structure (TEB etc.) */
181 static inline int is_valid_address( void *addr )
183 return addr && !((unsigned long)addr % sizeof(int));
186 /* create a new thread */
187 struct thread *create_thread( int fd, struct process *process )
189 struct thread *thread;
191 if (!(thread = alloc_object( &thread_ops ))) return NULL;
193 init_thread_structure( thread );
195 thread->process = (struct process *)grab_object( process );
196 thread->desktop = process->desktop;
197 if (!current) current = thread;
199 list_add_head( &thread_list, &thread->entry );
201 if (!(thread->id = alloc_ptid( thread )))
203 release_object( thread );
206 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
208 release_object( thread );
212 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
213 add_process_thread( thread->process, thread );
217 /* handle a client event */
218 static void thread_poll_event( struct fd *fd, int event )
220 struct thread *thread = get_fd_user( fd );
221 assert( thread->obj.ops == &thread_ops );
223 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
224 else if (event & POLLIN) read_request( thread );
225 else if (event & POLLOUT) write_reply( thread );
228 /* cleanup everything that is no longer needed by a dead thread */
229 /* used by destroy_thread and kill_thread */
230 static void cleanup_thread( struct thread *thread )
234 clear_apc_queue( &thread->system_apc );
235 clear_apc_queue( &thread->user_apc );
236 free( thread->req_data );
237 free( thread->reply_data );
238 if (thread->request_fd) release_object( thread->request_fd );
239 if (thread->reply_fd) release_object( thread->reply_fd );
240 if (thread->wait_fd) release_object( thread->wait_fd );
241 free( thread->suspend_context );
242 free_msg_queue( thread );
243 cleanup_clipboard_thread(thread);
244 destroy_thread_windows( thread );
245 close_thread_desktop( thread );
246 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
248 if (thread->inflight[i].client != -1)
250 close( thread->inflight[i].server );
251 thread->inflight[i].client = thread->inflight[i].server = -1;
254 thread->req_data = NULL;
255 thread->reply_data = NULL;
256 thread->request_fd = NULL;
257 thread->reply_fd = NULL;
258 thread->wait_fd = NULL;
259 thread->context = NULL;
260 thread->suspend_context = NULL;
264 /* destroy a thread when its refcount is 0 */
265 static void destroy_thread( struct object *obj )
267 struct thread *thread = (struct thread *)obj;
268 assert( obj->ops == &thread_ops );
270 assert( !thread->debug_ctx ); /* cannot still be debugging something */
271 list_remove( &thread->entry );
272 cleanup_thread( thread );
273 release_object( thread->process );
274 if (thread->id) free_ptid( thread->id );
275 if (thread->token) release_object( thread->token );
278 /* dump a thread on stdout for debugging purposes */
279 static void dump_thread( struct object *obj, int verbose )
281 struct thread *thread = (struct thread *)obj;
282 assert( obj->ops == &thread_ops );
284 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
285 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
288 static int thread_signaled( struct object *obj, struct thread *thread )
290 struct thread *mythread = (struct thread *)obj;
291 return (mythread->state == TERMINATED);
294 static unsigned int thread_map_access( struct object *obj, unsigned int access )
296 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
297 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
298 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
299 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
300 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
303 static void dump_thread_apc( struct object *obj, int verbose )
305 struct thread_apc *apc = (struct thread_apc *)obj;
306 assert( obj->ops == &thread_apc_ops );
308 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
311 static int thread_apc_signaled( struct object *obj, struct thread *thread )
313 struct thread_apc *apc = (struct thread_apc *)obj;
314 return apc->executed;
317 static void thread_apc_destroy( struct object *obj )
319 struct thread_apc *apc = (struct thread_apc *)obj;
320 if (apc->caller) release_object( apc->caller );
323 /* queue an async procedure call */
324 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
326 struct thread_apc *apc;
328 if ((apc = alloc_object( &thread_apc_ops )))
330 apc->call = *call_data;
334 apc->result.type = APC_NONE;
339 /* get a thread pointer from a thread id (and increment the refcount) */
340 struct thread *get_thread_from_id( thread_id_t id )
342 struct object *obj = get_ptid_entry( id );
344 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
345 set_error( STATUS_INVALID_CID );
349 /* get a thread from a handle (and increment the refcount) */
350 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
352 return (struct thread *)get_handle_obj( current->process, handle,
353 access, &thread_ops );
356 /* find a thread from a Unix tid */
357 struct thread *get_thread_from_tid( int tid )
359 struct thread *thread;
361 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
363 if (thread->unix_tid == tid) return thread;
368 /* find a thread from a Unix pid */
369 struct thread *get_thread_from_pid( int pid )
371 struct thread *thread;
373 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
375 if (thread->unix_pid == pid) return thread;
380 /* set all information about a thread */
381 static void set_thread_info( struct thread *thread,
382 const struct set_thread_info_request *req )
384 if (req->mask & SET_THREAD_INFO_PRIORITY)
385 thread->priority = req->priority;
386 if (req->mask & SET_THREAD_INFO_AFFINITY)
388 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
389 else thread->affinity = req->affinity;
391 if (req->mask & SET_THREAD_INFO_TOKEN)
392 security_set_thread_token( thread, req->token );
395 /* stop a thread (at the Unix level) */
396 void stop_thread( struct thread *thread )
398 if (thread->context) return; /* already inside a debug event, no need for a signal */
399 /* can't stop a thread while initialisation is in progress */
400 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
403 /* suspend a thread */
404 static int suspend_thread( struct thread *thread )
406 int old_count = thread->suspend;
407 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
409 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
411 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
415 /* resume a thread */
416 static int resume_thread( struct thread *thread )
418 int old_count = thread->suspend;
419 if (thread->suspend > 0)
421 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
426 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
427 int add_queue( struct object *obj, struct wait_queue_entry *entry )
431 list_add_tail( &obj->wait_queue, &entry->entry );
435 /* remove a thread from an object wait queue */
436 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
438 list_remove( &entry->entry );
439 release_object( obj );
443 static void end_wait( struct thread *thread )
445 struct thread_wait *wait = thread->wait;
446 struct wait_queue_entry *entry;
450 thread->wait = wait->next;
451 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
452 entry->obj->ops->remove_queue( entry->obj, entry );
453 if (wait->user) remove_timeout_user( wait->user );
457 /* build the thread wait structure */
458 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
460 struct thread_wait *wait;
461 struct wait_queue_entry *entry;
464 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
465 wait->next = current->wait;
466 wait->thread = current;
470 current->wait = wait;
471 if (flags & SELECT_TIMEOUT)
473 wait->timeout.tv_sec = timeout->sec;
474 wait->timeout.tv_usec = timeout->usec;
477 for (i = 0, entry = wait->queues; i < count; i++, entry++)
479 struct object *obj = objects[i];
480 entry->thread = current;
481 if (!obj->ops->add_queue( obj, entry ))
491 /* check if the thread waiting condition is satisfied */
492 static int check_wait( struct thread *thread )
495 struct thread_wait *wait = thread->wait;
496 struct wait_queue_entry *entry = wait->queues;
498 /* Suspended threads may not acquire locks, but they can run system APCs */
499 if (thread->process->suspend + thread->suspend > 0)
501 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
502 return STATUS_USER_APC;
507 if (wait->flags & SELECT_ALL)
510 /* Note: we must check them all anyway, as some objects may
511 * want to do something when signaled, even if others are not */
512 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
513 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
514 if (not_ok) goto other_checks;
515 /* Wait satisfied: tell it to all objects */
517 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
518 if (entry->obj->ops->satisfied( entry->obj, thread ))
519 signaled = STATUS_ABANDONED_WAIT_0;
524 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
526 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
527 /* Wait satisfied: tell it to the object */
529 if (entry->obj->ops->satisfied( entry->obj, thread ))
530 signaled = i + STATUS_ABANDONED_WAIT_0;
536 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
537 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
538 if (wait->flags & SELECT_TIMEOUT)
540 if (!time_before( ¤t_time, &wait->timeout )) return STATUS_TIMEOUT;
545 /* send the wakeup signal to a thread */
546 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
548 struct wake_up_reply reply;
551 reply.cookie = cookie;
552 reply.signaled = signaled;
553 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
556 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
557 else if (errno == EPIPE)
558 kill_thread( thread, 0 ); /* normal death */
560 fatal_protocol_perror( thread, "write" );
564 /* attempt to wake up a thread */
565 /* return >0 if OK, 0 if the wait condition is still not satisfied */
566 int wake_thread( struct thread *thread )
571 for (count = 0; thread->wait; count++)
573 if ((signaled = check_wait( thread )) == -1) break;
575 cookie = thread->wait->cookie;
576 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
577 thread->id, signaled, cookie );
579 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
585 /* thread wait timeout */
586 static void thread_timeout( void *ptr )
588 struct thread_wait *wait = ptr;
589 struct thread *thread = wait->thread;
590 void *cookie = wait->cookie;
593 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
594 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
596 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
597 thread->id, (int)STATUS_TIMEOUT, cookie );
599 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
600 /* check if other objects have become signaled in the meantime */
601 wake_thread( thread );
604 /* try signaling an event flag, a semaphore or a mutex */
605 static int signal_object( obj_handle_t handle )
610 obj = get_handle_obj( current->process, handle, 0, NULL );
613 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
614 release_object( obj );
619 /* select on a list of handles */
620 static void select_on( int count, void *cookie, const obj_handle_t *handles,
621 int flags, const abs_time_t *timeout, obj_handle_t signal_obj )
624 struct object *objects[MAXIMUM_WAIT_OBJECTS];
626 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
628 set_error( STATUS_INVALID_PARAMETER );
631 for (i = 0; i < count; i++)
633 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
637 if (i < count) goto done;
638 if (!wait_on( count, objects, flags, timeout )) goto done;
640 /* signal the object */
643 if (!signal_object( signal_obj ))
648 /* check if we woke ourselves up */
649 if (!current->wait) goto done;
652 if ((ret = check_wait( current )) != -1)
654 /* condition is already satisfied */
660 /* now we need to wait */
661 if (flags & SELECT_TIMEOUT)
663 if (!(current->wait->user = add_timeout_user( ¤t->wait->timeout,
664 thread_timeout, current->wait )))
670 current->wait->cookie = cookie;
671 set_error( STATUS_PENDING );
674 while (--i >= 0) release_object( objects[i] );
677 /* attempt to wake threads sleeping on the object wait queue */
678 void wake_up( struct object *obj, int max )
680 struct list *ptr, *next;
682 LIST_FOR_EACH_SAFE( ptr, next, &obj->wait_queue )
684 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
685 if (wake_thread( entry->thread ))
687 if (max && !--max) break;
692 /* return the apc queue to use for a given apc type */
693 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
700 return &thread->user_apc;
702 return &thread->system_apc;
706 /* queue an existing APC to a given thread */
707 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
711 if (!thread) /* find a suitable thread inside the process */
713 struct thread *candidate;
715 /* first try to find a waiting thread */
716 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
718 if (candidate->state == TERMINATED) continue;
719 if (process->suspend || candidate->suspend ||
720 (candidate->wait && (candidate->wait->flags & SELECT_INTERRUPTIBLE)))
728 /* then use the first one that accepts a signal */
729 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
731 if (send_thread_signal( candidate, SIGUSR1 ))
738 if (!thread) return 0; /* nothing found */
742 if (thread->state == TERMINATED) return 0;
743 /* cancel a possible previous APC with the same owner */
744 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
747 queue = get_apc_queue( thread, apc->call.type );
749 list_add_tail( queue, &apc->entry );
750 if (!list_prev( queue, &apc->entry )) /* first one */
751 wake_thread( thread );
756 /* queue an async procedure call */
757 int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data )
759 struct thread_apc *apc;
762 if ((apc = create_apc( owner, call_data )))
764 ret = queue_apc( NULL, thread, apc );
765 release_object( apc );
770 /* cancel the async procedure call owned by a specific object */
771 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
773 struct thread_apc *apc;
774 struct list *queue = get_apc_queue( thread, type );
776 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
778 if (apc->owner != owner) continue;
779 list_remove( &apc->entry );
781 wake_up( &apc->obj, 0 );
782 release_object( apc );
787 /* remove the head apc from the queue; the returned object must be released by the caller */
788 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
790 struct thread_apc *apc = NULL;
791 struct list *ptr = list_head( &thread->system_apc );
793 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
796 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
802 /* clear an APC queue, cancelling all the APCs on it */
803 static void clear_apc_queue( struct list *queue )
807 while ((ptr = list_head( queue )))
809 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
810 list_remove( &apc->entry );
812 wake_up( &apc->obj, 0 );
813 release_object( apc );
817 /* add an fd to the inflight list */
818 /* return list index, or -1 on error */
819 int thread_add_inflight_fd( struct thread *thread, int client, int server )
823 if (server == -1) return -1;
830 /* first check if we already have an entry for this fd */
831 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
832 if (thread->inflight[i].client == client)
834 close( thread->inflight[i].server );
835 thread->inflight[i].server = server;
839 /* now find a free spot to store it */
840 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
841 if (thread->inflight[i].client == -1)
843 thread->inflight[i].client = client;
844 thread->inflight[i].server = server;
850 /* get an inflight fd and purge it from the list */
851 /* the fd must be closed when no longer used */
852 int thread_get_inflight_fd( struct thread *thread, int client )
856 if (client == -1) return -1;
860 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
862 if (thread->inflight[i].client == client)
864 ret = thread->inflight[i].server;
865 thread->inflight[i].server = thread->inflight[i].client = -1;
869 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
873 /* kill a thread on the spot */
874 void kill_thread( struct thread *thread, int violent_death )
876 if (thread->state == TERMINATED) return; /* already killed */
877 thread->state = TERMINATED;
878 thread->exit_time = current_time;
879 if (current == thread) current = NULL;
881 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
882 thread->id, thread->exit_code );
885 while (thread->wait) end_wait( thread );
886 send_thread_wakeup( thread, NULL, STATUS_PENDING );
887 /* if it is waiting on the socket, we don't need to send a SIGTERM */
890 kill_console_processes( thread, 0 );
891 debug_exit_thread( thread );
892 abandon_mutexes( thread );
893 wake_up( &thread->obj, 0 );
894 if (violent_death) send_thread_signal( thread, SIGTERM );
895 cleanup_thread( thread );
896 remove_process_thread( thread->process, thread );
897 release_object( thread );
900 /* trigger a breakpoint event in a given thread */
901 void break_thread( struct thread *thread )
903 struct debug_event_exception data;
905 assert( thread->context );
907 data.record.ExceptionCode = STATUS_BREAKPOINT;
908 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
909 data.record.ExceptionRecord = NULL;
910 data.record.ExceptionAddress = get_context_ip( thread->context );
911 data.record.NumberParameters = 0;
913 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
914 thread->debug_break = 0;
917 /* take a snapshot of currently running threads */
918 struct thread_snapshot *thread_snap( int *count )
920 struct thread_snapshot *snapshot, *ptr;
921 struct thread *thread;
924 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
925 if (thread->state != TERMINATED) total++;
926 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
928 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
930 if (thread->state == TERMINATED) continue;
931 ptr->thread = thread;
932 ptr->count = thread->obj.refcount;
933 ptr->priority = thread->priority;
934 grab_object( thread );
941 /* gets the current impersonation token */
942 struct token *thread_get_impersonation_token( struct thread *thread )
945 return thread->token;
947 return thread->process->token;
950 /* create a new thread */
951 DECL_HANDLER(new_thread)
953 struct thread *thread;
954 int request_fd = thread_get_inflight_fd( current, req->request_fd );
956 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
958 if (request_fd != -1) close( request_fd );
959 set_error( STATUS_INVALID_HANDLE );
963 if ((thread = create_thread( request_fd, current->process )))
965 if (req->suspend) thread->suspend++;
966 reply->tid = get_thread_id( thread );
967 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
969 /* thread object will be released when the thread gets killed */
972 kill_thread( thread, 1 );
976 /* initialize a new thread */
977 DECL_HANDLER(init_thread)
979 struct process *process = current->process;
980 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
981 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
983 if (current->reply_fd) /* already initialised */
985 set_error( STATUS_INVALID_PARAMETER );
989 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
991 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, ¤t->obj );
993 if (!current->reply_fd) goto error;
997 set_error( STATUS_TOO_MANY_OPENED_FILES ); /* most likely reason */
1000 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, ¤t->obj )))
1003 if (!is_valid_address(req->teb) || !is_valid_address(req->peb) || !is_valid_address(req->ldt_copy))
1005 set_error( STATUS_INVALID_PARAMETER );
1009 current->unix_pid = req->unix_pid;
1010 current->unix_tid = req->unix_tid;
1011 current->teb = req->teb;
1013 if (!process->peb) /* first thread, initialize the process too */
1015 process->unix_pid = current->unix_pid;
1016 process->peb = req->peb;
1017 process->ldt_copy = req->ldt_copy;
1018 reply->info_size = init_process( current );
1022 if (process->unix_pid != current->unix_pid)
1023 process->unix_pid = -1; /* can happen with linuxthreads */
1024 if (current->suspend + process->suspend > 0) stop_thread( current );
1025 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
1027 debug_level = max( debug_level, req->debug_level );
1029 reply->pid = get_process_id( process );
1030 reply->tid = get_thread_id( current );
1031 reply->version = SERVER_PROTOCOL_VERSION;
1032 reply->server_start.sec = server_start_time.tv_sec;
1033 reply->server_start.usec = server_start_time.tv_usec;
1037 if (reply_fd != -1) close( reply_fd );
1038 if (wait_fd != -1) close( wait_fd );
1041 /* terminate a thread */
1042 DECL_HANDLER(terminate_thread)
1044 struct thread *thread;
1048 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1050 thread->exit_code = req->exit_code;
1051 if (thread != current) kill_thread( thread, 1 );
1055 reply->last = (thread->process->running_threads == 1);
1057 release_object( thread );
1061 /* open a handle to a thread */
1062 DECL_HANDLER(open_thread)
1064 struct thread *thread = get_thread_from_id( req->tid );
1069 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1070 release_object( thread );
1074 /* fetch information about a thread */
1075 DECL_HANDLER(get_thread_info)
1077 struct thread *thread;
1078 obj_handle_t handle = req->handle;
1080 if (!handle) thread = get_thread_from_id( req->tid_in );
1081 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
1085 reply->pid = get_process_id( thread->process );
1086 reply->tid = get_thread_id( thread );
1087 reply->teb = thread->teb;
1088 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1089 reply->priority = thread->priority;
1090 reply->affinity = thread->affinity;
1091 reply->creation_time.sec = thread->creation_time.tv_sec;
1092 reply->creation_time.usec = thread->creation_time.tv_usec;
1093 reply->exit_time.sec = thread->exit_time.tv_sec;
1094 reply->exit_time.usec = thread->exit_time.tv_usec;
1095 reply->last = thread->process->running_threads == 1;
1097 release_object( thread );
1101 /* set information about a thread */
1102 DECL_HANDLER(set_thread_info)
1104 struct thread *thread;
1106 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1108 set_thread_info( thread, req );
1109 release_object( thread );
1113 /* suspend a thread */
1114 DECL_HANDLER(suspend_thread)
1116 struct thread *thread;
1118 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1120 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1121 else reply->count = suspend_thread( thread );
1122 release_object( thread );
1126 /* resume a thread */
1127 DECL_HANDLER(resume_thread)
1129 struct thread *thread;
1131 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1133 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1134 else reply->count = resume_thread( thread );
1135 release_object( thread );
1139 /* select on a handle list */
1140 DECL_HANDLER(select)
1142 int count = get_req_data_size() / sizeof(obj_handle_t);
1143 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout, req->signal );
1146 /* queue an APC for a thread or process */
1147 DECL_HANDLER(queue_apc)
1149 struct thread *thread = NULL;
1150 struct process *process = NULL;
1151 struct thread_apc *apc;
1153 if (!(apc = create_apc( NULL, &req->call ))) return;
1155 switch (apc->call.type)
1159 thread = get_thread_from_handle( req->thread, THREAD_SET_CONTEXT );
1161 case APC_VIRTUAL_ALLOC:
1162 case APC_VIRTUAL_FREE:
1163 case APC_VIRTUAL_PROTECT:
1164 case APC_VIRTUAL_FLUSH:
1165 case APC_VIRTUAL_LOCK:
1166 case APC_VIRTUAL_UNLOCK:
1167 case APC_UNMAP_VIEW:
1168 process = get_process_from_handle( req->process, PROCESS_VM_OPERATION );
1170 case APC_VIRTUAL_QUERY:
1171 process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION );
1174 process = get_process_from_handle( req->process, PROCESS_VM_OPERATION );
1175 if (process && process != current->process)
1177 /* duplicate the handle into the target process */
1178 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1179 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1180 if (handle) apc->call.map_view.handle = handle;
1183 release_object( process );
1188 case APC_CREATE_THREAD:
1189 process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD );
1192 set_error( STATUS_INVALID_PARAMETER );
1198 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1199 release_object( thread );
1203 reply->self = (process == current->process);
1206 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1209 if (queue_apc( process, NULL, apc ))
1211 apc->caller = (struct thread *)grab_object( current );
1212 reply->handle = handle;
1216 close_handle( current->process, handle );
1217 set_error( STATUS_PROCESS_IS_TERMINATING );
1221 release_object( process );
1224 release_object( apc );
1227 /* get next APC to call */
1228 DECL_HANDLER(get_apc)
1230 struct thread_apc *apc;
1231 int system_only = !req->alertable;
1235 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev,
1236 0, &thread_apc_ops ))) return;
1237 apc->result = req->result;
1239 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1241 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1242 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1243 close_handle( current->process, apc->result.create_thread.handle );
1244 apc->result.create_thread.handle = handle;
1245 clear_error(); /* ignore errors from the above calls */
1247 wake_up( &apc->obj, 0 );
1248 close_handle( current->process, req->prev );
1249 release_object( apc );
1252 if (current->suspend + current->process->suspend > 0) system_only = 1;
1256 if (!(apc = thread_dequeue_apc( current, system_only )))
1259 set_error( STATUS_PENDING );
1262 /* Optimization: ignore APC_NONE calls, they are only used to
1263 * wake up a thread, but since we got here the thread woke up already.
1265 if (apc->call.type != APC_NONE) break;
1267 wake_up( &apc->obj, 0 );
1268 release_object( apc );
1271 if ((reply->handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1272 reply->call = apc->call;
1273 release_object( apc );
1276 /* Get the result of an APC call */
1277 DECL_HANDLER(get_apc_result)
1279 struct thread_apc *apc;
1281 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1282 0, &thread_apc_ops ))) return;
1283 if (!apc->executed) set_error( STATUS_PENDING );
1286 reply->result = apc->result;
1287 /* close the handle directly to avoid an extra round-trip */
1288 close_handle( current->process, req->handle );
1290 release_object( apc );
1293 /* retrieve the current context of a thread */
1294 DECL_HANDLER(get_thread_context)
1296 struct thread *thread;
1299 if (get_reply_max_size() < sizeof(CONTEXT))
1301 set_error( STATUS_INVALID_PARAMETER );
1304 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1308 if (thread != current || !thread->suspend_context)
1310 /* not suspended, shouldn't happen */
1311 set_error( STATUS_INVALID_PARAMETER );
1315 if (thread->context == thread->suspend_context) thread->context = NULL;
1316 set_reply_data_ptr( thread->suspend_context, sizeof(CONTEXT) );
1317 thread->suspend_context = NULL;
1320 else if (thread != current && !thread->context)
1322 /* thread is not suspended, retry (if it's still running) */
1323 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1324 else set_error( STATUS_PENDING );
1326 else if ((context = set_reply_data_size( sizeof(CONTEXT) )))
1328 unsigned int flags = get_context_system_regs( req->flags );
1330 memset( context, 0, sizeof(CONTEXT) );
1331 context->ContextFlags = get_context_cpu_flag();
1332 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1333 if (flags) get_thread_context( thread, context, flags );
1335 reply->self = (thread == current);
1336 release_object( thread );
1339 /* set the current context of a thread */
1340 DECL_HANDLER(set_thread_context)
1342 struct thread *thread;
1344 if (get_req_data_size() < sizeof(CONTEXT))
1346 set_error( STATUS_INVALID_PARAMETER );
1349 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1353 if (thread != current || thread->context)
1355 /* nested suspend or exception, shouldn't happen */
1356 set_error( STATUS_INVALID_PARAMETER );
1358 else if ((thread->suspend_context = mem_alloc( sizeof(CONTEXT) )))
1360 memcpy( thread->suspend_context, get_req_data(), sizeof(CONTEXT) );
1361 thread->context = thread->suspend_context;
1362 if (thread->debug_break) break_thread( thread );
1365 else if (thread != current && !thread->context)
1367 /* thread is not suspended, retry (if it's still running) */
1368 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1369 else set_error( STATUS_PENDING );
1373 const CONTEXT *context = get_req_data();
1374 unsigned int flags = get_context_system_regs( req->flags );
1376 if (flags) set_thread_context( thread, context, flags );
1377 if (thread->context && !get_error())
1378 copy_context( thread->context, context, req->flags & ~flags );
1380 reply->self = (thread == current);
1381 release_object( thread );
1384 /* fetch a selector entry for a thread */
1385 DECL_HANDLER(get_selector_entry)
1387 struct thread *thread;
1388 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1390 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1391 release_object( thread );