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 int executed; /* has it been executed by the client? */
75 void *func; /* function to call in client */
76 enum apc_type type; /* type of apc function */
77 void *arg1; /* function arguments */
82 static void dump_thread_apc( struct object *obj, int verbose );
83 static int thread_apc_signaled( struct object *obj, struct thread *thread );
85 static const struct object_ops thread_apc_ops =
87 sizeof(struct thread_apc), /* size */
88 dump_thread_apc, /* dump */
89 add_queue, /* add_queue */
90 remove_queue, /* remove_queue */
91 thread_apc_signaled, /* signaled */
92 no_satisfied, /* satisfied */
93 no_signal, /* signal */
94 no_get_fd, /* get_fd */
95 no_map_access, /* map_access */
96 no_lookup_name, /* lookup_name */
97 no_close_handle, /* close_handle */
98 no_destroy /* destroy */
102 /* thread operations */
104 static void dump_thread( struct object *obj, int verbose );
105 static int thread_signaled( struct object *obj, struct thread *thread );
106 static unsigned int thread_map_access( struct object *obj, unsigned int access );
107 static void thread_poll_event( struct fd *fd, int event );
108 static void destroy_thread( struct object *obj );
109 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
111 static const struct object_ops thread_ops =
113 sizeof(struct thread), /* size */
114 dump_thread, /* dump */
115 add_queue, /* add_queue */
116 remove_queue, /* remove_queue */
117 thread_signaled, /* signaled */
118 no_satisfied, /* satisfied */
119 no_signal, /* signal */
120 no_get_fd, /* get_fd */
121 thread_map_access, /* map_access */
122 no_lookup_name, /* lookup_name */
123 no_close_handle, /* close_handle */
124 destroy_thread /* destroy */
127 static const struct fd_ops thread_fd_ops =
129 NULL, /* get_poll_events */
130 thread_poll_event, /* poll_event */
131 no_flush, /* flush */
132 no_get_file_info, /* get_file_info */
133 no_queue_async, /* queue_async */
134 no_cancel_async /* cancel_async */
137 static struct list thread_list = LIST_INIT(thread_list);
139 /* initialize the structure for a newly allocated thread */
140 inline static void init_thread_structure( struct thread *thread )
144 thread->unix_pid = -1; /* not known yet */
145 thread->unix_tid = -1; /* not known yet */
146 thread->context = NULL;
147 thread->suspend_context = NULL;
149 thread->debug_ctx = NULL;
150 thread->debug_event = NULL;
151 thread->debug_break = 0;
152 thread->queue = NULL;
155 thread->req_data = NULL;
156 thread->req_toread = 0;
157 thread->reply_data = NULL;
158 thread->reply_towrite = 0;
159 thread->request_fd = NULL;
160 thread->reply_fd = NULL;
161 thread->wait_fd = NULL;
162 thread->state = RUNNING;
163 thread->exit_code = 0;
164 thread->priority = 0;
165 thread->affinity = 1;
167 thread->desktop_users = 0;
168 thread->token = NULL;
170 thread->creation_time = current_time;
171 thread->exit_time.tv_sec = thread->exit_time.tv_usec = 0;
173 list_init( &thread->mutex_list );
174 list_init( &thread->system_apc );
175 list_init( &thread->user_apc );
177 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
178 thread->inflight[i].server = thread->inflight[i].client = -1;
181 /* check if address looks valid for a client-side data structure (TEB etc.) */
182 static inline int is_valid_address( void *addr )
184 return addr && !((unsigned long)addr % sizeof(int));
187 /* create a new thread */
188 struct thread *create_thread( int fd, struct process *process )
190 struct thread *thread;
192 if (!(thread = alloc_object( &thread_ops ))) return NULL;
194 init_thread_structure( thread );
196 thread->process = (struct process *)grab_object( process );
197 thread->desktop = process->desktop;
198 if (!current) current = thread;
200 list_add_head( &thread_list, &thread->entry );
202 if (!(thread->id = alloc_ptid( thread )))
204 release_object( thread );
207 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
209 release_object( thread );
213 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
214 add_process_thread( thread->process, thread );
218 /* handle a client event */
219 static void thread_poll_event( struct fd *fd, int event )
221 struct thread *thread = get_fd_user( fd );
222 assert( thread->obj.ops == &thread_ops );
224 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
225 else if (event & POLLIN) read_request( thread );
226 else if (event & POLLOUT) write_reply( thread );
229 /* cleanup everything that is no longer needed by a dead thread */
230 /* used by destroy_thread and kill_thread */
231 static void cleanup_thread( struct thread *thread )
234 struct thread_apc *apc;
236 while ((apc = thread_dequeue_apc( thread, 0 ))) release_object( apc );
237 free( thread->req_data );
238 free( thread->reply_data );
239 if (thread->request_fd) release_object( thread->request_fd );
240 if (thread->reply_fd) release_object( thread->reply_fd );
241 if (thread->wait_fd) release_object( thread->wait_fd );
242 free( thread->suspend_context );
243 free_msg_queue( thread );
244 cleanup_clipboard_thread(thread);
245 destroy_thread_windows( thread );
246 close_thread_desktop( thread );
247 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
249 if (thread->inflight[i].client != -1)
251 close( thread->inflight[i].server );
252 thread->inflight[i].client = thread->inflight[i].server = -1;
255 thread->req_data = NULL;
256 thread->reply_data = NULL;
257 thread->request_fd = NULL;
258 thread->reply_fd = NULL;
259 thread->wait_fd = NULL;
260 thread->context = NULL;
261 thread->suspend_context = NULL;
265 /* destroy a thread when its refcount is 0 */
266 static void destroy_thread( struct object *obj )
268 struct thread *thread = (struct thread *)obj;
269 assert( obj->ops == &thread_ops );
271 assert( !thread->debug_ctx ); /* cannot still be debugging something */
272 list_remove( &thread->entry );
273 cleanup_thread( thread );
274 release_object( thread->process );
275 if (thread->id) free_ptid( thread->id );
276 if (thread->token) release_object( thread->token );
279 /* dump a thread on stdout for debugging purposes */
280 static void dump_thread( struct object *obj, int verbose )
282 struct thread *thread = (struct thread *)obj;
283 assert( obj->ops == &thread_ops );
285 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
286 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
289 static int thread_signaled( struct object *obj, struct thread *thread )
291 struct thread *mythread = (struct thread *)obj;
292 return (mythread->state == TERMINATED);
295 static unsigned int thread_map_access( struct object *obj, unsigned int access )
297 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
298 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
299 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
300 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
301 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
304 static void dump_thread_apc( struct object *obj, int verbose )
306 struct thread_apc *apc = (struct thread_apc *)obj;
307 assert( obj->ops == &thread_apc_ops );
309 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->type );
312 static int thread_apc_signaled( struct object *obj, struct thread *thread )
314 struct thread_apc *apc = (struct thread_apc *)obj;
315 return apc->executed;
318 /* get a thread pointer from a thread id (and increment the refcount) */
319 struct thread *get_thread_from_id( thread_id_t id )
321 struct object *obj = get_ptid_entry( id );
323 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
324 set_error( STATUS_INVALID_CID );
328 /* get a thread from a handle (and increment the refcount) */
329 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
331 return (struct thread *)get_handle_obj( current->process, handle,
332 access, &thread_ops );
335 /* find a thread from a Unix tid */
336 struct thread *get_thread_from_tid( int tid )
338 struct thread *thread;
340 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
342 if (thread->unix_tid == tid) return thread;
347 /* find a thread from a Unix pid */
348 struct thread *get_thread_from_pid( int pid )
350 struct thread *thread;
352 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
354 if (thread->unix_pid == pid) return thread;
359 /* set all information about a thread */
360 static void set_thread_info( struct thread *thread,
361 const struct set_thread_info_request *req )
363 if (req->mask & SET_THREAD_INFO_PRIORITY)
364 thread->priority = req->priority;
365 if (req->mask & SET_THREAD_INFO_AFFINITY)
367 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
368 else thread->affinity = req->affinity;
370 if (req->mask & SET_THREAD_INFO_TOKEN)
371 security_set_thread_token( thread, req->token );
374 /* stop a thread (at the Unix level) */
375 void stop_thread( struct thread *thread )
377 if (thread->context) return; /* already inside a debug event, no need for a signal */
378 /* can't stop a thread while initialisation is in progress */
379 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
382 /* suspend a thread */
383 static int suspend_thread( struct thread *thread )
385 int old_count = thread->suspend;
386 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
388 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
390 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
394 /* resume a thread */
395 static int resume_thread( struct thread *thread )
397 int old_count = thread->suspend;
398 if (thread->suspend > 0)
400 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
405 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
406 int add_queue( struct object *obj, struct wait_queue_entry *entry )
410 list_add_tail( &obj->wait_queue, &entry->entry );
414 /* remove a thread from an object wait queue */
415 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
417 list_remove( &entry->entry );
418 release_object( obj );
422 static void end_wait( struct thread *thread )
424 struct thread_wait *wait = thread->wait;
425 struct wait_queue_entry *entry;
429 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
430 entry->obj->ops->remove_queue( entry->obj, entry );
431 if (wait->user) remove_timeout_user( wait->user );
432 thread->wait = wait->next;
436 /* build the thread wait structure */
437 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
439 struct thread_wait *wait;
440 struct wait_queue_entry *entry;
443 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
444 wait->next = current->wait;
445 wait->thread = current;
449 current->wait = wait;
450 if (flags & SELECT_TIMEOUT)
452 wait->timeout.tv_sec = timeout->sec;
453 wait->timeout.tv_usec = timeout->usec;
456 for (i = 0, entry = wait->queues; i < count; i++, entry++)
458 struct object *obj = objects[i];
459 entry->thread = current;
460 if (!obj->ops->add_queue( obj, entry ))
470 /* check if the thread waiting condition is satisfied */
471 static int check_wait( struct thread *thread )
474 struct thread_wait *wait = thread->wait;
475 struct wait_queue_entry *entry = wait->queues;
477 /* Suspended threads may not acquire locks */
478 if (thread->process->suspend + thread->suspend > 0) return -1;
481 if (wait->flags & SELECT_ALL)
484 /* Note: we must check them all anyway, as some objects may
485 * want to do something when signaled, even if others are not */
486 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
487 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
488 if (not_ok) goto other_checks;
489 /* Wait satisfied: tell it to all objects */
491 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
492 if (entry->obj->ops->satisfied( entry->obj, thread ))
493 signaled = STATUS_ABANDONED_WAIT_0;
498 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
500 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
501 /* Wait satisfied: tell it to the object */
503 if (entry->obj->ops->satisfied( entry->obj, thread ))
504 signaled = i + STATUS_ABANDONED_WAIT_0;
510 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
511 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
512 if (wait->flags & SELECT_TIMEOUT)
514 if (!time_before( ¤t_time, &wait->timeout )) return STATUS_TIMEOUT;
519 /* send the wakeup signal to a thread */
520 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
522 struct wake_up_reply reply;
525 reply.cookie = cookie;
526 reply.signaled = signaled;
527 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
530 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
531 else if (errno == EPIPE)
532 kill_thread( thread, 0 ); /* normal death */
534 fatal_protocol_perror( thread, "write" );
538 /* attempt to wake up a thread */
539 /* return >0 if OK, 0 if the wait condition is still not satisfied */
540 int wake_thread( struct thread *thread )
545 for (count = 0; thread->wait; count++)
547 if ((signaled = check_wait( thread )) == -1) break;
549 cookie = thread->wait->cookie;
550 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
551 thread->id, signaled, cookie );
553 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
559 /* thread wait timeout */
560 static void thread_timeout( void *ptr )
562 struct thread_wait *wait = ptr;
563 struct thread *thread = wait->thread;
564 void *cookie = wait->cookie;
567 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
568 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
570 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
571 thread->id, (int)STATUS_TIMEOUT, cookie );
573 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
574 /* check if other objects have become signaled in the meantime */
575 wake_thread( thread );
578 /* try signaling an event flag, a semaphore or a mutex */
579 static int signal_object( obj_handle_t handle )
584 obj = get_handle_obj( current->process, handle, 0, NULL );
587 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
588 release_object( obj );
593 /* select on a list of handles */
594 static void select_on( int count, void *cookie, const obj_handle_t *handles,
595 int flags, const abs_time_t *timeout, obj_handle_t signal_obj )
598 struct object *objects[MAXIMUM_WAIT_OBJECTS];
600 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
602 set_error( STATUS_INVALID_PARAMETER );
605 for (i = 0; i < count; i++)
607 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
611 if (i < count) goto done;
612 if (!wait_on( count, objects, flags, timeout )) goto done;
614 /* signal the object */
617 if (!signal_object( signal_obj ))
622 /* check if we woke ourselves up */
623 if (!current->wait) goto done;
626 if ((ret = check_wait( current )) != -1)
628 /* condition is already satisfied */
634 /* now we need to wait */
635 if (flags & SELECT_TIMEOUT)
637 if (!(current->wait->user = add_timeout_user( ¤t->wait->timeout,
638 thread_timeout, current->wait )))
644 current->wait->cookie = cookie;
645 set_error( STATUS_PENDING );
648 while (--i >= 0) release_object( objects[i] );
651 /* attempt to wake threads sleeping on the object wait queue */
652 void wake_up( struct object *obj, int max )
654 struct list *ptr, *next;
656 LIST_FOR_EACH_SAFE( ptr, next, &obj->wait_queue )
658 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
659 if (wake_thread( entry->thread ))
661 if (max && !--max) break;
666 /* queue an async procedure call */
667 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
668 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
670 struct thread_apc *apc;
671 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
673 /* cancel a possible previous APC with the same owner */
674 if (owner) thread_cancel_apc( thread, owner, system );
675 if (thread->state == TERMINATED) return 0;
677 if (!(apc = alloc_object( &thread_apc_ops ))) return 0;
685 list_add_tail( queue, &apc->entry );
686 if (!list_prev( queue, &apc->entry )) /* first one */
687 wake_thread( thread );
692 /* cancel the async procedure call owned by a specific object */
693 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
695 struct thread_apc *apc;
696 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
697 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
699 if (apc->owner != owner) continue;
700 list_remove( &apc->entry );
701 release_object( apc );
706 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
707 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
709 struct thread_apc *apc = NULL;
710 struct list *ptr = list_head( &thread->system_apc );
712 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
715 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
721 /* add an fd to the inflight list */
722 /* return list index, or -1 on error */
723 int thread_add_inflight_fd( struct thread *thread, int client, int server )
727 if (server == -1) return -1;
734 /* first check if we already have an entry for this fd */
735 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
736 if (thread->inflight[i].client == client)
738 close( thread->inflight[i].server );
739 thread->inflight[i].server = server;
743 /* now find a free spot to store it */
744 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
745 if (thread->inflight[i].client == -1)
747 thread->inflight[i].client = client;
748 thread->inflight[i].server = server;
754 /* get an inflight fd and purge it from the list */
755 /* the fd must be closed when no longer used */
756 int thread_get_inflight_fd( struct thread *thread, int client )
760 if (client == -1) return -1;
764 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
766 if (thread->inflight[i].client == client)
768 ret = thread->inflight[i].server;
769 thread->inflight[i].server = thread->inflight[i].client = -1;
773 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
777 /* kill a thread on the spot */
778 void kill_thread( struct thread *thread, int violent_death )
780 if (thread->state == TERMINATED) return; /* already killed */
781 thread->state = TERMINATED;
782 thread->exit_time = current_time;
783 if (current == thread) current = NULL;
785 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
786 thread->id, thread->exit_code );
789 while (thread->wait) end_wait( thread );
790 send_thread_wakeup( thread, NULL, STATUS_PENDING );
791 /* if it is waiting on the socket, we don't need to send a SIGTERM */
794 kill_console_processes( thread, 0 );
795 debug_exit_thread( thread );
796 abandon_mutexes( thread );
797 wake_up( &thread->obj, 0 );
798 if (violent_death) send_thread_signal( thread, SIGTERM );
799 cleanup_thread( thread );
800 remove_process_thread( thread->process, thread );
801 release_object( thread );
804 /* trigger a breakpoint event in a given thread */
805 void break_thread( struct thread *thread )
807 struct debug_event_exception data;
809 assert( thread->context );
811 data.record.ExceptionCode = STATUS_BREAKPOINT;
812 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
813 data.record.ExceptionRecord = NULL;
814 data.record.ExceptionAddress = get_context_ip( thread->context );
815 data.record.NumberParameters = 0;
817 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
818 thread->debug_break = 0;
821 /* take a snapshot of currently running threads */
822 struct thread_snapshot *thread_snap( int *count )
824 struct thread_snapshot *snapshot, *ptr;
825 struct thread *thread;
828 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
829 if (thread->state != TERMINATED) total++;
830 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
832 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
834 if (thread->state == TERMINATED) continue;
835 ptr->thread = thread;
836 ptr->count = thread->obj.refcount;
837 ptr->priority = thread->priority;
838 grab_object( thread );
845 /* gets the current impersonation token */
846 struct token *thread_get_impersonation_token( struct thread *thread )
849 return thread->token;
851 return thread->process->token;
854 /* create a new thread */
855 DECL_HANDLER(new_thread)
857 struct thread *thread;
858 int request_fd = thread_get_inflight_fd( current, req->request_fd );
860 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
862 if (request_fd != -1) close( request_fd );
863 set_error( STATUS_INVALID_HANDLE );
867 if ((thread = create_thread( request_fd, current->process )))
869 if (req->suspend) thread->suspend++;
870 reply->tid = get_thread_id( thread );
871 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
873 /* thread object will be released when the thread gets killed */
876 kill_thread( thread, 1 );
880 /* initialize a new thread */
881 DECL_HANDLER(init_thread)
883 struct process *process = current->process;
884 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
885 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
887 if (current->reply_fd) /* already initialised */
889 set_error( STATUS_INVALID_PARAMETER );
893 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
895 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, ¤t->obj );
897 if (!current->reply_fd) goto error;
901 set_error( STATUS_TOO_MANY_OPENED_FILES ); /* most likely reason */
904 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, ¤t->obj )))
907 if (!is_valid_address(req->teb) || !is_valid_address(req->peb) || !is_valid_address(req->ldt_copy))
909 set_error( STATUS_INVALID_PARAMETER );
913 current->unix_pid = req->unix_pid;
914 current->unix_tid = req->unix_tid;
915 current->teb = req->teb;
917 if (!process->peb) /* first thread, initialize the process too */
919 process->unix_pid = current->unix_pid;
920 process->peb = req->peb;
921 process->ldt_copy = req->ldt_copy;
922 reply->info_size = init_process( current );
926 if (process->unix_pid != current->unix_pid)
927 process->unix_pid = -1; /* can happen with linuxthreads */
928 if (current->suspend + process->suspend > 0) stop_thread( current );
929 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
931 debug_level = max( debug_level, req->debug_level );
933 reply->pid = get_process_id( process );
934 reply->tid = get_thread_id( current );
935 reply->version = SERVER_PROTOCOL_VERSION;
936 reply->server_start.sec = server_start_time.tv_sec;
937 reply->server_start.usec = server_start_time.tv_usec;
941 if (reply_fd != -1) close( reply_fd );
942 if (wait_fd != -1) close( wait_fd );
945 /* terminate a thread */
946 DECL_HANDLER(terminate_thread)
948 struct thread *thread;
952 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
954 thread->exit_code = req->exit_code;
955 if (thread != current) kill_thread( thread, 1 );
959 reply->last = (thread->process->running_threads == 1);
961 release_object( thread );
965 /* open a handle to a thread */
966 DECL_HANDLER(open_thread)
968 struct thread *thread = get_thread_from_id( req->tid );
973 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
974 release_object( thread );
978 /* fetch information about a thread */
979 DECL_HANDLER(get_thread_info)
981 struct thread *thread;
982 obj_handle_t handle = req->handle;
984 if (!handle) thread = get_thread_from_id( req->tid_in );
985 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
989 reply->pid = get_process_id( thread->process );
990 reply->tid = get_thread_id( thread );
991 reply->teb = thread->teb;
992 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
993 reply->priority = thread->priority;
994 reply->affinity = thread->affinity;
995 reply->creation_time.sec = thread->creation_time.tv_sec;
996 reply->creation_time.usec = thread->creation_time.tv_usec;
997 reply->exit_time.sec = thread->exit_time.tv_sec;
998 reply->exit_time.usec = thread->exit_time.tv_usec;
999 reply->last = thread->process->running_threads == 1;
1001 release_object( thread );
1005 /* set information about a thread */
1006 DECL_HANDLER(set_thread_info)
1008 struct thread *thread;
1010 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1012 set_thread_info( thread, req );
1013 release_object( thread );
1017 /* suspend a thread */
1018 DECL_HANDLER(suspend_thread)
1020 struct thread *thread;
1022 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1024 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1025 else reply->count = suspend_thread( thread );
1026 release_object( thread );
1030 /* resume a thread */
1031 DECL_HANDLER(resume_thread)
1033 struct thread *thread;
1035 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1037 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1038 else reply->count = resume_thread( thread );
1039 release_object( thread );
1043 /* select on a handle list */
1044 DECL_HANDLER(select)
1046 int count = get_req_data_size() / sizeof(obj_handle_t);
1047 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout, req->signal );
1050 /* queue an APC for a thread */
1051 DECL_HANDLER(queue_apc)
1053 struct thread *thread;
1054 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
1056 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
1057 req->arg1, req->arg2, req->arg3 );
1058 release_object( thread );
1062 /* get next APC to call */
1063 DECL_HANDLER(get_apc)
1065 struct thread_apc *apc;
1069 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev,
1070 0, &thread_apc_ops ))) return;
1072 wake_up( &apc->obj, 0 );
1073 close_handle( current->process, req->prev );
1074 release_object( apc );
1079 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
1082 set_error( STATUS_PENDING );
1085 /* Optimization: ignore APCs that have a NULL func; they are only used
1086 * to wake up a thread, but since we got here the thread woke up already.
1087 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1089 if (apc->func || apc->type == APC_ASYNC_IO) break;
1090 release_object( apc );
1093 if ((reply->handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1095 reply->func = apc->func;
1096 reply->type = apc->type;
1097 reply->arg1 = apc->arg1;
1098 reply->arg2 = apc->arg2;
1099 reply->arg3 = apc->arg3;
1101 release_object( apc );
1104 /* retrieve the current context of a thread */
1105 DECL_HANDLER(get_thread_context)
1107 struct thread *thread;
1110 if (get_reply_max_size() < sizeof(CONTEXT))
1112 set_error( STATUS_INVALID_PARAMETER );
1115 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1119 if (thread != current || !thread->suspend_context)
1121 /* not suspended, shouldn't happen */
1122 set_error( STATUS_INVALID_PARAMETER );
1126 if (thread->context == thread->suspend_context) thread->context = NULL;
1127 set_reply_data_ptr( thread->suspend_context, sizeof(CONTEXT) );
1128 thread->suspend_context = NULL;
1131 else if (thread != current && !thread->context)
1133 /* thread is not suspended, retry (if it's still running) */
1134 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1135 else set_error( STATUS_PENDING );
1137 else if ((context = set_reply_data_size( sizeof(CONTEXT) )))
1139 unsigned int flags = get_context_system_regs( req->flags );
1141 memset( context, 0, sizeof(CONTEXT) );
1142 context->ContextFlags = get_context_cpu_flag();
1143 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1144 if (flags) get_thread_context( thread, context, flags );
1146 reply->self = (thread == current);
1147 release_object( thread );
1150 /* set the current context of a thread */
1151 DECL_HANDLER(set_thread_context)
1153 struct thread *thread;
1155 if (get_req_data_size() < sizeof(CONTEXT))
1157 set_error( STATUS_INVALID_PARAMETER );
1160 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1164 if (thread != current || thread->context)
1166 /* nested suspend or exception, shouldn't happen */
1167 set_error( STATUS_INVALID_PARAMETER );
1169 else if ((thread->suspend_context = mem_alloc( sizeof(CONTEXT) )))
1171 memcpy( thread->suspend_context, get_req_data(), sizeof(CONTEXT) );
1172 thread->context = thread->suspend_context;
1173 if (thread->debug_break) break_thread( thread );
1176 else if (thread != current && !thread->context)
1178 /* thread is not suspended, retry (if it's still running) */
1179 if (thread->state != RUNNING) set_error( STATUS_ACCESS_DENIED );
1180 else set_error( STATUS_PENDING );
1184 const CONTEXT *context = get_req_data();
1185 unsigned int flags = get_context_system_regs( req->flags );
1187 if (flags) set_thread_context( thread, context, flags );
1188 if (thread->context && !get_error())
1189 copy_context( thread->context, context, req->flags & ~flags );
1191 reply->self = (thread == current);
1192 release_object( thread );
1195 /* fetch a selector entry for a thread */
1196 DECL_HANDLER(get_selector_entry)
1198 struct thread *thread;
1199 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1201 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1202 release_object( thread );