amstream: Increase parent IAMMediaStream refcount in IDirectDrawMediaStream::CreateSa...
[wine.git] / server / thread.c
blob9b14174578ee3122e9be558b2991ad36be6d44c7
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 #ifdef __i386__
57 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86);
58 #elif defined(__x86_64__)
59 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86_64) | CPU_FLAG(CPU_x86);
60 #elif defined(__powerpc__)
61 static const unsigned int supported_cpus = CPU_FLAG(CPU_POWERPC);
62 #elif defined(__arm__)
63 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM);
64 #elif defined(__aarch64__)
65 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM64) | CPU_FLAG(CPU_ARM);
66 #else
67 #error Unsupported CPU
68 #endif
70 /* thread queues */
72 struct thread_wait
74 struct thread_wait *next; /* next wait structure for this thread */
75 struct thread *thread; /* owner thread */
76 int count; /* count of objects */
77 int flags;
78 int abandoned;
79 enum select_op select;
80 client_ptr_t key; /* wait key for keyed events */
81 client_ptr_t cookie; /* magic cookie to return to client */
82 abstime_t when;
83 struct timeout_user *user;
84 struct wait_queue_entry queues[1];
87 /* asynchronous procedure calls */
89 struct thread_apc
91 struct object obj; /* object header */
92 struct list entry; /* queue linked list */
93 struct thread *caller; /* thread that queued this apc */
94 struct object *owner; /* object that queued this apc */
95 int executed; /* has it been executed by the client? */
96 apc_call_t call; /* call arguments */
97 apc_result_t result; /* call results once executed */
100 static void dump_thread_apc( struct object *obj, int verbose );
101 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry );
102 static void thread_apc_destroy( struct object *obj );
103 static void clear_apc_queue( struct list *queue );
105 static const struct object_ops thread_apc_ops =
107 sizeof(struct thread_apc), /* size */
108 dump_thread_apc, /* dump */
109 no_get_type, /* get_type */
110 add_queue, /* add_queue */
111 remove_queue, /* remove_queue */
112 thread_apc_signaled, /* signaled */
113 no_satisfied, /* satisfied */
114 no_signal, /* signal */
115 no_get_fd, /* get_fd */
116 no_map_access, /* map_access */
117 default_get_sd, /* get_sd */
118 default_set_sd, /* set_sd */
119 no_lookup_name, /* lookup_name */
120 no_link_name, /* link_name */
121 NULL, /* unlink_name */
122 no_open_file, /* open_file */
123 no_kernel_obj_list, /* get_kernel_obj_list */
124 no_close_handle, /* close_handle */
125 thread_apc_destroy /* destroy */
129 /* thread CPU context */
131 struct context
133 struct object obj; /* object header */
134 unsigned int status; /* status of the context */
135 context_t regs; /* context data */
138 static void dump_context( struct object *obj, int verbose );
139 static int context_signaled( struct object *obj, struct wait_queue_entry *entry );
141 static const struct object_ops context_ops =
143 sizeof(struct context), /* size */
144 dump_context, /* dump */
145 no_get_type, /* get_type */
146 add_queue, /* add_queue */
147 remove_queue, /* remove_queue */
148 context_signaled, /* signaled */
149 no_satisfied, /* satisfied */
150 no_signal, /* signal */
151 no_get_fd, /* get_fd */
152 no_map_access, /* map_access */
153 default_get_sd, /* get_sd */
154 default_set_sd, /* set_sd */
155 no_lookup_name, /* lookup_name */
156 no_link_name, /* link_name */
157 NULL, /* unlink_name */
158 no_open_file, /* open_file */
159 no_kernel_obj_list, /* get_kernel_obj_list */
160 no_close_handle, /* close_handle */
161 no_destroy /* destroy */
165 /* thread operations */
167 static void dump_thread( struct object *obj, int verbose );
168 static struct object_type *thread_get_type( struct object *obj );
169 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
170 static unsigned int thread_map_access( struct object *obj, unsigned int access );
171 static void thread_poll_event( struct fd *fd, int event );
172 static struct list *thread_get_kernel_obj_list( struct object *obj );
173 static void destroy_thread( struct object *obj );
175 static const struct object_ops thread_ops =
177 sizeof(struct thread), /* size */
178 dump_thread, /* dump */
179 thread_get_type, /* get_type */
180 add_queue, /* add_queue */
181 remove_queue, /* remove_queue */
182 thread_signaled, /* signaled */
183 no_satisfied, /* satisfied */
184 no_signal, /* signal */
185 no_get_fd, /* get_fd */
186 thread_map_access, /* map_access */
187 default_get_sd, /* get_sd */
188 default_set_sd, /* set_sd */
189 no_lookup_name, /* lookup_name */
190 no_link_name, /* link_name */
191 NULL, /* unlink_name */
192 no_open_file, /* open_file */
193 thread_get_kernel_obj_list, /* get_kernel_obj_list */
194 no_close_handle, /* close_handle */
195 destroy_thread /* destroy */
198 static const struct fd_ops thread_fd_ops =
200 NULL, /* get_poll_events */
201 thread_poll_event, /* poll_event */
202 NULL, /* flush */
203 NULL, /* get_fd_type */
204 NULL, /* ioctl */
205 NULL, /* queue_async */
206 NULL /* reselect_async */
209 static struct list thread_list = LIST_INIT(thread_list);
211 /* initialize the structure for a newly allocated thread */
212 static inline void init_thread_structure( struct thread *thread )
214 int i;
216 thread->unix_pid = -1; /* not known yet */
217 thread->unix_tid = -1; /* not known yet */
218 thread->context = NULL;
219 thread->teb = 0;
220 thread->entry_point = 0;
221 thread->debug_ctx = NULL;
222 thread->system_regs = 0;
223 thread->queue = NULL;
224 thread->wait = NULL;
225 thread->error = 0;
226 thread->req_data = NULL;
227 thread->req_toread = 0;
228 thread->reply_data = NULL;
229 thread->reply_towrite = 0;
230 thread->request_fd = NULL;
231 thread->reply_fd = NULL;
232 thread->wait_fd = NULL;
233 thread->state = RUNNING;
234 thread->exit_code = 0;
235 thread->priority = 0;
236 thread->suspend = 0;
237 thread->dbg_hidden = 0;
238 thread->desktop_users = 0;
239 thread->token = NULL;
240 thread->desc = NULL;
241 thread->desc_len = 0;
243 thread->creation_time = current_time;
244 thread->exit_time = 0;
246 list_init( &thread->mutex_list );
247 list_init( &thread->system_apc );
248 list_init( &thread->user_apc );
249 list_init( &thread->kernel_object );
251 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
252 thread->inflight[i].server = thread->inflight[i].client = -1;
255 /* check if address looks valid for a client-side data structure (TEB etc.) */
256 static inline int is_valid_address( client_ptr_t addr )
258 return addr && !(addr % sizeof(int));
262 /* dump a context on stdout for debugging purposes */
263 static void dump_context( struct object *obj, int verbose )
265 struct context *context = (struct context *)obj;
266 assert( obj->ops == &context_ops );
268 fprintf( stderr, "context flags=%x\n", context->regs.flags );
272 static int context_signaled( struct object *obj, struct wait_queue_entry *entry )
274 struct context *context = (struct context *)obj;
275 return context->status != STATUS_PENDING;
279 static struct context *create_thread_context( struct thread *thread )
281 struct context *context;
282 if (!(context = alloc_object( &context_ops ))) return NULL;
283 context->status = STATUS_PENDING;
284 memset( &context->regs, 0, sizeof(context->regs) );
285 context->regs.cpu = thread->process->cpu;
286 return context;
290 /* create a new thread */
291 struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
293 struct thread *thread;
294 int request_pipe[2];
296 if (fd == -1)
298 if (pipe( request_pipe ) == -1)
300 file_set_error();
301 return NULL;
303 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
305 close( request_pipe[0] );
306 close( request_pipe[1] );
307 return NULL;
309 close( request_pipe[1] );
310 fd = request_pipe[0];
313 if (process->is_terminating)
315 close( fd );
316 set_error( STATUS_PROCESS_IS_TERMINATING );
317 return NULL;
320 if (!(thread = alloc_object( &thread_ops )))
322 close( fd );
323 return NULL;
326 init_thread_structure( thread );
328 thread->process = (struct process *)grab_object( process );
329 thread->desktop = process->desktop;
330 thread->affinity = process->affinity;
331 if (!current) current = thread;
333 list_add_tail( &thread_list, &thread->entry );
335 if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
336 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
337 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
338 process->token ))
340 close( fd );
341 release_object( thread );
342 return NULL;
344 if (!(thread->id = alloc_ptid( thread )))
346 close( fd );
347 release_object( thread );
348 return NULL;
350 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
352 release_object( thread );
353 return NULL;
356 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
357 add_process_thread( thread->process, thread );
358 return thread;
361 /* handle a client event */
362 static void thread_poll_event( struct fd *fd, int event )
364 struct thread *thread = get_fd_user( fd );
365 assert( thread->obj.ops == &thread_ops );
367 grab_object( thread );
368 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
369 else if (event & POLLIN) read_request( thread );
370 else if (event & POLLOUT) write_reply( thread );
371 release_object( thread );
374 static struct list *thread_get_kernel_obj_list( struct object *obj )
376 struct thread *thread = (struct thread *)obj;
377 return &thread->kernel_object;
380 /* cleanup everything that is no longer needed by a dead thread */
381 /* used by destroy_thread and kill_thread */
382 static void cleanup_thread( struct thread *thread )
384 int i;
386 if (thread->context)
388 thread->context->status = STATUS_ACCESS_DENIED;
389 wake_up( &thread->context->obj, 0 );
390 release_object( thread->context );
391 thread->context = NULL;
393 clear_apc_queue( &thread->system_apc );
394 clear_apc_queue( &thread->user_apc );
395 free( thread->req_data );
396 free( thread->reply_data );
397 if (thread->request_fd) release_object( thread->request_fd );
398 if (thread->reply_fd) release_object( thread->reply_fd );
399 if (thread->wait_fd) release_object( thread->wait_fd );
400 cleanup_clipboard_thread(thread);
401 destroy_thread_windows( thread );
402 free_msg_queue( thread );
403 close_thread_desktop( thread );
404 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
406 if (thread->inflight[i].client != -1)
408 close( thread->inflight[i].server );
409 thread->inflight[i].client = thread->inflight[i].server = -1;
412 free( thread->desc );
413 thread->req_data = NULL;
414 thread->reply_data = NULL;
415 thread->request_fd = NULL;
416 thread->reply_fd = NULL;
417 thread->wait_fd = NULL;
418 thread->desktop = 0;
419 thread->desc = NULL;
420 thread->desc_len = 0;
423 /* destroy a thread when its refcount is 0 */
424 static void destroy_thread( struct object *obj )
426 struct thread *thread = (struct thread *)obj;
427 assert( obj->ops == &thread_ops );
429 assert( !thread->debug_ctx ); /* cannot still be debugging something */
430 list_remove( &thread->entry );
431 cleanup_thread( thread );
432 release_object( thread->process );
433 if (thread->id) free_ptid( thread->id );
434 if (thread->token) release_object( thread->token );
437 /* dump a thread on stdout for debugging purposes */
438 static void dump_thread( struct object *obj, int verbose )
440 struct thread *thread = (struct thread *)obj;
441 assert( obj->ops == &thread_ops );
443 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
444 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
447 static struct object_type *thread_get_type( struct object *obj )
449 static const WCHAR name[] = {'T','h','r','e','a','d'};
450 static const struct unicode_str str = { name, sizeof(name) };
451 return get_object_type( &str );
454 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
456 struct thread *mythread = (struct thread *)obj;
457 return (mythread->state == TERMINATED);
460 static unsigned int thread_map_access( struct object *obj, unsigned int access )
462 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
463 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
464 THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
465 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
466 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
468 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
469 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
471 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
474 static void dump_thread_apc( struct object *obj, int verbose )
476 struct thread_apc *apc = (struct thread_apc *)obj;
477 assert( obj->ops == &thread_apc_ops );
479 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
482 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
484 struct thread_apc *apc = (struct thread_apc *)obj;
485 return apc->executed;
488 static void thread_apc_destroy( struct object *obj )
490 struct thread_apc *apc = (struct thread_apc *)obj;
491 if (apc->caller) release_object( apc->caller );
492 if (apc->owner) release_object( apc->owner );
495 /* queue an async procedure call */
496 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
498 struct thread_apc *apc;
500 if ((apc = alloc_object( &thread_apc_ops )))
502 apc->call = *call_data;
503 apc->caller = NULL;
504 apc->owner = owner;
505 apc->executed = 0;
506 apc->result.type = APC_NONE;
507 if (owner) grab_object( owner );
509 return apc;
512 /* get a thread pointer from a thread id (and increment the refcount) */
513 struct thread *get_thread_from_id( thread_id_t id )
515 struct object *obj = get_ptid_entry( id );
517 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
518 set_error( STATUS_INVALID_CID );
519 return NULL;
522 /* get a thread from a handle (and increment the refcount) */
523 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
525 return (struct thread *)get_handle_obj( current->process, handle,
526 access, &thread_ops );
529 /* find a thread from a Unix tid */
530 struct thread *get_thread_from_tid( int tid )
532 struct thread *thread;
534 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
536 if (thread->unix_tid == tid) return thread;
538 return NULL;
541 /* find a thread from a Unix pid */
542 struct thread *get_thread_from_pid( int pid )
544 struct thread *thread;
546 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
548 if (thread->unix_pid == pid) return thread;
550 return NULL;
553 int set_thread_affinity( struct thread *thread, affinity_t affinity )
555 int ret = 0;
556 #ifdef HAVE_SCHED_SETAFFINITY
557 if (thread->unix_tid != -1)
559 cpu_set_t set;
560 int i;
561 affinity_t mask;
563 CPU_ZERO( &set );
564 for (i = 0, mask = 1; mask; i++, mask <<= 1)
565 if (affinity & mask) CPU_SET( i, &set );
567 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
569 #endif
570 if (!ret) thread->affinity = affinity;
571 return ret;
574 affinity_t get_thread_affinity( struct thread *thread )
576 affinity_t mask = 0;
577 #ifdef HAVE_SCHED_SETAFFINITY
578 if (thread->unix_tid != -1)
580 cpu_set_t set;
581 unsigned int i;
583 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
584 for (i = 0; i < 8 * sizeof(mask); i++)
585 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
587 #endif
588 if (!mask) mask = ~(affinity_t)0;
589 return mask;
592 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
593 #define THREAD_PRIORITY_REALTIME_LOWEST -7
595 /* set all information about a thread */
596 static void set_thread_info( struct thread *thread,
597 const struct set_thread_info_request *req )
599 if (req->mask & SET_THREAD_INFO_PRIORITY)
601 int max = THREAD_PRIORITY_HIGHEST;
602 int min = THREAD_PRIORITY_LOWEST;
603 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
605 max = THREAD_PRIORITY_REALTIME_HIGHEST;
606 min = THREAD_PRIORITY_REALTIME_LOWEST;
608 if ((req->priority >= min && req->priority <= max) ||
609 req->priority == THREAD_PRIORITY_IDLE ||
610 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
611 thread->priority = req->priority;
612 else
613 set_error( STATUS_INVALID_PARAMETER );
615 if (req->mask & SET_THREAD_INFO_AFFINITY)
617 if ((req->affinity & thread->process->affinity) != req->affinity)
618 set_error( STATUS_INVALID_PARAMETER );
619 else if (thread->state == TERMINATED)
620 set_error( STATUS_THREAD_IS_TERMINATING );
621 else if (set_thread_affinity( thread, req->affinity ))
622 file_set_error();
624 if (req->mask & SET_THREAD_INFO_TOKEN)
625 security_set_thread_token( thread, req->token );
626 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
627 thread->entry_point = req->entry_point;
628 if (req->mask & SET_THREAD_INFO_DBG_HIDDEN)
629 thread->dbg_hidden = 1;
630 if (req->mask & SET_THREAD_INFO_DESCRIPTION)
632 WCHAR *desc;
633 data_size_t desc_len = get_req_data_size();
635 if (desc_len)
637 if ((desc = mem_alloc( desc_len )))
639 memcpy( desc, get_req_data(), desc_len );
640 free( thread->desc );
641 thread->desc = desc;
642 thread->desc_len = desc_len;
645 else
647 free( thread->desc );
648 thread->desc = NULL;
649 thread->desc_len = 0;
654 /* stop a thread (at the Unix level) */
655 void stop_thread( struct thread *thread )
657 if (thread->context) return; /* already suspended, no need for a signal */
658 if (!(thread->context = create_thread_context( thread ))) return;
659 /* can't stop a thread while initialisation is in progress */
660 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
663 /* suspend a thread */
664 int suspend_thread( struct thread *thread )
666 int old_count = thread->suspend;
667 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
669 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
671 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
672 return old_count;
675 /* resume a thread */
676 int resume_thread( struct thread *thread )
678 int old_count = thread->suspend;
679 if (thread->suspend > 0)
681 if (!(--thread->suspend)) resume_delayed_debug_events( thread );
682 if (!(thread->suspend + thread->process->suspend)) wake_thread( thread );
684 return old_count;
687 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
688 int add_queue( struct object *obj, struct wait_queue_entry *entry )
690 grab_object( obj );
691 entry->obj = obj;
692 list_add_tail( &obj->wait_queue, &entry->entry );
693 return 1;
696 /* remove a thread from an object wait queue */
697 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
699 list_remove( &entry->entry );
700 release_object( obj );
703 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
705 return entry->wait->thread;
708 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
710 return entry->wait->select;
713 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
715 return entry->wait->key;
718 void make_wait_abandoned( struct wait_queue_entry *entry )
720 entry->wait->abandoned = 1;
723 /* finish waiting */
724 static unsigned int end_wait( struct thread *thread, unsigned int status )
726 struct thread_wait *wait = thread->wait;
727 struct wait_queue_entry *entry;
728 int i;
730 assert( wait );
731 thread->wait = wait->next;
733 if (status < wait->count) /* wait satisfied, tell it to the objects */
735 if (wait->select == SELECT_WAIT_ALL)
737 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
738 entry->obj->ops->satisfied( entry->obj, entry );
740 else
742 entry = wait->queues + status;
743 entry->obj->ops->satisfied( entry->obj, entry );
745 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
747 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
748 entry->obj->ops->remove_queue( entry->obj, entry );
749 if (wait->user) remove_timeout_user( wait->user );
750 free( wait );
751 return status;
754 /* build the thread wait structure */
755 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
756 int flags, abstime_t when )
758 struct thread_wait *wait;
759 struct wait_queue_entry *entry;
760 unsigned int i;
762 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
763 wait->next = current->wait;
764 wait->thread = current;
765 wait->count = count;
766 wait->flags = flags;
767 wait->select = select_op->op;
768 wait->cookie = 0;
769 wait->user = NULL;
770 wait->when = when;
771 wait->abandoned = 0;
772 current->wait = wait;
774 for (i = 0, entry = wait->queues; i < count; i++, entry++)
776 struct object *obj = objects[i];
777 entry->wait = wait;
778 if (!obj->ops->add_queue( obj, entry ))
780 wait->count = i;
781 end_wait( current, get_error() );
782 return 0;
785 return 1;
788 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
789 int flags, abstime_t when )
791 struct object *objects[MAXIMUM_WAIT_OBJECTS];
792 unsigned int i;
793 int ret = 0;
795 assert( count <= MAXIMUM_WAIT_OBJECTS );
797 for (i = 0; i < count; i++)
798 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
799 break;
801 if (i == count) ret = wait_on( select_op, count, objects, flags, when );
803 while (i > 0) release_object( objects[--i] );
804 return ret;
807 /* check if the thread waiting condition is satisfied */
808 static int check_wait( struct thread *thread )
810 int i;
811 struct thread_wait *wait = thread->wait;
812 struct wait_queue_entry *entry;
814 assert( wait );
816 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
817 return STATUS_KERNEL_APC;
819 /* Suspended threads may not acquire locks, but they can run system APCs */
820 if (thread->process->suspend + thread->suspend > 0) return -1;
822 if (wait->select == SELECT_WAIT_ALL)
824 int not_ok = 0;
825 /* Note: we must check them all anyway, as some objects may
826 * want to do something when signaled, even if others are not */
827 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
828 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
829 if (!not_ok) return STATUS_WAIT_0;
831 else
833 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
834 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
837 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
838 if (wait->when >= 0 && wait->when <= current_time) return STATUS_TIMEOUT;
839 if (wait->when < 0 && -wait->when <= monotonic_time) return STATUS_TIMEOUT;
840 return -1;
843 /* send the wakeup signal to a thread */
844 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
846 struct wake_up_reply reply;
847 int ret;
849 /* check if we're waking current suspend wait */
850 if (thread->context && thread->suspend_cookie == cookie
851 && signaled != STATUS_KERNEL_APC && signaled != STATUS_USER_APC)
853 if (!thread->context->regs.flags)
855 release_object( thread->context );
856 thread->context = NULL;
858 else signaled = STATUS_KERNEL_APC; /* signal a fake APC so that client calls select to get a new context */
861 memset( &reply, 0, sizeof(reply) );
862 reply.cookie = cookie;
863 reply.signaled = signaled;
864 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
865 return 0;
866 if (ret >= 0)
867 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
868 else if (errno == EPIPE)
869 kill_thread( thread, 0 ); /* normal death */
870 else
871 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
872 return -1;
875 /* attempt to wake up a thread */
876 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
877 int wake_thread( struct thread *thread )
879 int signaled, count;
880 client_ptr_t cookie;
882 for (count = 0; thread->wait; count++)
884 if ((signaled = check_wait( thread )) == -1) break;
886 cookie = thread->wait->cookie;
887 signaled = end_wait( thread, signaled );
888 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
889 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
891 if (!count) count = -1;
892 break;
895 return count;
898 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
899 int wake_thread_queue_entry( struct wait_queue_entry *entry )
901 struct thread_wait *wait = entry->wait;
902 struct thread *thread = wait->thread;
903 int signaled;
904 client_ptr_t cookie;
906 if (thread->wait != wait) return 0; /* not the current wait */
907 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
909 assert( wait->select != SELECT_WAIT_ALL );
911 cookie = wait->cookie;
912 signaled = end_wait( thread, entry - wait->queues );
913 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
915 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
916 wake_thread( thread ); /* check other waits too */
918 return 1;
921 /* thread wait timeout */
922 static void thread_timeout( void *ptr )
924 struct thread_wait *wait = ptr;
925 struct thread *thread = wait->thread;
926 client_ptr_t cookie = wait->cookie;
928 wait->user = NULL;
929 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
930 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
932 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
933 end_wait( thread, STATUS_TIMEOUT );
935 assert( cookie );
936 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
937 /* check if other objects have become signaled in the meantime */
938 wake_thread( thread );
941 /* try signaling an event flag, a semaphore or a mutex */
942 static int signal_object( obj_handle_t handle )
944 struct object *obj;
945 int ret = 0;
947 obj = get_handle_obj( current->process, handle, 0, NULL );
948 if (obj)
950 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
951 release_object( obj );
953 return ret;
956 /* select on a list of handles */
957 static void select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
958 int flags, abstime_t when )
960 int ret;
961 unsigned int count;
962 struct object *object;
964 switch (select_op->op)
966 case SELECT_NONE:
967 if (!wait_on( select_op, 0, NULL, flags, when )) return;
968 break;
970 case SELECT_WAIT:
971 case SELECT_WAIT_ALL:
972 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
973 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
975 set_error( STATUS_INVALID_PARAMETER );
976 return;
978 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, when ))
979 return;
980 break;
982 case SELECT_SIGNAL_AND_WAIT:
983 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, when ))
984 return;
985 if (select_op->signal_and_wait.signal)
987 if (!signal_object( select_op->signal_and_wait.signal ))
989 end_wait( current, get_error() );
990 return;
992 /* check if we woke ourselves up */
993 if (!current->wait) return;
995 break;
997 case SELECT_KEYED_EVENT_WAIT:
998 case SELECT_KEYED_EVENT_RELEASE:
999 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
1000 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
1001 if (!object) return;
1002 ret = wait_on( select_op, 1, &object, flags, when );
1003 release_object( object );
1004 if (!ret) return;
1005 current->wait->key = select_op->keyed_event.key;
1006 break;
1008 default:
1009 set_error( STATUS_INVALID_PARAMETER );
1010 return;
1013 if ((ret = check_wait( current )) != -1)
1015 /* condition is already satisfied */
1016 set_error( end_wait( current, ret ));
1017 return;
1020 /* now we need to wait */
1021 if (current->wait->when != TIMEOUT_INFINITE)
1023 if (!(current->wait->user = add_timeout_user( abstime_to_timeout(current->wait->when),
1024 thread_timeout, current->wait )))
1026 end_wait( current, get_error() );
1027 return;
1030 current->wait->cookie = cookie;
1031 set_error( STATUS_PENDING );
1032 return;
1035 /* attempt to wake threads sleeping on the object wait queue */
1036 void wake_up( struct object *obj, int max )
1038 struct list *ptr;
1039 int ret;
1041 LIST_FOR_EACH( ptr, &obj->wait_queue )
1043 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
1044 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
1045 if (ret > 0 && max && !--max) break;
1046 /* restart at the head of the list since a wake up can change the object wait queue */
1047 ptr = &obj->wait_queue;
1051 /* return the apc queue to use for a given apc type */
1052 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
1054 switch(type)
1056 case APC_NONE:
1057 case APC_USER:
1058 case APC_TIMER:
1059 return &thread->user_apc;
1060 default:
1061 return &thread->system_apc;
1065 /* check if thread is currently waiting for a (system) apc */
1066 static inline int is_in_apc_wait( struct thread *thread )
1068 return (thread->process->suspend || thread->suspend ||
1069 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
1072 /* queue an existing APC to a given thread */
1073 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
1075 struct list *queue;
1077 if (thread && thread->state == TERMINATED && process)
1078 thread = NULL;
1080 if (!thread) /* find a suitable thread inside the process */
1082 struct thread *candidate;
1084 /* first try to find a waiting thread */
1085 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1087 if (candidate->state == TERMINATED) continue;
1088 if (is_in_apc_wait( candidate ))
1090 thread = candidate;
1091 break;
1094 if (!thread)
1096 /* then use the first one that accepts a signal */
1097 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1099 if (send_thread_signal( candidate, SIGUSR1 ))
1101 thread = candidate;
1102 break;
1106 if (!thread) return 0; /* nothing found */
1107 queue = get_apc_queue( thread, apc->call.type );
1109 else
1111 if (thread->state == TERMINATED) return 0;
1112 queue = get_apc_queue( thread, apc->call.type );
1113 /* send signal for system APCs if needed */
1114 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
1116 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
1118 /* cancel a possible previous APC with the same owner */
1119 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
1122 grab_object( apc );
1123 list_add_tail( queue, &apc->entry );
1124 if (!list_prev( queue, &apc->entry )) /* first one */
1125 wake_thread( thread );
1127 return 1;
1130 /* queue an async procedure call */
1131 int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1133 struct thread_apc *apc;
1134 int ret = 0;
1136 if ((apc = create_apc( owner, call_data )))
1138 ret = queue_apc( process, thread, apc );
1139 release_object( apc );
1141 return ret;
1144 /* cancel the async procedure call owned by a specific object */
1145 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1147 struct thread_apc *apc;
1148 struct list *queue = get_apc_queue( thread, type );
1150 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1152 if (apc->owner != owner) continue;
1153 list_remove( &apc->entry );
1154 apc->executed = 1;
1155 wake_up( &apc->obj, 0 );
1156 release_object( apc );
1157 return;
1161 /* remove the head apc from the queue; the returned object must be released by the caller */
1162 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system )
1164 struct thread_apc *apc = NULL;
1165 struct list *ptr = list_head( system ? &thread->system_apc : &thread->user_apc );
1167 if (ptr)
1169 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1170 list_remove( ptr );
1172 return apc;
1175 /* clear an APC queue, cancelling all the APCs on it */
1176 static void clear_apc_queue( struct list *queue )
1178 struct list *ptr;
1180 while ((ptr = list_head( queue )))
1182 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1183 list_remove( &apc->entry );
1184 apc->executed = 1;
1185 wake_up( &apc->obj, 0 );
1186 release_object( apc );
1190 /* add an fd to the inflight list */
1191 /* return list index, or -1 on error */
1192 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1194 int i;
1196 if (server == -1) return -1;
1197 if (client == -1)
1199 close( server );
1200 return -1;
1203 /* first check if we already have an entry for this fd */
1204 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1205 if (thread->inflight[i].client == client)
1207 close( thread->inflight[i].server );
1208 thread->inflight[i].server = server;
1209 return i;
1212 /* now find a free spot to store it */
1213 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1214 if (thread->inflight[i].client == -1)
1216 thread->inflight[i].client = client;
1217 thread->inflight[i].server = server;
1218 return i;
1221 close( server );
1222 return -1;
1225 /* get an inflight fd and purge it from the list */
1226 /* the fd must be closed when no longer used */
1227 int thread_get_inflight_fd( struct thread *thread, int client )
1229 int i, ret;
1231 if (client == -1) return -1;
1235 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1237 if (thread->inflight[i].client == client)
1239 ret = thread->inflight[i].server;
1240 thread->inflight[i].server = thread->inflight[i].client = -1;
1241 return ret;
1244 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1245 return -1;
1248 /* kill a thread on the spot */
1249 void kill_thread( struct thread *thread, int violent_death )
1251 if (thread->state == TERMINATED) return; /* already killed */
1252 thread->state = TERMINATED;
1253 thread->exit_time = current_time;
1254 if (current == thread) current = NULL;
1255 if (debug_level)
1256 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1257 thread->id, thread->exit_code );
1258 if (thread->wait)
1260 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1261 send_thread_wakeup( thread, 0, thread->exit_code );
1262 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1263 violent_death = 0;
1265 kill_console_processes( thread, 0 );
1266 debug_exit_thread( thread );
1267 abandon_mutexes( thread );
1268 wake_up( &thread->obj, 0 );
1269 if (violent_death) send_thread_signal( thread, SIGQUIT );
1270 cleanup_thread( thread );
1271 remove_process_thread( thread->process, thread );
1272 release_object( thread );
1275 /* copy parts of a context structure */
1276 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1278 assert( to->cpu == from->cpu );
1279 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1280 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1281 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1282 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1283 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1284 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1287 /* return the context flags that correspond to system regs */
1288 /* (system regs are the ones we can't access on the client side) */
1289 static unsigned int get_context_system_regs( enum cpu_type cpu )
1291 switch (cpu)
1293 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1294 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1295 case CPU_POWERPC: return 0;
1296 case CPU_ARM: return SERVER_CTX_DEBUG_REGISTERS;
1297 case CPU_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
1299 return 0;
1302 /* gets the current impersonation token */
1303 struct token *thread_get_impersonation_token( struct thread *thread )
1305 if (thread->token)
1306 return thread->token;
1307 else
1308 return thread->process->token;
1311 /* check if a cpu type can be supported on this server */
1312 int is_cpu_supported( enum cpu_type cpu )
1314 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1316 if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
1317 if (!(supported_cpus & prefix_cpu_mask))
1318 set_error( STATUS_NOT_SUPPORTED );
1319 else if (supported_cpus & CPU_FLAG(cpu))
1320 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1321 else
1322 set_error( STATUS_INVALID_IMAGE_FORMAT );
1323 return 0;
1326 /* return the cpu mask for supported cpus */
1327 unsigned int get_supported_cpu_mask(void)
1329 return supported_cpus & get_prefix_cpu_mask();
1332 /* create a new thread */
1333 DECL_HANDLER(new_thread)
1335 struct thread *thread;
1336 struct process *process;
1337 struct unicode_str name;
1338 const struct security_descriptor *sd;
1339 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1340 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1342 if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1344 if (request_fd != -1) close( request_fd );
1345 return;
1348 if (process != current->process)
1350 if (request_fd != -1) /* can't create a request fd in a different process */
1352 close( request_fd );
1353 set_error( STATUS_INVALID_PARAMETER );
1354 goto done;
1356 if (process->running_threads) /* only the initial thread can be created in another process */
1358 set_error( STATUS_ACCESS_DENIED );
1359 goto done;
1362 else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1364 if (request_fd != -1) close( request_fd );
1365 set_error( STATUS_INVALID_HANDLE );
1366 goto done;
1369 if ((thread = create_thread( request_fd, process, sd )))
1371 thread->system_regs = current->system_regs;
1372 if (req->suspend) thread->suspend++;
1373 reply->tid = get_thread_id( thread );
1374 if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
1375 req->access, objattr->attributes )))
1377 /* thread object will be released when the thread gets killed */
1378 goto done;
1380 kill_thread( thread, 1 );
1382 done:
1383 release_object( process );
1386 /* initialize a new thread */
1387 DECL_HANDLER(init_thread)
1389 struct process *process = current->process;
1390 int wait_fd, reply_fd;
1392 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1394 set_error( STATUS_TOO_MANY_OPENED_FILES );
1395 return;
1397 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1399 set_error( STATUS_TOO_MANY_OPENED_FILES );
1400 goto error;
1403 if (current->reply_fd) /* already initialised */
1405 set_error( STATUS_INVALID_PARAMETER );
1406 goto error;
1409 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1411 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1412 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1413 if (!current->reply_fd || !current->wait_fd) return;
1415 if (!is_valid_address(req->teb))
1417 set_error( STATUS_INVALID_PARAMETER );
1418 return;
1421 current->unix_pid = req->unix_pid;
1422 current->unix_tid = req->unix_tid;
1423 current->teb = req->teb;
1424 current->entry_point = process->peb ? req->entry : 0;
1426 if (!process->peb) /* first thread, initialize the process too */
1428 if (!is_cpu_supported( req->cpu )) return;
1429 process->unix_pid = current->unix_pid;
1430 process->peb = req->entry;
1431 process->cpu = req->cpu;
1432 reply->info_size = init_process( current );
1433 if (!process->parent_id)
1434 process->affinity = current->affinity = get_thread_affinity( current );
1435 else
1436 set_thread_affinity( current, current->affinity );
1438 else
1440 if (req->cpu != process->cpu)
1442 set_error( STATUS_INVALID_PARAMETER );
1443 return;
1445 if (process->unix_pid != current->unix_pid)
1446 process->unix_pid = -1; /* can happen with linuxthreads */
1447 init_thread_context( current );
1448 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1449 set_thread_affinity( current, current->affinity );
1451 debug_level = max( debug_level, req->debug_level );
1453 reply->pid = get_process_id( process );
1454 reply->tid = get_thread_id( current );
1455 reply->version = SERVER_PROTOCOL_VERSION;
1456 reply->server_start = server_start_time;
1457 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1458 reply->suspend = (current->suspend || process->suspend || current->context != NULL);
1459 return;
1461 error:
1462 if (reply_fd != -1) close( reply_fd );
1463 if (wait_fd != -1) close( wait_fd );
1466 /* terminate a thread */
1467 DECL_HANDLER(terminate_thread)
1469 struct thread *thread;
1471 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1473 thread->exit_code = req->exit_code;
1474 if (thread != current) kill_thread( thread, 1 );
1475 else reply->self = 1;
1476 release_object( thread );
1480 /* open a handle to a thread */
1481 DECL_HANDLER(open_thread)
1483 struct thread *thread = get_thread_from_id( req->tid );
1485 reply->handle = 0;
1486 if (thread)
1488 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1489 release_object( thread );
1493 /* fetch information about a thread */
1494 DECL_HANDLER(get_thread_info)
1496 struct thread *thread;
1497 unsigned int access = req->access & (THREAD_QUERY_INFORMATION | THREAD_QUERY_LIMITED_INFORMATION);
1499 if (!access) access = THREAD_QUERY_LIMITED_INFORMATION;
1500 thread = get_thread_from_handle( req->handle, access );
1501 if (thread)
1503 reply->pid = get_process_id( thread->process );
1504 reply->tid = get_thread_id( thread );
1505 reply->teb = thread->teb;
1506 reply->entry_point = thread->entry_point;
1507 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1508 reply->priority = thread->priority;
1509 reply->affinity = thread->affinity;
1510 reply->last = thread->process->running_threads == 1;
1511 reply->suspend_count = thread->suspend;
1512 reply->dbg_hidden = thread->dbg_hidden;
1513 reply->desc_len = thread->desc_len;
1515 if (thread->desc && get_reply_max_size())
1517 if (thread->desc_len <= get_reply_max_size())
1518 set_reply_data( thread->desc, thread->desc_len );
1519 else
1520 set_error( STATUS_BUFFER_TOO_SMALL );
1523 release_object( thread );
1527 /* fetch information about thread times */
1528 DECL_HANDLER(get_thread_times)
1530 struct thread *thread;
1532 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION )))
1534 reply->creation_time = thread->creation_time;
1535 reply->exit_time = thread->exit_time;
1536 reply->unix_pid = thread->unix_pid;
1537 reply->unix_tid = thread->unix_tid;
1539 release_object( thread );
1543 /* set information about a thread */
1544 DECL_HANDLER(set_thread_info)
1546 struct thread *thread;
1548 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1550 set_thread_info( thread, req );
1551 release_object( thread );
1555 /* suspend a thread */
1556 DECL_HANDLER(suspend_thread)
1558 struct thread *thread;
1560 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1562 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1563 else reply->count = suspend_thread( thread );
1564 release_object( thread );
1568 /* resume a thread */
1569 DECL_HANDLER(resume_thread)
1571 struct thread *thread;
1573 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1575 reply->count = resume_thread( thread );
1576 release_object( thread );
1580 /* select on a handle list */
1581 DECL_HANDLER(select)
1583 select_op_t select_op;
1584 data_size_t op_size;
1585 struct thread_apc *apc;
1586 const apc_result_t *result = get_req_data();
1588 if (get_req_data_size() < sizeof(*result) ||
1589 get_req_data_size() - sizeof(*result) < req->size ||
1590 req->size & 3)
1592 set_error( STATUS_INVALID_PARAMETER );
1593 return;
1596 if (get_req_data_size() - sizeof(*result) - req->size == sizeof(context_t))
1598 const context_t *context = (const context_t *)((const char *)(result + 1) + req->size);
1599 if ((current->context && current->context->status != STATUS_PENDING) || context->cpu != current->process->cpu)
1601 set_error( STATUS_INVALID_PARAMETER );
1602 return;
1605 if (!current->context && !(current->context = create_thread_context( current ))) return;
1606 copy_context( &current->context->regs, context,
1607 context->flags & ~(current->context->regs.flags | get_context_system_regs(current->process->cpu)) );
1608 current->context->status = STATUS_SUCCESS;
1609 current->suspend_cookie = req->cookie;
1610 wake_up( &current->context->obj, 0 );
1613 if (!req->cookie)
1615 set_error( STATUS_INVALID_PARAMETER );
1616 return;
1619 op_size = min( req->size, sizeof(select_op) );
1620 memset( &select_op, 0, sizeof(select_op) );
1621 memcpy( &select_op, result + 1, op_size );
1623 /* first store results of previous apc */
1624 if (req->prev_apc)
1626 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1627 0, &thread_apc_ops ))) return;
1628 apc->result = *result;
1629 apc->executed = 1;
1630 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1632 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1633 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1634 close_handle( current->process, apc->result.create_thread.handle );
1635 apc->result.create_thread.handle = handle;
1636 clear_error(); /* ignore errors from the above calls */
1638 else if (apc->result.type == APC_ASYNC_IO)
1640 if (apc->owner)
1641 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
1643 wake_up( &apc->obj, 0 );
1644 close_handle( current->process, req->prev_apc );
1645 release_object( apc );
1648 select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1650 while (get_error() == STATUS_USER_APC)
1652 if (!(apc = thread_dequeue_apc( current, 0 )))
1653 break;
1654 /* Optimization: ignore APC_NONE calls, they are only used to
1655 * wake up a thread, but since we got here the thread woke up already.
1657 if (apc->call.type != APC_NONE &&
1658 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1660 reply->call = apc->call;
1661 release_object( apc );
1662 break;
1664 apc->executed = 1;
1665 wake_up( &apc->obj, 0 );
1666 release_object( apc );
1669 if (get_error() == STATUS_KERNEL_APC)
1671 apc = thread_dequeue_apc( current, 1 );
1672 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1673 reply->call = apc->call;
1674 else
1676 apc->executed = 1;
1677 wake_up( &apc->obj, 0 );
1679 release_object( apc );
1681 else if (get_error() != STATUS_PENDING && get_reply_max_size() == sizeof(context_t) &&
1682 current->context && current->suspend_cookie == req->cookie)
1684 if (current->context->regs.flags)
1686 unsigned int system_flags = get_context_system_regs(current->process->cpu) &
1687 current->context->regs.flags;
1688 if (system_flags) set_thread_context( current, &current->context->regs, system_flags );
1689 set_reply_data( &current->context->regs, sizeof(context_t) );
1691 release_object( current->context );
1692 current->context = NULL;
1696 /* queue an APC for a thread or process */
1697 DECL_HANDLER(queue_apc)
1699 struct thread *thread = NULL;
1700 struct process *process = NULL;
1701 struct thread_apc *apc;
1703 if (!(apc = create_apc( NULL, &req->call ))) return;
1705 switch (apc->call.type)
1707 case APC_NONE:
1708 case APC_USER:
1709 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1710 break;
1711 case APC_VIRTUAL_ALLOC:
1712 case APC_VIRTUAL_FREE:
1713 case APC_VIRTUAL_PROTECT:
1714 case APC_VIRTUAL_FLUSH:
1715 case APC_VIRTUAL_LOCK:
1716 case APC_VIRTUAL_UNLOCK:
1717 case APC_UNMAP_VIEW:
1718 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1719 break;
1720 case APC_VIRTUAL_QUERY:
1721 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1722 break;
1723 case APC_MAP_VIEW:
1724 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1725 if (process && process != current->process)
1727 /* duplicate the handle into the target process */
1728 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1729 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1730 if (handle) apc->call.map_view.handle = handle;
1731 else
1733 release_object( process );
1734 process = NULL;
1737 break;
1738 case APC_CREATE_THREAD:
1739 case APC_BREAK_PROCESS:
1740 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1741 break;
1742 default:
1743 set_error( STATUS_INVALID_PARAMETER );
1744 break;
1747 if (thread)
1749 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1750 release_object( thread );
1752 else if (process)
1754 reply->self = (process == current->process);
1755 if (!reply->self)
1757 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1758 if (handle)
1760 if (queue_apc( process, NULL, apc ))
1762 apc->caller = (struct thread *)grab_object( current );
1763 reply->handle = handle;
1765 else
1767 close_handle( current->process, handle );
1768 set_error( STATUS_PROCESS_IS_TERMINATING );
1772 release_object( process );
1775 release_object( apc );
1778 /* Get the result of an APC call */
1779 DECL_HANDLER(get_apc_result)
1781 struct thread_apc *apc;
1783 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1784 0, &thread_apc_ops ))) return;
1786 if (apc->executed) reply->result = apc->result;
1787 else set_error( STATUS_PENDING );
1789 /* close the handle directly to avoid an extra round-trip */
1790 close_handle( current->process, req->handle );
1791 release_object( apc );
1794 /* retrieve the current context of a thread */
1795 DECL_HANDLER(get_thread_context)
1797 struct context *thread_context = NULL;
1798 unsigned int system_flags;
1799 struct thread *thread;
1800 context_t *context;
1802 if (get_reply_max_size() < sizeof(context_t))
1804 set_error( STATUS_INVALID_PARAMETER );
1805 return;
1808 if ((thread_context = (struct context *)get_handle_obj( current->process, req->handle, 0, &context_ops )))
1810 close_handle( current->process, req->handle ); /* avoid extra server call */
1811 system_flags = get_context_system_regs( thread_context->regs.cpu );
1813 else if ((thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT )))
1815 clear_error();
1816 system_flags = get_context_system_regs( thread->process->cpu );
1817 if (thread->state == RUNNING)
1819 reply->self = (thread == current);
1820 if (thread != current) stop_thread( thread );
1821 if (thread->context)
1823 /* make sure that system regs are valid in thread context */
1824 if (thread->unix_tid != -1 && (req->flags & system_flags & ~thread->context->regs.flags))
1825 get_thread_context( thread, &thread->context->regs, req->flags & system_flags );
1826 if (!get_error()) thread_context = (struct context *)grab_object( thread->context );
1828 else if (!get_error() && (context = set_reply_data_size( sizeof(context_t) )))
1830 assert( reply->self );
1831 memset( context, 0, sizeof(context_t) );
1832 context->cpu = thread->process->cpu;
1833 if (req->flags & system_flags)
1835 get_thread_context( thread, context, req->flags & system_flags );
1836 context->flags |= req->flags & system_flags;
1840 else set_error( STATUS_UNSUCCESSFUL );
1841 release_object( thread );
1843 if (get_error() || !thread_context) return;
1845 set_error( thread_context->status );
1846 if (!thread_context->status && (context = set_reply_data_size( sizeof(context_t) )))
1848 memset( context, 0, sizeof(context_t) );
1849 context->cpu = thread_context->regs.cpu;
1850 copy_context( context, &thread_context->regs, req->flags );
1851 context->flags = req->flags;
1853 else if (thread_context->status == STATUS_PENDING)
1855 reply->handle = alloc_handle( current->process, thread_context, SYNCHRONIZE, 0 );
1858 release_object( thread_context );
1861 /* set the current context of a thread */
1862 DECL_HANDLER(set_thread_context)
1864 struct thread *thread;
1865 const context_t *context = get_req_data();
1867 if (get_req_data_size() < sizeof(context_t))
1869 set_error( STATUS_INVALID_PARAMETER );
1870 return;
1872 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1873 reply->self = (thread == current);
1875 if (thread->state == TERMINATED) set_error( STATUS_UNSUCCESSFUL );
1876 else if (context->cpu != thread->process->cpu) set_error( STATUS_INVALID_PARAMETER );
1877 else
1879 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1881 if (thread != current) stop_thread( thread );
1882 else if (system_flags) set_thread_context( thread, context, system_flags );
1883 if (thread->context && !get_error())
1885 copy_context( &thread->context->regs, context, context->flags );
1886 thread->context->regs.flags |= context->flags;
1890 release_object( thread );
1893 /* fetch a selector entry for a thread */
1894 DECL_HANDLER(get_selector_entry)
1896 struct thread *thread;
1897 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1899 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1900 release_object( thread );