server: Initial support for Wow64 registry redirection.
[wine] / server / thread.c
1 /*
2  * Server-side thread management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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
41
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
45 #include "winternl.h"
46
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"
54
55
56 #define CPU_FLAG(cpu) (1 << (cpu))
57 #ifdef __i386__
58 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86);
59 #elif defined(__x86_64__)
60 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86_64) | CPU_FLAG(CPU_x86);
61 #elif defined(__ALPHA__)
62 static const unsigned int supported_cpus = CPU_FLAG(CPU_ALPHA);
63 #elif defined(__powerpc__)
64 static const unsigned int supported_cpus = CPU_FLAG(CPU_POWERPC);
65 #elif defined(__sparc__)
66 static const unsigned int supported_cpus = CPU_FLAG(CPU_SPARC);
67 #else
68 #error Unsupported CPU
69 #endif
70 #define CPU_64BIT_MASK CPU_FLAG(CPU_x86_64)
71
72 /* thread queues */
73
74 struct thread_wait
75 {
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     client_ptr_t            cookie;     /* magic cookie to return to client */
81     timeout_t               timeout;
82     struct timeout_user    *user;
83     struct wait_queue_entry queues[1];
84 };
85
86 /* asynchronous procedure calls */
87
88 struct thread_apc
89 {
90     struct object       obj;      /* object header */
91     struct list         entry;    /* queue linked list */
92     struct thread      *caller;   /* thread that queued this apc */
93     struct object      *owner;    /* object that queued this apc */
94     int                 executed; /* has it been executed by the client? */
95     apc_call_t          call;     /* call arguments */
96     apc_result_t        result;   /* call results once executed */
97 };
98
99 static void dump_thread_apc( struct object *obj, int verbose );
100 static int thread_apc_signaled( struct object *obj, struct thread *thread );
101 static void thread_apc_destroy( struct object *obj );
102 static void clear_apc_queue( struct list *queue );
103
104 static const struct object_ops thread_apc_ops =
105 {
106     sizeof(struct thread_apc),  /* size */
107     dump_thread_apc,            /* dump */
108     no_get_type,                /* get_type */
109     add_queue,                  /* add_queue */
110     remove_queue,               /* remove_queue */
111     thread_apc_signaled,        /* signaled */
112     no_satisfied,               /* satisfied */
113     no_signal,                  /* signal */
114     no_get_fd,                  /* get_fd */
115     no_map_access,              /* map_access */
116     default_get_sd,             /* get_sd */
117     default_set_sd,             /* set_sd */
118     no_lookup_name,             /* lookup_name */
119     no_open_file,               /* open_file */
120     no_close_handle,            /* close_handle */
121     thread_apc_destroy          /* destroy */
122 };
123
124
125 /* thread operations */
126
127 static void dump_thread( struct object *obj, int verbose );
128 static int thread_signaled( struct object *obj, struct thread *thread );
129 static unsigned int thread_map_access( struct object *obj, unsigned int access );
130 static void thread_poll_event( struct fd *fd, int event );
131 static void destroy_thread( struct object *obj );
132
133 static const struct object_ops thread_ops =
134 {
135     sizeof(struct thread),      /* size */
136     dump_thread,                /* dump */
137     no_get_type,                /* get_type */
138     add_queue,                  /* add_queue */
139     remove_queue,               /* remove_queue */
140     thread_signaled,            /* signaled */
141     no_satisfied,               /* satisfied */
142     no_signal,                  /* signal */
143     no_get_fd,                  /* get_fd */
144     thread_map_access,          /* map_access */
145     default_get_sd,             /* get_sd */
146     default_set_sd,             /* set_sd */
147     no_lookup_name,             /* lookup_name */
148     no_open_file,               /* open_file */
149     no_close_handle,            /* close_handle */
150     destroy_thread              /* destroy */
151 };
152
153 static const struct fd_ops thread_fd_ops =
154 {
155     NULL,                       /* get_poll_events */
156     thread_poll_event,          /* poll_event */
157     NULL,                       /* flush */
158     NULL,                       /* get_fd_type */
159     NULL,                       /* ioctl */
160     NULL,                       /* queue_async */
161     NULL,                       /* reselect_async */
162     NULL                        /* cancel_async */
163 };
164
165 static struct list thread_list = LIST_INIT(thread_list);
166
167 /* initialize the structure for a newly allocated thread */
168 static inline void init_thread_structure( struct thread *thread )
169 {
170     int i;
171
172     thread->unix_pid        = -1;  /* not known yet */
173     thread->unix_tid        = -1;  /* not known yet */
174     thread->context         = NULL;
175     thread->suspend_context = NULL;
176     thread->teb             = 0;
177     thread->debug_ctx       = NULL;
178     thread->debug_event     = NULL;
179     thread->debug_break     = 0;
180     thread->queue           = NULL;
181     thread->wait            = NULL;
182     thread->error           = 0;
183     thread->req_data        = NULL;
184     thread->req_toread      = 0;
185     thread->reply_data      = NULL;
186     thread->reply_towrite   = 0;
187     thread->request_fd      = NULL;
188     thread->reply_fd        = NULL;
189     thread->wait_fd         = NULL;
190     thread->state           = RUNNING;
191     thread->exit_code       = 0;
192     thread->priority        = 0;
193     thread->affinity        = ~0;
194     thread->suspend         = 0;
195     thread->desktop_users   = 0;
196     thread->token           = NULL;
197
198     thread->creation_time = current_time;
199     thread->exit_time     = 0;
200
201     list_init( &thread->mutex_list );
202     list_init( &thread->system_apc );
203     list_init( &thread->user_apc );
204
205     for (i = 0; i < MAX_INFLIGHT_FDS; i++)
206         thread->inflight[i].server = thread->inflight[i].client = -1;
207 }
208
209 /* check if address looks valid for a client-side data structure (TEB etc.) */
210 static inline int is_valid_address( client_ptr_t addr )
211 {
212     return addr && !(addr % sizeof(int));
213 }
214
215 /* create a new thread */
216 struct thread *create_thread( int fd, struct process *process )
217 {
218     struct thread *thread;
219
220     if (!(thread = alloc_object( &thread_ops ))) return NULL;
221
222     init_thread_structure( thread );
223
224     thread->process = (struct process *)grab_object( process );
225     thread->desktop = process->desktop;
226     thread->affinity = process->affinity;
227     if (!current) current = thread;
228
229     list_add_head( &thread_list, &thread->entry );
230
231     if (!(thread->id = alloc_ptid( thread )))
232     {
233         release_object( thread );
234         return NULL;
235     }
236     if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
237     {
238         release_object( thread );
239         return NULL;
240     }
241
242     set_fd_events( thread->request_fd, POLLIN );  /* start listening to events */
243     add_process_thread( thread->process, thread );
244     return thread;
245 }
246
247 /* handle a client event */
248 static void thread_poll_event( struct fd *fd, int event )
249 {
250     struct thread *thread = get_fd_user( fd );
251     assert( thread->obj.ops == &thread_ops );
252
253     if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
254     else if (event & POLLIN) read_request( thread );
255     else if (event & POLLOUT) write_reply( thread );
256 }
257
258 /* cleanup everything that is no longer needed by a dead thread */
259 /* used by destroy_thread and kill_thread */
260 static void cleanup_thread( struct thread *thread )
261 {
262     int i;
263
264     clear_apc_queue( &thread->system_apc );
265     clear_apc_queue( &thread->user_apc );
266     free( thread->req_data );
267     free( thread->reply_data );
268     if (thread->request_fd) release_object( thread->request_fd );
269     if (thread->reply_fd) release_object( thread->reply_fd );
270     if (thread->wait_fd) release_object( thread->wait_fd );
271     free( thread->suspend_context );
272     cleanup_clipboard_thread(thread);
273     destroy_thread_windows( thread );
274     free_msg_queue( thread );
275     close_thread_desktop( thread );
276     for (i = 0; i < MAX_INFLIGHT_FDS; i++)
277     {
278         if (thread->inflight[i].client != -1)
279         {
280             close( thread->inflight[i].server );
281             thread->inflight[i].client = thread->inflight[i].server = -1;
282         }
283     }
284     thread->req_data = NULL;
285     thread->reply_data = NULL;
286     thread->request_fd = NULL;
287     thread->reply_fd = NULL;
288     thread->wait_fd = NULL;
289     thread->context = NULL;
290     thread->suspend_context = NULL;
291     thread->desktop = 0;
292 }
293
294 /* destroy a thread when its refcount is 0 */
295 static void destroy_thread( struct object *obj )
296 {
297     struct thread *thread = (struct thread *)obj;
298     assert( obj->ops == &thread_ops );
299
300     assert( !thread->debug_ctx );  /* cannot still be debugging something */
301     list_remove( &thread->entry );
302     cleanup_thread( thread );
303     release_object( thread->process );
304     if (thread->id) free_ptid( thread->id );
305     if (thread->token) release_object( thread->token );
306 }
307
308 /* dump a thread on stdout for debugging purposes */
309 static void dump_thread( struct object *obj, int verbose )
310 {
311     struct thread *thread = (struct thread *)obj;
312     assert( obj->ops == &thread_ops );
313
314     fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
315              thread->id, thread->unix_pid, thread->unix_tid, thread->state );
316 }
317
318 static int thread_signaled( struct object *obj, struct thread *thread )
319 {
320     struct thread *mythread = (struct thread *)obj;
321     return (mythread->state == TERMINATED);
322 }
323
324 static unsigned int thread_map_access( struct object *obj, unsigned int access )
325 {
326     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
327     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
328     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
329     if (access & GENERIC_ALL)     access |= THREAD_ALL_ACCESS;
330     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
331 }
332
333 static void dump_thread_apc( struct object *obj, int verbose )
334 {
335     struct thread_apc *apc = (struct thread_apc *)obj;
336     assert( obj->ops == &thread_apc_ops );
337
338     fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
339 }
340
341 static int thread_apc_signaled( struct object *obj, struct thread *thread )
342 {
343     struct thread_apc *apc = (struct thread_apc *)obj;
344     return apc->executed;
345 }
346
347 static void thread_apc_destroy( struct object *obj )
348 {
349     struct thread_apc *apc = (struct thread_apc *)obj;
350     if (apc->caller) release_object( apc->caller );
351     if (apc->owner) release_object( apc->owner );
352 }
353
354 /* queue an async procedure call */
355 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
356 {
357     struct thread_apc *apc;
358
359     if ((apc = alloc_object( &thread_apc_ops )))
360     {
361         apc->call        = *call_data;
362         apc->caller      = NULL;
363         apc->owner       = owner;
364         apc->executed    = 0;
365         apc->result.type = APC_NONE;
366         if (owner) grab_object( owner );
367     }
368     return apc;
369 }
370
371 /* get a thread pointer from a thread id (and increment the refcount) */
372 struct thread *get_thread_from_id( thread_id_t id )
373 {
374     struct object *obj = get_ptid_entry( id );
375
376     if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
377     set_error( STATUS_INVALID_CID );
378     return NULL;
379 }
380
381 /* get a thread from a handle (and increment the refcount) */
382 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
383 {
384     return (struct thread *)get_handle_obj( current->process, handle,
385                                             access, &thread_ops );
386 }
387
388 /* find a thread from a Unix tid */
389 struct thread *get_thread_from_tid( int tid )
390 {
391     struct thread *thread;
392
393     LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
394     {
395         if (thread->unix_tid == tid) return thread;
396     }
397     return NULL;
398 }
399
400 /* find a thread from a Unix pid */
401 struct thread *get_thread_from_pid( int pid )
402 {
403     struct thread *thread;
404
405     LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
406     {
407         if (thread->unix_pid == pid) return thread;
408     }
409     return NULL;
410 }
411
412 /* determine if the thread is wow64 (32-bit client running on 64-bit server) */
413 int is_wow64_thread( struct thread *thread )
414 {
415     return (supported_cpus & CPU_64BIT_MASK) && !(CPU_FLAG(thread->process->cpu) & CPU_64BIT_MASK);
416 }
417
418 int set_thread_affinity( struct thread *thread, affinity_t affinity )
419 {
420     int ret = 0;
421 #ifdef HAVE_SCHED_SETAFFINITY
422     if (thread->unix_tid != -1)
423     {
424         cpu_set_t set;
425         int i;
426         affinity_t mask;
427
428         CPU_ZERO( &set );
429         for (i = 0, mask = 1; mask; i++, mask <<= 1)
430             if (affinity & mask) CPU_SET( i, &set );
431
432         ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
433     }
434 #endif
435     if (!ret) thread->affinity = affinity;
436     return ret;
437 }
438
439 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
440 #define THREAD_PRIORITY_REALTIME_LOWEST -7
441
442 /* set all information about a thread */
443 static void set_thread_info( struct thread *thread,
444                              const struct set_thread_info_request *req )
445 {
446     if (req->mask & SET_THREAD_INFO_PRIORITY)
447     {
448         int max = THREAD_PRIORITY_HIGHEST;
449         int min = THREAD_PRIORITY_LOWEST;
450         if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
451         {
452             max = THREAD_PRIORITY_REALTIME_HIGHEST;
453             min = THREAD_PRIORITY_REALTIME_LOWEST;
454         }
455         if ((req->priority >= min && req->priority <= max) ||
456             req->priority == THREAD_PRIORITY_IDLE ||
457             req->priority == THREAD_PRIORITY_TIME_CRITICAL)
458             thread->priority = req->priority;
459         else
460             set_error( STATUS_INVALID_PARAMETER );
461     }
462     if (req->mask & SET_THREAD_INFO_AFFINITY)
463     {
464         if ((req->affinity & thread->process->affinity) != req->affinity)
465             set_error( STATUS_INVALID_PARAMETER );
466         else if (thread->state == TERMINATED)
467             set_error( STATUS_ACCESS_DENIED );
468         else if (set_thread_affinity( thread, req->affinity ))
469             file_set_error();
470     }
471     if (req->mask & SET_THREAD_INFO_TOKEN)
472         security_set_thread_token( thread, req->token );
473 }
474
475 /* stop a thread (at the Unix level) */
476 void stop_thread( struct thread *thread )
477 {
478     if (thread->context) return;  /* already inside a debug event, no need for a signal */
479     /* can't stop a thread while initialisation is in progress */
480     if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
481 }
482
483 /* suspend a thread */
484 static int suspend_thread( struct thread *thread )
485 {
486     int old_count = thread->suspend;
487     if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
488     {
489         if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
490     }
491     else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
492     return old_count;
493 }
494
495 /* resume a thread */
496 static int resume_thread( struct thread *thread )
497 {
498     int old_count = thread->suspend;
499     if (thread->suspend > 0)
500     {
501         if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
502     }
503     return old_count;
504 }
505
506 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
507 int add_queue( struct object *obj, struct wait_queue_entry *entry )
508 {
509     grab_object( obj );
510     entry->obj = obj;
511     list_add_tail( &obj->wait_queue, &entry->entry );
512     return 1;
513 }
514
515 /* remove a thread from an object wait queue */
516 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
517 {
518     list_remove( &entry->entry );
519     release_object( obj );
520 }
521
522 /* finish waiting */
523 static void end_wait( struct thread *thread )
524 {
525     struct thread_wait *wait = thread->wait;
526     struct wait_queue_entry *entry;
527     int i;
528
529     assert( wait );
530     thread->wait = wait->next;
531     for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
532         entry->obj->ops->remove_queue( entry->obj, entry );
533     if (wait->user) remove_timeout_user( wait->user );
534     free( wait );
535 }
536
537 /* build the thread wait structure */
538 static int wait_on( unsigned int count, struct object *objects[], int flags, timeout_t timeout )
539 {
540     struct thread_wait *wait;
541     struct wait_queue_entry *entry;
542     unsigned int i;
543
544     if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
545     wait->next    = current->wait;
546     wait->thread  = current;
547     wait->count   = count;
548     wait->flags   = flags;
549     wait->user    = NULL;
550     wait->timeout = timeout;
551     current->wait = wait;
552
553     for (i = 0, entry = wait->queues; i < count; i++, entry++)
554     {
555         struct object *obj = objects[i];
556         entry->thread = current;
557         if (!obj->ops->add_queue( obj, entry ))
558         {
559             wait->count = i;
560             end_wait( current );
561             return 0;
562         }
563     }
564     return 1;
565 }
566
567 /* check if the thread waiting condition is satisfied */
568 static int check_wait( struct thread *thread )
569 {
570     int i, signaled;
571     struct thread_wait *wait = thread->wait;
572     struct wait_queue_entry *entry = wait->queues;
573
574     assert( wait );
575
576     if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
577         return STATUS_USER_APC;
578
579     /* Suspended threads may not acquire locks, but they can run system APCs */
580     if (thread->process->suspend + thread->suspend > 0) return -1;
581
582     if (wait->flags & SELECT_ALL)
583     {
584         int not_ok = 0;
585         /* Note: we must check them all anyway, as some objects may
586          * want to do something when signaled, even if others are not */
587         for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
588             not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
589         if (not_ok) goto other_checks;
590         /* Wait satisfied: tell it to all objects */
591         signaled = 0;
592         for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
593             if (entry->obj->ops->satisfied( entry->obj, thread ))
594                 signaled = STATUS_ABANDONED_WAIT_0;
595         return signaled;
596     }
597     else
598     {
599         for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
600         {
601             if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
602             /* Wait satisfied: tell it to the object */
603             signaled = i;
604             if (entry->obj->ops->satisfied( entry->obj, thread ))
605                 signaled = i + STATUS_ABANDONED_WAIT_0;
606             return signaled;
607         }
608     }
609
610  other_checks:
611     if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
612     if (wait->timeout <= current_time) return STATUS_TIMEOUT;
613     return -1;
614 }
615
616 /* send the wakeup signal to a thread */
617 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
618 {
619     struct wake_up_reply reply;
620     int ret;
621
622     memset( &reply, 0, sizeof(reply) );
623     reply.cookie   = cookie;
624     reply.signaled = signaled;
625     if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
626         return 0;
627     if (ret >= 0)
628         fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
629     else if (errno == EPIPE)
630         kill_thread( thread, 0 );  /* normal death */
631     else
632         fatal_protocol_perror( thread, "write" );
633     return -1;
634 }
635
636 /* attempt to wake up a thread */
637 /* return >0 if OK, 0 if the wait condition is still not satisfied */
638 int wake_thread( struct thread *thread )
639 {
640     int signaled, count;
641     client_ptr_t cookie;
642
643     for (count = 0; thread->wait; count++)
644     {
645         if ((signaled = check_wait( thread )) == -1) break;
646
647         cookie = thread->wait->cookie;
648         if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
649         end_wait( thread );
650         if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
651             break;
652     }
653     return count;
654 }
655
656 /* thread wait timeout */
657 static void thread_timeout( void *ptr )
658 {
659     struct thread_wait *wait = ptr;
660     struct thread *thread = wait->thread;
661     client_ptr_t cookie = wait->cookie;
662
663     wait->user = NULL;
664     if (thread->wait != wait) return; /* not the top-level wait, ignore it */
665     if (thread->suspend + thread->process->suspend > 0) return;  /* suspended, ignore it */
666
667     if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
668     end_wait( thread );
669     if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
670     /* check if other objects have become signaled in the meantime */
671     wake_thread( thread );
672 }
673
674 /* try signaling an event flag, a semaphore or a mutex */
675 static int signal_object( obj_handle_t handle )
676 {
677     struct object *obj;
678     int ret = 0;
679
680     obj = get_handle_obj( current->process, handle, 0, NULL );
681     if (obj)
682     {
683         ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
684         release_object( obj );
685     }
686     return ret;
687 }
688
689 /* select on a list of handles */
690 static timeout_t select_on( unsigned int count, client_ptr_t cookie, const obj_handle_t *handles,
691                             int flags, timeout_t timeout, obj_handle_t signal_obj )
692 {
693     int ret;
694     unsigned int i;
695     struct object *objects[MAXIMUM_WAIT_OBJECTS];
696
697     if (timeout <= 0) timeout = current_time - timeout;
698
699     if (count > MAXIMUM_WAIT_OBJECTS)
700     {
701         set_error( STATUS_INVALID_PARAMETER );
702         return 0;
703     }
704     for (i = 0; i < count; i++)
705     {
706         if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
707             break;
708     }
709
710     if (i < count) goto done;
711     if (!wait_on( count, objects, flags, timeout )) goto done;
712
713     /* signal the object */
714     if (signal_obj)
715     {
716         if (!signal_object( signal_obj ))
717         {
718             end_wait( current );
719             goto done;
720         }
721         /* check if we woke ourselves up */
722         if (!current->wait) goto done;
723     }
724
725     if ((ret = check_wait( current )) != -1)
726     {
727         /* condition is already satisfied */
728         end_wait( current );
729         set_error( ret );
730         goto done;
731     }
732
733     /* now we need to wait */
734     if (current->wait->timeout != TIMEOUT_INFINITE)
735     {
736         if (!(current->wait->user = add_timeout_user( current->wait->timeout,
737                                                       thread_timeout, current->wait )))
738         {
739             end_wait( current );
740             goto done;
741         }
742     }
743     current->wait->cookie = cookie;
744     set_error( STATUS_PENDING );
745
746 done:
747     while (i > 0) release_object( objects[--i] );
748     return timeout;
749 }
750
751 /* attempt to wake threads sleeping on the object wait queue */
752 void wake_up( struct object *obj, int max )
753 {
754     struct list *ptr;
755
756     LIST_FOR_EACH( ptr, &obj->wait_queue )
757     {
758         struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
759         if (!wake_thread( entry->thread )) continue;
760         if (max && !--max) break;
761         /* restart at the head of the list since a wake up can change the object wait queue */
762         ptr = &obj->wait_queue;
763     }
764 }
765
766 /* return the apc queue to use for a given apc type */
767 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
768 {
769     switch(type)
770     {
771     case APC_NONE:
772     case APC_USER:
773     case APC_TIMER:
774         return &thread->user_apc;
775     default:
776         return &thread->system_apc;
777     }
778 }
779
780 /* check if thread is currently waiting for a (system) apc */
781 static inline int is_in_apc_wait( struct thread *thread )
782 {
783     return (thread->process->suspend || thread->suspend ||
784             (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
785 }
786
787 /* queue an existing APC to a given thread */
788 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
789 {
790     struct list *queue;
791
792     if (!thread)  /* find a suitable thread inside the process */
793     {
794         struct thread *candidate;
795
796         /* first try to find a waiting thread */
797         LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
798         {
799             if (candidate->state == TERMINATED) continue;
800             if (is_in_apc_wait( candidate ))
801             {
802                 thread = candidate;
803                 break;
804             }
805         }
806         if (!thread)
807         {
808             /* then use the first one that accepts a signal */
809             LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
810             {
811                 if (send_thread_signal( candidate, SIGUSR1 ))
812                 {
813                     thread = candidate;
814                     break;
815                 }
816             }
817         }
818         if (!thread) return 0;  /* nothing found */
819         queue = get_apc_queue( thread, apc->call.type );
820     }
821     else
822     {
823         if (thread->state == TERMINATED) return 0;
824         queue = get_apc_queue( thread, apc->call.type );
825         /* send signal for system APCs if needed */
826         if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
827         {
828             if (!send_thread_signal( thread, SIGUSR1 )) return 0;
829         }
830         /* cancel a possible previous APC with the same owner */
831         if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
832     }
833
834     grab_object( apc );
835     list_add_tail( queue, &apc->entry );
836     if (!list_prev( queue, &apc->entry ))  /* first one */
837         wake_thread( thread );
838
839     return 1;
840 }
841
842 /* queue an async procedure call */
843 int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data )
844 {
845     struct thread_apc *apc;
846     int ret = 0;
847
848     if ((apc = create_apc( owner, call_data )))
849     {
850         ret = queue_apc( NULL, thread, apc );
851         release_object( apc );
852     }
853     return ret;
854 }
855
856 /* cancel the async procedure call owned by a specific object */
857 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
858 {
859     struct thread_apc *apc;
860     struct list *queue = get_apc_queue( thread, type );
861
862     LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
863     {
864         if (apc->owner != owner) continue;
865         list_remove( &apc->entry );
866         apc->executed = 1;
867         wake_up( &apc->obj, 0 );
868         release_object( apc );
869         return;
870     }
871 }
872
873 /* remove the head apc from the queue; the returned object must be released by the caller */
874 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
875 {
876     struct thread_apc *apc = NULL;
877     struct list *ptr = list_head( &thread->system_apc );
878
879     if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
880     if (ptr)
881     {
882         apc = LIST_ENTRY( ptr, struct thread_apc, entry );
883         list_remove( ptr );
884     }
885     return apc;
886 }
887
888 /* clear an APC queue, cancelling all the APCs on it */
889 static void clear_apc_queue( struct list *queue )
890 {
891     struct list *ptr;
892
893     while ((ptr = list_head( queue )))
894     {
895         struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
896         list_remove( &apc->entry );
897         apc->executed = 1;
898         wake_up( &apc->obj, 0 );
899         release_object( apc );
900     }
901 }
902
903 /* add an fd to the inflight list */
904 /* return list index, or -1 on error */
905 int thread_add_inflight_fd( struct thread *thread, int client, int server )
906 {
907     int i;
908
909     if (server == -1) return -1;
910     if (client == -1)
911     {
912         close( server );
913         return -1;
914     }
915
916     /* first check if we already have an entry for this fd */
917     for (i = 0; i < MAX_INFLIGHT_FDS; i++)
918         if (thread->inflight[i].client == client)
919         {
920             close( thread->inflight[i].server );
921             thread->inflight[i].server = server;
922             return i;
923         }
924
925     /* now find a free spot to store it */
926     for (i = 0; i < MAX_INFLIGHT_FDS; i++)
927         if (thread->inflight[i].client == -1)
928         {
929             thread->inflight[i].client = client;
930             thread->inflight[i].server = server;
931             return i;
932         }
933     return -1;
934 }
935
936 /* get an inflight fd and purge it from the list */
937 /* the fd must be closed when no longer used */
938 int thread_get_inflight_fd( struct thread *thread, int client )
939 {
940     int i, ret;
941
942     if (client == -1) return -1;
943
944     do
945     {
946         for (i = 0; i < MAX_INFLIGHT_FDS; i++)
947         {
948             if (thread->inflight[i].client == client)
949             {
950                 ret = thread->inflight[i].server;
951                 thread->inflight[i].server = thread->inflight[i].client = -1;
952                 return ret;
953             }
954         }
955     } while (!receive_fd( thread->process ));  /* in case it is still in the socket buffer */
956     return -1;
957 }
958
959 /* kill a thread on the spot */
960 void kill_thread( struct thread *thread, int violent_death )
961 {
962     if (thread->state == TERMINATED) return;  /* already killed */
963     thread->state = TERMINATED;
964     thread->exit_time = current_time;
965     if (current == thread) current = NULL;
966     if (debug_level)
967         fprintf( stderr,"%04x: *killed* exit_code=%d\n",
968                  thread->id, thread->exit_code );
969     if (thread->wait)
970     {
971         while (thread->wait) end_wait( thread );
972         send_thread_wakeup( thread, 0, STATUS_PENDING );
973         /* if it is waiting on the socket, we don't need to send a SIGQUIT */
974         violent_death = 0;
975     }
976     kill_console_processes( thread, 0 );
977     debug_exit_thread( thread );
978     abandon_mutexes( thread );
979     wake_up( &thread->obj, 0 );
980     if (violent_death) send_thread_signal( thread, SIGQUIT );
981     cleanup_thread( thread );
982     remove_process_thread( thread->process, thread );
983     release_object( thread );
984 }
985
986 /* copy parts of a context structure */
987 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
988 {
989     assert( to->cpu == from->cpu );
990     to->flags |= flags;
991     if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
992     if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
993     if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
994     if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
995     if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
996     if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
997 }
998
999 /* return the context flags that correspond to system regs */
1000 /* (system regs are the ones we can't access on the client side) */
1001 static unsigned int get_context_system_regs( enum cpu_type cpu )
1002 {
1003     switch (cpu)
1004     {
1005     case CPU_x86:     return SERVER_CTX_DEBUG_REGISTERS;
1006     case CPU_x86_64:  return SERVER_CTX_DEBUG_REGISTERS;
1007     case CPU_ALPHA:   return 0;
1008     case CPU_POWERPC: return 0;
1009     case CPU_SPARC:   return 0;
1010     }
1011     return 0;
1012 }
1013
1014 /* trigger a breakpoint event in a given thread */
1015 void break_thread( struct thread *thread )
1016 {
1017     debug_event_t data;
1018
1019     assert( thread->context );
1020
1021     memset( &data, 0, sizeof(data) );
1022     data.exception.first     = 1;
1023     data.exception.exc_code  = STATUS_BREAKPOINT;
1024     data.exception.flags     = EXCEPTION_CONTINUABLE;
1025     switch (thread->context->cpu)
1026     {
1027     case CPU_x86:
1028         data.exception.address = thread->context->ctl.i386_regs.eip;
1029         break;
1030     case CPU_x86_64:
1031         data.exception.address = thread->context->ctl.x86_64_regs.rip;
1032         break;
1033     case CPU_ALPHA:
1034         data.exception.address = thread->context->ctl.alpha_regs.fir;
1035         break;
1036     case CPU_POWERPC:
1037         data.exception.address = thread->context->ctl.powerpc_regs.iar;
1038         break;
1039     case CPU_SPARC:
1040         data.exception.address = thread->context->ctl.sparc_regs.pc;
1041         break;
1042     }
1043     generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
1044     thread->debug_break = 0;
1045 }
1046
1047 /* take a snapshot of currently running threads */
1048 struct thread_snapshot *thread_snap( int *count )
1049 {
1050     struct thread_snapshot *snapshot, *ptr;
1051     struct thread *thread;
1052     int total = 0;
1053
1054     LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1055         if (thread->state != TERMINATED) total++;
1056     if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1057     ptr = snapshot;
1058     LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1059     {
1060         if (thread->state == TERMINATED) continue;
1061         ptr->thread   = thread;
1062         ptr->count    = thread->obj.refcount;
1063         ptr->priority = thread->priority;
1064         grab_object( thread );
1065         ptr++;
1066     }
1067     *count = total;
1068     return snapshot;
1069 }
1070
1071 /* gets the current impersonation token */
1072 struct token *thread_get_impersonation_token( struct thread *thread )
1073 {
1074     if (thread->token)
1075         return thread->token;
1076     else
1077         return thread->process->token;
1078 }
1079
1080 /* create a new thread */
1081 DECL_HANDLER(new_thread)
1082 {
1083     struct thread *thread;
1084     int request_fd = thread_get_inflight_fd( current, req->request_fd );
1085
1086     if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1087     {
1088         if (request_fd != -1) close( request_fd );
1089         set_error( STATUS_INVALID_HANDLE );
1090         return;
1091     }
1092
1093     if ((thread = create_thread( request_fd, current->process )))
1094     {
1095         if (req->suspend) thread->suspend++;
1096         reply->tid = get_thread_id( thread );
1097         if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
1098         {
1099             /* thread object will be released when the thread gets killed */
1100             return;
1101         }
1102         kill_thread( thread, 1 );
1103     }
1104 }
1105
1106 /* initialize a new thread */
1107 DECL_HANDLER(init_thread)
1108 {
1109     struct process *process = current->process;
1110     int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
1111     int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
1112
1113     if (current->reply_fd)  /* already initialised */
1114     {
1115         set_error( STATUS_INVALID_PARAMETER );
1116         goto error;
1117     }
1118
1119     if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1120
1121     current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1122     reply_fd = -1;
1123     if (!current->reply_fd) goto error;
1124
1125     if (wait_fd == -1)
1126     {
1127         set_error( STATUS_TOO_MANY_OPENED_FILES );  /* most likely reason */
1128         return;
1129     }
1130     if (!(current->wait_fd  = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 )))
1131         return;
1132
1133     if (!is_valid_address(req->teb))
1134     {
1135         set_error( STATUS_INVALID_PARAMETER );
1136         return;
1137     }
1138
1139     current->unix_pid = req->unix_pid;
1140     current->unix_tid = req->unix_tid;
1141     current->teb      = req->teb;
1142
1143     if (!process->peb)  /* first thread, initialize the process too */
1144     {
1145         if (!CPU_FLAG(req->cpu) || !(supported_cpus & CPU_FLAG(req->cpu)))
1146         {
1147             set_error( STATUS_NOT_SUPPORTED );
1148             return;
1149         }
1150         process->unix_pid = current->unix_pid;
1151         process->peb      = req->entry;
1152         process->cpu      = req->cpu;
1153         reply->info_size  = init_process( current );
1154     }
1155     else
1156     {
1157         if (req->cpu != process->cpu)
1158         {
1159             set_error( STATUS_INVALID_PARAMETER );
1160             return;
1161         }
1162         if (process->unix_pid != current->unix_pid)
1163             process->unix_pid = -1;  /* can happen with linuxthreads */
1164         if (current->suspend + process->suspend > 0) stop_thread( current );
1165         generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1166     }
1167     debug_level = max( debug_level, req->debug_level );
1168     set_thread_affinity( current, current->affinity );
1169
1170     reply->pid     = get_process_id( process );
1171     reply->tid     = get_thread_id( current );
1172     reply->version = SERVER_PROTOCOL_VERSION;
1173     reply->server_start = server_start_time;
1174     reply->all_cpus     = supported_cpus;
1175     return;
1176
1177  error:
1178     if (reply_fd != -1) close( reply_fd );
1179     if (wait_fd != -1) close( wait_fd );
1180 }
1181
1182 /* terminate a thread */
1183 DECL_HANDLER(terminate_thread)
1184 {
1185     struct thread *thread;
1186
1187     reply->self = 0;
1188     reply->last = 0;
1189     if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1190     {
1191         thread->exit_code = req->exit_code;
1192         if (thread != current) kill_thread( thread, 1 );
1193         else
1194         {
1195             reply->self = 1;
1196             reply->last = (thread->process->running_threads == 1);
1197         }
1198         release_object( thread );
1199     }
1200 }
1201
1202 /* open a handle to a thread */
1203 DECL_HANDLER(open_thread)
1204 {
1205     struct thread *thread = get_thread_from_id( req->tid );
1206
1207     reply->handle = 0;
1208     if (thread)
1209     {
1210         reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1211         release_object( thread );
1212     }
1213 }
1214
1215 /* fetch information about a thread */
1216 DECL_HANDLER(get_thread_info)
1217 {
1218     struct thread *thread;
1219     obj_handle_t handle = req->handle;
1220
1221     if (!handle) thread = get_thread_from_id( req->tid_in );
1222     else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
1223
1224     if (thread)
1225     {
1226         reply->pid            = get_process_id( thread->process );
1227         reply->tid            = get_thread_id( thread );
1228         reply->teb            = thread->teb;
1229         reply->exit_code      = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1230         reply->priority       = thread->priority;
1231         reply->affinity       = thread->affinity;
1232         reply->creation_time  = thread->creation_time;
1233         reply->exit_time      = thread->exit_time;
1234         reply->last           = thread->process->running_threads == 1;
1235
1236         release_object( thread );
1237     }
1238 }
1239
1240 /* set information about a thread */
1241 DECL_HANDLER(set_thread_info)
1242 {
1243     struct thread *thread;
1244
1245     if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1246     {
1247         set_thread_info( thread, req );
1248         release_object( thread );
1249     }
1250 }
1251
1252 /* suspend a thread */
1253 DECL_HANDLER(suspend_thread)
1254 {
1255     struct thread *thread;
1256
1257     if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1258     {
1259         if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1260         else reply->count = suspend_thread( thread );
1261         release_object( thread );
1262     }
1263 }
1264
1265 /* resume a thread */
1266 DECL_HANDLER(resume_thread)
1267 {
1268     struct thread *thread;
1269
1270     if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1271     {
1272         reply->count = resume_thread( thread );
1273         release_object( thread );
1274     }
1275 }
1276
1277 /* select on a handle list */
1278 DECL_HANDLER(select)
1279 {
1280     struct thread_apc *apc;
1281     unsigned int count;
1282     const apc_result_t *result = get_req_data();
1283     const obj_handle_t *handles = (const obj_handle_t *)(result + 1);
1284
1285     if (get_req_data_size() < sizeof(*result))
1286     {
1287         set_error( STATUS_INVALID_PARAMETER );
1288         return;
1289     }
1290     count = (get_req_data_size() - sizeof(*result)) / sizeof(obj_handle_t);
1291
1292     /* first store results of previous apc */
1293     if (req->prev_apc)
1294     {
1295         if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1296                                                          0, &thread_apc_ops ))) return;
1297         apc->result = *result;
1298         apc->executed = 1;
1299         if (apc->result.type == APC_CREATE_THREAD)  /* transfer the handle to the caller process */
1300         {
1301             obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1302                                                     apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1303             close_handle( current->process, apc->result.create_thread.handle );
1304             apc->result.create_thread.handle = handle;
1305             clear_error();  /* ignore errors from the above calls */
1306         }
1307         else if (apc->result.type == APC_ASYNC_IO)
1308         {
1309             if (apc->owner)
1310                 async_set_result( apc->owner, apc->result.async_io.status,
1311                                   apc->result.async_io.total, apc->result.async_io.apc );
1312         }
1313         wake_up( &apc->obj, 0 );
1314         close_handle( current->process, req->prev_apc );
1315         release_object( apc );
1316     }
1317
1318     reply->timeout = select_on( count, req->cookie, handles, req->flags, req->timeout, req->signal );
1319
1320     if (get_error() == STATUS_USER_APC)
1321     {
1322         for (;;)
1323         {
1324             if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1325                 break;
1326             /* Optimization: ignore APC_NONE calls, they are only used to
1327              * wake up a thread, but since we got here the thread woke up already.
1328              */
1329             if (apc->call.type != APC_NONE)
1330             {
1331                 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1332                     reply->call = apc->call;
1333                 release_object( apc );
1334                 break;
1335             }
1336             apc->executed = 1;
1337             wake_up( &apc->obj, 0 );
1338             release_object( apc );
1339         }
1340     }
1341 }
1342
1343 /* queue an APC for a thread or process */
1344 DECL_HANDLER(queue_apc)
1345 {
1346     struct thread *thread = NULL;
1347     struct process *process = NULL;
1348     struct thread_apc *apc;
1349
1350     if (!(apc = create_apc( NULL, &req->call ))) return;
1351
1352     switch (apc->call.type)
1353     {
1354     case APC_NONE:
1355     case APC_USER:
1356         thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1357         break;
1358     case APC_VIRTUAL_ALLOC:
1359     case APC_VIRTUAL_FREE:
1360     case APC_VIRTUAL_PROTECT:
1361     case APC_VIRTUAL_FLUSH:
1362     case APC_VIRTUAL_LOCK:
1363     case APC_VIRTUAL_UNLOCK:
1364     case APC_UNMAP_VIEW:
1365         process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1366         break;
1367     case APC_VIRTUAL_QUERY:
1368         process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1369         break;
1370     case APC_MAP_VIEW:
1371         process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1372         if (process && process != current->process)
1373         {
1374             /* duplicate the handle into the target process */
1375             obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1376                                                     process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1377             if (handle) apc->call.map_view.handle = handle;
1378             else
1379             {
1380                 release_object( process );
1381                 process = NULL;
1382             }
1383         }
1384         break;
1385     case APC_CREATE_THREAD:
1386         process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1387         break;
1388     default:
1389         set_error( STATUS_INVALID_PARAMETER );
1390         break;
1391     }
1392
1393     if (thread)
1394     {
1395         if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1396         release_object( thread );
1397     }
1398     else if (process)
1399     {
1400         reply->self = (process == current->process);
1401         if (!reply->self)
1402         {
1403             obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1404             if (handle)
1405             {
1406                 if (queue_apc( process, NULL, apc ))
1407                 {
1408                     apc->caller = (struct thread *)grab_object( current );
1409                     reply->handle = handle;
1410                 }
1411                 else
1412                 {
1413                     close_handle( current->process, handle );
1414                     set_error( STATUS_PROCESS_IS_TERMINATING );
1415                 }
1416             }
1417         }
1418         release_object( process );
1419     }
1420
1421     release_object( apc );
1422 }
1423
1424 /* Get the result of an APC call */
1425 DECL_HANDLER(get_apc_result)
1426 {
1427     struct thread_apc *apc;
1428
1429     if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1430                                                      0, &thread_apc_ops ))) return;
1431     if (!apc->executed) set_error( STATUS_PENDING );
1432     else
1433     {
1434         reply->result = apc->result;
1435         /* close the handle directly to avoid an extra round-trip */
1436         close_handle( current->process, req->handle );
1437     }
1438     release_object( apc );
1439 }
1440
1441 /* retrieve the current context of a thread */
1442 DECL_HANDLER(get_thread_context)
1443 {
1444     struct thread *thread;
1445     context_t *context;
1446
1447     if (get_reply_max_size() < sizeof(context_t))
1448     {
1449         set_error( STATUS_INVALID_PARAMETER );
1450         return;
1451     }
1452     if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1453
1454     if (req->suspend)
1455     {
1456         if (thread != current || !thread->suspend_context)
1457         {
1458             /* not suspended, shouldn't happen */
1459             set_error( STATUS_INVALID_PARAMETER );
1460         }
1461         else
1462         {
1463             if (thread->context == thread->suspend_context) thread->context = NULL;
1464             set_reply_data_ptr( thread->suspend_context, sizeof(context_t) );
1465             thread->suspend_context = NULL;
1466         }
1467     }
1468     else if (thread != current && !thread->context)
1469     {
1470         /* thread is not suspended, retry (if it's still running) */
1471         if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1472         else set_error( STATUS_PENDING );
1473     }
1474     else if ((context = set_reply_data_size( sizeof(context_t) )))
1475     {
1476         unsigned int flags = get_context_system_regs( thread->process->cpu );
1477
1478         memset( context, 0, sizeof(context_t) );
1479         context->cpu = thread->process->cpu;
1480         if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1481         if (flags) get_thread_context( thread, context, flags );
1482     }
1483     reply->self = (thread == current);
1484     release_object( thread );
1485 }
1486
1487 /* set the current context of a thread */
1488 DECL_HANDLER(set_thread_context)
1489 {
1490     struct thread *thread;
1491     const context_t *context = get_req_data();
1492
1493     if (get_req_data_size() < sizeof(context_t))
1494     {
1495         set_error( STATUS_INVALID_PARAMETER );
1496         return;
1497     }
1498     if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1499
1500     if (req->suspend)
1501     {
1502         if (thread != current || thread->context || context->cpu != thread->process->cpu)
1503         {
1504             /* nested suspend or exception, shouldn't happen */
1505             set_error( STATUS_INVALID_PARAMETER );
1506         }
1507         else if ((thread->suspend_context = mem_alloc( sizeof(context_t) )))
1508         {
1509             memcpy( thread->suspend_context, get_req_data(), sizeof(context_t) );
1510             thread->context = thread->suspend_context;
1511             if (thread->debug_break) break_thread( thread );
1512         }
1513     }
1514     else if (thread != current && !thread->context)
1515     {
1516         /* thread is not suspended, retry (if it's still running) */
1517         if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1518         else set_error( STATUS_PENDING );
1519     }
1520     else if (context->cpu == thread->process->cpu)
1521     {
1522         unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1523         unsigned int client_flags = context->flags & ~system_flags;
1524
1525         if (system_flags) set_thread_context( thread, context, system_flags );
1526         if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1527     }
1528     else set_error( STATUS_INVALID_PARAMETER );
1529
1530     reply->self = (thread == current);
1531     release_object( thread );
1532 }
1533
1534 /* fetch a selector entry for a thread */
1535 DECL_HANDLER(get_selector_entry)
1536 {
1537     struct thread *thread;
1538     if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1539     {
1540         get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1541         release_object( thread );
1542     }
1543 }