2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
13 #include <sys/types.h>
30 struct wait_queue_entry
32 struct wait_queue_entry
*next
;
33 struct wait_queue_entry
*prev
;
35 struct thread
*thread
;
40 int count
; /* count of objects */
42 struct timeval timeout
;
43 struct wait_queue_entry queues
[1];
46 /* asynchronous procedure calls */
50 void *func
; /* function to call in client */
51 void *param
; /* function param */
53 #define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */
56 /* thread operations */
58 static void dump_thread( struct object
*obj
, int verbose
);
59 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
60 static void destroy_thread( struct object
*obj
);
62 static const struct object_ops thread_ops
=
76 static struct thread initial_thread
;
77 static struct thread
*first_thread
= &initial_thread
;
79 /* initialization of a thread structure */
80 static void init_thread( struct thread
*thread
, int fd
)
82 init_object( &thread
->obj
, &thread_ops
, NULL
);
83 thread
->client_fd
= fd
;
84 thread
->unix_pid
= 0; /* not known yet */
86 thread
->debugger
= NULL
;
89 thread
->apc_count
= 0;
91 thread
->state
= STARTING
;
92 thread
->exit_code
= 0x103; /* STILL_ACTIVE */
95 thread
->priority
= THREAD_PRIORITY_NORMAL
;
100 /* create the initial thread and start the main server loop */
101 void create_initial_thread( int fd
)
103 current
= &initial_thread
;
104 init_thread( &initial_thread
, fd
);
105 initial_thread
.process
= create_initial_process();
106 add_process_thread( initial_thread
.process
, &initial_thread
);
107 add_client( fd
, &initial_thread
);
108 grab_object( &initial_thread
); /* so that we never free it */
112 /* create a new thread */
113 static struct thread
*create_thread( int fd
, void *pid
, int suspend
, int inherit
, int *handle
)
115 struct thread
*thread
;
116 struct process
*process
;
118 if (!(thread
= mem_alloc( sizeof(*thread
) ))) return NULL
;
120 if (!(process
= get_process_from_id( pid
)))
125 init_thread( thread
, fd
);
126 thread
->process
= process
;
128 if (suspend
) thread
->suspend
++;
130 thread
->next
= first_thread
;
131 first_thread
->prev
= thread
;
132 first_thread
= thread
;
133 add_process_thread( process
, thread
);
135 if ((*handle
= alloc_handle( current
->process
, thread
,
136 THREAD_ALL_ACCESS
, inherit
)) == -1) goto error
;
137 if (add_client( fd
, thread
) == -1)
139 SET_ERROR( ERROR_TOO_MANY_OPEN_FILES
);
145 if (current
) close_handle( current
->process
, *handle
);
146 remove_process_thread( process
, thread
);
147 release_object( thread
);
151 /* destroy a thread when its refcount is 0 */
152 static void destroy_thread( struct object
*obj
)
154 struct thread
*thread
= (struct thread
*)obj
;
155 assert( obj
->ops
== &thread_ops
);
157 release_object( thread
->process
);
158 if (thread
->next
) thread
->next
->prev
= thread
->prev
;
159 if (thread
->prev
) thread
->prev
->next
= thread
->next
;
160 else first_thread
= thread
->next
;
161 if (thread
->apc
) free( thread
->apc
);
162 if (debug_level
) memset( thread
, 0xaa, sizeof(thread
) ); /* catch errors */
166 /* dump a thread on stdout for debugging purposes */
167 static void dump_thread( struct object
*obj
, int verbose
)
169 struct thread
*thread
= (struct thread
*)obj
;
170 assert( obj
->ops
== &thread_ops
);
172 fprintf( stderr
, "Thread pid=%d fd=%d\n",
173 thread
->unix_pid
, thread
->client_fd
);
176 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
178 struct thread
*mythread
= (struct thread
*)obj
;
179 return (mythread
->state
== TERMINATED
);
182 /* get a thread pointer from a thread id (and increment the refcount) */
183 struct thread
*get_thread_from_id( void *id
)
185 struct thread
*t
= first_thread
;
186 while (t
&& (t
!= id
)) t
= t
->next
;
187 if (t
) grab_object( t
);
191 /* get a thread from a handle (and increment the refcount) */
192 struct thread
*get_thread_from_handle( int handle
, unsigned int access
)
194 return (struct thread
*)get_handle_obj( current
->process
, handle
,
195 access
, &thread_ops
);
198 /* set all information about a thread */
199 static void set_thread_info( struct thread
*thread
,
200 struct set_thread_info_request
*req
)
202 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
203 thread
->priority
= req
->priority
;
204 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
206 if (req
->affinity
!= 1) SET_ERROR( ERROR_INVALID_PARAMETER
);
207 else thread
->affinity
= req
->affinity
;
211 /* suspend a thread */
212 static int suspend_thread( struct thread
*thread
)
214 int old_count
= thread
->suspend
;
215 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
217 if (!thread
->suspend
++)
219 if (thread
->unix_pid
) kill( thread
->unix_pid
, SIGSTOP
);
225 /* resume a thread */
226 static int resume_thread( struct thread
*thread
)
228 int old_count
= thread
->suspend
;
229 if (thread
->suspend
> 0)
231 if (!--thread
->suspend
)
233 if (thread
->unix_pid
) kill( thread
->unix_pid
, SIGCONT
);
239 /* suspend all threads but the current */
240 void suspend_all_threads( void )
242 struct thread
*thread
;
243 for ( thread
= first_thread
; thread
; thread
= thread
->next
)
244 if ( thread
!= current
)
245 suspend_thread( thread
);
248 /* resume all threads but the current */
249 void resume_all_threads( void )
251 struct thread
*thread
;
252 for ( thread
= first_thread
; thread
; thread
= thread
->next
)
253 if ( thread
!= current
)
254 resume_thread( thread
);
257 /* send a reply to a thread */
258 int send_reply( struct thread
*thread
, int pass_fd
, int n
,
259 ... /* arg_1, len_1, ..., arg_n, len_n */ )
261 struct iovec vec
[16];
267 for (i
= 0; i
< n
; i
++)
269 vec
[i
].iov_base
= va_arg( args
, void * );
270 vec
[i
].iov_len
= va_arg( args
, int );
273 return send_reply_v( thread
->client_fd
, thread
->error
, pass_fd
, vec
, n
);
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
->flags
& SELECT_TIMEOUT
) set_select_timeout( thread
->client_fd
, NULL
);
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;
332 if (flags
& SELECT_TIMEOUT
)
334 gettimeofday( &wait
->timeout
, 0 );
337 wait
->timeout
.tv_usec
+= (timeout
% 1000) * 1000;
338 if (wait
->timeout
.tv_usec
>= 1000000)
340 wait
->timeout
.tv_usec
-= 1000000;
341 wait
->timeout
.tv_sec
++;
343 wait
->timeout
.tv_sec
+= timeout
/ 1000;
347 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
349 if (!(obj
= get_handle_obj( thread
->process
, handles
[i
],
350 SYNCHRONIZE
, NULL
)))
356 entry
->thread
= thread
;
357 if (!obj
->ops
->add_queue( obj
, entry
))
363 release_object( obj
);
368 /* check if the thread waiting condition is satisfied */
369 static int check_wait( struct thread
*thread
, int *signaled
)
372 struct thread_wait
*wait
= thread
->wait
;
373 struct wait_queue_entry
*entry
= wait
->queues
;
376 if (wait
->flags
& SELECT_ALL
)
378 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
379 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) goto other_checks
;
380 /* Wait satisfied: tell it to all objects */
382 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
383 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
384 *signaled
= STATUS_ABANDONED_WAIT_0
;
389 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
391 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
392 /* Wait satisfied: tell it to the object */
394 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
395 *signaled
+= STATUS_ABANDONED_WAIT_0
;
401 if ((wait
->flags
& SELECT_ALERTABLE
) && thread
->apc
)
403 *signaled
= STATUS_USER_APC
;
406 if (wait
->flags
& SELECT_TIMEOUT
)
409 gettimeofday( &now
, NULL
);
410 if ((now
.tv_sec
> wait
->timeout
.tv_sec
) ||
411 ((now
.tv_sec
== wait
->timeout
.tv_sec
) &&
412 (now
.tv_usec
>= wait
->timeout
.tv_usec
)))
414 *signaled
= STATUS_TIMEOUT
;
421 /* send the select reply to wake up the client */
422 static void send_select_reply( struct thread
*thread
, int signaled
)
424 struct select_reply reply
;
425 reply
.signaled
= signaled
;
426 if ((signaled
== STATUS_USER_APC
) && thread
->apc
)
428 struct thread_apc
*apc
= thread
->apc
;
429 int len
= thread
->apc_count
* sizeof(*apc
);
431 thread
->apc_count
= 0;
432 send_reply( thread
, -1, 2, &reply
, sizeof(reply
),
436 else send_reply( thread
, -1, 1, &reply
, sizeof(reply
) );
439 /* attempt to wake up a thread */
440 /* return 1 if OK, 0 if the wait condition is still not satisfied */
441 static int wake_thread( struct thread
*thread
)
445 if (!check_wait( thread
, &signaled
)) return 0;
447 send_select_reply( thread
, signaled
);
451 /* sleep on a list of objects */
452 static void sleep_on( struct thread
*thread
, int count
, int *handles
, int flags
, int timeout
)
454 assert( !thread
->wait
);
455 if (!wait_on( thread
, count
, handles
, flags
, timeout
))
457 /* return an error */
458 send_select_reply( thread
, -1 );
461 if (!wake_thread( thread
))
463 /* we need to wait */
464 if (flags
& SELECT_TIMEOUT
)
465 set_select_timeout( thread
->client_fd
, &thread
->wait
->timeout
);
469 /* timeout for the current thread */
470 void thread_timeout(void)
472 assert( current
->wait
);
474 send_select_reply( current
, STATUS_TIMEOUT
);
477 /* attempt to wake threads sleeping on the object wait queue */
478 void wake_up( struct object
*obj
, int max
)
480 struct wait_queue_entry
*entry
= obj
->head
;
484 struct wait_queue_entry
*next
= entry
->next
;
485 if (wake_thread( entry
->thread
))
487 if (max
&& !--max
) break;
493 /* queue an async procedure call */
494 static int thread_queue_apc( struct thread
*thread
, void *func
, void *param
)
496 struct thread_apc
*apc
;
499 if (!(thread
->apc
= mem_alloc( MAX_THREAD_APC
* sizeof(*apc
) )))
501 thread
->apc_count
= 0;
503 else if (thread
->apc_count
>= MAX_THREAD_APC
) return 0;
504 thread
->apc
[thread
->apc_count
].func
= func
;
505 thread
->apc
[thread
->apc_count
].param
= param
;
507 if (thread
->wait
) wake_thread( thread
);
511 /* kill a thread on the spot */
512 void kill_thread( struct thread
*thread
, int exit_code
)
514 if (thread
->state
== TERMINATED
) return; /* already killed */
515 if (thread
->unix_pid
) kill( thread
->unix_pid
, SIGTERM
);
516 remove_client( thread
->client_fd
, exit_code
); /* this will call thread_killed */
519 /* a thread has been killed */
520 void thread_killed( struct thread
*thread
, int exit_code
)
522 thread
->state
= TERMINATED
;
523 thread
->exit_code
= exit_code
;
524 if (thread
->wait
) end_wait( thread
);
525 abandon_mutexes( thread
);
526 remove_process_thread( thread
->process
, thread
);
527 wake_up( &thread
->obj
, 0 );
528 release_object( thread
);
531 /* create a new thread */
532 DECL_HANDLER(new_thread
)
534 struct new_thread_reply reply
;
537 if ((new_fd
= dup(fd
)) != -1)
539 reply
.tid
= create_thread( new_fd
, req
->pid
, req
->suspend
,
540 req
->inherit
, &reply
.handle
);
541 if (!reply
.tid
) close( new_fd
);
544 SET_ERROR( ERROR_TOO_MANY_OPEN_FILES
);
546 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
549 /* initialize a new thread */
550 DECL_HANDLER(init_thread
)
552 struct init_thread_reply reply
;
554 if (current
->state
!= STARTING
)
556 fatal_protocol_error( "init_thread: already running\n" );
559 current
->state
= RUNNING
;
560 current
->unix_pid
= req
->unix_pid
;
561 if (current
->suspend
> 0) kill( current
->unix_pid
, SIGSTOP
);
562 reply
.pid
= current
->process
;
564 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
567 /* terminate a thread */
568 DECL_HANDLER(terminate_thread
)
570 struct thread
*thread
;
572 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
574 kill_thread( thread
, req
->exit_code
);
575 release_object( thread
);
577 if (current
) send_reply( current
, -1, 0 );
580 /* fetch information about a thread */
581 DECL_HANDLER(get_thread_info
)
583 struct thread
*thread
;
584 struct get_thread_info_reply reply
= { 0, 0, 0 };
586 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
589 reply
.exit_code
= thread
->exit_code
;
590 reply
.priority
= thread
->priority
;
591 release_object( thread
);
593 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
596 /* set information about a thread */
597 DECL_HANDLER(set_thread_info
)
599 struct thread
*thread
;
601 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
603 set_thread_info( thread
, req
);
604 release_object( thread
);
606 send_reply( current
, -1, 0 );
609 /* suspend a thread */
610 DECL_HANDLER(suspend_thread
)
612 struct thread
*thread
;
613 struct suspend_thread_reply reply
= { -1 };
614 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
616 reply
.count
= suspend_thread( thread
);
617 release_object( thread
);
619 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
623 /* resume a thread */
624 DECL_HANDLER(resume_thread
)
626 struct thread
*thread
;
627 struct resume_thread_reply reply
= { -1 };
628 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
630 reply
.count
= resume_thread( thread
);
631 release_object( thread
);
633 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
637 /* select on a handle list */
640 if (len
!= req
->count
* sizeof(int))
641 fatal_protocol_error( "select: bad length %d for %d handles\n",
643 sleep_on( current
, req
->count
, (int *)data
, req
->flags
, req
->timeout
);
646 /* queue an APC for a thread */
647 DECL_HANDLER(queue_apc
)
649 struct thread
*thread
;
650 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
652 thread_queue_apc( thread
, req
->func
, req
->param
);
653 release_object( thread
);
655 send_reply( current
, -1, 0 );