blob: 5932644a849ece34171e29fe91818af330e16ae1 [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 Julliard642d3131998-07-12 19:29:36 +000028#include "winerror.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000029
30#include "handle.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000031#include "process.h"
32#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000033#include "request.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000034
35
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000036/* thread queues */
37
38struct wait_queue_entry
39{
40 struct wait_queue_entry *next;
41 struct wait_queue_entry *prev;
42 struct object *obj;
43 struct thread *thread;
44};
45
46struct thread_wait
47{
48 int count; /* count of objects */
49 int flags;
50 struct timeval timeout;
Alexandre Julliard57e11311999-05-16 16:59:38 +000051 struct timeout_user *user;
Alexandre Julliard9de03f42000-01-04 02:23:38 +000052 sleep_reply reply; /* function to build the reply */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000053 struct wait_queue_entry queues[1];
54};
55
Alexandre Julliard62a8b431999-01-19 17:48:23 +000056/* asynchronous procedure calls */
57
58struct thread_apc
59{
60 void *func; /* function to call in client */
61 void *param; /* function param */
62};
63#define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */
64
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000065
Alexandre Julliard642d3131998-07-12 19:29:36 +000066/* thread operations */
67
Alexandre Julliard767e6f61998-08-09 12:47:43 +000068static void dump_thread( struct object *obj, int verbose );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000069static int thread_signaled( struct object *obj, struct thread *thread );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000070extern void thread_poll_event( struct object *obj, int event );
Alexandre Julliard642d3131998-07-12 19:29:36 +000071static void destroy_thread( struct object *obj );
72
73static const struct object_ops thread_ops =
74{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000075 sizeof(struct thread), /* size */
76 dump_thread, /* dump */
77 add_queue, /* add_queue */
78 remove_queue, /* remove_queue */
79 thread_signaled, /* signaled */
80 no_satisfied, /* satisfied */
81 NULL, /* get_poll_events */
82 thread_poll_event, /* poll_event */
83 no_read_fd, /* get_read_fd */
84 no_write_fd, /* get_write_fd */
85 no_flush, /* flush */
86 no_get_file_info, /* get_file_info */
87 destroy_thread /* destroy */
Alexandre Julliard642d3131998-07-12 19:29:36 +000088};
89
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +000090static struct thread *first_thread;
Alexandre Julliard767e6f61998-08-09 12:47:43 +000091
Alexandre Julliard5bc78081999-06-22 17:26:53 +000092/* allocate the buffer for the communication with the client */
93static int alloc_client_buffer( struct thread *thread )
Alexandre Julliard642d3131998-07-12 19:29:36 +000094{
Alexandre Julliard5bc78081999-06-22 17:26:53 +000095 int fd;
96
97 if ((fd = create_anonymous_file()) == -1) return -1;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000098 if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error;
99 if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE,
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000100 MAP_SHARED, fd, 0 )) == (void*)-1) goto error;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000101 return fd;
102
103 error:
104 file_set_error();
105 if (fd != -1) close( fd );
106 return -1;
107}
108
109/* create a new thread */
110static struct thread *create_thread( int fd, struct process *process, int suspend )
111{
112 struct thread *thread;
113 int buf_fd;
114
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 */
121 thread->teb = NULL;
122 thread->mutex = NULL;
123 thread->debug_ctx = NULL;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000124 thread->wait = NULL;
125 thread->apc = NULL;
126 thread->apc_count = 0;
127 thread->error = 0;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000128 thread->pass_fd = -1;
Alexandre Julliard0a707831999-11-08 05:31:47 +0000129 thread->state = RUNNING;
130 thread->attached = 0;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000131 thread->exit_code = 0x103; /* STILL_ACTIVE */
132 thread->next = NULL;
133 thread->prev = NULL;
134 thread->priority = THREAD_PRIORITY_NORMAL;
135 thread->affinity = 1;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000136 thread->suspend = (suspend != 0);
137 thread->buffer = (void *)-1;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000138 thread->last_req = REQ_GET_THREAD_BUFFER;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000139
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000140 if (!first_thread) /* creating the first thread */
Alexandre Julliardf692d441999-03-21 19:23:54 +0000141 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000142 current = thread;
143 thread->process = process = create_initial_process();
144 assert( process );
Alexandre Julliardf692d441999-03-21 19:23:54 +0000145 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000146 else thread->process = (struct process *)grab_object( process );
Alexandre Julliardf692d441999-03-21 19:23:54 +0000147
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000148 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000149 first_thread = thread;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000150 add_process_thread( process, thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000151
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000152 if ((buf_fd = alloc_client_buffer( thread )) == -1) goto error;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000153
154 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000155 set_reply_fd( thread, buf_fd ); /* send the fd to the client */
156 send_reply( thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000157 return thread;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000158
159 error:
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000160 remove_process_thread( process, thread );
161 release_object( thread );
162 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000163}
164
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000165/* create the initial thread and start the main server loop */
166void create_initial_thread( int fd )
167{
168 create_thread( fd, NULL, 0 );
169 select_loop();
170}
171
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000172/* handle a client event */
173void thread_poll_event( struct object *obj, int event )
174{
175 struct thread *thread = (struct thread *)obj;
176 assert( obj->ops == &thread_ops );
177
178 if (event & (POLLERR | POLLHUP)) kill_thread( thread, BROKEN_PIPE );
179 else
180 {
181 if (event & POLLOUT) write_request( thread );
182 if (event & POLLIN) read_request( thread );
183 }
184}
185
Alexandre Julliard642d3131998-07-12 19:29:36 +0000186/* destroy a thread when its refcount is 0 */
187static void destroy_thread( struct object *obj )
188{
189 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 Julliard62a8b431999-01-19 17:48:23 +0000197 if (thread->apc) free( thread->apc );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000198 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000199 if (thread->pass_fd != -1) close( thread->pass_fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000200}
201
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000202/* dump a thread on stdout for debugging purposes */
203static void dump_thread( struct object *obj, int verbose )
204{
205 struct thread *thread = (struct thread *)obj;
206 assert( obj->ops == &thread_ops );
207
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000208 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
209 thread->unix_pid, thread->teb, thread->state );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000210}
211
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000212static int thread_signaled( struct object *obj, struct thread *thread )
213{
214 struct thread *mythread = (struct thread *)obj;
215 return (mythread->state == TERMINATED);
216}
217
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000218/* get a thread pointer from a thread id (and increment the refcount) */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000219struct thread *get_thread_from_id( void *id )
220{
221 struct thread *t = first_thread;
222 while (t && (t != id)) t = t->next;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000223 if (t) grab_object( t );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000224 return t;
225}
226
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000227/* get a thread from a handle (and increment the refcount) */
228struct thread *get_thread_from_handle( int handle, unsigned int access )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000229{
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000230 return (struct thread *)get_handle_obj( current->process, handle,
231 access, &thread_ops );
232}
233
Alexandre Julliard578c1001999-11-14 21:23:21 +0000234/* find a thread from a Unix pid */
235struct thread *get_thread_from_pid( int pid )
236{
237 struct thread *t = first_thread;
238 while (t && (t->unix_pid != pid)) t = t->next;
239 return t;
240}
241
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000242/* set all information about a thread */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000243static void set_thread_info( struct thread *thread,
244 struct set_thread_info_request *req )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000245{
246 if (req->mask & SET_THREAD_INFO_PRIORITY)
247 thread->priority = req->priority;
248 if (req->mask & SET_THREAD_INFO_AFFINITY)
249 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000250 if (req->affinity != 1) set_error( ERROR_INVALID_PARAMETER );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000251 else thread->affinity = req->affinity;
252 }
253}
254
255/* suspend a thread */
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000256int suspend_thread( struct thread *thread, int check_limit )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000257{
258 int old_count = thread->suspend;
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000259 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000260 {
Alexandre Julliard0a707831999-11-08 05:31:47 +0000261 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000262 }
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000263 else set_error( ERROR_SIGNAL_REFUSED );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000264 return old_count;
265}
266
267/* resume a thread */
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000268int resume_thread( struct thread *thread )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000269{
270 int old_count = thread->suspend;
271 if (thread->suspend > 0)
272 {
Alexandre Julliard0a707831999-11-08 05:31:47 +0000273 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000274 }
275 return old_count;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000276}
277
Ulrich Weigand371fd751999-04-11 17:13:03 +0000278/* suspend all threads but the current */
279void suspend_all_threads( void )
280{
281 struct thread *thread;
282 for ( thread = first_thread; thread; thread = thread->next )
283 if ( thread != current )
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000284 suspend_thread( thread, 0 );
Ulrich Weigand371fd751999-04-11 17:13:03 +0000285}
286
287/* resume all threads but the current */
288void resume_all_threads( void )
289{
290 struct thread *thread;
291 for ( thread = first_thread; thread; thread = thread->next )
292 if ( thread != current )
293 resume_thread( thread );
294}
295
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000296/* add a thread to an object wait queue; return 1 if OK, 0 on error */
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000297int add_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000298{
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000299 grab_object( obj );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000300 entry->obj = obj;
301 entry->prev = obj->tail;
302 entry->next = NULL;
303 if (obj->tail) obj->tail->next = entry;
304 else obj->head = entry;
305 obj->tail = entry;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000306 return 1;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000307}
308
309/* remove a thread from an object wait queue */
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000310void remove_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000311{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000312 if (entry->next) entry->next->prev = entry->prev;
313 else obj->tail = entry->prev;
314 if (entry->prev) entry->prev->next = entry->next;
315 else obj->head = entry->next;
316 release_object( obj );
317}
318
319/* finish waiting */
320static void end_wait( struct thread *thread )
321{
322 struct thread_wait *wait = thread->wait;
323 struct wait_queue_entry *entry;
324 int i;
325
326 assert( wait );
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000327 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
328 entry->obj->ops->remove_queue( entry->obj, entry );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000329 if (wait->user) remove_timeout_user( wait->user );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000330 free( wait );
331 thread->wait = NULL;
332}
333
334/* build the thread wait structure */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000335static int wait_on( int count, struct object *objects[], int flags,
336 int timeout, sleep_reply func )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000337{
338 struct thread_wait *wait;
339 struct wait_queue_entry *entry;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000340 int i;
341
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000342 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000343 current->wait = wait;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000344 wait->count = count;
345 wait->flags = flags;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000346 wait->user = NULL;
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000347 wait->reply = func;
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000348 if (flags & SELECT_TIMEOUT)
349 {
350 gettimeofday( &wait->timeout, 0 );
351 add_timeout( &wait->timeout, timeout );
352 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000353
354 for (i = 0, entry = wait->queues; i < count; i++, entry++)
355 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000356 struct object *obj = objects[i];
357 entry->thread = current;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000358 if (!obj->ops->add_queue( obj, entry ))
359 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000360 wait->count = i;
361 end_wait( current );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000362 return 0;
363 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000364 }
365 return 1;
366}
367
368/* check if the thread waiting condition is satisfied */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000369static int check_wait( struct thread *thread, struct object **object )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000370{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000371 int i, signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000372 struct thread_wait *wait = thread->wait;
373 struct wait_queue_entry *entry = wait->queues;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000374
375 assert( wait );
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000376 *object = NULL;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000377 if (wait->flags & SELECT_ALL)
378 {
Alexandre Julliard57e11311999-05-16 16:59:38 +0000379 int not_ok = 0;
380 /* Note: we must check them all anyway, as some objects may
381 * want to do something when signaled, even if others are not */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000382 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
Alexandre Julliard57e11311999-05-16 16:59:38 +0000383 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
384 if (not_ok) goto other_checks;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000385 /* Wait satisfied: tell it to all objects */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000386 signaled = 0;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000387 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
388 if (entry->obj->ops->satisfied( entry->obj, thread ))
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000389 signaled = STATUS_ABANDONED_WAIT_0;
390 return signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000391 }
392 else
393 {
394 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
395 {
396 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
397 /* Wait satisfied: tell it to the object */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000398 signaled = i;
399 *object = entry->obj;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000400 if (entry->obj->ops->satisfied( entry->obj, thread ))
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000401 signaled = i + STATUS_ABANDONED_WAIT_0;
402 return signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000403 }
404 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000405
406 other_checks:
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000407 if ((wait->flags & SELECT_ALERTABLE) && thread->apc) return STATUS_USER_APC;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000408 if (wait->flags & SELECT_TIMEOUT)
409 {
410 struct timeval now;
411 gettimeofday( &now, NULL );
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000412 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000413 }
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000414 return -1;
415}
416
417/* build a reply to the select request */
418static void build_select_reply( struct thread *thread, struct object *obj, int signaled )
419{
420 struct select_request *req = get_req_ptr( thread );
421 req->signaled = signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000422}
423
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000424/* attempt to wake up a thread */
425/* return 1 if OK, 0 if the wait condition is still not satisfied */
426static int wake_thread( struct thread *thread )
427{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000428 int signaled;
429 struct object *object;
430 if ((signaled = check_wait( thread, &object )) == -1) return 0;
431 thread->error = 0;
432 thread->wait->reply( thread, object, signaled );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000433 end_wait( thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000434 return 1;
435}
436
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000437/* thread wait timeout */
438static void thread_timeout( void *ptr )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000439{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000440 struct thread *thread = ptr;
441 if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread );
442 assert( thread->wait );
443 thread->error = 0;
444 thread->wait->user = NULL;
445 thread->wait->reply( thread, NULL, STATUS_TIMEOUT );
446 end_wait( thread );
447 send_reply( thread );
448}
449
450/* sleep on a list of objects */
451int sleep_on( int count, struct object *objects[], int flags, int timeout, sleep_reply func )
452{
453 assert( !current->wait );
454 if (!wait_on( count, objects, flags, timeout, func )) return 0;
455 if (wake_thread( current )) return 1;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000456 /* now we need to wait */
457 if (flags & SELECT_TIMEOUT)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000458 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000459 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
460 thread_timeout, current )))
461 {
462 end_wait( current );
463 return 0;
464 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000465 }
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000466 return 1;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000467}
468
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000469/* select on a list of handles */
470static int select_on( int count, int *handles, int flags, int timeout )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000471{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000472 int ret = 0;
473 int i;
474 struct object *objects[MAXIMUM_WAIT_OBJECTS];
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000475
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000476 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
477 {
478 set_error( ERROR_INVALID_PARAMETER );
479 return 0;
480 }
481 for (i = 0; i < count; i++)
482 {
483 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
484 break;
485 }
486 if (i == count) ret = sleep_on( count, objects, flags, timeout, build_select_reply );
487 while (--i >= 0) release_object( objects[i] );
488 return ret;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000489}
490
491/* attempt to wake threads sleeping on the object wait queue */
492void wake_up( struct object *obj, int max )
493{
494 struct wait_queue_entry *entry = obj->head;
495
496 while (entry)
497 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000498 struct thread *thread = entry->thread;
499 entry = entry->next;
500 if (wake_thread( thread ))
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000501 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000502 send_reply( thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000503 if (max && !--max) break;
504 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000505 }
506}
507
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000508/* queue an async procedure call */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000509static int thread_queue_apc( struct thread *thread, void *func, void *param )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000510{
511 struct thread_apc *apc;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000512 if (!thread->apc)
513 {
514 if (!(thread->apc = mem_alloc( MAX_THREAD_APC * sizeof(*apc) )))
515 return 0;
516 thread->apc_count = 0;
517 }
518 else if (thread->apc_count >= MAX_THREAD_APC) return 0;
519 thread->apc[thread->apc_count].func = func;
520 thread->apc[thread->apc_count].param = param;
521 thread->apc_count++;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000522 if (thread->wait)
523 {
524 if (wake_thread( thread )) send_reply( thread );
525 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000526 return 1;
527}
528
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000529/* kill a thread on the spot */
530void kill_thread( struct thread *thread, int exit_code )
531{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000532 if (thread->state == TERMINATED) return; /* already killed */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000533 thread->state = TERMINATED;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000534 thread->exit_code = exit_code;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000535 if (current == thread) current = NULL;
536 if (debug_level) trace_kill( thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000537 if (thread->wait) end_wait( thread );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000538 debug_exit_thread( thread, exit_code );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000539 abandon_mutexes( thread );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000540 remove_process_thread( thread->process, thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000541 wake_up( &thread->obj, 0 );
Alexandre Julliard0a707831999-11-08 05:31:47 +0000542 detach_thread( thread );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000543 remove_select_user( &thread->obj );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000544 release_object( thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000545}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000546
547/* create a new thread */
548DECL_HANDLER(new_thread)
549{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000550 struct thread *thread;
551 struct process *process;
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000552 int sock[2];
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000553
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000554 if (!(process = get_process_from_id( req->pid ))) return;
555
556 if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000557 {
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000558 if ((thread = create_thread( sock[0], process, req->suspend )))
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000559 {
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000560 req->tid = thread;
561 if ((req->handle = alloc_handle( current->process, thread,
562 THREAD_ALL_ACCESS, req->inherit )) != -1)
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000563 {
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000564 set_reply_fd( current, sock[1] );
565 release_object( process );
566 /* thread object will be released when the thread gets killed */
567 return;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000568 }
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000569 release_object( thread );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000570 }
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000571 close( sock[1] );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000572 }
Alexandre Julliard95e7acb2000-01-04 02:40:22 +0000573 else file_set_error();
574 release_object( process );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000575}
576
577/* retrieve the thread buffer file descriptor */
578DECL_HANDLER(get_thread_buffer)
579{
580 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000581}
582
583/* initialize a new thread */
584DECL_HANDLER(init_thread)
585{
Alexandre Julliard0a707831999-11-08 05:31:47 +0000586 if (current->unix_pid)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000587 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000588 fatal_protocol_error( current, "init_thread: already running\n" );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000589 return;
590 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000591 current->unix_pid = req->unix_pid;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000592 current->teb = req->teb;
Alexandre Julliard0a707831999-11-08 05:31:47 +0000593 if (current->suspend + current->process->suspend > 0) stop_thread( current );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000594 req->pid = current->process;
595 req->tid = current;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000596}
597
598/* terminate a thread */
599DECL_HANDLER(terminate_thread)
600{
601 struct thread *thread;
602
603 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
604 {
605 kill_thread( thread, req->exit_code );
606 release_object( thread );
607 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000608}
609
610/* fetch information about a thread */
611DECL_HANDLER(get_thread_info)
612{
613 struct thread *thread;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000614
615 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
616 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000617 req->tid = thread;
618 req->exit_code = thread->exit_code;
619 req->priority = thread->priority;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000620 release_object( thread );
621 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000622}
623
624/* set information about a thread */
625DECL_HANDLER(set_thread_info)
626{
627 struct thread *thread;
628
629 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
630 {
631 set_thread_info( thread, req );
632 release_object( thread );
633 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000634}
635
636/* suspend a thread */
637DECL_HANDLER(suspend_thread)
638{
639 struct thread *thread;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000640
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000641 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
642 {
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000643 req->count = suspend_thread( thread, 1 );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000644 release_object( thread );
645 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000646}
647
648/* resume a thread */
649DECL_HANDLER(resume_thread)
650{
651 struct thread *thread;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000652
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000653 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
654 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000655 req->count = resume_thread( thread );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000656 release_object( thread );
657 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000658}
659
660/* select on a handle list */
661DECL_HANDLER(select)
662{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000663 if (!select_on( req->count, req->handles, req->flags, req->timeout ))
664 req->signaled = -1;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000665}
666
667/* queue an APC for a thread */
668DECL_HANDLER(queue_apc)
669{
670 struct thread *thread;
671 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
672 {
673 thread_queue_apc( thread, req->func, req->param );
674 release_object( thread );
675 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000676}
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000677
678/* get list of APC to call */
679DECL_HANDLER(get_apcs)
680{
681 if ((req->count = current->apc_count))
682 {
683 memcpy( req->apcs, current->apc, current->apc_count * sizeof(*current->apc) );
684 free( current->apc );
685 current->apc = NULL;
686 current->apc_count = 0;
687 }
688}