Change Favourite(s) into Favorite(s).
[wine/wine-kai.git] / server / thread.c
blobde2368450bf036f8174903fbe3c102a640ade87d
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 "windef.h"
37 #include "winbase.h"
39 #include "file.h"
40 #include "handle.h"
41 #include "process.h"
42 #include "thread.h"
43 #include "request.h"
44 #include "user.h"
47 /* thread queues */
49 struct thread_wait
51 struct thread_wait *next; /* next wait structure for this thread */
52 struct thread *thread; /* owner thread */
53 int count; /* count of objects */
54 int flags;
55 void *cookie; /* magic cookie to return to client */
56 struct timeval timeout;
57 struct timeout_user *user;
58 struct wait_queue_entry queues[1];
61 /* asynchronous procedure calls */
63 struct thread_apc
65 struct list entry; /* queue linked list */
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 */
103 no_cancel_async /* cancel_async */
106 static struct list thread_list = LIST_INIT(thread_list);
107 static struct thread *booting_thread;
109 /* initialize the structure for a newly allocated thread */
110 inline static void init_thread_structure( struct thread *thread )
112 int i;
114 thread->unix_pid = -1; /* not known yet */
115 thread->unix_tid = -1; /* not known yet */
116 thread->context = NULL;
117 thread->teb = NULL;
118 thread->debug_ctx = NULL;
119 thread->debug_event = NULL;
120 thread->queue = NULL;
121 thread->wait = NULL;
122 thread->error = 0;
123 thread->req_data = NULL;
124 thread->req_toread = 0;
125 thread->reply_data = NULL;
126 thread->reply_towrite = 0;
127 thread->request_fd = NULL;
128 thread->reply_fd = NULL;
129 thread->wait_fd = NULL;
130 thread->state = RUNNING;
131 thread->attached = 0;
132 thread->exit_code = 0;
133 thread->priority = THREAD_PRIORITY_NORMAL;
134 thread->affinity = 1;
135 thread->suspend = 0;
136 thread->creation_time = time(NULL);
137 thread->exit_time = 0;
139 list_init( &thread->mutex_list );
140 list_init( &thread->system_apc );
141 list_init( &thread->user_apc );
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 list_add_head( &thread_list, &thread->entry );
167 if (!(thread->id = alloc_ptid( thread )))
169 release_object( thread );
170 return NULL;
172 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
174 release_object( thread );
175 return NULL;
178 thread->token = (struct token *) grab_object( process->token );
180 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
181 add_process_thread( thread->process, thread );
182 return thread;
185 /* handle a client event */
186 static void thread_poll_event( struct fd *fd, int event )
188 struct thread *thread = get_fd_user( fd );
189 assert( thread->obj.ops == &thread_ops );
191 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
192 else if (event & POLLIN) read_request( thread );
193 else if (event & POLLOUT) write_reply( thread );
196 /* cleanup everything that is no longer needed by a dead thread */
197 /* used by destroy_thread and kill_thread */
198 static void cleanup_thread( struct thread *thread )
200 int i;
201 struct thread_apc *apc;
203 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
204 if (thread->req_data) free( thread->req_data );
205 if (thread->reply_data) free( thread->reply_data );
206 if (thread->request_fd) release_object( thread->request_fd );
207 if (thread->reply_fd) release_object( thread->reply_fd );
208 if (thread->wait_fd) release_object( thread->wait_fd );
209 free_msg_queue( thread );
210 cleanup_clipboard_thread(thread);
211 destroy_thread_windows( thread );
212 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
214 if (thread->inflight[i].client != -1)
216 close( thread->inflight[i].server );
217 thread->inflight[i].client = thread->inflight[i].server = -1;
220 thread->req_data = NULL;
221 thread->reply_data = NULL;
222 thread->request_fd = NULL;
223 thread->reply_fd = NULL;
224 thread->wait_fd = 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 list_remove( &thread->entry );
242 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
243 cleanup_thread( thread );
244 release_object( thread->process );
245 if (thread->id) free_ptid( thread->id );
246 if (thread->token) release_object( thread->token );
249 /* dump a thread on stdout for debugging purposes */
250 static void dump_thread( struct object *obj, int verbose )
252 struct thread *thread = (struct thread *)obj;
253 assert( obj->ops == &thread_ops );
255 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
256 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
259 static int thread_signaled( struct object *obj, struct thread *thread )
261 struct thread *mythread = (struct thread *)obj;
262 return (mythread->state == TERMINATED);
265 /* get a thread pointer from a thread id (and increment the refcount) */
266 struct thread *get_thread_from_id( thread_id_t id )
268 struct object *obj = get_ptid_entry( id );
270 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
271 set_win32_error( ERROR_INVALID_THREAD_ID );
272 return NULL;
275 /* get a thread from a handle (and increment the refcount) */
276 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
278 return (struct thread *)get_handle_obj( current->process, handle,
279 access, &thread_ops );
282 /* find a thread from a Unix pid */
283 struct thread *get_thread_from_pid( int pid )
285 struct thread *thread;
287 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
289 if (thread->unix_tid == pid) return thread;
291 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
293 if (thread->unix_pid == pid) return thread;
295 return NULL;
298 /* set all information about a thread */
299 static void set_thread_info( struct thread *thread,
300 const struct set_thread_info_request *req )
302 if (req->mask & SET_THREAD_INFO_PRIORITY)
303 thread->priority = req->priority;
304 if (req->mask & SET_THREAD_INFO_AFFINITY)
306 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
307 else thread->affinity = req->affinity;
311 /* stop a thread (at the Unix level) */
312 void stop_thread( struct thread *thread )
314 /* can't stop a thread while initialisation is in progress */
315 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
318 /* suspend a thread */
319 static int suspend_thread( struct thread *thread )
321 int old_count = thread->suspend;
322 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
324 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
326 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
327 return old_count;
330 /* resume a thread */
331 static int resume_thread( struct thread *thread )
333 int old_count = thread->suspend;
334 if (thread->suspend > 0)
336 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
338 return old_count;
341 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
342 int add_queue( struct object *obj, struct wait_queue_entry *entry )
344 grab_object( obj );
345 entry->obj = obj;
346 entry->prev = obj->tail;
347 entry->next = NULL;
348 if (obj->tail) obj->tail->next = entry;
349 else obj->head = entry;
350 obj->tail = entry;
351 return 1;
354 /* remove a thread from an object wait queue */
355 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
357 if (entry->next) entry->next->prev = entry->prev;
358 else obj->tail = entry->prev;
359 if (entry->prev) entry->prev->next = entry->next;
360 else obj->head = entry->next;
361 release_object( obj );
364 /* finish waiting */
365 static void end_wait( struct thread *thread )
367 struct thread_wait *wait = thread->wait;
368 struct wait_queue_entry *entry;
369 int i;
371 assert( wait );
372 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
373 entry->obj->ops->remove_queue( entry->obj, entry );
374 if (wait->user) remove_timeout_user( wait->user );
375 thread->wait = wait->next;
376 free( wait );
379 /* build the thread wait structure */
380 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
382 struct thread_wait *wait;
383 struct wait_queue_entry *entry;
384 int i;
386 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
387 wait->next = current->wait;
388 wait->thread = current;
389 wait->count = count;
390 wait->flags = flags;
391 wait->user = NULL;
392 current->wait = wait;
393 if (flags & SELECT_TIMEOUT)
395 wait->timeout.tv_sec = timeout->sec;
396 wait->timeout.tv_usec = timeout->usec;
399 for (i = 0, entry = wait->queues; i < count; i++, entry++)
401 struct object *obj = objects[i];
402 entry->thread = current;
403 if (!obj->ops->add_queue( obj, entry ))
405 wait->count = i;
406 end_wait( current );
407 return 0;
410 return 1;
413 /* check if the thread waiting condition is satisfied */
414 static int check_wait( struct thread *thread )
416 int i, signaled;
417 struct thread_wait *wait = thread->wait;
418 struct wait_queue_entry *entry = wait->queues;
420 /* Suspended threads may not acquire locks */
421 if( thread->process->suspend + thread->suspend > 0 ) return -1;
423 assert( wait );
424 if (wait->flags & SELECT_ALL)
426 int not_ok = 0;
427 /* Note: we must check them all anyway, as some objects may
428 * want to do something when signaled, even if others are not */
429 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
430 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
431 if (not_ok) goto other_checks;
432 /* Wait satisfied: tell it to all objects */
433 signaled = 0;
434 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
435 if (entry->obj->ops->satisfied( entry->obj, thread ))
436 signaled = STATUS_ABANDONED_WAIT_0;
437 return signaled;
439 else
441 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
443 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
444 /* Wait satisfied: tell it to the object */
445 signaled = i;
446 if (entry->obj->ops->satisfied( entry->obj, thread ))
447 signaled = i + STATUS_ABANDONED_WAIT_0;
448 return signaled;
452 other_checks:
453 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
454 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
455 if (wait->flags & SELECT_TIMEOUT)
457 struct timeval now;
458 gettimeofday( &now, NULL );
459 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
461 return -1;
464 /* send the wakeup signal to a thread */
465 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
467 struct wake_up_reply reply;
468 int ret;
470 reply.cookie = cookie;
471 reply.signaled = signaled;
472 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
473 return 0;
474 if (ret >= 0)
475 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
476 else if (errno == EPIPE)
477 kill_thread( thread, 0 ); /* normal death */
478 else
479 fatal_protocol_perror( thread, "write" );
480 return -1;
483 /* attempt to wake up a thread */
484 /* return >0 if OK, 0 if the wait condition is still not satisfied */
485 int wake_thread( struct thread *thread )
487 int signaled, count;
488 void *cookie;
490 for (count = 0; thread->wait; count++)
492 if ((signaled = check_wait( thread )) == -1) break;
494 cookie = thread->wait->cookie;
495 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
496 thread->id, signaled, cookie );
497 end_wait( thread );
498 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
499 break;
501 return count;
504 /* thread wait timeout */
505 static void thread_timeout( void *ptr )
507 struct thread_wait *wait = ptr;
508 struct thread *thread = wait->thread;
509 void *cookie = wait->cookie;
511 wait->user = NULL;
512 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
513 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
515 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
516 thread->id, STATUS_TIMEOUT, cookie );
517 end_wait( thread );
518 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
519 /* check if other objects have become signaled in the meantime */
520 wake_thread( thread );
523 /* select on a list of handles */
524 static void select_on( int count, void *cookie, const obj_handle_t *handles,
525 int flags, const abs_time_t *timeout )
527 int ret, i;
528 struct object *objects[MAXIMUM_WAIT_OBJECTS];
530 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
532 set_error( STATUS_INVALID_PARAMETER );
533 return;
535 for (i = 0; i < count; i++)
537 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
538 break;
541 if (i < count) goto done;
542 if (!wait_on( count, objects, flags, timeout )) goto done;
544 if ((ret = check_wait( current )) != -1)
546 /* condition is already satisfied */
547 end_wait( current );
548 set_error( ret );
549 goto done;
552 /* now we need to wait */
553 if (flags & SELECT_TIMEOUT)
555 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
556 thread_timeout, current->wait )))
558 end_wait( current );
559 goto done;
562 current->wait->cookie = cookie;
563 set_error( STATUS_PENDING );
565 done:
566 while (--i >= 0) release_object( objects[i] );
569 /* attempt to wake threads sleeping on the object wait queue */
570 void wake_up( struct object *obj, int max )
572 struct wait_queue_entry *entry = obj->head;
574 while (entry)
576 struct thread *thread = entry->thread;
577 entry = entry->next;
578 if (wake_thread( thread ))
580 if (max && !--max) break;
585 /* queue an async procedure call */
586 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
587 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
589 struct thread_apc *apc;
590 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
592 /* cancel a possible previous APC with the same owner */
593 if (owner) thread_cancel_apc( thread, owner, system );
594 if (thread->state == TERMINATED) return 0;
596 if (!(apc = mem_alloc( sizeof(*apc) ))) return 0;
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 list_add_tail( queue, &apc->entry );
604 if (!list_prev( queue, &apc->entry )) /* first one */
605 wake_thread( thread );
607 return 1;
610 /* cancel the async procedure call owned by a specific object */
611 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
613 struct thread_apc *apc;
614 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
615 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
617 if (apc->owner != owner) continue;
618 list_remove( &apc->entry );
619 free( apc );
620 return;
624 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
625 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
627 struct thread_apc *apc = NULL;
628 struct list *ptr = list_head( &thread->system_apc );
630 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
631 if (ptr)
633 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
634 list_remove( ptr );
636 return apc;
639 /* add an fd to the inflight list */
640 /* return list index, or -1 on error */
641 int thread_add_inflight_fd( struct thread *thread, int client, int server )
643 int i;
645 if (server == -1) return -1;
646 if (client == -1)
648 close( server );
649 return -1;
652 /* first check if we already have an entry for this fd */
653 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
654 if (thread->inflight[i].client == client)
656 close( thread->inflight[i].server );
657 thread->inflight[i].server = server;
658 return i;
661 /* now find a free spot to store it */
662 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
663 if (thread->inflight[i].client == -1)
665 thread->inflight[i].client = client;
666 thread->inflight[i].server = server;
667 return i;
669 return -1;
672 /* get an inflight fd and purge it from the list */
673 /* the fd must be closed when no longer used */
674 int thread_get_inflight_fd( struct thread *thread, int client )
676 int i, ret;
678 if (client == -1) return -1;
682 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
684 if (thread->inflight[i].client == client)
686 ret = thread->inflight[i].server;
687 thread->inflight[i].server = thread->inflight[i].client = -1;
688 return ret;
691 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
692 return -1;
695 /* retrieve an LDT selector entry */
696 static void get_selector_entry( struct thread *thread, int entry,
697 unsigned int *base, unsigned int *limit,
698 unsigned char *flags )
700 if (!thread->process->ldt_copy)
702 set_error( STATUS_ACCESS_DENIED );
703 return;
705 if (entry >= 8192)
707 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
708 return;
710 if (suspend_for_ptrace( thread ))
712 unsigned char flags_buf[4];
713 int *addr = (int *)thread->process->ldt_copy + entry;
714 if (read_thread_int( thread, addr, base ) == -1) goto done;
715 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
716 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
717 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
718 *flags = flags_buf[entry & 3];
719 done:
720 resume_after_ptrace( thread );
724 /* kill a thread on the spot */
725 void kill_thread( struct thread *thread, int violent_death )
727 if (thread->state == TERMINATED) return; /* already killed */
728 thread->state = TERMINATED;
729 thread->exit_time = time(NULL);
730 if (current == thread) current = NULL;
731 if (debug_level)
732 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
733 thread->id, thread->exit_code );
734 if (thread->wait)
736 while (thread->wait) end_wait( thread );
737 send_thread_wakeup( thread, NULL, STATUS_PENDING );
738 /* if it is waiting on the socket, we don't need to send a SIGTERM */
739 violent_death = 0;
741 kill_console_processes( thread, 0 );
742 debug_exit_thread( thread );
743 abandon_mutexes( thread );
744 remove_process_thread( thread->process, thread );
745 wake_up( &thread->obj, 0 );
746 detach_thread( thread, violent_death ? SIGTERM : 0 );
747 cleanup_thread( thread );
748 release_object( thread );
751 /* take a snapshot of currently running threads */
752 struct thread_snapshot *thread_snap( int *count )
754 struct thread_snapshot *snapshot, *ptr;
755 struct thread *thread;
756 int total = 0;
758 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
759 if (thread->state != TERMINATED) total++;
760 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
761 ptr = snapshot;
762 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
764 if (thread->state == TERMINATED) continue;
765 ptr->thread = thread;
766 ptr->count = thread->obj.refcount;
767 ptr->priority = thread->priority;
768 grab_object( thread );
769 ptr++;
771 *count = total;
772 return snapshot;
775 /* signal that we are finished booting on the client side */
776 DECL_HANDLER(boot_done)
778 debug_level = max( debug_level, req->debug_level );
779 if (current == booting_thread)
781 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
782 lock_master_socket(0); /* allow other clients now */
786 /* create a new thread */
787 DECL_HANDLER(new_thread)
789 struct thread *thread;
790 int request_fd = thread_get_inflight_fd( current, req->request_fd );
792 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
794 if (request_fd != -1) close( request_fd );
795 set_error( STATUS_INVALID_HANDLE );
796 return;
799 if ((thread = create_thread( request_fd, current->process )))
801 if (req->suspend) thread->suspend++;
802 reply->tid = get_thread_id( thread );
803 if ((reply->handle = alloc_handle( current->process, thread,
804 THREAD_ALL_ACCESS, req->inherit )))
806 /* thread object will be released when the thread gets killed */
807 return;
809 kill_thread( thread, 1 );
813 /* initialize a new thread */
814 DECL_HANDLER(init_thread)
816 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
817 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
819 if (current->unix_pid != -1)
821 fatal_protocol_error( current, "init_thread: already running\n" );
822 goto error;
824 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
826 fatal_protocol_error( current, "bad reply fd\n" );
827 goto error;
829 if (wait_fd == -1)
831 fatal_protocol_error( current, "bad wait fd\n" );
832 goto error;
834 if (!(current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj )))
836 reply_fd = -1;
837 fatal_protocol_error( current, "could not allocate reply fd\n" );
838 goto error;
840 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj )))
841 return;
843 current->unix_pid = req->unix_pid;
844 current->unix_tid = req->unix_tid;
845 current->teb = req->teb;
847 if (current->suspend + current->process->suspend > 0) stop_thread( current );
848 if (current->process->running_threads > 1)
849 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
851 reply->pid = get_process_id( current->process );
852 reply->tid = get_thread_id( current );
853 reply->boot = (current == booting_thread);
854 reply->version = SERVER_PROTOCOL_VERSION;
855 return;
857 error:
858 if (reply_fd != -1) close( reply_fd );
859 if (wait_fd != -1) close( wait_fd );
862 /* terminate a thread */
863 DECL_HANDLER(terminate_thread)
865 struct thread *thread;
867 reply->self = 0;
868 reply->last = 0;
869 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
871 thread->exit_code = req->exit_code;
872 if (thread != current) kill_thread( thread, 1 );
873 else
875 reply->self = 1;
876 reply->last = (thread->process->running_threads == 1);
878 release_object( thread );
882 /* open a handle to a thread */
883 DECL_HANDLER(open_thread)
885 struct thread *thread = get_thread_from_id( req->tid );
887 reply->handle = 0;
888 if (thread)
890 reply->handle = alloc_handle( current->process, thread, req->access, req->inherit );
891 release_object( thread );
895 /* fetch information about a thread */
896 DECL_HANDLER(get_thread_info)
898 struct thread *thread;
899 obj_handle_t handle = req->handle;
901 if (!handle) thread = get_thread_from_id( req->tid_in );
902 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
904 if (thread)
906 reply->pid = get_process_id( thread->process );
907 reply->tid = get_thread_id( thread );
908 reply->teb = thread->teb;
909 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
910 reply->priority = thread->priority;
911 reply->affinity = thread->affinity;
912 reply->creation_time = thread->creation_time;
913 reply->exit_time = thread->exit_time;
915 release_object( thread );
919 /* set information about a thread */
920 DECL_HANDLER(set_thread_info)
922 struct thread *thread;
924 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
926 set_thread_info( thread, req );
927 release_object( thread );
931 /* suspend a thread */
932 DECL_HANDLER(suspend_thread)
934 struct thread *thread;
936 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
938 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
939 else reply->count = suspend_thread( thread );
940 release_object( thread );
944 /* resume a thread */
945 DECL_HANDLER(resume_thread)
947 struct thread *thread;
949 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
951 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
952 else reply->count = resume_thread( thread );
953 release_object( thread );
957 /* select on a handle list */
958 DECL_HANDLER(select)
960 int count = get_req_data_size() / sizeof(int);
961 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout );
964 /* queue an APC for a thread */
965 DECL_HANDLER(queue_apc)
967 struct thread *thread;
968 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
970 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
971 req->arg1, req->arg2, req->arg3 );
972 release_object( thread );
976 /* get next APC to call */
977 DECL_HANDLER(get_apc)
979 struct thread_apc *apc;
981 for (;;)
983 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
985 /* no more APCs */
986 reply->func = NULL;
987 reply->type = APC_NONE;
988 return;
990 /* Optimization: ignore APCs that have a NULL func; they are only used
991 * to wake up a thread, but since we got here the thread woke up already.
992 * Exception: for APC_ASYNC_IO, func == NULL is legal.
994 if (apc->func || apc->type == APC_ASYNC_IO) break;
995 free( apc );
997 reply->func = apc->func;
998 reply->type = apc->type;
999 reply->arg1 = apc->arg1;
1000 reply->arg2 = apc->arg2;
1001 reply->arg3 = apc->arg3;
1002 free( apc );
1005 /* fetch a selector entry for a thread */
1006 DECL_HANDLER(get_selector_entry)
1008 struct thread *thread;
1009 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1011 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1012 release_object( thread );