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
)
222 set_error( STATUS_PROCESS_IS_TERMINATING
);
226 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
228 init_thread_structure( thread
);
230 thread
->process
= (struct process
*)grab_object( process
);
231 thread
->desktop
= process
->desktop
;
232 thread
->affinity
= process
->affinity
;
233 if (!current
) current
= thread
;
235 list_add_head( &thread_list
, &thread
->entry
);
237 if (!(thread
->id
= alloc_ptid( thread
)))
239 release_object( thread
);
242 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
244 release_object( thread
);
248 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
249 add_process_thread( thread
->process
, thread
);
253 /* handle a client event */
254 static void thread_poll_event( struct fd
*fd
, int event
)
256 struct thread
*thread
= get_fd_user( fd
);
257 assert( thread
->obj
.ops
== &thread_ops
);
259 grab_object( thread
);
260 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
261 else if (event
& POLLIN
) read_request( thread
);
262 else if (event
& POLLOUT
) write_reply( thread
);
263 release_object( thread
);
266 /* cleanup everything that is no longer needed by a dead thread */
267 /* used by destroy_thread and kill_thread */
268 static void cleanup_thread( struct thread
*thread
)
272 clear_apc_queue( &thread
->system_apc
);
273 clear_apc_queue( &thread
->user_apc
);
274 free( thread
->req_data
);
275 free( thread
->reply_data
);
276 if (thread
->request_fd
) release_object( thread
->request_fd
);
277 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
278 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
279 free( thread
->suspend_context
);
280 cleanup_clipboard_thread(thread
);
281 destroy_thread_windows( thread
);
282 free_msg_queue( thread
);
283 close_thread_desktop( thread
);
284 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
286 if (thread
->inflight
[i
].client
!= -1)
288 close( thread
->inflight
[i
].server
);
289 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
292 thread
->req_data
= NULL
;
293 thread
->reply_data
= NULL
;
294 thread
->request_fd
= NULL
;
295 thread
->reply_fd
= NULL
;
296 thread
->wait_fd
= NULL
;
297 thread
->context
= NULL
;
298 thread
->suspend_context
= NULL
;
302 /* destroy a thread when its refcount is 0 */
303 static void destroy_thread( struct object
*obj
)
305 struct thread
*thread
= (struct thread
*)obj
;
306 assert( obj
->ops
== &thread_ops
);
308 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
309 list_remove( &thread
->entry
);
310 cleanup_thread( thread
);
311 release_object( thread
->process
);
312 if (thread
->id
) free_ptid( thread
->id
);
313 if (thread
->token
) release_object( thread
->token
);
316 /* dump a thread on stdout for debugging purposes */
317 static void dump_thread( struct object
*obj
, int verbose
)
319 struct thread
*thread
= (struct thread
*)obj
;
320 assert( obj
->ops
== &thread_ops
);
322 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
323 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
326 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
328 struct thread
*mythread
= (struct thread
*)obj
;
329 return (mythread
->state
== TERMINATED
);
332 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
334 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
| THREAD_QUERY_LIMITED_INFORMATION
;
335 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
|
336 THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
| THREAD_SET_LIMITED_INFORMATION
;
337 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_QUERY_LIMITED_INFORMATION
;
338 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
339 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
342 static void dump_thread_apc( struct object
*obj
, int verbose
)
344 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
345 assert( obj
->ops
== &thread_apc_ops
);
347 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
350 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
352 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
353 return apc
->executed
;
356 static void thread_apc_destroy( struct object
*obj
)
358 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
359 if (apc
->caller
) release_object( apc
->caller
);
360 if (apc
->owner
) release_object( apc
->owner
);
363 /* queue an async procedure call */
364 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
366 struct thread_apc
*apc
;
368 if ((apc
= alloc_object( &thread_apc_ops
)))
370 apc
->call
= *call_data
;
374 apc
->result
.type
= APC_NONE
;
375 if (owner
) grab_object( owner
);
380 /* get a thread pointer from a thread id (and increment the refcount) */
381 struct thread
*get_thread_from_id( thread_id_t id
)
383 struct object
*obj
= get_ptid_entry( id
);
385 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
386 set_error( STATUS_INVALID_CID
);
390 /* get a thread from a handle (and increment the refcount) */
391 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
393 return (struct thread
*)get_handle_obj( current
->process
, handle
,
394 access
, &thread_ops
);
397 /* find a thread from a Unix tid */
398 struct thread
*get_thread_from_tid( int tid
)
400 struct thread
*thread
;
402 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
404 if (thread
->unix_tid
== tid
) return thread
;
409 /* find a thread from a Unix pid */
410 struct thread
*get_thread_from_pid( int pid
)
412 struct thread
*thread
;
414 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
416 if (thread
->unix_pid
== pid
) return thread
;
421 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
424 #ifdef HAVE_SCHED_SETAFFINITY
425 if (thread
->unix_tid
!= -1)
432 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
433 if (affinity
& mask
) CPU_SET( i
, &set
);
435 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
438 if (!ret
) thread
->affinity
= affinity
;
442 affinity_t
get_thread_affinity( struct thread
*thread
)
445 #ifdef HAVE_SCHED_SETAFFINITY
446 if (thread
->unix_tid
!= -1)
451 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
452 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
453 if (CPU_ISSET( i
, &set
)) mask
|= 1 << i
;
456 if (!mask
) mask
= ~0;
460 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
461 #define THREAD_PRIORITY_REALTIME_LOWEST -7
463 /* set all information about a thread */
464 static void set_thread_info( struct thread
*thread
,
465 const struct set_thread_info_request
*req
)
467 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
469 int max
= THREAD_PRIORITY_HIGHEST
;
470 int min
= THREAD_PRIORITY_LOWEST
;
471 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
473 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
474 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
476 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
477 req
->priority
== THREAD_PRIORITY_IDLE
||
478 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
479 thread
->priority
= req
->priority
;
481 set_error( STATUS_INVALID_PARAMETER
);
483 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
485 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
486 set_error( STATUS_INVALID_PARAMETER
);
487 else if (thread
->state
== TERMINATED
)
488 set_error( STATUS_THREAD_IS_TERMINATING
);
489 else if (set_thread_affinity( thread
, req
->affinity
))
492 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
493 security_set_thread_token( thread
, req
->token
);
496 /* stop a thread (at the Unix level) */
497 void stop_thread( struct thread
*thread
)
499 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
500 /* can't stop a thread while initialisation is in progress */
501 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
504 /* stop a thread if it's supposed to be suspended */
505 void stop_thread_if_suspended( struct thread
*thread
)
507 if (thread
->suspend
+ thread
->process
->suspend
> 0) stop_thread( thread
);
510 /* suspend a thread */
511 static int suspend_thread( struct thread
*thread
)
513 int old_count
= thread
->suspend
;
514 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
516 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
518 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
522 /* resume a thread */
523 static int resume_thread( struct thread
*thread
)
525 int old_count
= thread
->suspend
;
526 if (thread
->suspend
> 0)
528 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
533 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
534 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
538 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
542 /* remove a thread from an object wait queue */
543 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
545 list_remove( &entry
->entry
);
546 release_object( obj
);
549 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
551 return entry
->wait
->thread
;
554 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
556 return entry
->wait
->select
;
559 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
561 return entry
->wait
->key
;
564 void make_wait_abandoned( struct wait_queue_entry
*entry
)
566 entry
->wait
->abandoned
= 1;
570 static void end_wait( struct thread
*thread
)
572 struct thread_wait
*wait
= thread
->wait
;
573 struct wait_queue_entry
*entry
;
577 thread
->wait
= wait
->next
;
578 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
579 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
580 if (wait
->user
) remove_timeout_user( wait
->user
);
584 /* build the thread wait structure */
585 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
586 int flags
, timeout_t timeout
)
588 struct thread_wait
*wait
;
589 struct wait_queue_entry
*entry
;
592 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
593 wait
->next
= current
->wait
;
594 wait
->thread
= current
;
597 wait
->select
= select_op
->op
;
599 wait
->timeout
= timeout
;
601 current
->wait
= wait
;
603 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
605 struct object
*obj
= objects
[i
];
607 if (!obj
->ops
->add_queue( obj
, entry
))
617 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
618 int flags
, timeout_t timeout
)
620 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
624 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
626 for (i
= 0; i
< count
; i
++)
627 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
630 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, timeout
);
632 while (i
> 0) release_object( objects
[--i
] );
636 /* check if the thread waiting condition is satisfied */
637 static int check_wait( struct thread
*thread
)
640 struct thread_wait
*wait
= thread
->wait
;
641 struct wait_queue_entry
*entry
;
645 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
646 return STATUS_USER_APC
;
648 /* Suspended threads may not acquire locks, but they can run system APCs */
649 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
651 if (wait
->select
== SELECT_WAIT_ALL
)
654 /* Note: we must check them all anyway, as some objects may
655 * want to do something when signaled, even if others are not */
656 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
657 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
658 if (not_ok
) goto other_checks
;
659 /* Wait satisfied: tell it to all objects */
660 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
661 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
662 return wait
->abandoned
? STATUS_ABANDONED_WAIT_0
: STATUS_WAIT_0
;
666 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
668 if (!entry
->obj
->ops
->signaled( entry
->obj
, entry
)) continue;
669 /* Wait satisfied: tell it to the object */
670 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
671 if (wait
->abandoned
) i
+= STATUS_ABANDONED_WAIT_0
;
677 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
678 if (wait
->timeout
<= current_time
) return STATUS_TIMEOUT
;
682 /* send the wakeup signal to a thread */
683 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
685 struct wake_up_reply reply
;
688 memset( &reply
, 0, sizeof(reply
) );
689 reply
.cookie
= cookie
;
690 reply
.signaled
= signaled
;
691 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
694 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
695 else if (errno
== EPIPE
)
696 kill_thread( thread
, 0 ); /* normal death */
698 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
702 /* attempt to wake up a thread */
703 /* return >0 if OK, 0 if the wait condition is still not satisfied */
704 int wake_thread( struct thread
*thread
)
709 for (count
= 0; thread
->wait
; count
++)
711 if ((signaled
= check_wait( thread
)) == -1) break;
713 cookie
= thread
->wait
->cookie
;
714 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
716 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
722 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
723 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
725 struct thread_wait
*wait
= entry
->wait
;
726 struct thread
*thread
= wait
->thread
;
730 if (thread
->wait
!= wait
) return 0; /* not the current wait */
731 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
733 assert( wait
->select
!= SELECT_WAIT_ALL
);
735 signaled
= entry
- wait
->queues
;
736 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
737 if (wait
->abandoned
) signaled
+= STATUS_ABANDONED_WAIT_0
;
739 cookie
= wait
->cookie
;
740 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
743 if (send_thread_wakeup( thread
, cookie
, signaled
) != -1)
744 wake_thread( thread
); /* check other waits too */
749 /* thread wait timeout */
750 static void thread_timeout( void *ptr
)
752 struct thread_wait
*wait
= ptr
;
753 struct thread
*thread
= wait
->thread
;
754 client_ptr_t cookie
= wait
->cookie
;
757 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
758 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
760 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
762 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
763 /* check if other objects have become signaled in the meantime */
764 wake_thread( thread
);
767 /* try signaling an event flag, a semaphore or a mutex */
768 static int signal_object( obj_handle_t handle
)
773 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
776 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
777 release_object( obj
);
782 /* select on a list of handles */
783 static timeout_t
select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
784 int flags
, timeout_t timeout
)
788 struct object
*object
;
790 if (timeout
<= 0) timeout
= current_time
- timeout
;
792 switch (select_op
->op
)
795 if (!wait_on( select_op
, 0, NULL
, flags
, timeout
)) return timeout
;
799 case SELECT_WAIT_ALL
:
800 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
801 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
803 set_error( STATUS_INVALID_PARAMETER
);
806 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, timeout
))
810 case SELECT_SIGNAL_AND_WAIT
:
811 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, timeout
))
813 if (select_op
->signal_and_wait
.signal
)
815 if (!signal_object( select_op
->signal_and_wait
.signal
))
820 /* check if we woke ourselves up */
821 if (!current
->wait
) return timeout
;
825 case SELECT_KEYED_EVENT_WAIT
:
826 case SELECT_KEYED_EVENT_RELEASE
:
827 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
828 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
829 if (!object
) return timeout
;
830 ret
= wait_on( select_op
, 1, &object
, flags
, timeout
);
831 release_object( object
);
832 if (!ret
) return timeout
;
833 current
->wait
->key
= select_op
->keyed_event
.key
;
837 set_error( STATUS_INVALID_PARAMETER
);
841 if ((ret
= check_wait( current
)) != -1)
843 /* condition is already satisfied */
849 /* now we need to wait */
850 if (current
->wait
->timeout
!= TIMEOUT_INFINITE
)
852 if (!(current
->wait
->user
= add_timeout_user( current
->wait
->timeout
,
853 thread_timeout
, current
->wait
)))
859 current
->wait
->cookie
= cookie
;
860 set_error( STATUS_PENDING
);
864 /* attempt to wake threads sleeping on the object wait queue */
865 void wake_up( struct object
*obj
, int max
)
869 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
871 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
872 if (!wake_thread( get_wait_queue_thread( entry
))) continue;
873 if (max
&& !--max
) break;
874 /* restart at the head of the list since a wake up can change the object wait queue */
875 ptr
= &obj
->wait_queue
;
879 /* return the apc queue to use for a given apc type */
880 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
887 return &thread
->user_apc
;
889 return &thread
->system_apc
;
893 /* check if thread is currently waiting for a (system) apc */
894 static inline int is_in_apc_wait( struct thread
*thread
)
896 return (thread
->process
->suspend
|| thread
->suspend
||
897 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
900 /* queue an existing APC to a given thread */
901 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
905 if (!thread
) /* find a suitable thread inside the process */
907 struct thread
*candidate
;
909 /* first try to find a waiting thread */
910 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
912 if (candidate
->state
== TERMINATED
) continue;
913 if (is_in_apc_wait( candidate
))
921 /* then use the first one that accepts a signal */
922 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
924 if (send_thread_signal( candidate
, SIGUSR1
))
931 if (!thread
) return 0; /* nothing found */
932 queue
= get_apc_queue( thread
, apc
->call
.type
);
936 if (thread
->state
== TERMINATED
) return 0;
937 queue
= get_apc_queue( thread
, apc
->call
.type
);
938 /* send signal for system APCs if needed */
939 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
941 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
943 /* cancel a possible previous APC with the same owner */
944 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
948 list_add_tail( queue
, &apc
->entry
);
949 if (!list_prev( queue
, &apc
->entry
)) /* first one */
950 wake_thread( thread
);
955 /* queue an async procedure call */
956 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
958 struct thread_apc
*apc
;
961 if ((apc
= create_apc( owner
, call_data
)))
963 ret
= queue_apc( NULL
, thread
, apc
);
964 release_object( apc
);
969 /* cancel the async procedure call owned by a specific object */
970 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
972 struct thread_apc
*apc
;
973 struct list
*queue
= get_apc_queue( thread
, type
);
975 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
977 if (apc
->owner
!= owner
) continue;
978 list_remove( &apc
->entry
);
980 wake_up( &apc
->obj
, 0 );
981 release_object( apc
);
986 /* remove the head apc from the queue; the returned object must be released by the caller */
987 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
989 struct thread_apc
*apc
= NULL
;
990 struct list
*ptr
= list_head( &thread
->system_apc
);
992 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
995 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1001 /* clear an APC queue, cancelling all the APCs on it */
1002 static void clear_apc_queue( struct list
*queue
)
1006 while ((ptr
= list_head( queue
)))
1008 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1009 list_remove( &apc
->entry
);
1011 wake_up( &apc
->obj
, 0 );
1012 release_object( apc
);
1016 /* add an fd to the inflight list */
1017 /* return list index, or -1 on error */
1018 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1022 if (server
== -1) return -1;
1029 /* first check if we already have an entry for this fd */
1030 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1031 if (thread
->inflight
[i
].client
== client
)
1033 close( thread
->inflight
[i
].server
);
1034 thread
->inflight
[i
].server
= server
;
1038 /* now find a free spot to store it */
1039 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1040 if (thread
->inflight
[i
].client
== -1)
1042 thread
->inflight
[i
].client
= client
;
1043 thread
->inflight
[i
].server
= server
;
1049 /* get an inflight fd and purge it from the list */
1050 /* the fd must be closed when no longer used */
1051 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1055 if (client
== -1) return -1;
1059 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1061 if (thread
->inflight
[i
].client
== client
)
1063 ret
= thread
->inflight
[i
].server
;
1064 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1068 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1072 /* kill a thread on the spot */
1073 void kill_thread( struct thread
*thread
, int violent_death
)
1075 if (thread
->state
== TERMINATED
) return; /* already killed */
1076 thread
->state
= TERMINATED
;
1077 thread
->exit_time
= current_time
;
1078 if (current
== thread
) current
= NULL
;
1080 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1081 thread
->id
, thread
->exit_code
);
1084 while (thread
->wait
) end_wait( thread
);
1085 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1086 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1089 kill_console_processes( thread
, 0 );
1090 debug_exit_thread( thread
);
1091 abandon_mutexes( thread
);
1092 wake_up( &thread
->obj
, 0 );
1093 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1094 cleanup_thread( thread
);
1095 remove_process_thread( thread
->process
, thread
);
1096 release_object( thread
);
1099 /* copy parts of a context structure */
1100 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1102 assert( to
->cpu
== from
->cpu
);
1104 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1105 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1106 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1107 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1108 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1109 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1112 /* return the context flags that correspond to system regs */
1113 /* (system regs are the ones we can't access on the client side) */
1114 static unsigned int get_context_system_regs( enum cpu_type cpu
)
1118 case CPU_x86
: return SERVER_CTX_DEBUG_REGISTERS
;
1119 case CPU_x86_64
: return SERVER_CTX_DEBUG_REGISTERS
;
1120 case CPU_POWERPC
: return 0;
1121 case CPU_ARM
: return 0;
1122 case CPU_ARM64
: return 0;
1127 /* trigger a breakpoint event in a given thread */
1128 void break_thread( struct thread
*thread
)
1132 assert( thread
->context
);
1134 memset( &data
, 0, sizeof(data
) );
1135 data
.exception
.first
= 1;
1136 data
.exception
.exc_code
= STATUS_BREAKPOINT
;
1137 data
.exception
.flags
= EXCEPTION_CONTINUABLE
;
1138 switch (thread
->context
->cpu
)
1141 data
.exception
.address
= thread
->context
->ctl
.i386_regs
.eip
;
1144 data
.exception
.address
= thread
->context
->ctl
.x86_64_regs
.rip
;
1147 data
.exception
.address
= thread
->context
->ctl
.powerpc_regs
.iar
;
1150 data
.exception
.address
= thread
->context
->ctl
.arm_regs
.pc
;
1153 data
.exception
.address
= thread
->context
->ctl
.arm64_regs
.pc
;
1156 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
1157 thread
->debug_break
= 0;
1160 /* take a snapshot of currently running threads */
1161 struct thread_snapshot
*thread_snap( int *count
)
1163 struct thread_snapshot
*snapshot
, *ptr
;
1164 struct thread
*thread
;
1167 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1168 if (thread
->state
!= TERMINATED
) total
++;
1169 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
1171 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1173 if (thread
->state
== TERMINATED
) continue;
1174 ptr
->thread
= thread
;
1175 ptr
->count
= thread
->obj
.refcount
;
1176 ptr
->priority
= thread
->priority
;
1177 grab_object( thread
);
1184 /* gets the current impersonation token */
1185 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1188 return thread
->token
;
1190 return thread
->process
->token
;
1193 /* check if a cpu type can be supported on this server */
1194 int is_cpu_supported( enum cpu_type cpu
)
1196 unsigned int prefix_cpu_mask
= get_prefix_cpu_mask();
1198 if (CPU_FLAG(cpu
) && (supported_cpus
& prefix_cpu_mask
& CPU_FLAG(cpu
))) return 1;
1199 if (!(supported_cpus
& prefix_cpu_mask
))
1200 set_error( STATUS_NOT_SUPPORTED
);
1201 else if (supported_cpus
& CPU_FLAG(cpu
))
1202 set_error( STATUS_INVALID_IMAGE_WIN_64
); /* server supports it but not the prefix */
1204 set_error( STATUS_INVALID_IMAGE_FORMAT
);
1208 /* create a new thread */
1209 DECL_HANDLER(new_thread
)
1211 struct thread
*thread
;
1212 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1214 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1216 if (request_fd
!= -1) close( request_fd
);
1217 set_error( STATUS_INVALID_HANDLE
);
1221 if ((thread
= create_thread( request_fd
, current
->process
)))
1223 if (req
->suspend
) thread
->suspend
++;
1224 reply
->tid
= get_thread_id( thread
);
1225 if ((reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
)))
1227 /* thread object will be released when the thread gets killed */
1230 kill_thread( thread
, 1 );
1234 /* initialize a new thread */
1235 DECL_HANDLER(init_thread
)
1237 struct process
*process
= current
->process
;
1238 int wait_fd
, reply_fd
;
1240 if ((reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
)) == -1)
1242 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1245 if ((wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
)) == -1)
1247 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1251 if (current
->reply_fd
) /* already initialised */
1253 set_error( STATUS_INVALID_PARAMETER
);
1257 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1259 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1260 current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 );
1261 if (!current
->reply_fd
|| !current
->wait_fd
) return;
1263 if (!is_valid_address(req
->teb
))
1265 set_error( STATUS_INVALID_PARAMETER
);
1269 current
->unix_pid
= req
->unix_pid
;
1270 current
->unix_tid
= req
->unix_tid
;
1271 current
->teb
= req
->teb
;
1273 if (!process
->peb
) /* first thread, initialize the process too */
1275 if (!is_cpu_supported( req
->cpu
)) return;
1276 process
->unix_pid
= current
->unix_pid
;
1277 process
->peb
= req
->entry
;
1278 process
->cpu
= req
->cpu
;
1279 reply
->info_size
= init_process( current
);
1280 if (!process
->parent
)
1281 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1283 set_thread_affinity( current
, current
->affinity
);
1287 if (req
->cpu
!= process
->cpu
)
1289 set_error( STATUS_INVALID_PARAMETER
);
1292 if (process
->unix_pid
!= current
->unix_pid
)
1293 process
->unix_pid
= -1; /* can happen with linuxthreads */
1294 stop_thread_if_suspended( current
);
1295 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, &req
->entry
);
1296 set_thread_affinity( current
, current
->affinity
);
1298 debug_level
= max( debug_level
, req
->debug_level
);
1300 reply
->pid
= get_process_id( process
);
1301 reply
->tid
= get_thread_id( current
);
1302 reply
->version
= SERVER_PROTOCOL_VERSION
;
1303 reply
->server_start
= server_start_time
;
1304 reply
->all_cpus
= supported_cpus
& get_prefix_cpu_mask();
1308 if (reply_fd
!= -1) close( reply_fd
);
1309 if (wait_fd
!= -1) close( wait_fd
);
1312 /* terminate a thread */
1313 DECL_HANDLER(terminate_thread
)
1315 struct thread
*thread
;
1319 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1321 thread
->exit_code
= req
->exit_code
;
1322 if (thread
!= current
) kill_thread( thread
, 1 );
1326 reply
->last
= (thread
->process
->running_threads
== 1);
1328 release_object( thread
);
1332 /* open a handle to a thread */
1333 DECL_HANDLER(open_thread
)
1335 struct thread
*thread
= get_thread_from_id( req
->tid
);
1340 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1341 release_object( thread
);
1345 /* fetch information about a thread */
1346 DECL_HANDLER(get_thread_info
)
1348 struct thread
*thread
;
1349 obj_handle_t handle
= req
->handle
;
1351 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1352 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
1356 reply
->pid
= get_process_id( thread
->process
);
1357 reply
->tid
= get_thread_id( thread
);
1358 reply
->teb
= thread
->teb
;
1359 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1360 reply
->priority
= thread
->priority
;
1361 reply
->affinity
= thread
->affinity
;
1362 reply
->creation_time
= thread
->creation_time
;
1363 reply
->exit_time
= thread
->exit_time
;
1364 reply
->last
= thread
->process
->running_threads
== 1;
1366 release_object( thread
);
1370 /* set information about a thread */
1371 DECL_HANDLER(set_thread_info
)
1373 struct thread
*thread
;
1375 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1377 set_thread_info( thread
, req
);
1378 release_object( thread
);
1382 /* suspend a thread */
1383 DECL_HANDLER(suspend_thread
)
1385 struct thread
*thread
;
1387 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1389 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1390 else reply
->count
= suspend_thread( thread
);
1391 release_object( thread
);
1395 /* resume a thread */
1396 DECL_HANDLER(resume_thread
)
1398 struct thread
*thread
;
1400 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1402 reply
->count
= resume_thread( thread
);
1403 release_object( thread
);
1407 /* select on a handle list */
1408 DECL_HANDLER(select
)
1410 select_op_t select_op
;
1411 data_size_t op_size
;
1412 struct thread_apc
*apc
;
1413 const apc_result_t
*result
= get_req_data();
1415 if (get_req_data_size() < sizeof(*result
))
1417 set_error( STATUS_INVALID_PARAMETER
);
1420 op_size
= min( get_req_data_size() - sizeof(*result
), sizeof(select_op
) );
1421 memset( &select_op
, 0, sizeof(select_op
) );
1422 memcpy( &select_op
, result
+ 1, op_size
);
1424 /* first store results of previous apc */
1427 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1428 0, &thread_apc_ops
))) return;
1429 apc
->result
= *result
;
1431 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1433 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1434 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1435 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1436 apc
->result
.create_thread
.handle
= handle
;
1437 clear_error(); /* ignore errors from the above calls */
1439 else if (apc
->result
.type
== APC_ASYNC_IO
)
1442 async_set_result( apc
->owner
, apc
->result
.async_io
.status
,
1443 apc
->result
.async_io
.total
, apc
->result
.async_io
.apc
);
1445 wake_up( &apc
->obj
, 0 );
1446 close_handle( current
->process
, req
->prev_apc
);
1447 release_object( apc
);
1450 reply
->timeout
= select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1452 if (get_error() == STATUS_USER_APC
)
1456 if (!(apc
= thread_dequeue_apc( current
, !(req
->flags
& SELECT_ALERTABLE
) )))
1458 /* Optimization: ignore APC_NONE calls, they are only used to
1459 * wake up a thread, but since we got here the thread woke up already.
1461 if (apc
->call
.type
!= APC_NONE
)
1463 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1464 reply
->call
= apc
->call
;
1465 release_object( apc
);
1469 wake_up( &apc
->obj
, 0 );
1470 release_object( apc
);
1475 /* queue an APC for a thread or process */
1476 DECL_HANDLER(queue_apc
)
1478 struct thread
*thread
= NULL
;
1479 struct process
*process
= NULL
;
1480 struct thread_apc
*apc
;
1482 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1484 switch (apc
->call
.type
)
1488 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1490 case APC_VIRTUAL_ALLOC
:
1491 case APC_VIRTUAL_FREE
:
1492 case APC_VIRTUAL_PROTECT
:
1493 case APC_VIRTUAL_FLUSH
:
1494 case APC_VIRTUAL_LOCK
:
1495 case APC_VIRTUAL_UNLOCK
:
1496 case APC_UNMAP_VIEW
:
1497 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1499 case APC_VIRTUAL_QUERY
:
1500 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1503 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1504 if (process
&& process
!= current
->process
)
1506 /* duplicate the handle into the target process */
1507 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1508 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1509 if (handle
) apc
->call
.map_view
.handle
= handle
;
1512 release_object( process
);
1517 case APC_CREATE_THREAD
:
1518 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1521 set_error( STATUS_INVALID_PARAMETER
);
1527 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1528 release_object( thread
);
1532 reply
->self
= (process
== current
->process
);
1535 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1538 if (queue_apc( process
, NULL
, apc
))
1540 apc
->caller
= (struct thread
*)grab_object( current
);
1541 reply
->handle
= handle
;
1545 close_handle( current
->process
, handle
);
1546 set_error( STATUS_PROCESS_IS_TERMINATING
);
1550 release_object( process
);
1553 release_object( apc
);
1556 /* Get the result of an APC call */
1557 DECL_HANDLER(get_apc_result
)
1559 struct thread_apc
*apc
;
1561 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1562 0, &thread_apc_ops
))) return;
1563 if (!apc
->executed
) set_error( STATUS_PENDING
);
1566 reply
->result
= apc
->result
;
1567 /* close the handle directly to avoid an extra round-trip */
1568 close_handle( current
->process
, req
->handle
);
1570 release_object( apc
);
1573 /* retrieve the current context of a thread */
1574 DECL_HANDLER(get_thread_context
)
1576 struct thread
*thread
;
1579 if (get_reply_max_size() < sizeof(context_t
))
1581 set_error( STATUS_INVALID_PARAMETER
);
1584 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1585 reply
->self
= (thread
== current
);
1587 if (thread
!= current
&& !thread
->context
)
1589 /* thread is not suspended, retry (if it's still running) */
1590 if (thread
->state
== RUNNING
)
1592 set_error( STATUS_PENDING
);
1595 release_object( thread
);
1596 /* make sure we have suspend access */
1597 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1598 suspend_thread( thread
);
1601 else set_error( STATUS_UNSUCCESSFUL
);
1603 else if ((context
= set_reply_data_size( sizeof(context_t
) )))
1605 unsigned int flags
= get_context_system_regs( thread
->process
->cpu
);
1607 memset( context
, 0, sizeof(context_t
) );
1608 context
->cpu
= thread
->process
->cpu
;
1609 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1610 if (flags
) get_thread_context( thread
, context
, flags
);
1612 release_object( thread
);
1615 /* set the current context of a thread */
1616 DECL_HANDLER(set_thread_context
)
1618 struct thread
*thread
;
1619 const context_t
*context
= get_req_data();
1621 if (get_req_data_size() < sizeof(context_t
))
1623 set_error( STATUS_INVALID_PARAMETER
);
1626 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1627 reply
->self
= (thread
== current
);
1629 if (thread
!= current
&& !thread
->context
)
1631 /* thread is not suspended, retry (if it's still running) */
1632 if (thread
->state
== RUNNING
)
1634 set_error( STATUS_PENDING
);
1637 release_object( thread
);
1638 /* make sure we have suspend access */
1639 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1640 suspend_thread( thread
);
1643 else set_error( STATUS_UNSUCCESSFUL
);
1645 else if (context
->cpu
== thread
->process
->cpu
)
1647 unsigned int system_flags
= get_context_system_regs(context
->cpu
) & context
->flags
;
1648 unsigned int client_flags
= context
->flags
& ~system_flags
;
1650 if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1651 if (thread
->context
&& !get_error()) copy_context( thread
->context
, context
, client_flags
);
1653 else set_error( STATUS_INVALID_PARAMETER
);
1655 release_object( thread
);
1658 /* retrieve the suspended context of a thread */
1659 DECL_HANDLER(get_suspend_context
)
1661 if (get_reply_max_size() < sizeof(context_t
))
1663 set_error( STATUS_INVALID_PARAMETER
);
1667 if (current
->suspend_context
)
1669 set_reply_data_ptr( current
->suspend_context
, sizeof(context_t
) );
1670 if (current
->context
== current
->suspend_context
)
1672 current
->context
= NULL
;
1673 stop_thread_if_suspended( current
);
1675 current
->suspend_context
= NULL
;
1677 else set_error( STATUS_INVALID_PARAMETER
); /* not suspended, shouldn't happen */
1680 /* store the suspended context of a thread */
1681 DECL_HANDLER(set_suspend_context
)
1683 const context_t
*context
= get_req_data();
1685 if (get_req_data_size() < sizeof(context_t
))
1687 set_error( STATUS_INVALID_PARAMETER
);
1691 if (current
->context
|| context
->cpu
!= current
->process
->cpu
)
1693 /* nested suspend or exception, shouldn't happen */
1694 set_error( STATUS_INVALID_PARAMETER
);
1696 else if ((current
->suspend_context
= mem_alloc( sizeof(context_t
) )))
1698 memcpy( current
->suspend_context
, get_req_data(), sizeof(context_t
) );
1699 current
->context
= current
->suspend_context
;
1700 if (current
->debug_break
) break_thread( current
);
1704 /* fetch a selector entry for a thread */
1705 DECL_HANDLER(get_selector_entry
)
1707 struct thread
*thread
;
1708 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1710 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1711 release_object( thread
);