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>
54 struct thread_wait
*next
; /* next wait structure for this thread */
55 struct thread
*thread
; /* owner thread */
56 int count
; /* count of objects */
58 void *cookie
; /* magic cookie to return to client */
59 struct timeval timeout
;
60 struct timeout_user
*user
;
61 struct wait_queue_entry queues
[1];
64 /* asynchronous procedure calls */
68 struct list entry
; /* queue linked list */
69 struct object
*owner
; /* object that queued this apc */
70 void *func
; /* function to call in client */
71 enum apc_type type
; /* type of apc function */
72 int nb_args
; /* number of arguments */
73 void *arg1
; /* function arguments */
79 /* thread operations */
81 static void dump_thread( struct object
*obj
, int verbose
);
82 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
83 static void thread_poll_event( struct fd
*fd
, int event
);
84 static void destroy_thread( struct object
*obj
);
85 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
);
87 static const struct object_ops thread_ops
=
89 sizeof(struct thread
), /* size */
90 dump_thread
, /* dump */
91 add_queue
, /* add_queue */
92 remove_queue
, /* remove_queue */
93 thread_signaled
, /* signaled */
94 no_satisfied
, /* satisfied */
95 no_signal
, /* signal */
96 no_get_fd
, /* get_fd */
97 destroy_thread
/* destroy */
100 static const struct fd_ops thread_fd_ops
=
102 NULL
, /* get_poll_events */
103 thread_poll_event
, /* poll_event */
104 no_flush
, /* flush */
105 no_get_file_info
, /* get_file_info */
106 no_queue_async
, /* queue_async */
107 no_cancel_async
/* cancel_async */
110 static struct list thread_list
= LIST_INIT(thread_list
);
111 static struct thread
*booting_thread
;
113 /* initialize the structure for a newly allocated thread */
114 inline static void init_thread_structure( struct thread
*thread
)
118 thread
->unix_pid
= -1; /* not known yet */
119 thread
->unix_tid
= -1; /* not known yet */
120 thread
->context
= NULL
;
122 thread
->debug_ctx
= NULL
;
123 thread
->debug_event
= NULL
;
124 thread
->queue
= NULL
;
127 thread
->req_data
= NULL
;
128 thread
->req_toread
= 0;
129 thread
->reply_data
= NULL
;
130 thread
->reply_towrite
= 0;
131 thread
->request_fd
= NULL
;
132 thread
->reply_fd
= NULL
;
133 thread
->wait_fd
= NULL
;
134 thread
->state
= RUNNING
;
135 thread
->attached
= 0;
136 thread
->exit_code
= 0;
137 thread
->priority
= THREAD_PRIORITY_NORMAL
;
138 thread
->affinity
= 1;
140 thread
->creation_time
= time(NULL
);
141 thread
->exit_time
= 0;
143 list_init( &thread
->mutex_list
);
144 list_init( &thread
->system_apc
);
145 list_init( &thread
->user_apc
);
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 thread
->desktop
= process
->desktop
;
162 if (!current
) current
= thread
;
164 if (!booting_thread
) /* first thread ever */
166 booting_thread
= thread
;
167 lock_master_socket(1);
170 list_add_head( &thread_list
, &thread
->entry
);
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 close_thread_desktop( thread
);
218 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
220 if (thread
->inflight
[i
].client
!= -1)
222 close( thread
->inflight
[i
].server
);
223 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
226 thread
->req_data
= NULL
;
227 thread
->reply_data
= NULL
;
228 thread
->request_fd
= NULL
;
229 thread
->reply_fd
= NULL
;
230 thread
->wait_fd
= NULL
;
233 if (thread
== booting_thread
) /* killing booting thread */
235 booting_thread
= NULL
;
236 lock_master_socket(0);
240 /* destroy a thread when its refcount is 0 */
241 static void destroy_thread( struct object
*obj
)
243 struct thread_apc
*apc
;
244 struct thread
*thread
= (struct thread
*)obj
;
245 assert( obj
->ops
== &thread_ops
);
247 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
248 list_remove( &thread
->entry
);
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
)
292 struct thread
*thread
;
294 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
296 if (thread
->unix_tid
== pid
) return thread
;
298 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
300 if (thread
->unix_pid
== pid
) return thread
;
305 /* set all information about a thread */
306 static void set_thread_info( struct thread
*thread
,
307 const struct set_thread_info_request
*req
)
309 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
310 thread
->priority
= req
->priority
;
311 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
313 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
314 else thread
->affinity
= req
->affinity
;
318 /* stop a thread (at the Unix level) */
319 void stop_thread( struct thread
*thread
)
321 /* can't stop a thread while initialisation is in progress */
322 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
325 /* suspend a thread */
326 static int suspend_thread( struct thread
*thread
)
328 int old_count
= thread
->suspend
;
329 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
331 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
333 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
337 /* resume a thread */
338 static int resume_thread( struct thread
*thread
)
340 int old_count
= thread
->suspend
;
341 if (thread
->suspend
> 0)
343 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
348 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
349 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
353 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
357 /* remove a thread from an object wait queue */
358 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
360 list_remove( &entry
->entry
);
361 release_object( obj
);
365 static void end_wait( struct thread
*thread
)
367 struct thread_wait
*wait
= thread
->wait
;
368 struct wait_queue_entry
*entry
;
372 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
373 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
374 if (wait
->user
) remove_timeout_user( wait
->user
);
375 thread
->wait
= wait
->next
;
379 /* build the thread wait structure */
380 static int wait_on( int count
, struct object
*objects
[], int flags
, const abs_time_t
*timeout
)
382 struct thread_wait
*wait
;
383 struct wait_queue_entry
*entry
;
386 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
387 wait
->next
= current
->wait
;
388 wait
->thread
= current
;
392 current
->wait
= wait
;
393 if (flags
& SELECT_TIMEOUT
)
395 wait
->timeout
.tv_sec
= timeout
->sec
;
396 wait
->timeout
.tv_usec
= timeout
->usec
;
399 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
401 struct object
*obj
= objects
[i
];
402 entry
->thread
= current
;
403 if (!obj
->ops
->add_queue( obj
, entry
))
413 /* check if the thread waiting condition is satisfied */
414 static int check_wait( struct thread
*thread
)
417 struct thread_wait
*wait
= thread
->wait
;
418 struct wait_queue_entry
*entry
= wait
->queues
;
420 /* Suspended threads may not acquire locks */
421 if( thread
->process
->suspend
+ thread
->suspend
> 0 ) return -1;
424 if (wait
->flags
& SELECT_ALL
)
427 /* Note: we must check them all anyway, as some objects may
428 * want to do something when signaled, even if others are not */
429 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
430 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
431 if (not_ok
) goto other_checks
;
432 /* Wait satisfied: tell it to all objects */
434 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
435 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
436 signaled
= STATUS_ABANDONED_WAIT_0
;
441 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
443 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
444 /* Wait satisfied: tell it to the object */
446 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
447 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
453 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty(&thread
->system_apc
)) return STATUS_USER_APC
;
454 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
455 if (wait
->flags
& SELECT_TIMEOUT
)
458 gettimeofday( &now
, NULL
);
459 if (!time_before( &now
, &wait
->timeout
)) return STATUS_TIMEOUT
;
464 /* send the wakeup signal to a thread */
465 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
467 struct wake_up_reply reply
;
470 reply
.cookie
= cookie
;
471 reply
.signaled
= signaled
;
472 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
475 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
476 else if (errno
== EPIPE
)
477 kill_thread( thread
, 0 ); /* normal death */
479 fatal_protocol_perror( thread
, "write" );
483 /* attempt to wake up a thread */
484 /* return >0 if OK, 0 if the wait condition is still not satisfied */
485 int wake_thread( struct thread
*thread
)
490 for (count
= 0; thread
->wait
; count
++)
492 if ((signaled
= check_wait( thread
)) == -1) break;
494 cookie
= thread
->wait
->cookie
;
495 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
496 thread
->id
, signaled
, cookie
);
498 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
504 /* thread wait timeout */
505 static void thread_timeout( void *ptr
)
507 struct thread_wait
*wait
= ptr
;
508 struct thread
*thread
= wait
->thread
;
509 void *cookie
= wait
->cookie
;
512 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
513 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
515 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
516 thread
->id
, STATUS_TIMEOUT
, cookie
);
518 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
519 /* check if other objects have become signaled in the meantime */
520 wake_thread( thread
);
523 /* try signaling an event flag, a semaphore or a mutex */
524 static int signal_object( obj_handle_t handle
)
529 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
532 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
533 release_object( obj
);
538 /* select on a list of handles */
539 static void select_on( int count
, void *cookie
, const obj_handle_t
*handles
,
540 int flags
, const abs_time_t
*timeout
, obj_handle_t signal_obj
)
543 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
545 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
547 set_error( STATUS_INVALID_PARAMETER
);
550 for (i
= 0; i
< count
; i
++)
552 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
556 if (i
< count
) goto done
;
557 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
559 /* signal the object */
562 if (!signal_object( signal_obj
))
567 /* check if we woke ourselves up */
568 if (!current
->wait
) goto done
;
571 if ((ret
= check_wait( current
)) != -1)
573 /* condition is already satisfied */
579 /* now we need to wait */
580 if (flags
& SELECT_TIMEOUT
)
582 if (!(current
->wait
->user
= add_timeout_user( ¤t
->wait
->timeout
,
583 thread_timeout
, current
->wait
)))
589 current
->wait
->cookie
= cookie
;
590 set_error( STATUS_PENDING
);
593 while (--i
>= 0) release_object( objects
[i
] );
596 /* attempt to wake threads sleeping on the object wait queue */
597 void wake_up( struct object
*obj
, int max
)
599 struct list
*ptr
, *next
;
601 LIST_FOR_EACH_SAFE( ptr
, next
, &obj
->wait_queue
)
603 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
604 if (wake_thread( entry
->thread
))
606 if (max
&& !--max
) break;
611 /* queue an async procedure call */
612 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, void *func
,
613 enum apc_type type
, int system
, void *arg1
, void *arg2
, void *arg3
)
615 struct thread_apc
*apc
;
616 struct list
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
618 /* cancel a possible previous APC with the same owner */
619 if (owner
) thread_cancel_apc( thread
, owner
, system
);
620 if (thread
->state
== TERMINATED
) return 0;
622 if (!(apc
= mem_alloc( sizeof(*apc
) ))) return 0;
629 list_add_tail( queue
, &apc
->entry
);
630 if (!list_prev( queue
, &apc
->entry
)) /* first one */
631 wake_thread( thread
);
636 /* cancel the async procedure call owned by a specific object */
637 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, int system
)
639 struct thread_apc
*apc
;
640 struct list
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
641 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
643 if (apc
->owner
!= owner
) continue;
644 list_remove( &apc
->entry
);
650 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
651 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
653 struct thread_apc
*apc
= NULL
;
654 struct list
*ptr
= list_head( &thread
->system_apc
);
656 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
659 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
665 /* add an fd to the inflight list */
666 /* return list index, or -1 on error */
667 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
671 if (server
== -1) return -1;
678 /* first check if we already have an entry for this fd */
679 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
680 if (thread
->inflight
[i
].client
== client
)
682 close( thread
->inflight
[i
].server
);
683 thread
->inflight
[i
].server
= server
;
687 /* now find a free spot to store it */
688 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
689 if (thread
->inflight
[i
].client
== -1)
691 thread
->inflight
[i
].client
= client
;
692 thread
->inflight
[i
].server
= server
;
698 /* get an inflight fd and purge it from the list */
699 /* the fd must be closed when no longer used */
700 int thread_get_inflight_fd( struct thread
*thread
, int client
)
704 if (client
== -1) return -1;
708 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
710 if (thread
->inflight
[i
].client
== client
)
712 ret
= thread
->inflight
[i
].server
;
713 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
717 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
721 /* retrieve an LDT selector entry */
722 static void get_selector_entry( struct thread
*thread
, int entry
,
723 unsigned int *base
, unsigned int *limit
,
724 unsigned char *flags
)
726 if (!thread
->process
->ldt_copy
)
728 set_error( STATUS_ACCESS_DENIED
);
733 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
736 if (suspend_for_ptrace( thread
))
738 unsigned char flags_buf
[4];
739 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
740 if (read_thread_int( thread
, addr
, base
) == -1) goto done
;
741 if (read_thread_int( thread
, addr
+ 8192, limit
) == -1) goto done
;
742 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
743 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
744 *flags
= flags_buf
[entry
& 3];
746 resume_after_ptrace( thread
);
750 /* kill a thread on the spot */
751 void kill_thread( struct thread
*thread
, int violent_death
)
753 if (thread
->state
== TERMINATED
) return; /* already killed */
754 thread
->state
= TERMINATED
;
755 thread
->exit_time
= time(NULL
);
756 if (current
== thread
) current
= NULL
;
758 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
759 thread
->id
, thread
->exit_code
);
762 while (thread
->wait
) end_wait( thread
);
763 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
764 /* if it is waiting on the socket, we don't need to send a SIGTERM */
767 kill_console_processes( thread
, 0 );
768 debug_exit_thread( thread
);
769 abandon_mutexes( thread
);
770 remove_process_thread( thread
->process
, thread
);
771 wake_up( &thread
->obj
, 0 );
772 detach_thread( thread
, violent_death
? SIGTERM
: 0 );
773 cleanup_thread( thread
);
774 release_object( thread
);
777 /* take a snapshot of currently running threads */
778 struct thread_snapshot
*thread_snap( int *count
)
780 struct thread_snapshot
*snapshot
, *ptr
;
781 struct thread
*thread
;
784 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
785 if (thread
->state
!= TERMINATED
) total
++;
786 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
788 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
790 if (thread
->state
== TERMINATED
) continue;
791 ptr
->thread
= thread
;
792 ptr
->count
= thread
->obj
.refcount
;
793 ptr
->priority
= thread
->priority
;
794 grab_object( thread
);
801 /* gets the current impersonation token */
802 struct token
*thread_get_impersonation_token( struct thread
*thread
)
805 return thread
->token
;
807 return thread
->process
->token
;
810 /* signal that we are finished booting on the client side */
811 DECL_HANDLER(boot_done
)
813 debug_level
= max( debug_level
, req
->debug_level
);
814 if (current
== booting_thread
)
816 booting_thread
= (struct thread
*)~0UL; /* make sure it doesn't match other threads */
817 lock_master_socket(0); /* allow other clients now */
821 /* create a new thread */
822 DECL_HANDLER(new_thread
)
824 struct thread
*thread
;
825 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
827 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
829 if (request_fd
!= -1) close( request_fd
);
830 set_error( STATUS_INVALID_HANDLE
);
834 if ((thread
= create_thread( request_fd
, current
->process
)))
836 if (req
->suspend
) thread
->suspend
++;
837 reply
->tid
= get_thread_id( thread
);
838 if ((reply
->handle
= alloc_handle( current
->process
, thread
,
839 THREAD_ALL_ACCESS
, req
->inherit
)))
841 /* thread object will be released when the thread gets killed */
844 kill_thread( thread
, 1 );
848 /* initialize a new thread */
849 DECL_HANDLER(init_thread
)
851 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
852 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
854 if (current
->unix_pid
!= -1)
856 fatal_protocol_error( current
, "init_thread: already running\n" );
859 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1)
861 fatal_protocol_error( current
, "bad reply fd\n" );
866 fatal_protocol_error( current
, "bad wait fd\n" );
869 if (!(current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
)))
872 fatal_protocol_error( current
, "could not allocate reply fd\n" );
875 if (!(current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
)))
878 current
->unix_pid
= req
->unix_pid
;
879 current
->unix_tid
= req
->unix_tid
;
880 current
->teb
= req
->teb
;
882 if (current
->suspend
+ current
->process
->suspend
> 0) stop_thread( current
);
883 if (current
->process
->running_threads
> 1)
884 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
886 reply
->pid
= get_process_id( current
->process
);
887 reply
->tid
= get_thread_id( current
);
888 reply
->boot
= (current
== booting_thread
);
889 reply
->version
= SERVER_PROTOCOL_VERSION
;
893 if (reply_fd
!= -1) close( reply_fd
);
894 if (wait_fd
!= -1) close( wait_fd
);
897 /* terminate a thread */
898 DECL_HANDLER(terminate_thread
)
900 struct thread
*thread
;
904 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
906 thread
->exit_code
= req
->exit_code
;
907 if (thread
!= current
) kill_thread( thread
, 1 );
911 reply
->last
= (thread
->process
->running_threads
== 1);
913 release_object( thread
);
917 /* open a handle to a thread */
918 DECL_HANDLER(open_thread
)
920 struct thread
*thread
= get_thread_from_id( req
->tid
);
925 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->inherit
);
926 release_object( thread
);
930 /* fetch information about a thread */
931 DECL_HANDLER(get_thread_info
)
933 struct thread
*thread
;
934 obj_handle_t handle
= req
->handle
;
936 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
937 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
941 reply
->pid
= get_process_id( thread
->process
);
942 reply
->tid
= get_thread_id( thread
);
943 reply
->teb
= thread
->teb
;
944 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STILL_ACTIVE
;
945 reply
->priority
= thread
->priority
;
946 reply
->affinity
= thread
->affinity
;
947 reply
->creation_time
= thread
->creation_time
;
948 reply
->exit_time
= thread
->exit_time
;
950 release_object( thread
);
954 /* set information about a thread */
955 DECL_HANDLER(set_thread_info
)
957 struct thread
*thread
;
959 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
961 set_thread_info( thread
, req
);
962 release_object( thread
);
966 /* suspend a thread */
967 DECL_HANDLER(suspend_thread
)
969 struct thread
*thread
;
971 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
973 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
974 else reply
->count
= suspend_thread( thread
);
975 release_object( thread
);
979 /* resume a thread */
980 DECL_HANDLER(resume_thread
)
982 struct thread
*thread
;
984 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
986 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
987 else reply
->count
= resume_thread( thread
);
988 release_object( thread
);
992 /* select on a handle list */
995 int count
= get_req_data_size() / sizeof(int);
996 select_on( count
, req
->cookie
, get_req_data(), req
->flags
, &req
->timeout
, req
->signal
);
999 /* queue an APC for a thread */
1000 DECL_HANDLER(queue_apc
)
1002 struct thread
*thread
;
1003 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
1005 thread_queue_apc( thread
, NULL
, req
->func
, APC_USER
, !req
->user
,
1006 req
->arg1
, req
->arg2
, req
->arg3
);
1007 release_object( thread
);
1011 /* get next APC to call */
1012 DECL_HANDLER(get_apc
)
1014 struct thread_apc
*apc
;
1018 if (!(apc
= thread_dequeue_apc( current
, !req
->alertable
)))
1022 reply
->type
= APC_NONE
;
1025 /* Optimization: ignore APCs that have a NULL func; they are only used
1026 * to wake up a thread, but since we got here the thread woke up already.
1027 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1029 if (apc
->func
|| apc
->type
== APC_ASYNC_IO
) break;
1032 reply
->func
= apc
->func
;
1033 reply
->type
= apc
->type
;
1034 reply
->arg1
= apc
->arg1
;
1035 reply
->arg2
= apc
->arg2
;
1036 reply
->arg3
= apc
->arg3
;
1040 /* fetch a selector entry for a thread */
1041 DECL_HANDLER(get_selector_entry
)
1043 struct thread
*thread
;
1044 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1046 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1047 release_object( thread
);