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
22 #include "wine/port.h"
32 #include <sys/types.h>
54 struct thread_wait
*next
; /* next wait structure for this thread */
55 struct thread
*thread
; /* owner thread */
56 int count
; /* count of objects */
58 void *cookie
; /* magic cookie to return to client */
59 struct timeval timeout
;
60 struct timeout_user
*user
;
61 struct wait_queue_entry queues
[1];
64 /* asynchronous procedure calls */
68 struct list entry
; /* queue linked list */
69 struct object
*owner
; /* object that queued this apc */
70 void *func
; /* function to call in client */
71 enum apc_type type
; /* type of apc function */
72 int nb_args
; /* number of arguments */
73 void *arg1
; /* function arguments */
79 /* thread operations */
81 static void dump_thread( struct object
*obj
, int verbose
);
82 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
83 static void thread_poll_event( struct fd
*fd
, int event
);
84 static void destroy_thread( struct object
*obj
);
85 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
);
87 static const struct object_ops thread_ops
=
89 sizeof(struct thread
), /* size */
90 dump_thread
, /* dump */
91 add_queue
, /* add_queue */
92 remove_queue
, /* remove_queue */
93 thread_signaled
, /* signaled */
94 no_satisfied
, /* satisfied */
95 no_signal
, /* signal */
96 no_get_fd
, /* get_fd */
97 no_close_handle
, /* close_handle */
98 destroy_thread
/* destroy */
101 static const struct fd_ops thread_fd_ops
=
103 NULL
, /* get_poll_events */
104 thread_poll_event
, /* poll_event */
105 no_flush
, /* flush */
106 no_get_file_info
, /* get_file_info */
107 no_queue_async
, /* queue_async */
108 no_cancel_async
/* cancel_async */
111 static struct list thread_list
= LIST_INIT(thread_list
);
112 static struct thread
*booting_thread
;
114 /* initialize the structure for a newly allocated thread */
115 inline static void init_thread_structure( struct thread
*thread
)
119 thread
->unix_pid
= -1; /* not known yet */
120 thread
->unix_tid
= -1; /* not known yet */
121 thread
->context
= NULL
;
123 thread
->debug_ctx
= NULL
;
124 thread
->debug_event
= NULL
;
125 thread
->queue
= NULL
;
128 thread
->req_data
= NULL
;
129 thread
->req_toread
= 0;
130 thread
->reply_data
= NULL
;
131 thread
->reply_towrite
= 0;
132 thread
->request_fd
= NULL
;
133 thread
->reply_fd
= NULL
;
134 thread
->wait_fd
= NULL
;
135 thread
->state
= RUNNING
;
136 thread
->attached
= 0;
137 thread
->exit_code
= 0;
138 thread
->priority
= THREAD_PRIORITY_NORMAL
;
139 thread
->affinity
= 1;
141 thread
->creation_time
= time(NULL
);
142 thread
->exit_time
= 0;
143 thread
->desktop_users
= 0;
145 list_init( &thread
->mutex_list
);
146 list_init( &thread
->system_apc
);
147 list_init( &thread
->user_apc
);
149 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
150 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
153 /* check if address looks valid for a client-side data structure (TEB etc.) */
154 static inline int is_valid_address( void *addr
)
156 return addr
&& !((unsigned int)addr
% sizeof(int));
159 /* create a new thread */
160 struct thread
*create_thread( int fd
, struct process
*process
)
162 struct thread
*thread
;
164 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
166 init_thread_structure( thread
);
168 thread
->process
= (struct process
*)grab_object( process
);
169 thread
->desktop
= process
->desktop
;
170 if (!current
) current
= thread
;
172 if (!booting_thread
) /* first thread ever */
174 booting_thread
= thread
;
175 lock_master_socket(1);
178 list_add_head( &thread_list
, &thread
->entry
);
180 if (!(thread
->id
= alloc_ptid( thread
)))
182 release_object( thread
);
185 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
)))
187 release_object( thread
);
191 thread
->token
= (struct token
*) grab_object( process
->token
);
193 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
194 add_process_thread( thread
->process
, thread
);
198 /* handle a client event */
199 static void thread_poll_event( struct fd
*fd
, int event
)
201 struct thread
*thread
= get_fd_user( fd
);
202 assert( thread
->obj
.ops
== &thread_ops
);
204 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
205 else if (event
& POLLIN
) read_request( thread
);
206 else if (event
& POLLOUT
) write_reply( thread
);
209 /* cleanup everything that is no longer needed by a dead thread */
210 /* used by destroy_thread and kill_thread */
211 static void cleanup_thread( struct thread
*thread
)
214 struct thread_apc
*apc
;
216 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
217 if (thread
->req_data
) free( thread
->req_data
);
218 if (thread
->reply_data
) free( thread
->reply_data
);
219 if (thread
->request_fd
) release_object( thread
->request_fd
);
220 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
221 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
222 free_msg_queue( thread
);
223 cleanup_clipboard_thread(thread
);
224 destroy_thread_windows( thread
);
225 close_thread_desktop( thread
);
226 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
228 if (thread
->inflight
[i
].client
!= -1)
230 close( thread
->inflight
[i
].server
);
231 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
234 thread
->req_data
= NULL
;
235 thread
->reply_data
= NULL
;
236 thread
->request_fd
= NULL
;
237 thread
->reply_fd
= NULL
;
238 thread
->wait_fd
= NULL
;
241 if (thread
== booting_thread
) /* killing booting thread */
243 booting_thread
= NULL
;
244 lock_master_socket(0);
248 /* destroy a thread when its refcount is 0 */
249 static void destroy_thread( struct object
*obj
)
251 struct thread_apc
*apc
;
252 struct thread
*thread
= (struct thread
*)obj
;
253 assert( obj
->ops
== &thread_ops
);
255 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
256 list_remove( &thread
->entry
);
257 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
258 cleanup_thread( thread
);
259 release_object( thread
->process
);
260 if (thread
->id
) free_ptid( thread
->id
);
261 if (thread
->token
) release_object( thread
->token
);
264 /* dump a thread on stdout for debugging purposes */
265 static void dump_thread( struct object
*obj
, int verbose
)
267 struct thread
*thread
= (struct thread
*)obj
;
268 assert( obj
->ops
== &thread_ops
);
270 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
271 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->teb
, thread
->state
);
274 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
276 struct thread
*mythread
= (struct thread
*)obj
;
277 return (mythread
->state
== TERMINATED
);
280 /* get a thread pointer from a thread id (and increment the refcount) */
281 struct thread
*get_thread_from_id( thread_id_t id
)
283 struct object
*obj
= get_ptid_entry( id
);
285 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
286 set_win32_error( ERROR_INVALID_THREAD_ID
);
290 /* get a thread from a handle (and increment the refcount) */
291 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
293 return (struct thread
*)get_handle_obj( current
->process
, handle
,
294 access
, &thread_ops
);
297 /* find a thread from a Unix pid */
298 struct thread
*get_thread_from_pid( int pid
)
300 struct thread
*thread
;
302 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
304 if (thread
->unix_tid
== pid
) return thread
;
306 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
308 if (thread
->unix_pid
== pid
) return thread
;
313 /* set all information about a thread */
314 static void set_thread_info( struct thread
*thread
,
315 const struct set_thread_info_request
*req
)
317 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
318 thread
->priority
= req
->priority
;
319 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
321 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
322 else thread
->affinity
= req
->affinity
;
324 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
325 security_set_thread_token( thread
, req
->token
);
328 /* stop a thread (at the Unix level) */
329 void stop_thread( struct thread
*thread
)
331 /* can't stop a thread while initialisation is in progress */
332 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
335 /* suspend a thread */
336 static int suspend_thread( struct thread
*thread
)
338 int old_count
= thread
->suspend
;
339 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
341 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
343 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
347 /* resume a thread */
348 static int resume_thread( struct thread
*thread
)
350 int old_count
= thread
->suspend
;
351 if (thread
->suspend
> 0)
353 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
358 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
359 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
363 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
367 /* remove a thread from an object wait queue */
368 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
370 list_remove( &entry
->entry
);
371 release_object( obj
);
375 static void end_wait( struct thread
*thread
)
377 struct thread_wait
*wait
= thread
->wait
;
378 struct wait_queue_entry
*entry
;
382 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
383 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
384 if (wait
->user
) remove_timeout_user( wait
->user
);
385 thread
->wait
= wait
->next
;
389 /* build the thread wait structure */
390 static int wait_on( int count
, struct object
*objects
[], int flags
, const abs_time_t
*timeout
)
392 struct thread_wait
*wait
;
393 struct wait_queue_entry
*entry
;
396 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
397 wait
->next
= current
->wait
;
398 wait
->thread
= current
;
402 current
->wait
= wait
;
403 if (flags
& SELECT_TIMEOUT
)
405 wait
->timeout
.tv_sec
= timeout
->sec
;
406 wait
->timeout
.tv_usec
= timeout
->usec
;
409 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
411 struct object
*obj
= objects
[i
];
412 entry
->thread
= current
;
413 if (!obj
->ops
->add_queue( obj
, entry
))
423 /* check if the thread waiting condition is satisfied */
424 static int check_wait( struct thread
*thread
)
427 struct thread_wait
*wait
= thread
->wait
;
428 struct wait_queue_entry
*entry
= wait
->queues
;
430 /* Suspended threads may not acquire locks */
431 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
434 if (wait
->flags
& SELECT_ALL
)
437 /* Note: we must check them all anyway, as some objects may
438 * want to do something when signaled, even if others are not */
439 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
440 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
441 if (not_ok
) goto other_checks
;
442 /* Wait satisfied: tell it to all objects */
444 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
445 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
446 signaled
= STATUS_ABANDONED_WAIT_0
;
451 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
453 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
454 /* Wait satisfied: tell it to the object */
456 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
457 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
463 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty(&thread
->system_apc
)) return STATUS_USER_APC
;
464 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
465 if (wait
->flags
& SELECT_TIMEOUT
)
468 gettimeofday( &now
, NULL
);
469 if (!time_before( &now
, &wait
->timeout
)) return STATUS_TIMEOUT
;
474 /* send the wakeup signal to a thread */
475 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
477 struct wake_up_reply reply
;
480 reply
.cookie
= cookie
;
481 reply
.signaled
= signaled
;
482 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
485 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
486 else if (errno
== EPIPE
)
487 kill_thread( thread
, 0 ); /* normal death */
489 fatal_protocol_perror( thread
, "write" );
493 /* attempt to wake up a thread */
494 /* return >0 if OK, 0 if the wait condition is still not satisfied */
495 int wake_thread( struct thread
*thread
)
500 for (count
= 0; thread
->wait
; count
++)
502 if ((signaled
= check_wait( thread
)) == -1) break;
504 cookie
= thread
->wait
->cookie
;
505 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
506 thread
->id
, signaled
, cookie
);
508 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
514 /* thread wait timeout */
515 static void thread_timeout( void *ptr
)
517 struct thread_wait
*wait
= ptr
;
518 struct thread
*thread
= wait
->thread
;
519 void *cookie
= wait
->cookie
;
522 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
523 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
525 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
526 thread
->id
, STATUS_TIMEOUT
, cookie
);
528 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
529 /* check if other objects have become signaled in the meantime */
530 wake_thread( thread
);
533 /* try signaling an event flag, a semaphore or a mutex */
534 static int signal_object( obj_handle_t handle
)
539 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
542 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
543 release_object( obj
);
548 /* select on a list of handles */
549 static void select_on( int count
, void *cookie
, const obj_handle_t
*handles
,
550 int flags
, const abs_time_t
*timeout
, obj_handle_t signal_obj
)
553 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
555 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
557 set_error( STATUS_INVALID_PARAMETER
);
560 for (i
= 0; i
< count
; i
++)
562 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
566 if (i
< count
) goto done
;
567 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
569 /* signal the object */
572 if (!signal_object( signal_obj
))
577 /* check if we woke ourselves up */
578 if (!current
->wait
) goto done
;
581 if ((ret
= check_wait( current
)) != -1)
583 /* condition is already satisfied */
589 /* now we need to wait */
590 if (flags
& SELECT_TIMEOUT
)
592 if (!(current
->wait
->user
= add_timeout_user( ¤t
->wait
->timeout
,
593 thread_timeout
, current
->wait
)))
599 current
->wait
->cookie
= cookie
;
600 set_error( STATUS_PENDING
);
603 while (--i
>= 0) release_object( objects
[i
] );
606 /* attempt to wake threads sleeping on the object wait queue */
607 void wake_up( struct object
*obj
, int max
)
609 struct list
*ptr
, *next
;
611 LIST_FOR_EACH_SAFE( ptr
, next
, &obj
->wait_queue
)
613 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
614 if (wake_thread( entry
->thread
))
616 if (max
&& !--max
) break;
621 /* queue an async procedure call */
622 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, void *func
,
623 enum apc_type type
, int system
, void *arg1
, void *arg2
, void *arg3
)
625 struct thread_apc
*apc
;
626 struct list
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
628 /* cancel a possible previous APC with the same owner */
629 if (owner
) thread_cancel_apc( thread
, owner
, system
);
630 if (thread
->state
== TERMINATED
) return 0;
632 if (!(apc
= mem_alloc( sizeof(*apc
) ))) return 0;
639 list_add_tail( queue
, &apc
->entry
);
640 if (!list_prev( queue
, &apc
->entry
)) /* first one */
641 wake_thread( thread
);
646 /* cancel the async procedure call owned by a specific object */
647 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, int system
)
649 struct thread_apc
*apc
;
650 struct list
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
651 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
653 if (apc
->owner
!= owner
) continue;
654 list_remove( &apc
->entry
);
660 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
661 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
663 struct thread_apc
*apc
= NULL
;
664 struct list
*ptr
= list_head( &thread
->system_apc
);
666 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
669 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
675 /* add an fd to the inflight list */
676 /* return list index, or -1 on error */
677 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
681 if (server
== -1) return -1;
688 /* first check if we already have an entry for this fd */
689 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
690 if (thread
->inflight
[i
].client
== client
)
692 close( thread
->inflight
[i
].server
);
693 thread
->inflight
[i
].server
= server
;
697 /* now find a free spot to store it */
698 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
699 if (thread
->inflight
[i
].client
== -1)
701 thread
->inflight
[i
].client
= client
;
702 thread
->inflight
[i
].server
= server
;
708 /* get an inflight fd and purge it from the list */
709 /* the fd must be closed when no longer used */
710 int thread_get_inflight_fd( struct thread
*thread
, int client
)
714 if (client
== -1) return -1;
718 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
720 if (thread
->inflight
[i
].client
== client
)
722 ret
= thread
->inflight
[i
].server
;
723 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
727 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
731 /* retrieve an LDT selector entry */
732 static void get_selector_entry( struct thread
*thread
, int entry
,
733 unsigned int *base
, unsigned int *limit
,
734 unsigned char *flags
)
736 if (!thread
->process
->ldt_copy
)
738 set_error( STATUS_ACCESS_DENIED
);
743 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
746 if (suspend_for_ptrace( thread
))
748 unsigned char flags_buf
[4];
749 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
750 if (read_thread_int( thread
, addr
, base
) == -1) goto done
;
751 if (read_thread_int( thread
, addr
+ 8192, limit
) == -1) goto done
;
752 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
753 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
754 *flags
= flags_buf
[entry
& 3];
756 resume_after_ptrace( thread
);
760 /* kill a thread on the spot */
761 void kill_thread( struct thread
*thread
, int violent_death
)
763 if (thread
->state
== TERMINATED
) return; /* already killed */
764 thread
->state
= TERMINATED
;
765 thread
->exit_time
= time(NULL
);
766 if (current
== thread
) current
= NULL
;
768 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
769 thread
->id
, thread
->exit_code
);
772 while (thread
->wait
) end_wait( thread
);
773 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
774 /* if it is waiting on the socket, we don't need to send a SIGTERM */
777 kill_console_processes( thread
, 0 );
778 debug_exit_thread( thread
);
779 abandon_mutexes( thread
);
780 remove_process_thread( thread
->process
, thread
);
781 wake_up( &thread
->obj
, 0 );
782 detach_thread( thread
, violent_death
? SIGTERM
: 0 );
783 cleanup_thread( thread
);
784 release_object( thread
);
787 /* take a snapshot of currently running threads */
788 struct thread_snapshot
*thread_snap( int *count
)
790 struct thread_snapshot
*snapshot
, *ptr
;
791 struct thread
*thread
;
794 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
795 if (thread
->state
!= TERMINATED
) total
++;
796 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
798 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
800 if (thread
->state
== TERMINATED
) continue;
801 ptr
->thread
= thread
;
802 ptr
->count
= thread
->obj
.refcount
;
803 ptr
->priority
= thread
->priority
;
804 grab_object( thread
);
811 /* gets the current impersonation token */
812 struct token
*thread_get_impersonation_token( struct thread
*thread
)
815 return thread
->token
;
817 return thread
->process
->token
;
820 /* signal that we are finished booting on the client side */
821 DECL_HANDLER(boot_done
)
823 if (current
== booting_thread
)
825 booting_thread
= (struct thread
*)~0UL; /* make sure it doesn't match other threads */
826 lock_master_socket(0); /* allow other clients now */
830 /* create a new thread */
831 DECL_HANDLER(new_thread
)
833 struct thread
*thread
;
834 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
836 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
838 if (request_fd
!= -1) close( request_fd
);
839 set_error( STATUS_INVALID_HANDLE
);
843 if ((thread
= create_thread( request_fd
, current
->process
)))
845 if (req
->suspend
) thread
->suspend
++;
846 reply
->tid
= get_thread_id( thread
);
847 if ((reply
->handle
= alloc_handle( current
->process
, thread
,
848 THREAD_ALL_ACCESS
, req
->inherit
)))
850 /* thread object will be released when the thread gets killed */
853 kill_thread( thread
, 1 );
857 /* initialize a new thread */
858 DECL_HANDLER(init_thread
)
860 struct process
*process
= current
->process
;
861 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
862 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
864 if (current
->unix_pid
!= -1)
866 fatal_protocol_error( current
, "init_thread: already running\n" );
869 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1)
871 fatal_protocol_error( current
, "bad reply fd\n" );
876 fatal_protocol_error( current
, "bad wait fd\n" );
879 if (!(current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
)))
882 fatal_protocol_error( current
, "could not allocate reply fd\n" );
885 if (!(current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
)))
888 if (!is_valid_address(req
->teb
) || !is_valid_address(req
->peb
) || !is_valid_address(req
->ldt_copy
))
890 set_error( STATUS_INVALID_PARAMETER
);
894 current
->unix_pid
= req
->unix_pid
;
895 current
->unix_tid
= req
->unix_tid
;
896 current
->teb
= req
->teb
;
898 if (!process
->peb
) /* first thread, initialize the process too */
900 process
->peb
= req
->peb
;
901 process
->ldt_copy
= req
->ldt_copy
;
902 init_process( current
);
906 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
907 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
909 debug_level
= max( debug_level
, req
->debug_level
);
911 reply
->pid
= get_process_id( process
);
912 reply
->tid
= get_thread_id( current
);
913 reply
->version
= SERVER_PROTOCOL_VERSION
;
917 if (reply_fd
!= -1) close( reply_fd
);
918 if (wait_fd
!= -1) close( wait_fd
);
921 /* terminate a thread */
922 DECL_HANDLER(terminate_thread
)
924 struct thread
*thread
;
928 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
930 thread
->exit_code
= req
->exit_code
;
931 if (thread
!= current
) kill_thread( thread
, 1 );
935 reply
->last
= (thread
->process
->running_threads
== 1);
937 release_object( thread
);
941 /* open a handle to a thread */
942 DECL_HANDLER(open_thread
)
944 struct thread
*thread
= get_thread_from_id( req
->tid
);
949 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->inherit
);
950 release_object( thread
);
954 /* fetch information about a thread */
955 DECL_HANDLER(get_thread_info
)
957 struct thread
*thread
;
958 obj_handle_t handle
= req
->handle
;
960 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
961 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
965 reply
->pid
= get_process_id( thread
->process
);
966 reply
->tid
= get_thread_id( thread
);
967 reply
->teb
= thread
->teb
;
968 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STILL_ACTIVE
;
969 reply
->priority
= thread
->priority
;
970 reply
->affinity
= thread
->affinity
;
971 reply
->creation_time
= thread
->creation_time
;
972 reply
->exit_time
= thread
->exit_time
;
974 release_object( thread
);
978 /* set information about a thread */
979 DECL_HANDLER(set_thread_info
)
981 struct thread
*thread
;
983 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
985 set_thread_info( thread
, req
);
986 release_object( thread
);
990 /* suspend a thread */
991 DECL_HANDLER(suspend_thread
)
993 struct thread
*thread
;
995 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
997 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
998 else reply
->count
= suspend_thread( thread
);
999 release_object( thread
);
1003 /* resume a thread */
1004 DECL_HANDLER(resume_thread
)
1006 struct thread
*thread
;
1008 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1010 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1011 else reply
->count
= resume_thread( thread
);
1012 release_object( thread
);
1016 /* select on a handle list */
1017 DECL_HANDLER(select
)
1019 int count
= get_req_data_size() / sizeof(int);
1020 select_on( count
, req
->cookie
, get_req_data(), req
->flags
, &req
->timeout
, req
->signal
);
1023 /* queue an APC for a thread */
1024 DECL_HANDLER(queue_apc
)
1026 struct thread
*thread
;
1027 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
1029 thread_queue_apc( thread
, NULL
, req
->func
, APC_USER
, !req
->user
,
1030 req
->arg1
, req
->arg2
, req
->arg3
);
1031 release_object( thread
);
1035 /* get next APC to call */
1036 DECL_HANDLER(get_apc
)
1038 struct thread_apc
*apc
;
1042 if (!(apc
= thread_dequeue_apc( current
, !req
->alertable
)))
1046 reply
->type
= APC_NONE
;
1049 /* Optimization: ignore APCs that have a NULL func; they are only used
1050 * to wake up a thread, but since we got here the thread woke up already.
1051 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1053 if (apc
->func
|| apc
->type
== APC_ASYNC_IO
) break;
1056 reply
->func
= apc
->func
;
1057 reply
->type
= apc
->type
;
1058 reply
->arg1
= apc
->arg1
;
1059 reply
->arg2
= apc
->arg2
;
1060 reply
->arg3
= apc
->arg3
;
1064 /* fetch a selector entry for a thread */
1065 DECL_HANDLER(get_selector_entry
)
1067 struct thread
*thread
;
1068 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1070 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1071 release_object( thread
);