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
) | CPU_FLAG(CPU_ARM
);
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_link_name
, /* link_name */
121 NULL
, /* unlink_name */
122 no_open_file
, /* open_file */
123 no_close_handle
, /* close_handle */
124 thread_apc_destroy
/* destroy */
128 /* thread operations */
130 static void dump_thread( struct object
*obj
, int verbose
);
131 static struct object_type
*thread_get_type( struct object
*obj
);
132 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
133 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
134 static void thread_poll_event( struct fd
*fd
, int event
);
135 static void destroy_thread( struct object
*obj
);
137 static const struct object_ops thread_ops
=
139 sizeof(struct thread
), /* size */
140 dump_thread
, /* dump */
141 thread_get_type
, /* get_type */
142 add_queue
, /* add_queue */
143 remove_queue
, /* remove_queue */
144 thread_signaled
, /* signaled */
145 no_satisfied
, /* satisfied */
146 no_signal
, /* signal */
147 no_get_fd
, /* get_fd */
148 thread_map_access
, /* map_access */
149 default_get_sd
, /* get_sd */
150 default_set_sd
, /* set_sd */
151 no_lookup_name
, /* lookup_name */
152 no_link_name
, /* link_name */
153 NULL
, /* unlink_name */
154 no_open_file
, /* open_file */
155 no_close_handle
, /* close_handle */
156 destroy_thread
/* destroy */
159 static const struct fd_ops thread_fd_ops
=
161 NULL
, /* get_poll_events */
162 thread_poll_event
, /* poll_event */
164 NULL
, /* get_fd_type */
166 NULL
, /* queue_async */
167 NULL
/* reselect_async */
170 static struct list thread_list
= LIST_INIT(thread_list
);
172 /* initialize the structure for a newly allocated thread */
173 static inline void init_thread_structure( struct thread
*thread
)
177 thread
->unix_pid
= -1; /* not known yet */
178 thread
->unix_tid
= -1; /* not known yet */
179 thread
->context
= NULL
;
180 thread
->suspend_context
= NULL
;
182 thread
->entry_point
= 0;
183 thread
->debug_ctx
= NULL
;
184 thread
->debug_event
= NULL
;
185 thread
->debug_break
= 0;
186 thread
->system_regs
= 0;
187 thread
->queue
= NULL
;
190 thread
->req_data
= NULL
;
191 thread
->req_toread
= 0;
192 thread
->reply_data
= NULL
;
193 thread
->reply_towrite
= 0;
194 thread
->request_fd
= NULL
;
195 thread
->reply_fd
= NULL
;
196 thread
->wait_fd
= NULL
;
197 thread
->state
= RUNNING
;
198 thread
->exit_code
= 0;
199 thread
->priority
= 0;
201 thread
->desktop_users
= 0;
202 thread
->token
= NULL
;
204 thread
->creation_time
= current_time
;
205 thread
->exit_time
= 0;
207 list_init( &thread
->mutex_list
);
208 list_init( &thread
->system_apc
);
209 list_init( &thread
->user_apc
);
211 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
212 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
215 /* check if address looks valid for a client-side data structure (TEB etc.) */
216 static inline int is_valid_address( client_ptr_t addr
)
218 return addr
&& !(addr
% sizeof(int));
221 /* create a new thread */
222 struct thread
*create_thread( int fd
, struct process
*process
, const struct security_descriptor
*sd
)
224 struct thread
*thread
;
229 if (pipe( request_pipe
) == -1)
234 if (send_client_fd( process
, request_pipe
[1], SERVER_PROTOCOL_VERSION
) == -1)
236 close( request_pipe
[0] );
237 close( request_pipe
[1] );
240 close( request_pipe
[1] );
241 fd
= request_pipe
[0];
244 if (process
->is_terminating
)
247 set_error( STATUS_PROCESS_IS_TERMINATING
);
251 if (!(thread
= alloc_object( &thread_ops
)))
257 init_thread_structure( thread
);
259 thread
->process
= (struct process
*)grab_object( process
);
260 thread
->desktop
= process
->desktop
;
261 thread
->affinity
= process
->affinity
;
262 if (!current
) current
= thread
;
264 list_add_head( &thread_list
, &thread
->entry
);
266 if (sd
&& !set_sd_defaults_from_token( &thread
->obj
, sd
,
267 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
268 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
,
272 release_object( thread
);
275 if (!(thread
->id
= alloc_ptid( thread
)))
278 release_object( thread
);
281 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
283 release_object( thread
);
287 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
288 add_process_thread( thread
->process
, thread
);
292 /* handle a client event */
293 static void thread_poll_event( struct fd
*fd
, int event
)
295 struct thread
*thread
= get_fd_user( fd
);
296 assert( thread
->obj
.ops
== &thread_ops
);
298 grab_object( thread
);
299 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
300 else if (event
& POLLIN
) read_request( thread
);
301 else if (event
& POLLOUT
) write_reply( thread
);
302 release_object( thread
);
305 /* cleanup everything that is no longer needed by a dead thread */
306 /* used by destroy_thread and kill_thread */
307 static void cleanup_thread( struct thread
*thread
)
311 clear_apc_queue( &thread
->system_apc
);
312 clear_apc_queue( &thread
->user_apc
);
313 free( thread
->req_data
);
314 free( thread
->reply_data
);
315 if (thread
->request_fd
) release_object( thread
->request_fd
);
316 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
317 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
318 free( thread
->suspend_context
);
319 cleanup_clipboard_thread(thread
);
320 destroy_thread_windows( thread
);
321 free_msg_queue( thread
);
322 close_thread_desktop( thread
);
323 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
325 if (thread
->inflight
[i
].client
!= -1)
327 close( thread
->inflight
[i
].server
);
328 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
331 thread
->req_data
= NULL
;
332 thread
->reply_data
= NULL
;
333 thread
->request_fd
= NULL
;
334 thread
->reply_fd
= NULL
;
335 thread
->wait_fd
= NULL
;
336 thread
->context
= NULL
;
337 thread
->suspend_context
= NULL
;
341 /* destroy a thread when its refcount is 0 */
342 static void destroy_thread( struct object
*obj
)
344 struct thread
*thread
= (struct thread
*)obj
;
345 assert( obj
->ops
== &thread_ops
);
347 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
348 list_remove( &thread
->entry
);
349 cleanup_thread( thread
);
350 release_object( thread
->process
);
351 if (thread
->id
) free_ptid( thread
->id
);
352 if (thread
->token
) release_object( thread
->token
);
355 /* dump a thread on stdout for debugging purposes */
356 static void dump_thread( struct object
*obj
, int verbose
)
358 struct thread
*thread
= (struct thread
*)obj
;
359 assert( obj
->ops
== &thread_ops
);
361 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
362 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
365 static struct object_type
*thread_get_type( struct object
*obj
)
367 static const WCHAR name
[] = {'T','h','r','e','a','d'};
368 static const struct unicode_str str
= { name
, sizeof(name
) };
369 return get_object_type( &str
);
372 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
374 struct thread
*mythread
= (struct thread
*)obj
;
375 return (mythread
->state
== TERMINATED
);
378 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
380 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
;
381 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
|
382 THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
;
383 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_QUERY_LIMITED_INFORMATION
;
384 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
386 if (access
& THREAD_QUERY_INFORMATION
) access
|= THREAD_QUERY_LIMITED_INFORMATION
;
387 if (access
& THREAD_SET_INFORMATION
) access
|= THREAD_SET_LIMITED_INFORMATION
;
389 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
392 static void dump_thread_apc( struct object
*obj
, int verbose
)
394 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
395 assert( obj
->ops
== &thread_apc_ops
);
397 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
400 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
402 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
403 return apc
->executed
;
406 static void thread_apc_destroy( struct object
*obj
)
408 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
409 if (apc
->caller
) release_object( apc
->caller
);
410 if (apc
->owner
) release_object( apc
->owner
);
413 /* queue an async procedure call */
414 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
416 struct thread_apc
*apc
;
418 if ((apc
= alloc_object( &thread_apc_ops
)))
420 apc
->call
= *call_data
;
424 apc
->result
.type
= APC_NONE
;
425 if (owner
) grab_object( owner
);
430 /* get a thread pointer from a thread id (and increment the refcount) */
431 struct thread
*get_thread_from_id( thread_id_t id
)
433 struct object
*obj
= get_ptid_entry( id
);
435 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
436 set_error( STATUS_INVALID_CID
);
440 /* get a thread from a handle (and increment the refcount) */
441 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
443 return (struct thread
*)get_handle_obj( current
->process
, handle
,
444 access
, &thread_ops
);
447 /* find a thread from a Unix tid */
448 struct thread
*get_thread_from_tid( int tid
)
450 struct thread
*thread
;
452 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
454 if (thread
->unix_tid
== tid
) return thread
;
459 /* find a thread from a Unix pid */
460 struct thread
*get_thread_from_pid( int pid
)
462 struct thread
*thread
;
464 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
466 if (thread
->unix_pid
== pid
) return thread
;
471 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
474 #ifdef HAVE_SCHED_SETAFFINITY
475 if (thread
->unix_tid
!= -1)
482 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
483 if (affinity
& mask
) CPU_SET( i
, &set
);
485 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
488 if (!ret
) thread
->affinity
= affinity
;
492 affinity_t
get_thread_affinity( struct thread
*thread
)
495 #ifdef HAVE_SCHED_SETAFFINITY
496 if (thread
->unix_tid
!= -1)
501 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
502 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
503 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
506 if (!mask
) mask
= ~(affinity_t
)0;
510 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
511 #define THREAD_PRIORITY_REALTIME_LOWEST -7
513 /* set all information about a thread */
514 static void set_thread_info( struct thread
*thread
,
515 const struct set_thread_info_request
*req
)
517 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
519 int max
= THREAD_PRIORITY_HIGHEST
;
520 int min
= THREAD_PRIORITY_LOWEST
;
521 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
523 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
524 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
526 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
527 req
->priority
== THREAD_PRIORITY_IDLE
||
528 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
529 thread
->priority
= req
->priority
;
531 set_error( STATUS_INVALID_PARAMETER
);
533 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
535 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
536 set_error( STATUS_INVALID_PARAMETER
);
537 else if (thread
->state
== TERMINATED
)
538 set_error( STATUS_THREAD_IS_TERMINATING
);
539 else if (set_thread_affinity( thread
, req
->affinity
))
542 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
543 security_set_thread_token( thread
, req
->token
);
544 if (req
->mask
& SET_THREAD_INFO_ENTRYPOINT
)
545 thread
->entry_point
= req
->entry_point
;
548 /* stop a thread (at the Unix level) */
549 void stop_thread( struct thread
*thread
)
551 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
552 /* can't stop a thread while initialisation is in progress */
553 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
556 /* stop a thread if it's supposed to be suspended */
557 void stop_thread_if_suspended( struct thread
*thread
)
559 if (thread
->suspend
+ thread
->process
->suspend
> 0) stop_thread( thread
);
562 /* suspend a thread */
563 static int suspend_thread( struct thread
*thread
)
565 int old_count
= thread
->suspend
;
566 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
568 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
570 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
574 /* resume a thread */
575 static int resume_thread( struct thread
*thread
)
577 int old_count
= thread
->suspend
;
578 if (thread
->suspend
> 0)
580 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
585 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
586 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
590 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
594 /* remove a thread from an object wait queue */
595 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
597 list_remove( &entry
->entry
);
598 release_object( obj
);
601 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
603 return entry
->wait
->thread
;
606 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
608 return entry
->wait
->select
;
611 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
613 return entry
->wait
->key
;
616 void make_wait_abandoned( struct wait_queue_entry
*entry
)
618 entry
->wait
->abandoned
= 1;
622 static unsigned int end_wait( struct thread
*thread
, unsigned int status
)
624 struct thread_wait
*wait
= thread
->wait
;
625 struct wait_queue_entry
*entry
;
629 thread
->wait
= wait
->next
;
631 if (status
< wait
->count
) /* wait satisfied, tell it to the objects */
633 if (wait
->select
== SELECT_WAIT_ALL
)
635 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
636 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
640 entry
= wait
->queues
+ status
;
641 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
643 if (wait
->abandoned
) status
+= STATUS_ABANDONED_WAIT_0
;
645 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
646 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
647 if (wait
->user
) remove_timeout_user( wait
->user
);
652 /* build the thread wait structure */
653 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
654 int flags
, timeout_t timeout
)
656 struct thread_wait
*wait
;
657 struct wait_queue_entry
*entry
;
660 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
661 wait
->next
= current
->wait
;
662 wait
->thread
= current
;
665 wait
->select
= select_op
->op
;
668 wait
->timeout
= timeout
;
670 current
->wait
= wait
;
672 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
674 struct object
*obj
= objects
[i
];
676 if (!obj
->ops
->add_queue( obj
, entry
))
679 end_wait( current
, get_error() );
686 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
687 int flags
, timeout_t timeout
)
689 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
693 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
695 for (i
= 0; i
< count
; i
++)
696 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
699 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, timeout
);
701 while (i
> 0) release_object( objects
[--i
] );
705 /* check if the thread waiting condition is satisfied */
706 static int check_wait( struct thread
*thread
)
709 struct thread_wait
*wait
= thread
->wait
;
710 struct wait_queue_entry
*entry
;
714 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
715 return STATUS_USER_APC
;
717 /* Suspended threads may not acquire locks, but they can run system APCs */
718 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
720 if (wait
->select
== SELECT_WAIT_ALL
)
723 /* Note: we must check them all anyway, as some objects may
724 * want to do something when signaled, even if others are not */
725 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
726 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
727 if (!not_ok
) return STATUS_WAIT_0
;
731 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
732 if (entry
->obj
->ops
->signaled( entry
->obj
, entry
)) return i
;
735 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
736 if (wait
->timeout
<= current_time
) return STATUS_TIMEOUT
;
740 /* send the wakeup signal to a thread */
741 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
743 struct wake_up_reply reply
;
746 memset( &reply
, 0, sizeof(reply
) );
747 reply
.cookie
= cookie
;
748 reply
.signaled
= signaled
;
749 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
752 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
753 else if (errno
== EPIPE
)
754 kill_thread( thread
, 0 ); /* normal death */
756 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
760 /* attempt to wake up a thread */
761 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
762 int wake_thread( struct thread
*thread
)
767 for (count
= 0; thread
->wait
; count
++)
769 if ((signaled
= check_wait( thread
)) == -1) break;
771 cookie
= thread
->wait
->cookie
;
772 signaled
= end_wait( thread
, signaled
);
773 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
774 if (cookie
&& send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
776 if (!count
) count
= -1;
783 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
784 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
786 struct thread_wait
*wait
= entry
->wait
;
787 struct thread
*thread
= wait
->thread
;
791 if (thread
->wait
!= wait
) return 0; /* not the current wait */
792 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
794 assert( wait
->select
!= SELECT_WAIT_ALL
);
796 cookie
= wait
->cookie
;
797 signaled
= end_wait( thread
, entry
- wait
->queues
);
798 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
800 if (!cookie
|| send_thread_wakeup( thread
, cookie
, signaled
) != -1)
801 wake_thread( thread
); /* check other waits too */
806 /* thread wait timeout */
807 static void thread_timeout( void *ptr
)
809 struct thread_wait
*wait
= ptr
;
810 struct thread
*thread
= wait
->thread
;
811 client_ptr_t cookie
= wait
->cookie
;
814 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
815 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
817 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
818 end_wait( thread
, STATUS_TIMEOUT
);
821 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
822 /* check if other objects have become signaled in the meantime */
823 wake_thread( thread
);
826 /* try signaling an event flag, a semaphore or a mutex */
827 static int signal_object( obj_handle_t handle
)
832 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
835 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
836 release_object( obj
);
841 /* select on a list of handles */
842 static timeout_t
select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
843 int flags
, timeout_t timeout
)
847 struct object
*object
;
849 if (timeout
<= 0) timeout
= current_time
- timeout
;
851 switch (select_op
->op
)
854 if (!wait_on( select_op
, 0, NULL
, flags
, timeout
)) return timeout
;
858 case SELECT_WAIT_ALL
:
859 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
860 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
862 set_error( STATUS_INVALID_PARAMETER
);
865 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, timeout
))
869 case SELECT_SIGNAL_AND_WAIT
:
870 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, timeout
))
872 if (select_op
->signal_and_wait
.signal
)
874 if (!signal_object( select_op
->signal_and_wait
.signal
))
876 end_wait( current
, get_error() );
879 /* check if we woke ourselves up */
880 if (!current
->wait
) return timeout
;
884 case SELECT_KEYED_EVENT_WAIT
:
885 case SELECT_KEYED_EVENT_RELEASE
:
886 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
887 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
888 if (!object
) return timeout
;
889 ret
= wait_on( select_op
, 1, &object
, flags
, timeout
);
890 release_object( object
);
891 if (!ret
) return timeout
;
892 current
->wait
->key
= select_op
->keyed_event
.key
;
896 set_error( STATUS_INVALID_PARAMETER
);
900 if ((ret
= check_wait( current
)) != -1)
902 /* condition is already satisfied */
903 set_error( end_wait( current
, ret
));
907 /* now we need to wait */
908 if (current
->wait
->timeout
!= TIMEOUT_INFINITE
)
910 if (!(current
->wait
->user
= add_timeout_user( current
->wait
->timeout
,
911 thread_timeout
, current
->wait
)))
913 end_wait( current
, get_error() );
917 current
->wait
->cookie
= cookie
;
918 set_error( STATUS_PENDING
);
922 /* attempt to wake threads sleeping on the object wait queue */
923 void wake_up( struct object
*obj
, int max
)
928 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
930 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
931 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
932 if (ret
> 0 && max
&& !--max
) break;
933 /* restart at the head of the list since a wake up can change the object wait queue */
934 ptr
= &obj
->wait_queue
;
938 /* return the apc queue to use for a given apc type */
939 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
946 return &thread
->user_apc
;
948 return &thread
->system_apc
;
952 /* check if thread is currently waiting for a (system) apc */
953 static inline int is_in_apc_wait( struct thread
*thread
)
955 return (thread
->process
->suspend
|| thread
->suspend
||
956 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
959 /* queue an existing APC to a given thread */
960 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
964 if (thread
&& thread
->state
== TERMINATED
&& process
)
967 if (!thread
) /* find a suitable thread inside the process */
969 struct thread
*candidate
;
971 /* first try to find a waiting thread */
972 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
974 if (candidate
->state
== TERMINATED
) continue;
975 if (is_in_apc_wait( candidate
))
983 /* then use the first one that accepts a signal */
984 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
986 if (send_thread_signal( candidate
, SIGUSR1
))
993 if (!thread
) return 0; /* nothing found */
994 queue
= get_apc_queue( thread
, apc
->call
.type
);
998 if (thread
->state
== TERMINATED
) return 0;
999 queue
= get_apc_queue( thread
, apc
->call
.type
);
1000 /* send signal for system APCs if needed */
1001 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
1003 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
1005 /* cancel a possible previous APC with the same owner */
1006 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
1010 list_add_tail( queue
, &apc
->entry
);
1011 if (!list_prev( queue
, &apc
->entry
)) /* first one */
1012 wake_thread( thread
);
1017 /* queue an async procedure call */
1018 int thread_queue_apc( struct process
*process
, struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
1020 struct thread_apc
*apc
;
1023 if ((apc
= create_apc( owner
, call_data
)))
1025 ret
= queue_apc( process
, thread
, apc
);
1026 release_object( apc
);
1031 /* cancel the async procedure call owned by a specific object */
1032 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
1034 struct thread_apc
*apc
;
1035 struct list
*queue
= get_apc_queue( thread
, type
);
1037 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
1039 if (apc
->owner
!= owner
) continue;
1040 list_remove( &apc
->entry
);
1042 wake_up( &apc
->obj
, 0 );
1043 release_object( apc
);
1048 /* remove the head apc from the queue; the returned object must be released by the caller */
1049 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
1051 struct thread_apc
*apc
= NULL
;
1052 struct list
*ptr
= list_head( &thread
->system_apc
);
1054 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
1057 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1063 /* clear an APC queue, cancelling all the APCs on it */
1064 static void clear_apc_queue( struct list
*queue
)
1068 while ((ptr
= list_head( queue
)))
1070 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1071 list_remove( &apc
->entry
);
1073 wake_up( &apc
->obj
, 0 );
1074 release_object( apc
);
1078 /* add an fd to the inflight list */
1079 /* return list index, or -1 on error */
1080 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1084 if (server
== -1) return -1;
1091 /* first check if we already have an entry for this fd */
1092 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1093 if (thread
->inflight
[i
].client
== client
)
1095 close( thread
->inflight
[i
].server
);
1096 thread
->inflight
[i
].server
= server
;
1100 /* now find a free spot to store it */
1101 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1102 if (thread
->inflight
[i
].client
== -1)
1104 thread
->inflight
[i
].client
= client
;
1105 thread
->inflight
[i
].server
= server
;
1113 /* get an inflight fd and purge it from the list */
1114 /* the fd must be closed when no longer used */
1115 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1119 if (client
== -1) return -1;
1123 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1125 if (thread
->inflight
[i
].client
== client
)
1127 ret
= thread
->inflight
[i
].server
;
1128 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1132 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1136 /* kill a thread on the spot */
1137 void kill_thread( struct thread
*thread
, int violent_death
)
1139 if (thread
->state
== TERMINATED
) return; /* already killed */
1140 thread
->state
= TERMINATED
;
1141 thread
->exit_time
= current_time
;
1142 if (current
== thread
) current
= NULL
;
1144 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1145 thread
->id
, thread
->exit_code
);
1148 while (thread
->wait
) end_wait( thread
, STATUS_THREAD_IS_TERMINATING
);
1149 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1150 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1153 kill_console_processes( thread
, 0 );
1154 debug_exit_thread( thread
);
1155 abandon_mutexes( thread
);
1156 wake_up( &thread
->obj
, 0 );
1157 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1158 cleanup_thread( thread
);
1159 remove_process_thread( thread
->process
, thread
);
1160 release_object( thread
);
1163 /* copy parts of a context structure */
1164 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1166 assert( to
->cpu
== from
->cpu
);
1168 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1169 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1170 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1171 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1172 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1173 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1176 /* return the context flags that correspond to system regs */
1177 /* (system regs are the ones we can't access on the client side) */
1178 static unsigned int get_context_system_regs( enum cpu_type cpu
)
1182 case CPU_x86
: return SERVER_CTX_DEBUG_REGISTERS
;
1183 case CPU_x86_64
: return SERVER_CTX_DEBUG_REGISTERS
;
1184 case CPU_POWERPC
: return 0;
1185 case CPU_ARM
: return SERVER_CTX_DEBUG_REGISTERS
;
1186 case CPU_ARM64
: return SERVER_CTX_DEBUG_REGISTERS
;
1191 /* trigger a breakpoint event in a given thread */
1192 void break_thread( struct thread
*thread
)
1196 assert( thread
->context
);
1198 memset( &data
, 0, sizeof(data
) );
1199 data
.exception
.first
= 1;
1200 data
.exception
.exc_code
= STATUS_BREAKPOINT
;
1201 data
.exception
.flags
= EXCEPTION_CONTINUABLE
;
1202 switch (thread
->context
->cpu
)
1205 data
.exception
.address
= thread
->context
->ctl
.i386_regs
.eip
;
1208 data
.exception
.address
= thread
->context
->ctl
.x86_64_regs
.rip
;
1211 data
.exception
.address
= thread
->context
->ctl
.powerpc_regs
.iar
;
1214 data
.exception
.address
= thread
->context
->ctl
.arm_regs
.pc
;
1217 data
.exception
.address
= thread
->context
->ctl
.arm64_regs
.pc
;
1220 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
1221 thread
->debug_break
= 0;
1224 /* take a snapshot of currently running threads */
1225 struct thread_snapshot
*thread_snap( int *count
)
1227 struct thread_snapshot
*snapshot
, *ptr
;
1228 struct thread
*thread
;
1231 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1232 if (thread
->state
!= TERMINATED
) total
++;
1233 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
1235 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1237 if (thread
->state
== TERMINATED
) continue;
1238 ptr
->thread
= thread
;
1239 ptr
->count
= thread
->obj
.refcount
;
1240 ptr
->priority
= thread
->priority
;
1241 grab_object( thread
);
1248 /* gets the current impersonation token */
1249 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1252 return thread
->token
;
1254 return thread
->process
->token
;
1257 /* check if a cpu type can be supported on this server */
1258 int is_cpu_supported( enum cpu_type cpu
)
1260 unsigned int prefix_cpu_mask
= get_prefix_cpu_mask();
1262 if (supported_cpus
& prefix_cpu_mask
& CPU_FLAG(cpu
)) return 1;
1263 if (!(supported_cpus
& prefix_cpu_mask
))
1264 set_error( STATUS_NOT_SUPPORTED
);
1265 else if (supported_cpus
& CPU_FLAG(cpu
))
1266 set_error( STATUS_INVALID_IMAGE_WIN_64
); /* server supports it but not the prefix */
1268 set_error( STATUS_INVALID_IMAGE_FORMAT
);
1272 /* return the cpu mask for supported cpus */
1273 unsigned int get_supported_cpu_mask(void)
1275 return supported_cpus
& get_prefix_cpu_mask();
1278 /* create a new thread */
1279 DECL_HANDLER(new_thread
)
1281 struct thread
*thread
;
1282 struct process
*process
;
1283 struct unicode_str name
;
1284 const struct security_descriptor
*sd
;
1285 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1286 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1288 if (!(process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
)))
1290 if (request_fd
!= -1) close( request_fd
);
1294 if (process
!= current
->process
)
1296 if (request_fd
!= -1) /* can't create a request fd in a different process */
1298 close( request_fd
);
1299 set_error( STATUS_INVALID_PARAMETER
);
1302 if (process
->running_threads
) /* only the initial thread can be created in another process */
1304 set_error( STATUS_ACCESS_DENIED
);
1308 else if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1310 if (request_fd
!= -1) close( request_fd
);
1311 set_error( STATUS_INVALID_HANDLE
);
1315 if ((thread
= create_thread( request_fd
, process
, sd
)))
1317 thread
->system_regs
= current
->system_regs
;
1318 if (req
->suspend
) thread
->suspend
++;
1319 reply
->tid
= get_thread_id( thread
);
1320 if ((reply
->handle
= alloc_handle_no_access_check( current
->process
, thread
,
1321 req
->access
, objattr
->attributes
)))
1323 /* thread object will be released when the thread gets killed */
1326 kill_thread( thread
, 1 );
1329 release_object( process
);
1332 /* initialize a new thread */
1333 DECL_HANDLER(init_thread
)
1335 struct process
*process
= current
->process
;
1336 int wait_fd
, reply_fd
;
1338 if ((reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
)) == -1)
1340 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1343 if ((wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
)) == -1)
1345 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1349 if (current
->reply_fd
) /* already initialised */
1351 set_error( STATUS_INVALID_PARAMETER
);
1355 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1357 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1358 current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 );
1359 if (!current
->reply_fd
|| !current
->wait_fd
) return;
1361 if (!is_valid_address(req
->teb
))
1363 set_error( STATUS_INVALID_PARAMETER
);
1367 current
->unix_pid
= req
->unix_pid
;
1368 current
->unix_tid
= req
->unix_tid
;
1369 current
->teb
= req
->teb
;
1370 current
->entry_point
= process
->peb
? req
->entry
: 0;
1372 if (!process
->peb
) /* first thread, initialize the process too */
1374 if (!is_cpu_supported( req
->cpu
)) return;
1375 process
->unix_pid
= current
->unix_pid
;
1376 process
->peb
= req
->entry
;
1377 process
->cpu
= req
->cpu
;
1378 reply
->info_size
= init_process( current
);
1379 if (!process
->parent_id
)
1380 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1382 set_thread_affinity( current
, current
->affinity
);
1386 if (req
->cpu
!= process
->cpu
)
1388 set_error( STATUS_INVALID_PARAMETER
);
1391 if (process
->unix_pid
!= current
->unix_pid
)
1392 process
->unix_pid
= -1; /* can happen with linuxthreads */
1393 init_thread_context( current
);
1394 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, &req
->entry
);
1395 set_thread_affinity( current
, current
->affinity
);
1397 debug_level
= max( debug_level
, req
->debug_level
);
1399 reply
->pid
= get_process_id( process
);
1400 reply
->tid
= get_thread_id( current
);
1401 reply
->version
= SERVER_PROTOCOL_VERSION
;
1402 reply
->server_start
= server_start_time
;
1403 reply
->all_cpus
= supported_cpus
& get_prefix_cpu_mask();
1404 reply
->suspend
= (current
->suspend
|| process
->suspend
);
1408 if (reply_fd
!= -1) close( reply_fd
);
1409 if (wait_fd
!= -1) close( wait_fd
);
1412 /* terminate a thread */
1413 DECL_HANDLER(terminate_thread
)
1415 struct thread
*thread
;
1419 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1421 thread
->exit_code
= req
->exit_code
;
1422 if (thread
!= current
) kill_thread( thread
, 1 );
1426 reply
->last
= (thread
->process
->running_threads
== 1);
1428 release_object( thread
);
1432 /* open a handle to a thread */
1433 DECL_HANDLER(open_thread
)
1435 struct thread
*thread
= get_thread_from_id( req
->tid
);
1440 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1441 release_object( thread
);
1445 /* fetch information about a thread */
1446 DECL_HANDLER(get_thread_info
)
1448 struct thread
*thread
;
1449 obj_handle_t handle
= req
->handle
;
1451 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1452 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_LIMITED_INFORMATION
);
1456 reply
->pid
= get_process_id( thread
->process
);
1457 reply
->tid
= get_thread_id( thread
);
1458 reply
->teb
= thread
->teb
;
1459 reply
->entry_point
= thread
->entry_point
;
1460 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1461 reply
->priority
= thread
->priority
;
1462 reply
->affinity
= thread
->affinity
;
1463 reply
->last
= thread
->process
->running_threads
== 1;
1465 release_object( thread
);
1469 /* fetch information about thread times */
1470 DECL_HANDLER(get_thread_times
)
1472 struct thread
*thread
;
1474 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1476 reply
->creation_time
= thread
->creation_time
;
1477 reply
->exit_time
= thread
->exit_time
;
1479 release_object( thread
);
1483 /* set information about a thread */
1484 DECL_HANDLER(set_thread_info
)
1486 struct thread
*thread
;
1488 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1490 set_thread_info( thread
, req
);
1491 release_object( thread
);
1495 /* suspend a thread */
1496 DECL_HANDLER(suspend_thread
)
1498 struct thread
*thread
;
1500 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1502 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1503 else reply
->count
= suspend_thread( thread
);
1504 release_object( thread
);
1508 /* resume a thread */
1509 DECL_HANDLER(resume_thread
)
1511 struct thread
*thread
;
1513 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1515 reply
->count
= resume_thread( thread
);
1516 release_object( thread
);
1520 /* select on a handle list */
1521 DECL_HANDLER(select
)
1523 select_op_t select_op
;
1524 data_size_t op_size
;
1525 struct thread_apc
*apc
;
1526 const apc_result_t
*result
= get_req_data();
1528 if (get_req_data_size() < sizeof(*result
))
1530 set_error( STATUS_INVALID_PARAMETER
);
1535 set_error( STATUS_INVALID_PARAMETER
);
1539 op_size
= min( get_req_data_size() - sizeof(*result
), sizeof(select_op
) );
1540 memset( &select_op
, 0, sizeof(select_op
) );
1541 memcpy( &select_op
, result
+ 1, op_size
);
1543 /* first store results of previous apc */
1546 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1547 0, &thread_apc_ops
))) return;
1548 apc
->result
= *result
;
1550 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1552 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1553 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1554 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1555 apc
->result
.create_thread
.handle
= handle
;
1556 clear_error(); /* ignore errors from the above calls */
1558 else if (apc
->result
.type
== APC_ASYNC_IO
)
1561 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
1563 wake_up( &apc
->obj
, 0 );
1564 close_handle( current
->process
, req
->prev_apc
);
1565 release_object( apc
);
1568 reply
->timeout
= select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1570 if (get_error() == STATUS_USER_APC
)
1574 if (!(apc
= thread_dequeue_apc( current
, !(req
->flags
& SELECT_ALERTABLE
) )))
1576 /* Optimization: ignore APC_NONE calls, they are only used to
1577 * wake up a thread, but since we got here the thread woke up already.
1579 if (apc
->call
.type
!= APC_NONE
&&
1580 (reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1582 reply
->call
= apc
->call
;
1583 release_object( apc
);
1587 wake_up( &apc
->obj
, 0 );
1588 release_object( apc
);
1593 /* queue an APC for a thread or process */
1594 DECL_HANDLER(queue_apc
)
1596 struct thread
*thread
= NULL
;
1597 struct process
*process
= NULL
;
1598 struct thread_apc
*apc
;
1600 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1602 switch (apc
->call
.type
)
1606 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1608 case APC_VIRTUAL_ALLOC
:
1609 case APC_VIRTUAL_FREE
:
1610 case APC_VIRTUAL_PROTECT
:
1611 case APC_VIRTUAL_FLUSH
:
1612 case APC_VIRTUAL_LOCK
:
1613 case APC_VIRTUAL_UNLOCK
:
1614 case APC_UNMAP_VIEW
:
1615 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1617 case APC_VIRTUAL_QUERY
:
1618 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1621 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1622 if (process
&& process
!= current
->process
)
1624 /* duplicate the handle into the target process */
1625 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1626 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1627 if (handle
) apc
->call
.map_view
.handle
= handle
;
1630 release_object( process
);
1635 case APC_CREATE_THREAD
:
1636 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1639 set_error( STATUS_INVALID_PARAMETER
);
1645 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1646 release_object( thread
);
1650 reply
->self
= (process
== current
->process
);
1653 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1656 if (queue_apc( process
, NULL
, apc
))
1658 apc
->caller
= (struct thread
*)grab_object( current
);
1659 reply
->handle
= handle
;
1663 close_handle( current
->process
, handle
);
1664 set_error( STATUS_PROCESS_IS_TERMINATING
);
1668 release_object( process
);
1671 release_object( apc
);
1674 /* Get the result of an APC call */
1675 DECL_HANDLER(get_apc_result
)
1677 struct thread_apc
*apc
;
1679 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1680 0, &thread_apc_ops
))) return;
1682 if (apc
->executed
) reply
->result
= apc
->result
;
1683 else set_error( STATUS_PENDING
);
1685 /* close the handle directly to avoid an extra round-trip */
1686 close_handle( current
->process
, req
->handle
);
1687 release_object( apc
);
1690 /* retrieve the current context of a thread */
1691 DECL_HANDLER(get_thread_context
)
1693 struct thread
*thread
;
1696 if (get_reply_max_size() < sizeof(context_t
))
1698 set_error( STATUS_INVALID_PARAMETER
);
1701 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1702 reply
->self
= (thread
== current
);
1704 if (thread
!= current
&& !thread
->context
)
1706 /* thread is not suspended, retry (if it's still running) */
1707 if (thread
->state
== RUNNING
)
1709 set_error( STATUS_PENDING
);
1712 release_object( thread
);
1713 /* make sure we have suspend access */
1714 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1715 suspend_thread( thread
);
1718 else set_error( STATUS_UNSUCCESSFUL
);
1720 else if ((context
= set_reply_data_size( sizeof(context_t
) )))
1722 unsigned int flags
= get_context_system_regs( thread
->process
->cpu
);
1724 memset( context
, 0, sizeof(context_t
) );
1725 context
->cpu
= thread
->process
->cpu
;
1726 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1727 if (req
->flags
& flags
) get_thread_context( thread
, context
, req
->flags
& flags
);
1729 release_object( thread
);
1732 /* set the current context of a thread */
1733 DECL_HANDLER(set_thread_context
)
1735 struct thread
*thread
;
1736 const context_t
*context
= get_req_data();
1738 if (get_req_data_size() < sizeof(context_t
))
1740 set_error( STATUS_INVALID_PARAMETER
);
1743 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1744 reply
->self
= (thread
== current
);
1746 if (thread
!= current
&& !thread
->context
)
1748 /* thread is not suspended, retry (if it's still running) */
1749 if (thread
->state
== RUNNING
)
1751 set_error( STATUS_PENDING
);
1754 release_object( thread
);
1755 /* make sure we have suspend access */
1756 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1757 suspend_thread( thread
);
1760 else set_error( STATUS_UNSUCCESSFUL
);
1762 else if (context
->cpu
== thread
->process
->cpu
)
1764 unsigned int system_flags
= get_context_system_regs(context
->cpu
) & context
->flags
;
1765 unsigned int client_flags
= context
->flags
& ~system_flags
;
1767 if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1768 if (thread
->context
&& !get_error()) copy_context( thread
->context
, context
, client_flags
);
1770 else set_error( STATUS_INVALID_PARAMETER
);
1772 release_object( thread
);
1775 /* retrieve the suspended context of a thread */
1776 DECL_HANDLER(get_suspend_context
)
1778 if (get_reply_max_size() < sizeof(context_t
))
1780 set_error( STATUS_INVALID_PARAMETER
);
1784 if (current
->suspend_context
)
1786 if (current
->suspend_context
->flags
)
1787 set_reply_data_ptr( current
->suspend_context
, sizeof(context_t
) );
1789 free( current
->suspend_context
);
1790 if (current
->context
== current
->suspend_context
)
1792 current
->context
= NULL
;
1793 stop_thread_if_suspended( current
);
1795 current
->suspend_context
= NULL
;
1797 else set_error( STATUS_INVALID_PARAMETER
); /* not suspended, shouldn't happen */
1800 /* store the suspended context of a thread */
1801 DECL_HANDLER(set_suspend_context
)
1803 const context_t
*context
= get_req_data();
1805 if (get_req_data_size() < sizeof(context_t
))
1807 set_error( STATUS_INVALID_PARAMETER
);
1811 if (current
->context
|| context
->cpu
!= current
->process
->cpu
)
1813 /* nested suspend or exception, shouldn't happen */
1814 set_error( STATUS_INVALID_PARAMETER
);
1816 else if ((current
->suspend_context
= mem_alloc( sizeof(context_t
) )))
1818 memcpy( current
->suspend_context
, get_req_data(), sizeof(context_t
) );
1819 current
->suspend_context
->flags
= 0; /* to keep track of what is modified */
1820 current
->context
= current
->suspend_context
;
1821 if (current
->debug_break
) break_thread( current
);
1825 /* fetch a selector entry for a thread */
1826 DECL_HANDLER(get_selector_entry
)
1828 struct thread
*thread
;
1829 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1831 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1832 release_object( thread
);