blob: 18991324f4c54e148d3bb30109906c6d23cb8a97 [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 Julliard95e7acb2000-01-04 02:40:22 +000019#ifdef HAVE_SYS_SOCKET_H
20# include <sys/socket.h>
21#endif
Alexandre Julliard767e6f61998-08-09 12:47:43 +000022#include <sys/uio.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000023#include <unistd.h>
Michael Vekslerf935c591999-02-09 15:49:39 +000024#include <stdarg.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000025
Michael Vekslerf935c591999-02-09 15:49:39 +000026
27#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{
51 void *func; /* function to call in client */
52 void *param; /* function param */
53};
54#define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */
55
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000056
Alexandre Julliard642d3131998-07-12 19:29:36 +000057/* thread operations */
58
Alexandre Julliard767e6f61998-08-09 12:47:43 +000059static void dump_thread( struct object *obj, int verbose );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000060static int thread_signaled( struct object *obj, struct thread *thread );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000061extern void thread_poll_event( struct object *obj, int event );
Alexandre Julliard642d3131998-07-12 19:29:36 +000062static void destroy_thread( struct object *obj );
63
64static const struct object_ops thread_ops =
65{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000066 sizeof(struct thread), /* size */
67 dump_thread, /* dump */
68 add_queue, /* add_queue */
69 remove_queue, /* remove_queue */
70 thread_signaled, /* signaled */
71 no_satisfied, /* satisfied */
72 NULL, /* get_poll_events */
73 thread_poll_event, /* poll_event */
74 no_read_fd, /* get_read_fd */
75 no_write_fd, /* get_write_fd */
76 no_flush, /* flush */
77 no_get_file_info, /* get_file_info */
78 destroy_thread /* destroy */
Alexandre Julliard642d3131998-07-12 19:29:36 +000079};
80
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +000081static struct thread *first_thread;
Alexandre Julliard2fe57772000-01-25 01:40:27 +000082static struct thread *booting_thread;
Alexandre Julliard767e6f61998-08-09 12:47:43 +000083
Alexandre Julliard5bc78081999-06-22 17:26:53 +000084/* allocate the buffer for the communication with the client */
85static int alloc_client_buffer( struct thread *thread )
Alexandre Julliard642d3131998-07-12 19:29:36 +000086{
Alexandre Julliardff81d782000-03-08 12:01:30 +000087 struct get_thread_buffer_request *req;
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 Julliardff81d782000-03-08 12:01:30 +000094 /* build the first request into the buffer and send it */
95 req = thread->buffer;
96 req->pid = get_process_id( thread->process );
97 req->tid = get_thread_id( thread );
98 req->boot = (thread == booting_thread);
Alexandre Julliard5fb54562000-03-08 22:01:02 +000099 req->version = SERVER_PROTOCOL_VERSION;
Alexandre Julliardff81d782000-03-08 12:01:30 +0000100 set_reply_fd( thread, fd );
101 send_reply( thread );
102 return 1;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000103
104 error:
105 file_set_error();
106 if (fd != -1) close( fd );
Alexandre Julliardff81d782000-03-08 12:01:30 +0000107 return 0;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000108}
109
110/* create a new thread */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000111struct thread *create_thread( int fd, struct process *process )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000112{
113 struct thread *thread;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000114
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000115 int flags = fcntl( fd, F_GETFL, 0 );
116 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000117
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000118 if (!(thread = alloc_object( &thread_ops, fd ))) return NULL;
119
Alexandre Julliard57e11311999-05-16 16:59:38 +0000120 thread->unix_pid = 0; /* not known yet */
Alexandre Julliardea0d0282000-03-10 22:16:10 +0000121 thread->context = NULL;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000122 thread->teb = NULL;
123 thread->mutex = NULL;
124 thread->debug_ctx = NULL;
Alexandre Julliardf6507ed2000-04-06 22:05:16 +0000125 thread->debug_event = NULL;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000126 thread->queue = NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000127 thread->info = NULL;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000128 thread->wait = NULL;
129 thread->apc = NULL;
130 thread->apc_count = 0;
131 thread->error = 0;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000132 thread->pass_fd = -1;
Alexandre Julliard0a707831999-11-08 05:31:47 +0000133 thread->state = RUNNING;
134 thread->attached = 0;
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000135 thread->exit_code = 0;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000136 thread->next = NULL;
137 thread->prev = NULL;
138 thread->priority = THREAD_PRIORITY_NORMAL;
139 thread->affinity = 1;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000140 thread->suspend = 0;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000141 thread->buffer = (void *)-1;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000142 thread->last_req = REQ_GET_THREAD_BUFFER;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000143 thread->process = (struct process *)grab_object( process );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000144
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000145 if (!current) current = thread;
146
147 if (!booting_thread) /* first thread ever */
Alexandre Julliardf692d441999-03-21 19:23:54 +0000148 {
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000149 booting_thread = thread;
150 lock_master_socket(1);
Alexandre Julliardf692d441999-03-21 19:23:54 +0000151 }
Alexandre Julliardf692d441999-03-21 19:23:54 +0000152
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000153 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000154 first_thread = thread;
155
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000156 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
Alexandre Julliardff81d782000-03-08 12:01:30 +0000157 if (!alloc_client_buffer( thread )) goto error;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000158 return thread;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000159
160 error:
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000161 release_object( thread );
162 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000163}
164
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000165/* handle a client event */
166void thread_poll_event( struct object *obj, int event )
167{
168 struct thread *thread = (struct thread *)obj;
169 assert( obj->ops == &thread_ops );
170
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000171 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000172 else
173 {
174 if (event & POLLOUT) write_request( thread );
175 if (event & POLLIN) read_request( thread );
176 }
177}
178
Alexandre Julliard642d3131998-07-12 19:29:36 +0000179/* destroy a thread when its refcount is 0 */
180static void destroy_thread( struct object *obj )
181{
182 struct thread *thread = (struct thread *)obj;
183 assert( obj->ops == &thread_ops );
184
Alexandre Julliarde712e071999-05-23 19:53:30 +0000185 assert( !thread->debug_ctx ); /* cannot still be debugging something */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000186 release_object( thread->process );
187 if (thread->next) thread->next->prev = thread->prev;
188 if (thread->prev) thread->prev->next = thread->next;
189 else first_thread = thread->next;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000190 if (thread->apc) free( thread->apc );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000191 if (thread->info) release_object( thread->info );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000192 if (thread->queue) release_object( thread->queue );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000193 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000194 if (thread->pass_fd != -1) close( thread->pass_fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000195}
196
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000197/* dump a thread on stdout for debugging purposes */
198static void dump_thread( struct object *obj, int verbose )
199{
200 struct thread *thread = (struct thread *)obj;
201 assert( obj->ops == &thread_ops );
202
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000203 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
204 thread->unix_pid, thread->teb, thread->state );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000205}
206
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000207static int thread_signaled( struct object *obj, struct thread *thread )
208{
209 struct thread *mythread = (struct thread *)obj;
210 return (mythread->state == TERMINATED);
211}
212
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000213/* get a thread pointer from a thread id (and increment the refcount) */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000214struct thread *get_thread_from_id( void *id )
215{
216 struct thread *t = first_thread;
217 while (t && (t != id)) t = t->next;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000218 if (t) grab_object( t );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000219 return t;
220}
221
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000222/* get a thread from a handle (and increment the refcount) */
223struct thread *get_thread_from_handle( int handle, unsigned int access )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000224{
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000225 return (struct thread *)get_handle_obj( current->process, handle,
226 access, &thread_ops );
227}
228
Alexandre Julliard578c1001999-11-14 21:23:21 +0000229/* find a thread from a Unix pid */
230struct thread *get_thread_from_pid( int pid )
231{
232 struct thread *t = first_thread;
233 while (t && (t->unix_pid != pid)) t = t->next;
234 return t;
235}
236
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000237/* set all information about a thread */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000238static void set_thread_info( struct thread *thread,
239 struct set_thread_info_request *req )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000240{
241 if (req->mask & SET_THREAD_INFO_PRIORITY)
242 thread->priority = req->priority;
243 if (req->mask & SET_THREAD_INFO_AFFINITY)
244 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000245 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000246 else thread->affinity = req->affinity;
247 }
248}
249
250/* suspend a thread */
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000251int suspend_thread( struct thread *thread, int check_limit )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000252{
253 int old_count = thread->suspend;
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000254 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000255 {
Alexandre Julliard0a707831999-11-08 05:31:47 +0000256 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000257 }
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000258 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000259 return old_count;
260}
261
262/* resume a thread */
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000263int resume_thread( struct thread *thread )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000264{
265 int old_count = thread->suspend;
266 if (thread->suspend > 0)
267 {
Alexandre Julliard0a707831999-11-08 05:31:47 +0000268 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000269 }
270 return old_count;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000271}
272
Ulrich Weigand371fd751999-04-11 17:13:03 +0000273/* suspend all threads but the current */
274void suspend_all_threads( void )
275{
276 struct thread *thread;
277 for ( thread = first_thread; thread; thread = thread->next )
278 if ( thread != current )
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000279 suspend_thread( thread, 0 );
Ulrich Weigand371fd751999-04-11 17:13:03 +0000280}
281
282/* resume all threads but the current */
283void resume_all_threads( void )
284{
285 struct thread *thread;
286 for ( thread = first_thread; thread; thread = thread->next )
287 if ( thread != current )
288 resume_thread( thread );
289}
290
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000291/* add a thread to an object wait queue; return 1 if OK, 0 on error */
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000292int add_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000293{
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000294 grab_object( obj );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000295 entry->obj = obj;
296 entry->prev = obj->tail;
297 entry->next = NULL;
298 if (obj->tail) obj->tail->next = entry;
299 else obj->head = entry;
300 obj->tail = entry;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000301 return 1;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000302}
303
304/* remove a thread from an object wait queue */
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000305void remove_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000306{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000307 if (entry->next) entry->next->prev = entry->prev;
308 else obj->tail = entry->prev;
309 if (entry->prev) entry->prev->next = entry->next;
310 else obj->head = entry->next;
311 release_object( obj );
312}
313
314/* finish waiting */
315static void end_wait( struct thread *thread )
316{
317 struct thread_wait *wait = thread->wait;
318 struct wait_queue_entry *entry;
319 int i;
320
321 assert( wait );
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000322 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
323 entry->obj->ops->remove_queue( entry->obj, entry );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000324 if (wait->user) remove_timeout_user( wait->user );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000325 free( wait );
326 thread->wait = NULL;
327}
328
329/* build the thread wait structure */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000330static int wait_on( int count, struct object *objects[], int flags,
331 int timeout, sleep_reply func )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000332{
333 struct thread_wait *wait;
334 struct wait_queue_entry *entry;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000335 int i;
336
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000337 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000338 current->wait = wait;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000339 wait->count = count;
340 wait->flags = flags;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000341 wait->user = NULL;
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000342 wait->reply = func;
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000343 if (flags & SELECT_TIMEOUT)
344 {
345 gettimeofday( &wait->timeout, 0 );
346 add_timeout( &wait->timeout, timeout );
347 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000348
349 for (i = 0, entry = wait->queues; i < count; i++, entry++)
350 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000351 struct object *obj = objects[i];
352 entry->thread = current;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000353 if (!obj->ops->add_queue( obj, entry ))
354 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000355 wait->count = i;
356 end_wait( current );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000357 return 0;
358 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000359 }
360 return 1;
361}
362
363/* check if the thread waiting condition is satisfied */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000364static int check_wait( struct thread *thread, struct object **object )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000365{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000366 int i, signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000367 struct thread_wait *wait = thread->wait;
368 struct wait_queue_entry *entry = wait->queues;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000369
370 assert( wait );
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000371 *object = NULL;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000372 if (wait->flags & SELECT_ALL)
373 {
Alexandre Julliard57e11311999-05-16 16:59:38 +0000374 int not_ok = 0;
375 /* Note: we must check them all anyway, as some objects may
376 * want to do something when signaled, even if others are not */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000377 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
Alexandre Julliard57e11311999-05-16 16:59:38 +0000378 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
379 if (not_ok) goto other_checks;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000380 /* Wait satisfied: tell it to all objects */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000381 signaled = 0;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000382 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
383 if (entry->obj->ops->satisfied( entry->obj, thread ))
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000384 signaled = STATUS_ABANDONED_WAIT_0;
385 return signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000386 }
387 else
388 {
389 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
390 {
391 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
392 /* Wait satisfied: tell it to the object */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000393 signaled = i;
394 *object = entry->obj;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000395 if (entry->obj->ops->satisfied( entry->obj, thread ))
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000396 signaled = i + STATUS_ABANDONED_WAIT_0;
397 return signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000398 }
399 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000400
401 other_checks:
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000402 if ((wait->flags & SELECT_ALERTABLE) && thread->apc) return STATUS_USER_APC;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000403 if (wait->flags & SELECT_TIMEOUT)
404 {
405 struct timeval now;
406 gettimeofday( &now, NULL );
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000407 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000408 }
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000409 return -1;
410}
411
412/* build a reply to the select request */
413static void build_select_reply( struct thread *thread, struct object *obj, int signaled )
414{
415 struct select_request *req = get_req_ptr( thread );
416 req->signaled = signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000417}
418
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000419/* attempt to wake up a thread */
420/* return 1 if OK, 0 if the wait condition is still not satisfied */
421static int wake_thread( struct thread *thread )
422{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000423 int signaled;
424 struct object *object;
425 if ((signaled = check_wait( thread, &object )) == -1) return 0;
426 thread->error = 0;
427 thread->wait->reply( thread, object, signaled );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000428 end_wait( thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000429 return 1;
430}
431
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000432/* thread wait timeout */
433static void thread_timeout( void *ptr )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000434{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000435 struct thread *thread = ptr;
436 if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread );
437 assert( thread->wait );
438 thread->error = 0;
439 thread->wait->user = NULL;
440 thread->wait->reply( thread, NULL, STATUS_TIMEOUT );
441 end_wait( thread );
442 send_reply( thread );
443}
444
445/* sleep on a list of objects */
446int sleep_on( int count, struct object *objects[], int flags, int timeout, sleep_reply func )
447{
448 assert( !current->wait );
449 if (!wait_on( count, objects, flags, timeout, func )) return 0;
450 if (wake_thread( current )) return 1;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000451 /* now we need to wait */
452 if (flags & SELECT_TIMEOUT)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000453 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000454 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
455 thread_timeout, current )))
456 {
457 end_wait( current );
458 return 0;
459 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000460 }
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000461 return 1;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000462}
463
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000464/* select on a list of handles */
465static int select_on( int count, int *handles, int flags, int timeout )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000466{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000467 int ret = 0;
468 int i;
469 struct object *objects[MAXIMUM_WAIT_OBJECTS];
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000470
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000471 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
472 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000473 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000474 return 0;
475 }
476 for (i = 0; i < count; i++)
477 {
478 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
479 break;
480 }
481 if (i == count) ret = sleep_on( count, objects, flags, timeout, build_select_reply );
482 while (--i >= 0) release_object( objects[i] );
483 return ret;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000484}
485
486/* attempt to wake threads sleeping on the object wait queue */
487void wake_up( struct object *obj, int max )
488{
489 struct wait_queue_entry *entry = obj->head;
490
491 while (entry)
492 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000493 struct thread *thread = entry->thread;
494 entry = entry->next;
495 if (wake_thread( thread ))
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000496 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000497 send_reply( thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000498 if (max && !--max) break;
499 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000500 }
501}
502
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000503/* queue an async procedure call */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000504static int thread_queue_apc( struct thread *thread, void *func, void *param )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000505{
506 struct thread_apc *apc;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000507 if (!thread->apc)
508 {
509 if (!(thread->apc = mem_alloc( MAX_THREAD_APC * sizeof(*apc) )))
510 return 0;
511 thread->apc_count = 0;
512 }
513 else if (thread->apc_count >= MAX_THREAD_APC) return 0;
514 thread->apc[thread->apc_count].func = func;
515 thread->apc[thread->apc_count].param = param;
516 thread->apc_count++;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000517 if (thread->wait)
518 {
519 if (wake_thread( thread )) send_reply( thread );
520 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000521 return 1;
522}
523
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000524/* retrieve an LDT selector entry */
525static void get_selector_entry( struct thread *thread, int entry,
526 unsigned int *base, unsigned int *limit,
527 unsigned char *flags )
528{
529 if (!thread->process->ldt_copy || !thread->process->ldt_flags)
530 {
531 set_error( STATUS_ACCESS_DENIED );
532 return;
533 }
534 if (entry >= 8192)
535 {
536 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
537 return;
538 }
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000539 if (suspend_for_ptrace( thread ))
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000540 {
541 unsigned char flags_buf[4];
542 int *addr = (int *)thread->process->ldt_copy + 2 * entry;
543 if (read_thread_int( thread, addr, base ) == -1) goto done;
544 if (read_thread_int( thread, addr + 1, limit ) == -1) goto done;
545 addr = (int *)thread->process->ldt_flags + (entry >> 2);
546 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
547 *flags = flags_buf[entry & 3];
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000548 done:
549 resume_thread( thread );
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000550 }
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000551}
552
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000553/* kill a thread on the spot */
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000554void kill_thread( struct thread *thread, int violent_death )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000555{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000556 if (thread->state == TERMINATED) return; /* already killed */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000557 thread->state = TERMINATED;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000558 if (current == thread) current = NULL;
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000559 if (debug_level)
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000560 fprintf( stderr,"%08x: *killed* exit_code=%d\n",
561 (unsigned int)thread, thread->exit_code );
562 if (thread->wait)
563 {
564 end_wait( thread );
565 /* if it is waiting on the socket, we don't need to send a SIGTERM */
566 violent_death = 0;
567 }
Alexandre Julliardff81d782000-03-08 12:01:30 +0000568 debug_exit_thread( thread );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000569 abandon_mutexes( thread );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000570 remove_process_thread( thread->process, thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000571 wake_up( &thread->obj, 0 );
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000572 detach_thread( thread, violent_death ? SIGTERM : 0 );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000573 remove_select_user( &thread->obj );
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000574 munmap( thread->buffer, MAX_REQUEST_LENGTH );
575 thread->buffer = (void *)-1;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000576 release_object( thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000577}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000578
Alexandre Julliard07d84462000-04-16 19:45:05 +0000579/* take a snapshot of currently running threads */
580struct thread_snapshot *thread_snap( int *count )
581{
582 struct thread_snapshot *snapshot, *ptr;
583 struct thread *thread;
584 int total = 0;
585
586 for (thread = first_thread; thread; thread = thread->next)
587 if (thread->state != TERMINATED) total++;
588 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
589 ptr = snapshot;
590 for (thread = first_thread; thread; thread = thread->next)
591 {
592 if (thread->state == TERMINATED) continue;
593 ptr->thread = thread;
594 ptr->count = thread->obj.refcount;
595 ptr->priority = thread->priority;
596 grab_object( thread );
597 ptr++;
598 }
599 *count = total;
600 return snapshot;
601}
602
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000603/* signal that we are finished booting on the client side */
604DECL_HANDLER(boot_done)
605{
Alexandre Julliardf6507ed2000-04-06 22:05:16 +0000606 debug_level = max( debug_level, req->debug_level );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000607 /* Make sure last_req is initialized */
608 current->last_req = REQ_BOOT_DONE;
609 if (current == booting_thread)
610 {
611 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
612 lock_master_socket(0); /* allow other clients now */
613 }
614}
615
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000616/* create a new thread */
617DECL_HANDLER(new_thread)
618{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000619 struct thread *thread;
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000620 int sock[2];
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000621
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000622 if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000623 {
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000624 if ((thread = create_thread( sock[0], current->process )))
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000625 {
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000626 if (req->suspend) thread->suspend++;
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000627 req->tid = thread;
628 if ((req->handle = alloc_handle( current->process, thread,
629 THREAD_ALL_ACCESS, req->inherit )) != -1)
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000630 {
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000631 set_reply_fd( current, sock[1] );
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000632 /* thread object will be released when the thread gets killed */
Alexandre Julliard6a72dc52000-04-14 13:42:00 +0000633 add_process_thread( current->process, thread );
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000634 return;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000635 }
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000636 release_object( thread );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000637 }
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000638 close( sock[1] );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000639 }
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000640 else file_set_error();
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000641}
642
643/* retrieve the thread buffer file descriptor */
644DECL_HANDLER(get_thread_buffer)
645{
646 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000647}
648
649/* initialize a new thread */
650DECL_HANDLER(init_thread)
651{
Alexandre Julliard0a707831999-11-08 05:31:47 +0000652 if (current->unix_pid)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000653 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000654 fatal_protocol_error( current, "init_thread: already running\n" );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000655 return;
656 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000657 current->unix_pid = req->unix_pid;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000658 current->teb = req->teb;
Alexandre Julliard0a707831999-11-08 05:31:47 +0000659 if (current->suspend + current->process->suspend > 0) stop_thread( current );
Alexandre Julliardff81d782000-03-08 12:01:30 +0000660 if (current->process->running_threads > 1)
Alexandre Julliardb73421d2000-03-30 19:30:24 +0000661 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000662}
663
664/* terminate a thread */
665DECL_HANDLER(terminate_thread)
666{
667 struct thread *thread;
668
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000669 req->self = 0;
670 req->last = 0;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000671 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
672 {
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000673 thread->exit_code = req->exit_code;
674 if (thread != current) kill_thread( thread, 1 );
675 else
676 {
677 req->self = 1;
678 req->last = (thread->process->running_threads == 1);
679 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000680 release_object( thread );
681 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000682}
683
684/* fetch information about a thread */
685DECL_HANDLER(get_thread_info)
686{
687 struct thread *thread;
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000688 int handle = req->handle;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000689
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000690 if (handle == -1) thread = get_thread_from_id( req->tid_in );
691 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
692
693 if (thread)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000694 {
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000695 req->tid = get_thread_id( thread );
696 req->teb = thread->teb;
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000697 req->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000698 req->priority = thread->priority;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000699 release_object( thread );
700 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000701}
702
703/* set information about a thread */
704DECL_HANDLER(set_thread_info)
705{
706 struct thread *thread;
707
708 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
709 {
710 set_thread_info( thread, req );
711 release_object( thread );
712 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000713}
714
715/* suspend a thread */
716DECL_HANDLER(suspend_thread)
717{
718 struct thread *thread;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000719
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000720 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
721 {
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000722 req->count = suspend_thread( thread, 1 );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000723 release_object( thread );
724 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000725}
726
727/* resume a thread */
728DECL_HANDLER(resume_thread)
729{
730 struct thread *thread;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000731
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000732 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
733 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000734 req->count = resume_thread( thread );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000735 release_object( thread );
736 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000737}
738
739/* select on a handle list */
740DECL_HANDLER(select)
741{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000742 if (!select_on( req->count, req->handles, req->flags, req->timeout ))
743 req->signaled = -1;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000744}
745
746/* queue an APC for a thread */
747DECL_HANDLER(queue_apc)
748{
749 struct thread *thread;
750 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
751 {
752 thread_queue_apc( thread, req->func, req->param );
753 release_object( thread );
754 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000755}
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000756
757/* get list of APC to call */
758DECL_HANDLER(get_apcs)
759{
760 if ((req->count = current->apc_count))
761 {
762 memcpy( req->apcs, current->apc, current->apc_count * sizeof(*current->apc) );
763 free( current->apc );
764 current->apc = NULL;
765 current->apc_count = 0;
766 }
767}
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000768
769/* fetch a selector entry for a thread */
770DECL_HANDLER(get_selector_entry)
771{
772 struct thread *thread;
773 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
774 {
775 get_selector_entry( thread, req->entry, &req->base, &req->limit, &req->flags );
776 release_object( thread );
777 }
778}