GetLongPathName rewrite.
[wine.git] / server / thread.c
blobb04215893fa1556fe217d943e8eac66a43f400bd
1 /*
2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #ifdef HAVE_SYS_MMAN_H
16 #include <sys/mman.h>
17 #endif
18 #include <sys/types.h>
19 #ifdef HAVE_SYS_SOCKET_H
20 # include <sys/socket.h>
21 #endif
22 #include <sys/uio.h>
23 #include <unistd.h>
24 #include <stdarg.h>
27 #include "winbase.h"
29 #include "handle.h"
30 #include "process.h"
31 #include "thread.h"
32 #include "request.h"
35 /* thread queues */
37 struct wait_queue_entry
39 struct wait_queue_entry *next;
40 struct wait_queue_entry *prev;
41 struct object *obj;
42 struct thread *thread;
45 struct thread_wait
47 int count; /* count of objects */
48 int flags;
49 struct timeval timeout;
50 struct timeout_user *user;
51 sleep_reply reply; /* function to build the reply */
52 struct wait_queue_entry queues[1];
55 /* asynchronous procedure calls */
57 struct thread_apc
59 void *func; /* function to call in client */
60 void *param; /* function param */
62 #define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */
65 /* thread operations */
67 static void dump_thread( struct object *obj, int verbose );
68 static int thread_signaled( struct object *obj, struct thread *thread );
69 extern void thread_poll_event( struct object *obj, int event );
70 static void destroy_thread( struct object *obj );
72 static const struct object_ops thread_ops =
74 sizeof(struct thread), /* size */
75 dump_thread, /* dump */
76 add_queue, /* add_queue */
77 remove_queue, /* remove_queue */
78 thread_signaled, /* signaled */
79 no_satisfied, /* satisfied */
80 NULL, /* get_poll_events */
81 thread_poll_event, /* poll_event */
82 no_read_fd, /* get_read_fd */
83 no_write_fd, /* get_write_fd */
84 no_flush, /* flush */
85 no_get_file_info, /* get_file_info */
86 destroy_thread /* destroy */
89 static struct thread *first_thread;
90 static struct thread *booting_thread;
92 /* allocate the buffer for the communication with the client */
93 static int alloc_client_buffer( struct thread *thread )
95 int fd;
97 if ((fd = create_anonymous_file()) == -1) return -1;
98 if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error;
99 if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE,
100 MAP_SHARED, fd, 0 )) == (void*)-1) goto error;
101 return fd;
103 error:
104 file_set_error();
105 if (fd != -1) close( fd );
106 return -1;
109 /* create a new thread */
110 struct thread *create_thread( int fd, struct process *process, int suspend )
112 struct thread *thread;
113 int buf_fd;
115 int flags = fcntl( fd, F_GETFL, 0 );
116 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
118 if (!(thread = alloc_object( &thread_ops, fd ))) return NULL;
120 thread->unix_pid = 0; /* not known yet */
121 thread->teb = NULL;
122 thread->mutex = NULL;
123 thread->debug_ctx = NULL;
124 thread->wait = NULL;
125 thread->apc = NULL;
126 thread->apc_count = 0;
127 thread->error = 0;
128 thread->pass_fd = -1;
129 thread->state = RUNNING;
130 thread->attached = 0;
131 thread->exit_code = STILL_ACTIVE;
132 thread->next = NULL;
133 thread->prev = NULL;
134 thread->priority = THREAD_PRIORITY_NORMAL;
135 thread->affinity = 1;
136 thread->suspend = (suspend != 0);
137 thread->buffer = (void *)-1;
138 thread->last_req = REQ_GET_THREAD_BUFFER;
139 thread->process = (struct process *)grab_object( process );
141 if (!current) current = thread;
143 if (!booting_thread) /* first thread ever */
145 booting_thread = thread;
146 lock_master_socket(1);
149 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
150 first_thread = thread;
151 add_process_thread( process, thread );
153 if ((buf_fd = alloc_client_buffer( thread )) == -1) goto error;
155 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
156 set_reply_fd( thread, buf_fd ); /* send the fd to the client */
157 send_reply( thread );
158 return thread;
160 error:
161 remove_process_thread( process, thread );
162 release_object( thread );
163 return NULL;
166 /* handle a client event */
167 void thread_poll_event( struct object *obj, int event )
169 struct thread *thread = (struct thread *)obj;
170 assert( obj->ops == &thread_ops );
172 if (event & (POLLERR | POLLHUP)) kill_thread( thread, BROKEN_PIPE );
173 else
175 if (event & POLLOUT) write_request( thread );
176 if (event & POLLIN) read_request( thread );
180 /* destroy a thread when its refcount is 0 */
181 static void destroy_thread( struct object *obj )
183 struct thread *thread = (struct thread *)obj;
184 assert( obj->ops == &thread_ops );
186 assert( !thread->debug_ctx ); /* cannot still be debugging something */
187 release_object( thread->process );
188 if (thread->next) thread->next->prev = thread->prev;
189 if (thread->prev) thread->prev->next = thread->next;
190 else first_thread = thread->next;
191 if (thread->apc) free( thread->apc );
192 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
193 if (thread->pass_fd != -1) close( thread->pass_fd );
196 /* dump a thread on stdout for debugging purposes */
197 static void dump_thread( struct object *obj, int verbose )
199 struct thread *thread = (struct thread *)obj;
200 assert( obj->ops == &thread_ops );
202 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
203 thread->unix_pid, thread->teb, thread->state );
206 static int thread_signaled( struct object *obj, struct thread *thread )
208 struct thread *mythread = (struct thread *)obj;
209 return (mythread->state == TERMINATED);
212 /* get a thread pointer from a thread id (and increment the refcount) */
213 struct thread *get_thread_from_id( void *id )
215 struct thread *t = first_thread;
216 while (t && (t != id)) t = t->next;
217 if (t) grab_object( t );
218 return t;
221 /* get a thread from a handle (and increment the refcount) */
222 struct thread *get_thread_from_handle( int handle, unsigned int access )
224 return (struct thread *)get_handle_obj( current->process, handle,
225 access, &thread_ops );
228 /* find a thread from a Unix pid */
229 struct thread *get_thread_from_pid( int pid )
231 struct thread *t = first_thread;
232 while (t && (t->unix_pid != pid)) t = t->next;
233 return t;
236 /* set all information about a thread */
237 static void set_thread_info( struct thread *thread,
238 struct set_thread_info_request *req )
240 if (req->mask & SET_THREAD_INFO_PRIORITY)
241 thread->priority = req->priority;
242 if (req->mask & SET_THREAD_INFO_AFFINITY)
244 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
245 else thread->affinity = req->affinity;
249 /* suspend a thread */
250 int suspend_thread( struct thread *thread, int check_limit )
252 int old_count = thread->suspend;
253 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
255 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
257 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
258 return old_count;
261 /* resume a thread */
262 int resume_thread( struct thread *thread )
264 int old_count = thread->suspend;
265 if (thread->suspend > 0)
267 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
269 return old_count;
272 /* suspend all threads but the current */
273 void suspend_all_threads( void )
275 struct thread *thread;
276 for ( thread = first_thread; thread; thread = thread->next )
277 if ( thread != current )
278 suspend_thread( thread, 0 );
281 /* resume all threads but the current */
282 void resume_all_threads( void )
284 struct thread *thread;
285 for ( thread = first_thread; thread; thread = thread->next )
286 if ( thread != current )
287 resume_thread( thread );
290 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
291 int add_queue( struct object *obj, struct wait_queue_entry *entry )
293 grab_object( obj );
294 entry->obj = obj;
295 entry->prev = obj->tail;
296 entry->next = NULL;
297 if (obj->tail) obj->tail->next = entry;
298 else obj->head = entry;
299 obj->tail = entry;
300 return 1;
303 /* remove a thread from an object wait queue */
304 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
306 if (entry->next) entry->next->prev = entry->prev;
307 else obj->tail = entry->prev;
308 if (entry->prev) entry->prev->next = entry->next;
309 else obj->head = entry->next;
310 release_object( obj );
313 /* finish waiting */
314 static void end_wait( struct thread *thread )
316 struct thread_wait *wait = thread->wait;
317 struct wait_queue_entry *entry;
318 int i;
320 assert( wait );
321 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
322 entry->obj->ops->remove_queue( entry->obj, entry );
323 if (wait->user) remove_timeout_user( wait->user );
324 free( wait );
325 thread->wait = NULL;
328 /* build the thread wait structure */
329 static int wait_on( int count, struct object *objects[], int flags,
330 int timeout, sleep_reply func )
332 struct thread_wait *wait;
333 struct wait_queue_entry *entry;
334 int i;
336 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
337 current->wait = wait;
338 wait->count = count;
339 wait->flags = flags;
340 wait->user = NULL;
341 wait->reply = func;
342 if (flags & SELECT_TIMEOUT)
344 gettimeofday( &wait->timeout, 0 );
345 add_timeout( &wait->timeout, timeout );
348 for (i = 0, entry = wait->queues; i < count; i++, entry++)
350 struct object *obj = objects[i];
351 entry->thread = current;
352 if (!obj->ops->add_queue( obj, entry ))
354 wait->count = i;
355 end_wait( current );
356 return 0;
359 return 1;
362 /* check if the thread waiting condition is satisfied */
363 static int check_wait( struct thread *thread, struct object **object )
365 int i, signaled;
366 struct thread_wait *wait = thread->wait;
367 struct wait_queue_entry *entry = wait->queues;
369 assert( wait );
370 *object = NULL;
371 if (wait->flags & SELECT_ALL)
373 int not_ok = 0;
374 /* Note: we must check them all anyway, as some objects may
375 * want to do something when signaled, even if others are not */
376 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
377 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
378 if (not_ok) goto other_checks;
379 /* Wait satisfied: tell it to all objects */
380 signaled = 0;
381 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
382 if (entry->obj->ops->satisfied( entry->obj, thread ))
383 signaled = STATUS_ABANDONED_WAIT_0;
384 return signaled;
386 else
388 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
390 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
391 /* Wait satisfied: tell it to the object */
392 signaled = i;
393 *object = entry->obj;
394 if (entry->obj->ops->satisfied( entry->obj, thread ))
395 signaled = i + STATUS_ABANDONED_WAIT_0;
396 return signaled;
400 other_checks:
401 if ((wait->flags & SELECT_ALERTABLE) && thread->apc) return STATUS_USER_APC;
402 if (wait->flags & SELECT_TIMEOUT)
404 struct timeval now;
405 gettimeofday( &now, NULL );
406 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
408 return -1;
411 /* build a reply to the select request */
412 static void build_select_reply( struct thread *thread, struct object *obj, int signaled )
414 struct select_request *req = get_req_ptr( thread );
415 req->signaled = signaled;
418 /* attempt to wake up a thread */
419 /* return 1 if OK, 0 if the wait condition is still not satisfied */
420 static int wake_thread( struct thread *thread )
422 int signaled;
423 struct object *object;
424 if ((signaled = check_wait( thread, &object )) == -1) return 0;
425 thread->error = 0;
426 thread->wait->reply( thread, object, signaled );
427 end_wait( thread );
428 return 1;
431 /* thread wait timeout */
432 static void thread_timeout( void *ptr )
434 struct thread *thread = ptr;
435 if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread );
436 assert( thread->wait );
437 thread->error = 0;
438 thread->wait->user = NULL;
439 thread->wait->reply( thread, NULL, STATUS_TIMEOUT );
440 end_wait( thread );
441 send_reply( thread );
444 /* sleep on a list of objects */
445 int sleep_on( int count, struct object *objects[], int flags, int timeout, sleep_reply func )
447 assert( !current->wait );
448 if (!wait_on( count, objects, flags, timeout, func )) return 0;
449 if (wake_thread( current )) return 1;
450 /* now we need to wait */
451 if (flags & SELECT_TIMEOUT)
453 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
454 thread_timeout, current )))
456 end_wait( current );
457 return 0;
460 return 1;
463 /* select on a list of handles */
464 static int select_on( int count, int *handles, int flags, int timeout )
466 int ret = 0;
467 int i;
468 struct object *objects[MAXIMUM_WAIT_OBJECTS];
470 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
472 set_error( STATUS_INVALID_PARAMETER );
473 return 0;
475 for (i = 0; i < count; i++)
477 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
478 break;
480 if (i == count) ret = sleep_on( count, objects, flags, timeout, build_select_reply );
481 while (--i >= 0) release_object( objects[i] );
482 return ret;
485 /* attempt to wake threads sleeping on the object wait queue */
486 void wake_up( struct object *obj, int max )
488 struct wait_queue_entry *entry = obj->head;
490 while (entry)
492 struct thread *thread = entry->thread;
493 entry = entry->next;
494 if (wake_thread( thread ))
496 send_reply( thread );
497 if (max && !--max) break;
502 /* queue an async procedure call */
503 static int thread_queue_apc( struct thread *thread, void *func, void *param )
505 struct thread_apc *apc;
506 if (!thread->apc)
508 if (!(thread->apc = mem_alloc( MAX_THREAD_APC * sizeof(*apc) )))
509 return 0;
510 thread->apc_count = 0;
512 else if (thread->apc_count >= MAX_THREAD_APC) return 0;
513 thread->apc[thread->apc_count].func = func;
514 thread->apc[thread->apc_count].param = param;
515 thread->apc_count++;
516 if (thread->wait)
518 if (wake_thread( thread )) send_reply( thread );
520 return 1;
523 /* retrieve an LDT selector entry */
524 static void get_selector_entry( struct thread *thread, int entry,
525 unsigned int *base, unsigned int *limit,
526 unsigned char *flags )
528 if (!thread->process->ldt_copy || !thread->process->ldt_flags)
530 set_error( STATUS_ACCESS_DENIED );
531 return;
533 if (entry >= 8192)
535 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
536 return;
538 suspend_thread( thread, 0 );
539 if (thread->attached)
541 unsigned char flags_buf[4];
542 int *addr = (int *)thread->process->ldt_copy + 2 * entry;
543 if (read_thread_int( thread, addr, base ) == -1) goto done;
544 if (read_thread_int( thread, addr + 1, limit ) == -1) goto done;
545 addr = (int *)thread->process->ldt_flags + (entry >> 2);
546 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
547 *flags = flags_buf[entry & 3];
549 else set_error( STATUS_ACCESS_DENIED );
550 done:
551 resume_thread( thread );
554 /* kill a thread on the spot */
555 void kill_thread( struct thread *thread, int exit_code )
557 if (thread->state == TERMINATED) return; /* already killed */
558 thread->state = TERMINATED;
559 thread->exit_code = exit_code;
560 if (current == thread) current = NULL;
561 if (debug_level) trace_kill( thread );
562 if (thread->wait) end_wait( thread );
563 debug_exit_thread( thread, exit_code );
564 abandon_mutexes( thread );
565 remove_process_thread( thread->process, thread );
566 wake_up( &thread->obj, 0 );
567 detach_thread( thread );
568 remove_select_user( &thread->obj );
569 release_object( thread );
572 /* signal that we are finished booting on the client side */
573 DECL_HANDLER(boot_done)
575 debug_level = req->debug_level;
576 /* Make sure last_req is initialized */
577 current->last_req = REQ_BOOT_DONE;
578 if (current == booting_thread)
580 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
581 lock_master_socket(0); /* allow other clients now */
585 /* create a new thread */
586 DECL_HANDLER(new_thread)
588 struct thread *thread;
589 int sock[2];
591 if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1)
593 if ((thread = create_thread( sock[0], current->process, req->suspend )))
595 req->tid = thread;
596 if ((req->handle = alloc_handle( current->process, thread,
597 THREAD_ALL_ACCESS, req->inherit )) != -1)
599 set_reply_fd( current, sock[1] );
600 /* thread object will be released when the thread gets killed */
601 return;
603 release_object( thread );
605 close( sock[1] );
607 else file_set_error();
610 /* retrieve the thread buffer file descriptor */
611 DECL_HANDLER(get_thread_buffer)
613 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
616 /* initialize a new thread */
617 DECL_HANDLER(init_thread)
619 if (current->unix_pid)
621 fatal_protocol_error( current, "init_thread: already running\n" );
622 return;
624 current->unix_pid = req->unix_pid;
625 current->teb = req->teb;
626 if (current->suspend + current->process->suspend > 0) stop_thread( current );
627 req->pid = current->process;
628 req->tid = current;
629 req->boot = (current == booting_thread);
632 /* terminate a thread */
633 DECL_HANDLER(terminate_thread)
635 struct thread *thread;
637 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
639 kill_thread( thread, req->exit_code );
640 release_object( thread );
644 /* fetch information about a thread */
645 DECL_HANDLER(get_thread_info)
647 struct thread *thread;
649 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
651 req->tid = thread;
652 req->exit_code = thread->exit_code;
653 req->priority = thread->priority;
654 release_object( thread );
658 /* set information about a thread */
659 DECL_HANDLER(set_thread_info)
661 struct thread *thread;
663 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
665 set_thread_info( thread, req );
666 release_object( thread );
670 /* suspend a thread */
671 DECL_HANDLER(suspend_thread)
673 struct thread *thread;
675 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
677 req->count = suspend_thread( thread, 1 );
678 release_object( thread );
682 /* resume a thread */
683 DECL_HANDLER(resume_thread)
685 struct thread *thread;
687 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
689 req->count = resume_thread( thread );
690 release_object( thread );
694 /* select on a handle list */
695 DECL_HANDLER(select)
697 if (!select_on( req->count, req->handles, req->flags, req->timeout ))
698 req->signaled = -1;
701 /* queue an APC for a thread */
702 DECL_HANDLER(queue_apc)
704 struct thread *thread;
705 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
707 thread_queue_apc( thread, req->func, req->param );
708 release_object( thread );
712 /* get list of APC to call */
713 DECL_HANDLER(get_apcs)
715 if ((req->count = current->apc_count))
717 memcpy( req->apcs, current->apc, current->apc_count * sizeof(*current->apc) );
718 free( current->apc );
719 current->apc = NULL;
720 current->apc_count = 0;
724 /* fetch a selector entry for a thread */
725 DECL_HANDLER(get_selector_entry)
727 struct thread *thread;
728 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
730 get_selector_entry( thread, req->entry, &req->base, &req->limit, &req->flags );
731 release_object( thread );