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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 */
63 struct timeout_user
*user
;
64 struct wait_queue_entry queues
[1];
67 /* asynchronous procedure calls */
71 struct object obj
; /* object header */
72 struct list entry
; /* queue linked list */
73 struct thread
*caller
; /* thread that queued this apc */
74 struct object
*owner
; /* object that queued this apc */
75 int executed
; /* has it been executed by the client? */
76 apc_call_t call
; /* call arguments */
77 apc_result_t result
; /* call results once executed */
80 static void dump_thread_apc( struct object
*obj
, int verbose
);
81 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
);
82 static void thread_apc_destroy( struct object
*obj
);
83 static void clear_apc_queue( struct list
*queue
);
85 static const struct object_ops thread_apc_ops
=
87 sizeof(struct thread_apc
), /* size */
88 dump_thread_apc
, /* dump */
89 no_get_type
, /* get_type */
90 add_queue
, /* add_queue */
91 remove_queue
, /* remove_queue */
92 thread_apc_signaled
, /* signaled */
93 no_satisfied
, /* satisfied */
94 no_signal
, /* signal */
95 no_get_fd
, /* get_fd */
96 no_map_access
, /* map_access */
97 default_get_sd
, /* get_sd */
98 default_set_sd
, /* set_sd */
99 no_lookup_name
, /* lookup_name */
100 no_open_file
, /* open_file */
101 no_close_handle
, /* close_handle */
102 thread_apc_destroy
/* destroy */
106 /* thread operations */
108 static void dump_thread( struct object
*obj
, int verbose
);
109 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
110 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
111 static void thread_poll_event( struct fd
*fd
, int event
);
112 static void destroy_thread( struct object
*obj
);
114 static const struct object_ops thread_ops
=
116 sizeof(struct thread
), /* size */
117 dump_thread
, /* dump */
118 no_get_type
, /* get_type */
119 add_queue
, /* add_queue */
120 remove_queue
, /* remove_queue */
121 thread_signaled
, /* signaled */
122 no_satisfied
, /* satisfied */
123 no_signal
, /* signal */
124 no_get_fd
, /* get_fd */
125 thread_map_access
, /* map_access */
126 default_get_sd
, /* get_sd */
127 default_set_sd
, /* set_sd */
128 no_lookup_name
, /* lookup_name */
129 no_open_file
, /* open_file */
130 no_close_handle
, /* close_handle */
131 destroy_thread
/* destroy */
134 static const struct fd_ops thread_fd_ops
=
136 NULL
, /* get_poll_events */
137 thread_poll_event
, /* poll_event */
139 NULL
, /* get_fd_type */
141 NULL
, /* queue_async */
142 NULL
, /* reselect_async */
143 NULL
/* cancel_async */
146 static struct list thread_list
= LIST_INIT(thread_list
);
148 /* initialize the structure for a newly allocated thread */
149 static inline void init_thread_structure( struct thread
*thread
)
153 thread
->unix_pid
= -1; /* not known yet */
154 thread
->unix_tid
= -1; /* not known yet */
155 thread
->context
= NULL
;
156 thread
->suspend_context
= NULL
;
158 thread
->debug_ctx
= NULL
;
159 thread
->debug_event
= NULL
;
160 thread
->debug_break
= 0;
161 thread
->queue
= NULL
;
164 thread
->req_data
= NULL
;
165 thread
->req_toread
= 0;
166 thread
->reply_data
= NULL
;
167 thread
->reply_towrite
= 0;
168 thread
->request_fd
= NULL
;
169 thread
->reply_fd
= NULL
;
170 thread
->wait_fd
= NULL
;
171 thread
->state
= RUNNING
;
172 thread
->exit_code
= 0;
173 thread
->priority
= 0;
174 thread
->affinity
= 1;
176 thread
->desktop_users
= 0;
177 thread
->token
= NULL
;
179 thread
->creation_time
= current_time
;
180 thread
->exit_time
= 0;
182 list_init( &thread
->mutex_list
);
183 list_init( &thread
->system_apc
);
184 list_init( &thread
->user_apc
);
186 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
187 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
190 /* check if address looks valid for a client-side data structure (TEB etc.) */
191 static inline int is_valid_address( void *addr
)
193 return addr
&& !((unsigned long)addr
% sizeof(int));
196 /* create a new thread */
197 struct thread
*create_thread( int fd
, struct process
*process
)
199 struct thread
*thread
;
201 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
203 init_thread_structure( thread
);
205 thread
->process
= (struct process
*)grab_object( process
);
206 thread
->desktop
= process
->desktop
;
207 if (!current
) current
= thread
;
209 list_add_head( &thread_list
, &thread
->entry
);
211 if (!(thread
->id
= alloc_ptid( thread
)))
213 release_object( thread
);
216 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
218 release_object( thread
);
222 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
223 add_process_thread( thread
->process
, thread
);
227 /* handle a client event */
228 static void thread_poll_event( struct fd
*fd
, int event
)
230 struct thread
*thread
= get_fd_user( fd
);
231 assert( thread
->obj
.ops
== &thread_ops
);
233 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
234 else if (event
& POLLIN
) read_request( thread
);
235 else if (event
& POLLOUT
) write_reply( thread
);
238 /* cleanup everything that is no longer needed by a dead thread */
239 /* used by destroy_thread and kill_thread */
240 static void cleanup_thread( struct thread
*thread
)
244 clear_apc_queue( &thread
->system_apc
);
245 clear_apc_queue( &thread
->user_apc
);
246 free( thread
->req_data
);
247 free( thread
->reply_data
);
248 if (thread
->request_fd
) release_object( thread
->request_fd
);
249 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
250 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
251 free( thread
->suspend_context
);
252 free_msg_queue( thread
);
253 cleanup_clipboard_thread(thread
);
254 destroy_thread_windows( thread
);
255 close_thread_desktop( thread
);
256 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
258 if (thread
->inflight
[i
].client
!= -1)
260 close( thread
->inflight
[i
].server
);
261 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
264 thread
->req_data
= NULL
;
265 thread
->reply_data
= NULL
;
266 thread
->request_fd
= NULL
;
267 thread
->reply_fd
= NULL
;
268 thread
->wait_fd
= NULL
;
269 thread
->context
= NULL
;
270 thread
->suspend_context
= NULL
;
274 /* destroy a thread when its refcount is 0 */
275 static void destroy_thread( struct object
*obj
)
277 struct thread
*thread
= (struct thread
*)obj
;
278 assert( obj
->ops
== &thread_ops
);
280 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
281 list_remove( &thread
->entry
);
282 cleanup_thread( thread
);
283 release_object( thread
->process
);
284 if (thread
->id
) free_ptid( thread
->id
);
285 if (thread
->token
) release_object( thread
->token
);
288 /* dump a thread on stdout for debugging purposes */
289 static void dump_thread( struct object
*obj
, int verbose
)
291 struct thread
*thread
= (struct thread
*)obj
;
292 assert( obj
->ops
== &thread_ops
);
294 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
295 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->teb
, thread
->state
);
298 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
300 struct thread
*mythread
= (struct thread
*)obj
;
301 return (mythread
->state
== TERMINATED
);
304 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
306 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
307 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
308 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
309 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
310 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
313 static void dump_thread_apc( struct object
*obj
, int verbose
)
315 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
316 assert( obj
->ops
== &thread_apc_ops
);
318 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
321 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
)
323 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
324 return apc
->executed
;
327 static void thread_apc_destroy( struct object
*obj
)
329 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
330 if (apc
->caller
) release_object( apc
->caller
);
331 if (apc
->owner
) release_object( apc
->owner
);
334 /* queue an async procedure call */
335 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
337 struct thread_apc
*apc
;
339 if ((apc
= alloc_object( &thread_apc_ops
)))
341 apc
->call
= *call_data
;
345 apc
->result
.type
= APC_NONE
;
346 if (owner
) grab_object( owner
);
351 /* get a thread pointer from a thread id (and increment the refcount) */
352 struct thread
*get_thread_from_id( thread_id_t id
)
354 struct object
*obj
= get_ptid_entry( id
);
356 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
357 set_error( STATUS_INVALID_CID
);
361 /* get a thread from a handle (and increment the refcount) */
362 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
364 return (struct thread
*)get_handle_obj( current
->process
, handle
,
365 access
, &thread_ops
);
368 /* find a thread from a Unix tid */
369 struct thread
*get_thread_from_tid( int tid
)
371 struct thread
*thread
;
373 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
375 if (thread
->unix_tid
== tid
) return thread
;
380 /* find a thread from a Unix pid */
381 struct thread
*get_thread_from_pid( int pid
)
383 struct thread
*thread
;
385 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
387 if (thread
->unix_pid
== pid
) return thread
;
392 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
393 #define THREAD_PRIORITY_REALTIME_LOWEST -7
395 /* set all information about a thread */
396 static void set_thread_info( struct thread
*thread
,
397 const struct set_thread_info_request
*req
)
399 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
401 int max
= THREAD_PRIORITY_HIGHEST
;
402 int min
= THREAD_PRIORITY_LOWEST
;
403 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
405 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
406 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
408 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
409 req
->priority
== THREAD_PRIORITY_IDLE
||
410 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
411 thread
->priority
= req
->priority
;
413 set_error( STATUS_INVALID_PARAMETER
);
415 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
417 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
418 else thread
->affinity
= req
->affinity
;
420 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
421 security_set_thread_token( thread
, req
->token
);
424 /* stop a thread (at the Unix level) */
425 void stop_thread( struct thread
*thread
)
427 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
428 /* can't stop a thread while initialisation is in progress */
429 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
432 /* suspend a thread */
433 static int suspend_thread( struct thread
*thread
)
435 int old_count
= thread
->suspend
;
436 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
438 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
440 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
444 /* resume a thread */
445 static int resume_thread( struct thread
*thread
)
447 int old_count
= thread
->suspend
;
448 if (thread
->suspend
> 0)
450 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
455 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
456 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
460 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
464 /* remove a thread from an object wait queue */
465 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
467 list_remove( &entry
->entry
);
468 release_object( obj
);
472 static void end_wait( struct thread
*thread
)
474 struct thread_wait
*wait
= thread
->wait
;
475 struct wait_queue_entry
*entry
;
479 thread
->wait
= wait
->next
;
480 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
481 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
482 if (wait
->user
) remove_timeout_user( wait
->user
);
486 /* build the thread wait structure */
487 static int wait_on( unsigned int count
, struct object
*objects
[], int flags
, timeout_t timeout
)
489 struct thread_wait
*wait
;
490 struct wait_queue_entry
*entry
;
493 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
494 wait
->next
= current
->wait
;
495 wait
->thread
= current
;
499 wait
->timeout
= timeout
;
500 current
->wait
= wait
;
502 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
504 struct object
*obj
= objects
[i
];
505 entry
->thread
= current
;
506 if (!obj
->ops
->add_queue( obj
, entry
))
516 /* check if the thread waiting condition is satisfied */
517 static int check_wait( struct thread
*thread
)
520 struct thread_wait
*wait
= thread
->wait
;
521 struct wait_queue_entry
*entry
= wait
->queues
;
525 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
526 return STATUS_USER_APC
;
528 /* Suspended threads may not acquire locks, but they can run system APCs */
529 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
531 if (wait
->flags
& SELECT_ALL
)
534 /* Note: we must check them all anyway, as some objects may
535 * want to do something when signaled, even if others are not */
536 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
537 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
538 if (not_ok
) goto other_checks
;
539 /* Wait satisfied: tell it to all objects */
541 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
542 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
543 signaled
= STATUS_ABANDONED_WAIT_0
;
548 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
550 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
551 /* Wait satisfied: tell it to the object */
553 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
554 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
560 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
561 if (wait
->timeout
<= current_time
) return STATUS_TIMEOUT
;
565 /* send the wakeup signal to a thread */
566 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
568 struct wake_up_reply reply
;
571 reply
.cookie
= cookie
;
572 reply
.signaled
= signaled
;
573 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
576 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
577 else if (errno
== EPIPE
)
578 kill_thread( thread
, 0 ); /* normal death */
580 fatal_protocol_perror( thread
, "write" );
584 /* attempt to wake up a thread */
585 /* return >0 if OK, 0 if the wait condition is still not satisfied */
586 int wake_thread( struct thread
*thread
)
591 for (count
= 0; thread
->wait
; count
++)
593 if ((signaled
= check_wait( thread
)) == -1) break;
595 cookie
= thread
->wait
->cookie
;
596 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
597 thread
->id
, signaled
, cookie
);
599 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
605 /* thread wait timeout */
606 static void thread_timeout( void *ptr
)
608 struct thread_wait
*wait
= ptr
;
609 struct thread
*thread
= wait
->thread
;
610 void *cookie
= wait
->cookie
;
613 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
614 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
616 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
617 thread
->id
, (int)STATUS_TIMEOUT
, cookie
);
619 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
620 /* check if other objects have become signaled in the meantime */
621 wake_thread( thread
);
624 /* try signaling an event flag, a semaphore or a mutex */
625 static int signal_object( obj_handle_t handle
)
630 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
633 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
634 release_object( obj
);
639 /* select on a list of handles */
640 static timeout_t
select_on( unsigned int count
, void *cookie
, const obj_handle_t
*handles
,
641 int flags
, timeout_t timeout
, obj_handle_t signal_obj
)
645 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
647 if (timeout
<= 0) timeout
= current_time
- timeout
;
649 if (count
> MAXIMUM_WAIT_OBJECTS
)
651 set_error( STATUS_INVALID_PARAMETER
);
654 for (i
= 0; i
< count
; i
++)
656 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
660 if (i
< count
) goto done
;
661 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
663 /* signal the object */
666 if (!signal_object( signal_obj
))
671 /* check if we woke ourselves up */
672 if (!current
->wait
) goto done
;
675 if ((ret
= check_wait( current
)) != -1)
677 /* condition is already satisfied */
683 /* now we need to wait */
684 if (current
->wait
->timeout
!= TIMEOUT_INFINITE
)
686 if (!(current
->wait
->user
= add_timeout_user( current
->wait
->timeout
,
687 thread_timeout
, current
->wait
)))
693 current
->wait
->cookie
= cookie
;
694 set_error( STATUS_PENDING
);
697 while (i
> 0) release_object( objects
[--i
] );
701 /* attempt to wake threads sleeping on the object wait queue */
702 void wake_up( struct object
*obj
, int max
)
704 struct list
*ptr
, *next
;
706 LIST_FOR_EACH_SAFE( ptr
, next
, &obj
->wait_queue
)
708 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
709 if (wake_thread( entry
->thread
))
711 if (max
&& !--max
) break;
716 /* return the apc queue to use for a given apc type */
717 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
724 return &thread
->user_apc
;
726 return &thread
->system_apc
;
730 /* check if thread is currently waiting for a (system) apc */
731 static inline int is_in_apc_wait( struct thread
*thread
)
733 return (thread
->process
->suspend
|| thread
->suspend
||
734 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
737 /* queue an existing APC to a given thread */
738 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
742 if (!thread
) /* find a suitable thread inside the process */
744 struct thread
*candidate
;
746 /* first try to find a waiting thread */
747 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
749 if (candidate
->state
== TERMINATED
) continue;
750 if (is_in_apc_wait( candidate
))
758 /* then use the first one that accepts a signal */
759 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
761 if (send_thread_signal( candidate
, SIGUSR1
))
768 if (!thread
) return 0; /* nothing found */
769 queue
= get_apc_queue( thread
, apc
->call
.type
);
773 if (thread
->state
== TERMINATED
) return 0;
774 queue
= get_apc_queue( thread
, apc
->call
.type
);
775 /* send signal for system APCs if needed */
776 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
778 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
780 /* cancel a possible previous APC with the same owner */
781 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
785 list_add_tail( queue
, &apc
->entry
);
786 if (!list_prev( queue
, &apc
->entry
)) /* first one */
787 wake_thread( thread
);
792 /* queue an async procedure call */
793 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
795 struct thread_apc
*apc
;
798 if ((apc
= create_apc( owner
, call_data
)))
800 ret
= queue_apc( NULL
, thread
, apc
);
801 release_object( apc
);
806 /* cancel the async procedure call owned by a specific object */
807 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
809 struct thread_apc
*apc
;
810 struct list
*queue
= get_apc_queue( thread
, type
);
812 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
814 if (apc
->owner
!= owner
) continue;
815 list_remove( &apc
->entry
);
817 wake_up( &apc
->obj
, 0 );
818 release_object( apc
);
823 /* remove the head apc from the queue; the returned object must be released by the caller */
824 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
826 struct thread_apc
*apc
= NULL
;
827 struct list
*ptr
= list_head( &thread
->system_apc
);
829 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
832 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
838 /* clear an APC queue, cancelling all the APCs on it */
839 static void clear_apc_queue( struct list
*queue
)
843 while ((ptr
= list_head( queue
)))
845 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
846 list_remove( &apc
->entry
);
848 wake_up( &apc
->obj
, 0 );
849 release_object( apc
);
853 /* add an fd to the inflight list */
854 /* return list index, or -1 on error */
855 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
859 if (server
== -1) return -1;
866 /* first check if we already have an entry for this fd */
867 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
868 if (thread
->inflight
[i
].client
== client
)
870 close( thread
->inflight
[i
].server
);
871 thread
->inflight
[i
].server
= server
;
875 /* now find a free spot to store it */
876 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
877 if (thread
->inflight
[i
].client
== -1)
879 thread
->inflight
[i
].client
= client
;
880 thread
->inflight
[i
].server
= server
;
886 /* get an inflight fd and purge it from the list */
887 /* the fd must be closed when no longer used */
888 int thread_get_inflight_fd( struct thread
*thread
, int client
)
892 if (client
== -1) return -1;
896 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
898 if (thread
->inflight
[i
].client
== client
)
900 ret
= thread
->inflight
[i
].server
;
901 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
905 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
909 /* kill a thread on the spot */
910 void kill_thread( struct thread
*thread
, int violent_death
)
912 if (thread
->state
== TERMINATED
) return; /* already killed */
913 thread
->state
= TERMINATED
;
914 thread
->exit_time
= current_time
;
915 if (current
== thread
) current
= NULL
;
917 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
918 thread
->id
, thread
->exit_code
);
921 while (thread
->wait
) end_wait( thread
);
922 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
923 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
926 kill_console_processes( thread
, 0 );
927 debug_exit_thread( thread
);
928 abandon_mutexes( thread
);
929 wake_up( &thread
->obj
, 0 );
930 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
931 cleanup_thread( thread
);
932 remove_process_thread( thread
->process
, thread
);
933 release_object( thread
);
936 /* trigger a breakpoint event in a given thread */
937 void break_thread( struct thread
*thread
)
939 struct debug_event_exception data
;
941 assert( thread
->context
);
943 data
.record
.ExceptionCode
= STATUS_BREAKPOINT
;
944 data
.record
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
945 data
.record
.ExceptionRecord
= NULL
;
946 data
.record
.ExceptionAddress
= get_context_ip( thread
->context
);
947 data
.record
.NumberParameters
= 0;
949 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
950 thread
->debug_break
= 0;
953 /* take a snapshot of currently running threads */
954 struct thread_snapshot
*thread_snap( int *count
)
956 struct thread_snapshot
*snapshot
, *ptr
;
957 struct thread
*thread
;
960 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
961 if (thread
->state
!= TERMINATED
) total
++;
962 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
964 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
966 if (thread
->state
== TERMINATED
) continue;
967 ptr
->thread
= thread
;
968 ptr
->count
= thread
->obj
.refcount
;
969 ptr
->priority
= thread
->priority
;
970 grab_object( thread
);
977 /* gets the current impersonation token */
978 struct token
*thread_get_impersonation_token( struct thread
*thread
)
981 return thread
->token
;
983 return thread
->process
->token
;
986 /* create a new thread */
987 DECL_HANDLER(new_thread
)
989 struct thread
*thread
;
990 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
992 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
994 if (request_fd
!= -1) close( request_fd
);
995 set_error( STATUS_INVALID_HANDLE
);
999 if ((thread
= create_thread( request_fd
, current
->process
)))
1001 if (req
->suspend
) thread
->suspend
++;
1002 reply
->tid
= get_thread_id( thread
);
1003 if ((reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
)))
1005 /* thread object will be released when the thread gets killed */
1008 kill_thread( thread
, 1 );
1012 /* initialize a new thread */
1013 DECL_HANDLER(init_thread
)
1015 struct process
*process
= current
->process
;
1016 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
1017 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
1019 if (current
->reply_fd
) /* already initialised */
1021 set_error( STATUS_INVALID_PARAMETER
);
1025 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1027 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1029 if (!current
->reply_fd
) goto error
;
1033 set_error( STATUS_TOO_MANY_OPENED_FILES
); /* most likely reason */
1036 if (!(current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 )))
1039 if (!is_valid_address(req
->teb
) || !is_valid_address(req
->peb
) || !is_valid_address(req
->ldt_copy
))
1041 set_error( STATUS_INVALID_PARAMETER
);
1045 current
->unix_pid
= req
->unix_pid
;
1046 current
->unix_tid
= req
->unix_tid
;
1047 current
->teb
= req
->teb
;
1049 if (!process
->peb
) /* first thread, initialize the process too */
1051 process
->unix_pid
= current
->unix_pid
;
1052 process
->peb
= req
->peb
;
1053 process
->ldt_copy
= req
->ldt_copy
;
1054 reply
->info_size
= init_process( current
);
1058 if (process
->unix_pid
!= current
->unix_pid
)
1059 process
->unix_pid
= -1; /* can happen with linuxthreads */
1060 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
1061 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
1063 debug_level
= max( debug_level
, req
->debug_level
);
1065 reply
->pid
= get_process_id( process
);
1066 reply
->tid
= get_thread_id( current
);
1067 reply
->version
= SERVER_PROTOCOL_VERSION
;
1068 reply
->server_start
= server_start_time
;
1072 if (reply_fd
!= -1) close( reply_fd
);
1073 if (wait_fd
!= -1) close( wait_fd
);
1076 /* terminate a thread */
1077 DECL_HANDLER(terminate_thread
)
1079 struct thread
*thread
;
1083 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1085 thread
->exit_code
= req
->exit_code
;
1086 if (thread
!= current
) kill_thread( thread
, 1 );
1090 reply
->last
= (thread
->process
->running_threads
== 1);
1092 release_object( thread
);
1096 /* open a handle to a thread */
1097 DECL_HANDLER(open_thread
)
1099 struct thread
*thread
= get_thread_from_id( req
->tid
);
1104 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1105 release_object( thread
);
1109 /* fetch information about a thread */
1110 DECL_HANDLER(get_thread_info
)
1112 struct thread
*thread
;
1113 obj_handle_t handle
= req
->handle
;
1115 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1116 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
1120 reply
->pid
= get_process_id( thread
->process
);
1121 reply
->tid
= get_thread_id( thread
);
1122 reply
->teb
= thread
->teb
;
1123 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1124 reply
->priority
= thread
->priority
;
1125 reply
->affinity
= thread
->affinity
;
1126 reply
->creation_time
= thread
->creation_time
;
1127 reply
->exit_time
= thread
->exit_time
;
1128 reply
->last
= thread
->process
->running_threads
== 1;
1130 release_object( thread
);
1134 /* set information about a thread */
1135 DECL_HANDLER(set_thread_info
)
1137 struct thread
*thread
;
1139 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1141 set_thread_info( thread
, req
);
1142 release_object( thread
);
1146 /* suspend a thread */
1147 DECL_HANDLER(suspend_thread
)
1149 struct thread
*thread
;
1151 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1153 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1154 else reply
->count
= suspend_thread( thread
);
1155 release_object( thread
);
1159 /* resume a thread */
1160 DECL_HANDLER(resume_thread
)
1162 struct thread
*thread
;
1164 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1166 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1167 else reply
->count
= resume_thread( thread
);
1168 release_object( thread
);
1172 /* select on a handle list */
1173 DECL_HANDLER(select
)
1175 struct thread_apc
*apc
;
1177 const apc_result_t
*result
= get_req_data();
1178 const obj_handle_t
*handles
= (const obj_handle_t
*)(result
+ 1);
1180 if (get_req_data_size() < sizeof(*result
))
1182 set_error( STATUS_INVALID_PARAMETER
);
1185 count
= (get_req_data_size() - sizeof(*result
)) / sizeof(obj_handle_t
);
1187 /* first store results of previous apc */
1190 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1191 0, &thread_apc_ops
))) return;
1192 apc
->result
= *result
;
1194 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1196 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1197 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1198 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1199 apc
->result
.create_thread
.handle
= handle
;
1200 clear_error(); /* ignore errors from the above calls */
1202 else if (apc
->result
.type
== APC_ASYNC_IO
)
1204 if (apc
->owner
) async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
1206 wake_up( &apc
->obj
, 0 );
1207 close_handle( current
->process
, req
->prev_apc
);
1208 release_object( apc
);
1211 reply
->timeout
= select_on( count
, req
->cookie
, handles
, req
->flags
, req
->timeout
, req
->signal
);
1213 if (get_error() == STATUS_USER_APC
)
1217 if (!(apc
= thread_dequeue_apc( current
, !(req
->flags
& SELECT_ALERTABLE
) )))
1219 /* Optimization: ignore APC_NONE calls, they are only used to
1220 * wake up a thread, but since we got here the thread woke up already.
1222 if (apc
->call
.type
!= APC_NONE
)
1224 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1225 reply
->call
= apc
->call
;
1226 release_object( apc
);
1230 wake_up( &apc
->obj
, 0 );
1231 release_object( apc
);
1236 /* queue an APC for a thread or process */
1237 DECL_HANDLER(queue_apc
)
1239 struct thread
*thread
= NULL
;
1240 struct process
*process
= NULL
;
1241 struct thread_apc
*apc
;
1243 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1245 switch (apc
->call
.type
)
1249 thread
= get_thread_from_handle( req
->thread
, THREAD_SET_CONTEXT
);
1251 case APC_VIRTUAL_ALLOC
:
1252 case APC_VIRTUAL_FREE
:
1253 case APC_VIRTUAL_PROTECT
:
1254 case APC_VIRTUAL_FLUSH
:
1255 case APC_VIRTUAL_LOCK
:
1256 case APC_VIRTUAL_UNLOCK
:
1257 case APC_UNMAP_VIEW
:
1258 process
= get_process_from_handle( req
->process
, PROCESS_VM_OPERATION
);
1260 case APC_VIRTUAL_QUERY
:
1261 process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
);
1264 process
= get_process_from_handle( req
->process
, PROCESS_VM_OPERATION
);
1265 if (process
&& process
!= current
->process
)
1267 /* duplicate the handle into the target process */
1268 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1269 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1270 if (handle
) apc
->call
.map_view
.handle
= handle
;
1273 release_object( process
);
1278 case APC_CREATE_THREAD
:
1279 process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
);
1282 set_error( STATUS_INVALID_PARAMETER
);
1288 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1289 release_object( thread
);
1293 reply
->self
= (process
== current
->process
);
1296 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1299 if (queue_apc( process
, NULL
, apc
))
1301 apc
->caller
= (struct thread
*)grab_object( current
);
1302 reply
->handle
= handle
;
1306 close_handle( current
->process
, handle
);
1307 set_error( STATUS_PROCESS_IS_TERMINATING
);
1311 release_object( process
);
1314 release_object( apc
);
1317 /* Get the result of an APC call */
1318 DECL_HANDLER(get_apc_result
)
1320 struct thread_apc
*apc
;
1322 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1323 0, &thread_apc_ops
))) return;
1324 if (!apc
->executed
) set_error( STATUS_PENDING
);
1327 reply
->result
= apc
->result
;
1328 /* close the handle directly to avoid an extra round-trip */
1329 close_handle( current
->process
, req
->handle
);
1331 release_object( apc
);
1334 /* retrieve the current context of a thread */
1335 DECL_HANDLER(get_thread_context
)
1337 struct thread
*thread
;
1340 if (get_reply_max_size() < sizeof(CONTEXT
))
1342 set_error( STATUS_INVALID_PARAMETER
);
1345 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1349 if (thread
!= current
|| !thread
->suspend_context
)
1351 /* not suspended, shouldn't happen */
1352 set_error( STATUS_INVALID_PARAMETER
);
1356 if (thread
->context
== thread
->suspend_context
) thread
->context
= NULL
;
1357 set_reply_data_ptr( thread
->suspend_context
, sizeof(CONTEXT
) );
1358 thread
->suspend_context
= NULL
;
1361 else if (thread
!= current
&& !thread
->context
)
1363 /* thread is not suspended, retry (if it's still running) */
1364 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1365 else set_error( STATUS_PENDING
);
1367 else if ((context
= set_reply_data_size( sizeof(CONTEXT
) )))
1369 unsigned int flags
= get_context_system_regs( req
->flags
);
1371 memset( context
, 0, sizeof(CONTEXT
) );
1372 context
->ContextFlags
= get_context_cpu_flag();
1373 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1374 if (flags
) get_thread_context( thread
, context
, flags
);
1376 reply
->self
= (thread
== current
);
1377 release_object( thread
);
1380 /* set the current context of a thread */
1381 DECL_HANDLER(set_thread_context
)
1383 struct thread
*thread
;
1385 if (get_req_data_size() < sizeof(CONTEXT
))
1387 set_error( STATUS_INVALID_PARAMETER
);
1390 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1394 if (thread
!= current
|| thread
->context
)
1396 /* nested suspend or exception, shouldn't happen */
1397 set_error( STATUS_INVALID_PARAMETER
);
1399 else if ((thread
->suspend_context
= mem_alloc( sizeof(CONTEXT
) )))
1401 memcpy( thread
->suspend_context
, get_req_data(), sizeof(CONTEXT
) );
1402 thread
->context
= thread
->suspend_context
;
1403 if (thread
->debug_break
) break_thread( thread
);
1406 else if (thread
!= current
&& !thread
->context
)
1408 /* thread is not suspended, retry (if it's still running) */
1409 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1410 else set_error( STATUS_PENDING
);
1414 const CONTEXT
*context
= get_req_data();
1415 unsigned int flags
= get_context_system_regs( req
->flags
);
1417 if (flags
) set_thread_context( thread
, context
, flags
);
1418 if (thread
->context
&& !get_error())
1419 copy_context( thread
->context
, context
, req
->flags
& ~flags
);
1421 reply
->self
= (thread
== current
);
1422 release_object( thread
);
1425 /* fetch a selector entry for a thread */
1426 DECL_HANDLER(get_selector_entry
)
1428 struct thread
*thread
;
1429 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1431 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1432 release_object( thread
);