New mechanism to transfer file descriptors from client to server.
[wine/multimedia.git] / server / thread.c
blob25361c4af2b728e31ad2b7093f93ce764ac5c2b0
1 /*
2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #ifdef HAVE_SYS_MMAN_H
17 #include <sys/mman.h>
18 #endif
19 #include <sys/types.h>
20 #ifdef HAVE_SYS_SOCKET_H
21 # include <sys/socket.h>
22 #endif
23 #include <sys/uio.h>
24 #include <unistd.h>
25 #include <stdarg.h>
27 #include "winbase.h"
29 #include "handle.h"
30 #include "process.h"
31 #include "thread.h"
32 #include "request.h"
35 /* thread queues */
37 struct thread_wait
39 int count; /* count of objects */
40 int flags;
41 struct timeval timeout;
42 struct timeout_user *user;
43 struct wait_queue_entry queues[1];
46 /* asynchronous procedure calls */
48 struct thread_apc
50 struct thread_apc *next; /* queue linked list */
51 struct thread_apc *prev;
52 struct object *owner; /* object that queued this apc */
53 void *func; /* function to call in client */
54 enum apc_type type; /* type of apc function */
55 int nb_args; /* number of arguments */
56 void *args[1]; /* function arguments */
60 /* thread operations */
62 static void dump_thread( struct object *obj, int verbose );
63 static int thread_signaled( struct object *obj, struct thread *thread );
64 static void thread_poll_event( struct object *obj, int event );
65 static void destroy_thread( struct object *obj );
66 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
68 static const struct object_ops thread_ops =
70 sizeof(struct thread), /* size */
71 dump_thread, /* dump */
72 add_queue, /* add_queue */
73 remove_queue, /* remove_queue */
74 thread_signaled, /* signaled */
75 no_satisfied, /* satisfied */
76 NULL, /* get_poll_events */
77 thread_poll_event, /* poll_event */
78 no_get_fd, /* get_fd */
79 no_flush, /* flush */
80 no_get_file_info, /* get_file_info */
81 destroy_thread /* destroy */
84 static struct thread *first_thread;
85 static struct thread *booting_thread;
87 /* allocate the buffer for the communication with the client */
88 static int alloc_client_buffer( struct thread *thread )
90 union generic_request *req;
91 int fd = -1, fd_pipe[2], wait_pipe[2];
93 wait_pipe[0] = wait_pipe[1] = -1;
94 if (pipe( fd_pipe ) == -1)
96 file_set_error();
97 return 0;
99 if (pipe( wait_pipe ) == -1) goto error;
100 if ((fd = create_anonymous_file()) == -1) goto error;
101 if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error;
102 if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE,
103 MAP_SHARED, fd, 0 )) == (void*)-1) goto error;
104 if (!(thread->request_fd = create_request_socket( thread ))) goto error;
105 thread->reply_fd = fd_pipe[1];
106 thread->wait_fd = wait_pipe[1];
108 /* make the pipes non-blocking */
109 fcntl( fd_pipe[1], F_SETFL, O_NONBLOCK );
110 fcntl( wait_pipe[1], F_SETFL, O_NONBLOCK );
112 /* build the first request into the buffer and send it */
113 req = thread->buffer;
114 req->get_thread_buffer.pid = get_process_id( thread->process );
115 req->get_thread_buffer.tid = get_thread_id( thread );
116 req->get_thread_buffer.boot = (thread == booting_thread);
117 req->get_thread_buffer.version = SERVER_PROTOCOL_VERSION;
119 /* add it here since send_client_fd may call kill_thread */
120 add_process_thread( thread->process, thread );
122 send_client_fd( thread, fd_pipe[0], 0 );
123 send_client_fd( thread, wait_pipe[0], 0 );
124 send_client_fd( thread, fd, 0 );
125 send_reply( thread, req );
126 close( fd_pipe[0] );
127 close( wait_pipe[0] );
128 close( fd );
130 return 1;
132 error:
133 file_set_error();
134 if (fd != -1) close( fd );
135 close( fd_pipe[0] );
136 close( fd_pipe[1] );
137 if (wait_pipe[0] != -1) close( wait_pipe[0] );
138 if (wait_pipe[1] != -1) close( wait_pipe[1] );
139 return 0;
142 /* initialize the structure for a newly allocated thread */
143 inline static void init_thread_structure( struct thread *thread )
145 int i;
147 thread->unix_pid = 0; /* not known yet */
148 thread->context = NULL;
149 thread->teb = NULL;
150 thread->mutex = NULL;
151 thread->debug_ctx = NULL;
152 thread->debug_event = NULL;
153 thread->queue = NULL;
154 thread->info = NULL;
155 thread->wait = NULL;
156 thread->system_apc.head = NULL;
157 thread->system_apc.tail = NULL;
158 thread->user_apc.head = NULL;
159 thread->user_apc.tail = NULL;
160 thread->error = 0;
161 thread->request_fd = NULL;
162 thread->reply_fd = -1;
163 thread->wait_fd = -1;
164 thread->state = RUNNING;
165 thread->attached = 0;
166 thread->exit_code = 0;
167 thread->next = NULL;
168 thread->prev = NULL;
169 thread->priority = THREAD_PRIORITY_NORMAL;
170 thread->affinity = 1;
171 thread->suspend = 0;
172 thread->buffer = (void *)-1;
173 thread->last_req = REQ_get_thread_buffer;
175 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
176 thread->inflight[i].server = thread->inflight[i].client = -1;
179 /* create a new thread */
180 struct thread *create_thread( int fd, struct process *process )
182 struct thread *thread;
184 int flags = fcntl( fd, F_GETFL, 0 );
185 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
187 if (!(thread = alloc_object( &thread_ops, fd ))) return NULL;
189 init_thread_structure( thread );
191 thread->process = (struct process *)grab_object( process );
192 if (!current) current = thread;
194 if (!booting_thread) /* first thread ever */
196 booting_thread = thread;
197 lock_master_socket(1);
200 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
201 first_thread = thread;
203 #if 0
204 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
205 #endif
206 if (!alloc_client_buffer( thread )) goto error;
207 return thread;
209 error:
210 release_object( thread );
211 return NULL;
214 /* handle a client event */
215 static void thread_poll_event( struct object *obj, int event )
217 struct thread *thread = (struct thread *)obj;
218 assert( obj->ops == &thread_ops );
220 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
221 #if 0
222 else if (event & POLLIN) read_request( thread );
223 #endif
226 /* cleanup everything that is no longer needed by a dead thread */
227 /* used by destroy_thread and kill_thread */
228 static void cleanup_thread( struct thread *thread )
230 int i;
231 struct thread_apc *apc;
233 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
234 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
235 if (thread->reply_fd != -1) close( thread->reply_fd );
236 if (thread->wait_fd != -1) close( thread->wait_fd );
237 if (thread->request_fd) release_object( thread->request_fd );
238 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
240 if (thread->inflight[i].client != -1)
242 close( thread->inflight[i].server );
243 thread->inflight[i].client = thread->inflight[i].server = -1;
246 thread->buffer = (void *)-1;
247 thread->reply_fd = -1;
248 thread->wait_fd = -1;
249 thread->request_fd = NULL;
252 /* destroy a thread when its refcount is 0 */
253 static void destroy_thread( struct object *obj )
255 struct thread_apc *apc;
256 struct thread *thread = (struct thread *)obj;
257 assert( obj->ops == &thread_ops );
259 assert( !thread->debug_ctx ); /* cannot still be debugging something */
260 release_object( thread->process );
261 if (thread->next) thread->next->prev = thread->prev;
262 if (thread->prev) thread->prev->next = thread->next;
263 else first_thread = thread->next;
264 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
265 if (thread->info) release_object( thread->info );
266 if (thread->queue) release_object( thread->queue );
267 cleanup_thread( thread );
270 /* dump a thread on stdout for debugging purposes */
271 static void dump_thread( struct object *obj, int verbose )
273 struct thread *thread = (struct thread *)obj;
274 assert( obj->ops == &thread_ops );
276 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
277 thread->unix_pid, thread->teb, thread->state );
280 static int thread_signaled( struct object *obj, struct thread *thread )
282 struct thread *mythread = (struct thread *)obj;
283 return (mythread->state == TERMINATED);
286 /* get a thread pointer from a thread id (and increment the refcount) */
287 struct thread *get_thread_from_id( void *id )
289 struct thread *t = first_thread;
290 while (t && (t != id)) t = t->next;
291 if (t) grab_object( t );
292 return t;
295 /* get a thread from a handle (and increment the refcount) */
296 struct thread *get_thread_from_handle( handle_t handle, unsigned int access )
298 return (struct thread *)get_handle_obj( current->process, handle,
299 access, &thread_ops );
302 /* find a thread from a Unix pid */
303 struct thread *get_thread_from_pid( int pid )
305 struct thread *t = first_thread;
306 while (t && (t->unix_pid != pid)) t = t->next;
307 return t;
310 /* set all information about a thread */
311 static void set_thread_info( struct thread *thread,
312 struct set_thread_info_request *req )
314 if (req->mask & SET_THREAD_INFO_PRIORITY)
315 thread->priority = req->priority;
316 if (req->mask & SET_THREAD_INFO_AFFINITY)
318 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
319 else thread->affinity = req->affinity;
323 /* suspend a thread */
324 int suspend_thread( struct thread *thread, int check_limit )
326 int old_count = thread->suspend;
327 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
329 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
331 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
332 return old_count;
335 /* resume a thread */
336 int resume_thread( struct thread *thread )
338 int old_count = thread->suspend;
339 if (thread->suspend > 0)
341 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
343 return old_count;
346 /* suspend all threads but the current */
347 void suspend_all_threads( void )
349 struct thread *thread;
350 for ( thread = first_thread; thread; thread = thread->next )
351 if ( thread != current )
352 suspend_thread( thread, 0 );
355 /* resume all threads but the current */
356 void resume_all_threads( void )
358 struct thread *thread;
359 for ( thread = first_thread; thread; thread = thread->next )
360 if ( thread != current )
361 resume_thread( thread );
364 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
365 int add_queue( struct object *obj, struct wait_queue_entry *entry )
367 grab_object( obj );
368 entry->obj = obj;
369 entry->prev = obj->tail;
370 entry->next = NULL;
371 if (obj->tail) obj->tail->next = entry;
372 else obj->head = entry;
373 obj->tail = entry;
374 return 1;
377 /* remove a thread from an object wait queue */
378 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
380 if (entry->next) entry->next->prev = entry->prev;
381 else obj->tail = entry->prev;
382 if (entry->prev) entry->prev->next = entry->next;
383 else obj->head = entry->next;
384 release_object( obj );
387 /* finish waiting */
388 static void end_wait( struct thread *thread )
390 struct thread_wait *wait = thread->wait;
391 struct wait_queue_entry *entry;
392 int i;
394 assert( wait );
395 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
396 entry->obj->ops->remove_queue( entry->obj, entry );
397 if (wait->user) remove_timeout_user( wait->user );
398 free( wait );
399 thread->wait = NULL;
402 /* build the thread wait structure */
403 static int wait_on( int count, struct object *objects[], int flags, int sec, int usec )
405 struct thread_wait *wait;
406 struct wait_queue_entry *entry;
407 int i;
409 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
410 current->wait = wait;
411 wait->count = count;
412 wait->flags = flags;
413 wait->user = NULL;
414 if (flags & SELECT_TIMEOUT)
416 wait->timeout.tv_sec = sec;
417 wait->timeout.tv_usec = usec;
420 for (i = 0, entry = wait->queues; i < count; i++, entry++)
422 struct object *obj = objects[i];
423 entry->thread = current;
424 if (!obj->ops->add_queue( obj, entry ))
426 wait->count = i;
427 end_wait( current );
428 return 0;
431 return 1;
434 /* check if the thread waiting condition is satisfied */
435 static int check_wait( struct thread *thread )
437 int i, signaled;
438 struct thread_wait *wait = thread->wait;
439 struct wait_queue_entry *entry = wait->queues;
441 assert( wait );
442 if (wait->flags & SELECT_ALL)
444 int not_ok = 0;
445 /* Note: we must check them all anyway, as some objects may
446 * want to do something when signaled, even if others are not */
447 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
448 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
449 if (not_ok) goto other_checks;
450 /* Wait satisfied: tell it to all objects */
451 signaled = 0;
452 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
453 if (entry->obj->ops->satisfied( entry->obj, thread ))
454 signaled = STATUS_ABANDONED_WAIT_0;
455 return signaled;
457 else
459 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
461 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
462 /* Wait satisfied: tell it to the object */
463 signaled = i;
464 if (entry->obj->ops->satisfied( entry->obj, thread ))
465 signaled = i + STATUS_ABANDONED_WAIT_0;
466 return signaled;
470 other_checks:
471 if ((wait->flags & SELECT_INTERRUPTIBLE) && thread->system_apc.head) return STATUS_USER_APC;
472 if ((wait->flags & SELECT_ALERTABLE) && thread->user_apc.head) return STATUS_USER_APC;
473 if (wait->flags & SELECT_TIMEOUT)
475 struct timeval now;
476 gettimeofday( &now, NULL );
477 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
479 return -1;
482 /* attempt to wake up a thread */
483 /* return 1 if OK, 0 if the wait condition is still not satisfied */
484 static int wake_thread( struct thread *thread )
486 int signaled;
487 if ((signaled = check_wait( thread )) == -1) return 0;
489 if (debug_level) fprintf( stderr, "%08x: *wakeup* object=%d\n",
490 (unsigned int)thread, signaled );
491 end_wait( thread );
492 send_thread_wakeup( thread, signaled );
493 return 1;
496 /* thread wait timeout */
497 static void thread_timeout( void *ptr )
499 struct thread *thread = ptr;
501 if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread );
503 assert( thread->wait );
504 thread->wait->user = NULL;
505 end_wait( thread );
506 send_thread_wakeup( thread, STATUS_TIMEOUT );
509 /* select on a list of handles */
510 static void select_on( int count, handle_t *handles, int flags, int sec, int usec )
512 int ret, i;
513 struct object *objects[MAXIMUM_WAIT_OBJECTS];
515 assert( !current->wait );
517 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
519 set_error( STATUS_INVALID_PARAMETER );
520 return;
522 for (i = 0; i < count; i++)
524 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
525 break;
528 if (i < count) goto done;
529 if (!wait_on( count, objects, flags, sec, usec )) goto done;
531 if ((ret = check_wait( current )) != -1)
533 /* condition is already satisfied */
534 end_wait( current );
535 set_error( ret );
536 goto done;
539 /* now we need to wait */
540 if (flags & SELECT_TIMEOUT)
542 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
543 thread_timeout, current )))
545 end_wait( current );
546 goto done;
549 set_error( STATUS_PENDING );
551 done:
552 while (--i >= 0) release_object( objects[i] );
555 /* attempt to wake threads sleeping on the object wait queue */
556 void wake_up( struct object *obj, int max )
558 struct wait_queue_entry *entry = obj->head;
560 while (entry)
562 struct thread *thread = entry->thread;
563 entry = entry->next;
564 if (wake_thread( thread ))
566 if (max && !--max) break;
571 /* queue an async procedure call */
572 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
573 enum apc_type type, int system, int nb_args, ... )
575 struct thread_apc *apc;
576 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
578 /* cancel a possible previous APC with the same owner */
579 if (owner) thread_cancel_apc( thread, owner, system );
581 if (!(apc = mem_alloc( sizeof(*apc) + (nb_args-1)*sizeof(apc->args[0]) ))) return 0;
582 apc->prev = queue->tail;
583 apc->next = NULL;
584 apc->owner = owner;
585 apc->func = func;
586 apc->type = type;
587 apc->nb_args = nb_args;
588 if (nb_args)
590 int i;
591 va_list args;
592 va_start( args, nb_args );
593 for (i = 0; i < nb_args; i++) apc->args[i] = va_arg( args, void * );
594 va_end( args );
596 queue->tail = apc;
597 if (!apc->prev) /* first one */
599 queue->head = apc;
600 if (thread->wait) wake_thread( thread );
602 return 1;
605 /* cancel the async procedure call owned by a specific object */
606 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
608 struct thread_apc *apc;
609 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
610 for (apc = queue->head; apc; apc = apc->next)
612 if (apc->owner != owner) continue;
613 if (apc->next) apc->next->prev = apc->prev;
614 else queue->tail = apc->prev;
615 if (apc->prev) apc->prev->next = apc->next;
616 else queue->head = apc->next;
617 free( apc );
618 return;
622 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
623 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
625 struct thread_apc *apc;
626 struct apc_queue *queue = &thread->system_apc;
628 if (!queue->head && !system_only) queue = &thread->user_apc;
629 if ((apc = queue->head))
631 if (apc->next) apc->next->prev = NULL;
632 else queue->tail = NULL;
633 queue->head = apc->next;
635 return apc;
638 /* add an fd to the inflight list */
639 /* return list index, or -1 on error */
640 int thread_add_inflight_fd( struct thread *thread, int client, int server )
642 int i;
644 if (server == -1) return -1;
645 if (client == -1)
647 close( server );
648 return -1;
651 /* first check if we already have an entry for this fd */
652 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
653 if (thread->inflight[i].client == client)
655 close( thread->inflight[i].server );
656 thread->inflight[i].server = server;
657 return i;
660 /* now find a free spot to store it */
661 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
662 if (thread->inflight[i].client == -1)
664 thread->inflight[i].client = client;
665 thread->inflight[i].server = server;
666 return i;
668 return -1;
671 /* get an inflight fd and purge it from the list */
672 /* the fd must be closed when no longer used */
673 int thread_get_inflight_fd( struct thread *thread, int client )
675 int i, ret;
677 if (client == -1) return -1;
681 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
683 if (thread->inflight[i].client == client)
685 ret = thread->inflight[i].server;
686 thread->inflight[i].server = thread->inflight[i].client = -1;
687 return ret;
690 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
691 return -1;
694 /* retrieve an LDT selector entry */
695 static void get_selector_entry( struct thread *thread, int entry,
696 unsigned int *base, unsigned int *limit,
697 unsigned char *flags )
699 if (!thread->process->ldt_copy)
701 set_error( STATUS_ACCESS_DENIED );
702 return;
704 if (entry >= 8192)
706 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
707 return;
709 if (suspend_for_ptrace( thread ))
711 unsigned char flags_buf[4];
712 int *addr = (int *)thread->process->ldt_copy + entry;
713 if (read_thread_int( thread, addr, base ) == -1) goto done;
714 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
715 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
716 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
717 *flags = flags_buf[entry & 3];
718 done:
719 resume_thread( thread );
723 /* kill a thread on the spot */
724 void kill_thread( struct thread *thread, int violent_death )
726 if (thread->state == TERMINATED) return; /* already killed */
727 thread->state = TERMINATED;
728 if (current == thread) current = NULL;
729 if (debug_level)
730 fprintf( stderr,"%08x: *killed* exit_code=%d\n",
731 (unsigned int)thread, thread->exit_code );
732 if (thread->wait)
734 end_wait( thread );
735 /* if it is waiting on the socket, we don't need to send a SIGTERM */
736 violent_death = 0;
738 debug_exit_thread( thread );
739 abandon_mutexes( thread );
740 remove_process_thread( thread->process, thread );
741 wake_up( &thread->obj, 0 );
742 detach_thread( thread, violent_death ? SIGTERM : 0 );
743 remove_select_user( &thread->obj );
744 cleanup_thread( thread );
745 release_object( thread );
748 /* take a snapshot of currently running threads */
749 struct thread_snapshot *thread_snap( int *count )
751 struct thread_snapshot *snapshot, *ptr;
752 struct thread *thread;
753 int total = 0;
755 for (thread = first_thread; thread; thread = thread->next)
756 if (thread->state != TERMINATED) total++;
757 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
758 ptr = snapshot;
759 for (thread = first_thread; thread; thread = thread->next)
761 if (thread->state == TERMINATED) continue;
762 ptr->thread = thread;
763 ptr->count = thread->obj.refcount;
764 ptr->priority = thread->priority;
765 grab_object( thread );
766 ptr++;
768 *count = total;
769 return snapshot;
772 /* signal that we are finished booting on the client side */
773 DECL_HANDLER(boot_done)
775 debug_level = max( debug_level, req->debug_level );
776 /* Make sure last_req is initialized */
777 current->last_req = REQ_boot_done;
778 if (current == booting_thread)
780 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
781 lock_master_socket(0); /* allow other clients now */
785 /* create a new thread */
786 DECL_HANDLER(new_thread)
788 struct thread *thread;
789 int sock[2];
791 if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1)
793 if ((thread = create_thread( sock[0], current->process )))
795 if (req->suspend) thread->suspend++;
796 req->tid = thread;
797 if ((req->handle = alloc_handle( current->process, thread,
798 THREAD_ALL_ACCESS, req->inherit )))
800 send_client_fd( current, sock[1], req->handle );
801 close( sock[1] );
802 /* thread object will be released when the thread gets killed */
803 return;
805 kill_thread( thread, 1 );
807 close( sock[1] );
809 else file_set_error();
812 /* retrieve the thread buffer file descriptor */
813 DECL_HANDLER(get_thread_buffer)
815 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
818 /* initialize a new thread */
819 DECL_HANDLER(init_thread)
821 if (current->unix_pid)
823 fatal_protocol_error( current, "init_thread: already running\n" );
824 return;
826 current->unix_pid = req->unix_pid;
827 current->teb = req->teb;
828 if (current->suspend + current->process->suspend > 0) stop_thread( current );
829 if (current->process->running_threads > 1)
830 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
833 /* terminate a thread */
834 DECL_HANDLER(terminate_thread)
836 struct thread *thread;
838 req->self = 0;
839 req->last = 0;
840 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
842 thread->exit_code = req->exit_code;
843 if (thread != current) kill_thread( thread, 1 );
844 else
846 req->self = 1;
847 req->last = (thread->process->running_threads == 1);
849 release_object( thread );
853 /* fetch information about a thread */
854 DECL_HANDLER(get_thread_info)
856 struct thread *thread;
857 handle_t handle = req->handle;
859 if (!handle) thread = get_thread_from_id( req->tid_in );
860 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
862 if (thread)
864 req->tid = get_thread_id( thread );
865 req->teb = thread->teb;
866 req->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
867 req->priority = thread->priority;
868 release_object( thread );
872 /* set information about a thread */
873 DECL_HANDLER(set_thread_info)
875 struct thread *thread;
877 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
879 set_thread_info( thread, req );
880 release_object( thread );
884 /* suspend a thread */
885 DECL_HANDLER(suspend_thread)
887 struct thread *thread;
889 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
891 req->count = suspend_thread( thread, 1 );
892 release_object( thread );
896 /* resume a thread */
897 DECL_HANDLER(resume_thread)
899 struct thread *thread;
901 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
903 req->count = resume_thread( thread );
904 release_object( thread );
908 /* select on a handle list */
909 DECL_HANDLER(select)
911 int count = get_req_data_size(req) / sizeof(int);
912 select_on( count, get_req_data(req), req->flags, req->sec, req->usec );
915 /* queue an APC for a thread */
916 DECL_HANDLER(queue_apc)
918 struct thread *thread;
919 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
921 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user, 1, req->param );
922 release_object( thread );
926 /* get next APC to call */
927 DECL_HANDLER(get_apc)
929 struct thread_apc *apc;
930 size_t size;
932 for (;;)
934 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
936 /* no more APCs */
937 req->func = NULL;
938 req->type = APC_NONE;
939 set_req_data_size( req, 0 );
940 return;
942 /* Optimization: ignore APCs that have a NULL func; they are only used
943 * to wake up a thread, but since we got here the thread woke up already.
945 if (apc->func) break;
946 free( apc );
948 size = apc->nb_args * sizeof(apc->args[0]);
949 if (size > get_req_data_size(req)) size = get_req_data_size(req);
950 req->func = apc->func;
951 req->type = apc->type;
952 memcpy( get_req_data(req), apc->args, size );
953 set_req_data_size( req, size );
954 free( apc );
957 /* fetch a selector entry for a thread */
958 DECL_HANDLER(get_selector_entry)
960 struct thread *thread;
961 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
963 get_selector_entry( thread, req->entry, &req->base, &req->limit, &req->flags );
964 release_object( thread );