2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
32 #include <sys/types.h>
50 struct thread_wait
*next
; /* next wait structure for this thread */
51 struct thread
*thread
; /* owner thread */
52 int count
; /* count of objects */
54 void *cookie
; /* magic cookie to return to client */
55 struct timeval timeout
;
56 struct timeout_user
*user
;
57 struct wait_queue_entry queues
[1];
60 /* asynchronous procedure calls */
64 struct thread_apc
*next
; /* queue linked list */
65 struct thread_apc
*prev
;
66 struct object
*owner
; /* object that queued this apc */
67 void *func
; /* function to call in client */
68 enum apc_type type
; /* type of apc function */
69 int nb_args
; /* number of arguments */
70 void *args
[1]; /* function arguments */
74 /* thread operations */
76 static void dump_thread( struct object
*obj
, int verbose
);
77 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
78 static void thread_poll_event( struct object
*obj
, int event
);
79 static void destroy_thread( struct object
*obj
);
80 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
);
82 static const struct object_ops thread_ops
=
84 sizeof(struct thread
), /* size */
85 dump_thread
, /* dump */
86 add_queue
, /* add_queue */
87 remove_queue
, /* remove_queue */
88 thread_signaled
, /* signaled */
89 no_satisfied
, /* satisfied */
90 no_get_fd
, /* get_fd */
91 no_get_file_info
, /* get_file_info */
92 destroy_thread
/* destroy */
95 static const struct fd_ops thread_fd_ops
=
97 NULL
, /* get_poll_events */
98 thread_poll_event
, /* poll_event */
100 no_get_file_info
, /* get_file_info */
101 no_queue_async
/* queue_async */
104 static struct thread
*first_thread
;
105 static struct thread
*booting_thread
;
107 /* initialize the structure for a newly allocated thread */
108 inline static void init_thread_structure( struct thread
*thread
)
112 thread
->unix_pid
= 0; /* not known yet */
113 thread
->context
= NULL
;
115 thread
->mutex
= NULL
;
116 thread
->debug_ctx
= NULL
;
117 thread
->debug_event
= NULL
;
118 thread
->queue
= NULL
;
119 thread
->hooks
= NULL
;
122 thread
->system_apc
.head
= NULL
;
123 thread
->system_apc
.tail
= NULL
;
124 thread
->user_apc
.head
= NULL
;
125 thread
->user_apc
.tail
= NULL
;
127 thread
->req_data
= NULL
;
128 thread
->req_toread
= 0;
129 thread
->reply_data
= NULL
;
130 thread
->reply_towrite
= 0;
131 thread
->reply_fd
= -1;
132 thread
->wait_fd
= -1;
133 thread
->state
= RUNNING
;
134 thread
->attached
= 0;
135 thread
->exit_code
= 0;
138 thread
->priority
= THREAD_PRIORITY_NORMAL
;
139 thread
->affinity
= 1;
141 thread
->creation_time
= time(NULL
);
142 thread
->exit_time
= 0;
144 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
145 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
148 /* create a new thread */
149 struct thread
*create_thread( int fd
, struct process
*process
)
151 struct thread
*thread
;
153 if (!(thread
= alloc_fd_object( &thread_ops
, &thread_fd_ops
, fd
))) return NULL
;
155 init_thread_structure( thread
);
157 thread
->process
= (struct process
*)grab_object( process
);
158 thread
->request_fd
= fd
;
159 if (!current
) current
= thread
;
161 if (!booting_thread
) /* first thread ever */
163 booting_thread
= thread
;
164 lock_master_socket(1);
167 if ((thread
->next
= first_thread
) != NULL
) thread
->next
->prev
= thread
;
168 first_thread
= thread
;
170 if (!(thread
->id
= alloc_ptid( thread
)))
172 release_object( thread
);
176 set_select_events( &thread
->obj
, POLLIN
); /* start listening to events */
177 add_process_thread( thread
->process
, thread
);
181 /* handle a client event */
182 static void thread_poll_event( struct object
*obj
, int event
)
184 struct thread
*thread
= (struct thread
*)obj
;
185 assert( obj
->ops
== &thread_ops
);
187 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
188 else if (event
& POLLIN
) read_request( thread
);
189 else if (event
& POLLOUT
) write_reply( thread
);
192 /* cleanup everything that is no longer needed by a dead thread */
193 /* used by destroy_thread and kill_thread */
194 static void cleanup_thread( struct thread
*thread
)
197 struct thread_apc
*apc
;
199 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
200 if (thread
->req_data
) free( thread
->req_data
);
201 if (thread
->reply_data
) free( thread
->reply_data
);
202 if (thread
->request_fd
!= -1) close( thread
->request_fd
);
203 if (thread
->reply_fd
!= -1) close( thread
->reply_fd
);
204 if (thread
->wait_fd
!= -1) close( thread
->wait_fd
);
205 if (thread
->hooks
) release_object( thread
->hooks
);
206 free_msg_queue( thread
);
207 destroy_thread_windows( thread
);
208 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
210 if (thread
->inflight
[i
].client
!= -1)
212 close( thread
->inflight
[i
].server
);
213 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
216 thread
->req_data
= NULL
;
217 thread
->reply_data
= NULL
;
218 thread
->request_fd
= -1;
219 thread
->reply_fd
= -1;
220 thread
->wait_fd
= -1;
221 thread
->hooks
= NULL
;
223 if (thread
== booting_thread
) /* killing booting thread */
225 booting_thread
= NULL
;
226 lock_master_socket(0);
230 /* destroy a thread when its refcount is 0 */
231 static void destroy_thread( struct object
*obj
)
233 struct thread_apc
*apc
;
234 struct thread
*thread
= (struct thread
*)obj
;
235 assert( obj
->ops
== &thread_ops
);
237 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
238 if (thread
->next
) thread
->next
->prev
= thread
->prev
;
239 if (thread
->prev
) thread
->prev
->next
= thread
->next
;
240 else first_thread
= thread
->next
;
241 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
242 if (thread
->info
) release_object( thread
->info
);
243 cleanup_thread( thread
);
244 release_object( thread
->process
);
245 if (thread
->id
) free_ptid( thread
->id
);
248 /* dump a thread on stdout for debugging purposes */
249 static void dump_thread( struct object
*obj
, int verbose
)
251 struct thread
*thread
= (struct thread
*)obj
;
252 assert( obj
->ops
== &thread_ops
);
254 fprintf( stderr
, "Thread id=%04x unix pid=%d teb=%p state=%d\n",
255 thread
->id
, thread
->unix_pid
, thread
->teb
, thread
->state
);
258 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
260 struct thread
*mythread
= (struct thread
*)obj
;
261 return (mythread
->state
== TERMINATED
);
264 /* get a thread pointer from a thread id (and increment the refcount) */
265 struct thread
*get_thread_from_id( thread_id_t id
)
267 struct object
*obj
= get_ptid_entry( id
);
269 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
270 set_error( STATUS_INVALID_PARAMETER
);
274 /* get a thread from a handle (and increment the refcount) */
275 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
277 return (struct thread
*)get_handle_obj( current
->process
, handle
,
278 access
, &thread_ops
);
281 /* find a thread from a Unix pid */
282 struct thread
*get_thread_from_pid( int pid
)
284 struct thread
*t
= first_thread
;
285 while (t
&& (t
->unix_pid
!= pid
)) t
= t
->next
;
289 /* set all information about a thread */
290 static void set_thread_info( struct thread
*thread
,
291 const struct set_thread_info_request
*req
)
293 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
294 thread
->priority
= req
->priority
;
295 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
297 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
298 else thread
->affinity
= req
->affinity
;
302 /* suspend a thread */
303 int suspend_thread( struct thread
*thread
, int check_limit
)
305 int old_count
= thread
->suspend
;
306 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
|| !check_limit
)
308 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
310 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
314 /* resume a thread */
315 int resume_thread( struct thread
*thread
)
317 int old_count
= thread
->suspend
;
318 if (thread
->suspend
> 0)
320 if (!(--thread
->suspend
+ thread
->process
->suspend
)) continue_thread( thread
);
325 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
326 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
330 entry
->prev
= obj
->tail
;
332 if (obj
->tail
) obj
->tail
->next
= entry
;
333 else obj
->head
= entry
;
338 /* remove a thread from an object wait queue */
339 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
341 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
342 else obj
->tail
= entry
->prev
;
343 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
344 else obj
->head
= entry
->next
;
345 release_object( obj
);
349 static void end_wait( struct thread
*thread
)
351 struct thread_wait
*wait
= thread
->wait
;
352 struct wait_queue_entry
*entry
;
356 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
357 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
358 if (wait
->user
) remove_timeout_user( wait
->user
);
359 thread
->wait
= wait
->next
;
363 /* build the thread wait structure */
364 static int wait_on( int count
, struct object
*objects
[], int flags
, int sec
, int usec
)
366 struct thread_wait
*wait
;
367 struct wait_queue_entry
*entry
;
370 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
371 wait
->next
= current
->wait
;
372 wait
->thread
= current
;
376 current
->wait
= wait
;
377 if (flags
& SELECT_TIMEOUT
)
379 wait
->timeout
.tv_sec
= sec
;
380 wait
->timeout
.tv_usec
= usec
;
383 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
385 struct object
*obj
= objects
[i
];
386 entry
->thread
= current
;
387 if (!obj
->ops
->add_queue( obj
, entry
))
397 /* check if the thread waiting condition is satisfied */
398 static int check_wait( struct thread
*thread
)
401 struct thread_wait
*wait
= thread
->wait
;
402 struct wait_queue_entry
*entry
= wait
->queues
;
405 if (wait
->flags
& SELECT_ALL
)
408 /* Note: we must check them all anyway, as some objects may
409 * want to do something when signaled, even if others are not */
410 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
411 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
412 if (not_ok
) goto other_checks
;
413 /* Wait satisfied: tell it to all objects */
415 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
416 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
417 signaled
= STATUS_ABANDONED_WAIT_0
;
422 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
424 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
425 /* Wait satisfied: tell it to the object */
427 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
428 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
434 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && thread
->system_apc
.head
) return STATUS_USER_APC
;
435 if ((wait
->flags
& SELECT_ALERTABLE
) && thread
->user_apc
.head
) return STATUS_USER_APC
;
436 if (wait
->flags
& SELECT_TIMEOUT
)
439 gettimeofday( &now
, NULL
);
440 if (!time_before( &now
, &wait
->timeout
)) return STATUS_TIMEOUT
;
445 /* send the wakeup signal to a thread */
446 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
448 struct wake_up_reply reply
;
451 reply
.cookie
= cookie
;
452 reply
.signaled
= signaled
;
453 if ((ret
= write( thread
->wait_fd
, &reply
, sizeof(reply
) )) == sizeof(reply
)) return 0;
455 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
456 else if (errno
== EPIPE
)
457 kill_thread( thread
, 0 ); /* normal death */
459 fatal_protocol_perror( thread
, "write" );
463 /* attempt to wake up a thread */
464 /* return >0 if OK, 0 if the wait condition is still not satisfied */
465 static int wake_thread( struct thread
*thread
)
470 for (count
= 0; thread
->wait
; count
++)
472 if ((signaled
= check_wait( thread
)) == -1) break;
474 cookie
= thread
->wait
->cookie
;
475 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
476 thread
->id
, signaled
, cookie
);
478 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
484 /* thread wait timeout */
485 static void thread_timeout( void *ptr
)
487 struct thread_wait
*wait
= ptr
;
488 struct thread
*thread
= wait
->thread
;
489 void *cookie
= wait
->cookie
;
492 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
494 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
495 thread
->id
, STATUS_TIMEOUT
, cookie
);
497 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
498 /* check if other objects have become signaled in the meantime */
499 wake_thread( thread
);
502 /* select on a list of handles */
503 static void select_on( int count
, void *cookie
, const obj_handle_t
*handles
,
504 int flags
, int sec
, int usec
)
507 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
509 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
511 set_error( STATUS_INVALID_PARAMETER
);
514 for (i
= 0; i
< count
; i
++)
516 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
520 if (i
< count
) goto done
;
521 if (!wait_on( count
, objects
, flags
, sec
, usec
)) goto done
;
523 if ((ret
= check_wait( current
)) != -1)
525 /* condition is already satisfied */
531 /* now we need to wait */
532 if (flags
& SELECT_TIMEOUT
)
534 if (!(current
->wait
->user
= add_timeout_user( ¤t
->wait
->timeout
,
535 thread_timeout
, current
->wait
)))
541 current
->wait
->cookie
= cookie
;
542 set_error( STATUS_PENDING
);
545 while (--i
>= 0) release_object( objects
[i
] );
548 /* attempt to wake threads sleeping on the object wait queue */
549 void wake_up( struct object
*obj
, int max
)
551 struct wait_queue_entry
*entry
= obj
->head
;
555 struct thread
*thread
= entry
->thread
;
557 if (wake_thread( thread
))
559 if (max
&& !--max
) break;
564 /* queue an async procedure call */
565 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, void *func
,
566 enum apc_type type
, int system
, int nb_args
, ... )
568 struct thread_apc
*apc
;
569 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
571 /* cancel a possible previous APC with the same owner */
572 if (owner
) thread_cancel_apc( thread
, owner
, system
);
573 if (thread
->state
== TERMINATED
) return 0;
575 if (!(apc
= mem_alloc( sizeof(*apc
) + (nb_args
-1)*sizeof(apc
->args
[0]) ))) return 0;
576 apc
->prev
= queue
->tail
;
581 apc
->nb_args
= nb_args
;
586 va_start( args
, nb_args
);
587 for (i
= 0; i
< nb_args
; i
++) apc
->args
[i
] = va_arg( args
, void * );
591 if (!apc
->prev
) /* first one */
594 wake_thread( thread
);
596 else apc
->prev
->next
= apc
;
601 /* cancel the async procedure call owned by a specific object */
602 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, int system
)
604 struct thread_apc
*apc
;
605 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
606 for (apc
= queue
->head
; apc
; apc
= apc
->next
)
608 if (apc
->owner
!= owner
) continue;
609 if (apc
->next
) apc
->next
->prev
= apc
->prev
;
610 else queue
->tail
= apc
->prev
;
611 if (apc
->prev
) apc
->prev
->next
= apc
->next
;
612 else queue
->head
= apc
->next
;
618 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
619 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
621 struct thread_apc
*apc
;
622 struct apc_queue
*queue
= &thread
->system_apc
;
624 if (!queue
->head
&& !system_only
) queue
= &thread
->user_apc
;
625 if ((apc
= queue
->head
))
627 if (apc
->next
) apc
->next
->prev
= NULL
;
628 else queue
->tail
= NULL
;
629 queue
->head
= apc
->next
;
634 /* add an fd to the inflight list */
635 /* return list index, or -1 on error */
636 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
640 if (server
== -1) return -1;
647 /* first check if we already have an entry for this fd */
648 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
649 if (thread
->inflight
[i
].client
== client
)
651 close( thread
->inflight
[i
].server
);
652 thread
->inflight
[i
].server
= server
;
656 /* now find a free spot to store it */
657 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
658 if (thread
->inflight
[i
].client
== -1)
660 thread
->inflight
[i
].client
= client
;
661 thread
->inflight
[i
].server
= server
;
667 /* get an inflight fd and purge it from the list */
668 /* the fd must be closed when no longer used */
669 int thread_get_inflight_fd( struct thread
*thread
, int client
)
673 if (client
== -1) return -1;
677 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
679 if (thread
->inflight
[i
].client
== client
)
681 ret
= thread
->inflight
[i
].server
;
682 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
686 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
690 /* retrieve an LDT selector entry */
691 static void get_selector_entry( struct thread
*thread
, int entry
,
692 unsigned int *base
, unsigned int *limit
,
693 unsigned char *flags
)
695 if (!thread
->process
->ldt_copy
)
697 set_error( STATUS_ACCESS_DENIED
);
702 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
705 if (suspend_for_ptrace( thread
))
707 unsigned char flags_buf
[4];
708 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
709 if (read_thread_int( thread
, addr
, base
) == -1) goto done
;
710 if (read_thread_int( thread
, addr
+ 8192, limit
) == -1) goto done
;
711 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
712 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
713 *flags
= flags_buf
[entry
& 3];
715 resume_thread( thread
);
719 /* kill a thread on the spot */
720 void kill_thread( struct thread
*thread
, int violent_death
)
722 if (thread
->state
== TERMINATED
) return; /* already killed */
723 thread
->state
= TERMINATED
;
724 thread
->exit_time
= time(NULL
);
725 if (current
== thread
) current
= NULL
;
727 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
728 thread
->id
, thread
->exit_code
);
731 while (thread
->wait
) end_wait( thread
);
732 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
733 /* if it is waiting on the socket, we don't need to send a SIGTERM */
736 kill_console_processes( thread
, 0 );
737 debug_exit_thread( thread
);
738 abandon_mutexes( thread
);
739 remove_process_thread( thread
->process
, thread
);
740 wake_up( &thread
->obj
, 0 );
741 detach_thread( thread
, violent_death
? SIGTERM
: 0 );
742 if (thread
->request_fd
== thread
->obj
.fd
) thread
->request_fd
= -1;
743 if (thread
->reply_fd
== thread
->obj
.fd
) thread
->reply_fd
= -1;
744 remove_select_user( &thread
->obj
);
745 cleanup_thread( thread
);
746 release_object( thread
);
749 /* take a snapshot of currently running threads */
750 struct thread_snapshot
*thread_snap( int *count
)
752 struct thread_snapshot
*snapshot
, *ptr
;
753 struct thread
*thread
;
756 for (thread
= first_thread
; thread
; thread
= thread
->next
)
757 if (thread
->state
!= TERMINATED
) total
++;
758 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
760 for (thread
= first_thread
; thread
; thread
= thread
->next
)
762 if (thread
->state
== TERMINATED
) continue;
763 ptr
->thread
= thread
;
764 ptr
->count
= thread
->obj
.refcount
;
765 ptr
->priority
= thread
->priority
;
766 grab_object( thread
);
773 /* signal that we are finished booting on the client side */
774 DECL_HANDLER(boot_done
)
776 debug_level
= max( debug_level
, req
->debug_level
);
777 if (current
== booting_thread
)
779 booting_thread
= (struct thread
*)~0UL; /* make sure it doesn't match other threads */
780 lock_master_socket(0); /* allow other clients now */
784 /* create a new thread */
785 DECL_HANDLER(new_thread
)
787 struct thread
*thread
;
788 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
790 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
792 if (request_fd
!= -1) close( request_fd
);
793 set_error( STATUS_INVALID_HANDLE
);
797 if ((thread
= create_thread( request_fd
, current
->process
)))
799 if (req
->suspend
) thread
->suspend
++;
800 reply
->tid
= get_thread_id( thread
);
801 if ((reply
->handle
= alloc_handle( current
->process
, thread
,
802 THREAD_ALL_ACCESS
, req
->inherit
)))
804 /* thread object will be released when the thread gets killed */
807 kill_thread( thread
, 1 );
811 /* initialize a new thread */
812 DECL_HANDLER(init_thread
)
814 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
815 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
817 if (current
->unix_pid
)
819 fatal_protocol_error( current
, "init_thread: already running\n" );
822 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1)
824 fatal_protocol_error( current
, "bad reply fd\n" );
829 fatal_protocol_error( current
, "bad wait fd\n" );
833 current
->unix_pid
= req
->unix_pid
;
834 current
->teb
= req
->teb
;
835 current
->reply_fd
= reply_fd
;
836 current
->wait_fd
= wait_fd
;
838 if (current
->suspend
+ current
->process
->suspend
> 0) stop_thread( current
);
839 if (current
->process
->running_threads
> 1)
840 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
842 reply
->pid
= get_process_id( current
->process
);
843 reply
->tid
= get_thread_id( current
);
844 reply
->boot
= (current
== booting_thread
);
845 reply
->version
= SERVER_PROTOCOL_VERSION
;
849 if (reply_fd
!= -1) close( reply_fd
);
850 if (wait_fd
!= -1) close( wait_fd
);
853 /* terminate a thread */
854 DECL_HANDLER(terminate_thread
)
856 struct thread
*thread
;
860 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
862 thread
->exit_code
= req
->exit_code
;
863 if (thread
!= current
) kill_thread( thread
, 1 );
867 reply
->last
= (thread
->process
->running_threads
== 1);
869 release_object( thread
);
873 /* open a handle to a thread */
874 DECL_HANDLER(open_thread
)
876 struct thread
*thread
= get_thread_from_id( req
->tid
);
881 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->inherit
);
882 release_object( thread
);
886 /* fetch information about a thread */
887 DECL_HANDLER(get_thread_info
)
889 struct thread
*thread
;
890 obj_handle_t handle
= req
->handle
;
892 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
893 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
897 reply
->tid
= get_thread_id( thread
);
898 reply
->teb
= thread
->teb
;
899 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STILL_ACTIVE
;
900 reply
->priority
= thread
->priority
;
901 reply
->creation_time
= thread
->creation_time
;
902 reply
->exit_time
= thread
->exit_time
;
904 release_object( thread
);
908 /* set information about a thread */
909 DECL_HANDLER(set_thread_info
)
911 struct thread
*thread
;
913 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
915 set_thread_info( thread
, req
);
916 release_object( thread
);
920 /* suspend a thread */
921 DECL_HANDLER(suspend_thread
)
923 struct thread
*thread
;
925 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
927 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
928 else reply
->count
= suspend_thread( thread
, 1 );
929 release_object( thread
);
933 /* resume a thread */
934 DECL_HANDLER(resume_thread
)
936 struct thread
*thread
;
938 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
940 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
941 else reply
->count
= resume_thread( thread
);
942 release_object( thread
);
946 /* select on a handle list */
949 int count
= get_req_data_size() / sizeof(int);
950 select_on( count
, req
->cookie
, get_req_data(), req
->flags
, req
->sec
, req
->usec
);
953 /* queue an APC for a thread */
954 DECL_HANDLER(queue_apc
)
956 struct thread
*thread
;
957 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
959 thread_queue_apc( thread
, NULL
, req
->func
, APC_USER
, !req
->user
, 1, req
->param
);
960 release_object( thread
);
964 /* get next APC to call */
965 DECL_HANDLER(get_apc
)
967 struct thread_apc
*apc
;
972 if (!(apc
= thread_dequeue_apc( current
, !req
->alertable
)))
976 reply
->type
= APC_NONE
;
979 /* Optimization: ignore APCs that have a NULL func; they are only used
980 * to wake up a thread, but since we got here the thread woke up already.
981 * Exception: for APC_ASYNC_IO, func == NULL is legal.
983 if (apc
->func
|| apc
->type
== APC_ASYNC_IO
) break;
986 size
= apc
->nb_args
* sizeof(apc
->args
[0]);
987 if (size
> get_reply_max_size()) size
= get_reply_max_size();
988 reply
->func
= apc
->func
;
989 reply
->type
= apc
->type
;
990 set_reply_data( apc
->args
, size
);
994 /* fetch a selector entry for a thread */
995 DECL_HANDLER(get_selector_entry
)
997 struct thread
*thread
;
998 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1000 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1001 release_object( thread
);