Bugfix: memory overwrite bug.
[wine/multimedia.git] / server / thread.c
blobfff2079aa41a8358fcc4b48fd2ac55910ff2632a
1 /*
2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/uio.h>
15 #include <unistd.h>
17 #include "winnt.h"
18 #include "winerror.h"
19 #include "server.h"
20 #include "server/thread.h"
23 /* thread queues */
25 struct wait_queue_entry
27 struct wait_queue_entry *next;
28 struct wait_queue_entry *prev;
29 struct object *obj;
30 struct thread *thread;
33 struct thread_wait
35 int count; /* count of objects */
36 int flags;
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 =
51 dump_thread,
52 thread_signaled,
53 thread_satisfied,
54 destroy_thread
57 static struct thread *first_thread;
60 /* create a new thread */
61 struct thread *create_thread( int fd, void *pid, int *thread_handle,
62 int *process_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();
71 if (!process)
73 free( thread );
74 return NULL;
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 */
81 thread->name = NULL;
82 thread->mutex = NULL;
83 thread->wait = NULL;
84 thread->error = 0;
85 thread->state = STARTING;
86 thread->exit_code = 0x103; /* STILL_ACTIVE */
87 thread->next = first_thread;
88 thread->prev = NULL;
90 if (first_thread) first_thread->prev = thread;
91 first_thread = thread;
92 add_process_thread( process, thread );
94 *thread_handle = *process_handle = -1;
95 if (current)
97 if ((*thread_handle = alloc_handle( current->process, thread,
98 THREAD_ALL_ACCESS, 0 )) == -1)
99 goto error;
101 if (current && !pid)
103 if ((*process_handle = alloc_handle( current->process, process,
104 PROCESS_ALL_ACCESS, 0 )) == -1)
105 goto error;
108 if (add_client( fd, thread ) == -1) goto error;
110 return thread;
112 error:
113 if (current)
115 close_handle( current->process, *thread_handle );
116 close_handle( current->process, *process_handle );
118 remove_process_thread( process, thread );
119 release_object( thread );
120 return NULL;
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 */
135 free( thread );
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 )
156 return 0;
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 );
165 return 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 )
179 reply->pid = thread;
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];
188 va_list args;
189 int i;
191 assert( n < 16 );
192 va_start( args, n );
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 );
198 va_end( args );
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 )
205 entry->obj = obj;
206 entry->prev = obj->tail;
207 entry->next = NULL;
208 if (obj->tail) obj->tail->next = entry;
209 else obj->head = entry;
210 obj->tail = 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 );
225 /* finish waiting */
226 static void end_wait( struct thread *thread )
228 struct thread_wait *wait = thread->wait;
229 struct wait_queue_entry *entry;
230 int i;
232 assert( wait );
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 );
236 free( wait );
237 thread->wait = 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;
246 struct object *obj;
247 int i;
249 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
251 SET_ERROR( ERROR_INVALID_PARAMETER );
252 return 0;
254 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
255 thread->wait = wait;
256 wait->count = count;
257 wait->flags = flags;
258 if (flags & SELECT_TIMEOUT)
260 gettimeofday( &wait->timeout, 0 );
261 if (timeout)
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 )))
278 wait->count = i - 1;
279 end_wait( thread );
280 return 0;
282 entry->thread = thread;
283 add_queue( obj, entry );
285 return 1;
288 /* check if the thread waiting condition is satisfied */
289 static int check_wait( struct thread *thread, int *signaled )
291 int i;
292 struct thread_wait *wait = thread->wait;
293 struct wait_queue_entry *entry = wait->queues;
294 struct timeval now;
296 assert( wait );
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 */
302 *signaled = 0;
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;
306 return 1;
308 else
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 */
314 *signaled = i;
315 if (entry->obj->ops->satisfied( entry->obj, thread ))
316 *signaled += STATUS_ABANDONED_WAIT_0;
317 return 1;
320 check_timeout:
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;
328 return 1;
330 return 0;
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 );
339 reply.signaled = -1;
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 );
346 return;
348 end_wait( thread );
349 done:
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;
360 end_wait( thread );
361 send_reply( thread, -1, 1, &reply, sizeof(reply) );
362 return 1;
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;
373 end_wait( current );
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;
382 while (entry)
384 struct wait_queue_entry *next = entry->next;
385 if (wake_thread( entry->thread ))
387 if (max && !--max) break;
389 entry = next;
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 );