blob: d6e9bef4cdec1f87a6a79e2f32d811eccf7a6620 [file] [log] [blame]
Alexandre Julliard642d3131998-07-12 19:29:36 +00001/*
2 * Server-side thread management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
5 */
6
Howard Abrams13277481999-07-10 13:16:29 +00007#include "config.h"
8
Alexandre Julliard642d3131998-07-12 19:29:36 +00009#include <assert.h>
10#include <fcntl.h>
Alexandre Julliard767e6f61998-08-09 12:47:43 +000011#include <signal.h>
Alexandre Julliardea1afce2000-08-22 20:08:37 +000012#include <stdarg.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000013#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
Howard Abrams13277481999-07-10 13:16:29 +000016#ifdef HAVE_SYS_MMAN_H
Alexandre Julliard5bc78081999-06-22 17:26:53 +000017#include <sys/mman.h>
Howard Abrams13277481999-07-10 13:16:29 +000018#endif
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000019#include <sys/types.h>
Alexandre Julliard95e7acb2000-01-04 02:40:22 +000020#ifdef HAVE_SYS_SOCKET_H
21# include <sys/socket.h>
22#endif
Alexandre Julliard767e6f61998-08-09 12:47:43 +000023#include <sys/uio.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000024#include <unistd.h>
Michael Vekslerf935c591999-02-09 15:49:39 +000025#include <stdarg.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000026
Michael Vekslerf935c591999-02-09 15:49:39 +000027#include "winbase.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000028
29#include "handle.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000030#include "process.h"
31#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000032#include "request.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000033
34
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000035/* thread queues */
36
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000037struct thread_wait
38{
39 int count; /* count of objects */
40 int flags;
41 struct timeval timeout;
Alexandre Julliard57e11311999-05-16 16:59:38 +000042 struct timeout_user *user;
Alexandre Julliard9de03f42000-01-04 02:23:38 +000043 sleep_reply reply; /* function to build the reply */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000044 struct wait_queue_entry queues[1];
45};
46
Alexandre Julliard62a8b431999-01-19 17:48:23 +000047/* asynchronous procedure calls */
48
49struct thread_apc
50{
Alexandre Julliardea1afce2000-08-22 20:08:37 +000051 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 */
Alexandre Julliard62a8b431999-01-19 17:48:23 +000058};
Alexandre Julliard62a8b431999-01-19 17:48:23 +000059
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000060
Alexandre Julliard642d3131998-07-12 19:29:36 +000061/* thread operations */
62
Alexandre Julliard767e6f61998-08-09 12:47:43 +000063static void dump_thread( struct object *obj, int verbose );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000064static int thread_signaled( struct object *obj, struct thread *thread );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000065extern void thread_poll_event( struct object *obj, int event );
Alexandre Julliard642d3131998-07-12 19:29:36 +000066static void destroy_thread( struct object *obj );
Alexandre Julliardea1afce2000-08-22 20:08:37 +000067static struct thread_apc *thread_dequeue_apc( struct thread *thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +000068
69static const struct object_ops thread_ops =
70{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000071 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_read_fd, /* get_read_fd */
80 no_write_fd, /* get_write_fd */
81 no_flush, /* flush */
82 no_get_file_info, /* get_file_info */
83 destroy_thread /* destroy */
Alexandre Julliard642d3131998-07-12 19:29:36 +000084};
85
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +000086static struct thread *first_thread;
Alexandre Julliard2fe57772000-01-25 01:40:27 +000087static struct thread *booting_thread;
Alexandre Julliard767e6f61998-08-09 12:47:43 +000088
Alexandre Julliard5bc78081999-06-22 17:26:53 +000089/* allocate the buffer for the communication with the client */
90static int alloc_client_buffer( struct thread *thread )
Alexandre Julliard642d3131998-07-12 19:29:36 +000091{
Alexandre Julliardff81d782000-03-08 12:01:30 +000092 struct get_thread_buffer_request *req;
Alexandre Julliard5bc78081999-06-22 17:26:53 +000093 int fd;
94
95 if ((fd = create_anonymous_file()) == -1) return -1;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000096 if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error;
97 if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE,
Alexandre Julliard5bc78081999-06-22 17:26:53 +000098 MAP_SHARED, fd, 0 )) == (void*)-1) goto error;
Alexandre Julliard86113532000-08-29 03:54:30 +000099 thread->buffer_info = (struct server_buffer_info *)((char *)thread->buffer + MAX_REQUEST_LENGTH) - 1;
Alexandre Julliardff81d782000-03-08 12:01:30 +0000100 /* build the first request into the buffer and send it */
101 req = thread->buffer;
102 req->pid = get_process_id( thread->process );
103 req->tid = get_thread_id( thread );
104 req->boot = (thread == booting_thread);
Alexandre Julliard5fb54562000-03-08 22:01:02 +0000105 req->version = SERVER_PROTOCOL_VERSION;
Alexandre Julliardff81d782000-03-08 12:01:30 +0000106 set_reply_fd( thread, fd );
107 send_reply( thread );
108 return 1;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000109
110 error:
111 file_set_error();
112 if (fd != -1) close( fd );
Alexandre Julliardff81d782000-03-08 12:01:30 +0000113 return 0;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000114}
115
116/* create a new thread */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000117struct thread *create_thread( int fd, struct process *process )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000118{
119 struct thread *thread;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000120
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000121 int flags = fcntl( fd, F_GETFL, 0 );
122 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000123
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000124 if (!(thread = alloc_object( &thread_ops, fd ))) return NULL;
125
Alexandre Julliard57e11311999-05-16 16:59:38 +0000126 thread->unix_pid = 0; /* not known yet */
Alexandre Julliardea0d0282000-03-10 22:16:10 +0000127 thread->context = NULL;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000128 thread->teb = NULL;
129 thread->mutex = NULL;
130 thread->debug_ctx = NULL;
Alexandre Julliardf6507ed2000-04-06 22:05:16 +0000131 thread->debug_event = NULL;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000132 thread->queue = NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000133 thread->info = NULL;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000134 thread->wait = NULL;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000135 thread->apc_head = NULL;
136 thread->apc_tail = NULL;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000137 thread->error = 0;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000138 thread->pass_fd = -1;
Alexandre Julliard0a707831999-11-08 05:31:47 +0000139 thread->state = RUNNING;
140 thread->attached = 0;
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000141 thread->exit_code = 0;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000142 thread->next = NULL;
143 thread->prev = NULL;
144 thread->priority = THREAD_PRIORITY_NORMAL;
145 thread->affinity = 1;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000146 thread->suspend = 0;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000147 thread->buffer = (void *)-1;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000148 thread->last_req = REQ_GET_THREAD_BUFFER;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000149 thread->process = (struct process *)grab_object( process );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000150
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000151 if (!current) current = thread;
152
153 if (!booting_thread) /* first thread ever */
Alexandre Julliardf692d441999-03-21 19:23:54 +0000154 {
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000155 booting_thread = thread;
156 lock_master_socket(1);
Alexandre Julliardf692d441999-03-21 19:23:54 +0000157 }
Alexandre Julliardf692d441999-03-21 19:23:54 +0000158
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000159 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000160 first_thread = thread;
161
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000162 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
Alexandre Julliardff81d782000-03-08 12:01:30 +0000163 if (!alloc_client_buffer( thread )) goto error;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000164 return thread;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000165
166 error:
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000167 release_object( thread );
168 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000169}
170
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000171/* handle a client event */
172void thread_poll_event( struct object *obj, int event )
173{
174 struct thread *thread = (struct thread *)obj;
175 assert( obj->ops == &thread_ops );
176
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000177 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000178 else
179 {
180 if (event & POLLOUT) write_request( thread );
181 if (event & POLLIN) read_request( thread );
182 }
183}
184
Alexandre Julliard642d3131998-07-12 19:29:36 +0000185/* destroy a thread when its refcount is 0 */
186static void destroy_thread( struct object *obj )
187{
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000188 struct thread_apc *apc;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000189 struct thread *thread = (struct thread *)obj;
190 assert( obj->ops == &thread_ops );
191
Alexandre Julliarde712e071999-05-23 19:53:30 +0000192 assert( !thread->debug_ctx ); /* cannot still be debugging something */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000193 release_object( thread->process );
194 if (thread->next) thread->next->prev = thread->prev;
195 if (thread->prev) thread->prev->next = thread->next;
196 else first_thread = thread->next;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000197 while ((apc = thread_dequeue_apc( thread ))) free( apc );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000198 if (thread->info) release_object( thread->info );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000199 if (thread->queue) release_object( thread->queue );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000200 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000201 if (thread->pass_fd != -1) close( thread->pass_fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000202}
203
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000204/* dump a thread on stdout for debugging purposes */
205static void dump_thread( struct object *obj, int verbose )
206{
207 struct thread *thread = (struct thread *)obj;
208 assert( obj->ops == &thread_ops );
209
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000210 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
211 thread->unix_pid, thread->teb, thread->state );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000212}
213
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000214static int thread_signaled( struct object *obj, struct thread *thread )
215{
216 struct thread *mythread = (struct thread *)obj;
217 return (mythread->state == TERMINATED);
218}
219
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000220/* get a thread pointer from a thread id (and increment the refcount) */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000221struct thread *get_thread_from_id( void *id )
222{
223 struct thread *t = first_thread;
224 while (t && (t != id)) t = t->next;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000225 if (t) grab_object( t );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000226 return t;
227}
228
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000229/* get a thread from a handle (and increment the refcount) */
230struct thread *get_thread_from_handle( int handle, unsigned int access )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000231{
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000232 return (struct thread *)get_handle_obj( current->process, handle,
233 access, &thread_ops );
234}
235
Alexandre Julliard578c1001999-11-14 21:23:21 +0000236/* find a thread from a Unix pid */
237struct thread *get_thread_from_pid( int pid )
238{
239 struct thread *t = first_thread;
240 while (t && (t->unix_pid != pid)) t = t->next;
241 return t;
242}
243
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000244/* set all information about a thread */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000245static void set_thread_info( struct thread *thread,
246 struct set_thread_info_request *req )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000247{
248 if (req->mask & SET_THREAD_INFO_PRIORITY)
249 thread->priority = req->priority;
250 if (req->mask & SET_THREAD_INFO_AFFINITY)
251 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000252 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000253 else thread->affinity = req->affinity;
254 }
255}
256
257/* suspend a thread */
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000258int suspend_thread( struct thread *thread, int check_limit )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000259{
260 int old_count = thread->suspend;
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000261 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000262 {
Alexandre Julliard0a707831999-11-08 05:31:47 +0000263 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000264 }
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000265 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000266 return old_count;
267}
268
269/* resume a thread */
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000270int resume_thread( struct thread *thread )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000271{
272 int old_count = thread->suspend;
273 if (thread->suspend > 0)
274 {
Alexandre Julliard0a707831999-11-08 05:31:47 +0000275 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000276 }
277 return old_count;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000278}
279
Ulrich Weigand371fd751999-04-11 17:13:03 +0000280/* suspend all threads but the current */
281void suspend_all_threads( void )
282{
283 struct thread *thread;
284 for ( thread = first_thread; thread; thread = thread->next )
285 if ( thread != current )
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000286 suspend_thread( thread, 0 );
Ulrich Weigand371fd751999-04-11 17:13:03 +0000287}
288
289/* resume all threads but the current */
290void resume_all_threads( void )
291{
292 struct thread *thread;
293 for ( thread = first_thread; thread; thread = thread->next )
294 if ( thread != current )
295 resume_thread( thread );
296}
297
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000298/* add a thread to an object wait queue; return 1 if OK, 0 on error */
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000299int add_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000300{
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000301 grab_object( obj );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000302 entry->obj = obj;
303 entry->prev = obj->tail;
304 entry->next = NULL;
305 if (obj->tail) obj->tail->next = entry;
306 else obj->head = entry;
307 obj->tail = entry;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000308 return 1;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000309}
310
311/* remove a thread from an object wait queue */
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000312void remove_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000313{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000314 if (entry->next) entry->next->prev = entry->prev;
315 else obj->tail = entry->prev;
316 if (entry->prev) entry->prev->next = entry->next;
317 else obj->head = entry->next;
318 release_object( obj );
319}
320
321/* finish waiting */
322static void end_wait( struct thread *thread )
323{
324 struct thread_wait *wait = thread->wait;
325 struct wait_queue_entry *entry;
326 int i;
327
328 assert( wait );
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000329 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
330 entry->obj->ops->remove_queue( entry->obj, entry );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000331 if (wait->user) remove_timeout_user( wait->user );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000332 free( wait );
333 thread->wait = NULL;
334}
335
336/* build the thread wait structure */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000337static int wait_on( int count, struct object *objects[], int flags,
338 int timeout, sleep_reply func )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000339{
340 struct thread_wait *wait;
341 struct wait_queue_entry *entry;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000342 int i;
343
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000344 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000345 current->wait = wait;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000346 wait->count = count;
347 wait->flags = flags;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000348 wait->user = NULL;
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000349 wait->reply = func;
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000350 if (flags & SELECT_TIMEOUT)
351 {
352 gettimeofday( &wait->timeout, 0 );
353 add_timeout( &wait->timeout, timeout );
354 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000355
356 for (i = 0, entry = wait->queues; i < count; i++, entry++)
357 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000358 struct object *obj = objects[i];
359 entry->thread = current;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000360 if (!obj->ops->add_queue( obj, entry ))
361 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000362 wait->count = i;
363 end_wait( current );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000364 return 0;
365 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000366 }
367 return 1;
368}
369
370/* check if the thread waiting condition is satisfied */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000371static int check_wait( struct thread *thread, struct object **object )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000372{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000373 int i, signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000374 struct thread_wait *wait = thread->wait;
375 struct wait_queue_entry *entry = wait->queues;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000376
377 assert( wait );
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000378 *object = NULL;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000379 if (wait->flags & SELECT_ALL)
380 {
Alexandre Julliard57e11311999-05-16 16:59:38 +0000381 int not_ok = 0;
382 /* Note: we must check them all anyway, as some objects may
383 * want to do something when signaled, even if others are not */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000384 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
Alexandre Julliard57e11311999-05-16 16:59:38 +0000385 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
386 if (not_ok) goto other_checks;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000387 /* Wait satisfied: tell it to all objects */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000388 signaled = 0;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000389 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
390 if (entry->obj->ops->satisfied( entry->obj, thread ))
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000391 signaled = STATUS_ABANDONED_WAIT_0;
392 return signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000393 }
394 else
395 {
396 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
397 {
398 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
399 /* Wait satisfied: tell it to the object */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000400 signaled = i;
401 *object = entry->obj;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000402 if (entry->obj->ops->satisfied( entry->obj, thread ))
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000403 signaled = i + STATUS_ABANDONED_WAIT_0;
404 return signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000405 }
406 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000407
408 other_checks:
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000409 if ((wait->flags & SELECT_ALERTABLE) && thread->apc_head) return STATUS_USER_APC;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000410 if (wait->flags & SELECT_TIMEOUT)
411 {
412 struct timeval now;
413 gettimeofday( &now, NULL );
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000414 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000415 }
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000416 return -1;
417}
418
419/* build a reply to the select request */
420static void build_select_reply( struct thread *thread, struct object *obj, int signaled )
421{
422 struct select_request *req = get_req_ptr( thread );
423 req->signaled = signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000424}
425
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000426/* attempt to wake up a thread */
427/* return 1 if OK, 0 if the wait condition is still not satisfied */
428static int wake_thread( struct thread *thread )
429{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000430 int signaled;
431 struct object *object;
432 if ((signaled = check_wait( thread, &object )) == -1) return 0;
433 thread->error = 0;
434 thread->wait->reply( thread, object, signaled );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000435 end_wait( thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000436 return 1;
437}
438
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000439/* thread wait timeout */
440static void thread_timeout( void *ptr )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000441{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000442 struct thread *thread = ptr;
443 if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread );
444 assert( thread->wait );
445 thread->error = 0;
446 thread->wait->user = NULL;
447 thread->wait->reply( thread, NULL, STATUS_TIMEOUT );
448 end_wait( thread );
449 send_reply( thread );
450}
451
452/* sleep on a list of objects */
453int sleep_on( int count, struct object *objects[], int flags, int timeout, sleep_reply func )
454{
455 assert( !current->wait );
456 if (!wait_on( count, objects, flags, timeout, func )) return 0;
457 if (wake_thread( current )) return 1;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000458 /* now we need to wait */
459 if (flags & SELECT_TIMEOUT)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000460 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000461 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
462 thread_timeout, current )))
463 {
464 end_wait( current );
465 return 0;
466 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000467 }
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000468 return 1;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000469}
470
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000471/* select on a list of handles */
472static int select_on( int count, int *handles, int flags, int timeout )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000473{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000474 int ret = 0;
475 int i;
476 struct object *objects[MAXIMUM_WAIT_OBJECTS];
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000477
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000478 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
479 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000480 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000481 return 0;
482 }
483 for (i = 0; i < count; i++)
484 {
485 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
486 break;
487 }
488 if (i == count) ret = sleep_on( count, objects, flags, timeout, build_select_reply );
489 while (--i >= 0) release_object( objects[i] );
490 return ret;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000491}
492
493/* attempt to wake threads sleeping on the object wait queue */
494void wake_up( struct object *obj, int max )
495{
496 struct wait_queue_entry *entry = obj->head;
497
498 while (entry)
499 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000500 struct thread *thread = entry->thread;
501 entry = entry->next;
502 if (wake_thread( thread ))
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000503 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000504 send_reply( thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000505 if (max && !--max) break;
506 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000507 }
508}
509
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000510/* queue an async procedure call */
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000511int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
512 enum apc_type type, int nb_args, ... )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000513{
514 struct thread_apc *apc;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000515
516 /* cancel a possible previous APC with the same owner */
517 if (owner) thread_cancel_apc( thread, owner );
518
519 if (!(apc = mem_alloc( sizeof(*apc) + (nb_args-1)*sizeof(apc->args[0]) ))) return 0;
520 apc->prev = thread->apc_tail;
521 apc->next = NULL;
522 apc->owner = owner;
523 apc->func = func;
524 apc->type = type;
525 apc->nb_args = nb_args;
526 if (nb_args)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000527 {
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000528 int i;
529 va_list args;
530 va_start( args, nb_args );
531 for (i = 0; i < nb_args; i++) apc->args[i] = va_arg( args, void * );
532 va_end( args );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000533 }
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000534 thread->apc_tail = apc;
535 if (!apc->prev) /* first one */
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000536 {
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000537 thread->apc_head = apc;
538 if (thread->wait && wake_thread( thread )) send_reply( thread );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000539 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000540 return 1;
541}
542
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000543/* cancel the async procedure call owned by a specific object */
544void thread_cancel_apc( struct thread *thread, struct object *owner )
545{
546 struct thread_apc *apc;
547 for (apc = thread->apc_head; apc; apc = apc->next)
548 {
549 if (apc->owner != owner) continue;
550 if (apc->next) apc->next->prev = apc->prev;
551 else thread->apc_tail = apc->prev;
552 if (apc->prev) apc->prev->next = apc->next;
553 else thread->apc_head = apc->next;
554 free( apc );
555 return;
556 }
557}
558
559/* remove the head apc from the queue; the returned pointer must be freed by the caller */
560static struct thread_apc *thread_dequeue_apc( struct thread *thread )
561{
562 struct thread_apc *apc = thread->apc_head;
563 if (apc)
564 {
565 if (apc->next) apc->next->prev = NULL;
566 else thread->apc_tail = NULL;
567 thread->apc_head = apc->next;
568 }
569 return apc;
570}
571
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000572/* retrieve an LDT selector entry */
573static void get_selector_entry( struct thread *thread, int entry,
574 unsigned int *base, unsigned int *limit,
575 unsigned char *flags )
576{
577 if (!thread->process->ldt_copy || !thread->process->ldt_flags)
578 {
579 set_error( STATUS_ACCESS_DENIED );
580 return;
581 }
582 if (entry >= 8192)
583 {
584 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
585 return;
586 }
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000587 if (suspend_for_ptrace( thread ))
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000588 {
589 unsigned char flags_buf[4];
590 int *addr = (int *)thread->process->ldt_copy + 2 * entry;
591 if (read_thread_int( thread, addr, base ) == -1) goto done;
592 if (read_thread_int( thread, addr + 1, limit ) == -1) goto done;
593 addr = (int *)thread->process->ldt_flags + (entry >> 2);
594 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
595 *flags = flags_buf[entry & 3];
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000596 done:
597 resume_thread( thread );
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000598 }
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000599}
600
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000601/* kill a thread on the spot */
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000602void kill_thread( struct thread *thread, int violent_death )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000603{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000604 if (thread->state == TERMINATED) return; /* already killed */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000605 thread->state = TERMINATED;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000606 if (current == thread) current = NULL;
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000607 if (debug_level)
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000608 fprintf( stderr,"%08x: *killed* exit_code=%d\n",
609 (unsigned int)thread, thread->exit_code );
610 if (thread->wait)
611 {
612 end_wait( thread );
613 /* if it is waiting on the socket, we don't need to send a SIGTERM */
614 violent_death = 0;
615 }
Alexandre Julliardff81d782000-03-08 12:01:30 +0000616 debug_exit_thread( thread );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000617 abandon_mutexes( thread );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000618 remove_process_thread( thread->process, thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000619 wake_up( &thread->obj, 0 );
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000620 detach_thread( thread, violent_death ? SIGTERM : 0 );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000621 remove_select_user( &thread->obj );
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000622 munmap( thread->buffer, MAX_REQUEST_LENGTH );
623 thread->buffer = (void *)-1;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000624 release_object( thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000625}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000626
Alexandre Julliard07d84462000-04-16 19:45:05 +0000627/* take a snapshot of currently running threads */
628struct thread_snapshot *thread_snap( int *count )
629{
630 struct thread_snapshot *snapshot, *ptr;
631 struct thread *thread;
632 int total = 0;
633
634 for (thread = first_thread; thread; thread = thread->next)
635 if (thread->state != TERMINATED) total++;
636 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
637 ptr = snapshot;
638 for (thread = first_thread; thread; thread = thread->next)
639 {
640 if (thread->state == TERMINATED) continue;
641 ptr->thread = thread;
642 ptr->count = thread->obj.refcount;
643 ptr->priority = thread->priority;
644 grab_object( thread );
645 ptr++;
646 }
647 *count = total;
648 return snapshot;
649}
650
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000651/* signal that we are finished booting on the client side */
652DECL_HANDLER(boot_done)
653{
Alexandre Julliardf6507ed2000-04-06 22:05:16 +0000654 debug_level = max( debug_level, req->debug_level );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000655 /* Make sure last_req is initialized */
656 current->last_req = REQ_BOOT_DONE;
657 if (current == booting_thread)
658 {
659 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
660 lock_master_socket(0); /* allow other clients now */
661 }
662}
663
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000664/* create a new thread */
665DECL_HANDLER(new_thread)
666{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000667 struct thread *thread;
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000668 int sock[2];
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000669
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000670 if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000671 {
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000672 if ((thread = create_thread( sock[0], current->process )))
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000673 {
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000674 if (req->suspend) thread->suspend++;
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000675 req->tid = thread;
676 if ((req->handle = alloc_handle( current->process, thread,
677 THREAD_ALL_ACCESS, req->inherit )) != -1)
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000678 {
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000679 set_reply_fd( current, sock[1] );
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000680 /* thread object will be released when the thread gets killed */
Alexandre Julliard6a72dc52000-04-14 13:42:00 +0000681 add_process_thread( current->process, thread );
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000682 return;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000683 }
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000684 release_object( thread );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000685 }
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000686 close( sock[1] );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000687 }
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000688 else file_set_error();
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000689}
690
691/* retrieve the thread buffer file descriptor */
692DECL_HANDLER(get_thread_buffer)
693{
694 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000695}
696
697/* initialize a new thread */
698DECL_HANDLER(init_thread)
699{
Alexandre Julliard0a707831999-11-08 05:31:47 +0000700 if (current->unix_pid)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000701 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000702 fatal_protocol_error( current, "init_thread: already running\n" );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000703 return;
704 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000705 current->unix_pid = req->unix_pid;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000706 current->teb = req->teb;
Alexandre Julliard0a707831999-11-08 05:31:47 +0000707 if (current->suspend + current->process->suspend > 0) stop_thread( current );
Alexandre Julliardff81d782000-03-08 12:01:30 +0000708 if (current->process->running_threads > 1)
Alexandre Julliardb73421d2000-03-30 19:30:24 +0000709 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000710}
711
712/* terminate a thread */
713DECL_HANDLER(terminate_thread)
714{
715 struct thread *thread;
716
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000717 req->self = 0;
718 req->last = 0;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000719 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
720 {
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000721 thread->exit_code = req->exit_code;
722 if (thread != current) kill_thread( thread, 1 );
723 else
724 {
725 req->self = 1;
726 req->last = (thread->process->running_threads == 1);
727 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000728 release_object( thread );
729 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000730}
731
732/* fetch information about a thread */
733DECL_HANDLER(get_thread_info)
734{
735 struct thread *thread;
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000736 int handle = req->handle;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000737
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000738 if (handle == -1) thread = get_thread_from_id( req->tid_in );
739 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
740
741 if (thread)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000742 {
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000743 req->tid = get_thread_id( thread );
744 req->teb = thread->teb;
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000745 req->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000746 req->priority = thread->priority;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000747 release_object( thread );
748 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000749}
750
751/* set information about a thread */
752DECL_HANDLER(set_thread_info)
753{
754 struct thread *thread;
755
756 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
757 {
758 set_thread_info( thread, req );
759 release_object( thread );
760 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000761}
762
763/* suspend a thread */
764DECL_HANDLER(suspend_thread)
765{
766 struct thread *thread;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000767
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000768 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
769 {
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000770 req->count = suspend_thread( thread, 1 );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000771 release_object( thread );
772 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000773}
774
775/* resume a thread */
776DECL_HANDLER(resume_thread)
777{
778 struct thread *thread;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000779
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000780 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
781 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000782 req->count = resume_thread( thread );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000783 release_object( thread );
784 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000785}
786
787/* select on a handle list */
788DECL_HANDLER(select)
789{
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000790 int count = get_req_data_size(req) / sizeof(int);
791 if (!select_on( count, get_req_data(req), req->flags, req->timeout ))
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000792 req->signaled = -1;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000793}
794
795/* queue an APC for a thread */
796DECL_HANDLER(queue_apc)
797{
798 struct thread *thread;
799 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
800 {
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000801 thread_queue_apc( thread, NULL, req->func, APC_USER, 1, req->param );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000802 release_object( thread );
803 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000804}
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000805
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000806/* get next APC to call */
807DECL_HANDLER(get_apc)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000808{
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000809 struct thread_apc *apc;
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000810 size_t size;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000811
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000812 for (;;)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000813 {
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000814 if (!(apc = thread_dequeue_apc( current )))
815 {
816 /* no more APCs */
817 req->func = NULL;
818 req->type = APC_NONE;
819 set_req_data_size( req, 0 );
820 return;
821 }
822 /* Optimization: ignore APCs that have a NULL func; they are only used
823 * to wake up a thread, but since we got here the thread woke up already.
824 */
825 if (apc->func) break;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000826 free( apc );
827 }
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000828 size = apc->nb_args * sizeof(apc->args[0]);
829 if (size > get_req_data_size(req)) size = get_req_data_size(req);
830 req->func = apc->func;
831 req->type = apc->type;
832 memcpy( get_req_data(req), apc->args, size );
833 set_req_data_size( req, size );
834 free( apc );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000835}
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000836
837/* fetch a selector entry for a thread */
838DECL_HANDLER(get_selector_entry)
839{
840 struct thread *thread;
841 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
842 {
843 get_selector_entry( thread, req->entry, &req->base, &req->limit, &req->flags );
844 release_object( thread );
845 }
846}