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 client_ptr_t cookie
; /* magic cookie to return to client */
80 struct timeout_user
*user
;
81 struct wait_queue_entry queues
[1];
84 /* asynchronous procedure calls */
88 struct object obj
; /* object header */
89 struct list entry
; /* queue linked list */
90 struct thread
*caller
; /* thread that queued this apc */
91 struct object
*owner
; /* object that queued this apc */
92 int executed
; /* has it been executed by the client? */
93 apc_call_t call
; /* call arguments */
94 apc_result_t result
; /* call results once executed */
97 static void dump_thread_apc( struct object
*obj
, int verbose
);
98 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
);
99 static void thread_apc_destroy( struct object
*obj
);
100 static void clear_apc_queue( struct list
*queue
);
102 static const struct object_ops thread_apc_ops
=
104 sizeof(struct thread_apc
), /* size */
105 dump_thread_apc
, /* dump */
106 no_get_type
, /* get_type */
107 add_queue
, /* add_queue */
108 remove_queue
, /* remove_queue */
109 thread_apc_signaled
, /* signaled */
110 no_satisfied
, /* satisfied */
111 no_signal
, /* signal */
112 no_get_fd
, /* get_fd */
113 no_map_access
, /* map_access */
114 default_get_sd
, /* get_sd */
115 default_set_sd
, /* set_sd */
116 no_lookup_name
, /* lookup_name */
117 no_open_file
, /* open_file */
118 no_close_handle
, /* close_handle */
119 thread_apc_destroy
/* destroy */
123 /* thread operations */
125 static void dump_thread( struct object
*obj
, int verbose
);
126 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
127 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
128 static void thread_poll_event( struct fd
*fd
, int event
);
129 static void destroy_thread( struct object
*obj
);
131 static const struct object_ops thread_ops
=
133 sizeof(struct thread
), /* size */
134 dump_thread
, /* dump */
135 no_get_type
, /* get_type */
136 add_queue
, /* add_queue */
137 remove_queue
, /* remove_queue */
138 thread_signaled
, /* signaled */
139 no_satisfied
, /* satisfied */
140 no_signal
, /* signal */
141 no_get_fd
, /* get_fd */
142 thread_map_access
, /* map_access */
143 default_get_sd
, /* get_sd */
144 default_set_sd
, /* set_sd */
145 no_lookup_name
, /* lookup_name */
146 no_open_file
, /* open_file */
147 no_close_handle
, /* close_handle */
148 destroy_thread
/* destroy */
151 static const struct fd_ops thread_fd_ops
=
153 NULL
, /* get_poll_events */
154 thread_poll_event
, /* poll_event */
156 NULL
, /* get_fd_type */
158 NULL
, /* queue_async */
159 NULL
, /* reselect_async */
160 NULL
/* cancel_async */
163 static struct list thread_list
= LIST_INIT(thread_list
);
165 /* initialize the structure for a newly allocated thread */
166 static inline void init_thread_structure( struct thread
*thread
)
170 thread
->unix_pid
= -1; /* not known yet */
171 thread
->unix_tid
= -1; /* not known yet */
172 thread
->context
= NULL
;
173 thread
->suspend_context
= NULL
;
175 thread
->debug_ctx
= NULL
;
176 thread
->debug_event
= NULL
;
177 thread
->debug_break
= 0;
178 thread
->queue
= NULL
;
181 thread
->req_data
= NULL
;
182 thread
->req_toread
= 0;
183 thread
->reply_data
= NULL
;
184 thread
->reply_towrite
= 0;
185 thread
->request_fd
= NULL
;
186 thread
->reply_fd
= NULL
;
187 thread
->wait_fd
= NULL
;
188 thread
->state
= RUNNING
;
189 thread
->exit_code
= 0;
190 thread
->priority
= 0;
192 thread
->desktop_users
= 0;
193 thread
->token
= NULL
;
195 thread
->creation_time
= current_time
;
196 thread
->exit_time
= 0;
198 list_init( &thread
->mutex_list
);
199 list_init( &thread
->system_apc
);
200 list_init( &thread
->user_apc
);
202 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
203 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
206 /* check if address looks valid for a client-side data structure (TEB etc.) */
207 static inline int is_valid_address( client_ptr_t addr
)
209 return addr
&& !(addr
% sizeof(int));
212 /* create a new thread */
213 struct thread
*create_thread( int fd
, struct process
*process
)
215 struct thread
*thread
;
217 if (process
->is_terminating
)
219 set_error( STATUS_PROCESS_IS_TERMINATING
);
223 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
225 init_thread_structure( thread
);
227 thread
->process
= (struct process
*)grab_object( process
);
228 thread
->desktop
= process
->desktop
;
229 thread
->affinity
= process
->affinity
;
230 if (!current
) current
= thread
;
232 list_add_head( &thread_list
, &thread
->entry
);
234 if (!(thread
->id
= alloc_ptid( thread
)))
236 release_object( thread
);
239 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
241 release_object( thread
);
245 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
246 add_process_thread( thread
->process
, thread
);
250 /* handle a client event */
251 static void thread_poll_event( struct fd
*fd
, int event
)
253 struct thread
*thread
= get_fd_user( fd
);
254 assert( thread
->obj
.ops
== &thread_ops
);
256 grab_object( thread
);
257 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
258 else if (event
& POLLIN
) read_request( thread
);
259 else if (event
& POLLOUT
) write_reply( thread
);
260 release_object( thread
);
263 /* cleanup everything that is no longer needed by a dead thread */
264 /* used by destroy_thread and kill_thread */
265 static void cleanup_thread( struct thread
*thread
)
269 clear_apc_queue( &thread
->system_apc
);
270 clear_apc_queue( &thread
->user_apc
);
271 free( thread
->req_data
);
272 free( thread
->reply_data
);
273 if (thread
->request_fd
) release_object( thread
->request_fd
);
274 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
275 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
276 free( thread
->suspend_context
);
277 cleanup_clipboard_thread(thread
);
278 destroy_thread_windows( thread
);
279 free_msg_queue( thread
);
280 close_thread_desktop( thread
);
281 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
283 if (thread
->inflight
[i
].client
!= -1)
285 close( thread
->inflight
[i
].server
);
286 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
289 thread
->req_data
= NULL
;
290 thread
->reply_data
= NULL
;
291 thread
->request_fd
= NULL
;
292 thread
->reply_fd
= NULL
;
293 thread
->wait_fd
= NULL
;
294 thread
->context
= NULL
;
295 thread
->suspend_context
= NULL
;
299 /* destroy a thread when its refcount is 0 */
300 static void destroy_thread( struct object
*obj
)
302 struct thread
*thread
= (struct thread
*)obj
;
303 assert( obj
->ops
== &thread_ops
);
305 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
306 list_remove( &thread
->entry
);
307 cleanup_thread( thread
);
308 release_object( thread
->process
);
309 if (thread
->id
) free_ptid( thread
->id
);
310 if (thread
->token
) release_object( thread
->token
);
313 /* dump a thread on stdout for debugging purposes */
314 static void dump_thread( struct object
*obj
, int verbose
)
316 struct thread
*thread
= (struct thread
*)obj
;
317 assert( obj
->ops
== &thread_ops
);
319 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
320 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
323 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
325 struct thread
*mythread
= (struct thread
*)obj
;
326 return (mythread
->state
== TERMINATED
);
329 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
331 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
332 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
333 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
334 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
335 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
338 static void dump_thread_apc( struct object
*obj
, int verbose
)
340 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
341 assert( obj
->ops
== &thread_apc_ops
);
343 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
346 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
)
348 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
349 return apc
->executed
;
352 static void thread_apc_destroy( struct object
*obj
)
354 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
355 if (apc
->caller
) release_object( apc
->caller
);
356 if (apc
->owner
) release_object( apc
->owner
);
359 /* queue an async procedure call */
360 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
362 struct thread_apc
*apc
;
364 if ((apc
= alloc_object( &thread_apc_ops
)))
366 apc
->call
= *call_data
;
370 apc
->result
.type
= APC_NONE
;
371 if (owner
) grab_object( owner
);
376 /* get a thread pointer from a thread id (and increment the refcount) */
377 struct thread
*get_thread_from_id( thread_id_t id
)
379 struct object
*obj
= get_ptid_entry( id
);
381 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
382 set_error( STATUS_INVALID_CID
);
386 /* get a thread from a handle (and increment the refcount) */
387 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
389 return (struct thread
*)get_handle_obj( current
->process
, handle
,
390 access
, &thread_ops
);
393 /* find a thread from a Unix tid */
394 struct thread
*get_thread_from_tid( int tid
)
396 struct thread
*thread
;
398 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
400 if (thread
->unix_tid
== tid
) return thread
;
405 /* find a thread from a Unix pid */
406 struct thread
*get_thread_from_pid( int pid
)
408 struct thread
*thread
;
410 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
412 if (thread
->unix_pid
== pid
) return thread
;
417 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
420 #ifdef HAVE_SCHED_SETAFFINITY
421 if (thread
->unix_tid
!= -1)
428 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
429 if (affinity
& mask
) CPU_SET( i
, &set
);
431 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
434 if (!ret
) thread
->affinity
= affinity
;
438 affinity_t
get_thread_affinity( struct thread
*thread
)
441 #ifdef HAVE_SCHED_SETAFFINITY
442 if (thread
->unix_tid
!= -1)
447 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
448 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
449 if (CPU_ISSET( i
, &set
)) mask
|= 1 << i
;
452 if (!mask
) mask
= ~0;
456 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
457 #define THREAD_PRIORITY_REALTIME_LOWEST -7
459 /* set all information about a thread */
460 static void set_thread_info( struct thread
*thread
,
461 const struct set_thread_info_request
*req
)
463 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
465 int max
= THREAD_PRIORITY_HIGHEST
;
466 int min
= THREAD_PRIORITY_LOWEST
;
467 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
469 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
470 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
472 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
473 req
->priority
== THREAD_PRIORITY_IDLE
||
474 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
475 thread
->priority
= req
->priority
;
477 set_error( STATUS_INVALID_PARAMETER
);
479 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
481 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
482 set_error( STATUS_INVALID_PARAMETER
);
483 else if (thread
->state
== TERMINATED
)
484 set_error( STATUS_THREAD_IS_TERMINATING
);
485 else if (set_thread_affinity( thread
, req
->affinity
))
488 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
489 security_set_thread_token( thread
, req
->token
);
492 /* stop a thread (at the Unix level) */
493 void stop_thread( struct thread
*thread
)
495 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
496 /* can't stop a thread while initialisation is in progress */
497 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
500 /* stop a thread if it's supposed to be suspended */
501 void stop_thread_if_suspended( struct thread
*thread
)
503 if (thread
->suspend
+ thread
->process
->suspend
> 0) stop_thread( thread
);
506 /* suspend a thread */
507 static int suspend_thread( struct thread
*thread
)
509 int old_count
= thread
->suspend
;
510 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
512 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
514 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
518 /* resume a thread */
519 static int resume_thread( struct thread
*thread
)
521 int old_count
= thread
->suspend
;
522 if (thread
->suspend
> 0)
524 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
529 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
530 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
534 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
538 /* remove a thread from an object wait queue */
539 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
541 list_remove( &entry
->entry
);
542 release_object( obj
);
546 static void end_wait( struct thread
*thread
)
548 struct thread_wait
*wait
= thread
->wait
;
549 struct wait_queue_entry
*entry
;
553 thread
->wait
= wait
->next
;
554 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
555 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
556 if (wait
->user
) remove_timeout_user( wait
->user
);
560 /* build the thread wait structure */
561 static int wait_on( unsigned int count
, struct object
*objects
[], int flags
, timeout_t timeout
)
563 struct thread_wait
*wait
;
564 struct wait_queue_entry
*entry
;
567 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
568 wait
->next
= current
->wait
;
569 wait
->thread
= current
;
573 wait
->timeout
= timeout
;
574 current
->wait
= wait
;
576 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
578 struct object
*obj
= objects
[i
];
579 entry
->thread
= current
;
580 if (!obj
->ops
->add_queue( obj
, entry
))
590 /* check if the thread waiting condition is satisfied */
591 static int check_wait( struct thread
*thread
)
594 struct thread_wait
*wait
= thread
->wait
;
595 struct wait_queue_entry
*entry
;
599 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
600 return STATUS_USER_APC
;
602 /* Suspended threads may not acquire locks, but they can run system APCs */
603 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
605 if (wait
->flags
& SELECT_ALL
)
608 /* Note: we must check them all anyway, as some objects may
609 * want to do something when signaled, even if others are not */
610 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
611 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
612 if (not_ok
) goto other_checks
;
613 /* Wait satisfied: tell it to all objects */
615 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
616 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
617 signaled
= STATUS_ABANDONED_WAIT_0
;
622 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
624 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
625 /* Wait satisfied: tell it to the object */
627 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
628 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
634 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
635 if (wait
->timeout
<= current_time
) return STATUS_TIMEOUT
;
639 /* send the wakeup signal to a thread */
640 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
642 struct wake_up_reply reply
;
645 memset( &reply
, 0, sizeof(reply
) );
646 reply
.cookie
= cookie
;
647 reply
.signaled
= signaled
;
648 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
651 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
652 else if (errno
== EPIPE
)
653 kill_thread( thread
, 0 ); /* normal death */
655 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
659 /* attempt to wake up a thread */
660 /* return >0 if OK, 0 if the wait condition is still not satisfied */
661 int wake_thread( struct thread
*thread
)
666 for (count
= 0; thread
->wait
; count
++)
668 if ((signaled
= check_wait( thread
)) == -1) break;
670 cookie
= thread
->wait
->cookie
;
671 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
673 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
679 /* thread wait timeout */
680 static void thread_timeout( void *ptr
)
682 struct thread_wait
*wait
= ptr
;
683 struct thread
*thread
= wait
->thread
;
684 client_ptr_t cookie
= wait
->cookie
;
687 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
688 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
690 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
692 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
693 /* check if other objects have become signaled in the meantime */
694 wake_thread( thread
);
697 /* try signaling an event flag, a semaphore or a mutex */
698 static int signal_object( obj_handle_t handle
)
703 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
706 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
707 release_object( obj
);
712 /* select on a list of handles */
713 static timeout_t
select_on( unsigned int count
, client_ptr_t cookie
, const obj_handle_t
*handles
,
714 int flags
, timeout_t timeout
, obj_handle_t signal_obj
)
718 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
720 if (timeout
<= 0) timeout
= current_time
- timeout
;
722 if (count
> MAXIMUM_WAIT_OBJECTS
)
724 set_error( STATUS_INVALID_PARAMETER
);
727 for (i
= 0; i
< count
; i
++)
729 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
733 if (i
< count
) goto done
;
734 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
736 /* signal the object */
739 if (!signal_object( signal_obj
))
744 /* check if we woke ourselves up */
745 if (!current
->wait
) goto done
;
748 if ((ret
= check_wait( current
)) != -1)
750 /* condition is already satisfied */
756 /* now we need to wait */
757 if (current
->wait
->timeout
!= TIMEOUT_INFINITE
)
759 if (!(current
->wait
->user
= add_timeout_user( current
->wait
->timeout
,
760 thread_timeout
, current
->wait
)))
766 current
->wait
->cookie
= cookie
;
767 set_error( STATUS_PENDING
);
770 while (i
> 0) release_object( objects
[--i
] );
774 /* attempt to wake threads sleeping on the object wait queue */
775 void wake_up( struct object
*obj
, int max
)
779 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
781 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
782 if (!wake_thread( entry
->thread
)) continue;
783 if (max
&& !--max
) break;
784 /* restart at the head of the list since a wake up can change the object wait queue */
785 ptr
= &obj
->wait_queue
;
789 /* return the apc queue to use for a given apc type */
790 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
797 return &thread
->user_apc
;
799 return &thread
->system_apc
;
803 /* check if thread is currently waiting for a (system) apc */
804 static inline int is_in_apc_wait( struct thread
*thread
)
806 return (thread
->process
->suspend
|| thread
->suspend
||
807 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
810 /* queue an existing APC to a given thread */
811 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
815 if (!thread
) /* find a suitable thread inside the process */
817 struct thread
*candidate
;
819 /* first try to find a waiting thread */
820 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
822 if (candidate
->state
== TERMINATED
) continue;
823 if (is_in_apc_wait( candidate
))
831 /* then use the first one that accepts a signal */
832 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
834 if (send_thread_signal( candidate
, SIGUSR1
))
841 if (!thread
) return 0; /* nothing found */
842 queue
= get_apc_queue( thread
, apc
->call
.type
);
846 if (thread
->state
== TERMINATED
) return 0;
847 queue
= get_apc_queue( thread
, apc
->call
.type
);
848 /* send signal for system APCs if needed */
849 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
851 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
853 /* cancel a possible previous APC with the same owner */
854 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
858 list_add_tail( queue
, &apc
->entry
);
859 if (!list_prev( queue
, &apc
->entry
)) /* first one */
860 wake_thread( thread
);
865 /* queue an async procedure call */
866 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
868 struct thread_apc
*apc
;
871 if ((apc
= create_apc( owner
, call_data
)))
873 ret
= queue_apc( NULL
, thread
, apc
);
874 release_object( apc
);
879 /* cancel the async procedure call owned by a specific object */
880 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
882 struct thread_apc
*apc
;
883 struct list
*queue
= get_apc_queue( thread
, type
);
885 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
887 if (apc
->owner
!= owner
) continue;
888 list_remove( &apc
->entry
);
890 wake_up( &apc
->obj
, 0 );
891 release_object( apc
);
896 /* remove the head apc from the queue; the returned object must be released by the caller */
897 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
899 struct thread_apc
*apc
= NULL
;
900 struct list
*ptr
= list_head( &thread
->system_apc
);
902 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
905 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
911 /* clear an APC queue, cancelling all the APCs on it */
912 static void clear_apc_queue( struct list
*queue
)
916 while ((ptr
= list_head( queue
)))
918 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
919 list_remove( &apc
->entry
);
921 wake_up( &apc
->obj
, 0 );
922 release_object( apc
);
926 /* add an fd to the inflight list */
927 /* return list index, or -1 on error */
928 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
932 if (server
== -1) return -1;
939 /* first check if we already have an entry for this fd */
940 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
941 if (thread
->inflight
[i
].client
== client
)
943 close( thread
->inflight
[i
].server
);
944 thread
->inflight
[i
].server
= server
;
948 /* now find a free spot to store it */
949 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
950 if (thread
->inflight
[i
].client
== -1)
952 thread
->inflight
[i
].client
= client
;
953 thread
->inflight
[i
].server
= server
;
959 /* get an inflight fd and purge it from the list */
960 /* the fd must be closed when no longer used */
961 int thread_get_inflight_fd( struct thread
*thread
, int client
)
965 if (client
== -1) return -1;
969 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
971 if (thread
->inflight
[i
].client
== client
)
973 ret
= thread
->inflight
[i
].server
;
974 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
978 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
982 /* kill a thread on the spot */
983 void kill_thread( struct thread
*thread
, int violent_death
)
985 if (thread
->state
== TERMINATED
) return; /* already killed */
986 thread
->state
= TERMINATED
;
987 thread
->exit_time
= current_time
;
988 if (current
== thread
) current
= NULL
;
990 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
991 thread
->id
, thread
->exit_code
);
994 while (thread
->wait
) end_wait( thread
);
995 send_thread_wakeup( thread
, 0, thread
->exit_code
);
996 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
999 kill_console_processes( thread
, 0 );
1000 debug_exit_thread( thread
);
1001 abandon_mutexes( thread
);
1002 wake_up( &thread
->obj
, 0 );
1003 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1004 cleanup_thread( thread
);
1005 remove_process_thread( thread
->process
, thread
);
1006 release_object( thread
);
1009 /* copy parts of a context structure */
1010 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1012 assert( to
->cpu
== from
->cpu
);
1014 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1015 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1016 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1017 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1018 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1019 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1022 /* return the context flags that correspond to system regs */
1023 /* (system regs are the ones we can't access on the client side) */
1024 static unsigned int get_context_system_regs( enum cpu_type cpu
)
1028 case CPU_x86
: return SERVER_CTX_DEBUG_REGISTERS
;
1029 case CPU_x86_64
: return SERVER_CTX_DEBUG_REGISTERS
;
1030 case CPU_POWERPC
: return 0;
1031 case CPU_ARM
: return 0;
1032 case CPU_ARM64
: return 0;
1037 /* trigger a breakpoint event in a given thread */
1038 void break_thread( struct thread
*thread
)
1042 assert( thread
->context
);
1044 memset( &data
, 0, sizeof(data
) );
1045 data
.exception
.first
= 1;
1046 data
.exception
.exc_code
= STATUS_BREAKPOINT
;
1047 data
.exception
.flags
= EXCEPTION_CONTINUABLE
;
1048 switch (thread
->context
->cpu
)
1051 data
.exception
.address
= thread
->context
->ctl
.i386_regs
.eip
;
1054 data
.exception
.address
= thread
->context
->ctl
.x86_64_regs
.rip
;
1057 data
.exception
.address
= thread
->context
->ctl
.powerpc_regs
.iar
;
1060 data
.exception
.address
= thread
->context
->ctl
.arm_regs
.pc
;
1063 data
.exception
.address
= thread
->context
->ctl
.arm64_regs
.pc
;
1066 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
1067 thread
->debug_break
= 0;
1070 /* take a snapshot of currently running threads */
1071 struct thread_snapshot
*thread_snap( int *count
)
1073 struct thread_snapshot
*snapshot
, *ptr
;
1074 struct thread
*thread
;
1077 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1078 if (thread
->state
!= TERMINATED
) total
++;
1079 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
1081 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1083 if (thread
->state
== TERMINATED
) continue;
1084 ptr
->thread
= thread
;
1085 ptr
->count
= thread
->obj
.refcount
;
1086 ptr
->priority
= thread
->priority
;
1087 grab_object( thread
);
1094 /* gets the current impersonation token */
1095 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1098 return thread
->token
;
1100 return thread
->process
->token
;
1103 /* create a new thread */
1104 DECL_HANDLER(new_thread
)
1106 struct thread
*thread
;
1107 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1109 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1111 if (request_fd
!= -1) close( request_fd
);
1112 set_error( STATUS_INVALID_HANDLE
);
1116 if ((thread
= create_thread( request_fd
, current
->process
)))
1118 if (req
->suspend
) thread
->suspend
++;
1119 reply
->tid
= get_thread_id( thread
);
1120 if ((reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
)))
1122 /* thread object will be released when the thread gets killed */
1125 kill_thread( thread
, 1 );
1129 /* initialize a new thread */
1130 DECL_HANDLER(init_thread
)
1132 unsigned int prefix_cpu_mask
= get_prefix_cpu_mask();
1133 struct process
*process
= current
->process
;
1134 int wait_fd
, reply_fd
;
1136 if ((reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
)) == -1)
1138 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1141 if ((wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
)) == -1)
1143 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1147 if (current
->reply_fd
) /* already initialised */
1149 set_error( STATUS_INVALID_PARAMETER
);
1153 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1155 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1156 current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 );
1157 if (!current
->reply_fd
|| !current
->wait_fd
) return;
1159 if (!is_valid_address(req
->teb
))
1161 set_error( STATUS_INVALID_PARAMETER
);
1165 current
->unix_pid
= req
->unix_pid
;
1166 current
->unix_tid
= req
->unix_tid
;
1167 current
->teb
= req
->teb
;
1169 if (!process
->peb
) /* first thread, initialize the process too */
1171 if (!CPU_FLAG(req
->cpu
) || !(supported_cpus
& prefix_cpu_mask
& CPU_FLAG(req
->cpu
)))
1173 if (!(supported_cpus
& CPU_64BIT_MASK
))
1174 set_error( STATUS_NOT_SUPPORTED
);
1176 set_error( STATUS_NOT_REGISTRY_FILE
); /* server supports it but not the prefix */
1179 process
->unix_pid
= current
->unix_pid
;
1180 process
->peb
= req
->entry
;
1181 process
->cpu
= req
->cpu
;
1182 reply
->info_size
= init_process( current
);
1183 if (!process
->parent
)
1184 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1186 set_thread_affinity( current
, current
->affinity
);
1190 if (req
->cpu
!= process
->cpu
)
1192 set_error( STATUS_INVALID_PARAMETER
);
1195 if (process
->unix_pid
!= current
->unix_pid
)
1196 process
->unix_pid
= -1; /* can happen with linuxthreads */
1197 stop_thread_if_suspended( current
);
1198 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, &req
->entry
);
1199 set_thread_affinity( current
, current
->affinity
);
1201 debug_level
= max( debug_level
, req
->debug_level
);
1203 reply
->pid
= get_process_id( process
);
1204 reply
->tid
= get_thread_id( current
);
1205 reply
->version
= SERVER_PROTOCOL_VERSION
;
1206 reply
->server_start
= server_start_time
;
1207 reply
->all_cpus
= supported_cpus
& prefix_cpu_mask
;
1211 if (reply_fd
!= -1) close( reply_fd
);
1212 if (wait_fd
!= -1) close( wait_fd
);
1215 /* terminate a thread */
1216 DECL_HANDLER(terminate_thread
)
1218 struct thread
*thread
;
1222 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1224 thread
->exit_code
= req
->exit_code
;
1225 if (thread
!= current
) kill_thread( thread
, 1 );
1229 reply
->last
= (thread
->process
->running_threads
== 1);
1231 release_object( thread
);
1235 /* open a handle to a thread */
1236 DECL_HANDLER(open_thread
)
1238 struct thread
*thread
= get_thread_from_id( req
->tid
);
1243 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1244 release_object( thread
);
1248 /* fetch information about a thread */
1249 DECL_HANDLER(get_thread_info
)
1251 struct thread
*thread
;
1252 obj_handle_t handle
= req
->handle
;
1254 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1255 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
1259 reply
->pid
= get_process_id( thread
->process
);
1260 reply
->tid
= get_thread_id( thread
);
1261 reply
->teb
= thread
->teb
;
1262 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1263 reply
->priority
= thread
->priority
;
1264 reply
->affinity
= thread
->affinity
;
1265 reply
->creation_time
= thread
->creation_time
;
1266 reply
->exit_time
= thread
->exit_time
;
1267 reply
->last
= thread
->process
->running_threads
== 1;
1269 release_object( thread
);
1273 /* set information about a thread */
1274 DECL_HANDLER(set_thread_info
)
1276 struct thread
*thread
;
1278 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1280 set_thread_info( thread
, req
);
1281 release_object( thread
);
1285 /* suspend a thread */
1286 DECL_HANDLER(suspend_thread
)
1288 struct thread
*thread
;
1290 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1292 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1293 else reply
->count
= suspend_thread( thread
);
1294 release_object( thread
);
1298 /* resume a thread */
1299 DECL_HANDLER(resume_thread
)
1301 struct thread
*thread
;
1303 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1305 reply
->count
= resume_thread( thread
);
1306 release_object( thread
);
1310 /* select on a handle list */
1311 DECL_HANDLER(select
)
1313 struct thread_apc
*apc
;
1315 const apc_result_t
*result
= get_req_data();
1316 const obj_handle_t
*handles
= (const obj_handle_t
*)(result
+ 1);
1318 if (get_req_data_size() < sizeof(*result
))
1320 set_error( STATUS_INVALID_PARAMETER
);
1323 count
= (get_req_data_size() - sizeof(*result
)) / sizeof(obj_handle_t
);
1325 /* first store results of previous apc */
1328 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1329 0, &thread_apc_ops
))) return;
1330 apc
->result
= *result
;
1332 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1334 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1335 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1336 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1337 apc
->result
.create_thread
.handle
= handle
;
1338 clear_error(); /* ignore errors from the above calls */
1340 else if (apc
->result
.type
== APC_ASYNC_IO
)
1343 async_set_result( apc
->owner
, apc
->result
.async_io
.status
,
1344 apc
->result
.async_io
.total
, apc
->result
.async_io
.apc
);
1346 wake_up( &apc
->obj
, 0 );
1347 close_handle( current
->process
, req
->prev_apc
);
1348 release_object( apc
);
1351 reply
->timeout
= select_on( count
, req
->cookie
, handles
, req
->flags
, req
->timeout
, req
->signal
);
1353 if (get_error() == STATUS_USER_APC
)
1357 if (!(apc
= thread_dequeue_apc( current
, !(req
->flags
& SELECT_ALERTABLE
) )))
1359 /* Optimization: ignore APC_NONE calls, they are only used to
1360 * wake up a thread, but since we got here the thread woke up already.
1362 if (apc
->call
.type
!= APC_NONE
)
1364 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1365 reply
->call
= apc
->call
;
1366 release_object( apc
);
1370 wake_up( &apc
->obj
, 0 );
1371 release_object( apc
);
1376 /* queue an APC for a thread or process */
1377 DECL_HANDLER(queue_apc
)
1379 struct thread
*thread
= NULL
;
1380 struct process
*process
= NULL
;
1381 struct thread_apc
*apc
;
1383 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1385 switch (apc
->call
.type
)
1389 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1391 case APC_VIRTUAL_ALLOC
:
1392 case APC_VIRTUAL_FREE
:
1393 case APC_VIRTUAL_PROTECT
:
1394 case APC_VIRTUAL_FLUSH
:
1395 case APC_VIRTUAL_LOCK
:
1396 case APC_VIRTUAL_UNLOCK
:
1397 case APC_UNMAP_VIEW
:
1398 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1400 case APC_VIRTUAL_QUERY
:
1401 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1404 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1405 if (process
&& process
!= current
->process
)
1407 /* duplicate the handle into the target process */
1408 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1409 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1410 if (handle
) apc
->call
.map_view
.handle
= handle
;
1413 release_object( process
);
1418 case APC_CREATE_THREAD
:
1419 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1422 set_error( STATUS_INVALID_PARAMETER
);
1428 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1429 release_object( thread
);
1433 reply
->self
= (process
== current
->process
);
1436 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1439 if (queue_apc( process
, NULL
, apc
))
1441 apc
->caller
= (struct thread
*)grab_object( current
);
1442 reply
->handle
= handle
;
1446 close_handle( current
->process
, handle
);
1447 set_error( STATUS_PROCESS_IS_TERMINATING
);
1451 release_object( process
);
1454 release_object( apc
);
1457 /* Get the result of an APC call */
1458 DECL_HANDLER(get_apc_result
)
1460 struct thread_apc
*apc
;
1462 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1463 0, &thread_apc_ops
))) return;
1464 if (!apc
->executed
) set_error( STATUS_PENDING
);
1467 reply
->result
= apc
->result
;
1468 /* close the handle directly to avoid an extra round-trip */
1469 close_handle( current
->process
, req
->handle
);
1471 release_object( apc
);
1474 /* retrieve the current context of a thread */
1475 DECL_HANDLER(get_thread_context
)
1477 struct thread
*thread
;
1480 if (get_reply_max_size() < sizeof(context_t
))
1482 set_error( STATUS_INVALID_PARAMETER
);
1485 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1486 reply
->self
= (thread
== current
);
1488 if (thread
!= current
&& !thread
->context
)
1490 /* thread is not suspended, retry (if it's still running) */
1491 if (thread
->state
== RUNNING
)
1493 set_error( STATUS_PENDING
);
1496 release_object( thread
);
1497 /* make sure we have suspend access */
1498 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1499 suspend_thread( thread
);
1502 else set_error( STATUS_UNSUCCESSFUL
);
1504 else if ((context
= set_reply_data_size( sizeof(context_t
) )))
1506 unsigned int flags
= get_context_system_regs( thread
->process
->cpu
);
1508 memset( context
, 0, sizeof(context_t
) );
1509 context
->cpu
= thread
->process
->cpu
;
1510 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1511 if (flags
) get_thread_context( thread
, context
, flags
);
1513 release_object( thread
);
1516 /* set the current context of a thread */
1517 DECL_HANDLER(set_thread_context
)
1519 struct thread
*thread
;
1520 const context_t
*context
= get_req_data();
1522 if (get_req_data_size() < sizeof(context_t
))
1524 set_error( STATUS_INVALID_PARAMETER
);
1527 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1528 reply
->self
= (thread
== current
);
1530 if (thread
!= current
&& !thread
->context
)
1532 /* thread is not suspended, retry (if it's still running) */
1533 if (thread
->state
== RUNNING
)
1535 set_error( STATUS_PENDING
);
1538 release_object( thread
);
1539 /* make sure we have suspend access */
1540 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1541 suspend_thread( thread
);
1544 else set_error( STATUS_UNSUCCESSFUL
);
1546 else if (context
->cpu
== thread
->process
->cpu
)
1548 unsigned int system_flags
= get_context_system_regs(context
->cpu
) & context
->flags
;
1549 unsigned int client_flags
= context
->flags
& ~system_flags
;
1551 if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1552 if (thread
->context
&& !get_error()) copy_context( thread
->context
, context
, client_flags
);
1554 else set_error( STATUS_INVALID_PARAMETER
);
1556 release_object( thread
);
1559 /* retrieve the suspended context of a thread */
1560 DECL_HANDLER(get_suspend_context
)
1562 if (get_reply_max_size() < sizeof(context_t
))
1564 set_error( STATUS_INVALID_PARAMETER
);
1568 if (current
->suspend_context
)
1570 set_reply_data_ptr( current
->suspend_context
, sizeof(context_t
) );
1571 if (current
->context
== current
->suspend_context
)
1573 current
->context
= NULL
;
1574 stop_thread_if_suspended( current
);
1576 current
->suspend_context
= NULL
;
1578 else set_error( STATUS_INVALID_PARAMETER
); /* not suspended, shouldn't happen */
1581 /* store the suspended context of a thread */
1582 DECL_HANDLER(set_suspend_context
)
1584 const context_t
*context
= get_req_data();
1586 if (get_req_data_size() < sizeof(context_t
))
1588 set_error( STATUS_INVALID_PARAMETER
);
1592 if (current
->context
|| context
->cpu
!= current
->process
->cpu
)
1594 /* nested suspend or exception, shouldn't happen */
1595 set_error( STATUS_INVALID_PARAMETER
);
1597 else if ((current
->suspend_context
= mem_alloc( sizeof(context_t
) )))
1599 memcpy( current
->suspend_context
, get_req_data(), sizeof(context_t
) );
1600 current
->context
= current
->suspend_context
;
1601 if (current
->debug_break
) break_thread( current
);
1605 /* fetch a selector entry for a thread */
1606 DECL_HANDLER(get_selector_entry
)
1608 struct thread
*thread
;
1609 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1611 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1612 release_object( thread
);