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>
40 #define WIN32_NO_STATUS
57 struct thread_wait
*next
; /* next wait structure for this thread */
58 struct thread
*thread
; /* owner thread */
59 int count
; /* count of objects */
61 void *cookie
; /* magic cookie to return to client */
62 struct timeval timeout
;
63 struct timeout_user
*user
;
64 struct wait_queue_entry queues
[1];
67 /* asynchronous procedure calls */
71 struct list entry
; /* queue linked list */
72 struct object
*owner
; /* object that queued this apc */
73 void *func
; /* function to call in client */
74 enum apc_type type
; /* type of apc function */
75 int nb_args
; /* number of arguments */
76 void *arg1
; /* function arguments */
82 /* thread operations */
84 static void dump_thread( struct object
*obj
, int verbose
);
85 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
86 static void thread_poll_event( struct fd
*fd
, int event
);
87 static void destroy_thread( struct object
*obj
);
88 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
);
90 static const struct object_ops thread_ops
=
92 sizeof(struct thread
), /* size */
93 dump_thread
, /* dump */
94 add_queue
, /* add_queue */
95 remove_queue
, /* remove_queue */
96 thread_signaled
, /* signaled */
97 no_satisfied
, /* satisfied */
98 no_signal
, /* signal */
99 no_get_fd
, /* get_fd */
100 no_lookup_name
, /* lookup_name */
101 no_close_handle
, /* close_handle */
102 destroy_thread
/* destroy */
105 static const struct fd_ops thread_fd_ops
=
107 NULL
, /* get_poll_events */
108 thread_poll_event
, /* poll_event */
109 no_flush
, /* flush */
110 no_get_file_info
, /* get_file_info */
111 no_queue_async
, /* queue_async */
112 no_cancel_async
/* cancel_async */
115 static struct list thread_list
= LIST_INIT(thread_list
);
117 /* initialize the structure for a newly allocated thread */
118 inline static void init_thread_structure( struct thread
*thread
)
122 thread
->unix_pid
= -1; /* not known yet */
123 thread
->unix_tid
= -1; /* not known yet */
124 thread
->context
= NULL
;
125 thread
->suspend_context
= NULL
;
127 thread
->debug_ctx
= NULL
;
128 thread
->debug_event
= NULL
;
129 thread
->queue
= NULL
;
132 thread
->req_data
= NULL
;
133 thread
->req_toread
= 0;
134 thread
->reply_data
= NULL
;
135 thread
->reply_towrite
= 0;
136 thread
->request_fd
= NULL
;
137 thread
->reply_fd
= NULL
;
138 thread
->wait_fd
= NULL
;
139 thread
->state
= RUNNING
;
140 thread
->attached
= 0;
141 thread
->exit_code
= 0;
142 thread
->priority
= THREAD_PRIORITY_NORMAL
;
143 thread
->affinity
= 1;
145 thread
->creation_time
= time(NULL
);
146 thread
->exit_time
= 0;
147 thread
->desktop_users
= 0;
149 list_init( &thread
->mutex_list
);
150 list_init( &thread
->system_apc
);
151 list_init( &thread
->user_apc
);
153 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
154 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
157 /* check if address looks valid for a client-side data structure (TEB etc.) */
158 static inline int is_valid_address( void *addr
)
160 return addr
&& !((unsigned int)addr
% sizeof(int));
163 /* create a new thread */
164 struct thread
*create_thread( int fd
, struct process
*process
)
166 struct thread
*thread
;
168 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
170 init_thread_structure( thread
);
172 thread
->process
= (struct process
*)grab_object( process
);
173 thread
->desktop
= process
->desktop
;
174 if (!current
) current
= thread
;
176 list_add_head( &thread_list
, &thread
->entry
);
178 if (!(thread
->id
= alloc_ptid( thread
)))
180 release_object( thread
);
183 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
)))
185 release_object( thread
);
189 thread
->token
= (struct token
*) grab_object( process
->token
);
191 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
192 add_process_thread( thread
->process
, thread
);
196 /* handle a client event */
197 static void thread_poll_event( struct fd
*fd
, int event
)
199 struct thread
*thread
= get_fd_user( fd
);
200 assert( thread
->obj
.ops
== &thread_ops
);
202 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
203 else if (event
& POLLIN
) read_request( thread
);
204 else if (event
& POLLOUT
) write_reply( thread
);
207 /* cleanup everything that is no longer needed by a dead thread */
208 /* used by destroy_thread and kill_thread */
209 static void cleanup_thread( struct thread
*thread
)
212 struct thread_apc
*apc
;
214 while ((apc
= thread_dequeue_apc( thread
, 0 ))) free( apc
);
215 if (thread
->req_data
) free( thread
->req_data
);
216 if (thread
->reply_data
) free( thread
->reply_data
);
217 if (thread
->request_fd
) release_object( thread
->request_fd
);
218 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
219 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
220 if (thread
->suspend_context
) free( thread
->suspend_context
);
221 free_msg_queue( thread
);
222 cleanup_clipboard_thread(thread
);
223 destroy_thread_windows( thread
);
224 close_thread_desktop( thread
);
225 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
227 if (thread
->inflight
[i
].client
!= -1)
229 close( thread
->inflight
[i
].server
);
230 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
233 thread
->req_data
= NULL
;
234 thread
->reply_data
= NULL
;
235 thread
->request_fd
= NULL
;
236 thread
->reply_fd
= NULL
;
237 thread
->wait_fd
= NULL
;
238 thread
->context
= NULL
;
239 thread
->suspend_context
= NULL
;
243 /* destroy a thread when its refcount is 0 */
244 static void destroy_thread( struct object
*obj
)
246 struct thread
*thread
= (struct thread
*)obj
;
247 assert( obj
->ops
== &thread_ops
);
249 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
250 list_remove( &thread
->entry
);
251 cleanup_thread( thread
);
252 release_object( thread
->process
);
253 if (thread
->id
) free_ptid( thread
->id
);
254 if (thread
->token
) release_object( thread
->token
);
257 /* dump a thread on stdout for debugging purposes */
258 static void dump_thread( struct object
*obj
, int verbose
)
260 struct thread
*thread
= (struct thread
*)obj
;
261 assert( obj
->ops
== &thread_ops
);
263 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
264 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->teb
, thread
->state
);
267 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
269 struct thread
*mythread
= (struct thread
*)obj
;
270 return (mythread
->state
== TERMINATED
);
273 /* get a thread pointer from a thread id (and increment the refcount) */
274 struct thread
*get_thread_from_id( thread_id_t id
)
276 struct object
*obj
= get_ptid_entry( id
);
278 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
279 set_error( STATUS_INVALID_CID
);
283 /* get a thread from a handle (and increment the refcount) */
284 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
286 return (struct thread
*)get_handle_obj( current
->process
, handle
,
287 access
, &thread_ops
);
290 /* find a thread from a Unix pid */
291 struct thread
*get_thread_from_pid( int pid
)
293 struct thread
*thread
;
295 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
297 if (thread
->unix_tid
== pid
) return thread
;
299 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
301 if (thread
->unix_pid
== pid
) return thread
;
306 /* set all information about a thread */
307 static void set_thread_info( struct thread
*thread
,
308 const struct set_thread_info_request
*req
)
310 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
311 thread
->priority
= req
->priority
;
312 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
314 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
315 else thread
->affinity
= req
->affinity
;
317 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
318 security_set_thread_token( thread
, req
->token
);
321 /* stop a thread (at the Unix level) */
322 void stop_thread( struct thread
*thread
)
324 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
325 /* can't stop a thread while initialisation is in progress */
326 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
329 /* suspend a thread */
330 static int suspend_thread( struct thread
*thread
)
332 int old_count
= thread
->suspend
;
333 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
335 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
337 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
341 /* resume a thread */
342 static int resume_thread( struct thread
*thread
)
344 int old_count
= thread
->suspend
;
345 if (thread
->suspend
> 0)
347 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
352 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
353 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
357 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
361 /* remove a thread from an object wait queue */
362 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
364 list_remove( &entry
->entry
);
365 release_object( obj
);
369 static void end_wait( struct thread
*thread
)
371 struct thread_wait
*wait
= thread
->wait
;
372 struct wait_queue_entry
*entry
;
376 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
377 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
378 if (wait
->user
) remove_timeout_user( wait
->user
);
379 thread
->wait
= wait
->next
;
383 /* build the thread wait structure */
384 static int wait_on( int count
, struct object
*objects
[], int flags
, const abs_time_t
*timeout
)
386 struct thread_wait
*wait
;
387 struct wait_queue_entry
*entry
;
390 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
391 wait
->next
= current
->wait
;
392 wait
->thread
= current
;
396 current
->wait
= wait
;
397 if (flags
& SELECT_TIMEOUT
)
399 wait
->timeout
.tv_sec
= timeout
->sec
;
400 wait
->timeout
.tv_usec
= timeout
->usec
;
403 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
405 struct object
*obj
= objects
[i
];
406 entry
->thread
= current
;
407 if (!obj
->ops
->add_queue( obj
, entry
))
417 /* check if the thread waiting condition is satisfied */
418 static int check_wait( struct thread
*thread
)
421 struct thread_wait
*wait
= thread
->wait
;
422 struct wait_queue_entry
*entry
= wait
->queues
;
424 /* Suspended threads may not acquire locks */
425 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
428 if (wait
->flags
& SELECT_ALL
)
431 /* Note: we must check them all anyway, as some objects may
432 * want to do something when signaled, even if others are not */
433 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
434 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
435 if (not_ok
) goto other_checks
;
436 /* Wait satisfied: tell it to all objects */
438 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
439 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
440 signaled
= STATUS_ABANDONED_WAIT_0
;
445 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
447 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
448 /* Wait satisfied: tell it to the object */
450 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
451 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
457 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty(&thread
->system_apc
)) return STATUS_USER_APC
;
458 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
459 if (wait
->flags
& SELECT_TIMEOUT
)
462 gettimeofday( &now
, NULL
);
463 if (!time_before( &now
, &wait
->timeout
)) return STATUS_TIMEOUT
;
468 /* send the wakeup signal to a thread */
469 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
471 struct wake_up_reply reply
;
474 reply
.cookie
= cookie
;
475 reply
.signaled
= signaled
;
476 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
479 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
480 else if (errno
== EPIPE
)
481 kill_thread( thread
, 0 ); /* normal death */
483 fatal_protocol_perror( thread
, "write" );
487 /* attempt to wake up a thread */
488 /* return >0 if OK, 0 if the wait condition is still not satisfied */
489 int wake_thread( struct thread
*thread
)
494 for (count
= 0; thread
->wait
; count
++)
496 if ((signaled
= check_wait( thread
)) == -1) break;
498 cookie
= thread
->wait
->cookie
;
499 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
500 thread
->id
, signaled
, cookie
);
502 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
508 /* thread wait timeout */
509 static void thread_timeout( void *ptr
)
511 struct thread_wait
*wait
= ptr
;
512 struct thread
*thread
= wait
->thread
;
513 void *cookie
= wait
->cookie
;
516 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
517 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
519 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
520 thread
->id
, (int)STATUS_TIMEOUT
, cookie
);
522 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
523 /* check if other objects have become signaled in the meantime */
524 wake_thread( thread
);
527 /* try signaling an event flag, a semaphore or a mutex */
528 static int signal_object( obj_handle_t handle
)
533 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
536 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
537 release_object( obj
);
542 /* select on a list of handles */
543 static void select_on( int count
, void *cookie
, const obj_handle_t
*handles
,
544 int flags
, const abs_time_t
*timeout
, obj_handle_t signal_obj
)
547 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
549 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
551 set_error( STATUS_INVALID_PARAMETER
);
554 for (i
= 0; i
< count
; i
++)
556 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
560 if (i
< count
) goto done
;
561 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
563 /* signal the object */
566 if (!signal_object( signal_obj
))
571 /* check if we woke ourselves up */
572 if (!current
->wait
) goto done
;
575 if ((ret
= check_wait( current
)) != -1)
577 /* condition is already satisfied */
583 /* now we need to wait */
584 if (flags
& SELECT_TIMEOUT
)
586 if (!(current
->wait
->user
= add_timeout_user( ¤t
->wait
->timeout
,
587 thread_timeout
, current
->wait
)))
593 current
->wait
->cookie
= cookie
;
594 set_error( STATUS_PENDING
);
597 while (--i
>= 0) release_object( objects
[i
] );
600 /* attempt to wake threads sleeping on the object wait queue */
601 void wake_up( struct object
*obj
, int max
)
603 struct list
*ptr
, *next
;
605 LIST_FOR_EACH_SAFE( ptr
, next
, &obj
->wait_queue
)
607 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
608 if (wake_thread( entry
->thread
))
610 if (max
&& !--max
) break;
615 /* queue an async procedure call */
616 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, void *func
,
617 enum apc_type type
, int system
, void *arg1
, void *arg2
, void *arg3
)
619 struct thread_apc
*apc
;
620 struct list
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
622 /* cancel a possible previous APC with the same owner */
623 if (owner
) thread_cancel_apc( thread
, owner
, system
);
624 if (thread
->state
== TERMINATED
) return 0;
626 if (!(apc
= mem_alloc( sizeof(*apc
) ))) return 0;
633 list_add_tail( queue
, &apc
->entry
);
634 if (!list_prev( queue
, &apc
->entry
)) /* first one */
635 wake_thread( thread
);
640 /* cancel the async procedure call owned by a specific object */
641 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, int system
)
643 struct thread_apc
*apc
;
644 struct list
*queue
= system
? &thread
->system_apc
: &thread
->user_apc
;
645 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
647 if (apc
->owner
!= owner
) continue;
648 list_remove( &apc
->entry
);
654 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
655 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
657 struct thread_apc
*apc
= NULL
;
658 struct list
*ptr
= list_head( &thread
->system_apc
);
660 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
663 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
669 /* add an fd to the inflight list */
670 /* return list index, or -1 on error */
671 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
675 if (server
== -1) return -1;
682 /* first check if we already have an entry for this fd */
683 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
684 if (thread
->inflight
[i
].client
== client
)
686 close( thread
->inflight
[i
].server
);
687 thread
->inflight
[i
].server
= server
;
691 /* now find a free spot to store it */
692 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
693 if (thread
->inflight
[i
].client
== -1)
695 thread
->inflight
[i
].client
= client
;
696 thread
->inflight
[i
].server
= server
;
702 /* get an inflight fd and purge it from the list */
703 /* the fd must be closed when no longer used */
704 int thread_get_inflight_fd( struct thread
*thread
, int client
)
708 if (client
== -1) return -1;
712 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
714 if (thread
->inflight
[i
].client
== client
)
716 ret
= thread
->inflight
[i
].server
;
717 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
721 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
725 /* retrieve an LDT selector entry */
726 static void get_selector_entry( struct thread
*thread
, int entry
,
727 unsigned int *base
, unsigned int *limit
,
728 unsigned char *flags
)
730 if (!thread
->process
->ldt_copy
)
732 set_error( STATUS_ACCESS_DENIED
);
737 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
740 if (suspend_for_ptrace( thread
))
742 unsigned char flags_buf
[4];
743 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
744 if (read_thread_int( thread
, addr
, (int *)base
) == -1) goto done
;
745 if (read_thread_int( thread
, addr
+ 8192, (int *)limit
) == -1) goto done
;
746 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
747 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
748 *flags
= flags_buf
[entry
& 3];
750 resume_after_ptrace( thread
);
754 /* kill a thread on the spot */
755 void kill_thread( struct thread
*thread
, int violent_death
)
757 if (thread
->state
== TERMINATED
) return; /* already killed */
758 thread
->state
= TERMINATED
;
759 thread
->exit_time
= time(NULL
);
760 if (current
== thread
) current
= NULL
;
762 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
763 thread
->id
, thread
->exit_code
);
766 while (thread
->wait
) end_wait( thread
);
767 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
768 /* if it is waiting on the socket, we don't need to send a SIGTERM */
771 kill_console_processes( thread
, 0 );
772 debug_exit_thread( thread
);
773 abandon_mutexes( thread
);
774 wake_up( &thread
->obj
, 0 );
775 if (violent_death
) send_thread_signal( thread
, SIGTERM
);
776 cleanup_thread( thread
);
777 remove_process_thread( thread
->process
, thread
);
778 release_object( thread
);
781 /* take a snapshot of currently running threads */
782 struct thread_snapshot
*thread_snap( int *count
)
784 struct thread_snapshot
*snapshot
, *ptr
;
785 struct thread
*thread
;
788 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
789 if (thread
->state
!= TERMINATED
) total
++;
790 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
792 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
794 if (thread
->state
== TERMINATED
) continue;
795 ptr
->thread
= thread
;
796 ptr
->count
= thread
->obj
.refcount
;
797 ptr
->priority
= thread
->priority
;
798 grab_object( thread
);
805 /* gets the current impersonation token */
806 struct token
*thread_get_impersonation_token( struct thread
*thread
)
809 return thread
->token
;
811 return thread
->process
->token
;
814 /* create a new thread */
815 DECL_HANDLER(new_thread
)
817 struct thread
*thread
;
818 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
820 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
822 if (request_fd
!= -1) close( request_fd
);
823 set_error( STATUS_INVALID_HANDLE
);
827 if ((thread
= create_thread( request_fd
, current
->process
)))
829 if (req
->suspend
) thread
->suspend
++;
830 reply
->tid
= get_thread_id( thread
);
831 if ((reply
->handle
= alloc_handle( current
->process
, thread
,
832 THREAD_ALL_ACCESS
, req
->inherit
)))
834 /* thread object will be released when the thread gets killed */
837 kill_thread( thread
, 1 );
841 /* initialize a new thread */
842 DECL_HANDLER(init_thread
)
844 struct process
*process
= current
->process
;
845 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
846 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
848 if (current
->unix_pid
!= -1)
850 fatal_protocol_error( current
, "init_thread: already running\n" );
853 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1)
855 fatal_protocol_error( current
, "bad reply fd\n" );
860 fatal_protocol_error( current
, "bad wait fd\n" );
863 if (!(current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
)))
866 fatal_protocol_error( current
, "could not allocate reply fd\n" );
869 if (!(current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
)))
872 if (!is_valid_address(req
->teb
) || !is_valid_address(req
->peb
) || !is_valid_address(req
->ldt_copy
))
874 set_error( STATUS_INVALID_PARAMETER
);
878 current
->unix_pid
= req
->unix_pid
;
879 current
->unix_tid
= req
->unix_tid
;
880 current
->teb
= req
->teb
;
882 if (!process
->peb
) /* first thread, initialize the process too */
884 process
->peb
= req
->peb
;
885 process
->ldt_copy
= req
->ldt_copy
;
886 reply
->info_size
= init_process( current
);
890 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
891 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
893 debug_level
= max( debug_level
, req
->debug_level
);
895 reply
->pid
= get_process_id( process
);
896 reply
->tid
= get_thread_id( current
);
897 reply
->version
= SERVER_PROTOCOL_VERSION
;
898 reply
->server_start
= server_start_time
;
902 if (reply_fd
!= -1) close( reply_fd
);
903 if (wait_fd
!= -1) close( wait_fd
);
906 /* terminate a thread */
907 DECL_HANDLER(terminate_thread
)
909 struct thread
*thread
;
913 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
915 thread
->exit_code
= req
->exit_code
;
916 if (thread
!= current
) kill_thread( thread
, 1 );
920 reply
->last
= (thread
->process
->running_threads
== 1);
922 release_object( thread
);
926 /* open a handle to a thread */
927 DECL_HANDLER(open_thread
)
929 struct thread
*thread
= get_thread_from_id( req
->tid
);
934 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->inherit
);
935 release_object( thread
);
939 /* fetch information about a thread */
940 DECL_HANDLER(get_thread_info
)
942 struct thread
*thread
;
943 obj_handle_t handle
= req
->handle
;
945 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
946 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
950 reply
->pid
= get_process_id( thread
->process
);
951 reply
->tid
= get_thread_id( thread
);
952 reply
->teb
= thread
->teb
;
953 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STILL_ACTIVE
;
954 reply
->priority
= thread
->priority
;
955 reply
->affinity
= thread
->affinity
;
956 reply
->creation_time
= thread
->creation_time
;
957 reply
->exit_time
= thread
->exit_time
;
959 release_object( thread
);
963 /* set information about a thread */
964 DECL_HANDLER(set_thread_info
)
966 struct thread
*thread
;
968 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
970 set_thread_info( thread
, req
);
971 release_object( thread
);
975 /* suspend a thread */
976 DECL_HANDLER(suspend_thread
)
978 struct thread
*thread
;
980 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
982 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
983 else reply
->count
= suspend_thread( thread
);
984 release_object( thread
);
988 /* resume a thread */
989 DECL_HANDLER(resume_thread
)
991 struct thread
*thread
;
993 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
995 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
996 else reply
->count
= resume_thread( thread
);
997 release_object( thread
);
1001 /* select on a handle list */
1002 DECL_HANDLER(select
)
1004 int count
= get_req_data_size() / sizeof(int);
1005 select_on( count
, req
->cookie
, get_req_data(), req
->flags
, &req
->timeout
, req
->signal
);
1008 /* queue an APC for a thread */
1009 DECL_HANDLER(queue_apc
)
1011 struct thread
*thread
;
1012 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
1014 thread_queue_apc( thread
, NULL
, req
->func
, APC_USER
, !req
->user
,
1015 req
->arg1
, req
->arg2
, req
->arg3
);
1016 release_object( thread
);
1020 /* get next APC to call */
1021 DECL_HANDLER(get_apc
)
1023 struct thread_apc
*apc
;
1027 if (!(apc
= thread_dequeue_apc( current
, !req
->alertable
)))
1031 reply
->type
= APC_NONE
;
1034 /* Optimization: ignore APCs that have a NULL func; they are only used
1035 * to wake up a thread, but since we got here the thread woke up already.
1036 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1038 if (apc
->func
|| apc
->type
== APC_ASYNC_IO
) break;
1041 reply
->func
= apc
->func
;
1042 reply
->type
= apc
->type
;
1043 reply
->arg1
= apc
->arg1
;
1044 reply
->arg2
= apc
->arg2
;
1045 reply
->arg3
= apc
->arg3
;
1049 /* retrieve the current context of a thread */
1050 DECL_HANDLER(get_thread_context
)
1052 struct thread
*thread
;
1055 if (get_reply_max_size() < sizeof(CONTEXT
))
1057 set_error( STATUS_INVALID_PARAMETER
);
1060 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1064 if (thread
!= current
|| !thread
->suspend_context
)
1066 /* not suspended, shouldn't happen */
1067 set_error( STATUS_INVALID_PARAMETER
);
1071 if (thread
->context
== thread
->suspend_context
) thread
->context
= NULL
;
1072 set_reply_data_ptr( thread
->suspend_context
, sizeof(CONTEXT
) );
1073 thread
->suspend_context
= NULL
;
1076 else if (thread
!= current
&& !thread
->context
)
1078 /* thread is not suspended, retry (if it's still running) */
1079 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1080 else set_error( STATUS_PENDING
);
1082 else if ((data
= set_reply_data_size( sizeof(CONTEXT
) )))
1084 memset( data
, 0, sizeof(CONTEXT
) );
1085 get_thread_context( thread
, data
, req
->flags
);
1087 release_object( thread
);
1090 /* set the current context of a thread */
1091 DECL_HANDLER(set_thread_context
)
1093 struct thread
*thread
;
1095 if (get_req_data_size() < sizeof(CONTEXT
))
1097 set_error( STATUS_INVALID_PARAMETER
);
1100 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1104 if (thread
!= current
|| thread
->context
)
1106 /* nested suspend or exception, shouldn't happen */
1107 set_error( STATUS_INVALID_PARAMETER
);
1109 else if ((thread
->suspend_context
= mem_alloc( sizeof(CONTEXT
) )))
1111 memcpy( thread
->suspend_context
, get_req_data(), sizeof(CONTEXT
) );
1112 thread
->context
= thread
->suspend_context
;
1115 else if (thread
!= current
&& !thread
->context
)
1117 /* thread is not suspended, retry (if it's still running) */
1118 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1119 else set_error( STATUS_PENDING
);
1123 set_thread_context( thread
, get_req_data(), req
->flags
);
1125 release_object( thread
);
1128 /* fetch a selector entry for a thread */
1129 DECL_HANDLER(get_selector_entry
)
1131 struct thread
*thread
;
1132 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1134 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1135 release_object( thread
);