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