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