blob: ced318977e889d1b65b23eda200ad17ced7051a5 [file] [log] [blame]
Alexandre Julliard642d3131998-07-12 19:29:36 +00001/*
2 * Server-side thread management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard642d3131998-07-12 19:29:36 +000019 */
20
Howard Abrams13277481999-07-10 13:16:29 +000021#include "config.h"
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000022#include "wine/port.h"
Howard Abrams13277481999-07-10 13:16:29 +000023
Alexandre Julliard642d3131998-07-12 19:29:36 +000024#include <assert.h>
Alexandre Julliarde5dedb12001-03-08 01:16:41 +000025#include <errno.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000026#include <fcntl.h>
Alexandre Julliard767e6f61998-08-09 12:47:43 +000027#include <signal.h>
Alexandre Julliardea1afce2000-08-22 20:08:37 +000028#include <stdarg.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000032#include <sys/types.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000033#include <unistd.h>
Ryan Cumming24f4ece2002-11-25 01:33:38 +000034#include <time.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000035
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000036#include "windef.h"
Michael Vekslerf935c591999-02-09 15:49:39 +000037#include "winbase.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000038
Alexandre Julliard863637b2003-01-30 00:26:44 +000039#include "file.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000040#include "handle.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000041#include "process.h"
42#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000043#include "request.h"
Alexandre Julliard1a66d222001-08-28 18:44:52 +000044#include "user.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000045
46
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000047/* thread queues */
48
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000049struct thread_wait
50{
Alexandre Julliarde5dedb12001-03-08 01:16:41 +000051 struct thread_wait *next; /* next wait structure for this thread */
52 struct thread *thread; /* owner thread */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000053 int count; /* count of objects */
54 int flags;
Alexandre Julliarde5dedb12001-03-08 01:16:41 +000055 void *cookie; /* magic cookie to return to client */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000056 struct timeval timeout;
Alexandre Julliard57e11311999-05-16 16:59:38 +000057 struct timeout_user *user;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000058 struct wait_queue_entry queues[1];
59};
60
Alexandre Julliard62a8b431999-01-19 17:48:23 +000061/* asynchronous procedure calls */
62
63struct thread_apc
64{
Alexandre Julliardea1afce2000-08-22 20:08:37 +000065 struct thread_apc *next; /* queue linked list */
66 struct thread_apc *prev;
67 struct object *owner; /* object that queued this apc */
68 void *func; /* function to call in client */
69 enum apc_type type; /* type of apc function */
70 int nb_args; /* number of arguments */
Alexandre Julliard088bcf92003-04-04 22:26:34 +000071 void *arg1; /* function arguments */
72 void *arg2;
73 void *arg3;
Alexandre Julliard62a8b431999-01-19 17:48:23 +000074};
Alexandre Julliard62a8b431999-01-19 17:48:23 +000075
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000076
Alexandre Julliard642d3131998-07-12 19:29:36 +000077/* thread operations */
78
Alexandre Julliard767e6f61998-08-09 12:47:43 +000079static void dump_thread( struct object *obj, int verbose );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000080static int thread_signaled( struct object *obj, struct thread *thread );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000081static void thread_poll_event( struct fd *fd, int event );
Alexandre Julliard642d3131998-07-12 19:29:36 +000082static void destroy_thread( struct object *obj );
Alexandre Julliard23623802001-01-06 01:48:51 +000083static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
Alexandre Julliard642d3131998-07-12 19:29:36 +000084
85static const struct object_ops thread_ops =
86{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000087 sizeof(struct thread), /* size */
88 dump_thread, /* dump */
89 add_queue, /* add_queue */
90 remove_queue, /* remove_queue */
91 thread_signaled, /* signaled */
92 no_satisfied, /* satisfied */
Alexandre Julliard863637b2003-01-30 00:26:44 +000093 no_get_fd, /* get_fd */
Alexandre Julliard863637b2003-01-30 00:26:44 +000094 destroy_thread /* destroy */
95};
96
97static const struct fd_ops thread_fd_ops =
98{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000099 NULL, /* get_poll_events */
100 thread_poll_event, /* poll_event */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000101 no_flush, /* flush */
102 no_get_file_info, /* get_file_info */
Alexandre Julliard863637b2003-01-30 00:26:44 +0000103 no_queue_async /* queue_async */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000104};
105
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000106static struct thread *first_thread;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000107static struct thread *booting_thread;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000108
Alexandre Julliardf5242402001-02-28 21:45:23 +0000109/* initialize the structure for a newly allocated thread */
110inline static void init_thread_structure( struct thread *thread )
111{
112 int i;
113
Alexandre Julliard02a53c12003-02-25 04:17:22 +0000114 thread->unix_pid = -1; /* not known yet */
Alexandre Julliarda8497bd2003-03-22 21:00:09 +0000115 thread->unix_tid = -1; /* not known yet */
Alexandre Julliardf5242402001-02-28 21:45:23 +0000116 thread->context = NULL;
117 thread->teb = NULL;
118 thread->mutex = NULL;
119 thread->debug_ctx = NULL;
120 thread->debug_event = NULL;
121 thread->queue = NULL;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000122 thread->wait = NULL;
123 thread->system_apc.head = NULL;
124 thread->system_apc.tail = NULL;
125 thread->user_apc.head = NULL;
126 thread->user_apc.tail = NULL;
127 thread->error = 0;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000128 thread->req_data = NULL;
129 thread->req_toread = 0;
130 thread->reply_data = NULL;
131 thread->reply_towrite = 0;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000132 thread->request_fd = NULL;
133 thread->reply_fd = NULL;
134 thread->wait_fd = NULL;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000135 thread->state = RUNNING;
136 thread->attached = 0;
137 thread->exit_code = 0;
138 thread->next = NULL;
139 thread->prev = NULL;
140 thread->priority = THREAD_PRIORITY_NORMAL;
141 thread->affinity = 1;
142 thread->suspend = 0;
Ryan Cumming24f4ece2002-11-25 01:33:38 +0000143 thread->creation_time = time(NULL);
144 thread->exit_time = 0;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000145
146 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
147 thread->inflight[i].server = thread->inflight[i].client = -1;
148}
149
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000150/* create a new thread */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000151struct thread *create_thread( int fd, struct process *process )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000152{
153 struct thread *thread;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000154
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000155 if (!(thread = alloc_object( &thread_ops ))) return NULL;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000156
Alexandre Julliardf5242402001-02-28 21:45:23 +0000157 init_thread_structure( thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000158
Alexandre Julliardf5242402001-02-28 21:45:23 +0000159 thread->process = (struct process *)grab_object( process );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000160 if (!current) current = thread;
161
162 if (!booting_thread) /* first thread ever */
Alexandre Julliardf692d441999-03-21 19:23:54 +0000163 {
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000164 booting_thread = thread;
165 lock_master_socket(1);
Alexandre Julliardf692d441999-03-21 19:23:54 +0000166 }
Alexandre Julliardf692d441999-03-21 19:23:54 +0000167
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000168 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000169 first_thread = thread;
170
Alexandre Julliard91befe12003-02-01 01:38:40 +0000171 if (!(thread->id = alloc_ptid( thread )))
172 {
173 release_object( thread );
174 return NULL;
175 }
Alexandre Julliard580da242003-03-12 22:38:14 +0000176 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000177 {
178 release_object( thread );
179 return NULL;
180 }
Alexandre Julliard91befe12003-02-01 01:38:40 +0000181
Mike McCormack36cd6f52003-07-24 00:07:00 +0000182 thread->token = (struct token *) grab_object( process->token );
183
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000184 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000185 add_process_thread( thread->process, thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000186 return thread;
187}
188
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000189/* handle a client event */
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000190static void thread_poll_event( struct fd *fd, int event )
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000191{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000192 struct thread *thread = get_fd_user( fd );
193 assert( thread->obj.ops == &thread_ops );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000194
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000195 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000196 else if (event & POLLIN) read_request( thread );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000197 else if (event & POLLOUT) write_reply( thread );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000198}
199
200/* cleanup everything that is no longer needed by a dead thread */
201/* used by destroy_thread and kill_thread */
202static void cleanup_thread( struct thread *thread )
203{
204 int i;
205 struct thread_apc *apc;
206
207 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000208 if (thread->req_data) free( thread->req_data );
209 if (thread->reply_data) free( thread->reply_data );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000210 if (thread->request_fd) release_object( thread->request_fd );
211 if (thread->reply_fd) release_object( thread->reply_fd );
212 if (thread->wait_fd) release_object( thread->wait_fd );
Alexandre Julliard31022d62002-08-16 23:30:41 +0000213 free_msg_queue( thread );
Ulrich Czekallab2df5f92003-06-23 23:02:02 +0000214 cleanup_clipboard_thread(thread);
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000215 destroy_thread_windows( thread );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000216 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
217 {
218 if (thread->inflight[i].client != -1)
219 {
220 close( thread->inflight[i].server );
221 thread->inflight[i].client = thread->inflight[i].server = -1;
222 }
223 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000224 thread->req_data = NULL;
225 thread->reply_data = NULL;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000226 thread->request_fd = NULL;
227 thread->reply_fd = NULL;
228 thread->wait_fd = NULL;
Alexandre Julliardff732502002-06-22 01:20:36 +0000229
230 if (thread == booting_thread) /* killing booting thread */
231 {
232 booting_thread = NULL;
233 lock_master_socket(0);
234 }
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000235}
236
Alexandre Julliard642d3131998-07-12 19:29:36 +0000237/* destroy a thread when its refcount is 0 */
238static void destroy_thread( struct object *obj )
239{
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000240 struct thread_apc *apc;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000241 struct thread *thread = (struct thread *)obj;
242 assert( obj->ops == &thread_ops );
243
Alexandre Julliarde712e071999-05-23 19:53:30 +0000244 assert( !thread->debug_ctx ); /* cannot still be debugging something */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000245 if (thread->next) thread->next->prev = thread->prev;
246 if (thread->prev) thread->prev->next = thread->next;
247 else first_thread = thread->next;
Alexandre Julliard23623802001-01-06 01:48:51 +0000248 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000249 cleanup_thread( thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000250 release_object( thread->process );
Alexandre Julliard91befe12003-02-01 01:38:40 +0000251 if (thread->id) free_ptid( thread->id );
Mike McCormack36cd6f52003-07-24 00:07:00 +0000252 if (thread->token) release_object( thread->token );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000253}
254
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000255/* dump a thread on stdout for debugging purposes */
256static void dump_thread( struct object *obj, int verbose )
257{
258 struct thread *thread = (struct thread *)obj;
259 assert( obj->ops == &thread_ops );
260
Alexandre Julliarda8497bd2003-03-22 21:00:09 +0000261 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
262 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000263}
264
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000265static int thread_signaled( struct object *obj, struct thread *thread )
266{
267 struct thread *mythread = (struct thread *)obj;
268 return (mythread->state == TERMINATED);
269}
270
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000271/* get a thread pointer from a thread id (and increment the refcount) */
Alexandre Julliard54f22872002-10-03 19:54:57 +0000272struct thread *get_thread_from_id( thread_id_t id )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000273{
Alexandre Julliard91befe12003-02-01 01:38:40 +0000274 struct object *obj = get_ptid_entry( id );
275
276 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
277 set_error( STATUS_INVALID_PARAMETER );
278 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000279}
280
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000281/* get a thread from a handle (and increment the refcount) */
Alexandre Julliard51885742002-05-30 20:12:58 +0000282struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000283{
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000284 return (struct thread *)get_handle_obj( current->process, handle,
285 access, &thread_ops );
286}
287
Alexandre Julliard578c1001999-11-14 21:23:21 +0000288/* find a thread from a Unix pid */
289struct thread *get_thread_from_pid( int pid )
290{
Alexandre Julliarda8497bd2003-03-22 21:00:09 +0000291 struct thread *t;
292
293 for (t = first_thread; t; t = t->next) if (t->unix_tid == pid) return t;
294 for (t = first_thread; t; t = t->next) if (t->unix_pid == pid) return t;
295 return NULL;
Alexandre Julliard578c1001999-11-14 21:23:21 +0000296}
297
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000298/* set all information about a thread */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000299static void set_thread_info( struct thread *thread,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000300 const struct set_thread_info_request *req )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000301{
302 if (req->mask & SET_THREAD_INFO_PRIORITY)
303 thread->priority = req->priority;
304 if (req->mask & SET_THREAD_INFO_AFFINITY)
305 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000306 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000307 else thread->affinity = req->affinity;
308 }
309}
310
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000311/* stop a thread (at the Unix level) */
312void stop_thread( struct thread *thread )
313{
314 /* can't stop a thread while initialisation is in progress */
315 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
316}
317
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000318/* suspend a thread */
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000319static int suspend_thread( struct thread *thread )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000320{
321 int old_count = thread->suspend;
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000322 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000323 {
Alexandre Julliard0a707831999-11-08 05:31:47 +0000324 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000325 }
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000326 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000327 return old_count;
328}
329
330/* resume a thread */
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000331static int resume_thread( struct thread *thread )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000332{
333 int old_count = thread->suspend;
334 if (thread->suspend > 0)
335 {
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000336 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000337 }
338 return old_count;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000339}
340
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000341/* add a thread to an object wait queue; return 1 if OK, 0 on error */
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000342int add_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000343{
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000344 grab_object( obj );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000345 entry->obj = obj;
346 entry->prev = obj->tail;
347 entry->next = NULL;
348 if (obj->tail) obj->tail->next = entry;
349 else obj->head = entry;
350 obj->tail = entry;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000351 return 1;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000352}
353
354/* remove a thread from an object wait queue */
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000355void remove_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000356{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000357 if (entry->next) entry->next->prev = entry->prev;
358 else obj->tail = entry->prev;
359 if (entry->prev) entry->prev->next = entry->next;
360 else obj->head = entry->next;
361 release_object( obj );
362}
363
364/* finish waiting */
365static void end_wait( struct thread *thread )
366{
367 struct thread_wait *wait = thread->wait;
368 struct wait_queue_entry *entry;
369 int i;
370
371 assert( wait );
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000372 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
373 entry->obj->ops->remove_queue( entry->obj, entry );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000374 if (wait->user) remove_timeout_user( wait->user );
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000375 thread->wait = wait->next;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000376 free( wait );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000377}
378
379/* build the thread wait structure */
Alexandre Julliard462172a2003-04-02 22:48:59 +0000380static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000381{
382 struct thread_wait *wait;
383 struct wait_queue_entry *entry;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000384 int i;
385
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000386 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000387 wait->next = current->wait;
388 wait->thread = current;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000389 wait->count = count;
390 wait->flags = flags;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000391 wait->user = NULL;
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000392 current->wait = wait;
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000393 if (flags & SELECT_TIMEOUT)
394 {
Alexandre Julliard462172a2003-04-02 22:48:59 +0000395 wait->timeout.tv_sec = timeout->sec;
396 wait->timeout.tv_usec = timeout->usec;
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000397 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000398
399 for (i = 0, entry = wait->queues; i < count; i++, entry++)
400 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000401 struct object *obj = objects[i];
402 entry->thread = current;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000403 if (!obj->ops->add_queue( obj, entry ))
404 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000405 wait->count = i;
406 end_wait( current );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000407 return 0;
408 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000409 }
410 return 1;
411}
412
413/* check if the thread waiting condition is satisfied */
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000414static int check_wait( struct thread *thread )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000415{
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000416 int i, signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000417 struct thread_wait *wait = thread->wait;
418 struct wait_queue_entry *entry = wait->queues;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000419
Peter Hunnisetta3c5ad42003-02-28 21:50:47 +0000420 /* Suspended threads may not acquire locks */
421 if( thread->process->suspend + thread->suspend > 0 ) return -1;
422
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000423 assert( wait );
424 if (wait->flags & SELECT_ALL)
425 {
Alexandre Julliard57e11311999-05-16 16:59:38 +0000426 int not_ok = 0;
427 /* Note: we must check them all anyway, as some objects may
428 * want to do something when signaled, even if others are not */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000429 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
Alexandre Julliard57e11311999-05-16 16:59:38 +0000430 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
431 if (not_ok) goto other_checks;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000432 /* Wait satisfied: tell it to all objects */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000433 signaled = 0;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000434 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
435 if (entry->obj->ops->satisfied( entry->obj, thread ))
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000436 signaled = STATUS_ABANDONED_WAIT_0;
437 return signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000438 }
439 else
440 {
441 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
442 {
443 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
444 /* Wait satisfied: tell it to the object */
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000445 signaled = i;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000446 if (entry->obj->ops->satisfied( entry->obj, thread ))
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000447 signaled = i + STATUS_ABANDONED_WAIT_0;
448 return signaled;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000449 }
450 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000451
452 other_checks:
Alexandre Julliard23623802001-01-06 01:48:51 +0000453 if ((wait->flags & SELECT_INTERRUPTIBLE) && thread->system_apc.head) return STATUS_USER_APC;
454 if ((wait->flags & SELECT_ALERTABLE) && thread->user_apc.head) return STATUS_USER_APC;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000455 if (wait->flags & SELECT_TIMEOUT)
456 {
457 struct timeval now;
458 gettimeofday( &now, NULL );
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000459 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000460 }
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000461 return -1;
462}
463
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000464/* send the wakeup signal to a thread */
465static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
466{
467 struct wake_up_reply reply;
468 int ret;
469
470 reply.cookie = cookie;
471 reply.signaled = signaled;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000472 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
473 return 0;
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000474 if (ret >= 0)
475 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
476 else if (errno == EPIPE)
477 kill_thread( thread, 0 ); /* normal death */
478 else
479 fatal_protocol_perror( thread, "write" );
480 return -1;
481}
482
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000483/* attempt to wake up a thread */
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000484/* return >0 if OK, 0 if the wait condition is still not satisfied */
Peter Hunnisetta3c5ad42003-02-28 21:50:47 +0000485int wake_thread( struct thread *thread )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000486{
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000487 int signaled, count;
488 void *cookie;
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000489
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000490 for (count = 0; thread->wait; count++)
491 {
492 if ((signaled = check_wait( thread )) == -1) break;
493
494 cookie = thread->wait->cookie;
Alexandre Julliard91befe12003-02-01 01:38:40 +0000495 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
496 thread->id, signaled, cookie );
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000497 end_wait( thread );
Andreas Mohrd66130a2001-08-07 19:31:43 +0000498 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
499 break;
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000500 }
501 return count;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000502}
503
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000504/* thread wait timeout */
505static void thread_timeout( void *ptr )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000506{
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000507 struct thread_wait *wait = ptr;
508 struct thread *thread = wait->thread;
509 void *cookie = wait->cookie;
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000510
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000511 wait->user = NULL;
512 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000513 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000514
Alexandre Julliard91befe12003-02-01 01:38:40 +0000515 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
516 thread->id, STATUS_TIMEOUT, cookie );
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000517 end_wait( thread );
Alexandre Julliardff732502002-06-22 01:20:36 +0000518 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000519 /* check if other objects have become signaled in the meantime */
520 wake_thread( thread );
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000521}
522
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000523/* select on a list of handles */
Alexandre Julliard51885742002-05-30 20:12:58 +0000524static void select_on( int count, void *cookie, const obj_handle_t *handles,
Alexandre Julliard462172a2003-04-02 22:48:59 +0000525 int flags, const abs_time_t *timeout )
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000526{
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000527 int ret, i;
528 struct object *objects[MAXIMUM_WAIT_OBJECTS];
529
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000530 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
531 {
532 set_error( STATUS_INVALID_PARAMETER );
533 return;
534 }
535 for (i = 0; i < count; i++)
536 {
537 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
538 break;
539 }
540
541 if (i < count) goto done;
Alexandre Julliard462172a2003-04-02 22:48:59 +0000542 if (!wait_on( count, objects, flags, timeout )) goto done;
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000543
544 if ((ret = check_wait( current )) != -1)
545 {
546 /* condition is already satisfied */
547 end_wait( current );
548 set_error( ret );
549 goto done;
550 }
551
Alexandre Julliard57e11311999-05-16 16:59:38 +0000552 /* now we need to wait */
553 if (flags & SELECT_TIMEOUT)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000554 {
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000555 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000556 thread_timeout, current->wait )))
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000557 {
558 end_wait( current );
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000559 goto done;
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000560 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000561 }
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000562 current->wait->cookie = cookie;
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000563 set_error( STATUS_PENDING );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000564
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000565done:
Alexandre Julliard9de03f42000-01-04 02:23:38 +0000566 while (--i >= 0) release_object( objects[i] );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000567}
568
569/* attempt to wake threads sleeping on the object wait queue */
570void wake_up( struct object *obj, int max )
571{
572 struct wait_queue_entry *entry = obj->head;
573
574 while (entry)
575 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000576 struct thread *thread = entry->thread;
577 entry = entry->next;
578 if (wake_thread( thread ))
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000579 {
580 if (max && !--max) break;
581 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000582 }
583}
584
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000585/* queue an async procedure call */
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000586int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
Alexandre Julliard088bcf92003-04-04 22:26:34 +0000587 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000588{
589 struct thread_apc *apc;
Alexandre Julliard23623802001-01-06 01:48:51 +0000590 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000591
592 /* cancel a possible previous APC with the same owner */
Alexandre Julliard23623802001-01-06 01:48:51 +0000593 if (owner) thread_cancel_apc( thread, owner, system );
Alexandre Julliard15979fd2002-03-23 18:50:04 +0000594 if (thread->state == TERMINATED) return 0;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000595
Alexandre Julliard088bcf92003-04-04 22:26:34 +0000596 if (!(apc = mem_alloc( sizeof(*apc) ))) return 0;
597 apc->prev = queue->tail;
598 apc->next = NULL;
599 apc->owner = owner;
600 apc->func = func;
601 apc->type = type;
602 apc->arg1 = arg1;
603 apc->arg2 = arg2;
604 apc->arg3 = arg3;
Alexandre Julliard23623802001-01-06 01:48:51 +0000605 queue->tail = apc;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000606 if (!apc->prev) /* first one */
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000607 {
Alexandre Julliard23623802001-01-06 01:48:51 +0000608 queue->head = apc;
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000609 wake_thread( thread );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000610 }
Martin Wilckb1c45b92002-01-09 19:09:57 +0000611 else apc->prev->next = apc;
612
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000613 return 1;
614}
615
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000616/* cancel the async procedure call owned by a specific object */
Alexandre Julliard23623802001-01-06 01:48:51 +0000617void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000618{
619 struct thread_apc *apc;
Alexandre Julliard23623802001-01-06 01:48:51 +0000620 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
621 for (apc = queue->head; apc; apc = apc->next)
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000622 {
623 if (apc->owner != owner) continue;
624 if (apc->next) apc->next->prev = apc->prev;
Alexandre Julliard23623802001-01-06 01:48:51 +0000625 else queue->tail = apc->prev;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000626 if (apc->prev) apc->prev->next = apc->next;
Alexandre Julliard23623802001-01-06 01:48:51 +0000627 else queue->head = apc->next;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000628 free( apc );
629 return;
630 }
631}
632
633/* remove the head apc from the queue; the returned pointer must be freed by the caller */
Alexandre Julliard23623802001-01-06 01:48:51 +0000634static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000635{
Alexandre Julliard23623802001-01-06 01:48:51 +0000636 struct thread_apc *apc;
637 struct apc_queue *queue = &thread->system_apc;
638
639 if (!queue->head && !system_only) queue = &thread->user_apc;
640 if ((apc = queue->head))
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000641 {
642 if (apc->next) apc->next->prev = NULL;
Alexandre Julliard23623802001-01-06 01:48:51 +0000643 else queue->tail = NULL;
644 queue->head = apc->next;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000645 }
646 return apc;
647}
648
Alexandre Julliardf5242402001-02-28 21:45:23 +0000649/* add an fd to the inflight list */
650/* return list index, or -1 on error */
651int thread_add_inflight_fd( struct thread *thread, int client, int server )
652{
653 int i;
654
655 if (server == -1) return -1;
656 if (client == -1)
657 {
658 close( server );
659 return -1;
660 }
661
662 /* first check if we already have an entry for this fd */
663 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
664 if (thread->inflight[i].client == client)
665 {
666 close( thread->inflight[i].server );
667 thread->inflight[i].server = server;
668 return i;
669 }
670
671 /* now find a free spot to store it */
672 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
673 if (thread->inflight[i].client == -1)
674 {
675 thread->inflight[i].client = client;
676 thread->inflight[i].server = server;
677 return i;
678 }
679 return -1;
680}
681
682/* get an inflight fd and purge it from the list */
683/* the fd must be closed when no longer used */
684int thread_get_inflight_fd( struct thread *thread, int client )
685{
686 int i, ret;
687
688 if (client == -1) return -1;
689
690 do
691 {
692 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
693 {
694 if (thread->inflight[i].client == client)
695 {
696 ret = thread->inflight[i].server;
697 thread->inflight[i].server = thread->inflight[i].client = -1;
698 return ret;
699 }
700 }
701 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
702 return -1;
703}
704
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000705/* retrieve an LDT selector entry */
706static void get_selector_entry( struct thread *thread, int entry,
707 unsigned int *base, unsigned int *limit,
708 unsigned char *flags )
709{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000710 if (!thread->process->ldt_copy)
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000711 {
712 set_error( STATUS_ACCESS_DENIED );
713 return;
714 }
715 if (entry >= 8192)
716 {
717 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
718 return;
719 }
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000720 if (suspend_for_ptrace( thread ))
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000721 {
722 unsigned char flags_buf[4];
Alexandre Julliard914406f2000-11-14 01:54:49 +0000723 int *addr = (int *)thread->process->ldt_copy + entry;
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000724 if (read_thread_int( thread, addr, base ) == -1) goto done;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000725 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
726 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000727 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
728 *flags = flags_buf[entry & 3];
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000729 done:
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000730 resume_after_ptrace( thread );
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000731 }
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000732}
733
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000734/* kill a thread on the spot */
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000735void kill_thread( struct thread *thread, int violent_death )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000736{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000737 if (thread->state == TERMINATED) return; /* already killed */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000738 thread->state = TERMINATED;
Ryan Cumming24f4ece2002-11-25 01:33:38 +0000739 thread->exit_time = time(NULL);
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000740 if (current == thread) current = NULL;
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000741 if (debug_level)
Alexandre Julliard91befe12003-02-01 01:38:40 +0000742 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
743 thread->id, thread->exit_code );
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000744 if (thread->wait)
745 {
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000746 while (thread->wait) end_wait( thread );
747 send_thread_wakeup( thread, NULL, STATUS_PENDING );
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000748 /* if it is waiting on the socket, we don't need to send a SIGTERM */
749 violent_death = 0;
750 }
Eric Pouech3940d8a2001-12-04 20:17:43 +0000751 kill_console_processes( thread, 0 );
Alexandre Julliardff81d782000-03-08 12:01:30 +0000752 debug_exit_thread( thread );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000753 abandon_mutexes( thread );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000754 remove_process_thread( thread->process, thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000755 wake_up( &thread->obj, 0 );
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000756 detach_thread( thread, violent_death ? SIGTERM : 0 );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000757 cleanup_thread( thread );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000758 release_object( thread );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000759}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000760
Alexandre Julliard07d84462000-04-16 19:45:05 +0000761/* take a snapshot of currently running threads */
762struct thread_snapshot *thread_snap( int *count )
763{
764 struct thread_snapshot *snapshot, *ptr;
765 struct thread *thread;
766 int total = 0;
767
768 for (thread = first_thread; thread; thread = thread->next)
769 if (thread->state != TERMINATED) total++;
770 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
771 ptr = snapshot;
772 for (thread = first_thread; thread; thread = thread->next)
773 {
774 if (thread->state == TERMINATED) continue;
775 ptr->thread = thread;
776 ptr->count = thread->obj.refcount;
777 ptr->priority = thread->priority;
778 grab_object( thread );
779 ptr++;
780 }
781 *count = total;
782 return snapshot;
783}
784
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000785/* signal that we are finished booting on the client side */
786DECL_HANDLER(boot_done)
787{
Alexandre Julliardf6507ed2000-04-06 22:05:16 +0000788 debug_level = max( debug_level, req->debug_level );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000789 if (current == booting_thread)
790 {
791 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
792 lock_master_socket(0); /* allow other clients now */
793 }
794}
795
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000796/* create a new thread */
797DECL_HANDLER(new_thread)
798{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000799 struct thread *thread;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000800 int request_fd = thread_get_inflight_fd( current, req->request_fd );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000801
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000802 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000803 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000804 if (request_fd != -1) close( request_fd );
Alexandre Julliard8859d772001-03-01 22:13:49 +0000805 set_error( STATUS_INVALID_HANDLE );
806 return;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000807 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000808
Alexandre Julliard8859d772001-03-01 22:13:49 +0000809 if ((thread = create_thread( request_fd, current->process )))
810 {
811 if (req->suspend) thread->suspend++;
Alexandre Julliard54f22872002-10-03 19:54:57 +0000812 reply->tid = get_thread_id( thread );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000813 if ((reply->handle = alloc_handle( current->process, thread,
814 THREAD_ALL_ACCESS, req->inherit )))
Alexandre Julliard8859d772001-03-01 22:13:49 +0000815 {
816 /* thread object will be released when the thread gets killed */
817 return;
818 }
819 kill_thread( thread, 1 );
Alexandre Julliard8859d772001-03-01 22:13:49 +0000820 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000821}
822
823/* initialize a new thread */
824DECL_HANDLER(init_thread)
825{
Alexandre Julliard8859d772001-03-01 22:13:49 +0000826 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
827 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
828
Alexandre Julliard02a53c12003-02-25 04:17:22 +0000829 if (current->unix_pid != -1)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000830 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000831 fatal_protocol_error( current, "init_thread: already running\n" );
Alexandre Julliard8859d772001-03-01 22:13:49 +0000832 goto error;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000833 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000834 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
Alexandre Julliard8859d772001-03-01 22:13:49 +0000835 {
836 fatal_protocol_error( current, "bad reply fd\n" );
837 goto error;
838 }
839 if (wait_fd == -1)
840 {
841 fatal_protocol_error( current, "bad wait fd\n" );
842 goto error;
843 }
Alexandre Julliard3c68ab02003-10-27 22:10:22 +0000844 if (!(current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj )))
845 {
846 reply_fd = -1;
847 fatal_protocol_error( current, "could not allocate reply fd\n" );
848 goto error;
849 }
850 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj )))
851 return;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000852
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000853 current->unix_pid = req->unix_pid;
Alexandre Julliarda8497bd2003-03-22 21:00:09 +0000854 current->unix_tid = req->unix_tid;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000855 current->teb = req->teb;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000856
Alexandre Julliard0a707831999-11-08 05:31:47 +0000857 if (current->suspend + current->process->suspend > 0) stop_thread( current );
Alexandre Julliardff81d782000-03-08 12:01:30 +0000858 if (current->process->running_threads > 1)
Alexandre Julliardb73421d2000-03-30 19:30:24 +0000859 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
Alexandre Julliard8859d772001-03-01 22:13:49 +0000860
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000861 reply->pid = get_process_id( current->process );
862 reply->tid = get_thread_id( current );
863 reply->boot = (current == booting_thread);
864 reply->version = SERVER_PROTOCOL_VERSION;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000865 return;
866
867 error:
868 if (reply_fd != -1) close( reply_fd );
869 if (wait_fd != -1) close( wait_fd );
870}
871
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000872/* terminate a thread */
873DECL_HANDLER(terminate_thread)
874{
875 struct thread *thread;
876
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000877 reply->self = 0;
878 reply->last = 0;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000879 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
880 {
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000881 thread->exit_code = req->exit_code;
882 if (thread != current) kill_thread( thread, 1 );
883 else
884 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000885 reply->self = 1;
886 reply->last = (thread->process->running_threads == 1);
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000887 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000888 release_object( thread );
889 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000890}
891
Chris Morgan417296c2002-04-02 00:49:05 +0000892/* open a handle to a thread */
893DECL_HANDLER(open_thread)
894{
895 struct thread *thread = get_thread_from_id( req->tid );
896
897 reply->handle = 0;
898 if (thread)
899 {
900 reply->handle = alloc_handle( current->process, thread, req->access, req->inherit );
901 release_object( thread );
902 }
903}
904
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000905/* fetch information about a thread */
906DECL_HANDLER(get_thread_info)
907{
908 struct thread *thread;
Alexandre Julliard51885742002-05-30 20:12:58 +0000909 obj_handle_t handle = req->handle;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000910
Alexandre Julliard8081e5a2001-01-05 04:08:07 +0000911 if (!handle) thread = get_thread_from_id( req->tid_in );
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000912 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
913
914 if (thread)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000915 {
Alexandre Julliard4f196ea2003-07-09 02:57:57 +0000916 reply->pid = get_process_id( thread->process );
Ryan Cumming24f4ece2002-11-25 01:33:38 +0000917 reply->tid = get_thread_id( thread );
918 reply->teb = thread->teb;
919 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
920 reply->priority = thread->priority;
Alexandre Julliard4f196ea2003-07-09 02:57:57 +0000921 reply->affinity = thread->affinity;
Ryan Cumming24f4ece2002-11-25 01:33:38 +0000922 reply->creation_time = thread->creation_time;
923 reply->exit_time = thread->exit_time;
924
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000925 release_object( thread );
926 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000927}
928
929/* set information about a thread */
930DECL_HANDLER(set_thread_info)
931{
932 struct thread *thread;
933
934 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
935 {
936 set_thread_info( thread, req );
937 release_object( thread );
938 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000939}
940
941/* suspend a thread */
942DECL_HANDLER(suspend_thread)
943{
944 struct thread *thread;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000945
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000946 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
947 {
François Gougetf6662572002-04-02 02:53:45 +0000948 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000949 else reply->count = suspend_thread( thread );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000950 release_object( thread );
951 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000952}
953
954/* resume a thread */
955DECL_HANDLER(resume_thread)
956{
957 struct thread *thread;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000958
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000959 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
960 {
François Gougetf6662572002-04-02 02:53:45 +0000961 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
962 else reply->count = resume_thread( thread );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000963 release_object( thread );
964 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000965}
966
967/* select on a handle list */
968DECL_HANDLER(select)
969{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000970 int count = get_req_data_size() / sizeof(int);
Alexandre Julliard462172a2003-04-02 22:48:59 +0000971 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000972}
973
974/* queue an APC for a thread */
975DECL_HANDLER(queue_apc)
976{
977 struct thread *thread;
978 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
979 {
Alexandre Julliard088bcf92003-04-04 22:26:34 +0000980 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
981 req->arg1, req->arg2, req->arg3 );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000982 release_object( thread );
983 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000984}
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000985
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000986/* get next APC to call */
987DECL_HANDLER(get_apc)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000988{
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000989 struct thread_apc *apc;
990
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000991 for (;;)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000992 {
Alexandre Julliard23623802001-01-06 01:48:51 +0000993 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000994 {
995 /* no more APCs */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000996 reply->func = NULL;
997 reply->type = APC_NONE;
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000998 return;
999 }
1000 /* Optimization: ignore APCs that have a NULL func; they are only used
1001 * to wake up a thread, but since we got here the thread woke up already.
Martin Wilck2b47fb32002-04-05 22:53:57 +00001002 * Exception: for APC_ASYNC_IO, func == NULL is legal.
Alexandre Julliard9c2370b2000-08-30 00:00:48 +00001003 */
Martin Wilck2b47fb32002-04-05 22:53:57 +00001004 if (apc->func || apc->type == APC_ASYNC_IO) break;
Alexandre Julliardea1afce2000-08-22 20:08:37 +00001005 free( apc );
1006 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001007 reply->func = apc->func;
1008 reply->type = apc->type;
Alexandre Julliard088bcf92003-04-04 22:26:34 +00001009 reply->arg1 = apc->arg1;
1010 reply->arg2 = apc->arg2;
1011 reply->arg3 = apc->arg3;
Alexandre Julliard9c2370b2000-08-30 00:00:48 +00001012 free( apc );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +00001013}
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +00001014
1015/* fetch a selector entry for a thread */
1016DECL_HANDLER(get_selector_entry)
1017{
1018 struct thread *thread;
1019 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1020 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001021 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +00001022 release_object( thread );
1023 }
1024}