2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
15 #ifdef HAVE_SYS_MMAN_H
18 #include <sys/types.h>
35 struct wait_queue_entry
37 struct wait_queue_entry *next;
38 struct wait_queue_entry *prev;
40 struct thread *thread;
45 int count; /* count of objects */
47 struct timeval timeout;
48 struct timeout_user *user;
49 struct wait_queue_entry queues[1];
52 /* asynchronous procedure calls */
56 void *func; /* function to call in client */
57 void *param; /* function param */
59 #define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */
62 /* thread operations */
64 static void dump_thread( struct object *obj, int verbose );
65 static int thread_signaled( struct object *obj, struct thread *thread );
66 extern void thread_poll_event( struct object *obj, int event );
67 static void destroy_thread( struct object *obj );
69 static const struct object_ops thread_ops =
71 sizeof(struct thread), /* size */
72 dump_thread, /* dump */
73 add_queue, /* add_queue */
74 remove_queue, /* remove_queue */
75 thread_signaled, /* signaled */
76 no_satisfied, /* satisfied */
77 NULL, /* get_poll_events */
78 thread_poll_event, /* poll_event */
79 no_read_fd, /* get_read_fd */
80 no_write_fd, /* get_write_fd */
82 no_get_file_info, /* get_file_info */
83 destroy_thread /* destroy */
86 static struct thread *first_thread;
88 /* allocate the buffer for the communication with the client */
89 static int alloc_client_buffer( struct thread *thread )
93 if ((fd = create_anonymous_file()) == -1) return -1;
94 if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error;
95 if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE,
96 MAP_SHARED, fd, 0 )) == (void*)-1) goto error;
101 if (fd != -1) close( fd );
105 /* create a new thread */
106 static struct thread *create_thread( int fd, struct process *process, int suspend )
108 struct thread *thread;
111 int flags = fcntl( fd, F_GETFL, 0 );
112 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
114 if (!(thread = alloc_object( &thread_ops, fd ))) return NULL;
116 thread->unix_pid = 0; /* not known yet */
118 thread->mutex = NULL;
119 thread->debug_ctx = NULL;
120 thread->debug_event = NULL;
121 thread->exit_event = NULL;
124 thread->apc_count = 0;
126 thread->pass_fd = -1;
127 thread->state = RUNNING;
128 thread->attached = 0;
129 thread->exit_code = 0x103; /* STILL_ACTIVE */
132 thread->priority = THREAD_PRIORITY_NORMAL;
133 thread->affinity = 1;
134 thread->suspend = (suspend != 0);
135 thread->buffer = (void *)-1;
136 thread->last_req = REQ_GET_THREAD_BUFFER;
138 if (!first_thread) /* creating the first thread */
141 thread->process = process = create_initial_process();
144 else thread->process = (struct process *)grab_object( process );
146 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
147 first_thread = thread;
148 add_process_thread( process, thread );
150 if ((buf_fd = alloc_client_buffer( thread )) == -1) goto error;
152 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
153 set_reply_fd( thread, buf_fd ); /* send the fd to the client */
154 send_reply( thread );
158 remove_process_thread( process, thread );
159 release_object( thread );
163 /* create the initial thread and start the main server loop */
164 void create_initial_thread( int fd )
166 create_thread( fd, NULL, 0 );
170 /* handle a client event */
171 void thread_poll_event( struct object *obj, int event )
173 struct thread *thread = (struct thread *)obj;
174 assert( obj->ops == &thread_ops );
176 if (event & (POLLERR | POLLHUP)) kill_thread( thread, BROKEN_PIPE );
179 if (event & POLLOUT) write_request( thread );
180 if (event & POLLIN) read_request( thread );
184 /* destroy a thread when its refcount is 0 */
185 static void destroy_thread( struct object *obj )
187 struct thread *thread = (struct thread *)obj;
188 assert( obj->ops == &thread_ops );
190 assert( !thread->debug_ctx ); /* cannot still be debugging something */
191 release_object( thread->process );
192 if (thread->next) thread->next->prev = thread->prev;
193 if (thread->prev) thread->prev->next = thread->next;
194 else first_thread = thread->next;
195 if (thread->apc) free( thread->apc );
196 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
197 if (thread->pass_fd != -1) close( thread->pass_fd );
200 /* dump a thread on stdout for debugging purposes */
201 static void dump_thread( struct object *obj, int verbose )
203 struct thread *thread = (struct thread *)obj;
204 assert( obj->ops == &thread_ops );
206 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
207 thread->unix_pid, thread->teb, thread->state );
210 static int thread_signaled( struct object *obj, struct thread *thread )
212 struct thread *mythread = (struct thread *)obj;
213 return (mythread->state == TERMINATED);
216 /* get a thread pointer from a thread id (and increment the refcount) */
217 struct thread *get_thread_from_id( void *id )
219 struct thread *t = first_thread;
220 while (t && (t != id)) t = t->next;
221 if (t) grab_object( t );
225 /* get a thread from a handle (and increment the refcount) */
226 struct thread *get_thread_from_handle( int handle, unsigned int access )
228 return (struct thread *)get_handle_obj( current->process, handle,
229 access, &thread_ops );
232 /* find a thread from a Unix pid */
233 struct thread *get_thread_from_pid( int pid )
235 struct thread *t = first_thread;
236 while (t && (t->unix_pid != pid)) t = t->next;
240 /* set all information about a thread */
241 static void set_thread_info( struct thread *thread,
242 struct set_thread_info_request *req )
244 if (req->mask & SET_THREAD_INFO_PRIORITY)
245 thread->priority = req->priority;
246 if (req->mask & SET_THREAD_INFO_AFFINITY)
248 if (req->affinity != 1) set_error( ERROR_INVALID_PARAMETER );
249 else thread->affinity = req->affinity;
253 /* suspend a thread */
254 int suspend_thread( struct thread *thread, int check_limit )
256 int old_count = thread->suspend;
257 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
259 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
261 else set_error( ERROR_SIGNAL_REFUSED );
265 /* resume a thread */
266 int resume_thread( struct thread *thread )
268 int old_count = thread->suspend;
269 if (thread->suspend > 0)
271 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
276 /* suspend all threads but the current */
277 void suspend_all_threads( void )
279 struct thread *thread;
280 for ( thread = first_thread; thread; thread = thread->next )
281 if ( thread != current )
282 suspend_thread( thread, 0 );
285 /* resume all threads but the current */
286 void resume_all_threads( void )
288 struct thread *thread;
289 for ( thread = first_thread; thread; thread = thread->next )
290 if ( thread != current )
291 resume_thread( thread );
294 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
295 int add_queue( struct object *obj, struct wait_queue_entry *entry )
299 entry->prev = obj->tail;
301 if (obj->tail) obj->tail->next = entry;
302 else obj->head = entry;
307 /* remove a thread from an object wait queue */
308 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
310 if (entry->next) entry->next->prev = entry->prev;
311 else obj->tail = entry->prev;
312 if (entry->prev) entry->prev->next = entry->next;
313 else obj->head = entry->next;
314 release_object( obj );
318 static void end_wait( struct thread *thread )
320 struct thread_wait *wait = thread->wait;
321 struct wait_queue_entry *entry;
325 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
326 entry->obj->ops->remove_queue( entry->obj, entry );
327 if (wait->user) remove_timeout_user( wait->user );
332 /* build the thread wait structure */
333 static int wait_on( struct thread *thread, int count,
334 int *handles, int flags, int timeout )
336 struct thread_wait *wait;
337 struct wait_queue_entry *entry;
341 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
343 set_error( ERROR_INVALID_PARAMETER );
346 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
351 if (flags & SELECT_TIMEOUT)
353 gettimeofday( &wait->timeout, 0 );
354 add_timeout( &wait->timeout, timeout );
357 for (i = 0, entry = wait->queues; i < count; i++, entry++)
359 if (!(obj = get_handle_obj( thread->process, handles[i],
360 SYNCHRONIZE, NULL )))
366 entry->thread = thread;
367 if (!obj->ops->add_queue( obj, entry ))
373 release_object( obj );
378 /* check if the thread waiting condition is satisfied */
379 static int check_wait( struct thread *thread, int *signaled )
382 struct thread_wait *wait = thread->wait;
383 struct wait_queue_entry *entry = wait->queues;
386 if (wait->flags & SELECT_ALL)
389 /* Note: we must check them all anyway, as some objects may
390 * want to do something when signaled, even if others are not */
391 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
392 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
393 if (not_ok) goto other_checks;
394 /* Wait satisfied: tell it to all objects */
396 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
397 if (entry->obj->ops->satisfied( entry->obj, thread ))
398 *signaled = STATUS_ABANDONED_WAIT_0;
403 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
405 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
406 /* Wait satisfied: tell it to the object */
408 if (entry->obj->ops->satisfied( entry->obj, thread ))
409 *signaled = i + STATUS_ABANDONED_WAIT_0;
415 if ((wait->flags & SELECT_ALERTABLE) && thread->apc)
417 *signaled = STATUS_USER_APC;
420 if (wait->flags & SELECT_TIMEOUT)
423 gettimeofday( &now, NULL );
424 if (!time_before( &now, &wait->timeout ))
426 *signaled = STATUS_TIMEOUT;
433 /* attempt to wake up a thread */
434 /* return 1 if OK, 0 if the wait condition is still not satisfied */
435 static int wake_thread( struct thread *thread )
437 struct select_request *req = get_req_ptr( thread );
439 if (!check_wait( thread, &req->signaled )) return 0;
444 /* sleep on a list of objects */
445 static void sleep_on( struct thread *thread, int count, int *handles, int flags, int timeout )
447 struct select_request *req;
448 assert( !thread->wait );
449 if (!wait_on( thread, count, handles, flags, timeout )) goto error;
450 if (wake_thread( thread )) return;
451 /* now we need to wait */
452 if (flags & SELECT_TIMEOUT)
454 if (!(thread->wait->user = add_timeout_user( &thread->wait->timeout,
455 call_timeout_handler, thread )))
458 thread->state = SLEEPING;
462 req = get_req_ptr( thread );
466 /* timeout for the current thread */
467 void thread_timeout(void)
469 struct select_request *req = get_req_ptr( current );
471 assert( current->wait );
472 current->wait->user = NULL;
474 req->signaled = STATUS_TIMEOUT;
475 send_reply( current );
478 /* attempt to wake threads sleeping on the object wait queue */
479 void wake_up( struct object *obj, int max )
481 struct wait_queue_entry *entry = obj->head;
485 struct thread *thread = entry->thread;
487 if (wake_thread( thread ))
489 send_reply( thread );
490 if (max && !--max) break;
495 /* queue an async procedure call */
496 static int thread_queue_apc( struct thread *thread, void *func, void *param )
498 struct thread_apc *apc;
501 if (!(thread->apc = mem_alloc( MAX_THREAD_APC * sizeof(*apc) )))
503 thread->apc_count = 0;
505 else if (thread->apc_count >= MAX_THREAD_APC) return 0;
506 thread->apc[thread->apc_count].func = func;
507 thread->apc[thread->apc_count].param = param;
511 if (wake_thread( thread )) send_reply( thread );
516 /* kill a thread on the spot */
517 void kill_thread( struct thread *thread, int exit_code )
519 if (thread->state == TERMINATED) return; /* already killed */
520 thread->state = TERMINATED;
521 thread->exit_code = exit_code;
522 if (current == thread) current = NULL;
523 if (debug_level) trace_kill( thread );
524 if (thread->wait) end_wait( thread );
525 debug_exit_thread( thread, exit_code );
526 abandon_mutexes( thread );
527 remove_process_thread( thread->process, thread );
528 wake_up( &thread->obj, 0 );
529 detach_thread( thread );
530 remove_select_user( &thread->obj );
531 release_object( thread );
534 /* create a new thread */
535 DECL_HANDLER(new_thread)
537 struct thread *thread;
538 struct process *process;
540 if ((process = get_process_from_id( req->pid )))
542 if ((fd = dup(fd)) != -1)
544 if ((thread = create_thread( fd, process, req->suspend )))
547 if ((req->handle = alloc_handle( current->process, thread,
548 THREAD_ALL_ACCESS, req->inherit )) == -1)
549 release_object( thread );
550 /* else will be released when the thread gets killed */
554 else file_set_error();
555 release_object( process );
559 /* retrieve the thread buffer file descriptor */
560 DECL_HANDLER(get_thread_buffer)
562 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
565 /* initialize a new thread */
566 DECL_HANDLER(init_thread)
568 if (current->unix_pid)
570 fatal_protocol_error( current, "init_thread: already running\n" );
573 current->unix_pid = req->unix_pid;
574 current->teb = req->teb;
575 if (current->suspend + current->process->suspend > 0) stop_thread( current );
576 req->pid = current->process;
580 /* terminate a thread */
581 DECL_HANDLER(terminate_thread)
583 struct thread *thread;
585 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
587 kill_thread( thread, req->exit_code );
588 release_object( thread );
592 /* fetch information about a thread */
593 DECL_HANDLER(get_thread_info)
595 struct thread *thread;
597 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
600 req->exit_code = thread->exit_code;
601 req->priority = thread->priority;
602 release_object( thread );
606 /* set information about a thread */
607 DECL_HANDLER(set_thread_info)
609 struct thread *thread;
611 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
613 set_thread_info( thread, req );
614 release_object( thread );
618 /* suspend a thread */
619 DECL_HANDLER(suspend_thread)
621 struct thread *thread;
623 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
625 req->count = suspend_thread( thread, 1 );
626 release_object( thread );
630 /* resume a thread */
631 DECL_HANDLER(resume_thread)
633 struct thread *thread;
635 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
637 req->count = resume_thread( thread );
638 release_object( thread );
642 /* select on a handle list */
645 sleep_on( current, req->count, req->handles, req->flags, req->timeout );
648 /* queue an APC for a thread */
649 DECL_HANDLER(queue_apc)
651 struct thread *thread;
652 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
654 thread_queue_apc( thread, req->func, req->param );
655 release_object( thread );
659 /* get list of APC to call */
660 DECL_HANDLER(get_apcs)
662 if ((req->count = current->apc_count))
664 memcpy( req->apcs, current->apc, current->apc_count * sizeof(*current->apc) );
665 free( current->apc );
667 current->apc_count = 0;