2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
17 #ifdef HAVE_SYS_MMAN_H
20 #include <sys/types.h>
36 struct thread_wait
*next
; /* next wait structure for this thread */
37 struct thread
*thread
; /* owner thread */
38 int count
; /* count of objects */
40 void *cookie
; /* magic cookie to return to client */
41 struct timeval timeout
;
42 struct timeout_user
*user
;
43 struct wait_queue_entry queues
[1];
46 /* asynchronous procedure calls */
50 struct thread_apc
*next
; /* queue linked list */
51 struct thread_apc
*prev
;
52 struct object
*owner
; /* object that queued this apc */
53 void *func
; /* function to call in client */
54 enum apc_type type
; /* type of apc function */
55 int nb_args
; /* number of arguments */
56 void *args
[1]; /* function arguments */
60 /* thread operations */
62 static void dump_thread( struct object
*obj
, int verbose
);
63 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
64 static void thread_poll_event( struct object
*obj
, int event
);
65 static void destroy_thread( struct object
*obj
);
66 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
);
68 static const struct object_ops thread_ops
=
70 sizeof(struct thread
), /* size */
71 dump_thread
, /* dump */
72 add_queue
, /* add_queue */
73 remove_queue
, /* remove_queue */
74 thread_signaled
, /* signaled */
75 no_satisfied
, /* satisfied */
76 NULL
, /* get_poll_events */
77 thread_poll_event
, /* poll_event */
78 no_get_fd
, /* get_fd */
80 no_get_file_info
, /* get_file_info */
81 destroy_thread
/* destroy */
84 static struct thread
*first_thread
;
85 static struct thread
*booting_thread
;
87 /* initialize the structure for a newly allocated thread */
88 inline static void init_thread_structure( struct thread
*thread
)
92 thread
->unix_pid
= 0; /* not known yet */
93 thread
->context
= NULL
;
96 thread
->debug_ctx
= NULL
;
97 thread
->debug_event
= NULL
;
101 thread
->system_apc
.head
= NULL
;
102 thread
->system_apc
.tail
= NULL
;
103 thread
->user_apc
.head
= NULL
;
104 thread
->user_apc
.tail
= NULL
;
106 thread
->request_fd
= NULL
;
107 thread
->reply_fd
= -1;
108 thread
->wait_fd
= -1;
109 thread
->state
= RUNNING
;
110 thread
->attached
= 0;
111 thread
->exit_code
= 0;
114 thread
->priority
= THREAD_PRIORITY_NORMAL
;
115 thread
->affinity
= 1;
117 thread
->buffer
= (void *)-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 if (!current
) current
= thread
;
135 if (!booting_thread
) /* first thread ever */
137 booting_thread
= thread
;
138 lock_master_socket(1);
141 if ((thread
->next
= first_thread
) != NULL
) thread
->next
->prev
= thread
;
142 first_thread
= thread
;
144 fcntl( fd
, F_SETFL
, O_NONBLOCK
);
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
);
160 /* cleanup everything that is no longer needed by a dead thread */
161 /* used by destroy_thread and kill_thread */
162 static void cleanup_thread( struct thread
*thread
)
165 struct thread_apc
*apc
;
167 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
168 if (thread
->buffer
!= (void *)-1) munmap( thread
->buffer
, MAX_REQUEST_LENGTH
);
169 if (thread
->reply_fd
!= -1) close( thread
->reply_fd
);
170 if (thread
->wait_fd
!= -1) close( thread
->wait_fd
);
171 if (thread
->request_fd
) release_object( thread
->request_fd
);
174 if (thread
->process
->queue
== thread
->queue
)
176 release_object( thread
->process
->queue
);
177 thread
->process
->queue
= NULL
;
179 release_object( thread
->queue
);
180 thread
->queue
= NULL
;
182 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
184 if (thread
->inflight
[i
].client
!= -1)
186 close( thread
->inflight
[i
].server
);
187 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
190 thread
->buffer
= (void *)-1;
191 thread
->reply_fd
= -1;
192 thread
->wait_fd
= -1;
193 thread
->request_fd
= NULL
;
196 /* destroy a thread when its refcount is 0 */
197 static void destroy_thread( struct object
*obj
)
199 struct thread_apc
*apc
;
200 struct thread
*thread
= (struct thread
*)obj
;
201 assert( obj
->ops
== &thread_ops
);
203 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
204 if (thread
->next
) thread
->next
->prev
= thread
->prev
;
205 if (thread
->prev
) thread
->prev
->next
= thread
->next
;
206 else first_thread
= thread
->next
;
207 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
208 if (thread
->info
) release_object( thread
->info
);
209 cleanup_thread( thread
);
210 release_object( thread
->process
);
213 /* dump a thread on stdout for debugging purposes */
214 static void dump_thread( struct object
*obj
, int verbose
)
216 struct thread
*thread
= (struct thread
*)obj
;
217 assert( obj
->ops
== &thread_ops
);
219 fprintf( stderr
, "Thread pid=%d teb=%p state=%d\n",
220 thread
->unix_pid
, thread
->teb
, thread
->state
);
223 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
225 struct thread
*mythread
= (struct thread
*)obj
;
226 return (mythread
->state
== TERMINATED
);
229 /* get a thread pointer from a thread id (and increment the refcount) */
230 struct thread
*get_thread_from_id( void *id
)
232 struct thread
*t
= first_thread
;
233 while (t
&& (t
!= id
)) t
= t
->next
;
234 if (t
) grab_object( t
);
235 else set_error( STATUS_INVALID_PARAMETER
);
239 /* get a thread from a handle (and increment the refcount) */
240 struct thread
*get_thread_from_handle( handle_t handle
, unsigned int access
)
242 return (struct thread
*)get_handle_obj( current
->process
, handle
,
243 access
, &thread_ops
);
246 /* find a thread from a Unix pid */
247 struct thread
*get_thread_from_pid( int pid
)
249 struct thread
*t
= first_thread
;
250 while (t
&& (t
->unix_pid
!= pid
)) t
= t
->next
;
254 /* set all information about a thread */
255 static void set_thread_info( struct thread
*thread
,
256 struct set_thread_info_request
*req
)
258 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
259 thread
->priority
= req
->priority
;
260 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
262 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
263 else thread
->affinity
= req
->affinity
;
267 /* suspend a thread */
268 int suspend_thread( struct thread
*thread
, int check_limit
)
270 int old_count
= thread
->suspend
;
271 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
|| !check_limit
)
273 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
275 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
279 /* resume a thread */
280 int resume_thread( struct thread
*thread
)
282 int old_count
= thread
->suspend
;
283 if (thread
->suspend
> 0)
285 if (!(--thread
->suspend
+ thread
->process
->suspend
)) continue_thread( thread
);
290 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
291 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
295 entry
->prev
= obj
->tail
;
297 if (obj
->tail
) obj
->tail
->next
= entry
;
298 else obj
->head
= entry
;
303 /* remove a thread from an object wait queue */
304 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
306 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
307 else obj
->tail
= entry
->prev
;
308 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
309 else obj
->head
= entry
->next
;
310 release_object( obj
);
314 static void end_wait( struct thread
*thread
)
316 struct thread_wait
*wait
= thread
->wait
;
317 struct wait_queue_entry
*entry
;
321 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
322 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
323 if (wait
->user
) remove_timeout_user( wait
->user
);
324 thread
->wait
= wait
->next
;
328 /* build the thread wait structure */
329 static int wait_on( int count
, struct object
*objects
[], int flags
, int sec
, int usec
)
331 struct thread_wait
*wait
;
332 struct wait_queue_entry
*entry
;
335 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
336 wait
->next
= current
->wait
;
337 wait
->thread
= current
;
341 current
->wait
= wait
;
342 if (flags
& SELECT_TIMEOUT
)
344 wait
->timeout
.tv_sec
= sec
;
345 wait
->timeout
.tv_usec
= usec
;
348 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
350 struct object
*obj
= objects
[i
];
351 entry
->thread
= current
;
352 if (!obj
->ops
->add_queue( obj
, entry
))
362 /* check if the thread waiting condition is satisfied */
363 static int check_wait( struct thread
*thread
)
366 struct thread_wait
*wait
= thread
->wait
;
367 struct wait_queue_entry
*entry
= wait
->queues
;
370 if (wait
->flags
& SELECT_ALL
)
373 /* Note: we must check them all anyway, as some objects may
374 * want to do something when signaled, even if others are not */
375 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
376 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
377 if (not_ok
) goto other_checks
;
378 /* Wait satisfied: tell it to all objects */
380 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
381 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
382 signaled
= STATUS_ABANDONED_WAIT_0
;
387 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
389 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
390 /* Wait satisfied: tell it to the object */
392 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
393 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
399 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && thread
->system_apc
.head
) return STATUS_USER_APC
;
400 if ((wait
->flags
& SELECT_ALERTABLE
) && thread
->user_apc
.head
) return STATUS_USER_APC
;
401 if (wait
->flags
& SELECT_TIMEOUT
)
404 gettimeofday( &now
, NULL
);
405 if (!time_before( &now
, &wait
->timeout
)) return STATUS_TIMEOUT
;
410 /* send the wakeup signal to a thread */
411 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
413 struct wake_up_reply reply
;
416 reply
.cookie
= cookie
;
417 reply
.signaled
= signaled
;
418 if ((ret
= write( thread
->wait_fd
, &reply
, sizeof(reply
) )) == sizeof(reply
)) return 0;
420 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
421 else if (errno
== EPIPE
)
422 kill_thread( thread
, 0 ); /* normal death */
424 fatal_protocol_perror( thread
, "write" );
428 /* attempt to wake up a thread */
429 /* return >0 if OK, 0 if the wait condition is still not satisfied */
430 static int wake_thread( struct thread
*thread
)
435 for (count
= 0; thread
->wait
; count
++)
437 if ((signaled
= check_wait( thread
)) == -1) break;
439 cookie
= thread
->wait
->cookie
;
440 if (debug_level
) fprintf( stderr
, "%08x: *wakeup* signaled=%d cookie=%p\n",
441 (unsigned int)thread
, signaled
, cookie
);
443 send_thread_wakeup( thread
, cookie
, signaled
);
448 /* thread wait timeout */
449 static void thread_timeout( void *ptr
)
451 struct thread_wait
*wait
= ptr
;
452 struct thread
*thread
= wait
->thread
;
453 void *cookie
= wait
->cookie
;
456 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
458 if (debug_level
) fprintf( stderr
, "%08x: *wakeup* signaled=%d cookie=%p\n",
459 (unsigned int)thread
, STATUS_TIMEOUT
, cookie
);
461 send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
);
462 /* check if other objects have become signaled in the meantime */
463 wake_thread( thread
);
466 /* select on a list of handles */
467 static void select_on( int count
, void *cookie
, handle_t
*handles
, int flags
, int sec
, int usec
)
470 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
472 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
474 set_error( STATUS_INVALID_PARAMETER
);
477 for (i
= 0; i
< count
; i
++)
479 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
483 if (i
< count
) goto done
;
484 if (!wait_on( count
, objects
, flags
, sec
, usec
)) goto done
;
486 if ((ret
= check_wait( current
)) != -1)
488 /* condition is already satisfied */
494 /* now we need to wait */
495 if (flags
& SELECT_TIMEOUT
)
497 if (!(current
->wait
->user
= add_timeout_user( ¤t
->wait
->timeout
,
498 thread_timeout
, current
->wait
)))
504 current
->wait
->cookie
= cookie
;
505 set_error( STATUS_PENDING
);
508 while (--i
>= 0) release_object( objects
[i
] );
511 /* attempt to wake threads sleeping on the object wait queue */
512 void wake_up( struct object
*obj
, int max
)
514 struct wait_queue_entry
*entry
= obj
->head
;
518 struct thread
*thread
= entry
->thread
;
520 if (wake_thread( thread
))
522 if (max
&& !--max
) break;
527 /* queue an async procedure call */
528 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, void *func
,
529 enum apc_type type
, int system
, int nb_args
, ... )
531 struct thread_apc
*apc
;
532 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
534 /* cancel a possible previous APC with the same owner */
535 if (owner
) thread_cancel_apc( thread
, owner
, system
);
537 if (!(apc
= mem_alloc( sizeof(*apc
) + (nb_args
-1)*sizeof(apc
->args
[0]) ))) return 0;
538 apc
->prev
= queue
->tail
;
543 apc
->nb_args
= nb_args
;
548 va_start( args
, nb_args
);
549 for (i
= 0; i
< nb_args
; i
++) apc
->args
[i
] = va_arg( args
, void * );
553 if (!apc
->prev
) /* first one */
556 wake_thread( thread
);
561 /* cancel the async procedure call owned by a specific object */
562 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, int system
)
564 struct thread_apc
*apc
;
565 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
566 for (apc
= queue
->head
; apc
; apc
= apc
->next
)
568 if (apc
->owner
!= owner
) continue;
569 if (apc
->next
) apc
->next
->prev
= apc
->prev
;
570 else queue
->tail
= apc
->prev
;
571 if (apc
->prev
) apc
->prev
->next
= apc
->next
;
572 else queue
->head
= apc
->next
;
578 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
579 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
581 struct thread_apc
*apc
;
582 struct apc_queue
*queue
= &thread
->system_apc
;
584 if (!queue
->head
&& !system_only
) queue
= &thread
->user_apc
;
585 if ((apc
= queue
->head
))
587 if (apc
->next
) apc
->next
->prev
= NULL
;
588 else queue
->tail
= NULL
;
589 queue
->head
= apc
->next
;
594 /* add an fd to the inflight list */
595 /* return list index, or -1 on error */
596 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
600 if (server
== -1) return -1;
607 /* first check if we already have an entry for this fd */
608 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
609 if (thread
->inflight
[i
].client
== client
)
611 close( thread
->inflight
[i
].server
);
612 thread
->inflight
[i
].server
= server
;
616 /* now find a free spot to store it */
617 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
618 if (thread
->inflight
[i
].client
== -1)
620 thread
->inflight
[i
].client
= client
;
621 thread
->inflight
[i
].server
= server
;
627 /* get an inflight fd and purge it from the list */
628 /* the fd must be closed when no longer used */
629 int thread_get_inflight_fd( struct thread
*thread
, int client
)
633 if (client
== -1) return -1;
637 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
639 if (thread
->inflight
[i
].client
== client
)
641 ret
= thread
->inflight
[i
].server
;
642 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
646 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
650 /* retrieve an LDT selector entry */
651 static void get_selector_entry( struct thread
*thread
, int entry
,
652 unsigned int *base
, unsigned int *limit
,
653 unsigned char *flags
)
655 if (!thread
->process
->ldt_copy
)
657 set_error( STATUS_ACCESS_DENIED
);
662 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
665 if (suspend_for_ptrace( thread
))
667 unsigned char flags_buf
[4];
668 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
669 if (read_thread_int( thread
, addr
, base
) == -1) goto done
;
670 if (read_thread_int( thread
, addr
+ 8192, limit
) == -1) goto done
;
671 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
672 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
673 *flags
= flags_buf
[entry
& 3];
675 resume_thread( thread
);
679 /* kill a thread on the spot */
680 void kill_thread( struct thread
*thread
, int violent_death
)
682 if (thread
->state
== TERMINATED
) return; /* already killed */
683 thread
->state
= TERMINATED
;
684 if (current
== thread
) current
= NULL
;
686 fprintf( stderr
,"%08x: *killed* exit_code=%d\n",
687 (unsigned int)thread
, thread
->exit_code
);
690 while (thread
->wait
) end_wait( thread
);
691 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
692 /* if it is waiting on the socket, we don't need to send a SIGTERM */
695 debug_exit_thread( thread
);
696 abandon_mutexes( thread
);
697 remove_process_thread( thread
->process
, thread
);
698 wake_up( &thread
->obj
, 0 );
699 detach_thread( thread
, violent_death
? SIGTERM
: 0 );
700 remove_select_user( &thread
->obj
);
701 cleanup_thread( thread
);
702 release_object( thread
);
705 /* take a snapshot of currently running threads */
706 struct thread_snapshot
*thread_snap( int *count
)
708 struct thread_snapshot
*snapshot
, *ptr
;
709 struct thread
*thread
;
712 for (thread
= first_thread
; thread
; thread
= thread
->next
)
713 if (thread
->state
!= TERMINATED
) total
++;
714 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
716 for (thread
= first_thread
; thread
; thread
= thread
->next
)
718 if (thread
->state
== TERMINATED
) continue;
719 ptr
->thread
= thread
;
720 ptr
->count
= thread
->obj
.refcount
;
721 ptr
->priority
= thread
->priority
;
722 grab_object( thread
);
729 /* signal that we are finished booting on the client side */
730 DECL_HANDLER(boot_done
)
732 debug_level
= max( debug_level
, req
->debug_level
);
733 if (current
== booting_thread
)
735 booting_thread
= (struct thread
*)~0UL; /* make sure it doesn't match other threads */
736 lock_master_socket(0); /* allow other clients now */
740 /* create a new thread */
741 DECL_HANDLER(new_thread
)
743 struct thread
*thread
;
744 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
746 if (request_fd
== -1)
748 set_error( STATUS_INVALID_HANDLE
);
752 if ((thread
= create_thread( request_fd
, current
->process
)))
754 if (req
->suspend
) thread
->suspend
++;
756 if ((req
->handle
= alloc_handle( current
->process
, thread
,
757 THREAD_ALL_ACCESS
, req
->inherit
)))
759 /* thread object will be released when the thread gets killed */
762 kill_thread( thread
, 1 );
767 /* initialize a new thread */
768 DECL_HANDLER(init_thread
)
770 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
771 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
773 if (current
->unix_pid
)
775 fatal_protocol_error( current
, "init_thread: already running\n" );
780 fatal_protocol_error( current
, "bad reply fd\n" );
785 fatal_protocol_error( current
, "bad wait fd\n" );
789 current
->unix_pid
= req
->unix_pid
;
790 current
->teb
= req
->teb
;
791 current
->reply_fd
= reply_fd
;
792 current
->wait_fd
= wait_fd
;
794 if (current
->suspend
+ current
->process
->suspend
> 0) stop_thread( current
);
795 if (current
->process
->running_threads
> 1)
796 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
798 req
->pid
= get_process_id( current
->process
);
799 req
->tid
= get_thread_id( current
);
800 req
->boot
= (current
== booting_thread
);
801 req
->version
= SERVER_PROTOCOL_VERSION
;
805 if (reply_fd
!= -1) close( reply_fd
);
806 if (wait_fd
!= -1) close( wait_fd
);
809 /* set the shared buffer for a thread */
810 DECL_HANDLER(set_thread_buffer
)
812 unsigned int size
= MAX_REQUEST_LENGTH
;
813 unsigned int offset
= 0;
814 int fd
= thread_get_inflight_fd( current
, req
->fd
);
817 req
->offset
= offset
;
821 if (ftruncate( fd
, size
) == -1) file_set_error();
824 void *buffer
= mmap( 0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, offset
);
825 if (buffer
== (void *)-1) file_set_error();
828 if (current
->buffer
!= (void *)-1) munmap( current
->buffer
, size
);
829 current
->buffer
= buffer
;
834 else set_error( STATUS_INVALID_HANDLE
);
837 /* terminate a thread */
838 DECL_HANDLER(terminate_thread
)
840 struct thread
*thread
;
844 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
846 thread
->exit_code
= req
->exit_code
;
847 if (thread
!= current
) kill_thread( thread
, 1 );
851 req
->last
= (thread
->process
->running_threads
== 1);
853 release_object( thread
);
857 /* fetch information about a thread */
858 DECL_HANDLER(get_thread_info
)
860 struct thread
*thread
;
861 handle_t handle
= req
->handle
;
863 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
864 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
868 req
->tid
= get_thread_id( thread
);
869 req
->teb
= thread
->teb
;
870 req
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STILL_ACTIVE
;
871 req
->priority
= thread
->priority
;
872 release_object( thread
);
876 /* set information about a thread */
877 DECL_HANDLER(set_thread_info
)
879 struct thread
*thread
;
881 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
883 set_thread_info( thread
, req
);
884 release_object( thread
);
888 /* suspend a thread */
889 DECL_HANDLER(suspend_thread
)
891 struct thread
*thread
;
893 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
895 req
->count
= suspend_thread( thread
, 1 );
896 release_object( thread
);
900 /* resume a thread */
901 DECL_HANDLER(resume_thread
)
903 struct thread
*thread
;
905 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
907 req
->count
= resume_thread( thread
);
908 release_object( thread
);
912 /* select on a handle list */
915 int count
= get_req_data_size(req
) / sizeof(int);
916 select_on( count
, req
->cookie
, get_req_data(req
), req
->flags
, req
->sec
, req
->usec
);
919 /* queue an APC for a thread */
920 DECL_HANDLER(queue_apc
)
922 struct thread
*thread
;
923 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
925 thread_queue_apc( thread
, NULL
, req
->func
, APC_USER
, !req
->user
, 1, req
->param
);
926 release_object( thread
);
930 /* get next APC to call */
931 DECL_HANDLER(get_apc
)
933 struct thread_apc
*apc
;
938 if (!(apc
= thread_dequeue_apc( current
, !req
->alertable
)))
942 req
->type
= APC_NONE
;
943 set_req_data_size( req
, 0 );
946 /* Optimization: ignore APCs that have a NULL func; they are only used
947 * to wake up a thread, but since we got here the thread woke up already.
949 if (apc
->func
) break;
952 size
= apc
->nb_args
* sizeof(apc
->args
[0]);
953 if (size
> get_req_data_size(req
)) size
= get_req_data_size(req
);
954 req
->func
= apc
->func
;
955 req
->type
= apc
->type
;
956 memcpy( get_req_data(req
), apc
->args
, size
);
957 set_req_data_size( req
, size
);
961 /* fetch a selector entry for a thread */
962 DECL_HANDLER(get_selector_entry
)
964 struct thread
*thread
;
965 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
967 get_selector_entry( thread
, req
->entry
, &req
->base
, &req
->limit
, &req
->flags
);
968 release_object( thread
);