sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- A user-space pthreads implementation. vg_scheduler.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, an x86 protected-mode emulator |
| 8 | designed for debugging and profiling binaries on x86-Unixes. |
| 9 | |
| 10 | Copyright (C) 2000-2002 Julian Seward |
| 11 | jseward@acm.org |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 28 | The GNU General Public License is contained in the file COPYING. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #include "vg_include.h" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 32 | #include "valgrind.h" /* for VG_USERREQ__RUNNING_ON_VALGRIND and |
| 33 | VG_USERREQ__DISCARD_TRANSLATIONS */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 34 | |
sewardj | b60c1ad | 2002-05-29 20:23:26 +0000 | [diff] [blame] | 35 | /* BORKAGE/ISSUES as of 29 May 02 |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 36 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 37 | - Currently, when a signal is run, just the ThreadStatus.status fields |
| 38 | are saved in the signal frame, along with the CPU state. Question: |
| 39 | should I also save and restore: |
| 40 | ThreadStatus.joiner |
| 41 | ThreadStatus.waited_on_mid |
| 42 | ThreadStatus.awaken_at |
| 43 | ThreadStatus.retval |
| 44 | Currently unsure, and so am not doing so. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 45 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 46 | - Signals interrupting read/write and nanosleep: SA_RESTART settings. |
| 47 | Read/write correctly return with EINTR when SA_RESTART isn't |
| 48 | specified and they are interrupted by a signal. nanosleep just |
| 49 | pretends signals don't exist -- should be fixed. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 50 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 51 | - So, what's the deal with signals and mutexes? If a thread is |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 52 | blocked on a mutex, or for a condition variable for that matter, can |
| 53 | signals still be delivered to it? This has serious consequences -- |
| 54 | deadlocks, etc. |
| 55 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 56 | - Signals still not really right. Each thread should have its |
| 57 | own pending-set, but there is just one process-wide pending set. |
| 58 | |
sewardj | b60c1ad | 2002-05-29 20:23:26 +0000 | [diff] [blame] | 59 | TODO for valgrind-1.0: |
| 60 | |
sewardj | 055fbb8 | 2002-05-30 00:40:55 +0000 | [diff] [blame] | 61 | - Update assertion checking in scheduler_sanity(). |
| 62 | |
sewardj | b60c1ad | 2002-05-29 20:23:26 +0000 | [diff] [blame] | 63 | TODO sometime: |
| 64 | |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 65 | - poll() in the vg_libpthread.c -- should it handle the nanosleep |
| 66 | being interrupted by a signal? Ditto accept? |
| 67 | |
sewardj | b60c1ad | 2002-05-29 20:23:26 +0000 | [diff] [blame] | 68 | - Mutex scrubbing - clearup_after_thread_exit: look for threads |
| 69 | blocked on mutexes held by the exiting thread, and release them |
| 70 | appropriately. (??) |
| 71 | |
| 72 | - pthread_atfork |
| 73 | |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 74 | */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | /* --------------------------------------------------------------------- |
| 78 | Types and globals for the scheduler. |
| 79 | ------------------------------------------------------------------ */ |
| 80 | |
| 81 | /* type ThreadId is defined in vg_include.h. */ |
| 82 | |
| 83 | /* struct ThreadState is defined in vg_include.h. */ |
| 84 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 85 | /* Globals. A statically allocated array of threads. NOTE: [0] is |
| 86 | never used, to simplify the simulation of initialisers for |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 87 | LinuxThreads. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 88 | ThreadState VG_(threads)[VG_N_THREADS]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 89 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 90 | /* The process' fork-handler stack. */ |
| 91 | static Int vg_fhstack_used = 0; |
| 92 | static ForkHandlerEntry vg_fhstack[VG_N_FORKHANDLERSTACK]; |
| 93 | |
| 94 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 95 | /* The tid of the thread currently in VG_(baseBlock). */ |
| 96 | static Int vg_tid_currently_in_baseBlock = VG_INVALID_THREADID; |
| 97 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 98 | |
| 99 | /* vg_oursignalhandler() might longjmp(). Here's the jmp_buf. */ |
| 100 | jmp_buf VG_(scheduler_jmpbuf); |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 101 | /* This says whether scheduler_jmpbuf is actually valid. Needed so |
| 102 | that our signal handler doesn't longjmp when the buffer isn't |
| 103 | actually valid. */ |
| 104 | Bool VG_(scheduler_jmpbuf_valid) = False; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 105 | /* ... and if so, here's the signal which caused it to do so. */ |
| 106 | Int VG_(longjmpd_on_signal); |
| 107 | |
| 108 | |
| 109 | /* Machinery to keep track of which threads are waiting on which |
| 110 | fds. */ |
| 111 | typedef |
| 112 | struct { |
| 113 | /* The thread which made the request. */ |
| 114 | ThreadId tid; |
| 115 | |
| 116 | /* The next two fields describe the request. */ |
| 117 | /* File descriptor waited for. -1 means this slot is not in use */ |
| 118 | Int fd; |
| 119 | /* The syscall number the fd is used in. */ |
| 120 | Int syscall_no; |
| 121 | |
| 122 | /* False => still waiting for select to tell us the fd is ready |
| 123 | to go. True => the fd is ready, but the results have not yet |
| 124 | been delivered back to the calling thread. Once the latter |
| 125 | happens, this entire record is marked as no longer in use, by |
| 126 | making the fd field be -1. */ |
| 127 | Bool ready; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 128 | |
| 129 | /* The result from SK_(pre_blocking_syscall)(); is passed to |
| 130 | * SK_(post_blocking_syscall)(). */ |
| 131 | void* pre_result; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 132 | } |
| 133 | VgWaitedOnFd; |
| 134 | |
| 135 | static VgWaitedOnFd vg_waiting_fds[VG_N_WAITING_FDS]; |
| 136 | |
| 137 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 138 | /* Keeping track of keys. */ |
| 139 | typedef |
| 140 | struct { |
| 141 | /* Has this key been allocated ? */ |
| 142 | Bool inuse; |
| 143 | /* If .inuse==True, records the address of the associated |
| 144 | destructor, or NULL if none. */ |
| 145 | void (*destructor)(void*); |
| 146 | } |
| 147 | ThreadKeyState; |
| 148 | |
| 149 | /* And our array of thread keys. */ |
| 150 | static ThreadKeyState vg_thread_keys[VG_N_THREAD_KEYS]; |
| 151 | |
| 152 | typedef UInt ThreadKey; |
| 153 | |
| 154 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 155 | UInt VG_(written_shadow_reg); |
| 156 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 157 | /* Forwards */ |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 158 | static void do_client_request ( ThreadId tid ); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 159 | static void scheduler_sanity ( void ); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 160 | static void do_pthread_cond_timedwait_TIMEOUT ( ThreadId tid ); |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 161 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 162 | /* --------------------------------------------------------------------- |
| 163 | Helper functions for the scheduler. |
| 164 | ------------------------------------------------------------------ */ |
| 165 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 166 | __inline__ |
| 167 | Bool VG_(is_valid_tid) ( ThreadId tid ) |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 168 | { |
| 169 | /* tid is unsigned, hence no < 0 test. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 170 | if (tid == 0) return False; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 171 | if (tid >= VG_N_THREADS) return False; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 172 | if (VG_(threads)[tid].status == VgTs_Empty) return False; |
| 173 | return True; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | __inline__ |
| 178 | Bool VG_(is_valid_or_empty_tid) ( ThreadId tid ) |
| 179 | { |
| 180 | /* tid is unsigned, hence no < 0 test. */ |
| 181 | if (tid == 0) return False; |
| 182 | if (tid >= VG_N_THREADS) return False; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 183 | return True; |
| 184 | } |
| 185 | |
| 186 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 187 | /* For constructing error messages only: try and identify a thread |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 188 | whose stack satisfies the predicate p, or return VG_INVALID_THREADID |
| 189 | if none do. A small complication is dealing with any currently |
| 190 | VG_(baseBlock)-resident thread. |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 191 | */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 192 | ThreadId VG_(any_matching_thread_stack) |
| 193 | ( Bool (*p) ( Addr stack_min, Addr stack_max )) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 194 | { |
| 195 | ThreadId tid, tid_to_skip; |
| 196 | |
| 197 | tid_to_skip = VG_INVALID_THREADID; |
| 198 | |
| 199 | /* First check to see if there's a currently-loaded thread in |
| 200 | VG_(baseBlock). */ |
| 201 | if (vg_tid_currently_in_baseBlock != VG_INVALID_THREADID) { |
| 202 | tid = vg_tid_currently_in_baseBlock; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 203 | if ( p ( VG_(baseBlock)[VGOFF_(m_esp)], |
| 204 | VG_(threads)[tid].stack_highest_word) ) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 205 | return tid; |
| 206 | else |
| 207 | tid_to_skip = tid; |
| 208 | } |
| 209 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 210 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 211 | if (VG_(threads)[tid].status == VgTs_Empty) continue; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 212 | if (tid == tid_to_skip) continue; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 213 | if ( p ( VG_(threads)[tid].m_esp, |
| 214 | VG_(threads)[tid].stack_highest_word) ) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 215 | return tid; |
| 216 | } |
| 217 | return VG_INVALID_THREADID; |
| 218 | } |
| 219 | |
| 220 | |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 221 | /* Print the scheduler status. */ |
| 222 | void VG_(pp_sched_status) ( void ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 223 | { |
| 224 | Int i; |
| 225 | VG_(printf)("\nsched status:\n"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 226 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 227 | if (VG_(threads)[i].status == VgTs_Empty) continue; |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 228 | VG_(printf)("\nThread %d: status = ", i); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 229 | switch (VG_(threads)[i].status) { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 230 | case VgTs_Runnable: VG_(printf)("Runnable"); break; |
| 231 | case VgTs_WaitFD: VG_(printf)("WaitFD"); break; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 232 | case VgTs_WaitJoinee: VG_(printf)("WaitJoinee(%d)", |
| 233 | VG_(threads)[i].joiner_jee_tid); |
| 234 | break; |
| 235 | case VgTs_WaitJoiner: VG_(printf)("WaitJoiner"); break; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 236 | case VgTs_Sleeping: VG_(printf)("Sleeping"); break; |
| 237 | case VgTs_WaitMX: VG_(printf)("WaitMX"); break; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 238 | case VgTs_WaitCV: VG_(printf)("WaitCV"); break; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 239 | case VgTs_WaitSIG: VG_(printf)("WaitSIG"); break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 240 | default: VG_(printf)("???"); break; |
| 241 | } |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 242 | VG_(printf)(", associated_mx = %p, associated_cv = %p\n", |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 243 | VG_(threads)[i].associated_mx, |
| 244 | VG_(threads)[i].associated_cv ); |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 245 | VG_(pp_ExeContext)( |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 246 | VG_(get_ExeContext2)( VG_(threads)[i].m_eip, VG_(threads)[i].m_ebp, |
| 247 | VG_(threads)[i].m_esp, |
| 248 | VG_(threads)[i].stack_highest_word) |
| 249 | ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 250 | } |
| 251 | VG_(printf)("\n"); |
| 252 | } |
| 253 | |
| 254 | static |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 255 | void add_waiting_fd ( ThreadId tid, Int fd, Int syscall_no, void* pre_res ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 256 | { |
| 257 | Int i; |
| 258 | |
| 259 | vg_assert(fd != -1); /* avoid total chaos */ |
| 260 | |
| 261 | for (i = 0; i < VG_N_WAITING_FDS; i++) |
| 262 | if (vg_waiting_fds[i].fd == -1) |
| 263 | break; |
| 264 | |
| 265 | if (i == VG_N_WAITING_FDS) |
| 266 | VG_(panic)("add_waiting_fd: VG_N_WAITING_FDS is too low"); |
| 267 | /* |
| 268 | VG_(printf)("add_waiting_fd: add (tid %d, fd %d) at slot %d\n", |
| 269 | tid, fd, i); |
| 270 | */ |
| 271 | vg_waiting_fds[i].fd = fd; |
| 272 | vg_waiting_fds[i].tid = tid; |
| 273 | vg_waiting_fds[i].ready = False; |
| 274 | vg_waiting_fds[i].syscall_no = syscall_no; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 275 | vg_waiting_fds[i].pre_result = pre_res; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
| 279 | |
| 280 | static |
| 281 | void print_sched_event ( ThreadId tid, Char* what ) |
| 282 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 283 | VG_(message)(Vg_DebugMsg, " SCHED[%d]: %s", tid, what ); |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
| 287 | static |
| 288 | void print_pthread_event ( ThreadId tid, Char* what ) |
| 289 | { |
| 290 | VG_(message)(Vg_DebugMsg, "PTHREAD[%d]: %s", tid, what ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
| 294 | static |
| 295 | Char* name_of_sched_event ( UInt event ) |
| 296 | { |
| 297 | switch (event) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 298 | case VG_TRC_EBP_JMP_SYSCALL: return "SYSCALL"; |
| 299 | case VG_TRC_EBP_JMP_CLIENTREQ: return "CLIENTREQ"; |
| 300 | case VG_TRC_INNER_COUNTERZERO: return "COUNTERZERO"; |
| 301 | case VG_TRC_INNER_FASTMISS: return "FASTMISS"; |
| 302 | case VG_TRC_UNRESUMABLE_SIGNAL: return "FATALSIGNAL"; |
| 303 | default: return "??UNKNOWN??"; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /* Create a translation of the client basic block beginning at |
| 309 | orig_addr, and add it to the translation cache & translation table. |
| 310 | This probably doesn't really belong here, but, hey ... |
| 311 | */ |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 312 | static |
| 313 | void create_translation_for ( ThreadId tid, Addr orig_addr ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 314 | { |
| 315 | Addr trans_addr; |
| 316 | TTEntry tte; |
| 317 | Int orig_size, trans_size; |
| 318 | /* Ensure there is space to hold a translation. */ |
| 319 | VG_(maybe_do_lru_pass)(); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 320 | VG_(translate)( &VG_(threads)[tid], |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 321 | orig_addr, &orig_size, &trans_addr, &trans_size ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 322 | /* Copy data at trans_addr into the translation cache. |
| 323 | Returned pointer is to the code, not to the 4-byte |
| 324 | header. */ |
| 325 | /* Since the .orig_size and .trans_size fields are |
| 326 | UShort, be paranoid. */ |
| 327 | vg_assert(orig_size > 0 && orig_size < 65536); |
| 328 | vg_assert(trans_size > 0 && trans_size < 65536); |
| 329 | tte.orig_size = orig_size; |
| 330 | tte.orig_addr = orig_addr; |
| 331 | tte.trans_size = trans_size; |
| 332 | tte.trans_addr = VG_(copy_to_transcache) |
| 333 | ( trans_addr, trans_size ); |
| 334 | tte.mru_epoch = VG_(current_epoch); |
| 335 | /* Free the intermediary -- was allocated by VG_(emit_code). */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 336 | VG_(arena_free)( VG_AR_JITTER, (void*)trans_addr ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 337 | /* Add to trans tab and set back pointer. */ |
| 338 | VG_(add_to_trans_tab) ( &tte ); |
| 339 | /* Update stats. */ |
| 340 | VG_(this_epoch_in_count) ++; |
| 341 | VG_(this_epoch_in_osize) += orig_size; |
| 342 | VG_(this_epoch_in_tsize) += trans_size; |
| 343 | VG_(overall_in_count) ++; |
| 344 | VG_(overall_in_osize) += orig_size; |
| 345 | VG_(overall_in_tsize) += trans_size; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
| 349 | /* Allocate a completely empty ThreadState record. */ |
| 350 | static |
| 351 | ThreadId vg_alloc_ThreadState ( void ) |
| 352 | { |
| 353 | Int i; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 354 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 355 | if (VG_(threads)[i].status == VgTs_Empty) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 356 | return i; |
| 357 | } |
| 358 | VG_(printf)("vg_alloc_ThreadState: no free slots available\n"); |
| 359 | VG_(printf)("Increase VG_N_THREADS, rebuild and try again.\n"); |
| 360 | VG_(panic)("VG_N_THREADS is too low"); |
| 361 | /*NOTREACHED*/ |
| 362 | } |
| 363 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 364 | ThreadState* VG_(get_ThreadState)( ThreadId tid ) |
| 365 | { |
| 366 | vg_assert(tid >= 0 && tid < VG_N_THREADS); |
| 367 | return & VG_(threads)[tid]; |
| 368 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 369 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 370 | ThreadState* VG_(get_current_thread_state) ( void ) |
| 371 | { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 372 | vg_assert(VG_(is_valid_tid)(vg_tid_currently_in_baseBlock)); |
| 373 | return & VG_(threads)[vg_tid_currently_in_baseBlock]; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | |
| 377 | ThreadId VG_(get_current_tid) ( void ) |
| 378 | { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 379 | vg_assert(VG_(is_valid_tid)(vg_tid_currently_in_baseBlock)); |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 380 | return vg_tid_currently_in_baseBlock; |
| 381 | } |
| 382 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 383 | ThreadId VG_(get_current_tid_1_if_root) ( void ) |
| 384 | { |
| 385 | if (0 == vg_tid_currently_in_baseBlock) |
| 386 | return 1; /* root thread */ |
| 387 | |
| 388 | vg_assert(VG_(is_valid_tid)(vg_tid_currently_in_baseBlock)); |
| 389 | return vg_tid_currently_in_baseBlock; |
| 390 | } |
| 391 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 392 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 393 | /* Copy the saved state of a thread into VG_(baseBlock), ready for it |
| 394 | to be run. */ |
| 395 | __inline__ |
| 396 | void VG_(load_thread_state) ( ThreadId tid ) |
| 397 | { |
| 398 | Int i; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 399 | vg_assert(vg_tid_currently_in_baseBlock == VG_INVALID_THREADID); |
| 400 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 401 | VG_(baseBlock)[VGOFF_(ldt)] = (UInt)VG_(threads)[tid].ldt; |
| 402 | VG_(baseBlock)[VGOFF_(m_cs)] = VG_(threads)[tid].m_cs; |
| 403 | VG_(baseBlock)[VGOFF_(m_ss)] = VG_(threads)[tid].m_ss; |
| 404 | VG_(baseBlock)[VGOFF_(m_ds)] = VG_(threads)[tid].m_ds; |
| 405 | VG_(baseBlock)[VGOFF_(m_es)] = VG_(threads)[tid].m_es; |
| 406 | VG_(baseBlock)[VGOFF_(m_fs)] = VG_(threads)[tid].m_fs; |
| 407 | VG_(baseBlock)[VGOFF_(m_gs)] = VG_(threads)[tid].m_gs; |
| 408 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 409 | VG_(baseBlock)[VGOFF_(m_eax)] = VG_(threads)[tid].m_eax; |
| 410 | VG_(baseBlock)[VGOFF_(m_ebx)] = VG_(threads)[tid].m_ebx; |
| 411 | VG_(baseBlock)[VGOFF_(m_ecx)] = VG_(threads)[tid].m_ecx; |
| 412 | VG_(baseBlock)[VGOFF_(m_edx)] = VG_(threads)[tid].m_edx; |
| 413 | VG_(baseBlock)[VGOFF_(m_esi)] = VG_(threads)[tid].m_esi; |
| 414 | VG_(baseBlock)[VGOFF_(m_edi)] = VG_(threads)[tid].m_edi; |
| 415 | VG_(baseBlock)[VGOFF_(m_ebp)] = VG_(threads)[tid].m_ebp; |
| 416 | VG_(baseBlock)[VGOFF_(m_esp)] = VG_(threads)[tid].m_esp; |
| 417 | VG_(baseBlock)[VGOFF_(m_eflags)] = VG_(threads)[tid].m_eflags; |
| 418 | VG_(baseBlock)[VGOFF_(m_eip)] = VG_(threads)[tid].m_eip; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 419 | |
| 420 | for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 421 | VG_(baseBlock)[VGOFF_(m_fpustate) + i] = VG_(threads)[tid].m_fpu[i]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 422 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 423 | if (VG_(needs).shadow_regs) { |
| 424 | VG_(baseBlock)[VGOFF_(sh_eax)] = VG_(threads)[tid].sh_eax; |
| 425 | VG_(baseBlock)[VGOFF_(sh_ebx)] = VG_(threads)[tid].sh_ebx; |
| 426 | VG_(baseBlock)[VGOFF_(sh_ecx)] = VG_(threads)[tid].sh_ecx; |
| 427 | VG_(baseBlock)[VGOFF_(sh_edx)] = VG_(threads)[tid].sh_edx; |
| 428 | VG_(baseBlock)[VGOFF_(sh_esi)] = VG_(threads)[tid].sh_esi; |
| 429 | VG_(baseBlock)[VGOFF_(sh_edi)] = VG_(threads)[tid].sh_edi; |
| 430 | VG_(baseBlock)[VGOFF_(sh_ebp)] = VG_(threads)[tid].sh_ebp; |
| 431 | VG_(baseBlock)[VGOFF_(sh_esp)] = VG_(threads)[tid].sh_esp; |
| 432 | VG_(baseBlock)[VGOFF_(sh_eflags)] = VG_(threads)[tid].sh_eflags; |
| 433 | } else { |
| 434 | /* Fields shouldn't be used -- check their values haven't changed. */ |
| 435 | /* Nb: they are written to by some macros like SET_EDX, but they |
| 436 | * should just write VG_UNUSED_SHADOW_REG_VALUE. */ |
| 437 | vg_assert( |
| 438 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_eax && |
| 439 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_ebx && |
| 440 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_ecx && |
| 441 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_edx && |
| 442 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_esi && |
| 443 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_edi && |
| 444 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_ebp && |
| 445 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_esp && |
| 446 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_eflags); |
| 447 | } |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 448 | |
| 449 | vg_tid_currently_in_baseBlock = tid; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | |
| 453 | /* Copy the state of a thread from VG_(baseBlock), presumably after it |
| 454 | has been descheduled. For sanity-check purposes, fill the vacated |
| 455 | VG_(baseBlock) with garbage so as to make the system more likely to |
| 456 | fail quickly if we erroneously continue to poke around inside |
| 457 | VG_(baseBlock) without first doing a load_thread_state(). |
| 458 | */ |
| 459 | __inline__ |
| 460 | void VG_(save_thread_state) ( ThreadId tid ) |
| 461 | { |
| 462 | Int i; |
| 463 | const UInt junk = 0xDEADBEEF; |
| 464 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 465 | vg_assert(vg_tid_currently_in_baseBlock != VG_INVALID_THREADID); |
| 466 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 467 | |
| 468 | /* We don't copy out the LDT entry, because it can never be changed |
| 469 | by the normal actions of the thread, only by the modify_ldt |
| 470 | syscall, in which case we will correctly be updating |
| 471 | VG_(threads)[tid].ldt. */ |
| 472 | vg_assert((void*)VG_(threads)[tid].ldt |
| 473 | == (void*)VG_(baseBlock)[VGOFF_(ldt)]); |
| 474 | |
| 475 | VG_(threads)[tid].m_cs = VG_(baseBlock)[VGOFF_(m_cs)]; |
| 476 | VG_(threads)[tid].m_ss = VG_(baseBlock)[VGOFF_(m_ss)]; |
| 477 | VG_(threads)[tid].m_ds = VG_(baseBlock)[VGOFF_(m_ds)]; |
| 478 | VG_(threads)[tid].m_es = VG_(baseBlock)[VGOFF_(m_es)]; |
| 479 | VG_(threads)[tid].m_fs = VG_(baseBlock)[VGOFF_(m_fs)]; |
| 480 | VG_(threads)[tid].m_gs = VG_(baseBlock)[VGOFF_(m_gs)]; |
| 481 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 482 | VG_(threads)[tid].m_eax = VG_(baseBlock)[VGOFF_(m_eax)]; |
| 483 | VG_(threads)[tid].m_ebx = VG_(baseBlock)[VGOFF_(m_ebx)]; |
| 484 | VG_(threads)[tid].m_ecx = VG_(baseBlock)[VGOFF_(m_ecx)]; |
| 485 | VG_(threads)[tid].m_edx = VG_(baseBlock)[VGOFF_(m_edx)]; |
| 486 | VG_(threads)[tid].m_esi = VG_(baseBlock)[VGOFF_(m_esi)]; |
| 487 | VG_(threads)[tid].m_edi = VG_(baseBlock)[VGOFF_(m_edi)]; |
| 488 | VG_(threads)[tid].m_ebp = VG_(baseBlock)[VGOFF_(m_ebp)]; |
| 489 | VG_(threads)[tid].m_esp = VG_(baseBlock)[VGOFF_(m_esp)]; |
| 490 | VG_(threads)[tid].m_eflags = VG_(baseBlock)[VGOFF_(m_eflags)]; |
| 491 | VG_(threads)[tid].m_eip = VG_(baseBlock)[VGOFF_(m_eip)]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 492 | |
| 493 | for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 494 | VG_(threads)[tid].m_fpu[i] = VG_(baseBlock)[VGOFF_(m_fpustate) + i]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 495 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 496 | if (VG_(needs).shadow_regs) { |
| 497 | VG_(threads)[tid].sh_eax = VG_(baseBlock)[VGOFF_(sh_eax)]; |
| 498 | VG_(threads)[tid].sh_ebx = VG_(baseBlock)[VGOFF_(sh_ebx)]; |
| 499 | VG_(threads)[tid].sh_ecx = VG_(baseBlock)[VGOFF_(sh_ecx)]; |
| 500 | VG_(threads)[tid].sh_edx = VG_(baseBlock)[VGOFF_(sh_edx)]; |
| 501 | VG_(threads)[tid].sh_esi = VG_(baseBlock)[VGOFF_(sh_esi)]; |
| 502 | VG_(threads)[tid].sh_edi = VG_(baseBlock)[VGOFF_(sh_edi)]; |
| 503 | VG_(threads)[tid].sh_ebp = VG_(baseBlock)[VGOFF_(sh_ebp)]; |
| 504 | VG_(threads)[tid].sh_esp = VG_(baseBlock)[VGOFF_(sh_esp)]; |
| 505 | VG_(threads)[tid].sh_eflags = VG_(baseBlock)[VGOFF_(sh_eflags)]; |
| 506 | } else { |
| 507 | /* Fill with recognisable junk */ |
| 508 | VG_(threads)[tid].sh_eax = |
| 509 | VG_(threads)[tid].sh_ebx = |
| 510 | VG_(threads)[tid].sh_ecx = |
| 511 | VG_(threads)[tid].sh_edx = |
| 512 | VG_(threads)[tid].sh_esi = |
| 513 | VG_(threads)[tid].sh_edi = |
| 514 | VG_(threads)[tid].sh_ebp = |
| 515 | VG_(threads)[tid].sh_esp = |
| 516 | VG_(threads)[tid].sh_eflags = VG_UNUSED_SHADOW_REG_VALUE; |
| 517 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 518 | |
| 519 | /* Fill it up with junk. */ |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 520 | VG_(baseBlock)[VGOFF_(ldt)] = junk; |
| 521 | VG_(baseBlock)[VGOFF_(m_cs)] = junk; |
| 522 | VG_(baseBlock)[VGOFF_(m_ss)] = junk; |
| 523 | VG_(baseBlock)[VGOFF_(m_ds)] = junk; |
| 524 | VG_(baseBlock)[VGOFF_(m_es)] = junk; |
| 525 | VG_(baseBlock)[VGOFF_(m_fs)] = junk; |
| 526 | VG_(baseBlock)[VGOFF_(m_gs)] = junk; |
| 527 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 528 | VG_(baseBlock)[VGOFF_(m_eax)] = junk; |
| 529 | VG_(baseBlock)[VGOFF_(m_ebx)] = junk; |
| 530 | VG_(baseBlock)[VGOFF_(m_ecx)] = junk; |
| 531 | VG_(baseBlock)[VGOFF_(m_edx)] = junk; |
| 532 | VG_(baseBlock)[VGOFF_(m_esi)] = junk; |
| 533 | VG_(baseBlock)[VGOFF_(m_edi)] = junk; |
| 534 | VG_(baseBlock)[VGOFF_(m_ebp)] = junk; |
| 535 | VG_(baseBlock)[VGOFF_(m_esp)] = junk; |
| 536 | VG_(baseBlock)[VGOFF_(m_eflags)] = junk; |
| 537 | VG_(baseBlock)[VGOFF_(m_eip)] = junk; |
| 538 | |
| 539 | for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++) |
| 540 | VG_(baseBlock)[VGOFF_(m_fpustate) + i] = junk; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 541 | |
| 542 | vg_tid_currently_in_baseBlock = VG_INVALID_THREADID; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | |
| 546 | /* Run the thread tid for a while, and return a VG_TRC_* value to the |
| 547 | scheduler indicating what happened. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 548 | static |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 549 | UInt run_thread_for_a_while ( ThreadId tid ) |
| 550 | { |
sewardj | 7ccc5c2 | 2002-04-24 21:39:11 +0000 | [diff] [blame] | 551 | volatile UInt trc = 0; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 552 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 553 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 554 | vg_assert(VG_(bbs_to_go) > 0); |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 555 | vg_assert(!VG_(scheduler_jmpbuf_valid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 556 | |
sewardj | 671ff54 | 2002-05-07 09:25:30 +0000 | [diff] [blame] | 557 | VGP_PUSHCC(VgpRun); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 558 | VG_(load_thread_state) ( tid ); |
| 559 | if (__builtin_setjmp(VG_(scheduler_jmpbuf)) == 0) { |
| 560 | /* try this ... */ |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 561 | VG_(scheduler_jmpbuf_valid) = True; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 562 | trc = VG_(run_innerloop)(); |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 563 | VG_(scheduler_jmpbuf_valid) = False; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 564 | /* We get here if the client didn't take a fault. */ |
| 565 | } else { |
| 566 | /* We get here if the client took a fault, which caused our |
| 567 | signal handler to longjmp. */ |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 568 | VG_(scheduler_jmpbuf_valid) = False; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 569 | vg_assert(trc == 0); |
| 570 | trc = VG_TRC_UNRESUMABLE_SIGNAL; |
| 571 | } |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 572 | |
| 573 | vg_assert(!VG_(scheduler_jmpbuf_valid)); |
| 574 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 575 | VG_(save_thread_state) ( tid ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 576 | VGP_POPCC(VgpRun); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 577 | return trc; |
| 578 | } |
| 579 | |
| 580 | |
| 581 | /* Increment the LRU epoch counter. */ |
| 582 | static |
| 583 | void increment_epoch ( void ) |
| 584 | { |
| 585 | VG_(current_epoch)++; |
| 586 | if (VG_(clo_verbosity) > 2) { |
| 587 | UInt tt_used, tc_used; |
| 588 | VG_(get_tt_tc_used) ( &tt_used, &tc_used ); |
| 589 | VG_(message)(Vg_UserMsg, |
| 590 | "%lu bbs, in: %d (%d -> %d), out %d (%d -> %d), TT %d, TC %d", |
| 591 | VG_(bbs_done), |
| 592 | VG_(this_epoch_in_count), |
| 593 | VG_(this_epoch_in_osize), |
| 594 | VG_(this_epoch_in_tsize), |
| 595 | VG_(this_epoch_out_count), |
| 596 | VG_(this_epoch_out_osize), |
| 597 | VG_(this_epoch_out_tsize), |
| 598 | tt_used, tc_used |
| 599 | ); |
| 600 | } |
| 601 | VG_(this_epoch_in_count) = 0; |
| 602 | VG_(this_epoch_in_osize) = 0; |
| 603 | VG_(this_epoch_in_tsize) = 0; |
| 604 | VG_(this_epoch_out_count) = 0; |
| 605 | VG_(this_epoch_out_osize) = 0; |
| 606 | VG_(this_epoch_out_tsize) = 0; |
| 607 | } |
| 608 | |
| 609 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 610 | static |
| 611 | void mostly_clear_thread_record ( ThreadId tid ) |
| 612 | { |
| 613 | Int j; |
| 614 | vg_assert(tid >= 0 && tid < VG_N_THREADS); |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 615 | VG_(threads)[tid].ldt = NULL; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 616 | VG_(threads)[tid].tid = tid; |
| 617 | VG_(threads)[tid].status = VgTs_Empty; |
| 618 | VG_(threads)[tid].associated_mx = NULL; |
| 619 | VG_(threads)[tid].associated_cv = NULL; |
| 620 | VG_(threads)[tid].awaken_at = 0; |
| 621 | VG_(threads)[tid].joinee_retval = NULL; |
| 622 | VG_(threads)[tid].joiner_thread_return = NULL; |
| 623 | VG_(threads)[tid].joiner_jee_tid = VG_INVALID_THREADID; |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 624 | VG_(threads)[tid].detached = False; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 625 | VG_(threads)[tid].cancel_st = True; /* PTHREAD_CANCEL_ENABLE */ |
| 626 | VG_(threads)[tid].cancel_ty = True; /* PTHREAD_CANCEL_DEFERRED */ |
| 627 | VG_(threads)[tid].cancel_pend = NULL; /* not pending */ |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 628 | VG_(threads)[tid].custack_used = 0; |
sewardj | 9a2224b | 2002-06-19 10:17:40 +0000 | [diff] [blame] | 629 | VG_(threads)[tid].n_signals_returned = 0; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 630 | VG_(ksigemptyset)(&VG_(threads)[tid].sig_mask); |
| 631 | VG_(ksigemptyset)(&VG_(threads)[tid].sigs_waited_for); |
| 632 | for (j = 0; j < VG_N_THREAD_KEYS; j++) |
| 633 | VG_(threads)[tid].specifics[j] = NULL; |
| 634 | } |
| 635 | |
| 636 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 637 | /* Initialise the scheduler. Create a single "main" thread ready to |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 638 | run, with special ThreadId of one. This is called at startup; the |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 639 | caller takes care to park the client's state is parked in |
| 640 | VG_(baseBlock). |
| 641 | */ |
| 642 | void VG_(scheduler_init) ( void ) |
| 643 | { |
| 644 | Int i; |
| 645 | Addr startup_esp; |
| 646 | ThreadId tid_main; |
| 647 | |
| 648 | startup_esp = VG_(baseBlock)[VGOFF_(m_esp)]; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 649 | |
| 650 | if (VG_STACK_MATCHES_BASE(startup_esp, VG_STARTUP_STACK_BASE_1) |
daywalker | a256220 | 2002-07-15 19:39:51 +0000 | [diff] [blame] | 651 | || VG_STACK_MATCHES_BASE(startup_esp, VG_STARTUP_STACK_BASE_2) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 652 | || VG_STACK_MATCHES_BASE(startup_esp, VG_STARTUP_STACK_BASE_3) |
| 653 | || VG_STACK_MATCHES_BASE(startup_esp, VG_STARTUP_STACK_BASE_4)) { |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 654 | /* Jolly good! */ |
| 655 | } else { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 656 | VG_(printf)( |
| 657 | "%%esp at startup = %p is not near %p, %p, %p or %p; aborting\n", |
| 658 | (void*)startup_esp, |
| 659 | (void*)VG_STARTUP_STACK_BASE_1, |
| 660 | (void*)VG_STARTUP_STACK_BASE_2, |
| 661 | (void*)VG_STARTUP_STACK_BASE_3, |
| 662 | (void*)VG_STARTUP_STACK_BASE_4 |
| 663 | ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 664 | VG_(panic)("unexpected %esp at startup"); |
| 665 | } |
| 666 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 667 | for (i = 0 /* NB; not 1 */; i < VG_N_THREADS; i++) { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 668 | mostly_clear_thread_record(i); |
| 669 | VG_(threads)[i].stack_size = 0; |
| 670 | VG_(threads)[i].stack_base = (Addr)NULL; |
| 671 | VG_(threads)[i].stack_highest_word = (Addr)NULL; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | for (i = 0; i < VG_N_WAITING_FDS; i++) |
| 675 | vg_waiting_fds[i].fd = -1; /* not in use */ |
| 676 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 677 | for (i = 0; i < VG_N_THREAD_KEYS; i++) { |
| 678 | vg_thread_keys[i].inuse = False; |
| 679 | vg_thread_keys[i].destructor = NULL; |
| 680 | } |
| 681 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 682 | vg_fhstack_used = 0; |
| 683 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 684 | /* Assert this is thread zero, which has certain magic |
| 685 | properties. */ |
| 686 | tid_main = vg_alloc_ThreadState(); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 687 | vg_assert(tid_main == 1); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 688 | VG_(threads)[tid_main].status = VgTs_Runnable; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 689 | |
| 690 | /* Copy VG_(baseBlock) state to tid_main's slot. */ |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 691 | vg_tid_currently_in_baseBlock = tid_main; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 692 | VG_(save_thread_state) ( tid_main ); |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 693 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 694 | VG_(threads)[tid_main].stack_highest_word |
| 695 | = VG_(threads)[tid_main].m_esp /* -4 ??? */; |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 696 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 697 | /* So now ... */ |
| 698 | vg_assert(vg_tid_currently_in_baseBlock == VG_INVALID_THREADID); |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 699 | |
| 700 | /* Not running client code right now. */ |
| 701 | VG_(scheduler_jmpbuf_valid) = False; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | |
| 705 | /* What if fd isn't a valid fd? */ |
| 706 | static |
| 707 | void set_fd_nonblocking ( Int fd ) |
| 708 | { |
| 709 | Int res = VG_(fcntl)( fd, VKI_F_GETFL, 0 ); |
| 710 | vg_assert(!VG_(is_kerror)(res)); |
| 711 | res |= VKI_O_NONBLOCK; |
| 712 | res = VG_(fcntl)( fd, VKI_F_SETFL, res ); |
| 713 | vg_assert(!VG_(is_kerror)(res)); |
| 714 | } |
| 715 | |
| 716 | static |
| 717 | void set_fd_blocking ( Int fd ) |
| 718 | { |
| 719 | Int res = VG_(fcntl)( fd, VKI_F_GETFL, 0 ); |
| 720 | vg_assert(!VG_(is_kerror)(res)); |
| 721 | res &= ~VKI_O_NONBLOCK; |
| 722 | res = VG_(fcntl)( fd, VKI_F_SETFL, res ); |
| 723 | vg_assert(!VG_(is_kerror)(res)); |
| 724 | } |
| 725 | |
| 726 | static |
| 727 | Bool fd_is_blockful ( Int fd ) |
| 728 | { |
| 729 | Int res = VG_(fcntl)( fd, VKI_F_GETFL, 0 ); |
| 730 | vg_assert(!VG_(is_kerror)(res)); |
| 731 | return (res & VKI_O_NONBLOCK) ? False : True; |
| 732 | } |
| 733 | |
sewardj | 3947e62 | 2002-05-23 16:52:11 +0000 | [diff] [blame] | 734 | static |
| 735 | Bool fd_is_valid ( Int fd ) |
| 736 | { |
| 737 | Int res = VG_(fcntl)( fd, VKI_F_GETFL, 0 ); |
| 738 | return VG_(is_kerror)(res) ? False : True; |
| 739 | } |
| 740 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 741 | |
| 742 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 743 | /* vthread tid is returning from a signal handler; modify its |
| 744 | stack/regs accordingly. */ |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 745 | |
| 746 | /* [Helper fn for handle_signal_return] tid, assumed to be in WaitFD |
| 747 | for read or write, has been interrupted by a signal. Find and |
| 748 | clear the relevant vg_waiting_fd[] entry. Most of the code in this |
| 749 | procedure is total paranoia, if you look closely. */ |
| 750 | static |
| 751 | void cleanup_waiting_fd_table ( ThreadId tid ) |
| 752 | { |
| 753 | Int i, waiters; |
| 754 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 755 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 756 | vg_assert(VG_(threads)[tid].status == VgTs_WaitFD); |
| 757 | vg_assert(VG_(threads)[tid].m_eax == __NR_read |
| 758 | || VG_(threads)[tid].m_eax == __NR_write); |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 759 | |
| 760 | /* Excessively paranoidly ... find the fd this op was waiting |
| 761 | for, and mark it as not being waited on. */ |
| 762 | waiters = 0; |
| 763 | for (i = 0; i < VG_N_WAITING_FDS; i++) { |
| 764 | if (vg_waiting_fds[i].tid == tid) { |
| 765 | waiters++; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 766 | vg_assert(vg_waiting_fds[i].syscall_no == VG_(threads)[tid].m_eax); |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | vg_assert(waiters == 1); |
| 770 | for (i = 0; i < VG_N_WAITING_FDS; i++) |
| 771 | if (vg_waiting_fds[i].tid == tid) |
| 772 | break; |
| 773 | vg_assert(i < VG_N_WAITING_FDS); |
| 774 | vg_assert(vg_waiting_fds[i].fd != -1); |
| 775 | vg_waiting_fds[i].fd = -1; /* not in use */ |
| 776 | } |
| 777 | |
| 778 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 779 | static |
| 780 | void handle_signal_return ( ThreadId tid ) |
| 781 | { |
| 782 | Char msg_buf[100]; |
| 783 | Bool restart_blocked_syscalls; |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 784 | struct vki_timespec * rem; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 785 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 786 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 787 | |
sewardj | 9a2224b | 2002-06-19 10:17:40 +0000 | [diff] [blame] | 788 | /* Increment signal-returned counter. Used only to implement |
| 789 | pause(). */ |
| 790 | VG_(threads)[tid].n_signals_returned++; |
| 791 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 792 | restart_blocked_syscalls = VG_(signal_returns)(tid); |
| 793 | |
| 794 | if (restart_blocked_syscalls) |
| 795 | /* Easy; we don't have to do anything. */ |
| 796 | return; |
| 797 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 798 | if (VG_(threads)[tid].status == VgTs_WaitFD |
| 799 | && (VG_(threads)[tid].m_eax == __NR_read |
| 800 | || VG_(threads)[tid].m_eax == __NR_write)) { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 801 | /* read() or write() interrupted. Force a return with EINTR. */ |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 802 | cleanup_waiting_fd_table(tid); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 803 | VG_(threads)[tid].m_eax = -VKI_EINTR; |
| 804 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 805 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 806 | if (VG_(clo_trace_sched)) { |
| 807 | VG_(sprintf)(msg_buf, |
| 808 | "read() / write() interrupted by signal; return EINTR" ); |
| 809 | print_sched_event(tid, msg_buf); |
| 810 | } |
| 811 | return; |
| 812 | } |
| 813 | |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 814 | if (VG_(threads)[tid].status == VgTs_Sleeping |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 815 | && VG_(threads)[tid].m_eax == __NR_nanosleep) { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 816 | /* We interrupted a nanosleep(). The right thing to do is to |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 817 | write the unused time to nanosleep's second param, but that's |
| 818 | too much effort ... we just say that 1 nanosecond was not |
| 819 | used, and return EINTR. */ |
| 820 | rem = (struct vki_timespec *)VG_(threads)[tid].m_ecx; /* arg2 */ |
| 821 | if (rem != NULL) { |
| 822 | rem->tv_sec = 0; |
| 823 | rem->tv_nsec = 1; |
| 824 | } |
| 825 | SET_EAX(tid, -VKI_EINTR); |
| 826 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 827 | return; |
| 828 | } |
| 829 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 830 | if (VG_(threads)[tid].status == VgTs_WaitFD) { |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 831 | VG_(panic)("handle_signal_return: unknown interrupted syscall"); |
| 832 | } |
| 833 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 834 | /* All other cases? Just return. */ |
| 835 | } |
| 836 | |
| 837 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 838 | static |
| 839 | void sched_do_syscall ( ThreadId tid ) |
| 840 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 841 | UInt saved_eax; |
| 842 | UInt res, syscall_no; |
| 843 | UInt fd; |
| 844 | void* pre_res; |
| 845 | Bool orig_fd_blockness; |
| 846 | Char msg_buf[100]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 847 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 848 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 849 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 850 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 851 | syscall_no = VG_(threads)[tid].m_eax; /* syscall number */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 852 | |
| 853 | if (syscall_no == __NR_nanosleep) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 854 | UInt t_now, t_awaken; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 855 | struct vki_timespec* req; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 856 | req = (struct vki_timespec*)VG_(threads)[tid].m_ebx; /* arg1 */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 857 | t_now = VG_(read_millisecond_timer)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 858 | t_awaken |
| 859 | = t_now |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 860 | + (UInt)1000ULL * (UInt)(req->tv_sec) |
| 861 | + (UInt)(req->tv_nsec) / 1000000; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 862 | VG_(threads)[tid].status = VgTs_Sleeping; |
| 863 | VG_(threads)[tid].awaken_at = t_awaken; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 864 | if (VG_(clo_trace_sched)) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 865 | VG_(sprintf)(msg_buf, "at %d: nanosleep for %d", |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 866 | t_now, t_awaken-t_now); |
| 867 | print_sched_event(tid, msg_buf); |
| 868 | } |
| 869 | /* Force the scheduler to run something else for a while. */ |
| 870 | return; |
| 871 | } |
| 872 | |
sewardj | aec22c0 | 2002-04-29 01:58:08 +0000 | [diff] [blame] | 873 | if (syscall_no != __NR_read && syscall_no != __NR_write) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 874 | /* We think it's non-blocking. Just do it in the normal way. */ |
| 875 | VG_(perform_assumed_nonblocking_syscall)(tid); |
| 876 | /* The thread is still runnable. */ |
| 877 | return; |
| 878 | } |
| 879 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 880 | /* Set the fd to nonblocking, and do the syscall, which will return |
| 881 | immediately, in order to lodge a request with the Linux kernel. |
| 882 | We later poll for I/O completion using select(). */ |
| 883 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 884 | fd = VG_(threads)[tid].m_ebx /* arg1 */; |
sewardj | 3947e62 | 2002-05-23 16:52:11 +0000 | [diff] [blame] | 885 | |
| 886 | /* Deal with error case immediately. */ |
| 887 | if (!fd_is_valid(fd)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 888 | if (VG_(needs).core_errors) |
| 889 | VG_(message)(Vg_UserMsg, |
| 890 | "Warning: invalid file descriptor %d in syscall %s", |
| 891 | fd, syscall_no == __NR_read ? "read()" : "write()" ); |
| 892 | pre_res = VG_(pre_known_blocking_syscall)(tid, syscall_no); |
sewardj | 3947e62 | 2002-05-23 16:52:11 +0000 | [diff] [blame] | 893 | KERNEL_DO_SYSCALL(tid, res); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 894 | VG_(post_known_blocking_syscall)(tid, syscall_no, pre_res, res); |
sewardj | 3947e62 | 2002-05-23 16:52:11 +0000 | [diff] [blame] | 895 | /* We're still runnable. */ |
| 896 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | /* From here onwards we know that fd is valid. */ |
| 901 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 902 | orig_fd_blockness = fd_is_blockful(fd); |
| 903 | set_fd_nonblocking(fd); |
| 904 | vg_assert(!fd_is_blockful(fd)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 905 | pre_res = VG_(pre_known_blocking_syscall)(tid, syscall_no); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 906 | |
| 907 | /* This trashes the thread's %eax; we have to preserve it. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 908 | saved_eax = VG_(threads)[tid].m_eax; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 909 | KERNEL_DO_SYSCALL(tid,res); |
| 910 | |
| 911 | /* Restore original blockfulness of the fd. */ |
| 912 | if (orig_fd_blockness) |
| 913 | set_fd_blocking(fd); |
| 914 | else |
| 915 | set_fd_nonblocking(fd); |
| 916 | |
sewardj | aec22c0 | 2002-04-29 01:58:08 +0000 | [diff] [blame] | 917 | if (res != -VKI_EWOULDBLOCK || !orig_fd_blockness) { |
| 918 | /* Finish off in the normal way. Don't restore %EAX, since that |
| 919 | now (correctly) holds the result of the call. We get here if either: |
| 920 | 1. The call didn't block, or |
| 921 | 2. The fd was already in nonblocking mode before we started to |
| 922 | mess with it. In this case, we're not expecting to handle |
| 923 | the I/O completion -- the client is. So don't file a |
| 924 | completion-wait entry. |
| 925 | */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 926 | VG_(post_known_blocking_syscall)(tid, syscall_no, pre_res, res); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 927 | /* We're still runnable. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 928 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 929 | |
| 930 | } else { |
| 931 | |
sewardj | aec22c0 | 2002-04-29 01:58:08 +0000 | [diff] [blame] | 932 | vg_assert(res == -VKI_EWOULDBLOCK && orig_fd_blockness); |
| 933 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 934 | /* It would have blocked. First, restore %EAX to what it was |
| 935 | before our speculative call. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 936 | VG_(threads)[tid].m_eax = saved_eax; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 937 | /* Put this fd in a table of fds on which we are waiting for |
| 938 | completion. The arguments for select() later are constructed |
| 939 | from this table. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 940 | add_waiting_fd(tid, fd, saved_eax /* which holds the syscall # */, |
| 941 | pre_res); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 942 | /* Deschedule thread until an I/O completion happens. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 943 | VG_(threads)[tid].status = VgTs_WaitFD; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 944 | if (VG_(clo_trace_sched)) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 945 | VG_(sprintf)(msg_buf,"block until I/O ready on fd %d", fd); |
| 946 | print_sched_event(tid, msg_buf); |
| 947 | } |
| 948 | |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | |
| 953 | /* Find out which of the fds in vg_waiting_fds are now ready to go, by |
| 954 | making enquiries with select(), and mark them as ready. We have to |
| 955 | wait for the requesting threads to fall into the the WaitFD state |
| 956 | before we can actually finally deliver the results, so this |
| 957 | procedure doesn't do that; complete_blocked_syscalls() does it. |
| 958 | |
| 959 | It might seem odd that a thread which has done a blocking syscall |
| 960 | is not in WaitFD state; the way this can happen is if it initially |
| 961 | becomes WaitFD, but then a signal is delivered to it, so it becomes |
| 962 | Runnable for a while. In this case we have to wait for the |
| 963 | sighandler to return, whereupon the WaitFD state is resumed, and |
| 964 | only at that point can the I/O result be delivered to it. However, |
| 965 | this point may be long after the fd is actually ready. |
| 966 | |
| 967 | So, poll_for_ready_fds() merely detects fds which are ready. |
| 968 | complete_blocked_syscalls() does the second half of the trick, |
| 969 | possibly much later: it delivers the results from ready fds to |
| 970 | threads in WaitFD state. |
| 971 | */ |
sewardj | 9a199dc | 2002-04-14 13:01:38 +0000 | [diff] [blame] | 972 | static |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 973 | void poll_for_ready_fds ( void ) |
| 974 | { |
| 975 | vki_ksigset_t saved_procmask; |
| 976 | vki_fd_set readfds; |
| 977 | vki_fd_set writefds; |
| 978 | vki_fd_set exceptfds; |
| 979 | struct vki_timeval timeout; |
| 980 | Int fd, fd_max, i, n_ready, syscall_no, n_ok; |
| 981 | ThreadId tid; |
| 982 | Bool rd_ok, wr_ok, ex_ok; |
| 983 | Char msg_buf[100]; |
| 984 | |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 985 | struct vki_timespec* rem; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 986 | UInt t_now; |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 987 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 988 | /* Awaken any sleeping threads whose sleep has expired. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 989 | for (tid = 1; tid < VG_N_THREADS; tid++) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 990 | if (VG_(threads)[tid].status == VgTs_Sleeping) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 991 | break; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 992 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 993 | /* Avoid pointless calls to VG_(read_millisecond_timer). */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 994 | if (tid < VG_N_THREADS) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 995 | t_now = VG_(read_millisecond_timer)(); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 996 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 997 | if (VG_(threads)[tid].status != VgTs_Sleeping) |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 998 | continue; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 999 | if (t_now >= VG_(threads)[tid].awaken_at) { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1000 | /* Resume this thread. Set to zero the remaining-time |
| 1001 | (second) arg of nanosleep, since it's used up all its |
| 1002 | time. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1003 | vg_assert(VG_(threads)[tid].m_eax == __NR_nanosleep); |
| 1004 | rem = (struct vki_timespec *)VG_(threads)[tid].m_ecx; /* arg2 */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1005 | if (rem != NULL) { |
| 1006 | rem->tv_sec = 0; |
| 1007 | rem->tv_nsec = 0; |
| 1008 | } |
| 1009 | /* Make the syscall return 0 (success). */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1010 | VG_(threads)[tid].m_eax = 0; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1011 | /* Reschedule this thread. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1012 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1013 | if (VG_(clo_trace_sched)) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1014 | VG_(sprintf)(msg_buf, "at %d: nanosleep done", |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1015 | t_now); |
| 1016 | print_sched_event(tid, msg_buf); |
| 1017 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1021 | |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 1022 | /* And look for threads waiting on file descriptors which are now |
| 1023 | ready for I/O.*/ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1024 | timeout.tv_sec = 0; |
| 1025 | timeout.tv_usec = 0; |
| 1026 | |
| 1027 | VKI_FD_ZERO(&readfds); |
| 1028 | VKI_FD_ZERO(&writefds); |
| 1029 | VKI_FD_ZERO(&exceptfds); |
| 1030 | fd_max = -1; |
| 1031 | for (i = 0; i < VG_N_WAITING_FDS; i++) { |
| 1032 | if (vg_waiting_fds[i].fd == -1 /* not in use */) |
| 1033 | continue; |
| 1034 | if (vg_waiting_fds[i].ready /* already ready? */) |
| 1035 | continue; |
| 1036 | fd = vg_waiting_fds[i].fd; |
| 1037 | /* VG_(printf)("adding QUERY for fd %d\n", fd); */ |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 1038 | vg_assert(fd >= 0); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1039 | if (fd > fd_max) |
| 1040 | fd_max = fd; |
| 1041 | tid = vg_waiting_fds[i].tid; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1042 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1043 | syscall_no = vg_waiting_fds[i].syscall_no; |
| 1044 | switch (syscall_no) { |
sewardj | 3984b85 | 2002-05-12 03:00:17 +0000 | [diff] [blame] | 1045 | case __NR_read: |
| 1046 | /* In order to catch timeout events on fds which are |
| 1047 | readable and which have been ioctl(TCSETA)'d with a |
| 1048 | VTIMEout, we appear to need to ask if the fd is |
| 1049 | writable, for some reason. Ask me not why. Since this |
| 1050 | is strange and potentially troublesome we only do it if |
| 1051 | the user asks specially. */ |
sewardj | 8d365b5 | 2002-05-12 10:52:16 +0000 | [diff] [blame] | 1052 | if (VG_(strstr)(VG_(clo_weird_hacks), "ioctl-VTIME") != NULL) |
sewardj | 3984b85 | 2002-05-12 03:00:17 +0000 | [diff] [blame] | 1053 | VKI_FD_SET(fd, &writefds); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1054 | VKI_FD_SET(fd, &readfds); break; |
| 1055 | case __NR_write: |
| 1056 | VKI_FD_SET(fd, &writefds); break; |
| 1057 | default: |
| 1058 | VG_(panic)("poll_for_ready_fds: unexpected syscall"); |
| 1059 | /*NOTREACHED*/ |
| 1060 | break; |
| 1061 | } |
| 1062 | } |
| 1063 | |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 1064 | /* Short cut: if no fds are waiting, give up now. */ |
| 1065 | if (fd_max == -1) |
| 1066 | return; |
| 1067 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1068 | /* BLOCK ALL SIGNALS. We don't want the complication of select() |
| 1069 | getting interrupted. */ |
| 1070 | VG_(block_all_host_signals)( &saved_procmask ); |
| 1071 | |
| 1072 | n_ready = VG_(select) |
| 1073 | ( fd_max+1, &readfds, &writefds, &exceptfds, &timeout); |
| 1074 | if (VG_(is_kerror)(n_ready)) { |
| 1075 | VG_(printf)("poll_for_ready_fds: select returned %d\n", n_ready); |
| 1076 | VG_(panic)("poll_for_ready_fds: select failed?!"); |
| 1077 | /*NOTREACHED*/ |
| 1078 | } |
| 1079 | |
| 1080 | /* UNBLOCK ALL SIGNALS */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1081 | VG_(restore_all_host_signals)( &saved_procmask ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1082 | |
| 1083 | /* VG_(printf)("poll_for_io_completions: %d fs ready\n", n_ready); */ |
| 1084 | |
| 1085 | if (n_ready == 0) |
| 1086 | return; |
| 1087 | |
| 1088 | /* Inspect all the fds we know about, and handle any completions that |
| 1089 | have happened. */ |
| 1090 | /* |
| 1091 | VG_(printf)("\n\n"); |
| 1092 | for (fd = 0; fd < 100; fd++) |
| 1093 | if (VKI_FD_ISSET(fd, &writefds) || VKI_FD_ISSET(fd, &readfds)) { |
| 1094 | VG_(printf)("X"); } else { VG_(printf)("."); }; |
| 1095 | VG_(printf)("\n\nfd_max = %d\n", fd_max); |
| 1096 | */ |
| 1097 | |
| 1098 | for (fd = 0; fd <= fd_max; fd++) { |
| 1099 | rd_ok = VKI_FD_ISSET(fd, &readfds); |
| 1100 | wr_ok = VKI_FD_ISSET(fd, &writefds); |
| 1101 | ex_ok = VKI_FD_ISSET(fd, &exceptfds); |
| 1102 | |
| 1103 | n_ok = (rd_ok ? 1 : 0) + (wr_ok ? 1 : 0) + (ex_ok ? 1 : 0); |
| 1104 | if (n_ok == 0) |
| 1105 | continue; |
| 1106 | if (n_ok > 1) { |
| 1107 | VG_(printf)("offending fd = %d\n", fd); |
| 1108 | VG_(panic)("poll_for_ready_fds: multiple events on fd"); |
| 1109 | } |
sewardj | bc7d878 | 2002-06-30 12:44:54 +0000 | [diff] [blame] | 1110 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1111 | /* An I/O event completed for fd. Find the thread which |
| 1112 | requested this. */ |
| 1113 | for (i = 0; i < VG_N_WAITING_FDS; i++) { |
| 1114 | if (vg_waiting_fds[i].fd == -1 /* not in use */) |
| 1115 | continue; |
| 1116 | if (vg_waiting_fds[i].fd == fd) |
| 1117 | break; |
| 1118 | } |
| 1119 | |
| 1120 | /* And a bit more paranoia ... */ |
| 1121 | vg_assert(i >= 0 && i < VG_N_WAITING_FDS); |
| 1122 | |
| 1123 | /* Mark the fd as ready. */ |
| 1124 | vg_assert(! vg_waiting_fds[i].ready); |
| 1125 | vg_waiting_fds[i].ready = True; |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | /* See comment attached to poll_for_ready_fds() for explaination. */ |
sewardj | 9a199dc | 2002-04-14 13:01:38 +0000 | [diff] [blame] | 1131 | static |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1132 | void complete_blocked_syscalls ( void ) |
| 1133 | { |
| 1134 | Int fd, i, res, syscall_no; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1135 | void* pre_res; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1136 | ThreadId tid; |
| 1137 | Char msg_buf[100]; |
| 1138 | |
| 1139 | /* Inspect all the outstanding fds we know about. */ |
| 1140 | |
| 1141 | for (i = 0; i < VG_N_WAITING_FDS; i++) { |
| 1142 | if (vg_waiting_fds[i].fd == -1 /* not in use */) |
| 1143 | continue; |
| 1144 | if (! vg_waiting_fds[i].ready) |
| 1145 | continue; |
| 1146 | |
| 1147 | fd = vg_waiting_fds[i].fd; |
| 1148 | tid = vg_waiting_fds[i].tid; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1149 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1150 | |
| 1151 | /* The thread actually has to be waiting for the I/O event it |
| 1152 | requested before we can deliver the result! */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1153 | if (VG_(threads)[tid].status != VgTs_WaitFD) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1154 | continue; |
| 1155 | |
| 1156 | /* Ok, actually do it! We can safely use %EAX as the syscall |
| 1157 | number, because the speculative call made by |
| 1158 | sched_do_syscall() doesn't change %EAX in the case where the |
| 1159 | call would have blocked. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1160 | syscall_no = vg_waiting_fds[i].syscall_no; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1161 | vg_assert(syscall_no == VG_(threads)[tid].m_eax); |
sewardj | bc7d878 | 2002-06-30 12:44:54 +0000 | [diff] [blame] | 1162 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1163 | pre_res = vg_waiting_fds[i].pre_result; |
| 1164 | |
sewardj | bc7d878 | 2002-06-30 12:44:54 +0000 | [diff] [blame] | 1165 | /* In a rare case pertaining to writing into a pipe, write() |
| 1166 | will block when asked to write > 4096 bytes even though the |
| 1167 | kernel claims, when asked via select(), that blocking will |
| 1168 | not occur for a write on that fd. This can cause deadlocks. |
| 1169 | An easy answer is to limit the size of the write to 4096 |
| 1170 | anyway and hope that the client program's logic can handle |
| 1171 | the short write. That shoulds dubious to me, so we don't do |
| 1172 | it by default. */ |
| 1173 | if (syscall_no == __NR_write |
| 1174 | && VG_(threads)[tid].m_edx /* arg3, count */ > 4096 |
| 1175 | && VG_(strstr)(VG_(clo_weird_hacks), "truncate-writes") != NULL) { |
| 1176 | /* VG_(printf)("truncate write from %d to 4096\n", |
| 1177 | VG_(threads)[tid].m_edx ); */ |
| 1178 | VG_(threads)[tid].m_edx = 4096; |
| 1179 | } |
| 1180 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1181 | KERNEL_DO_SYSCALL(tid,res); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1182 | VG_(post_known_blocking_syscall)(tid, syscall_no, pre_res, res); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1183 | |
| 1184 | /* Reschedule. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1185 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1186 | /* Mark slot as no longer in use. */ |
| 1187 | vg_waiting_fds[i].fd = -1; |
| 1188 | /* pp_sched_status(); */ |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 1189 | if (VG_(clo_trace_sched)) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1190 | VG_(sprintf)(msg_buf,"resume due to I/O completion on fd %d", fd); |
| 1191 | print_sched_event(tid, msg_buf); |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | |
| 1197 | static |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1198 | void check_for_pthread_cond_timedwait ( void ) |
| 1199 | { |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1200 | Int i, now; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1201 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1202 | if (VG_(threads)[i].status != VgTs_WaitCV) |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1203 | continue; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1204 | if (VG_(threads)[i].awaken_at == 0xFFFFFFFF /* no timeout */) |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1205 | continue; |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1206 | now = VG_(read_millisecond_timer)(); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1207 | if (now >= VG_(threads)[i].awaken_at) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1208 | do_pthread_cond_timedwait_TIMEOUT(i); |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1209 | } |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | static |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1215 | void nanosleep_for_a_while ( void ) |
| 1216 | { |
| 1217 | Int res; |
| 1218 | struct vki_timespec req; |
| 1219 | struct vki_timespec rem; |
| 1220 | req.tv_sec = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1221 | req.tv_nsec = 10 * 1000 * 1000; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1222 | res = VG_(nanosleep)( &req, &rem ); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1223 | vg_assert(res == 0 /* ok */ || res == 1 /* interrupted by signal */); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | /* --------------------------------------------------------------------- |
| 1228 | The scheduler proper. |
| 1229 | ------------------------------------------------------------------ */ |
| 1230 | |
| 1231 | /* Run user-space threads until either |
| 1232 | * Deadlock occurs |
| 1233 | * One thread asks to shutdown Valgrind |
| 1234 | * The specified number of basic blocks has gone by. |
| 1235 | */ |
| 1236 | VgSchedReturnCode VG_(scheduler) ( void ) |
| 1237 | { |
| 1238 | ThreadId tid, tid_next; |
| 1239 | UInt trc; |
| 1240 | UInt dispatch_ctr_SAVED; |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1241 | Int done_this_time, n_in_bounded_wait; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1242 | Addr trans_addr; |
sewardj | 14e0342 | 2002-04-24 19:51:31 +0000 | [diff] [blame] | 1243 | Bool sigs_delivered; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1244 | |
| 1245 | /* For the LRU structures, records when the epoch began. */ |
| 1246 | ULong lru_epoch_started_at = 0; |
| 1247 | |
| 1248 | /* Start with the root thread. tid in general indicates the |
| 1249 | currently runnable/just-finished-running thread. */ |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1250 | VG_(last_run_tid) = tid = 1; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1251 | |
| 1252 | /* This is the top level scheduler loop. It falls into three |
| 1253 | phases. */ |
| 1254 | while (True) { |
| 1255 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1256 | /* ======================= Phase 0 of 3 ======================= |
| 1257 | Be paranoid. Always a good idea. */ |
sewardj | d7fd4d2 | 2002-04-24 01:57:27 +0000 | [diff] [blame] | 1258 | stage1: |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1259 | scheduler_sanity(); |
sewardj | 0c3b53f | 2002-05-01 01:58:35 +0000 | [diff] [blame] | 1260 | VG_(do_sanity_checks)( False ); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1261 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1262 | /* ======================= Phase 1 of 3 ======================= |
| 1263 | Handle I/O completions and signals. This may change the |
| 1264 | status of various threads. Then select a new thread to run, |
| 1265 | or declare deadlock, or sleep if there are no runnable |
| 1266 | threads but some are blocked on I/O. */ |
| 1267 | |
| 1268 | /* Age the LRU structures if an epoch has been completed. */ |
| 1269 | if (VG_(bbs_done) - lru_epoch_started_at >= VG_BBS_PER_EPOCH) { |
| 1270 | lru_epoch_started_at = VG_(bbs_done); |
| 1271 | increment_epoch(); |
| 1272 | } |
| 1273 | |
| 1274 | /* Was a debug-stop requested? */ |
| 1275 | if (VG_(bbs_to_go) == 0) |
| 1276 | goto debug_stop; |
| 1277 | |
| 1278 | /* Do the following loop until a runnable thread is found, or |
| 1279 | deadlock is detected. */ |
| 1280 | while (True) { |
| 1281 | |
| 1282 | /* For stats purposes only. */ |
| 1283 | VG_(num_scheduling_events_MAJOR) ++; |
| 1284 | |
| 1285 | /* See if any I/O operations which we were waiting for have |
| 1286 | completed, and, if so, make runnable the relevant waiting |
| 1287 | threads. */ |
| 1288 | poll_for_ready_fds(); |
| 1289 | complete_blocked_syscalls(); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1290 | check_for_pthread_cond_timedwait(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1291 | |
| 1292 | /* See if there are any signals which need to be delivered. If |
| 1293 | so, choose thread(s) to deliver them to, and build signal |
| 1294 | delivery frames on those thread(s) stacks. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1295 | |
| 1296 | /* Be careful about delivering signals to a thread waiting |
| 1297 | for a mutex. In particular, when the handler is running, |
| 1298 | that thread is temporarily apparently-not-waiting for the |
| 1299 | mutex, so if it is unlocked by another thread whilst the |
| 1300 | handler is running, this thread is not informed. When the |
| 1301 | handler returns, the thread resumes waiting on the mutex, |
| 1302 | even if, as a result, it has missed the unlocking of it. |
| 1303 | Potential deadlock. This sounds all very strange, but the |
| 1304 | POSIX standard appears to require this behaviour. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1305 | sigs_delivered = VG_(deliver_signals)(); |
sewardj | 14e0342 | 2002-04-24 19:51:31 +0000 | [diff] [blame] | 1306 | if (sigs_delivered) |
sewardj | 0c3b53f | 2002-05-01 01:58:35 +0000 | [diff] [blame] | 1307 | VG_(do_sanity_checks)( False ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1308 | |
| 1309 | /* Try and find a thread (tid) to run. */ |
| 1310 | tid_next = tid; |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1311 | n_in_bounded_wait = 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1312 | while (True) { |
| 1313 | tid_next++; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1314 | if (tid_next >= VG_N_THREADS) tid_next = 1; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1315 | if (VG_(threads)[tid_next].status == VgTs_WaitFD |
| 1316 | || VG_(threads)[tid_next].status == VgTs_Sleeping |
| 1317 | || VG_(threads)[tid_next].status == VgTs_WaitSIG |
| 1318 | || (VG_(threads)[tid_next].status == VgTs_WaitCV |
| 1319 | && VG_(threads)[tid_next].awaken_at != 0xFFFFFFFF)) |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1320 | n_in_bounded_wait ++; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1321 | if (VG_(threads)[tid_next].status == VgTs_Runnable) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1322 | break; /* We can run this one. */ |
| 1323 | if (tid_next == tid) |
| 1324 | break; /* been all the way round */ |
| 1325 | } |
| 1326 | tid = tid_next; |
| 1327 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1328 | if (VG_(threads)[tid].status == VgTs_Runnable) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1329 | /* Found a suitable candidate. Fall out of this loop, so |
| 1330 | we can advance to stage 2 of the scheduler: actually |
| 1331 | running the thread. */ |
| 1332 | break; |
| 1333 | } |
| 1334 | |
| 1335 | /* We didn't find a runnable thread. Now what? */ |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1336 | if (n_in_bounded_wait == 0) { |
sewardj | 54cacf0 | 2002-04-12 23:24:59 +0000 | [diff] [blame] | 1337 | /* No runnable threads and no prospect of any appearing |
| 1338 | even if we wait for an arbitrary length of time. In |
| 1339 | short, we have a deadlock. */ |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 1340 | VG_(pp_sched_status)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1341 | return VgSrc_Deadlock; |
| 1342 | } |
| 1343 | |
| 1344 | /* At least one thread is in a fd-wait state. Delay for a |
| 1345 | while, and go round again, in the hope that eventually a |
| 1346 | thread becomes runnable. */ |
| 1347 | nanosleep_for_a_while(); |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1348 | /* pp_sched_status(); */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1349 | /* VG_(printf)("."); */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | |
| 1353 | /* ======================= Phase 2 of 3 ======================= |
| 1354 | Wahey! We've finally decided that thread tid is runnable, so |
| 1355 | we now do that. Run it for as much of a quanta as possible. |
| 1356 | Trivial requests are handled and the thread continues. The |
| 1357 | aim is not to do too many of Phase 1 since it is expensive. */ |
| 1358 | |
| 1359 | if (0) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1360 | VG_(printf)("SCHED: tid %d\n", tid); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1361 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1362 | VG_TRACK( thread_run, tid ); |
| 1363 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1364 | /* Figure out how many bbs to ask vg_run_innerloop to do. Note |
| 1365 | that it decrements the counter before testing it for zero, so |
| 1366 | that if VG_(dispatch_ctr) is set to N you get at most N-1 |
| 1367 | iterations. Also this means that VG_(dispatch_ctr) must |
| 1368 | exceed zero before entering the innerloop. Also also, the |
| 1369 | decrement is done before the bb is actually run, so you |
| 1370 | always get at least one decrement even if nothing happens. |
| 1371 | */ |
| 1372 | if (VG_(bbs_to_go) >= VG_SCHEDULING_QUANTUM) |
| 1373 | VG_(dispatch_ctr) = VG_SCHEDULING_QUANTUM + 1; |
| 1374 | else |
| 1375 | VG_(dispatch_ctr) = (UInt)VG_(bbs_to_go) + 1; |
| 1376 | |
| 1377 | /* ... and remember what we asked for. */ |
| 1378 | dispatch_ctr_SAVED = VG_(dispatch_ctr); |
| 1379 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1380 | /* paranoia ... */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1381 | vg_assert(VG_(threads)[tid].tid == tid); |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1382 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1383 | /* Actually run thread tid. */ |
| 1384 | while (True) { |
| 1385 | |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1386 | VG_(last_run_tid) = tid; |
| 1387 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1388 | /* For stats purposes only. */ |
| 1389 | VG_(num_scheduling_events_MINOR) ++; |
| 1390 | |
| 1391 | if (0) |
| 1392 | VG_(message)(Vg_DebugMsg, "thread %d: running for %d bbs", |
| 1393 | tid, VG_(dispatch_ctr) - 1 ); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1394 | # if 0 |
| 1395 | if (VG_(bbs_done) > 31700000 + 0) { |
| 1396 | dispatch_ctr_SAVED = VG_(dispatch_ctr) = 2; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1397 | VG_(translate)(&VG_(threads)[tid], VG_(threads)[tid].m_eip, |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1398 | NULL,NULL,NULL); |
| 1399 | } |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1400 | vg_assert(VG_(threads)[tid].m_eip != 0); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1401 | # endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1402 | |
| 1403 | trc = run_thread_for_a_while ( tid ); |
| 1404 | |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1405 | # if 0 |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1406 | if (0 == VG_(threads)[tid].m_eip) { |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1407 | VG_(printf)("tid = %d, dc = %llu\n", tid, VG_(bbs_done)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1408 | vg_assert(0 != VG_(threads)[tid].m_eip); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1409 | } |
| 1410 | # endif |
| 1411 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1412 | /* Deal quickly with trivial scheduling events, and resume the |
| 1413 | thread. */ |
| 1414 | |
| 1415 | if (trc == VG_TRC_INNER_FASTMISS) { |
| 1416 | vg_assert(VG_(dispatch_ctr) > 0); |
| 1417 | |
| 1418 | /* Trivial event. Miss in the fast-cache. Do a full |
| 1419 | lookup for it. */ |
| 1420 | trans_addr |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1421 | = VG_(search_transtab) ( VG_(threads)[tid].m_eip ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1422 | if (trans_addr == (Addr)0) { |
| 1423 | /* Not found; we need to request a translation. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1424 | create_translation_for( |
| 1425 | tid, VG_(threads)[tid].m_eip ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1426 | trans_addr = VG_(search_transtab) ( VG_(threads)[tid].m_eip ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1427 | if (trans_addr == (Addr)0) |
| 1428 | VG_(panic)("VG_TRC_INNER_FASTMISS: missing tt_fast entry"); |
| 1429 | } |
| 1430 | continue; /* with this thread */ |
| 1431 | } |
| 1432 | |
| 1433 | if (trc == VG_TRC_EBP_JMP_CLIENTREQ) { |
sewardj | 18a62ff | 2002-07-12 22:30:51 +0000 | [diff] [blame] | 1434 | UInt reqno = *(UInt*)(VG_(threads)[tid].m_eax); |
| 1435 | /* VG_(printf)("request 0x%x\n", reqno); */ |
sewardj | 1fe7b00 | 2002-07-16 01:43:15 +0000 | [diff] [blame] | 1436 | |
| 1437 | /* Are we really absolutely totally quitting? */ |
| 1438 | if (reqno == VG_USERREQ__LIBC_FREERES_DONE) { |
| 1439 | if (0 || VG_(clo_trace_syscalls) || VG_(clo_trace_sched)) { |
| 1440 | VG_(message)(Vg_DebugMsg, |
| 1441 | "__libc_freeres() done; really quitting!"); |
| 1442 | } |
| 1443 | return VgSrc_ExitSyscall; |
| 1444 | } |
| 1445 | |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1446 | do_client_request(tid); |
| 1447 | /* Following the request, we try and continue with the |
| 1448 | same thread if still runnable. If not, go back to |
| 1449 | Stage 1 to select a new thread to run. */ |
sewardj | 18a62ff | 2002-07-12 22:30:51 +0000 | [diff] [blame] | 1450 | if (VG_(threads)[tid].status == VgTs_Runnable |
| 1451 | && reqno != VG_USERREQ__PTHREAD_YIELD) |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1452 | continue; /* with this thread */ |
| 1453 | else |
| 1454 | goto stage1; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1457 | if (trc == VG_TRC_EBP_JMP_SYSCALL) { |
| 1458 | /* Do a syscall for the vthread tid. This could cause it |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1459 | to become non-runnable. One special case: spot the |
| 1460 | client doing calls to exit() and take this as the cue |
| 1461 | to exit. */ |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1462 | # if 0 |
| 1463 | { UInt* esp; Int i; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1464 | esp=(UInt*)VG_(threads)[tid].m_esp; |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1465 | VG_(printf)("\nBEFORE\n"); |
| 1466 | for (i = 10; i >= -10; i--) |
| 1467 | VG_(printf)("%2d %p = 0x%x\n", i, &esp[i], esp[i]); |
| 1468 | } |
| 1469 | # endif |
| 1470 | |
sewardj | 1fe7b00 | 2002-07-16 01:43:15 +0000 | [diff] [blame] | 1471 | /* Deal with calling __libc_freeres() at exit. When the |
| 1472 | client does __NR_exit, it's exiting for good. So we |
| 1473 | then run VG_(__libc_freeres_wrapper). That quits by |
| 1474 | doing VG_USERREQ__LIBC_FREERES_DONE, and at that point |
| 1475 | we really exit. To be safe we nuke all other threads |
sewardj | ade9d0d | 2002-07-26 10:52:48 +0000 | [diff] [blame] | 1476 | currently running. |
| 1477 | |
| 1478 | If not valgrinding (cachegrinding, etc) don't do this. |
| 1479 | __libc_freeres does some invalid frees which crash |
| 1480 | the unprotected malloc/free system. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1481 | |
| 1482 | /* If __NR_exit, remember the supplied argument. */ |
| 1483 | if (VG_(threads)[tid].m_eax == __NR_exit) |
| 1484 | VG_(exitcode) = VG_(threads)[tid].m_ebx; /* syscall arg1 */ |
| 1485 | |
sewardj | ade9d0d | 2002-07-26 10:52:48 +0000 | [diff] [blame] | 1486 | if (VG_(threads)[tid].m_eax == __NR_exit |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1487 | && ! VG_(needs).run_libc_freeres) { |
sewardj | ade9d0d | 2002-07-26 10:52:48 +0000 | [diff] [blame] | 1488 | if (VG_(clo_trace_syscalls) || VG_(clo_trace_sched)) { |
| 1489 | VG_(message)(Vg_DebugMsg, |
| 1490 | "Caught __NR_exit; quitting"); |
| 1491 | } |
| 1492 | return VgSrc_ExitSyscall; |
| 1493 | } |
| 1494 | |
sewardj | 1fe7b00 | 2002-07-16 01:43:15 +0000 | [diff] [blame] | 1495 | if (VG_(threads)[tid].m_eax == __NR_exit) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1496 | vg_assert(VG_(needs).run_libc_freeres); |
sewardj | 1fe7b00 | 2002-07-16 01:43:15 +0000 | [diff] [blame] | 1497 | if (0 || VG_(clo_trace_syscalls) || VG_(clo_trace_sched)) { |
| 1498 | VG_(message)(Vg_DebugMsg, |
| 1499 | "Caught __NR_exit; running __libc_freeres()"); |
| 1500 | } |
| 1501 | VG_(nuke_all_threads_except) ( tid ); |
| 1502 | VG_(threads)[tid].m_eip = (UInt)(&VG_(__libc_freeres_wrapper)); |
| 1503 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 1504 | goto stage1; /* party on, dudes (but not for much longer :) */ |
| 1505 | } |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1506 | |
sewardj | 83798bf | 2002-05-24 00:11:16 +0000 | [diff] [blame] | 1507 | /* Trap syscalls to __NR_sched_yield and just have this |
| 1508 | thread yield instead. Not essential, just an |
| 1509 | optimisation. */ |
| 1510 | if (VG_(threads)[tid].m_eax == __NR_sched_yield) { |
| 1511 | SET_EAX(tid, 0); /* syscall returns with success */ |
| 1512 | goto stage1; /* find a new thread to run */ |
| 1513 | } |
| 1514 | |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1515 | sched_do_syscall(tid); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1516 | |
| 1517 | # if 0 |
| 1518 | { UInt* esp; Int i; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1519 | esp=(UInt*)VG_(threads)[tid].m_esp; |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1520 | VG_(printf)("AFTER\n"); |
| 1521 | for (i = 10; i >= -10; i--) |
| 1522 | VG_(printf)("%2d %p = 0x%x\n", i, &esp[i], esp[i]); |
| 1523 | } |
| 1524 | # endif |
| 1525 | |
sewardj | 77f0fc1 | 2002-07-12 01:23:03 +0000 | [diff] [blame] | 1526 | if (VG_(threads)[tid].status == VgTs_Runnable) { |
| 1527 | /* Better do a signal check, since if in a tight loop |
| 1528 | with a slow syscall it may be a very long time |
| 1529 | before we get back to the main signal check in Stage 1. */ |
| 1530 | sigs_delivered = VG_(deliver_signals)(); |
| 1531 | if (sigs_delivered) |
| 1532 | VG_(do_sanity_checks)( False ); |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1533 | continue; /* with this thread */ |
sewardj | 77f0fc1 | 2002-07-12 01:23:03 +0000 | [diff] [blame] | 1534 | } else { |
| 1535 | goto stage1; |
| 1536 | } |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
sewardj | d7fd4d2 | 2002-04-24 01:57:27 +0000 | [diff] [blame] | 1539 | /* It's an event we can't quickly deal with. Give up running |
| 1540 | this thread and handle things the expensive way. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1541 | break; |
| 1542 | } |
| 1543 | |
| 1544 | /* ======================= Phase 3 of 3 ======================= |
| 1545 | Handle non-trivial thread requests, mostly pthread stuff. */ |
| 1546 | |
| 1547 | /* Ok, we've fallen out of the dispatcher for a |
| 1548 | non-completely-trivial reason. First, update basic-block |
| 1549 | counters. */ |
| 1550 | |
| 1551 | done_this_time = (Int)dispatch_ctr_SAVED - (Int)VG_(dispatch_ctr) - 1; |
| 1552 | vg_assert(done_this_time >= 0); |
| 1553 | VG_(bbs_to_go) -= (ULong)done_this_time; |
| 1554 | VG_(bbs_done) += (ULong)done_this_time; |
| 1555 | |
| 1556 | if (0 && trc != VG_TRC_INNER_FASTMISS) |
| 1557 | VG_(message)(Vg_DebugMsg, "thread %d: completed %d bbs, trc %d", |
| 1558 | tid, done_this_time, (Int)trc ); |
| 1559 | |
| 1560 | if (0 && trc != VG_TRC_INNER_FASTMISS) |
| 1561 | VG_(message)(Vg_DebugMsg, "thread %d: %ld bbs, event %s", |
| 1562 | tid, VG_(bbs_done), |
| 1563 | name_of_sched_event(trc) ); |
sewardj | 9d1b5d3 | 2002-04-17 19:40:49 +0000 | [diff] [blame] | 1564 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1565 | /* Examine the thread's return code to figure out why it |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1566 | stopped. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1567 | |
| 1568 | switch (trc) { |
| 1569 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1570 | case VG_TRC_INNER_COUNTERZERO: |
| 1571 | /* Timeslice is out. Let a new thread be scheduled, |
| 1572 | simply by doing nothing, causing us to arrive back at |
| 1573 | Phase 1. */ |
| 1574 | if (VG_(bbs_to_go) == 0) { |
| 1575 | goto debug_stop; |
| 1576 | } |
| 1577 | vg_assert(VG_(dispatch_ctr) == 0); |
| 1578 | break; |
| 1579 | |
| 1580 | case VG_TRC_UNRESUMABLE_SIGNAL: |
| 1581 | /* It got a SIGSEGV/SIGBUS, which we need to deliver right |
| 1582 | away. Again, do nothing, so we wind up back at Phase |
| 1583 | 1, whereupon the signal will be "delivered". */ |
| 1584 | break; |
| 1585 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1586 | default: |
| 1587 | VG_(printf)("\ntrc = %d\n", trc); |
| 1588 | VG_(panic)("VG_(scheduler), phase 3: " |
| 1589 | "unexpected thread return code"); |
| 1590 | /* NOTREACHED */ |
| 1591 | break; |
| 1592 | |
| 1593 | } /* switch (trc) */ |
| 1594 | |
| 1595 | /* That completes Phase 3 of 3. Return now to the top of the |
| 1596 | main scheduler loop, to Phase 1 of 3. */ |
| 1597 | |
| 1598 | } /* top-level scheduler loop */ |
| 1599 | |
| 1600 | |
| 1601 | /* NOTREACHED */ |
| 1602 | VG_(panic)("scheduler: post-main-loop ?!"); |
| 1603 | /* NOTREACHED */ |
| 1604 | |
| 1605 | debug_stop: |
| 1606 | /* If we exited because of a debug stop, print the translation |
| 1607 | of the last block executed -- by translating it again, and |
| 1608 | throwing away the result. */ |
| 1609 | VG_(printf)( |
| 1610 | "======vvvvvvvv====== LAST TRANSLATION ======vvvvvvvv======\n"); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1611 | VG_(translate)( &VG_(threads)[tid], |
| 1612 | VG_(threads)[tid].m_eip, NULL, NULL, NULL ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1613 | VG_(printf)("\n"); |
| 1614 | VG_(printf)( |
| 1615 | "======^^^^^^^^====== LAST TRANSLATION ======^^^^^^^^======\n"); |
| 1616 | |
| 1617 | return VgSrc_BbsDone; |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | /* --------------------------------------------------------------------- |
| 1622 | The pthread implementation. |
| 1623 | ------------------------------------------------------------------ */ |
| 1624 | |
| 1625 | #include <pthread.h> |
| 1626 | #include <errno.h> |
| 1627 | |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 1628 | #define VG_PTHREAD_STACK_MIN \ |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 1629 | (VG_PTHREAD_STACK_SIZE - VG_AR_CLIENT_STACKBASE_REDZONE_SZB) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1630 | |
| 1631 | /* /usr/include/bits/pthreadtypes.h: |
| 1632 | typedef unsigned long int pthread_t; |
| 1633 | */ |
| 1634 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1635 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1636 | /* ----------------------------------------------------------- |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1637 | Thread CREATION, JOINAGE and CANCELLATION: HELPER FNS |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1638 | -------------------------------------------------------- */ |
| 1639 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1640 | /* We've decided to action a cancellation on tid. Make it jump to |
| 1641 | thread_exit_wrapper() in vg_libpthread.c, passing PTHREAD_CANCELED |
| 1642 | as the arg. */ |
| 1643 | static |
| 1644 | void make_thread_jump_to_cancelhdlr ( ThreadId tid ) |
| 1645 | { |
| 1646 | Char msg_buf[100]; |
| 1647 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1648 | /* Push PTHREAD_CANCELED on the stack and jump to the cancellation |
| 1649 | handler -- which is really thread_exit_wrapper() in |
| 1650 | vg_libpthread.c. */ |
| 1651 | vg_assert(VG_(threads)[tid].cancel_pend != NULL); |
| 1652 | VG_(threads)[tid].m_esp -= 4; |
| 1653 | * (UInt*)(VG_(threads)[tid].m_esp) = (UInt)PTHREAD_CANCELED; |
| 1654 | VG_(threads)[tid].m_eip = (UInt)VG_(threads)[tid].cancel_pend; |
| 1655 | VG_(threads)[tid].status = VgTs_Runnable; |
| 1656 | /* Make sure we aren't cancelled again whilst handling this |
| 1657 | cancellation. */ |
| 1658 | VG_(threads)[tid].cancel_st = False; |
| 1659 | if (VG_(clo_trace_sched)) { |
| 1660 | VG_(sprintf)(msg_buf, |
| 1661 | "jump to cancellation handler (hdlr = %p)", |
| 1662 | VG_(threads)[tid].cancel_pend); |
| 1663 | print_sched_event(tid, msg_buf); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | |
| 1668 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1669 | /* Release resources and generally clean up once a thread has finally |
| 1670 | disappeared. */ |
| 1671 | static |
| 1672 | void cleanup_after_thread_exited ( ThreadId tid ) |
| 1673 | { |
sewardj | 89f20fd | 2002-06-30 10:57:30 +0000 | [diff] [blame] | 1674 | Int i; |
sewardj | 3a951cf | 2002-05-15 22:25:47 +0000 | [diff] [blame] | 1675 | vki_ksigset_t irrelevant_sigmask; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1676 | vg_assert(VG_(is_valid_or_empty_tid)(tid)); |
| 1677 | vg_assert(VG_(threads)[tid].status == VgTs_Empty); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1678 | /* Its stack is now off-limits */ |
| 1679 | VG_TRACK( die_mem_stack, VG_(threads)[tid].stack_base, |
| 1680 | VG_(threads)[tid].stack_size ); |
| 1681 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1682 | /* Forget about any pending signals directed specifically at this |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1683 | thread, and get rid of signal handlers specifically arranged for |
| 1684 | this thread. */ |
sewardj | 3a951cf | 2002-05-15 22:25:47 +0000 | [diff] [blame] | 1685 | VG_(block_all_host_signals)( &irrelevant_sigmask ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1686 | VG_(handle_SCSS_change)( False /* lazy update */ ); |
sewardj | 89f20fd | 2002-06-30 10:57:30 +0000 | [diff] [blame] | 1687 | |
| 1688 | /* Clean up the waiting_fd table */ |
| 1689 | for (i = 0; i < VG_N_WAITING_FDS; i++) { |
| 1690 | if (vg_waiting_fds[i].tid == tid) { |
| 1691 | vg_waiting_fds[i].fd = -1; /* not in use */ |
| 1692 | } |
| 1693 | } |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 1694 | |
| 1695 | /* Deallocate its LDT, if it ever had one. */ |
| 1696 | VG_(deallocate_LDT_for_thread)( VG_(threads)[tid].ldt ); |
| 1697 | VG_(threads)[tid].ldt = NULL; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1701 | /* Look for matching pairs of threads waiting for joiners and threads |
| 1702 | waiting for joinees. For each such pair copy the return value of |
| 1703 | the joinee into the joiner, let the joiner resume and discard the |
| 1704 | joinee. */ |
| 1705 | static |
| 1706 | void maybe_rendezvous_joiners_and_joinees ( void ) |
| 1707 | { |
| 1708 | Char msg_buf[100]; |
| 1709 | void** thread_return; |
| 1710 | ThreadId jnr, jee; |
| 1711 | |
| 1712 | for (jnr = 1; jnr < VG_N_THREADS; jnr++) { |
| 1713 | if (VG_(threads)[jnr].status != VgTs_WaitJoinee) |
| 1714 | continue; |
| 1715 | jee = VG_(threads)[jnr].joiner_jee_tid; |
| 1716 | if (jee == VG_INVALID_THREADID) |
| 1717 | continue; |
| 1718 | vg_assert(VG_(is_valid_tid)(jee)); |
| 1719 | if (VG_(threads)[jee].status != VgTs_WaitJoiner) |
| 1720 | continue; |
| 1721 | /* ok! jnr is waiting to join with jee, and jee is waiting to be |
| 1722 | joined by ... well, any thread. So let's do it! */ |
| 1723 | |
| 1724 | /* Copy return value to where joiner wants it. */ |
| 1725 | thread_return = VG_(threads)[jnr].joiner_thread_return; |
| 1726 | if (thread_return != NULL) { |
| 1727 | /* CHECK thread_return writable */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1728 | VG_TRACK( pre_mem_write, Vg_CorePThread, &VG_(threads)[jnr], |
| 1729 | "pthread_join: thread_return", |
| 1730 | (Addr)thread_return, sizeof(void*)); |
sewardj | 5a3798b | 2002-06-04 23:24:22 +0000 | [diff] [blame] | 1731 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1732 | *thread_return = VG_(threads)[jee].joinee_retval; |
| 1733 | /* Not really right, since it makes the thread's return value |
| 1734 | appear to be defined even if it isn't. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1735 | VG_TRACK( post_mem_write, (Addr)thread_return, sizeof(void*) ); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | /* Joinee is discarded */ |
| 1739 | VG_(threads)[jee].status = VgTs_Empty; /* bye! */ |
| 1740 | cleanup_after_thread_exited ( jee ); |
| 1741 | if (VG_(clo_trace_sched)) { |
| 1742 | VG_(sprintf)(msg_buf, |
| 1743 | "rendezvous with joinee %d. %d resumes, %d exits.", |
| 1744 | jee, jnr, jee ); |
| 1745 | print_sched_event(jnr, msg_buf); |
| 1746 | } |
| 1747 | |
| 1748 | /* joiner returns with success */ |
| 1749 | VG_(threads)[jnr].status = VgTs_Runnable; |
| 1750 | SET_EDX(jnr, 0); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1755 | /* Nuke all threads other than tid. POSIX specifies that this should |
| 1756 | happen in __NR_exec, and after a __NR_fork() when I am the child, |
| 1757 | as POSIX requires. */ |
| 1758 | void VG_(nuke_all_threads_except) ( ThreadId me ) |
| 1759 | { |
| 1760 | ThreadId tid; |
| 1761 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
| 1762 | if (tid == me |
| 1763 | || VG_(threads)[tid].status == VgTs_Empty) |
| 1764 | continue; |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 1765 | if (0) |
| 1766 | VG_(printf)( |
| 1767 | "VG_(nuke_all_threads_except): nuking tid %d\n", tid); |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1768 | VG_(threads)[tid].status = VgTs_Empty; |
| 1769 | cleanup_after_thread_exited( tid ); |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1774 | /* ----------------------------------------------------------- |
| 1775 | Thread CREATION, JOINAGE and CANCELLATION: REQUESTS |
| 1776 | -------------------------------------------------------- */ |
| 1777 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1778 | static |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1779 | void do__cleanup_push ( ThreadId tid, CleanupEntry* cu ) |
| 1780 | { |
| 1781 | Int sp; |
| 1782 | Char msg_buf[100]; |
| 1783 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1784 | sp = VG_(threads)[tid].custack_used; |
| 1785 | if (VG_(clo_trace_sched)) { |
| 1786 | VG_(sprintf)(msg_buf, |
| 1787 | "cleanup_push (fn %p, arg %p) -> slot %d", |
| 1788 | cu->fn, cu->arg, sp); |
| 1789 | print_sched_event(tid, msg_buf); |
| 1790 | } |
| 1791 | vg_assert(sp >= 0 && sp <= VG_N_CLEANUPSTACK); |
| 1792 | if (sp == VG_N_CLEANUPSTACK) |
| 1793 | VG_(panic)("do__cleanup_push: VG_N_CLEANUPSTACK is too small." |
| 1794 | " Increase and recompile."); |
| 1795 | VG_(threads)[tid].custack[sp] = *cu; |
| 1796 | sp++; |
| 1797 | VG_(threads)[tid].custack_used = sp; |
| 1798 | SET_EDX(tid, 0); |
| 1799 | } |
| 1800 | |
| 1801 | |
| 1802 | static |
| 1803 | void do__cleanup_pop ( ThreadId tid, CleanupEntry* cu ) |
| 1804 | { |
| 1805 | Int sp; |
| 1806 | Char msg_buf[100]; |
| 1807 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1808 | sp = VG_(threads)[tid].custack_used; |
| 1809 | if (VG_(clo_trace_sched)) { |
| 1810 | VG_(sprintf)(msg_buf, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1811 | "cleanup_pop from slot %d", sp-1); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1812 | print_sched_event(tid, msg_buf); |
| 1813 | } |
| 1814 | vg_assert(sp >= 0 && sp <= VG_N_CLEANUPSTACK); |
| 1815 | if (sp == 0) { |
| 1816 | SET_EDX(tid, -1); |
| 1817 | return; |
| 1818 | } |
| 1819 | sp--; |
| 1820 | *cu = VG_(threads)[tid].custack[sp]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1821 | // JJJ: no corresponding pre_mem_write check?? |
| 1822 | VG_TRACK( post_mem_write, (Addr)cu, sizeof(CleanupEntry) ); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1823 | VG_(threads)[tid].custack_used = sp; |
| 1824 | SET_EDX(tid, 0); |
| 1825 | } |
| 1826 | |
| 1827 | |
| 1828 | static |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 1829 | void do_pthread_yield ( ThreadId tid ) |
| 1830 | { |
| 1831 | Char msg_buf[100]; |
| 1832 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 1833 | if (VG_(clo_trace_sched)) { |
| 1834 | VG_(sprintf)(msg_buf, "yield"); |
| 1835 | print_sched_event(tid, msg_buf); |
| 1836 | } |
| 1837 | SET_EDX(tid, 0); |
| 1838 | } |
| 1839 | |
| 1840 | |
| 1841 | static |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1842 | void do__testcancel ( ThreadId tid ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1843 | { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1844 | Char msg_buf[100]; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1845 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1846 | if (VG_(clo_trace_sched)) { |
| 1847 | VG_(sprintf)(msg_buf, "testcancel"); |
| 1848 | print_sched_event(tid, msg_buf); |
| 1849 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1850 | if (/* is there a cancellation pending on this thread? */ |
| 1851 | VG_(threads)[tid].cancel_pend != NULL |
| 1852 | && /* is this thread accepting cancellations? */ |
| 1853 | VG_(threads)[tid].cancel_st) { |
| 1854 | /* Ok, let's do the cancellation. */ |
| 1855 | make_thread_jump_to_cancelhdlr ( tid ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1856 | } else { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1857 | /* No, we keep going. */ |
| 1858 | SET_EDX(tid, 0); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1859 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | |
| 1863 | static |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1864 | void do__set_cancelstate ( ThreadId tid, Int state ) |
| 1865 | { |
| 1866 | Bool old_st; |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1867 | Char msg_buf[100]; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1868 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1869 | if (VG_(clo_trace_sched)) { |
| 1870 | VG_(sprintf)(msg_buf, "set_cancelstate to %d (%s)", state, |
| 1871 | state==PTHREAD_CANCEL_ENABLE |
| 1872 | ? "ENABLE" |
| 1873 | : (state==PTHREAD_CANCEL_DISABLE ? "DISABLE" : "???")); |
| 1874 | print_sched_event(tid, msg_buf); |
| 1875 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1876 | old_st = VG_(threads)[tid].cancel_st; |
| 1877 | if (state == PTHREAD_CANCEL_ENABLE) { |
| 1878 | VG_(threads)[tid].cancel_st = True; |
| 1879 | } else |
| 1880 | if (state == PTHREAD_CANCEL_DISABLE) { |
| 1881 | VG_(threads)[tid].cancel_st = False; |
| 1882 | } else { |
| 1883 | VG_(panic)("do__set_cancelstate"); |
| 1884 | } |
| 1885 | SET_EDX(tid, old_st ? PTHREAD_CANCEL_ENABLE |
| 1886 | : PTHREAD_CANCEL_DISABLE); |
| 1887 | } |
| 1888 | |
| 1889 | |
| 1890 | static |
| 1891 | void do__set_canceltype ( ThreadId tid, Int type ) |
| 1892 | { |
| 1893 | Bool old_ty; |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1894 | Char msg_buf[100]; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1895 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1896 | if (VG_(clo_trace_sched)) { |
| 1897 | VG_(sprintf)(msg_buf, "set_canceltype to %d (%s)", type, |
| 1898 | type==PTHREAD_CANCEL_ASYNCHRONOUS |
| 1899 | ? "ASYNCHRONOUS" |
| 1900 | : (type==PTHREAD_CANCEL_DEFERRED ? "DEFERRED" : "???")); |
| 1901 | print_sched_event(tid, msg_buf); |
| 1902 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1903 | old_ty = VG_(threads)[tid].cancel_ty; |
| 1904 | if (type == PTHREAD_CANCEL_ASYNCHRONOUS) { |
| 1905 | VG_(threads)[tid].cancel_ty = False; |
| 1906 | } else |
| 1907 | if (type == PTHREAD_CANCEL_DEFERRED) { |
sewardj | af00b6d | 2002-05-29 23:30:28 +0000 | [diff] [blame] | 1908 | VG_(threads)[tid].cancel_ty = True; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1909 | } else { |
| 1910 | VG_(panic)("do__set_canceltype"); |
| 1911 | } |
| 1912 | SET_EDX(tid, old_ty ? PTHREAD_CANCEL_DEFERRED |
| 1913 | : PTHREAD_CANCEL_ASYNCHRONOUS); |
| 1914 | } |
| 1915 | |
| 1916 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1917 | /* Set or get the detach state for thread det. */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1918 | static |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1919 | void do__set_or_get_detach ( ThreadId tid, |
| 1920 | Int what, ThreadId det ) |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1921 | { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1922 | ThreadId i; |
| 1923 | Char msg_buf[100]; |
| 1924 | /* VG_(printf)("do__set_or_get_detach tid %d what %d det %d\n", |
| 1925 | tid, what, det); */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1926 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1927 | if (VG_(clo_trace_sched)) { |
| 1928 | VG_(sprintf)(msg_buf, "set_or_get_detach %d (%s) for tid %d", what, |
| 1929 | what==0 ? "not-detached" : ( |
| 1930 | what==1 ? "detached" : ( |
| 1931 | what==2 ? "fetch old value" : "???")), |
| 1932 | det ); |
| 1933 | print_sched_event(tid, msg_buf); |
| 1934 | } |
| 1935 | |
| 1936 | if (!VG_(is_valid_tid)(det)) { |
| 1937 | SET_EDX(tid, -1); |
| 1938 | return; |
| 1939 | } |
| 1940 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1941 | switch (what) { |
| 1942 | case 2: /* get */ |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1943 | SET_EDX(tid, VG_(threads)[det].detached ? 1 : 0); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1944 | return; |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1945 | case 1: /* set detached. If someone is in a join-wait for det, |
| 1946 | do not detach. */ |
| 1947 | for (i = 1; i < VG_N_THREADS; i++) { |
| 1948 | if (VG_(threads)[i].status == VgTs_WaitJoinee |
| 1949 | && VG_(threads)[i].joiner_jee_tid == det) { |
| 1950 | SET_EDX(tid, 0); |
| 1951 | if (VG_(clo_trace_sched)) { |
| 1952 | VG_(sprintf)(msg_buf, |
| 1953 | "tid %d not detached because %d in join-wait for it %d", |
| 1954 | det, i); |
| 1955 | print_sched_event(tid, msg_buf); |
| 1956 | } |
| 1957 | return; |
| 1958 | } |
| 1959 | } |
| 1960 | VG_(threads)[det].detached = True; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1961 | SET_EDX(tid, 0); |
| 1962 | return; |
| 1963 | case 0: /* set not detached */ |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1964 | VG_(threads)[det].detached = False; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1965 | SET_EDX(tid, 0); |
| 1966 | return; |
| 1967 | default: |
| 1968 | VG_(panic)("do__set_or_get_detach"); |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | |
| 1973 | static |
| 1974 | void do__set_cancelpend ( ThreadId tid, |
| 1975 | ThreadId cee, |
| 1976 | void (*cancelpend_hdlr)(void*) ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1977 | { |
| 1978 | Char msg_buf[100]; |
| 1979 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1980 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1981 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 1982 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1983 | if (!VG_(is_valid_tid)(cee)) { |
| 1984 | if (VG_(clo_trace_sched)) { |
| 1985 | VG_(sprintf)(msg_buf, |
| 1986 | "set_cancelpend for invalid tid %d", cee); |
| 1987 | print_sched_event(tid, msg_buf); |
| 1988 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1989 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1990 | "pthread_cancel: target thread does not exist, or invalid"); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1991 | SET_EDX(tid, -VKI_ESRCH); |
| 1992 | return; |
| 1993 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1994 | |
| 1995 | VG_(threads)[cee].cancel_pend = cancelpend_hdlr; |
| 1996 | |
| 1997 | if (VG_(clo_trace_sched)) { |
| 1998 | VG_(sprintf)(msg_buf, |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1999 | "set_cancelpend (hdlr = %p, set by tid %d)", |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2000 | cancelpend_hdlr, tid); |
| 2001 | print_sched_event(cee, msg_buf); |
| 2002 | } |
| 2003 | |
| 2004 | /* Thread doing the cancelling returns with success. */ |
| 2005 | SET_EDX(tid, 0); |
| 2006 | |
| 2007 | /* Perhaps we can nuke the cancellee right now? */ |
| 2008 | do__testcancel(cee); |
| 2009 | } |
| 2010 | |
| 2011 | |
| 2012 | static |
| 2013 | void do_pthread_join ( ThreadId tid, |
| 2014 | ThreadId jee, void** thread_return ) |
| 2015 | { |
| 2016 | Char msg_buf[100]; |
| 2017 | ThreadId i; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2018 | /* jee, the joinee, is the thread specified as an arg in thread |
| 2019 | tid's call to pthread_join. So tid is the join-er. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2020 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2021 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2022 | |
| 2023 | if (jee == tid) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2024 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2025 | "pthread_join: attempt to join to self"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2026 | SET_EDX(tid, EDEADLK); /* libc constant, not a kernel one */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2027 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2028 | return; |
| 2029 | } |
| 2030 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2031 | /* Flush any completed pairs, so as to make sure what we're looking |
| 2032 | at is up-to-date. */ |
| 2033 | maybe_rendezvous_joiners_and_joinees(); |
| 2034 | |
| 2035 | /* Is this a sane request? */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2036 | if (jee < 0 |
| 2037 | || jee >= VG_N_THREADS |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2038 | || VG_(threads)[jee].status == VgTs_Empty) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2039 | /* Invalid thread to join to. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2040 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2041 | "pthread_join: target thread does not exist, or invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2042 | SET_EDX(tid, EINVAL); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2043 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2044 | return; |
| 2045 | } |
| 2046 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2047 | /* Is anyone else already in a join-wait for jee? */ |
| 2048 | for (i = 1; i < VG_N_THREADS; i++) { |
| 2049 | if (i == tid) continue; |
| 2050 | if (VG_(threads)[i].status == VgTs_WaitJoinee |
| 2051 | && VG_(threads)[i].joiner_jee_tid == jee) { |
| 2052 | /* Someone already did join on this thread */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2053 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2054 | "pthread_join: another thread already " |
| 2055 | "in join-wait for target thread"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2056 | SET_EDX(tid, EINVAL); |
| 2057 | VG_(threads)[tid].status = VgTs_Runnable; |
| 2058 | return; |
| 2059 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2062 | /* Mark this thread as waiting for the joinee. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2063 | VG_(threads)[tid].status = VgTs_WaitJoinee; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2064 | VG_(threads)[tid].joiner_thread_return = thread_return; |
| 2065 | VG_(threads)[tid].joiner_jee_tid = jee; |
| 2066 | |
| 2067 | /* Look for matching joiners and joinees and do the right thing. */ |
| 2068 | maybe_rendezvous_joiners_and_joinees(); |
| 2069 | |
| 2070 | /* Return value is irrelevant since this this thread becomes |
| 2071 | non-runnable. maybe_resume_joiner() will cause it to return the |
| 2072 | right value when it resumes. */ |
| 2073 | |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 2074 | if (VG_(clo_trace_sched)) { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2075 | VG_(sprintf)(msg_buf, |
| 2076 | "wait for joinee %d (may already be ready)", jee); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2077 | print_sched_event(tid, msg_buf); |
| 2078 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2082 | /* ( void* ): calling thread waits for joiner and returns the void* to |
| 2083 | it. This is one of two ways in which a thread can finally exit -- |
| 2084 | the other is do__quit. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2085 | static |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2086 | void do__wait_joiner ( ThreadId tid, void* retval ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2087 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2088 | Char msg_buf[100]; |
| 2089 | vg_assert(VG_(is_valid_tid)(tid)); |
| 2090 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 2091 | if (VG_(clo_trace_sched)) { |
| 2092 | VG_(sprintf)(msg_buf, |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 2093 | "do__wait_joiner(retval = %p) (non-detached thread exit)", retval); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2094 | print_sched_event(tid, msg_buf); |
| 2095 | } |
| 2096 | VG_(threads)[tid].status = VgTs_WaitJoiner; |
| 2097 | VG_(threads)[tid].joinee_retval = retval; |
| 2098 | maybe_rendezvous_joiners_and_joinees(); |
| 2099 | } |
| 2100 | |
| 2101 | |
| 2102 | /* ( no-args ): calling thread disappears from the system forever. |
| 2103 | Reclaim resources. */ |
| 2104 | static |
| 2105 | void do__quit ( ThreadId tid ) |
| 2106 | { |
| 2107 | Char msg_buf[100]; |
| 2108 | vg_assert(VG_(is_valid_tid)(tid)); |
| 2109 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 2110 | VG_(threads)[tid].status = VgTs_Empty; /* bye! */ |
| 2111 | cleanup_after_thread_exited ( tid ); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2112 | if (VG_(clo_trace_sched)) { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 2113 | VG_(sprintf)(msg_buf, "do__quit (detached thread exit)"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2114 | print_sched_event(tid, msg_buf); |
| 2115 | } |
| 2116 | /* Return value is irrelevant; this thread will not get |
| 2117 | rescheduled. */ |
| 2118 | } |
| 2119 | |
| 2120 | |
| 2121 | /* Should never be entered. If it is, will be on the simulated |
| 2122 | CPU. */ |
| 2123 | static |
| 2124 | void do__apply_in_new_thread_bogusRA ( void ) |
| 2125 | { |
| 2126 | VG_(panic)("do__apply_in_new_thread_bogusRA"); |
| 2127 | } |
| 2128 | |
| 2129 | /* (Fn, Arg): Create a new thread and run Fn applied to Arg in it. Fn |
| 2130 | MUST NOT return -- ever. Eventually it will do either __QUIT or |
| 2131 | __WAIT_JOINER. Return the child tid to the parent. */ |
| 2132 | static |
| 2133 | void do__apply_in_new_thread ( ThreadId parent_tid, |
| 2134 | void* (*fn)(void *), |
| 2135 | void* arg ) |
| 2136 | { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2137 | Addr new_stack; |
| 2138 | UInt new_stk_szb; |
| 2139 | ThreadId tid; |
| 2140 | Char msg_buf[100]; |
| 2141 | |
| 2142 | /* Paranoia ... */ |
| 2143 | vg_assert(sizeof(pthread_t) == sizeof(UInt)); |
| 2144 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2145 | vg_assert(VG_(threads)[parent_tid].status != VgTs_Empty); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2146 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 2147 | tid = vg_alloc_ThreadState(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2148 | |
| 2149 | /* If we've created the main thread's tid, we're in deep trouble :) */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2150 | vg_assert(tid != 1); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2151 | vg_assert(VG_(is_valid_or_empty_tid)(tid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2152 | |
| 2153 | /* Copy the parent's CPU state into the child's, in a roundabout |
| 2154 | way (via baseBlock). */ |
| 2155 | VG_(load_thread_state)(parent_tid); |
| 2156 | VG_(save_thread_state)(tid); |
| 2157 | |
| 2158 | /* Consider allocating the child a stack, if the one it already has |
| 2159 | is inadequate. */ |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 2160 | new_stk_szb = VG_PTHREAD_STACK_MIN; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2161 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2162 | if (new_stk_szb > VG_(threads)[tid].stack_size) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2163 | /* Again, for good measure :) We definitely don't want to be |
| 2164 | allocating a stack for the main thread. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2165 | vg_assert(tid != 1); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2166 | /* for now, we don't handle the case of anything other than |
| 2167 | assigning it for the first time. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2168 | vg_assert(VG_(threads)[tid].stack_size == 0); |
| 2169 | vg_assert(VG_(threads)[tid].stack_base == (Addr)NULL); |
sewardj | e904795 | 2002-06-05 20:28:33 +0000 | [diff] [blame] | 2170 | new_stack = (Addr)VG_(get_memory_from_mmap)( new_stk_szb, |
| 2171 | "new thread stack" ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2172 | VG_(threads)[tid].stack_base = new_stack; |
| 2173 | VG_(threads)[tid].stack_size = new_stk_szb; |
| 2174 | VG_(threads)[tid].stack_highest_word |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2175 | = new_stack + new_stk_szb |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 2176 | - VG_AR_CLIENT_STACKBASE_REDZONE_SZB; /* -4 ??? */; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2177 | } |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 2178 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2179 | /* Having got memory to hold the thread's stack: |
| 2180 | - set %esp as base + size |
| 2181 | - mark everything below %esp inaccessible |
| 2182 | - mark redzone at stack end inaccessible |
| 2183 | */ |
| 2184 | VG_(threads)[tid].m_esp = VG_(threads)[tid].stack_base |
| 2185 | + VG_(threads)[tid].stack_size |
| 2186 | - VG_AR_CLIENT_STACKBASE_REDZONE_SZB; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 2187 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2188 | VG_TRACK ( die_mem_stack, VG_(threads)[tid].stack_base, |
| 2189 | + new_stk_szb - VG_AR_CLIENT_STACKBASE_REDZONE_SZB); |
| 2190 | VG_TRACK ( ban_mem_stack, VG_(threads)[tid].m_esp, |
| 2191 | VG_AR_CLIENT_STACKBASE_REDZONE_SZB ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2192 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2193 | /* push two args */ |
| 2194 | VG_(threads)[tid].m_esp -= 8; |
| 2195 | VG_TRACK ( new_mem_stack, (Addr)VG_(threads)[tid].m_esp, 2 * 4 ); |
| 2196 | VG_TRACK ( pre_mem_write, Vg_CorePThread, & VG_(threads)[tid], |
| 2197 | "new thread: stack", |
| 2198 | (Addr)VG_(threads)[tid].m_esp, 2 * 4 ); |
| 2199 | |
| 2200 | /* push arg and (bogus) return address */ |
| 2201 | * (UInt*)(VG_(threads)[tid].m_esp+4) = (UInt)arg; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2202 | * (UInt*)(VG_(threads)[tid].m_esp) |
| 2203 | = (UInt)&do__apply_in_new_thread_bogusRA; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2204 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2205 | VG_TRACK ( post_mem_write, VG_(threads)[tid].m_esp, 2 * 4 ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2206 | |
| 2207 | /* this is where we start */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2208 | VG_(threads)[tid].m_eip = (UInt)fn; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2209 | |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 2210 | if (VG_(clo_trace_sched)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2211 | VG_(sprintf)(msg_buf, "new thread, created by %d", parent_tid ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2212 | print_sched_event(tid, msg_buf); |
| 2213 | } |
| 2214 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2215 | /* Create new thread with default attrs: |
| 2216 | deferred cancellation, not detached |
| 2217 | */ |
| 2218 | mostly_clear_thread_record(tid); |
| 2219 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2220 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2221 | /* We inherit our parent's signal mask. */ |
| 2222 | VG_(threads)[tid].sig_mask = VG_(threads)[parent_tid].sig_mask; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2223 | VG_(ksigemptyset)(&VG_(threads)[tid].sigs_waited_for); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2224 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 2225 | /* We inherit our parent's LDT. */ |
| 2226 | if (VG_(threads)[parent_tid].ldt == NULL) { |
| 2227 | /* We hope this is the common case. */ |
| 2228 | VG_(threads)[tid].ldt = NULL; |
| 2229 | } else { |
| 2230 | /* No luck .. we have to take a copy of the parent's. */ |
| 2231 | VG_(threads)[tid].ldt |
| 2232 | = VG_(allocate_LDT_for_thread)( VG_(threads)[parent_tid].ldt ); |
| 2233 | } |
| 2234 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2235 | /* return child's tid to parent */ |
| 2236 | SET_EDX(parent_tid, tid); /* success */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2240 | /* ----------------------------------------------------------- |
| 2241 | MUTEXes |
| 2242 | -------------------------------------------------------- */ |
| 2243 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2244 | /* pthread_mutex_t is a struct with at 5 words: |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2245 | typedef struct |
| 2246 | { |
| 2247 | int __m_reserved; -- Reserved for future use |
| 2248 | int __m_count; -- Depth of recursive locking |
| 2249 | _pthread_descr __m_owner; -- Owner thread (if recursive or errcheck) |
| 2250 | int __m_kind; -- Mutex kind: fast, recursive or errcheck |
| 2251 | struct _pthread_fastlock __m_lock; -- Underlying fast lock |
| 2252 | } pthread_mutex_t; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2253 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2254 | #define PTHREAD_MUTEX_INITIALIZER \ |
| 2255 | {0, 0, 0, PTHREAD_MUTEX_TIMED_NP, __LOCK_INITIALIZER} |
| 2256 | # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \ |
| 2257 | {0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, __LOCK_INITIALIZER} |
| 2258 | # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \ |
| 2259 | {0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, __LOCK_INITIALIZER} |
| 2260 | # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \ |
| 2261 | {0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, __LOCK_INITIALIZER} |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2262 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2263 | How we use it: |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2264 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2265 | __m_kind never changes and indicates whether or not it is recursive. |
| 2266 | |
| 2267 | __m_count indicates the lock count; if 0, the mutex is not owned by |
| 2268 | anybody. |
| 2269 | |
| 2270 | __m_owner has a ThreadId value stuffed into it. We carefully arrange |
| 2271 | that ThreadId == 0 is invalid (VG_INVALID_THREADID), so that |
| 2272 | statically initialised mutexes correctly appear |
| 2273 | to belong to nobody. |
| 2274 | |
| 2275 | In summary, a not-in-use mutex is distinguised by having __m_owner |
| 2276 | == 0 (VG_INVALID_THREADID) and __m_count == 0 too. If one of those |
| 2277 | conditions holds, the other should too. |
| 2278 | |
| 2279 | There is no linked list of threads waiting for this mutex. Instead |
| 2280 | a thread in WaitMX state points at the mutex with its waited_on_mx |
| 2281 | field. This makes _unlock() inefficient, but simple to implement the |
| 2282 | right semantics viz-a-viz signals. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2283 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2284 | We don't have to deal with mutex initialisation; the client side |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2285 | deals with that for us. |
| 2286 | */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2287 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2288 | /* Helper fns ... */ |
| 2289 | static |
| 2290 | void release_one_thread_waiting_on_mutex ( pthread_mutex_t* mutex, |
| 2291 | Char* caller ) |
| 2292 | { |
| 2293 | Int i; |
| 2294 | Char msg_buf[100]; |
| 2295 | |
| 2296 | /* Find some arbitrary thread waiting on this mutex, and make it |
| 2297 | runnable. If none are waiting, mark the mutex as not held. */ |
| 2298 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2299 | if (VG_(threads)[i].status == VgTs_Empty) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2300 | continue; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2301 | if (VG_(threads)[i].status == VgTs_WaitMX |
| 2302 | && VG_(threads)[i].associated_mx == mutex) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2303 | break; |
| 2304 | } |
| 2305 | |
| 2306 | vg_assert(i <= VG_N_THREADS); |
| 2307 | if (i == VG_N_THREADS) { |
| 2308 | /* Nobody else is waiting on it. */ |
| 2309 | mutex->__m_count = 0; |
| 2310 | mutex->__m_owner = VG_INVALID_THREADID; |
| 2311 | } else { |
| 2312 | /* Notionally transfer the hold to thread i, whose |
| 2313 | pthread_mutex_lock() call now returns with 0 (success). */ |
| 2314 | /* The .count is already == 1. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2315 | vg_assert(VG_(threads)[i].associated_mx == mutex); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2316 | mutex->__m_owner = (_pthread_descr)i; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2317 | VG_(threads)[i].status = VgTs_Runnable; |
| 2318 | VG_(threads)[i].associated_mx = NULL; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2319 | /* m_edx already holds pth_mx_lock() success (0) */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2320 | |
| 2321 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2322 | VG_(sprintf)(msg_buf, "%s mx %p: RESUME", |
| 2323 | caller, mutex ); |
| 2324 | print_pthread_event(i, msg_buf); |
| 2325 | } |
| 2326 | } |
| 2327 | } |
| 2328 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2329 | |
| 2330 | static |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2331 | void do_pthread_mutex_lock( ThreadId tid, |
| 2332 | Bool is_trylock, |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2333 | pthread_mutex_t* mutex ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2334 | { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2335 | Char msg_buf[100]; |
| 2336 | Char* caller |
sewardj | 8ccc2be | 2002-05-10 20:26:37 +0000 | [diff] [blame] | 2337 | = is_trylock ? "pthread_mutex_trylock" |
| 2338 | : "pthread_mutex_lock "; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2339 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2340 | if (VG_(clo_trace_pthread_level) >= 2) { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2341 | VG_(sprintf)(msg_buf, "%s mx %p ...", caller, mutex ); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2342 | print_pthread_event(tid, msg_buf); |
| 2343 | } |
| 2344 | |
| 2345 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2346 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2347 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2348 | |
| 2349 | /* POSIX doesn't mandate this, but for sanity ... */ |
| 2350 | if (mutex == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2351 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2352 | "pthread_mutex_lock/trylock: mutex is NULL"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2353 | SET_EDX(tid, EINVAL); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2354 | return; |
| 2355 | } |
| 2356 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2357 | /* More paranoia ... */ |
| 2358 | switch (mutex->__m_kind) { |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2359 | # ifndef GLIBC_2_1 |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2360 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2361 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
| 2362 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2363 | # ifdef GLIBC_2_1 |
sewardj | 8e651d7 | 2002-05-10 21:00:19 +0000 | [diff] [blame] | 2364 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2365 | # endif |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2366 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 2367 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2368 | if (mutex->__m_count >= 0) break; |
| 2369 | /* else fall thru */ |
| 2370 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2371 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2372 | "pthread_mutex_lock/trylock: mutex is invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2373 | SET_EDX(tid, EINVAL); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2374 | return; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2377 | if (mutex->__m_count > 0) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2378 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2379 | vg_assert(VG_(is_valid_tid)((ThreadId)mutex->__m_owner)); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2380 | |
| 2381 | /* Someone has it already. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2382 | if ((ThreadId)mutex->__m_owner == tid) { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2383 | /* It's locked -- by me! */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2384 | if (mutex->__m_kind == PTHREAD_MUTEX_RECURSIVE_NP) { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2385 | /* return 0 (success). */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2386 | mutex->__m_count++; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2387 | SET_EDX(tid, 0); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 2388 | if (0) |
| 2389 | VG_(printf)("!!!!!! tid %d, mx %p -> locked %d\n", |
| 2390 | tid, mutex, mutex->__m_count); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2391 | return; |
| 2392 | } else { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2393 | if (is_trylock) |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2394 | SET_EDX(tid, EBUSY); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2395 | else |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2396 | SET_EDX(tid, EDEADLK); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2397 | return; |
| 2398 | } |
| 2399 | } else { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2400 | /* Someone else has it; we have to wait. Mark ourselves |
| 2401 | thusly. */ |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 2402 | /* GUARD: __m_count > 0 && __m_owner is valid */ |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2403 | if (is_trylock) { |
| 2404 | /* caller is polling; so return immediately. */ |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2405 | SET_EDX(tid, EBUSY); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2406 | } else { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2407 | VG_(threads)[tid].status = VgTs_WaitMX; |
| 2408 | VG_(threads)[tid].associated_mx = mutex; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2409 | SET_EDX(tid, 0); /* pth_mx_lock success value */ |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2410 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2411 | VG_(sprintf)(msg_buf, "%s mx %p: BLOCK", |
| 2412 | caller, mutex ); |
| 2413 | print_pthread_event(tid, msg_buf); |
| 2414 | } |
| 2415 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2416 | return; |
| 2417 | } |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2418 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2419 | } else { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2420 | /* Nobody owns it. Sanity check ... */ |
| 2421 | vg_assert(mutex->__m_owner == VG_INVALID_THREADID); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2422 | /* We get it! [for the first time]. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2423 | mutex->__m_count = 1; |
| 2424 | mutex->__m_owner = (_pthread_descr)tid; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2425 | |
| 2426 | VG_TRACK( post_mutex_lock, tid, mutex); |
| 2427 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2428 | /* return 0 (success). */ |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2429 | SET_EDX(tid, 0); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2430 | } |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2431 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | |
| 2435 | static |
| 2436 | void do_pthread_mutex_unlock ( ThreadId tid, |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2437 | pthread_mutex_t* mutex ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2438 | { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2439 | Char msg_buf[100]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2440 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 2441 | if (VG_(clo_trace_pthread_level) >= 2) { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2442 | VG_(sprintf)(msg_buf, "pthread_mutex_unlock mx %p ...", mutex ); |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 2443 | print_pthread_event(tid, msg_buf); |
| 2444 | } |
| 2445 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2446 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2447 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2448 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2449 | |
| 2450 | if (mutex == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2451 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2452 | "pthread_mutex_unlock: mutex is NULL"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2453 | SET_EDX(tid, EINVAL); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2454 | return; |
| 2455 | } |
| 2456 | |
| 2457 | /* More paranoia ... */ |
| 2458 | switch (mutex->__m_kind) { |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2459 | # ifndef GLIBC_2_1 |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2460 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2461 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
| 2462 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2463 | # ifdef GLIBC_2_1 |
sewardj | 8e651d7 | 2002-05-10 21:00:19 +0000 | [diff] [blame] | 2464 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2465 | # endif |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2466 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 2467 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2468 | if (mutex->__m_count >= 0) break; |
| 2469 | /* else fall thru */ |
| 2470 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2471 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2472 | "pthread_mutex_unlock: mutex is invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2473 | SET_EDX(tid, EINVAL); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2474 | return; |
| 2475 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2476 | |
| 2477 | /* Barf if we don't currently hold the mutex. */ |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2478 | if (mutex->__m_count == 0) { |
| 2479 | /* nobody holds it */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2480 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2481 | "pthread_mutex_unlock: mutex is not locked"); |
| 2482 | SET_EDX(tid, EPERM); |
| 2483 | return; |
| 2484 | } |
| 2485 | |
| 2486 | if ((ThreadId)mutex->__m_owner != tid) { |
| 2487 | /* we don't hold it */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2488 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2489 | "pthread_mutex_unlock: mutex is locked by a different thread"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2490 | SET_EDX(tid, EPERM); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2491 | return; |
| 2492 | } |
| 2493 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2494 | /* If it's a multiply-locked recursive mutex, just decrement the |
| 2495 | lock count and return. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2496 | if (mutex->__m_count > 1) { |
| 2497 | vg_assert(mutex->__m_kind == PTHREAD_MUTEX_RECURSIVE_NP); |
| 2498 | mutex->__m_count --; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2499 | SET_EDX(tid, 0); /* success */ |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2500 | return; |
| 2501 | } |
| 2502 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2503 | /* Now we're sure it is locked exactly once, and by the thread who |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2504 | is now doing an unlock on it. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2505 | vg_assert(mutex->__m_count == 1); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2506 | vg_assert((ThreadId)mutex->__m_owner == tid); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2507 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2508 | VG_TRACK( post_mutex_unlock, tid, mutex); |
| 2509 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2510 | /* Release at max one thread waiting on this mutex. */ |
| 2511 | release_one_thread_waiting_on_mutex ( mutex, "pthread_mutex_lock" ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2512 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2513 | /* Our (tid's) pth_unlock() returns with 0 (success). */ |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2514 | SET_EDX(tid, 0); /* Success. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2518 | /* ----------------------------------------------------------- |
| 2519 | CONDITION VARIABLES |
| 2520 | -------------------------------------------------------- */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2521 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2522 | /* The relevant native types are as follows: |
| 2523 | (copied from /usr/include/bits/pthreadtypes.h) |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2524 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2525 | -- Conditions (not abstract because of PTHREAD_COND_INITIALIZER |
| 2526 | typedef struct |
| 2527 | { |
| 2528 | struct _pthread_fastlock __c_lock; -- Protect against concurrent access |
| 2529 | _pthread_descr __c_waiting; -- Threads waiting on this condition |
| 2530 | } pthread_cond_t; |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2531 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2532 | -- Attribute for conditionally variables. |
| 2533 | typedef struct |
| 2534 | { |
| 2535 | int __dummy; |
| 2536 | } pthread_condattr_t; |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2537 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2538 | #define PTHREAD_COND_INITIALIZER {__LOCK_INITIALIZER, 0} |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2539 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2540 | We don't use any fields of pthread_cond_t for anything at all. |
| 2541 | Only the identity of the CVs is important. |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2542 | |
| 2543 | Linux pthreads supports no attributes on condition variables, so we |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2544 | don't need to think too hard there. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2545 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2546 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2547 | static |
| 2548 | void do_pthread_cond_timedwait_TIMEOUT ( ThreadId tid ) |
| 2549 | { |
| 2550 | Char msg_buf[100]; |
| 2551 | pthread_mutex_t* mx; |
| 2552 | pthread_cond_t* cv; |
| 2553 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2554 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2555 | && VG_(threads)[tid].status == VgTs_WaitCV |
| 2556 | && VG_(threads)[tid].awaken_at != 0xFFFFFFFF); |
| 2557 | mx = VG_(threads)[tid].associated_mx; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2558 | vg_assert(mx != NULL); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2559 | cv = VG_(threads)[tid].associated_cv; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2560 | vg_assert(cv != NULL); |
| 2561 | |
| 2562 | if (mx->__m_owner == VG_INVALID_THREADID) { |
| 2563 | /* Currently unheld; hand it out to thread tid. */ |
| 2564 | vg_assert(mx->__m_count == 0); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2565 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2566 | SET_EDX(tid, ETIMEDOUT); /* pthread_cond_wait return value */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2567 | VG_(threads)[tid].associated_cv = NULL; |
| 2568 | VG_(threads)[tid].associated_mx = NULL; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2569 | mx->__m_owner = (_pthread_descr)tid; |
| 2570 | mx->__m_count = 1; |
| 2571 | |
| 2572 | if (VG_(clo_trace_pthread_level) >= 1) { |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2573 | VG_(sprintf)(msg_buf, |
| 2574 | "pthread_cond_timedwai cv %p: TIMEOUT with mx %p", |
| 2575 | cv, mx ); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2576 | print_pthread_event(tid, msg_buf); |
| 2577 | } |
| 2578 | } else { |
| 2579 | /* Currently held. Make thread tid be blocked on it. */ |
| 2580 | vg_assert(mx->__m_count > 0); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2581 | VG_(threads)[tid].status = VgTs_WaitMX; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2582 | SET_EDX(tid, ETIMEDOUT); /* pthread_cond_wait return value */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2583 | VG_(threads)[tid].associated_cv = NULL; |
| 2584 | VG_(threads)[tid].associated_mx = mx; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2585 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2586 | VG_(sprintf)(msg_buf, |
| 2587 | "pthread_cond_timedwai cv %p: TIMEOUT -> BLOCK for mx %p", |
| 2588 | cv, mx ); |
| 2589 | print_pthread_event(tid, msg_buf); |
| 2590 | } |
| 2591 | |
| 2592 | } |
| 2593 | } |
| 2594 | |
| 2595 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2596 | static |
| 2597 | void release_N_threads_waiting_on_cond ( pthread_cond_t* cond, |
| 2598 | Int n_to_release, |
| 2599 | Char* caller ) |
| 2600 | { |
| 2601 | Int i; |
| 2602 | Char msg_buf[100]; |
| 2603 | pthread_mutex_t* mx; |
| 2604 | |
| 2605 | while (True) { |
| 2606 | if (n_to_release == 0) |
| 2607 | return; |
| 2608 | |
| 2609 | /* Find a thread waiting on this CV. */ |
| 2610 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2611 | if (VG_(threads)[i].status == VgTs_Empty) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2612 | continue; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2613 | if (VG_(threads)[i].status == VgTs_WaitCV |
| 2614 | && VG_(threads)[i].associated_cv == cond) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2615 | break; |
| 2616 | } |
| 2617 | vg_assert(i <= VG_N_THREADS); |
| 2618 | |
| 2619 | if (i == VG_N_THREADS) { |
| 2620 | /* Nobody else is waiting on it. */ |
| 2621 | return; |
| 2622 | } |
| 2623 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2624 | mx = VG_(threads)[i].associated_mx; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2625 | vg_assert(mx != NULL); |
| 2626 | |
| 2627 | if (mx->__m_owner == VG_INVALID_THREADID) { |
| 2628 | /* Currently unheld; hand it out to thread i. */ |
| 2629 | vg_assert(mx->__m_count == 0); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2630 | VG_(threads)[i].status = VgTs_Runnable; |
| 2631 | VG_(threads)[i].associated_cv = NULL; |
| 2632 | VG_(threads)[i].associated_mx = NULL; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2633 | mx->__m_owner = (_pthread_descr)i; |
| 2634 | mx->__m_count = 1; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2635 | /* .m_edx already holds pth_cond_wait success value (0) */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2636 | |
| 2637 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2638 | VG_(sprintf)(msg_buf, "%s cv %p: RESUME with mx %p", |
| 2639 | caller, cond, mx ); |
| 2640 | print_pthread_event(i, msg_buf); |
| 2641 | } |
| 2642 | |
| 2643 | } else { |
| 2644 | /* Currently held. Make thread i be blocked on it. */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2645 | vg_assert(mx->__m_count > 0); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2646 | VG_(threads)[i].status = VgTs_WaitMX; |
| 2647 | VG_(threads)[i].associated_cv = NULL; |
| 2648 | VG_(threads)[i].associated_mx = mx; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2649 | SET_EDX(i, 0); /* pth_cond_wait success value */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2650 | |
| 2651 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2652 | VG_(sprintf)(msg_buf, "%s cv %p: BLOCK for mx %p", |
| 2653 | caller, cond, mx ); |
| 2654 | print_pthread_event(i, msg_buf); |
| 2655 | } |
| 2656 | |
| 2657 | } |
| 2658 | |
| 2659 | n_to_release--; |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | |
| 2664 | static |
| 2665 | void do_pthread_cond_wait ( ThreadId tid, |
| 2666 | pthread_cond_t *cond, |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2667 | pthread_mutex_t *mutex, |
| 2668 | UInt ms_end ) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2669 | { |
| 2670 | Char msg_buf[100]; |
| 2671 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2672 | /* If ms_end == 0xFFFFFFFF, wait forever (no timeout). Otherwise, |
| 2673 | ms_end is the ending millisecond. */ |
| 2674 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2675 | /* pre: mutex should be a valid mutex and owned by tid. */ |
| 2676 | if (VG_(clo_trace_pthread_level) >= 2) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2677 | VG_(sprintf)(msg_buf, "pthread_cond_wait cv %p, mx %p, end %d ...", |
| 2678 | cond, mutex, ms_end ); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2679 | print_pthread_event(tid, msg_buf); |
| 2680 | } |
| 2681 | |
| 2682 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2683 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2684 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2685 | |
| 2686 | if (mutex == NULL || cond == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2687 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2688 | "pthread_cond_wait/timedwait: cond or mutex is NULL"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2689 | SET_EDX(tid, EINVAL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2690 | return; |
| 2691 | } |
| 2692 | |
| 2693 | /* More paranoia ... */ |
| 2694 | switch (mutex->__m_kind) { |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2695 | # ifndef GLIBC_2_1 |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2696 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2697 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
| 2698 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2699 | # ifdef GLIBC_2_1 |
sewardj | 8e651d7 | 2002-05-10 21:00:19 +0000 | [diff] [blame] | 2700 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2701 | # endif |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2702 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 2703 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2704 | if (mutex->__m_count >= 0) break; |
| 2705 | /* else fall thru */ |
| 2706 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2707 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2708 | "pthread_cond_wait/timedwait: mutex is invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2709 | SET_EDX(tid, EINVAL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2710 | return; |
| 2711 | } |
| 2712 | |
| 2713 | /* Barf if we don't currently hold the mutex. */ |
| 2714 | if (mutex->__m_count == 0 /* nobody holds it */ |
| 2715 | || (ThreadId)mutex->__m_owner != tid /* we don't hold it */) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2716 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2717 | "pthread_cond_wait/timedwait: mutex is unlocked " |
| 2718 | "or is locked but not owned by thread"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2719 | SET_EDX(tid, EINVAL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2720 | return; |
| 2721 | } |
| 2722 | |
| 2723 | /* Queue ourselves on the condition. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2724 | VG_(threads)[tid].status = VgTs_WaitCV; |
| 2725 | VG_(threads)[tid].associated_cv = cond; |
| 2726 | VG_(threads)[tid].associated_mx = mutex; |
| 2727 | VG_(threads)[tid].awaken_at = ms_end; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2728 | |
| 2729 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2730 | VG_(sprintf)(msg_buf, |
| 2731 | "pthread_cond_wait cv %p, mx %p: BLOCK", |
| 2732 | cond, mutex ); |
| 2733 | print_pthread_event(tid, msg_buf); |
| 2734 | } |
| 2735 | |
| 2736 | /* Release the mutex. */ |
| 2737 | release_one_thread_waiting_on_mutex ( mutex, "pthread_cond_wait " ); |
| 2738 | } |
| 2739 | |
| 2740 | |
| 2741 | static |
| 2742 | void do_pthread_cond_signal_or_broadcast ( ThreadId tid, |
| 2743 | Bool broadcast, |
| 2744 | pthread_cond_t *cond ) |
| 2745 | { |
| 2746 | Char msg_buf[100]; |
| 2747 | Char* caller |
| 2748 | = broadcast ? "pthread_cond_broadcast" |
| 2749 | : "pthread_cond_signal "; |
| 2750 | |
| 2751 | if (VG_(clo_trace_pthread_level) >= 2) { |
| 2752 | VG_(sprintf)(msg_buf, "%s cv %p ...", |
| 2753 | caller, cond ); |
| 2754 | print_pthread_event(tid, msg_buf); |
| 2755 | } |
| 2756 | |
| 2757 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2758 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2759 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2760 | |
| 2761 | if (cond == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2762 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2763 | "pthread_cond_signal/broadcast: cond is NULL"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2764 | SET_EDX(tid, EINVAL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2765 | return; |
| 2766 | } |
| 2767 | |
| 2768 | release_N_threads_waiting_on_cond ( |
| 2769 | cond, |
| 2770 | broadcast ? VG_N_THREADS : 1, |
| 2771 | caller |
| 2772 | ); |
| 2773 | |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2774 | SET_EDX(tid, 0); /* success */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2777 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2778 | /* ----------------------------------------------------------- |
| 2779 | THREAD SPECIFIC DATA |
| 2780 | -------------------------------------------------------- */ |
| 2781 | |
| 2782 | static __inline__ |
| 2783 | Bool is_valid_key ( ThreadKey k ) |
| 2784 | { |
| 2785 | /* k unsigned; hence no < 0 check */ |
| 2786 | if (k >= VG_N_THREAD_KEYS) return False; |
| 2787 | if (!vg_thread_keys[k].inuse) return False; |
| 2788 | return True; |
| 2789 | } |
| 2790 | |
| 2791 | static |
| 2792 | void do_pthread_key_create ( ThreadId tid, |
| 2793 | pthread_key_t* key, |
| 2794 | void (*destructor)(void*) ) |
| 2795 | { |
| 2796 | Int i; |
| 2797 | Char msg_buf[100]; |
| 2798 | |
| 2799 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2800 | VG_(sprintf)(msg_buf, "pthread_key_create *key %p, destr %p", |
| 2801 | key, destructor ); |
| 2802 | print_pthread_event(tid, msg_buf); |
| 2803 | } |
| 2804 | |
| 2805 | vg_assert(sizeof(pthread_key_t) == sizeof(ThreadKey)); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2806 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2807 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2808 | |
| 2809 | for (i = 0; i < VG_N_THREAD_KEYS; i++) |
| 2810 | if (!vg_thread_keys[i].inuse) |
| 2811 | break; |
| 2812 | |
| 2813 | if (i == VG_N_THREAD_KEYS) { |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2814 | /* SET_EDX(tid, EAGAIN); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2815 | return; |
| 2816 | */ |
| 2817 | VG_(panic)("pthread_key_create: VG_N_THREAD_KEYS is too low;" |
| 2818 | " increase and recompile"); |
| 2819 | } |
| 2820 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 2821 | vg_thread_keys[i].inuse = True; |
| 2822 | vg_thread_keys[i].destructor = destructor; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2823 | |
sewardj | 5a3798b | 2002-06-04 23:24:22 +0000 | [diff] [blame] | 2824 | /* check key for addressibility */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2825 | VG_TRACK( pre_mem_write, Vg_CorePThread, &VG_(threads)[tid], |
| 2826 | "pthread_key_create: key", |
| 2827 | (Addr)key, sizeof(pthread_key_t)); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2828 | *key = i; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2829 | VG_TRACK( post_mem_write, (Addr)key, sizeof(pthread_key_t) ); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2830 | |
| 2831 | SET_EDX(tid, 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | |
| 2835 | static |
| 2836 | void do_pthread_key_delete ( ThreadId tid, pthread_key_t key ) |
| 2837 | { |
| 2838 | Char msg_buf[100]; |
| 2839 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2840 | VG_(sprintf)(msg_buf, "pthread_key_delete key %d", |
| 2841 | key ); |
| 2842 | print_pthread_event(tid, msg_buf); |
| 2843 | } |
| 2844 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2845 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2846 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2847 | |
| 2848 | if (!is_valid_key(key)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2849 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2850 | "pthread_key_delete: key is invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2851 | SET_EDX(tid, EINVAL); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2852 | return; |
| 2853 | } |
| 2854 | |
| 2855 | vg_thread_keys[key].inuse = False; |
| 2856 | |
| 2857 | /* Optional. We're not required to do this, although it shouldn't |
| 2858 | make any difference to programs which use the key/specifics |
| 2859 | functions correctly. */ |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2860 | # if 1 |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2861 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2862 | if (VG_(threads)[tid].status != VgTs_Empty) |
| 2863 | VG_(threads)[tid].specifics[key] = NULL; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2864 | } |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2865 | # endif |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | |
| 2869 | static |
| 2870 | void do_pthread_getspecific ( ThreadId tid, pthread_key_t key ) |
| 2871 | { |
| 2872 | Char msg_buf[100]; |
| 2873 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2874 | VG_(sprintf)(msg_buf, "pthread_getspecific key %d", |
| 2875 | key ); |
| 2876 | print_pthread_event(tid, msg_buf); |
| 2877 | } |
| 2878 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2879 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2880 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2881 | |
| 2882 | if (!is_valid_key(key)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2883 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2884 | "pthread_getspecific: key is invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2885 | SET_EDX(tid, (UInt)NULL); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2886 | return; |
| 2887 | } |
| 2888 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2889 | SET_EDX(tid, (UInt)VG_(threads)[tid].specifics[key]); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | |
| 2893 | static |
| 2894 | void do_pthread_setspecific ( ThreadId tid, |
| 2895 | pthread_key_t key, |
| 2896 | void *pointer ) |
| 2897 | { |
| 2898 | Char msg_buf[100]; |
| 2899 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2900 | VG_(sprintf)(msg_buf, "pthread_setspecific key %d, ptr %p", |
| 2901 | key, pointer ); |
| 2902 | print_pthread_event(tid, msg_buf); |
| 2903 | } |
| 2904 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2905 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2906 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2907 | |
| 2908 | if (!is_valid_key(key)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2909 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2910 | "pthread_setspecific: key is invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2911 | SET_EDX(tid, EINVAL); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2912 | return; |
| 2913 | } |
| 2914 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2915 | VG_(threads)[tid].specifics[key] = pointer; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2916 | SET_EDX(tid, 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 2920 | /* Helper for calling destructors at thread exit. If key is valid, |
| 2921 | copy the thread's specific value into cu->arg and put the *key*'s |
| 2922 | destructor fn address in cu->fn. Then return 0 to the caller. |
| 2923 | Otherwise return non-zero to the caller. */ |
| 2924 | static |
| 2925 | void do__get_key_destr_and_spec ( ThreadId tid, |
| 2926 | pthread_key_t key, |
| 2927 | CleanupEntry* cu ) |
| 2928 | { |
| 2929 | Char msg_buf[100]; |
| 2930 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2931 | VG_(sprintf)(msg_buf, |
| 2932 | "get_key_destr_and_arg (key = %d)", key ); |
| 2933 | print_pthread_event(tid, msg_buf); |
| 2934 | } |
| 2935 | vg_assert(VG_(is_valid_tid)(tid)); |
| 2936 | vg_assert(key >= 0 && key < VG_N_THREAD_KEYS); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2937 | |
| 2938 | // JJJ: no pre_mem_write check?? |
| 2939 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 2940 | if (!vg_thread_keys[key].inuse) { |
| 2941 | SET_EDX(tid, -1); |
| 2942 | return; |
| 2943 | } |
| 2944 | cu->fn = vg_thread_keys[key].destructor; |
| 2945 | cu->arg = VG_(threads)[tid].specifics[key]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2946 | VG_TRACK( post_mem_write, (Addr)cu, sizeof(CleanupEntry) ); |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 2947 | SET_EDX(tid, 0); |
| 2948 | } |
| 2949 | |
| 2950 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2951 | /* --------------------------------------------------- |
| 2952 | SIGNALS |
| 2953 | ------------------------------------------------ */ |
| 2954 | |
| 2955 | /* See comment in vg_libthread.c:pthread_sigmask() regarding |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2956 | deliberate confusion of types sigset_t and vki_sigset_t. Return 0 |
| 2957 | for OK and 1 for some kind of addressing error, which the |
| 2958 | vg_libpthread.c routine turns into return values 0 and EFAULT |
| 2959 | respectively. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2960 | static |
| 2961 | void do_pthread_sigmask ( ThreadId tid, |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2962 | Int vki_how, |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2963 | vki_ksigset_t* newmask, |
| 2964 | vki_ksigset_t* oldmask ) |
| 2965 | { |
| 2966 | Char msg_buf[100]; |
| 2967 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2968 | VG_(sprintf)(msg_buf, |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2969 | "pthread_sigmask vki_how %d, newmask %p, oldmask %p", |
| 2970 | vki_how, newmask, oldmask ); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2971 | print_pthread_event(tid, msg_buf); |
| 2972 | } |
| 2973 | |
| 2974 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2975 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2976 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2977 | if (newmask) |
| 2978 | VG_TRACK( pre_mem_read, Vg_CorePThread, &VG_(threads)[tid], |
| 2979 | "pthread_sigmask: newmask", |
| 2980 | (Addr)newmask, sizeof(vki_ksigset_t)); |
| 2981 | if (oldmask) |
| 2982 | VG_TRACK( pre_mem_write, Vg_CorePThread, &VG_(threads)[tid], |
| 2983 | "pthread_sigmask: oldmask", |
| 2984 | (Addr)oldmask, sizeof(vki_ksigset_t)); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2985 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2986 | VG_(do_pthread_sigmask_SCSS_upd) ( tid, vki_how, newmask, oldmask ); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2987 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2988 | if (oldmask) |
| 2989 | VG_TRACK( post_mem_write, (Addr)oldmask, sizeof(vki_ksigset_t) ); |
sewardj | 3a951cf | 2002-05-15 22:25:47 +0000 | [diff] [blame] | 2990 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2991 | /* Success. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2992 | SET_EDX(tid, 0); |
| 2993 | } |
| 2994 | |
| 2995 | |
| 2996 | static |
| 2997 | void do_sigwait ( ThreadId tid, |
| 2998 | vki_ksigset_t* set, |
| 2999 | Int* sig ) |
| 3000 | { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3001 | vki_ksigset_t irrelevant_sigmask; |
| 3002 | Char msg_buf[100]; |
| 3003 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3004 | if (VG_(clo_trace_signals) || VG_(clo_trace_sched)) { |
| 3005 | VG_(sprintf)(msg_buf, |
| 3006 | "suspend due to sigwait(): set %p, sig %p", |
| 3007 | set, sig ); |
| 3008 | print_pthread_event(tid, msg_buf); |
| 3009 | } |
| 3010 | |
| 3011 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3012 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3013 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3014 | /* Change SCSS */ |
| 3015 | VG_(threads)[tid].sigs_waited_for = *set; |
| 3016 | VG_(threads)[tid].status = VgTs_WaitSIG; |
| 3017 | |
| 3018 | VG_(block_all_host_signals)( &irrelevant_sigmask ); |
| 3019 | VG_(handle_SCSS_change)( False /* lazy update */ ); |
| 3020 | } |
| 3021 | |
| 3022 | |
| 3023 | static |
| 3024 | void do_pthread_kill ( ThreadId tid, /* me */ |
| 3025 | ThreadId thread, /* thread to signal */ |
| 3026 | Int sig ) |
| 3027 | { |
| 3028 | Char msg_buf[100]; |
| 3029 | |
| 3030 | if (VG_(clo_trace_signals) || VG_(clo_trace_pthread_level) >= 1) { |
| 3031 | VG_(sprintf)(msg_buf, |
| 3032 | "pthread_kill thread %d, signo %d", |
| 3033 | thread, sig ); |
| 3034 | print_pthread_event(tid, msg_buf); |
| 3035 | } |
| 3036 | |
| 3037 | vg_assert(VG_(is_valid_tid)(tid) |
| 3038 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 3039 | |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 3040 | if (!VG_(is_valid_tid)(thread)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3041 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 3042 | "pthread_kill: invalid target thread"); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3043 | SET_EDX(tid, -VKI_ESRCH); |
| 3044 | return; |
| 3045 | } |
| 3046 | |
| 3047 | if (sig < 1 || sig > VKI_KNSIG) { |
| 3048 | SET_EDX(tid, -VKI_EINVAL); |
| 3049 | return; |
| 3050 | } |
| 3051 | |
| 3052 | VG_(send_signal_to_thread)( thread, sig ); |
| 3053 | SET_EDX(tid, 0); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3057 | /* ----------------------------------------------------------- |
| 3058 | FORK HANDLERS. |
| 3059 | -------------------------------------------------------- */ |
| 3060 | |
| 3061 | static |
| 3062 | void do__set_fhstack_used ( ThreadId tid, Int n ) |
| 3063 | { |
| 3064 | Char msg_buf[100]; |
| 3065 | if (VG_(clo_trace_sched)) { |
| 3066 | VG_(sprintf)(msg_buf, "set_fhstack_used to %d", n ); |
| 3067 | print_pthread_event(tid, msg_buf); |
| 3068 | } |
| 3069 | |
| 3070 | vg_assert(VG_(is_valid_tid)(tid) |
| 3071 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 3072 | |
| 3073 | if (n >= 0 && n < VG_N_FORKHANDLERSTACK) { |
| 3074 | vg_fhstack_used = n; |
| 3075 | SET_EDX(tid, 0); |
| 3076 | } else { |
| 3077 | SET_EDX(tid, -1); |
| 3078 | } |
| 3079 | } |
| 3080 | |
| 3081 | |
| 3082 | static |
| 3083 | void do__get_fhstack_used ( ThreadId tid ) |
| 3084 | { |
| 3085 | Int n; |
| 3086 | Char msg_buf[100]; |
| 3087 | if (VG_(clo_trace_sched)) { |
| 3088 | VG_(sprintf)(msg_buf, "get_fhstack_used" ); |
| 3089 | print_pthread_event(tid, msg_buf); |
| 3090 | } |
| 3091 | |
| 3092 | vg_assert(VG_(is_valid_tid)(tid) |
| 3093 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 3094 | |
| 3095 | n = vg_fhstack_used; |
| 3096 | vg_assert(n >= 0 && n < VG_N_FORKHANDLERSTACK); |
| 3097 | SET_EDX(tid, n); |
| 3098 | } |
| 3099 | |
| 3100 | static |
| 3101 | void do__set_fhstack_entry ( ThreadId tid, Int n, ForkHandlerEntry* fh ) |
| 3102 | { |
| 3103 | Char msg_buf[100]; |
| 3104 | if (VG_(clo_trace_sched)) { |
| 3105 | VG_(sprintf)(msg_buf, "set_fhstack_entry %d to %p", n, fh ); |
| 3106 | print_pthread_event(tid, msg_buf); |
| 3107 | } |
| 3108 | |
| 3109 | vg_assert(VG_(is_valid_tid)(tid) |
| 3110 | && VG_(threads)[tid].status == VgTs_Runnable); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3111 | VG_TRACK( pre_mem_read, Vg_CorePThread, &VG_(threads)[tid], |
| 3112 | "pthread_atfork: prepare/parent/child", |
| 3113 | (Addr)fh, sizeof(ForkHandlerEntry)); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3114 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3115 | if (n < 0 || n >= VG_N_FORKHANDLERSTACK) { |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3116 | SET_EDX(tid, -1); |
| 3117 | return; |
| 3118 | } |
| 3119 | |
| 3120 | vg_fhstack[n] = *fh; |
| 3121 | SET_EDX(tid, 0); |
| 3122 | } |
| 3123 | |
| 3124 | |
| 3125 | static |
| 3126 | void do__get_fhstack_entry ( ThreadId tid, Int n, /*OUT*/ |
| 3127 | ForkHandlerEntry* fh ) |
| 3128 | { |
| 3129 | Char msg_buf[100]; |
| 3130 | if (VG_(clo_trace_sched)) { |
| 3131 | VG_(sprintf)(msg_buf, "get_fhstack_entry %d", n ); |
| 3132 | print_pthread_event(tid, msg_buf); |
| 3133 | } |
| 3134 | |
| 3135 | vg_assert(VG_(is_valid_tid)(tid) |
| 3136 | && VG_(threads)[tid].status == VgTs_Runnable); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3137 | VG_TRACK( pre_mem_write, Vg_CorePThread, &VG_(threads)[tid], |
| 3138 | "fork: prepare/parent/child", |
| 3139 | (Addr)fh, sizeof(ForkHandlerEntry)); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3140 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3141 | if (n < 0 || n >= VG_N_FORKHANDLERSTACK) { |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3142 | SET_EDX(tid, -1); |
| 3143 | return; |
| 3144 | } |
| 3145 | |
| 3146 | *fh = vg_fhstack[n]; |
| 3147 | SET_EDX(tid, 0); |
| 3148 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3149 | VG_TRACK( post_mem_write, (Addr)fh, sizeof(ForkHandlerEntry) ); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3153 | /* --------------------------------------------------------------------- |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3154 | Handle client requests. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3155 | ------------------------------------------------------------------ */ |
| 3156 | |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3157 | /* Do a client request for the thread tid. After the request, tid may |
| 3158 | or may not still be runnable; if not, the scheduler will have to |
| 3159 | choose a new thread to run. |
| 3160 | */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3161 | static |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3162 | void do_client_request ( ThreadId tid ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3163 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3164 | # define RETURN_WITH(vvv) \ |
| 3165 | { tst->m_edx = (vvv); \ |
| 3166 | tst->sh_edx = VG_(written_shadow_reg); \ |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | ThreadState* tst = &VG_(threads)[tid]; |
| 3170 | UInt* arg = (UInt*)(VG_(threads)[tid].m_eax); |
| 3171 | UInt req_no = arg[0]; |
| 3172 | |
| 3173 | /* VG_(printf)("req no = 0x%x\n", req_no); */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3174 | switch (req_no) { |
| 3175 | |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3176 | case VG_USERREQ__MALLOC: |
| 3177 | RETURN_WITH( |
| 3178 | (UInt)VG_(client_malloc) ( tst, arg[1], Vg_AllocMalloc ) |
| 3179 | ); |
| 3180 | break; |
| 3181 | |
| 3182 | case VG_USERREQ__BUILTIN_NEW: |
| 3183 | RETURN_WITH( |
| 3184 | (UInt)VG_(client_malloc) ( tst, arg[1], Vg_AllocNew ) |
| 3185 | ); |
| 3186 | break; |
| 3187 | |
| 3188 | case VG_USERREQ__BUILTIN_VEC_NEW: |
| 3189 | RETURN_WITH( |
| 3190 | (UInt)VG_(client_malloc) ( tst, arg[1], Vg_AllocNewVec ) |
| 3191 | ); |
| 3192 | break; |
| 3193 | |
| 3194 | case VG_USERREQ__FREE: |
| 3195 | VG_(client_free) ( tst, (void*)arg[1], Vg_AllocMalloc ); |
| 3196 | RETURN_WITH(0); /* irrelevant */ |
| 3197 | break; |
| 3198 | |
| 3199 | case VG_USERREQ__BUILTIN_DELETE: |
| 3200 | VG_(client_free) ( tst, (void*)arg[1], Vg_AllocNew ); |
| 3201 | RETURN_WITH(0); /* irrelevant */ |
| 3202 | break; |
| 3203 | |
| 3204 | case VG_USERREQ__BUILTIN_VEC_DELETE: |
| 3205 | VG_(client_free) ( tst, (void*)arg[1], Vg_AllocNewVec ); |
| 3206 | RETURN_WITH(0); /* irrelevant */ |
| 3207 | break; |
| 3208 | |
| 3209 | case VG_USERREQ__CALLOC: |
| 3210 | RETURN_WITH( |
| 3211 | (UInt)VG_(client_calloc) ( tst, arg[1], arg[2] ) |
| 3212 | ); |
| 3213 | break; |
| 3214 | |
| 3215 | case VG_USERREQ__REALLOC: |
| 3216 | RETURN_WITH( |
| 3217 | (UInt)VG_(client_realloc) ( tst, (void*)arg[1], arg[2] ) |
| 3218 | ); |
| 3219 | break; |
| 3220 | |
| 3221 | case VG_USERREQ__MEMALIGN: |
| 3222 | RETURN_WITH( |
| 3223 | (UInt)VG_(client_memalign) ( tst, arg[1], arg[2] ) |
| 3224 | ); |
| 3225 | break; |
| 3226 | |
| 3227 | case VG_USERREQ__PTHREAD_GET_THREADID: |
| 3228 | RETURN_WITH(tid); |
| 3229 | break; |
| 3230 | |
| 3231 | case VG_USERREQ__RUNNING_ON_VALGRIND: |
| 3232 | RETURN_WITH(1); |
| 3233 | break; |
| 3234 | |
| 3235 | case VG_USERREQ__GET_PTHREAD_TRACE_LEVEL: |
| 3236 | RETURN_WITH(VG_(clo_trace_pthread_level)); |
| 3237 | break; |
| 3238 | |
| 3239 | case VG_USERREQ__READ_MILLISECOND_TIMER: |
| 3240 | RETURN_WITH(VG_(read_millisecond_timer)()); |
| 3241 | break; |
| 3242 | |
| 3243 | /* Some of these may make thread tid non-runnable, but the |
| 3244 | scheduler checks for that on return from this function. */ |
| 3245 | case VG_USERREQ__PTHREAD_MUTEX_LOCK: |
| 3246 | do_pthread_mutex_lock( tid, False, (void *)(arg[1]) ); |
| 3247 | break; |
| 3248 | |
| 3249 | case VG_USERREQ__PTHREAD_MUTEX_TRYLOCK: |
| 3250 | do_pthread_mutex_lock( tid, True, (void *)(arg[1]) ); |
| 3251 | break; |
| 3252 | |
| 3253 | case VG_USERREQ__PTHREAD_MUTEX_UNLOCK: |
| 3254 | do_pthread_mutex_unlock( tid, (void *)(arg[1]) ); |
| 3255 | break; |
| 3256 | |
| 3257 | case VG_USERREQ__PTHREAD_GETSPECIFIC: |
| 3258 | do_pthread_getspecific ( tid, (UInt)(arg[1]) ); |
| 3259 | break; |
| 3260 | |
| 3261 | case VG_USERREQ__SET_CANCELTYPE: |
| 3262 | do__set_canceltype ( tid, arg[1] ); |
| 3263 | break; |
| 3264 | |
| 3265 | case VG_USERREQ__CLEANUP_PUSH: |
| 3266 | do__cleanup_push ( tid, (CleanupEntry*)(arg[1]) ); |
| 3267 | break; |
| 3268 | |
| 3269 | case VG_USERREQ__CLEANUP_POP: |
| 3270 | do__cleanup_pop ( tid, (CleanupEntry*)(arg[1]) ); |
| 3271 | break; |
| 3272 | |
| 3273 | case VG_USERREQ__TESTCANCEL: |
| 3274 | do__testcancel ( tid ); |
| 3275 | break; |
| 3276 | |
| 3277 | case VG_USERREQ__GET_N_SIGS_RETURNED: |
| 3278 | RETURN_WITH(VG_(threads)[tid].n_signals_returned); |
| 3279 | break; |
| 3280 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3281 | case VG_USERREQ__PTHREAD_JOIN: |
| 3282 | do_pthread_join( tid, arg[1], (void**)(arg[2]) ); |
| 3283 | break; |
| 3284 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3285 | case VG_USERREQ__PTHREAD_COND_WAIT: |
| 3286 | do_pthread_cond_wait( tid, |
| 3287 | (pthread_cond_t *)(arg[1]), |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3288 | (pthread_mutex_t *)(arg[2]), |
| 3289 | 0xFFFFFFFF /* no timeout */ ); |
| 3290 | break; |
| 3291 | |
| 3292 | case VG_USERREQ__PTHREAD_COND_TIMEDWAIT: |
| 3293 | do_pthread_cond_wait( tid, |
| 3294 | (pthread_cond_t *)(arg[1]), |
| 3295 | (pthread_mutex_t *)(arg[2]), |
| 3296 | arg[3] /* timeout millisecond point */ ); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3297 | break; |
| 3298 | |
| 3299 | case VG_USERREQ__PTHREAD_COND_SIGNAL: |
| 3300 | do_pthread_cond_signal_or_broadcast( |
| 3301 | tid, |
| 3302 | False, /* signal, not broadcast */ |
| 3303 | (pthread_cond_t *)(arg[1]) ); |
| 3304 | break; |
| 3305 | |
| 3306 | case VG_USERREQ__PTHREAD_COND_BROADCAST: |
| 3307 | do_pthread_cond_signal_or_broadcast( |
| 3308 | tid, |
| 3309 | True, /* broadcast, not signal */ |
| 3310 | (pthread_cond_t *)(arg[1]) ); |
| 3311 | break; |
| 3312 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3313 | case VG_USERREQ__PTHREAD_KEY_CREATE: |
| 3314 | do_pthread_key_create ( tid, |
| 3315 | (pthread_key_t*)(arg[1]), |
| 3316 | (void(*)(void*))(arg[2]) ); |
| 3317 | break; |
| 3318 | |
| 3319 | case VG_USERREQ__PTHREAD_KEY_DELETE: |
| 3320 | do_pthread_key_delete ( tid, |
| 3321 | (pthread_key_t)(arg[1]) ); |
| 3322 | break; |
| 3323 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3324 | case VG_USERREQ__PTHREAD_SETSPECIFIC: |
| 3325 | do_pthread_setspecific ( tid, |
| 3326 | (pthread_key_t)(arg[1]), |
| 3327 | (void*)(arg[2]) ); |
| 3328 | break; |
| 3329 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3330 | case VG_USERREQ__PTHREAD_SIGMASK: |
| 3331 | do_pthread_sigmask ( tid, |
| 3332 | arg[1], |
| 3333 | (vki_ksigset_t*)(arg[2]), |
| 3334 | (vki_ksigset_t*)(arg[3]) ); |
| 3335 | break; |
| 3336 | |
| 3337 | case VG_USERREQ__SIGWAIT: |
| 3338 | do_sigwait ( tid, |
| 3339 | (vki_ksigset_t*)(arg[1]), |
| 3340 | (Int*)(arg[2]) ); |
| 3341 | break; |
| 3342 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3343 | case VG_USERREQ__PTHREAD_KILL: |
| 3344 | do_pthread_kill ( tid, arg[1], arg[2] ); |
| 3345 | break; |
| 3346 | |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 3347 | case VG_USERREQ__PTHREAD_YIELD: |
| 3348 | do_pthread_yield ( tid ); |
sewardj | 18a62ff | 2002-07-12 22:30:51 +0000 | [diff] [blame] | 3349 | /* On return from do_client_request(), the scheduler will |
| 3350 | select a new thread to run. */ |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 3351 | break; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3352 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 3353 | case VG_USERREQ__SET_CANCELSTATE: |
| 3354 | do__set_cancelstate ( tid, arg[1] ); |
| 3355 | break; |
| 3356 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 3357 | case VG_USERREQ__SET_OR_GET_DETACH: |
| 3358 | do__set_or_get_detach ( tid, arg[1], arg[2] ); |
| 3359 | break; |
| 3360 | |
| 3361 | case VG_USERREQ__SET_CANCELPEND: |
| 3362 | do__set_cancelpend ( tid, arg[1], (void(*)(void*))arg[2] ); |
| 3363 | break; |
| 3364 | |
| 3365 | case VG_USERREQ__WAIT_JOINER: |
| 3366 | do__wait_joiner ( tid, (void*)arg[1] ); |
| 3367 | break; |
| 3368 | |
| 3369 | case VG_USERREQ__QUIT: |
| 3370 | do__quit ( tid ); |
| 3371 | break; |
| 3372 | |
| 3373 | case VG_USERREQ__APPLY_IN_NEW_THREAD: |
| 3374 | do__apply_in_new_thread ( tid, (void*(*)(void*))arg[1], |
| 3375 | (void*)arg[2] ); |
| 3376 | break; |
| 3377 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 3378 | case VG_USERREQ__GET_KEY_D_AND_S: |
| 3379 | do__get_key_destr_and_spec ( tid, |
| 3380 | (pthread_key_t)arg[1], |
| 3381 | (CleanupEntry*)arg[2] ); |
| 3382 | break; |
| 3383 | |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 3384 | case VG_USERREQ__NUKE_OTHER_THREADS: |
| 3385 | VG_(nuke_all_threads_except) ( tid ); |
| 3386 | SET_EDX(tid, 0); |
| 3387 | break; |
| 3388 | |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 3389 | case VG_USERREQ__PTHREAD_ERROR: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3390 | VG_(record_pthread_error)( tid, (Char*)(arg[1]) ); |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 3391 | SET_EDX(tid, 0); |
| 3392 | break; |
| 3393 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3394 | case VG_USERREQ__SET_FHSTACK_USED: |
| 3395 | do__set_fhstack_used( tid, (Int)(arg[1]) ); |
| 3396 | break; |
| 3397 | |
| 3398 | case VG_USERREQ__GET_FHSTACK_USED: |
| 3399 | do__get_fhstack_used( tid ); |
| 3400 | break; |
| 3401 | |
| 3402 | case VG_USERREQ__SET_FHSTACK_ENTRY: |
| 3403 | do__set_fhstack_entry( tid, (Int)(arg[1]), |
| 3404 | (ForkHandlerEntry*)(arg[2]) ); |
| 3405 | break; |
| 3406 | |
| 3407 | case VG_USERREQ__GET_FHSTACK_ENTRY: |
| 3408 | do__get_fhstack_entry( tid, (Int)(arg[1]), |
| 3409 | (ForkHandlerEntry*)(arg[2]) ); |
| 3410 | break; |
| 3411 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 3412 | case VG_USERREQ__SIGNAL_RETURNS: |
| 3413 | handle_signal_return(tid); |
| 3414 | break; |
sewardj | 54cacf0 | 2002-04-12 23:24:59 +0000 | [diff] [blame] | 3415 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3416 | /* Requests from the client program */ |
| 3417 | |
| 3418 | case VG_USERREQ__DISCARD_TRANSLATIONS: |
| 3419 | if (VG_(clo_verbosity) > 2) |
| 3420 | VG_(printf)( "client request: DISCARD_TRANSLATIONS," |
| 3421 | " addr %p, len %d\n", |
| 3422 | (void*)arg[1], arg[2] ); |
| 3423 | |
| 3424 | VG_(invalidate_translations)( arg[1], arg[2] ); |
| 3425 | |
| 3426 | SET_EDX( tid, 0 ); /* return value is meaningless */ |
| 3427 | break; |
| 3428 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3429 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3430 | if (VG_(needs).client_requests) { |
| 3431 | if (VG_(clo_verbosity) > 2) |
| 3432 | VG_(printf)("client request: code %d, addr %p, len %d\n", |
| 3433 | arg[0], (void*)arg[1], arg[2] ); |
| 3434 | |
| 3435 | SET_EDX(tid, |
| 3436 | SK_(handle_client_request) ( &VG_(threads)[tid], arg ) |
| 3437 | ); |
| 3438 | } else { |
| 3439 | VG_(printf)("\nError:\n" |
| 3440 | " unhandled client request: 0x%x. Perhaps\n" |
| 3441 | " VG_(needs).client_requests should be set?\n", |
| 3442 | arg[0]); |
| 3443 | VG_(panic)("do_client_request: unknown request"); |
| 3444 | /*NOTREACHED*/ |
| 3445 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3446 | break; |
| 3447 | } |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3448 | |
| 3449 | # undef RETURN_WITH |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3453 | /* --------------------------------------------------------------------- |
| 3454 | Sanity checking. |
| 3455 | ------------------------------------------------------------------ */ |
| 3456 | |
| 3457 | /* Internal consistency checks on the sched/pthread structures. */ |
| 3458 | static |
| 3459 | void scheduler_sanity ( void ) |
| 3460 | { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3461 | pthread_mutex_t* mx; |
| 3462 | pthread_cond_t* cv; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3463 | Int i; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3464 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3465 | /* VG_(printf)("scheduler_sanity\n"); */ |
| 3466 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3467 | mx = VG_(threads)[i].associated_mx; |
| 3468 | cv = VG_(threads)[i].associated_cv; |
| 3469 | if (VG_(threads)[i].status == VgTs_WaitMX) { |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3470 | /* If we're waiting on a MX: (1) the mx is not null, (2, 3) |
| 3471 | it's actually held by someone, since otherwise this thread |
| 3472 | is deadlocked, (4) the mutex's owner is not us, since |
| 3473 | otherwise this thread is also deadlocked. The logic in |
| 3474 | do_pthread_mutex_lock rejects attempts by a thread to lock |
| 3475 | a (non-recursive) mutex which it already owns. |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 3476 | |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3477 | (2) has been seen to fail sometimes. I don't know why. |
| 3478 | Possibly to do with signals. */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3479 | vg_assert(cv == NULL); |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 3480 | /* 1 */ vg_assert(mx != NULL); |
| 3481 | /* 2 */ vg_assert(mx->__m_count > 0); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3482 | /* 3 */ vg_assert(VG_(is_valid_tid)((ThreadId)mx->__m_owner)); |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 3483 | /* 4 */ vg_assert(i != (ThreadId)mx->__m_owner); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3484 | } else |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3485 | if (VG_(threads)[i].status == VgTs_WaitCV) { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3486 | vg_assert(cv != NULL); |
| 3487 | vg_assert(mx != NULL); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3488 | } else { |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 3489 | /* Unfortunately these don't hold true when a sighandler is |
| 3490 | running. To be fixed. */ |
| 3491 | /* vg_assert(cv == NULL); */ |
| 3492 | /* vg_assert(mx == NULL); */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3493 | } |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3494 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3495 | if (VG_(threads)[i].status != VgTs_Empty) { |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3496 | Int |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3497 | stack_used = (Addr)VG_(threads)[i].stack_highest_word |
| 3498 | - (Addr)VG_(threads)[i].m_esp; |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3499 | if (i > 1 /* not the root thread */ |
| 3500 | && stack_used |
| 3501 | >= (VG_PTHREAD_STACK_MIN - 1000 /* paranoia */)) { |
| 3502 | VG_(message)(Vg_UserMsg, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3503 | "Error: STACK OVERFLOW: " |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3504 | "thread %d: stack used %d, available %d", |
| 3505 | i, stack_used, VG_PTHREAD_STACK_MIN ); |
| 3506 | VG_(message)(Vg_UserMsg, |
| 3507 | "Terminating Valgrind. If thread(s) " |
| 3508 | "really need more stack, increase"); |
| 3509 | VG_(message)(Vg_UserMsg, |
| 3510 | "VG_PTHREAD_STACK_SIZE in vg_include.h and recompile."); |
| 3511 | VG_(exit)(1); |
| 3512 | } |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3513 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3514 | if (VG_(threads)[i].status == VgTs_WaitSIG) { |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3515 | vg_assert( ! VG_(kisemptysigset)( |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3516 | & VG_(threads)[i].sigs_waited_for) ); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3517 | } else { |
| 3518 | vg_assert( VG_(kisemptysigset)( |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3519 | & VG_(threads)[i].sigs_waited_for) ); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3522 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3523 | } |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3524 | |
| 3525 | for (i = 0; i < VG_N_THREAD_KEYS; i++) { |
| 3526 | if (!vg_thread_keys[i].inuse) |
| 3527 | vg_assert(vg_thread_keys[i].destructor == NULL); |
| 3528 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3532 | /*--------------------------------------------------------------------*/ |
| 3533 | /*--- end vg_scheduler.c ---*/ |
| 3534 | /*--------------------------------------------------------------------*/ |