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_kernel_obj_list
, /* get_kernel_obj_list */
124 no_close_handle
, /* close_handle */
125 thread_apc_destroy
/* destroy */
129 /* thread operations */
131 static void dump_thread( struct object
*obj
, int verbose
);
132 static struct object_type
*thread_get_type( struct object
*obj
);
133 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
134 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
135 static void thread_poll_event( struct fd
*fd
, int event
);
136 static struct list
*thread_get_kernel_obj_list( struct object
*obj
);
137 static void destroy_thread( struct object
*obj
);
139 static const struct object_ops thread_ops
=
141 sizeof(struct thread
), /* size */
142 dump_thread
, /* dump */
143 thread_get_type
, /* get_type */
144 add_queue
, /* add_queue */
145 remove_queue
, /* remove_queue */
146 thread_signaled
, /* signaled */
147 no_satisfied
, /* satisfied */
148 no_signal
, /* signal */
149 no_get_fd
, /* get_fd */
150 thread_map_access
, /* map_access */
151 default_get_sd
, /* get_sd */
152 default_set_sd
, /* set_sd */
153 no_lookup_name
, /* lookup_name */
154 no_link_name
, /* link_name */
155 NULL
, /* unlink_name */
156 no_open_file
, /* open_file */
157 thread_get_kernel_obj_list
, /* get_kernel_obj_list */
158 no_close_handle
, /* close_handle */
159 destroy_thread
/* destroy */
162 static const struct fd_ops thread_fd_ops
=
164 NULL
, /* get_poll_events */
165 thread_poll_event
, /* poll_event */
167 NULL
, /* get_fd_type */
169 NULL
, /* queue_async */
170 NULL
/* reselect_async */
173 static struct list thread_list
= LIST_INIT(thread_list
);
175 /* initialize the structure for a newly allocated thread */
176 static inline void init_thread_structure( struct thread
*thread
)
180 thread
->unix_pid
= -1; /* not known yet */
181 thread
->unix_tid
= -1; /* not known yet */
182 thread
->context
= NULL
;
183 thread
->suspend_context
= NULL
;
185 thread
->entry_point
= 0;
186 thread
->debug_ctx
= NULL
;
187 thread
->system_regs
= 0;
188 thread
->queue
= NULL
;
191 thread
->req_data
= NULL
;
192 thread
->req_toread
= 0;
193 thread
->reply_data
= NULL
;
194 thread
->reply_towrite
= 0;
195 thread
->request_fd
= NULL
;
196 thread
->reply_fd
= NULL
;
197 thread
->wait_fd
= NULL
;
198 thread
->state
= RUNNING
;
199 thread
->exit_code
= 0;
200 thread
->priority
= 0;
202 thread
->desktop_users
= 0;
203 thread
->token
= NULL
;
205 thread
->desc_len
= 0;
207 thread
->creation_time
= current_time
;
208 thread
->exit_time
= 0;
210 list_init( &thread
->mutex_list
);
211 list_init( &thread
->system_apc
);
212 list_init( &thread
->user_apc
);
213 list_init( &thread
->kernel_object
);
215 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
216 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
219 /* check if address looks valid for a client-side data structure (TEB etc.) */
220 static inline int is_valid_address( client_ptr_t addr
)
222 return addr
&& !(addr
% sizeof(int));
225 /* create a new thread */
226 struct thread
*create_thread( int fd
, struct process
*process
, const struct security_descriptor
*sd
)
228 struct thread
*thread
;
233 if (pipe( request_pipe
) == -1)
238 if (send_client_fd( process
, request_pipe
[1], SERVER_PROTOCOL_VERSION
) == -1)
240 close( request_pipe
[0] );
241 close( request_pipe
[1] );
244 close( request_pipe
[1] );
245 fd
= request_pipe
[0];
248 if (process
->is_terminating
)
251 set_error( STATUS_PROCESS_IS_TERMINATING
);
255 if (!(thread
= alloc_object( &thread_ops
)))
261 init_thread_structure( thread
);
263 thread
->process
= (struct process
*)grab_object( process
);
264 thread
->desktop
= process
->desktop
;
265 thread
->affinity
= process
->affinity
;
266 if (!current
) current
= thread
;
268 list_add_head( &thread_list
, &thread
->entry
);
270 if (sd
&& !set_sd_defaults_from_token( &thread
->obj
, sd
,
271 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
272 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
,
276 release_object( thread
);
279 if (!(thread
->id
= alloc_ptid( thread
)))
282 release_object( thread
);
285 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
287 release_object( thread
);
291 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
292 add_process_thread( thread
->process
, thread
);
296 /* handle a client event */
297 static void thread_poll_event( struct fd
*fd
, int event
)
299 struct thread
*thread
= get_fd_user( fd
);
300 assert( thread
->obj
.ops
== &thread_ops
);
302 grab_object( thread
);
303 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
304 else if (event
& POLLIN
) read_request( thread
);
305 else if (event
& POLLOUT
) write_reply( thread
);
306 release_object( thread
);
309 static struct list
*thread_get_kernel_obj_list( struct object
*obj
)
311 struct thread
*thread
= (struct thread
*)obj
;
312 return &thread
->kernel_object
;
315 /* cleanup everything that is no longer needed by a dead thread */
316 /* used by destroy_thread and kill_thread */
317 static void cleanup_thread( struct thread
*thread
)
321 clear_apc_queue( &thread
->system_apc
);
322 clear_apc_queue( &thread
->user_apc
);
323 free( thread
->req_data
);
324 free( thread
->reply_data
);
325 if (thread
->request_fd
) release_object( thread
->request_fd
);
326 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
327 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
328 free( thread
->suspend_context
);
329 cleanup_clipboard_thread(thread
);
330 destroy_thread_windows( thread
);
331 free_msg_queue( thread
);
332 close_thread_desktop( thread
);
333 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
335 if (thread
->inflight
[i
].client
!= -1)
337 close( thread
->inflight
[i
].server
);
338 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
341 free( thread
->desc
);
342 thread
->req_data
= NULL
;
343 thread
->reply_data
= NULL
;
344 thread
->request_fd
= NULL
;
345 thread
->reply_fd
= NULL
;
346 thread
->wait_fd
= NULL
;
347 thread
->context
= NULL
;
348 thread
->suspend_context
= NULL
;
351 thread
->desc_len
= 0;
354 /* destroy a thread when its refcount is 0 */
355 static void destroy_thread( struct object
*obj
)
357 struct thread
*thread
= (struct thread
*)obj
;
358 assert( obj
->ops
== &thread_ops
);
360 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
361 list_remove( &thread
->entry
);
362 cleanup_thread( thread
);
363 release_object( thread
->process
);
364 if (thread
->id
) free_ptid( thread
->id
);
365 if (thread
->token
) release_object( thread
->token
);
368 /* dump a thread on stdout for debugging purposes */
369 static void dump_thread( struct object
*obj
, int verbose
)
371 struct thread
*thread
= (struct thread
*)obj
;
372 assert( obj
->ops
== &thread_ops
);
374 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
375 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
378 static struct object_type
*thread_get_type( struct object
*obj
)
380 static const WCHAR name
[] = {'T','h','r','e','a','d'};
381 static const struct unicode_str str
= { name
, sizeof(name
) };
382 return get_object_type( &str
);
385 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
387 struct thread
*mythread
= (struct thread
*)obj
;
388 return (mythread
->state
== TERMINATED
);
391 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
393 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
;
394 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
|
395 THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
;
396 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_QUERY_LIMITED_INFORMATION
;
397 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
399 if (access
& THREAD_QUERY_INFORMATION
) access
|= THREAD_QUERY_LIMITED_INFORMATION
;
400 if (access
& THREAD_SET_INFORMATION
) access
|= THREAD_SET_LIMITED_INFORMATION
;
402 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
405 static void dump_thread_apc( struct object
*obj
, int verbose
)
407 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
408 assert( obj
->ops
== &thread_apc_ops
);
410 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
413 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
415 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
416 return apc
->executed
;
419 static void thread_apc_destroy( struct object
*obj
)
421 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
422 if (apc
->caller
) release_object( apc
->caller
);
423 if (apc
->owner
) release_object( apc
->owner
);
426 /* queue an async procedure call */
427 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
429 struct thread_apc
*apc
;
431 if ((apc
= alloc_object( &thread_apc_ops
)))
433 apc
->call
= *call_data
;
437 apc
->result
.type
= APC_NONE
;
438 if (owner
) grab_object( owner
);
443 /* get a thread pointer from a thread id (and increment the refcount) */
444 struct thread
*get_thread_from_id( thread_id_t id
)
446 struct object
*obj
= get_ptid_entry( id
);
448 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
449 set_error( STATUS_INVALID_CID
);
453 /* get a thread from a handle (and increment the refcount) */
454 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
456 return (struct thread
*)get_handle_obj( current
->process
, handle
,
457 access
, &thread_ops
);
460 /* find a thread from a Unix tid */
461 struct thread
*get_thread_from_tid( int tid
)
463 struct thread
*thread
;
465 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
467 if (thread
->unix_tid
== tid
) return thread
;
472 /* find a thread from a Unix pid */
473 struct thread
*get_thread_from_pid( int pid
)
475 struct thread
*thread
;
477 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
479 if (thread
->unix_pid
== pid
) return thread
;
484 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
487 #ifdef HAVE_SCHED_SETAFFINITY
488 if (thread
->unix_tid
!= -1)
495 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
496 if (affinity
& mask
) CPU_SET( i
, &set
);
498 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
501 if (!ret
) thread
->affinity
= affinity
;
505 affinity_t
get_thread_affinity( struct thread
*thread
)
508 #ifdef HAVE_SCHED_SETAFFINITY
509 if (thread
->unix_tid
!= -1)
514 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
515 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
516 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
519 if (!mask
) mask
= ~(affinity_t
)0;
523 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
524 #define THREAD_PRIORITY_REALTIME_LOWEST -7
526 /* set all information about a thread */
527 static void set_thread_info( struct thread
*thread
,
528 const struct set_thread_info_request
*req
)
530 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
532 int max
= THREAD_PRIORITY_HIGHEST
;
533 int min
= THREAD_PRIORITY_LOWEST
;
534 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
536 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
537 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
539 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
540 req
->priority
== THREAD_PRIORITY_IDLE
||
541 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
542 thread
->priority
= req
->priority
;
544 set_error( STATUS_INVALID_PARAMETER
);
546 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
548 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
549 set_error( STATUS_INVALID_PARAMETER
);
550 else if (thread
->state
== TERMINATED
)
551 set_error( STATUS_THREAD_IS_TERMINATING
);
552 else if (set_thread_affinity( thread
, req
->affinity
))
555 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
556 security_set_thread_token( thread
, req
->token
);
557 if (req
->mask
& SET_THREAD_INFO_ENTRYPOINT
)
558 thread
->entry_point
= req
->entry_point
;
559 if (req
->mask
& SET_THREAD_INFO_DESCRIPTION
)
562 data_size_t desc_len
= get_req_data_size();
566 if ((desc
= mem_alloc( desc_len
)))
568 memcpy( desc
, get_req_data(), desc_len
);
569 free( thread
->desc
);
571 thread
->desc_len
= desc_len
;
576 free( thread
->desc
);
578 thread
->desc_len
= 0;
583 /* stop a thread (at the Unix level) */
584 void stop_thread( struct thread
*thread
)
586 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
587 /* can't stop a thread while initialisation is in progress */
588 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
591 /* stop a thread if it's supposed to be suspended */
592 void stop_thread_if_suspended( struct thread
*thread
)
594 if (thread
->suspend
+ thread
->process
->suspend
> 0) stop_thread( thread
);
597 /* suspend a thread */
598 int suspend_thread( struct thread
*thread
)
600 int old_count
= thread
->suspend
;
601 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
603 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
605 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
609 /* resume a thread */
610 int resume_thread( struct thread
*thread
)
612 int old_count
= thread
->suspend
;
613 if (thread
->suspend
> 0)
615 if (!(--thread
->suspend
)) resume_delayed_debug_events( thread
);
616 if (!(thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
621 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
622 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
626 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
630 /* remove a thread from an object wait queue */
631 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
633 list_remove( &entry
->entry
);
634 release_object( obj
);
637 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
639 return entry
->wait
->thread
;
642 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
644 return entry
->wait
->select
;
647 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
649 return entry
->wait
->key
;
652 void make_wait_abandoned( struct wait_queue_entry
*entry
)
654 entry
->wait
->abandoned
= 1;
658 static unsigned int end_wait( struct thread
*thread
, unsigned int status
)
660 struct thread_wait
*wait
= thread
->wait
;
661 struct wait_queue_entry
*entry
;
665 thread
->wait
= wait
->next
;
667 if (status
< wait
->count
) /* wait satisfied, tell it to the objects */
669 if (wait
->select
== SELECT_WAIT_ALL
)
671 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
672 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
676 entry
= wait
->queues
+ status
;
677 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
679 if (wait
->abandoned
) status
+= STATUS_ABANDONED_WAIT_0
;
681 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
682 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
683 if (wait
->user
) remove_timeout_user( wait
->user
);
688 /* build the thread wait structure */
689 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
690 int flags
, abstime_t when
)
692 struct thread_wait
*wait
;
693 struct wait_queue_entry
*entry
;
696 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
697 wait
->next
= current
->wait
;
698 wait
->thread
= current
;
701 wait
->select
= select_op
->op
;
706 current
->wait
= wait
;
708 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
710 struct object
*obj
= objects
[i
];
712 if (!obj
->ops
->add_queue( obj
, entry
))
715 end_wait( current
, get_error() );
722 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
723 int flags
, abstime_t when
)
725 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
729 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
731 for (i
= 0; i
< count
; i
++)
732 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
735 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, when
);
737 while (i
> 0) release_object( objects
[--i
] );
741 /* check if the thread waiting condition is satisfied */
742 static int check_wait( struct thread
*thread
)
745 struct thread_wait
*wait
= thread
->wait
;
746 struct wait_queue_entry
*entry
;
750 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
751 return STATUS_KERNEL_APC
;
753 /* Suspended threads may not acquire locks, but they can run system APCs */
754 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
756 if (wait
->select
== SELECT_WAIT_ALL
)
759 /* Note: we must check them all anyway, as some objects may
760 * want to do something when signaled, even if others are not */
761 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
762 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
763 if (!not_ok
) return STATUS_WAIT_0
;
767 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
768 if (entry
->obj
->ops
->signaled( entry
->obj
, entry
)) return i
;
771 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
772 if (wait
->when
>= 0 && wait
->when
<= current_time
) return STATUS_TIMEOUT
;
773 if (wait
->when
< 0 && -wait
->when
<= monotonic_time
) return STATUS_TIMEOUT
;
777 /* send the wakeup signal to a thread */
778 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
780 struct wake_up_reply reply
;
783 memset( &reply
, 0, sizeof(reply
) );
784 reply
.cookie
= cookie
;
785 reply
.signaled
= signaled
;
786 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
789 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
790 else if (errno
== EPIPE
)
791 kill_thread( thread
, 0 ); /* normal death */
793 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
797 /* attempt to wake up a thread */
798 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
799 int wake_thread( struct thread
*thread
)
804 for (count
= 0; thread
->wait
; count
++)
806 if ((signaled
= check_wait( thread
)) == -1) break;
808 cookie
= thread
->wait
->cookie
;
809 signaled
= end_wait( thread
, signaled
);
810 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
811 if (cookie
&& send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
813 if (!count
) count
= -1;
820 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
821 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
823 struct thread_wait
*wait
= entry
->wait
;
824 struct thread
*thread
= wait
->thread
;
828 if (thread
->wait
!= wait
) return 0; /* not the current wait */
829 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
831 assert( wait
->select
!= SELECT_WAIT_ALL
);
833 cookie
= wait
->cookie
;
834 signaled
= end_wait( thread
, entry
- wait
->queues
);
835 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
837 if (!cookie
|| send_thread_wakeup( thread
, cookie
, signaled
) != -1)
838 wake_thread( thread
); /* check other waits too */
843 /* thread wait timeout */
844 static void thread_timeout( void *ptr
)
846 struct thread_wait
*wait
= ptr
;
847 struct thread
*thread
= wait
->thread
;
848 client_ptr_t cookie
= wait
->cookie
;
851 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
852 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
854 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
855 end_wait( thread
, STATUS_TIMEOUT
);
858 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
859 /* check if other objects have become signaled in the meantime */
860 wake_thread( thread
);
863 /* try signaling an event flag, a semaphore or a mutex */
864 static int signal_object( obj_handle_t handle
)
869 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
872 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
873 release_object( obj
);
878 /* select on a list of handles */
879 static void select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
880 int flags
, abstime_t when
)
884 struct object
*object
;
886 switch (select_op
->op
)
889 if (!wait_on( select_op
, 0, NULL
, flags
, when
)) return;
893 case SELECT_WAIT_ALL
:
894 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
895 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
897 set_error( STATUS_INVALID_PARAMETER
);
900 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, when
))
904 case SELECT_SIGNAL_AND_WAIT
:
905 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, when
))
907 if (select_op
->signal_and_wait
.signal
)
909 if (!signal_object( select_op
->signal_and_wait
.signal
))
911 end_wait( current
, get_error() );
914 /* check if we woke ourselves up */
915 if (!current
->wait
) return;
919 case SELECT_KEYED_EVENT_WAIT
:
920 case SELECT_KEYED_EVENT_RELEASE
:
921 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
922 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
924 ret
= wait_on( select_op
, 1, &object
, flags
, when
);
925 release_object( object
);
927 current
->wait
->key
= select_op
->keyed_event
.key
;
931 set_error( STATUS_INVALID_PARAMETER
);
935 if ((ret
= check_wait( current
)) != -1)
937 /* condition is already satisfied */
938 set_error( end_wait( current
, ret
));
942 /* now we need to wait */
943 if (current
->wait
->when
!= TIMEOUT_INFINITE
)
945 if (!(current
->wait
->user
= add_timeout_user( abstime_to_timeout(current
->wait
->when
),
946 thread_timeout
, current
->wait
)))
948 end_wait( current
, get_error() );
952 current
->wait
->cookie
= cookie
;
953 set_error( STATUS_PENDING
);
957 /* attempt to wake threads sleeping on the object wait queue */
958 void wake_up( struct object
*obj
, int max
)
963 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
965 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
966 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
967 if (ret
> 0 && max
&& !--max
) break;
968 /* restart at the head of the list since a wake up can change the object wait queue */
969 ptr
= &obj
->wait_queue
;
973 /* return the apc queue to use for a given apc type */
974 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
981 return &thread
->user_apc
;
983 return &thread
->system_apc
;
987 /* check if thread is currently waiting for a (system) apc */
988 static inline int is_in_apc_wait( struct thread
*thread
)
990 return (thread
->process
->suspend
|| thread
->suspend
||
991 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
994 /* queue an existing APC to a given thread */
995 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
999 if (thread
&& thread
->state
== TERMINATED
&& process
)
1002 if (!thread
) /* find a suitable thread inside the process */
1004 struct thread
*candidate
;
1006 /* first try to find a waiting thread */
1007 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1009 if (candidate
->state
== TERMINATED
) continue;
1010 if (is_in_apc_wait( candidate
))
1018 /* then use the first one that accepts a signal */
1019 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1021 if (send_thread_signal( candidate
, SIGUSR1
))
1028 if (!thread
) return 0; /* nothing found */
1029 queue
= get_apc_queue( thread
, apc
->call
.type
);
1033 if (thread
->state
== TERMINATED
) return 0;
1034 queue
= get_apc_queue( thread
, apc
->call
.type
);
1035 /* send signal for system APCs if needed */
1036 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
1038 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
1040 /* cancel a possible previous APC with the same owner */
1041 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
1045 list_add_tail( queue
, &apc
->entry
);
1046 if (!list_prev( queue
, &apc
->entry
)) /* first one */
1047 wake_thread( thread
);
1052 /* queue an async procedure call */
1053 int thread_queue_apc( struct process
*process
, struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
1055 struct thread_apc
*apc
;
1058 if ((apc
= create_apc( owner
, call_data
)))
1060 ret
= queue_apc( process
, thread
, apc
);
1061 release_object( apc
);
1066 /* cancel the async procedure call owned by a specific object */
1067 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
1069 struct thread_apc
*apc
;
1070 struct list
*queue
= get_apc_queue( thread
, type
);
1072 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
1074 if (apc
->owner
!= owner
) continue;
1075 list_remove( &apc
->entry
);
1077 wake_up( &apc
->obj
, 0 );
1078 release_object( apc
);
1083 /* remove the head apc from the queue; the returned object must be released by the caller */
1084 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system
)
1086 struct thread_apc
*apc
= NULL
;
1087 struct list
*ptr
= list_head( system
? &thread
->system_apc
: &thread
->user_apc
);
1091 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1097 /* clear an APC queue, cancelling all the APCs on it */
1098 static void clear_apc_queue( struct list
*queue
)
1102 while ((ptr
= list_head( queue
)))
1104 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1105 list_remove( &apc
->entry
);
1107 wake_up( &apc
->obj
, 0 );
1108 release_object( apc
);
1112 /* add an fd to the inflight list */
1113 /* return list index, or -1 on error */
1114 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1118 if (server
== -1) return -1;
1125 /* first check if we already have an entry for this fd */
1126 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1127 if (thread
->inflight
[i
].client
== client
)
1129 close( thread
->inflight
[i
].server
);
1130 thread
->inflight
[i
].server
= server
;
1134 /* now find a free spot to store it */
1135 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1136 if (thread
->inflight
[i
].client
== -1)
1138 thread
->inflight
[i
].client
= client
;
1139 thread
->inflight
[i
].server
= server
;
1147 /* get an inflight fd and purge it from the list */
1148 /* the fd must be closed when no longer used */
1149 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1153 if (client
== -1) return -1;
1157 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1159 if (thread
->inflight
[i
].client
== client
)
1161 ret
= thread
->inflight
[i
].server
;
1162 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1166 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1170 /* kill a thread on the spot */
1171 void kill_thread( struct thread
*thread
, int violent_death
)
1173 if (thread
->state
== TERMINATED
) return; /* already killed */
1174 thread
->state
= TERMINATED
;
1175 thread
->exit_time
= current_time
;
1176 if (current
== thread
) current
= NULL
;
1178 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1179 thread
->id
, thread
->exit_code
);
1182 while (thread
->wait
) end_wait( thread
, STATUS_THREAD_IS_TERMINATING
);
1183 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1184 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1187 kill_console_processes( thread
, 0 );
1188 debug_exit_thread( thread
);
1189 abandon_mutexes( thread
);
1190 wake_up( &thread
->obj
, 0 );
1191 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1192 cleanup_thread( thread
);
1193 remove_process_thread( thread
->process
, thread
);
1194 release_object( thread
);
1197 /* copy parts of a context structure */
1198 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1200 assert( to
->cpu
== from
->cpu
);
1202 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1203 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1204 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1205 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1206 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1207 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1210 /* return the context flags that correspond to system regs */
1211 /* (system regs are the ones we can't access on the client side) */
1212 static unsigned int get_context_system_regs( enum cpu_type cpu
)
1216 case CPU_x86
: return SERVER_CTX_DEBUG_REGISTERS
;
1217 case CPU_x86_64
: return SERVER_CTX_DEBUG_REGISTERS
;
1218 case CPU_POWERPC
: return 0;
1219 case CPU_ARM
: return SERVER_CTX_DEBUG_REGISTERS
;
1220 case CPU_ARM64
: return SERVER_CTX_DEBUG_REGISTERS
;
1225 /* take a snapshot of currently running threads */
1226 struct thread_snapshot
*thread_snap( int *count
)
1228 struct thread_snapshot
*snapshot
, *ptr
;
1229 struct thread
*thread
;
1232 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1233 if (thread
->state
!= TERMINATED
) total
++;
1234 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
1236 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1238 if (thread
->state
== TERMINATED
) continue;
1239 ptr
->thread
= thread
;
1240 ptr
->count
= thread
->obj
.refcount
;
1241 ptr
->priority
= thread
->priority
;
1242 grab_object( thread
);
1249 /* gets the current impersonation token */
1250 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1253 return thread
->token
;
1255 return thread
->process
->token
;
1258 /* check if a cpu type can be supported on this server */
1259 int is_cpu_supported( enum cpu_type cpu
)
1261 unsigned int prefix_cpu_mask
= get_prefix_cpu_mask();
1263 if (supported_cpus
& prefix_cpu_mask
& CPU_FLAG(cpu
)) return 1;
1264 if (!(supported_cpus
& prefix_cpu_mask
))
1265 set_error( STATUS_NOT_SUPPORTED
);
1266 else if (supported_cpus
& CPU_FLAG(cpu
))
1267 set_error( STATUS_INVALID_IMAGE_WIN_64
); /* server supports it but not the prefix */
1269 set_error( STATUS_INVALID_IMAGE_FORMAT
);
1273 /* return the cpu mask for supported cpus */
1274 unsigned int get_supported_cpu_mask(void)
1276 return supported_cpus
& get_prefix_cpu_mask();
1279 /* create a new thread */
1280 DECL_HANDLER(new_thread
)
1282 struct thread
*thread
;
1283 struct process
*process
;
1284 struct unicode_str name
;
1285 const struct security_descriptor
*sd
;
1286 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1287 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1289 if (!(process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
)))
1291 if (request_fd
!= -1) close( request_fd
);
1295 if (process
!= current
->process
)
1297 if (request_fd
!= -1) /* can't create a request fd in a different process */
1299 close( request_fd
);
1300 set_error( STATUS_INVALID_PARAMETER
);
1303 if (process
->running_threads
) /* only the initial thread can be created in another process */
1305 set_error( STATUS_ACCESS_DENIED
);
1309 else if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1311 if (request_fd
!= -1) close( request_fd
);
1312 set_error( STATUS_INVALID_HANDLE
);
1316 if ((thread
= create_thread( request_fd
, process
, sd
)))
1318 thread
->system_regs
= current
->system_regs
;
1319 if (req
->suspend
) thread
->suspend
++;
1320 reply
->tid
= get_thread_id( thread
);
1321 if ((reply
->handle
= alloc_handle_no_access_check( current
->process
, thread
,
1322 req
->access
, objattr
->attributes
)))
1324 /* thread object will be released when the thread gets killed */
1327 kill_thread( thread
, 1 );
1330 release_object( process
);
1333 /* initialize a new thread */
1334 DECL_HANDLER(init_thread
)
1336 struct process
*process
= current
->process
;
1337 int wait_fd
, reply_fd
;
1339 if ((reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
)) == -1)
1341 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1344 if ((wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
)) == -1)
1346 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1350 if (current
->reply_fd
) /* already initialised */
1352 set_error( STATUS_INVALID_PARAMETER
);
1356 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1358 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1359 current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 );
1360 if (!current
->reply_fd
|| !current
->wait_fd
) return;
1362 if (!is_valid_address(req
->teb
))
1364 set_error( STATUS_INVALID_PARAMETER
);
1368 current
->unix_pid
= req
->unix_pid
;
1369 current
->unix_tid
= req
->unix_tid
;
1370 current
->teb
= req
->teb
;
1371 current
->entry_point
= process
->peb
? req
->entry
: 0;
1373 if (!process
->peb
) /* first thread, initialize the process too */
1375 if (!is_cpu_supported( req
->cpu
)) return;
1376 process
->unix_pid
= current
->unix_pid
;
1377 process
->peb
= req
->entry
;
1378 process
->cpu
= req
->cpu
;
1379 reply
->info_size
= init_process( current
);
1380 if (!process
->parent_id
)
1381 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1383 set_thread_affinity( current
, current
->affinity
);
1387 if (req
->cpu
!= process
->cpu
)
1389 set_error( STATUS_INVALID_PARAMETER
);
1392 if (process
->unix_pid
!= current
->unix_pid
)
1393 process
->unix_pid
= -1; /* can happen with linuxthreads */
1394 init_thread_context( current
);
1395 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, &req
->entry
);
1396 set_thread_affinity( current
, current
->affinity
);
1398 debug_level
= max( debug_level
, req
->debug_level
);
1400 reply
->pid
= get_process_id( process
);
1401 reply
->tid
= get_thread_id( current
);
1402 reply
->version
= SERVER_PROTOCOL_VERSION
;
1403 reply
->server_start
= server_start_time
;
1404 reply
->all_cpus
= supported_cpus
& get_prefix_cpu_mask();
1405 reply
->suspend
= (current
->suspend
|| process
->suspend
);
1409 if (reply_fd
!= -1) close( reply_fd
);
1410 if (wait_fd
!= -1) close( wait_fd
);
1413 /* terminate a thread */
1414 DECL_HANDLER(terminate_thread
)
1416 struct thread
*thread
;
1420 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1422 thread
->exit_code
= req
->exit_code
;
1423 if (thread
!= current
) kill_thread( thread
, 1 );
1427 reply
->last
= (thread
->process
->running_threads
== 1);
1429 release_object( thread
);
1433 /* open a handle to a thread */
1434 DECL_HANDLER(open_thread
)
1436 struct thread
*thread
= get_thread_from_id( req
->tid
);
1441 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1442 release_object( thread
);
1446 /* fetch information about a thread */
1447 DECL_HANDLER(get_thread_info
)
1449 struct thread
*thread
;
1450 obj_handle_t handle
= req
->handle
;
1452 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1453 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_LIMITED_INFORMATION
);
1457 reply
->pid
= get_process_id( thread
->process
);
1458 reply
->tid
= get_thread_id( thread
);
1459 reply
->teb
= thread
->teb
;
1460 reply
->entry_point
= thread
->entry_point
;
1461 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1462 reply
->priority
= thread
->priority
;
1463 reply
->affinity
= thread
->affinity
;
1464 reply
->last
= thread
->process
->running_threads
== 1;
1465 reply
->suspend_count
= thread
->suspend
;
1466 reply
->desc_len
= thread
->desc_len
;
1468 if (thread
->desc
&& get_reply_max_size())
1470 if (thread
->desc_len
<= get_reply_max_size())
1471 set_reply_data( thread
->desc
, thread
->desc_len
);
1473 set_error( STATUS_BUFFER_TOO_SMALL
);
1476 release_object( thread
);
1480 /* fetch information about thread times */
1481 DECL_HANDLER(get_thread_times
)
1483 struct thread
*thread
;
1485 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1487 reply
->creation_time
= thread
->creation_time
;
1488 reply
->exit_time
= thread
->exit_time
;
1490 release_object( thread
);
1494 /* set information about a thread */
1495 DECL_HANDLER(set_thread_info
)
1497 struct thread
*thread
;
1499 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1501 set_thread_info( thread
, req
);
1502 release_object( thread
);
1506 /* suspend a thread */
1507 DECL_HANDLER(suspend_thread
)
1509 struct thread
*thread
;
1511 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1513 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1514 else reply
->count
= suspend_thread( thread
);
1515 release_object( thread
);
1519 /* resume a thread */
1520 DECL_HANDLER(resume_thread
)
1522 struct thread
*thread
;
1524 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1526 reply
->count
= resume_thread( thread
);
1527 release_object( thread
);
1531 /* select on a handle list */
1532 DECL_HANDLER(select
)
1534 select_op_t select_op
;
1535 data_size_t op_size
;
1536 struct thread_apc
*apc
;
1537 const apc_result_t
*result
= get_req_data();
1539 if (get_req_data_size() < sizeof(*result
))
1541 set_error( STATUS_INVALID_PARAMETER
);
1546 set_error( STATUS_INVALID_PARAMETER
);
1550 op_size
= min( get_req_data_size() - sizeof(*result
), sizeof(select_op
) );
1551 memset( &select_op
, 0, sizeof(select_op
) );
1552 memcpy( &select_op
, result
+ 1, op_size
);
1554 /* first store results of previous apc */
1557 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1558 0, &thread_apc_ops
))) return;
1559 apc
->result
= *result
;
1561 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1563 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1564 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1565 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1566 apc
->result
.create_thread
.handle
= handle
;
1567 clear_error(); /* ignore errors from the above calls */
1569 else if (apc
->result
.type
== APC_ASYNC_IO
)
1572 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
1574 wake_up( &apc
->obj
, 0 );
1575 close_handle( current
->process
, req
->prev_apc
);
1576 release_object( apc
);
1579 select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1581 while (get_error() == STATUS_USER_APC
)
1583 if (!(apc
= thread_dequeue_apc( current
, 0 )))
1585 /* Optimization: ignore APC_NONE calls, they are only used to
1586 * wake up a thread, but since we got here the thread woke up already.
1588 if (apc
->call
.type
!= APC_NONE
&&
1589 (reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1591 reply
->call
= apc
->call
;
1592 release_object( apc
);
1596 wake_up( &apc
->obj
, 0 );
1597 release_object( apc
);
1600 if (get_error() == STATUS_KERNEL_APC
)
1602 apc
= thread_dequeue_apc( current
, 1 );
1603 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1604 reply
->call
= apc
->call
;
1608 wake_up( &apc
->obj
, 0 );
1610 release_object( apc
);
1614 /* queue an APC for a thread or process */
1615 DECL_HANDLER(queue_apc
)
1617 struct thread
*thread
= NULL
;
1618 struct process
*process
= NULL
;
1619 struct thread_apc
*apc
;
1621 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1623 switch (apc
->call
.type
)
1627 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1629 case APC_VIRTUAL_ALLOC
:
1630 case APC_VIRTUAL_FREE
:
1631 case APC_VIRTUAL_PROTECT
:
1632 case APC_VIRTUAL_FLUSH
:
1633 case APC_VIRTUAL_LOCK
:
1634 case APC_VIRTUAL_UNLOCK
:
1635 case APC_UNMAP_VIEW
:
1636 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1638 case APC_VIRTUAL_QUERY
:
1639 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1642 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1643 if (process
&& process
!= current
->process
)
1645 /* duplicate the handle into the target process */
1646 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1647 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1648 if (handle
) apc
->call
.map_view
.handle
= handle
;
1651 release_object( process
);
1656 case APC_CREATE_THREAD
:
1657 case APC_BREAK_PROCESS
:
1658 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1661 set_error( STATUS_INVALID_PARAMETER
);
1667 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1668 release_object( thread
);
1672 reply
->self
= (process
== current
->process
);
1675 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1678 if (queue_apc( process
, NULL
, apc
))
1680 apc
->caller
= (struct thread
*)grab_object( current
);
1681 reply
->handle
= handle
;
1685 close_handle( current
->process
, handle
);
1686 set_error( STATUS_PROCESS_IS_TERMINATING
);
1690 release_object( process
);
1693 release_object( apc
);
1696 /* Get the result of an APC call */
1697 DECL_HANDLER(get_apc_result
)
1699 struct thread_apc
*apc
;
1701 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1702 0, &thread_apc_ops
))) return;
1704 if (apc
->executed
) reply
->result
= apc
->result
;
1705 else set_error( STATUS_PENDING
);
1707 /* close the handle directly to avoid an extra round-trip */
1708 close_handle( current
->process
, req
->handle
);
1709 release_object( apc
);
1712 /* retrieve the current context of a thread */
1713 DECL_HANDLER(get_thread_context
)
1715 struct thread
*thread
;
1718 if (get_reply_max_size() < sizeof(context_t
))
1720 set_error( STATUS_INVALID_PARAMETER
);
1723 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1724 reply
->self
= (thread
== current
);
1726 if (thread
!= current
&& !thread
->context
)
1728 /* thread is not suspended, retry (if it's still running) */
1729 if (thread
->state
== RUNNING
)
1731 set_error( STATUS_PENDING
);
1734 release_object( thread
);
1735 /* make sure we have suspend access */
1736 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1737 suspend_thread( thread
);
1740 else set_error( STATUS_UNSUCCESSFUL
);
1742 else if ((context
= set_reply_data_size( sizeof(context_t
) )))
1744 unsigned int flags
= get_context_system_regs( thread
->process
->cpu
);
1746 memset( context
, 0, sizeof(context_t
) );
1747 context
->cpu
= thread
->process
->cpu
;
1748 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1749 if (req
->flags
& flags
) get_thread_context( thread
, context
, req
->flags
& flags
);
1751 release_object( thread
);
1754 /* set the current context of a thread */
1755 DECL_HANDLER(set_thread_context
)
1757 struct thread
*thread
;
1758 const context_t
*context
= get_req_data();
1760 if (get_req_data_size() < sizeof(context_t
))
1762 set_error( STATUS_INVALID_PARAMETER
);
1765 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1766 reply
->self
= (thread
== current
);
1768 if (thread
!= current
&& !thread
->context
)
1770 /* thread is not suspended, retry (if it's still running) */
1771 if (thread
->state
== RUNNING
)
1773 set_error( STATUS_PENDING
);
1776 release_object( thread
);
1777 /* make sure we have suspend access */
1778 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1779 suspend_thread( thread
);
1782 else set_error( STATUS_UNSUCCESSFUL
);
1784 else if (context
->cpu
== thread
->process
->cpu
)
1786 unsigned int system_flags
= get_context_system_regs(context
->cpu
) & context
->flags
;
1787 unsigned int client_flags
= context
->flags
& ~system_flags
;
1789 if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1790 if (thread
->context
&& !get_error()) copy_context( thread
->context
, context
, client_flags
);
1792 else set_error( STATUS_INVALID_PARAMETER
);
1794 release_object( thread
);
1797 /* retrieve the suspended context of a thread */
1798 DECL_HANDLER(get_suspend_context
)
1800 if (get_reply_max_size() < sizeof(context_t
))
1802 set_error( STATUS_INVALID_PARAMETER
);
1806 if (current
->suspend_context
)
1808 if (current
->suspend_context
->flags
)
1809 set_reply_data_ptr( current
->suspend_context
, sizeof(context_t
) );
1811 free( current
->suspend_context
);
1812 if (current
->context
== current
->suspend_context
)
1814 current
->context
= NULL
;
1815 stop_thread_if_suspended( current
);
1817 current
->suspend_context
= NULL
;
1819 else set_error( STATUS_INVALID_PARAMETER
); /* not suspended, shouldn't happen */
1822 /* store the suspended context of a thread */
1823 DECL_HANDLER(set_suspend_context
)
1825 const context_t
*context
= get_req_data();
1827 if (get_req_data_size() < sizeof(context_t
))
1829 set_error( STATUS_INVALID_PARAMETER
);
1833 if (current
->context
|| context
->cpu
!= current
->process
->cpu
)
1835 /* nested suspend or exception, shouldn't happen */
1836 set_error( STATUS_INVALID_PARAMETER
);
1838 else if ((current
->suspend_context
= mem_alloc( sizeof(context_t
) )))
1840 memcpy( current
->suspend_context
, get_req_data(), sizeof(context_t
) );
1841 current
->suspend_context
->flags
= 0; /* to keep track of what is modified */
1842 current
->context
= current
->suspend_context
;
1846 /* fetch a selector entry for a thread */
1847 DECL_HANDLER(get_selector_entry
)
1849 struct thread
*thread
;
1850 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1852 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1853 release_object( thread
);