d2d1: Remove unused D3D10 interfaces.
[wine.git] / server / thread.c
blob703b23d73d166e19ebcdc2e8d9f1b2da3b906128
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <time.h>
35 #ifdef HAVE_POLL_H
36 #include <poll.h>
37 #endif
38 #ifdef HAVE_SCHED_H
39 #include <sched.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
45 #include "winternl.h"
47 #include "file.h"
48 #include "handle.h"
49 #include "process.h"
50 #include "thread.h"
51 #include "request.h"
52 #include "user.h"
53 #include "security.h"
56 /* thread queues */
58 struct thread_wait
60 struct thread_wait *next; /* next wait structure for this thread */
61 struct thread *thread; /* owner thread */
62 int count; /* count of objects */
63 int flags;
64 int abandoned;
65 enum select_op select;
66 client_ptr_t key; /* wait key for keyed events */
67 client_ptr_t cookie; /* magic cookie to return to client */
68 abstime_t when;
69 struct timeout_user *user;
70 int status; /* status to return (unless STATUS_PENDING) */
71 struct wait_queue_entry queues[1];
74 /* asynchronous procedure calls */
76 struct thread_apc
78 struct object obj; /* object header */
79 struct list entry; /* queue linked list */
80 struct thread *caller; /* thread that queued this apc */
81 struct object *owner; /* object that queued this apc */
82 int executed; /* has it been executed by the client? */
83 apc_call_t call; /* call arguments */
84 apc_result_t result; /* call results once executed */
87 static void dump_thread_apc( struct object *obj, int verbose );
88 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry );
89 static void thread_apc_destroy( struct object *obj );
90 static void clear_apc_queue( struct list *queue );
92 static const struct object_ops thread_apc_ops =
94 sizeof(struct thread_apc), /* size */
95 &no_type, /* type */
96 dump_thread_apc, /* dump */
97 add_queue, /* add_queue */
98 remove_queue, /* remove_queue */
99 thread_apc_signaled, /* signaled */
100 no_satisfied, /* satisfied */
101 no_signal, /* signal */
102 no_get_fd, /* get_fd */
103 default_map_access, /* map_access */
104 default_get_sd, /* get_sd */
105 default_set_sd, /* set_sd */
106 no_get_full_name, /* get_full_name */
107 no_lookup_name, /* lookup_name */
108 no_link_name, /* link_name */
109 NULL, /* unlink_name */
110 no_open_file, /* open_file */
111 no_kernel_obj_list, /* get_kernel_obj_list */
112 no_close_handle, /* close_handle */
113 thread_apc_destroy /* destroy */
117 /* thread CPU context */
119 struct context
121 struct object obj; /* object header */
122 unsigned int status; /* status of the context */
123 context_t regs[3]; /* context data */
125 #define CTX_NATIVE 0 /* context for native machine */
126 #define CTX_WOW 1 /* context if thread is inside WoW */
127 #define CTX_PENDING 2 /* pending native context when we don't know whether thread is inside WoW */
129 static void dump_context( struct object *obj, int verbose );
130 static int context_signaled( struct object *obj, struct wait_queue_entry *entry );
132 static const struct object_ops context_ops =
134 sizeof(struct context), /* size */
135 &no_type, /* type */
136 dump_context, /* dump */
137 add_queue, /* add_queue */
138 remove_queue, /* remove_queue */
139 context_signaled, /* signaled */
140 no_satisfied, /* satisfied */
141 no_signal, /* signal */
142 no_get_fd, /* get_fd */
143 default_map_access, /* map_access */
144 default_get_sd, /* get_sd */
145 default_set_sd, /* set_sd */
146 no_get_full_name, /* get_full_name */
147 no_lookup_name, /* lookup_name */
148 no_link_name, /* link_name */
149 NULL, /* unlink_name */
150 no_open_file, /* open_file */
151 no_kernel_obj_list, /* get_kernel_obj_list */
152 no_close_handle, /* close_handle */
153 no_destroy /* destroy */
157 /* thread operations */
159 static const WCHAR thread_name[] = {'T','h','r','e','a','d'};
161 struct type_descr thread_type =
163 { thread_name, sizeof(thread_name) }, /* name */
164 THREAD_ALL_ACCESS, /* valid_access */
165 { /* mapping */
166 STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT,
167 STANDARD_RIGHTS_WRITE | THREAD_SET_LIMITED_INFORMATION | THREAD_SET_INFORMATION
168 | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_TERMINATE | 0x04,
169 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_RESUME | THREAD_QUERY_LIMITED_INFORMATION,
170 THREAD_ALL_ACCESS
174 static void dump_thread( struct object *obj, int verbose );
175 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
176 static unsigned int thread_map_access( struct object *obj, unsigned int access );
177 static void thread_poll_event( struct fd *fd, int event );
178 static struct list *thread_get_kernel_obj_list( struct object *obj );
179 static void destroy_thread( struct object *obj );
181 static const struct object_ops thread_ops =
183 sizeof(struct thread), /* size */
184 &thread_type, /* type */
185 dump_thread, /* dump */
186 add_queue, /* add_queue */
187 remove_queue, /* remove_queue */
188 thread_signaled, /* signaled */
189 no_satisfied, /* satisfied */
190 no_signal, /* signal */
191 no_get_fd, /* get_fd */
192 thread_map_access, /* map_access */
193 default_get_sd, /* get_sd */
194 default_set_sd, /* set_sd */
195 no_get_full_name, /* get_full_name */
196 no_lookup_name, /* lookup_name */
197 no_link_name, /* link_name */
198 NULL, /* unlink_name */
199 no_open_file, /* open_file */
200 thread_get_kernel_obj_list, /* get_kernel_obj_list */
201 no_close_handle, /* close_handle */
202 destroy_thread /* destroy */
205 static const struct fd_ops thread_fd_ops =
207 NULL, /* get_poll_events */
208 thread_poll_event, /* poll_event */
209 NULL, /* flush */
210 NULL, /* get_fd_type */
211 NULL, /* ioctl */
212 NULL, /* queue_async */
213 NULL /* reselect_async */
216 static struct list thread_list = LIST_INIT(thread_list);
218 /* initialize the structure for a newly allocated thread */
219 static inline void init_thread_structure( struct thread *thread )
221 int i;
223 thread->unix_pid = -1; /* not known yet */
224 thread->unix_tid = -1; /* not known yet */
225 thread->context = NULL;
226 thread->teb = 0;
227 thread->entry_point = 0;
228 thread->system_regs = 0;
229 thread->queue = NULL;
230 thread->wait = NULL;
231 thread->error = 0;
232 thread->req_data = NULL;
233 thread->req_toread = 0;
234 thread->reply_data = NULL;
235 thread->reply_towrite = 0;
236 thread->request_fd = NULL;
237 thread->reply_fd = NULL;
238 thread->wait_fd = NULL;
239 thread->state = RUNNING;
240 thread->exit_code = 0;
241 thread->priority = 0;
242 thread->suspend = 0;
243 thread->dbg_hidden = 0;
244 thread->desktop_users = 0;
245 thread->token = NULL;
246 thread->desc = NULL;
247 thread->desc_len = 0;
249 thread->creation_time = current_time;
250 thread->exit_time = 0;
252 list_init( &thread->mutex_list );
253 list_init( &thread->system_apc );
254 list_init( &thread->user_apc );
255 list_init( &thread->kernel_object );
257 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
258 thread->inflight[i].server = thread->inflight[i].client = -1;
261 /* check if address looks valid for a client-side data structure (TEB etc.) */
262 static inline int is_valid_address( client_ptr_t addr )
264 return addr && !(addr % sizeof(int));
268 /* dump a context on stdout for debugging purposes */
269 static void dump_context( struct object *obj, int verbose )
271 struct context *context = (struct context *)obj;
272 assert( obj->ops == &context_ops );
274 fprintf( stderr, "context flags=%x/%x\n",
275 context->regs[CTX_NATIVE].flags, context->regs[CTX_WOW].flags );
279 static int context_signaled( struct object *obj, struct wait_queue_entry *entry )
281 struct context *context = (struct context *)obj;
282 return context->status != STATUS_PENDING;
286 static struct context *create_thread_context( struct thread *thread )
288 struct context *context;
289 if (!(context = alloc_object( &context_ops ))) return NULL;
290 context->status = STATUS_PENDING;
291 memset( &context->regs, 0, sizeof(context->regs) );
292 context->regs[CTX_NATIVE].machine = native_machine;
293 context->regs[CTX_PENDING].machine = native_machine;
294 return context;
298 /* create a new thread */
299 struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
301 struct desktop *desktop;
302 struct thread *thread;
303 int request_pipe[2];
305 if (fd == -1)
307 if (pipe( request_pipe ) == -1)
309 file_set_error();
310 return NULL;
312 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
314 close( request_pipe[0] );
315 close( request_pipe[1] );
316 return NULL;
318 close( request_pipe[1] );
319 fd = request_pipe[0];
322 if (process->is_terminating)
324 close( fd );
325 set_error( STATUS_PROCESS_IS_TERMINATING );
326 return NULL;
329 if (!(thread = alloc_object( &thread_ops )))
331 close( fd );
332 return NULL;
335 init_thread_structure( thread );
337 thread->process = (struct process *)grab_object( process );
338 thread->desktop = 0;
339 thread->affinity = process->affinity;
340 if (!current) current = thread;
342 list_add_tail( &thread_list, &thread->entry );
344 if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
345 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
346 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
347 process->token ))
349 close( fd );
350 release_object( thread );
351 return NULL;
353 if (!(thread->id = alloc_ptid( thread )))
355 close( fd );
356 release_object( thread );
357 return NULL;
359 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
361 release_object( thread );
362 return NULL;
365 if (process->desktop)
367 if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) clear_error(); /* ignore errors */
368 else
370 set_thread_default_desktop( thread, desktop, process->desktop );
371 release_object( desktop );
375 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
376 add_process_thread( thread->process, thread );
377 return thread;
380 /* handle a client event */
381 static void thread_poll_event( struct fd *fd, int event )
383 struct thread *thread = get_fd_user( fd );
384 assert( thread->obj.ops == &thread_ops );
386 grab_object( thread );
387 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
388 else if (event & POLLIN) read_request( thread );
389 else if (event & POLLOUT) write_reply( thread );
390 release_object( thread );
393 static struct list *thread_get_kernel_obj_list( struct object *obj )
395 struct thread *thread = (struct thread *)obj;
396 return &thread->kernel_object;
399 /* cleanup everything that is no longer needed by a dead thread */
400 /* used by destroy_thread and kill_thread */
401 static void cleanup_thread( struct thread *thread )
403 int i;
405 if (thread->context)
407 thread->context->status = STATUS_ACCESS_DENIED;
408 wake_up( &thread->context->obj, 0 );
409 release_object( thread->context );
410 thread->context = NULL;
412 clear_apc_queue( &thread->system_apc );
413 clear_apc_queue( &thread->user_apc );
414 free( thread->req_data );
415 free( thread->reply_data );
416 if (thread->request_fd) release_object( thread->request_fd );
417 if (thread->reply_fd) release_object( thread->reply_fd );
418 if (thread->wait_fd) release_object( thread->wait_fd );
419 cleanup_clipboard_thread(thread);
420 destroy_thread_windows( thread );
421 free_msg_queue( thread );
422 release_thread_desktop( thread, 1 );
423 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
425 if (thread->inflight[i].client != -1)
427 close( thread->inflight[i].server );
428 thread->inflight[i].client = thread->inflight[i].server = -1;
431 free( thread->desc );
432 thread->req_data = NULL;
433 thread->reply_data = NULL;
434 thread->request_fd = NULL;
435 thread->reply_fd = NULL;
436 thread->wait_fd = NULL;
437 thread->desktop = 0;
438 thread->desc = NULL;
439 thread->desc_len = 0;
442 /* destroy a thread when its refcount is 0 */
443 static void destroy_thread( struct object *obj )
445 struct thread *thread = (struct thread *)obj;
446 assert( obj->ops == &thread_ops );
448 list_remove( &thread->entry );
449 cleanup_thread( thread );
450 release_object( thread->process );
451 if (thread->id) free_ptid( thread->id );
452 if (thread->token) release_object( thread->token );
455 /* dump a thread on stdout for debugging purposes */
456 static void dump_thread( struct object *obj, int verbose )
458 struct thread *thread = (struct thread *)obj;
459 assert( obj->ops == &thread_ops );
461 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
462 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
465 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
467 struct thread *mythread = (struct thread *)obj;
468 return (mythread->state == TERMINATED);
471 static unsigned int thread_map_access( struct object *obj, unsigned int access )
473 access = default_map_access( obj, access );
474 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
475 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
476 return access;
479 static void dump_thread_apc( struct object *obj, int verbose )
481 struct thread_apc *apc = (struct thread_apc *)obj;
482 assert( obj->ops == &thread_apc_ops );
484 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
487 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
489 struct thread_apc *apc = (struct thread_apc *)obj;
490 return apc->executed;
493 static void thread_apc_destroy( struct object *obj )
495 struct thread_apc *apc = (struct thread_apc *)obj;
497 if (apc->caller) release_object( apc->caller );
498 if (apc->owner)
500 if (apc->result.type == APC_ASYNC_IO)
501 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
502 else if (apc->call.type == APC_ASYNC_IO)
503 async_set_result( apc->owner, apc->call.async_io.status, 0 );
504 release_object( apc->owner );
508 /* queue an async procedure call */
509 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
511 struct thread_apc *apc;
513 if ((apc = alloc_object( &thread_apc_ops )))
515 apc->call = *call_data;
516 apc->caller = NULL;
517 apc->owner = owner;
518 apc->executed = 0;
519 apc->result.type = APC_NONE;
520 if (owner) grab_object( owner );
522 return apc;
525 /* get a thread pointer from a thread id (and increment the refcount) */
526 struct thread *get_thread_from_id( thread_id_t id )
528 struct object *obj = get_ptid_entry( id );
530 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
531 set_error( STATUS_INVALID_CID );
532 return NULL;
535 /* get a thread from a handle (and increment the refcount) */
536 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
538 return (struct thread *)get_handle_obj( current->process, handle,
539 access, &thread_ops );
542 /* find a thread from a Unix tid */
543 struct thread *get_thread_from_tid( int tid )
545 struct thread *thread;
547 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
549 if (thread->unix_tid == tid) return thread;
551 return NULL;
554 /* find a thread from a Unix pid */
555 struct thread *get_thread_from_pid( int pid )
557 struct thread *thread;
559 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
561 if (thread->unix_pid == pid) return thread;
563 return NULL;
566 int set_thread_affinity( struct thread *thread, affinity_t affinity )
568 int ret = 0;
569 #ifdef HAVE_SCHED_SETAFFINITY
570 if (thread->unix_tid != -1)
572 cpu_set_t set;
573 int i;
574 affinity_t mask;
576 CPU_ZERO( &set );
577 for (i = 0, mask = 1; mask; i++, mask <<= 1)
578 if (affinity & mask) CPU_SET( i, &set );
580 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
582 #endif
583 if (!ret) thread->affinity = affinity;
584 return ret;
587 affinity_t get_thread_affinity( struct thread *thread )
589 affinity_t mask = 0;
590 #ifdef HAVE_SCHED_SETAFFINITY
591 if (thread->unix_tid != -1)
593 cpu_set_t set;
594 unsigned int i;
596 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
597 for (i = 0; i < 8 * sizeof(mask); i++)
598 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
600 #endif
601 if (!mask) mask = ~(affinity_t)0;
602 return mask;
605 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
606 #define THREAD_PRIORITY_REALTIME_LOWEST -7
608 /* set all information about a thread */
609 static void set_thread_info( struct thread *thread,
610 const struct set_thread_info_request *req )
612 if (req->mask & SET_THREAD_INFO_PRIORITY)
614 int max = THREAD_PRIORITY_HIGHEST;
615 int min = THREAD_PRIORITY_LOWEST;
616 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
618 max = THREAD_PRIORITY_REALTIME_HIGHEST;
619 min = THREAD_PRIORITY_REALTIME_LOWEST;
621 if ((req->priority >= min && req->priority <= max) ||
622 req->priority == THREAD_PRIORITY_IDLE ||
623 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
624 thread->priority = req->priority;
625 else
626 set_error( STATUS_INVALID_PARAMETER );
628 if (req->mask & SET_THREAD_INFO_AFFINITY)
630 if ((req->affinity & thread->process->affinity) != req->affinity)
631 set_error( STATUS_INVALID_PARAMETER );
632 else if (thread->state == TERMINATED)
633 set_error( STATUS_THREAD_IS_TERMINATING );
634 else if (set_thread_affinity( thread, req->affinity ))
635 file_set_error();
637 if (req->mask & SET_THREAD_INFO_TOKEN)
638 security_set_thread_token( thread, req->token );
639 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
640 thread->entry_point = req->entry_point;
641 if (req->mask & SET_THREAD_INFO_DBG_HIDDEN)
642 thread->dbg_hidden = 1;
643 if (req->mask & SET_THREAD_INFO_DESCRIPTION)
645 WCHAR *desc;
646 data_size_t desc_len = get_req_data_size();
648 if (desc_len)
650 if ((desc = mem_alloc( desc_len )))
652 memcpy( desc, get_req_data(), desc_len );
653 free( thread->desc );
654 thread->desc = desc;
655 thread->desc_len = desc_len;
658 else
660 free( thread->desc );
661 thread->desc = NULL;
662 thread->desc_len = 0;
667 /* stop a thread (at the Unix level) */
668 void stop_thread( struct thread *thread )
670 if (thread->context) return; /* already suspended, no need for a signal */
671 if (!(thread->context = create_thread_context( thread ))) return;
672 /* can't stop a thread while initialisation is in progress */
673 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
676 /* suspend a thread */
677 int suspend_thread( struct thread *thread )
679 int old_count = thread->suspend;
680 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
682 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
684 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
685 return old_count;
688 /* resume a thread */
689 int resume_thread( struct thread *thread )
691 int old_count = thread->suspend;
692 if (thread->suspend > 0)
694 if (!(--thread->suspend)) resume_delayed_debug_events( thread );
695 if (!(thread->suspend + thread->process->suspend)) wake_thread( thread );
697 return old_count;
700 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
701 int add_queue( struct object *obj, struct wait_queue_entry *entry )
703 grab_object( obj );
704 entry->obj = obj;
705 list_add_tail( &obj->wait_queue, &entry->entry );
706 return 1;
709 /* remove a thread from an object wait queue */
710 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
712 list_remove( &entry->entry );
713 release_object( obj );
716 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
718 return entry->wait->thread;
721 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
723 return entry->wait->select;
726 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
728 return entry->wait->key;
731 void make_wait_abandoned( struct wait_queue_entry *entry )
733 entry->wait->abandoned = 1;
736 void set_wait_status( struct wait_queue_entry *entry, int status )
738 entry->wait->status = status;
741 /* finish waiting */
742 static unsigned int end_wait( struct thread *thread, unsigned int status )
744 struct thread_wait *wait = thread->wait;
745 struct wait_queue_entry *entry;
746 int i;
748 assert( wait );
749 thread->wait = wait->next;
751 if (status < wait->count) /* wait satisfied, tell it to the objects */
753 wait->status = status;
754 if (wait->select == SELECT_WAIT_ALL)
756 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
757 entry->obj->ops->satisfied( entry->obj, entry );
759 else
761 entry = wait->queues + status;
762 entry->obj->ops->satisfied( entry->obj, entry );
764 status = wait->status;
765 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
767 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
768 entry->obj->ops->remove_queue( entry->obj, entry );
769 if (wait->user) remove_timeout_user( wait->user );
770 free( wait );
771 return status;
774 /* build the thread wait structure */
775 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
776 int flags, abstime_t when )
778 struct thread_wait *wait;
779 struct wait_queue_entry *entry;
780 unsigned int i;
782 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
783 wait->next = current->wait;
784 wait->thread = current;
785 wait->count = count;
786 wait->flags = flags;
787 wait->select = select_op->op;
788 wait->cookie = 0;
789 wait->user = NULL;
790 wait->when = when;
791 wait->abandoned = 0;
792 current->wait = wait;
794 for (i = 0, entry = wait->queues; i < count; i++, entry++)
796 struct object *obj = objects[i];
797 entry->wait = wait;
798 if (!obj->ops->add_queue( obj, entry ))
800 wait->count = i;
801 end_wait( current, get_error() );
802 return 0;
805 return 1;
808 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
809 int flags, abstime_t when )
811 struct object *objects[MAXIMUM_WAIT_OBJECTS];
812 unsigned int i;
813 int ret = 0;
815 assert( count <= MAXIMUM_WAIT_OBJECTS );
817 for (i = 0; i < count; i++)
818 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
819 break;
821 if (i == count) ret = wait_on( select_op, count, objects, flags, when );
823 while (i > 0) release_object( objects[--i] );
824 return ret;
827 /* check if the thread waiting condition is satisfied */
828 static int check_wait( struct thread *thread )
830 int i;
831 struct thread_wait *wait = thread->wait;
832 struct wait_queue_entry *entry;
834 assert( wait );
836 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
837 return STATUS_KERNEL_APC;
839 /* Suspended threads may not acquire locks, but they can run system APCs */
840 if (thread->process->suspend + thread->suspend > 0) return -1;
842 if (wait->select == SELECT_WAIT_ALL)
844 int not_ok = 0;
845 /* Note: we must check them all anyway, as some objects may
846 * want to do something when signaled, even if others are not */
847 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
848 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
849 if (!not_ok) return STATUS_WAIT_0;
851 else
853 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
854 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
857 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
858 if (wait->when >= 0 && wait->when <= current_time) return STATUS_TIMEOUT;
859 if (wait->when < 0 && -wait->when <= monotonic_time) return STATUS_TIMEOUT;
860 return -1;
863 /* send the wakeup signal to a thread */
864 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
866 struct wake_up_reply reply;
867 int ret;
869 /* check if we're waking current suspend wait */
870 if (thread->context && thread->suspend_cookie == cookie
871 && signaled != STATUS_KERNEL_APC && signaled != STATUS_USER_APC)
873 if (!thread->context->regs[CTX_NATIVE].flags && !thread->context->regs[CTX_WOW].flags)
875 release_object( thread->context );
876 thread->context = NULL;
878 else signaled = STATUS_KERNEL_APC; /* signal a fake APC so that client calls select to get a new context */
881 memset( &reply, 0, sizeof(reply) );
882 reply.cookie = cookie;
883 reply.signaled = signaled;
884 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
885 return 0;
886 if (ret >= 0)
887 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
888 else if (errno == EPIPE)
889 kill_thread( thread, 0 ); /* normal death */
890 else
891 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
892 return -1;
895 /* attempt to wake up a thread */
896 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
897 int wake_thread( struct thread *thread )
899 int signaled, count;
900 client_ptr_t cookie;
902 for (count = 0; thread->wait; count++)
904 if ((signaled = check_wait( thread )) == -1) break;
906 cookie = thread->wait->cookie;
907 signaled = end_wait( thread, signaled );
908 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
909 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
911 if (!count) count = -1;
912 break;
915 return count;
918 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
919 int wake_thread_queue_entry( struct wait_queue_entry *entry )
921 struct thread_wait *wait = entry->wait;
922 struct thread *thread = wait->thread;
923 int signaled;
924 client_ptr_t cookie;
926 if (thread->wait != wait) return 0; /* not the current wait */
927 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
929 assert( wait->select != SELECT_WAIT_ALL );
931 cookie = wait->cookie;
932 signaled = end_wait( thread, entry - wait->queues );
933 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
935 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
936 wake_thread( thread ); /* check other waits too */
938 return 1;
941 /* thread wait timeout */
942 static void thread_timeout( void *ptr )
944 struct thread_wait *wait = ptr;
945 struct thread *thread = wait->thread;
946 client_ptr_t cookie = wait->cookie;
948 wait->user = NULL;
949 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
950 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
952 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
953 end_wait( thread, STATUS_TIMEOUT );
955 assert( cookie );
956 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
957 /* check if other objects have become signaled in the meantime */
958 wake_thread( thread );
961 /* try signaling an event flag, a semaphore or a mutex */
962 static int signal_object( obj_handle_t handle )
964 struct object *obj;
965 int ret = 0;
967 obj = get_handle_obj( current->process, handle, 0, NULL );
968 if (obj)
970 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
971 release_object( obj );
973 return ret;
976 /* select on a list of handles */
977 static void select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
978 int flags, abstime_t when )
980 int ret;
981 unsigned int count;
982 struct object *object;
984 switch (select_op->op)
986 case SELECT_NONE:
987 if (!wait_on( select_op, 0, NULL, flags, when )) return;
988 break;
990 case SELECT_WAIT:
991 case SELECT_WAIT_ALL:
992 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
993 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
995 set_error( STATUS_INVALID_PARAMETER );
996 return;
998 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, when ))
999 return;
1000 break;
1002 case SELECT_SIGNAL_AND_WAIT:
1003 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, when ))
1004 return;
1005 if (select_op->signal_and_wait.signal)
1007 if (!signal_object( select_op->signal_and_wait.signal ))
1009 end_wait( current, get_error() );
1010 return;
1012 /* check if we woke ourselves up */
1013 if (!current->wait) return;
1015 break;
1017 case SELECT_KEYED_EVENT_WAIT:
1018 case SELECT_KEYED_EVENT_RELEASE:
1019 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
1020 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
1021 if (!object) return;
1022 ret = wait_on( select_op, 1, &object, flags, when );
1023 release_object( object );
1024 if (!ret) return;
1025 current->wait->key = select_op->keyed_event.key;
1026 break;
1028 default:
1029 set_error( STATUS_INVALID_PARAMETER );
1030 return;
1033 if ((ret = check_wait( current )) != -1)
1035 /* condition is already satisfied */
1036 set_error( end_wait( current, ret ));
1037 return;
1040 /* now we need to wait */
1041 if (current->wait->when != TIMEOUT_INFINITE)
1043 if (!(current->wait->user = add_timeout_user( abstime_to_timeout(current->wait->when),
1044 thread_timeout, current->wait )))
1046 end_wait( current, get_error() );
1047 return;
1050 current->wait->cookie = cookie;
1051 set_error( STATUS_PENDING );
1052 return;
1055 /* attempt to wake threads sleeping on the object wait queue */
1056 void wake_up( struct object *obj, int max )
1058 struct list *ptr;
1059 int ret;
1061 LIST_FOR_EACH( ptr, &obj->wait_queue )
1063 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
1064 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
1065 if (ret > 0 && max && !--max) break;
1066 /* restart at the head of the list since a wake up can change the object wait queue */
1067 ptr = &obj->wait_queue;
1071 /* return the apc queue to use for a given apc type */
1072 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
1074 switch(type)
1076 case APC_NONE:
1077 return NULL;
1078 case APC_USER:
1079 return &thread->user_apc;
1080 default:
1081 return &thread->system_apc;
1085 /* check if thread is currently waiting for a (system) apc */
1086 static inline int is_in_apc_wait( struct thread *thread )
1088 return (thread->process->suspend || thread->suspend ||
1089 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
1092 /* queue an existing APC to a given thread */
1093 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
1095 struct list *queue;
1097 if (thread && thread->state == TERMINATED && process)
1098 thread = NULL;
1100 if (!thread) /* find a suitable thread inside the process */
1102 struct thread *candidate;
1104 /* first try to find a waiting thread */
1105 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1107 if (candidate->state == TERMINATED) continue;
1108 if (is_in_apc_wait( candidate ))
1110 thread = candidate;
1111 break;
1114 if (!thread)
1116 /* then use the first one that accepts a signal */
1117 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1119 if (send_thread_signal( candidate, SIGUSR1 ))
1121 thread = candidate;
1122 break;
1126 if (!thread) return 0; /* nothing found */
1127 if (!(queue = get_apc_queue( thread, apc->call.type ))) return 1;
1129 else
1131 if (thread->state == TERMINATED) return 0;
1132 if (!(queue = get_apc_queue( thread, apc->call.type ))) return 1;
1133 /* send signal for system APCs if needed */
1134 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
1136 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
1138 /* cancel a possible previous APC with the same owner */
1139 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
1142 grab_object( apc );
1143 list_add_tail( queue, &apc->entry );
1144 if (!list_prev( queue, &apc->entry )) /* first one */
1145 wake_thread( thread );
1147 return 1;
1150 /* queue an async procedure call */
1151 int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1153 struct thread_apc *apc;
1154 int ret = 0;
1156 if ((apc = create_apc( owner, call_data )))
1158 ret = queue_apc( process, thread, apc );
1159 release_object( apc );
1161 return ret;
1164 /* cancel the async procedure call owned by a specific object */
1165 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1167 struct thread_apc *apc;
1168 struct list *queue = get_apc_queue( thread, type );
1170 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1172 if (apc->owner != owner) continue;
1173 list_remove( &apc->entry );
1174 apc->executed = 1;
1175 wake_up( &apc->obj, 0 );
1176 release_object( apc );
1177 return;
1181 /* remove the head apc from the queue; the returned object must be released by the caller */
1182 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system )
1184 struct thread_apc *apc = NULL;
1185 struct list *ptr = list_head( system ? &thread->system_apc : &thread->user_apc );
1187 if (ptr)
1189 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1190 list_remove( ptr );
1192 return apc;
1195 /* clear an APC queue, cancelling all the APCs on it */
1196 static void clear_apc_queue( struct list *queue )
1198 struct list *ptr;
1200 while ((ptr = list_head( queue )))
1202 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1203 list_remove( &apc->entry );
1204 apc->executed = 1;
1205 wake_up( &apc->obj, 0 );
1206 release_object( apc );
1210 /* add an fd to the inflight list */
1211 /* return list index, or -1 on error */
1212 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1214 int i;
1216 if (server == -1) return -1;
1217 if (client == -1)
1219 close( server );
1220 return -1;
1223 /* first check if we already have an entry for this fd */
1224 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1225 if (thread->inflight[i].client == client)
1227 close( thread->inflight[i].server );
1228 thread->inflight[i].server = server;
1229 return i;
1232 /* now find a free spot to store it */
1233 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1234 if (thread->inflight[i].client == -1)
1236 thread->inflight[i].client = client;
1237 thread->inflight[i].server = server;
1238 return i;
1241 close( server );
1242 return -1;
1245 /* get an inflight fd and purge it from the list */
1246 /* the fd must be closed when no longer used */
1247 int thread_get_inflight_fd( struct thread *thread, int client )
1249 int i, ret;
1251 if (client == -1) return -1;
1255 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1257 if (thread->inflight[i].client == client)
1259 ret = thread->inflight[i].server;
1260 thread->inflight[i].server = thread->inflight[i].client = -1;
1261 return ret;
1264 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1265 return -1;
1268 /* kill a thread on the spot */
1269 void kill_thread( struct thread *thread, int violent_death )
1271 if (thread->state == TERMINATED) return; /* already killed */
1272 thread->state = TERMINATED;
1273 thread->exit_time = current_time;
1274 if (current == thread) current = NULL;
1275 if (debug_level)
1276 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1277 thread->id, thread->exit_code );
1278 if (thread->wait)
1280 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1281 send_thread_wakeup( thread, 0, thread->exit_code );
1282 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1283 violent_death = 0;
1285 kill_console_processes( thread, 0 );
1286 abandon_mutexes( thread );
1287 wake_up( &thread->obj, 0 );
1288 if (violent_death) send_thread_signal( thread, SIGQUIT );
1289 cleanup_thread( thread );
1290 remove_process_thread( thread->process, thread );
1291 release_object( thread );
1294 /* copy parts of a context structure */
1295 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1297 assert( to->machine == from->machine );
1298 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1299 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1300 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1301 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1302 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1303 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1304 if (flags & SERVER_CTX_YMM_REGISTERS) to->ymm = from->ymm;
1307 /* return the context flags that correspond to system regs */
1308 /* (system regs are the ones we can't access on the client side) */
1309 static unsigned int get_context_system_regs( unsigned short machine )
1311 switch (machine)
1313 case IMAGE_FILE_MACHINE_I386: return SERVER_CTX_DEBUG_REGISTERS;
1314 case IMAGE_FILE_MACHINE_AMD64: return SERVER_CTX_DEBUG_REGISTERS;
1315 case IMAGE_FILE_MACHINE_ARMNT: return SERVER_CTX_DEBUG_REGISTERS;
1316 case IMAGE_FILE_MACHINE_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
1318 return 0;
1321 /* gets the current impersonation token */
1322 struct token *thread_get_impersonation_token( struct thread *thread )
1324 if (thread->token)
1325 return thread->token;
1326 else
1327 return thread->process->token;
1330 /* create a new thread */
1331 DECL_HANDLER(new_thread)
1333 struct thread *thread;
1334 struct process *process;
1335 struct unicode_str name;
1336 const struct security_descriptor *sd;
1337 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1338 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1340 if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1342 if (request_fd != -1) close( request_fd );
1343 return;
1346 if (process != current->process)
1348 if (request_fd != -1) /* can't create a request fd in a different process */
1350 close( request_fd );
1351 set_error( STATUS_INVALID_PARAMETER );
1352 goto done;
1354 if (process->running_threads) /* only the initial thread can be created in another process */
1356 set_error( STATUS_ACCESS_DENIED );
1357 goto done;
1360 else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1362 if (request_fd != -1) close( request_fd );
1363 set_error( STATUS_INVALID_HANDLE );
1364 goto done;
1367 if ((thread = create_thread( request_fd, process, sd )))
1369 thread->system_regs = current->system_regs;
1370 if (req->suspend) thread->suspend++;
1371 reply->tid = get_thread_id( thread );
1372 if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
1373 req->access, objattr->attributes )))
1375 /* thread object will be released when the thread gets killed */
1376 goto done;
1378 kill_thread( thread, 1 );
1380 done:
1381 release_object( process );
1384 static int init_thread( struct thread *thread, int reply_fd, int wait_fd )
1386 if ((reply_fd = thread_get_inflight_fd( thread, reply_fd )) == -1)
1388 set_error( STATUS_TOO_MANY_OPENED_FILES );
1389 return 0;
1391 if ((wait_fd = thread_get_inflight_fd( thread, wait_fd )) == -1)
1393 set_error( STATUS_TOO_MANY_OPENED_FILES );
1394 goto error;
1397 if (thread->reply_fd) /* already initialised */
1399 set_error( STATUS_INVALID_PARAMETER );
1400 goto error;
1403 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1405 thread->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &thread->obj, 0 );
1406 thread->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &thread->obj, 0 );
1407 return thread->reply_fd && thread->wait_fd;
1409 error:
1410 if (reply_fd != -1) close( reply_fd );
1411 if (wait_fd != -1) close( wait_fd );
1412 return 0;
1415 /* initialize the first thread of a new process */
1416 DECL_HANDLER(init_first_thread)
1418 struct process *process = current->process;
1420 if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
1422 current->unix_pid = process->unix_pid = req->unix_pid;
1423 current->unix_tid = req->unix_tid;
1425 if (!process->parent_id)
1426 process->affinity = current->affinity = get_thread_affinity( current );
1427 else
1428 set_thread_affinity( current, current->affinity );
1430 debug_level = max( debug_level, req->debug_level );
1432 reply->pid = get_process_id( process );
1433 reply->tid = get_thread_id( current );
1434 reply->info_size = get_process_startup_info_size( process );
1435 reply->server_start = server_start_time;
1436 set_reply_data( supported_machines,
1437 min( supported_machines_count * sizeof(unsigned short), get_reply_max_size() ));
1440 /* initialize a new thread */
1441 DECL_HANDLER(init_thread)
1443 if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
1445 if (!is_valid_address(req->teb))
1447 set_error( STATUS_INVALID_PARAMETER );
1448 return;
1451 current->unix_pid = current->process->unix_pid;
1452 current->unix_tid = req->unix_tid;
1453 current->teb = req->teb;
1454 current->entry_point = req->entry;
1456 init_thread_context( current );
1457 generate_debug_event( current, DbgCreateThreadStateChange, &req->entry );
1458 set_thread_affinity( current, current->affinity );
1460 reply->pid = get_process_id( current->process );
1461 reply->tid = get_thread_id( current );
1462 reply->suspend = (current->suspend || current->process->suspend || current->context != NULL);
1465 /* terminate a thread */
1466 DECL_HANDLER(terminate_thread)
1468 struct thread *thread;
1470 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1472 thread->exit_code = req->exit_code;
1473 if (thread != current) kill_thread( thread, 1 );
1474 else reply->self = 1;
1475 release_object( thread );
1479 /* open a handle to a thread */
1480 DECL_HANDLER(open_thread)
1482 struct thread *thread = get_thread_from_id( req->tid );
1484 reply->handle = 0;
1485 if (thread)
1487 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1488 release_object( thread );
1492 /* fetch information about a thread */
1493 DECL_HANDLER(get_thread_info)
1495 struct thread *thread;
1496 unsigned int access = req->access & (THREAD_QUERY_INFORMATION | THREAD_QUERY_LIMITED_INFORMATION);
1498 if (!access) access = THREAD_QUERY_LIMITED_INFORMATION;
1499 thread = get_thread_from_handle( req->handle, access );
1500 if (thread)
1502 reply->pid = get_process_id( thread->process );
1503 reply->tid = get_thread_id( thread );
1504 reply->teb = thread->teb;
1505 reply->entry_point = thread->entry_point;
1506 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1507 reply->priority = thread->priority;
1508 reply->affinity = thread->affinity;
1509 reply->last = thread->process->running_threads == 1;
1510 reply->suspend_count = thread->suspend;
1511 reply->dbg_hidden = thread->dbg_hidden;
1512 reply->desc_len = thread->desc_len;
1514 if (thread->desc && get_reply_max_size())
1516 if (thread->desc_len <= get_reply_max_size())
1517 set_reply_data( thread->desc, thread->desc_len );
1518 else
1519 set_error( STATUS_BUFFER_TOO_SMALL );
1522 release_object( thread );
1526 /* fetch information about thread times */
1527 DECL_HANDLER(get_thread_times)
1529 struct thread *thread;
1531 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION )))
1533 reply->creation_time = thread->creation_time;
1534 reply->exit_time = thread->exit_time;
1535 reply->unix_pid = thread->unix_pid;
1536 reply->unix_tid = thread->unix_tid;
1538 release_object( thread );
1542 /* set information about a thread */
1543 DECL_HANDLER(set_thread_info)
1545 struct thread *thread;
1547 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1549 set_thread_info( thread, req );
1550 release_object( thread );
1554 /* suspend a thread */
1555 DECL_HANDLER(suspend_thread)
1557 struct thread *thread;
1559 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1561 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1562 else reply->count = suspend_thread( thread );
1563 release_object( thread );
1567 /* resume a thread */
1568 DECL_HANDLER(resume_thread)
1570 struct thread *thread;
1572 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1574 reply->count = resume_thread( thread );
1575 release_object( thread );
1579 /* select on a handle list */
1580 DECL_HANDLER(select)
1582 select_op_t select_op;
1583 data_size_t op_size, ctx_size;
1584 struct context *ctx;
1585 struct thread_apc *apc;
1586 const apc_result_t *result = get_req_data();
1587 unsigned int ctx_count;
1589 if (get_req_data_size() < sizeof(*result)) goto invalid_param;
1590 if (get_req_data_size() - sizeof(*result) < req->size) goto invalid_param;
1591 if (req->size & 3) goto invalid_param;
1592 ctx_size = get_req_data_size() - sizeof(*result) - req->size;
1593 ctx_count = ctx_size / sizeof(context_t);
1594 if (ctx_count * sizeof(context_t) != ctx_size) goto invalid_param;
1595 if (ctx_count > 1 + (current->process->machine != native_machine)) goto invalid_param;
1597 if (ctx_count)
1599 const context_t *native_context = (const context_t *)((const char *)(result + 1) + req->size);
1600 const context_t *wow_context = (ctx_count > 1) ? native_context + 1 : NULL;
1601 unsigned int system_flags = get_context_system_regs( native_machine );
1603 if (current->context && current->context->status != STATUS_PENDING) goto invalid_param;
1605 if (native_context->machine == native_machine)
1607 if (wow_context && wow_context->machine != current->process->machine) goto invalid_param;
1609 else if (native_context->machine == current->process->machine)
1611 if (wow_context) goto invalid_param;
1612 wow_context = native_context;
1613 native_context = NULL;
1615 else goto invalid_param;
1617 if (!current->context && !(current->context = create_thread_context( current ))) return;
1619 ctx = current->context;
1620 if (native_context)
1622 copy_context( &ctx->regs[CTX_NATIVE], native_context,
1623 native_context->flags & ~(ctx->regs[CTX_NATIVE].flags | system_flags) );
1625 if (wow_context)
1627 ctx->regs[CTX_WOW].machine = current->process->machine;
1628 copy_context( &ctx->regs[CTX_WOW], wow_context, wow_context->flags & ~ctx->regs[CTX_WOW].flags );
1630 else if (ctx->regs[CTX_PENDING].flags)
1632 unsigned int flags = ctx->regs[CTX_PENDING].flags & ~ctx->regs[CTX_NATIVE].flags;
1633 copy_context( &ctx->regs[CTX_NATIVE], &ctx->regs[CTX_PENDING], flags );
1634 ctx->regs[CTX_NATIVE].flags |= flags;
1636 ctx->regs[CTX_PENDING].flags = 0;
1637 ctx->status = STATUS_SUCCESS;
1638 current->suspend_cookie = req->cookie;
1639 wake_up( &ctx->obj, 0 );
1642 if (!req->cookie) goto invalid_param;
1644 op_size = min( req->size, sizeof(select_op) );
1645 memset( &select_op, 0, sizeof(select_op) );
1646 memcpy( &select_op, result + 1, op_size );
1648 /* first store results of previous apc */
1649 if (req->prev_apc)
1651 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1652 0, &thread_apc_ops ))) return;
1653 apc->result = *result;
1654 apc->executed = 1;
1655 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1657 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1658 apc->caller->process, 0, 0, DUPLICATE_SAME_ACCESS );
1659 close_handle( current->process, apc->result.create_thread.handle );
1660 apc->result.create_thread.handle = handle;
1661 clear_error(); /* ignore errors from the above calls */
1663 wake_up( &apc->obj, 0 );
1664 close_handle( current->process, req->prev_apc );
1665 release_object( apc );
1668 select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1670 if (get_error() == STATUS_USER_APC)
1672 apc = thread_dequeue_apc( current, 0 );
1673 reply->call = apc->call;
1674 release_object( apc );
1676 else if (get_error() == STATUS_KERNEL_APC)
1678 apc = thread_dequeue_apc( current, 1 );
1679 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1680 reply->call = apc->call;
1681 else
1683 apc->executed = 1;
1684 wake_up( &apc->obj, 0 );
1686 release_object( apc );
1688 else if (get_error() != STATUS_PENDING && get_reply_max_size() >= sizeof(context_t) &&
1689 current->context && current->suspend_cookie == req->cookie)
1691 ctx = current->context;
1692 if (ctx->regs[CTX_NATIVE].flags || ctx->regs[CTX_WOW].flags)
1694 data_size_t size = (ctx->regs[CTX_WOW].flags ? 2 : 1) * sizeof(context_t);
1695 unsigned int system_flags = get_context_system_regs( native_machine ) & ctx->regs[CTX_NATIVE].flags;
1696 if (system_flags) set_thread_context( current, &ctx->regs[CTX_NATIVE], system_flags );
1697 set_reply_data( ctx->regs, min( size, get_reply_max_size() ));
1699 release_object( ctx );
1700 current->context = NULL;
1702 return;
1704 invalid_param:
1705 set_error( STATUS_INVALID_PARAMETER );
1708 /* queue an APC for a thread or process */
1709 DECL_HANDLER(queue_apc)
1711 struct thread *thread = NULL;
1712 struct process *process = NULL;
1713 struct thread_apc *apc;
1715 if (!(apc = create_apc( NULL, &req->call ))) return;
1717 switch (apc->call.type)
1719 case APC_NONE:
1720 case APC_USER:
1721 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1722 break;
1723 case APC_VIRTUAL_ALLOC:
1724 case APC_VIRTUAL_FREE:
1725 case APC_VIRTUAL_PROTECT:
1726 case APC_VIRTUAL_FLUSH:
1727 case APC_VIRTUAL_LOCK:
1728 case APC_VIRTUAL_UNLOCK:
1729 case APC_UNMAP_VIEW:
1730 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1731 break;
1732 case APC_VIRTUAL_QUERY:
1733 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1734 break;
1735 case APC_MAP_VIEW:
1736 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1737 if (process && process != current->process)
1739 /* duplicate the handle into the target process */
1740 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1741 process, 0, 0, DUPLICATE_SAME_ACCESS );
1742 if (handle) apc->call.map_view.handle = handle;
1743 else
1745 release_object( process );
1746 process = NULL;
1749 break;
1750 case APC_CREATE_THREAD:
1751 case APC_BREAK_PROCESS:
1752 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1753 break;
1754 case APC_DUP_HANDLE:
1755 process = get_process_from_handle( req->handle, PROCESS_DUP_HANDLE );
1756 if (process && process != current->process)
1758 /* duplicate the destination process handle into the target process */
1759 obj_handle_t handle = duplicate_handle( current->process, apc->call.dup_handle.dst_process,
1760 process, 0, 0, DUPLICATE_SAME_ACCESS );
1761 if (handle) apc->call.dup_handle.dst_process = handle;
1762 else
1764 release_object( process );
1765 process = NULL;
1768 break;
1769 default:
1770 set_error( STATUS_INVALID_PARAMETER );
1771 break;
1774 if (thread)
1776 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_UNSUCCESSFUL );
1777 release_object( thread );
1779 else if (process)
1781 reply->self = (process == current->process);
1782 if (!reply->self)
1784 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1785 if (handle)
1787 if (queue_apc( process, NULL, apc ))
1789 apc->caller = (struct thread *)grab_object( current );
1790 reply->handle = handle;
1792 else
1794 close_handle( current->process, handle );
1795 set_error( STATUS_PROCESS_IS_TERMINATING );
1799 release_object( process );
1802 release_object( apc );
1805 /* Get the result of an APC call */
1806 DECL_HANDLER(get_apc_result)
1808 struct thread_apc *apc;
1810 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1811 0, &thread_apc_ops ))) return;
1813 if (apc->executed) reply->result = apc->result;
1814 else set_error( STATUS_PENDING );
1816 /* close the handle directly to avoid an extra round-trip */
1817 close_handle( current->process, req->handle );
1818 release_object( apc );
1821 /* retrieve the current context of a thread */
1822 DECL_HANDLER(get_thread_context)
1824 struct context *thread_context = NULL;
1825 struct thread *thread;
1826 context_t *context;
1828 if (get_reply_max_size() < 2 * sizeof(context_t))
1830 set_error( STATUS_INVALID_PARAMETER );
1831 return;
1834 if (req->context)
1836 if (!(thread_context = (struct context *)get_handle_obj( current->process, req->context,
1837 0, &context_ops )))
1838 return;
1839 close_handle( current->process, req->context ); /* avoid extra server call */
1841 else
1843 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1844 if (req->machine != native_machine && req->machine != thread->process->machine)
1845 set_error( STATUS_INVALID_PARAMETER );
1846 else if (thread->state != RUNNING)
1847 set_error( STATUS_UNSUCCESSFUL );
1848 else
1850 unsigned int system_flags = get_context_system_regs( native_machine );
1851 reply->self = (thread == current);
1852 if (thread != current) stop_thread( thread );
1853 if (thread->context)
1855 /* make sure that system regs are valid in thread context */
1856 if (thread->unix_tid != -1 && (system_flags & ~thread->context->regs[CTX_NATIVE].flags))
1857 get_thread_context( thread, &thread->context->regs[CTX_NATIVE], system_flags );
1858 if (!get_error()) thread_context = (struct context *)grab_object( thread->context );
1860 else if (!get_error() && (context = set_reply_data_size( sizeof(context_t) )))
1862 assert( reply->self );
1863 memset( context, 0, sizeof(context_t) );
1864 context->machine = native_machine;
1865 if (system_flags) get_thread_context( thread, context, system_flags );
1868 release_object( thread );
1869 if (!thread_context) return;
1872 if (!thread_context->status)
1874 unsigned int system_flags = get_context_system_regs( native_machine );
1875 unsigned int native_flags = req->flags, wow_flags = 0;
1877 if (req->machine == thread_context->regs[CTX_WOW].machine)
1879 native_flags = req->flags & system_flags;
1880 wow_flags = req->flags & ~system_flags;
1882 if ((context = set_reply_data_size( (!!native_flags + !!wow_flags) * sizeof(context_t) )))
1884 if (native_flags)
1886 memset( context, 0, sizeof(*context) );
1887 context->machine = thread_context->regs[CTX_NATIVE].machine;
1888 copy_context( context, &thread_context->regs[CTX_NATIVE], native_flags );
1889 context->flags = native_flags;
1890 context++;
1892 if (wow_flags)
1894 memset( context, 0, sizeof(*context) );
1895 context->machine = thread_context->regs[CTX_WOW].machine;
1896 copy_context( context, &thread_context->regs[CTX_WOW], wow_flags );
1897 context->flags = wow_flags;
1901 else
1903 set_error( thread_context->status );
1904 if (thread_context->status == STATUS_PENDING)
1905 reply->handle = alloc_handle( current->process, thread_context, SYNCHRONIZE, 0 );
1908 release_object( thread_context );
1911 /* set the current context of a thread */
1912 DECL_HANDLER(set_thread_context)
1914 struct thread *thread;
1915 const context_t *contexts = get_req_data();
1916 unsigned int ctx_count = get_req_data_size() / sizeof(context_t);
1918 if (!ctx_count || ctx_count > 2 || ctx_count * sizeof(context_t) != get_req_data_size())
1920 set_error( STATUS_INVALID_PARAMETER );
1921 return;
1924 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1925 reply->self = (thread == current);
1927 if (contexts[CTX_NATIVE].machine != native_machine ||
1928 (ctx_count == 2 && contexts[CTX_WOW].machine != thread->process->machine))
1929 set_error( STATUS_INVALID_PARAMETER );
1930 else if (thread->state != TERMINATED)
1932 unsigned int flags, ctx = CTX_NATIVE;
1933 const context_t *context = &contexts[CTX_NATIVE];
1934 unsigned int system_flags = get_context_system_regs( native_machine ) & context->flags;
1936 if (thread != current) stop_thread( thread );
1937 else if (system_flags) set_thread_context( thread, context, system_flags );
1938 if (thread->context && !get_error())
1940 if (ctx_count == 2)
1942 /* If the target thread doesn't have a WoW context, set native instead.
1943 * If we don't know yet whether we have a WoW context, store native context
1944 * in CTX_PENDING and update when the target thread sends its context(s). */
1945 if (thread->context->status != STATUS_PENDING)
1947 ctx = thread->context->regs[CTX_WOW].machine ? CTX_WOW : CTX_NATIVE;
1948 context = &contexts[ctx];
1950 else ctx = CTX_PENDING;
1952 flags = context->flags;
1953 if (system_flags && ctx != CTX_NATIVE) /* system regs are always set from the native context */
1955 copy_context( &thread->context->regs[CTX_NATIVE], &contexts[CTX_NATIVE], system_flags );
1956 thread->context->regs[CTX_NATIVE].flags |= system_flags;
1957 flags &= ~system_flags;
1959 copy_context( &thread->context->regs[ctx], context, flags );
1960 thread->context->regs[ctx].flags |= flags;
1963 else set_error( STATUS_UNSUCCESSFUL );
1965 release_object( thread );
1968 /* fetch a selector entry for a thread */
1969 DECL_HANDLER(get_selector_entry)
1971 struct thread *thread;
1972 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1974 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1975 release_object( thread );
1979 /* Iterate thread list for process. Use global thread list to also
1980 * return terminated but not yet destroyed threads. */
1981 DECL_HANDLER(get_next_thread)
1983 struct thread *thread;
1984 struct process *process;
1985 struct list *ptr;
1987 if (req->flags > 1)
1989 set_error( STATUS_INVALID_PARAMETER );
1990 return;
1993 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1994 return;
1996 if (!req->last)
1998 ptr = req->flags ? list_tail( &thread_list ) : list_head( &thread_list );
2000 else if ((thread = get_thread_from_handle( req->last, 0 )))
2002 ptr = req->flags ? list_prev( &thread_list, &thread->entry )
2003 : list_next( &thread_list, &thread->entry );
2004 release_object( thread );
2006 else
2008 release_object( process );
2009 return;
2012 while (ptr)
2014 thread = LIST_ENTRY( ptr, struct thread, entry );
2015 if (thread->process == process)
2017 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
2018 release_object( process );
2019 return;
2021 ptr = req->flags ? list_prev( &thread_list, &thread->entry )
2022 : list_next( &thread_list, &thread->entry );
2024 set_error( STATUS_NO_MORE_ENTRIES );
2025 release_object( process );