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