New set of macros for server calls; makes requests without variable
[wine/multimedia.git] / server / thread.c
blobd1b32631d525a5dd8ba40a89e61d2399979ba8bd
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 extern 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 /* create a new thread */
143 struct thread *create_thread( int fd, struct process *process )
145 struct thread *thread;
147 int flags = fcntl( fd, F_GETFL, 0 );
148 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
150 if (!(thread = alloc_object( &thread_ops, fd ))) return NULL;
152 thread->unix_pid = 0; /* not known yet */
153 thread->context = NULL;
154 thread->teb = NULL;
155 thread->mutex = NULL;
156 thread->debug_ctx = NULL;
157 thread->debug_event = NULL;
158 thread->queue = NULL;
159 thread->info = NULL;
160 thread->wait = NULL;
161 thread->system_apc.head = NULL;
162 thread->system_apc.tail = NULL;
163 thread->user_apc.head = NULL;
164 thread->user_apc.tail = NULL;
165 thread->error = 0;
166 thread->pass_fd = -1;
167 thread->request_fd = NULL;
168 thread->reply_fd = -1;
169 thread->wait_fd = -1;
170 thread->state = RUNNING;
171 thread->attached = 0;
172 thread->exit_code = 0;
173 thread->next = NULL;
174 thread->prev = NULL;
175 thread->priority = THREAD_PRIORITY_NORMAL;
176 thread->affinity = 1;
177 thread->suspend = 0;
178 thread->buffer = (void *)-1;
179 thread->last_req = REQ_get_thread_buffer;
180 thread->process = (struct process *)grab_object( process );
182 if (!current) current = thread;
184 if (!booting_thread) /* first thread ever */
186 booting_thread = thread;
187 lock_master_socket(1);
190 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
191 first_thread = thread;
193 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
194 if (!alloc_client_buffer( thread )) goto error;
195 return thread;
197 error:
198 release_object( thread );
199 return NULL;
202 /* handle a client event */
203 void thread_poll_event( struct object *obj, int event )
205 struct thread *thread = (struct thread *)obj;
206 assert( obj->ops == &thread_ops );
208 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
209 else if (event & POLLIN) read_request( thread );
212 /* destroy a thread when its refcount is 0 */
213 static void destroy_thread( struct object *obj )
215 struct thread_apc *apc;
216 struct thread *thread = (struct thread *)obj;
217 assert( obj->ops == &thread_ops );
219 assert( !thread->debug_ctx ); /* cannot still be debugging something */
220 release_object( thread->process );
221 if (thread->next) thread->next->prev = thread->prev;
222 if (thread->prev) thread->prev->next = thread->next;
223 else first_thread = thread->next;
224 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
225 if (thread->info) release_object( thread->info );
226 if (thread->queue) release_object( thread->queue );
227 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
228 if (thread->reply_fd != -1) close( thread->reply_fd );
229 if (thread->wait_fd != -1) close( thread->wait_fd );
230 if (thread->pass_fd != -1) close( thread->pass_fd );
231 if (thread->request_fd) release_object( thread->request_fd );
234 /* dump a thread on stdout for debugging purposes */
235 static void dump_thread( struct object *obj, int verbose )
237 struct thread *thread = (struct thread *)obj;
238 assert( obj->ops == &thread_ops );
240 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
241 thread->unix_pid, thread->teb, thread->state );
244 static int thread_signaled( struct object *obj, struct thread *thread )
246 struct thread *mythread = (struct thread *)obj;
247 return (mythread->state == TERMINATED);
250 /* get a thread pointer from a thread id (and increment the refcount) */
251 struct thread *get_thread_from_id( void *id )
253 struct thread *t = first_thread;
254 while (t && (t != id)) t = t->next;
255 if (t) grab_object( t );
256 return t;
259 /* get a thread from a handle (and increment the refcount) */
260 struct thread *get_thread_from_handle( handle_t handle, unsigned int access )
262 return (struct thread *)get_handle_obj( current->process, handle,
263 access, &thread_ops );
266 /* find a thread from a Unix pid */
267 struct thread *get_thread_from_pid( int pid )
269 struct thread *t = first_thread;
270 while (t && (t->unix_pid != pid)) t = t->next;
271 return t;
274 /* set all information about a thread */
275 static void set_thread_info( struct thread *thread,
276 struct set_thread_info_request *req )
278 if (req->mask & SET_THREAD_INFO_PRIORITY)
279 thread->priority = req->priority;
280 if (req->mask & SET_THREAD_INFO_AFFINITY)
282 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
283 else thread->affinity = req->affinity;
287 /* suspend a thread */
288 int suspend_thread( struct thread *thread, int check_limit )
290 int old_count = thread->suspend;
291 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
293 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
295 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
296 return old_count;
299 /* resume a thread */
300 int resume_thread( struct thread *thread )
302 int old_count = thread->suspend;
303 if (thread->suspend > 0)
305 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
307 return old_count;
310 /* suspend all threads but the current */
311 void suspend_all_threads( void )
313 struct thread *thread;
314 for ( thread = first_thread; thread; thread = thread->next )
315 if ( thread != current )
316 suspend_thread( thread, 0 );
319 /* resume all threads but the current */
320 void resume_all_threads( void )
322 struct thread *thread;
323 for ( thread = first_thread; thread; thread = thread->next )
324 if ( thread != current )
325 resume_thread( thread );
328 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
329 int add_queue( struct object *obj, struct wait_queue_entry *entry )
331 grab_object( obj );
332 entry->obj = obj;
333 entry->prev = obj->tail;
334 entry->next = NULL;
335 if (obj->tail) obj->tail->next = entry;
336 else obj->head = entry;
337 obj->tail = entry;
338 return 1;
341 /* remove a thread from an object wait queue */
342 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
344 if (entry->next) entry->next->prev = entry->prev;
345 else obj->tail = entry->prev;
346 if (entry->prev) entry->prev->next = entry->next;
347 else obj->head = entry->next;
348 release_object( obj );
351 /* finish waiting */
352 static void end_wait( struct thread *thread )
354 struct thread_wait *wait = thread->wait;
355 struct wait_queue_entry *entry;
356 int i;
358 assert( wait );
359 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
360 entry->obj->ops->remove_queue( entry->obj, entry );
361 if (wait->user) remove_timeout_user( wait->user );
362 free( wait );
363 thread->wait = NULL;
366 /* build the thread wait structure */
367 static int wait_on( int count, struct object *objects[], int flags, int sec, int usec )
369 struct thread_wait *wait;
370 struct wait_queue_entry *entry;
371 int i;
373 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
374 current->wait = wait;
375 wait->count = count;
376 wait->flags = flags;
377 wait->user = NULL;
378 if (flags & SELECT_TIMEOUT)
380 wait->timeout.tv_sec = sec;
381 wait->timeout.tv_usec = usec;
384 for (i = 0, entry = wait->queues; i < count; i++, entry++)
386 struct object *obj = objects[i];
387 entry->thread = current;
388 if (!obj->ops->add_queue( obj, entry ))
390 wait->count = i;
391 end_wait( current );
392 return 0;
395 return 1;
398 /* check if the thread waiting condition is satisfied */
399 static int check_wait( struct thread *thread )
401 int i, signaled;
402 struct thread_wait *wait = thread->wait;
403 struct wait_queue_entry *entry = wait->queues;
405 assert( wait );
406 if (wait->flags & SELECT_ALL)
408 int not_ok = 0;
409 /* Note: we must check them all anyway, as some objects may
410 * want to do something when signaled, even if others are not */
411 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
412 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
413 if (not_ok) goto other_checks;
414 /* Wait satisfied: tell it to all objects */
415 signaled = 0;
416 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
417 if (entry->obj->ops->satisfied( entry->obj, thread ))
418 signaled = STATUS_ABANDONED_WAIT_0;
419 return signaled;
421 else
423 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
425 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
426 /* Wait satisfied: tell it to the object */
427 signaled = i;
428 if (entry->obj->ops->satisfied( entry->obj, thread ))
429 signaled = i + STATUS_ABANDONED_WAIT_0;
430 return signaled;
434 other_checks:
435 if ((wait->flags & SELECT_INTERRUPTIBLE) && thread->system_apc.head) return STATUS_USER_APC;
436 if ((wait->flags & SELECT_ALERTABLE) && thread->user_apc.head) return STATUS_USER_APC;
437 if (wait->flags & SELECT_TIMEOUT)
439 struct timeval now;
440 gettimeofday( &now, NULL );
441 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
443 return -1;
446 /* attempt to wake up a thread */
447 /* return 1 if OK, 0 if the wait condition is still not satisfied */
448 static int wake_thread( struct thread *thread )
450 int signaled;
451 if ((signaled = check_wait( thread )) == -1) return 0;
453 if (debug_level) fprintf( stderr, "%08x: *wakeup* object=%d\n",
454 (unsigned int)thread, signaled );
455 end_wait( thread );
456 send_thread_wakeup( thread, signaled );
457 return 1;
460 /* thread wait timeout */
461 static void thread_timeout( void *ptr )
463 struct thread *thread = ptr;
465 if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread );
467 assert( thread->wait );
468 thread->wait->user = NULL;
469 end_wait( thread );
470 send_thread_wakeup( thread, STATUS_TIMEOUT );
473 /* select on a list of handles */
474 static void select_on( int count, handle_t *handles, int flags, int sec, int usec )
476 int ret, i;
477 struct object *objects[MAXIMUM_WAIT_OBJECTS];
479 assert( !current->wait );
481 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
483 set_error( STATUS_INVALID_PARAMETER );
484 return;
486 for (i = 0; i < count; i++)
488 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
489 break;
492 if (i < count) goto done;
493 if (!wait_on( count, objects, flags, sec, usec )) goto done;
495 if ((ret = check_wait( current )) != -1)
497 /* condition is already satisfied */
498 end_wait( current );
499 set_error( ret );
500 goto done;
503 /* now we need to wait */
504 if (flags & SELECT_TIMEOUT)
506 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
507 thread_timeout, current )))
509 end_wait( current );
510 goto done;
513 set_error( STATUS_PENDING );
515 done:
516 while (--i >= 0) release_object( objects[i] );
519 /* attempt to wake threads sleeping on the object wait queue */
520 void wake_up( struct object *obj, int max )
522 struct wait_queue_entry *entry = obj->head;
524 while (entry)
526 struct thread *thread = entry->thread;
527 entry = entry->next;
528 if (wake_thread( thread ))
530 if (max && !--max) break;
535 /* queue an async procedure call */
536 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
537 enum apc_type type, int system, int nb_args, ... )
539 struct thread_apc *apc;
540 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
542 /* cancel a possible previous APC with the same owner */
543 if (owner) thread_cancel_apc( thread, owner, system );
545 if (!(apc = mem_alloc( sizeof(*apc) + (nb_args-1)*sizeof(apc->args[0]) ))) return 0;
546 apc->prev = queue->tail;
547 apc->next = NULL;
548 apc->owner = owner;
549 apc->func = func;
550 apc->type = type;
551 apc->nb_args = nb_args;
552 if (nb_args)
554 int i;
555 va_list args;
556 va_start( args, nb_args );
557 for (i = 0; i < nb_args; i++) apc->args[i] = va_arg( args, void * );
558 va_end( args );
560 queue->tail = apc;
561 if (!apc->prev) /* first one */
563 queue->head = apc;
564 if (thread->wait) wake_thread( thread );
566 return 1;
569 /* cancel the async procedure call owned by a specific object */
570 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
572 struct thread_apc *apc;
573 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
574 for (apc = queue->head; apc; apc = apc->next)
576 if (apc->owner != owner) continue;
577 if (apc->next) apc->next->prev = apc->prev;
578 else queue->tail = apc->prev;
579 if (apc->prev) apc->prev->next = apc->next;
580 else queue->head = apc->next;
581 free( apc );
582 return;
586 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
587 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
589 struct thread_apc *apc;
590 struct apc_queue *queue = &thread->system_apc;
592 if (!queue->head && !system_only) queue = &thread->user_apc;
593 if ((apc = queue->head))
595 if (apc->next) apc->next->prev = NULL;
596 else queue->tail = NULL;
597 queue->head = apc->next;
599 return apc;
602 /* retrieve an LDT selector entry */
603 static void get_selector_entry( struct thread *thread, int entry,
604 unsigned int *base, unsigned int *limit,
605 unsigned char *flags )
607 if (!thread->process->ldt_copy)
609 set_error( STATUS_ACCESS_DENIED );
610 return;
612 if (entry >= 8192)
614 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
615 return;
617 if (suspend_for_ptrace( thread ))
619 unsigned char flags_buf[4];
620 int *addr = (int *)thread->process->ldt_copy + entry;
621 if (read_thread_int( thread, addr, base ) == -1) goto done;
622 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
623 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
624 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
625 *flags = flags_buf[entry & 3];
626 done:
627 resume_thread( thread );
631 /* kill a thread on the spot */
632 void kill_thread( struct thread *thread, int violent_death )
634 if (thread->state == TERMINATED) return; /* already killed */
635 thread->state = TERMINATED;
636 if (current == thread) current = NULL;
637 if (debug_level)
638 fprintf( stderr,"%08x: *killed* exit_code=%d\n",
639 (unsigned int)thread, thread->exit_code );
640 if (thread->wait)
642 end_wait( thread );
643 /* if it is waiting on the socket, we don't need to send a SIGTERM */
644 violent_death = 0;
646 debug_exit_thread( thread );
647 abandon_mutexes( thread );
648 remove_process_thread( thread->process, thread );
649 wake_up( &thread->obj, 0 );
650 detach_thread( thread, violent_death ? SIGTERM : 0 );
651 remove_select_user( &thread->obj );
652 release_object( thread->request_fd );
653 close( thread->reply_fd );
654 close( thread->wait_fd );
655 munmap( thread->buffer, MAX_REQUEST_LENGTH );
656 thread->request_fd = NULL;
657 thread->reply_fd = -1;
658 thread->wait_fd = -1;
659 thread->buffer = (void *)-1;
660 release_object( thread );
663 /* take a snapshot of currently running threads */
664 struct thread_snapshot *thread_snap( int *count )
666 struct thread_snapshot *snapshot, *ptr;
667 struct thread *thread;
668 int total = 0;
670 for (thread = first_thread; thread; thread = thread->next)
671 if (thread->state != TERMINATED) total++;
672 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
673 ptr = snapshot;
674 for (thread = first_thread; thread; thread = thread->next)
676 if (thread->state == TERMINATED) continue;
677 ptr->thread = thread;
678 ptr->count = thread->obj.refcount;
679 ptr->priority = thread->priority;
680 grab_object( thread );
681 ptr++;
683 *count = total;
684 return snapshot;
687 /* signal that we are finished booting on the client side */
688 DECL_HANDLER(boot_done)
690 debug_level = max( debug_level, req->debug_level );
691 /* Make sure last_req is initialized */
692 current->last_req = REQ_boot_done;
693 if (current == booting_thread)
695 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
696 lock_master_socket(0); /* allow other clients now */
700 /* create a new thread */
701 DECL_HANDLER(new_thread)
703 struct thread *thread;
704 int sock[2];
706 if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1)
708 if ((thread = create_thread( sock[0], current->process )))
710 if (req->suspend) thread->suspend++;
711 req->tid = thread;
712 if ((req->handle = alloc_handle( current->process, thread,
713 THREAD_ALL_ACCESS, req->inherit )))
715 send_client_fd( current, sock[1], req->handle );
716 close( sock[1] );
717 /* thread object will be released when the thread gets killed */
718 return;
720 kill_thread( thread, 1 );
722 close( sock[1] );
724 else file_set_error();
727 /* retrieve the thread buffer file descriptor */
728 DECL_HANDLER(get_thread_buffer)
730 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
733 /* initialize a new thread */
734 DECL_HANDLER(init_thread)
736 if (current->unix_pid)
738 fatal_protocol_error( current, "init_thread: already running\n" );
739 return;
741 current->unix_pid = req->unix_pid;
742 current->teb = req->teb;
743 if (current->suspend + current->process->suspend > 0) stop_thread( current );
744 if (current->process->running_threads > 1)
745 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
748 /* terminate a thread */
749 DECL_HANDLER(terminate_thread)
751 struct thread *thread;
753 req->self = 0;
754 req->last = 0;
755 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
757 thread->exit_code = req->exit_code;
758 if (thread != current) kill_thread( thread, 1 );
759 else
761 req->self = 1;
762 req->last = (thread->process->running_threads == 1);
764 release_object( thread );
768 /* fetch information about a thread */
769 DECL_HANDLER(get_thread_info)
771 struct thread *thread;
772 handle_t handle = req->handle;
774 if (!handle) thread = get_thread_from_id( req->tid_in );
775 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
777 if (thread)
779 req->tid = get_thread_id( thread );
780 req->teb = thread->teb;
781 req->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
782 req->priority = thread->priority;
783 release_object( thread );
787 /* set information about a thread */
788 DECL_HANDLER(set_thread_info)
790 struct thread *thread;
792 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
794 set_thread_info( thread, req );
795 release_object( thread );
799 /* suspend a thread */
800 DECL_HANDLER(suspend_thread)
802 struct thread *thread;
804 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
806 req->count = suspend_thread( thread, 1 );
807 release_object( thread );
811 /* resume a thread */
812 DECL_HANDLER(resume_thread)
814 struct thread *thread;
816 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
818 req->count = resume_thread( thread );
819 release_object( thread );
823 /* select on a handle list */
824 DECL_HANDLER(select)
826 int count = get_req_data_size(req) / sizeof(int);
827 select_on( count, get_req_data(req), req->flags, req->sec, req->usec );
830 /* queue an APC for a thread */
831 DECL_HANDLER(queue_apc)
833 struct thread *thread;
834 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
836 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user, 1, req->param );
837 release_object( thread );
841 /* get next APC to call */
842 DECL_HANDLER(get_apc)
844 struct thread_apc *apc;
845 size_t size;
847 for (;;)
849 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
851 /* no more APCs */
852 req->func = NULL;
853 req->type = APC_NONE;
854 set_req_data_size( req, 0 );
855 return;
857 /* Optimization: ignore APCs that have a NULL func; they are only used
858 * to wake up a thread, but since we got here the thread woke up already.
860 if (apc->func) break;
861 free( apc );
863 size = apc->nb_args * sizeof(apc->args[0]);
864 if (size > get_req_data_size(req)) size = get_req_data_size(req);
865 req->func = apc->func;
866 req->type = apc->type;
867 memcpy( get_req_data(req), apc->args, size );
868 set_req_data_size( req, size );
869 free( apc );
872 /* fetch a selector entry for a thread */
873 DECL_HANDLER(get_selector_entry)
875 struct thread *thread;
876 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
878 get_selector_entry( thread, req->entry, &req->base, &req->limit, &req->flags );
879 release_object( thread );