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>
51 struct thread_wait
*next
; /* next wait structure for this thread */
52 struct thread
*thread
; /* owner thread */
53 int count
; /* count of objects */
55 void *cookie
; /* magic cookie to return to client */
56 struct timeval timeout
;
57 struct timeout_user
*user
;
58 struct wait_queue_entry queues
[1];
61 /* asynchronous procedure calls */
65 struct thread_apc
*next
; /* queue linked list */
66 struct thread_apc
*prev
;
67 struct object
*owner
; /* object that queued this apc */
68 void *func
; /* function to call in client */
69 enum apc_type type
; /* type of apc function */
70 int nb_args
; /* number of arguments */
71 void *arg1
; /* function arguments */
77 /* thread operations */
79 static void dump_thread( struct object
*obj
, int verbose
);
80 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
81 static void thread_poll_event( struct fd
*fd
, int event
);
82 static void destroy_thread( struct object
*obj
);
83 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
);
85 static const struct object_ops thread_ops
=
87 sizeof(struct thread
), /* size */
88 dump_thread
, /* dump */
89 add_queue
, /* add_queue */
90 remove_queue
, /* remove_queue */
91 thread_signaled
, /* signaled */
92 no_satisfied
, /* satisfied */
93 no_get_fd
, /* get_fd */
94 destroy_thread
/* destroy */
97 static const struct fd_ops thread_fd_ops
=
99 NULL
, /* get_poll_events */
100 thread_poll_event
, /* poll_event */
101 no_flush
, /* flush */
102 no_get_file_info
, /* get_file_info */
103 no_queue_async
, /* queue_async */
104 no_cancel_async
/* cancel_async */
107 static struct thread
*first_thread
;
108 static struct thread
*booting_thread
;
110 /* initialize the structure for a newly allocated thread */
111 inline static void init_thread_structure( struct thread
*thread
)
115 thread
->unix_pid
= -1; /* not known yet */
116 thread
->unix_tid
= -1; /* not known yet */
117 thread
->context
= NULL
;
119 thread
->mutex
= NULL
;
120 thread
->debug_ctx
= NULL
;
121 thread
->debug_event
= NULL
;
122 thread
->queue
= NULL
;
124 thread
->system_apc
.head
= NULL
;
125 thread
->system_apc
.tail
= NULL
;
126 thread
->user_apc
.head
= NULL
;
127 thread
->user_apc
.tail
= NULL
;
129 thread
->req_data
= NULL
;
130 thread
->req_toread
= 0;
131 thread
->reply_data
= NULL
;
132 thread
->reply_towrite
= 0;
133 thread
->request_fd
= NULL
;
134 thread
->reply_fd
= NULL
;
135 thread
->wait_fd
= NULL
;
136 thread
->state
= RUNNING
;
137 thread
->attached
= 0;
138 thread
->exit_code
= 0;
141 thread
->priority
= THREAD_PRIORITY_NORMAL
;
142 thread
->affinity
= 1;
144 thread
->creation_time
= time(NULL
);
145 thread
->exit_time
= 0;
147 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
148 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
151 /* create a new thread */
152 struct thread
*create_thread( int fd
, struct process
*process
)
154 struct thread
*thread
;
156 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
158 init_thread_structure( thread
);
160 thread
->process
= (struct process
*)grab_object( process
);
161 if (!current
) current
= thread
;
163 if (!booting_thread
) /* first thread ever */
165 booting_thread
= thread
;
166 lock_master_socket(1);
169 if ((thread
->next
= first_thread
) != NULL
) thread
->next
->prev
= thread
;
170 first_thread
= thread
;
172 if (!(thread
->id
= alloc_ptid( thread
)))
174 release_object( thread
);
177 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
)))
179 release_object( thread
);
183 thread
->token
= (struct token
*) grab_object( process
->token
);
185 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
186 add_process_thread( thread
->process
, thread
);
190 /* handle a client event */
191 static void thread_poll_event( struct fd
*fd
, int event
)
193 struct thread
*thread
= get_fd_user( fd
);
194 assert( thread
->obj
.ops
== &thread_ops
);
196 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
197 else if (event
& POLLIN
) read_request( thread
);
198 else if (event
& POLLOUT
) write_reply( thread
);
201 /* cleanup everything that is no longer needed by a dead thread */
202 /* used by destroy_thread and kill_thread */
203 static void cleanup_thread( struct thread
*thread
)
206 struct thread_apc
*apc
;
208 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
209 if (thread
->req_data
) free( thread
->req_data
);
210 if (thread
->reply_data
) free( thread
->reply_data
);
211 if (thread
->request_fd
) release_object( thread
->request_fd
);
212 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
213 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
214 free_msg_queue( thread
);
215 cleanup_clipboard_thread(thread
);
216 destroy_thread_windows( thread
);
217 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
219 if (thread
->inflight
[i
].client
!= -1)
221 close( thread
->inflight
[i
].server
);
222 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
225 thread
->req_data
= NULL
;
226 thread
->reply_data
= NULL
;
227 thread
->request_fd
= NULL
;
228 thread
->reply_fd
= NULL
;
229 thread
->wait_fd
= NULL
;
231 if (thread
== booting_thread
) /* killing booting thread */
233 booting_thread
= NULL
;
234 lock_master_socket(0);
238 /* destroy a thread when its refcount is 0 */
239 static void destroy_thread( struct object
*obj
)
241 struct thread_apc
*apc
;
242 struct thread
*thread
= (struct thread
*)obj
;
243 assert( obj
->ops
== &thread_ops
);
245 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
246 if (thread
->next
) thread
->next
->prev
= thread
->prev
;
247 if (thread
->prev
) thread
->prev
->next
= thread
->next
;
248 else first_thread
= thread
->next
;
249 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
250 cleanup_thread( thread
);
251 release_object( thread
->process
);
252 if (thread
->id
) free_ptid( thread
->id
);
253 if (thread
->token
) release_object( thread
->token
);
256 /* dump a thread on stdout for debugging purposes */
257 static void dump_thread( struct object
*obj
, int verbose
)
259 struct thread
*thread
= (struct thread
*)obj
;
260 assert( obj
->ops
== &thread_ops
);
262 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
263 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->teb
, thread
->state
);
266 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
268 struct thread
*mythread
= (struct thread
*)obj
;
269 return (mythread
->state
== TERMINATED
);
272 /* get a thread pointer from a thread id (and increment the refcount) */
273 struct thread
*get_thread_from_id( thread_id_t id
)
275 struct object
*obj
= get_ptid_entry( id
);
277 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
278 set_win32_error( ERROR_INVALID_THREAD_ID
);
282 /* get a thread from a handle (and increment the refcount) */
283 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
285 return (struct thread
*)get_handle_obj( current
->process
, handle
,
286 access
, &thread_ops
);
289 /* find a thread from a Unix pid */
290 struct thread
*get_thread_from_pid( int pid
)
294 for (t
= first_thread
; t
; t
= t
->next
) if (t
->unix_tid
== pid
) return t
;
295 for (t
= first_thread
; t
; t
= t
->next
) if (t
->unix_pid
== pid
) return t
;
299 /* set all information about a thread */
300 static void set_thread_info( struct thread
*thread
,
301 const struct set_thread_info_request
*req
)
303 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
304 thread
->priority
= req
->priority
;
305 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
307 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
308 else thread
->affinity
= req
->affinity
;
312 /* stop a thread (at the Unix level) */
313 void stop_thread( struct thread
*thread
)
315 /* can't stop a thread while initialisation is in progress */
316 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
319 /* suspend a thread */
320 static int suspend_thread( struct thread
*thread
)
322 int old_count
= thread
->suspend
;
323 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
325 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
327 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
331 /* resume a thread */
332 static int resume_thread( struct thread
*thread
)
334 int old_count
= thread
->suspend
;
335 if (thread
->suspend
> 0)
337 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
342 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
343 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
347 entry
->prev
= obj
->tail
;
349 if (obj
->tail
) obj
->tail
->next
= entry
;
350 else obj
->head
= entry
;
355 /* remove a thread from an object wait queue */
356 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
358 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
359 else obj
->tail
= entry
->prev
;
360 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
361 else obj
->head
= entry
->next
;
362 release_object( obj
);
366 static void end_wait( struct thread
*thread
)
368 struct thread_wait
*wait
= thread
->wait
;
369 struct wait_queue_entry
*entry
;
373 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
374 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
375 if (wait
->user
) remove_timeout_user( wait
->user
);
376 thread
->wait
= wait
->next
;
380 /* build the thread wait structure */
381 static int wait_on( int count
, struct object
*objects
[], int flags
, const abs_time_t
*timeout
)
383 struct thread_wait
*wait
;
384 struct wait_queue_entry
*entry
;
387 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
388 wait
->next
= current
->wait
;
389 wait
->thread
= current
;
393 current
->wait
= wait
;
394 if (flags
& SELECT_TIMEOUT
)
396 wait
->timeout
.tv_sec
= timeout
->sec
;
397 wait
->timeout
.tv_usec
= timeout
->usec
;
400 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
402 struct object
*obj
= objects
[i
];
403 entry
->thread
= current
;
404 if (!obj
->ops
->add_queue( obj
, entry
))
414 /* check if the thread waiting condition is satisfied */
415 static int check_wait( struct thread
*thread
)
418 struct thread_wait
*wait
= thread
->wait
;
419 struct wait_queue_entry
*entry
= wait
->queues
;
421 /* Suspended threads may not acquire locks */
422 if( thread
->process
->suspend
+ thread
->suspend
> 0 ) return -1;
425 if (wait
->flags
& SELECT_ALL
)
428 /* Note: we must check them all anyway, as some objects may
429 * want to do something when signaled, even if others are not */
430 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
431 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
432 if (not_ok
) goto other_checks
;
433 /* Wait satisfied: tell it to all objects */
435 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
436 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
437 signaled
= STATUS_ABANDONED_WAIT_0
;
442 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
444 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
445 /* Wait satisfied: tell it to the object */
447 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
448 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
454 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && thread
->system_apc
.head
) return STATUS_USER_APC
;
455 if ((wait
->flags
& SELECT_ALERTABLE
) && thread
->user_apc
.head
) return STATUS_USER_APC
;
456 if (wait
->flags
& SELECT_TIMEOUT
)
459 gettimeofday( &now
, NULL
);
460 if (!time_before( &now
, &wait
->timeout
)) return STATUS_TIMEOUT
;
465 /* send the wakeup signal to a thread */
466 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
468 struct wake_up_reply reply
;
471 reply
.cookie
= cookie
;
472 reply
.signaled
= signaled
;
473 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
476 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
477 else if (errno
== EPIPE
)
478 kill_thread( thread
, 0 ); /* normal death */
480 fatal_protocol_perror( thread
, "write" );
484 /* attempt to wake up a thread */
485 /* return >0 if OK, 0 if the wait condition is still not satisfied */
486 int wake_thread( struct thread
*thread
)
491 for (count
= 0; thread
->wait
; count
++)
493 if ((signaled
= check_wait( thread
)) == -1) break;
495 cookie
= thread
->wait
->cookie
;
496 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
497 thread
->id
, signaled
, cookie
);
499 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
505 /* thread wait timeout */
506 static void thread_timeout( void *ptr
)
508 struct thread_wait
*wait
= ptr
;
509 struct thread
*thread
= wait
->thread
;
510 void *cookie
= wait
->cookie
;
513 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
514 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
516 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
517 thread
->id
, STATUS_TIMEOUT
, cookie
);
519 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
520 /* check if other objects have become signaled in the meantime */
521 wake_thread( thread
);
524 /* select on a list of handles */
525 static void select_on( int count
, void *cookie
, const obj_handle_t
*handles
,
526 int flags
, const abs_time_t
*timeout
)
529 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
531 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
533 set_error( STATUS_INVALID_PARAMETER
);
536 for (i
= 0; i
< count
; i
++)
538 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
542 if (i
< count
) goto done
;
543 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
545 if ((ret
= check_wait( current
)) != -1)
547 /* condition is already satisfied */
553 /* now we need to wait */
554 if (flags
& SELECT_TIMEOUT
)
556 if (!(current
->wait
->user
= add_timeout_user( ¤t
->wait
->timeout
,
557 thread_timeout
, current
->wait
)))
563 current
->wait
->cookie
= cookie
;
564 set_error( STATUS_PENDING
);
567 while (--i
>= 0) release_object( objects
[i
] );
570 /* attempt to wake threads sleeping on the object wait queue */
571 void wake_up( struct object
*obj
, int max
)
573 struct wait_queue_entry
*entry
= obj
->head
;
577 struct thread
*thread
= entry
->thread
;
579 if (wake_thread( thread
))
581 if (max
&& !--max
) break;
586 /* queue an async procedure call */
587 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, void *func
,
588 enum apc_type type
, int system
, void *arg1
, void *arg2
, void *arg3
)
590 struct thread_apc
*apc
;
591 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
593 /* cancel a possible previous APC with the same owner */
594 if (owner
) thread_cancel_apc( thread
, owner
, system
);
595 if (thread
->state
== TERMINATED
) return 0;
597 if (!(apc
= mem_alloc( sizeof(*apc
) ))) return 0;
598 apc
->prev
= queue
->tail
;
607 if (!apc
->prev
) /* first one */
610 wake_thread( thread
);
612 else apc
->prev
->next
= apc
;
617 /* cancel the async procedure call owned by a specific object */
618 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, int system
)
620 struct thread_apc
*apc
;
621 struct apc_queue
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
622 for (apc
= queue
->head
; apc
; apc
= apc
->next
)
624 if (apc
->owner
!= owner
) continue;
625 if (apc
->next
) apc
->next
->prev
= apc
->prev
;
626 else queue
->tail
= apc
->prev
;
627 if (apc
->prev
) apc
->prev
->next
= apc
->next
;
628 else queue
->head
= apc
->next
;
634 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
635 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
637 struct thread_apc
*apc
;
638 struct apc_queue
*queue
= &thread
->system_apc
;
640 if (!queue
->head
&& !system_only
) queue
= &thread
->user_apc
;
641 if ((apc
= queue
->head
))
643 if (apc
->next
) apc
->next
->prev
= NULL
;
644 else queue
->tail
= NULL
;
645 queue
->head
= apc
->next
;
650 /* add an fd to the inflight list */
651 /* return list index, or -1 on error */
652 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
656 if (server
== -1) return -1;
663 /* first check if we already have an entry for this fd */
664 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
665 if (thread
->inflight
[i
].client
== client
)
667 close( thread
->inflight
[i
].server
);
668 thread
->inflight
[i
].server
= server
;
672 /* now find a free spot to store it */
673 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
674 if (thread
->inflight
[i
].client
== -1)
676 thread
->inflight
[i
].client
= client
;
677 thread
->inflight
[i
].server
= server
;
683 /* get an inflight fd and purge it from the list */
684 /* the fd must be closed when no longer used */
685 int thread_get_inflight_fd( struct thread
*thread
, int client
)
689 if (client
== -1) return -1;
693 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
695 if (thread
->inflight
[i
].client
== client
)
697 ret
= thread
->inflight
[i
].server
;
698 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
702 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
706 /* retrieve an LDT selector entry */
707 static void get_selector_entry( struct thread
*thread
, int entry
,
708 unsigned int *base
, unsigned int *limit
,
709 unsigned char *flags
)
711 if (!thread
->process
->ldt_copy
)
713 set_error( STATUS_ACCESS_DENIED
);
718 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
721 if (suspend_for_ptrace( thread
))
723 unsigned char flags_buf
[4];
724 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
725 if (read_thread_int( thread
, addr
, base
) == -1) goto done
;
726 if (read_thread_int( thread
, addr
+ 8192, limit
) == -1) goto done
;
727 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
728 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
729 *flags
= flags_buf
[entry
& 3];
731 resume_after_ptrace( thread
);
735 /* kill a thread on the spot */
736 void kill_thread( struct thread
*thread
, int violent_death
)
738 if (thread
->state
== TERMINATED
) return; /* already killed */
739 thread
->state
= TERMINATED
;
740 thread
->exit_time
= time(NULL
);
741 if (current
== thread
) current
= NULL
;
743 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
744 thread
->id
, thread
->exit_code
);
747 while (thread
->wait
) end_wait( thread
);
748 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
749 /* if it is waiting on the socket, we don't need to send a SIGTERM */
752 kill_console_processes( thread
, 0 );
753 debug_exit_thread( thread
);
754 abandon_mutexes( thread
);
755 remove_process_thread( thread
->process
, thread
);
756 wake_up( &thread
->obj
, 0 );
757 detach_thread( thread
, violent_death
? SIGTERM
: 0 );
758 cleanup_thread( thread
);
759 release_object( thread
);
762 /* take a snapshot of currently running threads */
763 struct thread_snapshot
*thread_snap( int *count
)
765 struct thread_snapshot
*snapshot
, *ptr
;
766 struct thread
*thread
;
769 for (thread
= first_thread
; thread
; thread
= thread
->next
)
770 if (thread
->state
!= TERMINATED
) total
++;
771 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
773 for (thread
= first_thread
; thread
; thread
= thread
->next
)
775 if (thread
->state
== TERMINATED
) continue;
776 ptr
->thread
= thread
;
777 ptr
->count
= thread
->obj
.refcount
;
778 ptr
->priority
= thread
->priority
;
779 grab_object( thread
);
786 /* signal that we are finished booting on the client side */
787 DECL_HANDLER(boot_done
)
789 debug_level
= max( debug_level
, req
->debug_level
);
790 if (current
== booting_thread
)
792 booting_thread
= (struct thread
*)~0UL; /* make sure it doesn't match other threads */
793 lock_master_socket(0); /* allow other clients now */
797 /* create a new thread */
798 DECL_HANDLER(new_thread
)
800 struct thread
*thread
;
801 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
803 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
805 if (request_fd
!= -1) close( request_fd
);
806 set_error( STATUS_INVALID_HANDLE
);
810 if ((thread
= create_thread( request_fd
, current
->process
)))
812 if (req
->suspend
) thread
->suspend
++;
813 reply
->tid
= get_thread_id( thread
);
814 if ((reply
->handle
= alloc_handle( current
->process
, thread
,
815 THREAD_ALL_ACCESS
, req
->inherit
)))
817 /* thread object will be released when the thread gets killed */
820 kill_thread( thread
, 1 );
824 /* initialize a new thread */
825 DECL_HANDLER(init_thread
)
827 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
828 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
830 if (current
->unix_pid
!= -1)
832 fatal_protocol_error( current
, "init_thread: already running\n" );
835 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1)
837 fatal_protocol_error( current
, "bad reply fd\n" );
842 fatal_protocol_error( current
, "bad wait fd\n" );
845 if (!(current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
)))
848 fatal_protocol_error( current
, "could not allocate reply fd\n" );
851 if (!(current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
)))
854 current
->unix_pid
= req
->unix_pid
;
855 current
->unix_tid
= req
->unix_tid
;
856 current
->teb
= req
->teb
;
858 if (current
->suspend
+ current
->process
->suspend
> 0) stop_thread( current
);
859 if (current
->process
->running_threads
> 1)
860 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
862 reply
->pid
= get_process_id( current
->process
);
863 reply
->tid
= get_thread_id( current
);
864 reply
->boot
= (current
== booting_thread
);
865 reply
->version
= SERVER_PROTOCOL_VERSION
;
869 if (reply_fd
!= -1) close( reply_fd
);
870 if (wait_fd
!= -1) close( wait_fd
);
873 /* terminate a thread */
874 DECL_HANDLER(terminate_thread
)
876 struct thread
*thread
;
880 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
882 thread
->exit_code
= req
->exit_code
;
883 if (thread
!= current
) kill_thread( thread
, 1 );
887 reply
->last
= (thread
->process
->running_threads
== 1);
889 release_object( thread
);
893 /* open a handle to a thread */
894 DECL_HANDLER(open_thread
)
896 struct thread
*thread
= get_thread_from_id( req
->tid
);
901 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->inherit
);
902 release_object( thread
);
906 /* fetch information about a thread */
907 DECL_HANDLER(get_thread_info
)
909 struct thread
*thread
;
910 obj_handle_t handle
= req
->handle
;
912 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
913 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
917 reply
->pid
= get_process_id( thread
->process
);
918 reply
->tid
= get_thread_id( thread
);
919 reply
->teb
= thread
->teb
;
920 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STILL_ACTIVE
;
921 reply
->priority
= thread
->priority
;
922 reply
->affinity
= thread
->affinity
;
923 reply
->creation_time
= thread
->creation_time
;
924 reply
->exit_time
= thread
->exit_time
;
926 release_object( thread
);
930 /* set information about a thread */
931 DECL_HANDLER(set_thread_info
)
933 struct thread
*thread
;
935 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
937 set_thread_info( thread
, req
);
938 release_object( thread
);
942 /* suspend a thread */
943 DECL_HANDLER(suspend_thread
)
945 struct thread
*thread
;
947 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
949 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
950 else reply
->count
= suspend_thread( thread
);
951 release_object( thread
);
955 /* resume a thread */
956 DECL_HANDLER(resume_thread
)
958 struct thread
*thread
;
960 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
962 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
963 else reply
->count
= resume_thread( thread
);
964 release_object( thread
);
968 /* select on a handle list */
971 int count
= get_req_data_size() / sizeof(int);
972 select_on( count
, req
->cookie
, get_req_data(), req
->flags
, &req
->timeout
);
975 /* queue an APC for a thread */
976 DECL_HANDLER(queue_apc
)
978 struct thread
*thread
;
979 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
981 thread_queue_apc( thread
, NULL
, req
->func
, APC_USER
, !req
->user
,
982 req
->arg1
, req
->arg2
, req
->arg3
);
983 release_object( thread
);
987 /* get next APC to call */
988 DECL_HANDLER(get_apc
)
990 struct thread_apc
*apc
;
994 if (!(apc
= thread_dequeue_apc( current
, !req
->alertable
)))
998 reply
->type
= APC_NONE
;
1001 /* Optimization: ignore APCs that have a NULL func; they are only used
1002 * to wake up a thread, but since we got here the thread woke up already.
1003 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1005 if (apc
->func
|| apc
->type
== APC_ASYNC_IO
) break;
1008 reply
->func
= apc
->func
;
1009 reply
->type
= apc
->type
;
1010 reply
->arg1
= apc
->arg1
;
1011 reply
->arg2
= apc
->arg2
;
1012 reply
->arg3
= apc
->arg3
;
1016 /* fetch a selector entry for a thread */
1017 DECL_HANDLER(get_selector_entry
)
1019 struct thread
*thread
;
1020 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1022 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1023 release_object( thread
);