2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
13 #include <sys/types.h>
20 #include "server/thread.h"
25 struct wait_queue_entry
27 struct wait_queue_entry
*next
;
28 struct wait_queue_entry
*prev
;
30 struct thread
*thread
;
35 int count
; /* count of objects */
37 struct timeval timeout
;
38 struct wait_queue_entry queues
[1];
42 /* thread operations */
44 static void dump_thread( struct object
*obj
, int verbose
);
45 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
46 static void destroy_thread( struct object
*obj
);
48 static const struct object_ops thread_ops
=
61 static struct thread
*first_thread
;
64 /* create a new thread */
65 struct thread
*create_thread( int fd
, void *pid
, int *thread_handle
,
68 struct thread
*thread
;
69 struct process
*process
;
71 if (!(thread
= mem_alloc( sizeof(*thread
) ))) return NULL
;
73 if (pid
) process
= get_process_from_id( pid
);
74 else process
= create_process();
81 init_object( &thread
->obj
, &thread_ops
, NULL
);
82 thread
->client_fd
= fd
;
83 thread
->process
= process
;
84 thread
->unix_pid
= 0; /* not known yet */
89 thread
->state
= STARTING
;
90 thread
->exit_code
= 0x103; /* STILL_ACTIVE */
91 thread
->next
= first_thread
;
94 if (first_thread
) first_thread
->prev
= thread
;
95 first_thread
= thread
;
96 add_process_thread( process
, thread
);
98 *thread_handle
= *process_handle
= -1;
101 if ((*thread_handle
= alloc_handle( current
->process
, thread
,
102 THREAD_ALL_ACCESS
, 0 )) == -1)
107 if ((*process_handle
= alloc_handle( current
->process
, process
,
108 PROCESS_ALL_ACCESS
, 0 )) == -1)
112 if (add_client( fd
, thread
) == -1) goto error
;
119 close_handle( current
->process
, *thread_handle
);
120 close_handle( current
->process
, *process_handle
);
122 remove_process_thread( process
, thread
);
123 release_object( thread
);
127 /* destroy a thread when its refcount is 0 */
128 static void destroy_thread( struct object
*obj
)
130 struct thread
*thread
= (struct thread
*)obj
;
131 assert( obj
->ops
== &thread_ops
);
133 release_object( thread
->process
);
134 if (thread
->next
) thread
->next
->prev
= thread
->prev
;
135 if (thread
->prev
) thread
->prev
->next
= thread
->next
;
136 else first_thread
= thread
->next
;
137 if (thread
->name
) free( thread
->name
);
138 if (debug_level
) memset( thread
, 0xaa, sizeof(thread
) ); /* catch errors */
142 /* dump a thread on stdout for debugging purposes */
143 static void dump_thread( struct object
*obj
, int verbose
)
145 struct thread
*thread
= (struct thread
*)obj
;
146 assert( obj
->ops
== &thread_ops
);
148 printf( "Thread pid=%d fd=%d name='%s'\n",
149 thread
->unix_pid
, thread
->client_fd
, thread
->name
);
152 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
154 struct thread
*mythread
= (struct thread
*)obj
;
155 return (mythread
->state
== TERMINATED
);
158 /* get a thread pointer from a thread id (and increment the refcount) */
159 struct thread
*get_thread_from_id( void *id
)
161 struct thread
*t
= first_thread
;
162 while (t
&& (t
!= id
)) t
= t
->next
;
163 if (t
) grab_object( t
);
167 /* get a thread from a handle (and increment the refcount) */
168 struct thread
*get_thread_from_handle( int handle
, unsigned int access
)
170 return (struct thread
*)get_handle_obj( current
->process
, handle
,
171 access
, &thread_ops
);
174 /* get all information about a thread */
175 void get_thread_info( struct thread
*thread
,
176 struct get_thread_info_reply
*reply
)
179 reply
->exit_code
= thread
->exit_code
;
182 /* send a reply to a thread */
183 int send_reply( struct thread
*thread
, int pass_fd
, int n
,
184 ... /* arg_1, len_1, ..., arg_n, len_n */ )
186 struct iovec vec
[16];
192 for (i
= 0; i
< n
; i
++)
194 vec
[i
].iov_base
= va_arg( args
, void * );
195 vec
[i
].iov_len
= va_arg( args
, int );
198 return send_reply_v( thread
->client_fd
, thread
->error
, pass_fd
, vec
, n
);
201 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
202 void add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
206 entry
->prev
= obj
->tail
;
208 if (obj
->tail
) obj
->tail
->next
= entry
;
209 else obj
->head
= entry
;
213 /* remove a thread from an object wait queue */
214 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
216 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
217 else obj
->tail
= entry
->prev
;
218 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
219 else obj
->head
= entry
->next
;
220 release_object( obj
);
224 static void end_wait( struct thread
*thread
)
226 struct thread_wait
*wait
= thread
->wait
;
227 struct wait_queue_entry
*entry
;
231 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
232 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
233 if (wait
->flags
& SELECT_TIMEOUT
) set_select_timeout( thread
->client_fd
, NULL
);
238 /* build the thread wait structure */
239 static int wait_on( struct thread
*thread
, int count
,
240 int *handles
, int flags
, int timeout
)
242 struct thread_wait
*wait
;
243 struct wait_queue_entry
*entry
;
247 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
249 SET_ERROR( ERROR_INVALID_PARAMETER
);
252 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
256 if (flags
& SELECT_TIMEOUT
)
258 gettimeofday( &wait
->timeout
, 0 );
261 wait
->timeout
.tv_usec
+= (timeout
% 1000) * 1000;
262 if (wait
->timeout
.tv_usec
>= 1000000)
264 wait
->timeout
.tv_usec
-= 1000000;
265 wait
->timeout
.tv_sec
++;
267 wait
->timeout
.tv_sec
+= timeout
/ 1000;
271 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
273 if (!(obj
= get_handle_obj( thread
->process
, handles
[i
],
274 SYNCHRONIZE
, NULL
)))
280 entry
->thread
= thread
;
281 obj
->ops
->add_queue( obj
, entry
);
282 release_object( obj
);
287 /* check if the thread waiting condition is satisfied */
288 static int check_wait( struct thread
*thread
, int *signaled
)
291 struct thread_wait
*wait
= thread
->wait
;
292 struct wait_queue_entry
*entry
= wait
->queues
;
296 if (wait
->flags
& SELECT_ALL
)
298 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
299 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) goto check_timeout
;
300 /* Wait satisfied: tell it to all objects */
302 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
303 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
304 *signaled
= STATUS_ABANDONED_WAIT_0
;
309 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
311 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
312 /* Wait satisfied: tell it to the object */
314 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
315 *signaled
+= STATUS_ABANDONED_WAIT_0
;
320 if (!(wait
->flags
& SELECT_TIMEOUT
)) return 0;
321 gettimeofday( &now
, NULL
);
322 if ((now
.tv_sec
> wait
->timeout
.tv_sec
) ||
323 ((now
.tv_sec
== wait
->timeout
.tv_sec
) &&
324 (now
.tv_usec
>= wait
->timeout
.tv_usec
)))
326 *signaled
= STATUS_TIMEOUT
;
332 /* sleep on a list of objects */
333 void sleep_on( struct thread
*thread
, int count
, int *handles
, int flags
, int timeout
)
335 struct select_reply reply
;
337 assert( !thread
->wait
);
339 if (!wait_on( thread
, count
, handles
, flags
, timeout
)) goto done
;
340 if (!check_wait( thread
, &reply
.signaled
))
342 /* we need to wait */
343 if (flags
& SELECT_TIMEOUT
)
344 set_select_timeout( thread
->client_fd
, &thread
->wait
->timeout
);
349 send_reply( thread
, -1, 1, &reply
, sizeof(reply
) );
352 /* attempt to wake up a thread */
353 /* return 1 if OK, 0 if the wait condition is still not satisfied */
354 static int wake_thread( struct thread
*thread
)
356 struct select_reply reply
;
358 if (!check_wait( thread
, &reply
.signaled
)) return 0;
360 send_reply( thread
, -1, 1, &reply
, sizeof(reply
) );
364 /* timeout for the current thread */
365 void thread_timeout(void)
367 struct select_reply reply
;
369 assert( current
->wait
);
371 reply
.signaled
= STATUS_TIMEOUT
;
373 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
376 /* attempt to wake threads sleeping on the object wait queue */
377 void wake_up( struct object
*obj
, int max
)
379 struct wait_queue_entry
*entry
= obj
->head
;
383 struct wait_queue_entry
*next
= entry
->next
;
384 if (wake_thread( entry
->thread
))
386 if (max
&& !--max
) break;
392 /* kill a thread on the spot */
393 void kill_thread( struct thread
*thread
, int exit_code
)
395 if (thread
->state
== TERMINATED
) return; /* already killed */
396 if (thread
->unix_pid
) kill( thread
->unix_pid
, SIGTERM
);
397 remove_client( thread
->client_fd
, exit_code
); /* this will call thread_killed */
400 /* a thread has been killed */
401 void thread_killed( struct thread
*thread
, int exit_code
)
403 thread
->state
= TERMINATED
;
404 thread
->exit_code
= exit_code
;
405 if (thread
->wait
) end_wait( thread
);
406 abandon_mutexes( thread
);
407 remove_process_thread( thread
->process
, thread
);
408 wake_up( &thread
->obj
, 0 );
409 release_object( thread
);