Release 20030408.
[wine/gsoc-2012-control.git] / server / thread.c
blobc6ef4acec4a9ddca9a663988a15617f86ea31275
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 *arg1; /* function arguments */
71 void *arg2;
72 void *arg3;
76 /* thread operations */
78 static void dump_thread( struct object *obj, int verbose );
79 static int thread_signaled( struct object *obj, struct thread *thread );
80 static void thread_poll_event( struct fd *fd, int event );
81 static void destroy_thread( struct object *obj );
82 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
84 static const struct object_ops thread_ops =
86 sizeof(struct thread), /* size */
87 dump_thread, /* dump */
88 add_queue, /* add_queue */
89 remove_queue, /* remove_queue */
90 thread_signaled, /* signaled */
91 no_satisfied, /* satisfied */
92 no_get_fd, /* get_fd */
93 destroy_thread /* destroy */
96 static const struct fd_ops thread_fd_ops =
98 NULL, /* get_poll_events */
99 thread_poll_event, /* poll_event */
100 no_flush, /* flush */
101 no_get_file_info, /* get_file_info */
102 no_queue_async /* queue_async */
105 static struct thread *first_thread;
106 static struct thread *booting_thread;
108 /* initialize the structure for a newly allocated thread */
109 inline static void init_thread_structure( struct thread *thread )
111 int i;
113 thread->unix_pid = -1; /* not known yet */
114 thread->unix_tid = -1; /* not known yet */
115 thread->context = NULL;
116 thread->teb = NULL;
117 thread->mutex = NULL;
118 thread->debug_ctx = NULL;
119 thread->debug_event = NULL;
120 thread->queue = NULL;
121 thread->hooks = NULL;
122 thread->wait = NULL;
123 thread->system_apc.head = NULL;
124 thread->system_apc.tail = NULL;
125 thread->user_apc.head = NULL;
126 thread->user_apc.tail = NULL;
127 thread->error = 0;
128 thread->req_data = NULL;
129 thread->req_toread = 0;
130 thread->reply_data = NULL;
131 thread->reply_towrite = 0;
132 thread->request_fd = NULL;
133 thread->reply_fd = NULL;
134 thread->wait_fd = NULL;
135 thread->state = RUNNING;
136 thread->attached = 0;
137 thread->exit_code = 0;
138 thread->next = NULL;
139 thread->prev = NULL;
140 thread->priority = THREAD_PRIORITY_NORMAL;
141 thread->affinity = 1;
142 thread->suspend = 0;
143 thread->creation_time = time(NULL);
144 thread->exit_time = 0;
146 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
147 thread->inflight[i].server = thread->inflight[i].client = -1;
150 /* create a new thread */
151 struct thread *create_thread( int fd, struct process *process )
153 struct thread *thread;
155 if (!(thread = alloc_object( &thread_ops ))) return NULL;
157 init_thread_structure( thread );
159 thread->process = (struct process *)grab_object( process );
160 if (!current) current = thread;
162 if (!booting_thread) /* first thread ever */
164 booting_thread = thread;
165 lock_master_socket(1);
168 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
169 first_thread = thread;
171 if (!(thread->id = alloc_ptid( thread )))
173 release_object( thread );
174 return NULL;
176 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
178 release_object( thread );
179 return NULL;
182 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
183 add_process_thread( thread->process, thread );
184 return thread;
187 /* handle a client event */
188 static void thread_poll_event( struct fd *fd, int event )
190 struct thread *thread = get_fd_user( fd );
191 assert( thread->obj.ops == &thread_ops );
193 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
194 else if (event & POLLIN) read_request( thread );
195 else if (event & POLLOUT) write_reply( thread );
198 /* cleanup everything that is no longer needed by a dead thread */
199 /* used by destroy_thread and kill_thread */
200 static void cleanup_thread( struct thread *thread )
202 int i;
203 struct thread_apc *apc;
205 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
206 if (thread->req_data) free( thread->req_data );
207 if (thread->reply_data) free( thread->reply_data );
208 if (thread->request_fd) release_object( thread->request_fd );
209 if (thread->reply_fd) release_object( thread->reply_fd );
210 if (thread->wait_fd) release_object( thread->wait_fd );
211 if (thread->hooks) release_object( thread->hooks );
212 free_msg_queue( thread );
213 destroy_thread_windows( thread );
214 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
216 if (thread->inflight[i].client != -1)
218 close( thread->inflight[i].server );
219 thread->inflight[i].client = thread->inflight[i].server = -1;
222 thread->req_data = NULL;
223 thread->reply_data = NULL;
224 thread->request_fd = NULL;
225 thread->reply_fd = NULL;
226 thread->wait_fd = NULL;
227 thread->hooks = NULL;
229 if (thread == booting_thread) /* killing booting thread */
231 booting_thread = NULL;
232 lock_master_socket(0);
236 /* destroy a thread when its refcount is 0 */
237 static void destroy_thread( struct object *obj )
239 struct thread_apc *apc;
240 struct thread *thread = (struct thread *)obj;
241 assert( obj->ops == &thread_ops );
243 assert( !thread->debug_ctx ); /* cannot still be debugging something */
244 if (thread->next) thread->next->prev = thread->prev;
245 if (thread->prev) thread->prev->next = thread->next;
246 else first_thread = thread->next;
247 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
248 cleanup_thread( thread );
249 release_object( thread->process );
250 if (thread->id) free_ptid( thread->id );
253 /* dump a thread on stdout for debugging purposes */
254 static void dump_thread( struct object *obj, int verbose )
256 struct thread *thread = (struct thread *)obj;
257 assert( obj->ops == &thread_ops );
259 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
260 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
263 static int thread_signaled( struct object *obj, struct thread *thread )
265 struct thread *mythread = (struct thread *)obj;
266 return (mythread->state == TERMINATED);
269 /* get a thread pointer from a thread id (and increment the refcount) */
270 struct thread *get_thread_from_id( thread_id_t id )
272 struct object *obj = get_ptid_entry( id );
274 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
275 set_error( STATUS_INVALID_PARAMETER );
276 return NULL;
279 /* get a thread from a handle (and increment the refcount) */
280 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
282 return (struct thread *)get_handle_obj( current->process, handle,
283 access, &thread_ops );
286 /* find a thread from a Unix pid */
287 struct thread *get_thread_from_pid( int pid )
289 struct thread *t;
291 for (t = first_thread; t; t = t->next) if (t->unix_tid == pid) return t;
292 for (t = first_thread; t; t = t->next) if (t->unix_pid == pid) return t;
293 return NULL;
296 /* set all information about a thread */
297 static void set_thread_info( struct thread *thread,
298 const struct set_thread_info_request *req )
300 if (req->mask & SET_THREAD_INFO_PRIORITY)
301 thread->priority = req->priority;
302 if (req->mask & SET_THREAD_INFO_AFFINITY)
304 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
305 else thread->affinity = req->affinity;
309 /* stop a thread (at the Unix level) */
310 void stop_thread( struct thread *thread )
312 /* can't stop a thread while initialisation is in progress */
313 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
316 /* suspend a thread */
317 static int suspend_thread( struct thread *thread )
319 int old_count = thread->suspend;
320 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
322 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
324 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
325 return old_count;
328 /* resume a thread */
329 static int resume_thread( struct thread *thread )
331 int old_count = thread->suspend;
332 if (thread->suspend > 0)
334 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
336 return old_count;
339 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
340 int add_queue( struct object *obj, struct wait_queue_entry *entry )
342 grab_object( obj );
343 entry->obj = obj;
344 entry->prev = obj->tail;
345 entry->next = NULL;
346 if (obj->tail) obj->tail->next = entry;
347 else obj->head = entry;
348 obj->tail = entry;
349 return 1;
352 /* remove a thread from an object wait queue */
353 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
355 if (entry->next) entry->next->prev = entry->prev;
356 else obj->tail = entry->prev;
357 if (entry->prev) entry->prev->next = entry->next;
358 else obj->head = entry->next;
359 release_object( obj );
362 /* finish waiting */
363 static void end_wait( struct thread *thread )
365 struct thread_wait *wait = thread->wait;
366 struct wait_queue_entry *entry;
367 int i;
369 assert( wait );
370 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
371 entry->obj->ops->remove_queue( entry->obj, entry );
372 if (wait->user) remove_timeout_user( wait->user );
373 thread->wait = wait->next;
374 free( wait );
377 /* build the thread wait structure */
378 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
380 struct thread_wait *wait;
381 struct wait_queue_entry *entry;
382 int i;
384 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
385 wait->next = current->wait;
386 wait->thread = current;
387 wait->count = count;
388 wait->flags = flags;
389 wait->user = NULL;
390 current->wait = wait;
391 if (flags & SELECT_TIMEOUT)
393 wait->timeout.tv_sec = timeout->sec;
394 wait->timeout.tv_usec = timeout->usec;
397 for (i = 0, entry = wait->queues; i < count; i++, entry++)
399 struct object *obj = objects[i];
400 entry->thread = current;
401 if (!obj->ops->add_queue( obj, entry ))
403 wait->count = i;
404 end_wait( current );
405 return 0;
408 return 1;
411 /* check if the thread waiting condition is satisfied */
412 static int check_wait( struct thread *thread )
414 int i, signaled;
415 struct thread_wait *wait = thread->wait;
416 struct wait_queue_entry *entry = wait->queues;
418 /* Suspended threads may not acquire locks */
419 if( thread->process->suspend + thread->suspend > 0 ) return -1;
421 assert( wait );
422 if (wait->flags & SELECT_ALL)
424 int not_ok = 0;
425 /* Note: we must check them all anyway, as some objects may
426 * want to do something when signaled, even if others are not */
427 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
428 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
429 if (not_ok) goto other_checks;
430 /* Wait satisfied: tell it to all objects */
431 signaled = 0;
432 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
433 if (entry->obj->ops->satisfied( entry->obj, thread ))
434 signaled = STATUS_ABANDONED_WAIT_0;
435 return signaled;
437 else
439 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
441 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
442 /* Wait satisfied: tell it to the object */
443 signaled = i;
444 if (entry->obj->ops->satisfied( entry->obj, thread ))
445 signaled = i + STATUS_ABANDONED_WAIT_0;
446 return signaled;
450 other_checks:
451 if ((wait->flags & SELECT_INTERRUPTIBLE) && thread->system_apc.head) return STATUS_USER_APC;
452 if ((wait->flags & SELECT_ALERTABLE) && thread->user_apc.head) return STATUS_USER_APC;
453 if (wait->flags & SELECT_TIMEOUT)
455 struct timeval now;
456 gettimeofday( &now, NULL );
457 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
459 return -1;
462 /* send the wakeup signal to a thread */
463 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
465 struct wake_up_reply reply;
466 int ret;
468 reply.cookie = cookie;
469 reply.signaled = signaled;
470 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
471 return 0;
472 if (ret >= 0)
473 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
474 else if (errno == EPIPE)
475 kill_thread( thread, 0 ); /* normal death */
476 else
477 fatal_protocol_perror( thread, "write" );
478 return -1;
481 /* attempt to wake up a thread */
482 /* return >0 if OK, 0 if the wait condition is still not satisfied */
483 int wake_thread( struct thread *thread )
485 int signaled, count;
486 void *cookie;
488 for (count = 0; thread->wait; count++)
490 if ((signaled = check_wait( thread )) == -1) break;
492 cookie = thread->wait->cookie;
493 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
494 thread->id, signaled, cookie );
495 end_wait( thread );
496 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
497 break;
499 return count;
502 /* thread wait timeout */
503 static void thread_timeout( void *ptr )
505 struct thread_wait *wait = ptr;
506 struct thread *thread = wait->thread;
507 void *cookie = wait->cookie;
509 wait->user = NULL;
510 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
511 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
513 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
514 thread->id, STATUS_TIMEOUT, cookie );
515 end_wait( thread );
516 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
517 /* check if other objects have become signaled in the meantime */
518 wake_thread( thread );
521 /* select on a list of handles */
522 static void select_on( int count, void *cookie, const obj_handle_t *handles,
523 int flags, const abs_time_t *timeout )
525 int ret, i;
526 struct object *objects[MAXIMUM_WAIT_OBJECTS];
528 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
530 set_error( STATUS_INVALID_PARAMETER );
531 return;
533 for (i = 0; i < count; i++)
535 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
536 break;
539 if (i < count) goto done;
540 if (!wait_on( count, objects, flags, timeout )) goto done;
542 if ((ret = check_wait( current )) != -1)
544 /* condition is already satisfied */
545 end_wait( current );
546 set_error( ret );
547 goto done;
550 /* now we need to wait */
551 if (flags & SELECT_TIMEOUT)
553 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
554 thread_timeout, current->wait )))
556 end_wait( current );
557 goto done;
560 current->wait->cookie = cookie;
561 set_error( STATUS_PENDING );
563 done:
564 while (--i >= 0) release_object( objects[i] );
567 /* attempt to wake threads sleeping on the object wait queue */
568 void wake_up( struct object *obj, int max )
570 struct wait_queue_entry *entry = obj->head;
572 while (entry)
574 struct thread *thread = entry->thread;
575 entry = entry->next;
576 if (wake_thread( thread ))
578 if (max && !--max) break;
583 /* queue an async procedure call */
584 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
585 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
587 struct thread_apc *apc;
588 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
590 /* cancel a possible previous APC with the same owner */
591 if (owner) thread_cancel_apc( thread, owner, system );
592 if (thread->state == TERMINATED) return 0;
594 if (!(apc = mem_alloc( sizeof(*apc) ))) return 0;
595 apc->prev = queue->tail;
596 apc->next = NULL;
597 apc->owner = owner;
598 apc->func = func;
599 apc->type = type;
600 apc->arg1 = arg1;
601 apc->arg2 = arg2;
602 apc->arg3 = arg3;
603 queue->tail = apc;
604 if (!apc->prev) /* first one */
606 queue->head = apc;
607 wake_thread( thread );
609 else apc->prev->next = apc;
611 return 1;
614 /* cancel the async procedure call owned by a specific object */
615 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
617 struct thread_apc *apc;
618 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
619 for (apc = queue->head; apc; apc = apc->next)
621 if (apc->owner != owner) continue;
622 if (apc->next) apc->next->prev = apc->prev;
623 else queue->tail = apc->prev;
624 if (apc->prev) apc->prev->next = apc->next;
625 else queue->head = apc->next;
626 free( apc );
627 return;
631 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
632 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
634 struct thread_apc *apc;
635 struct apc_queue *queue = &thread->system_apc;
637 if (!queue->head && !system_only) queue = &thread->user_apc;
638 if ((apc = queue->head))
640 if (apc->next) apc->next->prev = NULL;
641 else queue->tail = NULL;
642 queue->head = apc->next;
644 return apc;
647 /* add an fd to the inflight list */
648 /* return list index, or -1 on error */
649 int thread_add_inflight_fd( struct thread *thread, int client, int server )
651 int i;
653 if (server == -1) return -1;
654 if (client == -1)
656 close( server );
657 return -1;
660 /* first check if we already have an entry for this fd */
661 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
662 if (thread->inflight[i].client == client)
664 close( thread->inflight[i].server );
665 thread->inflight[i].server = server;
666 return i;
669 /* now find a free spot to store it */
670 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
671 if (thread->inflight[i].client == -1)
673 thread->inflight[i].client = client;
674 thread->inflight[i].server = server;
675 return i;
677 return -1;
680 /* get an inflight fd and purge it from the list */
681 /* the fd must be closed when no longer used */
682 int thread_get_inflight_fd( struct thread *thread, int client )
684 int i, ret;
686 if (client == -1) return -1;
690 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
692 if (thread->inflight[i].client == client)
694 ret = thread->inflight[i].server;
695 thread->inflight[i].server = thread->inflight[i].client = -1;
696 return ret;
699 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
700 return -1;
703 /* retrieve an LDT selector entry */
704 static void get_selector_entry( struct thread *thread, int entry,
705 unsigned int *base, unsigned int *limit,
706 unsigned char *flags )
708 if (!thread->process->ldt_copy)
710 set_error( STATUS_ACCESS_DENIED );
711 return;
713 if (entry >= 8192)
715 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
716 return;
718 if (suspend_for_ptrace( thread ))
720 unsigned char flags_buf[4];
721 int *addr = (int *)thread->process->ldt_copy + entry;
722 if (read_thread_int( thread, addr, base ) == -1) goto done;
723 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
724 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
725 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
726 *flags = flags_buf[entry & 3];
727 done:
728 resume_after_ptrace( thread );
732 /* kill a thread on the spot */
733 void kill_thread( struct thread *thread, int violent_death )
735 if (thread->state == TERMINATED) return; /* already killed */
736 thread->state = TERMINATED;
737 thread->exit_time = time(NULL);
738 if (current == thread) current = NULL;
739 if (debug_level)
740 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
741 thread->id, thread->exit_code );
742 if (thread->wait)
744 while (thread->wait) end_wait( thread );
745 send_thread_wakeup( thread, NULL, STATUS_PENDING );
746 /* if it is waiting on the socket, we don't need to send a SIGTERM */
747 violent_death = 0;
749 kill_console_processes( thread, 0 );
750 debug_exit_thread( thread );
751 abandon_mutexes( thread );
752 remove_process_thread( thread->process, thread );
753 wake_up( &thread->obj, 0 );
754 detach_thread( thread, violent_death ? SIGTERM : 0 );
755 cleanup_thread( thread );
756 release_object( thread );
759 /* take a snapshot of currently running threads */
760 struct thread_snapshot *thread_snap( int *count )
762 struct thread_snapshot *snapshot, *ptr;
763 struct thread *thread;
764 int total = 0;
766 for (thread = first_thread; thread; thread = thread->next)
767 if (thread->state != TERMINATED) total++;
768 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
769 ptr = snapshot;
770 for (thread = first_thread; thread; thread = thread->next)
772 if (thread->state == TERMINATED) continue;
773 ptr->thread = thread;
774 ptr->count = thread->obj.refcount;
775 ptr->priority = thread->priority;
776 grab_object( thread );
777 ptr++;
779 *count = total;
780 return snapshot;
783 /* signal that we are finished booting on the client side */
784 DECL_HANDLER(boot_done)
786 debug_level = max( debug_level, req->debug_level );
787 if (current == booting_thread)
789 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
790 lock_master_socket(0); /* allow other clients now */
794 /* create a new thread */
795 DECL_HANDLER(new_thread)
797 struct thread *thread;
798 int request_fd = thread_get_inflight_fd( current, req->request_fd );
800 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
802 if (request_fd != -1) close( request_fd );
803 set_error( STATUS_INVALID_HANDLE );
804 return;
807 if ((thread = create_thread( request_fd, current->process )))
809 if (req->suspend) thread->suspend++;
810 reply->tid = get_thread_id( thread );
811 if ((reply->handle = alloc_handle( current->process, thread,
812 THREAD_ALL_ACCESS, req->inherit )))
814 /* thread object will be released when the thread gets killed */
815 return;
817 kill_thread( thread, 1 );
821 /* initialize a new thread */
822 DECL_HANDLER(init_thread)
824 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
825 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
827 if (current->unix_pid != -1)
829 fatal_protocol_error( current, "init_thread: already running\n" );
830 goto error;
832 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
834 fatal_protocol_error( current, "bad reply fd\n" );
835 goto error;
837 if (wait_fd == -1)
839 fatal_protocol_error( current, "bad wait fd\n" );
840 goto error;
842 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj );
843 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj );
844 if (!current->reply_fd || !current->wait_fd) return;
846 current->unix_pid = req->unix_pid;
847 current->unix_tid = req->unix_tid;
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->timeout );
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,
972 req->arg1, req->arg2, req->arg3 );
973 release_object( thread );
977 /* get next APC to call */
978 DECL_HANDLER(get_apc)
980 struct thread_apc *apc;
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 reply->func = apc->func;
999 reply->type = apc->type;
1000 reply->arg1 = apc->arg1;
1001 reply->arg2 = apc->arg2;
1002 reply->arg3 = apc->arg3;
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 );