blob: 5529733b96461ac38c562af8c310c0497505c1b7 [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 Julliard642d3131998-07-12 19:29:36 +000012#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
Howard Abrams13277481999-07-10 13:16:29 +000015#ifdef HAVE_SYS_MMAN_H
Alexandre Julliard5bc78081999-06-22 17:26:53 +000016#include <sys/mman.h>
Howard Abrams13277481999-07-10 13:16:29 +000017#endif
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000018#include <sys/types.h>
Alexandre Julliard767e6f61998-08-09 12:47:43 +000019#include <sys/uio.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000020#include <unistd.h>
Michael Vekslerf935c591999-02-09 15:49:39 +000021#include <stdarg.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000022
Michael Vekslerf935c591999-02-09 15:49:39 +000023
24#include "winbase.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000025#include "winerror.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000026
27#include "handle.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000028#include "process.h"
29#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000030#include "request.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000031
32
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000033/* thread queues */
34
35struct wait_queue_entry
36{
37 struct wait_queue_entry *next;
38 struct wait_queue_entry *prev;
39 struct object *obj;
40 struct thread *thread;
41};
42
43struct thread_wait
44{
45 int count; /* count of objects */
46 int flags;
47 struct timeval timeout;
Alexandre Julliard57e11311999-05-16 16:59:38 +000048 struct timeout_user *user;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000049 struct wait_queue_entry queues[1];
50};
51
Alexandre Julliard62a8b431999-01-19 17:48:23 +000052/* asynchronous procedure calls */
53
54struct thread_apc
55{
56 void *func; /* function to call in client */
57 void *param; /* function param */
58};
59#define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */
60
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000061
Alexandre Julliard642d3131998-07-12 19:29:36 +000062/* thread operations */
63
Alexandre Julliard767e6f61998-08-09 12:47:43 +000064static void dump_thread( struct object *obj, int verbose );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000065static int thread_signaled( struct object *obj, struct thread *thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +000066static void destroy_thread( struct object *obj );
67
68static const struct object_ops thread_ops =
69{
Alexandre Julliard5bc78081999-06-22 17:26:53 +000070 sizeof(struct thread),
Alexandre Julliard767e6f61998-08-09 12:47:43 +000071 dump_thread,
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +000072 add_queue,
73 remove_queue,
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000074 thread_signaled,
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +000075 no_satisfied,
76 no_read_fd,
77 no_write_fd,
78 no_flush,
Alexandre Julliard05625391999-01-03 11:55:56 +000079 no_get_file_info,
Alexandre Julliard642d3131998-07-12 19:29:36 +000080 destroy_thread
81};
82
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +000083static struct thread *first_thread;
Alexandre Julliard767e6f61998-08-09 12:47:43 +000084
Alexandre Julliard5bc78081999-06-22 17:26:53 +000085/* allocate the buffer for the communication with the client */
86static int alloc_client_buffer( struct thread *thread )
Alexandre Julliard642d3131998-07-12 19:29:36 +000087{
Alexandre Julliard5bc78081999-06-22 17:26:53 +000088 int fd;
89
90 if ((fd = create_anonymous_file()) == -1) return -1;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000091 if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error;
92 if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE,
Alexandre Julliard5bc78081999-06-22 17:26:53 +000093 MAP_SHARED, fd, 0 )) == (void*)-1) goto error;
Alexandre Julliard5bc78081999-06-22 17:26:53 +000094 return fd;
95
96 error:
97 file_set_error();
98 if (fd != -1) close( fd );
99 return -1;
100}
101
102/* create a new thread */
103static struct thread *create_thread( int fd, struct process *process, int suspend )
104{
105 struct thread *thread;
106 int buf_fd;
107
108 if (!(thread = alloc_object( &thread_ops ))) return NULL;
109
Alexandre Julliard57e11311999-05-16 16:59:38 +0000110 thread->client = NULL;
111 thread->unix_pid = 0; /* not known yet */
112 thread->teb = NULL;
113 thread->mutex = NULL;
114 thread->debug_ctx = NULL;
115 thread->debug_first = NULL;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000116 thread->debug_event = NULL;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000117 thread->wait = NULL;
118 thread->apc = NULL;
119 thread->apc_count = 0;
120 thread->error = 0;
121 thread->state = STARTING;
122 thread->exit_code = 0x103; /* STILL_ACTIVE */
123 thread->next = NULL;
124 thread->prev = NULL;
125 thread->priority = THREAD_PRIORITY_NORMAL;
126 thread->affinity = 1;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000127 thread->suspend = (suspend != 0);
128 thread->buffer = (void *)-1;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000129 thread->last_req = REQ_GET_THREAD_BUFFER;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000130
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000131 if (!first_thread) /* creating the first thread */
Alexandre Julliardf692d441999-03-21 19:23:54 +0000132 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000133 current = thread;
134 thread->process = process = create_initial_process();
135 assert( process );
Alexandre Julliardf692d441999-03-21 19:23:54 +0000136 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000137 else thread->process = (struct process *)grab_object( process );
Alexandre Julliardf692d441999-03-21 19:23:54 +0000138
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000139 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000140 first_thread = thread;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000141 add_process_thread( process, thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000142
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000143 if ((buf_fd = alloc_client_buffer( thread )) == -1) goto error;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000144 if (!(thread->client = add_client( fd, thread )))
Alexandre Julliard642d3131998-07-12 19:29:36 +0000145 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000146 close( buf_fd );
Alexandre Julliardf692d441999-03-21 19:23:54 +0000147 goto error;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000148 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000149 set_reply_fd( thread, buf_fd ); /* send the fd to the client */
150 send_reply( thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000151 return thread;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000152
153 error:
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000154 remove_process_thread( process, thread );
155 release_object( thread );
156 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000157}
158
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000159/* create the initial thread and start the main server loop */
160void create_initial_thread( int fd )
161{
162 create_thread( fd, NULL, 0 );
163 select_loop();
164}
165
Alexandre Julliard642d3131998-07-12 19:29:36 +0000166/* destroy a thread when its refcount is 0 */
167static void destroy_thread( struct object *obj )
168{
169 struct thread *thread = (struct thread *)obj;
170 assert( obj->ops == &thread_ops );
171
Alexandre Julliarde712e071999-05-23 19:53:30 +0000172 assert( !thread->debug_ctx ); /* cannot still be debugging something */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000173 release_object( thread->process );
174 if (thread->next) thread->next->prev = thread->prev;
175 if (thread->prev) thread->prev->next = thread->next;
176 else first_thread = thread->next;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000177 if (thread->apc) free( thread->apc );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000178 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000179}
180
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000181/* dump a thread on stdout for debugging purposes */
182static void dump_thread( struct object *obj, int verbose )
183{
184 struct thread *thread = (struct thread *)obj;
185 assert( obj->ops == &thread_ops );
186
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000187 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
188 thread->unix_pid, thread->teb, thread->state );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000189}
190
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000191static int thread_signaled( struct object *obj, struct thread *thread )
192{
193 struct thread *mythread = (struct thread *)obj;
194 return (mythread->state == TERMINATED);
195}
196
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000197/* get a thread pointer from a thread id (and increment the refcount) */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000198struct thread *get_thread_from_id( void *id )
199{
200 struct thread *t = first_thread;
201 while (t && (t != id)) t = t->next;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000202 if (t) grab_object( t );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000203 return t;
204}
205
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000206/* get a thread from a handle (and increment the refcount) */
207struct thread *get_thread_from_handle( int handle, unsigned int access )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000208{
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000209 return (struct thread *)get_handle_obj( current->process, handle,
210 access, &thread_ops );
211}
212
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000213/* set all information about a thread */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000214static void set_thread_info( struct thread *thread,
215 struct set_thread_info_request *req )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000216{
217 if (req->mask & SET_THREAD_INFO_PRIORITY)
218 thread->priority = req->priority;
219 if (req->mask & SET_THREAD_INFO_AFFINITY)
220 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000221 if (req->affinity != 1) set_error( ERROR_INVALID_PARAMETER );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000222 else thread->affinity = req->affinity;
223 }
224}
225
226/* suspend a thread */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000227static int suspend_thread( struct thread *thread )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000228{
229 int old_count = thread->suspend;
230 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
231 {
Alexandre Julliard57e11311999-05-16 16:59:38 +0000232 if (!(thread->process->suspend + thread->suspend++))
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000233 {
234 if (thread->unix_pid) kill( thread->unix_pid, SIGSTOP );
235 }
236 }
237 return old_count;
238}
239
240/* resume a thread */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000241static int resume_thread( struct thread *thread )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000242{
243 int old_count = thread->suspend;
244 if (thread->suspend > 0)
245 {
Alexandre Julliard57e11311999-05-16 16:59:38 +0000246 if (!(--thread->suspend + thread->process->suspend))
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000247 {
248 if (thread->unix_pid) kill( thread->unix_pid, SIGCONT );
249 }
250 }
251 return old_count;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000252}
253
Ulrich Weigand371fd751999-04-11 17:13:03 +0000254/* suspend all threads but the current */
255void suspend_all_threads( void )
256{
257 struct thread *thread;
258 for ( thread = first_thread; thread; thread = thread->next )
259 if ( thread != current )
260 suspend_thread( thread );
261}
262
263/* resume all threads but the current */
264void resume_all_threads( void )
265{
266 struct thread *thread;
267 for ( thread = first_thread; thread; thread = thread->next )
268 if ( thread != current )
269 resume_thread( thread );
270}
271
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000272/* add a thread to an object wait queue; return 1 if OK, 0 on error */
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000273int add_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000274{
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000275 grab_object( obj );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000276 entry->obj = obj;
277 entry->prev = obj->tail;
278 entry->next = NULL;
279 if (obj->tail) obj->tail->next = entry;
280 else obj->head = entry;
281 obj->tail = entry;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000282 return 1;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000283}
284
285/* remove a thread from an object wait queue */
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000286void remove_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000287{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000288 if (entry->next) entry->next->prev = entry->prev;
289 else obj->tail = entry->prev;
290 if (entry->prev) entry->prev->next = entry->next;
291 else obj->head = entry->next;
292 release_object( obj );
293}
294
295/* finish waiting */
296static void end_wait( struct thread *thread )
297{
298 struct thread_wait *wait = thread->wait;
299 struct wait_queue_entry *entry;
300 int i;
301
302 assert( wait );
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000303 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
304 entry->obj->ops->remove_queue( entry->obj, entry );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000305 if (wait->user) remove_timeout_user( wait->user );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000306 free( wait );
307 thread->wait = NULL;
308}
309
310/* build the thread wait structure */
311static int wait_on( struct thread *thread, int count,
312 int *handles, int flags, int timeout )
313{
314 struct thread_wait *wait;
315 struct wait_queue_entry *entry;
316 struct object *obj;
317 int i;
318
319 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
320 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000321 set_error( ERROR_INVALID_PARAMETER );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000322 return 0;
323 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000324 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000325 thread->wait = wait;
326 wait->count = count;
327 wait->flags = flags;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000328 wait->user = NULL;
329 if (flags & SELECT_TIMEOUT) make_timeout( &wait->timeout, timeout );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000330
331 for (i = 0, entry = wait->queues; i < count; i++, entry++)
332 {
333 if (!(obj = get_handle_obj( thread->process, handles[i],
334 SYNCHRONIZE, NULL )))
335 {
336 wait->count = i - 1;
337 end_wait( thread );
338 return 0;
339 }
340 entry->thread = thread;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000341 if (!obj->ops->add_queue( obj, entry ))
342 {
343 wait->count = i - 1;
344 end_wait( thread );
345 return 0;
346 }
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000347 release_object( obj );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000348 }
349 return 1;
350}
351
352/* check if the thread waiting condition is satisfied */
353static int check_wait( struct thread *thread, int *signaled )
354{
355 int i;
356 struct thread_wait *wait = thread->wait;
357 struct wait_queue_entry *entry = wait->queues;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000358
359 assert( wait );
360 if (wait->flags & SELECT_ALL)
361 {
Alexandre Julliard57e11311999-05-16 16:59:38 +0000362 int not_ok = 0;
363 /* Note: we must check them all anyway, as some objects may
364 * want to do something when signaled, even if others are not */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000365 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
Alexandre Julliard57e11311999-05-16 16:59:38 +0000366 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
367 if (not_ok) goto other_checks;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000368 /* Wait satisfied: tell it to all objects */
369 *signaled = 0;
370 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
371 if (entry->obj->ops->satisfied( entry->obj, thread ))
372 *signaled = STATUS_ABANDONED_WAIT_0;
373 return 1;
374 }
375 else
376 {
377 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
378 {
379 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
380 /* Wait satisfied: tell it to the object */
381 *signaled = i;
382 if (entry->obj->ops->satisfied( entry->obj, thread ))
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000383 *signaled = i + STATUS_ABANDONED_WAIT_0;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000384 return 1;
385 }
386 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000387
388 other_checks:
389 if ((wait->flags & SELECT_ALERTABLE) && thread->apc)
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000390 {
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000391 *signaled = STATUS_USER_APC;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000392 return 1;
393 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000394 if (wait->flags & SELECT_TIMEOUT)
395 {
396 struct timeval now;
397 gettimeofday( &now, NULL );
398 if ((now.tv_sec > wait->timeout.tv_sec) ||
399 ((now.tv_sec == wait->timeout.tv_sec) &&
400 (now.tv_usec >= wait->timeout.tv_usec)))
401 {
402 *signaled = STATUS_TIMEOUT;
403 return 1;
404 }
405 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000406 return 0;
407}
408
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000409/* attempt to wake up a thread */
410/* return 1 if OK, 0 if the wait condition is still not satisfied */
411static int wake_thread( struct thread *thread )
412{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000413 struct select_request *req = get_req_ptr( thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000414
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000415 if (!check_wait( thread, &req->signaled )) return 0;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000416 end_wait( thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000417 return 1;
418}
419
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000420/* sleep on a list of objects */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000421static void sleep_on( struct thread *thread, int count, int *handles, int flags, int timeout )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000422{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000423 struct select_request *req;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000424 assert( !thread->wait );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000425 if (!wait_on( thread, count, handles, flags, timeout )) goto error;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000426 if (wake_thread( thread )) return;
427 /* now we need to wait */
428 if (flags & SELECT_TIMEOUT)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000429 {
Alexandre Julliard57e11311999-05-16 16:59:38 +0000430 if (!(thread->wait->user = add_timeout_user( &thread->wait->timeout,
431 call_timeout_handler, thread )))
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000432 goto error;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000433 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000434 thread->state = SLEEPING;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000435 return;
436
437 error:
438 req = get_req_ptr( thread );
439 req->signaled = -1;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000440}
441
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000442/* timeout for the current thread */
443void thread_timeout(void)
444{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000445 struct select_request *req = get_req_ptr( current );
446
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000447 assert( current->wait );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000448 current->wait->user = NULL;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000449 end_wait( current );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000450 req->signaled = STATUS_TIMEOUT;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000451 send_reply( current );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000452}
453
454/* attempt to wake threads sleeping on the object wait queue */
455void wake_up( struct object *obj, int max )
456{
457 struct wait_queue_entry *entry = obj->head;
458
459 while (entry)
460 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000461 struct thread *thread = entry->thread;
462 entry = entry->next;
463 if (wake_thread( thread ))
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000464 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000465 send_reply( thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000466 if (max && !--max) break;
467 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000468 }
469}
470
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000471/* queue an async procedure call */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000472static int thread_queue_apc( struct thread *thread, void *func, void *param )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000473{
474 struct thread_apc *apc;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000475 if (!thread->apc)
476 {
477 if (!(thread->apc = mem_alloc( MAX_THREAD_APC * sizeof(*apc) )))
478 return 0;
479 thread->apc_count = 0;
480 }
481 else if (thread->apc_count >= MAX_THREAD_APC) return 0;
482 thread->apc[thread->apc_count].func = func;
483 thread->apc[thread->apc_count].param = param;
484 thread->apc_count++;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000485 if (thread->wait)
486 {
487 if (wake_thread( thread )) send_reply( thread );
488 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000489 return 1;
490}
491
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000492/* kill a thread on the spot */
493void kill_thread( struct thread *thread, int exit_code )
494{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000495 if (thread->state == TERMINATED) return; /* already killed */
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000496 if (thread->unix_pid) kill( thread->unix_pid, SIGTERM );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000497 remove_client( thread->client, exit_code ); /* this will call thread_killed */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000498}
499
500/* a thread has been killed */
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000501void thread_killed( struct thread *thread, int exit_code )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000502{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000503 thread->state = TERMINATED;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000504 thread->exit_code = exit_code;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000505 if (thread->wait) end_wait( thread );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000506 debug_exit_thread( thread, exit_code );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000507 abandon_mutexes( thread );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000508 remove_process_thread( thread->process, thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000509 wake_up( &thread->obj, 0 );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000510 release_object( thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000511}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000512
513/* create a new thread */
514DECL_HANDLER(new_thread)
515{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000516 struct thread *thread;
517 struct process *process;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000518
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000519 if ((process = get_process_from_id( req->pid )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000520 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000521 if ((fd = dup(fd)) != -1)
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000522 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000523 if ((thread = create_thread( fd, process, req->suspend )))
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000524 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000525 req->tid = thread;
526 if ((req->handle = alloc_handle( current->process, thread,
527 THREAD_ALL_ACCESS, req->inherit )) == -1)
528 release_object( thread );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000529 /* else will be released when the thread gets killed */
530 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000531 else close( fd );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000532 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000533 else file_set_error();
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000534 release_object( process );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000535 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000536}
537
538/* retrieve the thread buffer file descriptor */
539DECL_HANDLER(get_thread_buffer)
540{
541 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000542}
543
544/* initialize a new thread */
545DECL_HANDLER(init_thread)
546{
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000547 if (current->state != STARTING)
548 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000549 fatal_protocol_error( current, "init_thread: already running\n" );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000550 return;
551 }
552 current->state = RUNNING;
553 current->unix_pid = req->unix_pid;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000554 current->teb = req->teb;
555 if (current->suspend + current->process->suspend > 0)
556 kill( current->unix_pid, SIGSTOP );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000557 req->pid = current->process;
558 req->tid = current;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000559}
560
561/* terminate a thread */
562DECL_HANDLER(terminate_thread)
563{
564 struct thread *thread;
565
566 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
567 {
568 kill_thread( thread, req->exit_code );
569 release_object( thread );
570 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000571}
572
573/* fetch information about a thread */
574DECL_HANDLER(get_thread_info)
575{
576 struct thread *thread;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000577
578 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
579 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000580 req->tid = thread;
581 req->exit_code = thread->exit_code;
582 req->priority = thread->priority;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000583 release_object( thread );
584 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000585}
586
587/* set information about a thread */
588DECL_HANDLER(set_thread_info)
589{
590 struct thread *thread;
591
592 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
593 {
594 set_thread_info( thread, req );
595 release_object( thread );
596 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000597}
598
599/* suspend a thread */
600DECL_HANDLER(suspend_thread)
601{
602 struct thread *thread;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000603
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000604 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
605 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000606 req->count = suspend_thread( thread );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000607 release_object( thread );
608 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000609}
610
611/* resume a thread */
612DECL_HANDLER(resume_thread)
613{
614 struct thread *thread;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000615
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000616 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
617 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000618 req->count = resume_thread( thread );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000619 release_object( thread );
620 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000621}
622
623/* select on a handle list */
624DECL_HANDLER(select)
625{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000626 sleep_on( current, req->count, req->handles, req->flags, req->timeout );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000627}
628
629/* queue an APC for a thread */
630DECL_HANDLER(queue_apc)
631{
632 struct thread *thread;
633 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
634 {
635 thread_queue_apc( thread, req->func, req->param );
636 release_object( thread );
637 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000638}
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000639
640/* get list of APC to call */
641DECL_HANDLER(get_apcs)
642{
643 if ((req->count = current->apc_count))
644 {
645 memcpy( req->apcs, current->apc, current->apc_count * sizeof(*current->apc) );
646 free( current->apc );
647 current->apc = NULL;
648 current->apc_count = 0;
649 }
650}