ntdll: Fix the Mac build with SDKs older than 10.14.
[wine.git] / server / thread.c
blobe753c8d0dda47f4734e13e281d1732bed2d8a1e7
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_kernel_obj_list, /* get_kernel_obj_list */
124 no_close_handle, /* close_handle */
125 thread_apc_destroy /* destroy */
129 /* thread operations */
131 static void dump_thread( struct object *obj, int verbose );
132 static struct object_type *thread_get_type( struct object *obj );
133 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
134 static unsigned int thread_map_access( struct object *obj, unsigned int access );
135 static void thread_poll_event( struct fd *fd, int event );
136 static struct list *thread_get_kernel_obj_list( struct object *obj );
137 static void destroy_thread( struct object *obj );
139 static const struct object_ops thread_ops =
141 sizeof(struct thread), /* size */
142 dump_thread, /* dump */
143 thread_get_type, /* get_type */
144 add_queue, /* add_queue */
145 remove_queue, /* remove_queue */
146 thread_signaled, /* signaled */
147 no_satisfied, /* satisfied */
148 no_signal, /* signal */
149 no_get_fd, /* get_fd */
150 thread_map_access, /* map_access */
151 default_get_sd, /* get_sd */
152 default_set_sd, /* set_sd */
153 no_lookup_name, /* lookup_name */
154 no_link_name, /* link_name */
155 NULL, /* unlink_name */
156 no_open_file, /* open_file */
157 thread_get_kernel_obj_list, /* get_kernel_obj_list */
158 no_close_handle, /* close_handle */
159 destroy_thread /* destroy */
162 static const struct fd_ops thread_fd_ops =
164 NULL, /* get_poll_events */
165 thread_poll_event, /* poll_event */
166 NULL, /* flush */
167 NULL, /* get_fd_type */
168 NULL, /* ioctl */
169 NULL, /* queue_async */
170 NULL /* reselect_async */
173 static struct list thread_list = LIST_INIT(thread_list);
175 /* initialize the structure for a newly allocated thread */
176 static inline void init_thread_structure( struct thread *thread )
178 int i;
180 thread->unix_pid = -1; /* not known yet */
181 thread->unix_tid = -1; /* not known yet */
182 thread->context = NULL;
183 thread->suspend_context = NULL;
184 thread->teb = 0;
185 thread->entry_point = 0;
186 thread->debug_ctx = NULL;
187 thread->system_regs = 0;
188 thread->queue = NULL;
189 thread->wait = NULL;
190 thread->error = 0;
191 thread->req_data = NULL;
192 thread->req_toread = 0;
193 thread->reply_data = NULL;
194 thread->reply_towrite = 0;
195 thread->request_fd = NULL;
196 thread->reply_fd = NULL;
197 thread->wait_fd = NULL;
198 thread->state = RUNNING;
199 thread->exit_code = 0;
200 thread->priority = 0;
201 thread->suspend = 0;
202 thread->desktop_users = 0;
203 thread->token = NULL;
205 thread->creation_time = current_time;
206 thread->exit_time = 0;
208 list_init( &thread->mutex_list );
209 list_init( &thread->system_apc );
210 list_init( &thread->user_apc );
211 list_init( &thread->kernel_object );
213 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
214 thread->inflight[i].server = thread->inflight[i].client = -1;
217 /* check if address looks valid for a client-side data structure (TEB etc.) */
218 static inline int is_valid_address( client_ptr_t addr )
220 return addr && !(addr % sizeof(int));
223 /* create a new thread */
224 struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
226 struct thread *thread;
227 int request_pipe[2];
229 if (fd == -1)
231 if (pipe( request_pipe ) == -1)
233 file_set_error();
234 return NULL;
236 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
238 close( request_pipe[0] );
239 close( request_pipe[1] );
240 return NULL;
242 close( request_pipe[1] );
243 fd = request_pipe[0];
246 if (process->is_terminating)
248 close( fd );
249 set_error( STATUS_PROCESS_IS_TERMINATING );
250 return NULL;
253 if (!(thread = alloc_object( &thread_ops )))
255 close( fd );
256 return NULL;
259 init_thread_structure( thread );
261 thread->process = (struct process *)grab_object( process );
262 thread->desktop = process->desktop;
263 thread->affinity = process->affinity;
264 if (!current) current = thread;
266 list_add_head( &thread_list, &thread->entry );
268 if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
269 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
270 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
271 process->token ))
273 close( fd );
274 release_object( thread );
275 return NULL;
277 if (!(thread->id = alloc_ptid( thread )))
279 close( fd );
280 release_object( thread );
281 return NULL;
283 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
285 release_object( thread );
286 return NULL;
289 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
290 add_process_thread( thread->process, thread );
291 return thread;
294 /* handle a client event */
295 static void thread_poll_event( struct fd *fd, int event )
297 struct thread *thread = get_fd_user( fd );
298 assert( thread->obj.ops == &thread_ops );
300 grab_object( thread );
301 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
302 else if (event & POLLIN) read_request( thread );
303 else if (event & POLLOUT) write_reply( thread );
304 release_object( thread );
307 static struct list *thread_get_kernel_obj_list( struct object *obj )
309 struct thread *thread = (struct thread *)obj;
310 return &thread->kernel_object;
313 /* cleanup everything that is no longer needed by a dead thread */
314 /* used by destroy_thread and kill_thread */
315 static void cleanup_thread( struct thread *thread )
317 int i;
319 clear_apc_queue( &thread->system_apc );
320 clear_apc_queue( &thread->user_apc );
321 free( thread->req_data );
322 free( thread->reply_data );
323 if (thread->request_fd) release_object( thread->request_fd );
324 if (thread->reply_fd) release_object( thread->reply_fd );
325 if (thread->wait_fd) release_object( thread->wait_fd );
326 free( thread->suspend_context );
327 cleanup_clipboard_thread(thread);
328 destroy_thread_windows( thread );
329 free_msg_queue( thread );
330 close_thread_desktop( thread );
331 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
333 if (thread->inflight[i].client != -1)
335 close( thread->inflight[i].server );
336 thread->inflight[i].client = thread->inflight[i].server = -1;
339 thread->req_data = NULL;
340 thread->reply_data = NULL;
341 thread->request_fd = NULL;
342 thread->reply_fd = NULL;
343 thread->wait_fd = NULL;
344 thread->context = NULL;
345 thread->suspend_context = NULL;
346 thread->desktop = 0;
349 /* destroy a thread when its refcount is 0 */
350 static void destroy_thread( struct object *obj )
352 struct thread *thread = (struct thread *)obj;
353 assert( obj->ops == &thread_ops );
355 assert( !thread->debug_ctx ); /* cannot still be debugging something */
356 list_remove( &thread->entry );
357 cleanup_thread( thread );
358 release_object( thread->process );
359 if (thread->id) free_ptid( thread->id );
360 if (thread->token) release_object( thread->token );
363 /* dump a thread on stdout for debugging purposes */
364 static void dump_thread( struct object *obj, int verbose )
366 struct thread *thread = (struct thread *)obj;
367 assert( obj->ops == &thread_ops );
369 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
370 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
373 static struct object_type *thread_get_type( struct object *obj )
375 static const WCHAR name[] = {'T','h','r','e','a','d'};
376 static const struct unicode_str str = { name, sizeof(name) };
377 return get_object_type( &str );
380 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
382 struct thread *mythread = (struct thread *)obj;
383 return (mythread->state == TERMINATED);
386 static unsigned int thread_map_access( struct object *obj, unsigned int access )
388 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
389 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
390 THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
391 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
392 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
394 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
395 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
397 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
400 static void dump_thread_apc( struct object *obj, int verbose )
402 struct thread_apc *apc = (struct thread_apc *)obj;
403 assert( obj->ops == &thread_apc_ops );
405 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
408 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
410 struct thread_apc *apc = (struct thread_apc *)obj;
411 return apc->executed;
414 static void thread_apc_destroy( struct object *obj )
416 struct thread_apc *apc = (struct thread_apc *)obj;
417 if (apc->caller) release_object( apc->caller );
418 if (apc->owner) release_object( apc->owner );
421 /* queue an async procedure call */
422 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
424 struct thread_apc *apc;
426 if ((apc = alloc_object( &thread_apc_ops )))
428 apc->call = *call_data;
429 apc->caller = NULL;
430 apc->owner = owner;
431 apc->executed = 0;
432 apc->result.type = APC_NONE;
433 if (owner) grab_object( owner );
435 return apc;
438 /* get a thread pointer from a thread id (and increment the refcount) */
439 struct thread *get_thread_from_id( thread_id_t id )
441 struct object *obj = get_ptid_entry( id );
443 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
444 set_error( STATUS_INVALID_CID );
445 return NULL;
448 /* get a thread from a handle (and increment the refcount) */
449 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
451 return (struct thread *)get_handle_obj( current->process, handle,
452 access, &thread_ops );
455 /* find a thread from a Unix tid */
456 struct thread *get_thread_from_tid( int tid )
458 struct thread *thread;
460 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
462 if (thread->unix_tid == tid) return thread;
464 return NULL;
467 /* find a thread from a Unix pid */
468 struct thread *get_thread_from_pid( int pid )
470 struct thread *thread;
472 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
474 if (thread->unix_pid == pid) return thread;
476 return NULL;
479 int set_thread_affinity( struct thread *thread, affinity_t affinity )
481 int ret = 0;
482 #ifdef HAVE_SCHED_SETAFFINITY
483 if (thread->unix_tid != -1)
485 cpu_set_t set;
486 int i;
487 affinity_t mask;
489 CPU_ZERO( &set );
490 for (i = 0, mask = 1; mask; i++, mask <<= 1)
491 if (affinity & mask) CPU_SET( i, &set );
493 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
495 #endif
496 if (!ret) thread->affinity = affinity;
497 return ret;
500 affinity_t get_thread_affinity( struct thread *thread )
502 affinity_t mask = 0;
503 #ifdef HAVE_SCHED_SETAFFINITY
504 if (thread->unix_tid != -1)
506 cpu_set_t set;
507 unsigned int i;
509 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
510 for (i = 0; i < 8 * sizeof(mask); i++)
511 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
513 #endif
514 if (!mask) mask = ~(affinity_t)0;
515 return mask;
518 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
519 #define THREAD_PRIORITY_REALTIME_LOWEST -7
521 /* set all information about a thread */
522 static void set_thread_info( struct thread *thread,
523 const struct set_thread_info_request *req )
525 if (req->mask & SET_THREAD_INFO_PRIORITY)
527 int max = THREAD_PRIORITY_HIGHEST;
528 int min = THREAD_PRIORITY_LOWEST;
529 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
531 max = THREAD_PRIORITY_REALTIME_HIGHEST;
532 min = THREAD_PRIORITY_REALTIME_LOWEST;
534 if ((req->priority >= min && req->priority <= max) ||
535 req->priority == THREAD_PRIORITY_IDLE ||
536 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
537 thread->priority = req->priority;
538 else
539 set_error( STATUS_INVALID_PARAMETER );
541 if (req->mask & SET_THREAD_INFO_AFFINITY)
543 if ((req->affinity & thread->process->affinity) != req->affinity)
544 set_error( STATUS_INVALID_PARAMETER );
545 else if (thread->state == TERMINATED)
546 set_error( STATUS_THREAD_IS_TERMINATING );
547 else if (set_thread_affinity( thread, req->affinity ))
548 file_set_error();
550 if (req->mask & SET_THREAD_INFO_TOKEN)
551 security_set_thread_token( thread, req->token );
552 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
553 thread->entry_point = req->entry_point;
556 /* stop a thread (at the Unix level) */
557 void stop_thread( struct thread *thread )
559 if (thread->context) return; /* already inside a debug event, no need for a signal */
560 /* can't stop a thread while initialisation is in progress */
561 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
564 /* stop a thread if it's supposed to be suspended */
565 void stop_thread_if_suspended( struct thread *thread )
567 if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
570 /* suspend a thread */
571 int suspend_thread( struct thread *thread )
573 int old_count = thread->suspend;
574 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
576 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
578 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
579 return old_count;
582 /* resume a thread */
583 int resume_thread( struct thread *thread )
585 int old_count = thread->suspend;
586 if (thread->suspend > 0)
588 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
590 return old_count;
593 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
594 int add_queue( struct object *obj, struct wait_queue_entry *entry )
596 grab_object( obj );
597 entry->obj = obj;
598 list_add_tail( &obj->wait_queue, &entry->entry );
599 return 1;
602 /* remove a thread from an object wait queue */
603 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
605 list_remove( &entry->entry );
606 release_object( obj );
609 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
611 return entry->wait->thread;
614 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
616 return entry->wait->select;
619 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
621 return entry->wait->key;
624 void make_wait_abandoned( struct wait_queue_entry *entry )
626 entry->wait->abandoned = 1;
629 /* finish waiting */
630 static unsigned int end_wait( struct thread *thread, unsigned int status )
632 struct thread_wait *wait = thread->wait;
633 struct wait_queue_entry *entry;
634 int i;
636 assert( wait );
637 thread->wait = wait->next;
639 if (status < wait->count) /* wait satisfied, tell it to the objects */
641 if (wait->select == SELECT_WAIT_ALL)
643 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
644 entry->obj->ops->satisfied( entry->obj, entry );
646 else
648 entry = wait->queues + status;
649 entry->obj->ops->satisfied( entry->obj, entry );
651 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
653 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
654 entry->obj->ops->remove_queue( entry->obj, entry );
655 if (wait->user) remove_timeout_user( wait->user );
656 free( wait );
657 return status;
660 /* build the thread wait structure */
661 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
662 int flags, timeout_t timeout )
664 struct thread_wait *wait;
665 struct wait_queue_entry *entry;
666 unsigned int i;
668 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
669 wait->next = current->wait;
670 wait->thread = current;
671 wait->count = count;
672 wait->flags = flags;
673 wait->select = select_op->op;
674 wait->cookie = 0;
675 wait->user = NULL;
676 wait->timeout = timeout;
677 wait->abandoned = 0;
678 current->wait = wait;
680 for (i = 0, entry = wait->queues; i < count; i++, entry++)
682 struct object *obj = objects[i];
683 entry->wait = wait;
684 if (!obj->ops->add_queue( obj, entry ))
686 wait->count = i;
687 end_wait( current, get_error() );
688 return 0;
691 return 1;
694 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
695 int flags, timeout_t timeout )
697 struct object *objects[MAXIMUM_WAIT_OBJECTS];
698 unsigned int i;
699 int ret = 0;
701 assert( count <= MAXIMUM_WAIT_OBJECTS );
703 for (i = 0; i < count; i++)
704 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
705 break;
707 if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
709 while (i > 0) release_object( objects[--i] );
710 return ret;
713 /* check if the thread waiting condition is satisfied */
714 static int check_wait( struct thread *thread )
716 int i;
717 struct thread_wait *wait = thread->wait;
718 struct wait_queue_entry *entry;
720 assert( wait );
722 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
723 return STATUS_USER_APC;
725 /* Suspended threads may not acquire locks, but they can run system APCs */
726 if (thread->process->suspend + thread->suspend > 0) return -1;
728 if (wait->select == SELECT_WAIT_ALL)
730 int not_ok = 0;
731 /* Note: we must check them all anyway, as some objects may
732 * want to do something when signaled, even if others are not */
733 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
734 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
735 if (!not_ok) return STATUS_WAIT_0;
737 else
739 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
740 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
743 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
744 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
745 return -1;
748 /* send the wakeup signal to a thread */
749 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
751 struct wake_up_reply reply;
752 int ret;
754 memset( &reply, 0, sizeof(reply) );
755 reply.cookie = cookie;
756 reply.signaled = signaled;
757 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
758 return 0;
759 if (ret >= 0)
760 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
761 else if (errno == EPIPE)
762 kill_thread( thread, 0 ); /* normal death */
763 else
764 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
765 return -1;
768 /* attempt to wake up a thread */
769 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
770 int wake_thread( struct thread *thread )
772 int signaled, count;
773 client_ptr_t cookie;
775 for (count = 0; thread->wait; count++)
777 if ((signaled = check_wait( thread )) == -1) break;
779 cookie = thread->wait->cookie;
780 signaled = end_wait( thread, signaled );
781 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
782 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
784 if (!count) count = -1;
785 break;
788 return count;
791 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
792 int wake_thread_queue_entry( struct wait_queue_entry *entry )
794 struct thread_wait *wait = entry->wait;
795 struct thread *thread = wait->thread;
796 int signaled;
797 client_ptr_t cookie;
799 if (thread->wait != wait) return 0; /* not the current wait */
800 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
802 assert( wait->select != SELECT_WAIT_ALL );
804 cookie = wait->cookie;
805 signaled = end_wait( thread, entry - wait->queues );
806 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
808 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
809 wake_thread( thread ); /* check other waits too */
811 return 1;
814 /* thread wait timeout */
815 static void thread_timeout( void *ptr )
817 struct thread_wait *wait = ptr;
818 struct thread *thread = wait->thread;
819 client_ptr_t cookie = wait->cookie;
821 wait->user = NULL;
822 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
823 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
825 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
826 end_wait( thread, STATUS_TIMEOUT );
828 assert( cookie );
829 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
830 /* check if other objects have become signaled in the meantime */
831 wake_thread( thread );
834 /* try signaling an event flag, a semaphore or a mutex */
835 static int signal_object( obj_handle_t handle )
837 struct object *obj;
838 int ret = 0;
840 obj = get_handle_obj( current->process, handle, 0, NULL );
841 if (obj)
843 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
844 release_object( obj );
846 return ret;
849 /* select on a list of handles */
850 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
851 int flags, timeout_t timeout )
853 int ret;
854 unsigned int count;
855 struct object *object;
857 if (timeout <= 0) timeout = current_time - timeout;
859 switch (select_op->op)
861 case SELECT_NONE:
862 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
863 break;
865 case SELECT_WAIT:
866 case SELECT_WAIT_ALL:
867 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
868 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
870 set_error( STATUS_INVALID_PARAMETER );
871 return 0;
873 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
874 return timeout;
875 break;
877 case SELECT_SIGNAL_AND_WAIT:
878 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
879 return timeout;
880 if (select_op->signal_and_wait.signal)
882 if (!signal_object( select_op->signal_and_wait.signal ))
884 end_wait( current, get_error() );
885 return timeout;
887 /* check if we woke ourselves up */
888 if (!current->wait) return timeout;
890 break;
892 case SELECT_KEYED_EVENT_WAIT:
893 case SELECT_KEYED_EVENT_RELEASE:
894 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
895 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
896 if (!object) return timeout;
897 ret = wait_on( select_op, 1, &object, flags, timeout );
898 release_object( object );
899 if (!ret) return timeout;
900 current->wait->key = select_op->keyed_event.key;
901 break;
903 default:
904 set_error( STATUS_INVALID_PARAMETER );
905 return 0;
908 if ((ret = check_wait( current )) != -1)
910 /* condition is already satisfied */
911 set_error( end_wait( current, ret ));
912 return timeout;
915 /* now we need to wait */
916 if (current->wait->timeout != TIMEOUT_INFINITE)
918 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
919 thread_timeout, current->wait )))
921 end_wait( current, get_error() );
922 return timeout;
925 current->wait->cookie = cookie;
926 set_error( STATUS_PENDING );
927 return timeout;
930 /* attempt to wake threads sleeping on the object wait queue */
931 void wake_up( struct object *obj, int max )
933 struct list *ptr;
934 int ret;
936 LIST_FOR_EACH( ptr, &obj->wait_queue )
938 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
939 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
940 if (ret > 0 && max && !--max) break;
941 /* restart at the head of the list since a wake up can change the object wait queue */
942 ptr = &obj->wait_queue;
946 /* return the apc queue to use for a given apc type */
947 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
949 switch(type)
951 case APC_NONE:
952 case APC_USER:
953 case APC_TIMER:
954 return &thread->user_apc;
955 default:
956 return &thread->system_apc;
960 /* check if thread is currently waiting for a (system) apc */
961 static inline int is_in_apc_wait( struct thread *thread )
963 return (thread->process->suspend || thread->suspend ||
964 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
967 /* queue an existing APC to a given thread */
968 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
970 struct list *queue;
972 if (thread && thread->state == TERMINATED && process)
973 thread = NULL;
975 if (!thread) /* find a suitable thread inside the process */
977 struct thread *candidate;
979 /* first try to find a waiting thread */
980 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
982 if (candidate->state == TERMINATED) continue;
983 if (is_in_apc_wait( candidate ))
985 thread = candidate;
986 break;
989 if (!thread)
991 /* then use the first one that accepts a signal */
992 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
994 if (send_thread_signal( candidate, SIGUSR1 ))
996 thread = candidate;
997 break;
1001 if (!thread) return 0; /* nothing found */
1002 queue = get_apc_queue( thread, apc->call.type );
1004 else
1006 if (thread->state == TERMINATED) return 0;
1007 queue = get_apc_queue( thread, apc->call.type );
1008 /* send signal for system APCs if needed */
1009 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
1011 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
1013 /* cancel a possible previous APC with the same owner */
1014 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
1017 grab_object( apc );
1018 list_add_tail( queue, &apc->entry );
1019 if (!list_prev( queue, &apc->entry )) /* first one */
1020 wake_thread( thread );
1022 return 1;
1025 /* queue an async procedure call */
1026 int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1028 struct thread_apc *apc;
1029 int ret = 0;
1031 if ((apc = create_apc( owner, call_data )))
1033 ret = queue_apc( process, thread, apc );
1034 release_object( apc );
1036 return ret;
1039 /* cancel the async procedure call owned by a specific object */
1040 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1042 struct thread_apc *apc;
1043 struct list *queue = get_apc_queue( thread, type );
1045 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1047 if (apc->owner != owner) continue;
1048 list_remove( &apc->entry );
1049 apc->executed = 1;
1050 wake_up( &apc->obj, 0 );
1051 release_object( apc );
1052 return;
1056 /* remove the head apc from the queue; the returned object must be released by the caller */
1057 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
1059 struct thread_apc *apc = NULL;
1060 struct list *ptr = list_head( &thread->system_apc );
1062 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
1063 if (ptr)
1065 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1066 list_remove( ptr );
1068 return apc;
1071 /* clear an APC queue, cancelling all the APCs on it */
1072 static void clear_apc_queue( struct list *queue )
1074 struct list *ptr;
1076 while ((ptr = list_head( queue )))
1078 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1079 list_remove( &apc->entry );
1080 apc->executed = 1;
1081 wake_up( &apc->obj, 0 );
1082 release_object( apc );
1086 /* add an fd to the inflight list */
1087 /* return list index, or -1 on error */
1088 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1090 int i;
1092 if (server == -1) return -1;
1093 if (client == -1)
1095 close( server );
1096 return -1;
1099 /* first check if we already have an entry for this fd */
1100 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1101 if (thread->inflight[i].client == client)
1103 close( thread->inflight[i].server );
1104 thread->inflight[i].server = server;
1105 return i;
1108 /* now find a free spot to store it */
1109 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1110 if (thread->inflight[i].client == -1)
1112 thread->inflight[i].client = client;
1113 thread->inflight[i].server = server;
1114 return i;
1117 close( server );
1118 return -1;
1121 /* get an inflight fd and purge it from the list */
1122 /* the fd must be closed when no longer used */
1123 int thread_get_inflight_fd( struct thread *thread, int client )
1125 int i, ret;
1127 if (client == -1) return -1;
1131 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1133 if (thread->inflight[i].client == client)
1135 ret = thread->inflight[i].server;
1136 thread->inflight[i].server = thread->inflight[i].client = -1;
1137 return ret;
1140 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1141 return -1;
1144 /* kill a thread on the spot */
1145 void kill_thread( struct thread *thread, int violent_death )
1147 if (thread->state == TERMINATED) return; /* already killed */
1148 thread->state = TERMINATED;
1149 thread->exit_time = current_time;
1150 if (current == thread) current = NULL;
1151 if (debug_level)
1152 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1153 thread->id, thread->exit_code );
1154 if (thread->wait)
1156 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1157 send_thread_wakeup( thread, 0, thread->exit_code );
1158 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1159 violent_death = 0;
1161 kill_console_processes( thread, 0 );
1162 debug_exit_thread( thread );
1163 abandon_mutexes( thread );
1164 wake_up( &thread->obj, 0 );
1165 if (violent_death) send_thread_signal( thread, SIGQUIT );
1166 cleanup_thread( thread );
1167 remove_process_thread( thread->process, thread );
1168 release_object( thread );
1171 /* copy parts of a context structure */
1172 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1174 assert( to->cpu == from->cpu );
1175 to->flags |= flags;
1176 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1177 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1178 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1179 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1180 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1181 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1184 /* return the context flags that correspond to system regs */
1185 /* (system regs are the ones we can't access on the client side) */
1186 static unsigned int get_context_system_regs( enum cpu_type cpu )
1188 switch (cpu)
1190 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1191 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1192 case CPU_POWERPC: return 0;
1193 case CPU_ARM: return SERVER_CTX_DEBUG_REGISTERS;
1194 case CPU_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
1196 return 0;
1199 /* take a snapshot of currently running threads */
1200 struct thread_snapshot *thread_snap( int *count )
1202 struct thread_snapshot *snapshot, *ptr;
1203 struct thread *thread;
1204 int total = 0;
1206 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1207 if (thread->state != TERMINATED) total++;
1208 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1209 ptr = snapshot;
1210 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1212 if (thread->state == TERMINATED) continue;
1213 ptr->thread = thread;
1214 ptr->count = thread->obj.refcount;
1215 ptr->priority = thread->priority;
1216 grab_object( thread );
1217 ptr++;
1219 *count = total;
1220 return snapshot;
1223 /* gets the current impersonation token */
1224 struct token *thread_get_impersonation_token( struct thread *thread )
1226 if (thread->token)
1227 return thread->token;
1228 else
1229 return thread->process->token;
1232 /* check if a cpu type can be supported on this server */
1233 int is_cpu_supported( enum cpu_type cpu )
1235 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1237 if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
1238 if (!(supported_cpus & prefix_cpu_mask))
1239 set_error( STATUS_NOT_SUPPORTED );
1240 else if (supported_cpus & CPU_FLAG(cpu))
1241 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1242 else
1243 set_error( STATUS_INVALID_IMAGE_FORMAT );
1244 return 0;
1247 /* return the cpu mask for supported cpus */
1248 unsigned int get_supported_cpu_mask(void)
1250 return supported_cpus & get_prefix_cpu_mask();
1253 /* create a new thread */
1254 DECL_HANDLER(new_thread)
1256 struct thread *thread;
1257 struct process *process;
1258 struct unicode_str name;
1259 const struct security_descriptor *sd;
1260 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1261 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1263 if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1265 if (request_fd != -1) close( request_fd );
1266 return;
1269 if (process != current->process)
1271 if (request_fd != -1) /* can't create a request fd in a different process */
1273 close( request_fd );
1274 set_error( STATUS_INVALID_PARAMETER );
1275 goto done;
1277 if (process->running_threads) /* only the initial thread can be created in another process */
1279 set_error( STATUS_ACCESS_DENIED );
1280 goto done;
1283 else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1285 if (request_fd != -1) close( request_fd );
1286 set_error( STATUS_INVALID_HANDLE );
1287 goto done;
1290 if ((thread = create_thread( request_fd, process, sd )))
1292 thread->system_regs = current->system_regs;
1293 if (req->suspend) thread->suspend++;
1294 reply->tid = get_thread_id( thread );
1295 if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
1296 req->access, objattr->attributes )))
1298 /* thread object will be released when the thread gets killed */
1299 goto done;
1301 kill_thread( thread, 1 );
1303 done:
1304 release_object( process );
1307 /* initialize a new thread */
1308 DECL_HANDLER(init_thread)
1310 struct process *process = current->process;
1311 int wait_fd, reply_fd;
1313 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1315 set_error( STATUS_TOO_MANY_OPENED_FILES );
1316 return;
1318 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1320 set_error( STATUS_TOO_MANY_OPENED_FILES );
1321 goto error;
1324 if (current->reply_fd) /* already initialised */
1326 set_error( STATUS_INVALID_PARAMETER );
1327 goto error;
1330 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1332 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1333 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1334 if (!current->reply_fd || !current->wait_fd) return;
1336 if (!is_valid_address(req->teb))
1338 set_error( STATUS_INVALID_PARAMETER );
1339 return;
1342 current->unix_pid = req->unix_pid;
1343 current->unix_tid = req->unix_tid;
1344 current->teb = req->teb;
1345 current->entry_point = process->peb ? req->entry : 0;
1347 if (!process->peb) /* first thread, initialize the process too */
1349 if (!is_cpu_supported( req->cpu )) return;
1350 process->unix_pid = current->unix_pid;
1351 process->peb = req->entry;
1352 process->cpu = req->cpu;
1353 reply->info_size = init_process( current );
1354 if (!process->parent_id)
1355 process->affinity = current->affinity = get_thread_affinity( current );
1356 else
1357 set_thread_affinity( current, current->affinity );
1359 else
1361 if (req->cpu != process->cpu)
1363 set_error( STATUS_INVALID_PARAMETER );
1364 return;
1366 if (process->unix_pid != current->unix_pid)
1367 process->unix_pid = -1; /* can happen with linuxthreads */
1368 init_thread_context( current );
1369 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1370 set_thread_affinity( current, current->affinity );
1372 debug_level = max( debug_level, req->debug_level );
1374 reply->pid = get_process_id( process );
1375 reply->tid = get_thread_id( current );
1376 reply->version = SERVER_PROTOCOL_VERSION;
1377 reply->server_start = server_start_time;
1378 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1379 reply->suspend = (current->suspend || process->suspend);
1380 return;
1382 error:
1383 if (reply_fd != -1) close( reply_fd );
1384 if (wait_fd != -1) close( wait_fd );
1387 /* terminate a thread */
1388 DECL_HANDLER(terminate_thread)
1390 struct thread *thread;
1392 reply->self = 0;
1393 reply->last = 0;
1394 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1396 thread->exit_code = req->exit_code;
1397 if (thread != current) kill_thread( thread, 1 );
1398 else
1400 reply->self = 1;
1401 reply->last = (thread->process->running_threads == 1);
1403 release_object( thread );
1407 /* open a handle to a thread */
1408 DECL_HANDLER(open_thread)
1410 struct thread *thread = get_thread_from_id( req->tid );
1412 reply->handle = 0;
1413 if (thread)
1415 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1416 release_object( thread );
1420 /* fetch information about a thread */
1421 DECL_HANDLER(get_thread_info)
1423 struct thread *thread;
1424 obj_handle_t handle = req->handle;
1426 if (!handle) thread = get_thread_from_id( req->tid_in );
1427 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION );
1429 if (thread)
1431 reply->pid = get_process_id( thread->process );
1432 reply->tid = get_thread_id( thread );
1433 reply->teb = thread->teb;
1434 reply->entry_point = thread->entry_point;
1435 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1436 reply->priority = thread->priority;
1437 reply->affinity = thread->affinity;
1438 reply->last = thread->process->running_threads == 1;
1440 release_object( thread );
1444 /* fetch information about thread times */
1445 DECL_HANDLER(get_thread_times)
1447 struct thread *thread;
1449 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1451 reply->creation_time = thread->creation_time;
1452 reply->exit_time = thread->exit_time;
1454 release_object( thread );
1458 /* set information about a thread */
1459 DECL_HANDLER(set_thread_info)
1461 struct thread *thread;
1463 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1465 set_thread_info( thread, req );
1466 release_object( thread );
1470 /* suspend a thread */
1471 DECL_HANDLER(suspend_thread)
1473 struct thread *thread;
1475 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1477 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1478 else reply->count = suspend_thread( thread );
1479 release_object( thread );
1483 /* resume a thread */
1484 DECL_HANDLER(resume_thread)
1486 struct thread *thread;
1488 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1490 reply->count = resume_thread( thread );
1491 release_object( thread );
1495 /* select on a handle list */
1496 DECL_HANDLER(select)
1498 select_op_t select_op;
1499 data_size_t op_size;
1500 struct thread_apc *apc;
1501 const apc_result_t *result = get_req_data();
1503 if (get_req_data_size() < sizeof(*result))
1505 set_error( STATUS_INVALID_PARAMETER );
1506 return;
1508 if (!req->cookie)
1510 set_error( STATUS_INVALID_PARAMETER );
1511 return;
1514 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1515 memset( &select_op, 0, sizeof(select_op) );
1516 memcpy( &select_op, result + 1, op_size );
1518 /* first store results of previous apc */
1519 if (req->prev_apc)
1521 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1522 0, &thread_apc_ops ))) return;
1523 apc->result = *result;
1524 apc->executed = 1;
1525 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1527 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1528 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1529 close_handle( current->process, apc->result.create_thread.handle );
1530 apc->result.create_thread.handle = handle;
1531 clear_error(); /* ignore errors from the above calls */
1533 else if (apc->result.type == APC_ASYNC_IO)
1535 if (apc->owner)
1536 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
1538 wake_up( &apc->obj, 0 );
1539 close_handle( current->process, req->prev_apc );
1540 release_object( apc );
1543 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1545 if (get_error() == STATUS_USER_APC)
1547 for (;;)
1549 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1550 break;
1551 /* Optimization: ignore APC_NONE calls, they are only used to
1552 * wake up a thread, but since we got here the thread woke up already.
1554 if (apc->call.type != APC_NONE &&
1555 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1557 reply->call = apc->call;
1558 release_object( apc );
1559 break;
1561 apc->executed = 1;
1562 wake_up( &apc->obj, 0 );
1563 release_object( apc );
1568 /* queue an APC for a thread or process */
1569 DECL_HANDLER(queue_apc)
1571 struct thread *thread = NULL;
1572 struct process *process = NULL;
1573 struct thread_apc *apc;
1575 if (!(apc = create_apc( NULL, &req->call ))) return;
1577 switch (apc->call.type)
1579 case APC_NONE:
1580 case APC_USER:
1581 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1582 break;
1583 case APC_VIRTUAL_ALLOC:
1584 case APC_VIRTUAL_FREE:
1585 case APC_VIRTUAL_PROTECT:
1586 case APC_VIRTUAL_FLUSH:
1587 case APC_VIRTUAL_LOCK:
1588 case APC_VIRTUAL_UNLOCK:
1589 case APC_UNMAP_VIEW:
1590 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1591 break;
1592 case APC_VIRTUAL_QUERY:
1593 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1594 break;
1595 case APC_MAP_VIEW:
1596 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1597 if (process && process != current->process)
1599 /* duplicate the handle into the target process */
1600 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1601 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1602 if (handle) apc->call.map_view.handle = handle;
1603 else
1605 release_object( process );
1606 process = NULL;
1609 break;
1610 case APC_CREATE_THREAD:
1611 case APC_BREAK_PROCESS:
1612 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1613 break;
1614 default:
1615 set_error( STATUS_INVALID_PARAMETER );
1616 break;
1619 if (thread)
1621 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1622 release_object( thread );
1624 else if (process)
1626 reply->self = (process == current->process);
1627 if (!reply->self)
1629 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1630 if (handle)
1632 if (queue_apc( process, NULL, apc ))
1634 apc->caller = (struct thread *)grab_object( current );
1635 reply->handle = handle;
1637 else
1639 close_handle( current->process, handle );
1640 set_error( STATUS_PROCESS_IS_TERMINATING );
1644 release_object( process );
1647 release_object( apc );
1650 /* Get the result of an APC call */
1651 DECL_HANDLER(get_apc_result)
1653 struct thread_apc *apc;
1655 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1656 0, &thread_apc_ops ))) return;
1658 if (apc->executed) reply->result = apc->result;
1659 else set_error( STATUS_PENDING );
1661 /* close the handle directly to avoid an extra round-trip */
1662 close_handle( current->process, req->handle );
1663 release_object( apc );
1666 /* retrieve the current context of a thread */
1667 DECL_HANDLER(get_thread_context)
1669 struct thread *thread;
1670 context_t *context;
1672 if (get_reply_max_size() < sizeof(context_t))
1674 set_error( STATUS_INVALID_PARAMETER );
1675 return;
1677 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1678 reply->self = (thread == current);
1680 if (thread != current && !thread->context)
1682 /* thread is not suspended, retry (if it's still running) */
1683 if (thread->state == RUNNING)
1685 set_error( STATUS_PENDING );
1686 if (req->suspend)
1688 release_object( thread );
1689 /* make sure we have suspend access */
1690 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1691 suspend_thread( thread );
1694 else set_error( STATUS_UNSUCCESSFUL );
1696 else if ((context = set_reply_data_size( sizeof(context_t) )))
1698 unsigned int flags = get_context_system_regs( thread->process->cpu );
1700 memset( context, 0, sizeof(context_t) );
1701 context->cpu = thread->process->cpu;
1702 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1703 if (req->flags & flags) get_thread_context( thread, context, req->flags & flags );
1705 release_object( thread );
1708 /* set the current context of a thread */
1709 DECL_HANDLER(set_thread_context)
1711 struct thread *thread;
1712 const context_t *context = get_req_data();
1714 if (get_req_data_size() < sizeof(context_t))
1716 set_error( STATUS_INVALID_PARAMETER );
1717 return;
1719 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1720 reply->self = (thread == current);
1722 if (thread != current && !thread->context)
1724 /* thread is not suspended, retry (if it's still running) */
1725 if (thread->state == RUNNING)
1727 set_error( STATUS_PENDING );
1728 if (req->suspend)
1730 release_object( thread );
1731 /* make sure we have suspend access */
1732 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1733 suspend_thread( thread );
1736 else set_error( STATUS_UNSUCCESSFUL );
1738 else if (context->cpu == thread->process->cpu)
1740 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1741 unsigned int client_flags = context->flags & ~system_flags;
1743 if (system_flags) set_thread_context( thread, context, system_flags );
1744 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1746 else set_error( STATUS_INVALID_PARAMETER );
1748 release_object( thread );
1751 /* retrieve the suspended context of a thread */
1752 DECL_HANDLER(get_suspend_context)
1754 if (get_reply_max_size() < sizeof(context_t))
1756 set_error( STATUS_INVALID_PARAMETER );
1757 return;
1760 if (current->suspend_context)
1762 if (current->suspend_context->flags)
1763 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1764 else
1765 free( current->suspend_context );
1766 if (current->context == current->suspend_context)
1768 current->context = NULL;
1769 stop_thread_if_suspended( current );
1771 current->suspend_context = NULL;
1773 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1776 /* store the suspended context of a thread */
1777 DECL_HANDLER(set_suspend_context)
1779 const context_t *context = get_req_data();
1781 if (get_req_data_size() < sizeof(context_t))
1783 set_error( STATUS_INVALID_PARAMETER );
1784 return;
1787 if (current->context || context->cpu != current->process->cpu)
1789 /* nested suspend or exception, shouldn't happen */
1790 set_error( STATUS_INVALID_PARAMETER );
1792 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1794 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1795 current->suspend_context->flags = 0; /* to keep track of what is modified */
1796 current->context = current->suspend_context;
1800 /* fetch a selector entry for a thread */
1801 DECL_HANDLER(get_selector_entry)
1803 struct thread *thread;
1804 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1806 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1807 release_object( thread );