dwrite: Use E_NOT_SUFFICIENT_BUFFER definition.
[wine.git] / server / thread.c
blob50d694098b846aaea2641c699cd091016b72f487
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 |= (affinity_t)1 << i;
455 #endif
456 if (!mask) mask = ~(affinity_t)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 and -1 on error */
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 */
718 if (!count) count = -1;
719 break;
722 return count;
725 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
726 int wake_thread_queue_entry( struct wait_queue_entry *entry )
728 struct thread_wait *wait = entry->wait;
729 struct thread *thread = wait->thread;
730 int signaled;
731 client_ptr_t cookie;
733 if (thread->wait != wait) return 0; /* not the current wait */
734 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
736 assert( wait->select != SELECT_WAIT_ALL );
738 signaled = entry - wait->queues;
739 entry->obj->ops->satisfied( entry->obj, entry );
740 if (wait->abandoned) signaled += STATUS_ABANDONED_WAIT_0;
742 cookie = wait->cookie;
743 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
744 end_wait( thread );
746 if (send_thread_wakeup( thread, cookie, signaled ) != -1)
747 wake_thread( thread ); /* check other waits too */
749 return 1;
752 /* thread wait timeout */
753 static void thread_timeout( void *ptr )
755 struct thread_wait *wait = ptr;
756 struct thread *thread = wait->thread;
757 client_ptr_t cookie = wait->cookie;
759 wait->user = NULL;
760 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
761 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
763 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
764 end_wait( thread );
765 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
766 /* check if other objects have become signaled in the meantime */
767 wake_thread( thread );
770 /* try signaling an event flag, a semaphore or a mutex */
771 static int signal_object( obj_handle_t handle )
773 struct object *obj;
774 int ret = 0;
776 obj = get_handle_obj( current->process, handle, 0, NULL );
777 if (obj)
779 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
780 release_object( obj );
782 return ret;
785 /* select on a list of handles */
786 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
787 int flags, timeout_t timeout )
789 int ret;
790 unsigned int count;
791 struct object *object;
793 if (timeout <= 0) timeout = current_time - timeout;
795 switch (select_op->op)
797 case SELECT_NONE:
798 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
799 break;
801 case SELECT_WAIT:
802 case SELECT_WAIT_ALL:
803 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
804 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
806 set_error( STATUS_INVALID_PARAMETER );
807 return 0;
809 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
810 return timeout;
811 break;
813 case SELECT_SIGNAL_AND_WAIT:
814 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
815 return timeout;
816 if (select_op->signal_and_wait.signal)
818 if (!signal_object( select_op->signal_and_wait.signal ))
820 end_wait( current );
821 return timeout;
823 /* check if we woke ourselves up */
824 if (!current->wait) return timeout;
826 break;
828 case SELECT_KEYED_EVENT_WAIT:
829 case SELECT_KEYED_EVENT_RELEASE:
830 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
831 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
832 if (!object) return timeout;
833 ret = wait_on( select_op, 1, &object, flags, timeout );
834 release_object( object );
835 if (!ret) return timeout;
836 current->wait->key = select_op->keyed_event.key;
837 break;
839 default:
840 set_error( STATUS_INVALID_PARAMETER );
841 return 0;
844 if ((ret = check_wait( current )) != -1)
846 /* condition is already satisfied */
847 end_wait( current );
848 set_error( ret );
849 return timeout;
852 /* now we need to wait */
853 if (current->wait->timeout != TIMEOUT_INFINITE)
855 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
856 thread_timeout, current->wait )))
858 end_wait( current );
859 return timeout;
862 current->wait->cookie = cookie;
863 set_error( STATUS_PENDING );
864 return timeout;
867 /* attempt to wake threads sleeping on the object wait queue */
868 void wake_up( struct object *obj, int max )
870 struct list *ptr;
871 int ret;
873 LIST_FOR_EACH( ptr, &obj->wait_queue )
875 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
876 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
877 if (ret > 0 && max && !--max) break;
878 /* restart at the head of the list since a wake up can change the object wait queue */
879 ptr = &obj->wait_queue;
883 /* return the apc queue to use for a given apc type */
884 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
886 switch(type)
888 case APC_NONE:
889 case APC_USER:
890 case APC_TIMER:
891 return &thread->user_apc;
892 default:
893 return &thread->system_apc;
897 /* check if thread is currently waiting for a (system) apc */
898 static inline int is_in_apc_wait( struct thread *thread )
900 return (thread->process->suspend || thread->suspend ||
901 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
904 /* queue an existing APC to a given thread */
905 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
907 struct list *queue;
909 if (!thread) /* find a suitable thread inside the process */
911 struct thread *candidate;
913 /* first try to find a waiting thread */
914 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
916 if (candidate->state == TERMINATED) continue;
917 if (is_in_apc_wait( candidate ))
919 thread = candidate;
920 break;
923 if (!thread)
925 /* then use the first one that accepts a signal */
926 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
928 if (send_thread_signal( candidate, SIGUSR1 ))
930 thread = candidate;
931 break;
935 if (!thread) return 0; /* nothing found */
936 queue = get_apc_queue( thread, apc->call.type );
938 else
940 if (thread->state == TERMINATED) return 0;
941 queue = get_apc_queue( thread, apc->call.type );
942 /* send signal for system APCs if needed */
943 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
945 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
947 /* cancel a possible previous APC with the same owner */
948 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
951 grab_object( apc );
952 list_add_tail( queue, &apc->entry );
953 if (!list_prev( queue, &apc->entry )) /* first one */
954 wake_thread( thread );
956 return 1;
959 /* queue an async procedure call */
960 int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data )
962 struct thread_apc *apc;
963 int ret = 0;
965 if ((apc = create_apc( owner, call_data )))
967 ret = queue_apc( NULL, thread, apc );
968 release_object( apc );
970 return ret;
973 /* cancel the async procedure call owned by a specific object */
974 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
976 struct thread_apc *apc;
977 struct list *queue = get_apc_queue( thread, type );
979 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
981 if (apc->owner != owner) continue;
982 list_remove( &apc->entry );
983 apc->executed = 1;
984 wake_up( &apc->obj, 0 );
985 release_object( apc );
986 return;
990 /* remove the head apc from the queue; the returned object must be released by the caller */
991 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
993 struct thread_apc *apc = NULL;
994 struct list *ptr = list_head( &thread->system_apc );
996 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
997 if (ptr)
999 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1000 list_remove( ptr );
1002 return apc;
1005 /* clear an APC queue, cancelling all the APCs on it */
1006 static void clear_apc_queue( struct list *queue )
1008 struct list *ptr;
1010 while ((ptr = list_head( queue )))
1012 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1013 list_remove( &apc->entry );
1014 apc->executed = 1;
1015 wake_up( &apc->obj, 0 );
1016 release_object( apc );
1020 /* add an fd to the inflight list */
1021 /* return list index, or -1 on error */
1022 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1024 int i;
1026 if (server == -1) return -1;
1027 if (client == -1)
1029 close( server );
1030 return -1;
1033 /* first check if we already have an entry for this fd */
1034 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1035 if (thread->inflight[i].client == client)
1037 close( thread->inflight[i].server );
1038 thread->inflight[i].server = server;
1039 return i;
1042 /* now find a free spot to store it */
1043 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1044 if (thread->inflight[i].client == -1)
1046 thread->inflight[i].client = client;
1047 thread->inflight[i].server = server;
1048 return i;
1050 return -1;
1053 /* get an inflight fd and purge it from the list */
1054 /* the fd must be closed when no longer used */
1055 int thread_get_inflight_fd( struct thread *thread, int client )
1057 int i, ret;
1059 if (client == -1) return -1;
1063 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1065 if (thread->inflight[i].client == client)
1067 ret = thread->inflight[i].server;
1068 thread->inflight[i].server = thread->inflight[i].client = -1;
1069 return ret;
1072 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1073 return -1;
1076 /* kill a thread on the spot */
1077 void kill_thread( struct thread *thread, int violent_death )
1079 if (thread->state == TERMINATED) return; /* already killed */
1080 thread->state = TERMINATED;
1081 thread->exit_time = current_time;
1082 if (current == thread) current = NULL;
1083 if (debug_level)
1084 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1085 thread->id, thread->exit_code );
1086 if (thread->wait)
1088 while (thread->wait) end_wait( thread );
1089 send_thread_wakeup( thread, 0, thread->exit_code );
1090 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1091 violent_death = 0;
1093 kill_console_processes( thread, 0 );
1094 debug_exit_thread( thread );
1095 abandon_mutexes( thread );
1096 wake_up( &thread->obj, 0 );
1097 if (violent_death) send_thread_signal( thread, SIGQUIT );
1098 cleanup_thread( thread );
1099 remove_process_thread( thread->process, thread );
1100 release_object( thread );
1103 /* copy parts of a context structure */
1104 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1106 assert( to->cpu == from->cpu );
1107 to->flags |= flags;
1108 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1109 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1110 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1111 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1112 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1113 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1116 /* return the context flags that correspond to system regs */
1117 /* (system regs are the ones we can't access on the client side) */
1118 static unsigned int get_context_system_regs( enum cpu_type cpu )
1120 switch (cpu)
1122 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1123 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1124 case CPU_POWERPC: return 0;
1125 case CPU_ARM: return 0;
1126 case CPU_ARM64: return 0;
1128 return 0;
1131 /* trigger a breakpoint event in a given thread */
1132 void break_thread( struct thread *thread )
1134 debug_event_t data;
1136 assert( thread->context );
1138 memset( &data, 0, sizeof(data) );
1139 data.exception.first = 1;
1140 data.exception.exc_code = STATUS_BREAKPOINT;
1141 data.exception.flags = EXCEPTION_CONTINUABLE;
1142 switch (thread->context->cpu)
1144 case CPU_x86:
1145 data.exception.address = thread->context->ctl.i386_regs.eip;
1146 break;
1147 case CPU_x86_64:
1148 data.exception.address = thread->context->ctl.x86_64_regs.rip;
1149 break;
1150 case CPU_POWERPC:
1151 data.exception.address = thread->context->ctl.powerpc_regs.iar;
1152 break;
1153 case CPU_ARM:
1154 data.exception.address = thread->context->ctl.arm_regs.pc;
1155 break;
1156 case CPU_ARM64:
1157 data.exception.address = thread->context->ctl.arm64_regs.pc;
1158 break;
1160 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
1161 thread->debug_break = 0;
1164 /* take a snapshot of currently running threads */
1165 struct thread_snapshot *thread_snap( int *count )
1167 struct thread_snapshot *snapshot, *ptr;
1168 struct thread *thread;
1169 int total = 0;
1171 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1172 if (thread->state != TERMINATED) total++;
1173 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1174 ptr = snapshot;
1175 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1177 if (thread->state == TERMINATED) continue;
1178 ptr->thread = thread;
1179 ptr->count = thread->obj.refcount;
1180 ptr->priority = thread->priority;
1181 grab_object( thread );
1182 ptr++;
1184 *count = total;
1185 return snapshot;
1188 /* gets the current impersonation token */
1189 struct token *thread_get_impersonation_token( struct thread *thread )
1191 if (thread->token)
1192 return thread->token;
1193 else
1194 return thread->process->token;
1197 /* check if a cpu type can be supported on this server */
1198 int is_cpu_supported( enum cpu_type cpu )
1200 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1202 if (CPU_FLAG(cpu) && (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu))) return 1;
1203 if (!(supported_cpus & prefix_cpu_mask))
1204 set_error( STATUS_NOT_SUPPORTED );
1205 else if (supported_cpus & CPU_FLAG(cpu))
1206 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1207 else
1208 set_error( STATUS_INVALID_IMAGE_FORMAT );
1209 return 0;
1212 /* create a new thread */
1213 DECL_HANDLER(new_thread)
1215 struct thread *thread;
1216 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1218 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1220 if (request_fd != -1) close( request_fd );
1221 set_error( STATUS_INVALID_HANDLE );
1222 return;
1225 if ((thread = create_thread( request_fd, current->process )))
1227 if (req->suspend) thread->suspend++;
1228 reply->tid = get_thread_id( thread );
1229 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
1231 /* thread object will be released when the thread gets killed */
1232 return;
1234 kill_thread( thread, 1 );
1238 /* initialize a new thread */
1239 DECL_HANDLER(init_thread)
1241 struct process *process = current->process;
1242 int wait_fd, reply_fd;
1244 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1246 set_error( STATUS_TOO_MANY_OPENED_FILES );
1247 return;
1249 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1251 set_error( STATUS_TOO_MANY_OPENED_FILES );
1252 goto error;
1255 if (current->reply_fd) /* already initialised */
1257 set_error( STATUS_INVALID_PARAMETER );
1258 goto error;
1261 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1263 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1264 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1265 if (!current->reply_fd || !current->wait_fd) return;
1267 if (!is_valid_address(req->teb))
1269 set_error( STATUS_INVALID_PARAMETER );
1270 return;
1273 current->unix_pid = req->unix_pid;
1274 current->unix_tid = req->unix_tid;
1275 current->teb = req->teb;
1277 if (!process->peb) /* first thread, initialize the process too */
1279 if (!is_cpu_supported( req->cpu )) return;
1280 process->unix_pid = current->unix_pid;
1281 process->peb = req->entry;
1282 process->cpu = req->cpu;
1283 reply->info_size = init_process( current );
1284 if (!process->parent)
1285 process->affinity = current->affinity = get_thread_affinity( current );
1286 else
1287 set_thread_affinity( current, current->affinity );
1289 else
1291 if (req->cpu != process->cpu)
1293 set_error( STATUS_INVALID_PARAMETER );
1294 return;
1296 if (process->unix_pid != current->unix_pid)
1297 process->unix_pid = -1; /* can happen with linuxthreads */
1298 stop_thread_if_suspended( current );
1299 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1300 set_thread_affinity( current, current->affinity );
1302 debug_level = max( debug_level, req->debug_level );
1304 reply->pid = get_process_id( process );
1305 reply->tid = get_thread_id( current );
1306 reply->version = SERVER_PROTOCOL_VERSION;
1307 reply->server_start = server_start_time;
1308 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1309 return;
1311 error:
1312 if (reply_fd != -1) close( reply_fd );
1313 if (wait_fd != -1) close( wait_fd );
1316 /* terminate a thread */
1317 DECL_HANDLER(terminate_thread)
1319 struct thread *thread;
1321 reply->self = 0;
1322 reply->last = 0;
1323 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1325 thread->exit_code = req->exit_code;
1326 if (thread != current) kill_thread( thread, 1 );
1327 else
1329 reply->self = 1;
1330 reply->last = (thread->process->running_threads == 1);
1332 release_object( thread );
1336 /* open a handle to a thread */
1337 DECL_HANDLER(open_thread)
1339 struct thread *thread = get_thread_from_id( req->tid );
1341 reply->handle = 0;
1342 if (thread)
1344 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1345 release_object( thread );
1349 /* fetch information about a thread */
1350 DECL_HANDLER(get_thread_info)
1352 struct thread *thread;
1353 obj_handle_t handle = req->handle;
1355 if (!handle) thread = get_thread_from_id( req->tid_in );
1356 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
1358 if (thread)
1360 reply->pid = get_process_id( thread->process );
1361 reply->tid = get_thread_id( thread );
1362 reply->teb = thread->teb;
1363 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1364 reply->priority = thread->priority;
1365 reply->affinity = thread->affinity;
1366 reply->creation_time = thread->creation_time;
1367 reply->exit_time = thread->exit_time;
1368 reply->last = thread->process->running_threads == 1;
1370 release_object( thread );
1374 /* set information about a thread */
1375 DECL_HANDLER(set_thread_info)
1377 struct thread *thread;
1379 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1381 set_thread_info( thread, req );
1382 release_object( thread );
1386 /* suspend a thread */
1387 DECL_HANDLER(suspend_thread)
1389 struct thread *thread;
1391 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1393 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1394 else reply->count = suspend_thread( thread );
1395 release_object( thread );
1399 /* resume a thread */
1400 DECL_HANDLER(resume_thread)
1402 struct thread *thread;
1404 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1406 reply->count = resume_thread( thread );
1407 release_object( thread );
1411 /* select on a handle list */
1412 DECL_HANDLER(select)
1414 select_op_t select_op;
1415 data_size_t op_size;
1416 struct thread_apc *apc;
1417 const apc_result_t *result = get_req_data();
1419 if (get_req_data_size() < sizeof(*result))
1421 set_error( STATUS_INVALID_PARAMETER );
1422 return;
1424 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1425 memset( &select_op, 0, sizeof(select_op) );
1426 memcpy( &select_op, result + 1, op_size );
1428 /* first store results of previous apc */
1429 if (req->prev_apc)
1431 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1432 0, &thread_apc_ops ))) return;
1433 apc->result = *result;
1434 apc->executed = 1;
1435 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1437 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1438 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1439 close_handle( current->process, apc->result.create_thread.handle );
1440 apc->result.create_thread.handle = handle;
1441 clear_error(); /* ignore errors from the above calls */
1443 else if (apc->result.type == APC_ASYNC_IO)
1445 if (apc->owner)
1446 async_set_result( apc->owner, apc->result.async_io.status,
1447 apc->result.async_io.total, apc->result.async_io.apc );
1449 wake_up( &apc->obj, 0 );
1450 close_handle( current->process, req->prev_apc );
1451 release_object( apc );
1454 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1456 if (get_error() == STATUS_USER_APC)
1458 for (;;)
1460 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1461 break;
1462 /* Optimization: ignore APC_NONE calls, they are only used to
1463 * wake up a thread, but since we got here the thread woke up already.
1465 if (apc->call.type != APC_NONE)
1467 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1468 reply->call = apc->call;
1469 release_object( apc );
1470 break;
1472 apc->executed = 1;
1473 wake_up( &apc->obj, 0 );
1474 release_object( apc );
1479 /* queue an APC for a thread or process */
1480 DECL_HANDLER(queue_apc)
1482 struct thread *thread = NULL;
1483 struct process *process = NULL;
1484 struct thread_apc *apc;
1486 if (!(apc = create_apc( NULL, &req->call ))) return;
1488 switch (apc->call.type)
1490 case APC_NONE:
1491 case APC_USER:
1492 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1493 break;
1494 case APC_VIRTUAL_ALLOC:
1495 case APC_VIRTUAL_FREE:
1496 case APC_VIRTUAL_PROTECT:
1497 case APC_VIRTUAL_FLUSH:
1498 case APC_VIRTUAL_LOCK:
1499 case APC_VIRTUAL_UNLOCK:
1500 case APC_UNMAP_VIEW:
1501 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1502 break;
1503 case APC_VIRTUAL_QUERY:
1504 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1505 break;
1506 case APC_MAP_VIEW:
1507 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1508 if (process && process != current->process)
1510 /* duplicate the handle into the target process */
1511 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1512 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1513 if (handle) apc->call.map_view.handle = handle;
1514 else
1516 release_object( process );
1517 process = NULL;
1520 break;
1521 case APC_CREATE_THREAD:
1522 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1523 break;
1524 default:
1525 set_error( STATUS_INVALID_PARAMETER );
1526 break;
1529 if (thread)
1531 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1532 release_object( thread );
1534 else if (process)
1536 reply->self = (process == current->process);
1537 if (!reply->self)
1539 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1540 if (handle)
1542 if (queue_apc( process, NULL, apc ))
1544 apc->caller = (struct thread *)grab_object( current );
1545 reply->handle = handle;
1547 else
1549 close_handle( current->process, handle );
1550 set_error( STATUS_PROCESS_IS_TERMINATING );
1554 release_object( process );
1557 release_object( apc );
1560 /* Get the result of an APC call */
1561 DECL_HANDLER(get_apc_result)
1563 struct thread_apc *apc;
1565 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1566 0, &thread_apc_ops ))) return;
1567 if (!apc->executed) set_error( STATUS_PENDING );
1568 else
1570 reply->result = apc->result;
1571 /* close the handle directly to avoid an extra round-trip */
1572 close_handle( current->process, req->handle );
1574 release_object( apc );
1577 /* retrieve the current context of a thread */
1578 DECL_HANDLER(get_thread_context)
1580 struct thread *thread;
1581 context_t *context;
1583 if (get_reply_max_size() < sizeof(context_t))
1585 set_error( STATUS_INVALID_PARAMETER );
1586 return;
1588 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1589 reply->self = (thread == current);
1591 if (thread != current && !thread->context)
1593 /* thread is not suspended, retry (if it's still running) */
1594 if (thread->state == RUNNING)
1596 set_error( STATUS_PENDING );
1597 if (req->suspend)
1599 release_object( thread );
1600 /* make sure we have suspend access */
1601 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1602 suspend_thread( thread );
1605 else set_error( STATUS_UNSUCCESSFUL );
1607 else if ((context = set_reply_data_size( sizeof(context_t) )))
1609 unsigned int flags = get_context_system_regs( thread->process->cpu );
1611 memset( context, 0, sizeof(context_t) );
1612 context->cpu = thread->process->cpu;
1613 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1614 if (flags) get_thread_context( thread, context, flags );
1616 release_object( thread );
1619 /* set the current context of a thread */
1620 DECL_HANDLER(set_thread_context)
1622 struct thread *thread;
1623 const context_t *context = get_req_data();
1625 if (get_req_data_size() < sizeof(context_t))
1627 set_error( STATUS_INVALID_PARAMETER );
1628 return;
1630 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1631 reply->self = (thread == current);
1633 if (thread != current && !thread->context)
1635 /* thread is not suspended, retry (if it's still running) */
1636 if (thread->state == RUNNING)
1638 set_error( STATUS_PENDING );
1639 if (req->suspend)
1641 release_object( thread );
1642 /* make sure we have suspend access */
1643 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1644 suspend_thread( thread );
1647 else set_error( STATUS_UNSUCCESSFUL );
1649 else if (context->cpu == thread->process->cpu)
1651 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1652 unsigned int client_flags = context->flags & ~system_flags;
1654 if (system_flags) set_thread_context( thread, context, system_flags );
1655 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1657 else set_error( STATUS_INVALID_PARAMETER );
1659 release_object( thread );
1662 /* retrieve the suspended context of a thread */
1663 DECL_HANDLER(get_suspend_context)
1665 if (get_reply_max_size() < sizeof(context_t))
1667 set_error( STATUS_INVALID_PARAMETER );
1668 return;
1671 if (current->suspend_context)
1673 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1674 if (current->context == current->suspend_context)
1676 current->context = NULL;
1677 stop_thread_if_suspended( current );
1679 current->suspend_context = NULL;
1681 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1684 /* store the suspended context of a thread */
1685 DECL_HANDLER(set_suspend_context)
1687 const context_t *context = get_req_data();
1689 if (get_req_data_size() < sizeof(context_t))
1691 set_error( STATUS_INVALID_PARAMETER );
1692 return;
1695 if (current->context || context->cpu != current->process->cpu)
1697 /* nested suspend or exception, shouldn't happen */
1698 set_error( STATUS_INVALID_PARAMETER );
1700 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1702 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1703 current->context = current->suspend_context;
1704 if (current->debug_break) break_thread( current );
1708 /* fetch a selector entry for a thread */
1709 DECL_HANDLER(get_selector_entry)
1711 struct thread *thread;
1712 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1714 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1715 release_object( thread );