2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
15 #ifdef HAVE_SYS_MMAN_H
18 #include <sys/types.h>
19 #ifdef HAVE_SYS_SOCKET_H
20 # include <sys/socket.h>
37 struct wait_queue_entry
39 struct wait_queue_entry *next;
40 struct wait_queue_entry *prev;
42 struct thread *thread;
47 int count; /* count of objects */
49 struct timeval timeout;
50 struct timeout_user *user;
51 sleep_reply reply; /* function to build the reply */
52 struct wait_queue_entry queues[1];
55 /* asynchronous procedure calls */
59 void *func; /* function to call in client */
60 void *param; /* function param */
62 #define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */
65 /* thread operations */
67 static void dump_thread( struct object *obj, int verbose );
68 static int thread_signaled( struct object *obj, struct thread *thread );
69 extern void thread_poll_event( struct object *obj, int event );
70 static void destroy_thread( struct object *obj );
72 static const struct object_ops thread_ops =
74 sizeof(struct thread), /* size */
75 dump_thread, /* dump */
76 add_queue, /* add_queue */
77 remove_queue, /* remove_queue */
78 thread_signaled, /* signaled */
79 no_satisfied, /* satisfied */
80 NULL, /* get_poll_events */
81 thread_poll_event, /* poll_event */
82 no_read_fd, /* get_read_fd */
83 no_write_fd, /* get_write_fd */
85 no_get_file_info, /* get_file_info */
86 destroy_thread /* destroy */
89 static struct thread *first_thread;
90 static struct thread *booting_thread;
92 /* allocate the buffer for the communication with the client */
93 static int alloc_client_buffer( struct thread *thread )
95 struct get_thread_buffer_request *req;
98 if ((fd = create_anonymous_file()) == -1) return -1;
99 if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error;
100 if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE,
101 MAP_SHARED, fd, 0 )) == (void*)-1) goto error;
102 /* build the first request into the buffer and send it */
103 req = thread->buffer;
104 req->pid = get_process_id( thread->process );
105 req->tid = get_thread_id( thread );
106 req->boot = (thread == booting_thread);
107 req->version = SERVER_PROTOCOL_VERSION;
108 set_reply_fd( thread, fd );
109 send_reply( thread );
114 if (fd != -1) close( fd );
118 /* create a new thread */
119 struct thread *create_thread( int fd, struct process *process, int suspend )
121 struct thread *thread;
123 int flags = fcntl( fd, F_GETFL, 0 );
124 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
126 if (!(thread = alloc_object( &thread_ops, fd ))) return NULL;
128 thread->unix_pid = 0; /* not known yet */
129 thread->context = NULL;
131 thread->mutex = NULL;
132 thread->debug_ctx = NULL;
135 thread->apc_count = 0;
137 thread->pass_fd = -1;
138 thread->state = RUNNING;
139 thread->attached = 0;
140 thread->exit_code = STILL_ACTIVE;
143 thread->priority = THREAD_PRIORITY_NORMAL;
144 thread->affinity = 1;
145 thread->suspend = (suspend != 0);
146 thread->buffer = (void *)-1;
147 thread->last_req = REQ_GET_THREAD_BUFFER;
148 thread->process = (struct process *)grab_object( process );
150 if (!current) current = thread;
152 if (!booting_thread) /* first thread ever */
154 booting_thread = thread;
155 lock_master_socket(1);
158 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
159 first_thread = thread;
160 add_process_thread( process, thread );
162 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
163 if (!alloc_client_buffer( thread )) goto error;
167 remove_process_thread( process, thread );
168 release_object( thread );
172 /* handle a client event */
173 void thread_poll_event( struct object *obj, int event )
175 struct thread *thread = (struct thread *)obj;
176 assert( obj->ops == &thread_ops );
178 if (event & (POLLERR | POLLHUP)) kill_thread( thread, BROKEN_PIPE );
181 if (event & POLLOUT) write_request( thread );
182 if (event & POLLIN) read_request( thread );
186 /* destroy a thread when its refcount is 0 */
187 static void destroy_thread( struct object *obj )
189 struct thread *thread = (struct thread *)obj;
190 assert( obj->ops == &thread_ops );
192 assert( !thread->debug_ctx ); /* cannot still be debugging something */
193 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;
197 if (thread->apc) free( thread->apc );
198 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
199 if (thread->pass_fd != -1) close( thread->pass_fd );
202 /* dump a thread on stdout for debugging purposes */
203 static void dump_thread( struct object *obj, int verbose )
205 struct thread *thread = (struct thread *)obj;
206 assert( obj->ops == &thread_ops );
208 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
209 thread->unix_pid, thread->teb, thread->state );
212 static int thread_signaled( struct object *obj, struct thread *thread )
214 struct thread *mythread = (struct thread *)obj;
215 return (mythread->state == TERMINATED);
218 /* get a thread pointer from a thread id (and increment the refcount) */
219 struct thread *get_thread_from_id( void *id )
221 struct thread *t = first_thread;
222 while (t && (t != id)) t = t->next;
223 if (t) grab_object( t );
227 /* get a thread from a handle (and increment the refcount) */
228 struct thread *get_thread_from_handle( int handle, unsigned int access )
230 return (struct thread *)get_handle_obj( current->process, handle,
231 access, &thread_ops );
234 /* find a thread from a Unix pid */
235 struct thread *get_thread_from_pid( int pid )
237 struct thread *t = first_thread;
238 while (t && (t->unix_pid != pid)) t = t->next;
242 /* set all information about a thread */
243 static void set_thread_info( struct thread *thread,
244 struct set_thread_info_request *req )
246 if (req->mask & SET_THREAD_INFO_PRIORITY)
247 thread->priority = req->priority;
248 if (req->mask & SET_THREAD_INFO_AFFINITY)
250 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
251 else thread->affinity = req->affinity;
255 /* suspend a thread */
256 int suspend_thread( struct thread *thread, int check_limit )
258 int old_count = thread->suspend;
259 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
261 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
263 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
267 /* resume a thread */
268 int resume_thread( struct thread *thread )
270 int old_count = thread->suspend;
271 if (thread->suspend > 0)
273 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
278 /* suspend all threads but the current */
279 void suspend_all_threads( void )
281 struct thread *thread;
282 for ( thread = first_thread; thread; thread = thread->next )
283 if ( thread != current )
284 suspend_thread( thread, 0 );
287 /* resume all threads but the current */
288 void resume_all_threads( void )
290 struct thread *thread;
291 for ( thread = first_thread; thread; thread = thread->next )
292 if ( thread != current )
293 resume_thread( thread );
296 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
297 int add_queue( struct object *obj, struct wait_queue_entry *entry )
301 entry->prev = obj->tail;
303 if (obj->tail) obj->tail->next = entry;
304 else obj->head = entry;
309 /* remove a thread from an object wait queue */
310 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
312 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 );
320 static void end_wait( struct thread *thread )
322 struct thread_wait *wait = thread->wait;
323 struct wait_queue_entry *entry;
327 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
328 entry->obj->ops->remove_queue( entry->obj, entry );
329 if (wait->user) remove_timeout_user( wait->user );
334 /* build the thread wait structure */
335 static int wait_on( int count, struct object *objects[], int flags,
336 int timeout, sleep_reply func )
338 struct thread_wait *wait;
339 struct wait_queue_entry *entry;
342 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
343 current->wait = wait;
348 if (flags & SELECT_TIMEOUT)
350 gettimeofday( &wait->timeout, 0 );
351 add_timeout( &wait->timeout, timeout );
354 for (i = 0, entry = wait->queues; i < count; i++, entry++)
356 struct object *obj = objects[i];
357 entry->thread = current;
358 if (!obj->ops->add_queue( obj, entry ))
368 /* check if the thread waiting condition is satisfied */
369 static int check_wait( struct thread *thread, struct object **object )
372 struct thread_wait *wait = thread->wait;
373 struct wait_queue_entry *entry = wait->queues;
377 if (wait->flags & SELECT_ALL)
380 /* Note: we must check them all anyway, as some objects may
381 * want to do something when signaled, even if others are not */
382 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
383 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
384 if (not_ok) goto other_checks;
385 /* Wait satisfied: tell it to all objects */
387 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
388 if (entry->obj->ops->satisfied( entry->obj, thread ))
389 signaled = STATUS_ABANDONED_WAIT_0;
394 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
396 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
397 /* Wait satisfied: tell it to the object */
399 *object = entry->obj;
400 if (entry->obj->ops->satisfied( entry->obj, thread ))
401 signaled = i + STATUS_ABANDONED_WAIT_0;
407 if ((wait->flags & SELECT_ALERTABLE) && thread->apc) return STATUS_USER_APC;
408 if (wait->flags & SELECT_TIMEOUT)
411 gettimeofday( &now, NULL );
412 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
417 /* build a reply to the select request */
418 static void build_select_reply( struct thread *thread, struct object *obj, int signaled )
420 struct select_request *req = get_req_ptr( thread );
421 req->signaled = signaled;
424 /* attempt to wake up a thread */
425 /* return 1 if OK, 0 if the wait condition is still not satisfied */
426 static int wake_thread( struct thread *thread )
429 struct object *object;
430 if ((signaled = check_wait( thread, &object )) == -1) return 0;
432 thread->wait->reply( thread, object, signaled );
437 /* thread wait timeout */
438 static void thread_timeout( void *ptr )
440 struct thread *thread = ptr;
441 if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread );
442 assert( thread->wait );
444 thread->wait->user = NULL;
445 thread->wait->reply( thread, NULL, STATUS_TIMEOUT );
447 send_reply( thread );
450 /* sleep on a list of objects */
451 int sleep_on( int count, struct object *objects[], int flags, int timeout, sleep_reply func )
453 assert( !current->wait );
454 if (!wait_on( count, objects, flags, timeout, func )) return 0;
455 if (wake_thread( current )) return 1;
456 /* now we need to wait */
457 if (flags & SELECT_TIMEOUT)
459 if (!(current->wait->user = add_timeout_user( ¤t->wait->timeout,
460 thread_timeout, current )))
469 /* select on a list of handles */
470 static int select_on( int count, int *handles, int flags, int timeout )
474 struct object *objects[MAXIMUM_WAIT_OBJECTS];
476 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
478 set_error( STATUS_INVALID_PARAMETER );
481 for (i = 0; i < count; i++)
483 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
486 if (i == count) ret = sleep_on( count, objects, flags, timeout, build_select_reply );
487 while (--i >= 0) release_object( objects[i] );
491 /* attempt to wake threads sleeping on the object wait queue */
492 void wake_up( struct object *obj, int max )
494 struct wait_queue_entry *entry = obj->head;
498 struct thread *thread = entry->thread;
500 if (wake_thread( thread ))
502 send_reply( thread );
503 if (max && !--max) break;
508 /* queue an async procedure call */
509 static int thread_queue_apc( struct thread *thread, void *func, void *param )
511 struct thread_apc *apc;
514 if (!(thread->apc = mem_alloc( MAX_THREAD_APC * sizeof(*apc) )))
516 thread->apc_count = 0;
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;
524 if (wake_thread( thread )) send_reply( thread );
529 /* retrieve an LDT selector entry */
530 static void get_selector_entry( struct thread *thread, int entry,
531 unsigned int *base, unsigned int *limit,
532 unsigned char *flags )
534 if (!thread->process->ldt_copy || !thread->process->ldt_flags)
536 set_error( STATUS_ACCESS_DENIED );
541 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
544 suspend_thread( thread, 0 );
545 if (thread->attached)
547 unsigned char flags_buf[4];
548 int *addr = (int *)thread->process->ldt_copy + 2 * entry;
549 if (read_thread_int( thread, addr, base ) == -1) goto done;
550 if (read_thread_int( thread, addr + 1, limit ) == -1) goto done;
551 addr = (int *)thread->process->ldt_flags + (entry >> 2);
552 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
553 *flags = flags_buf[entry & 3];
555 else set_error( STATUS_ACCESS_DENIED );
557 resume_thread( thread );
560 /* kill a thread on the spot */
561 void kill_thread( struct thread *thread, int exit_code )
563 if (thread->state == TERMINATED) return; /* already killed */
564 thread->state = TERMINATED;
565 thread->exit_code = exit_code;
566 if (current == thread) current = NULL;
568 fprintf( stderr,"%08x: *killed* exit_code=%d\n", (unsigned int)thread, exit_code );
569 if (thread->wait) end_wait( thread );
570 debug_exit_thread( thread );
571 abandon_mutexes( thread );
572 remove_process_thread( thread->process, thread );
573 wake_up( &thread->obj, 0 );
574 detach_thread( thread );
575 remove_select_user( &thread->obj );
576 release_object( thread );
579 /* signal that we are finished booting on the client side */
580 DECL_HANDLER(boot_done)
582 debug_level = req->debug_level;
583 /* Make sure last_req is initialized */
584 current->last_req = REQ_BOOT_DONE;
585 if (current == booting_thread)
587 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
588 lock_master_socket(0); /* allow other clients now */
592 /* create a new thread */
593 DECL_HANDLER(new_thread)
595 struct thread *thread;
598 if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1)
600 if ((thread = create_thread( sock[0], current->process, req->suspend )))
603 if ((req->handle = alloc_handle( current->process, thread,
604 THREAD_ALL_ACCESS, req->inherit )) != -1)
606 set_reply_fd( current, sock[1] );
607 /* thread object will be released when the thread gets killed */
610 release_object( thread );
614 else file_set_error();
617 /* retrieve the thread buffer file descriptor */
618 DECL_HANDLER(get_thread_buffer)
620 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
623 /* initialize a new thread */
624 DECL_HANDLER(init_thread)
626 if (current->unix_pid)
628 fatal_protocol_error( current, "init_thread: already running\n" );
631 current->unix_pid = req->unix_pid;
632 current->teb = req->teb;
633 current->entry = req->entry;
634 if (current->suspend + current->process->suspend > 0) stop_thread( current );
635 if (current->process->running_threads > 1)
636 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, current );
639 /* terminate a thread */
640 DECL_HANDLER(terminate_thread)
642 struct thread *thread;
644 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
646 kill_thread( thread, req->exit_code );
647 release_object( thread );
651 /* fetch information about a thread */
652 DECL_HANDLER(get_thread_info)
654 struct thread *thread;
656 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
659 req->exit_code = thread->exit_code;
660 req->priority = thread->priority;
661 release_object( thread );
665 /* set information about a thread */
666 DECL_HANDLER(set_thread_info)
668 struct thread *thread;
670 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
672 set_thread_info( thread, req );
673 release_object( thread );
677 /* suspend a thread */
678 DECL_HANDLER(suspend_thread)
680 struct thread *thread;
682 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
684 req->count = suspend_thread( thread, 1 );
685 release_object( thread );
689 /* resume a thread */
690 DECL_HANDLER(resume_thread)
692 struct thread *thread;
694 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
696 req->count = resume_thread( thread );
697 release_object( thread );
701 /* select on a handle list */
704 if (!select_on( req->count, req->handles, req->flags, req->timeout ))
708 /* queue an APC for a thread */
709 DECL_HANDLER(queue_apc)
711 struct thread *thread;
712 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
714 thread_queue_apc( thread, req->func, req->param );
715 release_object( thread );
719 /* get list of APC to call */
720 DECL_HANDLER(get_apcs)
722 if ((req->count = current->apc_count))
724 memcpy( req->apcs, current->apc, current->apc_count * sizeof(*current->apc) );
725 free( current->apc );
727 current->apc_count = 0;
731 /* fetch a selector entry for a thread */
732 DECL_HANDLER(get_selector_entry)
734 struct thread *thread;
735 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
737 get_selector_entry( thread, req->entry, &req->base, &req->limit, &req->flags );
738 release_object( thread );