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
57 static const unsigned int supported_cpus
= CPU_FLAG(CPU_x86
);
58 #elif defined(__x86_64__)
59 static const unsigned int supported_cpus
= CPU_FLAG(CPU_x86_64
) | CPU_FLAG(CPU_x86
);
60 #elif defined(__ALPHA__)
61 static const unsigned int supported_cpus
= CPU_FLAG(CPU_ALPHA
);
62 #elif defined(__powerpc__)
63 static const unsigned int supported_cpus
= CPU_FLAG(CPU_POWERPC
);
64 #elif defined(__sparc__)
65 static const unsigned int supported_cpus
= CPU_FLAG(CPU_SPARC
);
67 #error Unsupported CPU
74 struct thread_wait
*next
; /* next wait structure for this thread */
75 struct thread
*thread
; /* owner thread */
76 int count
; /* count of objects */
78 client_ptr_t cookie
; /* magic cookie to return to client */
80 struct timeout_user
*user
;
81 struct wait_queue_entry queues
[1];
84 /* asynchronous procedure calls */
88 struct object obj
; /* object header */
89 struct list entry
; /* queue linked list */
90 struct thread
*caller
; /* thread that queued this apc */
91 struct object
*owner
; /* object that queued this apc */
92 int executed
; /* has it been executed by the client? */
93 apc_call_t call
; /* call arguments */
94 apc_result_t result
; /* call results once executed */
97 static void dump_thread_apc( struct object
*obj
, int verbose
);
98 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
);
99 static void thread_apc_destroy( struct object
*obj
);
100 static void clear_apc_queue( struct list
*queue
);
102 static const struct object_ops thread_apc_ops
=
104 sizeof(struct thread_apc
), /* size */
105 dump_thread_apc
, /* dump */
106 no_get_type
, /* get_type */
107 add_queue
, /* add_queue */
108 remove_queue
, /* remove_queue */
109 thread_apc_signaled
, /* signaled */
110 no_satisfied
, /* satisfied */
111 no_signal
, /* signal */
112 no_get_fd
, /* get_fd */
113 no_map_access
, /* map_access */
114 default_get_sd
, /* get_sd */
115 default_set_sd
, /* set_sd */
116 no_lookup_name
, /* lookup_name */
117 no_open_file
, /* open_file */
118 no_close_handle
, /* close_handle */
119 thread_apc_destroy
/* destroy */
123 /* thread operations */
125 static void dump_thread( struct object
*obj
, int verbose
);
126 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
127 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
128 static void thread_poll_event( struct fd
*fd
, int event
);
129 static void destroy_thread( struct object
*obj
);
131 static const struct object_ops thread_ops
=
133 sizeof(struct thread
), /* size */
134 dump_thread
, /* dump */
135 no_get_type
, /* get_type */
136 add_queue
, /* add_queue */
137 remove_queue
, /* remove_queue */
138 thread_signaled
, /* signaled */
139 no_satisfied
, /* satisfied */
140 no_signal
, /* signal */
141 no_get_fd
, /* get_fd */
142 thread_map_access
, /* map_access */
143 default_get_sd
, /* get_sd */
144 default_set_sd
, /* set_sd */
145 no_lookup_name
, /* lookup_name */
146 no_open_file
, /* open_file */
147 no_close_handle
, /* close_handle */
148 destroy_thread
/* destroy */
151 static const struct fd_ops thread_fd_ops
=
153 NULL
, /* get_poll_events */
154 thread_poll_event
, /* poll_event */
156 NULL
, /* get_fd_type */
157 NULL
, /* removable */
159 NULL
, /* queue_async */
160 NULL
, /* async_event */
161 NULL
, /* async_terminated */
162 NULL
/* cancel_async */
165 static struct list thread_list
= LIST_INIT(thread_list
);
167 /* initialize the structure for a newly allocated thread */
168 static inline void init_thread_structure( struct thread
*thread
)
172 thread
->unix_pid
= -1; /* not known yet */
173 thread
->unix_tid
= -1; /* not known yet */
174 thread
->context
= NULL
;
175 thread
->suspend_context
= NULL
;
177 thread
->debug_ctx
= NULL
;
178 thread
->debug_event
= NULL
;
179 thread
->debug_break
= 0;
180 thread
->queue
= NULL
;
183 thread
->req_data
= NULL
;
184 thread
->req_toread
= 0;
185 thread
->reply_data
= NULL
;
186 thread
->reply_towrite
= 0;
187 thread
->request_fd
= NULL
;
188 thread
->reply_fd
= NULL
;
189 thread
->wait_fd
= NULL
;
190 thread
->state
= RUNNING
;
191 thread
->exit_code
= 0;
192 thread
->priority
= 0;
193 thread
->affinity
= ~0;
195 thread
->desktop_users
= 0;
196 thread
->token
= NULL
;
198 thread
->creation_time
= current_time
;
199 thread
->exit_time
= 0;
201 list_init( &thread
->mutex_list
);
202 list_init( &thread
->system_apc
);
203 list_init( &thread
->user_apc
);
205 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
206 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
209 /* check if address looks valid for a client-side data structure (TEB etc.) */
210 static inline int is_valid_address( client_ptr_t addr
)
212 return addr
&& !(addr
% sizeof(int));
215 /* create a new thread */
216 struct thread
*create_thread( int fd
, struct process
*process
)
218 struct thread
*thread
;
220 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
222 init_thread_structure( thread
);
224 thread
->process
= (struct process
*)grab_object( process
);
225 thread
->desktop
= process
->desktop
;
226 thread
->affinity
= process
->affinity
;
227 if (!current
) current
= thread
;
229 list_add_head( &thread_list
, &thread
->entry
);
231 if (!(thread
->id
= alloc_ptid( thread
)))
233 release_object( thread
);
236 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
238 release_object( thread
);
242 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
243 add_process_thread( thread
->process
, thread
);
247 /* handle a client event */
248 static void thread_poll_event( struct fd
*fd
, int event
)
250 struct thread
*thread
= get_fd_user( fd
);
251 assert( thread
->obj
.ops
== &thread_ops
);
253 grab_object( thread
);
254 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
255 else if (event
& POLLIN
) read_request( thread
);
256 else if (event
& POLLOUT
) write_reply( thread
);
257 release_object( thread
);
260 /* cleanup everything that is no longer needed by a dead thread */
261 /* used by destroy_thread and kill_thread */
262 static void cleanup_thread( struct thread
*thread
)
266 clear_apc_queue( &thread
->system_apc
);
267 clear_apc_queue( &thread
->user_apc
);
268 free( thread
->req_data
);
269 free( thread
->reply_data
);
270 if (thread
->request_fd
) release_object( thread
->request_fd
);
271 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
272 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
273 free( thread
->suspend_context
);
274 cleanup_clipboard_thread(thread
);
275 destroy_thread_windows( thread
);
276 free_msg_queue( thread
);
277 close_thread_desktop( thread
);
278 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
280 if (thread
->inflight
[i
].client
!= -1)
282 close( thread
->inflight
[i
].server
);
283 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
286 thread
->req_data
= NULL
;
287 thread
->reply_data
= NULL
;
288 thread
->request_fd
= NULL
;
289 thread
->reply_fd
= NULL
;
290 thread
->wait_fd
= NULL
;
291 thread
->context
= NULL
;
292 thread
->suspend_context
= NULL
;
296 /* destroy a thread when its refcount is 0 */
297 static void destroy_thread( struct object
*obj
)
299 struct thread
*thread
= (struct thread
*)obj
;
300 assert( obj
->ops
== &thread_ops
);
302 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
303 list_remove( &thread
->entry
);
304 cleanup_thread( thread
);
305 release_object( thread
->process
);
306 if (thread
->id
) free_ptid( thread
->id
);
307 if (thread
->token
) release_object( thread
->token
);
310 /* dump a thread on stdout for debugging purposes */
311 static void dump_thread( struct object
*obj
, int verbose
)
313 struct thread
*thread
= (struct thread
*)obj
;
314 assert( obj
->ops
== &thread_ops
);
316 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
317 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
320 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
322 struct thread
*mythread
= (struct thread
*)obj
;
323 return (mythread
->state
== TERMINATED
);
326 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
328 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
329 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
330 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
331 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
332 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
335 static void dump_thread_apc( struct object
*obj
, int verbose
)
337 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
338 assert( obj
->ops
== &thread_apc_ops
);
340 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
343 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
)
345 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
346 return apc
->executed
;
349 static void thread_apc_destroy( struct object
*obj
)
351 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
352 if (apc
->caller
) release_object( apc
->caller
);
353 if (apc
->owner
) release_object( apc
->owner
);
356 /* queue an async procedure call */
357 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
359 struct thread_apc
*apc
;
361 if ((apc
= alloc_object( &thread_apc_ops
)))
363 apc
->call
= *call_data
;
367 apc
->result
.type
= APC_NONE
;
368 if (owner
) grab_object( owner
);
373 /* get a thread pointer from a thread id (and increment the refcount) */
374 struct thread
*get_thread_from_id( thread_id_t id
)
376 struct object
*obj
= get_ptid_entry( id
);
378 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
379 set_error( STATUS_INVALID_CID
);
383 /* get a thread from a handle (and increment the refcount) */
384 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
386 return (struct thread
*)get_handle_obj( current
->process
, handle
,
387 access
, &thread_ops
);
390 /* find a thread from a Unix tid */
391 struct thread
*get_thread_from_tid( int tid
)
393 struct thread
*thread
;
395 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
397 if (thread
->unix_tid
== tid
) return thread
;
402 /* find a thread from a Unix pid */
403 struct thread
*get_thread_from_pid( int pid
)
405 struct thread
*thread
;
407 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
409 if (thread
->unix_pid
== pid
) return thread
;
414 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
417 #ifdef HAVE_SCHED_SETAFFINITY
418 if (thread
->unix_tid
!= -1)
425 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
426 if (affinity
& mask
) CPU_SET( i
, &set
);
428 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
431 if (!ret
) thread
->affinity
= affinity
;
435 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
436 #define THREAD_PRIORITY_REALTIME_LOWEST -7
438 /* set all information about a thread */
439 static void set_thread_info( struct thread
*thread
,
440 const struct set_thread_info_request
*req
)
442 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
444 int max
= THREAD_PRIORITY_HIGHEST
;
445 int min
= THREAD_PRIORITY_LOWEST
;
446 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
448 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
449 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
451 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
452 req
->priority
== THREAD_PRIORITY_IDLE
||
453 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
454 thread
->priority
= req
->priority
;
456 set_error( STATUS_INVALID_PARAMETER
);
458 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
460 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
461 set_error( STATUS_INVALID_PARAMETER
);
462 else if (thread
->state
== TERMINATED
)
463 set_error( STATUS_ACCESS_DENIED
);
464 else if (set_thread_affinity( thread
, req
->affinity
))
467 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
468 security_set_thread_token( thread
, req
->token
);
471 /* stop a thread (at the Unix level) */
472 void stop_thread( struct thread
*thread
)
474 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
475 /* can't stop a thread while initialisation is in progress */
476 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
479 /* suspend a thread */
480 static int suspend_thread( struct thread
*thread
)
482 int old_count
= thread
->suspend
;
483 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
485 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
487 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
491 /* resume a thread */
492 static int resume_thread( struct thread
*thread
)
494 int old_count
= thread
->suspend
;
495 if (thread
->suspend
> 0)
497 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
502 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
503 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
507 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
511 /* remove a thread from an object wait queue */
512 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
514 list_remove( &entry
->entry
);
515 release_object( obj
);
519 static void end_wait( struct thread
*thread
)
521 struct thread_wait
*wait
= thread
->wait
;
522 struct wait_queue_entry
*entry
;
526 thread
->wait
= wait
->next
;
527 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
528 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
529 if (wait
->user
) remove_timeout_user( wait
->user
);
533 /* build the thread wait structure */
534 static int wait_on( unsigned int count
, struct object
*objects
[], int flags
, timeout_t timeout
)
536 struct thread_wait
*wait
;
537 struct wait_queue_entry
*entry
;
540 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
541 wait
->next
= current
->wait
;
542 wait
->thread
= current
;
546 wait
->timeout
= timeout
;
547 current
->wait
= wait
;
549 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
551 struct object
*obj
= objects
[i
];
552 entry
->thread
= current
;
553 if (!obj
->ops
->add_queue( obj
, entry
))
563 /* check if the thread waiting condition is satisfied */
564 static int check_wait( struct thread
*thread
)
567 struct thread_wait
*wait
= thread
->wait
;
568 struct wait_queue_entry
*entry
= wait
->queues
;
572 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
573 return STATUS_USER_APC
;
575 /* Suspended threads may not acquire locks, but they can run system APCs */
576 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
578 if (wait
->flags
& SELECT_ALL
)
581 /* Note: we must check them all anyway, as some objects may
582 * want to do something when signaled, even if others are not */
583 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
584 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
585 if (not_ok
) goto other_checks
;
586 /* Wait satisfied: tell it to all objects */
588 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
589 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
590 signaled
= STATUS_ABANDONED_WAIT_0
;
595 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
597 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
598 /* Wait satisfied: tell it to the object */
600 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
601 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
607 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
608 if (wait
->timeout
<= current_time
) return STATUS_TIMEOUT
;
612 /* send the wakeup signal to a thread */
613 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
615 struct wake_up_reply reply
;
618 memset( &reply
, 0, sizeof(reply
) );
619 reply
.cookie
= cookie
;
620 reply
.signaled
= signaled
;
621 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
624 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
625 else if (errno
== EPIPE
)
626 kill_thread( thread
, 0 ); /* normal death */
628 fatal_protocol_perror( thread
, "write" );
632 /* attempt to wake up a thread */
633 /* return >0 if OK, 0 if the wait condition is still not satisfied */
634 int wake_thread( struct thread
*thread
)
639 for (count
= 0; thread
->wait
; count
++)
641 if ((signaled
= check_wait( thread
)) == -1) break;
643 cookie
= thread
->wait
->cookie
;
644 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
646 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
652 /* thread wait timeout */
653 static void thread_timeout( void *ptr
)
655 struct thread_wait
*wait
= ptr
;
656 struct thread
*thread
= wait
->thread
;
657 client_ptr_t cookie
= wait
->cookie
;
660 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
661 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
663 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
665 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
666 /* check if other objects have become signaled in the meantime */
667 wake_thread( thread
);
670 /* try signaling an event flag, a semaphore or a mutex */
671 static int signal_object( obj_handle_t handle
)
676 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
679 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
680 release_object( obj
);
685 /* select on a list of handles */
686 static timeout_t
select_on( unsigned int count
, client_ptr_t cookie
, const obj_handle_t
*handles
,
687 int flags
, timeout_t timeout
, obj_handle_t signal_obj
)
691 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
693 if (timeout
<= 0) timeout
= current_time
- timeout
;
695 if (count
> MAXIMUM_WAIT_OBJECTS
)
697 set_error( STATUS_INVALID_PARAMETER
);
700 for (i
= 0; i
< count
; i
++)
702 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
706 if (i
< count
) goto done
;
707 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
709 /* signal the object */
712 if (!signal_object( signal_obj
))
717 /* check if we woke ourselves up */
718 if (!current
->wait
) goto done
;
721 if ((ret
= check_wait( current
)) != -1)
723 /* condition is already satisfied */
729 /* now we need to wait */
730 if (current
->wait
->timeout
!= TIMEOUT_INFINITE
)
732 if (!(current
->wait
->user
= add_timeout_user( current
->wait
->timeout
,
733 thread_timeout
, current
->wait
)))
739 current
->wait
->cookie
= cookie
;
740 set_error( STATUS_PENDING
);
743 while (i
> 0) release_object( objects
[--i
] );
747 /* attempt to wake threads sleeping on the object wait queue */
748 void wake_up( struct object
*obj
, int max
)
752 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
754 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
755 if (!wake_thread( entry
->thread
)) continue;
756 if (max
&& !--max
) break;
757 /* restart at the head of the list since a wake up can change the object wait queue */
758 ptr
= &obj
->wait_queue
;
762 /* return the apc queue to use for a given apc type */
763 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
770 return &thread
->user_apc
;
772 return &thread
->system_apc
;
776 /* check if thread is currently waiting for a (system) apc */
777 static inline int is_in_apc_wait( struct thread
*thread
)
779 return (thread
->process
->suspend
|| thread
->suspend
||
780 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
783 /* queue an existing APC to a given thread */
784 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
788 if (!thread
) /* find a suitable thread inside the process */
790 struct thread
*candidate
;
792 /* first try to find a waiting thread */
793 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
795 if (candidate
->state
== TERMINATED
) continue;
796 if (is_in_apc_wait( candidate
))
804 /* then use the first one that accepts a signal */
805 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
807 if (send_thread_signal( candidate
, SIGUSR1
))
814 if (!thread
) return 0; /* nothing found */
815 queue
= get_apc_queue( thread
, apc
->call
.type
);
819 if (thread
->state
== TERMINATED
) return 0;
820 queue
= get_apc_queue( thread
, apc
->call
.type
);
821 /* send signal for system APCs if needed */
822 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
824 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
826 /* cancel a possible previous APC with the same owner */
827 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
831 list_add_tail( queue
, &apc
->entry
);
832 if (!list_prev( queue
, &apc
->entry
)) /* first one */
833 wake_thread( thread
);
838 /* queue an async procedure call */
839 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
841 struct thread_apc
*apc
;
844 if ((apc
= create_apc( owner
, call_data
)))
846 ret
= queue_apc( NULL
, thread
, apc
);
847 release_object( apc
);
852 /* cancel the async procedure call owned by a specific object */
853 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
855 struct thread_apc
*apc
;
856 struct list
*queue
= get_apc_queue( thread
, type
);
858 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
860 if (apc
->owner
!= owner
) continue;
861 list_remove( &apc
->entry
);
863 wake_up( &apc
->obj
, 0 );
864 release_object( apc
);
869 /* remove the head apc from the queue; the returned object must be released by the caller */
870 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
872 struct thread_apc
*apc
= NULL
;
873 struct list
*ptr
= list_head( &thread
->system_apc
);
875 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
878 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
884 /* clear an APC queue, cancelling all the APCs on it */
885 static void clear_apc_queue( struct list
*queue
)
889 while ((ptr
= list_head( queue
)))
891 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
892 list_remove( &apc
->entry
);
894 wake_up( &apc
->obj
, 0 );
895 release_object( apc
);
899 /* add an fd to the inflight list */
900 /* return list index, or -1 on error */
901 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
905 if (server
== -1) return -1;
912 /* first check if we already have an entry for this fd */
913 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
914 if (thread
->inflight
[i
].client
== client
)
916 close( thread
->inflight
[i
].server
);
917 thread
->inflight
[i
].server
= server
;
921 /* now find a free spot to store it */
922 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
923 if (thread
->inflight
[i
].client
== -1)
925 thread
->inflight
[i
].client
= client
;
926 thread
->inflight
[i
].server
= server
;
932 /* get an inflight fd and purge it from the list */
933 /* the fd must be closed when no longer used */
934 int thread_get_inflight_fd( struct thread
*thread
, int client
)
938 if (client
== -1) return -1;
942 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
944 if (thread
->inflight
[i
].client
== client
)
946 ret
= thread
->inflight
[i
].server
;
947 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
951 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
955 /* kill a thread on the spot */
956 void kill_thread( struct thread
*thread
, int violent_death
)
958 if (thread
->state
== TERMINATED
) return; /* already killed */
959 thread
->state
= TERMINATED
;
960 thread
->exit_time
= current_time
;
961 if (current
== thread
) current
= NULL
;
963 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
964 thread
->id
, thread
->exit_code
);
967 while (thread
->wait
) end_wait( thread
);
968 send_thread_wakeup( thread
, 0, STATUS_PENDING
);
969 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
972 kill_console_processes( thread
, 0 );
973 debug_exit_thread( thread
);
974 abandon_mutexes( thread
);
975 wake_up( &thread
->obj
, 0 );
976 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
977 cleanup_thread( thread
);
978 remove_process_thread( thread
->process
, thread
);
979 release_object( thread
);
982 /* copy parts of a context structure */
983 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
985 assert( to
->cpu
== from
->cpu
);
987 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
988 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
989 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
990 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
991 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
992 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
995 /* return the context flags that correspond to system regs */
996 /* (system regs are the ones we can't access on the client side) */
997 static unsigned int get_context_system_regs( enum cpu_type cpu
)
1001 case CPU_x86
: return SERVER_CTX_DEBUG_REGISTERS
;
1002 case CPU_x86_64
: return SERVER_CTX_DEBUG_REGISTERS
;
1003 case CPU_ALPHA
: return 0;
1004 case CPU_POWERPC
: return 0;
1005 case CPU_SPARC
: return 0;
1010 /* trigger a breakpoint event in a given thread */
1011 void break_thread( struct thread
*thread
)
1015 assert( thread
->context
);
1017 memset( &data
, 0, sizeof(data
) );
1018 data
.exception
.first
= 1;
1019 data
.exception
.exc_code
= STATUS_BREAKPOINT
;
1020 data
.exception
.flags
= EXCEPTION_CONTINUABLE
;
1021 switch (thread
->context
->cpu
)
1024 data
.exception
.address
= thread
->context
->ctl
.i386_regs
.eip
;
1027 data
.exception
.address
= thread
->context
->ctl
.x86_64_regs
.rip
;
1030 data
.exception
.address
= thread
->context
->ctl
.alpha_regs
.fir
;
1033 data
.exception
.address
= thread
->context
->ctl
.powerpc_regs
.iar
;
1036 data
.exception
.address
= thread
->context
->ctl
.sparc_regs
.pc
;
1039 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
1040 thread
->debug_break
= 0;
1043 /* take a snapshot of currently running threads */
1044 struct thread_snapshot
*thread_snap( int *count
)
1046 struct thread_snapshot
*snapshot
, *ptr
;
1047 struct thread
*thread
;
1050 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1051 if (thread
->state
!= TERMINATED
) total
++;
1052 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
1054 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1056 if (thread
->state
== TERMINATED
) continue;
1057 ptr
->thread
= thread
;
1058 ptr
->count
= thread
->obj
.refcount
;
1059 ptr
->priority
= thread
->priority
;
1060 grab_object( thread
);
1067 /* gets the current impersonation token */
1068 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1071 return thread
->token
;
1073 return thread
->process
->token
;
1076 /* create a new thread */
1077 DECL_HANDLER(new_thread
)
1079 struct thread
*thread
;
1080 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1082 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1084 if (request_fd
!= -1) close( request_fd
);
1085 set_error( STATUS_INVALID_HANDLE
);
1089 if ((thread
= create_thread( request_fd
, current
->process
)))
1091 if (req
->suspend
) thread
->suspend
++;
1092 reply
->tid
= get_thread_id( thread
);
1093 if ((reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
)))
1095 /* thread object will be released when the thread gets killed */
1098 kill_thread( thread
, 1 );
1102 /* initialize a new thread */
1103 DECL_HANDLER(init_thread
)
1105 unsigned int prefix_cpu_mask
= get_prefix_cpu_mask();
1106 struct process
*process
= current
->process
;
1107 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
1108 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
1110 if (current
->reply_fd
) /* already initialised */
1112 set_error( STATUS_INVALID_PARAMETER
);
1116 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1118 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1120 if (!current
->reply_fd
) goto error
;
1124 set_error( STATUS_TOO_MANY_OPENED_FILES
); /* most likely reason */
1127 if (!(current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 )))
1130 if (!is_valid_address(req
->teb
))
1132 set_error( STATUS_INVALID_PARAMETER
);
1136 current
->unix_pid
= req
->unix_pid
;
1137 current
->unix_tid
= req
->unix_tid
;
1138 current
->teb
= req
->teb
;
1140 if (!process
->peb
) /* first thread, initialize the process too */
1142 if (!CPU_FLAG(req
->cpu
) || !(supported_cpus
& prefix_cpu_mask
& CPU_FLAG(req
->cpu
)))
1144 if (!(supported_cpus
& CPU_64BIT_MASK
))
1145 set_error( STATUS_NOT_SUPPORTED
);
1147 set_error( STATUS_NOT_REGISTRY_FILE
); /* server supports it but not the prefix */
1150 process
->unix_pid
= current
->unix_pid
;
1151 process
->peb
= req
->entry
;
1152 process
->cpu
= req
->cpu
;
1153 reply
->info_size
= init_process( current
);
1157 if (req
->cpu
!= process
->cpu
)
1159 set_error( STATUS_INVALID_PARAMETER
);
1162 if (process
->unix_pid
!= current
->unix_pid
)
1163 process
->unix_pid
= -1; /* can happen with linuxthreads */
1164 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
1165 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, &req
->entry
);
1167 debug_level
= max( debug_level
, req
->debug_level
);
1168 set_thread_affinity( current
, current
->affinity
);
1170 reply
->pid
= get_process_id( process
);
1171 reply
->tid
= get_thread_id( current
);
1172 reply
->version
= SERVER_PROTOCOL_VERSION
;
1173 reply
->server_start
= server_start_time
;
1174 reply
->all_cpus
= supported_cpus
& prefix_cpu_mask
;
1178 if (reply_fd
!= -1) close( reply_fd
);
1179 if (wait_fd
!= -1) close( wait_fd
);
1182 /* terminate a thread */
1183 DECL_HANDLER(terminate_thread
)
1185 struct thread
*thread
;
1189 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1191 thread
->exit_code
= req
->exit_code
;
1192 if (thread
!= current
) kill_thread( thread
, 1 );
1196 reply
->last
= (thread
->process
->running_threads
== 1);
1198 release_object( thread
);
1202 /* open a handle to a thread */
1203 DECL_HANDLER(open_thread
)
1205 struct thread
*thread
= get_thread_from_id( req
->tid
);
1210 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1211 release_object( thread
);
1215 /* fetch information about a thread */
1216 DECL_HANDLER(get_thread_info
)
1218 struct thread
*thread
;
1219 obj_handle_t handle
= req
->handle
;
1221 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1222 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
1226 reply
->pid
= get_process_id( thread
->process
);
1227 reply
->tid
= get_thread_id( thread
);
1228 reply
->teb
= thread
->teb
;
1229 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1230 reply
->priority
= thread
->priority
;
1231 reply
->affinity
= thread
->affinity
;
1232 reply
->creation_time
= thread
->creation_time
;
1233 reply
->exit_time
= thread
->exit_time
;
1234 reply
->last
= thread
->process
->running_threads
== 1;
1236 release_object( thread
);
1240 /* set information about a thread */
1241 DECL_HANDLER(set_thread_info
)
1243 struct thread
*thread
;
1245 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1247 set_thread_info( thread
, req
);
1248 release_object( thread
);
1252 /* suspend a thread */
1253 DECL_HANDLER(suspend_thread
)
1255 struct thread
*thread
;
1257 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1259 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1260 else reply
->count
= suspend_thread( thread
);
1261 release_object( thread
);
1265 /* resume a thread */
1266 DECL_HANDLER(resume_thread
)
1268 struct thread
*thread
;
1270 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1272 reply
->count
= resume_thread( thread
);
1273 release_object( thread
);
1277 /* select on a handle list */
1278 DECL_HANDLER(select
)
1280 struct thread_apc
*apc
;
1282 const apc_result_t
*result
= get_req_data();
1283 const obj_handle_t
*handles
= (const obj_handle_t
*)(result
+ 1);
1285 if (get_req_data_size() < sizeof(*result
))
1287 set_error( STATUS_INVALID_PARAMETER
);
1290 count
= (get_req_data_size() - sizeof(*result
)) / sizeof(obj_handle_t
);
1292 /* first store results of previous apc */
1295 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1296 0, &thread_apc_ops
))) return;
1297 apc
->result
= *result
;
1299 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1301 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1302 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1303 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1304 apc
->result
.create_thread
.handle
= handle
;
1305 clear_error(); /* ignore errors from the above calls */
1307 else if (apc
->result
.type
== APC_ASYNC_IO
)
1310 async_set_result( apc
->owner
, apc
->result
.async_io
.status
,
1311 apc
->result
.async_io
.total
, apc
->result
.async_io
.apc
);
1313 wake_up( &apc
->obj
, 0 );
1314 close_handle( current
->process
, req
->prev_apc
);
1315 release_object( apc
);
1318 reply
->timeout
= select_on( count
, req
->cookie
, handles
, req
->flags
, req
->timeout
, req
->signal
);
1320 if (get_error() == STATUS_USER_APC
)
1324 if (!(apc
= thread_dequeue_apc( current
, !(req
->flags
& SELECT_ALERTABLE
) )))
1326 /* Optimization: ignore APC_NONE calls, they are only used to
1327 * wake up a thread, but since we got here the thread woke up already.
1329 if (apc
->call
.type
!= APC_NONE
)
1331 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1332 reply
->call
= apc
->call
;
1333 release_object( apc
);
1337 wake_up( &apc
->obj
, 0 );
1338 release_object( apc
);
1343 /* queue an APC for a thread or process */
1344 DECL_HANDLER(queue_apc
)
1346 struct thread
*thread
= NULL
;
1347 struct process
*process
= NULL
;
1348 struct thread_apc
*apc
;
1350 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1352 switch (apc
->call
.type
)
1356 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1358 case APC_VIRTUAL_ALLOC
:
1359 case APC_VIRTUAL_FREE
:
1360 case APC_VIRTUAL_PROTECT
:
1361 case APC_VIRTUAL_FLUSH
:
1362 case APC_VIRTUAL_LOCK
:
1363 case APC_VIRTUAL_UNLOCK
:
1364 case APC_UNMAP_VIEW
:
1365 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1367 case APC_VIRTUAL_QUERY
:
1368 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1371 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1372 if (process
&& process
!= current
->process
)
1374 /* duplicate the handle into the target process */
1375 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1376 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1377 if (handle
) apc
->call
.map_view
.handle
= handle
;
1380 release_object( process
);
1385 case APC_CREATE_THREAD
:
1386 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1389 set_error( STATUS_INVALID_PARAMETER
);
1395 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1396 release_object( thread
);
1400 reply
->self
= (process
== current
->process
);
1403 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1406 if (queue_apc( process
, NULL
, apc
))
1408 apc
->caller
= (struct thread
*)grab_object( current
);
1409 reply
->handle
= handle
;
1413 close_handle( current
->process
, handle
);
1414 set_error( STATUS_PROCESS_IS_TERMINATING
);
1418 release_object( process
);
1421 release_object( apc
);
1424 /* Get the result of an APC call */
1425 DECL_HANDLER(get_apc_result
)
1427 struct thread_apc
*apc
;
1429 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1430 0, &thread_apc_ops
))) return;
1431 if (!apc
->executed
) set_error( STATUS_PENDING
);
1434 reply
->result
= apc
->result
;
1435 /* close the handle directly to avoid an extra round-trip */
1436 close_handle( current
->process
, req
->handle
);
1438 release_object( apc
);
1441 /* retrieve the current context of a thread */
1442 DECL_HANDLER(get_thread_context
)
1444 struct thread
*thread
;
1447 if (get_reply_max_size() < sizeof(context_t
))
1449 set_error( STATUS_INVALID_PARAMETER
);
1452 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1456 if (thread
!= current
|| !thread
->suspend_context
)
1458 /* not suspended, shouldn't happen */
1459 set_error( STATUS_INVALID_PARAMETER
);
1463 if (thread
->context
== thread
->suspend_context
) thread
->context
= NULL
;
1464 set_reply_data_ptr( thread
->suspend_context
, sizeof(context_t
) );
1465 thread
->suspend_context
= NULL
;
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
);
1474 else if ((context
= set_reply_data_size( sizeof(context_t
) )))
1476 unsigned int flags
= get_context_system_regs( thread
->process
->cpu
);
1478 memset( context
, 0, sizeof(context_t
) );
1479 context
->cpu
= thread
->process
->cpu
;
1480 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1481 if (flags
) get_thread_context( thread
, context
, flags
);
1483 reply
->self
= (thread
== current
);
1484 release_object( thread
);
1487 /* set the current context of a thread */
1488 DECL_HANDLER(set_thread_context
)
1490 struct thread
*thread
;
1491 const context_t
*context
= get_req_data();
1493 if (get_req_data_size() < sizeof(context_t
))
1495 set_error( STATUS_INVALID_PARAMETER
);
1498 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1502 if (thread
!= current
|| thread
->context
|| context
->cpu
!= thread
->process
->cpu
)
1504 /* nested suspend or exception, shouldn't happen */
1505 set_error( STATUS_INVALID_PARAMETER
);
1507 else if ((thread
->suspend_context
= mem_alloc( sizeof(context_t
) )))
1509 memcpy( thread
->suspend_context
, get_req_data(), sizeof(context_t
) );
1510 thread
->context
= thread
->suspend_context
;
1511 if (thread
->debug_break
) break_thread( thread
);
1514 else if (thread
!= current
&& !thread
->context
)
1516 /* thread is not suspended, retry (if it's still running) */
1517 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1518 else set_error( STATUS_PENDING
);
1520 else if (context
->cpu
== thread
->process
->cpu
)
1522 unsigned int system_flags
= get_context_system_regs(context
->cpu
) & context
->flags
;
1523 unsigned int client_flags
= context
->flags
& ~system_flags
;
1525 if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1526 if (thread
->context
&& !get_error()) copy_context( thread
->context
, context
, client_flags
);
1528 else set_error( STATUS_INVALID_PARAMETER
);
1530 reply
->self
= (thread
== current
);
1531 release_object( thread
);
1534 /* fetch a selector entry for a thread */
1535 DECL_HANDLER(get_selector_entry
)
1537 struct thread
*thread
;
1538 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1540 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1541 release_object( thread
);