difxapi: Add stubs for DriverPackageUninstall{A,W}.
[wine.git] / server / thread.c
blob5f9738d8ab533330ce460bb338a1ea50acb3a4b3
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <time.h>
35 #ifdef HAVE_POLL_H
36 #include <poll.h>
37 #endif
38 #ifdef HAVE_SCHED_H
39 #include <sched.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
45 #include "winternl.h"
47 #include "file.h"
48 #include "handle.h"
49 #include "process.h"
50 #include "thread.h"
51 #include "request.h"
52 #include "user.h"
53 #include "security.h"
56 #ifdef __i386__
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);
66 #else
67 #error Unsupported CPU
68 #endif
70 /* thread queues */
72 struct thread_wait
74 struct thread_wait *next; /* next wait structure for this thread */
75 struct thread *thread; /* owner thread */
76 int count; /* count of objects */
77 int flags;
78 client_ptr_t cookie; /* magic cookie to return to client */
79 timeout_t timeout;
80 struct timeout_user *user;
81 struct wait_queue_entry queues[1];
84 /* asynchronous procedure calls */
86 struct thread_apc
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 */
155 NULL, /* flush */
156 NULL, /* get_fd_type */
157 NULL, /* ioctl */
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 )
168 int i;
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;
174 thread->teb = 0;
175 thread->debug_ctx = NULL;
176 thread->debug_event = NULL;
177 thread->debug_break = 0;
178 thread->queue = NULL;
179 thread->wait = NULL;
180 thread->error = 0;
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;
191 thread->suspend = 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 );
220 return NULL;
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 );
237 return NULL;
239 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
241 release_object( thread );
242 return NULL;
245 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
246 add_process_thread( thread->process, thread );
247 return 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 )
267 int i;
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;
296 thread->desktop = 0;
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;
367 apc->caller = NULL;
368 apc->owner = owner;
369 apc->executed = 0;
370 apc->result.type = APC_NONE;
371 if (owner) grab_object( owner );
373 return apc;
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 );
383 return NULL;
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;
402 return NULL;
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;
414 return NULL;
417 int set_thread_affinity( struct thread *thread, affinity_t affinity )
419 int ret = 0;
420 #ifdef HAVE_SCHED_SETAFFINITY
421 if (thread->unix_tid != -1)
423 cpu_set_t set;
424 int i;
425 affinity_t mask;
427 CPU_ZERO( &set );
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 );
433 #endif
434 if (!ret) thread->affinity = affinity;
435 return ret;
438 affinity_t get_thread_affinity( struct thread *thread )
440 affinity_t mask = 0;
441 #ifdef HAVE_SCHED_SETAFFINITY
442 if (thread->unix_tid != -1)
444 cpu_set_t set;
445 unsigned int i;
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;
451 #endif
452 if (!mask) mask = ~0;
453 return mask;
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;
476 else
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 ))
486 file_set_error();
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 );
515 return old_count;
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 );
526 return old_count;
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 )
532 grab_object( obj );
533 entry->obj = obj;
534 list_add_tail( &obj->wait_queue, &entry->entry );
535 return 1;
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 );
545 /* finish waiting */
546 static void end_wait( struct thread *thread )
548 struct thread_wait *wait = thread->wait;
549 struct wait_queue_entry *entry;
550 int i;
552 assert( wait );
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 );
557 free( wait );
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;
565 unsigned int i;
567 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
568 wait->next = current->wait;
569 wait->thread = current;
570 wait->count = count;
571 wait->flags = flags;
572 wait->user = NULL;
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 ))
582 wait->count = i;
583 end_wait( current );
584 return 0;
587 return 1;
590 /* check if the thread waiting condition is satisfied */
591 static int check_wait( struct thread *thread )
593 int i, signaled;
594 struct thread_wait *wait = thread->wait;
595 struct wait_queue_entry *entry;
597 assert( wait );
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)
607 int not_ok = 0;
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 */
614 signaled = 0;
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;
618 return signaled;
620 else
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 */
626 signaled = i;
627 if (entry->obj->ops->satisfied( entry->obj, thread ))
628 signaled = i + STATUS_ABANDONED_WAIT_0;
629 return signaled;
633 other_checks:
634 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
635 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
636 return -1;
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;
643 int ret;
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))
649 return 0;
650 if (ret >= 0)
651 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
652 else if (errno == EPIPE)
653 kill_thread( thread, 0 ); /* normal death */
654 else
655 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
656 return -1;
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 )
663 int signaled, count;
664 client_ptr_t cookie;
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 );
672 end_wait( thread );
673 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
674 break;
676 return count;
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;
686 wait->user = NULL;
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 );
691 end_wait( thread );
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 )
700 struct object *obj;
701 int ret = 0;
703 obj = get_handle_obj( current->process, handle, 0, NULL );
704 if (obj)
706 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
707 release_object( obj );
709 return ret;
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 )
716 int ret;
717 unsigned int i;
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 );
725 return 0;
727 for (i = 0; i < count; i++)
729 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
730 break;
733 if (i < count) goto done;
734 if (!wait_on( count, objects, flags, timeout )) goto done;
736 /* signal the object */
737 if (signal_obj)
739 if (!signal_object( signal_obj ))
741 end_wait( current );
742 goto done;
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 */
751 end_wait( current );
752 set_error( ret );
753 goto done;
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 )))
762 end_wait( current );
763 goto done;
766 current->wait->cookie = cookie;
767 set_error( STATUS_PENDING );
769 done:
770 while (i > 0) release_object( objects[--i] );
771 return timeout;
774 /* attempt to wake threads sleeping on the object wait queue */
775 void wake_up( struct object *obj, int max )
777 struct list *ptr;
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 )
792 switch(type)
794 case APC_NONE:
795 case APC_USER:
796 case APC_TIMER:
797 return &thread->user_apc;
798 default:
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 )
813 struct list *queue;
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 ))
825 thread = candidate;
826 break;
829 if (!thread)
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 ))
836 thread = candidate;
837 break;
841 if (!thread) return 0; /* nothing found */
842 queue = get_apc_queue( thread, apc->call.type );
844 else
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 );
857 grab_object( apc );
858 list_add_tail( queue, &apc->entry );
859 if (!list_prev( queue, &apc->entry )) /* first one */
860 wake_thread( thread );
862 return 1;
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;
869 int ret = 0;
871 if ((apc = create_apc( owner, call_data )))
873 ret = queue_apc( NULL, thread, apc );
874 release_object( apc );
876 return ret;
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 );
889 apc->executed = 1;
890 wake_up( &apc->obj, 0 );
891 release_object( apc );
892 return;
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 );
903 if (ptr)
905 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
906 list_remove( ptr );
908 return apc;
911 /* clear an APC queue, cancelling all the APCs on it */
912 static void clear_apc_queue( struct list *queue )
914 struct list *ptr;
916 while ((ptr = list_head( queue )))
918 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
919 list_remove( &apc->entry );
920 apc->executed = 1;
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 )
930 int i;
932 if (server == -1) return -1;
933 if (client == -1)
935 close( server );
936 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;
945 return i;
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;
954 return i;
956 return -1;
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 )
963 int i, ret;
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;
975 return ret;
978 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
979 return -1;
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;
989 if (debug_level)
990 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
991 thread->id, thread->exit_code );
992 if (thread->wait)
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 */
997 violent_death = 0;
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 );
1013 to->flags |= flags;
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 )
1026 switch (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;
1034 return 0;
1037 /* trigger a breakpoint event in a given thread */
1038 void break_thread( struct thread *thread )
1040 debug_event_t data;
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)
1050 case CPU_x86:
1051 data.exception.address = thread->context->ctl.i386_regs.eip;
1052 break;
1053 case CPU_x86_64:
1054 data.exception.address = thread->context->ctl.x86_64_regs.rip;
1055 break;
1056 case CPU_POWERPC:
1057 data.exception.address = thread->context->ctl.powerpc_regs.iar;
1058 break;
1059 case CPU_ARM:
1060 data.exception.address = thread->context->ctl.arm_regs.pc;
1061 break;
1062 case CPU_ARM64:
1063 data.exception.address = thread->context->ctl.arm64_regs.pc;
1064 break;
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;
1075 int total = 0;
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;
1080 ptr = snapshot;
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 );
1088 ptr++;
1090 *count = total;
1091 return snapshot;
1094 /* gets the current impersonation token */
1095 struct token *thread_get_impersonation_token( struct thread *thread )
1097 if (thread->token)
1098 return thread->token;
1099 else
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 );
1113 return;
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 */
1123 return;
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 );
1139 return;
1141 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1143 set_error( STATUS_TOO_MANY_OPENED_FILES );
1144 goto error;
1147 if (current->reply_fd) /* already initialised */
1149 set_error( STATUS_INVALID_PARAMETER );
1150 goto error;
1153 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1155 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1156 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1157 if (!current->reply_fd || !current->wait_fd) return;
1159 if (!is_valid_address(req->teb))
1161 set_error( STATUS_INVALID_PARAMETER );
1162 return;
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 );
1175 else
1176 set_error( STATUS_NOT_REGISTRY_FILE ); /* server supports it but not the prefix */
1177 return;
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 );
1185 else
1186 set_thread_affinity( current, current->affinity );
1188 else
1190 if (req->cpu != process->cpu)
1192 set_error( STATUS_INVALID_PARAMETER );
1193 return;
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;
1208 return;
1210 error:
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;
1220 reply->self = 0;
1221 reply->last = 0;
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 );
1226 else
1228 reply->self = 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 );
1240 reply->handle = 0;
1241 if (thread)
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 );
1257 if (thread)
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;
1314 unsigned int count;
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 );
1321 return;
1323 count = (get_req_data_size() - sizeof(*result)) / sizeof(obj_handle_t);
1325 /* first store results of previous apc */
1326 if (req->prev_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;
1331 apc->executed = 1;
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)
1342 if (apc->owner)
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)
1355 for (;;)
1357 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1358 break;
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 );
1367 break;
1369 apc->executed = 1;
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)
1387 case APC_NONE:
1388 case APC_USER:
1389 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1390 break;
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 );
1399 break;
1400 case APC_VIRTUAL_QUERY:
1401 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1402 break;
1403 case APC_MAP_VIEW:
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;
1411 else
1413 release_object( process );
1414 process = NULL;
1417 break;
1418 case APC_CREATE_THREAD:
1419 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1420 break;
1421 default:
1422 set_error( STATUS_INVALID_PARAMETER );
1423 break;
1426 if (thread)
1428 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1429 release_object( thread );
1431 else if (process)
1433 reply->self = (process == current->process);
1434 if (!reply->self)
1436 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1437 if (handle)
1439 if (queue_apc( process, NULL, apc ))
1441 apc->caller = (struct thread *)grab_object( current );
1442 reply->handle = handle;
1444 else
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 );
1465 else
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;
1478 context_t *context;
1480 if (get_reply_max_size() < sizeof(context_t))
1482 set_error( STATUS_INVALID_PARAMETER );
1483 return;
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 );
1494 if (req->suspend)
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 );
1525 return;
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 );
1536 if (req->suspend)
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 );
1565 return;
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 );
1589 return;
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 );