2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
17 #include <sys/types.h>
34 struct thread_wait
*next
; /* next wait structure for this thread */
35 struct thread
*thread
; /* owner thread */
36 int count
; /* count of objects */
38 void *cookie
; /* magic cookie to return to client */
39 struct timeval timeout
;
40 struct timeout_user
*user
;
41 struct wait_queue_entry queues
[1];
44 /* asynchronous procedure calls */
48 struct thread_apc
*next
; /* queue linked list */
49 struct thread_apc
*prev
;
50 struct object
*owner
; /* object that queued this apc */
51 void *func
; /* function to call in client */
52 enum apc_type type
; /* type of apc function */
53 int nb_args
; /* number of arguments */
54 void *args
[1]; /* function arguments */
58 /* thread operations */
60 static void dump_thread( struct object
*obj
, int verbose
);
61 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
62 static void thread_poll_event( struct object
*obj
, int event
);
63 static void destroy_thread( struct object
*obj
);
64 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
);
66 static const struct object_ops thread_ops
=
68 sizeof(struct thread
), /* size */
69 dump_thread
, /* dump */
70 add_queue
, /* add_queue */
71 remove_queue
, /* remove_queue */
72 thread_signaled
, /* signaled */
73 no_satisfied
, /* satisfied */
74 NULL
, /* get_poll_events */
75 thread_poll_event
, /* poll_event */
76 no_get_fd
, /* get_fd */
78 no_get_file_info
, /* get_file_info */
79 destroy_thread
/* destroy */
82 static struct thread
*first_thread
;
83 static struct thread
*booting_thread
;
85 /* initialize the structure for a newly allocated thread */
86 inline static void init_thread_structure( struct thread
*thread
)
90 thread
->unix_pid
= 0; /* not known yet */
91 thread
->context
= NULL
;
94 thread
->debug_ctx
= NULL
;
95 thread
->debug_event
= NULL
;
99 thread
->system_apc
.head
= NULL
;
100 thread
->system_apc
.tail
= NULL
;
101 thread
->user_apc
.head
= NULL
;
102 thread
->user_apc
.tail
= NULL
;
104 thread
->req_data
= NULL
;
105 thread
->req_toread
= 0;
106 thread
->reply_data
= NULL
;
107 thread
->reply_towrite
= 0;
108 thread
->reply_fd
= -1;
109 thread
->wait_fd
= -1;
110 thread
->state
= RUNNING
;
111 thread
->attached
= 0;
112 thread
->exit_code
= 0;
115 thread
->priority
= THREAD_PRIORITY_NORMAL
;
116 thread
->affinity
= 1;
119 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
120 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
123 /* create a new thread */
124 struct thread
*create_thread( int fd
, struct process
*process
)
126 struct thread
*thread
;
128 if (!(thread
= alloc_object( &thread_ops
, fd
))) return NULL
;
130 init_thread_structure( thread
);
132 thread
->process
= (struct process
*)grab_object( process
);
133 thread
->request_fd
= fd
;
134 if (!current
) current
= thread
;
136 if (!booting_thread
) /* first thread ever */
138 booting_thread
= thread
;
139 lock_master_socket(1);
142 if ((thread
->next
= first_thread
) != NULL
) thread
->next
->prev
= thread
;
143 first_thread
= thread
;
145 set_select_events( &thread
->obj
, POLLIN
); /* start listening to events */
146 add_process_thread( thread
->process
, thread
);
150 /* handle a client event */
151 static void thread_poll_event( struct object
*obj
, int event
)
153 struct thread
*thread
= (struct thread
*)obj
;
154 assert( obj
->ops
== &thread_ops
);
156 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
157 else if (event
& POLLIN
) read_request( thread
);
158 else if (event
& POLLOUT
) write_reply( thread
);
161 /* cleanup everything that is no longer needed by a dead thread */
162 /* used by destroy_thread and kill_thread */
163 static void cleanup_thread( struct thread
*thread
)
166 struct thread_apc
*apc
;
168 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
169 if (thread
->req_data
) free( thread
->req_data
);
170 if (thread
->reply_data
) free( thread
->reply_data
);
171 if (thread
->request_fd
!= -1) close( thread
->request_fd
);
172 if (thread
->reply_fd
!= -1) close( thread
->reply_fd
);
173 if (thread
->wait_fd
!= -1) close( thread
->wait_fd
);
176 if (thread
->process
->queue
== thread
->queue
)
178 release_object( thread
->process
->queue
);
179 thread
->process
->queue
= NULL
;
181 release_object( thread
->queue
);
182 thread
->queue
= NULL
;
184 destroy_thread_windows( thread
);
185 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
187 if (thread
->inflight
[i
].client
!= -1)
189 close( thread
->inflight
[i
].server
);
190 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
193 thread
->req_data
= NULL
;
194 thread
->reply_data
= NULL
;
195 thread
->request_fd
= -1;
196 thread
->reply_fd
= -1;
197 thread
->wait_fd
= -1;
200 /* destroy a thread when its refcount is 0 */
201 static void destroy_thread( struct object
*obj
)
203 struct thread_apc
*apc
;
204 struct thread
*thread
= (struct thread
*)obj
;
205 assert( obj
->ops
== &thread_ops
);
207 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
208 if (thread
->next
) thread
->next
->prev
= thread
->prev
;
209 if (thread
->prev
) thread
->prev
->next
= thread
->next
;
210 else first_thread
= thread
->next
;
211 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
212 if (thread
->info
) release_object( thread
->info
);
213 cleanup_thread( thread
);
214 release_object( thread
->process
);
217 /* dump a thread on stdout for debugging purposes */
218 static void dump_thread( struct object
*obj
, int verbose
)
220 struct thread
*thread
= (struct thread
*)obj
;
221 assert( obj
->ops
== &thread_ops
);
223 fprintf( stderr
, "Thread pid=%d teb=%p state=%d\n",
224 thread
->unix_pid
, thread
->teb
, thread
->state
);
227 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
229 struct thread
*mythread
= (struct thread
*)obj
;
230 return (mythread
->state
== TERMINATED
);
233 /* get a thread pointer from a thread id (and increment the refcount) */
234 struct thread
*get_thread_from_id( void *id
)
236 struct thread
*t
= first_thread
;
237 while (t
&& (t
!= id
)) t
= t
->next
;
238 if (t
) grab_object( t
);
239 else set_error( STATUS_INVALID_PARAMETER
);
243 /* get a thread from a handle (and increment the refcount) */
244 struct thread
*get_thread_from_handle( handle_t handle
, unsigned int access
)
246 return (struct thread
*)get_handle_obj( current
->process
, handle
,
247 access
, &thread_ops
);
250 /* find a thread from a Unix pid */
251 struct thread
*get_thread_from_pid( int pid
)
253 struct thread
*t
= first_thread
;
254 while (t
&& (t
->unix_pid
!= pid
)) t
= t
->next
;
258 /* set all information about a thread */
259 static void set_thread_info( struct thread
*thread
,
260 const struct set_thread_info_request
*req
)
262 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
263 thread
->priority
= req
->priority
;
264 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
266 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
267 else thread
->affinity
= req
->affinity
;
271 /* suspend a thread */
272 int suspend_thread( struct thread
*thread
, int check_limit
)
274 int old_count
= thread
->suspend
;
275 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
|| !check_limit
)
277 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
279 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
283 /* resume a thread */
284 int resume_thread( struct thread
*thread
)
286 int old_count
= thread
->suspend
;
287 if (thread
->suspend
> 0)
289 if (!(--thread
->suspend
+ thread
->process
->suspend
)) continue_thread( thread
);
294 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
295 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
299 entry
->prev
= obj
->tail
;
301 if (obj
->tail
) obj
->tail
->next
= entry
;
302 else obj
->head
= entry
;
307 /* remove a thread from an object wait queue */
308 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
310 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
311 else obj
->tail
= entry
->prev
;
312 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
313 else obj
->head
= entry
->next
;
314 release_object( obj
);
318 static void end_wait( struct thread
*thread
)
320 struct thread_wait
*wait
= thread
->wait
;
321 struct wait_queue_entry
*entry
;
325 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
326 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
327 if (wait
->user
) remove_timeout_user( wait
->user
);
328 thread
->wait
= wait
->next
;
332 /* build the thread wait structure */
333 static int wait_on( int count
, struct object
*objects
[], int flags
, int sec
, int usec
)
335 struct thread_wait
*wait
;
336 struct wait_queue_entry
*entry
;
339 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
340 wait
->next
= current
->wait
;
341 wait
->thread
= current
;
345 current
->wait
= wait
;
346 if (flags
& SELECT_TIMEOUT
)
348 wait
->timeout
.tv_sec
= sec
;
349 wait
->timeout
.tv_usec
= usec
;
352 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
354 struct object
*obj
= objects
[i
];
355 entry
->thread
= current
;
356 if (!obj
->ops
->add_queue( obj
, entry
))
366 /* check if the thread waiting condition is satisfied */
367 static int check_wait( struct thread
*thread
)
370 struct thread_wait
*wait
= thread
->wait
;
371 struct wait_queue_entry
*entry
= wait
->queues
;
374 if (wait
->flags
& SELECT_ALL
)
377 /* Note: we must check them all anyway, as some objects may
378 * want to do something when signaled, even if others are not */
379 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
380 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
381 if (not_ok
) goto other_checks
;
382 /* Wait satisfied: tell it to all objects */
384 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
385 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
386 signaled
= STATUS_ABANDONED_WAIT_0
;
391 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
393 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
394 /* Wait satisfied: tell it to the object */
396 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
397 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
403 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && thread
->system_apc
.head
) return STATUS_USER_APC
;
404 if ((wait
->flags
& SELECT_ALERTABLE
) && thread
->user_apc
.head
) return STATUS_USER_APC
;
405 if (wait
->flags
& SELECT_TIMEOUT
)
408 gettimeofday( &now
, NULL
);
409 if (!time_before( &now
, &wait
->timeout
)) return STATUS_TIMEOUT
;
414 /* send the wakeup signal to a thread */
415 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
417 struct wake_up_reply reply
;
420 reply
.cookie
= cookie
;
421 reply
.signaled
= signaled
;
422 if ((ret
= write( thread
->wait_fd
, &reply
, sizeof(reply
) )) == sizeof(reply
)) return 0;
424 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
425 else if (errno
== EPIPE
)
426 kill_thread( thread
, 0 ); /* normal death */
428 fatal_protocol_perror( thread
, "write" );
432 /* attempt to wake up a thread */
433 /* return >0 if OK, 0 if the wait condition is still not satisfied */
434 static int wake_thread( struct thread
*thread
)
439 for (count
= 0; thread
->wait
; count
++)
441 if ((signaled
= check_wait( thread
)) == -1) break;
443 cookie
= thread
->wait
->cookie
;
444 if (debug_level
) fprintf( stderr
, "%08x: *wakeup* signaled=%d cookie=%p\n",
445 (unsigned int)thread
, signaled
, cookie
);
447 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
453 /* thread wait timeout */
454 static void thread_timeout( void *ptr
)
456 struct thread_wait
*wait
= ptr
;
457 struct thread
*thread
= wait
->thread
;
458 void *cookie
= wait
->cookie
;
461 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
463 if (debug_level
) fprintf( stderr
, "%08x: *wakeup* signaled=%d cookie=%p\n",
464 (unsigned int)thread
, STATUS_TIMEOUT
, cookie
);
466 send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
);
467 /* check if other objects have become signaled in the meantime */
468 wake_thread( thread
);
471 /* select on a list of handles */
472 static void select_on( int count
, void *cookie
, const handle_t
*handles
,
473 int flags
, int sec
, int usec
)
476 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
478 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
480 set_error( STATUS_INVALID_PARAMETER
);
483 for (i
= 0; i
< count
; i
++)
485 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
489 if (i
< count
) goto done
;
490 if (!wait_on( count
, objects
, flags
, sec
, usec
)) goto done
;
492 if ((ret
= check_wait( current
)) != -1)
494 /* condition is already satisfied */
500 /* now we need to wait */
501 if (flags
& SELECT_TIMEOUT
)
503 if (!(current
->wait
->user
= add_timeout_user( ¤t
->wait
->timeout
,
504 thread_timeout
, current
->wait
)))
510 current
->wait
->cookie
= cookie
;
511 set_error( STATUS_PENDING
);
514 while (--i
>= 0) release_object( objects
[i
] );
517 /* attempt to wake threads sleeping on the object wait queue */
518 void wake_up( struct object
*obj
, int max
)
520 struct wait_queue_entry
*entry
= obj
->head
;
524 struct thread
*thread
= entry
->thread
;
526 if (wake_thread( thread
))
528 if (max
&& !--max
) break;
533 /* queue an async procedure call */
534 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, void *func
,
535 enum apc_type type
, int system
, int nb_args
, ... )
537 struct thread_apc
*apc
;
538 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
540 /* cancel a possible previous APC with the same owner */
541 if (owner
) thread_cancel_apc( thread
, owner
, system
);
543 if (!(apc
= mem_alloc( sizeof(*apc
) + (nb_args
-1)*sizeof(apc
->args
[0]) ))) return 0;
544 apc
->prev
= queue
->tail
;
549 apc
->nb_args
= nb_args
;
554 va_start( args
, nb_args
);
555 for (i
= 0; i
< nb_args
; i
++) apc
->args
[i
] = va_arg( args
, void * );
559 if (!apc
->prev
) /* first one */
562 wake_thread( thread
);
567 /* cancel the async procedure call owned by a specific object */
568 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, int system
)
570 struct thread_apc
*apc
;
571 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
572 for (apc
= queue
->head
; apc
; apc
= apc
->next
)
574 if (apc
->owner
!= owner
) continue;
575 if (apc
->next
) apc
->next
->prev
= apc
->prev
;
576 else queue
->tail
= apc
->prev
;
577 if (apc
->prev
) apc
->prev
->next
= apc
->next
;
578 else queue
->head
= apc
->next
;
584 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
585 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
587 struct thread_apc
*apc
;
588 struct apc_queue
*queue
= &thread
->system_apc
;
590 if (!queue
->head
&& !system_only
) queue
= &thread
->user_apc
;
591 if ((apc
= queue
->head
))
593 if (apc
->next
) apc
->next
->prev
= NULL
;
594 else queue
->tail
= NULL
;
595 queue
->head
= apc
->next
;
600 /* add an fd to the inflight list */
601 /* return list index, or -1 on error */
602 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
606 if (server
== -1) return -1;
613 /* first check if we already have an entry for this fd */
614 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
615 if (thread
->inflight
[i
].client
== client
)
617 close( thread
->inflight
[i
].server
);
618 thread
->inflight
[i
].server
= server
;
622 /* now find a free spot to store it */
623 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
624 if (thread
->inflight
[i
].client
== -1)
626 thread
->inflight
[i
].client
= client
;
627 thread
->inflight
[i
].server
= server
;
633 /* get an inflight fd and purge it from the list */
634 /* the fd must be closed when no longer used */
635 int thread_get_inflight_fd( struct thread
*thread
, int client
)
639 if (client
== -1) return -1;
643 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
645 if (thread
->inflight
[i
].client
== client
)
647 ret
= thread
->inflight
[i
].server
;
648 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
652 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
656 /* retrieve an LDT selector entry */
657 static void get_selector_entry( struct thread
*thread
, int entry
,
658 unsigned int *base
, unsigned int *limit
,
659 unsigned char *flags
)
661 if (!thread
->process
->ldt_copy
)
663 set_error( STATUS_ACCESS_DENIED
);
668 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
671 if (suspend_for_ptrace( thread
))
673 unsigned char flags_buf
[4];
674 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
675 if (read_thread_int( thread
, addr
, base
) == -1) goto done
;
676 if (read_thread_int( thread
, addr
+ 8192, limit
) == -1) goto done
;
677 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
678 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
679 *flags
= flags_buf
[entry
& 3];
681 resume_thread( thread
);
685 /* kill a thread on the spot */
686 void kill_thread( struct thread
*thread
, int violent_death
)
688 if (thread
->state
== TERMINATED
) return; /* already killed */
689 thread
->state
= TERMINATED
;
690 if (current
== thread
) current
= NULL
;
692 fprintf( stderr
,"%08x: *killed* exit_code=%d\n",
693 (unsigned int)thread
, thread
->exit_code
);
696 while (thread
->wait
) end_wait( thread
);
697 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
698 /* if it is waiting on the socket, we don't need to send a SIGTERM */
701 kill_console_processes( thread
, 0 );
702 debug_exit_thread( thread
);
703 abandon_mutexes( thread
);
704 remove_process_thread( thread
->process
, thread
);
705 wake_up( &thread
->obj
, 0 );
706 detach_thread( thread
, violent_death
? SIGTERM
: 0 );
707 if (thread
->request_fd
== thread
->obj
.fd
) thread
->request_fd
= -1;
708 if (thread
->reply_fd
== thread
->obj
.fd
) thread
->reply_fd
= -1;
709 remove_select_user( &thread
->obj
);
710 cleanup_thread( thread
);
711 release_object( thread
);
714 /* take a snapshot of currently running threads */
715 struct thread_snapshot
*thread_snap( int *count
)
717 struct thread_snapshot
*snapshot
, *ptr
;
718 struct thread
*thread
;
721 for (thread
= first_thread
; thread
; thread
= thread
->next
)
722 if (thread
->state
!= TERMINATED
) total
++;
723 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
725 for (thread
= first_thread
; thread
; thread
= thread
->next
)
727 if (thread
->state
== TERMINATED
) continue;
728 ptr
->thread
= thread
;
729 ptr
->count
= thread
->obj
.refcount
;
730 ptr
->priority
= thread
->priority
;
731 grab_object( thread
);
738 /* signal that we are finished booting on the client side */
739 DECL_HANDLER(boot_done
)
741 debug_level
= max( debug_level
, req
->debug_level
);
742 if (current
== booting_thread
)
744 booting_thread
= (struct thread
*)~0UL; /* make sure it doesn't match other threads */
745 lock_master_socket(0); /* allow other clients now */
749 /* create a new thread */
750 DECL_HANDLER(new_thread
)
752 struct thread
*thread
;
753 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
755 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
757 if (request_fd
!= -1) close( request_fd
);
758 set_error( STATUS_INVALID_HANDLE
);
762 if ((thread
= create_thread( request_fd
, current
->process
)))
764 if (req
->suspend
) thread
->suspend
++;
766 if ((reply
->handle
= alloc_handle( current
->process
, thread
,
767 THREAD_ALL_ACCESS
, req
->inherit
)))
769 /* thread object will be released when the thread gets killed */
772 kill_thread( thread
, 1 );
777 /* initialize a new thread */
778 DECL_HANDLER(init_thread
)
780 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
781 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
783 if (current
->unix_pid
)
785 fatal_protocol_error( current
, "init_thread: already running\n" );
788 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1)
790 fatal_protocol_error( current
, "bad reply fd\n" );
795 fatal_protocol_error( current
, "bad wait fd\n" );
799 current
->unix_pid
= req
->unix_pid
;
800 current
->teb
= req
->teb
;
801 current
->reply_fd
= reply_fd
;
802 current
->wait_fd
= wait_fd
;
804 if (current
->suspend
+ current
->process
->suspend
> 0) stop_thread( current
);
805 if (current
->process
->running_threads
> 1)
806 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
808 reply
->pid
= get_process_id( current
->process
);
809 reply
->tid
= get_thread_id( current
);
810 reply
->boot
= (current
== booting_thread
);
811 reply
->version
= SERVER_PROTOCOL_VERSION
;
815 if (reply_fd
!= -1) close( reply_fd
);
816 if (wait_fd
!= -1) close( wait_fd
);
819 /* terminate a thread */
820 DECL_HANDLER(terminate_thread
)
822 struct thread
*thread
;
826 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
828 thread
->exit_code
= req
->exit_code
;
829 if (thread
!= current
) kill_thread( thread
, 1 );
833 reply
->last
= (thread
->process
->running_threads
== 1);
835 release_object( thread
);
839 /* fetch information about a thread */
840 DECL_HANDLER(get_thread_info
)
842 struct thread
*thread
;
843 handle_t handle
= req
->handle
;
845 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
846 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
850 reply
->tid
= get_thread_id( thread
);
851 reply
->teb
= thread
->teb
;
852 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STILL_ACTIVE
;
853 reply
->priority
= thread
->priority
;
854 release_object( thread
);
858 /* set information about a thread */
859 DECL_HANDLER(set_thread_info
)
861 struct thread
*thread
;
863 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
865 set_thread_info( thread
, req
);
866 release_object( thread
);
870 /* suspend a thread */
871 DECL_HANDLER(suspend_thread
)
873 struct thread
*thread
;
875 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
877 reply
->count
= suspend_thread( thread
, 1 );
878 release_object( thread
);
882 /* resume a thread */
883 DECL_HANDLER(resume_thread
)
885 struct thread
*thread
;
887 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
889 reply
->count
= resume_thread( thread
);
890 release_object( thread
);
894 /* select on a handle list */
897 int count
= get_req_data_size() / sizeof(int);
898 select_on( count
, req
->cookie
, get_req_data(), req
->flags
, req
->sec
, req
->usec
);
901 /* queue an APC for a thread */
902 DECL_HANDLER(queue_apc
)
904 struct thread
*thread
;
905 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
907 thread_queue_apc( thread
, NULL
, req
->func
, APC_USER
, !req
->user
, 1, req
->param
);
908 release_object( thread
);
912 /* get next APC to call */
913 DECL_HANDLER(get_apc
)
915 struct thread_apc
*apc
;
920 if (!(apc
= thread_dequeue_apc( current
, !req
->alertable
)))
924 reply
->type
= APC_NONE
;
927 /* Optimization: ignore APCs that have a NULL func; they are only used
928 * to wake up a thread, but since we got here the thread woke up already.
930 if (apc
->func
) break;
933 size
= apc
->nb_args
* sizeof(apc
->args
[0]);
934 if (size
> get_reply_max_size()) size
= get_reply_max_size();
935 reply
->func
= apc
->func
;
936 reply
->type
= apc
->type
;
937 set_reply_data( apc
->args
, size
);
941 /* fetch a selector entry for a thread */
942 DECL_HANDLER(get_selector_entry
)
944 struct thread
*thread
;
945 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
947 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
948 release_object( thread
);