Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side thread management |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
| 5 | */ |
| 6 | |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 7 | #include "config.h" |
| 8 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <fcntl.h> |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 11 | #include <signal.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 15 | #ifdef HAVE_SYS_MMAN_H |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 16 | #include <sys/mman.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 17 | #endif |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 18 | #include <sys/types.h> |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 19 | #ifdef HAVE_SYS_SOCKET_H |
| 20 | # include <sys/socket.h> |
| 21 | #endif |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 22 | #include <sys/uio.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
Michael Veksler | f935c59 | 1999-02-09 15:49:39 +0000 | [diff] [blame] | 24 | #include <stdarg.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 25 | |
Michael Veksler | f935c59 | 1999-02-09 15:49:39 +0000 | [diff] [blame] | 26 | |
| 27 | #include "winbase.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 28 | |
| 29 | #include "handle.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 30 | #include "process.h" |
| 31 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 32 | #include "request.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 33 | |
| 34 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 35 | /* thread queues */ |
| 36 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 37 | struct thread_wait |
| 38 | { |
| 39 | int count; /* count of objects */ |
| 40 | int flags; |
| 41 | struct timeval timeout; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 42 | struct timeout_user *user; |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 43 | sleep_reply reply; /* function to build the reply */ |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 44 | struct wait_queue_entry queues[1]; |
| 45 | }; |
| 46 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 47 | /* asynchronous procedure calls */ |
| 48 | |
| 49 | struct thread_apc |
| 50 | { |
| 51 | void *func; /* function to call in client */ |
| 52 | void *param; /* function param */ |
| 53 | }; |
| 54 | #define MAX_THREAD_APC 16 /* Max outstanding APCs for a thread */ |
| 55 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 56 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 57 | /* thread operations */ |
| 58 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 59 | static void dump_thread( struct object *obj, int verbose ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 60 | static int thread_signaled( struct object *obj, struct thread *thread ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 61 | extern void thread_poll_event( struct object *obj, int event ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 62 | static void destroy_thread( struct object *obj ); |
| 63 | |
| 64 | static const struct object_ops thread_ops = |
| 65 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 66 | sizeof(struct thread), /* size */ |
| 67 | dump_thread, /* dump */ |
| 68 | add_queue, /* add_queue */ |
| 69 | remove_queue, /* remove_queue */ |
| 70 | thread_signaled, /* signaled */ |
| 71 | no_satisfied, /* satisfied */ |
| 72 | NULL, /* get_poll_events */ |
| 73 | thread_poll_event, /* poll_event */ |
| 74 | no_read_fd, /* get_read_fd */ |
| 75 | no_write_fd, /* get_write_fd */ |
| 76 | no_flush, /* flush */ |
| 77 | no_get_file_info, /* get_file_info */ |
| 78 | destroy_thread /* destroy */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 81 | static struct thread *first_thread; |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 82 | static struct thread *booting_thread; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 83 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 84 | /* allocate the buffer for the communication with the client */ |
| 85 | static int alloc_client_buffer( struct thread *thread ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 86 | { |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 87 | struct get_thread_buffer_request *req; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 88 | int fd; |
| 89 | |
| 90 | if ((fd = create_anonymous_file()) == -1) return -1; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 91 | if (ftruncate( fd, MAX_REQUEST_LENGTH ) == -1) goto error; |
| 92 | if ((thread->buffer = mmap( 0, MAX_REQUEST_LENGTH, PROT_READ | PROT_WRITE, |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 93 | MAP_SHARED, fd, 0 )) == (void*)-1) goto error; |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 94 | /* build the first request into the buffer and send it */ |
| 95 | req = thread->buffer; |
| 96 | req->pid = get_process_id( thread->process ); |
| 97 | req->tid = get_thread_id( thread ); |
| 98 | req->boot = (thread == booting_thread); |
Alexandre Julliard | 5fb5456 | 2000-03-08 22:01:02 +0000 | [diff] [blame] | 99 | req->version = SERVER_PROTOCOL_VERSION; |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 100 | set_reply_fd( thread, fd ); |
| 101 | send_reply( thread ); |
| 102 | return 1; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 103 | |
| 104 | error: |
| 105 | file_set_error(); |
| 106 | if (fd != -1) close( fd ); |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 107 | return 0; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* create a new thread */ |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 111 | struct thread *create_thread( int fd, struct process *process ) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 112 | { |
| 113 | struct thread *thread; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 114 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 115 | int flags = fcntl( fd, F_GETFL, 0 ); |
| 116 | fcntl( fd, F_SETFL, flags | O_NONBLOCK ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 117 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 118 | if (!(thread = alloc_object( &thread_ops, fd ))) return NULL; |
| 119 | |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 120 | thread->unix_pid = 0; /* not known yet */ |
Alexandre Julliard | ea0d028 | 2000-03-10 22:16:10 +0000 | [diff] [blame] | 121 | thread->context = NULL; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 122 | thread->teb = NULL; |
| 123 | thread->mutex = NULL; |
| 124 | thread->debug_ctx = NULL; |
Alexandre Julliard | f6507ed | 2000-04-06 22:05:16 +0000 | [diff] [blame] | 125 | thread->debug_event = NULL; |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 126 | thread->queue = NULL; |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 127 | thread->info = NULL; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 128 | thread->wait = NULL; |
| 129 | thread->apc = NULL; |
| 130 | thread->apc_count = 0; |
| 131 | thread->error = 0; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 132 | thread->pass_fd = -1; |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 133 | thread->state = RUNNING; |
| 134 | thread->attached = 0; |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 135 | thread->exit_code = 0; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 136 | thread->next = NULL; |
| 137 | thread->prev = NULL; |
| 138 | thread->priority = THREAD_PRIORITY_NORMAL; |
| 139 | thread->affinity = 1; |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 140 | thread->suspend = 0; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 141 | thread->buffer = (void *)-1; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 142 | thread->last_req = REQ_GET_THREAD_BUFFER; |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 143 | thread->process = (struct process *)grab_object( process ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 144 | |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 145 | if (!current) current = thread; |
| 146 | |
| 147 | if (!booting_thread) /* first thread ever */ |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 148 | { |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 149 | booting_thread = thread; |
| 150 | lock_master_socket(1); |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 151 | } |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 152 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 153 | if ((thread->next = first_thread) != NULL) thread->next->prev = thread; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 154 | first_thread = thread; |
| 155 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 156 | set_select_events( &thread->obj, POLLIN ); /* start listening to events */ |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 157 | if (!alloc_client_buffer( thread )) goto error; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 158 | return thread; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 159 | |
| 160 | error: |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 161 | release_object( thread ); |
| 162 | return NULL; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 165 | /* handle a client event */ |
| 166 | void thread_poll_event( struct object *obj, int event ) |
| 167 | { |
| 168 | struct thread *thread = (struct thread *)obj; |
| 169 | assert( obj->ops == &thread_ops ); |
| 170 | |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 171 | if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 172 | else |
| 173 | { |
| 174 | if (event & POLLOUT) write_request( thread ); |
| 175 | if (event & POLLIN) read_request( thread ); |
| 176 | } |
| 177 | } |
| 178 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 179 | /* destroy a thread when its refcount is 0 */ |
| 180 | static void destroy_thread( struct object *obj ) |
| 181 | { |
| 182 | struct thread *thread = (struct thread *)obj; |
| 183 | assert( obj->ops == &thread_ops ); |
| 184 | |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 185 | assert( !thread->debug_ctx ); /* cannot still be debugging something */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 186 | release_object( thread->process ); |
| 187 | if (thread->next) thread->next->prev = thread->prev; |
| 188 | if (thread->prev) thread->prev->next = thread->next; |
| 189 | else first_thread = thread->next; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 190 | if (thread->apc) free( thread->apc ); |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 191 | if (thread->info) release_object( thread->info ); |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 192 | if (thread->queue) release_object( thread->queue ); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 193 | if (thread->buffer != (void *)-1) munmap( thread->buffer, MAX_REQUEST_LENGTH ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 194 | if (thread->pass_fd != -1) close( thread->pass_fd ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 197 | /* dump a thread on stdout for debugging purposes */ |
| 198 | static void dump_thread( struct object *obj, int verbose ) |
| 199 | { |
| 200 | struct thread *thread = (struct thread *)obj; |
| 201 | assert( obj->ops == &thread_ops ); |
| 202 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 203 | fprintf( stderr, "Thread pid=%d teb=%p state=%d\n", |
| 204 | thread->unix_pid, thread->teb, thread->state ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 207 | static int thread_signaled( struct object *obj, struct thread *thread ) |
| 208 | { |
| 209 | struct thread *mythread = (struct thread *)obj; |
| 210 | return (mythread->state == TERMINATED); |
| 211 | } |
| 212 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 213 | /* get a thread pointer from a thread id (and increment the refcount) */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 214 | struct thread *get_thread_from_id( void *id ) |
| 215 | { |
| 216 | struct thread *t = first_thread; |
| 217 | while (t && (t != id)) t = t->next; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 218 | if (t) grab_object( t ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 219 | return t; |
| 220 | } |
| 221 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 222 | /* get a thread from a handle (and increment the refcount) */ |
| 223 | struct thread *get_thread_from_handle( int handle, unsigned int access ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 224 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 225 | return (struct thread *)get_handle_obj( current->process, handle, |
| 226 | access, &thread_ops ); |
| 227 | } |
| 228 | |
Alexandre Julliard | 578c100 | 1999-11-14 21:23:21 +0000 | [diff] [blame] | 229 | /* find a thread from a Unix pid */ |
| 230 | struct thread *get_thread_from_pid( int pid ) |
| 231 | { |
| 232 | struct thread *t = first_thread; |
| 233 | while (t && (t->unix_pid != pid)) t = t->next; |
| 234 | return t; |
| 235 | } |
| 236 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 237 | /* set all information about a thread */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 238 | static void set_thread_info( struct thread *thread, |
| 239 | struct set_thread_info_request *req ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 240 | { |
| 241 | if (req->mask & SET_THREAD_INFO_PRIORITY) |
| 242 | thread->priority = req->priority; |
| 243 | if (req->mask & SET_THREAD_INFO_AFFINITY) |
| 244 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 245 | if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 246 | else thread->affinity = req->affinity; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* suspend a thread */ |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 251 | int suspend_thread( struct thread *thread, int check_limit ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 252 | { |
| 253 | int old_count = thread->suspend; |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 254 | if (thread->suspend < MAXIMUM_SUSPEND_COUNT || !check_limit) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 255 | { |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 256 | if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 257 | } |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 258 | else set_error( STATUS_SUSPEND_COUNT_EXCEEDED ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 259 | return old_count; |
| 260 | } |
| 261 | |
| 262 | /* resume a thread */ |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 263 | int resume_thread( struct thread *thread ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 264 | { |
| 265 | int old_count = thread->suspend; |
| 266 | if (thread->suspend > 0) |
| 267 | { |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 268 | if (!(--thread->suspend + thread->process->suspend)) continue_thread( thread ); |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 269 | } |
| 270 | return old_count; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Ulrich Weigand | 371fd75 | 1999-04-11 17:13:03 +0000 | [diff] [blame] | 273 | /* suspend all threads but the current */ |
| 274 | void suspend_all_threads( void ) |
| 275 | { |
| 276 | struct thread *thread; |
| 277 | for ( thread = first_thread; thread; thread = thread->next ) |
| 278 | if ( thread != current ) |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 279 | suspend_thread( thread, 0 ); |
Ulrich Weigand | 371fd75 | 1999-04-11 17:13:03 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* resume all threads but the current */ |
| 283 | void resume_all_threads( void ) |
| 284 | { |
| 285 | struct thread *thread; |
| 286 | for ( thread = first_thread; thread; thread = thread->next ) |
| 287 | if ( thread != current ) |
| 288 | resume_thread( thread ); |
| 289 | } |
| 290 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 291 | /* add a thread to an object wait queue; return 1 if OK, 0 on error */ |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 292 | int add_queue( struct object *obj, struct wait_queue_entry *entry ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 293 | { |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 294 | grab_object( obj ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 295 | entry->obj = obj; |
| 296 | entry->prev = obj->tail; |
| 297 | entry->next = NULL; |
| 298 | if (obj->tail) obj->tail->next = entry; |
| 299 | else obj->head = entry; |
| 300 | obj->tail = entry; |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 301 | return 1; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* remove a thread from an object wait queue */ |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 305 | void remove_queue( struct object *obj, struct wait_queue_entry *entry ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 306 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 307 | if (entry->next) entry->next->prev = entry->prev; |
| 308 | else obj->tail = entry->prev; |
| 309 | if (entry->prev) entry->prev->next = entry->next; |
| 310 | else obj->head = entry->next; |
| 311 | release_object( obj ); |
| 312 | } |
| 313 | |
| 314 | /* finish waiting */ |
| 315 | static void end_wait( struct thread *thread ) |
| 316 | { |
| 317 | struct thread_wait *wait = thread->wait; |
| 318 | struct wait_queue_entry *entry; |
| 319 | int i; |
| 320 | |
| 321 | assert( wait ); |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 322 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 323 | entry->obj->ops->remove_queue( entry->obj, entry ); |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 324 | if (wait->user) remove_timeout_user( wait->user ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 325 | free( wait ); |
| 326 | thread->wait = NULL; |
| 327 | } |
| 328 | |
| 329 | /* build the thread wait structure */ |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 330 | static int wait_on( int count, struct object *objects[], int flags, |
| 331 | int timeout, sleep_reply func ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 332 | { |
| 333 | struct thread_wait *wait; |
| 334 | struct wait_queue_entry *entry; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 335 | int i; |
| 336 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 337 | if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0; |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 338 | current->wait = wait; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 339 | wait->count = count; |
| 340 | wait->flags = flags; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 341 | wait->user = NULL; |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 342 | wait->reply = func; |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 343 | if (flags & SELECT_TIMEOUT) |
| 344 | { |
| 345 | gettimeofday( &wait->timeout, 0 ); |
| 346 | add_timeout( &wait->timeout, timeout ); |
| 347 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 348 | |
| 349 | for (i = 0, entry = wait->queues; i < count; i++, entry++) |
| 350 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 351 | struct object *obj = objects[i]; |
| 352 | entry->thread = current; |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 353 | if (!obj->ops->add_queue( obj, entry )) |
| 354 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 355 | wait->count = i; |
| 356 | end_wait( current ); |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 357 | return 0; |
| 358 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 359 | } |
| 360 | return 1; |
| 361 | } |
| 362 | |
| 363 | /* check if the thread waiting condition is satisfied */ |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 364 | static int check_wait( struct thread *thread, struct object **object ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 365 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 366 | int i, signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 367 | struct thread_wait *wait = thread->wait; |
| 368 | struct wait_queue_entry *entry = wait->queues; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 369 | |
| 370 | assert( wait ); |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 371 | *object = NULL; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 372 | if (wait->flags & SELECT_ALL) |
| 373 | { |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 374 | int not_ok = 0; |
| 375 | /* Note: we must check them all anyway, as some objects may |
| 376 | * want to do something when signaled, even if others are not */ |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 377 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 378 | not_ok |= !entry->obj->ops->signaled( entry->obj, thread ); |
| 379 | if (not_ok) goto other_checks; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 380 | /* Wait satisfied: tell it to all objects */ |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 381 | signaled = 0; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 382 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 383 | if (entry->obj->ops->satisfied( entry->obj, thread )) |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 384 | signaled = STATUS_ABANDONED_WAIT_0; |
| 385 | return signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 386 | } |
| 387 | else |
| 388 | { |
| 389 | for (i = 0, entry = wait->queues; i < wait->count; i++, entry++) |
| 390 | { |
| 391 | if (!entry->obj->ops->signaled( entry->obj, thread )) continue; |
| 392 | /* Wait satisfied: tell it to the object */ |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 393 | signaled = i; |
| 394 | *object = entry->obj; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 395 | if (entry->obj->ops->satisfied( entry->obj, thread )) |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 396 | signaled = i + STATUS_ABANDONED_WAIT_0; |
| 397 | return signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 400 | |
| 401 | other_checks: |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 402 | if ((wait->flags & SELECT_ALERTABLE) && thread->apc) return STATUS_USER_APC; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 403 | if (wait->flags & SELECT_TIMEOUT) |
| 404 | { |
| 405 | struct timeval now; |
| 406 | gettimeofday( &now, NULL ); |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 407 | if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 408 | } |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | /* build a reply to the select request */ |
| 413 | static void build_select_reply( struct thread *thread, struct object *obj, int signaled ) |
| 414 | { |
| 415 | struct select_request *req = get_req_ptr( thread ); |
| 416 | req->signaled = signaled; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 419 | /* attempt to wake up a thread */ |
| 420 | /* return 1 if OK, 0 if the wait condition is still not satisfied */ |
| 421 | static int wake_thread( struct thread *thread ) |
| 422 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 423 | int signaled; |
| 424 | struct object *object; |
| 425 | if ((signaled = check_wait( thread, &object )) == -1) return 0; |
| 426 | thread->error = 0; |
| 427 | thread->wait->reply( thread, object, signaled ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 428 | end_wait( thread ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 429 | return 1; |
| 430 | } |
| 431 | |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 432 | /* thread wait timeout */ |
| 433 | static void thread_timeout( void *ptr ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 434 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 435 | struct thread *thread = ptr; |
| 436 | if (debug_level) fprintf( stderr, "%08x: *timeout*\n", (unsigned int)thread ); |
| 437 | assert( thread->wait ); |
| 438 | thread->error = 0; |
| 439 | thread->wait->user = NULL; |
| 440 | thread->wait->reply( thread, NULL, STATUS_TIMEOUT ); |
| 441 | end_wait( thread ); |
| 442 | send_reply( thread ); |
| 443 | } |
| 444 | |
| 445 | /* sleep on a list of objects */ |
| 446 | int sleep_on( int count, struct object *objects[], int flags, int timeout, sleep_reply func ) |
| 447 | { |
| 448 | assert( !current->wait ); |
| 449 | if (!wait_on( count, objects, flags, timeout, func )) return 0; |
| 450 | if (wake_thread( current )) return 1; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 451 | /* now we need to wait */ |
| 452 | if (flags & SELECT_TIMEOUT) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 453 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 454 | if (!(current->wait->user = add_timeout_user( ¤t->wait->timeout, |
| 455 | thread_timeout, current ))) |
| 456 | { |
| 457 | end_wait( current ); |
| 458 | return 0; |
| 459 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 460 | } |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 461 | return 1; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 464 | /* select on a list of handles */ |
| 465 | static int select_on( int count, int *handles, int flags, int timeout ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 466 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 467 | int ret = 0; |
| 468 | int i; |
| 469 | struct object *objects[MAXIMUM_WAIT_OBJECTS]; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 470 | |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 471 | if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS)) |
| 472 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 473 | set_error( STATUS_INVALID_PARAMETER ); |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | for (i = 0; i < count; i++) |
| 477 | { |
| 478 | if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL ))) |
| 479 | break; |
| 480 | } |
| 481 | if (i == count) ret = sleep_on( count, objects, flags, timeout, build_select_reply ); |
| 482 | while (--i >= 0) release_object( objects[i] ); |
| 483 | return ret; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* attempt to wake threads sleeping on the object wait queue */ |
| 487 | void wake_up( struct object *obj, int max ) |
| 488 | { |
| 489 | struct wait_queue_entry *entry = obj->head; |
| 490 | |
| 491 | while (entry) |
| 492 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 493 | struct thread *thread = entry->thread; |
| 494 | entry = entry->next; |
| 495 | if (wake_thread( thread )) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 496 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 497 | send_reply( thread ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 498 | if (max && !--max) break; |
| 499 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 503 | /* queue an async procedure call */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 504 | static int thread_queue_apc( struct thread *thread, void *func, void *param ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 505 | { |
| 506 | struct thread_apc *apc; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 507 | if (!thread->apc) |
| 508 | { |
| 509 | if (!(thread->apc = mem_alloc( MAX_THREAD_APC * sizeof(*apc) ))) |
| 510 | return 0; |
| 511 | thread->apc_count = 0; |
| 512 | } |
| 513 | else if (thread->apc_count >= MAX_THREAD_APC) return 0; |
| 514 | thread->apc[thread->apc_count].func = func; |
| 515 | thread->apc[thread->apc_count].param = param; |
| 516 | thread->apc_count++; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 517 | if (thread->wait) |
| 518 | { |
| 519 | if (wake_thread( thread )) send_reply( thread ); |
| 520 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 521 | return 1; |
| 522 | } |
| 523 | |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 524 | /* retrieve an LDT selector entry */ |
| 525 | static void get_selector_entry( struct thread *thread, int entry, |
| 526 | unsigned int *base, unsigned int *limit, |
| 527 | unsigned char *flags ) |
| 528 | { |
| 529 | if (!thread->process->ldt_copy || !thread->process->ldt_flags) |
| 530 | { |
| 531 | set_error( STATUS_ACCESS_DENIED ); |
| 532 | return; |
| 533 | } |
| 534 | if (entry >= 8192) |
| 535 | { |
| 536 | set_error( STATUS_INVALID_PARAMETER ); /* FIXME */ |
| 537 | return; |
| 538 | } |
Alexandre Julliard | 98aacc7 | 2000-03-15 19:46:14 +0000 | [diff] [blame] | 539 | if (suspend_for_ptrace( thread )) |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 540 | { |
| 541 | unsigned char flags_buf[4]; |
| 542 | int *addr = (int *)thread->process->ldt_copy + 2 * entry; |
| 543 | if (read_thread_int( thread, addr, base ) == -1) goto done; |
| 544 | if (read_thread_int( thread, addr + 1, limit ) == -1) goto done; |
| 545 | addr = (int *)thread->process->ldt_flags + (entry >> 2); |
| 546 | if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done; |
| 547 | *flags = flags_buf[entry & 3]; |
Alexandre Julliard | 98aacc7 | 2000-03-15 19:46:14 +0000 | [diff] [blame] | 548 | done: |
| 549 | resume_thread( thread ); |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 550 | } |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 553 | /* kill a thread on the spot */ |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 554 | void kill_thread( struct thread *thread, int violent_death ) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 555 | { |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 556 | if (thread->state == TERMINATED) return; /* already killed */ |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 557 | thread->state = TERMINATED; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 558 | if (current == thread) current = NULL; |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 559 | if (debug_level) |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 560 | fprintf( stderr,"%08x: *killed* exit_code=%d\n", |
| 561 | (unsigned int)thread, thread->exit_code ); |
| 562 | if (thread->wait) |
| 563 | { |
| 564 | end_wait( thread ); |
| 565 | /* if it is waiting on the socket, we don't need to send a SIGTERM */ |
| 566 | violent_death = 0; |
| 567 | } |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 568 | debug_exit_thread( thread ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 569 | abandon_mutexes( thread ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 570 | remove_process_thread( thread->process, thread ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 571 | wake_up( &thread->obj, 0 ); |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 572 | detach_thread( thread, violent_death ? SIGTERM : 0 ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 573 | remove_select_user( &thread->obj ); |
Alexandre Julliard | 9a0e28f | 2000-03-25 19:14:37 +0000 | [diff] [blame] | 574 | munmap( thread->buffer, MAX_REQUEST_LENGTH ); |
| 575 | thread->buffer = (void *)-1; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 576 | release_object( thread ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 577 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 578 | |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 579 | /* take a snapshot of currently running threads */ |
| 580 | struct thread_snapshot *thread_snap( int *count ) |
| 581 | { |
| 582 | struct thread_snapshot *snapshot, *ptr; |
| 583 | struct thread *thread; |
| 584 | int total = 0; |
| 585 | |
| 586 | for (thread = first_thread; thread; thread = thread->next) |
| 587 | if (thread->state != TERMINATED) total++; |
| 588 | if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL; |
| 589 | ptr = snapshot; |
| 590 | for (thread = first_thread; thread; thread = thread->next) |
| 591 | { |
| 592 | if (thread->state == TERMINATED) continue; |
| 593 | ptr->thread = thread; |
| 594 | ptr->count = thread->obj.refcount; |
| 595 | ptr->priority = thread->priority; |
| 596 | grab_object( thread ); |
| 597 | ptr++; |
| 598 | } |
| 599 | *count = total; |
| 600 | return snapshot; |
| 601 | } |
| 602 | |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 603 | /* signal that we are finished booting on the client side */ |
| 604 | DECL_HANDLER(boot_done) |
| 605 | { |
Alexandre Julliard | f6507ed | 2000-04-06 22:05:16 +0000 | [diff] [blame] | 606 | debug_level = max( debug_level, req->debug_level ); |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 607 | /* Make sure last_req is initialized */ |
| 608 | current->last_req = REQ_BOOT_DONE; |
| 609 | if (current == booting_thread) |
| 610 | { |
| 611 | booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */ |
| 612 | lock_master_socket(0); /* allow other clients now */ |
| 613 | } |
| 614 | } |
| 615 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 616 | /* create a new thread */ |
| 617 | DECL_HANDLER(new_thread) |
| 618 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 619 | struct thread *thread; |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 620 | int sock[2]; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 621 | |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 622 | if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 623 | { |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 624 | if ((thread = create_thread( sock[0], current->process ))) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 625 | { |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 626 | if (req->suspend) thread->suspend++; |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 627 | req->tid = thread; |
| 628 | if ((req->handle = alloc_handle( current->process, thread, |
| 629 | THREAD_ALL_ACCESS, req->inherit )) != -1) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 630 | { |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 631 | set_reply_fd( current, sock[1] ); |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 632 | /* thread object will be released when the thread gets killed */ |
Alexandre Julliard | 6a72dc5 | 2000-04-14 13:42:00 +0000 | [diff] [blame] | 633 | add_process_thread( current->process, thread ); |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 634 | return; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 635 | } |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 636 | release_object( thread ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 637 | } |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 638 | close( sock[1] ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 639 | } |
Alexandre Julliard | 95e7acb | 2000-01-04 02:40:22 +0000 | [diff] [blame] | 640 | else file_set_error(); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* retrieve the thread buffer file descriptor */ |
| 644 | DECL_HANDLER(get_thread_buffer) |
| 645 | { |
| 646 | fatal_protocol_error( current, "get_thread_buffer: should never get called directly\n" ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /* initialize a new thread */ |
| 650 | DECL_HANDLER(init_thread) |
| 651 | { |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 652 | if (current->unix_pid) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 653 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 654 | fatal_protocol_error( current, "init_thread: already running\n" ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 655 | return; |
| 656 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 657 | current->unix_pid = req->unix_pid; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 658 | current->teb = req->teb; |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 659 | if (current->suspend + current->process->suspend > 0) stop_thread( current ); |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 660 | if (current->process->running_threads > 1) |
Alexandre Julliard | b73421d | 2000-03-30 19:30:24 +0000 | [diff] [blame] | 661 | generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* terminate a thread */ |
| 665 | DECL_HANDLER(terminate_thread) |
| 666 | { |
| 667 | struct thread *thread; |
| 668 | |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 669 | req->self = 0; |
| 670 | req->last = 0; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 671 | if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE ))) |
| 672 | { |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 673 | thread->exit_code = req->exit_code; |
| 674 | if (thread != current) kill_thread( thread, 1 ); |
| 675 | else |
| 676 | { |
| 677 | req->self = 1; |
| 678 | req->last = (thread->process->running_threads == 1); |
| 679 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 680 | release_object( thread ); |
| 681 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* fetch information about a thread */ |
| 685 | DECL_HANDLER(get_thread_info) |
| 686 | { |
| 687 | struct thread *thread; |
Alexandre Julliard | 9a0e28f | 2000-03-25 19:14:37 +0000 | [diff] [blame] | 688 | int handle = req->handle; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 689 | |
Alexandre Julliard | 9a0e28f | 2000-03-25 19:14:37 +0000 | [diff] [blame] | 690 | if (handle == -1) thread = get_thread_from_id( req->tid_in ); |
| 691 | else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION ); |
| 692 | |
| 693 | if (thread) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 694 | { |
Alexandre Julliard | 9a0e28f | 2000-03-25 19:14:37 +0000 | [diff] [blame] | 695 | req->tid = get_thread_id( thread ); |
| 696 | req->teb = thread->teb; |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 697 | req->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 698 | req->priority = thread->priority; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 699 | release_object( thread ); |
| 700 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /* set information about a thread */ |
| 704 | DECL_HANDLER(set_thread_info) |
| 705 | { |
| 706 | struct thread *thread; |
| 707 | |
| 708 | if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION ))) |
| 709 | { |
| 710 | set_thread_info( thread, req ); |
| 711 | release_object( thread ); |
| 712 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* suspend a thread */ |
| 716 | DECL_HANDLER(suspend_thread) |
| 717 | { |
| 718 | struct thread *thread; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 719 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 720 | if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) |
| 721 | { |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 722 | req->count = suspend_thread( thread, 1 ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 723 | release_object( thread ); |
| 724 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* resume a thread */ |
| 728 | DECL_HANDLER(resume_thread) |
| 729 | { |
| 730 | struct thread *thread; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 731 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 732 | if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) |
| 733 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 734 | req->count = resume_thread( thread ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 735 | release_object( thread ); |
| 736 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /* select on a handle list */ |
| 740 | DECL_HANDLER(select) |
| 741 | { |
Alexandre Julliard | 9de03f4 | 2000-01-04 02:23:38 +0000 | [diff] [blame] | 742 | if (!select_on( req->count, req->handles, req->flags, req->timeout )) |
| 743 | req->signaled = -1; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* queue an APC for a thread */ |
| 747 | DECL_HANDLER(queue_apc) |
| 748 | { |
| 749 | struct thread *thread; |
| 750 | if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) |
| 751 | { |
| 752 | thread_queue_apc( thread, req->func, req->param ); |
| 753 | release_object( thread ); |
| 754 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 755 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 756 | |
| 757 | /* get list of APC to call */ |
| 758 | DECL_HANDLER(get_apcs) |
| 759 | { |
| 760 | if ((req->count = current->apc_count)) |
| 761 | { |
| 762 | memcpy( req->apcs, current->apc, current->apc_count * sizeof(*current->apc) ); |
| 763 | free( current->apc ); |
| 764 | current->apc = NULL; |
| 765 | current->apc_count = 0; |
| 766 | } |
| 767 | } |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 768 | |
| 769 | /* fetch a selector entry for a thread */ |
| 770 | DECL_HANDLER(get_selector_entry) |
| 771 | { |
| 772 | struct thread *thread; |
| 773 | if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION ))) |
| 774 | { |
| 775 | get_selector_entry( thread, req->entry, &req->base, &req->limit, &req->flags ); |
| 776 | release_object( thread ); |
| 777 | } |
| 778 | } |