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
55 extern int rtkit_make_realtime(pid_t process
, pid_t thread
, int priority
);
56 extern int rtkit_undo_realtime(pid_t thread
);
59 static const unsigned int supported_cpus
= CPU_FLAG(CPU_x86
);
60 #elif defined(__x86_64__)
61 static const unsigned int supported_cpus
= CPU_FLAG(CPU_x86_64
) | CPU_FLAG(CPU_x86
);
62 #elif defined(__powerpc__)
63 static const unsigned int supported_cpus
= CPU_FLAG(CPU_POWERPC
);
64 #elif defined(__sparc__)
65 static const unsigned int supported_cpus
= CPU_FLAG(CPU_SPARC
);
66 #elif defined(__arm__)
67 static const unsigned int supported_cpus
= CPU_FLAG(CPU_ARM
);
69 #error Unsupported CPU
76 struct thread_wait
*next
; /* next wait structure for this thread */
77 struct thread
*thread
; /* owner thread */
78 int count
; /* count of objects */
80 client_ptr_t cookie
; /* magic cookie to return to client */
82 struct timeout_user
*user
;
83 struct wait_queue_entry queues
[1];
86 /* asynchronous procedure calls */
90 struct object obj
; /* object header */
91 struct list entry
; /* queue linked list */
92 struct thread
*caller
; /* thread that queued this apc */
93 struct object
*owner
; /* object that queued this apc */
94 int executed
; /* has it been executed by the client? */
95 apc_call_t call
; /* call arguments */
96 apc_result_t result
; /* call results once executed */
99 static void dump_thread_apc( struct object
*obj
, int verbose
);
100 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
);
101 static void thread_apc_destroy( struct object
*obj
);
102 static void clear_apc_queue( struct list
*queue
);
104 static const struct object_ops thread_apc_ops
=
106 sizeof(struct thread_apc
), /* size */
107 dump_thread_apc
, /* dump */
108 no_get_type
, /* get_type */
109 add_queue
, /* add_queue */
110 remove_queue
, /* remove_queue */
111 thread_apc_signaled
, /* signaled */
112 no_satisfied
, /* satisfied */
113 no_signal
, /* signal */
114 no_get_fd
, /* get_fd */
115 no_map_access
, /* map_access */
116 default_get_sd
, /* get_sd */
117 default_set_sd
, /* set_sd */
118 no_lookup_name
, /* lookup_name */
119 no_open_file
, /* open_file */
120 no_close_handle
, /* close_handle */
121 thread_apc_destroy
/* destroy */
125 /* thread operations */
127 static void dump_thread( struct object
*obj
, int verbose
);
128 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
129 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
130 static void thread_poll_event( struct fd
*fd
, int event
);
131 static void destroy_thread( struct object
*obj
);
133 static const struct object_ops thread_ops
=
135 sizeof(struct thread
), /* size */
136 dump_thread
, /* dump */
137 no_get_type
, /* get_type */
138 add_queue
, /* add_queue */
139 remove_queue
, /* remove_queue */
140 thread_signaled
, /* signaled */
141 no_satisfied
, /* satisfied */
142 no_signal
, /* signal */
143 no_get_fd
, /* get_fd */
144 thread_map_access
, /* map_access */
145 default_get_sd
, /* get_sd */
146 default_set_sd
, /* set_sd */
147 no_lookup_name
, /* lookup_name */
148 no_open_file
, /* open_file */
149 no_close_handle
, /* close_handle */
150 destroy_thread
/* destroy */
153 static const struct fd_ops thread_fd_ops
=
155 NULL
, /* get_poll_events */
156 thread_poll_event
, /* poll_event */
158 NULL
, /* get_fd_type */
160 NULL
, /* queue_async */
161 NULL
, /* reselect_async */
162 NULL
/* cancel_async */
165 static struct list thread_list
= LIST_INIT(thread_list
);
167 /* initialize the structure for a newly allocated thread */
168 static inline void init_thread_structure( struct thread
*thread
)
172 thread
->unix_pid
= -1; /* not known yet */
173 thread
->unix_tid
= -1; /* not known yet */
174 thread
->context
= NULL
;
175 thread
->suspend_context
= NULL
;
177 thread
->debug_ctx
= NULL
;
178 thread
->debug_event
= NULL
;
179 thread
->debug_break
= 0;
180 thread
->queue
= NULL
;
183 thread
->req_data
= NULL
;
184 thread
->req_toread
= 0;
185 thread
->reply_data
= NULL
;
186 thread
->reply_towrite
= 0;
187 thread
->request_fd
= NULL
;
188 thread
->reply_fd
= NULL
;
189 thread
->wait_fd
= NULL
;
190 thread
->state
= RUNNING
;
191 thread
->exit_code
= 0;
192 thread
->priority
= 0;
194 thread
->desktop_users
= 0;
195 thread
->token
= NULL
;
197 thread
->creation_time
= current_time
;
198 thread
->exit_time
= 0;
200 list_init( &thread
->mutex_list
);
201 list_init( &thread
->system_apc
);
202 list_init( &thread
->user_apc
);
204 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
205 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
208 /* check if address looks valid for a client-side data structure (TEB etc.) */
209 static inline int is_valid_address( client_ptr_t addr
)
211 return addr
&& !(addr
% sizeof(int));
214 /* create a new thread */
215 struct thread
*create_thread( int fd
, struct process
*process
)
217 struct thread
*thread
;
219 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
221 init_thread_structure( thread
);
223 thread
->process
= (struct process
*)grab_object( process
);
224 thread
->desktop
= process
->desktop
;
225 thread
->affinity
= process
->affinity
;
226 if (!current
) current
= thread
;
228 list_add_head( &thread_list
, &thread
->entry
);
230 if (!(thread
->id
= alloc_ptid( thread
)))
232 release_object( thread
);
235 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
237 release_object( thread
);
241 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
242 add_process_thread( thread
->process
, thread
);
246 /* handle a client event */
247 static void thread_poll_event( struct fd
*fd
, int event
)
249 struct thread
*thread
= get_fd_user( fd
);
250 assert( thread
->obj
.ops
== &thread_ops
);
252 grab_object( thread
);
253 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
254 else if (event
& POLLIN
) read_request( thread
);
255 else if (event
& POLLOUT
) write_reply( thread
);
256 release_object( thread
);
259 /* cleanup everything that is no longer needed by a dead thread */
260 /* used by destroy_thread and kill_thread */
261 static void cleanup_thread( struct thread
*thread
)
265 clear_apc_queue( &thread
->system_apc
);
266 clear_apc_queue( &thread
->user_apc
);
267 free( thread
->req_data
);
268 free( thread
->reply_data
);
269 if (thread
->request_fd
) release_object( thread
->request_fd
);
270 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
271 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
272 free( thread
->suspend_context
);
273 cleanup_clipboard_thread(thread
);
274 destroy_thread_windows( thread
);
275 free_msg_queue( thread
);
276 close_thread_desktop( thread
);
277 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
279 if (thread
->inflight
[i
].client
!= -1)
281 close( thread
->inflight
[i
].server
);
282 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
285 thread
->req_data
= NULL
;
286 thread
->reply_data
= NULL
;
287 thread
->request_fd
= NULL
;
288 thread
->reply_fd
= NULL
;
289 thread
->wait_fd
= NULL
;
290 thread
->context
= NULL
;
291 thread
->suspend_context
= NULL
;
295 /* destroy a thread when its refcount is 0 */
296 static void destroy_thread( struct object
*obj
)
298 struct thread
*thread
= (struct thread
*)obj
;
299 assert( obj
->ops
== &thread_ops
);
301 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
302 list_remove( &thread
->entry
);
303 cleanup_thread( thread
);
304 release_object( thread
->process
);
305 if (thread
->id
) free_ptid( thread
->id
);
306 if (thread
->token
) release_object( thread
->token
);
309 /* dump a thread on stdout for debugging purposes */
310 static void dump_thread( struct object
*obj
, int verbose
)
312 struct thread
*thread
= (struct thread
*)obj
;
313 assert( obj
->ops
== &thread_ops
);
315 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
316 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
319 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
321 struct thread
*mythread
= (struct thread
*)obj
;
322 return (mythread
->state
== TERMINATED
);
325 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
327 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
328 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
329 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
330 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
331 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
334 static void dump_thread_apc( struct object
*obj
, int verbose
)
336 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
337 assert( obj
->ops
== &thread_apc_ops
);
339 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
342 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
)
344 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
345 return apc
->executed
;
348 static void thread_apc_destroy( struct object
*obj
)
350 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
351 if (apc
->caller
) release_object( apc
->caller
);
352 if (apc
->owner
) release_object( apc
->owner
);
355 /* queue an async procedure call */
356 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
358 struct thread_apc
*apc
;
360 if ((apc
= alloc_object( &thread_apc_ops
)))
362 apc
->call
= *call_data
;
366 apc
->result
.type
= APC_NONE
;
367 if (owner
) grab_object( owner
);
372 /* get a thread pointer from a thread id (and increment the refcount) */
373 struct thread
*get_thread_from_id( thread_id_t id
)
375 struct object
*obj
= get_ptid_entry( id
);
377 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
378 set_error( STATUS_INVALID_CID
);
382 /* get a thread from a handle (and increment the refcount) */
383 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
385 return (struct thread
*)get_handle_obj( current
->process
, handle
,
386 access
, &thread_ops
);
389 /* find a thread from a Unix tid */
390 struct thread
*get_thread_from_tid( int tid
)
392 struct thread
*thread
;
394 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
396 if (thread
->unix_tid
== tid
) return thread
;
401 /* find a thread from a Unix pid */
402 struct thread
*get_thread_from_pid( int pid
)
404 struct thread
*thread
;
406 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
408 if (thread
->unix_pid
== pid
) return thread
;
413 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
416 #ifdef HAVE_SCHED_SETAFFINITY
417 if (thread
->unix_tid
!= -1)
424 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
425 if (affinity
& mask
) CPU_SET( i
, &set
);
427 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
430 if (!ret
) thread
->affinity
= affinity
;
434 affinity_t
get_thread_affinity( struct thread
*thread
)
437 #ifdef HAVE_SCHED_SETAFFINITY
438 if (thread
->unix_tid
!= -1)
443 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
444 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
445 if (CPU_ISSET( i
, &set
)) mask
|= 1 << i
;
448 if (!mask
) mask
= ~0;
452 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
453 #define THREAD_PRIORITY_REALTIME_LOWEST -7
455 /* set all information about a thread */
456 static void set_thread_info( struct thread
*thread
,
457 const struct set_thread_info_request
*req
)
459 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
461 int max
= THREAD_PRIORITY_HIGHEST
;
462 int min
= THREAD_PRIORITY_LOWEST
;
463 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
465 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
466 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
468 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
469 req
->priority
== THREAD_PRIORITY_IDLE
||
470 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
472 if (thread
->unix_tid
== -1)
474 else if (thread
->priority
== THREAD_PRIORITY_TIME_CRITICAL
&&
475 req
->priority
!= THREAD_PRIORITY_TIME_CRITICAL
)
476 rtkit_undo_realtime(thread
->unix_tid
);
477 else if (thread
->priority
!= THREAD_PRIORITY_TIME_CRITICAL
&&
478 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
479 rtkit_make_realtime(thread
->unix_pid
, thread
->unix_tid
, 1);
480 thread
->priority
= req
->priority
;
483 set_error( STATUS_INVALID_PARAMETER
);
485 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
487 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
488 set_error( STATUS_INVALID_PARAMETER
);
489 else if (thread
->state
== TERMINATED
)
490 set_error( STATUS_ACCESS_DENIED
);
491 else if (set_thread_affinity( thread
, req
->affinity
))
494 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
495 security_set_thread_token( thread
, req
->token
);
498 /* stop a thread (at the Unix level) */
499 void stop_thread( struct thread
*thread
)
501 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
502 /* can't stop a thread while initialisation is in progress */
503 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
506 /* stop a thread if it's supposed to be suspended */
507 void stop_thread_if_suspended( struct thread
*thread
)
509 if (thread
->suspend
+ thread
->process
->suspend
> 0) stop_thread( thread
);
512 /* suspend a thread */
513 static int suspend_thread( struct thread
*thread
)
515 int old_count
= thread
->suspend
;
516 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
518 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
520 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
524 /* resume a thread */
525 static int resume_thread( struct thread
*thread
)
527 int old_count
= thread
->suspend
;
528 if (thread
->suspend
> 0)
530 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
535 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
536 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
540 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
544 /* remove a thread from an object wait queue */
545 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
547 list_remove( &entry
->entry
);
548 release_object( obj
);
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( unsigned int count
, struct object
*objects
[], int flags
, timeout_t timeout
)
569 struct thread_wait
*wait
;
570 struct wait_queue_entry
*entry
;
573 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
574 wait
->next
= current
->wait
;
575 wait
->thread
= current
;
579 wait
->timeout
= timeout
;
580 current
->wait
= wait
;
582 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
584 struct object
*obj
= objects
[i
];
585 entry
->thread
= current
;
586 if (!obj
->ops
->add_queue( obj
, entry
))
596 /* check if the thread waiting condition is satisfied */
597 static int check_wait( struct thread
*thread
)
600 struct thread_wait
*wait
= thread
->wait
;
601 struct wait_queue_entry
*entry
;
605 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
606 return STATUS_USER_APC
;
608 /* Suspended threads may not acquire locks, but they can run system APCs */
609 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
611 if (wait
->flags
& SELECT_ALL
)
614 /* Note: we must check them all anyway, as some objects may
615 * want to do something when signaled, even if others are not */
616 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
617 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
618 if (not_ok
) goto other_checks
;
619 /* Wait satisfied: tell it to all objects */
621 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
622 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
623 signaled
= STATUS_ABANDONED_WAIT_0
;
628 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
630 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
631 /* Wait satisfied: tell it to the object */
633 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
634 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
640 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
641 if (wait
->timeout
<= current_time
) return STATUS_TIMEOUT
;
645 /* send the wakeup signal to a thread */
646 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
648 struct wake_up_reply reply
;
651 memset( &reply
, 0, sizeof(reply
) );
652 reply
.cookie
= cookie
;
653 reply
.signaled
= signaled
;
654 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
657 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
658 else if (errno
== EPIPE
)
659 kill_thread( thread
, 0 ); /* normal death */
661 fatal_protocol_perror( thread
, "write" );
665 /* attempt to wake up a thread */
666 /* return >0 if OK, 0 if the wait condition is still not satisfied */
667 int wake_thread( struct thread
*thread
)
672 for (count
= 0; thread
->wait
; count
++)
674 if ((signaled
= check_wait( thread
)) == -1) break;
676 cookie
= thread
->wait
->cookie
;
677 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
679 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
685 /* thread wait timeout */
686 static void thread_timeout( void *ptr
)
688 struct thread_wait
*wait
= ptr
;
689 struct thread
*thread
= wait
->thread
;
690 client_ptr_t cookie
= wait
->cookie
;
693 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
694 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
696 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
698 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
699 /* check if other objects have become signaled in the meantime */
700 wake_thread( thread
);
703 /* try signaling an event flag, a semaphore or a mutex */
704 static int signal_object( obj_handle_t handle
)
709 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
712 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
713 release_object( obj
);
718 /* select on a list of handles */
719 static timeout_t
select_on( unsigned int count
, client_ptr_t cookie
, const obj_handle_t
*handles
,
720 int flags
, timeout_t timeout
, obj_handle_t signal_obj
)
724 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
726 if (timeout
<= 0) timeout
= current_time
- timeout
;
728 if (count
> MAXIMUM_WAIT_OBJECTS
)
730 set_error( STATUS_INVALID_PARAMETER
);
733 for (i
= 0; i
< count
; i
++)
735 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
739 if (i
< count
) goto done
;
740 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
742 /* signal the object */
745 if (!signal_object( signal_obj
))
750 /* check if we woke ourselves up */
751 if (!current
->wait
) goto done
;
754 if ((ret
= check_wait( current
)) != -1)
756 /* condition is already satisfied */
762 /* now we need to wait */
763 if (current
->wait
->timeout
!= TIMEOUT_INFINITE
)
765 if (!(current
->wait
->user
= add_timeout_user( current
->wait
->timeout
,
766 thread_timeout
, current
->wait
)))
772 current
->wait
->cookie
= cookie
;
773 set_error( STATUS_PENDING
);
776 while (i
> 0) release_object( objects
[--i
] );
780 /* attempt to wake threads sleeping on the object wait queue */
781 void wake_up( struct object
*obj
, int max
)
785 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
787 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
788 if (!wake_thread( entry
->thread
)) continue;
789 if (max
&& !--max
) break;
790 /* restart at the head of the list since a wake up can change the object wait queue */
791 ptr
= &obj
->wait_queue
;
795 /* return the apc queue to use for a given apc type */
796 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
803 return &thread
->user_apc
;
805 return &thread
->system_apc
;
809 /* check if thread is currently waiting for a (system) apc */
810 static inline int is_in_apc_wait( struct thread
*thread
)
812 return (thread
->process
->suspend
|| thread
->suspend
||
813 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
816 /* queue an existing APC to a given thread */
817 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
821 if (!thread
) /* find a suitable thread inside the process */
823 struct thread
*candidate
;
825 /* first try to find a waiting thread */
826 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
828 if (candidate
->state
== TERMINATED
) continue;
829 if (is_in_apc_wait( candidate
))
837 /* then use the first one that accepts a signal */
838 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
840 if (send_thread_signal( candidate
, SIGUSR1
))
847 if (!thread
) return 0; /* nothing found */
848 queue
= get_apc_queue( thread
, apc
->call
.type
);
852 if (thread
->state
== TERMINATED
) return 0;
853 queue
= get_apc_queue( thread
, apc
->call
.type
);
854 /* send signal for system APCs if needed */
855 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
857 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
859 /* cancel a possible previous APC with the same owner */
860 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
864 list_add_tail( queue
, &apc
->entry
);
865 if (!list_prev( queue
, &apc
->entry
)) /* first one */
866 wake_thread( thread
);
871 /* queue an async procedure call */
872 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
874 struct thread_apc
*apc
;
877 if ((apc
= create_apc( owner
, call_data
)))
879 ret
= queue_apc( NULL
, thread
, apc
);
880 release_object( apc
);
885 /* cancel the async procedure call owned by a specific object */
886 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
888 struct thread_apc
*apc
;
889 struct list
*queue
= get_apc_queue( thread
, type
);
891 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
893 if (apc
->owner
!= owner
) continue;
894 list_remove( &apc
->entry
);
896 wake_up( &apc
->obj
, 0 );
897 release_object( apc
);
902 /* remove the head apc from the queue; the returned object must be released by the caller */
903 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
905 struct thread_apc
*apc
= NULL
;
906 struct list
*ptr
= list_head( &thread
->system_apc
);
908 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
911 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
917 /* clear an APC queue, cancelling all the APCs on it */
918 static void clear_apc_queue( struct list
*queue
)
922 while ((ptr
= list_head( queue
)))
924 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
925 list_remove( &apc
->entry
);
927 wake_up( &apc
->obj
, 0 );
928 release_object( apc
);
932 /* add an fd to the inflight list */
933 /* return list index, or -1 on error */
934 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
938 if (server
== -1) return -1;
945 /* first check if we already have an entry for this fd */
946 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
947 if (thread
->inflight
[i
].client
== client
)
949 close( thread
->inflight
[i
].server
);
950 thread
->inflight
[i
].server
= server
;
954 /* now find a free spot to store it */
955 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
956 if (thread
->inflight
[i
].client
== -1)
958 thread
->inflight
[i
].client
= client
;
959 thread
->inflight
[i
].server
= server
;
965 /* get an inflight fd and purge it from the list */
966 /* the fd must be closed when no longer used */
967 int thread_get_inflight_fd( struct thread
*thread
, int client
)
971 if (client
== -1) return -1;
975 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
977 if (thread
->inflight
[i
].client
== client
)
979 ret
= thread
->inflight
[i
].server
;
980 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
984 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
988 /* kill a thread on the spot */
989 void kill_thread( struct thread
*thread
, int violent_death
)
991 if (thread
->state
== TERMINATED
) return; /* already killed */
992 thread
->state
= TERMINATED
;
993 thread
->exit_time
= current_time
;
994 if (current
== thread
) current
= NULL
;
996 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
997 thread
->id
, thread
->exit_code
);
1000 while (thread
->wait
) end_wait( thread
);
1001 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1002 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1005 kill_console_processes( thread
, 0 );
1006 debug_exit_thread( thread
);
1007 abandon_mutexes( thread
);
1008 wake_up( &thread
->obj
, 0 );
1009 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1010 cleanup_thread( thread
);
1011 remove_process_thread( thread
->process
, thread
);
1012 release_object( thread
);
1015 /* copy parts of a context structure */
1016 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1018 assert( to
->cpu
== from
->cpu
);
1020 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1021 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1022 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1023 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1024 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1025 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1028 /* return the context flags that correspond to system regs */
1029 /* (system regs are the ones we can't access on the client side) */
1030 static unsigned int get_context_system_regs( enum cpu_type cpu
)
1034 case CPU_x86
: return SERVER_CTX_DEBUG_REGISTERS
;
1035 case CPU_x86_64
: return SERVER_CTX_DEBUG_REGISTERS
;
1036 case CPU_POWERPC
: return 0;
1037 case CPU_ARM
: return 0;
1038 case CPU_SPARC
: return 0;
1043 /* trigger a breakpoint event in a given thread */
1044 void break_thread( struct thread
*thread
)
1048 assert( thread
->context
);
1050 memset( &data
, 0, sizeof(data
) );
1051 data
.exception
.first
= 1;
1052 data
.exception
.exc_code
= STATUS_BREAKPOINT
;
1053 data
.exception
.flags
= EXCEPTION_CONTINUABLE
;
1054 switch (thread
->context
->cpu
)
1057 data
.exception
.address
= thread
->context
->ctl
.i386_regs
.eip
;
1060 data
.exception
.address
= thread
->context
->ctl
.x86_64_regs
.rip
;
1063 data
.exception
.address
= thread
->context
->ctl
.powerpc_regs
.iar
;
1066 data
.exception
.address
= thread
->context
->ctl
.sparc_regs
.pc
;
1069 data
.exception
.address
= thread
->context
->ctl
.arm_regs
.pc
;
1072 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
1073 thread
->debug_break
= 0;
1076 /* take a snapshot of currently running threads */
1077 struct thread_snapshot
*thread_snap( int *count
)
1079 struct thread_snapshot
*snapshot
, *ptr
;
1080 struct thread
*thread
;
1083 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1084 if (thread
->state
!= TERMINATED
) total
++;
1085 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
1087 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
1089 if (thread
->state
== TERMINATED
) continue;
1090 ptr
->thread
= thread
;
1091 ptr
->count
= thread
->obj
.refcount
;
1092 ptr
->priority
= thread
->priority
;
1093 grab_object( thread
);
1100 /* gets the current impersonation token */
1101 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1104 return thread
->token
;
1106 return thread
->process
->token
;
1109 /* create a new thread */
1110 DECL_HANDLER(new_thread
)
1112 struct thread
*thread
;
1113 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1115 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1117 if (request_fd
!= -1) close( request_fd
);
1118 set_error( STATUS_INVALID_HANDLE
);
1122 if ((thread
= create_thread( request_fd
, current
->process
)))
1124 if (req
->suspend
) thread
->suspend
++;
1125 reply
->tid
= get_thread_id( thread
);
1126 if ((reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
)))
1128 /* thread object will be released when the thread gets killed */
1131 kill_thread( thread
, 1 );
1135 /* initialize a new thread */
1136 DECL_HANDLER(init_thread
)
1138 unsigned int prefix_cpu_mask
= get_prefix_cpu_mask();
1139 struct process
*process
= current
->process
;
1140 int wait_fd
, reply_fd
;
1142 if ((reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
)) == -1)
1144 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1147 if ((wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
)) == -1)
1149 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1153 if (current
->reply_fd
) /* already initialised */
1155 set_error( STATUS_INVALID_PARAMETER
);
1159 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1161 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1162 current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 );
1163 if (!current
->reply_fd
|| !current
->wait_fd
) return;
1165 if (!is_valid_address(req
->teb
))
1167 set_error( STATUS_INVALID_PARAMETER
);
1171 current
->unix_pid
= req
->unix_pid
;
1172 current
->unix_tid
= req
->unix_tid
;
1173 current
->teb
= req
->teb
;
1175 if (!process
->peb
) /* first thread, initialize the process too */
1177 if (!CPU_FLAG(req
->cpu
) || !(supported_cpus
& prefix_cpu_mask
& CPU_FLAG(req
->cpu
)))
1179 if (!(supported_cpus
& CPU_64BIT_MASK
))
1180 set_error( STATUS_NOT_SUPPORTED
);
1182 set_error( STATUS_NOT_REGISTRY_FILE
); /* server supports it but not the prefix */
1185 process
->unix_pid
= current
->unix_pid
;
1186 process
->peb
= req
->entry
;
1187 process
->cpu
= req
->cpu
;
1188 reply
->info_size
= init_process( current
);
1189 if (!process
->parent
)
1190 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1192 set_thread_affinity( current
, current
->affinity
);
1196 if (req
->cpu
!= process
->cpu
)
1198 set_error( STATUS_INVALID_PARAMETER
);
1201 if (process
->unix_pid
!= current
->unix_pid
)
1202 process
->unix_pid
= -1; /* can happen with linuxthreads */
1203 stop_thread_if_suspended( current
);
1204 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, &req
->entry
);
1205 set_thread_affinity( current
, current
->affinity
);
1207 debug_level
= max( debug_level
, req
->debug_level
);
1209 /* Raced with SetThreadPriority */
1210 if (current
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
1211 rtkit_make_realtime(current
->unix_pid
, current
->unix_tid
, 1);
1213 reply
->pid
= get_process_id( process
);
1214 reply
->tid
= get_thread_id( current
);
1215 reply
->version
= SERVER_PROTOCOL_VERSION
;
1216 reply
->server_start
= server_start_time
;
1217 reply
->all_cpus
= supported_cpus
& prefix_cpu_mask
;
1221 if (reply_fd
!= -1) close( reply_fd
);
1222 if (wait_fd
!= -1) close( wait_fd
);
1225 /* terminate a thread */
1226 DECL_HANDLER(terminate_thread
)
1228 struct thread
*thread
;
1232 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1234 thread
->exit_code
= req
->exit_code
;
1235 if (thread
!= current
) kill_thread( thread
, 1 );
1239 reply
->last
= (thread
->process
->running_threads
== 1);
1241 release_object( thread
);
1245 /* open a handle to a thread */
1246 DECL_HANDLER(open_thread
)
1248 struct thread
*thread
= get_thread_from_id( req
->tid
);
1253 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1254 release_object( thread
);
1258 /* fetch information about a thread */
1259 DECL_HANDLER(get_thread_info
)
1261 struct thread
*thread
;
1262 obj_handle_t handle
= req
->handle
;
1264 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
1265 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
1269 reply
->pid
= get_process_id( thread
->process
);
1270 reply
->tid
= get_thread_id( thread
);
1271 reply
->teb
= thread
->teb
;
1272 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1273 reply
->priority
= thread
->priority
;
1274 reply
->affinity
= thread
->affinity
;
1275 reply
->creation_time
= thread
->creation_time
;
1276 reply
->exit_time
= thread
->exit_time
;
1277 reply
->last
= thread
->process
->running_threads
== 1;
1279 release_object( thread
);
1283 /* set information about a thread */
1284 DECL_HANDLER(set_thread_info
)
1286 struct thread
*thread
;
1288 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1290 set_thread_info( thread
, req
);
1291 release_object( thread
);
1295 /* suspend a thread */
1296 DECL_HANDLER(suspend_thread
)
1298 struct thread
*thread
;
1300 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1302 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1303 else reply
->count
= suspend_thread( thread
);
1304 release_object( thread
);
1308 /* resume a thread */
1309 DECL_HANDLER(resume_thread
)
1311 struct thread
*thread
;
1313 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1315 reply
->count
= resume_thread( thread
);
1316 release_object( thread
);
1320 /* select on a handle list */
1321 DECL_HANDLER(select
)
1323 struct thread_apc
*apc
;
1325 const apc_result_t
*result
= get_req_data();
1326 const obj_handle_t
*handles
= (const obj_handle_t
*)(result
+ 1);
1328 if (get_req_data_size() < sizeof(*result
))
1330 set_error( STATUS_INVALID_PARAMETER
);
1333 count
= (get_req_data_size() - sizeof(*result
)) / sizeof(obj_handle_t
);
1335 /* first store results of previous apc */
1338 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1339 0, &thread_apc_ops
))) return;
1340 apc
->result
= *result
;
1342 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1344 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1345 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1346 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1347 apc
->result
.create_thread
.handle
= handle
;
1348 clear_error(); /* ignore errors from the above calls */
1350 else if (apc
->result
.type
== APC_ASYNC_IO
)
1353 async_set_result( apc
->owner
, apc
->result
.async_io
.status
,
1354 apc
->result
.async_io
.total
, apc
->result
.async_io
.apc
);
1356 wake_up( &apc
->obj
, 0 );
1357 close_handle( current
->process
, req
->prev_apc
);
1358 release_object( apc
);
1361 reply
->timeout
= select_on( count
, req
->cookie
, handles
, req
->flags
, req
->timeout
, req
->signal
);
1363 if (get_error() == STATUS_USER_APC
)
1367 if (!(apc
= thread_dequeue_apc( current
, !(req
->flags
& SELECT_ALERTABLE
) )))
1369 /* Optimization: ignore APC_NONE calls, they are only used to
1370 * wake up a thread, but since we got here the thread woke up already.
1372 if (apc
->call
.type
!= APC_NONE
)
1374 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1375 reply
->call
= apc
->call
;
1376 release_object( apc
);
1380 wake_up( &apc
->obj
, 0 );
1381 release_object( apc
);
1386 /* queue an APC for a thread or process */
1387 DECL_HANDLER(queue_apc
)
1389 struct thread
*thread
= NULL
;
1390 struct process
*process
= NULL
;
1391 struct thread_apc
*apc
;
1393 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1395 switch (apc
->call
.type
)
1399 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1401 case APC_VIRTUAL_ALLOC
:
1402 case APC_VIRTUAL_FREE
:
1403 case APC_VIRTUAL_PROTECT
:
1404 case APC_VIRTUAL_FLUSH
:
1405 case APC_VIRTUAL_LOCK
:
1406 case APC_VIRTUAL_UNLOCK
:
1407 case APC_UNMAP_VIEW
:
1408 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1410 case APC_VIRTUAL_QUERY
:
1411 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1414 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1415 if (process
&& process
!= current
->process
)
1417 /* duplicate the handle into the target process */
1418 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1419 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1420 if (handle
) apc
->call
.map_view
.handle
= handle
;
1423 release_object( process
);
1428 case APC_CREATE_THREAD
:
1429 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1432 set_error( STATUS_INVALID_PARAMETER
);
1438 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1439 release_object( thread
);
1443 reply
->self
= (process
== current
->process
);
1446 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1449 if (queue_apc( process
, NULL
, apc
))
1451 apc
->caller
= (struct thread
*)grab_object( current
);
1452 reply
->handle
= handle
;
1456 close_handle( current
->process
, handle
);
1457 set_error( STATUS_PROCESS_IS_TERMINATING
);
1461 release_object( process
);
1464 release_object( apc
);
1467 /* Get the result of an APC call */
1468 DECL_HANDLER(get_apc_result
)
1470 struct thread_apc
*apc
;
1472 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1473 0, &thread_apc_ops
))) return;
1474 if (!apc
->executed
) set_error( STATUS_PENDING
);
1477 reply
->result
= apc
->result
;
1478 /* close the handle directly to avoid an extra round-trip */
1479 close_handle( current
->process
, req
->handle
);
1481 release_object( apc
);
1484 /* retrieve the current context of a thread */
1485 DECL_HANDLER(get_thread_context
)
1487 struct thread
*thread
;
1490 if (get_reply_max_size() < sizeof(context_t
))
1492 set_error( STATUS_INVALID_PARAMETER
);
1495 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1496 reply
->self
= (thread
== current
);
1498 if (thread
!= current
&& !thread
->context
)
1500 /* thread is not suspended, retry (if it's still running) */
1501 if (thread
->state
== RUNNING
)
1503 set_error( STATUS_PENDING
);
1506 release_object( thread
);
1507 /* make sure we have suspend access */
1508 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1509 suspend_thread( thread
);
1512 else set_error( STATUS_UNSUCCESSFUL
);
1514 else if ((context
= set_reply_data_size( sizeof(context_t
) )))
1516 unsigned int flags
= get_context_system_regs( thread
->process
->cpu
);
1518 memset( context
, 0, sizeof(context_t
) );
1519 context
->cpu
= thread
->process
->cpu
;
1520 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1521 if (flags
) get_thread_context( thread
, context
, flags
);
1523 release_object( thread
);
1526 /* set the current context of a thread */
1527 DECL_HANDLER(set_thread_context
)
1529 struct thread
*thread
;
1530 const context_t
*context
= get_req_data();
1532 if (get_req_data_size() < sizeof(context_t
))
1534 set_error( STATUS_INVALID_PARAMETER
);
1537 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1538 reply
->self
= (thread
== current
);
1540 if (thread
!= current
&& !thread
->context
)
1542 /* thread is not suspended, retry (if it's still running) */
1543 if (thread
->state
== RUNNING
)
1545 set_error( STATUS_PENDING
);
1548 release_object( thread
);
1549 /* make sure we have suspend access */
1550 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
))) return;
1551 suspend_thread( thread
);
1554 else set_error( STATUS_UNSUCCESSFUL
);
1556 else if (context
->cpu
== thread
->process
->cpu
)
1558 unsigned int system_flags
= get_context_system_regs(context
->cpu
) & context
->flags
;
1559 unsigned int client_flags
= context
->flags
& ~system_flags
;
1561 if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1562 if (thread
->context
&& !get_error()) copy_context( thread
->context
, context
, client_flags
);
1564 else set_error( STATUS_INVALID_PARAMETER
);
1566 release_object( thread
);
1569 /* retrieve the suspended context of a thread */
1570 DECL_HANDLER(get_suspend_context
)
1572 if (get_reply_max_size() < sizeof(context_t
))
1574 set_error( STATUS_INVALID_PARAMETER
);
1578 if (current
->suspend_context
)
1580 set_reply_data_ptr( current
->suspend_context
, sizeof(context_t
) );
1581 if (current
->context
== current
->suspend_context
)
1583 current
->context
= NULL
;
1584 stop_thread_if_suspended( current
);
1586 current
->suspend_context
= NULL
;
1588 else set_error( STATUS_INVALID_PARAMETER
); /* not suspended, shouldn't happen */
1591 /* store the suspended context of a thread */
1592 DECL_HANDLER(set_suspend_context
)
1594 const context_t
*context
= get_req_data();
1596 if (get_req_data_size() < sizeof(context_t
))
1598 set_error( STATUS_INVALID_PARAMETER
);
1602 if (current
->context
|| context
->cpu
!= current
->process
->cpu
)
1604 /* nested suspend or exception, shouldn't happen */
1605 set_error( STATUS_INVALID_PARAMETER
);
1607 else if ((current
->suspend_context
= mem_alloc( sizeof(context_t
) )))
1609 memcpy( current
->suspend_context
, get_req_data(), sizeof(context_t
) );
1610 current
->context
= current
->suspend_context
;
1611 if (current
->debug_break
) break_thread( current
);
1615 /* fetch a selector entry for a thread */
1616 DECL_HANDLER(get_selector_entry
)
1618 struct thread
*thread
;
1619 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1621 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1622 release_object( thread
);