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 int thread_satisfied( struct object
*obj
, struct thread
*thread
);
47 static void destroy_thread( struct object
*obj
);
49 static const struct object_ops thread_ops
=
57 static struct thread
*first_thread
;
60 /* create a new thread */
61 struct thread
*create_thread( int fd
, void *pid
, int *thread_handle
,
64 struct thread
*thread
;
65 struct process
*process
;
67 if (!(thread
= mem_alloc( sizeof(*thread
) ))) return NULL
;
69 if (pid
) process
= get_process_from_id( pid
);
70 else process
= create_process();
77 init_object( &thread
->obj
, &thread_ops
, NULL
);
78 thread
->client_fd
= fd
;
79 thread
->process
= process
;
80 thread
->unix_pid
= 0; /* not known yet */
85 thread
->state
= STARTING
;
86 thread
->exit_code
= 0x103; /* STILL_ACTIVE */
87 thread
->next
= first_thread
;
90 if (first_thread
) first_thread
->prev
= thread
;
91 first_thread
= thread
;
92 add_process_thread( process
, thread
);
94 *thread_handle
= *process_handle
= -1;
97 if ((*thread_handle
= alloc_handle( current
->process
, thread
,
98 THREAD_ALL_ACCESS
, 0 )) == -1)
103 if ((*process_handle
= alloc_handle( current
->process
, process
,
104 PROCESS_ALL_ACCESS
, 0 )) == -1)
108 if (add_client( fd
, thread
) == -1) goto error
;
115 close_handle( current
->process
, *thread_handle
);
116 close_handle( current
->process
, *process_handle
);
118 remove_process_thread( process
, thread
);
119 release_object( thread
);
123 /* destroy a thread when its refcount is 0 */
124 static void destroy_thread( struct object
*obj
)
126 struct thread
*thread
= (struct thread
*)obj
;
127 assert( obj
->ops
== &thread_ops
);
129 release_object( thread
->process
);
130 if (thread
->next
) thread
->next
->prev
= thread
->prev
;
131 if (thread
->prev
) thread
->prev
->next
= thread
->next
;
132 else first_thread
= thread
->next
;
133 if (thread
->name
) free( thread
->name
);
134 if (debug_level
) memset( thread
, 0xaa, sizeof(thread
) ); /* catch errors */
138 /* dump a thread on stdout for debugging purposes */
139 static void dump_thread( struct object
*obj
, int verbose
)
141 struct thread
*thread
= (struct thread
*)obj
;
142 assert( obj
->ops
== &thread_ops
);
144 printf( "Thread pid=%d fd=%d name='%s'\n",
145 thread
->unix_pid
, thread
->client_fd
, thread
->name
);
148 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
150 struct thread
*mythread
= (struct thread
*)obj
;
151 return (mythread
->state
== TERMINATED
);
154 static int thread_satisfied( struct object
*obj
, struct thread
*thread
)
159 /* get a thread pointer from a thread id (and increment the refcount) */
160 struct thread
*get_thread_from_id( void *id
)
162 struct thread
*t
= first_thread
;
163 while (t
&& (t
!= id
)) t
= t
->next
;
164 if (t
) grab_object( t
);
168 /* get a thread from a handle (and increment the refcount) */
169 struct thread
*get_thread_from_handle( int handle
, unsigned int access
)
171 return (struct thread
*)get_handle_obj( current
->process
, handle
,
172 access
, &thread_ops
);
175 /* get all information about a thread */
176 void get_thread_info( struct thread
*thread
,
177 struct get_thread_info_reply
*reply
)
180 reply
->exit_code
= thread
->exit_code
;
183 /* send a reply to a thread */
184 int send_reply( struct thread
*thread
, int pass_fd
, int n
,
185 ... /* arg_1, len_1, ..., arg_n, len_n */ )
187 struct iovec vec
[16];
193 for (i
= 0; i
< n
; i
++)
195 vec
[i
].iov_base
= va_arg( args
, void * );
196 vec
[i
].iov_len
= va_arg( args
, int );
199 return send_reply_v( thread
->client_fd
, thread
->error
, pass_fd
, vec
, n
);
202 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
203 static 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 static void remove_queue( struct wait_queue_entry
*entry
)
216 struct object
*obj
= entry
->obj
;
218 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
219 else obj
->tail
= entry
->prev
;
220 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
221 else obj
->head
= entry
->next
;
222 release_object( obj
);
226 static void end_wait( struct thread
*thread
)
228 struct thread_wait
*wait
= thread
->wait
;
229 struct wait_queue_entry
*entry
;
233 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++)
234 remove_queue( entry
++ );
235 if (wait
->flags
& SELECT_TIMEOUT
) set_timeout( thread
->client_fd
, NULL
);
240 /* build the thread wait structure */
241 static int wait_on( struct thread
*thread
, int count
,
242 int *handles
, int flags
, int timeout
)
244 struct thread_wait
*wait
;
245 struct wait_queue_entry
*entry
;
249 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
251 SET_ERROR( ERROR_INVALID_PARAMETER
);
254 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
258 if (flags
& SELECT_TIMEOUT
)
260 gettimeofday( &wait
->timeout
, 0 );
263 wait
->timeout
.tv_usec
+= (timeout
% 1000) * 1000;
264 if (wait
->timeout
.tv_usec
>= 1000000)
266 wait
->timeout
.tv_usec
-= 1000000;
267 wait
->timeout
.tv_sec
++;
269 wait
->timeout
.tv_sec
+= timeout
/ 1000;
273 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
275 if (!(obj
= get_handle_obj( thread
->process
, handles
[i
],
276 SYNCHRONIZE
, NULL
)))
282 entry
->thread
= thread
;
283 add_queue( obj
, entry
);
288 /* check if the thread waiting condition is satisfied */
289 static int check_wait( struct thread
*thread
, int *signaled
)
292 struct thread_wait
*wait
= thread
->wait
;
293 struct wait_queue_entry
*entry
= wait
->queues
;
297 if (wait
->flags
& SELECT_ALL
)
299 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
300 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) goto check_timeout
;
301 /* Wait satisfied: tell it to all objects */
303 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
304 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
305 *signaled
= STATUS_ABANDONED_WAIT_0
;
310 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
312 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
313 /* Wait satisfied: tell it to the object */
315 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
316 *signaled
+= STATUS_ABANDONED_WAIT_0
;
321 if (!(wait
->flags
& SELECT_TIMEOUT
)) return 0;
322 gettimeofday( &now
, NULL
);
323 if ((now
.tv_sec
> wait
->timeout
.tv_sec
) ||
324 ((now
.tv_sec
== wait
->timeout
.tv_sec
) &&
325 (now
.tv_usec
>= wait
->timeout
.tv_usec
)))
327 *signaled
= STATUS_TIMEOUT
;
333 /* sleep on a list of objects */
334 void sleep_on( struct thread
*thread
, int count
, int *handles
, int flags
, int timeout
)
336 struct select_reply reply
;
338 assert( !thread
->wait
);
340 if (!wait_on( thread
, count
, handles
, flags
, timeout
)) goto done
;
341 if (!check_wait( thread
, &reply
.signaled
))
343 /* we need to wait */
344 if (flags
& SELECT_TIMEOUT
)
345 set_timeout( thread
->client_fd
, &thread
->wait
->timeout
);
350 send_reply( thread
, -1, 1, &reply
, sizeof(reply
) );
353 /* attempt to wake up a thread */
354 /* return 1 if OK, 0 if the wait condition is still not satisfied */
355 static int wake_thread( struct thread
*thread
)
357 struct select_reply reply
;
359 if (!check_wait( thread
, &reply
.signaled
)) return 0;
361 send_reply( thread
, -1, 1, &reply
, sizeof(reply
) );
365 /* timeout for the current thread */
366 void thread_timeout(void)
368 struct select_reply reply
;
370 assert( current
->wait
);
372 reply
.signaled
= STATUS_TIMEOUT
;
374 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
377 /* attempt to wake threads sleeping on the object wait queue */
378 void wake_up( struct object
*obj
, int max
)
380 struct wait_queue_entry
*entry
= obj
->head
;
384 struct wait_queue_entry
*next
= entry
->next
;
385 if (wake_thread( entry
->thread
))
387 if (max
&& !--max
) break;
393 /* kill a thread on the spot */
394 void kill_thread( struct thread
*thread
, int exit_code
)
396 if (thread
->state
== TERMINATED
) return; /* already killed */
397 if (thread
->unix_pid
) kill( thread
->unix_pid
, SIGTERM
);
398 remove_client( thread
->client_fd
, exit_code
); /* this will call thread_killed */
401 /* a thread has been killed */
402 void thread_killed( struct thread
*thread
, int exit_code
)
404 thread
->state
= TERMINATED
;
405 thread
->exit_code
= exit_code
;
406 if (thread
->wait
) end_wait( thread
);
407 abandon_mutexes( thread
);
408 remove_process_thread( thread
->process
, thread
);
409 wake_up( &thread
->obj
, 0 );
410 release_object( thread
);