TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / server / thread.c
blob194c6c372211fb9e2ec1cc4c276891bc841c5a81
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"
55 extern int rtkit_make_realtime(struct thread *thread, pid_t tid, int priority);
56 extern int rtkit_undo_realtime(struct thread *thread);
58 #ifdef __i386__
59 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86);
60 #elif defined(__x86_64__)
61 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86_64) | CPU_FLAG(CPU_x86);
62 #elif defined(__powerpc__)
63 static const unsigned int supported_cpus = CPU_FLAG(CPU_POWERPC);
64 #elif defined(__arm__)
65 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM);
66 #elif defined(__aarch64__)
67 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM64) | CPU_FLAG(CPU_ARM);
68 #else
69 #error Unsupported CPU
70 #endif
72 /* thread queues */
74 struct thread_wait
76 struct thread_wait *next; /* next wait structure for this thread */
77 struct thread *thread; /* owner thread */
78 int count; /* count of objects */
79 int flags;
80 int abandoned;
81 enum select_op select;
82 client_ptr_t key; /* wait key for keyed events */
83 client_ptr_t cookie; /* magic cookie to return to client */
84 timeout_t timeout;
85 struct timeout_user *user;
86 struct wait_queue_entry queues[1];
89 /* asynchronous procedure calls */
91 struct thread_apc
93 struct object obj; /* object header */
94 struct list entry; /* queue linked list */
95 struct thread *caller; /* thread that queued this apc */
96 struct object *owner; /* object that queued this apc */
97 int executed; /* has it been executed by the client? */
98 apc_call_t call; /* call arguments */
99 apc_result_t result; /* call results once executed */
102 static void dump_thread_apc( struct object *obj, int verbose );
103 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry );
104 static void thread_apc_destroy( struct object *obj );
105 static void clear_apc_queue( struct list *queue );
107 static const struct object_ops thread_apc_ops =
109 sizeof(struct thread_apc), /* size */
110 dump_thread_apc, /* dump */
111 no_get_type, /* get_type */
112 add_queue, /* add_queue */
113 remove_queue, /* remove_queue */
114 thread_apc_signaled, /* signaled */
115 no_satisfied, /* satisfied */
116 no_signal, /* signal */
117 no_get_fd, /* get_fd */
118 no_map_access, /* map_access */
119 default_get_sd, /* get_sd */
120 default_set_sd, /* set_sd */
121 no_lookup_name, /* lookup_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_open_file, /* open_file */
152 no_close_handle, /* close_handle */
153 destroy_thread /* destroy */
156 static const struct fd_ops thread_fd_ops =
158 NULL, /* get_poll_events */
159 thread_poll_event, /* poll_event */
160 NULL, /* flush */
161 NULL, /* get_fd_type */
162 NULL, /* ioctl */
163 NULL, /* queue_async */
164 NULL, /* reselect_async */
165 NULL /* cancel_async */
168 static struct list thread_list = LIST_INIT(thread_list);
170 /* initialize the structure for a newly allocated thread */
171 static inline void init_thread_structure( struct thread *thread )
173 int i;
175 thread->unix_pid = -1; /* not known yet */
176 thread->unix_tid = -1; /* not known yet */
177 thread->context = NULL;
178 thread->suspend_context = NULL;
179 thread->teb = 0;
180 thread->entry_point = 0;
181 thread->debug_ctx = NULL;
182 thread->debug_event = NULL;
183 thread->debug_break = 0;
184 thread->queue = NULL;
185 thread->wait = NULL;
186 thread->error = 0;
187 thread->req_data = NULL;
188 thread->req_toread = 0;
189 thread->reply_data = NULL;
190 thread->reply_towrite = 0;
191 thread->request_fd = NULL;
192 thread->reply_fd = NULL;
193 thread->wait_fd = NULL;
194 thread->state = RUNNING;
195 thread->exit_code = 0;
196 thread->priority = 0;
197 thread->suspend = 0;
198 thread->desktop_users = 0;
199 thread->token = NULL;
201 thread->creation_time = current_time;
202 thread->exit_time = 0;
204 list_init( &thread->mutex_list );
205 list_init( &thread->system_apc );
206 list_init( &thread->user_apc );
207 list_init( &thread->rt_entry );
208 thread->rt_prio = 0;
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 thread->unix_tid = -1;
284 if (!list_empty(&thread->rt_entry))
285 rtkit_undo_realtime(thread);
286 clear_apc_queue( &thread->system_apc );
287 clear_apc_queue( &thread->user_apc );
288 free( thread->req_data );
289 free( thread->reply_data );
290 if (thread->request_fd) release_object( thread->request_fd );
291 if (thread->reply_fd) release_object( thread->reply_fd );
292 if (thread->wait_fd) release_object( thread->wait_fd );
293 free( thread->suspend_context );
294 cleanup_clipboard_thread(thread);
295 destroy_thread_windows( thread );
296 free_msg_queue( thread );
297 close_thread_desktop( thread );
298 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
300 if (thread->inflight[i].client != -1)
302 close( thread->inflight[i].server );
303 thread->inflight[i].client = thread->inflight[i].server = -1;
306 thread->req_data = NULL;
307 thread->reply_data = NULL;
308 thread->request_fd = NULL;
309 thread->reply_fd = NULL;
310 thread->wait_fd = NULL;
311 thread->context = NULL;
312 thread->suspend_context = NULL;
313 thread->desktop = 0;
316 /* destroy a thread when its refcount is 0 */
317 static void destroy_thread( struct object *obj )
319 struct thread *thread = (struct thread *)obj;
320 assert( obj->ops == &thread_ops );
322 assert( !thread->debug_ctx ); /* cannot still be debugging something */
323 list_remove( &thread->entry );
324 cleanup_thread( thread );
325 release_object( thread->process );
326 if (thread->id) free_ptid( thread->id );
327 if (thread->token) release_object( thread->token );
330 /* dump a thread on stdout for debugging purposes */
331 static void dump_thread( struct object *obj, int verbose )
333 struct thread *thread = (struct thread *)obj;
334 assert( obj->ops == &thread_ops );
336 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
337 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
340 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
342 struct thread *mythread = (struct thread *)obj;
343 return (mythread->state == TERMINATED);
346 static unsigned int thread_map_access( struct object *obj, unsigned int access )
348 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
349 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
350 THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
351 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
352 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
354 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
355 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
357 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
360 static void dump_thread_apc( struct object *obj, int verbose )
362 struct thread_apc *apc = (struct thread_apc *)obj;
363 assert( obj->ops == &thread_apc_ops );
365 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
368 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
370 struct thread_apc *apc = (struct thread_apc *)obj;
371 return apc->executed;
374 static void thread_apc_destroy( struct object *obj )
376 struct thread_apc *apc = (struct thread_apc *)obj;
377 if (apc->caller) release_object( apc->caller );
378 if (apc->owner) release_object( apc->owner );
381 /* queue an async procedure call */
382 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
384 struct thread_apc *apc;
386 if ((apc = alloc_object( &thread_apc_ops )))
388 apc->call = *call_data;
389 apc->caller = NULL;
390 apc->owner = owner;
391 apc->executed = 0;
392 apc->result.type = APC_NONE;
393 if (owner) grab_object( owner );
395 return apc;
398 /* get a thread pointer from a thread id (and increment the refcount) */
399 struct thread *get_thread_from_id( thread_id_t id )
401 struct object *obj = get_ptid_entry( id );
403 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
404 set_error( STATUS_INVALID_CID );
405 return NULL;
408 /* get a thread from a handle (and increment the refcount) */
409 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
411 return (struct thread *)get_handle_obj( current->process, handle,
412 access, &thread_ops );
415 /* find a thread from a Unix tid */
416 struct thread *get_thread_from_tid( int tid )
418 struct thread *thread;
420 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
422 if (thread->unix_tid == tid) return thread;
424 return NULL;
427 /* find a thread from a Unix pid */
428 struct thread *get_thread_from_pid( int pid )
430 struct thread *thread;
432 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
434 if (thread->unix_pid == pid) return thread;
436 return NULL;
439 int set_thread_affinity( struct thread *thread, affinity_t affinity )
441 int ret = 0;
442 #ifdef HAVE_SCHED_SETAFFINITY
443 if (thread->unix_tid != -1)
445 cpu_set_t set;
446 int i;
447 affinity_t mask;
449 CPU_ZERO( &set );
450 for (i = 0, mask = 1; mask; i++, mask <<= 1)
451 if (affinity & mask) CPU_SET( i, &set );
453 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
455 #endif
456 if (!ret) thread->affinity = affinity;
457 return ret;
460 affinity_t get_thread_affinity( struct thread *thread )
462 affinity_t mask = 0;
463 #ifdef HAVE_SCHED_SETAFFINITY
464 if (thread->unix_tid != -1)
466 cpu_set_t set;
467 unsigned int i;
469 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
470 for (i = 0; i < 8 * sizeof(mask); i++)
471 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
473 #endif
474 if (!mask) mask = ~(affinity_t)0;
475 return mask;
478 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
479 #define THREAD_PRIORITY_REALTIME_LOWEST -7
481 static int rtprio(int ntprio)
483 if (ntprio == THREAD_PRIORITY_TIME_CRITICAL - 1)
484 return 1;
485 else if (ntprio == THREAD_PRIORITY_TIME_CRITICAL)
486 return 2;
487 return 0;
490 /* set all information about a thread */
491 static void set_thread_info( struct thread *thread,
492 const struct set_thread_info_request *req )
494 if (req->mask & SET_THREAD_INFO_PRIORITY)
496 int max = THREAD_PRIORITY_HIGHEST;
497 int min = THREAD_PRIORITY_LOWEST;
498 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
500 max = THREAD_PRIORITY_REALTIME_HIGHEST;
501 min = THREAD_PRIORITY_REALTIME_LOWEST;
503 if ((req->priority >= min && req->priority <= max) ||
504 req->priority == THREAD_PRIORITY_IDLE ||
505 req->priority == THREAD_PRIORITY_TIME_CRITICAL - 1 ||
506 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
508 int newprio = rtprio(req->priority);
509 if (thread->unix_tid == -1)
510 thread->rt_prio = newprio;
511 else if (thread->priority == THREAD_PRIORITY_TIME_CRITICAL && !newprio)
512 rtkit_undo_realtime(thread);
513 else if (thread->rt_prio != newprio)
514 rtkit_make_realtime(thread, thread->unix_tid, newprio);
516 if (newprio)
517 thread->priority = THREAD_PRIORITY_TIME_CRITICAL;
518 else
519 thread->priority = req->priority;
521 else
522 set_error( STATUS_INVALID_PARAMETER );
524 if (req->mask & SET_THREAD_INFO_AFFINITY)
526 if ((req->affinity & thread->process->affinity) != req->affinity)
527 set_error( STATUS_INVALID_PARAMETER );
528 else if (thread->state == TERMINATED)
529 set_error( STATUS_THREAD_IS_TERMINATING );
530 else if (set_thread_affinity( thread, req->affinity ))
531 file_set_error();
533 if (req->mask & SET_THREAD_INFO_TOKEN)
534 security_set_thread_token( thread, req->token );
535 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
536 thread->entry_point = req->entry_point;
539 /* stop a thread (at the Unix level) */
540 void stop_thread( struct thread *thread )
542 if (thread->context) return; /* already inside a debug event, no need for a signal */
543 /* can't stop a thread while initialisation is in progress */
544 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
547 /* stop a thread if it's supposed to be suspended */
548 void stop_thread_if_suspended( struct thread *thread )
550 if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
553 /* suspend a thread */
554 static int suspend_thread( struct thread *thread )
556 int old_count = thread->suspend;
557 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
559 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
561 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
562 return old_count;
565 /* resume a thread */
566 static int resume_thread( struct thread *thread )
568 int old_count = thread->suspend;
569 if (thread->suspend > 0)
571 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
573 return old_count;
576 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
577 int add_queue( struct object *obj, struct wait_queue_entry *entry )
579 grab_object( obj );
580 entry->obj = obj;
581 list_add_tail( &obj->wait_queue, &entry->entry );
582 return 1;
585 /* remove a thread from an object wait queue */
586 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
588 list_remove( &entry->entry );
589 release_object( obj );
592 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
594 return entry->wait->thread;
597 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
599 return entry->wait->select;
602 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
604 return entry->wait->key;
607 void make_wait_abandoned( struct wait_queue_entry *entry )
609 entry->wait->abandoned = 1;
612 /* finish waiting */
613 static void end_wait( struct thread *thread )
615 struct thread_wait *wait = thread->wait;
616 struct wait_queue_entry *entry;
617 int i;
619 assert( wait );
620 thread->wait = wait->next;
621 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
622 entry->obj->ops->remove_queue( entry->obj, entry );
623 if (wait->user) remove_timeout_user( wait->user );
624 free( wait );
627 /* build the thread wait structure */
628 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
629 int flags, timeout_t timeout )
631 struct thread_wait *wait;
632 struct wait_queue_entry *entry;
633 unsigned int i;
635 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
636 wait->next = current->wait;
637 wait->thread = current;
638 wait->count = count;
639 wait->flags = flags;
640 wait->select = select_op->op;
641 wait->cookie = 0;
642 wait->user = NULL;
643 wait->timeout = timeout;
644 wait->abandoned = 0;
645 current->wait = wait;
647 for (i = 0, entry = wait->queues; i < count; i++, entry++)
649 struct object *obj = objects[i];
650 entry->wait = wait;
651 if (!obj->ops->add_queue( obj, entry ))
653 wait->count = i;
654 end_wait( current );
655 return 0;
658 return 1;
661 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
662 int flags, timeout_t timeout )
664 struct object *objects[MAXIMUM_WAIT_OBJECTS];
665 unsigned int i;
666 int ret = 0;
668 assert( count <= MAXIMUM_WAIT_OBJECTS );
670 for (i = 0; i < count; i++)
671 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
672 break;
674 if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
676 while (i > 0) release_object( objects[--i] );
677 return ret;
680 /* check if the thread waiting condition is satisfied */
681 static int check_wait( struct thread *thread )
683 int i;
684 struct thread_wait *wait = thread->wait;
685 struct wait_queue_entry *entry;
687 assert( wait );
689 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
690 return STATUS_USER_APC;
692 /* Suspended threads may not acquire locks, but they can run system APCs */
693 if (thread->process->suspend + thread->suspend > 0) return -1;
695 if (wait->select == SELECT_WAIT_ALL)
697 int not_ok = 0;
698 /* Note: we must check them all anyway, as some objects may
699 * want to do something when signaled, even if others are not */
700 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
701 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
702 if (not_ok) goto other_checks;
703 /* Wait satisfied: tell it to all objects */
704 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
705 entry->obj->ops->satisfied( entry->obj, entry );
706 return wait->abandoned ? STATUS_ABANDONED_WAIT_0 : STATUS_WAIT_0;
708 else
710 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
712 if (!entry->obj->ops->signaled( entry->obj, entry )) continue;
713 /* Wait satisfied: tell it to the object */
714 entry->obj->ops->satisfied( entry->obj, entry );
715 if (wait->abandoned) i += STATUS_ABANDONED_WAIT_0;
716 return i;
720 other_checks:
721 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
722 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
723 return -1;
726 /* send the wakeup signal to a thread */
727 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
729 struct wake_up_reply reply;
730 int ret;
732 memset( &reply, 0, sizeof(reply) );
733 reply.cookie = cookie;
734 reply.signaled = signaled;
735 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
736 return 0;
737 if (ret >= 0)
738 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
739 else if (errno == EPIPE)
740 kill_thread( thread, 0 ); /* normal death */
741 else
742 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
743 return -1;
746 /* attempt to wake up a thread */
747 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
748 int wake_thread( struct thread *thread )
750 int signaled, count;
751 client_ptr_t cookie;
753 for (count = 0; thread->wait; count++)
755 if ((signaled = check_wait( thread )) == -1) break;
757 cookie = thread->wait->cookie;
758 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
759 end_wait( thread );
760 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
762 if (!count) count = -1;
763 break;
766 return count;
769 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
770 int wake_thread_queue_entry( struct wait_queue_entry *entry )
772 struct thread_wait *wait = entry->wait;
773 struct thread *thread = wait->thread;
774 int signaled;
775 client_ptr_t cookie;
777 if (thread->wait != wait) return 0; /* not the current wait */
778 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
780 assert( wait->select != SELECT_WAIT_ALL );
782 signaled = entry - wait->queues;
783 entry->obj->ops->satisfied( entry->obj, entry );
784 if (wait->abandoned) signaled += STATUS_ABANDONED_WAIT_0;
786 cookie = wait->cookie;
787 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
788 end_wait( thread );
790 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
791 wake_thread( thread ); /* check other waits too */
793 return 1;
796 /* thread wait timeout */
797 static void thread_timeout( void *ptr )
799 struct thread_wait *wait = ptr;
800 struct thread *thread = wait->thread;
801 client_ptr_t cookie = wait->cookie;
803 wait->user = NULL;
804 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
805 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
807 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
808 end_wait( thread );
810 assert( cookie );
811 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
812 /* check if other objects have become signaled in the meantime */
813 wake_thread( thread );
816 /* try signaling an event flag, a semaphore or a mutex */
817 static int signal_object( obj_handle_t handle )
819 struct object *obj;
820 int ret = 0;
822 obj = get_handle_obj( current->process, handle, 0, NULL );
823 if (obj)
825 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
826 release_object( obj );
828 return ret;
831 /* select on a list of handles */
832 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
833 int flags, timeout_t timeout )
835 int ret;
836 unsigned int count;
837 struct object *object;
839 if (timeout <= 0) timeout = current_time - timeout;
841 switch (select_op->op)
843 case SELECT_NONE:
844 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
845 break;
847 case SELECT_WAIT:
848 case SELECT_WAIT_ALL:
849 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
850 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
852 set_error( STATUS_INVALID_PARAMETER );
853 return 0;
855 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
856 return timeout;
857 break;
859 case SELECT_SIGNAL_AND_WAIT:
860 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
861 return timeout;
862 if (select_op->signal_and_wait.signal)
864 if (!signal_object( select_op->signal_and_wait.signal ))
866 end_wait( current );
867 return timeout;
869 /* check if we woke ourselves up */
870 if (!current->wait) return timeout;
872 break;
874 case SELECT_KEYED_EVENT_WAIT:
875 case SELECT_KEYED_EVENT_RELEASE:
876 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
877 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
878 if (!object) return timeout;
879 ret = wait_on( select_op, 1, &object, flags, timeout );
880 release_object( object );
881 if (!ret) return timeout;
882 current->wait->key = select_op->keyed_event.key;
883 break;
885 default:
886 set_error( STATUS_INVALID_PARAMETER );
887 return 0;
890 if ((ret = check_wait( current )) != -1)
892 /* condition is already satisfied */
893 end_wait( current );
894 set_error( ret );
895 return timeout;
898 /* now we need to wait */
899 if (current->wait->timeout != TIMEOUT_INFINITE)
901 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
902 thread_timeout, current->wait )))
904 end_wait( current );
905 return timeout;
908 current->wait->cookie = cookie;
909 set_error( STATUS_PENDING );
910 return timeout;
913 /* attempt to wake threads sleeping on the object wait queue */
914 void wake_up( struct object *obj, int max )
916 struct list *ptr;
917 int ret;
919 LIST_FOR_EACH( ptr, &obj->wait_queue )
921 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
922 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
923 if (ret > 0 && max && !--max) break;
924 /* restart at the head of the list since a wake up can change the object wait queue */
925 ptr = &obj->wait_queue;
929 /* return the apc queue to use for a given apc type */
930 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
932 switch(type)
934 case APC_NONE:
935 case APC_USER:
936 case APC_TIMER:
937 return &thread->user_apc;
938 default:
939 return &thread->system_apc;
943 /* check if thread is currently waiting for a (system) apc */
944 static inline int is_in_apc_wait( struct thread *thread )
946 return (thread->process->suspend || thread->suspend ||
947 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
950 /* queue an existing APC to a given thread */
951 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
953 struct list *queue;
955 if (!thread) /* find a suitable thread inside the process */
957 struct thread *candidate;
959 /* first try to find a waiting thread */
960 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
962 if (candidate->state == TERMINATED) continue;
963 if (is_in_apc_wait( candidate ))
965 thread = candidate;
966 break;
969 if (!thread)
971 /* then use the first one that accepts a signal */
972 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
974 if (send_thread_signal( candidate, SIGUSR1 ))
976 thread = candidate;
977 break;
981 if (!thread) return 0; /* nothing found */
982 queue = get_apc_queue( thread, apc->call.type );
984 else
986 if (thread->state == TERMINATED) return 0;
987 queue = get_apc_queue( thread, apc->call.type );
988 /* send signal for system APCs if needed */
989 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
991 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
993 /* cancel a possible previous APC with the same owner */
994 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
997 grab_object( apc );
998 list_add_tail( queue, &apc->entry );
999 if (!list_prev( queue, &apc->entry )) /* first one */
1000 wake_thread( thread );
1002 return 1;
1005 /* queue an async procedure call */
1006 int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data )
1008 struct thread_apc *apc;
1009 int ret = 0;
1011 if ((apc = create_apc( owner, call_data )))
1013 ret = queue_apc( NULL, thread, apc );
1014 release_object( apc );
1016 return ret;
1019 /* cancel the async procedure call owned by a specific object */
1020 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1022 struct thread_apc *apc;
1023 struct list *queue = get_apc_queue( thread, type );
1025 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1027 if (apc->owner != owner) continue;
1028 list_remove( &apc->entry );
1029 apc->executed = 1;
1030 wake_up( &apc->obj, 0 );
1031 release_object( apc );
1032 return;
1036 /* remove the head apc from the queue; the returned object must be released by the caller */
1037 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
1039 struct thread_apc *apc = NULL;
1040 struct list *ptr = list_head( &thread->system_apc );
1042 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
1043 if (ptr)
1045 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1046 list_remove( ptr );
1048 return apc;
1051 /* clear an APC queue, cancelling all the APCs on it */
1052 static void clear_apc_queue( struct list *queue )
1054 struct list *ptr;
1056 while ((ptr = list_head( queue )))
1058 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1059 list_remove( &apc->entry );
1060 apc->executed = 1;
1061 wake_up( &apc->obj, 0 );
1062 release_object( apc );
1066 /* add an fd to the inflight list */
1067 /* return list index, or -1 on error */
1068 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1070 int i;
1072 if (server == -1) return -1;
1073 if (client == -1)
1075 close( server );
1076 return -1;
1079 /* first check if we already have an entry for this fd */
1080 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1081 if (thread->inflight[i].client == client)
1083 close( thread->inflight[i].server );
1084 thread->inflight[i].server = server;
1085 return i;
1088 /* now find a free spot to store it */
1089 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1090 if (thread->inflight[i].client == -1)
1092 thread->inflight[i].client = client;
1093 thread->inflight[i].server = server;
1094 return i;
1097 close( server );
1098 return -1;
1101 /* get an inflight fd and purge it from the list */
1102 /* the fd must be closed when no longer used */
1103 int thread_get_inflight_fd( struct thread *thread, int client )
1105 int i, ret;
1107 if (client == -1) return -1;
1111 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1113 if (thread->inflight[i].client == client)
1115 ret = thread->inflight[i].server;
1116 thread->inflight[i].server = thread->inflight[i].client = -1;
1117 return ret;
1120 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1121 return -1;
1124 /* kill a thread on the spot */
1125 void kill_thread( struct thread *thread, int violent_death )
1127 if (thread->state == TERMINATED) return; /* already killed */
1128 thread->state = TERMINATED;
1129 thread->exit_time = current_time;
1130 if (current == thread) current = NULL;
1131 if (debug_level)
1132 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1133 thread->id, thread->exit_code );
1134 if (thread->wait)
1136 while (thread->wait) end_wait( thread );
1137 send_thread_wakeup( thread, 0, thread->exit_code );
1138 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1139 violent_death = 0;
1141 kill_console_processes( thread, 0 );
1142 debug_exit_thread( thread );
1143 abandon_mutexes( thread );
1144 wake_up( &thread->obj, 0 );
1145 if (violent_death) send_thread_signal( thread, SIGQUIT );
1146 cleanup_thread( thread );
1147 remove_process_thread( thread->process, thread );
1148 release_object( thread );
1151 /* copy parts of a context structure */
1152 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1154 assert( to->cpu == from->cpu );
1155 to->flags |= flags;
1156 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1157 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1158 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1159 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1160 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1161 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1164 /* return the context flags that correspond to system regs */
1165 /* (system regs are the ones we can't access on the client side) */
1166 static unsigned int get_context_system_regs( enum cpu_type cpu )
1168 switch (cpu)
1170 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1171 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1172 case CPU_POWERPC: return 0;
1173 case CPU_ARM: return 0;
1174 case CPU_ARM64: return 0;
1176 return 0;
1179 /* trigger a breakpoint event in a given thread */
1180 void break_thread( struct thread *thread )
1182 debug_event_t data;
1184 assert( thread->context );
1186 memset( &data, 0, sizeof(data) );
1187 data.exception.first = 1;
1188 data.exception.exc_code = STATUS_BREAKPOINT;
1189 data.exception.flags = EXCEPTION_CONTINUABLE;
1190 switch (thread->context->cpu)
1192 case CPU_x86:
1193 data.exception.address = thread->context->ctl.i386_regs.eip;
1194 break;
1195 case CPU_x86_64:
1196 data.exception.address = thread->context->ctl.x86_64_regs.rip;
1197 break;
1198 case CPU_POWERPC:
1199 data.exception.address = thread->context->ctl.powerpc_regs.iar;
1200 break;
1201 case CPU_ARM:
1202 data.exception.address = thread->context->ctl.arm_regs.pc;
1203 break;
1204 case CPU_ARM64:
1205 data.exception.address = thread->context->ctl.arm64_regs.pc;
1206 break;
1208 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
1209 thread->debug_break = 0;
1212 /* take a snapshot of currently running threads */
1213 struct thread_snapshot *thread_snap( int *count )
1215 struct thread_snapshot *snapshot, *ptr;
1216 struct thread *thread;
1217 int total = 0;
1219 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1220 if (thread->state != TERMINATED) total++;
1221 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1222 ptr = snapshot;
1223 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1225 if (thread->state == TERMINATED) continue;
1226 ptr->thread = thread;
1227 ptr->count = thread->obj.refcount;
1228 ptr->priority = thread->priority;
1229 grab_object( thread );
1230 ptr++;
1232 *count = total;
1233 return snapshot;
1236 /* gets the current impersonation token */
1237 struct token *thread_get_impersonation_token( struct thread *thread )
1239 if (thread->token)
1240 return thread->token;
1241 else
1242 return thread->process->token;
1245 /* check if a cpu type can be supported on this server */
1246 int is_cpu_supported( enum cpu_type cpu )
1248 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1250 if (CPU_FLAG(cpu) && (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu))) return 1;
1251 if (!(supported_cpus & prefix_cpu_mask))
1252 set_error( STATUS_NOT_SUPPORTED );
1253 else if (supported_cpus & CPU_FLAG(cpu))
1254 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1255 else
1256 set_error( STATUS_INVALID_IMAGE_FORMAT );
1257 return 0;
1260 /* create a new thread */
1261 DECL_HANDLER(new_thread)
1263 struct thread *thread;
1264 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1266 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1268 if (request_fd != -1) close( request_fd );
1269 set_error( STATUS_INVALID_HANDLE );
1270 return;
1273 if ((thread = create_thread( request_fd, current->process )))
1275 if (req->suspend) thread->suspend++;
1276 reply->tid = get_thread_id( thread );
1277 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
1279 /* thread object will be released when the thread gets killed */
1280 return;
1282 kill_thread( thread, 1 );
1286 /* initialize a new thread */
1287 DECL_HANDLER(init_thread)
1289 struct process *process = current->process;
1290 int wait_fd, reply_fd;
1292 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1294 set_error( STATUS_TOO_MANY_OPENED_FILES );
1295 return;
1297 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1299 set_error( STATUS_TOO_MANY_OPENED_FILES );
1300 goto error;
1303 if (current->reply_fd) /* already initialised */
1305 set_error( STATUS_INVALID_PARAMETER );
1306 goto error;
1309 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1311 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1312 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1313 if (!current->reply_fd || !current->wait_fd) return;
1315 if (!is_valid_address(req->teb))
1317 set_error( STATUS_INVALID_PARAMETER );
1318 return;
1321 current->unix_pid = req->unix_pid;
1322 current->unix_tid = req->unix_tid;
1323 current->teb = req->teb;
1324 current->entry_point = process->peb ? req->entry : 0;
1326 if (!process->peb) /* first thread, initialize the process too */
1328 if (!is_cpu_supported( req->cpu )) return;
1329 process->unix_pid = current->unix_pid;
1330 process->peb = req->entry;
1331 process->cpu = req->cpu;
1332 reply->info_size = init_process( current );
1333 if (!process->parent)
1334 process->affinity = current->affinity = get_thread_affinity( current );
1335 else
1336 set_thread_affinity( current, current->affinity );
1338 else
1340 if (req->cpu != process->cpu)
1342 set_error( STATUS_INVALID_PARAMETER );
1343 return;
1345 if (process->unix_pid != current->unix_pid)
1346 process->unix_pid = -1; /* can happen with linuxthreads */
1347 stop_thread_if_suspended( current );
1348 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1349 set_thread_affinity( current, current->affinity );
1351 debug_level = max( debug_level, req->debug_level );
1353 /* Raced with SetThreadPriority */
1354 if (current->priority == THREAD_PRIORITY_TIME_CRITICAL)
1355 rtkit_make_realtime(current, current->unix_tid, current->rt_prio);
1357 reply->pid = get_process_id( process );
1358 reply->tid = get_thread_id( current );
1359 reply->version = SERVER_PROTOCOL_VERSION;
1360 reply->server_start = server_start_time;
1361 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1362 return;
1364 error:
1365 if (reply_fd != -1) close( reply_fd );
1366 if (wait_fd != -1) close( wait_fd );
1369 /* terminate a thread */
1370 DECL_HANDLER(terminate_thread)
1372 struct thread *thread;
1374 reply->self = 0;
1375 reply->last = 0;
1376 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1378 thread->exit_code = req->exit_code;
1379 if (thread != current) kill_thread( thread, 1 );
1380 else
1382 reply->self = 1;
1383 reply->last = (thread->process->running_threads == 1);
1385 release_object( thread );
1389 /* open a handle to a thread */
1390 DECL_HANDLER(open_thread)
1392 struct thread *thread = get_thread_from_id( req->tid );
1394 reply->handle = 0;
1395 if (thread)
1397 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1398 release_object( thread );
1402 /* fetch information about a thread */
1403 DECL_HANDLER(get_thread_info)
1405 struct thread *thread;
1406 obj_handle_t handle = req->handle;
1408 if (!handle) thread = get_thread_from_id( req->tid_in );
1409 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION );
1411 if (thread)
1413 reply->pid = get_process_id( thread->process );
1414 reply->tid = get_thread_id( thread );
1415 reply->teb = thread->teb;
1416 reply->entry_point = thread->entry_point;
1417 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1418 reply->priority = thread->priority;
1419 reply->affinity = thread->affinity;
1420 reply->last = thread->process->running_threads == 1;
1422 release_object( thread );
1426 /* fetch information about thread times */
1427 DECL_HANDLER(get_thread_times)
1429 struct thread *thread;
1431 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1433 reply->creation_time = thread->creation_time;
1434 reply->exit_time = thread->exit_time;
1436 release_object( thread );
1440 /* set information about a thread */
1441 DECL_HANDLER(set_thread_info)
1443 struct thread *thread;
1445 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1447 set_thread_info( thread, req );
1448 release_object( thread );
1452 /* suspend a thread */
1453 DECL_HANDLER(suspend_thread)
1455 struct thread *thread;
1457 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1459 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1460 else reply->count = suspend_thread( thread );
1461 release_object( thread );
1465 /* resume a thread */
1466 DECL_HANDLER(resume_thread)
1468 struct thread *thread;
1470 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1472 reply->count = resume_thread( thread );
1473 release_object( thread );
1477 /* select on a handle list */
1478 DECL_HANDLER(select)
1480 select_op_t select_op;
1481 data_size_t op_size;
1482 struct thread_apc *apc;
1483 const apc_result_t *result = get_req_data();
1485 if (get_req_data_size() < sizeof(*result))
1487 set_error( STATUS_INVALID_PARAMETER );
1488 return;
1490 if (!req->cookie)
1492 set_error( STATUS_INVALID_PARAMETER );
1493 return;
1496 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1497 memset( &select_op, 0, sizeof(select_op) );
1498 memcpy( &select_op, result + 1, op_size );
1500 /* first store results of previous apc */
1501 if (req->prev_apc)
1503 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1504 0, &thread_apc_ops ))) return;
1505 apc->result = *result;
1506 apc->executed = 1;
1507 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1509 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1510 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1511 close_handle( current->process, apc->result.create_thread.handle );
1512 apc->result.create_thread.handle = handle;
1513 clear_error(); /* ignore errors from the above calls */
1515 else if (apc->result.type == APC_ASYNC_IO)
1517 if (apc->owner)
1518 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total,
1519 apc->result.async_io.apc, apc->result.async_io.arg );
1521 wake_up( &apc->obj, 0 );
1522 close_handle( current->process, req->prev_apc );
1523 release_object( apc );
1526 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1528 if (get_error() == STATUS_USER_APC)
1530 for (;;)
1532 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1533 break;
1534 /* Optimization: ignore APC_NONE calls, they are only used to
1535 * wake up a thread, but since we got here the thread woke up already.
1537 if (apc->call.type != APC_NONE &&
1538 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1540 reply->call = apc->call;
1541 release_object( apc );
1542 break;
1544 apc->executed = 1;
1545 wake_up( &apc->obj, 0 );
1546 release_object( apc );
1551 /* queue an APC for a thread or process */
1552 DECL_HANDLER(queue_apc)
1554 struct thread *thread = NULL;
1555 struct process *process = NULL;
1556 struct thread_apc *apc;
1558 if (!(apc = create_apc( NULL, &req->call ))) return;
1560 switch (apc->call.type)
1562 case APC_NONE:
1563 case APC_USER:
1564 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1565 break;
1566 case APC_VIRTUAL_ALLOC:
1567 case APC_VIRTUAL_FREE:
1568 case APC_VIRTUAL_PROTECT:
1569 case APC_VIRTUAL_FLUSH:
1570 case APC_VIRTUAL_LOCK:
1571 case APC_VIRTUAL_UNLOCK:
1572 case APC_UNMAP_VIEW:
1573 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1574 break;
1575 case APC_VIRTUAL_QUERY:
1576 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1577 break;
1578 case APC_MAP_VIEW:
1579 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1580 if (process && process != current->process)
1582 /* duplicate the handle into the target process */
1583 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1584 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1585 if (handle) apc->call.map_view.handle = handle;
1586 else
1588 release_object( process );
1589 process = NULL;
1592 break;
1593 case APC_CREATE_THREAD:
1594 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1595 break;
1596 default:
1597 set_error( STATUS_INVALID_PARAMETER );
1598 break;
1601 if (thread)
1603 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1604 release_object( thread );
1606 else if (process)
1608 reply->self = (process == current->process);
1609 if (!reply->self)
1611 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1612 if (handle)
1614 if (queue_apc( process, NULL, apc ))
1616 apc->caller = (struct thread *)grab_object( current );
1617 reply->handle = handle;
1619 else
1621 close_handle( current->process, handle );
1622 set_error( STATUS_PROCESS_IS_TERMINATING );
1626 release_object( process );
1629 release_object( apc );
1632 /* Get the result of an APC call */
1633 DECL_HANDLER(get_apc_result)
1635 struct thread_apc *apc;
1637 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1638 0, &thread_apc_ops ))) return;
1640 if (apc->executed) reply->result = apc->result;
1641 else set_error( STATUS_PENDING );
1643 /* close the handle directly to avoid an extra round-trip */
1644 close_handle( current->process, req->handle );
1645 release_object( apc );
1648 /* retrieve the current context of a thread */
1649 DECL_HANDLER(get_thread_context)
1651 struct thread *thread;
1652 context_t *context;
1654 if (get_reply_max_size() < sizeof(context_t))
1656 set_error( STATUS_INVALID_PARAMETER );
1657 return;
1659 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1660 reply->self = (thread == current);
1662 if (thread != current && !thread->context)
1664 /* thread is not suspended, retry (if it's still running) */
1665 if (thread->state == RUNNING)
1667 set_error( STATUS_PENDING );
1668 if (req->suspend)
1670 release_object( thread );
1671 /* make sure we have suspend access */
1672 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1673 suspend_thread( thread );
1676 else set_error( STATUS_UNSUCCESSFUL );
1678 else if ((context = set_reply_data_size( sizeof(context_t) )))
1680 unsigned int flags = get_context_system_regs( thread->process->cpu );
1682 memset( context, 0, sizeof(context_t) );
1683 context->cpu = thread->process->cpu;
1684 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1685 if (flags) get_thread_context( thread, context, flags );
1687 release_object( thread );
1690 /* set the current context of a thread */
1691 DECL_HANDLER(set_thread_context)
1693 struct thread *thread;
1694 const context_t *context = get_req_data();
1696 if (get_req_data_size() < sizeof(context_t))
1698 set_error( STATUS_INVALID_PARAMETER );
1699 return;
1701 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1702 reply->self = (thread == current);
1704 if (thread != current && !thread->context)
1706 /* thread is not suspended, retry (if it's still running) */
1707 if (thread->state == RUNNING)
1709 set_error( STATUS_PENDING );
1710 if (req->suspend)
1712 release_object( thread );
1713 /* make sure we have suspend access */
1714 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1715 suspend_thread( thread );
1718 else set_error( STATUS_UNSUCCESSFUL );
1720 else if (context->cpu == thread->process->cpu)
1722 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1723 unsigned int client_flags = context->flags & ~system_flags;
1725 if (system_flags) set_thread_context( thread, context, system_flags );
1726 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1728 else set_error( STATUS_INVALID_PARAMETER );
1730 release_object( thread );
1733 /* retrieve the suspended context of a thread */
1734 DECL_HANDLER(get_suspend_context)
1736 if (get_reply_max_size() < sizeof(context_t))
1738 set_error( STATUS_INVALID_PARAMETER );
1739 return;
1742 if (current->suspend_context)
1744 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1745 if (current->context == current->suspend_context)
1747 current->context = NULL;
1748 stop_thread_if_suspended( current );
1750 current->suspend_context = NULL;
1752 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1755 /* store the suspended context of a thread */
1756 DECL_HANDLER(set_suspend_context)
1758 const context_t *context = get_req_data();
1760 if (get_req_data_size() < sizeof(context_t))
1762 set_error( STATUS_INVALID_PARAMETER );
1763 return;
1766 if (current->context || context->cpu != current->process->cpu)
1768 /* nested suspend or exception, shouldn't happen */
1769 set_error( STATUS_INVALID_PARAMETER );
1771 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1773 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1774 current->context = current->suspend_context;
1775 if (current->debug_break) break_thread( current );
1779 /* fetch a selector entry for a thread */
1780 DECL_HANDLER(get_selector_entry)
1782 struct thread *thread;
1783 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1785 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1786 release_object( thread );