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(__powerpc__)
61 static const unsigned int supported_cpus
= CPU_FLAG(CPU_POWERPC
);
62 #elif defined(__arm__)
63 static const unsigned int supported_cpus
= CPU_FLAG(CPU_ARM
);
64 #elif defined(__aarch64__)
65 static const unsigned int supported_cpus
= CPU_FLAG(CPU_ARM64
);
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 */
79 enum select_op select
;
80 client_ptr_t key
; /* wait key for keyed events */
81 client_ptr_t cookie
; /* magic cookie to return to client */
83 struct timeout_user
*user
;
84 struct wait_queue_entry queues
[1];
87 /* asynchronous procedure calls */
91 struct object obj
; /* object header */
92 struct list entry
; /* queue linked list */
93 struct thread
*caller
; /* thread that queued this apc */
94 struct object
*owner
; /* object that queued this apc */
95 int executed
; /* has it been executed by the client? */
96 apc_call_t call
; /* call arguments */
97 apc_result_t result
; /* call results once executed */
100 static void dump_thread_apc( struct object
*obj
, int verbose
);
101 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
102 static void thread_apc_destroy( struct object
*obj
);
103 static void clear_apc_queue( struct list
*queue
);
105 static const struct object_ops thread_apc_ops
=
107 sizeof(struct thread_apc
), /* size */
108 dump_thread_apc
, /* dump */
109 no_get_type
, /* get_type */
110 add_queue
, /* add_queue */
111 remove_queue
, /* remove_queue */
112 thread_apc_signaled
, /* signaled */
113 no_satisfied
, /* satisfied */
114 no_signal
, /* signal */
115 no_get_fd
, /* get_fd */
116 no_map_access
, /* map_access */
117 default_get_sd
, /* get_sd */
118 default_set_sd
, /* set_sd */
119 no_lookup_name
, /* lookup_name */
120 no_open_file
, /* open_file */
121 no_close_handle
, /* close_handle */
122 thread_apc_destroy
/* destroy */
126 /* thread operations */
128 static void dump_thread( struct object
*obj
, int verbose
);
129 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
130 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
131 static void thread_poll_event( struct fd
*fd
, int event
);
132 static void destroy_thread( struct object
*obj
);
134 static const struct object_ops thread_ops
=
136 sizeof(struct thread
), /* size */
137 dump_thread
, /* dump */
138 no_get_type
, /* get_type */
139 add_queue
, /* add_queue */
140 remove_queue
, /* remove_queue */
141 thread_signaled
, /* signaled */
142 no_satisfied
, /* satisfied */
143 no_signal
, /* signal */
144 no_get_fd
, /* get_fd */
145 thread_map_access
, /* map_access */
146 default_get_sd
, /* get_sd */
147 default_set_sd
, /* set_sd */
148 no_lookup_name
, /* lookup_name */
149 no_open_file
, /* open_file */
150 no_close_handle
, /* close_handle */
151 destroy_thread
/* destroy */
154 static const struct fd_ops thread_fd_ops
=
156 NULL
, /* get_poll_events */
157 thread_poll_event
, /* poll_event */
159 NULL
, /* get_fd_type */
161 NULL
, /* queue_async */
162 NULL
, /* reselect_async */
163 NULL
/* cancel_async */
166 static struct list thread_list
= LIST_INIT(thread_list
);
168 /* initialize the structure for a newly allocated thread */
169 static inline void init_thread_structure( struct thread
*thread
)
173 thread
->unix_pid
= -1; /* not known yet */
174 thread
->unix_tid
= -1; /* not known yet */
175 thread
->context
= NULL
;
176 thread
->suspend_context
= NULL
;
178 thread
->debug_ctx
= NULL
;
179 thread
->debug_event
= NULL
;
180 thread
->debug_break
= 0;
181 thread
->queue
= NULL
;
184 thread
->req_data
= NULL
;
185 thread
->req_toread
= 0;
186 thread
->reply_data
= NULL
;
187 thread
->reply_towrite
= 0;
188 thread
->request_fd
= NULL
;
189 thread
->reply_fd
= NULL
;
190 thread
->wait_fd
= NULL
;
191 thread
->state
= RUNNING
;
192 thread
->exit_code
= 0;
193 thread
->priority
= 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 (process
->is_terminating
)
223 set_error( STATUS_PROCESS_IS_TERMINATING
);
227 if (!(thread
= alloc_object( &thread_ops
)))
233 init_thread_structure( thread
);
235 thread
->process
= (struct process
*)grab_object( process
);
236 thread
->desktop
= process
->desktop
;
237 thread
->affinity
= process
->affinity
;
238 if (!current
) current
= thread
;
240 list_add_head( &thread_list
, &thread
->entry
);
242 if (!(thread
->id
= alloc_ptid( thread
)))
245 release_object( thread
);
248 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
250 release_object( thread
);
254 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
255 add_process_thread( thread
->process
, thread
);
259 /* handle a client event */
260 static void thread_poll_event( struct fd
*fd
, int event
)
262 struct thread
*thread
= get_fd_user( fd
);
263 assert( thread
->obj
.ops
== &thread_ops
);
265 grab_object( thread
);
266 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
267 else if (event
& POLLIN
) read_request( thread
);
268 else if (event
& POLLOUT
) write_reply( thread
);
269 release_object( thread
);
272 /* cleanup everything that is no longer needed by a dead thread */
273 /* used by destroy_thread and kill_thread */
274 static void cleanup_thread( struct thread
*thread
)
278 clear_apc_queue( &thread
->system_apc
);
279 clear_apc_queue( &thread
->user_apc
);
280 free( thread
->req_data
);
281 free( thread
->reply_data
);
282 if (thread
->request_fd
) release_object( thread
->request_fd
);
283 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
284 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
285 free( thread
->suspend_context
);
286 cleanup_clipboard_thread(thread
);
287 destroy_thread_windows( thread
);
288 free_msg_queue( thread
);
289 close_thread_desktop( thread
);
290 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
292 if (thread
->inflight
[i
].client
!= -1)
294 close( thread
->inflight
[i
].server
);
295 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
298 thread
->req_data
= NULL
;
299 thread
->reply_data
= NULL
;
300 thread
->request_fd
= NULL
;
301 thread
->reply_fd
= NULL
;
302 thread
->wait_fd
= NULL
;
303 thread
->context
= NULL
;
304 thread
->suspend_context
= NULL
;
308 /* destroy a thread when its refcount is 0 */
309 static void destroy_thread( struct object
*obj
)
311 struct thread
*thread
= (struct thread
*)obj
;
312 assert( obj
->ops
== &thread_ops
);
314 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
315 list_remove( &thread
->entry
);
316 cleanup_thread( thread
);
317 release_object( thread
->process
);
318 if (thread
->id
) free_ptid( thread
->id
);
319 if (thread
->token
) release_object( thread
->token
);
322 /* dump a thread on stdout for debugging purposes */
323 static void dump_thread( struct object
*obj
, int verbose
)
325 struct thread
*thread
= (struct thread
*)obj
;
326 assert( obj
->ops
== &thread_ops
);
328 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
329 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
332 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
334 struct thread
*mythread
= (struct thread
*)obj
;
335 return (mythread
->state
== TERMINATED
);
338 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
340 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
| THREAD_QUERY_LIMITED_INFORMATION
;
341 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
|
342 THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
| THREAD_SET_LIMITED_INFORMATION
;
343 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_QUERY_LIMITED_INFORMATION
;
344 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
345 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
348 static void dump_thread_apc( struct object
*obj
, int verbose
)
350 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
351 assert( obj
->ops
== &thread_apc_ops
);
353 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
356 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
358 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
359 return apc
->executed
;
362 static void thread_apc_destroy( struct object
*obj
)
364 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
365 if (apc
->caller
) release_object( apc
->caller
);
366 if (apc
->owner
) release_object( apc
->owner
);
369 /* queue an async procedure call */
370 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
372 struct thread_apc
*apc
;
374 if ((apc
= alloc_object( &thread_apc_ops
)))
376 apc
->call
= *call_data
;
380 apc
->result
.type
= APC_NONE
;
381 if (owner
) grab_object( owner
);
386 /* get a thread pointer from a thread id (and increment the refcount) */
387 struct thread
*get_thread_from_id( thread_id_t id
)
389 struct object
*obj
= get_ptid_entry( id
);
391 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
392 set_error( STATUS_INVALID_CID
);
396 /* get a thread from a handle (and increment the refcount) */
397 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
399 return (struct thread
*)get_handle_obj( current
->process
, handle
,
400 access
, &thread_ops
);
403 /* find a thread from a Unix tid */
404 struct thread
*get_thread_from_tid( int tid
)
406 struct thread
*thread
;
408 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
410 if (thread
->unix_tid
== tid
) return thread
;
415 /* find a thread from a Unix pid */
416 struct thread
*get_thread_from_pid( int pid
)
418 struct thread
*thread
;
420 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
422 if (thread
->unix_pid
== pid
) return thread
;
427 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
430 #ifdef HAVE_SCHED_SETAFFINITY
431 if (thread
->unix_tid
!= -1)
438 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
439 if (affinity
& mask
) CPU_SET( i
, &set
);
441 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
444 if (!ret
) thread
->affinity
= affinity
;
448 affinity_t
get_thread_affinity( struct thread
*thread
)
451 #ifdef HAVE_SCHED_SETAFFINITY
452 if (thread
->unix_tid
!= -1)
457 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
458 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
459 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
462 if (!mask
) mask
= ~(affinity_t
)0;
466 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
467 #define THREAD_PRIORITY_REALTIME_LOWEST -7
469 /* set all information about a thread */
470 static void set_thread_info( struct thread
*thread
,
471 const struct set_thread_info_request
*req
)
473 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
475 int max
= THREAD_PRIORITY_HIGHEST
;
476 int min
= THREAD_PRIORITY_LOWEST
;
477 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
479 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
480 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
482 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
483 req
->priority
== THREAD_PRIORITY_IDLE
||
484 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
485 thread
->priority
= req
->priority
;
487 set_error( STATUS_INVALID_PARAMETER
);
489 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
491 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
492 set_error( STATUS_INVALID_PARAMETER
);
493 else if (thread
->state
== TERMINATED
)
494 set_error( STATUS_THREAD_IS_TERMINATING
);
495 else if (set_thread_affinity( thread
, req
->affinity
))
498 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
499 security_set_thread_token( thread
, req
->token
);
502 /* stop a thread (at the Unix level) */
503 void stop_thread( struct thread
*thread
)
505 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
506 /* can't stop a thread while initialisation is in progress */
507 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
510 /* stop a thread if it's supposed to be suspended */
511 void stop_thread_if_suspended( struct thread
*thread
)
513 if (thread
->suspend
+ thread
->process
->suspend
> 0) stop_thread( thread
);
516 /* suspend a thread */
517 static int suspend_thread( struct thread
*thread
)
519 int old_count
= thread
->suspend
;
520 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
522 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
524 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
528 /* resume a thread */
529 static int resume_thread( struct thread
*thread
)
531 int old_count
= thread
->suspend
;
532 if (thread
->suspend
> 0)
534 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
539 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
540 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
544 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
548 /* remove a thread from an object wait queue */
549 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
551 list_remove( &entry
->entry
);
552 release_object( obj
);
555 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
557 return entry
->wait
->thread
;
560 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
562 return entry
->wait
->select
;
565 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
567 return entry
->wait
->key
;
570 void make_wait_abandoned( struct wait_queue_entry
*entry
)
572 entry
->wait
->abandoned
= 1;
576 static void end_wait( struct thread
*thread
)
578 struct thread_wait
*wait
= thread
->wait
;
579 struct wait_queue_entry
*entry
;
583 thread
->wait
= wait
->next
;
584 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
585 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
586 if (wait
->user
) remove_timeout_user( wait
->user
);
590 /* build the thread wait structure */
591 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
592 int flags
, timeout_t timeout
)
594 struct thread_wait
*wait
;
595 struct wait_queue_entry
*entry
;
598 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
599 wait
->next
= current
->wait
;
600 wait
->thread
= current
;
603 wait
->select
= select_op
->op
;
605 wait
->timeout
= timeout
;
607 current
->wait
= wait
;
609 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
611 struct object
*obj
= objects
[i
];
613 if (!obj
->ops
->add_queue( obj
, entry
))
623 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
624 int flags
, timeout_t timeout
)
626 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
630 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
632 for (i
= 0; i
< count
; i
++)
633 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
636 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, timeout
);
638 while (i
> 0) release_object( objects
[--i
] );
642 /* check if the thread waiting condition is satisfied */
643 static int check_wait( struct thread
*thread
)
646 struct thread_wait
*wait
= thread
->wait
;
647 struct wait_queue_entry
*entry
;
651 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
652 return STATUS_USER_APC
;
654 /* Suspended threads may not acquire locks, but they can run system APCs */
655 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
657 if (wait
->select
== SELECT_WAIT_ALL
)
660 /* Note: we must check them all anyway, as some objects may
661 * want to do something when signaled, even if others are not */
662 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
663 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
664 if (not_ok
) goto other_checks
;
665 /* Wait satisfied: tell it to all objects */
666 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
667 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
668 return wait
->abandoned
? STATUS_ABANDONED_WAIT_0
: STATUS_WAIT_0
;
672 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
674 if (!entry
->obj
->ops
->signaled( entry
->obj
, entry
)) continue;
675 /* Wait satisfied: tell it to the object */
676 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
677 if (wait
->abandoned
) i
+= STATUS_ABANDONED_WAIT_0
;
683 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
684 if (wait
->timeout
<= current_time
) return STATUS_TIMEOUT
;
688 /* send the wakeup signal to a thread */
689 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
691 struct wake_up_reply reply
;
694 memset( &reply
, 0, sizeof(reply
) );
695 reply
.cookie
= cookie
;
696 reply
.signaled
= signaled
;
697 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
700 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
701 else if (errno
== EPIPE
)
702 kill_thread( thread
, 0 ); /* normal death */
704 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
708 /* attempt to wake up a thread */
709 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
710 int wake_thread( struct thread
*thread
)
715 for (count
= 0; thread
->wait
; count
++)
717 if ((signaled
= check_wait( thread
)) == -1) break;
719 cookie
= thread
->wait
->cookie
;
720 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
722 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
724 if (!count
) count
= -1;
731 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
732 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
734 struct thread_wait
*wait
= entry
->wait
;
735 struct thread
*thread
= wait
->thread
;
739 if (thread
->wait
!= wait
) return 0; /* not the current wait */
740 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
742 assert( wait
->select
!= SELECT_WAIT_ALL
);
744 signaled
= entry
- wait
->queues
;
745 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
746 if (wait
->abandoned
) signaled
+= STATUS_ABANDONED_WAIT_0
;
748 cookie
= wait
->cookie
;
749 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
752 if (send_thread_wakeup( thread
, cookie
, signaled
) != -1)
753 wake_thread( thread
); /* check other waits too */
758 /* thread wait timeout */
759 static void thread_timeout( void *ptr
)
761 struct thread_wait
*wait
= ptr
;
762 struct thread
*thread
= wait
->thread
;
763 client_ptr_t cookie
= wait
->cookie
;
766 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
767 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
769 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
771 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
772 /* check if other objects have become signaled in the meantime */
773 wake_thread( thread
);
776 /* try signaling an event flag, a semaphore or a mutex */
777 static int signal_object( obj_handle_t handle
)
782 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
785 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
786 release_object( obj
);
791 /* select on a list of handles */
792 static timeout_t
select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
793 int flags
, timeout_t timeout
)
797 struct object
*object
;
799 if (timeout
<= 0) timeout
= current_time
- timeout
;
801 switch (select_op
->op
)
804 if (!wait_on( select_op
, 0, NULL
, flags
, timeout
)) return timeout
;
808 case SELECT_WAIT_ALL
:
809 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
810 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
812 set_error( STATUS_INVALID_PARAMETER
);
815 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, timeout
))
819 case SELECT_SIGNAL_AND_WAIT
:
820 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, timeout
))
822 if (select_op
->signal_and_wait
.signal
)
824 if (!signal_object( select_op
->signal_and_wait
.signal
))
829 /* check if we woke ourselves up */
830 if (!current
->wait
) return timeout
;
834 case SELECT_KEYED_EVENT_WAIT
:
835 case SELECT_KEYED_EVENT_RELEASE
:
836 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
837 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
838 if (!object
) return timeout
;
839 ret
= wait_on( select_op
, 1, &object
, flags
, timeout
);
840 release_object( object
);
841 if (!ret
) return timeout
;
842 current
->wait
->key
= select_op
->keyed_event
.key
;
846 set_error( STATUS_INVALID_PARAMETER
);
850 if ((ret
= check_wait( current
)) != -1)
852 /* condition is already satisfied */
858 /* now we need to wait */
859 if (current
->wait
->timeout
!= TIMEOUT_INFINITE
)
861 if (!(current
->wait
->user
= add_timeout_user( current
->wait
->timeout
,
862 thread_timeout
, current
->wait
)))
868 current
->wait
->cookie
= cookie
;
869 set_error( STATUS_PENDING
);
873 /* attempt to wake threads sleeping on the object wait queue */
874 void wake_up( struct object
*obj
, int max
)
879 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
881 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
882 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
883 if (ret
> 0 && max
&& !--max
) break;
884 /* restart at the head of the list since a wake up can change the object wait queue */
885 ptr
= &obj
->wait_queue
;
889 /* return the apc queue to use for a given apc type */
890 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
897 return &thread
->user_apc
;
899 return &thread
->system_apc
;
903 /* check if thread is currently waiting for a (system) apc */
904 static inline int is_in_apc_wait( struct thread
*thread
)
906 return (thread
->process
->suspend
|| thread
->suspend
||
907 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
910 /* queue an existing APC to a given thread */
911 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
915 if (!thread
) /* find a suitable thread inside the process */
917 struct thread
*candidate
;
919 /* first try to find a waiting thread */
920 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
922 if (candidate
->state
== TERMINATED
) continue;
923 if (is_in_apc_wait( candidate
))
931 /* then use the first one that accepts a signal */
932 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
934 if (send_thread_signal( candidate
, SIGUSR1
))
941 if (!thread
) return 0; /* nothing found */
942 queue
= get_apc_queue( thread
, apc
->call
.type
);
946 if (thread
->state
== TERMINATED
) return 0;
947 queue
= get_apc_queue( thread
, apc
->call
.type
);
948 /* send signal for system APCs if needed */
949 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
951 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
953 /* cancel a possible previous APC with the same owner */
954 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
958 list_add_tail( queue
, &apc
->entry
);
959 if (!list_prev( queue
, &apc
->entry
)) /* first one */
960 wake_thread( thread
);
965 /* queue an async procedure call */
966 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
968 struct thread_apc
*apc
;
971 if ((apc
= create_apc( owner
, call_data
)))
973 ret
= queue_apc( NULL
, thread
, apc
);
974 release_object( apc
);
979 /* cancel the async procedure call owned by a specific object */
980 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
982 struct thread_apc
*apc
;
983 struct list
*queue
= get_apc_queue( thread
, type
);
985 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
987 if (apc
->owner
!= owner
) continue;
988 list_remove( &apc
->entry
);
990 wake_up( &apc
->obj
, 0 );
991 release_object( apc
);
996 /* remove the head apc from the queue; the returned object must be released by the caller */
997 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
999 struct thread_apc
*apc
= NULL
;
1000 struct list
*ptr
= list_head( &thread
->system_apc
);
1002 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
1005 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1011 /* clear an APC queue, cancelling all the APCs on it */
1012 static void clear_apc_queue( struct list
*queue
)
1016 while ((ptr
= list_head( queue
)))
1018 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1019 list_remove( &apc
->entry
);
1021 wake_up( &apc
->obj
, 0 );
1022 release_object( apc
);
1026 /* add an fd to the inflight list */
1027 /* return list index, or -1 on error */
1028 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1032 if (server
== -1) return -1;
1039 /* first check if we already have an entry for this fd */
1040 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1041 if (thread
->inflight
[i
].client
== client
)
1043 close( thread
->inflight
[i
].server
);
1044 thread
->inflight
[i
].server
= server
;
1048 /* now find a free spot to store it */
1049 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1050 if (thread
->inflight
[i
].client
== -1)
1052 thread
->inflight
[i
].client
= client
;
1053 thread
->inflight
[i
].server
= server
;
1061 /* get an inflight fd and purge it from the list */
1062 /* the fd must be closed when no longer used */
1063 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1067 if (client
== -1) return -1;
1071 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1073 if (thread
->inflight
[i
].client
== client
)
1075 ret
= thread
->inflight
[i
].server
;
1076 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1080 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1084 /* kill a thread on the spot */
1085 void kill_thread( struct thread
*thread
, int violent_death
)
1087 if (thread
->state
== TERMINATED
) return; /* already killed */
1088 thread
->state
= TERMINATED
;
1089 thread
->exit_time
= current_time
;
1090 if (current
== thread
) current
= NULL
;
1092 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1093 thread
->id
, thread
->exit_code
);
1096 while (thread
->wait
) end_wait( thread
);
1097 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1098 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1101 kill_console_processes( thread
, 0 );
1102 debug_exit_thread( thread
);
1103 abandon_mutexes( thread
);
1104 wake_up( &thread
->obj
, 0 );
1105 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1106 cleanup_thread( thread
);
1107 remove_process_thread( thread
->process
, thread
);
1108 release_object( thread
);
1111 /* copy parts of a context structure */
1112 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1114 assert( to
->cpu
== from
->cpu
);
1116 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1117 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1118 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1119 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1120 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1121 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1124 /* return the context flags that correspond to system regs */
1125 /* (system regs are the ones we can't access on the client side) */
1126 static unsigned int get_context_system_regs( enum cpu_type cpu
)
1130 case CPU_x86
: return SERVER_CTX_DEBUG_REGISTERS
;
1131 case CPU_x86_64
: return SERVER_CTX_DEBUG_REGISTERS
;
1132 case CPU_POWERPC
: return 0;
1133 case CPU_ARM
: return 0;
1134 case CPU_ARM64
: return 0;
1139 /* trigger a breakpoint event in a given thread */
1140 void break_thread( struct thread
*thread
)
1144 assert( thread
->context
);
1146 memset( &data
, 0, sizeof(data
) );
1147 data
.exception
.first
= 1;
1148 data
.exception
.exc_code
= STATUS_BREAKPOINT
;
1149 data
.exception
.flags
= EXCEPTION_CONTINUABLE
;
1150 switch (thread
->context
->cpu
)
1153 data
.exception
.address
= thread
->context
->ctl
.i386_regs
.eip
;
1156 data
.exception
.address
= thread
->context
->ctl
.x86_64_regs
.rip
;
1159 data
.exception
.address
= thread
->context
->ctl
.powerpc_regs
.iar
;
1162 data
.exception
.address
= thread
->context
->ctl
.arm_regs
.pc
;
1165 data
.exception
.address
= thread
->context
->ctl
.arm64_regs
.pc
;
1168 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
1169 thread
->debug_break
= 0;
1172 /* take a snapshot of currently running threads */
1173 struct thread_snapshot
*thread_snap( int *count
)
1175 struct thread_snapshot
*snapshot
, *ptr
;
1176 struct thread
*thread
;
1179 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1180 if (thread
->state
!= TERMINATED
) total
++;
1181 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
1183 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1185 if (thread
->state
== TERMINATED
) continue;
1186 ptr
->thread
= thread
;
1187 ptr
->count
= thread
->obj
.refcount
;
1188 ptr
->priority
= thread
->priority
;
1189 grab_object( thread
);
1196 /* gets the current impersonation token */
1197 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1200 return thread
->token
;
1202 return thread
->process
->token
;
1205 /* check if a cpu type can be supported on this server */
1206 int is_cpu_supported( enum cpu_type cpu
)
1208 unsigned int prefix_cpu_mask
= get_prefix_cpu_mask();
1210 if (CPU_FLAG(cpu
) && (supported_cpus
& prefix_cpu_mask
& CPU_FLAG(cpu
))) return 1;
1211 if (!(supported_cpus
& prefix_cpu_mask
))
1212 set_error( STATUS_NOT_SUPPORTED
);
1213 else if (supported_cpus
& CPU_FLAG(cpu
))
1214 set_error( STATUS_INVALID_IMAGE_WIN_64
); /* server supports it but not the prefix */
1216 set_error( STATUS_INVALID_IMAGE_FORMAT
);
1220 /* create a new thread */
1221 DECL_HANDLER(new_thread
)
1223 struct thread
*thread
;
1224 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1226 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1228 if (request_fd
!= -1) close( request_fd
);
1229 set_error( STATUS_INVALID_HANDLE
);
1233 if ((thread
= create_thread( request_fd
, current
->process
)))
1235 if (req
->suspend
) thread
->suspend
++;
1236 reply
->tid
= get_thread_id( thread
);
1237 if ((reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
)))
1239 /* thread object will be released when the thread gets killed */
1242 kill_thread( thread
, 1 );
1246 /* initialize a new thread */
1247 DECL_HANDLER(init_thread
)
1249 struct process
*process
= current
->process
;
1250 int wait_fd
, reply_fd
;
1252 if ((reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
)) == -1)
1254 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1257 if ((wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
)) == -1)
1259 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1263 if (current
->reply_fd
) /* already initialised */
1265 set_error( STATUS_INVALID_PARAMETER
);
1269 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1271 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1272 current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 );
1273 if (!current
->reply_fd
|| !current
->wait_fd
) return;
1275 if (!is_valid_address(req
->teb
))
1277 set_error( STATUS_INVALID_PARAMETER
);
1281 current
->unix_pid
= req
->unix_pid
;
1282 current
->unix_tid
= req
->unix_tid
;
1283 current
->teb
= req
->teb
;
1285 if (!process
->peb
) /* first thread, initialize the process too */
1287 if (!is_cpu_supported( req
->cpu
)) return;
1288 process
->unix_pid
= current
->unix_pid
;
1289 process
->peb
= req
->entry
;
1290 process
->cpu
= req
->cpu
;
1291 reply
->info_size
= init_process( current
);
1292 if (!process
->parent
)
1293 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1295 set_thread_affinity( current
, current
->affinity
);
1299 if (req
->cpu
!= process
->cpu
)
1301 set_error( STATUS_INVALID_PARAMETER
);
1304 if (process
->unix_pid
!= current
->unix_pid
)
1305 process
->unix_pid
= -1; /* can happen with linuxthreads */
1306 stop_thread_if_suspended( current
);
1307 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, &req
->entry
);
1308 set_thread_affinity( current
, current
->affinity
);
1310 debug_level
= max( debug_level
, req
->debug_level
);
1312 reply
->pid
= get_process_id( process
);
1313 reply
->tid
= get_thread_id( current
);
1314 reply
->version
= SERVER_PROTOCOL_VERSION
;
1315 reply
->server_start
= server_start_time
;
1316 reply
->all_cpus
= supported_cpus
& get_prefix_cpu_mask();
1320 if (reply_fd
!= -1) close( reply_fd
);
1321 if (wait_fd
!= -1) close( wait_fd
);
1324 /* terminate a thread */
1325 DECL_HANDLER(terminate_thread
)
1327 struct thread
*thread
;
1331 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1333 thread
->exit_code
= req
->exit_code
;
1334 if (thread
!= current
) kill_thread( thread
, 1 );
1338 reply
->last
= (thread
->process
->running_threads
== 1);
1340 release_object( thread
);
1344 /* open a handle to a thread */
1345 DECL_HANDLER(open_thread
)
1347 struct thread
*thread
= get_thread_from_id( req
->tid
);
1352 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1353 release_object( thread
);
1357 /* fetch information about a thread */
1358 DECL_HANDLER(get_thread_info
)
1360 struct thread
*thread
;
1361 obj_handle_t handle
= req
->handle
;
1363 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1364 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
1368 reply
->pid
= get_process_id( thread
->process
);
1369 reply
->tid
= get_thread_id( thread
);
1370 reply
->teb
= thread
->teb
;
1371 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1372 reply
->priority
= thread
->priority
;
1373 reply
->affinity
= thread
->affinity
;
1374 reply
->creation_time
= thread
->creation_time
;
1375 reply
->exit_time
= thread
->exit_time
;
1376 reply
->last
= thread
->process
->running_threads
== 1;
1378 release_object( thread
);
1382 /* set information about a thread */
1383 DECL_HANDLER(set_thread_info
)
1385 struct thread
*thread
;
1387 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1389 set_thread_info( thread
, req
);
1390 release_object( thread
);
1394 /* suspend a thread */
1395 DECL_HANDLER(suspend_thread
)
1397 struct thread
*thread
;
1399 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1401 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1402 else reply
->count
= suspend_thread( thread
);
1403 release_object( thread
);
1407 /* resume a thread */
1408 DECL_HANDLER(resume_thread
)
1410 struct thread
*thread
;
1412 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1414 reply
->count
= resume_thread( thread
);
1415 release_object( thread
);
1419 /* select on a handle list */
1420 DECL_HANDLER(select
)
1422 select_op_t select_op
;
1423 data_size_t op_size
;
1424 struct thread_apc
*apc
;
1425 const apc_result_t
*result
= get_req_data();
1427 if (get_req_data_size() < sizeof(*result
))
1429 set_error( STATUS_INVALID_PARAMETER
);
1432 op_size
= min( get_req_data_size() - sizeof(*result
), sizeof(select_op
) );
1433 memset( &select_op
, 0, sizeof(select_op
) );
1434 memcpy( &select_op
, result
+ 1, op_size
);
1436 /* first store results of previous apc */
1439 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1440 0, &thread_apc_ops
))) return;
1441 apc
->result
= *result
;
1443 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1445 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1446 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1447 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1448 apc
->result
.create_thread
.handle
= handle
;
1449 clear_error(); /* ignore errors from the above calls */
1451 else if (apc
->result
.type
== APC_ASYNC_IO
)
1454 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
,
1455 apc
->result
.async_io
.apc
, apc
->result
.async_io
.arg
);
1457 wake_up( &apc
->obj
, 0 );
1458 close_handle( current
->process
, req
->prev_apc
);
1459 release_object( apc
);
1462 reply
->timeout
= select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1464 if (get_error() == STATUS_USER_APC
)
1468 if (!(apc
= thread_dequeue_apc( current
, !(req
->flags
& SELECT_ALERTABLE
) )))
1470 /* Optimization: ignore APC_NONE calls, they are only used to
1471 * wake up a thread, but since we got here the thread woke up already.
1473 if (apc
->call
.type
!= APC_NONE
)
1475 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1476 reply
->call
= apc
->call
;
1477 release_object( apc
);
1481 wake_up( &apc
->obj
, 0 );
1482 release_object( apc
);
1487 /* queue an APC for a thread or process */
1488 DECL_HANDLER(queue_apc
)
1490 struct thread
*thread
= NULL
;
1491 struct process
*process
= NULL
;
1492 struct thread_apc
*apc
;
1494 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1496 switch (apc
->call
.type
)
1500 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1502 case APC_VIRTUAL_ALLOC
:
1503 case APC_VIRTUAL_FREE
:
1504 case APC_VIRTUAL_PROTECT
:
1505 case APC_VIRTUAL_FLUSH
:
1506 case APC_VIRTUAL_LOCK
:
1507 case APC_VIRTUAL_UNLOCK
:
1508 case APC_UNMAP_VIEW
:
1509 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1511 case APC_VIRTUAL_QUERY
:
1512 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1515 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1516 if (process
&& process
!= current
->process
)
1518 /* duplicate the handle into the target process */
1519 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1520 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1521 if (handle
) apc
->call
.map_view
.handle
= handle
;
1524 release_object( process
);
1529 case APC_CREATE_THREAD
:
1530 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1533 set_error( STATUS_INVALID_PARAMETER
);
1539 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1540 release_object( thread
);
1544 reply
->self
= (process
== current
->process
);
1547 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1550 if (queue_apc( process
, NULL
, apc
))
1552 apc
->caller
= (struct thread
*)grab_object( current
);
1553 reply
->handle
= handle
;
1557 close_handle( current
->process
, handle
);
1558 set_error( STATUS_PROCESS_IS_TERMINATING
);
1562 release_object( process
);
1565 release_object( apc
);
1568 /* Get the result of an APC call */
1569 DECL_HANDLER(get_apc_result
)
1571 struct thread_apc
*apc
;
1573 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1574 0, &thread_apc_ops
))) return;
1575 if (!apc
->executed
) set_error( STATUS_PENDING
);
1578 reply
->result
= apc
->result
;
1579 /* close the handle directly to avoid an extra round-trip */
1580 close_handle( current
->process
, req
->handle
);
1582 release_object( apc
);
1585 /* retrieve the current context of a thread */
1586 DECL_HANDLER(get_thread_context
)
1588 struct thread
*thread
;
1591 if (get_reply_max_size() < sizeof(context_t
))
1593 set_error( STATUS_INVALID_PARAMETER
);
1596 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1597 reply
->self
= (thread
== current
);
1599 if (thread
!= current
&& !thread
->context
)
1601 /* thread is not suspended, retry (if it's still running) */
1602 if (thread
->state
== RUNNING
)
1604 set_error( STATUS_PENDING
);
1607 release_object( thread
);
1608 /* make sure we have suspend access */
1609 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1610 suspend_thread( thread
);
1613 else set_error( STATUS_UNSUCCESSFUL
);
1615 else if ((context
= set_reply_data_size( sizeof(context_t
) )))
1617 unsigned int flags
= get_context_system_regs( thread
->process
->cpu
);
1619 memset( context
, 0, sizeof(context_t
) );
1620 context
->cpu
= thread
->process
->cpu
;
1621 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1622 if (flags
) get_thread_context( thread
, context
, flags
);
1624 release_object( thread
);
1627 /* set the current context of a thread */
1628 DECL_HANDLER(set_thread_context
)
1630 struct thread
*thread
;
1631 const context_t
*context
= get_req_data();
1633 if (get_req_data_size() < sizeof(context_t
))
1635 set_error( STATUS_INVALID_PARAMETER
);
1638 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1639 reply
->self
= (thread
== current
);
1641 if (thread
!= current
&& !thread
->context
)
1643 /* thread is not suspended, retry (if it's still running) */
1644 if (thread
->state
== RUNNING
)
1646 set_error( STATUS_PENDING
);
1649 release_object( thread
);
1650 /* make sure we have suspend access */
1651 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1652 suspend_thread( thread
);
1655 else set_error( STATUS_UNSUCCESSFUL
);
1657 else if (context
->cpu
== thread
->process
->cpu
)
1659 unsigned int system_flags
= get_context_system_regs(context
->cpu
) & context
->flags
;
1660 unsigned int client_flags
= context
->flags
& ~system_flags
;
1662 if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1663 if (thread
->context
&& !get_error()) copy_context( thread
->context
, context
, client_flags
);
1665 else set_error( STATUS_INVALID_PARAMETER
);
1667 release_object( thread
);
1670 /* retrieve the suspended context of a thread */
1671 DECL_HANDLER(get_suspend_context
)
1673 if (get_reply_max_size() < sizeof(context_t
))
1675 set_error( STATUS_INVALID_PARAMETER
);
1679 if (current
->suspend_context
)
1681 set_reply_data_ptr( current
->suspend_context
, sizeof(context_t
) );
1682 if (current
->context
== current
->suspend_context
)
1684 current
->context
= NULL
;
1685 stop_thread_if_suspended( current
);
1687 current
->suspend_context
= NULL
;
1689 else set_error( STATUS_INVALID_PARAMETER
); /* not suspended, shouldn't happen */
1692 /* store the suspended context of a thread */
1693 DECL_HANDLER(set_suspend_context
)
1695 const context_t
*context
= get_req_data();
1697 if (get_req_data_size() < sizeof(context_t
))
1699 set_error( STATUS_INVALID_PARAMETER
);
1703 if (current
->context
|| context
->cpu
!= current
->process
->cpu
)
1705 /* nested suspend or exception, shouldn't happen */
1706 set_error( STATUS_INVALID_PARAMETER
);
1708 else if ((current
->suspend_context
= mem_alloc( sizeof(context_t
) )))
1710 memcpy( current
->suspend_context
, get_req_data(), sizeof(context_t
) );
1711 current
->context
= current
->suspend_context
;
1712 if (current
->debug_break
) break_thread( current
);
1716 /* fetch a selector entry for a thread */
1717 DECL_HANDLER(get_selector_entry
)
1719 struct thread
*thread
;
1720 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1722 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1723 release_object( thread
);