Implemented processing for actions: SPI_GET/SETGRIDGRANULARITY,
[wine] / server / thread.c
1 /*
2  * Server-side thread management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include <assert.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <stdarg.h>
20
21 #include "winbase.h"
22
23 #include "handle.h"
24 #include "process.h"
25 #include "thread.h"
26 #include "request.h"
27 #include "user.h"
28
29
30 /* thread queues */
31
32 struct thread_wait
33 {
34     struct thread_wait     *next;       /* next wait structure for this thread */
35     struct thread          *thread;     /* owner thread */
36     int                     count;      /* count of objects */
37     int                     flags;
38     void                   *cookie;     /* magic cookie to return to client */
39     struct timeval          timeout;
40     struct timeout_user    *user;
41     struct wait_queue_entry queues[1];
42 };
43
44 /* asynchronous procedure calls */
45
46 struct thread_apc
47 {
48     struct thread_apc  *next;     /* queue linked list */
49     struct thread_apc  *prev;
50     struct object      *owner;    /* object that queued this apc */
51     void               *func;     /* function to call in client */
52     enum apc_type       type;     /* type of apc function */
53     int                 nb_args;  /* number of arguments */
54     void               *args[1];  /* function arguments */
55 };
56
57
58 /* thread operations */
59
60 static void dump_thread( struct object *obj, int verbose );
61 static int thread_signaled( struct object *obj, struct thread *thread );
62 static void thread_poll_event( struct object *obj, int event );
63 static void destroy_thread( struct object *obj );
64 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
65
66 static const struct object_ops thread_ops =
67 {
68     sizeof(struct thread),      /* size */
69     dump_thread,                /* dump */
70     add_queue,                  /* add_queue */
71     remove_queue,               /* remove_queue */
72     thread_signaled,            /* signaled */
73     no_satisfied,               /* satisfied */
74     NULL,                       /* get_poll_events */
75     thread_poll_event,          /* poll_event */
76     no_get_fd,                  /* get_fd */
77     no_flush,                   /* flush */
78     no_get_file_info,           /* get_file_info */
79     destroy_thread              /* destroy */
80 };
81
82 static struct thread *first_thread;
83 static struct thread *booting_thread;
84
85 /* initialize the structure for a newly allocated thread */
86 inline static void init_thread_structure( struct thread *thread )
87 {
88     int i;
89
90     thread->unix_pid        = 0;  /* not known yet */
91     thread->context         = NULL;
92     thread->teb             = NULL;
93     thread->mutex           = NULL;
94     thread->debug_ctx       = NULL;
95     thread->debug_event     = NULL;
96     thread->queue           = NULL;
97     thread->info            = NULL;
98     thread->wait            = NULL;
99     thread->system_apc.head = NULL;
100     thread->system_apc.tail = NULL;
101     thread->user_apc.head   = NULL;
102     thread->user_apc.tail   = NULL;
103     thread->error           = 0;
104     thread->req_data        = NULL;
105     thread->req_toread      = 0;
106     thread->reply_data      = NULL;
107     thread->reply_towrite   = 0;
108     thread->reply_fd        = -1;
109     thread->wait_fd         = -1;
110     thread->state           = RUNNING;
111     thread->attached        = 0;
112     thread->exit_code       = 0;
113     thread->next            = NULL;
114     thread->prev            = NULL;
115     thread->priority        = THREAD_PRIORITY_NORMAL;
116     thread->affinity        = 1;
117     thread->suspend         = 0;
118
119     for (i = 0; i < MAX_INFLIGHT_FDS; i++)
120         thread->inflight[i].server = thread->inflight[i].client = -1;
121 }
122
123 /* create a new thread */
124 struct thread *create_thread( int fd, struct process *process )
125 {
126     struct thread *thread;
127
128     if (!(thread = alloc_object( &thread_ops, fd ))) return NULL;
129
130     init_thread_structure( thread );
131
132     thread->process = (struct process *)grab_object( process );
133     thread->request_fd = fd;
134     if (!current) current = thread;
135
136     if (!booting_thread)  /* first thread ever */
137     {
138         booting_thread = thread;
139         lock_master_socket(1);
140     }
141
142     if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
143     first_thread = thread;
144
145     set_select_events( &thread->obj, POLLIN );  /* start listening to events */
146     add_process_thread( thread->process, thread );
147     return thread;
148 }
149
150 /* handle a client event */
151 static void thread_poll_event( struct object *obj, int event )
152 {
153     struct thread *thread = (struct thread *)obj;
154     assert( obj->ops == &thread_ops );
155
156     if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
157     else if (event & POLLIN) read_request( thread );
158     else if (event & POLLOUT) write_reply( thread );
159 }
160
161 /* cleanup everything that is no longer needed by a dead thread */
162 /* used by destroy_thread and kill_thread */
163 static void cleanup_thread( struct thread *thread )
164 {
165     int i;
166     struct thread_apc *apc;
167
168     while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
169     if (thread->req_data) free( thread->req_data );
170     if (thread->reply_data) free( thread->reply_data );
171     if (thread->request_fd != -1) close( thread->request_fd );
172     if (thread->reply_fd != -1) close( thread->reply_fd );
173     if (thread->wait_fd != -1) close( thread->wait_fd );
174     if (thread->queue)
175     {
176         if (thread->process->queue == thread->queue)
177         {
178             release_object( thread->process->queue );
179             thread->process->queue = NULL;
180         }
181         release_object( thread->queue );
182         thread->queue = NULL;
183     }
184     destroy_thread_windows( thread );
185     for (i = 0; i < MAX_INFLIGHT_FDS; i++)
186     {
187         if (thread->inflight[i].client != -1)
188         {
189             close( thread->inflight[i].server );
190             thread->inflight[i].client = thread->inflight[i].server = -1;
191         }
192     }
193     thread->req_data = NULL;
194     thread->reply_data = NULL;
195     thread->request_fd = -1;
196     thread->reply_fd = -1;
197     thread->wait_fd = -1;
198 }
199
200 /* destroy a thread when its refcount is 0 */
201 static void destroy_thread( struct object *obj )
202 {
203     struct thread_apc *apc;
204     struct thread *thread = (struct thread *)obj;
205     assert( obj->ops == &thread_ops );
206
207     assert( !thread->debug_ctx );  /* cannot still be debugging something */
208     if (thread->next) thread->next->prev = thread->prev;
209     if (thread->prev) thread->prev->next = thread->next;
210     else first_thread = thread->next;
211     while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
212     if (thread->info) release_object( thread->info );
213     cleanup_thread( thread );
214     release_object( thread->process );
215 }
216
217 /* dump a thread on stdout for debugging purposes */
218 static void dump_thread( struct object *obj, int verbose )
219 {
220     struct thread *thread = (struct thread *)obj;
221     assert( obj->ops == &thread_ops );
222
223     fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
224              thread->unix_pid, thread->teb, thread->state );
225 }
226
227 static int thread_signaled( struct object *obj, struct thread *thread )
228 {
229     struct thread *mythread = (struct thread *)obj;
230     return (mythread->state == TERMINATED);
231 }
232
233 /* get a thread pointer from a thread id (and increment the refcount) */
234 struct thread *get_thread_from_id( void *id )
235 {
236     struct thread *t = first_thread;
237     while (t && (t != id)) t = t->next;
238     if (t) grab_object( t );
239     else set_error( STATUS_INVALID_PARAMETER );
240     return t;
241 }
242
243 /* get a thread from a handle (and increment the refcount) */
244 struct thread *get_thread_from_handle( handle_t handle, unsigned int access )
245 {
246     return (struct thread *)get_handle_obj( current->process, handle,
247                                             access, &thread_ops );
248 }
249
250 /* find a thread from a Unix pid */
251 struct thread *get_thread_from_pid( int pid )
252 {
253     struct thread *t = first_thread;
254     while (t && (t->unix_pid != pid)) t = t->next;
255     return t;
256 }
257
258 /* set all information about a thread */
259 static void set_thread_info( struct thread *thread,
260                              const struct set_thread_info_request *req )
261 {
262     if (req->mask & SET_THREAD_INFO_PRIORITY)
263         thread->priority = req->priority;
264     if (req->mask & SET_THREAD_INFO_AFFINITY)
265     {
266         if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
267         else thread->affinity = req->affinity;
268     }
269 }
270
271 /* suspend a thread */
272 int suspend_thread( struct thread *thread, int check_limit )
273 {
274     int old_count = thread->suspend;
275     if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
276     {
277         if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
278     }
279     else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
280     return old_count;
281 }
282
283 /* resume a thread */
284 int resume_thread( struct thread *thread )
285 {
286     int old_count = thread->suspend;
287     if (thread->suspend > 0)
288     {
289         if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
290     }
291     return old_count;
292 }
293
294 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
295 int add_queue( struct object *obj, struct wait_queue_entry *entry )
296 {
297     grab_object( obj );
298     entry->obj    = obj;
299     entry->prev   = obj->tail;
300     entry->next   = NULL;
301     if (obj->tail) obj->tail->next = entry;
302     else obj->head = entry;
303     obj->tail = entry;
304     return 1;
305 }
306
307 /* remove a thread from an object wait queue */
308 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
309 {
310     if (entry->next) entry->next->prev = entry->prev;
311     else obj->tail = entry->prev;
312     if (entry->prev) entry->prev->next = entry->next;
313     else obj->head = entry->next;
314     release_object( obj );
315 }
316
317 /* finish waiting */
318 static void end_wait( struct thread *thread )
319 {
320     struct thread_wait *wait = thread->wait;
321     struct wait_queue_entry *entry;
322     int i;
323
324     assert( wait );
325     for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
326         entry->obj->ops->remove_queue( entry->obj, entry );
327     if (wait->user) remove_timeout_user( wait->user );
328     thread->wait = wait->next;
329     free( wait );
330 }
331
332 /* build the thread wait structure */
333 static int wait_on( int count, struct object *objects[], int flags, int sec, int usec )
334 {
335     struct thread_wait *wait;
336     struct wait_queue_entry *entry;
337     int i;
338
339     if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
340     wait->next    = current->wait;
341     wait->thread  = current;
342     wait->count   = count;
343     wait->flags   = flags;
344     wait->user    = NULL;
345     current->wait = wait;
346     if (flags & SELECT_TIMEOUT)
347     {
348         wait->timeout.tv_sec = sec;
349         wait->timeout.tv_usec = usec;
350     }
351
352     for (i = 0, entry = wait->queues; i < count; i++, entry++)
353     {
354         struct object *obj = objects[i];
355         entry->thread = current;
356         if (!obj->ops->add_queue( obj, entry ))
357         {
358             wait->count = i;
359             end_wait( current );
360             return 0;
361         }
362     }
363     return 1;
364 }
365
366 /* check if the thread waiting condition is satisfied */
367 static int check_wait( struct thread *thread )
368 {
369     int i, signaled;
370     struct thread_wait *wait = thread->wait;
371     struct wait_queue_entry *entry = wait->queues;
372
373     assert( wait );
374     if (wait->flags & SELECT_ALL)
375     {
376         int not_ok = 0;
377         /* Note: we must check them all anyway, as some objects may
378          * want to do something when signaled, even if others are not */
379         for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
380             not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
381         if (not_ok) goto other_checks;
382         /* Wait satisfied: tell it to all objects */
383         signaled = 0;
384         for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
385             if (entry->obj->ops->satisfied( entry->obj, thread ))
386                 signaled = STATUS_ABANDONED_WAIT_0;
387         return signaled;
388     }
389     else
390     {
391         for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
392         {
393             if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
394             /* Wait satisfied: tell it to the object */
395             signaled = i;
396             if (entry->obj->ops->satisfied( entry->obj, thread ))
397                 signaled = i + STATUS_ABANDONED_WAIT_0;
398             return signaled;
399         }
400     }
401
402  other_checks:
403     if ((wait->flags & SELECT_INTERRUPTIBLE) && thread->system_apc.head) return STATUS_USER_APC;
404     if ((wait->flags & SELECT_ALERTABLE) && thread->user_apc.head) return STATUS_USER_APC;
405     if (wait->flags & SELECT_TIMEOUT)
406     {
407         struct timeval now;
408         gettimeofday( &now, NULL );
409         if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
410     }
411     return -1;
412 }
413
414 /* send the wakeup signal to a thread */
415 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
416 {
417     struct wake_up_reply reply;
418     int ret;
419
420     reply.cookie   = cookie;
421     reply.signaled = signaled;
422     if ((ret = write( thread->wait_fd, &reply, sizeof(reply) )) == sizeof(reply)) return 0;
423     if (ret >= 0)
424         fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
425     else if (errno == EPIPE)
426         kill_thread( thread, 0 );  /* normal death */
427     else
428         fatal_protocol_perror( thread, "write" );
429     return -1;
430 }
431
432 /* attempt to wake up a thread */
433 /* return >0 if OK, 0 if the wait condition is still not satisfied */
434 static int wake_thread( struct thread *thread )
435 {
436     int signaled, count;
437     void *cookie;
438
439     for (count = 0; thread->wait; count++)
440     {
441         if ((signaled = check_wait( thread )) == -1) break;
442
443         cookie = thread->wait->cookie;
444         if (debug_level) fprintf( stderr, "%08x: *wakeup* signaled=%d cookie=%p\n",
445                                   (unsigned int)thread, signaled, cookie );
446         end_wait( thread );
447         if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
448             break;
449     }
450     return count;
451 }
452
453 /* thread wait timeout */
454 static void thread_timeout( void *ptr )
455 {
456     struct thread_wait *wait = ptr;
457     struct thread *thread = wait->thread;
458     void *cookie = wait->cookie;
459
460     wait->user = NULL;
461     if (thread->wait != wait) return; /* not the top-level wait, ignore it */
462
463     if (debug_level) fprintf( stderr, "%08x: *wakeup* signaled=%d cookie=%p\n",
464                               (unsigned int)thread, STATUS_TIMEOUT, cookie );
465     end_wait( thread );
466     send_thread_wakeup( thread, cookie, STATUS_TIMEOUT );
467     /* check if other objects have become signaled in the meantime */
468     wake_thread( thread );
469 }
470
471 /* select on a list of handles */
472 static void select_on( int count, void *cookie, const handle_t *handles,
473                        int flags, int sec, int usec )
474 {
475     int ret, i;
476     struct object *objects[MAXIMUM_WAIT_OBJECTS];
477
478     if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
479     {
480         set_error( STATUS_INVALID_PARAMETER );
481         return;
482     }
483     for (i = 0; i < count; i++)
484     {
485         if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
486             break;
487     }
488
489     if (i < count) goto done;
490     if (!wait_on( count, objects, flags, sec, usec )) goto done;
491
492     if ((ret = check_wait( current )) != -1)
493     {
494         /* condition is already satisfied */
495         end_wait( current );
496         set_error( ret );
497         goto done;
498     }
499
500     /* now we need to wait */
501     if (flags & SELECT_TIMEOUT)
502     {
503         if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
504                                                       thread_timeout, current->wait )))
505         {
506             end_wait( current );
507             goto done;
508         }
509     }
510     current->wait->cookie = cookie;
511     set_error( STATUS_PENDING );
512
513 done:
514     while (--i >= 0) release_object( objects[i] );
515 }
516
517 /* attempt to wake threads sleeping on the object wait queue */
518 void wake_up( struct object *obj, int max )
519 {
520     struct wait_queue_entry *entry = obj->head;
521
522     while (entry)
523     {
524         struct thread *thread = entry->thread;
525         entry = entry->next;
526         if (wake_thread( thread ))
527         {
528             if (max && !--max) break;
529         }
530     }
531 }
532
533 /* queue an async procedure call */
534 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
535                       enum apc_type type, int system, int nb_args, ... )
536 {
537     struct thread_apc *apc;
538     struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
539
540     /* cancel a possible previous APC with the same owner */
541     if (owner) thread_cancel_apc( thread, owner, system );
542
543     if (!(apc = mem_alloc( sizeof(*apc) + (nb_args-1)*sizeof(apc->args[0]) ))) return 0;
544     apc->prev    = queue->tail;
545     apc->next    = NULL;
546     apc->owner   = owner;
547     apc->func    = func;
548     apc->type    = type;
549     apc->nb_args = nb_args;
550     if (nb_args)
551     {
552         int i;
553         va_list args;
554         va_start( args, nb_args );
555         for (i = 0; i < nb_args; i++) apc->args[i] = va_arg( args, void * );
556         va_end( args );
557     }
558     queue->tail = apc;
559     if (!apc->prev)  /* first one */
560     {
561         queue->head = apc;
562         wake_thread( thread );
563     }
564     return 1;
565 }
566
567 /* cancel the async procedure call owned by a specific object */
568 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
569 {
570     struct thread_apc *apc;
571     struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
572     for (apc = queue->head; apc; apc = apc->next)
573     {
574         if (apc->owner != owner) continue;
575         if (apc->next) apc->next->prev = apc->prev;
576         else queue->tail = apc->prev;
577         if (apc->prev) apc->prev->next = apc->next;
578         else queue->head = apc->next;
579         free( apc );
580         return;
581     }
582 }
583
584 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
585 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
586 {
587     struct thread_apc *apc;
588     struct apc_queue *queue = &thread->system_apc;
589
590     if (!queue->head && !system_only) queue = &thread->user_apc;
591     if ((apc = queue->head))
592     {
593         if (apc->next) apc->next->prev = NULL;
594         else queue->tail = NULL;
595         queue->head = apc->next;
596     }
597     return apc;
598 }
599
600 /* add an fd to the inflight list */
601 /* return list index, or -1 on error */
602 int thread_add_inflight_fd( struct thread *thread, int client, int server )
603 {
604     int i;
605
606     if (server == -1) return -1;
607     if (client == -1)
608     {
609         close( server );
610         return -1;
611     }
612
613     /* first check if we already have an entry for this fd */
614     for (i = 0; i < MAX_INFLIGHT_FDS; i++)
615         if (thread->inflight[i].client == client)
616         {
617             close( thread->inflight[i].server );
618             thread->inflight[i].server = server;
619             return i;
620         }
621
622     /* now find a free spot to store it */
623     for (i = 0; i < MAX_INFLIGHT_FDS; i++)
624         if (thread->inflight[i].client == -1)
625         {
626             thread->inflight[i].client = client;
627             thread->inflight[i].server = server;
628             return i;
629         }
630     return -1;
631 }
632
633 /* get an inflight fd and purge it from the list */
634 /* the fd must be closed when no longer used */
635 int thread_get_inflight_fd( struct thread *thread, int client )
636 {
637     int i, ret;
638
639     if (client == -1) return -1;
640
641     do
642     {
643         for (i = 0; i < MAX_INFLIGHT_FDS; i++)
644         {
645             if (thread->inflight[i].client == client)
646             {
647                 ret = thread->inflight[i].server;
648                 thread->inflight[i].server = thread->inflight[i].client = -1;
649                 return ret;
650             }
651         }
652     } while (!receive_fd( thread->process ));  /* in case it is still in the socket buffer */
653     return -1;
654 }
655
656 /* retrieve an LDT selector entry */
657 static void get_selector_entry( struct thread *thread, int entry,
658                                 unsigned int *base, unsigned int *limit,
659                                 unsigned char *flags )
660 {
661     if (!thread->process->ldt_copy)
662     {
663         set_error( STATUS_ACCESS_DENIED );
664         return;
665     }
666     if (entry >= 8192)
667     {
668         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
669         return;
670     }
671     if (suspend_for_ptrace( thread ))
672     {
673         unsigned char flags_buf[4];
674         int *addr = (int *)thread->process->ldt_copy + entry;
675         if (read_thread_int( thread, addr, base ) == -1) goto done;
676         if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
677         addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
678         if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
679         *flags = flags_buf[entry & 3];
680     done:
681         resume_thread( thread );
682     }
683 }
684
685 /* kill a thread on the spot */
686 void kill_thread( struct thread *thread, int violent_death )
687 {
688     if (thread->state == TERMINATED) return;  /* already killed */
689     thread->state = TERMINATED;
690     if (current == thread) current = NULL;
691     if (debug_level)
692         fprintf( stderr,"%08x: *killed* exit_code=%d\n",
693                  (unsigned int)thread, thread->exit_code );
694     if (thread->wait)
695     {
696         while (thread->wait) end_wait( thread );
697         send_thread_wakeup( thread, NULL, STATUS_PENDING );
698         /* if it is waiting on the socket, we don't need to send a SIGTERM */
699         violent_death = 0;
700     }
701     kill_console_processes( thread, 0 );
702     debug_exit_thread( thread );
703     abandon_mutexes( thread );
704     remove_process_thread( thread->process, thread );
705     wake_up( &thread->obj, 0 );
706     detach_thread( thread, violent_death ? SIGTERM : 0 );
707     if (thread->request_fd == thread->obj.fd) thread->request_fd = -1;
708     if (thread->reply_fd == thread->obj.fd) thread->reply_fd = -1;
709     remove_select_user( &thread->obj );
710     cleanup_thread( thread );
711     release_object( thread );
712 }
713
714 /* take a snapshot of currently running threads */
715 struct thread_snapshot *thread_snap( int *count )
716 {
717     struct thread_snapshot *snapshot, *ptr;
718     struct thread *thread;
719     int total = 0;
720
721     for (thread = first_thread; thread; thread = thread->next)
722         if (thread->state != TERMINATED) total++;
723     if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
724     ptr = snapshot;
725     for (thread = first_thread; thread; thread = thread->next)
726     {
727         if (thread->state == TERMINATED) continue;
728         ptr->thread   = thread;
729         ptr->count    = thread->obj.refcount;
730         ptr->priority = thread->priority;
731         grab_object( thread );
732         ptr++;
733     }
734     *count = total;
735     return snapshot;
736 }
737
738 /* signal that we are finished booting on the client side */
739 DECL_HANDLER(boot_done)
740 {
741     debug_level = max( debug_level, req->debug_level );
742     if (current == booting_thread)
743     {
744         booting_thread = (struct thread *)~0UL;  /* make sure it doesn't match other threads */
745         lock_master_socket(0);  /* allow other clients now */
746     }
747 }
748
749 /* create a new thread */
750 DECL_HANDLER(new_thread)
751 {
752     struct thread *thread;
753     int request_fd = thread_get_inflight_fd( current, req->request_fd );
754
755     if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
756     {
757         if (request_fd != -1) close( request_fd );
758         set_error( STATUS_INVALID_HANDLE );
759         return;
760     }
761
762     if ((thread = create_thread( request_fd, current->process )))
763     {
764         if (req->suspend) thread->suspend++;
765         reply->tid = thread;
766         if ((reply->handle = alloc_handle( current->process, thread,
767                                            THREAD_ALL_ACCESS, req->inherit )))
768         {
769             /* thread object will be released when the thread gets killed */
770             return;
771         }
772         kill_thread( thread, 1 );
773         request_fd = -1;
774     }
775 }
776
777 /* initialize a new thread */
778 DECL_HANDLER(init_thread)
779 {
780     int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
781     int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
782
783     if (current->unix_pid)
784     {
785         fatal_protocol_error( current, "init_thread: already running\n" );
786         goto error;
787     }
788     if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
789     {
790         fatal_protocol_error( current, "bad reply fd\n" );
791         goto error;
792     }
793     if (wait_fd == -1)
794     {
795         fatal_protocol_error( current, "bad wait fd\n" );
796         goto error;
797     }
798
799     current->unix_pid = req->unix_pid;
800     current->teb      = req->teb;
801     current->reply_fd = reply_fd;
802     current->wait_fd  = wait_fd;
803
804     if (current->suspend + current->process->suspend > 0) stop_thread( current );
805     if (current->process->running_threads > 1)
806         generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
807
808     reply->pid     = get_process_id( current->process );
809     reply->tid     = get_thread_id( current );
810     reply->boot    = (current == booting_thread);
811     reply->version = SERVER_PROTOCOL_VERSION;
812     return;
813
814  error:
815     if (reply_fd != -1) close( reply_fd );
816     if (wait_fd != -1) close( wait_fd );
817 }
818
819 /* terminate a thread */
820 DECL_HANDLER(terminate_thread)
821 {
822     struct thread *thread;
823
824     reply->self = 0;
825     reply->last = 0;
826     if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
827     {
828         thread->exit_code = req->exit_code;
829         if (thread != current) kill_thread( thread, 1 );
830         else
831         {
832             reply->self = 1;
833             reply->last = (thread->process->running_threads == 1);
834         }
835         release_object( thread );
836     }
837 }
838
839 /* fetch information about a thread */
840 DECL_HANDLER(get_thread_info)
841 {
842     struct thread *thread;
843     handle_t handle = req->handle;
844
845     if (!handle) thread = get_thread_from_id( req->tid_in );
846     else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
847
848     if (thread)
849     {
850         reply->tid       = get_thread_id( thread );
851         reply->teb       = thread->teb;
852         reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
853         reply->priority  = thread->priority;
854         release_object( thread );
855     }
856 }
857
858 /* set information about a thread */
859 DECL_HANDLER(set_thread_info)
860 {
861     struct thread *thread;
862
863     if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
864     {
865         set_thread_info( thread, req );
866         release_object( thread );
867     }
868 }
869
870 /* suspend a thread */
871 DECL_HANDLER(suspend_thread)
872 {
873     struct thread *thread;
874
875     if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
876     {
877         reply->count = suspend_thread( thread, 1 );
878         release_object( thread );
879     }
880 }
881
882 /* resume a thread */
883 DECL_HANDLER(resume_thread)
884 {
885     struct thread *thread;
886
887     if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
888     {
889         reply->count = resume_thread( thread );
890         release_object( thread );
891     }
892 }
893
894 /* select on a handle list */
895 DECL_HANDLER(select)
896 {
897     int count = get_req_data_size() / sizeof(int);
898     select_on( count, req->cookie, get_req_data(), req->flags, req->sec, req->usec );
899 }
900
901 /* queue an APC for a thread */
902 DECL_HANDLER(queue_apc)
903 {
904     struct thread *thread;
905     if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
906     {
907         thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user, 1, req->param );
908         release_object( thread );
909     }
910 }
911
912 /* get next APC to call */
913 DECL_HANDLER(get_apc)
914 {
915     struct thread_apc *apc;
916     size_t size;
917
918     for (;;)
919     {
920         if (!(apc = thread_dequeue_apc( current, !req->alertable )))
921         {
922             /* no more APCs */
923             reply->func = NULL;
924             reply->type = APC_NONE;
925             return;
926         }
927         /* Optimization: ignore APCs that have a NULL func; they are only used
928          * to wake up a thread, but since we got here the thread woke up already.
929          */
930         if (apc->func) break;
931         free( apc );
932     }
933     size = apc->nb_args * sizeof(apc->args[0]);
934     if (size > get_reply_max_size()) size = get_reply_max_size();
935     reply->func = apc->func;
936     reply->type = apc->type;
937     set_reply_data( apc->args, size );
938     free( apc );
939 }
940
941 /* fetch a selector entry for a thread */
942 DECL_HANDLER(get_selector_entry)
943 {
944     struct thread *thread;
945     if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
946     {
947         get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
948         release_object( thread );
949     }
950 }