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