2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
14 #include <sys/types.h>
31 struct wait_queue_entry
33 struct wait_queue_entry
*next
;
34 struct wait_queue_entry
*prev
;
36 struct thread
*thread
;
41 int count
; /* count of objects */
43 struct timeval timeout
;
44 struct timeout_user
*user
;
45 struct wait_queue_entry queues
[1];
48 /* asynchronous procedure calls */
52 void *func
; /* function to call in client */
53 void *param
; /* function param */
55 #define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */
58 /* thread operations */
60 static void dump_thread( struct object
*obj
, int verbose
);
61 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
62 static void destroy_thread( struct object
*obj
);
64 static const struct object_ops thread_ops
=
66 sizeof(struct thread
),
79 static struct thread
*first_thread
;
81 /* allocate the buffer for the communication with the client */
82 static int alloc_client_buffer( struct thread
*thread
)
86 if ((fd
= create_anonymous_file()) == -1) return -1;
87 if (ftruncate( fd
, MAX_REQUEST_LENGTH
) == -1) goto error
;
88 if ((thread
->buffer
= mmap( 0, MAX_REQUEST_LENGTH
, PROT_READ
| PROT_WRITE
,
89 MAP_SHARED
, fd
, 0 )) == (void*)-1) goto error
;
94 if (fd
!= -1) close( fd
);
98 /* create a new thread */
99 static struct thread
*create_thread( int fd
, struct process
*process
, int suspend
)
101 struct thread
*thread
;
104 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
106 thread
->client
= NULL
;
107 thread
->unix_pid
= 0; /* not known yet */
109 thread
->mutex
= NULL
;
110 thread
->debug_ctx
= NULL
;
111 thread
->debug_first
= NULL
;
112 thread
->debug_event
= NULL
;
115 thread
->apc_count
= 0;
117 thread
->state
= STARTING
;
118 thread
->exit_code
= 0x103; /* STILL_ACTIVE */
121 thread
->priority
= THREAD_PRIORITY_NORMAL
;
122 thread
->affinity
= 1;
123 thread
->suspend
= (suspend
!= 0);
124 thread
->buffer
= (void *)-1;
125 thread
->last_req
= REQ_GET_THREAD_BUFFER
;
127 if (!first_thread
) /* creating the first thread */
130 thread
->process
= process
= create_initial_process();
133 else thread
->process
= (struct process
*)grab_object( process
);
135 if ((thread
->next
= first_thread
) != NULL
) thread
->next
->prev
= thread
;
136 first_thread
= thread
;
137 add_process_thread( process
, thread
);
139 if ((buf_fd
= alloc_client_buffer( thread
)) == -1) goto error
;
140 if (!(thread
->client
= add_client( fd
, thread
)))
145 set_reply_fd( thread
, buf_fd
); /* send the fd to the client */
146 send_reply( thread
);
150 remove_process_thread( process
, thread
);
151 release_object( thread
);
155 /* create the initial thread and start the main server loop */
156 void create_initial_thread( int fd
)
158 create_thread( fd
, NULL
, 0 );
162 /* destroy a thread when its refcount is 0 */
163 static void destroy_thread( struct object
*obj
)
165 struct thread
*thread
= (struct thread
*)obj
;
166 assert( obj
->ops
== &thread_ops
);
168 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
169 release_object( thread
->process
);
170 if (thread
->next
) thread
->next
->prev
= thread
->prev
;
171 if (thread
->prev
) thread
->prev
->next
= thread
->next
;
172 else first_thread
= thread
->next
;
173 if (thread
->apc
) free( thread
->apc
);
174 if (thread
->buffer
!= (void *)-1) munmap( thread
->buffer
, MAX_REQUEST_LENGTH
);
177 /* dump a thread on stdout for debugging purposes */
178 static void dump_thread( struct object
*obj
, int verbose
)
180 struct thread
*thread
= (struct thread
*)obj
;
181 assert( obj
->ops
== &thread_ops
);
183 fprintf( stderr
, "Thread pid=%d teb=%p state=%d\n",
184 thread
->unix_pid
, thread
->teb
, thread
->state
);
187 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
189 struct thread
*mythread
= (struct thread
*)obj
;
190 return (mythread
->state
== TERMINATED
);
193 /* get a thread pointer from a thread id (and increment the refcount) */
194 struct thread
*get_thread_from_id( void *id
)
196 struct thread
*t
= first_thread
;
197 while (t
&& (t
!= id
)) t
= t
->next
;
198 if (t
) grab_object( t
);
202 /* get a thread from a handle (and increment the refcount) */
203 struct thread
*get_thread_from_handle( int handle
, unsigned int access
)
205 return (struct thread
*)get_handle_obj( current
->process
, handle
,
206 access
, &thread_ops
);
209 /* set all information about a thread */
210 static void set_thread_info( struct thread
*thread
,
211 struct set_thread_info_request
*req
)
213 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
214 thread
->priority
= req
->priority
;
215 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
217 if (req
->affinity
!= 1) set_error( ERROR_INVALID_PARAMETER
);
218 else thread
->affinity
= req
->affinity
;
222 /* suspend a thread */
223 static int suspend_thread( struct thread
*thread
)
225 int old_count
= thread
->suspend
;
226 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
228 if (!(thread
->process
->suspend
+ thread
->suspend
++))
230 if (thread
->unix_pid
) kill( thread
->unix_pid
, SIGSTOP
);
236 /* resume a thread */
237 static int resume_thread( struct thread
*thread
)
239 int old_count
= thread
->suspend
;
240 if (thread
->suspend
> 0)
242 if (!(--thread
->suspend
+ thread
->process
->suspend
))
244 if (thread
->unix_pid
) kill( thread
->unix_pid
, SIGCONT
);
250 /* suspend all threads but the current */
251 void suspend_all_threads( void )
253 struct thread
*thread
;
254 for ( thread
= first_thread
; thread
; thread
= thread
->next
)
255 if ( thread
!= current
)
256 suspend_thread( thread
);
259 /* resume all threads but the current */
260 void resume_all_threads( void )
262 struct thread
*thread
;
263 for ( thread
= first_thread
; thread
; thread
= thread
->next
)
264 if ( thread
!= current
)
265 resume_thread( thread
);
268 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
269 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
273 entry
->prev
= obj
->tail
;
275 if (obj
->tail
) obj
->tail
->next
= entry
;
276 else obj
->head
= entry
;
281 /* remove a thread from an object wait queue */
282 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
284 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
285 else obj
->tail
= entry
->prev
;
286 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
287 else obj
->head
= entry
->next
;
288 release_object( obj
);
292 static void end_wait( struct thread
*thread
)
294 struct thread_wait
*wait
= thread
->wait
;
295 struct wait_queue_entry
*entry
;
299 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
300 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
301 if (wait
->user
) remove_timeout_user( wait
->user
);
306 /* build the thread wait structure */
307 static int wait_on( struct thread
*thread
, int count
,
308 int *handles
, int flags
, int timeout
)
310 struct thread_wait
*wait
;
311 struct wait_queue_entry
*entry
;
315 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
317 set_error( ERROR_INVALID_PARAMETER
);
320 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
325 if (flags
& SELECT_TIMEOUT
) make_timeout( &wait
->timeout
, timeout
);
327 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
329 if (!(obj
= get_handle_obj( thread
->process
, handles
[i
],
330 SYNCHRONIZE
, NULL
)))
336 entry
->thread
= thread
;
337 if (!obj
->ops
->add_queue( obj
, entry
))
343 release_object( obj
);
348 /* check if the thread waiting condition is satisfied */
349 static int check_wait( struct thread
*thread
, int *signaled
)
352 struct thread_wait
*wait
= thread
->wait
;
353 struct wait_queue_entry
*entry
= wait
->queues
;
356 if (wait
->flags
& SELECT_ALL
)
359 /* Note: we must check them all anyway, as some objects may
360 * want to do something when signaled, even if others are not */
361 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
362 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
363 if (not_ok
) goto other_checks
;
364 /* Wait satisfied: tell it to all objects */
366 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
367 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
368 *signaled
= STATUS_ABANDONED_WAIT_0
;
373 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
375 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
376 /* Wait satisfied: tell it to the object */
378 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
379 *signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
385 if ((wait
->flags
& SELECT_ALERTABLE
) && thread
->apc
)
387 *signaled
= STATUS_USER_APC
;
390 if (wait
->flags
& SELECT_TIMEOUT
)
393 gettimeofday( &now
, NULL
);
394 if ((now
.tv_sec
> wait
->timeout
.tv_sec
) ||
395 ((now
.tv_sec
== wait
->timeout
.tv_sec
) &&
396 (now
.tv_usec
>= wait
->timeout
.tv_usec
)))
398 *signaled
= STATUS_TIMEOUT
;
405 /* attempt to wake up a thread */
406 /* return 1 if OK, 0 if the wait condition is still not satisfied */
407 static int wake_thread( struct thread
*thread
)
409 struct select_request
*req
= get_req_ptr( thread
);
411 if (!check_wait( thread
, &req
->signaled
)) return 0;
416 /* sleep on a list of objects */
417 static void sleep_on( struct thread
*thread
, int count
, int *handles
, int flags
, int timeout
)
419 struct select_request
*req
;
420 assert( !thread
->wait
);
421 if (!wait_on( thread
, count
, handles
, flags
, timeout
)) goto error
;
422 if (wake_thread( thread
)) return;
423 /* now we need to wait */
424 if (flags
& SELECT_TIMEOUT
)
426 if (!(thread
->wait
->user
= add_timeout_user( &thread
->wait
->timeout
,
427 call_timeout_handler
, thread
)))
430 thread
->state
= SLEEPING
;
434 req
= get_req_ptr( thread
);
438 /* timeout for the current thread */
439 void thread_timeout(void)
441 struct select_request
*req
= get_req_ptr( current
);
443 assert( current
->wait
);
444 current
->wait
->user
= NULL
;
446 req
->signaled
= STATUS_TIMEOUT
;
447 send_reply( current
);
450 /* attempt to wake threads sleeping on the object wait queue */
451 void wake_up( struct object
*obj
, int max
)
453 struct wait_queue_entry
*entry
= obj
->head
;
457 struct thread
*thread
= entry
->thread
;
459 if (wake_thread( thread
))
461 send_reply( thread
);
462 if (max
&& !--max
) break;
467 /* queue an async procedure call */
468 static int thread_queue_apc( struct thread
*thread
, void *func
, void *param
)
470 struct thread_apc
*apc
;
473 if (!(thread
->apc
= mem_alloc( MAX_THREAD_APC
* sizeof(*apc
) )))
475 thread
->apc_count
= 0;
477 else if (thread
->apc_count
>= MAX_THREAD_APC
) return 0;
478 thread
->apc
[thread
->apc_count
].func
= func
;
479 thread
->apc
[thread
->apc_count
].param
= param
;
483 if (wake_thread( thread
)) send_reply( thread
);
488 /* kill a thread on the spot */
489 void kill_thread( struct thread
*thread
, int exit_code
)
491 if (thread
->state
== TERMINATED
) return; /* already killed */
492 if (thread
->unix_pid
) kill( thread
->unix_pid
, SIGTERM
);
493 remove_client( thread
->client
, exit_code
); /* this will call thread_killed */
496 /* a thread has been killed */
497 void thread_killed( struct thread
*thread
, int exit_code
)
499 thread
->state
= TERMINATED
;
500 thread
->exit_code
= exit_code
;
501 if (thread
->wait
) end_wait( thread
);
502 debug_exit_thread( thread
, exit_code
);
503 abandon_mutexes( thread
);
504 remove_process_thread( thread
->process
, thread
);
505 wake_up( &thread
->obj
, 0 );
506 release_object( thread
);
509 /* create a new thread */
510 DECL_HANDLER(new_thread
)
512 struct thread
*thread
;
513 struct process
*process
;
515 if ((process
= get_process_from_id( req
->pid
)))
517 if ((fd
= dup(fd
)) != -1)
519 if ((thread
= create_thread( fd
, process
, req
->suspend
)))
522 if ((req
->handle
= alloc_handle( current
->process
, thread
,
523 THREAD_ALL_ACCESS
, req
->inherit
)) == -1)
524 release_object( thread
);
525 /* else will be released when the thread gets killed */
529 else file_set_error();
530 release_object( process
);
534 /* retrieve the thread buffer file descriptor */
535 DECL_HANDLER(get_thread_buffer
)
537 fatal_protocol_error( current
, "get_thread_buffer: should never get called directly\n" );
540 /* initialize a new thread */
541 DECL_HANDLER(init_thread
)
543 if (current
->state
!= STARTING
)
545 fatal_protocol_error( current
, "init_thread: already running\n" );
548 current
->state
= RUNNING
;
549 current
->unix_pid
= req
->unix_pid
;
550 current
->teb
= req
->teb
;
551 if (current
->suspend
+ current
->process
->suspend
> 0)
552 kill( current
->unix_pid
, SIGSTOP
);
553 req
->pid
= current
->process
;
557 /* terminate a thread */
558 DECL_HANDLER(terminate_thread
)
560 struct thread
*thread
;
562 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
564 kill_thread( thread
, req
->exit_code
);
565 release_object( thread
);
569 /* fetch information about a thread */
570 DECL_HANDLER(get_thread_info
)
572 struct thread
*thread
;
574 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
577 req
->exit_code
= thread
->exit_code
;
578 req
->priority
= thread
->priority
;
579 release_object( thread
);
583 /* set information about a thread */
584 DECL_HANDLER(set_thread_info
)
586 struct thread
*thread
;
588 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
590 set_thread_info( thread
, req
);
591 release_object( thread
);
595 /* suspend a thread */
596 DECL_HANDLER(suspend_thread
)
598 struct thread
*thread
;
600 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
602 req
->count
= suspend_thread( thread
);
603 release_object( thread
);
607 /* resume a thread */
608 DECL_HANDLER(resume_thread
)
610 struct thread
*thread
;
612 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
614 req
->count
= resume_thread( thread
);
615 release_object( thread
);
619 /* select on a handle list */
622 sleep_on( current
, req
->count
, req
->handles
, req
->flags
, req
->timeout
);
625 /* queue an APC for a thread */
626 DECL_HANDLER(queue_apc
)
628 struct thread
*thread
;
629 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
631 thread_queue_apc( thread
, req
->func
, req
->param
);
632 release_object( thread
);
636 /* get list of APC to call */
637 DECL_HANDLER(get_apcs
)
639 if ((req
->count
= current
->apc_count
))
641 memcpy( req
->apcs
, current
->apc
, current
->apc_count
* sizeof(*current
->apc
) );
642 free( current
->apc
);
644 current
->apc_count
= 0;