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>
43 #define WIN32_NO_STATUS
60 struct thread_wait
*next
; /* next wait structure for this thread */
61 struct thread
*thread
; /* owner thread */
62 int count
; /* count of objects */
64 void *cookie
; /* magic cookie to return to client */
66 struct timeout_user
*user
;
67 struct wait_queue_entry queues
[1];
70 /* asynchronous procedure calls */
74 struct object obj
; /* object header */
75 struct list entry
; /* queue linked list */
76 struct thread
*caller
; /* thread that queued this apc */
77 struct object
*owner
; /* object that queued this apc */
78 int executed
; /* has it been executed by the client? */
79 apc_call_t call
; /* call arguments */
80 apc_result_t result
; /* call results once executed */
83 static void dump_thread_apc( struct object
*obj
, int verbose
);
84 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
);
85 static void thread_apc_destroy( struct object
*obj
);
86 static void clear_apc_queue( struct list
*queue
);
88 static const struct object_ops thread_apc_ops
=
90 sizeof(struct thread_apc
), /* size */
91 dump_thread_apc
, /* dump */
92 no_get_type
, /* get_type */
93 add_queue
, /* add_queue */
94 remove_queue
, /* remove_queue */
95 thread_apc_signaled
, /* signaled */
96 no_satisfied
, /* satisfied */
97 no_signal
, /* signal */
98 no_get_fd
, /* get_fd */
99 no_map_access
, /* map_access */
100 default_get_sd
, /* get_sd */
101 default_set_sd
, /* set_sd */
102 no_lookup_name
, /* lookup_name */
103 no_open_file
, /* open_file */
104 no_close_handle
, /* close_handle */
105 thread_apc_destroy
/* destroy */
109 /* thread operations */
111 static void dump_thread( struct object
*obj
, int verbose
);
112 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
113 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
114 static void thread_poll_event( struct fd
*fd
, int event
);
115 static void destroy_thread( struct object
*obj
);
117 static const struct object_ops thread_ops
=
119 sizeof(struct thread
), /* size */
120 dump_thread
, /* dump */
121 no_get_type
, /* get_type */
122 add_queue
, /* add_queue */
123 remove_queue
, /* remove_queue */
124 thread_signaled
, /* signaled */
125 no_satisfied
, /* satisfied */
126 no_signal
, /* signal */
127 no_get_fd
, /* get_fd */
128 thread_map_access
, /* map_access */
129 default_get_sd
, /* get_sd */
130 default_set_sd
, /* set_sd */
131 no_lookup_name
, /* lookup_name */
132 no_open_file
, /* open_file */
133 no_close_handle
, /* close_handle */
134 destroy_thread
/* destroy */
137 static const struct fd_ops thread_fd_ops
=
139 NULL
, /* get_poll_events */
140 thread_poll_event
, /* poll_event */
142 NULL
, /* get_fd_type */
144 NULL
, /* queue_async */
145 NULL
, /* reselect_async */
146 NULL
/* cancel_async */
149 static struct list thread_list
= LIST_INIT(thread_list
);
151 /* initialize the structure for a newly allocated thread */
152 static inline void init_thread_structure( struct thread
*thread
)
156 thread
->unix_pid
= -1; /* not known yet */
157 thread
->unix_tid
= -1; /* not known yet */
158 thread
->context
= NULL
;
159 thread
->suspend_context
= NULL
;
161 thread
->debug_ctx
= NULL
;
162 thread
->debug_event
= NULL
;
163 thread
->debug_break
= 0;
164 thread
->queue
= NULL
;
167 thread
->req_data
= NULL
;
168 thread
->req_toread
= 0;
169 thread
->reply_data
= NULL
;
170 thread
->reply_towrite
= 0;
171 thread
->request_fd
= NULL
;
172 thread
->reply_fd
= NULL
;
173 thread
->wait_fd
= NULL
;
174 thread
->state
= RUNNING
;
175 thread
->exit_code
= 0;
176 thread
->priority
= 0;
177 thread
->affinity
= ~0;
179 thread
->desktop_users
= 0;
180 thread
->token
= NULL
;
182 thread
->creation_time
= current_time
;
183 thread
->exit_time
= 0;
185 list_init( &thread
->mutex_list
);
186 list_init( &thread
->system_apc
);
187 list_init( &thread
->user_apc
);
189 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
190 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
193 /* check if address looks valid for a client-side data structure (TEB etc.) */
194 static inline int is_valid_address( void *addr
)
196 return addr
&& !((unsigned long)addr
% sizeof(int));
199 /* create a new thread */
200 struct thread
*create_thread( int fd
, struct process
*process
)
202 struct thread
*thread
;
204 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
206 init_thread_structure( thread
);
208 thread
->process
= (struct process
*)grab_object( process
);
209 thread
->desktop
= process
->desktop
;
210 if (!current
) current
= thread
;
212 list_add_head( &thread_list
, &thread
->entry
);
214 if (!(thread
->id
= alloc_ptid( thread
)))
216 release_object( thread
);
219 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
221 release_object( thread
);
225 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
226 add_process_thread( thread
->process
, thread
);
230 /* handle a client event */
231 static void thread_poll_event( struct fd
*fd
, int event
)
233 struct thread
*thread
= get_fd_user( fd
);
234 assert( thread
->obj
.ops
== &thread_ops
);
236 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
237 else if (event
& POLLIN
) read_request( thread
);
238 else if (event
& POLLOUT
) write_reply( thread
);
241 /* cleanup everything that is no longer needed by a dead thread */
242 /* used by destroy_thread and kill_thread */
243 static void cleanup_thread( struct thread
*thread
)
247 clear_apc_queue( &thread
->system_apc
);
248 clear_apc_queue( &thread
->user_apc
);
249 free( thread
->req_data
);
250 free( thread
->reply_data
);
251 if (thread
->request_fd
) release_object( thread
->request_fd
);
252 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
253 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
254 free( thread
->suspend_context
);
255 free_msg_queue( thread
);
256 cleanup_clipboard_thread(thread
);
257 destroy_thread_windows( thread
);
258 close_thread_desktop( thread
);
259 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
261 if (thread
->inflight
[i
].client
!= -1)
263 close( thread
->inflight
[i
].server
);
264 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
267 thread
->req_data
= NULL
;
268 thread
->reply_data
= NULL
;
269 thread
->request_fd
= NULL
;
270 thread
->reply_fd
= NULL
;
271 thread
->wait_fd
= NULL
;
272 thread
->context
= NULL
;
273 thread
->suspend_context
= NULL
;
277 /* destroy a thread when its refcount is 0 */
278 static void destroy_thread( struct object
*obj
)
280 struct thread
*thread
= (struct thread
*)obj
;
281 assert( obj
->ops
== &thread_ops
);
283 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
284 list_remove( &thread
->entry
);
285 cleanup_thread( thread
);
286 release_object( thread
->process
);
287 if (thread
->id
) free_ptid( thread
->id
);
288 if (thread
->token
) release_object( thread
->token
);
291 /* dump a thread on stdout for debugging purposes */
292 static void dump_thread( struct object
*obj
, int verbose
)
294 struct thread
*thread
= (struct thread
*)obj
;
295 assert( obj
->ops
== &thread_ops
);
297 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
298 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->teb
, thread
->state
);
301 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
303 struct thread
*mythread
= (struct thread
*)obj
;
304 return (mythread
->state
== TERMINATED
);
307 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
309 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
310 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
311 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
312 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
313 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
316 static void dump_thread_apc( struct object
*obj
, int verbose
)
318 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
319 assert( obj
->ops
== &thread_apc_ops
);
321 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
324 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
)
326 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
327 return apc
->executed
;
330 static void thread_apc_destroy( struct object
*obj
)
332 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
333 if (apc
->caller
) release_object( apc
->caller
);
334 if (apc
->owner
) release_object( apc
->owner
);
337 /* queue an async procedure call */
338 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
340 struct thread_apc
*apc
;
342 if ((apc
= alloc_object( &thread_apc_ops
)))
344 apc
->call
= *call_data
;
348 apc
->result
.type
= APC_NONE
;
349 if (owner
) grab_object( owner
);
354 /* get a thread pointer from a thread id (and increment the refcount) */
355 struct thread
*get_thread_from_id( thread_id_t id
)
357 struct object
*obj
= get_ptid_entry( id
);
359 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
360 set_error( STATUS_INVALID_CID
);
364 /* get a thread from a handle (and increment the refcount) */
365 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
367 return (struct thread
*)get_handle_obj( current
->process
, handle
,
368 access
, &thread_ops
);
371 /* find a thread from a Unix tid */
372 struct thread
*get_thread_from_tid( int tid
)
374 struct thread
*thread
;
376 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
378 if (thread
->unix_tid
== tid
) return thread
;
383 /* find a thread from a Unix pid */
384 struct thread
*get_thread_from_pid( int pid
)
386 struct thread
*thread
;
388 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
390 if (thread
->unix_pid
== pid
) return thread
;
395 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
396 #define THREAD_PRIORITY_REALTIME_LOWEST -7
398 static void set_thread_priority( int unix_tid
, int ntprio
)
401 struct sched_param param
;
402 int result
, scheduler
;
404 assert( unix_tid
!= -1 );
406 /*fprintf( stderr, "wineserver: set thread priority: %d\n", ntprio);*/
407 if (ntprio
== THREAD_PRIORITY_TIME_CRITICAL
)
409 param
.sched_priority
= 1;
410 scheduler
= SCHED_FIFO
;
412 /* TODO: add other priorities that have any effect (e.g. idle) */
415 param
.sched_priority
= 0;
416 scheduler
= SCHED_OTHER
;
419 result
= sched_setscheduler( unix_tid
, scheduler
, ¶m
);
424 if(once
&& ntprio
== THREAD_PRIORITY_TIME_CRITICAL
)
426 fprintf( stderr
, "wineserver: successfull set thread priority: %d (only printed once)\n", ntprio
);
432 if (result
== -EPERM
)
434 /* TODO: set to some sane fallback priority (a prior set priority may have succeded) */
438 fprintf( stderr
, "wineserver: Failed (EPERM) to change the priority of a thread to: %d (only printed once)\n", ntprio
);
445 fprintf( stderr
, "wineserver: Failed (with %d) to change the priority of a thread to: %d\n", result
, ntprio
);
448 perror( "wineserver: sched_setscheduler" );
452 /* set all information about a thread */
453 static void set_thread_info( struct thread
*thread
,
454 const struct set_thread_info_request
*req
)
456 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
458 int max
= THREAD_PRIORITY_HIGHEST
;
459 int min
= THREAD_PRIORITY_LOWEST
;
460 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
462 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
463 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
465 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
466 req
->priority
== THREAD_PRIORITY_IDLE
||
467 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
469 if ((thread
->priority
!= req
->priority
) && (thread
->unix_tid
!= -1))
470 set_thread_priority( thread
->unix_tid
, req
->priority
);
471 thread
->priority
= req
->priority
;
474 set_error( STATUS_INVALID_PARAMETER
);
476 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
477 thread
->affinity
= req
->affinity
;
478 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
479 security_set_thread_token( thread
, req
->token
);
482 /* stop a thread (at the Unix level) */
483 void stop_thread( struct thread
*thread
)
485 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
486 /* can't stop a thread while initialisation is in progress */
487 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
490 /* suspend a thread */
491 static int suspend_thread( struct thread
*thread
)
493 int old_count
= thread
->suspend
;
494 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
496 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
498 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
502 /* resume a thread */
503 static int resume_thread( struct thread
*thread
)
505 int old_count
= thread
->suspend
;
506 if (thread
->suspend
> 0)
508 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
513 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
514 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
518 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
522 /* remove a thread from an object wait queue */
523 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
525 list_remove( &entry
->entry
);
526 release_object( obj
);
530 static void end_wait( struct thread
*thread
)
532 struct thread_wait
*wait
= thread
->wait
;
533 struct wait_queue_entry
*entry
;
537 thread
->wait
= wait
->next
;
538 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
539 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
540 if (wait
->user
) remove_timeout_user( wait
->user
);
544 /* build the thread wait structure */
545 static int wait_on( unsigned int count
, struct object
*objects
[], int flags
, timeout_t timeout
)
547 struct thread_wait
*wait
;
548 struct wait_queue_entry
*entry
;
551 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
552 wait
->next
= current
->wait
;
553 wait
->thread
= current
;
557 wait
->timeout
= timeout
;
558 current
->wait
= wait
;
560 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
562 struct object
*obj
= objects
[i
];
563 entry
->thread
= current
;
564 if (!obj
->ops
->add_queue( obj
, entry
))
574 /* check if the thread waiting condition is satisfied */
575 static int check_wait( struct thread
*thread
)
578 struct thread_wait
*wait
= thread
->wait
;
579 struct wait_queue_entry
*entry
= wait
->queues
;
583 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
584 return STATUS_USER_APC
;
586 /* Suspended threads may not acquire locks, but they can run system APCs */
587 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
589 if (wait
->flags
& SELECT_ALL
)
592 /* Note: we must check them all anyway, as some objects may
593 * want to do something when signaled, even if others are not */
594 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
595 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
596 if (not_ok
) goto other_checks
;
597 /* Wait satisfied: tell it to all objects */
599 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
600 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
601 signaled
= STATUS_ABANDONED_WAIT_0
;
606 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
608 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
609 /* Wait satisfied: tell it to the object */
611 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
612 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
618 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
619 if (wait
->timeout
<= current_time
) return STATUS_TIMEOUT
;
623 /* send the wakeup signal to a thread */
624 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
626 struct wake_up_reply reply
;
629 reply
.cookie
= cookie
;
630 reply
.signaled
= signaled
;
631 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
634 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
635 else if (errno
== EPIPE
)
636 kill_thread( thread
, 0 ); /* normal death */
638 fatal_protocol_perror( thread
, "write" );
642 /* attempt to wake up a thread */
643 /* return >0 if OK, 0 if the wait condition is still not satisfied */
644 int wake_thread( struct thread
*thread
)
649 for (count
= 0; thread
->wait
; count
++)
651 if ((signaled
= check_wait( thread
)) == -1) break;
653 cookie
= thread
->wait
->cookie
;
654 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
655 thread
->id
, signaled
, cookie
);
657 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
663 /* thread wait timeout */
664 static void thread_timeout( void *ptr
)
666 struct thread_wait
*wait
= ptr
;
667 struct thread
*thread
= wait
->thread
;
668 void *cookie
= wait
->cookie
;
671 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
672 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
674 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
675 thread
->id
, (int)STATUS_TIMEOUT
, cookie
);
677 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
678 /* check if other objects have become signaled in the meantime */
679 wake_thread( thread
);
682 /* try signaling an event flag, a semaphore or a mutex */
683 static int signal_object( obj_handle_t handle
)
688 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
691 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
692 release_object( obj
);
697 /* select on a list of handles */
698 static timeout_t
select_on( unsigned int count
, void *cookie
, const obj_handle_t
*handles
,
699 int flags
, timeout_t timeout
, obj_handle_t signal_obj
)
703 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
705 if (timeout
<= 0) timeout
= current_time
- timeout
;
707 if (count
> MAXIMUM_WAIT_OBJECTS
)
709 set_error( STATUS_INVALID_PARAMETER
);
712 for (i
= 0; i
< count
; i
++)
714 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
718 if (i
< count
) goto done
;
719 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
721 /* signal the object */
724 if (!signal_object( signal_obj
))
729 /* check if we woke ourselves up */
730 if (!current
->wait
) goto done
;
733 if ((ret
= check_wait( current
)) != -1)
735 /* condition is already satisfied */
741 /* now we need to wait */
742 if (current
->wait
->timeout
!= TIMEOUT_INFINITE
)
744 if (!(current
->wait
->user
= add_timeout_user( current
->wait
->timeout
,
745 thread_timeout
, current
->wait
)))
751 current
->wait
->cookie
= cookie
;
752 set_error( STATUS_PENDING
);
755 while (i
> 0) release_object( objects
[--i
] );
759 /* attempt to wake threads sleeping on the object wait queue */
760 void wake_up( struct object
*obj
, int max
)
762 struct list
*ptr
, *next
;
764 LIST_FOR_EACH_SAFE( ptr
, next
, &obj
->wait_queue
)
766 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
767 if (wake_thread( entry
->thread
))
769 if (max
&& !--max
) break;
774 /* return the apc queue to use for a given apc type */
775 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
782 return &thread
->user_apc
;
784 return &thread
->system_apc
;
788 /* check if thread is currently waiting for a (system) apc */
789 static inline int is_in_apc_wait( struct thread
*thread
)
791 return (thread
->process
->suspend
|| thread
->suspend
||
792 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
795 /* queue an existing APC to a given thread */
796 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
800 if (!thread
) /* find a suitable thread inside the process */
802 struct thread
*candidate
;
804 /* first try to find a waiting thread */
805 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
807 if (candidate
->state
== TERMINATED
) continue;
808 if (is_in_apc_wait( candidate
))
816 /* then use the first one that accepts a signal */
817 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
819 if (send_thread_signal( candidate
, SIGUSR1
))
826 if (!thread
) return 0; /* nothing found */
827 queue
= get_apc_queue( thread
, apc
->call
.type
);
831 if (thread
->state
== TERMINATED
) return 0;
832 queue
= get_apc_queue( thread
, apc
->call
.type
);
833 /* send signal for system APCs if needed */
834 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
836 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
838 /* cancel a possible previous APC with the same owner */
839 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
843 list_add_tail( queue
, &apc
->entry
);
844 if (!list_prev( queue
, &apc
->entry
)) /* first one */
845 wake_thread( thread
);
850 /* queue an async procedure call */
851 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
853 struct thread_apc
*apc
;
856 if ((apc
= create_apc( owner
, call_data
)))
858 ret
= queue_apc( NULL
, thread
, apc
);
859 release_object( apc
);
864 /* cancel the async procedure call owned by a specific object */
865 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
867 struct thread_apc
*apc
;
868 struct list
*queue
= get_apc_queue( thread
, type
);
870 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
872 if (apc
->owner
!= owner
) continue;
873 list_remove( &apc
->entry
);
875 wake_up( &apc
->obj
, 0 );
876 release_object( apc
);
881 /* remove the head apc from the queue; the returned object must be released by the caller */
882 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
884 struct thread_apc
*apc
= NULL
;
885 struct list
*ptr
= list_head( &thread
->system_apc
);
887 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
890 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
896 /* clear an APC queue, cancelling all the APCs on it */
897 static void clear_apc_queue( struct list
*queue
)
901 while ((ptr
= list_head( queue
)))
903 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
904 list_remove( &apc
->entry
);
906 wake_up( &apc
->obj
, 0 );
907 release_object( apc
);
911 /* add an fd to the inflight list */
912 /* return list index, or -1 on error */
913 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
917 if (server
== -1) return -1;
924 /* first check if we already have an entry for this fd */
925 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
926 if (thread
->inflight
[i
].client
== client
)
928 close( thread
->inflight
[i
].server
);
929 thread
->inflight
[i
].server
= server
;
933 /* now find a free spot to store it */
934 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
935 if (thread
->inflight
[i
].client
== -1)
937 thread
->inflight
[i
].client
= client
;
938 thread
->inflight
[i
].server
= server
;
944 /* get an inflight fd and purge it from the list */
945 /* the fd must be closed when no longer used */
946 int thread_get_inflight_fd( struct thread
*thread
, int client
)
950 if (client
== -1) return -1;
954 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
956 if (thread
->inflight
[i
].client
== client
)
958 ret
= thread
->inflight
[i
].server
;
959 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
963 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
967 /* kill a thread on the spot */
968 void kill_thread( struct thread
*thread
, int violent_death
)
970 if (thread
->state
== TERMINATED
) return; /* already killed */
971 thread
->state
= TERMINATED
;
972 thread
->exit_time
= current_time
;
973 if (current
== thread
) current
= NULL
;
975 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
976 thread
->id
, thread
->exit_code
);
979 while (thread
->wait
) end_wait( thread
);
980 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
981 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
984 kill_console_processes( thread
, 0 );
985 debug_exit_thread( thread
);
986 abandon_mutexes( thread
);
987 wake_up( &thread
->obj
, 0 );
988 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
989 cleanup_thread( thread
);
990 remove_process_thread( thread
->process
, thread
);
991 release_object( thread
);
994 /* trigger a breakpoint event in a given thread */
995 void break_thread( struct thread
*thread
)
997 struct debug_event_exception data
;
999 assert( thread
->context
);
1001 data
.record
.ExceptionCode
= STATUS_BREAKPOINT
;
1002 data
.record
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1003 data
.record
.ExceptionRecord
= NULL
;
1004 data
.record
.ExceptionAddress
= get_context_ip( thread
->context
);
1005 data
.record
.NumberParameters
= 0;
1007 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
1008 thread
->debug_break
= 0;
1011 /* take a snapshot of currently running threads */
1012 struct thread_snapshot
*thread_snap( int *count
)
1014 struct thread_snapshot
*snapshot
, *ptr
;
1015 struct thread
*thread
;
1018 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1019 if (thread
->state
!= TERMINATED
) total
++;
1020 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
1022 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1024 if (thread
->state
== TERMINATED
) continue;
1025 ptr
->thread
= thread
;
1026 ptr
->count
= thread
->obj
.refcount
;
1027 ptr
->priority
= thread
->priority
;
1028 grab_object( thread
);
1035 /* gets the current impersonation token */
1036 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1039 return thread
->token
;
1041 return thread
->process
->token
;
1044 /* create a new thread */
1045 DECL_HANDLER(new_thread
)
1047 struct thread
*thread
;
1048 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1050 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1052 if (request_fd
!= -1) close( request_fd
);
1053 set_error( STATUS_INVALID_HANDLE
);
1057 if ((thread
= create_thread( request_fd
, current
->process
)))
1059 if (req
->suspend
) thread
->suspend
++;
1060 reply
->tid
= get_thread_id( thread
);
1061 if ((reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
)))
1063 /* thread object will be released when the thread gets killed */
1066 kill_thread( thread
, 1 );
1070 /* initialize a new thread */
1071 DECL_HANDLER(init_thread
)
1073 struct process
*process
= current
->process
;
1074 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
1075 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
1077 if (current
->reply_fd
) /* already initialised */
1079 set_error( STATUS_INVALID_PARAMETER
);
1083 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1085 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1087 if (!current
->reply_fd
) goto error
;
1091 set_error( STATUS_TOO_MANY_OPENED_FILES
); /* most likely reason */
1094 if (!(current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 )))
1097 if (!is_valid_address(req
->teb
) || !is_valid_address(req
->peb
) || !is_valid_address(req
->ldt_copy
))
1099 set_error( STATUS_INVALID_PARAMETER
);
1103 current
->unix_pid
= req
->unix_pid
;
1104 current
->unix_tid
= req
->unix_tid
;
1105 current
->teb
= req
->teb
;
1107 if (!process
->peb
) /* first thread, initialize the process too */
1109 process
->unix_pid
= current
->unix_pid
;
1110 process
->peb
= req
->peb
;
1111 process
->ldt_copy
= req
->ldt_copy
;
1112 reply
->info_size
= init_process( current
);
1116 if (process
->unix_pid
!= current
->unix_pid
)
1117 process
->unix_pid
= -1; /* can happen with linuxthreads */
1118 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
1119 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
1121 debug_level
= max( debug_level
, req
->debug_level
);
1123 /* we may have raced with a SetThreadPriority call */
1124 if (current
->priority
!= THREAD_PRIORITY_NORMAL
)
1125 set_thread_priority( current
->unix_tid
, current
->priority
);
1127 reply
->pid
= get_process_id( process
);
1128 reply
->tid
= get_thread_id( current
);
1129 reply
->version
= SERVER_PROTOCOL_VERSION
;
1130 reply
->server_start
= server_start_time
;
1134 if (reply_fd
!= -1) close( reply_fd
);
1135 if (wait_fd
!= -1) close( wait_fd
);
1138 /* terminate a thread */
1139 DECL_HANDLER(terminate_thread
)
1141 struct thread
*thread
;
1145 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1147 thread
->exit_code
= req
->exit_code
;
1148 if (thread
!= current
) kill_thread( thread
, 1 );
1152 reply
->last
= (thread
->process
->running_threads
== 1);
1154 release_object( thread
);
1158 /* open a handle to a thread */
1159 DECL_HANDLER(open_thread
)
1161 struct thread
*thread
= get_thread_from_id( req
->tid
);
1166 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1167 release_object( thread
);
1171 /* fetch information about a thread */
1172 DECL_HANDLER(get_thread_info
)
1174 struct thread
*thread
;
1175 obj_handle_t handle
= req
->handle
;
1177 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1178 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
1182 reply
->pid
= get_process_id( thread
->process
);
1183 reply
->tid
= get_thread_id( thread
);
1184 reply
->teb
= thread
->teb
;
1185 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1186 reply
->priority
= thread
->priority
;
1187 reply
->affinity
= thread
->affinity
;
1188 reply
->creation_time
= thread
->creation_time
;
1189 reply
->exit_time
= thread
->exit_time
;
1190 reply
->last
= thread
->process
->running_threads
== 1;
1192 release_object( thread
);
1196 /* set information about a thread */
1197 DECL_HANDLER(set_thread_info
)
1199 struct thread
*thread
;
1201 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1203 set_thread_info( thread
, req
);
1204 release_object( thread
);
1208 /* suspend a thread */
1209 DECL_HANDLER(suspend_thread
)
1211 struct thread
*thread
;
1213 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1215 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1216 else reply
->count
= suspend_thread( thread
);
1217 release_object( thread
);
1221 /* resume a thread */
1222 DECL_HANDLER(resume_thread
)
1224 struct thread
*thread
;
1226 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1228 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1229 else reply
->count
= resume_thread( thread
);
1230 release_object( thread
);
1234 /* select on a handle list */
1235 DECL_HANDLER(select
)
1237 struct thread_apc
*apc
;
1239 const apc_result_t
*result
= get_req_data();
1240 const obj_handle_t
*handles
= (const obj_handle_t
*)(result
+ 1);
1242 if (get_req_data_size() < sizeof(*result
))
1244 set_error( STATUS_INVALID_PARAMETER
);
1247 count
= (get_req_data_size() - sizeof(*result
)) / sizeof(obj_handle_t
);
1249 /* first store results of previous apc */
1252 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1253 0, &thread_apc_ops
))) return;
1254 apc
->result
= *result
;
1256 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1258 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1259 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1260 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1261 apc
->result
.create_thread
.handle
= handle
;
1262 clear_error(); /* ignore errors from the above calls */
1264 else if (apc
->result
.type
== APC_ASYNC_IO
)
1266 if (apc
->owner
) async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
1268 wake_up( &apc
->obj
, 0 );
1269 close_handle( current
->process
, req
->prev_apc
);
1270 release_object( apc
);
1273 reply
->timeout
= select_on( count
, req
->cookie
, handles
, req
->flags
, req
->timeout
, req
->signal
);
1275 if (get_error() == STATUS_USER_APC
)
1279 if (!(apc
= thread_dequeue_apc( current
, !(req
->flags
& SELECT_ALERTABLE
) )))
1281 /* Optimization: ignore APC_NONE calls, they are only used to
1282 * wake up a thread, but since we got here the thread woke up already.
1284 if (apc
->call
.type
!= APC_NONE
)
1286 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1287 reply
->call
= apc
->call
;
1288 release_object( apc
);
1292 wake_up( &apc
->obj
, 0 );
1293 release_object( apc
);
1298 /* queue an APC for a thread or process */
1299 DECL_HANDLER(queue_apc
)
1301 struct thread
*thread
= NULL
;
1302 struct process
*process
= NULL
;
1303 struct thread_apc
*apc
;
1305 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1307 switch (apc
->call
.type
)
1311 thread
= get_thread_from_handle( req
->thread
, THREAD_SET_CONTEXT
);
1313 case APC_VIRTUAL_ALLOC
:
1314 case APC_VIRTUAL_FREE
:
1315 case APC_VIRTUAL_PROTECT
:
1316 case APC_VIRTUAL_FLUSH
:
1317 case APC_VIRTUAL_LOCK
:
1318 case APC_VIRTUAL_UNLOCK
:
1319 case APC_UNMAP_VIEW
:
1320 process
= get_process_from_handle( req
->process
, PROCESS_VM_OPERATION
);
1322 case APC_VIRTUAL_QUERY
:
1323 process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
);
1326 process
= get_process_from_handle( req
->process
, PROCESS_VM_OPERATION
);
1327 if (process
&& process
!= current
->process
)
1329 /* duplicate the handle into the target process */
1330 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1331 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1332 if (handle
) apc
->call
.map_view
.handle
= handle
;
1335 release_object( process
);
1340 case APC_CREATE_THREAD
:
1341 process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
);
1344 set_error( STATUS_INVALID_PARAMETER
);
1350 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1351 release_object( thread
);
1355 reply
->self
= (process
== current
->process
);
1358 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1361 if (queue_apc( process
, NULL
, apc
))
1363 apc
->caller
= (struct thread
*)grab_object( current
);
1364 reply
->handle
= handle
;
1368 close_handle( current
->process
, handle
);
1369 set_error( STATUS_PROCESS_IS_TERMINATING
);
1373 release_object( process
);
1376 release_object( apc
);
1379 /* Get the result of an APC call */
1380 DECL_HANDLER(get_apc_result
)
1382 struct thread_apc
*apc
;
1384 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1385 0, &thread_apc_ops
))) return;
1386 if (!apc
->executed
) set_error( STATUS_PENDING
);
1389 reply
->result
= apc
->result
;
1390 /* close the handle directly to avoid an extra round-trip */
1391 close_handle( current
->process
, req
->handle
);
1393 release_object( apc
);
1396 /* retrieve the current context of a thread */
1397 DECL_HANDLER(get_thread_context
)
1399 struct thread
*thread
;
1402 if (get_reply_max_size() < sizeof(CONTEXT
))
1404 set_error( STATUS_INVALID_PARAMETER
);
1407 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1411 if (thread
!= current
|| !thread
->suspend_context
)
1413 /* not suspended, shouldn't happen */
1414 set_error( STATUS_INVALID_PARAMETER
);
1418 if (thread
->context
== thread
->suspend_context
) thread
->context
= NULL
;
1419 set_reply_data_ptr( thread
->suspend_context
, sizeof(CONTEXT
) );
1420 thread
->suspend_context
= NULL
;
1423 else if (thread
!= current
&& !thread
->context
)
1425 /* thread is not suspended, retry (if it's still running) */
1426 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1427 else set_error( STATUS_PENDING
);
1429 else if ((context
= set_reply_data_size( sizeof(CONTEXT
) )))
1431 unsigned int flags
= get_context_system_regs( req
->flags
);
1433 memset( context
, 0, sizeof(CONTEXT
) );
1434 context
->ContextFlags
= get_context_cpu_flag();
1435 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1436 if (flags
) get_thread_context( thread
, context
, flags
);
1438 reply
->self
= (thread
== current
);
1439 release_object( thread
);
1442 /* set the current context of a thread */
1443 DECL_HANDLER(set_thread_context
)
1445 struct thread
*thread
;
1447 if (get_req_data_size() < sizeof(CONTEXT
))
1449 set_error( STATUS_INVALID_PARAMETER
);
1452 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1456 if (thread
!= current
|| thread
->context
)
1458 /* nested suspend or exception, shouldn't happen */
1459 set_error( STATUS_INVALID_PARAMETER
);
1461 else if ((thread
->suspend_context
= mem_alloc( sizeof(CONTEXT
) )))
1463 memcpy( thread
->suspend_context
, get_req_data(), sizeof(CONTEXT
) );
1464 thread
->context
= thread
->suspend_context
;
1465 if (thread
->debug_break
) break_thread( thread
);
1468 else if (thread
!= current
&& !thread
->context
)
1470 /* thread is not suspended, retry (if it's still running) */
1471 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1472 else set_error( STATUS_PENDING
);
1476 const CONTEXT
*context
= get_req_data();
1477 unsigned int flags
= get_context_system_regs( req
->flags
);
1479 if (flags
) set_thread_context( thread
, context
, flags
);
1480 if (thread
->context
&& !get_error())
1481 copy_context( thread
->context
, context
, req
->flags
& ~flags
);
1483 reply
->self
= (thread
== current
);
1484 release_object( thread
);
1487 /* fetch a selector entry for a thread */
1488 DECL_HANDLER(get_selector_entry
)
1490 struct thread
*thread
;
1491 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1493 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1494 release_object( thread
);