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
->queue
= NULL
;
163 thread
->req_data
= NULL
;
164 thread
->req_toread
= 0;
165 thread
->reply_data
= NULL
;
166 thread
->reply_towrite
= 0;
167 thread
->request_fd
= NULL
;
168 thread
->reply_fd
= NULL
;
169 thread
->wait_fd
= NULL
;
170 thread
->state
= RUNNING
;
171 thread
->exit_code
= 0;
172 thread
->priority
= 0;
173 thread
->affinity
= 1;
175 thread
->desktop_users
= 0;
176 thread
->token
= NULL
;
178 thread
->creation_time
= current_time
;
179 thread
->exit_time
= 0;
181 list_init( &thread
->mutex_list
);
182 list_init( &thread
->system_apc
);
183 list_init( &thread
->user_apc
);
185 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
186 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
189 /* check if address looks valid for a client-side data structure (TEB etc.) */
190 static inline int is_valid_address( void *addr
)
192 return addr
&& !((unsigned long)addr
% sizeof(int));
195 /* create a new thread */
196 struct thread
*create_thread( int fd
, struct process
*process
)
198 struct thread
*thread
;
200 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
202 init_thread_structure( thread
);
204 thread
->process
= (struct process
*)grab_object( process
);
205 thread
->desktop
= process
->desktop
;
206 if (!current
) current
= thread
;
208 list_add_head( &thread_list
, &thread
->entry
);
210 if (!(thread
->id
= alloc_ptid( thread
)))
212 release_object( thread
);
215 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
217 release_object( thread
);
221 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
222 add_process_thread( thread
->process
, thread
);
226 /* handle a client event */
227 static void thread_poll_event( struct fd
*fd
, int event
)
229 struct thread
*thread
= get_fd_user( fd
);
230 assert( thread
->obj
.ops
== &thread_ops
);
232 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
233 else if (event
& POLLIN
) read_request( thread
);
234 else if (event
& POLLOUT
) write_reply( thread
);
237 /* cleanup everything that is no longer needed by a dead thread */
238 /* used by destroy_thread and kill_thread */
239 static void cleanup_thread( struct thread
*thread
)
243 clear_apc_queue( &thread
->system_apc
);
244 clear_apc_queue( &thread
->user_apc
);
245 free( thread
->req_data
);
246 free( thread
->reply_data
);
247 if (thread
->request_fd
) release_object( thread
->request_fd
);
248 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
249 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
250 free( thread
->suspend_context
);
251 free_msg_queue( thread
);
252 cleanup_clipboard_thread(thread
);
253 destroy_thread_windows( thread
);
254 close_thread_desktop( thread
);
255 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
257 if (thread
->inflight
[i
].client
!= -1)
259 close( thread
->inflight
[i
].server
);
260 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
263 thread
->req_data
= NULL
;
264 thread
->reply_data
= NULL
;
265 thread
->request_fd
= NULL
;
266 thread
->reply_fd
= NULL
;
267 thread
->wait_fd
= NULL
;
268 thread
->context
= NULL
;
269 thread
->suspend_context
= NULL
;
273 /* destroy a thread when its refcount is 0 */
274 static void destroy_thread( struct object
*obj
)
276 struct thread
*thread
= (struct thread
*)obj
;
277 assert( obj
->ops
== &thread_ops
);
279 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
280 list_remove( &thread
->entry
);
281 cleanup_thread( thread
);
282 release_object( thread
->process
);
283 if (thread
->id
) free_ptid( thread
->id
);
284 if (thread
->token
) release_object( thread
->token
);
287 /* dump a thread on stdout for debugging purposes */
288 static void dump_thread( struct object
*obj
, int verbose
)
290 struct thread
*thread
= (struct thread
*)obj
;
291 assert( obj
->ops
== &thread_ops
);
293 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
294 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->teb
, thread
->state
);
297 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
299 struct thread
*mythread
= (struct thread
*)obj
;
300 return (mythread
->state
== TERMINATED
);
303 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
305 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
306 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
307 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
308 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
309 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
312 static void dump_thread_apc( struct object
*obj
, int verbose
)
314 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
315 assert( obj
->ops
== &thread_apc_ops
);
317 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
320 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
)
322 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
323 return apc
->executed
;
326 static void thread_apc_destroy( struct object
*obj
)
328 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
329 if (apc
->caller
) release_object( apc
->caller
);
330 if (apc
->owner
) release_object( apc
->owner
);
333 /* queue an async procedure call */
334 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
336 struct thread_apc
*apc
;
338 if ((apc
= alloc_object( &thread_apc_ops
)))
340 apc
->call
= *call_data
;
344 apc
->result
.type
= APC_NONE
;
345 if (owner
) grab_object( owner
);
350 /* get a thread pointer from a thread id (and increment the refcount) */
351 struct thread
*get_thread_from_id( thread_id_t id
)
353 struct object
*obj
= get_ptid_entry( id
);
355 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
356 set_error( STATUS_INVALID_CID
);
360 /* get a thread from a handle (and increment the refcount) */
361 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
363 return (struct thread
*)get_handle_obj( current
->process
, handle
,
364 access
, &thread_ops
);
367 /* find a thread from a Unix tid */
368 struct thread
*get_thread_from_tid( int tid
)
370 struct thread
*thread
;
372 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
374 if (thread
->unix_tid
== tid
) return thread
;
379 /* find a thread from a Unix pid */
380 struct thread
*get_thread_from_pid( int pid
)
382 struct thread
*thread
;
384 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
386 if (thread
->unix_pid
== pid
) return thread
;
391 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
392 #define THREAD_PRIORITY_REALTIME_LOWEST -7
394 /* set all information about a thread */
395 static void set_thread_info( struct thread
*thread
,
396 const struct set_thread_info_request
*req
)
398 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
400 int max
= THREAD_PRIORITY_HIGHEST
;
401 int min
= THREAD_PRIORITY_LOWEST
;
402 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
404 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
405 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
407 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
408 req
->priority
== THREAD_PRIORITY_IDLE
||
409 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
410 thread
->priority
= req
->priority
;
412 set_error( STATUS_INVALID_PARAMETER
);
414 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
416 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
417 else thread
->affinity
= req
->affinity
;
419 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
420 security_set_thread_token( thread
, req
->token
);
423 /* stop a thread (at the Unix level) */
424 void stop_thread( struct thread
*thread
)
426 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
427 /* can't stop a thread while initialisation is in progress */
428 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
431 /* suspend a thread */
432 static int suspend_thread( struct thread
*thread
)
434 int old_count
= thread
->suspend
;
435 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
437 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
439 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
443 /* resume a thread */
444 static int resume_thread( struct thread
*thread
)
446 int old_count
= thread
->suspend
;
447 if (thread
->suspend
> 0)
449 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
454 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
455 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
459 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
463 /* remove a thread from an object wait queue */
464 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
466 list_remove( &entry
->entry
);
467 release_object( obj
);
471 static void end_wait( struct thread
*thread
)
473 struct thread_wait
*wait
= thread
->wait
;
474 struct wait_queue_entry
*entry
;
478 thread
->wait
= wait
->next
;
479 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
480 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
481 if (wait
->user
) remove_timeout_user( wait
->user
);
485 /* build the thread wait structure */
486 static int wait_on( unsigned int count
, struct object
*objects
[], int flags
, timeout_t timeout
)
488 struct thread_wait
*wait
;
489 struct wait_queue_entry
*entry
;
492 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
493 wait
->next
= current
->wait
;
494 wait
->thread
= current
;
498 wait
->timeout
= timeout
;
499 current
->wait
= wait
;
501 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
503 struct object
*obj
= objects
[i
];
504 entry
->thread
= current
;
505 if (!obj
->ops
->add_queue( obj
, entry
))
515 /* check if the thread waiting condition is satisfied */
516 static int check_wait( struct thread
*thread
)
519 struct thread_wait
*wait
= thread
->wait
;
520 struct wait_queue_entry
*entry
= wait
->queues
;
524 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
525 return STATUS_USER_APC
;
527 /* Suspended threads may not acquire locks, but they can run system APCs */
528 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
530 if (wait
->flags
& SELECT_ALL
)
533 /* Note: we must check them all anyway, as some objects may
534 * want to do something when signaled, even if others are not */
535 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
536 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
537 if (not_ok
) goto other_checks
;
538 /* Wait satisfied: tell it to all objects */
540 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
541 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
542 signaled
= STATUS_ABANDONED_WAIT_0
;
547 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
549 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
550 /* Wait satisfied: tell it to the object */
552 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
553 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
559 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
560 if (wait
->timeout
<= current_time
) return STATUS_TIMEOUT
;
564 /* send the wakeup signal to a thread */
565 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
567 struct wake_up_reply reply
;
570 reply
.cookie
= cookie
;
571 reply
.signaled
= signaled
;
572 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
575 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
576 else if (errno
== EPIPE
)
577 kill_thread( thread
, 0 ); /* normal death */
579 fatal_protocol_perror( thread
, "write" );
583 /* attempt to wake up a thread */
584 /* return >0 if OK, 0 if the wait condition is still not satisfied */
585 int wake_thread( struct thread
*thread
)
590 for (count
= 0; thread
->wait
; count
++)
592 if ((signaled
= check_wait( thread
)) == -1) break;
594 cookie
= thread
->wait
->cookie
;
595 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
596 thread
->id
, signaled
, cookie
);
598 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
604 /* thread wait timeout */
605 static void thread_timeout( void *ptr
)
607 struct thread_wait
*wait
= ptr
;
608 struct thread
*thread
= wait
->thread
;
609 void *cookie
= wait
->cookie
;
612 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
613 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
615 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
616 thread
->id
, (int)STATUS_TIMEOUT
, cookie
);
618 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
619 /* check if other objects have become signaled in the meantime */
620 wake_thread( thread
);
623 /* try signaling an event flag, a semaphore or a mutex */
624 static int signal_object( obj_handle_t handle
)
629 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
632 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
633 release_object( obj
);
638 /* select on a list of handles */
639 static timeout_t
select_on( unsigned int count
, void *cookie
, const obj_handle_t
*handles
,
640 int flags
, timeout_t timeout
, obj_handle_t signal_obj
)
644 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
646 if (timeout
<= 0) timeout
= current_time
- timeout
;
648 if (count
> MAXIMUM_WAIT_OBJECTS
)
650 set_error( STATUS_INVALID_PARAMETER
);
653 for (i
= 0; i
< count
; i
++)
655 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
659 if (i
< count
) goto done
;
660 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
662 /* signal the object */
665 if (!signal_object( signal_obj
))
670 /* check if we woke ourselves up */
671 if (!current
->wait
) goto done
;
674 if ((ret
= check_wait( current
)) != -1)
676 /* condition is already satisfied */
682 /* now we need to wait */
683 if (current
->wait
->timeout
!= TIMEOUT_INFINITE
)
685 if (!(current
->wait
->user
= add_timeout_user( current
->wait
->timeout
,
686 thread_timeout
, current
->wait
)))
692 current
->wait
->cookie
= cookie
;
693 set_error( STATUS_PENDING
);
696 while (i
> 0) release_object( objects
[--i
] );
700 /* attempt to wake threads sleeping on the object wait queue */
701 void wake_up( struct object
*obj
, int max
)
703 struct list
*ptr
, *next
;
705 LIST_FOR_EACH_SAFE( ptr
, next
, &obj
->wait_queue
)
707 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
708 if (wake_thread( entry
->thread
))
710 if (max
&& !--max
) break;
715 /* return the apc queue to use for a given apc type */
716 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
723 return &thread
->user_apc
;
725 return &thread
->system_apc
;
729 /* check if thread is currently waiting for a (system) apc */
730 static inline int is_in_apc_wait( struct thread
*thread
)
732 return (thread
->process
->suspend
|| thread
->suspend
||
733 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
736 /* queue an existing APC to a given thread */
737 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
741 if (!thread
) /* find a suitable thread inside the process */
743 struct thread
*candidate
;
745 /* first try to find a waiting thread */
746 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
748 if (candidate
->state
== TERMINATED
) continue;
749 if (is_in_apc_wait( candidate
))
757 /* then use the first one that accepts a signal */
758 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
760 if (send_thread_signal( candidate
, SIGUSR1
))
767 if (!thread
) return 0; /* nothing found */
768 queue
= get_apc_queue( thread
, apc
->call
.type
);
772 if (thread
->state
== TERMINATED
) return 0;
773 queue
= get_apc_queue( thread
, apc
->call
.type
);
774 /* send signal for system APCs if needed */
775 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
777 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
779 /* cancel a possible previous APC with the same owner */
780 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
784 list_add_tail( queue
, &apc
->entry
);
785 if (!list_prev( queue
, &apc
->entry
)) /* first one */
786 wake_thread( thread
);
791 /* queue an async procedure call */
792 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
794 struct thread_apc
*apc
;
797 if ((apc
= create_apc( owner
, call_data
)))
799 ret
= queue_apc( NULL
, thread
, apc
);
800 release_object( apc
);
805 /* cancel the async procedure call owned by a specific object */
806 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
808 struct thread_apc
*apc
;
809 struct list
*queue
= get_apc_queue( thread
, type
);
811 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
813 if (apc
->owner
!= owner
) continue;
814 list_remove( &apc
->entry
);
816 wake_up( &apc
->obj
, 0 );
817 release_object( apc
);
822 /* remove the head apc from the queue; the returned object must be released by the caller */
823 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
825 struct thread_apc
*apc
= NULL
;
826 struct list
*ptr
= list_head( &thread
->system_apc
);
828 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
831 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
837 /* clear an APC queue, cancelling all the APCs on it */
838 static void clear_apc_queue( struct list
*queue
)
842 while ((ptr
= list_head( queue
)))
844 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
845 list_remove( &apc
->entry
);
847 wake_up( &apc
->obj
, 0 );
848 release_object( apc
);
852 /* add an fd to the inflight list */
853 /* return list index, or -1 on error */
854 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
858 if (server
== -1) return -1;
865 /* first check if we already have an entry for this fd */
866 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
867 if (thread
->inflight
[i
].client
== client
)
869 close( thread
->inflight
[i
].server
);
870 thread
->inflight
[i
].server
= server
;
874 /* now find a free spot to store it */
875 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
876 if (thread
->inflight
[i
].client
== -1)
878 thread
->inflight
[i
].client
= client
;
879 thread
->inflight
[i
].server
= server
;
885 /* get an inflight fd and purge it from the list */
886 /* the fd must be closed when no longer used */
887 int thread_get_inflight_fd( struct thread
*thread
, int client
)
891 if (client
== -1) return -1;
895 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
897 if (thread
->inflight
[i
].client
== client
)
899 ret
= thread
->inflight
[i
].server
;
900 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
904 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
908 /* kill a thread on the spot */
909 void kill_thread( struct thread
*thread
, int violent_death
)
911 if (thread
->state
== TERMINATED
) return; /* already killed */
912 thread
->state
= TERMINATED
;
913 thread
->exit_time
= current_time
;
914 if (current
== thread
) current
= NULL
;
916 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
917 thread
->id
, thread
->exit_code
);
920 while (thread
->wait
) end_wait( thread
);
921 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
922 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
925 kill_console_processes( thread
, 0 );
926 debug_exit_thread( thread
);
927 abandon_mutexes( thread
);
928 wake_up( &thread
->obj
, 0 );
929 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
930 cleanup_thread( thread
);
931 remove_process_thread( thread
->process
, thread
);
932 release_object( thread
);
935 /* take a snapshot of currently running threads */
936 struct thread_snapshot
*thread_snap( int *count
)
938 struct thread_snapshot
*snapshot
, *ptr
;
939 struct thread
*thread
;
942 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
943 if (thread
->state
!= TERMINATED
) total
++;
944 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
946 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
948 if (thread
->state
== TERMINATED
) continue;
949 ptr
->thread
= thread
;
950 ptr
->count
= thread
->obj
.refcount
;
951 ptr
->priority
= thread
->priority
;
952 grab_object( thread
);
959 /* gets the current impersonation token */
960 struct token
*thread_get_impersonation_token( struct thread
*thread
)
963 return thread
->token
;
965 return thread
->process
->token
;
968 /* create a new thread */
969 DECL_HANDLER(new_thread
)
971 struct thread
*thread
;
972 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
974 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
976 if (request_fd
!= -1) close( request_fd
);
977 set_error( STATUS_INVALID_HANDLE
);
981 if ((thread
= create_thread( request_fd
, current
->process
)))
983 if (req
->suspend
) thread
->suspend
++;
984 reply
->tid
= get_thread_id( thread
);
985 if ((reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
)))
987 /* thread object will be released when the thread gets killed */
990 kill_thread( thread
, 1 );
994 /* initialize a new thread */
995 DECL_HANDLER(init_thread
)
997 struct process
*process
= current
->process
;
998 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
999 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
1001 if (current
->reply_fd
) /* already initialised */
1003 set_error( STATUS_INVALID_PARAMETER
);
1007 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1009 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1011 if (!current
->reply_fd
) goto error
;
1015 set_error( STATUS_TOO_MANY_OPENED_FILES
); /* most likely reason */
1018 if (!(current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 )))
1021 if (!is_valid_address(req
->teb
) || !is_valid_address(req
->peb
) || !is_valid_address(req
->ldt_copy
))
1023 set_error( STATUS_INVALID_PARAMETER
);
1027 current
->unix_pid
= req
->unix_pid
;
1028 current
->unix_tid
= req
->unix_tid
;
1029 current
->teb
= req
->teb
;
1031 if (!process
->peb
) /* first thread, initialize the process too */
1033 process
->unix_pid
= current
->unix_pid
;
1034 process
->peb
= req
->peb
;
1035 process
->ldt_copy
= req
->ldt_copy
;
1036 reply
->info_size
= init_process( current
);
1040 if (process
->unix_pid
!= current
->unix_pid
)
1041 process
->unix_pid
= -1; /* can happen with linuxthreads */
1042 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
1043 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
1045 debug_level
= max( debug_level
, req
->debug_level
);
1047 reply
->pid
= get_process_id( process
);
1048 reply
->tid
= get_thread_id( current
);
1049 reply
->version
= SERVER_PROTOCOL_VERSION
;
1050 reply
->server_start
= server_start_time
;
1054 if (reply_fd
!= -1) close( reply_fd
);
1055 if (wait_fd
!= -1) close( wait_fd
);
1058 /* terminate a thread */
1059 DECL_HANDLER(terminate_thread
)
1061 struct thread
*thread
;
1065 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1067 thread
->exit_code
= req
->exit_code
;
1068 if (thread
!= current
) kill_thread( thread
, 1 );
1072 reply
->last
= (thread
->process
->running_threads
== 1);
1074 release_object( thread
);
1078 /* open a handle to a thread */
1079 DECL_HANDLER(open_thread
)
1081 struct thread
*thread
= get_thread_from_id( req
->tid
);
1086 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1087 release_object( thread
);
1091 /* fetch information about a thread */
1092 DECL_HANDLER(get_thread_info
)
1094 struct thread
*thread
;
1095 obj_handle_t handle
= req
->handle
;
1097 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1098 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
1102 reply
->pid
= get_process_id( thread
->process
);
1103 reply
->tid
= get_thread_id( thread
);
1104 reply
->teb
= thread
->teb
;
1105 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1106 reply
->priority
= thread
->priority
;
1107 reply
->affinity
= thread
->affinity
;
1108 reply
->creation_time
= thread
->creation_time
;
1109 reply
->exit_time
= thread
->exit_time
;
1110 reply
->last
= thread
->process
->running_threads
== 1;
1112 release_object( thread
);
1116 /* set information about a thread */
1117 DECL_HANDLER(set_thread_info
)
1119 struct thread
*thread
;
1121 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1123 set_thread_info( thread
, req
);
1124 release_object( thread
);
1128 /* suspend a thread */
1129 DECL_HANDLER(suspend_thread
)
1131 struct thread
*thread
;
1133 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1135 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1136 else reply
->count
= suspend_thread( thread
);
1137 release_object( thread
);
1141 /* resume a thread */
1142 DECL_HANDLER(resume_thread
)
1144 struct thread
*thread
;
1146 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1148 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1149 else reply
->count
= resume_thread( thread
);
1150 release_object( thread
);
1154 /* select on a handle list */
1155 DECL_HANDLER(select
)
1157 struct thread_apc
*apc
;
1159 const apc_result_t
*result
= get_req_data();
1160 const obj_handle_t
*handles
= (const obj_handle_t
*)(result
+ 1);
1162 if (get_req_data_size() < sizeof(*result
))
1164 set_error( STATUS_INVALID_PARAMETER
);
1167 count
= (get_req_data_size() - sizeof(*result
)) / sizeof(obj_handle_t
);
1169 /* first store results of previous apc */
1172 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1173 0, &thread_apc_ops
))) return;
1174 apc
->result
= *result
;
1176 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1178 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1179 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1180 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1181 apc
->result
.create_thread
.handle
= handle
;
1182 clear_error(); /* ignore errors from the above calls */
1184 else if (apc
->result
.type
== APC_ASYNC_IO
)
1186 if (apc
->owner
) async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
1188 wake_up( &apc
->obj
, 0 );
1189 close_handle( current
->process
, req
->prev_apc
);
1190 release_object( apc
);
1193 reply
->timeout
= select_on( count
, req
->cookie
, handles
, req
->flags
, req
->timeout
, req
->signal
);
1195 if (get_error() == STATUS_USER_APC
)
1199 if (!(apc
= thread_dequeue_apc( current
, !(req
->flags
& SELECT_ALERTABLE
) )))
1201 /* Optimization: ignore APC_NONE calls, they are only used to
1202 * wake up a thread, but since we got here the thread woke up already.
1204 if (apc
->call
.type
!= APC_NONE
)
1206 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1207 reply
->call
= apc
->call
;
1208 release_object( apc
);
1212 wake_up( &apc
->obj
, 0 );
1213 release_object( apc
);
1218 /* queue an APC for a thread or process */
1219 DECL_HANDLER(queue_apc
)
1221 struct thread
*thread
= NULL
;
1222 struct process
*process
= NULL
;
1223 struct thread_apc
*apc
;
1225 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1227 switch (apc
->call
.type
)
1231 thread
= get_thread_from_handle( req
->thread
, THREAD_SET_CONTEXT
);
1233 case APC_VIRTUAL_ALLOC
:
1234 case APC_VIRTUAL_FREE
:
1235 case APC_VIRTUAL_PROTECT
:
1236 case APC_VIRTUAL_FLUSH
:
1237 case APC_VIRTUAL_LOCK
:
1238 case APC_VIRTUAL_UNLOCK
:
1239 case APC_UNMAP_VIEW
:
1240 process
= get_process_from_handle( req
->process
, PROCESS_VM_OPERATION
);
1242 case APC_VIRTUAL_QUERY
:
1243 process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
);
1246 process
= get_process_from_handle( req
->process
, PROCESS_VM_OPERATION
);
1247 if (process
&& process
!= current
->process
)
1249 /* duplicate the handle into the target process */
1250 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1251 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1252 if (handle
) apc
->call
.map_view
.handle
= handle
;
1255 release_object( process
);
1260 case APC_CREATE_THREAD
:
1261 process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
);
1264 set_error( STATUS_INVALID_PARAMETER
);
1270 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1271 release_object( thread
);
1275 reply
->self
= (process
== current
->process
);
1278 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1281 if (queue_apc( process
, NULL
, apc
))
1283 apc
->caller
= (struct thread
*)grab_object( current
);
1284 reply
->handle
= handle
;
1288 close_handle( current
->process
, handle
);
1289 set_error( STATUS_PROCESS_IS_TERMINATING
);
1293 release_object( process
);
1296 release_object( apc
);
1299 /* Get the result of an APC call */
1300 DECL_HANDLER(get_apc_result
)
1302 struct thread_apc
*apc
;
1304 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1305 0, &thread_apc_ops
))) return;
1306 if (!apc
->executed
) set_error( STATUS_PENDING
);
1309 reply
->result
= apc
->result
;
1310 /* close the handle directly to avoid an extra round-trip */
1311 close_handle( current
->process
, req
->handle
);
1313 release_object( apc
);
1316 /* retrieve the current context of a thread */
1317 DECL_HANDLER(get_thread_context
)
1319 struct thread
*thread
;
1322 if (get_reply_max_size() < sizeof(CONTEXT
))
1324 set_error( STATUS_INVALID_PARAMETER
);
1327 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1331 if (thread
!= current
|| !thread
->suspend_context
)
1333 /* not suspended, shouldn't happen */
1334 set_error( STATUS_INVALID_PARAMETER
);
1338 if (thread
->context
== thread
->suspend_context
) thread
->context
= NULL
;
1339 set_reply_data_ptr( thread
->suspend_context
, sizeof(CONTEXT
) );
1340 thread
->suspend_context
= NULL
;
1343 else if (thread
!= current
&& !thread
->context
)
1345 /* thread is not suspended, retry (if it's still running) */
1346 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1347 else set_error( STATUS_PENDING
);
1349 else if ((context
= set_reply_data_size( sizeof(CONTEXT
) )))
1351 unsigned int flags
= get_context_system_regs( req
->flags
);
1353 memset( context
, 0, sizeof(CONTEXT
) );
1354 context
->ContextFlags
= get_context_cpu_flag();
1355 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1356 if (flags
) get_thread_context( thread
, context
, flags
);
1358 reply
->self
= (thread
== current
);
1359 release_object( thread
);
1362 /* set the current context of a thread */
1363 DECL_HANDLER(set_thread_context
)
1365 struct thread
*thread
;
1367 if (get_req_data_size() < sizeof(CONTEXT
))
1369 set_error( STATUS_INVALID_PARAMETER
);
1372 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1376 if (thread
!= current
|| thread
->context
)
1378 /* nested suspend or exception, shouldn't happen */
1379 set_error( STATUS_INVALID_PARAMETER
);
1381 else if ((thread
->suspend_context
= mem_alloc( sizeof(CONTEXT
) )))
1383 memcpy( thread
->suspend_context
, get_req_data(), sizeof(CONTEXT
) );
1384 thread
->context
= thread
->suspend_context
;
1387 else if (thread
!= current
&& !thread
->context
)
1389 /* thread is not suspended, retry (if it's still running) */
1390 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1391 else set_error( STATUS_PENDING
);
1395 const CONTEXT
*context
= get_req_data();
1396 unsigned int flags
= get_context_system_regs( req
->flags
);
1398 if (flags
) set_thread_context( thread
, context
, flags
);
1399 if (thread
->context
&& !get_error())
1400 copy_context( thread
->context
, context
, req
->flags
& ~flags
);
1402 reply
->self
= (thread
== current
);
1403 release_object( thread
);
1406 /* fetch a selector entry for a thread */
1407 DECL_HANDLER(get_selector_entry
)
1409 struct thread
*thread
;
1410 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1412 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1413 release_object( thread
);