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 static void destroy_thread( struct object *obj );
68 static const struct object_ops thread_ops =
70 sizeof(struct thread),
83 static struct thread *first_thread;
85 /* allocate the buffer for the communication with the client */
86 static int alloc_client_buffer( struct thread *thread )
90 if ((fd = create_anonymous_file()) == -1) return -1;
91 if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error;
92 if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE,
93 MAP_SHARED, fd, 0 )) == (void*)-1) goto error;
98 if (fd != -1) close( fd );
102 /* create a new thread */
103 static struct thread *create_thread( int fd, struct process *process, int suspend )
105 struct thread *thread;
108 if (!(thread = alloc_object( &thread_ops ))) return NULL;
110 thread->client = NULL;
111 thread->unix_pid = 0; /* not known yet */
113 thread->mutex = NULL;
114 thread->debug_ctx = NULL;
115 thread->debug_event = NULL;
116 thread->exit_event = NULL;
119 thread->apc_count = 0;
121 thread->state = RUNNING;
122 thread->attached = 0;
123 thread->exit_code = 0x103; /* STILL_ACTIVE */
126 thread->priority = THREAD_PRIORITY_NORMAL;
127 thread->affinity = 1;
128 thread->suspend = (suspend != 0);
129 thread->buffer = (void *)-1;
130 thread->last_req = REQ_GET_THREAD_BUFFER;
132 if (!first_thread) /* creating the first thread */
135 thread->process = process = create_initial_process();
138 else thread->process = (struct process *)grab_object( process );
140 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
141 first_thread = thread;
142 add_process_thread( process, thread );
144 if ((buf_fd = alloc_client_buffer( thread )) == -1) goto error;
145 if (!(thread->client = add_client( fd, thread )))
150 set_reply_fd( thread, buf_fd ); /* send the fd to the client */
151 send_reply( thread );
155 remove_process_thread( process, thread );
156 release_object( thread );
160 /* create the initial thread and start the main server loop */
161 void create_initial_thread( int fd )
163 create_thread( fd, NULL, 0 );
167 /* destroy a thread when its refcount is 0 */
168 static void destroy_thread( struct object *obj )
170 struct thread *thread = (struct thread *)obj;
171 assert( obj->ops == &thread_ops );
173 assert( !thread->debug_ctx ); /* cannot still be debugging something */
174 release_object( thread->process );
175 if (thread->next) thread->next->prev = thread->prev;
176 if (thread->prev) thread->prev->next = thread->next;
177 else first_thread = thread->next;
178 if (thread->apc) free( thread->apc );
179 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
182 /* dump a thread on stdout for debugging purposes */
183 static void dump_thread( struct object *obj, int verbose )
185 struct thread *thread = (struct thread *)obj;
186 assert( obj->ops == &thread_ops );
188 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
189 thread->unix_pid, thread->teb, thread->state );
192 static int thread_signaled( struct object *obj, struct thread *thread )
194 struct thread *mythread = (struct thread *)obj;
195 return (mythread->state == TERMINATED);
198 /* get a thread pointer from a thread id (and increment the refcount) */
199 struct thread *get_thread_from_id( void *id )
201 struct thread *t = first_thread;
202 while (t && (t != id)) t = t->next;
203 if (t) grab_object( t );
207 /* get a thread from a handle (and increment the refcount) */
208 struct thread *get_thread_from_handle( int handle, unsigned int access )
210 return (struct thread *)get_handle_obj( current->process, handle,
211 access, &thread_ops );
214 /* find a thread from a Unix pid */
215 struct thread *get_thread_from_pid( int pid )
217 struct thread *t = first_thread;
218 while (t && (t->unix_pid != pid)) t = t->next;
222 /* set all information about a thread */
223 static void set_thread_info( struct thread *thread,
224 struct set_thread_info_request *req )
226 if (req->mask & SET_THREAD_INFO_PRIORITY)
227 thread->priority = req->priority;
228 if (req->mask & SET_THREAD_INFO_AFFINITY)
230 if (req->affinity != 1) set_error( ERROR_INVALID_PARAMETER );
231 else thread->affinity = req->affinity;
235 /* suspend a thread */
236 int suspend_thread( struct thread *thread, int check_limit )
238 int old_count = thread->suspend;
239 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
241 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
243 else set_error( ERROR_SIGNAL_REFUSED );
247 /* resume a thread */
248 int resume_thread( struct thread *thread )
250 int old_count = thread->suspend;
251 if (thread->suspend > 0)
253 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
258 /* suspend all threads but the current */
259 void suspend_all_threads( void )
261 struct thread *thread;
262 for ( thread = first_thread; thread; thread = thread->next )
263 if ( thread != current )
264 suspend_thread( thread, 0 );
267 /* resume all threads but the current */
268 void resume_all_threads( void )
270 struct thread *thread;
271 for ( thread = first_thread; thread; thread = thread->next )
272 if ( thread != current )
273 resume_thread( thread );
276 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
277 int add_queue( struct object *obj, struct wait_queue_entry *entry )
281 entry->prev = obj->tail;
283 if (obj->tail) obj->tail->next = entry;
284 else obj->head = entry;
289 /* remove a thread from an object wait queue */
290 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
292 if (entry->next) entry->next->prev = entry->prev;
293 else obj->tail = entry->prev;
294 if (entry->prev) entry->prev->next = entry->next;
295 else obj->head = entry->next;
296 release_object( obj );
300 static void end_wait( struct thread *thread )
302 struct thread_wait *wait = thread->wait;
303 struct wait_queue_entry *entry;
307 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
308 entry->obj->ops->remove_queue( entry->obj, entry );
309 if (wait->user) remove_timeout_user( wait->user );
314 /* build the thread wait structure */
315 static int wait_on( struct thread *thread, int count,
316 int *handles, int flags, int timeout )
318 struct thread_wait *wait;
319 struct wait_queue_entry *entry;
323 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
325 set_error( ERROR_INVALID_PARAMETER );
328 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
333 if (flags & SELECT_TIMEOUT)
335 gettimeofday( &wait->timeout, 0 );
336 add_timeout( &wait->timeout, timeout );
339 for (i = 0, entry = wait->queues; i < count; i++, entry++)
341 if (!(obj = get_handle_obj( thread->process, handles[i],
342 SYNCHRONIZE, NULL )))
348 entry->thread = thread;
349 if (!obj->ops->add_queue( obj, entry ))
355 release_object( obj );
360 /* check if the thread waiting condition is satisfied */
361 static int check_wait( struct thread *thread, int *signaled )
364 struct thread_wait *wait = thread->wait;
365 struct wait_queue_entry *entry = wait->queues;
368 if (wait->flags & SELECT_ALL)
371 /* Note: we must check them all anyway, as some objects may
372 * want to do something when signaled, even if others are not */
373 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
374 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
375 if (not_ok) goto other_checks;
376 /* Wait satisfied: tell it to all objects */
378 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
379 if (entry->obj->ops->satisfied( entry->obj, thread ))
380 *signaled = STATUS_ABANDONED_WAIT_0;
385 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
387 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
388 /* Wait satisfied: tell it to the object */
390 if (entry->obj->ops->satisfied( entry->obj, thread ))
391 *signaled = i + STATUS_ABANDONED_WAIT_0;
397 if ((wait->flags & SELECT_ALERTABLE) && thread->apc)
399 *signaled = STATUS_USER_APC;
402 if (wait->flags & SELECT_TIMEOUT)
405 gettimeofday( &now, NULL );
406 if (!time_before( &now, &wait->timeout ))
408 *signaled = STATUS_TIMEOUT;
415 /* attempt to wake up a thread */
416 /* return 1 if OK, 0 if the wait condition is still not satisfied */
417 static int wake_thread( struct thread *thread )
419 struct select_request *req = get_req_ptr( thread );
421 if (!check_wait( thread, &req->signaled )) return 0;
426 /* sleep on a list of objects */
427 static void sleep_on( struct thread *thread, int count, int *handles, int flags, int timeout )
429 struct select_request *req;
430 assert( !thread->wait );
431 if (!wait_on( thread, count, handles, flags, timeout )) goto error;
432 if (wake_thread( thread )) return;
433 /* now we need to wait */
434 if (flags & SELECT_TIMEOUT)
436 if (!(thread->wait->user = add_timeout_user( &thread->wait->timeout,
437 call_timeout_handler, thread )))
440 thread->state = SLEEPING;
444 req = get_req_ptr( thread );
448 /* timeout for the current thread */
449 void thread_timeout(void)
451 struct select_request *req = get_req_ptr( current );
453 assert( current->wait );
454 current->wait->user = NULL;
456 req->signaled = STATUS_TIMEOUT;
457 send_reply( current );
460 /* attempt to wake threads sleeping on the object wait queue */
461 void wake_up( struct object *obj, int max )
463 struct wait_queue_entry *entry = obj->head;
467 struct thread *thread = entry->thread;
469 if (wake_thread( thread ))
471 send_reply( thread );
472 if (max && !--max) break;
477 /* queue an async procedure call */
478 static int thread_queue_apc( struct thread *thread, void *func, void *param )
480 struct thread_apc *apc;
483 if (!(thread->apc = mem_alloc( MAX_THREAD_APC * sizeof(*apc) )))
485 thread->apc_count = 0;
487 else if (thread->apc_count >= MAX_THREAD_APC) return 0;
488 thread->apc[thread->apc_count].func = func;
489 thread->apc[thread->apc_count].param = param;
493 if (wake_thread( thread )) send_reply( thread );
498 /* kill a thread on the spot */
499 void kill_thread( struct thread *thread, int exit_code )
501 if (thread->state == TERMINATED) return; /* already killed */
502 remove_client( thread->client, exit_code ); /* this will call thread_killed */
505 /* a thread has been killed */
506 void thread_killed( struct thread *thread, int exit_code )
508 thread->state = TERMINATED;
509 thread->exit_code = exit_code;
510 thread->client = NULL;
511 if (thread->wait) end_wait( thread );
512 debug_exit_thread( thread, exit_code );
513 abandon_mutexes( thread );
514 remove_process_thread( thread->process, thread );
515 wake_up( &thread->obj, 0 );
516 detach_thread( thread );
517 release_object( thread );
520 /* create a new thread */
521 DECL_HANDLER(new_thread)
523 struct thread *thread;
524 struct process *process;
526 if ((process = get_process_from_id( req->pid )))
528 if ((fd = dup(fd)) != -1)
530 if ((thread = create_thread( fd, process, req->suspend )))
533 if ((req->handle = alloc_handle( current->process, thread,
534 THREAD_ALL_ACCESS, req->inherit )) == -1)
535 release_object( thread );
536 /* else will be released when the thread gets killed */
540 else file_set_error();
541 release_object( process );
545 /* retrieve the thread buffer file descriptor */
546 DECL_HANDLER(get_thread_buffer)
548 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
551 /* initialize a new thread */
552 DECL_HANDLER(init_thread)
554 if (current->unix_pid)
556 fatal_protocol_error( current, "init_thread: already running\n" );
559 current->unix_pid = req->unix_pid;
560 current->teb = req->teb;
561 if (current->suspend + current->process->suspend > 0) stop_thread( current );
562 req->pid = current->process;
566 /* terminate a thread */
567 DECL_HANDLER(terminate_thread)
569 struct thread *thread;
571 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
573 kill_thread( thread, req->exit_code );
574 release_object( thread );
578 /* fetch information about a thread */
579 DECL_HANDLER(get_thread_info)
581 struct thread *thread;
583 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
586 req->exit_code = thread->exit_code;
587 req->priority = thread->priority;
588 release_object( thread );
592 /* set information about a thread */
593 DECL_HANDLER(set_thread_info)
595 struct thread *thread;
597 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
599 set_thread_info( thread, req );
600 release_object( thread );
604 /* suspend a thread */
605 DECL_HANDLER(suspend_thread)
607 struct thread *thread;
609 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
611 req->count = suspend_thread( thread, 1 );
612 release_object( thread );
616 /* resume a thread */
617 DECL_HANDLER(resume_thread)
619 struct thread *thread;
621 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
623 req->count = resume_thread( thread );
624 release_object( thread );
628 /* select on a handle list */
631 sleep_on( current, req->count, req->handles, req->flags, req->timeout );
634 /* queue an APC for a thread */
635 DECL_HANDLER(queue_apc)
637 struct thread *thread;
638 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
640 thread_queue_apc( thread, req->func, req->param );
641 release_object( thread );
645 /* get list of APC to call */
646 DECL_HANDLER(get_apcs)
648 if ((req->count = current->apc_count))
650 memcpy( req->apcs, current->apc, current->apc_count * sizeof(*current->apc) );
651 free( current->apc );
653 current->apc_count = 0;