Added one safety check to AFM parsing.
[wine.git] / server / thread.c
blob20250d103da5eb4d1b0238b5407298f87a8f551b
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 sleep_reply reply; /* function to build the reply */
44 struct wait_queue_entry queues[1];
47 /* asynchronous procedure calls */
49 struct thread_apc
51 struct thread_apc *next; /* queue linked list */
52 struct thread_apc *prev;
53 struct object *owner; /* object that queued this apc */
54 void *func; /* function to call in client */
55 enum apc_type type; /* type of apc function */
56 int nb_args; /* number of arguments */
57 void *args[1]; /* function arguments */
61 /* thread operations */
63 static void dump_thread( struct object *obj, int verbose );
64 static int thread_signaled( struct object *obj, struct thread *thread );
65 extern void thread_poll_event( struct object *obj, int event );
66 static void destroy_thread( struct object *obj );
67 static struct thread_apc *thread_dequeue_apc( struct thread *thread );
69 static const struct object_ops thread_ops =
71 sizeof(struct thread), /* size */
72 dump_thread, /* dump */
73 add_queue, /* add_queue */
74 remove_queue, /* remove_queue */
75 thread_signaled, /* signaled */
76 no_satisfied, /* satisfied */
77 NULL, /* get_poll_events */
78 thread_poll_event, /* poll_event */
79 no_get_fd, /* get_fd */
80 no_flush, /* flush */
81 no_get_file_info, /* get_file_info */
82 destroy_thread /* destroy */
85 static struct thread *first_thread;
86 static struct thread *booting_thread;
88 /* allocate the buffer for the communication with the client */
89 static int alloc_client_buffer( struct thread *thread )
91 struct get_thread_buffer_request *req;
92 int fd, fd_pipe[2];
94 if (pipe( fd_pipe ) == -1)
96 file_set_error();
97 return 0;
99 if ((fd = create_anonymous_file()) == -1) goto error;
100 if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error;
101 if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE,
102 MAP_SHARED, fd, 0 )) == (void*)-1) goto error;
103 thread->buffer_info = (struct server_buffer_info *)((char *)thread->buffer + MAX_REQUEST_LENGTH) - 1;
104 if (!(thread->request_fd = create_request_socket( thread ))) goto error;
105 thread->reply_fd = fd_pipe[1];
106 /* build the first request into the buffer and send it */
107 req = thread->buffer;
108 req->pid = get_process_id( thread->process );
109 req->tid = get_thread_id( thread );
110 req->boot = (thread == booting_thread);
111 req->version = SERVER_PROTOCOL_VERSION;
113 /* add it here since send_client_fd may call kill_thread */
114 add_process_thread( thread->process, thread );
116 send_client_fd( thread, fd_pipe[0], -1 );
117 send_client_fd( thread, fd, -1 );
118 send_reply( thread );
119 close( fd_pipe[0] );
120 close( fd );
121 return 1;
123 error:
124 file_set_error();
125 if (fd != -1) close( fd );
126 close( fd_pipe[0] );
127 close( fd_pipe[1] );
128 return 0;
131 /* create a new thread */
132 struct thread *create_thread( int fd, struct process *process )
134 struct thread *thread;
136 int flags = fcntl( fd, F_GETFL, 0 );
137 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
139 if (!(thread = alloc_object( &thread_ops, fd ))) return NULL;
141 thread->unix_pid = 0; /* not known yet */
142 thread->context = NULL;
143 thread->teb = NULL;
144 thread->mutex = NULL;
145 thread->debug_ctx = NULL;
146 thread->debug_event = NULL;
147 thread->queue = NULL;
148 thread->info = NULL;
149 thread->wait = NULL;
150 thread->apc_head = NULL;
151 thread->apc_tail = NULL;
152 thread->error = 0;
153 thread->pass_fd = -1;
154 thread->request_fd = NULL;
155 thread->reply_fd = -1;
156 thread->state = RUNNING;
157 thread->attached = 0;
158 thread->exit_code = 0;
159 thread->next = NULL;
160 thread->prev = NULL;
161 thread->priority = THREAD_PRIORITY_NORMAL;
162 thread->affinity = 1;
163 thread->suspend = 0;
164 thread->buffer = (void *)-1;
165 thread->last_req = REQ_GET_THREAD_BUFFER;
166 thread->process = (struct process *)grab_object( process );
168 if (!current) current = thread;
170 if (!booting_thread) /* first thread ever */
172 booting_thread = thread;
173 lock_master_socket(1);
176 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
177 first_thread = thread;
179 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
180 if (!alloc_client_buffer( thread )) goto error;
181 return thread;
183 error:
184 release_object( thread );
185 return NULL;
188 /* handle a client event */
189 void thread_poll_event( struct object *obj, int event )
191 struct thread *thread = (struct thread *)obj;
192 assert( obj->ops == &thread_ops );
194 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
195 else
197 if (event & POLLOUT) write_request( thread );
198 if (event & POLLIN) read_request( thread );
202 /* destroy a thread when its refcount is 0 */
203 static void destroy_thread( struct object *obj )
205 struct thread_apc *apc;
206 struct thread *thread = (struct thread *)obj;
207 assert( obj->ops == &thread_ops );
209 assert( !thread->debug_ctx ); /* cannot still be debugging something */
210 release_object( thread->process );
211 if (thread->next) thread->next->prev = thread->prev;
212 if (thread->prev) thread->prev->next = thread->next;
213 else first_thread = thread->next;
214 while ((apc = thread_dequeue_apc( thread ))) free( apc );
215 if (thread->info) release_object( thread->info );
216 if (thread->queue) release_object( thread->queue );
217 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
218 if (thread->reply_fd != -1) close( thread->reply_fd );
219 if (thread->pass_fd != -1) close( thread->pass_fd );
220 if (thread->request_fd) release_object( thread->request_fd );
223 /* dump a thread on stdout for debugging purposes */
224 static void dump_thread( struct object *obj, int verbose )
226 struct thread *thread = (struct thread *)obj;
227 assert( obj->ops == &thread_ops );
229 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
230 thread->unix_pid, thread->teb, thread->state );
233 static int thread_signaled( struct object *obj, struct thread *thread )
235 struct thread *mythread = (struct thread *)obj;
236 return (mythread->state == TERMINATED);
239 /* get a thread pointer from a thread id (and increment the refcount) */
240 struct thread *get_thread_from_id( void *id )
242 struct thread *t = first_thread;
243 while (t && (t != id)) t = t->next;
244 if (t) grab_object( t );
245 return t;
248 /* get a thread from a handle (and increment the refcount) */
249 struct thread *get_thread_from_handle( int handle, unsigned int access )
251 return (struct thread *)get_handle_obj( current->process, handle,
252 access, &thread_ops );
255 /* find a thread from a Unix pid */
256 struct thread *get_thread_from_pid( int pid )
258 struct thread *t = first_thread;
259 while (t && (t->unix_pid != pid)) t = t->next;
260 return t;
263 /* set all information about a thread */
264 static void set_thread_info( struct thread *thread,
265 struct set_thread_info_request *req )
267 if (req->mask & SET_THREAD_INFO_PRIORITY)
268 thread->priority = req->priority;
269 if (req->mask & SET_THREAD_INFO_AFFINITY)
271 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
272 else thread->affinity = req->affinity;
276 /* suspend a thread */
277 int suspend_thread( struct thread *thread, int check_limit )
279 int old_count = thread->suspend;
280 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
282 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
284 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
285 return old_count;
288 /* resume a thread */
289 int resume_thread( struct thread *thread )
291 int old_count = thread->suspend;
292 if (thread->suspend > 0)
294 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
296 return old_count;
299 /* suspend all threads but the current */
300 void suspend_all_threads( void )
302 struct thread *thread;
303 for ( thread = first_thread; thread; thread = thread->next )
304 if ( thread != current )
305 suspend_thread( thread, 0 );
308 /* resume all threads but the current */
309 void resume_all_threads( void )
311 struct thread *thread;
312 for ( thread = first_thread; thread; thread = thread->next )
313 if ( thread != current )
314 resume_thread( thread );
317 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
318 int add_queue( struct object *obj, struct wait_queue_entry *entry )
320 grab_object( obj );
321 entry->obj = obj;
322 entry->prev = obj->tail;
323 entry->next = NULL;
324 if (obj->tail) obj->tail->next = entry;
325 else obj->head = entry;
326 obj->tail = entry;
327 return 1;
330 /* remove a thread from an object wait queue */
331 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
333 if (entry->next) entry->next->prev = entry->prev;
334 else obj->tail = entry->prev;
335 if (entry->prev) entry->prev->next = entry->next;
336 else obj->head = entry->next;
337 release_object( obj );
340 /* finish waiting */
341 static void end_wait( struct thread *thread )
343 struct thread_wait *wait = thread->wait;
344 struct wait_queue_entry *entry;
345 int i;
347 assert( wait );
348 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
349 entry->obj->ops->remove_queue( entry->obj, entry );
350 if (wait->user) remove_timeout_user( wait->user );
351 free( wait );
352 thread->wait = NULL;
355 /* build the thread wait structure */
356 static int wait_on( int count, struct object *objects[], int flags,
357 int timeout, sleep_reply func )
359 struct thread_wait *wait;
360 struct wait_queue_entry *entry;
361 int i;
363 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
364 current->wait = wait;
365 wait->count = count;
366 wait->flags = flags;
367 wait->user = NULL;
368 wait->reply = func;
369 if (flags & SELECT_TIMEOUT)
371 gettimeofday( &wait->timeout, 0 );
372 add_timeout( &wait->timeout, timeout );
375 for (i = 0, entry = wait->queues; i < count; i++, entry++)
377 struct object *obj = objects[i];
378 entry->thread = current;
379 if (!obj->ops->add_queue( obj, entry ))
381 wait->count = i;
382 end_wait( current );
383 return 0;
386 return 1;
389 /* check if the thread waiting condition is satisfied */
390 static int check_wait( struct thread *thread, struct object **object )
392 int i, signaled;
393 struct thread_wait *wait = thread->wait;
394 struct wait_queue_entry *entry = wait->queues;
396 assert( wait );
397 *object = NULL;
398 if (wait->flags & SELECT_ALL)
400 int not_ok = 0;
401 /* Note: we must check them all anyway, as some objects may
402 * want to do something when signaled, even if others are not */
403 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
404 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
405 if (not_ok) goto other_checks;
406 /* Wait satisfied: tell it to all objects */
407 signaled = 0;
408 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
409 if (entry->obj->ops->satisfied( entry->obj, thread ))
410 signaled = STATUS_ABANDONED_WAIT_0;
411 return signaled;
413 else
415 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
417 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
418 /* Wait satisfied: tell it to the object */
419 signaled = i;
420 *object = entry->obj;
421 if (entry->obj->ops->satisfied( entry->obj, thread ))
422 signaled = i + STATUS_ABANDONED_WAIT_0;
423 return signaled;
427 other_checks:
428 if ((wait->flags & SELECT_ALERTABLE) && thread->apc_head) return STATUS_USER_APC;
429 if (wait->flags & SELECT_TIMEOUT)
431 struct timeval now;
432 gettimeofday( &now, NULL );
433 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
435 return -1;
438 /* build a reply to the select request */
439 static void build_select_reply( struct thread *thread, struct object *obj, int signaled )
441 struct select_request *req = get_req_ptr( thread );
442 req->signaled = signaled;
445 /* attempt to wake up a thread */
446 /* return 1 if OK, 0 if the wait condition is still not satisfied */
447 static int wake_thread( struct thread *thread )
449 int signaled;
450 struct object *object;
451 if ((signaled = check_wait( thread, &object )) == -1) return 0;
452 thread->error = 0;
453 thread->wait->reply( thread, object, signaled );
454 end_wait( thread );
455 return 1;
458 /* thread wait timeout */
459 static void thread_timeout( void *ptr )
461 struct thread *thread = ptr;
462 if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread );
463 assert( thread->wait );
464 thread->error = 0;
465 thread->wait->user = NULL;
466 thread->wait->reply( thread, NULL, STATUS_TIMEOUT );
467 end_wait( thread );
468 send_reply( thread );
471 /* sleep on a list of objects */
472 int sleep_on( int count, struct object *objects[], int flags, int timeout, sleep_reply func )
474 assert( !current->wait );
475 if (!wait_on( count, objects, flags, timeout, func )) return 0;
476 if (wake_thread( current )) return 1;
477 /* now we need to wait */
478 if (flags & SELECT_TIMEOUT)
480 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
481 thread_timeout, current )))
483 end_wait( current );
484 return 0;
487 return 1;
490 /* select on a list of handles */
491 static int select_on( int count, int *handles, int flags, int timeout )
493 int ret = 0;
494 int i;
495 struct object *objects[MAXIMUM_WAIT_OBJECTS];
497 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
499 set_error( STATUS_INVALID_PARAMETER );
500 return 0;
502 for (i = 0; i < count; i++)
504 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
505 break;
507 if (i == count) ret = sleep_on( count, objects, flags, timeout, build_select_reply );
508 while (--i >= 0) release_object( objects[i] );
509 return ret;
512 /* attempt to wake threads sleeping on the object wait queue */
513 void wake_up( struct object *obj, int max )
515 struct wait_queue_entry *entry = obj->head;
517 while (entry)
519 struct thread *thread = entry->thread;
520 entry = entry->next;
521 if (wake_thread( thread ))
523 send_reply( thread );
524 if (max && !--max) break;
529 /* queue an async procedure call */
530 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
531 enum apc_type type, int nb_args, ... )
533 struct thread_apc *apc;
535 /* cancel a possible previous APC with the same owner */
536 if (owner) thread_cancel_apc( thread, owner );
538 if (!(apc = mem_alloc( sizeof(*apc) + (nb_args-1)*sizeof(apc->args[0]) ))) return 0;
539 apc->prev = thread->apc_tail;
540 apc->next = NULL;
541 apc->owner = owner;
542 apc->func = func;
543 apc->type = type;
544 apc->nb_args = nb_args;
545 if (nb_args)
547 int i;
548 va_list args;
549 va_start( args, nb_args );
550 for (i = 0; i < nb_args; i++) apc->args[i] = va_arg( args, void * );
551 va_end( args );
553 thread->apc_tail = apc;
554 if (!apc->prev) /* first one */
556 thread->apc_head = apc;
557 if (thread->wait && wake_thread( thread )) send_reply( thread );
559 return 1;
562 /* cancel the async procedure call owned by a specific object */
563 void thread_cancel_apc( struct thread *thread, struct object *owner )
565 struct thread_apc *apc;
566 for (apc = thread->apc_head; apc; apc = apc->next)
568 if (apc->owner != owner) continue;
569 if (apc->next) apc->next->prev = apc->prev;
570 else thread->apc_tail = apc->prev;
571 if (apc->prev) apc->prev->next = apc->next;
572 else thread->apc_head = apc->next;
573 free( apc );
574 return;
578 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
579 static struct thread_apc *thread_dequeue_apc( struct thread *thread )
581 struct thread_apc *apc = thread->apc_head;
582 if (apc)
584 if (apc->next) apc->next->prev = NULL;
585 else thread->apc_tail = NULL;
586 thread->apc_head = apc->next;
588 return apc;
591 /* retrieve an LDT selector entry */
592 static void get_selector_entry( struct thread *thread, int entry,
593 unsigned int *base, unsigned int *limit,
594 unsigned char *flags )
596 if (!thread->process->ldt_copy)
598 set_error( STATUS_ACCESS_DENIED );
599 return;
601 if (entry >= 8192)
603 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
604 return;
606 if (suspend_for_ptrace( thread ))
608 unsigned char flags_buf[4];
609 int *addr = (int *)thread->process->ldt_copy + entry;
610 if (read_thread_int( thread, addr, base ) == -1) goto done;
611 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
612 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
613 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
614 *flags = flags_buf[entry & 3];
615 done:
616 resume_thread( thread );
620 /* kill a thread on the spot */
621 void kill_thread( struct thread *thread, int violent_death )
623 if (thread->state == TERMINATED) return; /* already killed */
624 thread->state = TERMINATED;
625 if (current == thread) current = NULL;
626 if (debug_level)
627 fprintf( stderr,"%08x: *killed* exit_code=%d\n",
628 (unsigned int)thread, thread->exit_code );
629 if (thread->wait)
631 end_wait( thread );
632 /* if it is waiting on the socket, we don't need to send a SIGTERM */
633 violent_death = 0;
635 debug_exit_thread( thread );
636 abandon_mutexes( thread );
637 remove_process_thread( thread->process, thread );
638 wake_up( &thread->obj, 0 );
639 detach_thread( thread, violent_death ? SIGTERM : 0 );
640 remove_select_user( &thread->obj );
641 release_object( thread->request_fd );
642 close( thread->reply_fd );
643 munmap( thread->buffer, MAX_REQUEST_LENGTH );
644 thread->request_fd = NULL;
645 thread->reply_fd = -1;
646 thread->buffer = (void *)-1;
647 release_object( thread );
650 /* take a snapshot of currently running threads */
651 struct thread_snapshot *thread_snap( int *count )
653 struct thread_snapshot *snapshot, *ptr;
654 struct thread *thread;
655 int total = 0;
657 for (thread = first_thread; thread; thread = thread->next)
658 if (thread->state != TERMINATED) total++;
659 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
660 ptr = snapshot;
661 for (thread = first_thread; thread; thread = thread->next)
663 if (thread->state == TERMINATED) continue;
664 ptr->thread = thread;
665 ptr->count = thread->obj.refcount;
666 ptr->priority = thread->priority;
667 grab_object( thread );
668 ptr++;
670 *count = total;
671 return snapshot;
674 /* signal that we are finished booting on the client side */
675 DECL_HANDLER(boot_done)
677 debug_level = max( debug_level, req->debug_level );
678 /* Make sure last_req is initialized */
679 current->last_req = REQ_BOOT_DONE;
680 if (current == booting_thread)
682 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
683 lock_master_socket(0); /* allow other clients now */
687 /* create a new thread */
688 DECL_HANDLER(new_thread)
690 struct thread *thread;
691 int sock[2];
693 if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1)
695 if ((thread = create_thread( sock[0], current->process )))
697 if (req->suspend) thread->suspend++;
698 req->tid = thread;
699 if ((req->handle = alloc_handle( current->process, thread,
700 THREAD_ALL_ACCESS, req->inherit )) != -1)
702 send_client_fd( current, sock[1], req->handle );
703 close( sock[1] );
704 /* thread object will be released when the thread gets killed */
705 return;
707 kill_thread( thread, 1 );
709 close( sock[1] );
711 else file_set_error();
714 /* retrieve the thread buffer file descriptor */
715 DECL_HANDLER(get_thread_buffer)
717 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
720 /* initialize a new thread */
721 DECL_HANDLER(init_thread)
723 if (current->unix_pid)
725 fatal_protocol_error( current, "init_thread: already running\n" );
726 return;
728 current->unix_pid = req->unix_pid;
729 current->teb = req->teb;
730 if (current->suspend + current->process->suspend > 0) stop_thread( current );
731 if (current->process->running_threads > 1)
732 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
735 /* terminate a thread */
736 DECL_HANDLER(terminate_thread)
738 struct thread *thread;
740 req->self = 0;
741 req->last = 0;
742 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
744 thread->exit_code = req->exit_code;
745 if (thread != current) kill_thread( thread, 1 );
746 else
748 req->self = 1;
749 req->last = (thread->process->running_threads == 1);
751 release_object( thread );
755 /* fetch information about a thread */
756 DECL_HANDLER(get_thread_info)
758 struct thread *thread;
759 int handle = req->handle;
761 if (handle == -1) thread = get_thread_from_id( req->tid_in );
762 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
764 if (thread)
766 req->tid = get_thread_id( thread );
767 req->teb = thread->teb;
768 req->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
769 req->priority = thread->priority;
770 release_object( thread );
774 /* set information about a thread */
775 DECL_HANDLER(set_thread_info)
777 struct thread *thread;
779 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
781 set_thread_info( thread, req );
782 release_object( thread );
786 /* suspend a thread */
787 DECL_HANDLER(suspend_thread)
789 struct thread *thread;
791 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
793 req->count = suspend_thread( thread, 1 );
794 release_object( thread );
798 /* resume a thread */
799 DECL_HANDLER(resume_thread)
801 struct thread *thread;
803 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
805 req->count = resume_thread( thread );
806 release_object( thread );
810 /* select on a handle list */
811 DECL_HANDLER(select)
813 int count = get_req_data_size(req) / sizeof(int);
814 if (!select_on( count, get_req_data(req), req->flags, req->timeout ))
815 req->signaled = -1;
818 /* queue an APC for a thread */
819 DECL_HANDLER(queue_apc)
821 struct thread *thread;
822 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
824 thread_queue_apc( thread, NULL, req->func, APC_USER, 1, req->param );
825 release_object( thread );
829 /* get next APC to call */
830 DECL_HANDLER(get_apc)
832 struct thread_apc *apc;
833 size_t size;
835 for (;;)
837 if (!(apc = thread_dequeue_apc( current )))
839 /* no more APCs */
840 req->func = NULL;
841 req->type = APC_NONE;
842 set_req_data_size( req, 0 );
843 return;
845 /* Optimization: ignore APCs that have a NULL func; they are only used
846 * to wake up a thread, but since we got here the thread woke up already.
848 if (apc->func) break;
849 free( apc );
851 size = apc->nb_args * sizeof(apc->args[0]);
852 if (size > get_req_data_size(req)) size = get_req_data_size(req);
853 req->func = apc->func;
854 req->type = apc->type;
855 memcpy( get_req_data(req), apc->args, size );
856 set_req_data_size( req, size );
857 free( apc );
860 /* fetch a selector entry for a thread */
861 DECL_HANDLER(get_selector_entry)
863 struct thread *thread;
864 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
866 get_selector_entry( thread, req->entry, &req->base, &req->limit, &req->flags );
867 release_object( thread );