Removed unused file.
[wine/wine64.git] / server / thread.c
blob90e88aa1c4c03833f202ba0e4d59f0dbe57c65e6
1 /*
2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #ifdef HAVE_SYS_MMAN_H
18 #include <sys/mman.h>
19 #endif
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <stdarg.h>
24 #include "winbase.h"
26 #include "handle.h"
27 #include "process.h"
28 #include "thread.h"
29 #include "request.h"
32 /* thread queues */
34 struct thread_wait
36 struct thread_wait *next; /* next wait structure for this thread */
37 struct thread *thread; /* owner thread */
38 int count; /* count of objects */
39 int flags;
40 void *cookie; /* magic cookie to return to client */
41 struct timeval timeout;
42 struct timeout_user *user;
43 struct wait_queue_entry queues[1];
46 /* asynchronous procedure calls */
48 struct thread_apc
50 struct thread_apc *next; /* queue linked list */
51 struct thread_apc *prev;
52 struct object *owner; /* object that queued this apc */
53 void *func; /* function to call in client */
54 enum apc_type type; /* type of apc function */
55 int nb_args; /* number of arguments */
56 void *args[1]; /* function arguments */
60 /* thread operations */
62 static void dump_thread( struct object *obj, int verbose );
63 static int thread_signaled( struct object *obj, struct thread *thread );
64 static void thread_poll_event( struct object *obj, int event );
65 static void destroy_thread( struct object *obj );
66 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
68 static const struct object_ops thread_ops =
70 sizeof(struct thread), /* size */
71 dump_thread, /* dump */
72 add_queue, /* add_queue */
73 remove_queue, /* remove_queue */
74 thread_signaled, /* signaled */
75 no_satisfied, /* satisfied */
76 NULL, /* get_poll_events */
77 thread_poll_event, /* poll_event */
78 no_get_fd, /* get_fd */
79 no_flush, /* flush */
80 no_get_file_info, /* get_file_info */
81 destroy_thread /* destroy */
84 static struct thread *first_thread;
85 static struct thread *booting_thread;
87 /* initialize the structure for a newly allocated thread */
88 inline static void init_thread_structure( struct thread *thread )
90 int i;
92 thread->unix_pid = 0; /* not known yet */
93 thread->context = NULL;
94 thread->teb = NULL;
95 thread->mutex = NULL;
96 thread->debug_ctx = NULL;
97 thread->debug_event = NULL;
98 thread->queue = NULL;
99 thread->info = NULL;
100 thread->wait = NULL;
101 thread->system_apc.head = NULL;
102 thread->system_apc.tail = NULL;
103 thread->user_apc.head = NULL;
104 thread->user_apc.tail = NULL;
105 thread->error = 0;
106 thread->request_fd = NULL;
107 thread->reply_fd = -1;
108 thread->wait_fd = -1;
109 thread->state = RUNNING;
110 thread->attached = 0;
111 thread->exit_code = 0;
112 thread->next = NULL;
113 thread->prev = NULL;
114 thread->priority = THREAD_PRIORITY_NORMAL;
115 thread->affinity = 1;
116 thread->suspend = 0;
117 thread->buffer = (void *)-1;
119 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
120 thread->inflight[i].server = thread->inflight[i].client = -1;
123 /* create a new thread */
124 struct thread *create_thread( int fd, struct process *process )
126 struct thread *thread;
128 if (!(thread = alloc_object( &thread_ops, fd ))) return NULL;
130 init_thread_structure( thread );
132 thread->process = (struct process *)grab_object( process );
133 if (!current) current = thread;
135 if (!booting_thread) /* first thread ever */
137 booting_thread = thread;
138 lock_master_socket(1);
141 if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
142 first_thread = thread;
144 fcntl( fd, F_SETFL, O_NONBLOCK );
145 set_select_events( &thread->obj, POLLIN ); /* start listening to events */
146 add_process_thread( thread->process, thread );
147 return thread;
150 /* handle a client event */
151 static void thread_poll_event( struct object *obj, int event )
153 struct thread *thread = (struct thread *)obj;
154 assert( obj->ops == &thread_ops );
156 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
157 else if (event & POLLIN) read_request( thread );
160 /* cleanup everything that is no longer needed by a dead thread */
161 /* used by destroy_thread and kill_thread */
162 static void cleanup_thread( struct thread *thread )
164 int i;
165 struct thread_apc *apc;
167 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
168 if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH );
169 if (thread->reply_fd != -1) close( thread->reply_fd );
170 if (thread->wait_fd != -1) close( thread->wait_fd );
171 if (thread->request_fd) release_object( thread->request_fd );
172 if (thread->queue)
174 if (thread->process->queue == thread->queue)
176 release_object( thread->process->queue );
177 thread->process->queue = NULL;
179 release_object( thread->queue );
180 thread->queue = NULL;
182 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
184 if (thread->inflight[i].client != -1)
186 close( thread->inflight[i].server );
187 thread->inflight[i].client = thread->inflight[i].server = -1;
190 thread->buffer = (void *)-1;
191 thread->reply_fd = -1;
192 thread->wait_fd = -1;
193 thread->request_fd = NULL;
196 /* destroy a thread when its refcount is 0 */
197 static void destroy_thread( struct object *obj )
199 struct thread_apc *apc;
200 struct thread *thread = (struct thread *)obj;
201 assert( obj->ops == &thread_ops );
203 assert( !thread->debug_ctx ); /* cannot still be debugging something */
204 if (thread->next) thread->next->prev = thread->prev;
205 if (thread->prev) thread->prev->next = thread->next;
206 else first_thread = thread->next;
207 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
208 if (thread->info) release_object( thread->info );
209 cleanup_thread( thread );
210 release_object( thread->process );
213 /* dump a thread on stdout for debugging purposes */
214 static void dump_thread( struct object *obj, int verbose )
216 struct thread *thread = (struct thread *)obj;
217 assert( obj->ops == &thread_ops );
219 fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
220 thread->unix_pid, thread->teb, thread->state );
223 static int thread_signaled( struct object *obj, struct thread *thread )
225 struct thread *mythread = (struct thread *)obj;
226 return (mythread->state == TERMINATED);
229 /* get a thread pointer from a thread id (and increment the refcount) */
230 struct thread *get_thread_from_id( void *id )
232 struct thread *t = first_thread;
233 while (t && (t != id)) t = t->next;
234 if (t) grab_object( t );
235 else set_error( STATUS_INVALID_PARAMETER );
236 return t;
239 /* get a thread from a handle (and increment the refcount) */
240 struct thread *get_thread_from_handle( handle_t handle, unsigned int access )
242 return (struct thread *)get_handle_obj( current->process, handle,
243 access, &thread_ops );
246 /* find a thread from a Unix pid */
247 struct thread *get_thread_from_pid( int pid )
249 struct thread *t = first_thread;
250 while (t && (t->unix_pid != pid)) t = t->next;
251 return t;
254 /* set all information about a thread */
255 static void set_thread_info( struct thread *thread,
256 struct set_thread_info_request *req )
258 if (req->mask & SET_THREAD_INFO_PRIORITY)
259 thread->priority = req->priority;
260 if (req->mask & SET_THREAD_INFO_AFFINITY)
262 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
263 else thread->affinity = req->affinity;
267 /* suspend a thread */
268 int suspend_thread( struct thread *thread, int check_limit )
270 int old_count = thread->suspend;
271 if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit)
273 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
275 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
276 return old_count;
279 /* resume a thread */
280 int resume_thread( struct thread *thread )
282 int old_count = thread->suspend;
283 if (thread->suspend > 0)
285 if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread );
287 return old_count;
290 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
291 int add_queue( struct object *obj, struct wait_queue_entry *entry )
293 grab_object( obj );
294 entry->obj = obj;
295 entry->prev = obj->tail;
296 entry->next = NULL;
297 if (obj->tail) obj->tail->next = entry;
298 else obj->head = entry;
299 obj->tail = entry;
300 return 1;
303 /* remove a thread from an object wait queue */
304 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
306 if (entry->next) entry->next->prev = entry->prev;
307 else obj->tail = entry->prev;
308 if (entry->prev) entry->prev->next = entry->next;
309 else obj->head = entry->next;
310 release_object( obj );
313 /* finish waiting */
314 static void end_wait( struct thread *thread )
316 struct thread_wait *wait = thread->wait;
317 struct wait_queue_entry *entry;
318 int i;
320 assert( wait );
321 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
322 entry->obj->ops->remove_queue( entry->obj, entry );
323 if (wait->user) remove_timeout_user( wait->user );
324 thread->wait = wait->next;
325 free( wait );
328 /* build the thread wait structure */
329 static int wait_on( int count, struct object *objects[], int flags, int sec, int usec )
331 struct thread_wait *wait;
332 struct wait_queue_entry *entry;
333 int i;
335 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
336 wait->next = current->wait;
337 wait->thread = current;
338 wait->count = count;
339 wait->flags = flags;
340 wait->user = NULL;
341 current->wait = wait;
342 if (flags & SELECT_TIMEOUT)
344 wait->timeout.tv_sec = sec;
345 wait->timeout.tv_usec = usec;
348 for (i = 0, entry = wait->queues; i < count; i++, entry++)
350 struct object *obj = objects[i];
351 entry->thread = current;
352 if (!obj->ops->add_queue( obj, entry ))
354 wait->count = i;
355 end_wait( current );
356 return 0;
359 return 1;
362 /* check if the thread waiting condition is satisfied */
363 static int check_wait( struct thread *thread )
365 int i, signaled;
366 struct thread_wait *wait = thread->wait;
367 struct wait_queue_entry *entry = wait->queues;
369 assert( wait );
370 if (wait->flags & SELECT_ALL)
372 int not_ok = 0;
373 /* Note: we must check them all anyway, as some objects may
374 * want to do something when signaled, even if others are not */
375 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
376 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
377 if (not_ok) goto other_checks;
378 /* Wait satisfied: tell it to all objects */
379 signaled = 0;
380 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
381 if (entry->obj->ops->satisfied( entry->obj, thread ))
382 signaled = STATUS_ABANDONED_WAIT_0;
383 return signaled;
385 else
387 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
389 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
390 /* Wait satisfied: tell it to the object */
391 signaled = i;
392 if (entry->obj->ops->satisfied( entry->obj, thread ))
393 signaled = i + STATUS_ABANDONED_WAIT_0;
394 return signaled;
398 other_checks:
399 if ((wait->flags & SELECT_INTERRUPTIBLE) && thread->system_apc.head) return STATUS_USER_APC;
400 if ((wait->flags & SELECT_ALERTABLE) && thread->user_apc.head) return STATUS_USER_APC;
401 if (wait->flags & SELECT_TIMEOUT)
403 struct timeval now;
404 gettimeofday( &now, NULL );
405 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
407 return -1;
410 /* send the wakeup signal to a thread */
411 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
413 struct wake_up_reply reply;
414 int ret;
416 reply.cookie = cookie;
417 reply.signaled = signaled;
418 if ((ret = write( thread->wait_fd, &reply, sizeof(reply) )) == sizeof(reply)) return 0;
419 if (ret >= 0)
420 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
421 else if (errno == EPIPE)
422 kill_thread( thread, 0 ); /* normal death */
423 else
424 fatal_protocol_perror( thread, "write" );
425 return -1;
428 /* attempt to wake up a thread */
429 /* return >0 if OK, 0 if the wait condition is still not satisfied */
430 static int wake_thread( struct thread *thread )
432 int signaled, count;
433 void *cookie;
435 for (count = 0; thread->wait; count++)
437 if ((signaled = check_wait( thread )) == -1) break;
439 cookie = thread->wait->cookie;
440 if (debug_level) fprintf( stderr, "%08x: *wakeup* signaled=%d cookie=%p\n",
441 (unsigned int)thread, signaled, cookie );
442 end_wait( thread );
443 send_thread_wakeup( thread, cookie, signaled );
445 return count;
448 /* thread wait timeout */
449 static void thread_timeout( void *ptr )
451 struct thread_wait *wait = ptr;
452 struct thread *thread = wait->thread;
453 void *cookie = wait->cookie;
455 wait->user = NULL;
456 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
458 if (debug_level) fprintf( stderr, "%08x: *wakeup* signaled=%d cookie=%p\n",
459 (unsigned int)thread, STATUS_TIMEOUT, cookie );
460 end_wait( thread );
461 send_thread_wakeup( thread, cookie, STATUS_TIMEOUT );
462 /* check if other objects have become signaled in the meantime */
463 wake_thread( thread );
466 /* select on a list of handles */
467 static void select_on( int count, void *cookie, handle_t *handles, int flags, int sec, int usec )
469 int ret, i;
470 struct object *objects[MAXIMUM_WAIT_OBJECTS];
472 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
474 set_error( STATUS_INVALID_PARAMETER );
475 return;
477 for (i = 0; i < count; i++)
479 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
480 break;
483 if (i < count) goto done;
484 if (!wait_on( count, objects, flags, sec, usec )) goto done;
486 if ((ret = check_wait( current )) != -1)
488 /* condition is already satisfied */
489 end_wait( current );
490 set_error( ret );
491 goto done;
494 /* now we need to wait */
495 if (flags & SELECT_TIMEOUT)
497 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
498 thread_timeout, current->wait )))
500 end_wait( current );
501 goto done;
504 current->wait->cookie = cookie;
505 set_error( STATUS_PENDING );
507 done:
508 while (--i >= 0) release_object( objects[i] );
511 /* attempt to wake threads sleeping on the object wait queue */
512 void wake_up( struct object *obj, int max )
514 struct wait_queue_entry *entry = obj->head;
516 while (entry)
518 struct thread *thread = entry->thread;
519 entry = entry->next;
520 if (wake_thread( thread ))
522 if (max && !--max) break;
527 /* queue an async procedure call */
528 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
529 enum apc_type type, int system, int nb_args, ... )
531 struct thread_apc *apc;
532 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
534 /* cancel a possible previous APC with the same owner */
535 if (owner) thread_cancel_apc( thread, owner, system );
537 if (!(apc = mem_alloc( sizeof(*apc) + (nb_args-1)*sizeof(apc->args[0]) ))) return 0;
538 apc->prev = queue->tail;
539 apc->next = NULL;
540 apc->owner = owner;
541 apc->func = func;
542 apc->type = type;
543 apc->nb_args = nb_args;
544 if (nb_args)
546 int i;
547 va_list args;
548 va_start( args, nb_args );
549 for (i = 0; i < nb_args; i++) apc->args[i] = va_arg( args, void * );
550 va_end( args );
552 queue->tail = apc;
553 if (!apc->prev) /* first one */
555 queue->head = apc;
556 wake_thread( thread );
558 return 1;
561 /* cancel the async procedure call owned by a specific object */
562 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
564 struct thread_apc *apc;
565 struct apc_queue *queue = system ? &thread->system_apc : &thread->user_apc;
566 for (apc = queue->head; apc; apc = apc->next)
568 if (apc->owner != owner) continue;
569 if (apc->next) apc->next->prev = apc->prev;
570 else queue->tail = apc->prev;
571 if (apc->prev) apc->prev->next = apc->next;
572 else queue->head = apc->next;
573 free( apc );
574 return;
578 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
579 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
581 struct thread_apc *apc;
582 struct apc_queue *queue = &thread->system_apc;
584 if (!queue->head && !system_only) queue = &thread->user_apc;
585 if ((apc = queue->head))
587 if (apc->next) apc->next->prev = NULL;
588 else queue->tail = NULL;
589 queue->head = apc->next;
591 return apc;
594 /* add an fd to the inflight list */
595 /* return list index, or -1 on error */
596 int thread_add_inflight_fd( struct thread *thread, int client, int server )
598 int i;
600 if (server == -1) return -1;
601 if (client == -1)
603 close( server );
604 return -1;
607 /* first check if we already have an entry for this fd */
608 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
609 if (thread->inflight[i].client == client)
611 close( thread->inflight[i].server );
612 thread->inflight[i].server = server;
613 return i;
616 /* now find a free spot to store it */
617 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
618 if (thread->inflight[i].client == -1)
620 thread->inflight[i].client = client;
621 thread->inflight[i].server = server;
622 return i;
624 return -1;
627 /* get an inflight fd and purge it from the list */
628 /* the fd must be closed when no longer used */
629 int thread_get_inflight_fd( struct thread *thread, int client )
631 int i, ret;
633 if (client == -1) return -1;
637 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
639 if (thread->inflight[i].client == client)
641 ret = thread->inflight[i].server;
642 thread->inflight[i].server = thread->inflight[i].client = -1;
643 return ret;
646 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
647 return -1;
650 /* retrieve an LDT selector entry */
651 static void get_selector_entry( struct thread *thread, int entry,
652 unsigned int *base, unsigned int *limit,
653 unsigned char *flags )
655 if (!thread->process->ldt_copy)
657 set_error( STATUS_ACCESS_DENIED );
658 return;
660 if (entry >= 8192)
662 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
663 return;
665 if (suspend_for_ptrace( thread ))
667 unsigned char flags_buf[4];
668 int *addr = (int *)thread->process->ldt_copy + entry;
669 if (read_thread_int( thread, addr, base ) == -1) goto done;
670 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
671 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
672 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
673 *flags = flags_buf[entry & 3];
674 done:
675 resume_thread( thread );
679 /* kill a thread on the spot */
680 void kill_thread( struct thread *thread, int violent_death )
682 if (thread->state == TERMINATED) return; /* already killed */
683 thread->state = TERMINATED;
684 if (current == thread) current = NULL;
685 if (debug_level)
686 fprintf( stderr,"%08x: *killed* exit_code=%d\n",
687 (unsigned int)thread, thread->exit_code );
688 if (thread->wait)
690 while (thread->wait) end_wait( thread );
691 send_thread_wakeup( thread, NULL, STATUS_PENDING );
692 /* if it is waiting on the socket, we don't need to send a SIGTERM */
693 violent_death = 0;
695 debug_exit_thread( thread );
696 abandon_mutexes( thread );
697 remove_process_thread( thread->process, thread );
698 wake_up( &thread->obj, 0 );
699 detach_thread( thread, violent_death ? SIGTERM : 0 );
700 remove_select_user( &thread->obj );
701 cleanup_thread( thread );
702 release_object( thread );
705 /* take a snapshot of currently running threads */
706 struct thread_snapshot *thread_snap( int *count )
708 struct thread_snapshot *snapshot, *ptr;
709 struct thread *thread;
710 int total = 0;
712 for (thread = first_thread; thread; thread = thread->next)
713 if (thread->state != TERMINATED) total++;
714 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
715 ptr = snapshot;
716 for (thread = first_thread; thread; thread = thread->next)
718 if (thread->state == TERMINATED) continue;
719 ptr->thread = thread;
720 ptr->count = thread->obj.refcount;
721 ptr->priority = thread->priority;
722 grab_object( thread );
723 ptr++;
725 *count = total;
726 return snapshot;
729 /* signal that we are finished booting on the client side */
730 DECL_HANDLER(boot_done)
732 debug_level = max( debug_level, req->debug_level );
733 if (current == booting_thread)
735 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
736 lock_master_socket(0); /* allow other clients now */
740 /* create a new thread */
741 DECL_HANDLER(new_thread)
743 struct thread *thread;
744 int request_fd = thread_get_inflight_fd( current, req->request_fd );
746 if (request_fd == -1)
748 set_error( STATUS_INVALID_HANDLE );
749 return;
752 if ((thread = create_thread( request_fd, current->process )))
754 if (req->suspend) thread->suspend++;
755 req->tid = thread;
756 if ((req->handle = alloc_handle( current->process, thread,
757 THREAD_ALL_ACCESS, req->inherit )))
759 /* thread object will be released when the thread gets killed */
760 return;
762 kill_thread( thread, 1 );
763 request_fd = -1;
767 /* initialize a new thread */
768 DECL_HANDLER(init_thread)
770 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
771 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
773 if (current->unix_pid)
775 fatal_protocol_error( current, "init_thread: already running\n" );
776 goto error;
778 if (reply_fd == -1)
780 fatal_protocol_error( current, "bad reply fd\n" );
781 goto error;
783 if (wait_fd == -1)
785 fatal_protocol_error( current, "bad wait fd\n" );
786 goto error;
789 current->unix_pid = req->unix_pid;
790 current->teb = req->teb;
791 current->reply_fd = reply_fd;
792 current->wait_fd = wait_fd;
794 if (current->suspend + current->process->suspend > 0) stop_thread( current );
795 if (current->process->running_threads > 1)
796 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
798 req->pid = get_process_id( current->process );
799 req->tid = get_thread_id( current );
800 req->boot = (current == booting_thread);
801 req->version = SERVER_PROTOCOL_VERSION;
802 return;
804 error:
805 if (reply_fd != -1) close( reply_fd );
806 if (wait_fd != -1) close( wait_fd );
809 /* set the shared buffer for a thread */
810 DECL_HANDLER(set_thread_buffer)
812 unsigned int size = MAX_REQUEST_LENGTH;
813 unsigned int offset = 0;
814 int fd = thread_get_inflight_fd( current, req->fd );
816 req->size = size;
817 req->offset = offset;
819 if (fd != -1)
821 if (ftruncate( fd, size ) == -1) file_set_error();
822 else
824 void *buffer = mmap( 0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset );
825 if (buffer == (void *)-1) file_set_error();
826 else
828 if (current->buffer != (void *)-1) munmap( current->buffer, size );
829 current->buffer = buffer;
832 close( fd );
834 else set_error( STATUS_INVALID_HANDLE );
837 /* terminate a thread */
838 DECL_HANDLER(terminate_thread)
840 struct thread *thread;
842 req->self = 0;
843 req->last = 0;
844 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
846 thread->exit_code = req->exit_code;
847 if (thread != current) kill_thread( thread, 1 );
848 else
850 req->self = 1;
851 req->last = (thread->process->running_threads == 1);
853 release_object( thread );
857 /* fetch information about a thread */
858 DECL_HANDLER(get_thread_info)
860 struct thread *thread;
861 handle_t handle = req->handle;
863 if (!handle) thread = get_thread_from_id( req->tid_in );
864 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
866 if (thread)
868 req->tid = get_thread_id( thread );
869 req->teb = thread->teb;
870 req->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
871 req->priority = thread->priority;
872 release_object( thread );
876 /* set information about a thread */
877 DECL_HANDLER(set_thread_info)
879 struct thread *thread;
881 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
883 set_thread_info( thread, req );
884 release_object( thread );
888 /* suspend a thread */
889 DECL_HANDLER(suspend_thread)
891 struct thread *thread;
893 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
895 req->count = suspend_thread( thread, 1 );
896 release_object( thread );
900 /* resume a thread */
901 DECL_HANDLER(resume_thread)
903 struct thread *thread;
905 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
907 req->count = resume_thread( thread );
908 release_object( thread );
912 /* select on a handle list */
913 DECL_HANDLER(select)
915 int count = get_req_data_size(req) / sizeof(int);
916 select_on( count, req->cookie, get_req_data(req), req->flags, req->sec, req->usec );
919 /* queue an APC for a thread */
920 DECL_HANDLER(queue_apc)
922 struct thread *thread;
923 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
925 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user, 1, req->param );
926 release_object( thread );
930 /* get next APC to call */
931 DECL_HANDLER(get_apc)
933 struct thread_apc *apc;
934 size_t size;
936 for (;;)
938 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
940 /* no more APCs */
941 req->func = NULL;
942 req->type = APC_NONE;
943 set_req_data_size( req, 0 );
944 return;
946 /* Optimization: ignore APCs that have a NULL func; they are only used
947 * to wake up a thread, but since we got here the thread woke up already.
949 if (apc->func) break;
950 free( apc );
952 size = apc->nb_args * sizeof(apc->args[0]);
953 if (size > get_req_data_size(req)) size = get_req_data_size(req);
954 req->func = apc->func;
955 req->type = apc->type;
956 memcpy( get_req_data(req), apc->args, size );
957 set_req_data_size( req, size );
958 free( apc );
961 /* fetch a selector entry for a thread */
962 DECL_HANDLER(get_selector_entry)
964 struct thread *thread;
965 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
967 get_selector_entry( thread, req->entry, &req->base, &req->limit, &req->flags );
968 release_object( thread );