wined3d: Introduce wined3d_texture_use_pbo().
[wine.git] / server / thread.c
blobca02a8ad34f8fa7b3185138d769c8d49eb16df63
1 /*
2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <time.h>
35 #ifdef HAVE_POLL_H
36 #include <poll.h>
37 #endif
38 #ifdef HAVE_SCHED_H
39 #include <sched.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
45 #include "winternl.h"
47 #include "file.h"
48 #include "handle.h"
49 #include "process.h"
50 #include "thread.h"
51 #include "request.h"
52 #include "user.h"
53 #include "security.h"
56 #ifdef __i386__
57 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86);
58 #elif defined(__x86_64__)
59 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86_64) | CPU_FLAG(CPU_x86);
60 #elif defined(__powerpc__)
61 static const unsigned int supported_cpus = CPU_FLAG(CPU_POWERPC);
62 #elif defined(__arm__)
63 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM);
64 #elif defined(__aarch64__)
65 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM64) | CPU_FLAG(CPU_ARM);
66 #else
67 #error Unsupported CPU
68 #endif
70 /* thread queues */
72 struct thread_wait
74 struct thread_wait *next; /* next wait structure for this thread */
75 struct thread *thread; /* owner thread */
76 int count; /* count of objects */
77 int flags;
78 int abandoned;
79 enum select_op select;
80 client_ptr_t key; /* wait key for keyed events */
81 client_ptr_t cookie; /* magic cookie to return to client */
82 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_link_name, /* link_name */
121 NULL, /* unlink_name */
122 no_open_file, /* open_file */
123 no_close_handle, /* close_handle */
124 thread_apc_destroy /* destroy */
128 /* thread operations */
130 static void dump_thread( struct object *obj, int verbose );
131 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
132 static unsigned int thread_map_access( struct object *obj, unsigned int access );
133 static void thread_poll_event( struct fd *fd, int event );
134 static void destroy_thread( struct object *obj );
136 static const struct object_ops thread_ops =
138 sizeof(struct thread), /* size */
139 dump_thread, /* dump */
140 no_get_type, /* get_type */
141 add_queue, /* add_queue */
142 remove_queue, /* remove_queue */
143 thread_signaled, /* signaled */
144 no_satisfied, /* satisfied */
145 no_signal, /* signal */
146 no_get_fd, /* get_fd */
147 thread_map_access, /* map_access */
148 default_get_sd, /* get_sd */
149 default_set_sd, /* set_sd */
150 no_lookup_name, /* lookup_name */
151 no_link_name, /* link_name */
152 NULL, /* unlink_name */
153 no_open_file, /* open_file */
154 no_close_handle, /* close_handle */
155 destroy_thread /* destroy */
158 static const struct fd_ops thread_fd_ops =
160 NULL, /* get_poll_events */
161 thread_poll_event, /* poll_event */
162 NULL, /* flush */
163 NULL, /* get_fd_type */
164 NULL, /* ioctl */
165 NULL, /* queue_async */
166 NULL, /* reselect_async */
167 NULL /* cancel_async */
170 static struct list thread_list = LIST_INIT(thread_list);
172 /* initialize the structure for a newly allocated thread */
173 static inline void init_thread_structure( struct thread *thread )
175 int i;
177 thread->unix_pid = -1; /* not known yet */
178 thread->unix_tid = -1; /* not known yet */
179 thread->context = NULL;
180 thread->suspend_context = NULL;
181 thread->teb = 0;
182 thread->entry_point = 0;
183 thread->debug_ctx = NULL;
184 thread->debug_event = NULL;
185 thread->debug_break = 0;
186 thread->queue = NULL;
187 thread->wait = NULL;
188 thread->error = 0;
189 thread->req_data = NULL;
190 thread->req_toread = 0;
191 thread->reply_data = NULL;
192 thread->reply_towrite = 0;
193 thread->request_fd = NULL;
194 thread->reply_fd = NULL;
195 thread->wait_fd = NULL;
196 thread->state = RUNNING;
197 thread->exit_code = 0;
198 thread->priority = 0;
199 thread->suspend = 0;
200 thread->desktop_users = 0;
201 thread->token = NULL;
203 thread->creation_time = current_time;
204 thread->exit_time = 0;
206 list_init( &thread->mutex_list );
207 list_init( &thread->system_apc );
208 list_init( &thread->user_apc );
210 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
211 thread->inflight[i].server = thread->inflight[i].client = -1;
214 /* check if address looks valid for a client-side data structure (TEB etc.) */
215 static inline int is_valid_address( client_ptr_t addr )
217 return addr && !(addr % sizeof(int));
220 /* create a new thread */
221 struct thread *create_thread( int fd, struct process *process )
223 struct thread *thread;
225 if (process->is_terminating)
227 close( fd );
228 set_error( STATUS_PROCESS_IS_TERMINATING );
229 return NULL;
232 if (!(thread = alloc_object( &thread_ops )))
234 close( fd );
235 return NULL;
238 init_thread_structure( thread );
240 thread->process = (struct process *)grab_object( process );
241 thread->desktop = process->desktop;
242 thread->affinity = process->affinity;
243 if (!current) current = thread;
245 list_add_head( &thread_list, &thread->entry );
247 if (!(thread->id = alloc_ptid( thread )))
249 close( fd );
250 release_object( thread );
251 return NULL;
253 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
255 release_object( thread );
256 return NULL;
259 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
260 add_process_thread( thread->process, thread );
261 return thread;
264 /* handle a client event */
265 static void thread_poll_event( struct fd *fd, int event )
267 struct thread *thread = get_fd_user( fd );
268 assert( thread->obj.ops == &thread_ops );
270 grab_object( thread );
271 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
272 else if (event & POLLIN) read_request( thread );
273 else if (event & POLLOUT) write_reply( thread );
274 release_object( thread );
277 /* cleanup everything that is no longer needed by a dead thread */
278 /* used by destroy_thread and kill_thread */
279 static void cleanup_thread( struct thread *thread )
281 int i;
283 clear_apc_queue( &thread->system_apc );
284 clear_apc_queue( &thread->user_apc );
285 free( thread->req_data );
286 free( thread->reply_data );
287 if (thread->request_fd) release_object( thread->request_fd );
288 if (thread->reply_fd) release_object( thread->reply_fd );
289 if (thread->wait_fd) release_object( thread->wait_fd );
290 free( thread->suspend_context );
291 cleanup_clipboard_thread(thread);
292 destroy_thread_windows( thread );
293 free_msg_queue( thread );
294 close_thread_desktop( thread );
295 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
297 if (thread->inflight[i].client != -1)
299 close( thread->inflight[i].server );
300 thread->inflight[i].client = thread->inflight[i].server = -1;
303 thread->req_data = NULL;
304 thread->reply_data = NULL;
305 thread->request_fd = NULL;
306 thread->reply_fd = NULL;
307 thread->wait_fd = NULL;
308 thread->context = NULL;
309 thread->suspend_context = NULL;
310 thread->desktop = 0;
313 /* destroy a thread when its refcount is 0 */
314 static void destroy_thread( struct object *obj )
316 struct thread *thread = (struct thread *)obj;
317 assert( obj->ops == &thread_ops );
319 assert( !thread->debug_ctx ); /* cannot still be debugging something */
320 list_remove( &thread->entry );
321 cleanup_thread( thread );
322 release_object( thread->process );
323 if (thread->id) free_ptid( thread->id );
324 if (thread->token) release_object( thread->token );
327 /* dump a thread on stdout for debugging purposes */
328 static void dump_thread( struct object *obj, int verbose )
330 struct thread *thread = (struct thread *)obj;
331 assert( obj->ops == &thread_ops );
333 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
334 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
337 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
339 struct thread *mythread = (struct thread *)obj;
340 return (mythread->state == TERMINATED);
343 static unsigned int thread_map_access( struct object *obj, unsigned int access )
345 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
346 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
347 THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
348 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
349 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
351 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
352 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
354 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
357 static void dump_thread_apc( struct object *obj, int verbose )
359 struct thread_apc *apc = (struct thread_apc *)obj;
360 assert( obj->ops == &thread_apc_ops );
362 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
365 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
367 struct thread_apc *apc = (struct thread_apc *)obj;
368 return apc->executed;
371 static void thread_apc_destroy( struct object *obj )
373 struct thread_apc *apc = (struct thread_apc *)obj;
374 if (apc->caller) release_object( apc->caller );
375 if (apc->owner) release_object( apc->owner );
378 /* queue an async procedure call */
379 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
381 struct thread_apc *apc;
383 if ((apc = alloc_object( &thread_apc_ops )))
385 apc->call = *call_data;
386 apc->caller = NULL;
387 apc->owner = owner;
388 apc->executed = 0;
389 apc->result.type = APC_NONE;
390 if (owner) grab_object( owner );
392 return apc;
395 /* get a thread pointer from a thread id (and increment the refcount) */
396 struct thread *get_thread_from_id( thread_id_t id )
398 struct object *obj = get_ptid_entry( id );
400 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
401 set_error( STATUS_INVALID_CID );
402 return NULL;
405 /* get a thread from a handle (and increment the refcount) */
406 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
408 return (struct thread *)get_handle_obj( current->process, handle,
409 access, &thread_ops );
412 /* find a thread from a Unix tid */
413 struct thread *get_thread_from_tid( int tid )
415 struct thread *thread;
417 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
419 if (thread->unix_tid == tid) return thread;
421 return NULL;
424 /* find a thread from a Unix pid */
425 struct thread *get_thread_from_pid( int pid )
427 struct thread *thread;
429 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
431 if (thread->unix_pid == pid) return thread;
433 return NULL;
436 int set_thread_affinity( struct thread *thread, affinity_t affinity )
438 int ret = 0;
439 #ifdef HAVE_SCHED_SETAFFINITY
440 if (thread->unix_tid != -1)
442 cpu_set_t set;
443 int i;
444 affinity_t mask;
446 CPU_ZERO( &set );
447 for (i = 0, mask = 1; mask; i++, mask <<= 1)
448 if (affinity & mask) CPU_SET( i, &set );
450 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
452 #endif
453 if (!ret) thread->affinity = affinity;
454 return ret;
457 affinity_t get_thread_affinity( struct thread *thread )
459 affinity_t mask = 0;
460 #ifdef HAVE_SCHED_SETAFFINITY
461 if (thread->unix_tid != -1)
463 cpu_set_t set;
464 unsigned int i;
466 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
467 for (i = 0; i < 8 * sizeof(mask); i++)
468 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
470 #endif
471 if (!mask) mask = ~(affinity_t)0;
472 return mask;
475 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
476 #define THREAD_PRIORITY_REALTIME_LOWEST -7
478 /* set all information about a thread */
479 static void set_thread_info( struct thread *thread,
480 const struct set_thread_info_request *req )
482 if (req->mask & SET_THREAD_INFO_PRIORITY)
484 int max = THREAD_PRIORITY_HIGHEST;
485 int min = THREAD_PRIORITY_LOWEST;
486 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
488 max = THREAD_PRIORITY_REALTIME_HIGHEST;
489 min = THREAD_PRIORITY_REALTIME_LOWEST;
491 if ((req->priority >= min && req->priority <= max) ||
492 req->priority == THREAD_PRIORITY_IDLE ||
493 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
494 thread->priority = req->priority;
495 else
496 set_error( STATUS_INVALID_PARAMETER );
498 if (req->mask & SET_THREAD_INFO_AFFINITY)
500 if ((req->affinity & thread->process->affinity) != req->affinity)
501 set_error( STATUS_INVALID_PARAMETER );
502 else if (thread->state == TERMINATED)
503 set_error( STATUS_THREAD_IS_TERMINATING );
504 else if (set_thread_affinity( thread, req->affinity ))
505 file_set_error();
507 if (req->mask & SET_THREAD_INFO_TOKEN)
508 security_set_thread_token( thread, req->token );
509 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
510 thread->entry_point = req->entry_point;
513 /* stop a thread (at the Unix level) */
514 void stop_thread( struct thread *thread )
516 if (thread->context) return; /* already inside a debug event, no need for a signal */
517 /* can't stop a thread while initialisation is in progress */
518 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
521 /* stop a thread if it's supposed to be suspended */
522 void stop_thread_if_suspended( struct thread *thread )
524 if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
527 /* suspend a thread */
528 static int suspend_thread( struct thread *thread )
530 int old_count = thread->suspend;
531 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
533 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
535 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
536 return old_count;
539 /* resume a thread */
540 static int resume_thread( struct thread *thread )
542 int old_count = thread->suspend;
543 if (thread->suspend > 0)
545 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
547 return old_count;
550 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
551 int add_queue( struct object *obj, struct wait_queue_entry *entry )
553 grab_object( obj );
554 entry->obj = obj;
555 list_add_tail( &obj->wait_queue, &entry->entry );
556 return 1;
559 /* remove a thread from an object wait queue */
560 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
562 list_remove( &entry->entry );
563 release_object( obj );
566 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
568 return entry->wait->thread;
571 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
573 return entry->wait->select;
576 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
578 return entry->wait->key;
581 void make_wait_abandoned( struct wait_queue_entry *entry )
583 entry->wait->abandoned = 1;
586 /* finish waiting */
587 static void end_wait( struct thread *thread )
589 struct thread_wait *wait = thread->wait;
590 struct wait_queue_entry *entry;
591 int i;
593 assert( wait );
594 thread->wait = wait->next;
595 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
596 entry->obj->ops->remove_queue( entry->obj, entry );
597 if (wait->user) remove_timeout_user( wait->user );
598 free( wait );
601 /* build the thread wait structure */
602 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
603 int flags, timeout_t timeout )
605 struct thread_wait *wait;
606 struct wait_queue_entry *entry;
607 unsigned int i;
609 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
610 wait->next = current->wait;
611 wait->thread = current;
612 wait->count = count;
613 wait->flags = flags;
614 wait->select = select_op->op;
615 wait->cookie = 0;
616 wait->user = NULL;
617 wait->timeout = timeout;
618 wait->abandoned = 0;
619 current->wait = wait;
621 for (i = 0, entry = wait->queues; i < count; i++, entry++)
623 struct object *obj = objects[i];
624 entry->wait = wait;
625 if (!obj->ops->add_queue( obj, entry ))
627 wait->count = i;
628 end_wait( current );
629 return 0;
632 return 1;
635 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
636 int flags, timeout_t timeout )
638 struct object *objects[MAXIMUM_WAIT_OBJECTS];
639 unsigned int i;
640 int ret = 0;
642 assert( count <= MAXIMUM_WAIT_OBJECTS );
644 for (i = 0; i < count; i++)
645 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
646 break;
648 if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
650 while (i > 0) release_object( objects[--i] );
651 return ret;
654 /* check if the thread waiting condition is satisfied */
655 static int check_wait( struct thread *thread )
657 int i;
658 struct thread_wait *wait = thread->wait;
659 struct wait_queue_entry *entry;
661 assert( wait );
663 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
664 return STATUS_USER_APC;
666 /* Suspended threads may not acquire locks, but they can run system APCs */
667 if (thread->process->suspend + thread->suspend > 0) return -1;
669 if (wait->select == SELECT_WAIT_ALL)
671 int not_ok = 0;
672 /* Note: we must check them all anyway, as some objects may
673 * want to do something when signaled, even if others are not */
674 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
675 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
676 if (not_ok) goto other_checks;
677 /* Wait satisfied: tell it to all objects */
678 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
679 entry->obj->ops->satisfied( entry->obj, entry );
680 return wait->abandoned ? STATUS_ABANDONED_WAIT_0 : STATUS_WAIT_0;
682 else
684 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
686 if (!entry->obj->ops->signaled( entry->obj, entry )) continue;
687 /* Wait satisfied: tell it to the object */
688 entry->obj->ops->satisfied( entry->obj, entry );
689 if (wait->abandoned) i += STATUS_ABANDONED_WAIT_0;
690 return i;
694 other_checks:
695 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
696 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
697 return -1;
700 /* send the wakeup signal to a thread */
701 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
703 struct wake_up_reply reply;
704 int ret;
706 memset( &reply, 0, sizeof(reply) );
707 reply.cookie = cookie;
708 reply.signaled = signaled;
709 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
710 return 0;
711 if (ret >= 0)
712 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
713 else if (errno == EPIPE)
714 kill_thread( thread, 0 ); /* normal death */
715 else
716 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
717 return -1;
720 /* attempt to wake up a thread */
721 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
722 int wake_thread( struct thread *thread )
724 int signaled, count;
725 client_ptr_t cookie;
727 for (count = 0; thread->wait; count++)
729 if ((signaled = check_wait( thread )) == -1) break;
731 cookie = thread->wait->cookie;
732 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
733 end_wait( thread );
734 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
736 if (!count) count = -1;
737 break;
740 return count;
743 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
744 int wake_thread_queue_entry( struct wait_queue_entry *entry )
746 struct thread_wait *wait = entry->wait;
747 struct thread *thread = wait->thread;
748 int signaled;
749 client_ptr_t cookie;
751 if (thread->wait != wait) return 0; /* not the current wait */
752 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
754 assert( wait->select != SELECT_WAIT_ALL );
756 signaled = entry - wait->queues;
757 entry->obj->ops->satisfied( entry->obj, entry );
758 if (wait->abandoned) signaled += STATUS_ABANDONED_WAIT_0;
760 cookie = wait->cookie;
761 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
762 end_wait( thread );
764 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
765 wake_thread( thread ); /* check other waits too */
767 return 1;
770 /* thread wait timeout */
771 static void thread_timeout( void *ptr )
773 struct thread_wait *wait = ptr;
774 struct thread *thread = wait->thread;
775 client_ptr_t cookie = wait->cookie;
777 wait->user = NULL;
778 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
779 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
781 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
782 end_wait( thread );
784 assert( cookie );
785 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
786 /* check if other objects have become signaled in the meantime */
787 wake_thread( thread );
790 /* try signaling an event flag, a semaphore or a mutex */
791 static int signal_object( obj_handle_t handle )
793 struct object *obj;
794 int ret = 0;
796 obj = get_handle_obj( current->process, handle, 0, NULL );
797 if (obj)
799 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
800 release_object( obj );
802 return ret;
805 /* select on a list of handles */
806 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
807 int flags, timeout_t timeout )
809 int ret;
810 unsigned int count;
811 struct object *object;
813 if (timeout <= 0) timeout = current_time - timeout;
815 switch (select_op->op)
817 case SELECT_NONE:
818 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
819 break;
821 case SELECT_WAIT:
822 case SELECT_WAIT_ALL:
823 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
824 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
826 set_error( STATUS_INVALID_PARAMETER );
827 return 0;
829 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
830 return timeout;
831 break;
833 case SELECT_SIGNAL_AND_WAIT:
834 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
835 return timeout;
836 if (select_op->signal_and_wait.signal)
838 if (!signal_object( select_op->signal_and_wait.signal ))
840 end_wait( current );
841 return timeout;
843 /* check if we woke ourselves up */
844 if (!current->wait) return timeout;
846 break;
848 case SELECT_KEYED_EVENT_WAIT:
849 case SELECT_KEYED_EVENT_RELEASE:
850 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
851 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
852 if (!object) return timeout;
853 ret = wait_on( select_op, 1, &object, flags, timeout );
854 release_object( object );
855 if (!ret) return timeout;
856 current->wait->key = select_op->keyed_event.key;
857 break;
859 default:
860 set_error( STATUS_INVALID_PARAMETER );
861 return 0;
864 if ((ret = check_wait( current )) != -1)
866 /* condition is already satisfied */
867 end_wait( current );
868 set_error( ret );
869 return timeout;
872 /* now we need to wait */
873 if (current->wait->timeout != TIMEOUT_INFINITE)
875 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
876 thread_timeout, current->wait )))
878 end_wait( current );
879 return timeout;
882 current->wait->cookie = cookie;
883 set_error( STATUS_PENDING );
884 return timeout;
887 /* attempt to wake threads sleeping on the object wait queue */
888 void wake_up( struct object *obj, int max )
890 struct list *ptr;
891 int ret;
893 LIST_FOR_EACH( ptr, &obj->wait_queue )
895 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
896 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
897 if (ret > 0 && max && !--max) break;
898 /* restart at the head of the list since a wake up can change the object wait queue */
899 ptr = &obj->wait_queue;
903 /* return the apc queue to use for a given apc type */
904 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
906 switch(type)
908 case APC_NONE:
909 case APC_USER:
910 case APC_TIMER:
911 return &thread->user_apc;
912 default:
913 return &thread->system_apc;
917 /* check if thread is currently waiting for a (system) apc */
918 static inline int is_in_apc_wait( struct thread *thread )
920 return (thread->process->suspend || thread->suspend ||
921 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
924 /* queue an existing APC to a given thread */
925 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
927 struct list *queue;
929 if (!thread) /* find a suitable thread inside the process */
931 struct thread *candidate;
933 /* first try to find a waiting thread */
934 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
936 if (candidate->state == TERMINATED) continue;
937 if (is_in_apc_wait( candidate ))
939 thread = candidate;
940 break;
943 if (!thread)
945 /* then use the first one that accepts a signal */
946 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
948 if (send_thread_signal( candidate, SIGUSR1 ))
950 thread = candidate;
951 break;
955 if (!thread) return 0; /* nothing found */
956 queue = get_apc_queue( thread, apc->call.type );
958 else
960 if (thread->state == TERMINATED) return 0;
961 queue = get_apc_queue( thread, apc->call.type );
962 /* send signal for system APCs if needed */
963 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
965 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
967 /* cancel a possible previous APC with the same owner */
968 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
971 grab_object( apc );
972 list_add_tail( queue, &apc->entry );
973 if (!list_prev( queue, &apc->entry )) /* first one */
974 wake_thread( thread );
976 return 1;
979 /* queue an async procedure call */
980 int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data )
982 struct thread_apc *apc;
983 int ret = 0;
985 if ((apc = create_apc( owner, call_data )))
987 ret = queue_apc( NULL, thread, apc );
988 release_object( apc );
990 return ret;
993 /* cancel the async procedure call owned by a specific object */
994 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
996 struct thread_apc *apc;
997 struct list *queue = get_apc_queue( thread, type );
999 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1001 if (apc->owner != owner) continue;
1002 list_remove( &apc->entry );
1003 apc->executed = 1;
1004 wake_up( &apc->obj, 0 );
1005 release_object( apc );
1006 return;
1010 /* remove the head apc from the queue; the returned object must be released by the caller */
1011 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
1013 struct thread_apc *apc = NULL;
1014 struct list *ptr = list_head( &thread->system_apc );
1016 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
1017 if (ptr)
1019 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1020 list_remove( ptr );
1022 return apc;
1025 /* clear an APC queue, cancelling all the APCs on it */
1026 static void clear_apc_queue( struct list *queue )
1028 struct list *ptr;
1030 while ((ptr = list_head( queue )))
1032 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1033 list_remove( &apc->entry );
1034 apc->executed = 1;
1035 wake_up( &apc->obj, 0 );
1036 release_object( apc );
1040 /* add an fd to the inflight list */
1041 /* return list index, or -1 on error */
1042 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1044 int i;
1046 if (server == -1) return -1;
1047 if (client == -1)
1049 close( server );
1050 return -1;
1053 /* first check if we already have an entry for this fd */
1054 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1055 if (thread->inflight[i].client == client)
1057 close( thread->inflight[i].server );
1058 thread->inflight[i].server = server;
1059 return i;
1062 /* now find a free spot to store it */
1063 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1064 if (thread->inflight[i].client == -1)
1066 thread->inflight[i].client = client;
1067 thread->inflight[i].server = server;
1068 return i;
1071 close( server );
1072 return -1;
1075 /* get an inflight fd and purge it from the list */
1076 /* the fd must be closed when no longer used */
1077 int thread_get_inflight_fd( struct thread *thread, int client )
1079 int i, ret;
1081 if (client == -1) return -1;
1085 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1087 if (thread->inflight[i].client == client)
1089 ret = thread->inflight[i].server;
1090 thread->inflight[i].server = thread->inflight[i].client = -1;
1091 return ret;
1094 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1095 return -1;
1098 /* kill a thread on the spot */
1099 void kill_thread( struct thread *thread, int violent_death )
1101 if (thread->state == TERMINATED) return; /* already killed */
1102 thread->state = TERMINATED;
1103 thread->exit_time = current_time;
1104 if (current == thread) current = NULL;
1105 if (debug_level)
1106 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1107 thread->id, thread->exit_code );
1108 if (thread->wait)
1110 while (thread->wait) end_wait( thread );
1111 send_thread_wakeup( thread, 0, thread->exit_code );
1112 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1113 violent_death = 0;
1115 kill_console_processes( thread, 0 );
1116 debug_exit_thread( thread );
1117 abandon_mutexes( thread );
1118 wake_up( &thread->obj, 0 );
1119 if (violent_death) send_thread_signal( thread, SIGQUIT );
1120 cleanup_thread( thread );
1121 remove_process_thread( thread->process, thread );
1122 release_object( thread );
1125 /* copy parts of a context structure */
1126 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1128 assert( to->cpu == from->cpu );
1129 to->flags |= flags;
1130 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1131 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1132 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1133 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1134 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1135 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1138 /* return the context flags that correspond to system regs */
1139 /* (system regs are the ones we can't access on the client side) */
1140 static unsigned int get_context_system_regs( enum cpu_type cpu )
1142 switch (cpu)
1144 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1145 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1146 case CPU_POWERPC: return 0;
1147 case CPU_ARM: return 0;
1148 case CPU_ARM64: return 0;
1150 return 0;
1153 /* trigger a breakpoint event in a given thread */
1154 void break_thread( struct thread *thread )
1156 debug_event_t data;
1158 assert( thread->context );
1160 memset( &data, 0, sizeof(data) );
1161 data.exception.first = 1;
1162 data.exception.exc_code = STATUS_BREAKPOINT;
1163 data.exception.flags = EXCEPTION_CONTINUABLE;
1164 switch (thread->context->cpu)
1166 case CPU_x86:
1167 data.exception.address = thread->context->ctl.i386_regs.eip;
1168 break;
1169 case CPU_x86_64:
1170 data.exception.address = thread->context->ctl.x86_64_regs.rip;
1171 break;
1172 case CPU_POWERPC:
1173 data.exception.address = thread->context->ctl.powerpc_regs.iar;
1174 break;
1175 case CPU_ARM:
1176 data.exception.address = thread->context->ctl.arm_regs.pc;
1177 break;
1178 case CPU_ARM64:
1179 data.exception.address = thread->context->ctl.arm64_regs.pc;
1180 break;
1182 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
1183 thread->debug_break = 0;
1186 /* take a snapshot of currently running threads */
1187 struct thread_snapshot *thread_snap( int *count )
1189 struct thread_snapshot *snapshot, *ptr;
1190 struct thread *thread;
1191 int total = 0;
1193 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1194 if (thread->state != TERMINATED) total++;
1195 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1196 ptr = snapshot;
1197 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1199 if (thread->state == TERMINATED) continue;
1200 ptr->thread = thread;
1201 ptr->count = thread->obj.refcount;
1202 ptr->priority = thread->priority;
1203 grab_object( thread );
1204 ptr++;
1206 *count = total;
1207 return snapshot;
1210 /* gets the current impersonation token */
1211 struct token *thread_get_impersonation_token( struct thread *thread )
1213 if (thread->token)
1214 return thread->token;
1215 else
1216 return thread->process->token;
1219 /* check if a cpu type can be supported on this server */
1220 int is_cpu_supported( enum cpu_type cpu )
1222 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1224 if (CPU_FLAG(cpu) && (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu))) return 1;
1225 if (!(supported_cpus & prefix_cpu_mask))
1226 set_error( STATUS_NOT_SUPPORTED );
1227 else if (supported_cpus & CPU_FLAG(cpu))
1228 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1229 else
1230 set_error( STATUS_INVALID_IMAGE_FORMAT );
1231 return 0;
1234 /* create a new thread */
1235 DECL_HANDLER(new_thread)
1237 struct thread *thread;
1238 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1240 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1242 if (request_fd != -1) close( request_fd );
1243 set_error( STATUS_INVALID_HANDLE );
1244 return;
1247 if ((thread = create_thread( request_fd, current->process )))
1249 if (req->suspend) thread->suspend++;
1250 reply->tid = get_thread_id( thread );
1251 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
1253 /* thread object will be released when the thread gets killed */
1254 return;
1256 kill_thread( thread, 1 );
1260 /* initialize a new thread */
1261 DECL_HANDLER(init_thread)
1263 struct process *process = current->process;
1264 int wait_fd, reply_fd;
1266 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1268 set_error( STATUS_TOO_MANY_OPENED_FILES );
1269 return;
1271 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1273 set_error( STATUS_TOO_MANY_OPENED_FILES );
1274 goto error;
1277 if (current->reply_fd) /* already initialised */
1279 set_error( STATUS_INVALID_PARAMETER );
1280 goto error;
1283 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1285 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1286 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1287 if (!current->reply_fd || !current->wait_fd) return;
1289 if (!is_valid_address(req->teb))
1291 set_error( STATUS_INVALID_PARAMETER );
1292 return;
1295 current->unix_pid = req->unix_pid;
1296 current->unix_tid = req->unix_tid;
1297 current->teb = req->teb;
1298 current->entry_point = process->peb ? req->entry : 0;
1300 if (!process->peb) /* first thread, initialize the process too */
1302 if (!is_cpu_supported( req->cpu )) return;
1303 process->unix_pid = current->unix_pid;
1304 process->peb = req->entry;
1305 process->cpu = req->cpu;
1306 reply->info_size = init_process( current );
1307 if (!process->parent)
1308 process->affinity = current->affinity = get_thread_affinity( current );
1309 else
1310 set_thread_affinity( current, current->affinity );
1312 else
1314 if (req->cpu != process->cpu)
1316 set_error( STATUS_INVALID_PARAMETER );
1317 return;
1319 if (process->unix_pid != current->unix_pid)
1320 process->unix_pid = -1; /* can happen with linuxthreads */
1321 stop_thread_if_suspended( current );
1322 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1323 set_thread_affinity( current, current->affinity );
1325 debug_level = max( debug_level, req->debug_level );
1327 reply->pid = get_process_id( process );
1328 reply->tid = get_thread_id( current );
1329 reply->version = SERVER_PROTOCOL_VERSION;
1330 reply->server_start = server_start_time;
1331 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1332 return;
1334 error:
1335 if (reply_fd != -1) close( reply_fd );
1336 if (wait_fd != -1) close( wait_fd );
1339 /* terminate a thread */
1340 DECL_HANDLER(terminate_thread)
1342 struct thread *thread;
1344 reply->self = 0;
1345 reply->last = 0;
1346 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1348 thread->exit_code = req->exit_code;
1349 if (thread != current) kill_thread( thread, 1 );
1350 else
1352 reply->self = 1;
1353 reply->last = (thread->process->running_threads == 1);
1355 release_object( thread );
1359 /* open a handle to a thread */
1360 DECL_HANDLER(open_thread)
1362 struct thread *thread = get_thread_from_id( req->tid );
1364 reply->handle = 0;
1365 if (thread)
1367 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1368 release_object( thread );
1372 /* fetch information about a thread */
1373 DECL_HANDLER(get_thread_info)
1375 struct thread *thread;
1376 obj_handle_t handle = req->handle;
1378 if (!handle) thread = get_thread_from_id( req->tid_in );
1379 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION );
1381 if (thread)
1383 reply->pid = get_process_id( thread->process );
1384 reply->tid = get_thread_id( thread );
1385 reply->teb = thread->teb;
1386 reply->entry_point = thread->entry_point;
1387 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1388 reply->priority = thread->priority;
1389 reply->affinity = thread->affinity;
1390 reply->last = thread->process->running_threads == 1;
1392 release_object( thread );
1396 /* fetch information about thread times */
1397 DECL_HANDLER(get_thread_times)
1399 struct thread *thread;
1401 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1403 reply->creation_time = thread->creation_time;
1404 reply->exit_time = thread->exit_time;
1406 release_object( thread );
1410 /* set information about a thread */
1411 DECL_HANDLER(set_thread_info)
1413 struct thread *thread;
1415 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1417 set_thread_info( thread, req );
1418 release_object( thread );
1422 /* suspend a thread */
1423 DECL_HANDLER(suspend_thread)
1425 struct thread *thread;
1427 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1429 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1430 else reply->count = suspend_thread( thread );
1431 release_object( thread );
1435 /* resume a thread */
1436 DECL_HANDLER(resume_thread)
1438 struct thread *thread;
1440 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1442 reply->count = resume_thread( thread );
1443 release_object( thread );
1447 /* select on a handle list */
1448 DECL_HANDLER(select)
1450 select_op_t select_op;
1451 data_size_t op_size;
1452 struct thread_apc *apc;
1453 const apc_result_t *result = get_req_data();
1455 if (get_req_data_size() < sizeof(*result))
1457 set_error( STATUS_INVALID_PARAMETER );
1458 return;
1460 if (!req->cookie)
1462 set_error( STATUS_INVALID_PARAMETER );
1463 return;
1466 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1467 memset( &select_op, 0, sizeof(select_op) );
1468 memcpy( &select_op, result + 1, op_size );
1470 /* first store results of previous apc */
1471 if (req->prev_apc)
1473 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1474 0, &thread_apc_ops ))) return;
1475 apc->result = *result;
1476 apc->executed = 1;
1477 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1479 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1480 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1481 close_handle( current->process, apc->result.create_thread.handle );
1482 apc->result.create_thread.handle = handle;
1483 clear_error(); /* ignore errors from the above calls */
1485 else if (apc->result.type == APC_ASYNC_IO)
1487 if (apc->owner)
1488 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total,
1489 apc->result.async_io.apc, apc->result.async_io.arg );
1491 wake_up( &apc->obj, 0 );
1492 close_handle( current->process, req->prev_apc );
1493 release_object( apc );
1496 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1498 if (get_error() == STATUS_USER_APC)
1500 for (;;)
1502 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1503 break;
1504 /* Optimization: ignore APC_NONE calls, they are only used to
1505 * wake up a thread, but since we got here the thread woke up already.
1507 if (apc->call.type != APC_NONE &&
1508 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1510 reply->call = apc->call;
1511 release_object( apc );
1512 break;
1514 apc->executed = 1;
1515 wake_up( &apc->obj, 0 );
1516 release_object( apc );
1521 /* queue an APC for a thread or process */
1522 DECL_HANDLER(queue_apc)
1524 struct thread *thread = NULL;
1525 struct process *process = NULL;
1526 struct thread_apc *apc;
1528 if (!(apc = create_apc( NULL, &req->call ))) return;
1530 switch (apc->call.type)
1532 case APC_NONE:
1533 case APC_USER:
1534 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1535 break;
1536 case APC_VIRTUAL_ALLOC:
1537 case APC_VIRTUAL_FREE:
1538 case APC_VIRTUAL_PROTECT:
1539 case APC_VIRTUAL_FLUSH:
1540 case APC_VIRTUAL_LOCK:
1541 case APC_VIRTUAL_UNLOCK:
1542 case APC_UNMAP_VIEW:
1543 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1544 break;
1545 case APC_VIRTUAL_QUERY:
1546 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1547 break;
1548 case APC_MAP_VIEW:
1549 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1550 if (process && process != current->process)
1552 /* duplicate the handle into the target process */
1553 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1554 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1555 if (handle) apc->call.map_view.handle = handle;
1556 else
1558 release_object( process );
1559 process = NULL;
1562 break;
1563 case APC_CREATE_THREAD:
1564 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1565 break;
1566 default:
1567 set_error( STATUS_INVALID_PARAMETER );
1568 break;
1571 if (thread)
1573 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1574 release_object( thread );
1576 else if (process)
1578 reply->self = (process == current->process);
1579 if (!reply->self)
1581 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1582 if (handle)
1584 if (queue_apc( process, NULL, apc ))
1586 apc->caller = (struct thread *)grab_object( current );
1587 reply->handle = handle;
1589 else
1591 close_handle( current->process, handle );
1592 set_error( STATUS_PROCESS_IS_TERMINATING );
1596 release_object( process );
1599 release_object( apc );
1602 /* Get the result of an APC call */
1603 DECL_HANDLER(get_apc_result)
1605 struct thread_apc *apc;
1607 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1608 0, &thread_apc_ops ))) return;
1610 if (apc->executed) reply->result = apc->result;
1611 else set_error( STATUS_PENDING );
1613 /* close the handle directly to avoid an extra round-trip */
1614 close_handle( current->process, req->handle );
1615 release_object( apc );
1618 /* retrieve the current context of a thread */
1619 DECL_HANDLER(get_thread_context)
1621 struct thread *thread;
1622 context_t *context;
1624 if (get_reply_max_size() < sizeof(context_t))
1626 set_error( STATUS_INVALID_PARAMETER );
1627 return;
1629 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1630 reply->self = (thread == current);
1632 if (thread != current && !thread->context)
1634 /* thread is not suspended, retry (if it's still running) */
1635 if (thread->state == RUNNING)
1637 set_error( STATUS_PENDING );
1638 if (req->suspend)
1640 release_object( thread );
1641 /* make sure we have suspend access */
1642 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1643 suspend_thread( thread );
1646 else set_error( STATUS_UNSUCCESSFUL );
1648 else if ((context = set_reply_data_size( sizeof(context_t) )))
1650 unsigned int flags = get_context_system_regs( thread->process->cpu );
1652 memset( context, 0, sizeof(context_t) );
1653 context->cpu = thread->process->cpu;
1654 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1655 if (flags) get_thread_context( thread, context, flags );
1657 release_object( thread );
1660 /* set the current context of a thread */
1661 DECL_HANDLER(set_thread_context)
1663 struct thread *thread;
1664 const context_t *context = get_req_data();
1666 if (get_req_data_size() < sizeof(context_t))
1668 set_error( STATUS_INVALID_PARAMETER );
1669 return;
1671 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1672 reply->self = (thread == current);
1674 if (thread != current && !thread->context)
1676 /* thread is not suspended, retry (if it's still running) */
1677 if (thread->state == RUNNING)
1679 set_error( STATUS_PENDING );
1680 if (req->suspend)
1682 release_object( thread );
1683 /* make sure we have suspend access */
1684 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1685 suspend_thread( thread );
1688 else set_error( STATUS_UNSUCCESSFUL );
1690 else if (context->cpu == thread->process->cpu)
1692 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1693 unsigned int client_flags = context->flags & ~system_flags;
1695 if (system_flags) set_thread_context( thread, context, system_flags );
1696 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1698 else set_error( STATUS_INVALID_PARAMETER );
1700 release_object( thread );
1703 /* retrieve the suspended context of a thread */
1704 DECL_HANDLER(get_suspend_context)
1706 if (get_reply_max_size() < sizeof(context_t))
1708 set_error( STATUS_INVALID_PARAMETER );
1709 return;
1712 if (current->suspend_context)
1714 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1715 if (current->context == current->suspend_context)
1717 current->context = NULL;
1718 stop_thread_if_suspended( current );
1720 current->suspend_context = NULL;
1722 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1725 /* store the suspended context of a thread */
1726 DECL_HANDLER(set_suspend_context)
1728 const context_t *context = get_req_data();
1730 if (get_req_data_size() < sizeof(context_t))
1732 set_error( STATUS_INVALID_PARAMETER );
1733 return;
1736 if (current->context || context->cpu != current->process->cpu)
1738 /* nested suspend or exception, shouldn't happen */
1739 set_error( STATUS_INVALID_PARAMETER );
1741 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1743 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1744 current->context = current->suspend_context;
1745 if (current->debug_break) break_thread( current );
1749 /* fetch a selector entry for a thread */
1750 DECL_HANDLER(get_selector_entry)
1752 struct thread *thread;
1753 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1755 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1756 release_object( thread );