2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
13 #include <sys/types.h>
22 #include "server/thread.h"
23 #include "server/process.h"
28 struct wait_queue_entry
30 struct wait_queue_entry
*next
;
31 struct wait_queue_entry
*prev
;
33 struct thread
*thread
;
38 int count
; /* count of objects */
40 struct timeval timeout
;
41 struct wait_queue_entry queues
[1];
44 /* asynchronous procedure calls */
48 void *func
; /* function to call in client */
49 void *param
; /* function param */
51 #define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */
54 /* thread operations */
56 static void dump_thread( struct object
*obj
, int verbose
);
57 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
58 static void destroy_thread( struct object
*obj
);
60 static const struct object_ops thread_ops
=
74 static struct thread
*first_thread
;
77 /* create a new thread */
78 struct thread
*create_thread( int fd
, void *pid
, int *thread_handle
,
81 struct thread
*thread
;
82 struct process
*process
;
84 if (!(thread
= mem_alloc( sizeof(*thread
) ))) return NULL
;
86 if (pid
) process
= get_process_from_id( pid
);
87 else process
= create_process();
94 init_object( &thread
->obj
, &thread_ops
, NULL
);
95 thread
->client_fd
= fd
;
96 thread
->process
= process
;
97 thread
->unix_pid
= 0; /* not known yet */
102 thread
->apc_count
= 0;
104 thread
->state
= STARTING
;
105 thread
->exit_code
= 0x103; /* STILL_ACTIVE */
106 thread
->next
= first_thread
;
108 thread
->priority
= THREAD_PRIORITY_NORMAL
;
109 thread
->affinity
= 1;
112 if (first_thread
) first_thread
->prev
= thread
;
113 first_thread
= thread
;
114 add_process_thread( process
, thread
);
116 *thread_handle
= *process_handle
= -1;
119 if ((*thread_handle
= alloc_handle( current
->process
, thread
,
120 THREAD_ALL_ACCESS
, 0 )) == -1)
125 if ((*process_handle
= alloc_handle( current
->process
, process
,
126 PROCESS_ALL_ACCESS
, 0 )) == -1)
130 if (add_client( fd
, thread
) == -1) goto error
;
137 close_handle( current
->process
, *thread_handle
);
138 close_handle( current
->process
, *process_handle
);
140 remove_process_thread( process
, thread
);
141 release_object( thread
);
145 /* destroy a thread when its refcount is 0 */
146 static void destroy_thread( struct object
*obj
)
148 struct thread
*thread
= (struct thread
*)obj
;
149 assert( obj
->ops
== &thread_ops
);
151 release_object( thread
->process
);
152 if (thread
->next
) thread
->next
->prev
= thread
->prev
;
153 if (thread
->prev
) thread
->prev
->next
= thread
->next
;
154 else first_thread
= thread
->next
;
155 if (thread
->name
) free( thread
->name
);
156 if (thread
->apc
) free( thread
->apc
);
157 if (debug_level
) memset( thread
, 0xaa, sizeof(thread
) ); /* catch errors */
161 /* dump a thread on stdout for debugging purposes */
162 static void dump_thread( struct object
*obj
, int verbose
)
164 struct thread
*thread
= (struct thread
*)obj
;
165 assert( obj
->ops
== &thread_ops
);
167 fprintf( stderr
, "Thread pid=%d fd=%d name='%s'\n",
168 thread
->unix_pid
, thread
->client_fd
, thread
->name
);
171 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
173 struct thread
*mythread
= (struct thread
*)obj
;
174 return (mythread
->state
== TERMINATED
);
177 /* get a thread pointer from a thread id (and increment the refcount) */
178 struct thread
*get_thread_from_id( void *id
)
180 struct thread
*t
= first_thread
;
181 while (t
&& (t
!= id
)) t
= t
->next
;
182 if (t
) grab_object( t
);
186 /* get a thread from a handle (and increment the refcount) */
187 struct thread
*get_thread_from_handle( int handle
, unsigned int access
)
189 return (struct thread
*)get_handle_obj( current
->process
, handle
,
190 access
, &thread_ops
);
193 /* get all information about a thread */
194 void get_thread_info( struct thread
*thread
,
195 struct get_thread_info_reply
*reply
)
198 reply
->exit_code
= thread
->exit_code
;
199 reply
->priority
= thread
->priority
;
203 /* set all information about a thread */
204 void set_thread_info( struct thread
*thread
,
205 struct set_thread_info_request
*req
)
207 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
208 thread
->priority
= req
->priority
;
209 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
211 if (req
->affinity
!= 1) SET_ERROR( ERROR_INVALID_PARAMETER
);
212 else thread
->affinity
= req
->affinity
;
216 /* suspend a thread */
217 int suspend_thread( struct thread
*thread
)
219 int old_count
= thread
->suspend
;
220 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
222 if (!thread
->suspend
++)
224 if (thread
->unix_pid
) kill( thread
->unix_pid
, SIGSTOP
);
230 /* resume a thread */
231 int resume_thread( struct thread
*thread
)
233 int old_count
= thread
->suspend
;
234 if (thread
->suspend
> 0)
236 if (!--thread
->suspend
)
238 if (thread
->unix_pid
) kill( thread
->unix_pid
, SIGCONT
);
244 /* send a reply to a thread */
245 int send_reply( struct thread
*thread
, int pass_fd
, int n
,
246 ... /* arg_1, len_1, ..., arg_n, len_n */ )
248 struct iovec vec
[16];
254 for (i
= 0; i
< n
; i
++)
256 vec
[i
].iov_base
= va_arg( args
, void * );
257 vec
[i
].iov_len
= va_arg( args
, int );
260 return send_reply_v( thread
->client_fd
, thread
->error
, pass_fd
, vec
, n
);
263 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
264 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
268 entry
->prev
= obj
->tail
;
270 if (obj
->tail
) obj
->tail
->next
= entry
;
271 else obj
->head
= entry
;
276 /* remove a thread from an object wait queue */
277 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
279 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
280 else obj
->tail
= entry
->prev
;
281 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
282 else obj
->head
= entry
->next
;
283 release_object( obj
);
287 static void end_wait( struct thread
*thread
)
289 struct thread_wait
*wait
= thread
->wait
;
290 struct wait_queue_entry
*entry
;
294 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
295 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
296 if (wait
->flags
& SELECT_TIMEOUT
) set_select_timeout( thread
->client_fd
, NULL
);
301 /* build the thread wait structure */
302 static int wait_on( struct thread
*thread
, int count
,
303 int *handles
, int flags
, int timeout
)
305 struct thread_wait
*wait
;
306 struct wait_queue_entry
*entry
;
310 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
312 SET_ERROR( ERROR_INVALID_PARAMETER
);
315 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
319 if (flags
& SELECT_TIMEOUT
)
321 gettimeofday( &wait
->timeout
, 0 );
324 wait
->timeout
.tv_usec
+= (timeout
% 1000) * 1000;
325 if (wait
->timeout
.tv_usec
>= 1000000)
327 wait
->timeout
.tv_usec
-= 1000000;
328 wait
->timeout
.tv_sec
++;
330 wait
->timeout
.tv_sec
+= timeout
/ 1000;
334 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
336 if (!(obj
= get_handle_obj( thread
->process
, handles
[i
],
337 SYNCHRONIZE
, NULL
)))
343 entry
->thread
= thread
;
344 if (!obj
->ops
->add_queue( obj
, entry
))
350 release_object( obj
);
355 /* check if the thread waiting condition is satisfied */
356 static int check_wait( struct thread
*thread
, int *signaled
)
359 struct thread_wait
*wait
= thread
->wait
;
360 struct wait_queue_entry
*entry
= wait
->queues
;
363 if (wait
->flags
& SELECT_ALL
)
365 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
366 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) goto other_checks
;
367 /* Wait satisfied: tell it to all objects */
369 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
370 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
371 *signaled
= STATUS_ABANDONED_WAIT_0
;
376 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
378 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
379 /* Wait satisfied: tell it to the object */
381 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
382 *signaled
+= STATUS_ABANDONED_WAIT_0
;
388 if ((wait
->flags
& SELECT_ALERTABLE
) && thread
->apc
)
390 *signaled
= STATUS_USER_APC
;
393 if (wait
->flags
& SELECT_TIMEOUT
)
396 gettimeofday( &now
, NULL
);
397 if ((now
.tv_sec
> wait
->timeout
.tv_sec
) ||
398 ((now
.tv_sec
== wait
->timeout
.tv_sec
) &&
399 (now
.tv_usec
>= wait
->timeout
.tv_usec
)))
401 *signaled
= STATUS_TIMEOUT
;
408 /* send the select reply to wake up the client */
409 static void send_select_reply( struct thread
*thread
, int signaled
)
411 struct select_reply reply
;
412 reply
.signaled
= signaled
;
413 if ((signaled
== STATUS_USER_APC
) && thread
->apc
)
415 struct thread_apc
*apc
= thread
->apc
;
416 int len
= thread
->apc_count
* sizeof(*apc
);
418 thread
->apc_count
= 0;
419 send_reply( thread
, -1, 2, &reply
, sizeof(reply
),
423 else send_reply( thread
, -1, 1, &reply
, sizeof(reply
) );
426 /* attempt to wake up a thread */
427 /* return 1 if OK, 0 if the wait condition is still not satisfied */
428 static int wake_thread( struct thread
*thread
)
432 if (!check_wait( thread
, &signaled
)) return 0;
434 send_select_reply( thread
, signaled
);
438 /* sleep on a list of objects */
439 void sleep_on( struct thread
*thread
, int count
, int *handles
, int flags
, int timeout
)
441 assert( !thread
->wait
);
442 if (!wait_on( thread
, count
, handles
, flags
, timeout
))
444 /* return an error */
445 send_select_reply( thread
, -1 );
448 if (!wake_thread( thread
))
450 /* we need to wait */
451 if (flags
& SELECT_TIMEOUT
)
452 set_select_timeout( thread
->client_fd
, &thread
->wait
->timeout
);
456 /* timeout for the current thread */
457 void thread_timeout(void)
459 assert( current
->wait
);
461 send_select_reply( current
, STATUS_TIMEOUT
);
464 /* attempt to wake threads sleeping on the object wait queue */
465 void wake_up( struct object
*obj
, int max
)
467 struct wait_queue_entry
*entry
= obj
->head
;
471 struct wait_queue_entry
*next
= entry
->next
;
472 if (wake_thread( entry
->thread
))
474 if (max
&& !--max
) break;
480 /* queue an async procedure call */
481 int thread_queue_apc( struct thread
*thread
, void *func
, void *param
)
483 struct thread_apc
*apc
;
486 SET_ERROR( ERROR_INVALID_PARAMETER
);
491 if (!(thread
->apc
= mem_alloc( MAX_THREAD_APC
* sizeof(*apc
) )))
493 thread
->apc_count
= 0;
495 else if (thread
->apc_count
>= MAX_THREAD_APC
) return 0;
496 thread
->apc
[thread
->apc_count
].func
= func
;
497 thread
->apc
[thread
->apc_count
].param
= param
;
499 wake_thread( thread
);
503 /* kill a thread on the spot */
504 void kill_thread( struct thread
*thread
, int exit_code
)
506 if (thread
->state
== TERMINATED
) return; /* already killed */
507 if (thread
->unix_pid
) kill( thread
->unix_pid
, SIGTERM
);
508 remove_client( thread
->client_fd
, exit_code
); /* this will call thread_killed */
511 /* a thread has been killed */
512 void thread_killed( struct thread
*thread
, int exit_code
)
514 thread
->state
= TERMINATED
;
515 thread
->exit_code
= exit_code
;
516 if (thread
->wait
) end_wait( thread
);
517 abandon_mutexes( thread
);
518 remove_process_thread( thread
->process
, thread
);
519 wake_up( &thread
->obj
, 0 );
520 release_object( thread
);