d3d8/tests: Use create_device() in more tests.
[wine.git] / server / thread.c
blob896ce0f573868444d55382ebf214afd3d046161d
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 | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT | THREAD_QUERY_LIMITED_INFORMATION;
335 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
336 THREAD_TERMINATE | THREAD_SUSPEND_RESUME | THREAD_SET_LIMITED_INFORMATION;
337 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
338 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
339 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
342 static void dump_thread_apc( struct object *obj, int verbose )
344 struct thread_apc *apc = (struct thread_apc *)obj;
345 assert( obj->ops == &thread_apc_ops );
347 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
350 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
352 struct thread_apc *apc = (struct thread_apc *)obj;
353 return apc->executed;
356 static void thread_apc_destroy( struct object *obj )
358 struct thread_apc *apc = (struct thread_apc *)obj;
359 if (apc->caller) release_object( apc->caller );
360 if (apc->owner) release_object( apc->owner );
363 /* queue an async procedure call */
364 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
366 struct thread_apc *apc;
368 if ((apc = alloc_object( &thread_apc_ops )))
370 apc->call = *call_data;
371 apc->caller = NULL;
372 apc->owner = owner;
373 apc->executed = 0;
374 apc->result.type = APC_NONE;
375 if (owner) grab_object( owner );
377 return apc;
380 /* get a thread pointer from a thread id (and increment the refcount) */
381 struct thread *get_thread_from_id( thread_id_t id )
383 struct object *obj = get_ptid_entry( id );
385 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
386 set_error( STATUS_INVALID_CID );
387 return NULL;
390 /* get a thread from a handle (and increment the refcount) */
391 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
393 return (struct thread *)get_handle_obj( current->process, handle,
394 access, &thread_ops );
397 /* find a thread from a Unix tid */
398 struct thread *get_thread_from_tid( int tid )
400 struct thread *thread;
402 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
404 if (thread->unix_tid == tid) return thread;
406 return NULL;
409 /* find a thread from a Unix pid */
410 struct thread *get_thread_from_pid( int pid )
412 struct thread *thread;
414 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
416 if (thread->unix_pid == pid) return thread;
418 return NULL;
421 int set_thread_affinity( struct thread *thread, affinity_t affinity )
423 int ret = 0;
424 #ifdef HAVE_SCHED_SETAFFINITY
425 if (thread->unix_tid != -1)
427 cpu_set_t set;
428 int i;
429 affinity_t mask;
431 CPU_ZERO( &set );
432 for (i = 0, mask = 1; mask; i++, mask <<= 1)
433 if (affinity & mask) CPU_SET( i, &set );
435 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
437 #endif
438 if (!ret) thread->affinity = affinity;
439 return ret;
442 affinity_t get_thread_affinity( struct thread *thread )
444 affinity_t mask = 0;
445 #ifdef HAVE_SCHED_SETAFFINITY
446 if (thread->unix_tid != -1)
448 cpu_set_t set;
449 unsigned int i;
451 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
452 for (i = 0; i < 8 * sizeof(mask); i++)
453 if (CPU_ISSET( i, &set )) mask |= 1 << i;
455 #endif
456 if (!mask) mask = ~0;
457 return mask;
460 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
461 #define THREAD_PRIORITY_REALTIME_LOWEST -7
463 /* set all information about a thread */
464 static void set_thread_info( struct thread *thread,
465 const struct set_thread_info_request *req )
467 if (req->mask & SET_THREAD_INFO_PRIORITY)
469 int max = THREAD_PRIORITY_HIGHEST;
470 int min = THREAD_PRIORITY_LOWEST;
471 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
473 max = THREAD_PRIORITY_REALTIME_HIGHEST;
474 min = THREAD_PRIORITY_REALTIME_LOWEST;
476 if ((req->priority >= min && req->priority <= max) ||
477 req->priority == THREAD_PRIORITY_IDLE ||
478 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
479 thread->priority = req->priority;
480 else
481 set_error( STATUS_INVALID_PARAMETER );
483 if (req->mask & SET_THREAD_INFO_AFFINITY)
485 if ((req->affinity & thread->process->affinity) != req->affinity)
486 set_error( STATUS_INVALID_PARAMETER );
487 else if (thread->state == TERMINATED)
488 set_error( STATUS_THREAD_IS_TERMINATING );
489 else if (set_thread_affinity( thread, req->affinity ))
490 file_set_error();
492 if (req->mask & SET_THREAD_INFO_TOKEN)
493 security_set_thread_token( thread, req->token );
496 /* stop a thread (at the Unix level) */
497 void stop_thread( struct thread *thread )
499 if (thread->context) return; /* already inside a debug event, no need for a signal */
500 /* can't stop a thread while initialisation is in progress */
501 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
504 /* stop a thread if it's supposed to be suspended */
505 void stop_thread_if_suspended( struct thread *thread )
507 if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
510 /* suspend a thread */
511 static int suspend_thread( struct thread *thread )
513 int old_count = thread->suspend;
514 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
516 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
518 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
519 return old_count;
522 /* resume a thread */
523 static int resume_thread( struct thread *thread )
525 int old_count = thread->suspend;
526 if (thread->suspend > 0)
528 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
530 return old_count;
533 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
534 int add_queue( struct object *obj, struct wait_queue_entry *entry )
536 grab_object( obj );
537 entry->obj = obj;
538 list_add_tail( &obj->wait_queue, &entry->entry );
539 return 1;
542 /* remove a thread from an object wait queue */
543 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
545 list_remove( &entry->entry );
546 release_object( obj );
549 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
551 return entry->wait->thread;
554 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
556 return entry->wait->select;
559 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
561 return entry->wait->key;
564 void make_wait_abandoned( struct wait_queue_entry *entry )
566 entry->wait->abandoned = 1;
569 /* finish waiting */
570 static void end_wait( struct thread *thread )
572 struct thread_wait *wait = thread->wait;
573 struct wait_queue_entry *entry;
574 int i;
576 assert( wait );
577 thread->wait = wait->next;
578 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
579 entry->obj->ops->remove_queue( entry->obj, entry );
580 if (wait->user) remove_timeout_user( wait->user );
581 free( wait );
584 /* build the thread wait structure */
585 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
586 int flags, timeout_t timeout )
588 struct thread_wait *wait;
589 struct wait_queue_entry *entry;
590 unsigned int i;
592 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
593 wait->next = current->wait;
594 wait->thread = current;
595 wait->count = count;
596 wait->flags = flags;
597 wait->select = select_op->op;
598 wait->user = NULL;
599 wait->timeout = timeout;
600 wait->abandoned = 0;
601 current->wait = wait;
603 for (i = 0, entry = wait->queues; i < count; i++, entry++)
605 struct object *obj = objects[i];
606 entry->wait = wait;
607 if (!obj->ops->add_queue( obj, entry ))
609 wait->count = i;
610 end_wait( current );
611 return 0;
614 return 1;
617 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
618 int flags, timeout_t timeout )
620 struct object *objects[MAXIMUM_WAIT_OBJECTS];
621 unsigned int i;
622 int ret = 0;
624 assert( count <= MAXIMUM_WAIT_OBJECTS );
626 for (i = 0; i < count; i++)
627 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
628 break;
630 if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
632 while (i > 0) release_object( objects[--i] );
633 return ret;
636 /* check if the thread waiting condition is satisfied */
637 static int check_wait( struct thread *thread )
639 int i;
640 struct thread_wait *wait = thread->wait;
641 struct wait_queue_entry *entry;
643 assert( wait );
645 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
646 return STATUS_USER_APC;
648 /* Suspended threads may not acquire locks, but they can run system APCs */
649 if (thread->process->suspend + thread->suspend > 0) return -1;
651 if (wait->select == SELECT_WAIT_ALL)
653 int not_ok = 0;
654 /* Note: we must check them all anyway, as some objects may
655 * want to do something when signaled, even if others are not */
656 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
657 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
658 if (not_ok) goto other_checks;
659 /* Wait satisfied: tell it to all objects */
660 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
661 entry->obj->ops->satisfied( entry->obj, entry );
662 return wait->abandoned ? STATUS_ABANDONED_WAIT_0 : STATUS_WAIT_0;
664 else
666 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
668 if (!entry->obj->ops->signaled( entry->obj, entry )) continue;
669 /* Wait satisfied: tell it to the object */
670 entry->obj->ops->satisfied( entry->obj, entry );
671 if (wait->abandoned) i += STATUS_ABANDONED_WAIT_0;
672 return i;
676 other_checks:
677 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
678 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
679 return -1;
682 /* send the wakeup signal to a thread */
683 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
685 struct wake_up_reply reply;
686 int ret;
688 memset( &reply, 0, sizeof(reply) );
689 reply.cookie = cookie;
690 reply.signaled = signaled;
691 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
692 return 0;
693 if (ret >= 0)
694 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
695 else if (errno == EPIPE)
696 kill_thread( thread, 0 ); /* normal death */
697 else
698 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
699 return -1;
702 /* attempt to wake up a thread */
703 /* return >0 if OK, 0 if the wait condition is still not satisfied */
704 int wake_thread( struct thread *thread )
706 int signaled, count;
707 client_ptr_t cookie;
709 for (count = 0; thread->wait; count++)
711 if ((signaled = check_wait( thread )) == -1) break;
713 cookie = thread->wait->cookie;
714 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
715 end_wait( thread );
716 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
717 break;
719 return count;
722 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
723 int wake_thread_queue_entry( struct wait_queue_entry *entry )
725 struct thread_wait *wait = entry->wait;
726 struct thread *thread = wait->thread;
727 int signaled;
728 client_ptr_t cookie;
730 if (thread->wait != wait) return 0; /* not the current wait */
731 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
733 assert( wait->select != SELECT_WAIT_ALL );
735 signaled = entry - wait->queues;
736 entry->obj->ops->satisfied( entry->obj, entry );
737 if (wait->abandoned) signaled += STATUS_ABANDONED_WAIT_0;
739 cookie = wait->cookie;
740 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
741 end_wait( thread );
743 if (send_thread_wakeup( thread, cookie, signaled ) != -1)
744 wake_thread( thread ); /* check other waits too */
746 return 1;
749 /* thread wait timeout */
750 static void thread_timeout( void *ptr )
752 struct thread_wait *wait = ptr;
753 struct thread *thread = wait->thread;
754 client_ptr_t cookie = wait->cookie;
756 wait->user = NULL;
757 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
758 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
760 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
761 end_wait( thread );
762 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
763 /* check if other objects have become signaled in the meantime */
764 wake_thread( thread );
767 /* try signaling an event flag, a semaphore or a mutex */
768 static int signal_object( obj_handle_t handle )
770 struct object *obj;
771 int ret = 0;
773 obj = get_handle_obj( current->process, handle, 0, NULL );
774 if (obj)
776 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
777 release_object( obj );
779 return ret;
782 /* select on a list of handles */
783 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
784 int flags, timeout_t timeout )
786 int ret;
787 unsigned int count;
788 struct object *object;
790 if (timeout <= 0) timeout = current_time - timeout;
792 switch (select_op->op)
794 case SELECT_NONE:
795 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
796 break;
798 case SELECT_WAIT:
799 case SELECT_WAIT_ALL:
800 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
801 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
803 set_error( STATUS_INVALID_PARAMETER );
804 return 0;
806 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
807 return timeout;
808 break;
810 case SELECT_SIGNAL_AND_WAIT:
811 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
812 return timeout;
813 if (select_op->signal_and_wait.signal)
815 if (!signal_object( select_op->signal_and_wait.signal ))
817 end_wait( current );
818 return timeout;
820 /* check if we woke ourselves up */
821 if (!current->wait) return timeout;
823 break;
825 case SELECT_KEYED_EVENT_WAIT:
826 case SELECT_KEYED_EVENT_RELEASE:
827 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
828 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
829 if (!object) return timeout;
830 ret = wait_on( select_op, 1, &object, flags, timeout );
831 release_object( object );
832 if (!ret) return timeout;
833 current->wait->key = select_op->keyed_event.key;
834 break;
836 default:
837 set_error( STATUS_INVALID_PARAMETER );
838 return 0;
841 if ((ret = check_wait( current )) != -1)
843 /* condition is already satisfied */
844 end_wait( current );
845 set_error( ret );
846 return timeout;
849 /* now we need to wait */
850 if (current->wait->timeout != TIMEOUT_INFINITE)
852 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
853 thread_timeout, current->wait )))
855 end_wait( current );
856 return timeout;
859 current->wait->cookie = cookie;
860 set_error( STATUS_PENDING );
861 return timeout;
864 /* attempt to wake threads sleeping on the object wait queue */
865 void wake_up( struct object *obj, int max )
867 struct list *ptr;
869 LIST_FOR_EACH( ptr, &obj->wait_queue )
871 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
872 if (!wake_thread( get_wait_queue_thread( entry ))) continue;
873 if (max && !--max) break;
874 /* restart at the head of the list since a wake up can change the object wait queue */
875 ptr = &obj->wait_queue;
879 /* return the apc queue to use for a given apc type */
880 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
882 switch(type)
884 case APC_NONE:
885 case APC_USER:
886 case APC_TIMER:
887 return &thread->user_apc;
888 default:
889 return &thread->system_apc;
893 /* check if thread is currently waiting for a (system) apc */
894 static inline int is_in_apc_wait( struct thread *thread )
896 return (thread->process->suspend || thread->suspend ||
897 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
900 /* queue an existing APC to a given thread */
901 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
903 struct list *queue;
905 if (!thread) /* find a suitable thread inside the process */
907 struct thread *candidate;
909 /* first try to find a waiting thread */
910 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
912 if (candidate->state == TERMINATED) continue;
913 if (is_in_apc_wait( candidate ))
915 thread = candidate;
916 break;
919 if (!thread)
921 /* then use the first one that accepts a signal */
922 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
924 if (send_thread_signal( candidate, SIGUSR1 ))
926 thread = candidate;
927 break;
931 if (!thread) return 0; /* nothing found */
932 queue = get_apc_queue( thread, apc->call.type );
934 else
936 if (thread->state == TERMINATED) return 0;
937 queue = get_apc_queue( thread, apc->call.type );
938 /* send signal for system APCs if needed */
939 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
941 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
943 /* cancel a possible previous APC with the same owner */
944 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
947 grab_object( apc );
948 list_add_tail( queue, &apc->entry );
949 if (!list_prev( queue, &apc->entry )) /* first one */
950 wake_thread( thread );
952 return 1;
955 /* queue an async procedure call */
956 int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data )
958 struct thread_apc *apc;
959 int ret = 0;
961 if ((apc = create_apc( owner, call_data )))
963 ret = queue_apc( NULL, thread, apc );
964 release_object( apc );
966 return ret;
969 /* cancel the async procedure call owned by a specific object */
970 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
972 struct thread_apc *apc;
973 struct list *queue = get_apc_queue( thread, type );
975 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
977 if (apc->owner != owner) continue;
978 list_remove( &apc->entry );
979 apc->executed = 1;
980 wake_up( &apc->obj, 0 );
981 release_object( apc );
982 return;
986 /* remove the head apc from the queue; the returned object must be released by the caller */
987 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
989 struct thread_apc *apc = NULL;
990 struct list *ptr = list_head( &thread->system_apc );
992 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
993 if (ptr)
995 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
996 list_remove( ptr );
998 return apc;
1001 /* clear an APC queue, cancelling all the APCs on it */
1002 static void clear_apc_queue( struct list *queue )
1004 struct list *ptr;
1006 while ((ptr = list_head( queue )))
1008 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1009 list_remove( &apc->entry );
1010 apc->executed = 1;
1011 wake_up( &apc->obj, 0 );
1012 release_object( apc );
1016 /* add an fd to the inflight list */
1017 /* return list index, or -1 on error */
1018 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1020 int i;
1022 if (server == -1) return -1;
1023 if (client == -1)
1025 close( server );
1026 return -1;
1029 /* first check if we already have an entry for this fd */
1030 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1031 if (thread->inflight[i].client == client)
1033 close( thread->inflight[i].server );
1034 thread->inflight[i].server = server;
1035 return i;
1038 /* now find a free spot to store it */
1039 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1040 if (thread->inflight[i].client == -1)
1042 thread->inflight[i].client = client;
1043 thread->inflight[i].server = server;
1044 return i;
1046 return -1;
1049 /* get an inflight fd and purge it from the list */
1050 /* the fd must be closed when no longer used */
1051 int thread_get_inflight_fd( struct thread *thread, int client )
1053 int i, ret;
1055 if (client == -1) return -1;
1059 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1061 if (thread->inflight[i].client == client)
1063 ret = thread->inflight[i].server;
1064 thread->inflight[i].server = thread->inflight[i].client = -1;
1065 return ret;
1068 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1069 return -1;
1072 /* kill a thread on the spot */
1073 void kill_thread( struct thread *thread, int violent_death )
1075 if (thread->state == TERMINATED) return; /* already killed */
1076 thread->state = TERMINATED;
1077 thread->exit_time = current_time;
1078 if (current == thread) current = NULL;
1079 if (debug_level)
1080 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1081 thread->id, thread->exit_code );
1082 if (thread->wait)
1084 while (thread->wait) end_wait( thread );
1085 send_thread_wakeup( thread, 0, thread->exit_code );
1086 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1087 violent_death = 0;
1089 kill_console_processes( thread, 0 );
1090 debug_exit_thread( thread );
1091 abandon_mutexes( thread );
1092 wake_up( &thread->obj, 0 );
1093 if (violent_death) send_thread_signal( thread, SIGQUIT );
1094 cleanup_thread( thread );
1095 remove_process_thread( thread->process, thread );
1096 release_object( thread );
1099 /* copy parts of a context structure */
1100 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1102 assert( to->cpu == from->cpu );
1103 to->flags |= flags;
1104 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1105 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1106 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1107 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1108 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1109 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1112 /* return the context flags that correspond to system regs */
1113 /* (system regs are the ones we can't access on the client side) */
1114 static unsigned int get_context_system_regs( enum cpu_type cpu )
1116 switch (cpu)
1118 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1119 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1120 case CPU_POWERPC: return 0;
1121 case CPU_ARM: return 0;
1122 case CPU_ARM64: return 0;
1124 return 0;
1127 /* trigger a breakpoint event in a given thread */
1128 void break_thread( struct thread *thread )
1130 debug_event_t data;
1132 assert( thread->context );
1134 memset( &data, 0, sizeof(data) );
1135 data.exception.first = 1;
1136 data.exception.exc_code = STATUS_BREAKPOINT;
1137 data.exception.flags = EXCEPTION_CONTINUABLE;
1138 switch (thread->context->cpu)
1140 case CPU_x86:
1141 data.exception.address = thread->context->ctl.i386_regs.eip;
1142 break;
1143 case CPU_x86_64:
1144 data.exception.address = thread->context->ctl.x86_64_regs.rip;
1145 break;
1146 case CPU_POWERPC:
1147 data.exception.address = thread->context->ctl.powerpc_regs.iar;
1148 break;
1149 case CPU_ARM:
1150 data.exception.address = thread->context->ctl.arm_regs.pc;
1151 break;
1152 case CPU_ARM64:
1153 data.exception.address = thread->context->ctl.arm64_regs.pc;
1154 break;
1156 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
1157 thread->debug_break = 0;
1160 /* take a snapshot of currently running threads */
1161 struct thread_snapshot *thread_snap( int *count )
1163 struct thread_snapshot *snapshot, *ptr;
1164 struct thread *thread;
1165 int total = 0;
1167 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1168 if (thread->state != TERMINATED) total++;
1169 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1170 ptr = snapshot;
1171 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1173 if (thread->state == TERMINATED) continue;
1174 ptr->thread = thread;
1175 ptr->count = thread->obj.refcount;
1176 ptr->priority = thread->priority;
1177 grab_object( thread );
1178 ptr++;
1180 *count = total;
1181 return snapshot;
1184 /* gets the current impersonation token */
1185 struct token *thread_get_impersonation_token( struct thread *thread )
1187 if (thread->token)
1188 return thread->token;
1189 else
1190 return thread->process->token;
1193 /* check if a cpu type can be supported on this server */
1194 int is_cpu_supported( enum cpu_type cpu )
1196 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1198 if (CPU_FLAG(cpu) && (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu))) return 1;
1199 if (!(supported_cpus & prefix_cpu_mask))
1200 set_error( STATUS_NOT_SUPPORTED );
1201 else if (supported_cpus & CPU_FLAG(cpu))
1202 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1203 else
1204 set_error( STATUS_INVALID_IMAGE_FORMAT );
1205 return 0;
1208 /* create a new thread */
1209 DECL_HANDLER(new_thread)
1211 struct thread *thread;
1212 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1214 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1216 if (request_fd != -1) close( request_fd );
1217 set_error( STATUS_INVALID_HANDLE );
1218 return;
1221 if ((thread = create_thread( request_fd, current->process )))
1223 if (req->suspend) thread->suspend++;
1224 reply->tid = get_thread_id( thread );
1225 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
1227 /* thread object will be released when the thread gets killed */
1228 return;
1230 kill_thread( thread, 1 );
1234 /* initialize a new thread */
1235 DECL_HANDLER(init_thread)
1237 struct process *process = current->process;
1238 int wait_fd, reply_fd;
1240 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1242 set_error( STATUS_TOO_MANY_OPENED_FILES );
1243 return;
1245 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1247 set_error( STATUS_TOO_MANY_OPENED_FILES );
1248 goto error;
1251 if (current->reply_fd) /* already initialised */
1253 set_error( STATUS_INVALID_PARAMETER );
1254 goto error;
1257 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1259 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1260 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1261 if (!current->reply_fd || !current->wait_fd) return;
1263 if (!is_valid_address(req->teb))
1265 set_error( STATUS_INVALID_PARAMETER );
1266 return;
1269 current->unix_pid = req->unix_pid;
1270 current->unix_tid = req->unix_tid;
1271 current->teb = req->teb;
1273 if (!process->peb) /* first thread, initialize the process too */
1275 if (!is_cpu_supported( req->cpu )) return;
1276 process->unix_pid = current->unix_pid;
1277 process->peb = req->entry;
1278 process->cpu = req->cpu;
1279 reply->info_size = init_process( current );
1280 if (!process->parent)
1281 process->affinity = current->affinity = get_thread_affinity( current );
1282 else
1283 set_thread_affinity( current, current->affinity );
1285 else
1287 if (req->cpu != process->cpu)
1289 set_error( STATUS_INVALID_PARAMETER );
1290 return;
1292 if (process->unix_pid != current->unix_pid)
1293 process->unix_pid = -1; /* can happen with linuxthreads */
1294 stop_thread_if_suspended( current );
1295 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1296 set_thread_affinity( current, current->affinity );
1298 debug_level = max( debug_level, req->debug_level );
1300 reply->pid = get_process_id( process );
1301 reply->tid = get_thread_id( current );
1302 reply->version = SERVER_PROTOCOL_VERSION;
1303 reply->server_start = server_start_time;
1304 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1305 return;
1307 error:
1308 if (reply_fd != -1) close( reply_fd );
1309 if (wait_fd != -1) close( wait_fd );
1312 /* terminate a thread */
1313 DECL_HANDLER(terminate_thread)
1315 struct thread *thread;
1317 reply->self = 0;
1318 reply->last = 0;
1319 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1321 thread->exit_code = req->exit_code;
1322 if (thread != current) kill_thread( thread, 1 );
1323 else
1325 reply->self = 1;
1326 reply->last = (thread->process->running_threads == 1);
1328 release_object( thread );
1332 /* open a handle to a thread */
1333 DECL_HANDLER(open_thread)
1335 struct thread *thread = get_thread_from_id( req->tid );
1337 reply->handle = 0;
1338 if (thread)
1340 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1341 release_object( thread );
1345 /* fetch information about a thread */
1346 DECL_HANDLER(get_thread_info)
1348 struct thread *thread;
1349 obj_handle_t handle = req->handle;
1351 if (!handle) thread = get_thread_from_id( req->tid_in );
1352 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
1354 if (thread)
1356 reply->pid = get_process_id( thread->process );
1357 reply->tid = get_thread_id( thread );
1358 reply->teb = thread->teb;
1359 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1360 reply->priority = thread->priority;
1361 reply->affinity = thread->affinity;
1362 reply->creation_time = thread->creation_time;
1363 reply->exit_time = thread->exit_time;
1364 reply->last = thread->process->running_threads == 1;
1366 release_object( thread );
1370 /* set information about a thread */
1371 DECL_HANDLER(set_thread_info)
1373 struct thread *thread;
1375 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1377 set_thread_info( thread, req );
1378 release_object( thread );
1382 /* suspend a thread */
1383 DECL_HANDLER(suspend_thread)
1385 struct thread *thread;
1387 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1389 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1390 else reply->count = suspend_thread( thread );
1391 release_object( thread );
1395 /* resume a thread */
1396 DECL_HANDLER(resume_thread)
1398 struct thread *thread;
1400 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1402 reply->count = resume_thread( thread );
1403 release_object( thread );
1407 /* select on a handle list */
1408 DECL_HANDLER(select)
1410 select_op_t select_op;
1411 data_size_t op_size;
1412 struct thread_apc *apc;
1413 const apc_result_t *result = get_req_data();
1415 if (get_req_data_size() < sizeof(*result))
1417 set_error( STATUS_INVALID_PARAMETER );
1418 return;
1420 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1421 memset( &select_op, 0, sizeof(select_op) );
1422 memcpy( &select_op, result + 1, op_size );
1424 /* first store results of previous apc */
1425 if (req->prev_apc)
1427 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1428 0, &thread_apc_ops ))) return;
1429 apc->result = *result;
1430 apc->executed = 1;
1431 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1433 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1434 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1435 close_handle( current->process, apc->result.create_thread.handle );
1436 apc->result.create_thread.handle = handle;
1437 clear_error(); /* ignore errors from the above calls */
1439 else if (apc->result.type == APC_ASYNC_IO)
1441 if (apc->owner)
1442 async_set_result( apc->owner, apc->result.async_io.status,
1443 apc->result.async_io.total, apc->result.async_io.apc );
1445 wake_up( &apc->obj, 0 );
1446 close_handle( current->process, req->prev_apc );
1447 release_object( apc );
1450 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1452 if (get_error() == STATUS_USER_APC)
1454 for (;;)
1456 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1457 break;
1458 /* Optimization: ignore APC_NONE calls, they are only used to
1459 * wake up a thread, but since we got here the thread woke up already.
1461 if (apc->call.type != APC_NONE)
1463 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1464 reply->call = apc->call;
1465 release_object( apc );
1466 break;
1468 apc->executed = 1;
1469 wake_up( &apc->obj, 0 );
1470 release_object( apc );
1475 /* queue an APC for a thread or process */
1476 DECL_HANDLER(queue_apc)
1478 struct thread *thread = NULL;
1479 struct process *process = NULL;
1480 struct thread_apc *apc;
1482 if (!(apc = create_apc( NULL, &req->call ))) return;
1484 switch (apc->call.type)
1486 case APC_NONE:
1487 case APC_USER:
1488 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1489 break;
1490 case APC_VIRTUAL_ALLOC:
1491 case APC_VIRTUAL_FREE:
1492 case APC_VIRTUAL_PROTECT:
1493 case APC_VIRTUAL_FLUSH:
1494 case APC_VIRTUAL_LOCK:
1495 case APC_VIRTUAL_UNLOCK:
1496 case APC_UNMAP_VIEW:
1497 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1498 break;
1499 case APC_VIRTUAL_QUERY:
1500 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1501 break;
1502 case APC_MAP_VIEW:
1503 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1504 if (process && process != current->process)
1506 /* duplicate the handle into the target process */
1507 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1508 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1509 if (handle) apc->call.map_view.handle = handle;
1510 else
1512 release_object( process );
1513 process = NULL;
1516 break;
1517 case APC_CREATE_THREAD:
1518 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1519 break;
1520 default:
1521 set_error( STATUS_INVALID_PARAMETER );
1522 break;
1525 if (thread)
1527 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1528 release_object( thread );
1530 else if (process)
1532 reply->self = (process == current->process);
1533 if (!reply->self)
1535 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1536 if (handle)
1538 if (queue_apc( process, NULL, apc ))
1540 apc->caller = (struct thread *)grab_object( current );
1541 reply->handle = handle;
1543 else
1545 close_handle( current->process, handle );
1546 set_error( STATUS_PROCESS_IS_TERMINATING );
1550 release_object( process );
1553 release_object( apc );
1556 /* Get the result of an APC call */
1557 DECL_HANDLER(get_apc_result)
1559 struct thread_apc *apc;
1561 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1562 0, &thread_apc_ops ))) return;
1563 if (!apc->executed) set_error( STATUS_PENDING );
1564 else
1566 reply->result = apc->result;
1567 /* close the handle directly to avoid an extra round-trip */
1568 close_handle( current->process, req->handle );
1570 release_object( apc );
1573 /* retrieve the current context of a thread */
1574 DECL_HANDLER(get_thread_context)
1576 struct thread *thread;
1577 context_t *context;
1579 if (get_reply_max_size() < sizeof(context_t))
1581 set_error( STATUS_INVALID_PARAMETER );
1582 return;
1584 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1585 reply->self = (thread == current);
1587 if (thread != current && !thread->context)
1589 /* thread is not suspended, retry (if it's still running) */
1590 if (thread->state == RUNNING)
1592 set_error( STATUS_PENDING );
1593 if (req->suspend)
1595 release_object( thread );
1596 /* make sure we have suspend access */
1597 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1598 suspend_thread( thread );
1601 else set_error( STATUS_UNSUCCESSFUL );
1603 else if ((context = set_reply_data_size( sizeof(context_t) )))
1605 unsigned int flags = get_context_system_regs( thread->process->cpu );
1607 memset( context, 0, sizeof(context_t) );
1608 context->cpu = thread->process->cpu;
1609 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1610 if (flags) get_thread_context( thread, context, flags );
1612 release_object( thread );
1615 /* set the current context of a thread */
1616 DECL_HANDLER(set_thread_context)
1618 struct thread *thread;
1619 const context_t *context = get_req_data();
1621 if (get_req_data_size() < sizeof(context_t))
1623 set_error( STATUS_INVALID_PARAMETER );
1624 return;
1626 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1627 reply->self = (thread == current);
1629 if (thread != current && !thread->context)
1631 /* thread is not suspended, retry (if it's still running) */
1632 if (thread->state == RUNNING)
1634 set_error( STATUS_PENDING );
1635 if (req->suspend)
1637 release_object( thread );
1638 /* make sure we have suspend access */
1639 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1640 suspend_thread( thread );
1643 else set_error( STATUS_UNSUCCESSFUL );
1645 else if (context->cpu == thread->process->cpu)
1647 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1648 unsigned int client_flags = context->flags & ~system_flags;
1650 if (system_flags) set_thread_context( thread, context, system_flags );
1651 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1653 else set_error( STATUS_INVALID_PARAMETER );
1655 release_object( thread );
1658 /* retrieve the suspended context of a thread */
1659 DECL_HANDLER(get_suspend_context)
1661 if (get_reply_max_size() < sizeof(context_t))
1663 set_error( STATUS_INVALID_PARAMETER );
1664 return;
1667 if (current->suspend_context)
1669 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1670 if (current->context == current->suspend_context)
1672 current->context = NULL;
1673 stop_thread_if_suspended( current );
1675 current->suspend_context = NULL;
1677 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1680 /* store the suspended context of a thread */
1681 DECL_HANDLER(set_suspend_context)
1683 const context_t *context = get_req_data();
1685 if (get_req_data_size() < sizeof(context_t))
1687 set_error( STATUS_INVALID_PARAMETER );
1688 return;
1691 if (current->context || context->cpu != current->process->cpu)
1693 /* nested suspend or exception, shouldn't happen */
1694 set_error( STATUS_INVALID_PARAMETER );
1696 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1698 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1699 current->context = current->suspend_context;
1700 if (current->debug_break) break_thread( current );
1704 /* fetch a selector entry for a thread */
1705 DECL_HANDLER(get_selector_entry)
1707 struct thread *thread;
1708 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1710 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1711 release_object( thread );