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 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
449 /* thread wait timeout */
450 static void thread_timeout( void *ptr
)
452 struct thread_wait
*wait
= ptr
;
453 struct thread
*thread
= wait
->thread
;
454 void *cookie
= wait
->cookie
;
457 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
459 if (debug_level
) fprintf( stderr
, "%08x: *wakeup* signaled=%d cookie=%p\n",
460 (unsigned int)thread
, STATUS_TIMEOUT
, cookie
);
462 send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
);
463 /* check if other objects have become signaled in the meantime */
464 wake_thread( thread
);
467 /* select on a list of handles */
468 static void select_on( int count
, void *cookie
, handle_t
*handles
, int flags
, int sec
, int usec
)
471 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
473 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
475 set_error( STATUS_INVALID_PARAMETER
);
478 for (i
= 0; i
< count
; i
++)
480 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
484 if (i
< count
) goto done
;
485 if (!wait_on( count
, objects
, flags
, sec
, usec
)) goto done
;
487 if ((ret
= check_wait( current
)) != -1)
489 /* condition is already satisfied */
495 /* now we need to wait */
496 if (flags
& SELECT_TIMEOUT
)
498 if (!(current
->wait
->user
= add_timeout_user( ¤t
->wait
->timeout
,
499 thread_timeout
, current
->wait
)))
505 current
->wait
->cookie
= cookie
;
506 set_error( STATUS_PENDING
);
509 while (--i
>= 0) release_object( objects
[i
] );
512 /* attempt to wake threads sleeping on the object wait queue */
513 void wake_up( struct object
*obj
, int max
)
515 struct wait_queue_entry
*entry
= obj
->head
;
519 struct thread
*thread
= entry
->thread
;
521 if (wake_thread( thread
))
523 if (max
&& !--max
) break;
528 /* queue an async procedure call */
529 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, void *func
,
530 enum apc_type type
, int system
, int nb_args
, ... )
532 struct thread_apc
*apc
;
533 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
535 /* cancel a possible previous APC with the same owner */
536 if (owner
) thread_cancel_apc( thread
, owner
, system
);
538 if (!(apc
= mem_alloc( sizeof(*apc
) + (nb_args
-1)*sizeof(apc
->args
[0]) ))) return 0;
539 apc
->prev
= queue
->tail
;
544 apc
->nb_args
= nb_args
;
549 va_start( args
, nb_args
);
550 for (i
= 0; i
< nb_args
; i
++) apc
->args
[i
] = va_arg( args
, void * );
554 if (!apc
->prev
) /* first one */
557 wake_thread( thread
);
562 /* cancel the async procedure call owned by a specific object */
563 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, int system
)
565 struct thread_apc
*apc
;
566 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
567 for (apc
= queue
->head
; apc
; apc
= apc
->next
)
569 if (apc
->owner
!= owner
) continue;
570 if (apc
->next
) apc
->next
->prev
= apc
->prev
;
571 else queue
->tail
= apc
->prev
;
572 if (apc
->prev
) apc
->prev
->next
= apc
->next
;
573 else queue
->head
= apc
->next
;
579 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
580 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
582 struct thread_apc
*apc
;
583 struct apc_queue
*queue
= &thread
->system_apc
;
585 if (!queue
->head
&& !system_only
) queue
= &thread
->user_apc
;
586 if ((apc
= queue
->head
))
588 if (apc
->next
) apc
->next
->prev
= NULL
;
589 else queue
->tail
= NULL
;
590 queue
->head
= apc
->next
;
595 /* add an fd to the inflight list */
596 /* return list index, or -1 on error */
597 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
601 if (server
== -1) return -1;
608 /* first check if we already have an entry for this fd */
609 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
610 if (thread
->inflight
[i
].client
== client
)
612 close( thread
->inflight
[i
].server
);
613 thread
->inflight
[i
].server
= server
;
617 /* now find a free spot to store it */
618 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
619 if (thread
->inflight
[i
].client
== -1)
621 thread
->inflight
[i
].client
= client
;
622 thread
->inflight
[i
].server
= server
;
628 /* get an inflight fd and purge it from the list */
629 /* the fd must be closed when no longer used */
630 int thread_get_inflight_fd( struct thread
*thread
, int client
)
634 if (client
== -1) return -1;
638 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
640 if (thread
->inflight
[i
].client
== client
)
642 ret
= thread
->inflight
[i
].server
;
643 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
647 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
651 /* retrieve an LDT selector entry */
652 static void get_selector_entry( struct thread
*thread
, int entry
,
653 unsigned int *base
, unsigned int *limit
,
654 unsigned char *flags
)
656 if (!thread
->process
->ldt_copy
)
658 set_error( STATUS_ACCESS_DENIED
);
663 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
666 if (suspend_for_ptrace( thread
))
668 unsigned char flags_buf
[4];
669 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
670 if (read_thread_int( thread
, addr
, base
) == -1) goto done
;
671 if (read_thread_int( thread
, addr
+ 8192, limit
) == -1) goto done
;
672 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
673 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
674 *flags
= flags_buf
[entry
& 3];
676 resume_thread( thread
);
680 /* kill a thread on the spot */
681 void kill_thread( struct thread
*thread
, int violent_death
)
683 if (thread
->state
== TERMINATED
) return; /* already killed */
684 thread
->state
= TERMINATED
;
685 if (current
== thread
) current
= NULL
;
687 fprintf( stderr
,"%08x: *killed* exit_code=%d\n",
688 (unsigned int)thread
, thread
->exit_code
);
691 while (thread
->wait
) end_wait( thread
);
692 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
693 /* if it is waiting on the socket, we don't need to send a SIGTERM */
696 debug_exit_thread( thread
);
697 abandon_mutexes( thread
);
698 remove_process_thread( thread
->process
, thread
);
699 wake_up( &thread
->obj
, 0 );
700 detach_thread( thread
, violent_death
? SIGTERM
: 0 );
701 remove_select_user( &thread
->obj
);
702 cleanup_thread( thread
);
703 release_object( thread
);
706 /* take a snapshot of currently running threads */
707 struct thread_snapshot
*thread_snap( int *count
)
709 struct thread_snapshot
*snapshot
, *ptr
;
710 struct thread
*thread
;
713 for (thread
= first_thread
; thread
; thread
= thread
->next
)
714 if (thread
->state
!= TERMINATED
) total
++;
715 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
717 for (thread
= first_thread
; thread
; thread
= thread
->next
)
719 if (thread
->state
== TERMINATED
) continue;
720 ptr
->thread
= thread
;
721 ptr
->count
= thread
->obj
.refcount
;
722 ptr
->priority
= thread
->priority
;
723 grab_object( thread
);
730 /* signal that we are finished booting on the client side */
731 DECL_HANDLER(boot_done
)
733 debug_level
= max( debug_level
, req
->debug_level
);
734 if (current
== booting_thread
)
736 booting_thread
= (struct thread
*)~0UL; /* make sure it doesn't match other threads */
737 lock_master_socket(0); /* allow other clients now */
741 /* create a new thread */
742 DECL_HANDLER(new_thread
)
744 struct thread
*thread
;
745 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
747 if (request_fd
== -1)
749 set_error( STATUS_INVALID_HANDLE
);
753 if ((thread
= create_thread( request_fd
, current
->process
)))
755 if (req
->suspend
) thread
->suspend
++;
757 if ((req
->handle
= alloc_handle( current
->process
, thread
,
758 THREAD_ALL_ACCESS
, req
->inherit
)))
760 /* thread object will be released when the thread gets killed */
763 kill_thread( thread
, 1 );
768 /* initialize a new thread */
769 DECL_HANDLER(init_thread
)
771 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
772 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
774 if (current
->unix_pid
)
776 fatal_protocol_error( current
, "init_thread: already running\n" );
781 fatal_protocol_error( current
, "bad reply fd\n" );
786 fatal_protocol_error( current
, "bad wait fd\n" );
790 current
->unix_pid
= req
->unix_pid
;
791 current
->teb
= req
->teb
;
792 current
->reply_fd
= reply_fd
;
793 current
->wait_fd
= wait_fd
;
795 if (current
->suspend
+ current
->process
->suspend
> 0) stop_thread( current
);
796 if (current
->process
->running_threads
> 1)
797 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
799 req
->pid
= get_process_id( current
->process
);
800 req
->tid
= get_thread_id( current
);
801 req
->boot
= (current
== booting_thread
);
802 req
->version
= SERVER_PROTOCOL_VERSION
;
806 if (reply_fd
!= -1) close( reply_fd
);
807 if (wait_fd
!= -1) close( wait_fd
);
810 /* set the shared buffer for a thread */
811 DECL_HANDLER(set_thread_buffer
)
813 unsigned int size
= MAX_REQUEST_LENGTH
;
814 unsigned int offset
= 0;
815 int fd
= thread_get_inflight_fd( current
, req
->fd
);
818 req
->offset
= offset
;
822 if (ftruncate( fd
, size
) == -1) file_set_error();
825 void *buffer
= mmap( 0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, offset
);
826 if (buffer
== (void *)-1) file_set_error();
829 if (current
->buffer
!= (void *)-1) munmap( current
->buffer
, size
);
830 current
->buffer
= buffer
;
835 else set_error( STATUS_INVALID_HANDLE
);
838 /* terminate a thread */
839 DECL_HANDLER(terminate_thread
)
841 struct thread
*thread
;
845 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
847 thread
->exit_code
= req
->exit_code
;
848 if (thread
!= current
) kill_thread( thread
, 1 );
852 req
->last
= (thread
->process
->running_threads
== 1);
854 release_object( thread
);
858 /* fetch information about a thread */
859 DECL_HANDLER(get_thread_info
)
861 struct thread
*thread
;
862 handle_t handle
= req
->handle
;
864 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
865 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
869 req
->tid
= get_thread_id( thread
);
870 req
->teb
= thread
->teb
;
871 req
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STILL_ACTIVE
;
872 req
->priority
= thread
->priority
;
873 release_object( thread
);
877 /* set information about a thread */
878 DECL_HANDLER(set_thread_info
)
880 struct thread
*thread
;
882 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
884 set_thread_info( thread
, req
);
885 release_object( thread
);
889 /* suspend a thread */
890 DECL_HANDLER(suspend_thread
)
892 struct thread
*thread
;
894 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
896 req
->count
= suspend_thread( thread
, 1 );
897 release_object( thread
);
901 /* resume a thread */
902 DECL_HANDLER(resume_thread
)
904 struct thread
*thread
;
906 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
908 req
->count
= resume_thread( thread
);
909 release_object( thread
);
913 /* select on a handle list */
916 int count
= get_req_data_size(req
) / sizeof(int);
917 select_on( count
, req
->cookie
, get_req_data(req
), req
->flags
, req
->sec
, req
->usec
);
920 /* queue an APC for a thread */
921 DECL_HANDLER(queue_apc
)
923 struct thread
*thread
;
924 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
926 thread_queue_apc( thread
, NULL
, req
->func
, APC_USER
, !req
->user
, 1, req
->param
);
927 release_object( thread
);
931 /* get next APC to call */
932 DECL_HANDLER(get_apc
)
934 struct thread_apc
*apc
;
939 if (!(apc
= thread_dequeue_apc( current
, !req
->alertable
)))
943 req
->type
= APC_NONE
;
944 set_req_data_size( req
, 0 );
947 /* Optimization: ignore APCs that have a NULL func; they are only used
948 * to wake up a thread, but since we got here the thread woke up already.
950 if (apc
->func
) break;
953 size
= apc
->nb_args
* sizeof(apc
->args
[0]);
954 if (size
> get_req_data_size(req
)) size
= get_req_data_size(req
);
955 req
->func
= apc
->func
;
956 req
->type
= apc
->type
;
957 memcpy( get_req_data(req
), apc
->args
, size
);
958 set_req_data_size( req
, size
);
962 /* fetch a selector entry for a thread */
963 DECL_HANDLER(get_selector_entry
)
965 struct thread
*thread
;
966 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
968 get_selector_entry( thread
, req
->entry
, &req
->base
, &req
->limit
, &req
->flags
);
969 release_object( thread
);