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 NULL
, /* queue_async */
80 destroy_thread
/* destroy */
83 static struct thread
*first_thread
;
84 static struct thread
*booting_thread
;
86 /* initialize the structure for a newly allocated thread */
87 inline static void init_thread_structure( struct thread
*thread
)
91 thread
->unix_pid
= 0; /* not known yet */
92 thread
->context
= NULL
;
95 thread
->debug_ctx
= NULL
;
96 thread
->debug_event
= NULL
;
100 thread
->system_apc
.head
= NULL
;
101 thread
->system_apc
.tail
= NULL
;
102 thread
->user_apc
.head
= NULL
;
103 thread
->user_apc
.tail
= NULL
;
105 thread
->req_data
= NULL
;
106 thread
->req_toread
= 0;
107 thread
->reply_data
= NULL
;
108 thread
->reply_towrite
= 0;
109 thread
->reply_fd
= -1;
110 thread
->wait_fd
= -1;
111 thread
->state
= RUNNING
;
112 thread
->attached
= 0;
113 thread
->exit_code
= 0;
116 thread
->priority
= THREAD_PRIORITY_NORMAL
;
117 thread
->affinity
= 1;
120 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
121 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
124 /* create a new thread */
125 struct thread
*create_thread( int fd
, struct process
*process
)
127 struct thread
*thread
;
129 if (!(thread
= alloc_object( &thread_ops
, fd
))) return NULL
;
131 init_thread_structure( thread
);
133 thread
->process
= (struct process
*)grab_object( process
);
134 thread
->request_fd
= fd
;
135 if (!current
) current
= thread
;
137 if (!booting_thread
) /* first thread ever */
139 booting_thread
= thread
;
140 lock_master_socket(1);
143 if ((thread
->next
= first_thread
) != NULL
) thread
->next
->prev
= thread
;
144 first_thread
= thread
;
146 set_select_events( &thread
->obj
, POLLIN
); /* start listening to events */
147 add_process_thread( thread
->process
, thread
);
151 /* handle a client event */
152 static void thread_poll_event( struct object
*obj
, int event
)
154 struct thread
*thread
= (struct thread
*)obj
;
155 assert( obj
->ops
== &thread_ops
);
157 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
158 else if (event
& POLLIN
) read_request( thread
);
159 else if (event
& POLLOUT
) write_reply( thread
);
162 /* cleanup everything that is no longer needed by a dead thread */
163 /* used by destroy_thread and kill_thread */
164 static void cleanup_thread( struct thread
*thread
)
167 struct thread_apc
*apc
;
169 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
170 if (thread
->req_data
) free( thread
->req_data
);
171 if (thread
->reply_data
) free( thread
->reply_data
);
172 if (thread
->request_fd
!= -1) close( thread
->request_fd
);
173 if (thread
->reply_fd
!= -1) close( thread
->reply_fd
);
174 if (thread
->wait_fd
!= -1) close( thread
->wait_fd
);
177 if (thread
->process
->queue
== thread
->queue
)
179 release_object( thread
->process
->queue
);
180 thread
->process
->queue
= NULL
;
182 release_object( thread
->queue
);
183 thread
->queue
= NULL
;
185 destroy_thread_windows( thread
);
186 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
188 if (thread
->inflight
[i
].client
!= -1)
190 close( thread
->inflight
[i
].server
);
191 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
194 thread
->req_data
= NULL
;
195 thread
->reply_data
= NULL
;
196 thread
->request_fd
= -1;
197 thread
->reply_fd
= -1;
198 thread
->wait_fd
= -1;
201 /* destroy a thread when its refcount is 0 */
202 static void destroy_thread( struct object
*obj
)
204 struct thread_apc
*apc
;
205 struct thread
*thread
= (struct thread
*)obj
;
206 assert( obj
->ops
== &thread_ops
);
208 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
209 if (thread
->next
) thread
->next
->prev
= thread
->prev
;
210 if (thread
->prev
) thread
->prev
->next
= thread
->next
;
211 else first_thread
= thread
->next
;
212 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
213 if (thread
->info
) release_object( thread
->info
);
214 cleanup_thread( thread
);
215 release_object( thread
->process
);
218 /* dump a thread on stdout for debugging purposes */
219 static void dump_thread( struct object
*obj
, int verbose
)
221 struct thread
*thread
= (struct thread
*)obj
;
222 assert( obj
->ops
== &thread_ops
);
224 fprintf( stderr
, "Thread pid=%d teb=%p state=%d\n",
225 thread
->unix_pid
, thread
->teb
, thread
->state
);
228 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
230 struct thread
*mythread
= (struct thread
*)obj
;
231 return (mythread
->state
== TERMINATED
);
234 /* get a thread pointer from a thread id (and increment the refcount) */
235 struct thread
*get_thread_from_id( void *id
)
237 struct thread
*t
= first_thread
;
238 while (t
&& (t
!= id
)) t
= t
->next
;
239 if (t
) grab_object( t
);
240 else set_error( STATUS_INVALID_PARAMETER
);
244 /* get a thread from a handle (and increment the refcount) */
245 struct thread
*get_thread_from_handle( handle_t handle
, unsigned int access
)
247 return (struct thread
*)get_handle_obj( current
->process
, handle
,
248 access
, &thread_ops
);
251 /* find a thread from a Unix pid */
252 struct thread
*get_thread_from_pid( int pid
)
254 struct thread
*t
= first_thread
;
255 while (t
&& (t
->unix_pid
!= pid
)) t
= t
->next
;
259 /* set all information about a thread */
260 static void set_thread_info( struct thread
*thread
,
261 const struct set_thread_info_request
*req
)
263 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
264 thread
->priority
= req
->priority
;
265 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
267 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
268 else thread
->affinity
= req
->affinity
;
272 /* suspend a thread */
273 int suspend_thread( struct thread
*thread
, int check_limit
)
275 int old_count
= thread
->suspend
;
276 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
|| !check_limit
)
278 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
280 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
284 /* resume a thread */
285 int resume_thread( struct thread
*thread
)
287 int old_count
= thread
->suspend
;
288 if (thread
->suspend
> 0)
290 if (!(--thread
->suspend
+ thread
->process
->suspend
)) continue_thread( thread
);
295 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
296 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
300 entry
->prev
= obj
->tail
;
302 if (obj
->tail
) obj
->tail
->next
= entry
;
303 else obj
->head
= entry
;
308 /* remove a thread from an object wait queue */
309 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
311 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
312 else obj
->tail
= entry
->prev
;
313 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
314 else obj
->head
= entry
->next
;
315 release_object( obj
);
319 static void end_wait( struct thread
*thread
)
321 struct thread_wait
*wait
= thread
->wait
;
322 struct wait_queue_entry
*entry
;
326 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
327 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
328 if (wait
->user
) remove_timeout_user( wait
->user
);
329 thread
->wait
= wait
->next
;
333 /* build the thread wait structure */
334 static int wait_on( int count
, struct object
*objects
[], int flags
, int sec
, int usec
)
336 struct thread_wait
*wait
;
337 struct wait_queue_entry
*entry
;
340 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
341 wait
->next
= current
->wait
;
342 wait
->thread
= current
;
346 current
->wait
= wait
;
347 if (flags
& SELECT_TIMEOUT
)
349 wait
->timeout
.tv_sec
= sec
;
350 wait
->timeout
.tv_usec
= usec
;
353 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
355 struct object
*obj
= objects
[i
];
356 entry
->thread
= current
;
357 if (!obj
->ops
->add_queue( obj
, entry
))
367 /* check if the thread waiting condition is satisfied */
368 static int check_wait( struct thread
*thread
)
371 struct thread_wait
*wait
= thread
->wait
;
372 struct wait_queue_entry
*entry
= wait
->queues
;
375 if (wait
->flags
& SELECT_ALL
)
378 /* Note: we must check them all anyway, as some objects may
379 * want to do something when signaled, even if others are not */
380 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
381 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
382 if (not_ok
) goto other_checks
;
383 /* Wait satisfied: tell it to all objects */
385 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
386 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
387 signaled
= STATUS_ABANDONED_WAIT_0
;
392 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
394 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
395 /* Wait satisfied: tell it to the object */
397 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
398 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
404 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && thread
->system_apc
.head
) return STATUS_USER_APC
;
405 if ((wait
->flags
& SELECT_ALERTABLE
) && thread
->user_apc
.head
) return STATUS_USER_APC
;
406 if (wait
->flags
& SELECT_TIMEOUT
)
409 gettimeofday( &now
, NULL
);
410 if (!time_before( &now
, &wait
->timeout
)) return STATUS_TIMEOUT
;
415 /* send the wakeup signal to a thread */
416 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
418 struct wake_up_reply reply
;
421 reply
.cookie
= cookie
;
422 reply
.signaled
= signaled
;
423 if ((ret
= write( thread
->wait_fd
, &reply
, sizeof(reply
) )) == sizeof(reply
)) return 0;
425 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
426 else if (errno
== EPIPE
)
427 kill_thread( thread
, 0 ); /* normal death */
429 fatal_protocol_perror( thread
, "write" );
433 /* attempt to wake up a thread */
434 /* return >0 if OK, 0 if the wait condition is still not satisfied */
435 static int wake_thread( struct thread
*thread
)
440 for (count
= 0; thread
->wait
; count
++)
442 if ((signaled
= check_wait( thread
)) == -1) break;
444 cookie
= thread
->wait
->cookie
;
445 if (debug_level
) fprintf( stderr
, "%08x: *wakeup* signaled=%d cookie=%p\n",
446 (unsigned int)thread
, signaled
, cookie
);
448 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
454 /* thread wait timeout */
455 static void thread_timeout( void *ptr
)
457 struct thread_wait
*wait
= ptr
;
458 struct thread
*thread
= wait
->thread
;
459 void *cookie
= wait
->cookie
;
462 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
464 if (debug_level
) fprintf( stderr
, "%08x: *wakeup* signaled=%d cookie=%p\n",
465 (unsigned int)thread
, STATUS_TIMEOUT
, cookie
);
467 send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
);
468 /* check if other objects have become signaled in the meantime */
469 wake_thread( thread
);
472 /* select on a list of handles */
473 static void select_on( int count
, void *cookie
, const handle_t
*handles
,
474 int flags
, int sec
, int usec
)
477 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
479 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
481 set_error( STATUS_INVALID_PARAMETER
);
484 for (i
= 0; i
< count
; i
++)
486 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
490 if (i
< count
) goto done
;
491 if (!wait_on( count
, objects
, flags
, sec
, usec
)) goto done
;
493 if ((ret
= check_wait( current
)) != -1)
495 /* condition is already satisfied */
501 /* now we need to wait */
502 if (flags
& SELECT_TIMEOUT
)
504 if (!(current
->wait
->user
= add_timeout_user( ¤t
->wait
->timeout
,
505 thread_timeout
, current
->wait
)))
511 current
->wait
->cookie
= cookie
;
512 set_error( STATUS_PENDING
);
515 while (--i
>= 0) release_object( objects
[i
] );
518 /* attempt to wake threads sleeping on the object wait queue */
519 void wake_up( struct object
*obj
, int max
)
521 struct wait_queue_entry
*entry
= obj
->head
;
525 struct thread
*thread
= entry
->thread
;
527 if (wake_thread( thread
))
529 if (max
&& !--max
) break;
534 /* queue an async procedure call */
535 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, void *func
,
536 enum apc_type type
, int system
, int nb_args
, ... )
538 struct thread_apc
*apc
;
539 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
541 /* cancel a possible previous APC with the same owner */
542 if (owner
) thread_cancel_apc( thread
, owner
, system
);
544 if (!(apc
= mem_alloc( sizeof(*apc
) + (nb_args
-1)*sizeof(apc
->args
[0]) ))) return 0;
545 apc
->prev
= queue
->tail
;
550 apc
->nb_args
= nb_args
;
555 va_start( args
, nb_args
);
556 for (i
= 0; i
< nb_args
; i
++) apc
->args
[i
] = va_arg( args
, void * );
560 if (!apc
->prev
) /* first one */
563 wake_thread( thread
);
565 else apc
->prev
->next
= apc
;
570 /* cancel the async procedure call owned by a specific object */
571 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, int system
)
573 struct thread_apc
*apc
;
574 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
575 for (apc
= queue
->head
; apc
; apc
= apc
->next
)
577 if (apc
->owner
!= owner
) continue;
578 if (apc
->next
) apc
->next
->prev
= apc
->prev
;
579 else queue
->tail
= apc
->prev
;
580 if (apc
->prev
) apc
->prev
->next
= apc
->next
;
581 else queue
->head
= apc
->next
;
587 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
588 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
590 struct thread_apc
*apc
;
591 struct apc_queue
*queue
= &thread
->system_apc
;
593 if (!queue
->head
&& !system_only
) queue
= &thread
->user_apc
;
594 if ((apc
= queue
->head
))
596 if (apc
->next
) apc
->next
->prev
= NULL
;
597 else queue
->tail
= NULL
;
598 queue
->head
= apc
->next
;
603 /* add an fd to the inflight list */
604 /* return list index, or -1 on error */
605 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
609 if (server
== -1) return -1;
616 /* first check if we already have an entry for this fd */
617 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
618 if (thread
->inflight
[i
].client
== client
)
620 close( thread
->inflight
[i
].server
);
621 thread
->inflight
[i
].server
= server
;
625 /* now find a free spot to store it */
626 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
627 if (thread
->inflight
[i
].client
== -1)
629 thread
->inflight
[i
].client
= client
;
630 thread
->inflight
[i
].server
= server
;
636 /* get an inflight fd and purge it from the list */
637 /* the fd must be closed when no longer used */
638 int thread_get_inflight_fd( struct thread
*thread
, int client
)
642 if (client
== -1) return -1;
646 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
648 if (thread
->inflight
[i
].client
== client
)
650 ret
= thread
->inflight
[i
].server
;
651 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
655 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
659 /* retrieve an LDT selector entry */
660 static void get_selector_entry( struct thread
*thread
, int entry
,
661 unsigned int *base
, unsigned int *limit
,
662 unsigned char *flags
)
664 if (!thread
->process
->ldt_copy
)
666 set_error( STATUS_ACCESS_DENIED
);
671 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
674 if (suspend_for_ptrace( thread
))
676 unsigned char flags_buf
[4];
677 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
678 if (read_thread_int( thread
, addr
, base
) == -1) goto done
;
679 if (read_thread_int( thread
, addr
+ 8192, limit
) == -1) goto done
;
680 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
681 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
682 *flags
= flags_buf
[entry
& 3];
684 resume_thread( thread
);
688 /* kill a thread on the spot */
689 void kill_thread( struct thread
*thread
, int violent_death
)
691 if (thread
->state
== TERMINATED
) return; /* already killed */
692 thread
->state
= TERMINATED
;
693 if (current
== thread
) current
= NULL
;
695 fprintf( stderr
,"%08x: *killed* exit_code=%d\n",
696 (unsigned int)thread
, thread
->exit_code
);
699 while (thread
->wait
) end_wait( thread
);
700 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
701 /* if it is waiting on the socket, we don't need to send a SIGTERM */
704 kill_console_processes( thread
, 0 );
705 debug_exit_thread( thread
);
706 abandon_mutexes( thread
);
707 remove_process_thread( thread
->process
, thread
);
708 wake_up( &thread
->obj
, 0 );
709 detach_thread( thread
, violent_death
? SIGTERM
: 0 );
710 if (thread
->request_fd
== thread
->obj
.fd
) thread
->request_fd
= -1;
711 if (thread
->reply_fd
== thread
->obj
.fd
) thread
->reply_fd
= -1;
712 remove_select_user( &thread
->obj
);
713 cleanup_thread( thread
);
714 release_object( thread
);
717 /* take a snapshot of currently running threads */
718 struct thread_snapshot
*thread_snap( int *count
)
720 struct thread_snapshot
*snapshot
, *ptr
;
721 struct thread
*thread
;
724 for (thread
= first_thread
; thread
; thread
= thread
->next
)
725 if (thread
->state
!= TERMINATED
) total
++;
726 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
728 for (thread
= first_thread
; thread
; thread
= thread
->next
)
730 if (thread
->state
== TERMINATED
) continue;
731 ptr
->thread
= thread
;
732 ptr
->count
= thread
->obj
.refcount
;
733 ptr
->priority
= thread
->priority
;
734 grab_object( thread
);
741 /* signal that we are finished booting on the client side */
742 DECL_HANDLER(boot_done
)
744 debug_level
= max( debug_level
, req
->debug_level
);
745 if (current
== booting_thread
)
747 booting_thread
= (struct thread
*)~0UL; /* make sure it doesn't match other threads */
748 lock_master_socket(0); /* allow other clients now */
752 /* create a new thread */
753 DECL_HANDLER(new_thread
)
755 struct thread
*thread
;
756 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
758 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
760 if (request_fd
!= -1) close( request_fd
);
761 set_error( STATUS_INVALID_HANDLE
);
765 if ((thread
= create_thread( request_fd
, current
->process
)))
767 if (req
->suspend
) thread
->suspend
++;
769 if ((reply
->handle
= alloc_handle( current
->process
, thread
,
770 THREAD_ALL_ACCESS
, req
->inherit
)))
772 /* thread object will be released when the thread gets killed */
775 kill_thread( thread
, 1 );
780 /* initialize a new thread */
781 DECL_HANDLER(init_thread
)
783 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
784 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
786 if (current
->unix_pid
)
788 fatal_protocol_error( current
, "init_thread: already running\n" );
791 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1)
793 fatal_protocol_error( current
, "bad reply fd\n" );
798 fatal_protocol_error( current
, "bad wait fd\n" );
802 current
->unix_pid
= req
->unix_pid
;
803 current
->teb
= req
->teb
;
804 current
->reply_fd
= reply_fd
;
805 current
->wait_fd
= wait_fd
;
807 if (current
->suspend
+ current
->process
->suspend
> 0) stop_thread( current
);
808 if (current
->process
->running_threads
> 1)
809 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
811 reply
->pid
= get_process_id( current
->process
);
812 reply
->tid
= get_thread_id( current
);
813 reply
->boot
= (current
== booting_thread
);
814 reply
->version
= SERVER_PROTOCOL_VERSION
;
818 if (reply_fd
!= -1) close( reply_fd
);
819 if (wait_fd
!= -1) close( wait_fd
);
822 /* terminate a thread */
823 DECL_HANDLER(terminate_thread
)
825 struct thread
*thread
;
829 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
831 thread
->exit_code
= req
->exit_code
;
832 if (thread
!= current
) kill_thread( thread
, 1 );
836 reply
->last
= (thread
->process
->running_threads
== 1);
838 release_object( thread
);
842 /* fetch information about a thread */
843 DECL_HANDLER(get_thread_info
)
845 struct thread
*thread
;
846 handle_t handle
= req
->handle
;
848 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
849 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
853 reply
->tid
= get_thread_id( thread
);
854 reply
->teb
= thread
->teb
;
855 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STILL_ACTIVE
;
856 reply
->priority
= thread
->priority
;
857 release_object( thread
);
861 /* set information about a thread */
862 DECL_HANDLER(set_thread_info
)
864 struct thread
*thread
;
866 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
868 set_thread_info( thread
, req
);
869 release_object( thread
);
873 /* suspend a thread */
874 DECL_HANDLER(suspend_thread
)
876 struct thread
*thread
;
878 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
880 reply
->count
= suspend_thread( thread
, 1 );
881 release_object( thread
);
885 /* resume a thread */
886 DECL_HANDLER(resume_thread
)
888 struct thread
*thread
;
890 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
892 reply
->count
= resume_thread( thread
);
893 release_object( thread
);
897 /* select on a handle list */
900 int count
= get_req_data_size() / sizeof(int);
901 select_on( count
, req
->cookie
, get_req_data(), req
->flags
, req
->sec
, req
->usec
);
904 /* queue an APC for a thread */
905 DECL_HANDLER(queue_apc
)
907 struct thread
*thread
;
908 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
910 thread_queue_apc( thread
, NULL
, req
->func
, APC_USER
, !req
->user
, 1, req
->param
);
911 release_object( thread
);
915 /* get next APC to call */
916 DECL_HANDLER(get_apc
)
918 struct thread_apc
*apc
;
923 if (!(apc
= thread_dequeue_apc( current
, !req
->alertable
)))
927 reply
->type
= APC_NONE
;
930 /* Optimization: ignore APCs that have a NULL func; they are only used
931 * to wake up a thread, but since we got here the thread woke up already.
933 if (apc
->func
) break;
936 size
= apc
->nb_args
* sizeof(apc
->args
[0]);
937 if (size
> get_reply_max_size()) size
= get_reply_max_size();
938 reply
->func
= apc
->func
;
939 reply
->type
= apc
->type
;
940 set_reply_data( apc
->args
, size
);
944 /* fetch a selector entry for a thread */
945 DECL_HANDLER(get_selector_entry
)
947 struct thread
*thread
;
948 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
950 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
951 release_object( thread
);