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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
32 #include <sys/types.h>
40 #define WIN32_NO_STATUS
57 struct thread_wait *next; /* next wait structure for this thread */
58 struct thread *thread; /* owner thread */
59 int count; /* count of objects */
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 */
71 struct object obj; /* object header */
72 struct list entry; /* queue linked list */
73 struct object *owner; /* object that queued this apc */
74 void *func; /* function to call in client */
75 enum apc_type type; /* type of apc function */
76 void *arg1; /* function arguments */
81 static void dump_thread_apc( struct object *obj, int verbose );
82 static int thread_apc_signaled( struct object *obj, struct thread *thread );
84 static const struct object_ops thread_apc_ops =
86 sizeof(struct thread_apc), /* size */
87 dump_thread_apc, /* dump */
88 add_queue, /* add_queue */
89 remove_queue, /* remove_queue */
90 thread_apc_signaled, /* signaled */
91 no_satisfied, /* satisfied */
92 no_signal, /* signal */
93 no_get_fd, /* get_fd */
94 no_map_access, /* map_access */
95 no_lookup_name, /* lookup_name */
96 no_close_handle, /* close_handle */
97 no_destroy /* destroy */
101 /* thread operations */
103 static void dump_thread( struct object *obj, int verbose );
104 static int thread_signaled( struct object *obj, struct thread *thread );
105 static unsigned int thread_map_access( struct object *obj, unsigned int access );
106 static void thread_poll_event( struct fd *fd, int event );
107 static void destroy_thread( struct object *obj );
108 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
110 static const struct object_ops thread_ops =
112 sizeof(struct thread), /* size */
113 dump_thread, /* dump */
114 add_queue, /* add_queue */
115 remove_queue, /* remove_queue */
116 thread_signaled, /* signaled */
117 no_satisfied, /* satisfied */
118 no_signal, /* signal */
119 no_get_fd, /* get_fd */
120 thread_map_access, /* map_access */
121 no_lookup_name, /* lookup_name */
122 no_close_handle, /* close_handle */
123 destroy_thread /* destroy */
126 static const struct fd_ops thread_fd_ops =
128 NULL, /* get_poll_events */
129 thread_poll_event, /* poll_event */
130 no_flush, /* flush */
131 no_get_file_info, /* get_file_info */
132 no_queue_async, /* queue_async */
133 no_cancel_async /* cancel_async */
136 static struct list thread_list = LIST_INIT(thread_list);
138 /* initialize the structure for a newly allocated thread */
139 inline static void init_thread_structure( struct thread *thread )
143 thread->unix_pid = -1; /* not known yet */
144 thread->unix_tid = -1; /* not known yet */
145 thread->context = NULL;
146 thread->suspend_context = NULL;
148 thread->debug_ctx = NULL;
149 thread->debug_event = NULL;
150 thread->debug_break = 0;
151 thread->queue = NULL;
154 thread->req_data = NULL;
155 thread->req_toread = 0;
156 thread->reply_data = NULL;
157 thread->reply_towrite = 0;
158 thread->request_fd = NULL;
159 thread->reply_fd = NULL;
160 thread->wait_fd = NULL;
161 thread->state = RUNNING;
162 thread->exit_code = 0;
163 thread->priority = 0;
164 thread->affinity = 1;
166 thread->desktop_users = 0;
167 thread->token = NULL;
169 thread->creation_time = current_time;
170 thread->exit_time.tv_sec = thread->exit_time.tv_usec = 0;
172 list_init( &thread->mutex_list );
173 list_init( &thread->system_apc );
174 list_init( &thread->user_apc );
176 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
177 thread->inflight[i].server = thread->inflight[i].client = -1;
180 /* check if address looks valid for a client-side data structure (TEB etc.) */
181 static inline int is_valid_address( void *addr )
183 return addr && !((unsigned long)addr % sizeof(int));
186 /* create a new thread */
187 struct thread *create_thread( int fd, struct process *process )
189 struct thread *thread;
191 if (!(thread = alloc_object( &thread_ops ))) return NULL;
193 init_thread_structure( thread );
195 thread->process = (struct process *)grab_object( process );
196 thread->desktop = process->desktop;
197 if (!current) current = thread;
199 list_add_head( &thread_list, &thread->entry );
201 if (!(thread->id = alloc_ptid( thread )))
203 release_object( thread );
206 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
208 release_object( thread );
212 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
213 add_process_thread( thread->process, thread );
217 /* handle a client event */
218 static void thread_poll_event( struct fd *fd, int event )
220 struct thread *thread = get_fd_user( fd );
221 assert( thread->obj.ops == &thread_ops );
223 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
224 else if (event & POLLIN) read_request( thread );
225 else if (event & POLLOUT) write_reply( thread );
228 /* cleanup everything that is no longer needed by a dead thread */
229 /* used by destroy_thread and kill_thread */
230 static void cleanup_thread( struct thread *thread )
233 struct thread_apc *apc;
235 while ((apc = thread_dequeue_apc( thread, 0 ))) release_object( apc );
236 free( thread->req_data );
237 free( thread->reply_data );
238 if (thread->request_fd) release_object( thread->request_fd );
239 if (thread->reply_fd) release_object( thread->reply_fd );
240 if (thread->wait_fd) release_object( thread->wait_fd );
241 free( thread->suspend_context );
242 free_msg_queue( thread );
243 cleanup_clipboard_thread(thread);
244 destroy_thread_windows( thread );
245 close_thread_desktop( thread );
246 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
248 if (thread->inflight[i].client != -1)
250 close( thread->inflight[i].server );
251 thread->inflight[i].client = thread->inflight[i].server = -1;
254 thread->req_data = NULL;
255 thread->reply_data = NULL;
256 thread->request_fd = NULL;
257 thread->reply_fd = NULL;
258 thread->wait_fd = NULL;
259 thread->context = NULL;
260 thread->suspend_context = NULL;
264 /* destroy a thread when its refcount is 0 */
265 static void destroy_thread( struct object *obj )
267 struct thread *thread = (struct thread *)obj;
268 assert( obj->ops == &thread_ops );
270 assert( !thread->debug_ctx ); /* cannot still be debugging something */
271 list_remove( &thread->entry );
272 cleanup_thread( thread );
273 release_object( thread->process );
274 if (thread->id) free_ptid( thread->id );
275 if (thread->token) release_object( thread->token );
278 /* dump a thread on stdout for debugging purposes */
279 static void dump_thread( struct object *obj, int verbose )
281 struct thread *thread = (struct thread *)obj;
282 assert( obj->ops == &thread_ops );
284 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
285 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
288 static int thread_signaled( struct object *obj, struct thread *thread )
290 struct thread *mythread = (struct thread *)obj;
291 return (mythread->state == TERMINATED);
294 static unsigned int thread_map_access( struct object *obj, unsigned int access )
296 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
297 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
298 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
299 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
300 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
303 static void dump_thread_apc( struct object *obj, int verbose )
305 struct thread_apc *apc = (struct thread_apc *)obj;
306 assert( obj->ops == &thread_apc_ops );
308 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->type );
311 static int thread_apc_signaled( struct object *obj, struct thread *thread )
316 /* get a thread pointer from a thread id (and increment the refcount) */
317 struct thread *get_thread_from_id( thread_id_t id )
319 struct object *obj = get_ptid_entry( id );
321 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
322 set_error( STATUS_INVALID_CID );
326 /* get a thread from a handle (and increment the refcount) */
327 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
329 return (struct thread *)get_handle_obj( current->process, handle,
330 access, &thread_ops );
333 /* find a thread from a Unix tid */
334 struct thread *get_thread_from_tid( int tid )
336 struct thread *thread;
338 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
340 if (thread->unix_tid == tid) return thread;
345 /* find a thread from a Unix pid */
346 struct thread *get_thread_from_pid( int pid )
348 struct thread *thread;
350 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
352 if (thread->unix_pid == pid) return thread;
357 /* set all information about a thread */
358 static void set_thread_info( struct thread *thread,
359 const struct set_thread_info_request *req )
361 if (req->mask & SET_THREAD_INFO_PRIORITY)
362 thread->priority = req->priority;
363 if (req->mask & SET_THREAD_INFO_AFFINITY)
365 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
366 else thread->affinity = req->affinity;
368 if (req->mask & SET_THREAD_INFO_TOKEN)
369 security_set_thread_token( thread, req->token );
372 /* stop a thread (at the Unix level) */
373 void stop_thread( struct thread *thread )
375 if (thread->context) return; /* already inside a debug event, no need for a signal */
376 /* can't stop a thread while initialisation is in progress */
377 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
380 /* suspend a thread */
381 static int suspend_thread( struct thread *thread )
383 int old_count = thread->suspend;
384 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
386 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
388 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
392 /* resume a thread */
393 static int resume_thread( struct thread *thread )
395 int old_count = thread->suspend;
396 if (thread->suspend > 0)
398 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
403 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
404 int add_queue( struct object *obj, struct wait_queue_entry *entry )
408 list_add_tail( &obj->wait_queue, &entry->entry );
412 /* remove a thread from an object wait queue */
413 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
415 list_remove( &entry->entry );
416 release_object( obj );
420 static void end_wait( struct thread *thread )
422 struct thread_wait *wait = thread->wait;
423 struct wait_queue_entry *entry;
427 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
428 entry->obj->ops->remove_queue( entry->obj, entry );
429 if (wait->user) remove_timeout_user( wait->user );
430 thread->wait = wait->next;
434 /* build the thread wait structure */
435 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
437 struct thread_wait *wait;
438 struct wait_queue_entry *entry;
441 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
442 wait->next = current->wait;
443 wait->thread = current;
447 current->wait = wait;
448 if (flags & SELECT_TIMEOUT)
450 wait->timeout.tv_sec = timeout->sec;
451 wait->timeout.tv_usec = timeout->usec;
454 for (i = 0, entry = wait->queues; i < count; i++, entry++)
456 struct object *obj = objects[i];
457 entry->thread = current;
458 if (!obj->ops->add_queue( obj, entry ))
468 /* check if the thread waiting condition is satisfied */
469 static int check_wait( struct thread *thread )
472 struct thread_wait *wait = thread->wait;
473 struct wait_queue_entry *entry = wait->queues;
475 /* Suspended threads may not acquire locks */
476 if (thread->process->suspend + thread->suspend > 0) return -1;
479 if (wait->flags & SELECT_ALL)
482 /* Note: we must check them all anyway, as some objects may
483 * want to do something when signaled, even if others are not */
484 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
485 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
486 if (not_ok) goto other_checks;
487 /* Wait satisfied: tell it to all objects */
489 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
490 if (entry->obj->ops->satisfied( entry->obj, thread ))
491 signaled = STATUS_ABANDONED_WAIT_0;
496 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
498 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
499 /* Wait satisfied: tell it to the object */
501 if (entry->obj->ops->satisfied( entry->obj, thread ))
502 signaled = i + STATUS_ABANDONED_WAIT_0;
508 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
509 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
510 if (wait->flags & SELECT_TIMEOUT)
512 if (!time_before( ¤t_time, &wait->timeout )) return STATUS_TIMEOUT;
517 /* send the wakeup signal to a thread */
518 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
520 struct wake_up_reply reply;
523 reply.cookie = cookie;
524 reply.signaled = signaled;
525 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
528 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
529 else if (errno == EPIPE)
530 kill_thread( thread, 0 ); /* normal death */
532 fatal_protocol_perror( thread, "write" );
536 /* attempt to wake up a thread */
537 /* return >0 if OK, 0 if the wait condition is still not satisfied */
538 int wake_thread( struct thread *thread )
543 for (count = 0; thread->wait; count++)
545 if ((signaled = check_wait( thread )) == -1) break;
547 cookie = thread->wait->cookie;
548 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
549 thread->id, signaled, cookie );
551 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
557 /* thread wait timeout */
558 static void thread_timeout( void *ptr )
560 struct thread_wait *wait = ptr;
561 struct thread *thread = wait->thread;
562 void *cookie = wait->cookie;
565 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
566 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
568 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
569 thread->id, (int)STATUS_TIMEOUT, cookie );
571 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
572 /* check if other objects have become signaled in the meantime */
573 wake_thread( thread );
576 /* try signaling an event flag, a semaphore or a mutex */
577 static int signal_object( obj_handle_t handle )
582 obj = get_handle_obj( current->process, handle, 0, NULL );
585 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
586 release_object( obj );
591 /* select on a list of handles */
592 static void select_on( int count, void *cookie, const obj_handle_t *handles,
593 int flags, const abs_time_t *timeout, obj_handle_t signal_obj )
596 struct object *objects[MAXIMUM_WAIT_OBJECTS];
598 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
600 set_error( STATUS_INVALID_PARAMETER );
603 for (i = 0; i < count; i++)
605 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
609 if (i < count) goto done;
610 if (!wait_on( count, objects, flags, timeout )) goto done;
612 /* signal the object */
615 if (!signal_object( signal_obj ))
620 /* check if we woke ourselves up */
621 if (!current->wait) goto done;
624 if ((ret = check_wait( current )) != -1)
626 /* condition is already satisfied */
632 /* now we need to wait */
633 if (flags & SELECT_TIMEOUT)
635 if (!(current->wait->user = add_timeout_user( ¤t->wait->timeout,
636 thread_timeout, current->wait )))
642 current->wait->cookie = cookie;
643 set_error( STATUS_PENDING );
646 while (--i >= 0) release_object( objects[i] );
649 /* attempt to wake threads sleeping on the object wait queue */
650 void wake_up( struct object *obj, int max )
652 struct list *ptr, *next;
654 LIST_FOR_EACH_SAFE( ptr, next, &obj->wait_queue )
656 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
657 if (wake_thread( entry->thread ))
659 if (max && !--max) break;
664 /* queue an async procedure call */
665 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
666 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
668 struct thread_apc *apc;
669 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
671 /* cancel a possible previous APC with the same owner */
672 if (owner) thread_cancel_apc( thread, owner, system );
673 if (thread->state == TERMINATED) return 0;
675 if (!(apc = alloc_object( &thread_apc_ops ))) return 0;
682 list_add_tail( queue, &apc->entry );
683 if (!list_prev( queue, &apc->entry )) /* first one */
684 wake_thread( thread );
689 /* cancel the async procedure call owned by a specific object */
690 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
692 struct thread_apc *apc;
693 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
694 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
696 if (apc->owner != owner) continue;
697 list_remove( &apc->entry );
698 release_object( apc );
703 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
704 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
706 struct thread_apc *apc = NULL;
707 struct list *ptr = list_head( &thread->system_apc );
709 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
712 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
718 /* add an fd to the inflight list */
719 /* return list index, or -1 on error */
720 int thread_add_inflight_fd( struct thread *thread, int client, int server )
724 if (server == -1) return -1;
731 /* first check if we already have an entry for this fd */
732 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
733 if (thread->inflight[i].client == client)
735 close( thread->inflight[i].server );
736 thread->inflight[i].server = server;
740 /* now find a free spot to store it */
741 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
742 if (thread->inflight[i].client == -1)
744 thread->inflight[i].client = client;
745 thread->inflight[i].server = server;
751 /* get an inflight fd and purge it from the list */
752 /* the fd must be closed when no longer used */
753 int thread_get_inflight_fd( struct thread *thread, int client )
757 if (client == -1) return -1;
761 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
763 if (thread->inflight[i].client == client)
765 ret = thread->inflight[i].server;
766 thread->inflight[i].server = thread->inflight[i].client = -1;
770 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
774 /* kill a thread on the spot */
775 void kill_thread( struct thread *thread, int violent_death )
777 if (thread->state == TERMINATED) return; /* already killed */
778 thread->state = TERMINATED;
779 thread->exit_time = current_time;
780 if (current == thread) current = NULL;
782 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
783 thread->id, thread->exit_code );
786 while (thread->wait) end_wait( thread );
787 send_thread_wakeup( thread, NULL, STATUS_PENDING );
788 /* if it is waiting on the socket, we don't need to send a SIGTERM */
791 kill_console_processes( thread, 0 );
792 debug_exit_thread( thread );
793 abandon_mutexes( thread );
794 wake_up( &thread->obj, 0 );
795 if (violent_death) send_thread_signal( thread, SIGTERM );
796 cleanup_thread( thread );
797 remove_process_thread( thread->process, thread );
798 release_object( thread );
801 /* trigger a breakpoint event in a given thread */
802 void break_thread( struct thread *thread )
804 struct debug_event_exception data;
806 assert( thread->context );
808 data.record.ExceptionCode = STATUS_BREAKPOINT;
809 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
810 data.record.ExceptionRecord = NULL;
811 data.record.ExceptionAddress = get_context_ip( thread->context );
812 data.record.NumberParameters = 0;
814 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
815 thread->debug_break = 0;
818 /* take a snapshot of currently running threads */
819 struct thread_snapshot *thread_snap( int *count )
821 struct thread_snapshot *snapshot, *ptr;
822 struct thread *thread;
825 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
826 if (thread->state != TERMINATED) total++;
827 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
829 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
831 if (thread->state == TERMINATED) continue;
832 ptr->thread = thread;
833 ptr->count = thread->obj.refcount;
834 ptr->priority = thread->priority;
835 grab_object( thread );
842 /* gets the current impersonation token */
843 struct token *thread_get_impersonation_token( struct thread *thread )
846 return thread->token;
848 return thread->process->token;
851 /* create a new thread */
852 DECL_HANDLER(new_thread)
854 struct thread *thread;
855 int request_fd = thread_get_inflight_fd( current, req->request_fd );
857 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
859 if (request_fd != -1) close( request_fd );
860 set_error( STATUS_INVALID_HANDLE );
864 if ((thread = create_thread( request_fd, current->process )))
866 if (req->suspend) thread->suspend++;
867 reply->tid = get_thread_id( thread );
868 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
870 /* thread object will be released when the thread gets killed */
873 kill_thread( thread, 1 );
877 /* initialize a new thread */
878 DECL_HANDLER(init_thread)
880 struct process *process = current->process;
881 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
882 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
884 if (current->reply_fd) /* already initialised */
886 set_error( STATUS_INVALID_PARAMETER );
890 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
892 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, ¤t->obj );
894 if (!current->reply_fd) goto error;
898 set_error( STATUS_TOO_MANY_OPENED_FILES ); /* most likely reason */
901 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, ¤t->obj )))
904 if (!is_valid_address(req->teb) || !is_valid_address(req->peb) || !is_valid_address(req->ldt_copy))
906 set_error( STATUS_INVALID_PARAMETER );
910 current->unix_pid = req->unix_pid;
911 current->unix_tid = req->unix_tid;
912 current->teb = req->teb;
914 if (!process->peb) /* first thread, initialize the process too */
916 process->unix_pid = current->unix_pid;
917 process->peb = req->peb;
918 process->ldt_copy = req->ldt_copy;
919 reply->info_size = init_process( current );
923 if (process->unix_pid != current->unix_pid)
924 process->unix_pid = -1; /* can happen with linuxthreads */
925 if (current->suspend + process->suspend > 0) stop_thread( current );
926 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
928 debug_level = max( debug_level, req->debug_level );
930 reply->pid = get_process_id( process );
931 reply->tid = get_thread_id( current );
932 reply->version = SERVER_PROTOCOL_VERSION;
933 reply->server_start.sec = server_start_time.tv_sec;
934 reply->server_start.usec = server_start_time.tv_usec;
938 if (reply_fd != -1) close( reply_fd );
939 if (wait_fd != -1) close( wait_fd );
942 /* terminate a thread */
943 DECL_HANDLER(terminate_thread)
945 struct thread *thread;
949 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
951 thread->exit_code = req->exit_code;
952 if (thread != current) kill_thread( thread, 1 );
956 reply->last = (thread->process->running_threads == 1);
958 release_object( thread );
962 /* open a handle to a thread */
963 DECL_HANDLER(open_thread)
965 struct thread *thread = get_thread_from_id( req->tid );
970 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
971 release_object( thread );
975 /* fetch information about a thread */
976 DECL_HANDLER(get_thread_info)
978 struct thread *thread;
979 obj_handle_t handle = req->handle;
981 if (!handle) thread = get_thread_from_id( req->tid_in );
982 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
986 reply->pid = get_process_id( thread->process );
987 reply->tid = get_thread_id( thread );
988 reply->teb = thread->teb;
989 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
990 reply->priority = thread->priority;
991 reply->affinity = thread->affinity;
992 reply->creation_time.sec = thread->creation_time.tv_sec;
993 reply->creation_time.usec = thread->creation_time.tv_usec;
994 reply->exit_time.sec = thread->exit_time.tv_sec;
995 reply->exit_time.usec = thread->exit_time.tv_usec;
996 reply->last = thread->process->running_threads == 1;
998 release_object( thread );
1002 /* set information about a thread */
1003 DECL_HANDLER(set_thread_info)
1005 struct thread *thread;
1007 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1009 set_thread_info( thread, req );
1010 release_object( thread );
1014 /* suspend a thread */
1015 DECL_HANDLER(suspend_thread)
1017 struct thread *thread;
1019 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1021 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1022 else reply->count = suspend_thread( thread );
1023 release_object( thread );
1027 /* resume a thread */
1028 DECL_HANDLER(resume_thread)
1030 struct thread *thread;
1032 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1034 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1035 else reply->count = resume_thread( thread );
1036 release_object( thread );
1040 /* select on a handle list */
1041 DECL_HANDLER(select)
1043 int count = get_req_data_size() / sizeof(obj_handle_t);
1044 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout, req->signal );
1047 /* queue an APC for a thread */
1048 DECL_HANDLER(queue_apc)
1050 struct thread *thread;
1051 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
1053 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
1054 req->arg1, req->arg2, req->arg3 );
1055 release_object( thread );
1059 /* get next APC to call */
1060 DECL_HANDLER(get_apc)
1062 struct thread_apc *apc;
1066 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
1070 reply->type = APC_NONE;
1073 /* Optimization: ignore APCs that have a NULL func; they are only used
1074 * to wake up a thread, but since we got here the thread woke up already.
1075 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1077 if (apc->func || apc->type == APC_ASYNC_IO) break;
1078 release_object( apc );
1080 reply->func = apc->func;
1081 reply->type = apc->type;
1082 reply->arg1 = apc->arg1;
1083 reply->arg2 = apc->arg2;
1084 reply->arg3 = apc->arg3;
1085 release_object( apc );
1088 /* retrieve the current context of a thread */
1089 DECL_HANDLER(get_thread_context)
1091 struct thread *thread;
1094 if (get_reply_max_size() < sizeof(CONTEXT))
1096 set_error( STATUS_INVALID_PARAMETER );
1099 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1103 if (thread != current || !thread->suspend_context)
1105 /* not suspended, shouldn't happen */
1106 set_error( STATUS_INVALID_PARAMETER );
1110 if (thread->context == thread->suspend_context) thread->context = NULL;
1111 set_reply_data_ptr( thread->suspend_context, sizeof(CONTEXT) );
1112 thread->suspend_context = NULL;
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 if ((context = set_reply_data_size( sizeof(CONTEXT) )))
1123 unsigned int flags = get_context_system_regs( req->flags );
1125 memset( context, 0, sizeof(CONTEXT) );
1126 context->ContextFlags = get_context_cpu_flag();
1127 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1128 if (flags) get_thread_context( thread, context, flags );
1130 reply->self = (thread == current);
1131 release_object( thread );
1134 /* set the current context of a thread */
1135 DECL_HANDLER(set_thread_context)
1137 struct thread *thread;
1139 if (get_req_data_size() < sizeof(CONTEXT))
1141 set_error( STATUS_INVALID_PARAMETER );
1144 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1148 if (thread != current || thread->context)
1150 /* nested suspend or exception, shouldn't happen */
1151 set_error( STATUS_INVALID_PARAMETER );
1153 else if ((thread->suspend_context = mem_alloc( sizeof(CONTEXT) )))
1155 memcpy( thread->suspend_context, get_req_data(), sizeof(CONTEXT) );
1156 thread->context = thread->suspend_context;
1157 if (thread->debug_break) break_thread( thread );
1160 else if (thread != current && !thread->context)
1162 /* thread is not suspended, retry (if it's still running) */
1163 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1164 else set_error( STATUS_PENDING );
1168 const CONTEXT *context = get_req_data();
1169 unsigned int flags = get_context_system_regs( req->flags );
1171 if (flags) set_thread_context( thread, context, flags );
1172 if (thread->context && !get_error())
1173 copy_context( thread->context, context, req->flags & ~flags );
1175 reply->self = (thread == current);
1176 release_object( thread );
1179 /* fetch a selector entry for a thread */
1180 DECL_HANDLER(get_selector_entry)
1182 struct thread *thread;
1183 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1185 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1186 release_object( thread );