winedbg: simplify some code.
[wine/wine-gecko.git] / server / thread.c
blobff22cf61879ae579227d9e26b90a01dbb5223552
1 /*
2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <time.h>
35 #ifdef HAVE_POLL_H
36 #include <poll.h>
37 #endif
39 #include "ntstatus.h"
40 #define WIN32_NO_STATUS
41 #include "windef.h"
42 #include "winternl.h"
44 #include "file.h"
45 #include "handle.h"
46 #include "process.h"
47 #include "thread.h"
48 #include "request.h"
49 #include "user.h"
50 #include "security.h"
53 /* thread queues */
55 struct thread_wait
57 struct thread_wait *next; /* next wait structure for this thread */
58 struct thread *thread; /* owner thread */
59 int count; /* count of objects */
60 int flags;
61 void *cookie; /* magic cookie to return to client */
62 struct timeval timeout;
63 struct timeout_user *user;
64 struct wait_queue_entry queues[1];
67 /* asynchronous procedure calls */
69 struct thread_apc
71 struct list entry; /* queue linked list */
72 struct object *owner; /* object that queued this apc */
73 void *func; /* function to call in client */
74 enum apc_type type; /* type of apc function */
75 int nb_args; /* number of arguments */
76 void *arg1; /* function arguments */
77 void *arg2;
78 void *arg3;
82 /* thread operations */
84 static void dump_thread( struct object *obj, int verbose );
85 static int thread_signaled( struct object *obj, struct thread *thread );
86 static void thread_poll_event( struct fd *fd, int event );
87 static void destroy_thread( struct object *obj );
88 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
90 static const struct object_ops thread_ops =
92 sizeof(struct thread), /* size */
93 dump_thread, /* dump */
94 add_queue, /* add_queue */
95 remove_queue, /* remove_queue */
96 thread_signaled, /* signaled */
97 no_satisfied, /* satisfied */
98 no_signal, /* signal */
99 no_get_fd, /* get_fd */
100 no_lookup_name, /* lookup_name */
101 no_close_handle, /* close_handle */
102 destroy_thread /* destroy */
105 static const struct fd_ops thread_fd_ops =
107 NULL, /* get_poll_events */
108 thread_poll_event, /* poll_event */
109 no_flush, /* flush */
110 no_get_file_info, /* get_file_info */
111 no_queue_async, /* queue_async */
112 no_cancel_async /* cancel_async */
115 static struct list thread_list = LIST_INIT(thread_list);
117 /* initialize the structure for a newly allocated thread */
118 inline static void init_thread_structure( struct thread *thread )
120 int i;
122 thread->unix_pid = -1; /* not known yet */
123 thread->unix_tid = -1; /* not known yet */
124 thread->context = NULL;
125 thread->suspend_context = NULL;
126 thread->teb = NULL;
127 thread->debug_ctx = NULL;
128 thread->debug_event = NULL;
129 thread->queue = NULL;
130 thread->wait = NULL;
131 thread->error = 0;
132 thread->req_data = NULL;
133 thread->req_toread = 0;
134 thread->reply_data = NULL;
135 thread->reply_towrite = 0;
136 thread->request_fd = NULL;
137 thread->reply_fd = NULL;
138 thread->wait_fd = NULL;
139 thread->state = RUNNING;
140 thread->attached = 0;
141 thread->exit_code = 0;
142 thread->priority = THREAD_PRIORITY_NORMAL;
143 thread->affinity = 1;
144 thread->suspend = 0;
145 thread->creation_time = time(NULL);
146 thread->exit_time = 0;
147 thread->desktop_users = 0;
149 list_init( &thread->mutex_list );
150 list_init( &thread->system_apc );
151 list_init( &thread->user_apc );
153 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
154 thread->inflight[i].server = thread->inflight[i].client = -1;
157 /* check if address looks valid for a client-side data structure (TEB etc.) */
158 static inline int is_valid_address( void *addr )
160 return addr && !((unsigned int)addr % sizeof(int));
163 /* create a new thread */
164 struct thread *create_thread( int fd, struct process *process )
166 struct thread *thread;
168 if (!(thread = alloc_object( &thread_ops ))) return NULL;
170 init_thread_structure( thread );
172 thread->process = (struct process *)grab_object( process );
173 thread->desktop = process->desktop;
174 if (!current) current = thread;
176 list_add_head( &thread_list, &thread->entry );
178 if (!(thread->id = alloc_ptid( thread )))
180 release_object( thread );
181 return NULL;
183 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
185 release_object( thread );
186 return NULL;
189 thread->token = (struct token *) grab_object( process->token );
191 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
192 add_process_thread( thread->process, thread );
193 return thread;
196 /* handle a client event */
197 static void thread_poll_event( struct fd *fd, int event )
199 struct thread *thread = get_fd_user( fd );
200 assert( thread->obj.ops == &thread_ops );
202 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
203 else if (event & POLLIN) read_request( thread );
204 else if (event & POLLOUT) write_reply( thread );
207 /* cleanup everything that is no longer needed by a dead thread */
208 /* used by destroy_thread and kill_thread */
209 static void cleanup_thread( struct thread *thread )
211 int i;
212 struct thread_apc *apc;
214 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
215 if (thread->req_data) free( thread->req_data );
216 if (thread->reply_data) free( thread->reply_data );
217 if (thread->request_fd) release_object( thread->request_fd );
218 if (thread->reply_fd) release_object( thread->reply_fd );
219 if (thread->wait_fd) release_object( thread->wait_fd );
220 if (thread->suspend_context) free( thread->suspend_context );
221 free_msg_queue( thread );
222 cleanup_clipboard_thread(thread);
223 destroy_thread_windows( thread );
224 close_thread_desktop( thread );
225 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
227 if (thread->inflight[i].client != -1)
229 close( thread->inflight[i].server );
230 thread->inflight[i].client = thread->inflight[i].server = -1;
233 thread->req_data = NULL;
234 thread->reply_data = NULL;
235 thread->request_fd = NULL;
236 thread->reply_fd = NULL;
237 thread->wait_fd = NULL;
238 thread->context = NULL;
239 thread->suspend_context = NULL;
240 thread->desktop = 0;
243 /* destroy a thread when its refcount is 0 */
244 static void destroy_thread( struct object *obj )
246 struct thread *thread = (struct thread *)obj;
247 assert( obj->ops == &thread_ops );
249 assert( !thread->debug_ctx ); /* cannot still be debugging something */
250 list_remove( &thread->entry );
251 cleanup_thread( thread );
252 release_object( thread->process );
253 if (thread->id) free_ptid( thread->id );
254 if (thread->token) release_object( thread->token );
257 /* dump a thread on stdout for debugging purposes */
258 static void dump_thread( struct object *obj, int verbose )
260 struct thread *thread = (struct thread *)obj;
261 assert( obj->ops == &thread_ops );
263 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
264 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
267 static int thread_signaled( struct object *obj, struct thread *thread )
269 struct thread *mythread = (struct thread *)obj;
270 return (mythread->state == TERMINATED);
273 /* get a thread pointer from a thread id (and increment the refcount) */
274 struct thread *get_thread_from_id( thread_id_t id )
276 struct object *obj = get_ptid_entry( id );
278 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
279 set_error( STATUS_INVALID_CID );
280 return NULL;
283 /* get a thread from a handle (and increment the refcount) */
284 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
286 return (struct thread *)get_handle_obj( current->process, handle,
287 access, &thread_ops );
290 /* find a thread from a Unix pid */
291 struct thread *get_thread_from_pid( int pid )
293 struct thread *thread;
295 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
297 if (thread->unix_tid == pid) return thread;
299 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
301 if (thread->unix_pid == pid) return thread;
303 return NULL;
306 /* set all information about a thread */
307 static void set_thread_info( struct thread *thread,
308 const struct set_thread_info_request *req )
310 if (req->mask & SET_THREAD_INFO_PRIORITY)
311 thread->priority = req->priority;
312 if (req->mask & SET_THREAD_INFO_AFFINITY)
314 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
315 else thread->affinity = req->affinity;
317 if (req->mask & SET_THREAD_INFO_TOKEN)
318 security_set_thread_token( thread, req->token );
321 /* stop a thread (at the Unix level) */
322 void stop_thread( struct thread *thread )
324 if (thread->context) return; /* already inside a debug event, no need for a signal */
325 /* can't stop a thread while initialisation is in progress */
326 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
329 /* suspend a thread */
330 static int suspend_thread( struct thread *thread )
332 int old_count = thread->suspend;
333 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
335 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
337 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
338 return old_count;
341 /* resume a thread */
342 static int resume_thread( struct thread *thread )
344 int old_count = thread->suspend;
345 if (thread->suspend > 0)
347 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
349 return old_count;
352 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
353 int add_queue( struct object *obj, struct wait_queue_entry *entry )
355 grab_object( obj );
356 entry->obj = obj;
357 list_add_tail( &obj->wait_queue, &entry->entry );
358 return 1;
361 /* remove a thread from an object wait queue */
362 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
364 list_remove( &entry->entry );
365 release_object( obj );
368 /* finish waiting */
369 static void end_wait( struct thread *thread )
371 struct thread_wait *wait = thread->wait;
372 struct wait_queue_entry *entry;
373 int i;
375 assert( wait );
376 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
377 entry->obj->ops->remove_queue( entry->obj, entry );
378 if (wait->user) remove_timeout_user( wait->user );
379 thread->wait = wait->next;
380 free( wait );
383 /* build the thread wait structure */
384 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
386 struct thread_wait *wait;
387 struct wait_queue_entry *entry;
388 int i;
390 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
391 wait->next = current->wait;
392 wait->thread = current;
393 wait->count = count;
394 wait->flags = flags;
395 wait->user = NULL;
396 current->wait = wait;
397 if (flags & SELECT_TIMEOUT)
399 wait->timeout.tv_sec = timeout->sec;
400 wait->timeout.tv_usec = timeout->usec;
403 for (i = 0, entry = wait->queues; i < count; i++, entry++)
405 struct object *obj = objects[i];
406 entry->thread = current;
407 if (!obj->ops->add_queue( obj, entry ))
409 wait->count = i;
410 end_wait( current );
411 return 0;
414 return 1;
417 /* check if the thread waiting condition is satisfied */
418 static int check_wait( struct thread *thread )
420 int i, signaled;
421 struct thread_wait *wait = thread->wait;
422 struct wait_queue_entry *entry = wait->queues;
424 /* Suspended threads may not acquire locks */
425 if (thread->process->suspend + thread->suspend > 0) return -1;
427 assert( wait );
428 if (wait->flags & SELECT_ALL)
430 int not_ok = 0;
431 /* Note: we must check them all anyway, as some objects may
432 * want to do something when signaled, even if others are not */
433 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
434 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
435 if (not_ok) goto other_checks;
436 /* Wait satisfied: tell it to all objects */
437 signaled = 0;
438 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
439 if (entry->obj->ops->satisfied( entry->obj, thread ))
440 signaled = STATUS_ABANDONED_WAIT_0;
441 return signaled;
443 else
445 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
447 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
448 /* Wait satisfied: tell it to the object */
449 signaled = i;
450 if (entry->obj->ops->satisfied( entry->obj, thread ))
451 signaled = i + STATUS_ABANDONED_WAIT_0;
452 return signaled;
456 other_checks:
457 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
458 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
459 if (wait->flags & SELECT_TIMEOUT)
461 struct timeval now;
462 gettimeofday( &now, NULL );
463 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
465 return -1;
468 /* send the wakeup signal to a thread */
469 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
471 struct wake_up_reply reply;
472 int ret;
474 reply.cookie = cookie;
475 reply.signaled = signaled;
476 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
477 return 0;
478 if (ret >= 0)
479 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
480 else if (errno == EPIPE)
481 kill_thread( thread, 0 ); /* normal death */
482 else
483 fatal_protocol_perror( thread, "write" );
484 return -1;
487 /* attempt to wake up a thread */
488 /* return >0 if OK, 0 if the wait condition is still not satisfied */
489 int wake_thread( struct thread *thread )
491 int signaled, count;
492 void *cookie;
494 for (count = 0; thread->wait; count++)
496 if ((signaled = check_wait( thread )) == -1) break;
498 cookie = thread->wait->cookie;
499 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
500 thread->id, signaled, cookie );
501 end_wait( thread );
502 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
503 break;
505 return count;
508 /* thread wait timeout */
509 static void thread_timeout( void *ptr )
511 struct thread_wait *wait = ptr;
512 struct thread *thread = wait->thread;
513 void *cookie = wait->cookie;
515 wait->user = NULL;
516 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
517 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
519 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
520 thread->id, (int)STATUS_TIMEOUT, cookie );
521 end_wait( thread );
522 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
523 /* check if other objects have become signaled in the meantime */
524 wake_thread( thread );
527 /* try signaling an event flag, a semaphore or a mutex */
528 static int signal_object( obj_handle_t handle )
530 struct object *obj;
531 int ret = 0;
533 obj = get_handle_obj( current->process, handle, 0, NULL );
534 if (obj)
536 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
537 release_object( obj );
539 return ret;
542 /* select on a list of handles */
543 static void select_on( int count, void *cookie, const obj_handle_t *handles,
544 int flags, const abs_time_t *timeout, obj_handle_t signal_obj )
546 int ret, i;
547 struct object *objects[MAXIMUM_WAIT_OBJECTS];
549 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
551 set_error( STATUS_INVALID_PARAMETER );
552 return;
554 for (i = 0; i < count; i++)
556 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
557 break;
560 if (i < count) goto done;
561 if (!wait_on( count, objects, flags, timeout )) goto done;
563 /* signal the object */
564 if (signal_obj)
566 if (!signal_object( signal_obj ))
568 end_wait( current );
569 goto done;
571 /* check if we woke ourselves up */
572 if (!current->wait) goto done;
575 if ((ret = check_wait( current )) != -1)
577 /* condition is already satisfied */
578 end_wait( current );
579 set_error( ret );
580 goto done;
583 /* now we need to wait */
584 if (flags & SELECT_TIMEOUT)
586 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
587 thread_timeout, current->wait )))
589 end_wait( current );
590 goto done;
593 current->wait->cookie = cookie;
594 set_error( STATUS_PENDING );
596 done:
597 while (--i >= 0) release_object( objects[i] );
600 /* attempt to wake threads sleeping on the object wait queue */
601 void wake_up( struct object *obj, int max )
603 struct list *ptr, *next;
605 LIST_FOR_EACH_SAFE( ptr, next, &obj->wait_queue )
607 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
608 if (wake_thread( entry->thread ))
610 if (max && !--max) break;
615 /* queue an async procedure call */
616 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
617 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
619 struct thread_apc *apc;
620 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
622 /* cancel a possible previous APC with the same owner */
623 if (owner) thread_cancel_apc( thread, owner, system );
624 if (thread->state == TERMINATED) return 0;
626 if (!(apc = mem_alloc( sizeof(*apc) ))) return 0;
627 apc->owner = owner;
628 apc->func = func;
629 apc->type = type;
630 apc->arg1 = arg1;
631 apc->arg2 = arg2;
632 apc->arg3 = arg3;
633 list_add_tail( queue, &apc->entry );
634 if (!list_prev( queue, &apc->entry )) /* first one */
635 wake_thread( thread );
637 return 1;
640 /* cancel the async procedure call owned by a specific object */
641 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
643 struct thread_apc *apc;
644 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
645 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
647 if (apc->owner != owner) continue;
648 list_remove( &apc->entry );
649 free( apc );
650 return;
654 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
655 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
657 struct thread_apc *apc = NULL;
658 struct list *ptr = list_head( &thread->system_apc );
660 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
661 if (ptr)
663 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
664 list_remove( ptr );
666 return apc;
669 /* add an fd to the inflight list */
670 /* return list index, or -1 on error */
671 int thread_add_inflight_fd( struct thread *thread, int client, int server )
673 int i;
675 if (server == -1) return -1;
676 if (client == -1)
678 close( server );
679 return -1;
682 /* first check if we already have an entry for this fd */
683 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
684 if (thread->inflight[i].client == client)
686 close( thread->inflight[i].server );
687 thread->inflight[i].server = server;
688 return i;
691 /* now find a free spot to store it */
692 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
693 if (thread->inflight[i].client == -1)
695 thread->inflight[i].client = client;
696 thread->inflight[i].server = server;
697 return i;
699 return -1;
702 /* get an inflight fd and purge it from the list */
703 /* the fd must be closed when no longer used */
704 int thread_get_inflight_fd( struct thread *thread, int client )
706 int i, ret;
708 if (client == -1) return -1;
712 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
714 if (thread->inflight[i].client == client)
716 ret = thread->inflight[i].server;
717 thread->inflight[i].server = thread->inflight[i].client = -1;
718 return ret;
721 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
722 return -1;
725 /* retrieve an LDT selector entry */
726 static void get_selector_entry( struct thread *thread, int entry,
727 unsigned int *base, unsigned int *limit,
728 unsigned char *flags )
730 if (!thread->process->ldt_copy)
732 set_error( STATUS_ACCESS_DENIED );
733 return;
735 if (entry >= 8192)
737 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
738 return;
740 if (suspend_for_ptrace( thread ))
742 unsigned char flags_buf[4];
743 int *addr = (int *)thread->process->ldt_copy + entry;
744 if (read_thread_int( thread, addr, (int *)base ) == -1) goto done;
745 if (read_thread_int( thread, addr + 8192, (int *)limit ) == -1) goto done;
746 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
747 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
748 *flags = flags_buf[entry & 3];
749 done:
750 resume_after_ptrace( thread );
754 /* kill a thread on the spot */
755 void kill_thread( struct thread *thread, int violent_death )
757 if (thread->state == TERMINATED) return; /* already killed */
758 thread->state = TERMINATED;
759 thread->exit_time = time(NULL);
760 if (current == thread) current = NULL;
761 if (debug_level)
762 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
763 thread->id, thread->exit_code );
764 if (thread->wait)
766 while (thread->wait) end_wait( thread );
767 send_thread_wakeup( thread, NULL, STATUS_PENDING );
768 /* if it is waiting on the socket, we don't need to send a SIGTERM */
769 violent_death = 0;
771 kill_console_processes( thread, 0 );
772 debug_exit_thread( thread );
773 abandon_mutexes( thread );
774 wake_up( &thread->obj, 0 );
775 if (violent_death) send_thread_signal( thread, SIGTERM );
776 cleanup_thread( thread );
777 remove_process_thread( thread->process, thread );
778 release_object( thread );
781 /* take a snapshot of currently running threads */
782 struct thread_snapshot *thread_snap( int *count )
784 struct thread_snapshot *snapshot, *ptr;
785 struct thread *thread;
786 int total = 0;
788 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
789 if (thread->state != TERMINATED) total++;
790 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
791 ptr = snapshot;
792 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
794 if (thread->state == TERMINATED) continue;
795 ptr->thread = thread;
796 ptr->count = thread->obj.refcount;
797 ptr->priority = thread->priority;
798 grab_object( thread );
799 ptr++;
801 *count = total;
802 return snapshot;
805 /* gets the current impersonation token */
806 struct token *thread_get_impersonation_token( struct thread *thread )
808 if (thread->token)
809 return thread->token;
810 else
811 return thread->process->token;
814 /* create a new thread */
815 DECL_HANDLER(new_thread)
817 struct thread *thread;
818 int request_fd = thread_get_inflight_fd( current, req->request_fd );
820 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
822 if (request_fd != -1) close( request_fd );
823 set_error( STATUS_INVALID_HANDLE );
824 return;
827 if ((thread = create_thread( request_fd, current->process )))
829 if (req->suspend) thread->suspend++;
830 reply->tid = get_thread_id( thread );
831 if ((reply->handle = alloc_handle( current->process, thread,
832 THREAD_ALL_ACCESS, req->inherit )))
834 /* thread object will be released when the thread gets killed */
835 return;
837 kill_thread( thread, 1 );
841 /* initialize a new thread */
842 DECL_HANDLER(init_thread)
844 struct process *process = current->process;
845 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
846 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
848 if (current->unix_pid != -1)
850 fatal_protocol_error( current, "init_thread: already running\n" );
851 goto error;
853 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
855 fatal_protocol_error( current, "bad reply fd\n" );
856 goto error;
858 if (wait_fd == -1)
860 fatal_protocol_error( current, "bad wait fd\n" );
861 goto error;
863 if (!(current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj )))
865 reply_fd = -1;
866 fatal_protocol_error( current, "could not allocate reply fd\n" );
867 goto error;
869 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj )))
870 return;
872 if (!is_valid_address(req->teb) || !is_valid_address(req->peb) || !is_valid_address(req->ldt_copy))
874 set_error( STATUS_INVALID_PARAMETER );
875 return;
878 current->unix_pid = req->unix_pid;
879 current->unix_tid = req->unix_tid;
880 current->teb = req->teb;
882 if (!process->peb) /* first thread, initialize the process too */
884 process->peb = req->peb;
885 process->ldt_copy = req->ldt_copy;
886 reply->info_size = init_process( current );
888 else
890 if (current->suspend + process->suspend > 0) stop_thread( current );
891 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
893 debug_level = max( debug_level, req->debug_level );
895 reply->pid = get_process_id( process );
896 reply->tid = get_thread_id( current );
897 reply->version = SERVER_PROTOCOL_VERSION;
898 reply->server_start = server_start_time;
899 return;
901 error:
902 if (reply_fd != -1) close( reply_fd );
903 if (wait_fd != -1) close( wait_fd );
906 /* terminate a thread */
907 DECL_HANDLER(terminate_thread)
909 struct thread *thread;
911 reply->self = 0;
912 reply->last = 0;
913 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
915 thread->exit_code = req->exit_code;
916 if (thread != current) kill_thread( thread, 1 );
917 else
919 reply->self = 1;
920 reply->last = (thread->process->running_threads == 1);
922 release_object( thread );
926 /* open a handle to a thread */
927 DECL_HANDLER(open_thread)
929 struct thread *thread = get_thread_from_id( req->tid );
931 reply->handle = 0;
932 if (thread)
934 reply->handle = alloc_handle( current->process, thread, req->access, req->inherit );
935 release_object( thread );
939 /* fetch information about a thread */
940 DECL_HANDLER(get_thread_info)
942 struct thread *thread;
943 obj_handle_t handle = req->handle;
945 if (!handle) thread = get_thread_from_id( req->tid_in );
946 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
948 if (thread)
950 reply->pid = get_process_id( thread->process );
951 reply->tid = get_thread_id( thread );
952 reply->teb = thread->teb;
953 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
954 reply->priority = thread->priority;
955 reply->affinity = thread->affinity;
956 reply->creation_time = thread->creation_time;
957 reply->exit_time = thread->exit_time;
959 release_object( thread );
963 /* set information about a thread */
964 DECL_HANDLER(set_thread_info)
966 struct thread *thread;
968 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
970 set_thread_info( thread, req );
971 release_object( thread );
975 /* suspend a thread */
976 DECL_HANDLER(suspend_thread)
978 struct thread *thread;
980 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
982 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
983 else reply->count = suspend_thread( thread );
984 release_object( thread );
988 /* resume a thread */
989 DECL_HANDLER(resume_thread)
991 struct thread *thread;
993 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
995 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
996 else reply->count = resume_thread( thread );
997 release_object( thread );
1001 /* select on a handle list */
1002 DECL_HANDLER(select)
1004 int count = get_req_data_size() / sizeof(int);
1005 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout, req->signal );
1008 /* queue an APC for a thread */
1009 DECL_HANDLER(queue_apc)
1011 struct thread *thread;
1012 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
1014 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
1015 req->arg1, req->arg2, req->arg3 );
1016 release_object( thread );
1020 /* get next APC to call */
1021 DECL_HANDLER(get_apc)
1023 struct thread_apc *apc;
1025 for (;;)
1027 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
1029 /* no more APCs */
1030 reply->func = NULL;
1031 reply->type = APC_NONE;
1032 return;
1034 /* Optimization: ignore APCs that have a NULL func; they are only used
1035 * to wake up a thread, but since we got here the thread woke up already.
1036 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1038 if (apc->func || apc->type == APC_ASYNC_IO) break;
1039 free( apc );
1041 reply->func = apc->func;
1042 reply->type = apc->type;
1043 reply->arg1 = apc->arg1;
1044 reply->arg2 = apc->arg2;
1045 reply->arg3 = apc->arg3;
1046 free( apc );
1049 /* retrieve the current context of a thread */
1050 DECL_HANDLER(get_thread_context)
1052 struct thread *thread;
1053 void *data;
1055 if (get_reply_max_size() < sizeof(CONTEXT))
1057 set_error( STATUS_INVALID_PARAMETER );
1058 return;
1060 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1062 if (req->suspend)
1064 if (thread != current || !thread->suspend_context)
1066 /* not suspended, shouldn't happen */
1067 set_error( STATUS_INVALID_PARAMETER );
1069 else
1071 if (thread->context == thread->suspend_context) thread->context = NULL;
1072 set_reply_data_ptr( thread->suspend_context, sizeof(CONTEXT) );
1073 thread->suspend_context = NULL;
1076 else if (thread != current && !thread->context)
1078 /* thread is not suspended, retry (if it's still running) */
1079 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1080 else set_error( STATUS_PENDING );
1082 else if ((data = set_reply_data_size( sizeof(CONTEXT) )))
1084 memset( data, 0, sizeof(CONTEXT) );
1085 get_thread_context( thread, data, req->flags );
1087 release_object( thread );
1090 /* set the current context of a thread */
1091 DECL_HANDLER(set_thread_context)
1093 struct thread *thread;
1095 if (get_req_data_size() < sizeof(CONTEXT))
1097 set_error( STATUS_INVALID_PARAMETER );
1098 return;
1100 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1102 if (req->suspend)
1104 if (thread != current || thread->context)
1106 /* nested suspend or exception, shouldn't happen */
1107 set_error( STATUS_INVALID_PARAMETER );
1109 else if ((thread->suspend_context = mem_alloc( sizeof(CONTEXT) )))
1111 memcpy( thread->suspend_context, get_req_data(), sizeof(CONTEXT) );
1112 thread->context = thread->suspend_context;
1115 else if (thread != current && !thread->context)
1117 /* thread is not suspended, retry (if it's still running) */
1118 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1119 else set_error( STATUS_PENDING );
1121 else
1123 set_thread_context( thread, get_req_data(), req->flags );
1125 release_object( thread );
1128 /* fetch a selector entry for a thread */
1129 DECL_HANDLER(get_selector_entry)
1131 struct thread *thread;
1132 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1134 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1135 release_object( thread );