Set clipping region to client window in LISTBOX_Paint.
[wine.git] / server / thread.c
blob18991324f4c54e148d3bb30109906c6d23cb8a97
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 thread_wait
39 int count; /* count of objects */
40 int flags;
41 struct timeval timeout;
42 struct timeout_user *user;
43 sleep_reply reply; /* function to build the reply */
44 struct wait_queue_entry queues[1];
47 /* asynchronous procedure calls */
49 struct thread_apc
51 void *func; /* function to call in client */
52 void *param; /* function param */
54 #define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */
57 /* thread operations */
59 static void dump_thread( struct object *obj, int verbose );
60 static int thread_signaled( struct object *obj, struct thread *thread );
61 extern void thread_poll_event( struct object *obj, int event );
62 static void destroy_thread( struct object *obj );
64 static const struct object_ops thread_ops =
66 sizeof(struct thread), /* size */
67 dump_thread, /* dump */
68 add_queue, /* add_queue */
69 remove_queue, /* remove_queue */
70 thread_signaled, /* signaled */
71 no_satisfied, /* satisfied */
72 NULL, /* get_poll_events */
73 thread_poll_event, /* poll_event */
74 no_read_fd, /* get_read_fd */
75 no_write_fd, /* get_write_fd */
76 no_flush, /* flush */
77 no_get_file_info, /* get_file_info */
78 destroy_thread /* destroy */
81 static struct thread *first_thread;
82 static struct thread *booting_thread;
84 /* allocate the buffer for the communication with the client */
85 static int alloc_client_buffer( struct thread *thread )
87 struct get_thread_buffer_request *req;
88 int fd;
90 if ((fd = create_anonymous_file()) == -1) return -1;
91 if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error;
92 if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE,
93 MAP_SHARED, fd, 0 )) == (void*)-1) goto error;
94 /* build the first request into the buffer and send it */
95 req = thread->buffer;
96 req->pid = get_process_id( thread->process );
97 req->tid = get_thread_id( thread );
98 req->boot = (thread == booting_thread);
99 req->version = SERVER_PROTOCOL_VERSION;
100 set_reply_fd( thread, fd );
101 send_reply( thread );
102 return 1;
104 error:
105 file_set_error();
106 if (fd != -1) close( fd );
107 return 0;
110 /* create a new thread */
111 struct thread *create_thread( int fd, struct process *process )
113 struct thread *thread;
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->context = NULL;
122 thread->teb = NULL;
123 thread->mutex = NULL;
124 thread->debug_ctx = NULL;
125 thread->debug_event = NULL;
126 thread->queue = NULL;
127 thread->info = NULL;
128 thread->wait = NULL;
129 thread->apc = NULL;
130 thread->apc_count = 0;
131 thread->error = 0;
132 thread->pass_fd = -1;
133 thread->state = RUNNING;
134 thread->attached = 0;
135 thread->exit_code = 0;
136 thread->next = NULL;
137 thread->prev = NULL;
138 thread->priority = THREAD_PRIORITY_NORMAL;
139 thread->affinity = 1;
140 thread->suspend = 0;
141 thread->buffer = (void *)-1;
142 thread->last_req = REQ_GET_THREAD_BUFFER;
143 thread->process = (struct process *)grab_object( process );
145 if (!current) current = thread;
147 if (!booting_thread) /* first thread ever */
149 booting_thread = thread;
150 lock_master_socket(1);
153 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
154 first_thread = thread;
156 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
157 if (!alloc_client_buffer( thread )) goto error;
158 return thread;
160 error:
161 release_object( thread );
162 return NULL;
165 /* handle a client event */
166 void thread_poll_event( struct object *obj, int event )
168 struct thread *thread = (struct thread *)obj;
169 assert( obj->ops == &thread_ops );
171 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
172 else
174 if (event & POLLOUT) write_request( thread );
175 if (event & POLLIN) read_request( thread );
179 /* destroy a thread when its refcount is 0 */
180 static void destroy_thread( struct object *obj )
182 struct thread *thread = (struct thread *)obj;
183 assert( obj->ops == &thread_ops );
185 assert( !thread->debug_ctx ); /* cannot still be debugging something */
186 release_object( thread->process );
187 if (thread->next) thread->next->prev = thread->prev;
188 if (thread->prev) thread->prev->next = thread->next;
189 else first_thread = thread->next;
190 if (thread->apc) free( thread->apc );
191 if (thread->info) release_object( thread->info );
192 if (thread->queue) release_object( thread->queue );
193 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
194 if (thread->pass_fd != -1) close( thread->pass_fd );
197 /* dump a thread on stdout for debugging purposes */
198 static void dump_thread( struct object *obj, int verbose )
200 struct thread *thread = (struct thread *)obj;
201 assert( obj->ops == &thread_ops );
203 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
204 thread->unix_pid, thread->teb, thread->state );
207 static int thread_signaled( struct object *obj, struct thread *thread )
209 struct thread *mythread = (struct thread *)obj;
210 return (mythread->state == TERMINATED);
213 /* get a thread pointer from a thread id (and increment the refcount) */
214 struct thread *get_thread_from_id( void *id )
216 struct thread *t = first_thread;
217 while (t && (t != id)) t = t->next;
218 if (t) grab_object( t );
219 return t;
222 /* get a thread from a handle (and increment the refcount) */
223 struct thread *get_thread_from_handle( int handle, unsigned int access )
225 return (struct thread *)get_handle_obj( current->process, handle,
226 access, &thread_ops );
229 /* find a thread from a Unix pid */
230 struct thread *get_thread_from_pid( int pid )
232 struct thread *t = first_thread;
233 while (t && (t->unix_pid != pid)) t = t->next;
234 return t;
237 /* set all information about a thread */
238 static void set_thread_info( struct thread *thread,
239 struct set_thread_info_request *req )
241 if (req->mask & SET_THREAD_INFO_PRIORITY)
242 thread->priority = req->priority;
243 if (req->mask & SET_THREAD_INFO_AFFINITY)
245 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
246 else thread->affinity = req->affinity;
250 /* suspend a thread */
251 int suspend_thread( struct thread *thread, int check_limit )
253 int old_count = thread->suspend;
254 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
256 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
258 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
259 return old_count;
262 /* resume a thread */
263 int resume_thread( struct thread *thread )
265 int old_count = thread->suspend;
266 if (thread->suspend > 0)
268 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
270 return old_count;
273 /* suspend all threads but the current */
274 void suspend_all_threads( void )
276 struct thread *thread;
277 for ( thread = first_thread; thread; thread = thread->next )
278 if ( thread != current )
279 suspend_thread( thread, 0 );
282 /* resume all threads but the current */
283 void resume_all_threads( void )
285 struct thread *thread;
286 for ( thread = first_thread; thread; thread = thread->next )
287 if ( thread != current )
288 resume_thread( thread );
291 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
292 int add_queue( struct object *obj, struct wait_queue_entry *entry )
294 grab_object( obj );
295 entry->obj = obj;
296 entry->prev = obj->tail;
297 entry->next = NULL;
298 if (obj->tail) obj->tail->next = entry;
299 else obj->head = entry;
300 obj->tail = entry;
301 return 1;
304 /* remove a thread from an object wait queue */
305 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
307 if (entry->next) entry->next->prev = entry->prev;
308 else obj->tail = entry->prev;
309 if (entry->prev) entry->prev->next = entry->next;
310 else obj->head = entry->next;
311 release_object( obj );
314 /* finish waiting */
315 static void end_wait( struct thread *thread )
317 struct thread_wait *wait = thread->wait;
318 struct wait_queue_entry *entry;
319 int i;
321 assert( wait );
322 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
323 entry->obj->ops->remove_queue( entry->obj, entry );
324 if (wait->user) remove_timeout_user( wait->user );
325 free( wait );
326 thread->wait = NULL;
329 /* build the thread wait structure */
330 static int wait_on( int count, struct object *objects[], int flags,
331 int timeout, sleep_reply func )
333 struct thread_wait *wait;
334 struct wait_queue_entry *entry;
335 int i;
337 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
338 current->wait = wait;
339 wait->count = count;
340 wait->flags = flags;
341 wait->user = NULL;
342 wait->reply = func;
343 if (flags & SELECT_TIMEOUT)
345 gettimeofday( &wait->timeout, 0 );
346 add_timeout( &wait->timeout, timeout );
349 for (i = 0, entry = wait->queues; i < count; i++, entry++)
351 struct object *obj = objects[i];
352 entry->thread = current;
353 if (!obj->ops->add_queue( obj, entry ))
355 wait->count = i;
356 end_wait( current );
357 return 0;
360 return 1;
363 /* check if the thread waiting condition is satisfied */
364 static int check_wait( struct thread *thread, struct object **object )
366 int i, signaled;
367 struct thread_wait *wait = thread->wait;
368 struct wait_queue_entry *entry = wait->queues;
370 assert( wait );
371 *object = NULL;
372 if (wait->flags & SELECT_ALL)
374 int not_ok = 0;
375 /* Note: we must check them all anyway, as some objects may
376 * want to do something when signaled, even if others are not */
377 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
378 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
379 if (not_ok) goto other_checks;
380 /* Wait satisfied: tell it to all objects */
381 signaled = 0;
382 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
383 if (entry->obj->ops->satisfied( entry->obj, thread ))
384 signaled = STATUS_ABANDONED_WAIT_0;
385 return signaled;
387 else
389 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
391 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
392 /* Wait satisfied: tell it to the object */
393 signaled = i;
394 *object = entry->obj;
395 if (entry->obj->ops->satisfied( entry->obj, thread ))
396 signaled = i + STATUS_ABANDONED_WAIT_0;
397 return signaled;
401 other_checks:
402 if ((wait->flags & SELECT_ALERTABLE) && thread->apc) return STATUS_USER_APC;
403 if (wait->flags & SELECT_TIMEOUT)
405 struct timeval now;
406 gettimeofday( &now, NULL );
407 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
409 return -1;
412 /* build a reply to the select request */
413 static void build_select_reply( struct thread *thread, struct object *obj, int signaled )
415 struct select_request *req = get_req_ptr( thread );
416 req->signaled = signaled;
419 /* attempt to wake up a thread */
420 /* return 1 if OK, 0 if the wait condition is still not satisfied */
421 static int wake_thread( struct thread *thread )
423 int signaled;
424 struct object *object;
425 if ((signaled = check_wait( thread, &object )) == -1) return 0;
426 thread->error = 0;
427 thread->wait->reply( thread, object, signaled );
428 end_wait( thread );
429 return 1;
432 /* thread wait timeout */
433 static void thread_timeout( void *ptr )
435 struct thread *thread = ptr;
436 if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread );
437 assert( thread->wait );
438 thread->error = 0;
439 thread->wait->user = NULL;
440 thread->wait->reply( thread, NULL, STATUS_TIMEOUT );
441 end_wait( thread );
442 send_reply( thread );
445 /* sleep on a list of objects */
446 int sleep_on( int count, struct object *objects[], int flags, int timeout, sleep_reply func )
448 assert( !current->wait );
449 if (!wait_on( count, objects, flags, timeout, func )) return 0;
450 if (wake_thread( current )) return 1;
451 /* now we need to wait */
452 if (flags & SELECT_TIMEOUT)
454 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
455 thread_timeout, current )))
457 end_wait( current );
458 return 0;
461 return 1;
464 /* select on a list of handles */
465 static int select_on( int count, int *handles, int flags, int timeout )
467 int ret = 0;
468 int i;
469 struct object *objects[MAXIMUM_WAIT_OBJECTS];
471 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
473 set_error( STATUS_INVALID_PARAMETER );
474 return 0;
476 for (i = 0; i < count; i++)
478 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
479 break;
481 if (i == count) ret = sleep_on( count, objects, flags, timeout, build_select_reply );
482 while (--i >= 0) release_object( objects[i] );
483 return ret;
486 /* attempt to wake threads sleeping on the object wait queue */
487 void wake_up( struct object *obj, int max )
489 struct wait_queue_entry *entry = obj->head;
491 while (entry)
493 struct thread *thread = entry->thread;
494 entry = entry->next;
495 if (wake_thread( thread ))
497 send_reply( thread );
498 if (max && !--max) break;
503 /* queue an async procedure call */
504 static int thread_queue_apc( struct thread *thread, void *func, void *param )
506 struct thread_apc *apc;
507 if (!thread->apc)
509 if (!(thread->apc = mem_alloc( MAX_THREAD_APC * sizeof(*apc) )))
510 return 0;
511 thread->apc_count = 0;
513 else if (thread->apc_count >= MAX_THREAD_APC) return 0;
514 thread->apc[thread->apc_count].func = func;
515 thread->apc[thread->apc_count].param = param;
516 thread->apc_count++;
517 if (thread->wait)
519 if (wake_thread( thread )) send_reply( thread );
521 return 1;
524 /* retrieve an LDT selector entry */
525 static void get_selector_entry( struct thread *thread, int entry,
526 unsigned int *base, unsigned int *limit,
527 unsigned char *flags )
529 if (!thread->process->ldt_copy || !thread->process->ldt_flags)
531 set_error( STATUS_ACCESS_DENIED );
532 return;
534 if (entry >= 8192)
536 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
537 return;
539 if (suspend_for_ptrace( thread ))
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];
548 done:
549 resume_thread( thread );
553 /* kill a thread on the spot */
554 void kill_thread( struct thread *thread, int violent_death )
556 if (thread->state == TERMINATED) return; /* already killed */
557 thread->state = TERMINATED;
558 if (current == thread) current = NULL;
559 if (debug_level)
560 fprintf( stderr,"%08x: *killed* exit_code=%d\n",
561 (unsigned int)thread, thread->exit_code );
562 if (thread->wait)
564 end_wait( thread );
565 /* if it is waiting on the socket, we don't need to send a SIGTERM */
566 violent_death = 0;
568 debug_exit_thread( thread );
569 abandon_mutexes( thread );
570 remove_process_thread( thread->process, thread );
571 wake_up( &thread->obj, 0 );
572 detach_thread( thread, violent_death ? SIGTERM : 0 );
573 remove_select_user( &thread->obj );
574 munmap( thread->buffer, MAX_REQUEST_LENGTH );
575 thread->buffer = (void *)-1;
576 release_object( thread );
579 /* take a snapshot of currently running threads */
580 struct thread_snapshot *thread_snap( int *count )
582 struct thread_snapshot *snapshot, *ptr;
583 struct thread *thread;
584 int total = 0;
586 for (thread = first_thread; thread; thread = thread->next)
587 if (thread->state != TERMINATED) total++;
588 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
589 ptr = snapshot;
590 for (thread = first_thread; thread; thread = thread->next)
592 if (thread->state == TERMINATED) continue;
593 ptr->thread = thread;
594 ptr->count = thread->obj.refcount;
595 ptr->priority = thread->priority;
596 grab_object( thread );
597 ptr++;
599 *count = total;
600 return snapshot;
603 /* signal that we are finished booting on the client side */
604 DECL_HANDLER(boot_done)
606 debug_level = max( debug_level, req->debug_level );
607 /* Make sure last_req is initialized */
608 current->last_req = REQ_BOOT_DONE;
609 if (current == booting_thread)
611 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
612 lock_master_socket(0); /* allow other clients now */
616 /* create a new thread */
617 DECL_HANDLER(new_thread)
619 struct thread *thread;
620 int sock[2];
622 if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1)
624 if ((thread = create_thread( sock[0], current->process )))
626 if (req->suspend) thread->suspend++;
627 req->tid = thread;
628 if ((req->handle = alloc_handle( current->process, thread,
629 THREAD_ALL_ACCESS, req->inherit )) != -1)
631 set_reply_fd( current, sock[1] );
632 /* thread object will be released when the thread gets killed */
633 add_process_thread( current->process, thread );
634 return;
636 release_object( thread );
638 close( sock[1] );
640 else file_set_error();
643 /* retrieve the thread buffer file descriptor */
644 DECL_HANDLER(get_thread_buffer)
646 fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" );
649 /* initialize a new thread */
650 DECL_HANDLER(init_thread)
652 if (current->unix_pid)
654 fatal_protocol_error( current, "init_thread: already running\n" );
655 return;
657 current->unix_pid = req->unix_pid;
658 current->teb = req->teb;
659 if (current->suspend + current->process->suspend > 0) stop_thread( current );
660 if (current->process->running_threads > 1)
661 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
664 /* terminate a thread */
665 DECL_HANDLER(terminate_thread)
667 struct thread *thread;
669 req->self = 0;
670 req->last = 0;
671 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
673 thread->exit_code = req->exit_code;
674 if (thread != current) kill_thread( thread, 1 );
675 else
677 req->self = 1;
678 req->last = (thread->process->running_threads == 1);
680 release_object( thread );
684 /* fetch information about a thread */
685 DECL_HANDLER(get_thread_info)
687 struct thread *thread;
688 int handle = req->handle;
690 if (handle == -1) thread = get_thread_from_id( req->tid_in );
691 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
693 if (thread)
695 req->tid = get_thread_id( thread );
696 req->teb = thread->teb;
697 req->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
698 req->priority = thread->priority;
699 release_object( thread );
703 /* set information about a thread */
704 DECL_HANDLER(set_thread_info)
706 struct thread *thread;
708 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
710 set_thread_info( thread, req );
711 release_object( thread );
715 /* suspend a thread */
716 DECL_HANDLER(suspend_thread)
718 struct thread *thread;
720 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
722 req->count = suspend_thread( thread, 1 );
723 release_object( thread );
727 /* resume a thread */
728 DECL_HANDLER(resume_thread)
730 struct thread *thread;
732 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
734 req->count = resume_thread( thread );
735 release_object( thread );
739 /* select on a handle list */
740 DECL_HANDLER(select)
742 if (!select_on( req->count, req->handles, req->flags, req->timeout ))
743 req->signaled = -1;
746 /* queue an APC for a thread */
747 DECL_HANDLER(queue_apc)
749 struct thread *thread;
750 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
752 thread_queue_apc( thread, req->func, req->param );
753 release_object( thread );
757 /* get list of APC to call */
758 DECL_HANDLER(get_apcs)
760 if ((req->count = current->apc_count))
762 memcpy( req->apcs, current->apc, current->apc_count * sizeof(*current->apc) );
763 free( current->apc );
764 current->apc = NULL;
765 current->apc_count = 0;
769 /* fetch a selector entry for a thread */
770 DECL_HANDLER(get_selector_entry)
772 struct thread *thread;
773 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
775 get_selector_entry( thread, req->entry, &req->base, &req->limit, &req->flags );
776 release_object( thread );