Stub for SHAutoComplete.
[wine.git] / server / thread.c
blob3db8b1e11203da540314be3bbbf086310638a4d5
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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>
36 #include "winbase.h"
38 #include "file.h"
39 #include "handle.h"
40 #include "process.h"
41 #include "thread.h"
42 #include "request.h"
43 #include "user.h"
46 /* thread queues */
48 struct thread_wait
50 struct thread_wait *next; /* next wait structure for this thread */
51 struct thread *thread; /* owner thread */
52 int count; /* count of objects */
53 int flags;
54 void *cookie; /* magic cookie to return to client */
55 struct timeval timeout;
56 struct timeout_user *user;
57 struct wait_queue_entry queues[1];
60 /* asynchronous procedure calls */
62 struct thread_apc
64 struct thread_apc *next; /* queue linked list */
65 struct thread_apc *prev;
66 struct object *owner; /* object that queued this apc */
67 void *func; /* function to call in client */
68 enum apc_type type; /* type of apc function */
69 int nb_args; /* number of arguments */
70 void *args[1]; /* function arguments */
74 /* thread operations */
76 static void dump_thread( struct object *obj, int verbose );
77 static int thread_signaled( struct object *obj, struct thread *thread );
78 static void thread_poll_event( struct fd *fd, int event );
79 static void destroy_thread( struct object *obj );
80 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
82 static const struct object_ops thread_ops =
84 sizeof(struct thread), /* size */
85 dump_thread, /* dump */
86 add_queue, /* add_queue */
87 remove_queue, /* remove_queue */
88 thread_signaled, /* signaled */
89 no_satisfied, /* satisfied */
90 no_get_fd, /* get_fd */
91 destroy_thread /* destroy */
94 static const struct fd_ops thread_fd_ops =
96 NULL, /* get_poll_events */
97 thread_poll_event, /* poll_event */
98 no_flush, /* flush */
99 no_get_file_info, /* get_file_info */
100 no_queue_async /* queue_async */
103 static struct thread *first_thread;
104 static struct thread *booting_thread;
106 /* initialize the structure for a newly allocated thread */
107 inline static void init_thread_structure( struct thread *thread )
109 int i;
111 thread->unix_pid = -1; /* not known yet */
112 thread->context = NULL;
113 thread->teb = NULL;
114 thread->mutex = NULL;
115 thread->debug_ctx = NULL;
116 thread->debug_event = NULL;
117 thread->queue = NULL;
118 thread->hooks = NULL;
119 thread->wait = NULL;
120 thread->system_apc.head = NULL;
121 thread->system_apc.tail = NULL;
122 thread->user_apc.head = NULL;
123 thread->user_apc.tail = NULL;
124 thread->error = 0;
125 thread->req_data = NULL;
126 thread->req_toread = 0;
127 thread->reply_data = NULL;
128 thread->reply_towrite = 0;
129 thread->request_fd = NULL;
130 thread->reply_fd = NULL;
131 thread->wait_fd = NULL;
132 thread->state = RUNNING;
133 thread->attached = 0;
134 thread->exit_code = 0;
135 thread->next = NULL;
136 thread->prev = NULL;
137 thread->priority = THREAD_PRIORITY_NORMAL;
138 thread->affinity = 1;
139 thread->suspend = 0;
140 thread->creation_time = time(NULL);
141 thread->exit_time = 0;
143 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
144 thread->inflight[i].server = thread->inflight[i].client = -1;
147 /* create a new thread */
148 struct thread *create_thread( int fd, struct process *process )
150 struct thread *thread;
152 if (!(thread = alloc_object( &thread_ops ))) return NULL;
154 init_thread_structure( thread );
156 thread->process = (struct process *)grab_object( process );
157 if (!current) current = thread;
159 if (!booting_thread) /* first thread ever */
161 booting_thread = thread;
162 lock_master_socket(1);
165 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
166 first_thread = thread;
168 if (!(thread->id = alloc_ptid( thread )))
170 release_object( thread );
171 return NULL;
173 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
175 release_object( thread );
176 return NULL;
179 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
180 add_process_thread( thread->process, thread );
181 return thread;
184 /* handle a client event */
185 static void thread_poll_event( struct fd *fd, int event )
187 struct thread *thread = get_fd_user( fd );
188 assert( thread->obj.ops == &thread_ops );
190 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
191 else if (event & POLLIN) read_request( thread );
192 else if (event & POLLOUT) write_reply( thread );
195 /* cleanup everything that is no longer needed by a dead thread */
196 /* used by destroy_thread and kill_thread */
197 static void cleanup_thread( struct thread *thread )
199 int i;
200 struct thread_apc *apc;
202 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
203 if (thread->req_data) free( thread->req_data );
204 if (thread->reply_data) free( thread->reply_data );
205 if (thread->request_fd) release_object( thread->request_fd );
206 if (thread->reply_fd) release_object( thread->reply_fd );
207 if (thread->wait_fd) release_object( thread->wait_fd );
208 if (thread->hooks) release_object( thread->hooks );
209 free_msg_queue( thread );
210 destroy_thread_windows( thread );
211 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
213 if (thread->inflight[i].client != -1)
215 close( thread->inflight[i].server );
216 thread->inflight[i].client = thread->inflight[i].server = -1;
219 thread->req_data = NULL;
220 thread->reply_data = NULL;
221 thread->request_fd = NULL;
222 thread->reply_fd = NULL;
223 thread->wait_fd = NULL;
224 thread->hooks = NULL;
226 if (thread == booting_thread) /* killing booting thread */
228 booting_thread = NULL;
229 lock_master_socket(0);
233 /* destroy a thread when its refcount is 0 */
234 static void destroy_thread( struct object *obj )
236 struct thread_apc *apc;
237 struct thread *thread = (struct thread *)obj;
238 assert( obj->ops == &thread_ops );
240 assert( !thread->debug_ctx ); /* cannot still be debugging something */
241 if (thread->next) thread->next->prev = thread->prev;
242 if (thread->prev) thread->prev->next = thread->next;
243 else first_thread = thread->next;
244 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
245 cleanup_thread( thread );
246 release_object( thread->process );
247 if (thread->id) free_ptid( thread->id );
250 /* dump a thread on stdout for debugging purposes */
251 static void dump_thread( struct object *obj, int verbose )
253 struct thread *thread = (struct thread *)obj;
254 assert( obj->ops == &thread_ops );
256 fprintf( stderr, "Thread id=%04x unix pid=%d teb=%p state=%d\n",
257 thread->id, thread->unix_pid, thread->teb, thread->state );
260 static int thread_signaled( struct object *obj, struct thread *thread )
262 struct thread *mythread = (struct thread *)obj;
263 return (mythread->state == TERMINATED);
266 /* get a thread pointer from a thread id (and increment the refcount) */
267 struct thread *get_thread_from_id( thread_id_t id )
269 struct object *obj = get_ptid_entry( id );
271 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
272 set_error( STATUS_INVALID_PARAMETER );
273 return NULL;
276 /* get a thread from a handle (and increment the refcount) */
277 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
279 return (struct thread *)get_handle_obj( current->process, handle,
280 access, &thread_ops );
283 /* find a thread from a Unix pid */
284 struct thread *get_thread_from_pid( int pid )
286 struct thread *t = first_thread;
287 while (t && (t->unix_pid != pid)) t = t->next;
288 return t;
291 /* set all information about a thread */
292 static void set_thread_info( struct thread *thread,
293 const struct set_thread_info_request *req )
295 if (req->mask & SET_THREAD_INFO_PRIORITY)
296 thread->priority = req->priority;
297 if (req->mask & SET_THREAD_INFO_AFFINITY)
299 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
300 else thread->affinity = req->affinity;
304 /* stop a thread (at the Unix level) */
305 void stop_thread( struct thread *thread )
307 /* can't stop a thread while initialisation is in progress */
308 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
311 /* suspend a thread */
312 static int suspend_thread( struct thread *thread )
314 int old_count = thread->suspend;
315 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
317 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
319 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
320 return old_count;
323 /* resume a thread */
324 static int resume_thread( struct thread *thread )
326 int old_count = thread->suspend;
327 if (thread->suspend > 0)
329 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
331 return old_count;
334 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
335 int add_queue( struct object *obj, struct wait_queue_entry *entry )
337 grab_object( obj );
338 entry->obj = obj;
339 entry->prev = obj->tail;
340 entry->next = NULL;
341 if (obj->tail) obj->tail->next = entry;
342 else obj->head = entry;
343 obj->tail = entry;
344 return 1;
347 /* remove a thread from an object wait queue */
348 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
350 if (entry->next) entry->next->prev = entry->prev;
351 else obj->tail = entry->prev;
352 if (entry->prev) entry->prev->next = entry->next;
353 else obj->head = entry->next;
354 release_object( obj );
357 /* finish waiting */
358 static void end_wait( struct thread *thread )
360 struct thread_wait *wait = thread->wait;
361 struct wait_queue_entry *entry;
362 int i;
364 assert( wait );
365 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
366 entry->obj->ops->remove_queue( entry->obj, entry );
367 if (wait->user) remove_timeout_user( wait->user );
368 thread->wait = wait->next;
369 free( wait );
372 /* build the thread wait structure */
373 static int wait_on( int count, struct object *objects[], int flags, int sec, int usec )
375 struct thread_wait *wait;
376 struct wait_queue_entry *entry;
377 int i;
379 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
380 wait->next = current->wait;
381 wait->thread = current;
382 wait->count = count;
383 wait->flags = flags;
384 wait->user = NULL;
385 current->wait = wait;
386 if (flags & SELECT_TIMEOUT)
388 wait->timeout.tv_sec = sec;
389 wait->timeout.tv_usec = usec;
392 for (i = 0, entry = wait->queues; i < count; i++, entry++)
394 struct object *obj = objects[i];
395 entry->thread = current;
396 if (!obj->ops->add_queue( obj, entry ))
398 wait->count = i;
399 end_wait( current );
400 return 0;
403 return 1;
406 /* check if the thread waiting condition is satisfied */
407 static int check_wait( struct thread *thread )
409 int i, signaled;
410 struct thread_wait *wait = thread->wait;
411 struct wait_queue_entry *entry = wait->queues;
413 /* Suspended threads may not acquire locks */
414 if( thread->process->suspend + thread->suspend > 0 ) return -1;
416 assert( wait );
417 if (wait->flags & SELECT_ALL)
419 int not_ok = 0;
420 /* Note: we must check them all anyway, as some objects may
421 * want to do something when signaled, even if others are not */
422 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
423 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
424 if (not_ok) goto other_checks;
425 /* Wait satisfied: tell it to all objects */
426 signaled = 0;
427 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
428 if (entry->obj->ops->satisfied( entry->obj, thread ))
429 signaled = STATUS_ABANDONED_WAIT_0;
430 return signaled;
432 else
434 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
436 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
437 /* Wait satisfied: tell it to the object */
438 signaled = i;
439 if (entry->obj->ops->satisfied( entry->obj, thread ))
440 signaled = i + STATUS_ABANDONED_WAIT_0;
441 return signaled;
445 other_checks:
446 if ((wait->flags & SELECT_INTERRUPTIBLE) && thread->system_apc.head) return STATUS_USER_APC;
447 if ((wait->flags & SELECT_ALERTABLE) && thread->user_apc.head) return STATUS_USER_APC;
448 if (wait->flags & SELECT_TIMEOUT)
450 struct timeval now;
451 gettimeofday( &now, NULL );
452 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
454 return -1;
457 /* send the wakeup signal to a thread */
458 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
460 struct wake_up_reply reply;
461 int ret;
463 reply.cookie = cookie;
464 reply.signaled = signaled;
465 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
466 return 0;
467 if (ret >= 0)
468 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
469 else if (errno == EPIPE)
470 kill_thread( thread, 0 ); /* normal death */
471 else
472 fatal_protocol_perror( thread, "write" );
473 return -1;
476 /* attempt to wake up a thread */
477 /* return >0 if OK, 0 if the wait condition is still not satisfied */
478 int wake_thread( struct thread *thread )
480 int signaled, count;
481 void *cookie;
483 for (count = 0; thread->wait; count++)
485 if ((signaled = check_wait( thread )) == -1) break;
487 cookie = thread->wait->cookie;
488 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
489 thread->id, signaled, cookie );
490 end_wait( thread );
491 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
492 break;
494 return count;
497 /* thread wait timeout */
498 static void thread_timeout( void *ptr )
500 struct thread_wait *wait = ptr;
501 struct thread *thread = wait->thread;
502 void *cookie = wait->cookie;
504 wait->user = NULL;
505 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
506 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
508 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
509 thread->id, STATUS_TIMEOUT, cookie );
510 end_wait( thread );
511 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
512 /* check if other objects have become signaled in the meantime */
513 wake_thread( thread );
516 /* select on a list of handles */
517 static void select_on( int count, void *cookie, const obj_handle_t *handles,
518 int flags, int sec, int usec )
520 int ret, i;
521 struct object *objects[MAXIMUM_WAIT_OBJECTS];
523 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
525 set_error( STATUS_INVALID_PARAMETER );
526 return;
528 for (i = 0; i < count; i++)
530 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
531 break;
534 if (i < count) goto done;
535 if (!wait_on( count, objects, flags, sec, usec )) goto done;
537 if ((ret = check_wait( current )) != -1)
539 /* condition is already satisfied */
540 end_wait( current );
541 set_error( ret );
542 goto done;
545 /* now we need to wait */
546 if (flags & SELECT_TIMEOUT)
548 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
549 thread_timeout, current->wait )))
551 end_wait( current );
552 goto done;
555 current->wait->cookie = cookie;
556 set_error( STATUS_PENDING );
558 done:
559 while (--i >= 0) release_object( objects[i] );
562 /* attempt to wake threads sleeping on the object wait queue */
563 void wake_up( struct object *obj, int max )
565 struct wait_queue_entry *entry = obj->head;
567 while (entry)
569 struct thread *thread = entry->thread;
570 entry = entry->next;
571 if (wake_thread( thread ))
573 if (max && !--max) break;
578 /* queue an async procedure call */
579 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
580 enum apc_type type, int system, int nb_args, ... )
582 struct thread_apc *apc;
583 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
585 /* cancel a possible previous APC with the same owner */
586 if (owner) thread_cancel_apc( thread, owner, system );
587 if (thread->state == TERMINATED) return 0;
589 if (!(apc = mem_alloc( sizeof(*apc) + (nb_args-1)*sizeof(apc->args[0]) ))) return 0;
590 apc->prev = queue->tail;
591 apc->next = NULL;
592 apc->owner = owner;
593 apc->func = func;
594 apc->type = type;
595 apc->nb_args = nb_args;
596 if (nb_args)
598 int i;
599 va_list args;
600 va_start( args, nb_args );
601 for (i = 0; i < nb_args; i++) apc->args[i] = va_arg( args, void * );
602 va_end( args );
604 queue->tail = apc;
605 if (!apc->prev) /* first one */
607 queue->head = apc;
608 wake_thread( thread );
610 else apc->prev->next = apc;
612 return 1;
615 /* cancel the async procedure call owned by a specific object */
616 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
618 struct thread_apc *apc;
619 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
620 for (apc = queue->head; apc; apc = apc->next)
622 if (apc->owner != owner) continue;
623 if (apc->next) apc->next->prev = apc->prev;
624 else queue->tail = apc->prev;
625 if (apc->prev) apc->prev->next = apc->next;
626 else queue->head = apc->next;
627 free( apc );
628 return;
632 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
633 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
635 struct thread_apc *apc;
636 struct apc_queue *queue = &thread->system_apc;
638 if (!queue->head && !system_only) queue = &thread->user_apc;
639 if ((apc = queue->head))
641 if (apc->next) apc->next->prev = NULL;
642 else queue->tail = NULL;
643 queue->head = apc->next;
645 return apc;
648 /* add an fd to the inflight list */
649 /* return list index, or -1 on error */
650 int thread_add_inflight_fd( struct thread *thread, int client, int server )
652 int i;
654 if (server == -1) return -1;
655 if (client == -1)
657 close( server );
658 return -1;
661 /* first check if we already have an entry for this fd */
662 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
663 if (thread->inflight[i].client == client)
665 close( thread->inflight[i].server );
666 thread->inflight[i].server = server;
667 return i;
670 /* now find a free spot to store it */
671 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
672 if (thread->inflight[i].client == -1)
674 thread->inflight[i].client = client;
675 thread->inflight[i].server = server;
676 return i;
678 return -1;
681 /* get an inflight fd and purge it from the list */
682 /* the fd must be closed when no longer used */
683 int thread_get_inflight_fd( struct thread *thread, int client )
685 int i, ret;
687 if (client == -1) return -1;
691 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
693 if (thread->inflight[i].client == client)
695 ret = thread->inflight[i].server;
696 thread->inflight[i].server = thread->inflight[i].client = -1;
697 return ret;
700 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
701 return -1;
704 /* retrieve an LDT selector entry */
705 static void get_selector_entry( struct thread *thread, int entry,
706 unsigned int *base, unsigned int *limit,
707 unsigned char *flags )
709 if (!thread->process->ldt_copy)
711 set_error( STATUS_ACCESS_DENIED );
712 return;
714 if (entry >= 8192)
716 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
717 return;
719 if (suspend_for_ptrace( thread ))
721 unsigned char flags_buf[4];
722 int *addr = (int *)thread->process->ldt_copy + entry;
723 if (read_thread_int( thread, addr, base ) == -1) goto done;
724 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
725 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
726 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
727 *flags = flags_buf[entry & 3];
728 done:
729 resume_after_ptrace( thread );
733 /* kill a thread on the spot */
734 void kill_thread( struct thread *thread, int violent_death )
736 if (thread->state == TERMINATED) return; /* already killed */
737 thread->state = TERMINATED;
738 thread->exit_time = time(NULL);
739 if (current == thread) current = NULL;
740 if (debug_level)
741 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
742 thread->id, thread->exit_code );
743 if (thread->wait)
745 while (thread->wait) end_wait( thread );
746 send_thread_wakeup( thread, NULL, STATUS_PENDING );
747 /* if it is waiting on the socket, we don't need to send a SIGTERM */
748 violent_death = 0;
750 kill_console_processes( thread, 0 );
751 debug_exit_thread( thread );
752 abandon_mutexes( thread );
753 remove_process_thread( thread->process, thread );
754 wake_up( &thread->obj, 0 );
755 detach_thread( thread, violent_death ? SIGTERM : 0 );
756 cleanup_thread( thread );
757 release_object( thread );
760 /* take a snapshot of currently running threads */
761 struct thread_snapshot *thread_snap( int *count )
763 struct thread_snapshot *snapshot, *ptr;
764 struct thread *thread;
765 int total = 0;
767 for (thread = first_thread; thread; thread = thread->next)
768 if (thread->state != TERMINATED) total++;
769 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
770 ptr = snapshot;
771 for (thread = first_thread; thread; thread = thread->next)
773 if (thread->state == TERMINATED) continue;
774 ptr->thread = thread;
775 ptr->count = thread->obj.refcount;
776 ptr->priority = thread->priority;
777 grab_object( thread );
778 ptr++;
780 *count = total;
781 return snapshot;
784 /* signal that we are finished booting on the client side */
785 DECL_HANDLER(boot_done)
787 debug_level = max( debug_level, req->debug_level );
788 if (current == booting_thread)
790 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
791 lock_master_socket(0); /* allow other clients now */
795 /* create a new thread */
796 DECL_HANDLER(new_thread)
798 struct thread *thread;
799 int request_fd = thread_get_inflight_fd( current, req->request_fd );
801 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
803 if (request_fd != -1) close( request_fd );
804 set_error( STATUS_INVALID_HANDLE );
805 return;
808 if ((thread = create_thread( request_fd, current->process )))
810 if (req->suspend) thread->suspend++;
811 reply->tid = get_thread_id( thread );
812 if ((reply->handle = alloc_handle( current->process, thread,
813 THREAD_ALL_ACCESS, req->inherit )))
815 /* thread object will be released when the thread gets killed */
816 return;
818 kill_thread( thread, 1 );
822 /* initialize a new thread */
823 DECL_HANDLER(init_thread)
825 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
826 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
828 if (current->unix_pid != -1)
830 fatal_protocol_error( current, "init_thread: already running\n" );
831 goto error;
833 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
835 fatal_protocol_error( current, "bad reply fd\n" );
836 goto error;
838 if (wait_fd == -1)
840 fatal_protocol_error( current, "bad wait fd\n" );
841 goto error;
843 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj );
844 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj );
845 if (!current->reply_fd || !current->wait_fd) return;
847 current->unix_pid = req->unix_pid;
848 current->teb = req->teb;
850 if (current->suspend + current->process->suspend > 0) stop_thread( current );
851 if (current->process->running_threads > 1)
852 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
854 reply->pid = get_process_id( current->process );
855 reply->tid = get_thread_id( current );
856 reply->boot = (current == booting_thread);
857 reply->version = SERVER_PROTOCOL_VERSION;
858 return;
860 error:
861 if (reply_fd != -1) close( reply_fd );
862 if (wait_fd != -1) close( wait_fd );
865 /* terminate a thread */
866 DECL_HANDLER(terminate_thread)
868 struct thread *thread;
870 reply->self = 0;
871 reply->last = 0;
872 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
874 thread->exit_code = req->exit_code;
875 if (thread != current) kill_thread( thread, 1 );
876 else
878 reply->self = 1;
879 reply->last = (thread->process->running_threads == 1);
881 release_object( thread );
885 /* open a handle to a thread */
886 DECL_HANDLER(open_thread)
888 struct thread *thread = get_thread_from_id( req->tid );
890 reply->handle = 0;
891 if (thread)
893 reply->handle = alloc_handle( current->process, thread, req->access, req->inherit );
894 release_object( thread );
898 /* fetch information about a thread */
899 DECL_HANDLER(get_thread_info)
901 struct thread *thread;
902 obj_handle_t handle = req->handle;
904 if (!handle) thread = get_thread_from_id( req->tid_in );
905 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
907 if (thread)
909 reply->tid = get_thread_id( thread );
910 reply->teb = thread->teb;
911 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
912 reply->priority = thread->priority;
913 reply->creation_time = thread->creation_time;
914 reply->exit_time = thread->exit_time;
916 release_object( thread );
920 /* set information about a thread */
921 DECL_HANDLER(set_thread_info)
923 struct thread *thread;
925 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
927 set_thread_info( thread, req );
928 release_object( thread );
932 /* suspend a thread */
933 DECL_HANDLER(suspend_thread)
935 struct thread *thread;
937 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
939 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
940 else reply->count = suspend_thread( thread );
941 release_object( thread );
945 /* resume a thread */
946 DECL_HANDLER(resume_thread)
948 struct thread *thread;
950 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
952 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
953 else reply->count = resume_thread( thread );
954 release_object( thread );
958 /* select on a handle list */
959 DECL_HANDLER(select)
961 int count = get_req_data_size() / sizeof(int);
962 select_on( count, req->cookie, get_req_data(), req->flags, req->sec, req->usec );
965 /* queue an APC for a thread */
966 DECL_HANDLER(queue_apc)
968 struct thread *thread;
969 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
971 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user, 1, req->param );
972 release_object( thread );
976 /* get next APC to call */
977 DECL_HANDLER(get_apc)
979 struct thread_apc *apc;
980 size_t size;
982 for (;;)
984 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
986 /* no more APCs */
987 reply->func = NULL;
988 reply->type = APC_NONE;
989 return;
991 /* Optimization: ignore APCs that have a NULL func; they are only used
992 * to wake up a thread, but since we got here the thread woke up already.
993 * Exception: for APC_ASYNC_IO, func == NULL is legal.
995 if (apc->func || apc->type == APC_ASYNC_IO) break;
996 free( apc );
998 size = apc->nb_args * sizeof(apc->args[0]);
999 if (size > get_reply_max_size()) size = get_reply_max_size();
1000 reply->func = apc->func;
1001 reply->type = apc->type;
1002 set_reply_data( apc->args, size );
1003 free( apc );
1006 /* fetch a selector entry for a thread */
1007 DECL_HANDLER(get_selector_entry)
1009 struct thread *thread;
1010 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1012 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1013 release_object( thread );