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 add_queue
, /* add_queue */
90 remove_queue
, /* remove_queue */
91 thread_apc_signaled
, /* signaled */
92 no_satisfied
, /* satisfied */
93 no_signal
, /* signal */
94 no_get_fd
, /* get_fd */
95 no_map_access
, /* map_access */
96 no_lookup_name
, /* lookup_name */
97 no_open_file
, /* open_file */
98 no_close_handle
, /* close_handle */
99 thread_apc_destroy
/* destroy */
103 /* thread operations */
105 static void dump_thread( struct object
*obj
, int verbose
);
106 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
107 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
108 static void thread_poll_event( struct fd
*fd
, int event
);
109 static void destroy_thread( struct object
*obj
);
111 static const struct object_ops thread_ops
=
113 sizeof(struct thread
), /* size */
114 dump_thread
, /* dump */
115 add_queue
, /* add_queue */
116 remove_queue
, /* remove_queue */
117 thread_signaled
, /* signaled */
118 no_satisfied
, /* satisfied */
119 no_signal
, /* signal */
120 no_get_fd
, /* get_fd */
121 thread_map_access
, /* map_access */
122 no_lookup_name
, /* lookup_name */
123 no_open_file
, /* open_file */
124 no_close_handle
, /* close_handle */
125 destroy_thread
/* destroy */
128 static const struct fd_ops thread_fd_ops
=
130 NULL
, /* get_poll_events */
131 thread_poll_event
, /* poll_event */
133 NULL
, /* get_fd_type */
135 NULL
, /* queue_async */
136 NULL
, /* reselect_async */
137 NULL
/* cancel_async */
140 static struct list thread_list
= LIST_INIT(thread_list
);
142 /* initialize the structure for a newly allocated thread */
143 static inline void init_thread_structure( struct thread
*thread
)
147 thread
->unix_pid
= -1; /* not known yet */
148 thread
->unix_tid
= -1; /* not known yet */
149 thread
->context
= NULL
;
150 thread
->suspend_context
= NULL
;
152 thread
->debug_ctx
= NULL
;
153 thread
->debug_event
= NULL
;
154 thread
->debug_break
= 0;
155 thread
->queue
= NULL
;
158 thread
->req_data
= NULL
;
159 thread
->req_toread
= 0;
160 thread
->reply_data
= NULL
;
161 thread
->reply_towrite
= 0;
162 thread
->request_fd
= NULL
;
163 thread
->reply_fd
= NULL
;
164 thread
->wait_fd
= NULL
;
165 thread
->state
= RUNNING
;
166 thread
->exit_code
= 0;
167 thread
->priority
= 0;
168 thread
->affinity
= 1;
170 thread
->desktop_users
= 0;
171 thread
->token
= NULL
;
173 thread
->creation_time
= current_time
;
174 thread
->exit_time
= 0;
176 list_init( &thread
->mutex_list
);
177 list_init( &thread
->system_apc
);
178 list_init( &thread
->user_apc
);
180 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
181 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
184 /* check if address looks valid for a client-side data structure (TEB etc.) */
185 static inline int is_valid_address( void *addr
)
187 return addr
&& !((unsigned long)addr
% sizeof(int));
190 /* create a new thread */
191 struct thread
*create_thread( int fd
, struct process
*process
)
193 struct thread
*thread
;
195 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
197 init_thread_structure( thread
);
199 thread
->process
= (struct process
*)grab_object( process
);
200 thread
->desktop
= process
->desktop
;
201 if (!current
) current
= thread
;
203 list_add_head( &thread_list
, &thread
->entry
);
205 if (!(thread
->id
= alloc_ptid( thread
)))
207 release_object( thread
);
210 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
212 release_object( thread
);
216 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
217 add_process_thread( thread
->process
, thread
);
221 /* handle a client event */
222 static void thread_poll_event( struct fd
*fd
, int event
)
224 struct thread
*thread
= get_fd_user( fd
);
225 assert( thread
->obj
.ops
== &thread_ops
);
227 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
228 else if (event
& POLLIN
) read_request( thread
);
229 else if (event
& POLLOUT
) write_reply( thread
);
232 /* cleanup everything that is no longer needed by a dead thread */
233 /* used by destroy_thread and kill_thread */
234 static void cleanup_thread( struct thread
*thread
)
238 clear_apc_queue( &thread
->system_apc
);
239 clear_apc_queue( &thread
->user_apc
);
240 free( thread
->req_data
);
241 free( thread
->reply_data
);
242 if (thread
->request_fd
) release_object( thread
->request_fd
);
243 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
244 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
245 free( thread
->suspend_context
);
246 free_msg_queue( thread
);
247 cleanup_clipboard_thread(thread
);
248 destroy_thread_windows( thread
);
249 close_thread_desktop( thread
);
250 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
252 if (thread
->inflight
[i
].client
!= -1)
254 close( thread
->inflight
[i
].server
);
255 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
258 thread
->req_data
= NULL
;
259 thread
->reply_data
= NULL
;
260 thread
->request_fd
= NULL
;
261 thread
->reply_fd
= NULL
;
262 thread
->wait_fd
= NULL
;
263 thread
->context
= NULL
;
264 thread
->suspend_context
= NULL
;
268 /* destroy a thread when its refcount is 0 */
269 static void destroy_thread( struct object
*obj
)
271 struct thread
*thread
= (struct thread
*)obj
;
272 assert( obj
->ops
== &thread_ops
);
274 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
275 list_remove( &thread
->entry
);
276 cleanup_thread( thread
);
277 release_object( thread
->process
);
278 if (thread
->id
) free_ptid( thread
->id
);
279 if (thread
->token
) release_object( thread
->token
);
282 /* dump a thread on stdout for debugging purposes */
283 static void dump_thread( struct object
*obj
, int verbose
)
285 struct thread
*thread
= (struct thread
*)obj
;
286 assert( obj
->ops
== &thread_ops
);
288 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
289 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->teb
, thread
->state
);
292 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
294 struct thread
*mythread
= (struct thread
*)obj
;
295 return (mythread
->state
== TERMINATED
);
298 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
300 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
301 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
302 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
303 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
304 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
307 static void dump_thread_apc( struct object
*obj
, int verbose
)
309 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
310 assert( obj
->ops
== &thread_apc_ops
);
312 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
315 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
)
317 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
318 return apc
->executed
;
321 static void thread_apc_destroy( struct object
*obj
)
323 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
324 if (apc
->caller
) release_object( apc
->caller
);
325 if (apc
->owner
) release_object( apc
->owner
);
328 /* queue an async procedure call */
329 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
331 struct thread_apc
*apc
;
333 if ((apc
= alloc_object( &thread_apc_ops
)))
335 apc
->call
= *call_data
;
339 apc
->result
.type
= APC_NONE
;
340 if (owner
) grab_object( owner
);
345 /* get a thread pointer from a thread id (and increment the refcount) */
346 struct thread
*get_thread_from_id( thread_id_t id
)
348 struct object
*obj
= get_ptid_entry( id
);
350 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
351 set_error( STATUS_INVALID_CID
);
355 /* get a thread from a handle (and increment the refcount) */
356 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
358 return (struct thread
*)get_handle_obj( current
->process
, handle
,
359 access
, &thread_ops
);
362 /* find a thread from a Unix tid */
363 struct thread
*get_thread_from_tid( int tid
)
365 struct thread
*thread
;
367 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
369 if (thread
->unix_tid
== tid
) return thread
;
374 /* find a thread from a Unix pid */
375 struct thread
*get_thread_from_pid( int pid
)
377 struct thread
*thread
;
379 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
381 if (thread
->unix_pid
== pid
) return thread
;
386 /* set all information about a thread */
387 static void set_thread_info( struct thread
*thread
,
388 const struct set_thread_info_request
*req
)
390 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
391 thread
->priority
= req
->priority
;
392 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
394 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
395 else thread
->affinity
= req
->affinity
;
397 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
398 security_set_thread_token( thread
, req
->token
);
401 /* stop a thread (at the Unix level) */
402 void stop_thread( struct thread
*thread
)
404 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
405 /* can't stop a thread while initialisation is in progress */
406 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
409 /* suspend a thread */
410 static int suspend_thread( struct thread
*thread
)
412 int old_count
= thread
->suspend
;
413 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
415 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
417 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
421 /* resume a thread */
422 static int resume_thread( struct thread
*thread
)
424 int old_count
= thread
->suspend
;
425 if (thread
->suspend
> 0)
427 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
432 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
433 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
437 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
441 /* remove a thread from an object wait queue */
442 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
444 list_remove( &entry
->entry
);
445 release_object( obj
);
449 static void end_wait( struct thread
*thread
)
451 struct thread_wait
*wait
= thread
->wait
;
452 struct wait_queue_entry
*entry
;
456 thread
->wait
= wait
->next
;
457 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
458 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
459 if (wait
->user
) remove_timeout_user( wait
->user
);
463 /* build the thread wait structure */
464 static int wait_on( unsigned int count
, struct object
*objects
[], int flags
, timeout_t timeout
)
466 struct thread_wait
*wait
;
467 struct wait_queue_entry
*entry
;
470 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
471 wait
->next
= current
->wait
;
472 wait
->thread
= current
;
476 wait
->timeout
= timeout
;
477 current
->wait
= wait
;
479 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
481 struct object
*obj
= objects
[i
];
482 entry
->thread
= current
;
483 if (!obj
->ops
->add_queue( obj
, entry
))
493 /* check if the thread waiting condition is satisfied */
494 static int check_wait( struct thread
*thread
)
497 struct thread_wait
*wait
= thread
->wait
;
498 struct wait_queue_entry
*entry
= wait
->queues
;
502 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
503 return STATUS_USER_APC
;
505 /* Suspended threads may not acquire locks, but they can run system APCs */
506 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
508 if (wait
->flags
& SELECT_ALL
)
511 /* Note: we must check them all anyway, as some objects may
512 * want to do something when signaled, even if others are not */
513 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
514 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
515 if (not_ok
) goto other_checks
;
516 /* Wait satisfied: tell it to all objects */
518 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
519 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
520 signaled
= STATUS_ABANDONED_WAIT_0
;
525 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
527 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
528 /* Wait satisfied: tell it to the object */
530 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
531 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
537 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
538 if (wait
->timeout
<= current_time
) return STATUS_TIMEOUT
;
542 /* send the wakeup signal to a thread */
543 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
545 struct wake_up_reply reply
;
548 reply
.cookie
= cookie
;
549 reply
.signaled
= signaled
;
550 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
553 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
554 else if (errno
== EPIPE
)
555 kill_thread( thread
, 0 ); /* normal death */
557 fatal_protocol_perror( thread
, "write" );
561 /* attempt to wake up a thread */
562 /* return >0 if OK, 0 if the wait condition is still not satisfied */
563 int wake_thread( struct thread
*thread
)
568 for (count
= 0; thread
->wait
; count
++)
570 if ((signaled
= check_wait( thread
)) == -1) break;
572 cookie
= thread
->wait
->cookie
;
573 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
574 thread
->id
, signaled
, cookie
);
576 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
582 /* thread wait timeout */
583 static void thread_timeout( void *ptr
)
585 struct thread_wait
*wait
= ptr
;
586 struct thread
*thread
= wait
->thread
;
587 void *cookie
= wait
->cookie
;
590 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
591 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
593 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
594 thread
->id
, (int)STATUS_TIMEOUT
, cookie
);
596 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
597 /* check if other objects have become signaled in the meantime */
598 wake_thread( thread
);
601 /* try signaling an event flag, a semaphore or a mutex */
602 static int signal_object( obj_handle_t handle
)
607 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
610 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
611 release_object( obj
);
616 /* select on a list of handles */
617 static timeout_t
select_on( unsigned int count
, void *cookie
, const obj_handle_t
*handles
,
618 int flags
, timeout_t timeout
, obj_handle_t signal_obj
)
622 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
624 if (timeout
<= 0) timeout
= current_time
- timeout
;
626 if (count
> MAXIMUM_WAIT_OBJECTS
)
628 set_error( STATUS_INVALID_PARAMETER
);
631 for (i
= 0; i
< count
; i
++)
633 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
637 if (i
< count
) goto done
;
638 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
640 /* signal the object */
643 if (!signal_object( signal_obj
))
648 /* check if we woke ourselves up */
649 if (!current
->wait
) goto done
;
652 if ((ret
= check_wait( current
)) != -1)
654 /* condition is already satisfied */
660 /* now we need to wait */
661 if (current
->wait
->timeout
!= TIMEOUT_INFINITE
)
663 if (!(current
->wait
->user
= add_timeout_user( current
->wait
->timeout
,
664 thread_timeout
, current
->wait
)))
670 current
->wait
->cookie
= cookie
;
671 set_error( STATUS_PENDING
);
674 while (i
> 0) release_object( objects
[--i
] );
678 /* attempt to wake threads sleeping on the object wait queue */
679 void wake_up( struct object
*obj
, int max
)
681 struct list
*ptr
, *next
;
683 LIST_FOR_EACH_SAFE( ptr
, next
, &obj
->wait_queue
)
685 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
686 if (wake_thread( entry
->thread
))
688 if (max
&& !--max
) break;
693 /* return the apc queue to use for a given apc type */
694 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
701 return &thread
->user_apc
;
703 return &thread
->system_apc
;
707 /* check if thread is currently waiting for a (system) apc */
708 static inline int is_in_apc_wait( struct thread
*thread
)
710 return (thread
->process
->suspend
|| thread
->suspend
||
711 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
714 /* queue an existing APC to a given thread */
715 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
719 if (!thread
) /* find a suitable thread inside the process */
721 struct thread
*candidate
;
723 /* first try to find a waiting thread */
724 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
726 if (candidate
->state
== TERMINATED
) continue;
727 if (is_in_apc_wait( candidate
))
735 /* then use the first one that accepts a signal */
736 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
738 if (send_thread_signal( candidate
, SIGUSR1
))
745 if (!thread
) return 0; /* nothing found */
746 queue
= get_apc_queue( thread
, apc
->call
.type
);
750 if (thread
->state
== TERMINATED
) return 0;
751 queue
= get_apc_queue( thread
, apc
->call
.type
);
752 /* send signal for system APCs if needed */
753 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
755 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
757 /* cancel a possible previous APC with the same owner */
758 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
762 list_add_tail( queue
, &apc
->entry
);
763 if (!list_prev( queue
, &apc
->entry
)) /* first one */
764 wake_thread( thread
);
769 /* queue an async procedure call */
770 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
772 struct thread_apc
*apc
;
775 if ((apc
= create_apc( owner
, call_data
)))
777 ret
= queue_apc( NULL
, thread
, apc
);
778 release_object( apc
);
783 /* cancel the async procedure call owned by a specific object */
784 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
786 struct thread_apc
*apc
;
787 struct list
*queue
= get_apc_queue( thread
, type
);
789 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
791 if (apc
->owner
!= owner
) continue;
792 list_remove( &apc
->entry
);
794 wake_up( &apc
->obj
, 0 );
795 release_object( apc
);
800 /* remove the head apc from the queue; the returned object must be released by the caller */
801 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
803 struct thread_apc
*apc
= NULL
;
804 struct list
*ptr
= list_head( &thread
->system_apc
);
806 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
809 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
815 /* clear an APC queue, cancelling all the APCs on it */
816 static void clear_apc_queue( struct list
*queue
)
820 while ((ptr
= list_head( queue
)))
822 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
823 list_remove( &apc
->entry
);
825 wake_up( &apc
->obj
, 0 );
826 release_object( apc
);
830 /* add an fd to the inflight list */
831 /* return list index, or -1 on error */
832 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
836 if (server
== -1) return -1;
843 /* first check if we already have an entry for this fd */
844 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
845 if (thread
->inflight
[i
].client
== client
)
847 close( thread
->inflight
[i
].server
);
848 thread
->inflight
[i
].server
= server
;
852 /* now find a free spot to store it */
853 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
854 if (thread
->inflight
[i
].client
== -1)
856 thread
->inflight
[i
].client
= client
;
857 thread
->inflight
[i
].server
= server
;
863 /* get an inflight fd and purge it from the list */
864 /* the fd must be closed when no longer used */
865 int thread_get_inflight_fd( struct thread
*thread
, int client
)
869 if (client
== -1) return -1;
873 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
875 if (thread
->inflight
[i
].client
== client
)
877 ret
= thread
->inflight
[i
].server
;
878 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
882 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
886 /* kill a thread on the spot */
887 void kill_thread( struct thread
*thread
, int violent_death
)
889 if (thread
->state
== TERMINATED
) return; /* already killed */
890 thread
->state
= TERMINATED
;
891 thread
->exit_time
= current_time
;
892 if (current
== thread
) current
= NULL
;
894 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
895 thread
->id
, thread
->exit_code
);
898 while (thread
->wait
) end_wait( thread
);
899 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
900 /* if it is waiting on the socket, we don't need to send a SIGTERM */
903 kill_console_processes( thread
, 0 );
904 debug_exit_thread( thread
);
905 abandon_mutexes( thread
);
906 wake_up( &thread
->obj
, 0 );
907 if (violent_death
) send_thread_signal( thread
, SIGTERM
);
908 cleanup_thread( thread
);
909 remove_process_thread( thread
->process
, thread
);
910 release_object( thread
);
913 /* trigger a breakpoint event in a given thread */
914 void break_thread( struct thread
*thread
)
916 struct debug_event_exception data
;
918 assert( thread
->context
);
920 data
.record
.ExceptionCode
= STATUS_BREAKPOINT
;
921 data
.record
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
922 data
.record
.ExceptionRecord
= NULL
;
923 data
.record
.ExceptionAddress
= get_context_ip( thread
->context
);
924 data
.record
.NumberParameters
= 0;
926 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
927 thread
->debug_break
= 0;
930 /* take a snapshot of currently running threads */
931 struct thread_snapshot
*thread_snap( int *count
)
933 struct thread_snapshot
*snapshot
, *ptr
;
934 struct thread
*thread
;
937 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
938 if (thread
->state
!= TERMINATED
) total
++;
939 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
941 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
943 if (thread
->state
== TERMINATED
) continue;
944 ptr
->thread
= thread
;
945 ptr
->count
= thread
->obj
.refcount
;
946 ptr
->priority
= thread
->priority
;
947 grab_object( thread
);
954 /* gets the current impersonation token */
955 struct token
*thread_get_impersonation_token( struct thread
*thread
)
958 return thread
->token
;
960 return thread
->process
->token
;
963 /* create a new thread */
964 DECL_HANDLER(new_thread
)
966 struct thread
*thread
;
967 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
969 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
971 if (request_fd
!= -1) close( request_fd
);
972 set_error( STATUS_INVALID_HANDLE
);
976 if ((thread
= create_thread( request_fd
, current
->process
)))
978 if (req
->suspend
) thread
->suspend
++;
979 reply
->tid
= get_thread_id( thread
);
980 if ((reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
)))
982 /* thread object will be released when the thread gets killed */
985 kill_thread( thread
, 1 );
989 /* initialize a new thread */
990 DECL_HANDLER(init_thread
)
992 struct process
*process
= current
->process
;
993 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
994 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
996 if (current
->reply_fd
) /* already initialised */
998 set_error( STATUS_INVALID_PARAMETER
);
1002 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1004 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1006 if (!current
->reply_fd
) goto error
;
1010 set_error( STATUS_TOO_MANY_OPENED_FILES
); /* most likely reason */
1013 if (!(current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 )))
1016 if (!is_valid_address(req
->teb
) || !is_valid_address(req
->peb
) || !is_valid_address(req
->ldt_copy
))
1018 set_error( STATUS_INVALID_PARAMETER
);
1022 current
->unix_pid
= req
->unix_pid
;
1023 current
->unix_tid
= req
->unix_tid
;
1024 current
->teb
= req
->teb
;
1026 if (!process
->peb
) /* first thread, initialize the process too */
1028 process
->unix_pid
= current
->unix_pid
;
1029 process
->peb
= req
->peb
;
1030 process
->ldt_copy
= req
->ldt_copy
;
1031 reply
->info_size
= init_process( current
);
1035 if (process
->unix_pid
!= current
->unix_pid
)
1036 process
->unix_pid
= -1; /* can happen with linuxthreads */
1037 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
1038 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
1040 debug_level
= max( debug_level
, req
->debug_level
);
1042 reply
->pid
= get_process_id( process
);
1043 reply
->tid
= get_thread_id( current
);
1044 reply
->version
= SERVER_PROTOCOL_VERSION
;
1045 reply
->server_start
= server_start_time
;
1049 if (reply_fd
!= -1) close( reply_fd
);
1050 if (wait_fd
!= -1) close( wait_fd
);
1053 /* terminate a thread */
1054 DECL_HANDLER(terminate_thread
)
1056 struct thread
*thread
;
1060 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1062 thread
->exit_code
= req
->exit_code
;
1063 if (thread
!= current
) kill_thread( thread
, 1 );
1067 reply
->last
= (thread
->process
->running_threads
== 1);
1069 release_object( thread
);
1073 /* open a handle to a thread */
1074 DECL_HANDLER(open_thread
)
1076 struct thread
*thread
= get_thread_from_id( req
->tid
);
1081 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1082 release_object( thread
);
1086 /* fetch information about a thread */
1087 DECL_HANDLER(get_thread_info
)
1089 struct thread
*thread
;
1090 obj_handle_t handle
= req
->handle
;
1092 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1093 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
1097 reply
->pid
= get_process_id( thread
->process
);
1098 reply
->tid
= get_thread_id( thread
);
1099 reply
->teb
= thread
->teb
;
1100 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1101 reply
->priority
= thread
->priority
;
1102 reply
->affinity
= thread
->affinity
;
1103 reply
->creation_time
= thread
->creation_time
;
1104 reply
->exit_time
= thread
->exit_time
;
1105 reply
->last
= thread
->process
->running_threads
== 1;
1107 release_object( thread
);
1111 /* set information about a thread */
1112 DECL_HANDLER(set_thread_info
)
1114 struct thread
*thread
;
1116 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1118 set_thread_info( thread
, req
);
1119 release_object( thread
);
1123 /* suspend a thread */
1124 DECL_HANDLER(suspend_thread
)
1126 struct thread
*thread
;
1128 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1130 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1131 else reply
->count
= suspend_thread( thread
);
1132 release_object( thread
);
1136 /* resume a thread */
1137 DECL_HANDLER(resume_thread
)
1139 struct thread
*thread
;
1141 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1143 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1144 else reply
->count
= resume_thread( thread
);
1145 release_object( thread
);
1149 /* select on a handle list */
1150 DECL_HANDLER(select
)
1152 unsigned int count
= get_req_data_size() / sizeof(obj_handle_t
);
1153 reply
->timeout
= select_on( count
, req
->cookie
, get_req_data(), req
->flags
, req
->timeout
, req
->signal
);
1156 /* queue an APC for a thread or process */
1157 DECL_HANDLER(queue_apc
)
1159 struct thread
*thread
= NULL
;
1160 struct process
*process
= NULL
;
1161 struct thread_apc
*apc
;
1163 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1165 switch (apc
->call
.type
)
1169 thread
= get_thread_from_handle( req
->thread
, THREAD_SET_CONTEXT
);
1171 case APC_VIRTUAL_ALLOC
:
1172 case APC_VIRTUAL_FREE
:
1173 case APC_VIRTUAL_PROTECT
:
1174 case APC_VIRTUAL_FLUSH
:
1175 case APC_VIRTUAL_LOCK
:
1176 case APC_VIRTUAL_UNLOCK
:
1177 case APC_UNMAP_VIEW
:
1178 process
= get_process_from_handle( req
->process
, PROCESS_VM_OPERATION
);
1180 case APC_VIRTUAL_QUERY
:
1181 process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
);
1184 process
= get_process_from_handle( req
->process
, PROCESS_VM_OPERATION
);
1185 if (process
&& process
!= current
->process
)
1187 /* duplicate the handle into the target process */
1188 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1189 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1190 if (handle
) apc
->call
.map_view
.handle
= handle
;
1193 release_object( process
);
1198 case APC_CREATE_THREAD
:
1199 process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
);
1202 set_error( STATUS_INVALID_PARAMETER
);
1208 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1209 release_object( thread
);
1213 reply
->self
= (process
== current
->process
);
1216 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1219 if (queue_apc( process
, NULL
, apc
))
1221 apc
->caller
= (struct thread
*)grab_object( current
);
1222 reply
->handle
= handle
;
1226 close_handle( current
->process
, handle
);
1227 set_error( STATUS_PROCESS_IS_TERMINATING
);
1231 release_object( process
);
1234 release_object( apc
);
1237 /* get next APC to call */
1238 DECL_HANDLER(get_apc
)
1240 struct thread_apc
*apc
;
1241 int system_only
= !req
->alertable
;
1245 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev
,
1246 0, &thread_apc_ops
))) return;
1247 apc
->result
= req
->result
;
1249 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1251 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1252 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1253 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1254 apc
->result
.create_thread
.handle
= handle
;
1255 clear_error(); /* ignore errors from the above calls */
1257 else if (apc
->result
.type
== APC_ASYNC_IO
)
1259 if (apc
->owner
) async_set_result( apc
->owner
, apc
->result
.async_io
.status
);
1261 wake_up( &apc
->obj
, 0 );
1262 close_handle( current
->process
, req
->prev
);
1263 release_object( apc
);
1266 if (current
->suspend
+ current
->process
->suspend
> 0) system_only
= 1;
1270 if (!(apc
= thread_dequeue_apc( current
, system_only
)))
1273 set_error( STATUS_PENDING
);
1276 /* Optimization: ignore APC_NONE calls, they are only used to
1277 * wake up a thread, but since we got here the thread woke up already.
1279 if (apc
->call
.type
!= APC_NONE
) break;
1281 wake_up( &apc
->obj
, 0 );
1282 release_object( apc
);
1285 if ((reply
->handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1286 reply
->call
= apc
->call
;
1287 release_object( apc
);
1290 /* Get the result of an APC call */
1291 DECL_HANDLER(get_apc_result
)
1293 struct thread_apc
*apc
;
1295 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1296 0, &thread_apc_ops
))) return;
1297 if (!apc
->executed
) set_error( STATUS_PENDING
);
1300 reply
->result
= apc
->result
;
1301 /* close the handle directly to avoid an extra round-trip */
1302 close_handle( current
->process
, req
->handle
);
1304 release_object( apc
);
1307 /* retrieve the current context of a thread */
1308 DECL_HANDLER(get_thread_context
)
1310 struct thread
*thread
;
1313 if (get_reply_max_size() < sizeof(CONTEXT
))
1315 set_error( STATUS_INVALID_PARAMETER
);
1318 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1322 if (thread
!= current
|| !thread
->suspend_context
)
1324 /* not suspended, shouldn't happen */
1325 set_error( STATUS_INVALID_PARAMETER
);
1329 if (thread
->context
== thread
->suspend_context
) thread
->context
= NULL
;
1330 set_reply_data_ptr( thread
->suspend_context
, sizeof(CONTEXT
) );
1331 thread
->suspend_context
= NULL
;
1334 else if (thread
!= current
&& !thread
->context
)
1336 /* thread is not suspended, retry (if it's still running) */
1337 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1338 else set_error( STATUS_PENDING
);
1340 else if ((context
= set_reply_data_size( sizeof(CONTEXT
) )))
1342 unsigned int flags
= get_context_system_regs( req
->flags
);
1344 memset( context
, 0, sizeof(CONTEXT
) );
1345 context
->ContextFlags
= get_context_cpu_flag();
1346 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1347 if (flags
) get_thread_context( thread
, context
, flags
);
1349 reply
->self
= (thread
== current
);
1350 release_object( thread
);
1353 /* set the current context of a thread */
1354 DECL_HANDLER(set_thread_context
)
1356 struct thread
*thread
;
1358 if (get_req_data_size() < sizeof(CONTEXT
))
1360 set_error( STATUS_INVALID_PARAMETER
);
1363 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1367 if (thread
!= current
|| thread
->context
)
1369 /* nested suspend or exception, shouldn't happen */
1370 set_error( STATUS_INVALID_PARAMETER
);
1372 else if ((thread
->suspend_context
= mem_alloc( sizeof(CONTEXT
) )))
1374 memcpy( thread
->suspend_context
, get_req_data(), sizeof(CONTEXT
) );
1375 thread
->context
= thread
->suspend_context
;
1376 if (thread
->debug_break
) break_thread( thread
);
1379 else if (thread
!= current
&& !thread
->context
)
1381 /* thread is not suspended, retry (if it's still running) */
1382 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1383 else set_error( STATUS_PENDING
);
1387 const CONTEXT
*context
= get_req_data();
1388 unsigned int flags
= get_context_system_regs( req
->flags
);
1390 if (flags
) set_thread_context( thread
, context
, flags
);
1391 if (thread
->context
&& !get_error())
1392 copy_context( thread
->context
, context
, req
->flags
& ~flags
);
1394 reply
->self
= (thread
== current
);
1395 release_object( thread
);
1398 /* fetch a selector entry for a thread */
1399 DECL_HANDLER(get_selector_entry
)
1401 struct thread
*thread
;
1402 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1404 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1405 release_object( thread
);