2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
32 #include <sys/types.h>
43 #define WIN32_NO_STATUS
57 static const unsigned int supported_cpus
= CPU_FLAG(CPU_x86
);
58 #elif defined(__x86_64__)
59 static const unsigned int supported_cpus
= CPU_FLAG(CPU_x86_64
) | CPU_FLAG(CPU_x86
);
60 #elif defined(__powerpc__)
61 static const unsigned int supported_cpus
= CPU_FLAG(CPU_POWERPC
);
62 #elif defined(__arm__)
63 static const unsigned int supported_cpus
= CPU_FLAG(CPU_ARM
);
64 #elif defined(__aarch64__)
65 static const unsigned int supported_cpus
= CPU_FLAG(CPU_ARM64
);
67 #error Unsupported CPU
74 struct thread_wait
*next
; /* next wait structure for this thread */
75 struct thread
*thread
; /* owner thread */
76 int count
; /* count of objects */
78 enum select_op select
;
79 client_ptr_t cookie
; /* magic cookie to return to client */
81 struct timeout_user
*user
;
82 struct wait_queue_entry queues
[1];
85 /* asynchronous procedure calls */
89 struct object obj
; /* object header */
90 struct list entry
; /* queue linked list */
91 struct thread
*caller
; /* thread that queued this apc */
92 struct object
*owner
; /* object that queued this apc */
93 int executed
; /* has it been executed by the client? */
94 apc_call_t call
; /* call arguments */
95 apc_result_t result
; /* call results once executed */
98 static void dump_thread_apc( struct object
*obj
, int verbose
);
99 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
100 static void thread_apc_destroy( struct object
*obj
);
101 static void clear_apc_queue( struct list
*queue
);
103 static const struct object_ops thread_apc_ops
=
105 sizeof(struct thread_apc
), /* size */
106 dump_thread_apc
, /* dump */
107 no_get_type
, /* get_type */
108 add_queue
, /* add_queue */
109 remove_queue
, /* remove_queue */
110 thread_apc_signaled
, /* signaled */
111 no_satisfied
, /* satisfied */
112 no_signal
, /* signal */
113 no_get_fd
, /* get_fd */
114 no_map_access
, /* map_access */
115 default_get_sd
, /* get_sd */
116 default_set_sd
, /* set_sd */
117 no_lookup_name
, /* lookup_name */
118 no_open_file
, /* open_file */
119 no_close_handle
, /* close_handle */
120 thread_apc_destroy
/* destroy */
124 /* thread operations */
126 static void dump_thread( struct object
*obj
, int verbose
);
127 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
128 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
129 static void thread_poll_event( struct fd
*fd
, int event
);
130 static void destroy_thread( struct object
*obj
);
132 static const struct object_ops thread_ops
=
134 sizeof(struct thread
), /* size */
135 dump_thread
, /* dump */
136 no_get_type
, /* get_type */
137 add_queue
, /* add_queue */
138 remove_queue
, /* remove_queue */
139 thread_signaled
, /* signaled */
140 no_satisfied
, /* satisfied */
141 no_signal
, /* signal */
142 no_get_fd
, /* get_fd */
143 thread_map_access
, /* map_access */
144 default_get_sd
, /* get_sd */
145 default_set_sd
, /* set_sd */
146 no_lookup_name
, /* lookup_name */
147 no_open_file
, /* open_file */
148 no_close_handle
, /* close_handle */
149 destroy_thread
/* destroy */
152 static const struct fd_ops thread_fd_ops
=
154 NULL
, /* get_poll_events */
155 thread_poll_event
, /* poll_event */
157 NULL
, /* get_fd_type */
159 NULL
, /* queue_async */
160 NULL
, /* reselect_async */
161 NULL
/* cancel_async */
164 static struct list thread_list
= LIST_INIT(thread_list
);
166 /* initialize the structure for a newly allocated thread */
167 static inline void init_thread_structure( struct thread
*thread
)
171 thread
->unix_pid
= -1; /* not known yet */
172 thread
->unix_tid
= -1; /* not known yet */
173 thread
->context
= NULL
;
174 thread
->suspend_context
= NULL
;
176 thread
->debug_ctx
= NULL
;
177 thread
->debug_event
= NULL
;
178 thread
->debug_break
= 0;
179 thread
->queue
= NULL
;
182 thread
->req_data
= NULL
;
183 thread
->req_toread
= 0;
184 thread
->reply_data
= NULL
;
185 thread
->reply_towrite
= 0;
186 thread
->request_fd
= NULL
;
187 thread
->reply_fd
= NULL
;
188 thread
->wait_fd
= NULL
;
189 thread
->state
= RUNNING
;
190 thread
->exit_code
= 0;
191 thread
->priority
= 0;
193 thread
->desktop_users
= 0;
194 thread
->token
= NULL
;
196 thread
->creation_time
= current_time
;
197 thread
->exit_time
= 0;
199 list_init( &thread
->mutex_list
);
200 list_init( &thread
->system_apc
);
201 list_init( &thread
->user_apc
);
203 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
204 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
207 /* check if address looks valid for a client-side data structure (TEB etc.) */
208 static inline int is_valid_address( client_ptr_t addr
)
210 return addr
&& !(addr
% sizeof(int));
213 /* create a new thread */
214 struct thread
*create_thread( int fd
, struct process
*process
)
216 struct thread
*thread
;
218 if (process
->is_terminating
)
220 set_error( STATUS_PROCESS_IS_TERMINATING
);
224 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
226 init_thread_structure( thread
);
228 thread
->process
= (struct process
*)grab_object( process
);
229 thread
->desktop
= process
->desktop
;
230 thread
->affinity
= process
->affinity
;
231 if (!current
) current
= thread
;
233 list_add_head( &thread_list
, &thread
->entry
);
235 if (!(thread
->id
= alloc_ptid( thread
)))
237 release_object( thread
);
240 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
242 release_object( thread
);
246 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
247 add_process_thread( thread
->process
, thread
);
251 /* handle a client event */
252 static void thread_poll_event( struct fd
*fd
, int event
)
254 struct thread
*thread
= get_fd_user( fd
);
255 assert( thread
->obj
.ops
== &thread_ops
);
257 grab_object( thread
);
258 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
259 else if (event
& POLLIN
) read_request( thread
);
260 else if (event
& POLLOUT
) write_reply( thread
);
261 release_object( thread
);
264 /* cleanup everything that is no longer needed by a dead thread */
265 /* used by destroy_thread and kill_thread */
266 static void cleanup_thread( struct thread
*thread
)
270 clear_apc_queue( &thread
->system_apc
);
271 clear_apc_queue( &thread
->user_apc
);
272 free( thread
->req_data
);
273 free( thread
->reply_data
);
274 if (thread
->request_fd
) release_object( thread
->request_fd
);
275 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
276 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
277 free( thread
->suspend_context
);
278 cleanup_clipboard_thread(thread
);
279 destroy_thread_windows( thread
);
280 free_msg_queue( thread
);
281 close_thread_desktop( thread
);
282 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
284 if (thread
->inflight
[i
].client
!= -1)
286 close( thread
->inflight
[i
].server
);
287 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
290 thread
->req_data
= NULL
;
291 thread
->reply_data
= NULL
;
292 thread
->request_fd
= NULL
;
293 thread
->reply_fd
= NULL
;
294 thread
->wait_fd
= NULL
;
295 thread
->context
= NULL
;
296 thread
->suspend_context
= NULL
;
300 /* destroy a thread when its refcount is 0 */
301 static void destroy_thread( struct object
*obj
)
303 struct thread
*thread
= (struct thread
*)obj
;
304 assert( obj
->ops
== &thread_ops
);
306 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
307 list_remove( &thread
->entry
);
308 cleanup_thread( thread
);
309 release_object( thread
->process
);
310 if (thread
->id
) free_ptid( thread
->id
);
311 if (thread
->token
) release_object( thread
->token
);
314 /* dump a thread on stdout for debugging purposes */
315 static void dump_thread( struct object
*obj
, int verbose
)
317 struct thread
*thread
= (struct thread
*)obj
;
318 assert( obj
->ops
== &thread_ops
);
320 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
321 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
324 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
326 struct thread
*mythread
= (struct thread
*)obj
;
327 return (mythread
->state
== TERMINATED
);
330 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
332 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
333 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
334 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
335 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
336 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
339 static void dump_thread_apc( struct object
*obj
, int verbose
)
341 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
342 assert( obj
->ops
== &thread_apc_ops
);
344 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
347 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
349 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
350 return apc
->executed
;
353 static void thread_apc_destroy( struct object
*obj
)
355 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
356 if (apc
->caller
) release_object( apc
->caller
);
357 if (apc
->owner
) release_object( apc
->owner
);
360 /* queue an async procedure call */
361 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
363 struct thread_apc
*apc
;
365 if ((apc
= alloc_object( &thread_apc_ops
)))
367 apc
->call
= *call_data
;
371 apc
->result
.type
= APC_NONE
;
372 if (owner
) grab_object( owner
);
377 /* get a thread pointer from a thread id (and increment the refcount) */
378 struct thread
*get_thread_from_id( thread_id_t id
)
380 struct object
*obj
= get_ptid_entry( id
);
382 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
383 set_error( STATUS_INVALID_CID
);
387 /* get a thread from a handle (and increment the refcount) */
388 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
390 return (struct thread
*)get_handle_obj( current
->process
, handle
,
391 access
, &thread_ops
);
394 /* find a thread from a Unix tid */
395 struct thread
*get_thread_from_tid( int tid
)
397 struct thread
*thread
;
399 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
401 if (thread
->unix_tid
== tid
) return thread
;
406 /* find a thread from a Unix pid */
407 struct thread
*get_thread_from_pid( int pid
)
409 struct thread
*thread
;
411 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
413 if (thread
->unix_pid
== pid
) return thread
;
418 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
421 #ifdef HAVE_SCHED_SETAFFINITY
422 if (thread
->unix_tid
!= -1)
429 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
430 if (affinity
& mask
) CPU_SET( i
, &set
);
432 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
435 if (!ret
) thread
->affinity
= affinity
;
439 affinity_t
get_thread_affinity( struct thread
*thread
)
442 #ifdef HAVE_SCHED_SETAFFINITY
443 if (thread
->unix_tid
!= -1)
448 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
449 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
450 if (CPU_ISSET( i
, &set
)) mask
|= 1 << i
;
453 if (!mask
) mask
= ~0;
457 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
458 #define THREAD_PRIORITY_REALTIME_LOWEST -7
460 /* set all information about a thread */
461 static void set_thread_info( struct thread
*thread
,
462 const struct set_thread_info_request
*req
)
464 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
466 int max
= THREAD_PRIORITY_HIGHEST
;
467 int min
= THREAD_PRIORITY_LOWEST
;
468 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
470 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
471 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
473 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
474 req
->priority
== THREAD_PRIORITY_IDLE
||
475 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
476 thread
->priority
= req
->priority
;
478 set_error( STATUS_INVALID_PARAMETER
);
480 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
482 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
483 set_error( STATUS_INVALID_PARAMETER
);
484 else if (thread
->state
== TERMINATED
)
485 set_error( STATUS_THREAD_IS_TERMINATING
);
486 else if (set_thread_affinity( thread
, req
->affinity
))
489 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
490 security_set_thread_token( thread
, req
->token
);
493 /* stop a thread (at the Unix level) */
494 void stop_thread( struct thread
*thread
)
496 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
497 /* can't stop a thread while initialisation is in progress */
498 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
501 /* stop a thread if it's supposed to be suspended */
502 void stop_thread_if_suspended( struct thread
*thread
)
504 if (thread
->suspend
+ thread
->process
->suspend
> 0) stop_thread( thread
);
507 /* suspend a thread */
508 static int suspend_thread( struct thread
*thread
)
510 int old_count
= thread
->suspend
;
511 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
513 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
515 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
519 /* resume a thread */
520 static int resume_thread( struct thread
*thread
)
522 int old_count
= thread
->suspend
;
523 if (thread
->suspend
> 0)
525 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
530 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
531 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
535 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
539 /* remove a thread from an object wait queue */
540 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
542 list_remove( &entry
->entry
);
543 release_object( obj
);
546 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
548 return entry
->wait
->thread
;
552 static void end_wait( struct thread
*thread
)
554 struct thread_wait
*wait
= thread
->wait
;
555 struct wait_queue_entry
*entry
;
559 thread
->wait
= wait
->next
;
560 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
561 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
562 if (wait
->user
) remove_timeout_user( wait
->user
);
566 /* build the thread wait structure */
567 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
568 int flags
, timeout_t timeout
)
570 struct thread_wait
*wait
;
571 struct wait_queue_entry
*entry
;
574 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
575 wait
->next
= current
->wait
;
576 wait
->thread
= current
;
579 wait
->select
= select_op
->op
;
581 wait
->timeout
= timeout
;
582 current
->wait
= wait
;
584 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
586 struct object
*obj
= objects
[i
];
588 if (!obj
->ops
->add_queue( obj
, entry
))
598 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
599 int flags
, timeout_t timeout
)
601 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
605 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
607 for (i
= 0; i
< count
; i
++)
608 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
611 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, timeout
);
613 while (i
> 0) release_object( objects
[--i
] );
617 /* check if the thread waiting condition is satisfied */
618 static int check_wait( struct thread
*thread
)
621 struct thread_wait
*wait
= thread
->wait
;
622 struct wait_queue_entry
*entry
;
626 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
627 return STATUS_USER_APC
;
629 /* Suspended threads may not acquire locks, but they can run system APCs */
630 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
632 if (wait
->select
== SELECT_WAIT_ALL
)
635 /* Note: we must check them all anyway, as some objects may
636 * want to do something when signaled, even if others are not */
637 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
638 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
639 if (not_ok
) goto other_checks
;
640 /* Wait satisfied: tell it to all objects */
642 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
643 if (entry
->obj
->ops
->satisfied( entry
->obj
, entry
))
644 signaled
= STATUS_ABANDONED_WAIT_0
;
649 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
651 if (!entry
->obj
->ops
->signaled( entry
->obj
, entry
)) continue;
652 /* Wait satisfied: tell it to the object */
654 if (entry
->obj
->ops
->satisfied( entry
->obj
, entry
))
655 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
661 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
662 if (wait
->timeout
<= current_time
) return STATUS_TIMEOUT
;
666 /* send the wakeup signal to a thread */
667 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
669 struct wake_up_reply reply
;
672 memset( &reply
, 0, sizeof(reply
) );
673 reply
.cookie
= cookie
;
674 reply
.signaled
= signaled
;
675 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
678 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
679 else if (errno
== EPIPE
)
680 kill_thread( thread
, 0 ); /* normal death */
682 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
686 /* attempt to wake up a thread */
687 /* return >0 if OK, 0 if the wait condition is still not satisfied */
688 int wake_thread( struct thread
*thread
)
693 for (count
= 0; thread
->wait
; count
++)
695 if ((signaled
= check_wait( thread
)) == -1) break;
697 cookie
= thread
->wait
->cookie
;
698 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
700 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
706 /* thread wait timeout */
707 static void thread_timeout( void *ptr
)
709 struct thread_wait
*wait
= ptr
;
710 struct thread
*thread
= wait
->thread
;
711 client_ptr_t cookie
= wait
->cookie
;
714 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
715 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
717 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
719 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
720 /* check if other objects have become signaled in the meantime */
721 wake_thread( thread
);
724 /* try signaling an event flag, a semaphore or a mutex */
725 static int signal_object( obj_handle_t handle
)
730 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
733 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
734 release_object( obj
);
739 /* select on a list of handles */
740 static timeout_t
select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
741 int flags
, timeout_t timeout
)
746 if (timeout
<= 0) timeout
= current_time
- timeout
;
748 switch (select_op
->op
)
751 if (!wait_on( select_op
, 0, NULL
, flags
, timeout
)) return timeout
;
755 case SELECT_WAIT_ALL
:
756 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
757 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
759 set_error( STATUS_INVALID_PARAMETER
);
762 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, timeout
))
766 case SELECT_SIGNAL_AND_WAIT
:
767 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, timeout
))
769 if (select_op
->signal_and_wait
.signal
)
771 if (!signal_object( select_op
->signal_and_wait
.signal
))
776 /* check if we woke ourselves up */
777 if (!current
->wait
) return timeout
;
782 set_error( STATUS_INVALID_PARAMETER
);
786 if ((ret
= check_wait( current
)) != -1)
788 /* condition is already satisfied */
794 /* now we need to wait */
795 if (current
->wait
->timeout
!= TIMEOUT_INFINITE
)
797 if (!(current
->wait
->user
= add_timeout_user( current
->wait
->timeout
,
798 thread_timeout
, current
->wait
)))
804 current
->wait
->cookie
= cookie
;
805 set_error( STATUS_PENDING
);
809 /* attempt to wake threads sleeping on the object wait queue */
810 void wake_up( struct object
*obj
, int max
)
814 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
816 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
817 if (!wake_thread( get_wait_queue_thread( entry
))) continue;
818 if (max
&& !--max
) break;
819 /* restart at the head of the list since a wake up can change the object wait queue */
820 ptr
= &obj
->wait_queue
;
824 /* return the apc queue to use for a given apc type */
825 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
832 return &thread
->user_apc
;
834 return &thread
->system_apc
;
838 /* check if thread is currently waiting for a (system) apc */
839 static inline int is_in_apc_wait( struct thread
*thread
)
841 return (thread
->process
->suspend
|| thread
->suspend
||
842 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
845 /* queue an existing APC to a given thread */
846 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
850 if (!thread
) /* find a suitable thread inside the process */
852 struct thread
*candidate
;
854 /* first try to find a waiting thread */
855 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
857 if (candidate
->state
== TERMINATED
) continue;
858 if (is_in_apc_wait( candidate
))
866 /* then use the first one that accepts a signal */
867 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
869 if (send_thread_signal( candidate
, SIGUSR1
))
876 if (!thread
) return 0; /* nothing found */
877 queue
= get_apc_queue( thread
, apc
->call
.type
);
881 if (thread
->state
== TERMINATED
) return 0;
882 queue
= get_apc_queue( thread
, apc
->call
.type
);
883 /* send signal for system APCs if needed */
884 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
886 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
888 /* cancel a possible previous APC with the same owner */
889 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
893 list_add_tail( queue
, &apc
->entry
);
894 if (!list_prev( queue
, &apc
->entry
)) /* first one */
895 wake_thread( thread
);
900 /* queue an async procedure call */
901 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
903 struct thread_apc
*apc
;
906 if ((apc
= create_apc( owner
, call_data
)))
908 ret
= queue_apc( NULL
, thread
, apc
);
909 release_object( apc
);
914 /* cancel the async procedure call owned by a specific object */
915 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
917 struct thread_apc
*apc
;
918 struct list
*queue
= get_apc_queue( thread
, type
);
920 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
922 if (apc
->owner
!= owner
) continue;
923 list_remove( &apc
->entry
);
925 wake_up( &apc
->obj
, 0 );
926 release_object( apc
);
931 /* remove the head apc from the queue; the returned object must be released by the caller */
932 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
934 struct thread_apc
*apc
= NULL
;
935 struct list
*ptr
= list_head( &thread
->system_apc
);
937 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
940 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
946 /* clear an APC queue, cancelling all the APCs on it */
947 static void clear_apc_queue( struct list
*queue
)
951 while ((ptr
= list_head( queue
)))
953 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
954 list_remove( &apc
->entry
);
956 wake_up( &apc
->obj
, 0 );
957 release_object( apc
);
961 /* add an fd to the inflight list */
962 /* return list index, or -1 on error */
963 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
967 if (server
== -1) return -1;
974 /* first check if we already have an entry for this fd */
975 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
976 if (thread
->inflight
[i
].client
== client
)
978 close( thread
->inflight
[i
].server
);
979 thread
->inflight
[i
].server
= server
;
983 /* now find a free spot to store it */
984 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
985 if (thread
->inflight
[i
].client
== -1)
987 thread
->inflight
[i
].client
= client
;
988 thread
->inflight
[i
].server
= server
;
994 /* get an inflight fd and purge it from the list */
995 /* the fd must be closed when no longer used */
996 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1000 if (client
== -1) return -1;
1004 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1006 if (thread
->inflight
[i
].client
== client
)
1008 ret
= thread
->inflight
[i
].server
;
1009 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1013 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1017 /* kill a thread on the spot */
1018 void kill_thread( struct thread
*thread
, int violent_death
)
1020 if (thread
->state
== TERMINATED
) return; /* already killed */
1021 thread
->state
= TERMINATED
;
1022 thread
->exit_time
= current_time
;
1023 if (current
== thread
) current
= NULL
;
1025 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1026 thread
->id
, thread
->exit_code
);
1029 while (thread
->wait
) end_wait( thread
);
1030 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1031 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1034 kill_console_processes( thread
, 0 );
1035 debug_exit_thread( thread
);
1036 abandon_mutexes( thread
);
1037 wake_up( &thread
->obj
, 0 );
1038 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1039 cleanup_thread( thread
);
1040 remove_process_thread( thread
->process
, thread
);
1041 release_object( thread
);
1044 /* copy parts of a context structure */
1045 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1047 assert( to
->cpu
== from
->cpu
);
1049 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1050 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1051 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1052 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1053 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1054 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1057 /* return the context flags that correspond to system regs */
1058 /* (system regs are the ones we can't access on the client side) */
1059 static unsigned int get_context_system_regs( enum cpu_type cpu
)
1063 case CPU_x86
: return SERVER_CTX_DEBUG_REGISTERS
;
1064 case CPU_x86_64
: return SERVER_CTX_DEBUG_REGISTERS
;
1065 case CPU_POWERPC
: return 0;
1066 case CPU_ARM
: return 0;
1067 case CPU_ARM64
: return 0;
1072 /* trigger a breakpoint event in a given thread */
1073 void break_thread( struct thread
*thread
)
1077 assert( thread
->context
);
1079 memset( &data
, 0, sizeof(data
) );
1080 data
.exception
.first
= 1;
1081 data
.exception
.exc_code
= STATUS_BREAKPOINT
;
1082 data
.exception
.flags
= EXCEPTION_CONTINUABLE
;
1083 switch (thread
->context
->cpu
)
1086 data
.exception
.address
= thread
->context
->ctl
.i386_regs
.eip
;
1089 data
.exception
.address
= thread
->context
->ctl
.x86_64_regs
.rip
;
1092 data
.exception
.address
= thread
->context
->ctl
.powerpc_regs
.iar
;
1095 data
.exception
.address
= thread
->context
->ctl
.arm_regs
.pc
;
1098 data
.exception
.address
= thread
->context
->ctl
.arm64_regs
.pc
;
1101 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
1102 thread
->debug_break
= 0;
1105 /* take a snapshot of currently running threads */
1106 struct thread_snapshot
*thread_snap( int *count
)
1108 struct thread_snapshot
*snapshot
, *ptr
;
1109 struct thread
*thread
;
1112 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1113 if (thread
->state
!= TERMINATED
) total
++;
1114 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
1116 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1118 if (thread
->state
== TERMINATED
) continue;
1119 ptr
->thread
= thread
;
1120 ptr
->count
= thread
->obj
.refcount
;
1121 ptr
->priority
= thread
->priority
;
1122 grab_object( thread
);
1129 /* gets the current impersonation token */
1130 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1133 return thread
->token
;
1135 return thread
->process
->token
;
1138 /* create a new thread */
1139 DECL_HANDLER(new_thread
)
1141 struct thread
*thread
;
1142 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1144 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1146 if (request_fd
!= -1) close( request_fd
);
1147 set_error( STATUS_INVALID_HANDLE
);
1151 if ((thread
= create_thread( request_fd
, current
->process
)))
1153 if (req
->suspend
) thread
->suspend
++;
1154 reply
->tid
= get_thread_id( thread
);
1155 if ((reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
)))
1157 /* thread object will be released when the thread gets killed */
1160 kill_thread( thread
, 1 );
1164 /* initialize a new thread */
1165 DECL_HANDLER(init_thread
)
1167 unsigned int prefix_cpu_mask
= get_prefix_cpu_mask();
1168 struct process
*process
= current
->process
;
1169 int wait_fd
, reply_fd
;
1171 if ((reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
)) == -1)
1173 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1176 if ((wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
)) == -1)
1178 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1182 if (current
->reply_fd
) /* already initialised */
1184 set_error( STATUS_INVALID_PARAMETER
);
1188 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1190 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1191 current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 );
1192 if (!current
->reply_fd
|| !current
->wait_fd
) return;
1194 if (!is_valid_address(req
->teb
))
1196 set_error( STATUS_INVALID_PARAMETER
);
1200 current
->unix_pid
= req
->unix_pid
;
1201 current
->unix_tid
= req
->unix_tid
;
1202 current
->teb
= req
->teb
;
1204 if (!process
->peb
) /* first thread, initialize the process too */
1206 if (!CPU_FLAG(req
->cpu
) || !(supported_cpus
& prefix_cpu_mask
& CPU_FLAG(req
->cpu
)))
1208 if (!(supported_cpus
& CPU_64BIT_MASK
))
1209 set_error( STATUS_NOT_SUPPORTED
);
1211 set_error( STATUS_NOT_REGISTRY_FILE
); /* server supports it but not the prefix */
1214 process
->unix_pid
= current
->unix_pid
;
1215 process
->peb
= req
->entry
;
1216 process
->cpu
= req
->cpu
;
1217 reply
->info_size
= init_process( current
);
1218 if (!process
->parent
)
1219 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1221 set_thread_affinity( current
, current
->affinity
);
1225 if (req
->cpu
!= process
->cpu
)
1227 set_error( STATUS_INVALID_PARAMETER
);
1230 if (process
->unix_pid
!= current
->unix_pid
)
1231 process
->unix_pid
= -1; /* can happen with linuxthreads */
1232 stop_thread_if_suspended( current
);
1233 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, &req
->entry
);
1234 set_thread_affinity( current
, current
->affinity
);
1236 debug_level
= max( debug_level
, req
->debug_level
);
1238 reply
->pid
= get_process_id( process
);
1239 reply
->tid
= get_thread_id( current
);
1240 reply
->version
= SERVER_PROTOCOL_VERSION
;
1241 reply
->server_start
= server_start_time
;
1242 reply
->all_cpus
= supported_cpus
& prefix_cpu_mask
;
1246 if (reply_fd
!= -1) close( reply_fd
);
1247 if (wait_fd
!= -1) close( wait_fd
);
1250 /* terminate a thread */
1251 DECL_HANDLER(terminate_thread
)
1253 struct thread
*thread
;
1257 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1259 thread
->exit_code
= req
->exit_code
;
1260 if (thread
!= current
) kill_thread( thread
, 1 );
1264 reply
->last
= (thread
->process
->running_threads
== 1);
1266 release_object( thread
);
1270 /* open a handle to a thread */
1271 DECL_HANDLER(open_thread
)
1273 struct thread
*thread
= get_thread_from_id( req
->tid
);
1278 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1279 release_object( thread
);
1283 /* fetch information about a thread */
1284 DECL_HANDLER(get_thread_info
)
1286 struct thread
*thread
;
1287 obj_handle_t handle
= req
->handle
;
1289 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1290 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
1294 reply
->pid
= get_process_id( thread
->process
);
1295 reply
->tid
= get_thread_id( thread
);
1296 reply
->teb
= thread
->teb
;
1297 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1298 reply
->priority
= thread
->priority
;
1299 reply
->affinity
= thread
->affinity
;
1300 reply
->creation_time
= thread
->creation_time
;
1301 reply
->exit_time
= thread
->exit_time
;
1302 reply
->last
= thread
->process
->running_threads
== 1;
1304 release_object( thread
);
1308 /* set information about a thread */
1309 DECL_HANDLER(set_thread_info
)
1311 struct thread
*thread
;
1313 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1315 set_thread_info( thread
, req
);
1316 release_object( thread
);
1320 /* suspend a thread */
1321 DECL_HANDLER(suspend_thread
)
1323 struct thread
*thread
;
1325 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1327 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1328 else reply
->count
= suspend_thread( thread
);
1329 release_object( thread
);
1333 /* resume a thread */
1334 DECL_HANDLER(resume_thread
)
1336 struct thread
*thread
;
1338 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1340 reply
->count
= resume_thread( thread
);
1341 release_object( thread
);
1345 /* select on a handle list */
1346 DECL_HANDLER(select
)
1348 select_op_t select_op
;
1349 data_size_t op_size
;
1350 struct thread_apc
*apc
;
1351 const apc_result_t
*result
= get_req_data();
1353 if (get_req_data_size() < sizeof(*result
))
1355 set_error( STATUS_INVALID_PARAMETER
);
1358 op_size
= min( get_req_data_size() - sizeof(*result
), sizeof(select_op
) );
1359 memset( &select_op
, 0, sizeof(select_op
) );
1360 memcpy( &select_op
, result
+ 1, op_size
);
1362 /* first store results of previous apc */
1365 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1366 0, &thread_apc_ops
))) return;
1367 apc
->result
= *result
;
1369 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1371 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1372 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1373 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1374 apc
->result
.create_thread
.handle
= handle
;
1375 clear_error(); /* ignore errors from the above calls */
1377 else if (apc
->result
.type
== APC_ASYNC_IO
)
1380 async_set_result( apc
->owner
, apc
->result
.async_io
.status
,
1381 apc
->result
.async_io
.total
, apc
->result
.async_io
.apc
);
1383 wake_up( &apc
->obj
, 0 );
1384 close_handle( current
->process
, req
->prev_apc
);
1385 release_object( apc
);
1388 reply
->timeout
= select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1390 if (get_error() == STATUS_USER_APC
)
1394 if (!(apc
= thread_dequeue_apc( current
, !(req
->flags
& SELECT_ALERTABLE
) )))
1396 /* Optimization: ignore APC_NONE calls, they are only used to
1397 * wake up a thread, but since we got here the thread woke up already.
1399 if (apc
->call
.type
!= APC_NONE
)
1401 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1402 reply
->call
= apc
->call
;
1403 release_object( apc
);
1407 wake_up( &apc
->obj
, 0 );
1408 release_object( apc
);
1413 /* queue an APC for a thread or process */
1414 DECL_HANDLER(queue_apc
)
1416 struct thread
*thread
= NULL
;
1417 struct process
*process
= NULL
;
1418 struct thread_apc
*apc
;
1420 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1422 switch (apc
->call
.type
)
1426 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1428 case APC_VIRTUAL_ALLOC
:
1429 case APC_VIRTUAL_FREE
:
1430 case APC_VIRTUAL_PROTECT
:
1431 case APC_VIRTUAL_FLUSH
:
1432 case APC_VIRTUAL_LOCK
:
1433 case APC_VIRTUAL_UNLOCK
:
1434 case APC_UNMAP_VIEW
:
1435 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1437 case APC_VIRTUAL_QUERY
:
1438 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1441 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1442 if (process
&& process
!= current
->process
)
1444 /* duplicate the handle into the target process */
1445 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1446 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1447 if (handle
) apc
->call
.map_view
.handle
= handle
;
1450 release_object( process
);
1455 case APC_CREATE_THREAD
:
1456 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1459 set_error( STATUS_INVALID_PARAMETER
);
1465 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1466 release_object( thread
);
1470 reply
->self
= (process
== current
->process
);
1473 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1476 if (queue_apc( process
, NULL
, apc
))
1478 apc
->caller
= (struct thread
*)grab_object( current
);
1479 reply
->handle
= handle
;
1483 close_handle( current
->process
, handle
);
1484 set_error( STATUS_PROCESS_IS_TERMINATING
);
1488 release_object( process
);
1491 release_object( apc
);
1494 /* Get the result of an APC call */
1495 DECL_HANDLER(get_apc_result
)
1497 struct thread_apc
*apc
;
1499 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1500 0, &thread_apc_ops
))) return;
1501 if (!apc
->executed
) set_error( STATUS_PENDING
);
1504 reply
->result
= apc
->result
;
1505 /* close the handle directly to avoid an extra round-trip */
1506 close_handle( current
->process
, req
->handle
);
1508 release_object( apc
);
1511 /* retrieve the current context of a thread */
1512 DECL_HANDLER(get_thread_context
)
1514 struct thread
*thread
;
1517 if (get_reply_max_size() < sizeof(context_t
))
1519 set_error( STATUS_INVALID_PARAMETER
);
1522 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1523 reply
->self
= (thread
== current
);
1525 if (thread
!= current
&& !thread
->context
)
1527 /* thread is not suspended, retry (if it's still running) */
1528 if (thread
->state
== RUNNING
)
1530 set_error( STATUS_PENDING
);
1533 release_object( thread
);
1534 /* make sure we have suspend access */
1535 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1536 suspend_thread( thread
);
1539 else set_error( STATUS_UNSUCCESSFUL
);
1541 else if ((context
= set_reply_data_size( sizeof(context_t
) )))
1543 unsigned int flags
= get_context_system_regs( thread
->process
->cpu
);
1545 memset( context
, 0, sizeof(context_t
) );
1546 context
->cpu
= thread
->process
->cpu
;
1547 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1548 if (flags
) get_thread_context( thread
, context
, flags
);
1550 release_object( thread
);
1553 /* set the current context of a thread */
1554 DECL_HANDLER(set_thread_context
)
1556 struct thread
*thread
;
1557 const context_t
*context
= get_req_data();
1559 if (get_req_data_size() < sizeof(context_t
))
1561 set_error( STATUS_INVALID_PARAMETER
);
1564 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1565 reply
->self
= (thread
== current
);
1567 if (thread
!= current
&& !thread
->context
)
1569 /* thread is not suspended, retry (if it's still running) */
1570 if (thread
->state
== RUNNING
)
1572 set_error( STATUS_PENDING
);
1575 release_object( thread
);
1576 /* make sure we have suspend access */
1577 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1578 suspend_thread( thread
);
1581 else set_error( STATUS_UNSUCCESSFUL
);
1583 else if (context
->cpu
== thread
->process
->cpu
)
1585 unsigned int system_flags
= get_context_system_regs(context
->cpu
) & context
->flags
;
1586 unsigned int client_flags
= context
->flags
& ~system_flags
;
1588 if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1589 if (thread
->context
&& !get_error()) copy_context( thread
->context
, context
, client_flags
);
1591 else set_error( STATUS_INVALID_PARAMETER
);
1593 release_object( thread
);
1596 /* retrieve the suspended context of a thread */
1597 DECL_HANDLER(get_suspend_context
)
1599 if (get_reply_max_size() < sizeof(context_t
))
1601 set_error( STATUS_INVALID_PARAMETER
);
1605 if (current
->suspend_context
)
1607 set_reply_data_ptr( current
->suspend_context
, sizeof(context_t
) );
1608 if (current
->context
== current
->suspend_context
)
1610 current
->context
= NULL
;
1611 stop_thread_if_suspended( current
);
1613 current
->suspend_context
= NULL
;
1615 else set_error( STATUS_INVALID_PARAMETER
); /* not suspended, shouldn't happen */
1618 /* store the suspended context of a thread */
1619 DECL_HANDLER(set_suspend_context
)
1621 const context_t
*context
= get_req_data();
1623 if (get_req_data_size() < sizeof(context_t
))
1625 set_error( STATUS_INVALID_PARAMETER
);
1629 if (current
->context
|| context
->cpu
!= current
->process
->cpu
)
1631 /* nested suspend or exception, shouldn't happen */
1632 set_error( STATUS_INVALID_PARAMETER
);
1634 else if ((current
->suspend_context
= mem_alloc( sizeof(context_t
) )))
1636 memcpy( current
->suspend_context
, get_req_data(), sizeof(context_t
) );
1637 current
->context
= current
->suspend_context
;
1638 if (current
->debug_break
) break_thread( current
);
1642 /* fetch a selector entry for a thread */
1643 DECL_HANDLER(get_selector_entry
)
1645 struct thread
*thread
;
1646 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1648 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1649 release_object( thread
);