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
) make_timeout( &wait
->timeout
, timeout
);
335 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
337 if (!(obj
= get_handle_obj( thread
->process
, handles
[i
],
338 SYNCHRONIZE
, NULL
)))
344 entry
->thread
= thread
;
345 if (!obj
->ops
->add_queue( obj
, entry
))
351 release_object( obj
);
356 /* check if the thread waiting condition is satisfied */
357 static int check_wait( struct thread
*thread
, int *signaled
)
360 struct thread_wait
*wait
= thread
->wait
;
361 struct wait_queue_entry
*entry
= wait
->queues
;
364 if (wait
->flags
& SELECT_ALL
)
367 /* Note: we must check them all anyway, as some objects may
368 * want to do something when signaled, even if others are not */
369 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
370 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
371 if (not_ok
) goto other_checks
;
372 /* Wait satisfied: tell it to all objects */
374 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
375 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
376 *signaled
= STATUS_ABANDONED_WAIT_0
;
381 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
383 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
384 /* Wait satisfied: tell it to the object */
386 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
387 *signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
393 if ((wait
->flags
& SELECT_ALERTABLE
) && thread
->apc
)
395 *signaled
= STATUS_USER_APC
;
398 if (wait
->flags
& SELECT_TIMEOUT
)
401 gettimeofday( &now
, NULL
);
402 if ((now
.tv_sec
> wait
->timeout
.tv_sec
) ||
403 ((now
.tv_sec
== wait
->timeout
.tv_sec
) &&
404 (now
.tv_usec
>= wait
->timeout
.tv_usec
)))
406 *signaled
= STATUS_TIMEOUT
;
413 /* attempt to wake up a thread */
414 /* return 1 if OK, 0 if the wait condition is still not satisfied */
415 static int wake_thread( struct thread
*thread
)
417 struct select_request
*req
= get_req_ptr( thread
);
419 if (!check_wait( thread
, &req
->signaled
)) return 0;
424 /* sleep on a list of objects */
425 static void sleep_on( struct thread
*thread
, int count
, int *handles
, int flags
, int timeout
)
427 struct select_request
*req
;
428 assert( !thread
->wait
);
429 if (!wait_on( thread
, count
, handles
, flags
, timeout
)) goto error
;
430 if (wake_thread( thread
)) return;
431 /* now we need to wait */
432 if (flags
& SELECT_TIMEOUT
)
434 if (!(thread
->wait
->user
= add_timeout_user( &thread
->wait
->timeout
,
435 call_timeout_handler
, thread
)))
438 thread
->state
= SLEEPING
;
442 req
= get_req_ptr( thread
);
446 /* timeout for the current thread */
447 void thread_timeout(void)
449 struct select_request
*req
= get_req_ptr( current
);
451 assert( current
->wait
);
452 current
->wait
->user
= NULL
;
454 req
->signaled
= STATUS_TIMEOUT
;
455 send_reply( current
);
458 /* attempt to wake threads sleeping on the object wait queue */
459 void wake_up( struct object
*obj
, int max
)
461 struct wait_queue_entry
*entry
= obj
->head
;
465 struct thread
*thread
= entry
->thread
;
467 if (wake_thread( thread
))
469 send_reply( thread
);
470 if (max
&& !--max
) break;
475 /* queue an async procedure call */
476 static int thread_queue_apc( struct thread
*thread
, void *func
, void *param
)
478 struct thread_apc
*apc
;
481 if (!(thread
->apc
= mem_alloc( MAX_THREAD_APC
* sizeof(*apc
) )))
483 thread
->apc_count
= 0;
485 else if (thread
->apc_count
>= MAX_THREAD_APC
) return 0;
486 thread
->apc
[thread
->apc_count
].func
= func
;
487 thread
->apc
[thread
->apc_count
].param
= param
;
491 if (wake_thread( thread
)) send_reply( thread
);
496 /* kill a thread on the spot */
497 void kill_thread( struct thread
*thread
, int exit_code
)
499 if (thread
->state
== TERMINATED
) return; /* already killed */
500 remove_client( thread
->client
, exit_code
); /* this will call thread_killed */
503 /* a thread has been killed */
504 void thread_killed( struct thread
*thread
, int exit_code
)
506 thread
->state
= TERMINATED
;
507 thread
->exit_code
= exit_code
;
508 thread
->client
= NULL
;
509 if (thread
->wait
) end_wait( thread
);
510 debug_exit_thread( thread
, exit_code
);
511 abandon_mutexes( thread
);
512 remove_process_thread( thread
->process
, thread
);
513 wake_up( &thread
->obj
, 0 );
514 detach_thread( thread
);
515 release_object( thread
);
518 /* create a new thread */
519 DECL_HANDLER(new_thread
)
521 struct thread
*thread
;
522 struct process
*process
;
524 if ((process
= get_process_from_id( req
->pid
)))
526 if ((fd
= dup(fd
)) != -1)
528 if ((thread
= create_thread( fd
, process
, req
->suspend
)))
531 if ((req
->handle
= alloc_handle( current
->process
, thread
,
532 THREAD_ALL_ACCESS
, req
->inherit
)) == -1)
533 release_object( thread
);
534 /* else will be released when the thread gets killed */
538 else file_set_error();
539 release_object( process
);
543 /* retrieve the thread buffer file descriptor */
544 DECL_HANDLER(get_thread_buffer
)
546 fatal_protocol_error( current
, "get_thread_buffer: should never get called directly\n" );
549 /* initialize a new thread */
550 DECL_HANDLER(init_thread
)
552 if (current
->unix_pid
)
554 fatal_protocol_error( current
, "init_thread: already running\n" );
557 current
->unix_pid
= req
->unix_pid
;
558 current
->teb
= req
->teb
;
559 if (current
->suspend
+ current
->process
->suspend
> 0) stop_thread( current
);
560 req
->pid
= current
->process
;
564 /* terminate a thread */
565 DECL_HANDLER(terminate_thread
)
567 struct thread
*thread
;
569 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
571 kill_thread( thread
, req
->exit_code
);
572 release_object( thread
);
576 /* fetch information about a thread */
577 DECL_HANDLER(get_thread_info
)
579 struct thread
*thread
;
581 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
584 req
->exit_code
= thread
->exit_code
;
585 req
->priority
= thread
->priority
;
586 release_object( thread
);
590 /* set information about a thread */
591 DECL_HANDLER(set_thread_info
)
593 struct thread
*thread
;
595 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
597 set_thread_info( thread
, req
);
598 release_object( thread
);
602 /* suspend a thread */
603 DECL_HANDLER(suspend_thread
)
605 struct thread
*thread
;
607 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
609 req
->count
= suspend_thread( thread
, 1 );
610 release_object( thread
);
614 /* resume a thread */
615 DECL_HANDLER(resume_thread
)
617 struct thread
*thread
;
619 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
621 req
->count
= resume_thread( thread
);
622 release_object( thread
);
626 /* select on a handle list */
629 sleep_on( current
, req
->count
, req
->handles
, req
->flags
, req
->timeout
);
632 /* queue an APC for a thread */
633 DECL_HANDLER(queue_apc
)
635 struct thread
*thread
;
636 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
638 thread_queue_apc( thread
, req
->func
, req
->param
);
639 release_object( thread
);
643 /* get list of APC to call */
644 DECL_HANDLER(get_apcs
)
646 if ((req
->count
= current
->apc_count
))
648 memcpy( req
->apcs
, current
->apc
, current
->apc_count
* sizeof(*current
->apc
) );
649 free( current
->apc
);
651 current
->apc_count
= 0;