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