ws2_32/tests: Enable compilation with -D__WINESRC__.
[wine.git] / server / thread.c
blob32985ac0e57d611815eb1fb3eb3cb24b34455940
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);
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 timeout_t timeout;
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_open_file, /* open_file */
121 no_close_handle, /* close_handle */
122 thread_apc_destroy /* destroy */
126 /* thread operations */
128 static void dump_thread( struct object *obj, int verbose );
129 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
130 static unsigned int thread_map_access( struct object *obj, unsigned int access );
131 static void thread_poll_event( struct fd *fd, int event );
132 static void destroy_thread( struct object *obj );
134 static const struct object_ops thread_ops =
136 sizeof(struct thread), /* size */
137 dump_thread, /* dump */
138 no_get_type, /* get_type */
139 add_queue, /* add_queue */
140 remove_queue, /* remove_queue */
141 thread_signaled, /* signaled */
142 no_satisfied, /* satisfied */
143 no_signal, /* signal */
144 no_get_fd, /* get_fd */
145 thread_map_access, /* map_access */
146 default_get_sd, /* get_sd */
147 default_set_sd, /* set_sd */
148 no_lookup_name, /* lookup_name */
149 no_open_file, /* open_file */
150 no_close_handle, /* close_handle */
151 destroy_thread /* destroy */
154 static const struct fd_ops thread_fd_ops =
156 NULL, /* get_poll_events */
157 thread_poll_event, /* poll_event */
158 NULL, /* flush */
159 NULL, /* get_fd_type */
160 NULL, /* ioctl */
161 NULL, /* queue_async */
162 NULL, /* reselect_async */
163 NULL /* cancel_async */
166 static struct list thread_list = LIST_INIT(thread_list);
168 /* initialize the structure for a newly allocated thread */
169 static inline void init_thread_structure( struct thread *thread )
171 int i;
173 thread->unix_pid = -1; /* not known yet */
174 thread->unix_tid = -1; /* not known yet */
175 thread->context = NULL;
176 thread->suspend_context = NULL;
177 thread->teb = 0;
178 thread->debug_ctx = NULL;
179 thread->debug_event = NULL;
180 thread->debug_break = 0;
181 thread->queue = NULL;
182 thread->wait = NULL;
183 thread->error = 0;
184 thread->req_data = NULL;
185 thread->req_toread = 0;
186 thread->reply_data = NULL;
187 thread->reply_towrite = 0;
188 thread->request_fd = NULL;
189 thread->reply_fd = NULL;
190 thread->wait_fd = NULL;
191 thread->state = RUNNING;
192 thread->exit_code = 0;
193 thread->priority = 0;
194 thread->suspend = 0;
195 thread->desktop_users = 0;
196 thread->token = NULL;
198 thread->creation_time = current_time;
199 thread->exit_time = 0;
201 list_init( &thread->mutex_list );
202 list_init( &thread->system_apc );
203 list_init( &thread->user_apc );
205 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
206 thread->inflight[i].server = thread->inflight[i].client = -1;
209 /* check if address looks valid for a client-side data structure (TEB etc.) */
210 static inline int is_valid_address( client_ptr_t addr )
212 return addr && !(addr % sizeof(int));
215 /* create a new thread */
216 struct thread *create_thread( int fd, struct process *process )
218 struct thread *thread;
220 if (process->is_terminating)
222 set_error( STATUS_PROCESS_IS_TERMINATING );
223 return NULL;
226 if (!(thread = alloc_object( &thread_ops ))) return NULL;
228 init_thread_structure( thread );
230 thread->process = (struct process *)grab_object( process );
231 thread->desktop = process->desktop;
232 thread->affinity = process->affinity;
233 if (!current) current = thread;
235 list_add_head( &thread_list, &thread->entry );
237 if (!(thread->id = alloc_ptid( thread )))
239 release_object( thread );
240 return NULL;
242 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
244 release_object( thread );
245 return NULL;
248 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
249 add_process_thread( thread->process, thread );
250 return thread;
253 /* handle a client event */
254 static void thread_poll_event( struct fd *fd, int event )
256 struct thread *thread = get_fd_user( fd );
257 assert( thread->obj.ops == &thread_ops );
259 grab_object( thread );
260 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
261 else if (event & POLLIN) read_request( thread );
262 else if (event & POLLOUT) write_reply( thread );
263 release_object( thread );
266 /* cleanup everything that is no longer needed by a dead thread */
267 /* used by destroy_thread and kill_thread */
268 static void cleanup_thread( struct thread *thread )
270 int i;
272 clear_apc_queue( &thread->system_apc );
273 clear_apc_queue( &thread->user_apc );
274 free( thread->req_data );
275 free( thread->reply_data );
276 if (thread->request_fd) release_object( thread->request_fd );
277 if (thread->reply_fd) release_object( thread->reply_fd );
278 if (thread->wait_fd) release_object( thread->wait_fd );
279 free( thread->suspend_context );
280 cleanup_clipboard_thread(thread);
281 destroy_thread_windows( thread );
282 free_msg_queue( thread );
283 close_thread_desktop( thread );
284 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
286 if (thread->inflight[i].client != -1)
288 close( thread->inflight[i].server );
289 thread->inflight[i].client = thread->inflight[i].server = -1;
292 thread->req_data = NULL;
293 thread->reply_data = NULL;
294 thread->request_fd = NULL;
295 thread->reply_fd = NULL;
296 thread->wait_fd = NULL;
297 thread->context = NULL;
298 thread->suspend_context = NULL;
299 thread->desktop = 0;
302 /* destroy a thread when its refcount is 0 */
303 static void destroy_thread( struct object *obj )
305 struct thread *thread = (struct thread *)obj;
306 assert( obj->ops == &thread_ops );
308 assert( !thread->debug_ctx ); /* cannot still be debugging something */
309 list_remove( &thread->entry );
310 cleanup_thread( thread );
311 release_object( thread->process );
312 if (thread->id) free_ptid( thread->id );
313 if (thread->token) release_object( thread->token );
316 /* dump a thread on stdout for debugging purposes */
317 static void dump_thread( struct object *obj, int verbose )
319 struct thread *thread = (struct thread *)obj;
320 assert( obj->ops == &thread_ops );
322 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
323 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
326 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
328 struct thread *mythread = (struct thread *)obj;
329 return (mythread->state == TERMINATED);
332 static unsigned int thread_map_access( struct object *obj, unsigned int access )
334 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
335 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
336 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
337 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
338 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
341 static void dump_thread_apc( struct object *obj, int verbose )
343 struct thread_apc *apc = (struct thread_apc *)obj;
344 assert( obj->ops == &thread_apc_ops );
346 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
349 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
351 struct thread_apc *apc = (struct thread_apc *)obj;
352 return apc->executed;
355 static void thread_apc_destroy( struct object *obj )
357 struct thread_apc *apc = (struct thread_apc *)obj;
358 if (apc->caller) release_object( apc->caller );
359 if (apc->owner) release_object( apc->owner );
362 /* queue an async procedure call */
363 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
365 struct thread_apc *apc;
367 if ((apc = alloc_object( &thread_apc_ops )))
369 apc->call = *call_data;
370 apc->caller = NULL;
371 apc->owner = owner;
372 apc->executed = 0;
373 apc->result.type = APC_NONE;
374 if (owner) grab_object( owner );
376 return apc;
379 /* get a thread pointer from a thread id (and increment the refcount) */
380 struct thread *get_thread_from_id( thread_id_t id )
382 struct object *obj = get_ptid_entry( id );
384 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
385 set_error( STATUS_INVALID_CID );
386 return NULL;
389 /* get a thread from a handle (and increment the refcount) */
390 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
392 return (struct thread *)get_handle_obj( current->process, handle,
393 access, &thread_ops );
396 /* find a thread from a Unix tid */
397 struct thread *get_thread_from_tid( int tid )
399 struct thread *thread;
401 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
403 if (thread->unix_tid == tid) return thread;
405 return NULL;
408 /* find a thread from a Unix pid */
409 struct thread *get_thread_from_pid( int pid )
411 struct thread *thread;
413 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
415 if (thread->unix_pid == pid) return thread;
417 return NULL;
420 int set_thread_affinity( struct thread *thread, affinity_t affinity )
422 int ret = 0;
423 #ifdef HAVE_SCHED_SETAFFINITY
424 if (thread->unix_tid != -1)
426 cpu_set_t set;
427 int i;
428 affinity_t mask;
430 CPU_ZERO( &set );
431 for (i = 0, mask = 1; mask; i++, mask <<= 1)
432 if (affinity & mask) CPU_SET( i, &set );
434 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
436 #endif
437 if (!ret) thread->affinity = affinity;
438 return ret;
441 affinity_t get_thread_affinity( struct thread *thread )
443 affinity_t mask = 0;
444 #ifdef HAVE_SCHED_SETAFFINITY
445 if (thread->unix_tid != -1)
447 cpu_set_t set;
448 unsigned int i;
450 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
451 for (i = 0; i < 8 * sizeof(mask); i++)
452 if (CPU_ISSET( i, &set )) mask |= 1 << i;
454 #endif
455 if (!mask) mask = ~0;
456 return mask;
459 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
460 #define THREAD_PRIORITY_REALTIME_LOWEST -7
462 /* set all information about a thread */
463 static void set_thread_info( struct thread *thread,
464 const struct set_thread_info_request *req )
466 if (req->mask & SET_THREAD_INFO_PRIORITY)
468 int max = THREAD_PRIORITY_HIGHEST;
469 int min = THREAD_PRIORITY_LOWEST;
470 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
472 max = THREAD_PRIORITY_REALTIME_HIGHEST;
473 min = THREAD_PRIORITY_REALTIME_LOWEST;
475 if ((req->priority >= min && req->priority <= max) ||
476 req->priority == THREAD_PRIORITY_IDLE ||
477 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
478 thread->priority = req->priority;
479 else
480 set_error( STATUS_INVALID_PARAMETER );
482 if (req->mask & SET_THREAD_INFO_AFFINITY)
484 if ((req->affinity & thread->process->affinity) != req->affinity)
485 set_error( STATUS_INVALID_PARAMETER );
486 else if (thread->state == TERMINATED)
487 set_error( STATUS_THREAD_IS_TERMINATING );
488 else if (set_thread_affinity( thread, req->affinity ))
489 file_set_error();
491 if (req->mask & SET_THREAD_INFO_TOKEN)
492 security_set_thread_token( thread, req->token );
495 /* stop a thread (at the Unix level) */
496 void stop_thread( struct thread *thread )
498 if (thread->context) return; /* already inside a debug event, no need for a signal */
499 /* can't stop a thread while initialisation is in progress */
500 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
503 /* stop a thread if it's supposed to be suspended */
504 void stop_thread_if_suspended( struct thread *thread )
506 if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
509 /* suspend a thread */
510 static int suspend_thread( struct thread *thread )
512 int old_count = thread->suspend;
513 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
515 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
517 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
518 return old_count;
521 /* resume a thread */
522 static int resume_thread( struct thread *thread )
524 int old_count = thread->suspend;
525 if (thread->suspend > 0)
527 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
529 return old_count;
532 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
533 int add_queue( struct object *obj, struct wait_queue_entry *entry )
535 grab_object( obj );
536 entry->obj = obj;
537 list_add_tail( &obj->wait_queue, &entry->entry );
538 return 1;
541 /* remove a thread from an object wait queue */
542 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
544 list_remove( &entry->entry );
545 release_object( obj );
548 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
550 return entry->wait->thread;
553 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
555 return entry->wait->select;
558 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
560 return entry->wait->key;
563 void make_wait_abandoned( struct wait_queue_entry *entry )
565 entry->wait->abandoned = 1;
568 /* finish waiting */
569 static void end_wait( struct thread *thread )
571 struct thread_wait *wait = thread->wait;
572 struct wait_queue_entry *entry;
573 int i;
575 assert( wait );
576 thread->wait = wait->next;
577 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
578 entry->obj->ops->remove_queue( entry->obj, entry );
579 if (wait->user) remove_timeout_user( wait->user );
580 free( wait );
583 /* build the thread wait structure */
584 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
585 int flags, timeout_t timeout )
587 struct thread_wait *wait;
588 struct wait_queue_entry *entry;
589 unsigned int i;
591 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
592 wait->next = current->wait;
593 wait->thread = current;
594 wait->count = count;
595 wait->flags = flags;
596 wait->select = select_op->op;
597 wait->user = NULL;
598 wait->timeout = timeout;
599 wait->abandoned = 0;
600 current->wait = wait;
602 for (i = 0, entry = wait->queues; i < count; i++, entry++)
604 struct object *obj = objects[i];
605 entry->wait = wait;
606 if (!obj->ops->add_queue( obj, entry ))
608 wait->count = i;
609 end_wait( current );
610 return 0;
613 return 1;
616 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
617 int flags, timeout_t timeout )
619 struct object *objects[MAXIMUM_WAIT_OBJECTS];
620 unsigned int i;
621 int ret = 0;
623 assert( count <= MAXIMUM_WAIT_OBJECTS );
625 for (i = 0; i < count; i++)
626 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
627 break;
629 if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
631 while (i > 0) release_object( objects[--i] );
632 return ret;
635 /* check if the thread waiting condition is satisfied */
636 static int check_wait( struct thread *thread )
638 int i;
639 struct thread_wait *wait = thread->wait;
640 struct wait_queue_entry *entry;
642 assert( wait );
644 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
645 return STATUS_USER_APC;
647 /* Suspended threads may not acquire locks, but they can run system APCs */
648 if (thread->process->suspend + thread->suspend > 0) return -1;
650 if (wait->select == SELECT_WAIT_ALL)
652 int not_ok = 0;
653 /* Note: we must check them all anyway, as some objects may
654 * want to do something when signaled, even if others are not */
655 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
656 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
657 if (not_ok) goto other_checks;
658 /* Wait satisfied: tell it to all objects */
659 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
660 entry->obj->ops->satisfied( entry->obj, entry );
661 return wait->abandoned ? STATUS_ABANDONED_WAIT_0 : STATUS_WAIT_0;
663 else
665 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
667 if (!entry->obj->ops->signaled( entry->obj, entry )) continue;
668 /* Wait satisfied: tell it to the object */
669 entry->obj->ops->satisfied( entry->obj, entry );
670 if (wait->abandoned) i += STATUS_ABANDONED_WAIT_0;
671 return i;
675 other_checks:
676 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
677 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
678 return -1;
681 /* send the wakeup signal to a thread */
682 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
684 struct wake_up_reply reply;
685 int ret;
687 memset( &reply, 0, sizeof(reply) );
688 reply.cookie = cookie;
689 reply.signaled = signaled;
690 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
691 return 0;
692 if (ret >= 0)
693 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
694 else if (errno == EPIPE)
695 kill_thread( thread, 0 ); /* normal death */
696 else
697 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
698 return -1;
701 /* attempt to wake up a thread */
702 /* return >0 if OK, 0 if the wait condition is still not satisfied */
703 int wake_thread( struct thread *thread )
705 int signaled, count;
706 client_ptr_t cookie;
708 for (count = 0; thread->wait; count++)
710 if ((signaled = check_wait( thread )) == -1) break;
712 cookie = thread->wait->cookie;
713 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
714 end_wait( thread );
715 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
716 break;
718 return count;
721 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
722 int wake_thread_queue_entry( struct wait_queue_entry *entry )
724 struct thread_wait *wait = entry->wait;
725 struct thread *thread = wait->thread;
726 int signaled;
727 client_ptr_t cookie;
729 if (thread->wait != wait) return 0; /* not the current wait */
730 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
732 assert( wait->select != SELECT_WAIT_ALL );
734 signaled = entry - wait->queues;
735 entry->obj->ops->satisfied( entry->obj, entry );
736 if (wait->abandoned) signaled += STATUS_ABANDONED_WAIT_0;
738 cookie = wait->cookie;
739 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
740 end_wait( thread );
742 if (send_thread_wakeup( thread, cookie, signaled ) != -1)
743 wake_thread( thread ); /* check other waits too */
745 return 1;
748 /* thread wait timeout */
749 static void thread_timeout( void *ptr )
751 struct thread_wait *wait = ptr;
752 struct thread *thread = wait->thread;
753 client_ptr_t cookie = wait->cookie;
755 wait->user = NULL;
756 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
757 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
759 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
760 end_wait( thread );
761 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
762 /* check if other objects have become signaled in the meantime */
763 wake_thread( thread );
766 /* try signaling an event flag, a semaphore or a mutex */
767 static int signal_object( obj_handle_t handle )
769 struct object *obj;
770 int ret = 0;
772 obj = get_handle_obj( current->process, handle, 0, NULL );
773 if (obj)
775 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
776 release_object( obj );
778 return ret;
781 /* select on a list of handles */
782 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
783 int flags, timeout_t timeout )
785 int ret;
786 unsigned int count;
787 struct object *object;
789 if (timeout <= 0) timeout = current_time - timeout;
791 switch (select_op->op)
793 case SELECT_NONE:
794 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
795 break;
797 case SELECT_WAIT:
798 case SELECT_WAIT_ALL:
799 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
800 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
802 set_error( STATUS_INVALID_PARAMETER );
803 return 0;
805 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
806 return timeout;
807 break;
809 case SELECT_SIGNAL_AND_WAIT:
810 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
811 return timeout;
812 if (select_op->signal_and_wait.signal)
814 if (!signal_object( select_op->signal_and_wait.signal ))
816 end_wait( current );
817 return timeout;
819 /* check if we woke ourselves up */
820 if (!current->wait) return timeout;
822 break;
824 case SELECT_KEYED_EVENT_WAIT:
825 case SELECT_KEYED_EVENT_RELEASE:
826 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
827 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
828 if (!object) return timeout;
829 ret = wait_on( select_op, 1, &object, flags, timeout );
830 release_object( object );
831 if (!ret) return timeout;
832 current->wait->key = select_op->keyed_event.key;
833 break;
835 default:
836 set_error( STATUS_INVALID_PARAMETER );
837 return 0;
840 if ((ret = check_wait( current )) != -1)
842 /* condition is already satisfied */
843 end_wait( current );
844 set_error( ret );
845 return timeout;
848 /* now we need to wait */
849 if (current->wait->timeout != TIMEOUT_INFINITE)
851 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
852 thread_timeout, current->wait )))
854 end_wait( current );
855 return timeout;
858 current->wait->cookie = cookie;
859 set_error( STATUS_PENDING );
860 return timeout;
863 /* attempt to wake threads sleeping on the object wait queue */
864 void wake_up( struct object *obj, int max )
866 struct list *ptr;
868 LIST_FOR_EACH( ptr, &obj->wait_queue )
870 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
871 if (!wake_thread( get_wait_queue_thread( entry ))) continue;
872 if (max && !--max) break;
873 /* restart at the head of the list since a wake up can change the object wait queue */
874 ptr = &obj->wait_queue;
878 /* return the apc queue to use for a given apc type */
879 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
881 switch(type)
883 case APC_NONE:
884 case APC_USER:
885 case APC_TIMER:
886 return &thread->user_apc;
887 default:
888 return &thread->system_apc;
892 /* check if thread is currently waiting for a (system) apc */
893 static inline int is_in_apc_wait( struct thread *thread )
895 return (thread->process->suspend || thread->suspend ||
896 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
899 /* queue an existing APC to a given thread */
900 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
902 struct list *queue;
904 if (!thread) /* find a suitable thread inside the process */
906 struct thread *candidate;
908 /* first try to find a waiting thread */
909 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
911 if (candidate->state == TERMINATED) continue;
912 if (is_in_apc_wait( candidate ))
914 thread = candidate;
915 break;
918 if (!thread)
920 /* then use the first one that accepts a signal */
921 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
923 if (send_thread_signal( candidate, SIGUSR1 ))
925 thread = candidate;
926 break;
930 if (!thread) return 0; /* nothing found */
931 queue = get_apc_queue( thread, apc->call.type );
933 else
935 if (thread->state == TERMINATED) return 0;
936 queue = get_apc_queue( thread, apc->call.type );
937 /* send signal for system APCs if needed */
938 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
940 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
942 /* cancel a possible previous APC with the same owner */
943 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
946 grab_object( apc );
947 list_add_tail( queue, &apc->entry );
948 if (!list_prev( queue, &apc->entry )) /* first one */
949 wake_thread( thread );
951 return 1;
954 /* queue an async procedure call */
955 int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data )
957 struct thread_apc *apc;
958 int ret = 0;
960 if ((apc = create_apc( owner, call_data )))
962 ret = queue_apc( NULL, thread, apc );
963 release_object( apc );
965 return ret;
968 /* cancel the async procedure call owned by a specific object */
969 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
971 struct thread_apc *apc;
972 struct list *queue = get_apc_queue( thread, type );
974 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
976 if (apc->owner != owner) continue;
977 list_remove( &apc->entry );
978 apc->executed = 1;
979 wake_up( &apc->obj, 0 );
980 release_object( apc );
981 return;
985 /* remove the head apc from the queue; the returned object must be released by the caller */
986 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
988 struct thread_apc *apc = NULL;
989 struct list *ptr = list_head( &thread->system_apc );
991 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
992 if (ptr)
994 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
995 list_remove( ptr );
997 return apc;
1000 /* clear an APC queue, cancelling all the APCs on it */
1001 static void clear_apc_queue( struct list *queue )
1003 struct list *ptr;
1005 while ((ptr = list_head( queue )))
1007 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1008 list_remove( &apc->entry );
1009 apc->executed = 1;
1010 wake_up( &apc->obj, 0 );
1011 release_object( apc );
1015 /* add an fd to the inflight list */
1016 /* return list index, or -1 on error */
1017 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1019 int i;
1021 if (server == -1) return -1;
1022 if (client == -1)
1024 close( server );
1025 return -1;
1028 /* first check if we already have an entry for this fd */
1029 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1030 if (thread->inflight[i].client == client)
1032 close( thread->inflight[i].server );
1033 thread->inflight[i].server = server;
1034 return i;
1037 /* now find a free spot to store it */
1038 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1039 if (thread->inflight[i].client == -1)
1041 thread->inflight[i].client = client;
1042 thread->inflight[i].server = server;
1043 return i;
1045 return -1;
1048 /* get an inflight fd and purge it from the list */
1049 /* the fd must be closed when no longer used */
1050 int thread_get_inflight_fd( struct thread *thread, int client )
1052 int i, ret;
1054 if (client == -1) return -1;
1058 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1060 if (thread->inflight[i].client == client)
1062 ret = thread->inflight[i].server;
1063 thread->inflight[i].server = thread->inflight[i].client = -1;
1064 return ret;
1067 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1068 return -1;
1071 /* kill a thread on the spot */
1072 void kill_thread( struct thread *thread, int violent_death )
1074 if (thread->state == TERMINATED) return; /* already killed */
1075 thread->state = TERMINATED;
1076 thread->exit_time = current_time;
1077 if (current == thread) current = NULL;
1078 if (debug_level)
1079 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1080 thread->id, thread->exit_code );
1081 if (thread->wait)
1083 while (thread->wait) end_wait( thread );
1084 send_thread_wakeup( thread, 0, thread->exit_code );
1085 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1086 violent_death = 0;
1088 kill_console_processes( thread, 0 );
1089 debug_exit_thread( thread );
1090 abandon_mutexes( thread );
1091 wake_up( &thread->obj, 0 );
1092 if (violent_death) send_thread_signal( thread, SIGQUIT );
1093 cleanup_thread( thread );
1094 remove_process_thread( thread->process, thread );
1095 release_object( thread );
1098 /* copy parts of a context structure */
1099 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1101 assert( to->cpu == from->cpu );
1102 to->flags |= flags;
1103 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1104 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1105 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1106 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1107 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1108 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1111 /* return the context flags that correspond to system regs */
1112 /* (system regs are the ones we can't access on the client side) */
1113 static unsigned int get_context_system_regs( enum cpu_type cpu )
1115 switch (cpu)
1117 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1118 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1119 case CPU_POWERPC: return 0;
1120 case CPU_ARM: return 0;
1121 case CPU_ARM64: return 0;
1123 return 0;
1126 /* trigger a breakpoint event in a given thread */
1127 void break_thread( struct thread *thread )
1129 debug_event_t data;
1131 assert( thread->context );
1133 memset( &data, 0, sizeof(data) );
1134 data.exception.first = 1;
1135 data.exception.exc_code = STATUS_BREAKPOINT;
1136 data.exception.flags = EXCEPTION_CONTINUABLE;
1137 switch (thread->context->cpu)
1139 case CPU_x86:
1140 data.exception.address = thread->context->ctl.i386_regs.eip;
1141 break;
1142 case CPU_x86_64:
1143 data.exception.address = thread->context->ctl.x86_64_regs.rip;
1144 break;
1145 case CPU_POWERPC:
1146 data.exception.address = thread->context->ctl.powerpc_regs.iar;
1147 break;
1148 case CPU_ARM:
1149 data.exception.address = thread->context->ctl.arm_regs.pc;
1150 break;
1151 case CPU_ARM64:
1152 data.exception.address = thread->context->ctl.arm64_regs.pc;
1153 break;
1155 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
1156 thread->debug_break = 0;
1159 /* take a snapshot of currently running threads */
1160 struct thread_snapshot *thread_snap( int *count )
1162 struct thread_snapshot *snapshot, *ptr;
1163 struct thread *thread;
1164 int total = 0;
1166 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1167 if (thread->state != TERMINATED) total++;
1168 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1169 ptr = snapshot;
1170 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1172 if (thread->state == TERMINATED) continue;
1173 ptr->thread = thread;
1174 ptr->count = thread->obj.refcount;
1175 ptr->priority = thread->priority;
1176 grab_object( thread );
1177 ptr++;
1179 *count = total;
1180 return snapshot;
1183 /* gets the current impersonation token */
1184 struct token *thread_get_impersonation_token( struct thread *thread )
1186 if (thread->token)
1187 return thread->token;
1188 else
1189 return thread->process->token;
1192 /* create a new thread */
1193 DECL_HANDLER(new_thread)
1195 struct thread *thread;
1196 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1198 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1200 if (request_fd != -1) close( request_fd );
1201 set_error( STATUS_INVALID_HANDLE );
1202 return;
1205 if ((thread = create_thread( request_fd, current->process )))
1207 if (req->suspend) thread->suspend++;
1208 reply->tid = get_thread_id( thread );
1209 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
1211 /* thread object will be released when the thread gets killed */
1212 return;
1214 kill_thread( thread, 1 );
1218 /* initialize a new thread */
1219 DECL_HANDLER(init_thread)
1221 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1222 struct process *process = current->process;
1223 int wait_fd, reply_fd;
1225 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1227 set_error( STATUS_TOO_MANY_OPENED_FILES );
1228 return;
1230 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1232 set_error( STATUS_TOO_MANY_OPENED_FILES );
1233 goto error;
1236 if (current->reply_fd) /* already initialised */
1238 set_error( STATUS_INVALID_PARAMETER );
1239 goto error;
1242 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1244 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1245 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1246 if (!current->reply_fd || !current->wait_fd) return;
1248 if (!is_valid_address(req->teb))
1250 set_error( STATUS_INVALID_PARAMETER );
1251 return;
1254 current->unix_pid = req->unix_pid;
1255 current->unix_tid = req->unix_tid;
1256 current->teb = req->teb;
1258 if (!process->peb) /* first thread, initialize the process too */
1260 if (!CPU_FLAG(req->cpu) || !(supported_cpus & prefix_cpu_mask & CPU_FLAG(req->cpu)))
1262 if (!(supported_cpus & CPU_64BIT_MASK))
1263 set_error( STATUS_NOT_SUPPORTED );
1264 else
1265 set_error( STATUS_NOT_REGISTRY_FILE ); /* server supports it but not the prefix */
1266 return;
1268 process->unix_pid = current->unix_pid;
1269 process->peb = req->entry;
1270 process->cpu = req->cpu;
1271 reply->info_size = init_process( current );
1272 if (!process->parent)
1273 process->affinity = current->affinity = get_thread_affinity( current );
1274 else
1275 set_thread_affinity( current, current->affinity );
1277 else
1279 if (req->cpu != process->cpu)
1281 set_error( STATUS_INVALID_PARAMETER );
1282 return;
1284 if (process->unix_pid != current->unix_pid)
1285 process->unix_pid = -1; /* can happen with linuxthreads */
1286 stop_thread_if_suspended( current );
1287 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1288 set_thread_affinity( current, current->affinity );
1290 debug_level = max( debug_level, req->debug_level );
1292 reply->pid = get_process_id( process );
1293 reply->tid = get_thread_id( current );
1294 reply->version = SERVER_PROTOCOL_VERSION;
1295 reply->server_start = server_start_time;
1296 reply->all_cpus = supported_cpus & prefix_cpu_mask;
1297 return;
1299 error:
1300 if (reply_fd != -1) close( reply_fd );
1301 if (wait_fd != -1) close( wait_fd );
1304 /* terminate a thread */
1305 DECL_HANDLER(terminate_thread)
1307 struct thread *thread;
1309 reply->self = 0;
1310 reply->last = 0;
1311 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1313 thread->exit_code = req->exit_code;
1314 if (thread != current) kill_thread( thread, 1 );
1315 else
1317 reply->self = 1;
1318 reply->last = (thread->process->running_threads == 1);
1320 release_object( thread );
1324 /* open a handle to a thread */
1325 DECL_HANDLER(open_thread)
1327 struct thread *thread = get_thread_from_id( req->tid );
1329 reply->handle = 0;
1330 if (thread)
1332 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1333 release_object( thread );
1337 /* fetch information about a thread */
1338 DECL_HANDLER(get_thread_info)
1340 struct thread *thread;
1341 obj_handle_t handle = req->handle;
1343 if (!handle) thread = get_thread_from_id( req->tid_in );
1344 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
1346 if (thread)
1348 reply->pid = get_process_id( thread->process );
1349 reply->tid = get_thread_id( thread );
1350 reply->teb = thread->teb;
1351 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1352 reply->priority = thread->priority;
1353 reply->affinity = thread->affinity;
1354 reply->creation_time = thread->creation_time;
1355 reply->exit_time = thread->exit_time;
1356 reply->last = thread->process->running_threads == 1;
1358 release_object( thread );
1362 /* set information about a thread */
1363 DECL_HANDLER(set_thread_info)
1365 struct thread *thread;
1367 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1369 set_thread_info( thread, req );
1370 release_object( thread );
1374 /* suspend a thread */
1375 DECL_HANDLER(suspend_thread)
1377 struct thread *thread;
1379 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1381 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1382 else reply->count = suspend_thread( thread );
1383 release_object( thread );
1387 /* resume a thread */
1388 DECL_HANDLER(resume_thread)
1390 struct thread *thread;
1392 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1394 reply->count = resume_thread( thread );
1395 release_object( thread );
1399 /* select on a handle list */
1400 DECL_HANDLER(select)
1402 select_op_t select_op;
1403 data_size_t op_size;
1404 struct thread_apc *apc;
1405 const apc_result_t *result = get_req_data();
1407 if (get_req_data_size() < sizeof(*result))
1409 set_error( STATUS_INVALID_PARAMETER );
1410 return;
1412 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1413 memset( &select_op, 0, sizeof(select_op) );
1414 memcpy( &select_op, result + 1, op_size );
1416 /* first store results of previous apc */
1417 if (req->prev_apc)
1419 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1420 0, &thread_apc_ops ))) return;
1421 apc->result = *result;
1422 apc->executed = 1;
1423 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1425 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1426 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1427 close_handle( current->process, apc->result.create_thread.handle );
1428 apc->result.create_thread.handle = handle;
1429 clear_error(); /* ignore errors from the above calls */
1431 else if (apc->result.type == APC_ASYNC_IO)
1433 if (apc->owner)
1434 async_set_result( apc->owner, apc->result.async_io.status,
1435 apc->result.async_io.total, apc->result.async_io.apc );
1437 wake_up( &apc->obj, 0 );
1438 close_handle( current->process, req->prev_apc );
1439 release_object( apc );
1442 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1444 if (get_error() == STATUS_USER_APC)
1446 for (;;)
1448 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1449 break;
1450 /* Optimization: ignore APC_NONE calls, they are only used to
1451 * wake up a thread, but since we got here the thread woke up already.
1453 if (apc->call.type != APC_NONE)
1455 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1456 reply->call = apc->call;
1457 release_object( apc );
1458 break;
1460 apc->executed = 1;
1461 wake_up( &apc->obj, 0 );
1462 release_object( apc );
1467 /* queue an APC for a thread or process */
1468 DECL_HANDLER(queue_apc)
1470 struct thread *thread = NULL;
1471 struct process *process = NULL;
1472 struct thread_apc *apc;
1474 if (!(apc = create_apc( NULL, &req->call ))) return;
1476 switch (apc->call.type)
1478 case APC_NONE:
1479 case APC_USER:
1480 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1481 break;
1482 case APC_VIRTUAL_ALLOC:
1483 case APC_VIRTUAL_FREE:
1484 case APC_VIRTUAL_PROTECT:
1485 case APC_VIRTUAL_FLUSH:
1486 case APC_VIRTUAL_LOCK:
1487 case APC_VIRTUAL_UNLOCK:
1488 case APC_UNMAP_VIEW:
1489 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1490 break;
1491 case APC_VIRTUAL_QUERY:
1492 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1493 break;
1494 case APC_MAP_VIEW:
1495 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1496 if (process && process != current->process)
1498 /* duplicate the handle into the target process */
1499 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1500 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1501 if (handle) apc->call.map_view.handle = handle;
1502 else
1504 release_object( process );
1505 process = NULL;
1508 break;
1509 case APC_CREATE_THREAD:
1510 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1511 break;
1512 default:
1513 set_error( STATUS_INVALID_PARAMETER );
1514 break;
1517 if (thread)
1519 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1520 release_object( thread );
1522 else if (process)
1524 reply->self = (process == current->process);
1525 if (!reply->self)
1527 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1528 if (handle)
1530 if (queue_apc( process, NULL, apc ))
1532 apc->caller = (struct thread *)grab_object( current );
1533 reply->handle = handle;
1535 else
1537 close_handle( current->process, handle );
1538 set_error( STATUS_PROCESS_IS_TERMINATING );
1542 release_object( process );
1545 release_object( apc );
1548 /* Get the result of an APC call */
1549 DECL_HANDLER(get_apc_result)
1551 struct thread_apc *apc;
1553 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1554 0, &thread_apc_ops ))) return;
1555 if (!apc->executed) set_error( STATUS_PENDING );
1556 else
1558 reply->result = apc->result;
1559 /* close the handle directly to avoid an extra round-trip */
1560 close_handle( current->process, req->handle );
1562 release_object( apc );
1565 /* retrieve the current context of a thread */
1566 DECL_HANDLER(get_thread_context)
1568 struct thread *thread;
1569 context_t *context;
1571 if (get_reply_max_size() < sizeof(context_t))
1573 set_error( STATUS_INVALID_PARAMETER );
1574 return;
1576 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1577 reply->self = (thread == current);
1579 if (thread != current && !thread->context)
1581 /* thread is not suspended, retry (if it's still running) */
1582 if (thread->state == RUNNING)
1584 set_error( STATUS_PENDING );
1585 if (req->suspend)
1587 release_object( thread );
1588 /* make sure we have suspend access */
1589 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1590 suspend_thread( thread );
1593 else set_error( STATUS_UNSUCCESSFUL );
1595 else if ((context = set_reply_data_size( sizeof(context_t) )))
1597 unsigned int flags = get_context_system_regs( thread->process->cpu );
1599 memset( context, 0, sizeof(context_t) );
1600 context->cpu = thread->process->cpu;
1601 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1602 if (flags) get_thread_context( thread, context, flags );
1604 release_object( thread );
1607 /* set the current context of a thread */
1608 DECL_HANDLER(set_thread_context)
1610 struct thread *thread;
1611 const context_t *context = get_req_data();
1613 if (get_req_data_size() < sizeof(context_t))
1615 set_error( STATUS_INVALID_PARAMETER );
1616 return;
1618 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1619 reply->self = (thread == current);
1621 if (thread != current && !thread->context)
1623 /* thread is not suspended, retry (if it's still running) */
1624 if (thread->state == RUNNING)
1626 set_error( STATUS_PENDING );
1627 if (req->suspend)
1629 release_object( thread );
1630 /* make sure we have suspend access */
1631 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1632 suspend_thread( thread );
1635 else set_error( STATUS_UNSUCCESSFUL );
1637 else if (context->cpu == thread->process->cpu)
1639 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1640 unsigned int client_flags = context->flags & ~system_flags;
1642 if (system_flags) set_thread_context( thread, context, system_flags );
1643 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1645 else set_error( STATUS_INVALID_PARAMETER );
1647 release_object( thread );
1650 /* retrieve the suspended context of a thread */
1651 DECL_HANDLER(get_suspend_context)
1653 if (get_reply_max_size() < sizeof(context_t))
1655 set_error( STATUS_INVALID_PARAMETER );
1656 return;
1659 if (current->suspend_context)
1661 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1662 if (current->context == current->suspend_context)
1664 current->context = NULL;
1665 stop_thread_if_suspended( current );
1667 current->suspend_context = NULL;
1669 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1672 /* store the suspended context of a thread */
1673 DECL_HANDLER(set_suspend_context)
1675 const context_t *context = get_req_data();
1677 if (get_req_data_size() < sizeof(context_t))
1679 set_error( STATUS_INVALID_PARAMETER );
1680 return;
1683 if (current->context || context->cpu != current->process->cpu)
1685 /* nested suspend or exception, shouldn't happen */
1686 set_error( STATUS_INVALID_PARAMETER );
1688 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1690 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1691 current->context = current->suspend_context;
1692 if (current->debug_break) break_thread( current );
1696 /* fetch a selector entry for a thread */
1697 DECL_HANDLER(get_selector_entry)
1699 struct thread *thread;
1700 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1702 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1703 release_object( thread );