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 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2004 Julian Seward |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 31 | #include "valgrind.h" /* for VG_USERREQ__RUNNING_ON_VALGRIND and |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 32 | VG_USERREQ__DISCARD_TRANSLATIONS, and others */ |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 33 | #include "core.h" |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 34 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 35 | |
| 36 | /* --------------------------------------------------------------------- |
| 37 | Types and globals for the scheduler. |
| 38 | ------------------------------------------------------------------ */ |
| 39 | |
rjwalsh | 7109a8c | 2004-09-02 00:31:02 +0000 | [diff] [blame] | 40 | /* ThreadId and ThreadState are defined in core.h. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 41 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 42 | /* Globals. A statically allocated array of threads. NOTE: [0] is |
| 43 | never used, to simplify the simulation of initialisers for |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 44 | LinuxThreads. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 45 | ThreadState VG_(threads)[VG_N_THREADS]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 46 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 47 | /* The process' fork-handler stack. */ |
| 48 | static Int vg_fhstack_used = 0; |
| 49 | static ForkHandlerEntry vg_fhstack[VG_N_FORKHANDLERSTACK]; |
| 50 | |
| 51 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 52 | /* The tid of the thread currently running, or VG_INVALID_THREADID if |
| 53 | none. */ |
| 54 | static ThreadId vg_tid_currently_running = VG_INVALID_THREADID; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 55 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 56 | |
| 57 | /* vg_oursignalhandler() might longjmp(). Here's the jmp_buf. */ |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 58 | static jmp_buf scheduler_jmpbuf; |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 59 | /* This says whether scheduler_jmpbuf is actually valid. Needed so |
| 60 | that our signal handler doesn't longjmp when the buffer isn't |
| 61 | actually valid. */ |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 62 | static Bool scheduler_jmpbuf_valid = False; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 63 | /* ... and if so, here's the signal which caused it to do so. */ |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 64 | static Int longjmpd_on_signal; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 65 | /* If the current thread gets a syncronous unresumable signal, then |
| 66 | its details are placed here by the signal handler, to be passed to |
| 67 | the applications signal handler later on. */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 68 | static vki_siginfo_t unresumable_siginfo; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 69 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 70 | /* If != VG_INVALID_THREADID, this is the preferred tid to schedule */ |
| 71 | static ThreadId prefer_sched = VG_INVALID_THREADID; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 72 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 73 | /* Keeping track of keys. */ |
| 74 | typedef |
| 75 | struct { |
| 76 | /* Has this key been allocated ? */ |
| 77 | Bool inuse; |
| 78 | /* If .inuse==True, records the address of the associated |
| 79 | destructor, or NULL if none. */ |
| 80 | void (*destructor)(void*); |
| 81 | } |
| 82 | ThreadKeyState; |
| 83 | |
| 84 | /* And our array of thread keys. */ |
| 85 | static ThreadKeyState vg_thread_keys[VG_N_THREAD_KEYS]; |
| 86 | |
| 87 | typedef UInt ThreadKey; |
| 88 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 89 | /* The scheduler does need to know the address of it so it can be |
| 90 | called at program exit. */ |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 91 | static Addr __libc_freeres_wrapper; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 92 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 93 | /* Forwards */ |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 94 | static void do_client_request ( ThreadId tid, UWord* args ); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 95 | static void scheduler_sanity ( void ); |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 96 | static void do_pthread_mutex_timedlock_TIMEOUT ( ThreadId tid ); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 97 | static void do_pthread_cond_timedwait_TIMEOUT ( ThreadId tid ); |
thughes | a3afffc | 2004-08-25 18:58:04 +0000 | [diff] [blame] | 98 | static void maybe_rendezvous_joiners_and_joinees ( void ); |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 99 | |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 100 | /* Stats. */ |
| 101 | static UInt n_scheduling_events_MINOR = 0; |
| 102 | static UInt n_scheduling_events_MAJOR = 0; |
| 103 | |
| 104 | void VG_(print_scheduler_stats)(void) |
| 105 | { |
| 106 | VG_(message)(Vg_DebugMsg, |
| 107 | " %d/%d major/minor sched events.", |
| 108 | n_scheduling_events_MAJOR, n_scheduling_events_MINOR); |
| 109 | } |
| 110 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 111 | /* --------------------------------------------------------------------- |
| 112 | Helper functions for the scheduler. |
| 113 | ------------------------------------------------------------------ */ |
| 114 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 115 | __inline__ |
| 116 | Bool VG_(is_valid_tid) ( ThreadId tid ) |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 117 | { |
| 118 | /* tid is unsigned, hence no < 0 test. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 119 | if (tid == 0) return False; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 120 | if (tid >= VG_N_THREADS) return False; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 121 | if (VG_(threads)[tid].status == VgTs_Empty) return False; |
| 122 | return True; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | __inline__ |
nethercote | 36881a2 | 2004-08-04 14:03:16 +0000 | [diff] [blame] | 127 | Bool is_valid_or_empty_tid ( ThreadId tid ) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 128 | { |
| 129 | /* tid is unsigned, hence no < 0 test. */ |
| 130 | if (tid == 0) return False; |
| 131 | if (tid >= VG_N_THREADS) return False; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 132 | return True; |
| 133 | } |
| 134 | |
| 135 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 136 | /* For constructing error messages only: try and identify a thread |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 137 | whose stack satisfies the predicate p, or return VG_INVALID_THREADID |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 138 | if none do. |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 139 | */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 140 | ThreadId VG_(first_matching_thread_stack) |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 141 | ( Bool (*p) ( Addr stack_min, Addr stack_max, void* d ), |
| 142 | void* d ) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 143 | { |
| 144 | ThreadId tid, tid_to_skip; |
| 145 | |
| 146 | tid_to_skip = VG_INVALID_THREADID; |
| 147 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 148 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 149 | if (VG_(threads)[tid].status == VgTs_Empty) continue; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 150 | if (tid == tid_to_skip) continue; |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 151 | if ( p ( STACK_PTR(VG_(threads)[tid].arch), |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 152 | VG_(threads)[tid].stack_highest_word, d ) ) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 153 | return tid; |
| 154 | } |
| 155 | return VG_INVALID_THREADID; |
| 156 | } |
| 157 | |
| 158 | |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 159 | /* Print the scheduler status. */ |
| 160 | void VG_(pp_sched_status) ( void ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 161 | { |
| 162 | Int i; |
| 163 | VG_(printf)("\nsched status:\n"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 164 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 165 | if (VG_(threads)[i].status == VgTs_Empty) continue; |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 166 | VG_(printf)("\nThread %d: status = ", i); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 167 | switch (VG_(threads)[i].status) { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 168 | case VgTs_Runnable: VG_(printf)("Runnable"); break; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 169 | case VgTs_WaitJoinee: VG_(printf)("WaitJoinee(%d)", |
| 170 | VG_(threads)[i].joiner_jee_tid); |
| 171 | break; |
| 172 | case VgTs_WaitJoiner: VG_(printf)("WaitJoiner"); break; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 173 | case VgTs_Sleeping: VG_(printf)("Sleeping"); break; |
| 174 | case VgTs_WaitMX: VG_(printf)("WaitMX"); break; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 175 | case VgTs_WaitCV: VG_(printf)("WaitCV"); break; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 176 | case VgTs_WaitSys: VG_(printf)("WaitSys"); break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 177 | default: VG_(printf)("???"); break; |
| 178 | } |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 179 | VG_(printf)(", associated_mx = %p, associated_cv = %p\n", |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 180 | VG_(threads)[i].associated_mx, |
| 181 | VG_(threads)[i].associated_cv ); |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 182 | VG_(pp_ExeContext)( |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 183 | VG_(get_ExeContext2)( INSTR_PTR(VG_(threads)[i].arch), |
| 184 | FRAME_PTR(VG_(threads)[i].arch), |
| 185 | STACK_PTR(VG_(threads)[i].arch), |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 186 | VG_(threads)[i].stack_highest_word) |
| 187 | ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 188 | } |
| 189 | VG_(printf)("\n"); |
| 190 | } |
| 191 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 192 | static |
| 193 | void print_sched_event ( ThreadId tid, Char* what ) |
| 194 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 195 | VG_(message)(Vg_DebugMsg, " SCHED[%d]: %s", tid, what ); |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 196 | } |
| 197 | |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 198 | static |
| 199 | void print_pthread_event ( ThreadId tid, Char* what ) |
| 200 | { |
| 201 | VG_(message)(Vg_DebugMsg, "PTHREAD[%d]: %s", tid, what ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 202 | } |
| 203 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 204 | static |
| 205 | Char* name_of_sched_event ( UInt event ) |
| 206 | { |
| 207 | switch (event) { |
sewardj | d79ef68 | 2004-11-26 13:25:17 +0000 | [diff] [blame^] | 208 | case VEX_TRC_JMP_SYSCALL: return "SYSCALL"; |
| 209 | case VEX_TRC_JMP_CLIENTREQ: return "CLIENTREQ"; |
| 210 | case VEX_TRC_JMP_YIELD: return "YIELD"; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 211 | case VG_TRC_INNER_COUNTERZERO: return "COUNTERZERO"; |
| 212 | case VG_TRC_INNER_FASTMISS: return "FASTMISS"; |
| 213 | case VG_TRC_UNRESUMABLE_SIGNAL: return "FATALSIGNAL"; |
| 214 | default: return "??UNKNOWN??"; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 219 | /* Allocate a completely empty ThreadState record. */ |
| 220 | static |
| 221 | ThreadId vg_alloc_ThreadState ( void ) |
| 222 | { |
| 223 | Int i; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 224 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 225 | if (VG_(threads)[i].status == VgTs_Empty) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 226 | return i; |
| 227 | } |
| 228 | VG_(printf)("vg_alloc_ThreadState: no free slots available\n"); |
| 229 | VG_(printf)("Increase VG_N_THREADS, rebuild and try again.\n"); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 230 | VG_(core_panic)("VG_N_THREADS is too low"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 231 | /*NOTREACHED*/ |
| 232 | } |
| 233 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 234 | ThreadState *VG_(get_ThreadState)(ThreadId tid) |
| 235 | { |
| 236 | vg_assert(tid >= 0 && tid < VG_N_THREADS); |
| 237 | return &VG_(threads)[tid]; |
| 238 | } |
| 239 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 240 | /* Return True precisely when get_current_tid can return |
| 241 | successfully. */ |
| 242 | Bool VG_(running_a_thread) ( void ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 243 | { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 244 | if (vg_tid_currently_running == VG_INVALID_THREADID) |
| 245 | return False; |
| 246 | /* Otherwise, it must be a valid thread ID. */ |
| 247 | vg_assert(VG_(is_valid_tid)(vg_tid_currently_running)); |
| 248 | return True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 249 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 250 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 251 | ThreadId VG_(get_current_tid) ( void ) |
| 252 | { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 253 | if (vg_tid_currently_running == VG_INVALID_THREADID) |
| 254 | VG_(core_panic)("VG_(get_current_tid): not running generated code"); |
| 255 | /* Otherwise, it must be a valid thread ID. */ |
| 256 | vg_assert(VG_(is_valid_tid)(vg_tid_currently_running)); |
| 257 | return vg_tid_currently_running; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 258 | } |
| 259 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 260 | void VG_(resume_scheduler)(Int sigNo, vki_siginfo_t *info) |
nethercote | 75d2624 | 2004-08-01 22:59:18 +0000 | [diff] [blame] | 261 | { |
| 262 | if (scheduler_jmpbuf_valid) { |
| 263 | /* Can't continue; must longjmp back to the scheduler and thus |
| 264 | enter the sighandler immediately. */ |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 265 | vg_assert(vg_tid_currently_running != VG_INVALID_THREADID); |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 266 | VG_(memcpy)(&unresumable_siginfo, info, sizeof(vki_siginfo_t)); |
nethercote | 75d2624 | 2004-08-01 22:59:18 +0000 | [diff] [blame] | 267 | |
| 268 | longjmpd_on_signal = sigNo; |
| 269 | __builtin_longjmp(scheduler_jmpbuf,1); |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 270 | } else { |
| 271 | vg_assert(vg_tid_currently_running == VG_INVALID_THREADID); |
nethercote | 75d2624 | 2004-08-01 22:59:18 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 275 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 276 | static |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 277 | UInt run_thread_for_a_while ( ThreadId tid ) |
| 278 | { |
sewardj | 7ccc5c2 | 2002-04-24 21:39:11 +0000 | [diff] [blame] | 279 | volatile UInt trc = 0; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 280 | volatile Int dispatch_ctr_SAVED = VG_(dispatch_ctr); |
| 281 | volatile Int done_this_time; |
| 282 | |
sewardj | 873b313 | 2004-11-25 22:50:17 +0000 | [diff] [blame] | 283 | /* For paranoia purposes only */ |
| 284 | volatile Addr a_vex = (Addr) & VG_(threads)[tid].arch.vex; |
| 285 | volatile Addr a_vexsh = (Addr) & VG_(threads)[tid].arch.vex_shadow; |
| 286 | volatile Addr a_spill = (Addr) & VG_(threads)[tid].arch.vex_spill; |
| 287 | volatile UInt sz_vex = (UInt) sizeof VG_(threads)[tid].arch.vex; |
| 288 | volatile UInt sz_vexsh = (UInt) sizeof VG_(threads)[tid].arch.vex_shadow; |
| 289 | volatile UInt sz_spill = (UInt) sizeof VG_(threads)[tid].arch.vex_spill; |
| 290 | |
| 291 | /* Paranoia */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 292 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 293 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
nethercote | 75d2624 | 2004-08-01 22:59:18 +0000 | [diff] [blame] | 294 | vg_assert(!scheduler_jmpbuf_valid); |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 295 | vg_assert(vg_tid_currently_running == VG_INVALID_THREADID); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 296 | |
sewardj | 873b313 | 2004-11-25 22:50:17 +0000 | [diff] [blame] | 297 | /* Even more paranoia. Check that what we have matches |
| 298 | Vex's guest state layout requirements. */ |
| 299 | |
sewardj | 12a74b5 | 2004-11-26 11:57:41 +0000 | [diff] [blame] | 300 | # define IS_4_ALIGNED(_xx) (0 == ((_xx) & 3)) |
| 301 | # define IS_8_ALIGNED(_xx) (0 == ((_xx) & 7)) |
| 302 | # define IS_16_ALIGNED(_xx) (0 == ((_xx) & 0xF)) |
| 303 | |
| 304 | if (0) |
| 305 | VG_(printf)("%p %d %p %d %p %d\n", |
| 306 | (void*)a_vex, sz_vex, (void*)a_vexsh, sz_vexsh, |
| 307 | (void*)a_spill, sz_spill ); |
sewardj | 873b313 | 2004-11-25 22:50:17 +0000 | [diff] [blame] | 308 | |
| 309 | vg_assert(IS_8_ALIGNED(sz_vex)); |
| 310 | vg_assert(IS_8_ALIGNED(sz_vexsh)); |
sewardj | 12a74b5 | 2004-11-26 11:57:41 +0000 | [diff] [blame] | 311 | vg_assert(IS_16_ALIGNED(sz_spill)); |
| 312 | |
| 313 | vg_assert(IS_4_ALIGNED(a_vex)); |
| 314 | vg_assert(IS_4_ALIGNED(a_vexsh)); |
| 315 | vg_assert(IS_4_ALIGNED(a_spill)); |
sewardj | 873b313 | 2004-11-25 22:50:17 +0000 | [diff] [blame] | 316 | |
| 317 | vg_assert(sz_vex == sz_vexsh); |
| 318 | vg_assert(a_vex + sz_vex == a_vexsh); |
| 319 | |
| 320 | vg_assert(sz_spill == LibVEX_N_SPILL_BYTES); |
| 321 | vg_assert(a_vex + 2 * sz_vex == a_spill); |
| 322 | |
sewardj | 12a74b5 | 2004-11-26 11:57:41 +0000 | [diff] [blame] | 323 | # undef IS_4_ALIGNED |
sewardj | 873b313 | 2004-11-25 22:50:17 +0000 | [diff] [blame] | 324 | # undef IS_8_ALIGNED |
sewardj | 12a74b5 | 2004-11-26 11:57:41 +0000 | [diff] [blame] | 325 | # undef IS_16_ALIGNED |
sewardj | 873b313 | 2004-11-25 22:50:17 +0000 | [diff] [blame] | 326 | |
sewardj | 671ff54 | 2002-05-07 09:25:30 +0000 | [diff] [blame] | 327 | VGP_PUSHCC(VgpRun); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 328 | |
| 329 | /* there should be no undealt-with signals */ |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 330 | vg_assert(unresumable_siginfo.si_signo == 0); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 331 | |
nethercote | 75d2624 | 2004-08-01 22:59:18 +0000 | [diff] [blame] | 332 | if (__builtin_setjmp(scheduler_jmpbuf) == 0) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 333 | /* try this ... */ |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 334 | vg_tid_currently_running = tid; |
| 335 | scheduler_jmpbuf_valid = True; |
| 336 | trc = VG_(run_innerloop)( &VG_(threads)[tid].arch.vex ); |
| 337 | scheduler_jmpbuf_valid = False; |
| 338 | vg_tid_currently_running = VG_INVALID_THREADID; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 339 | /* We get here if the client didn't take a fault. */ |
| 340 | } else { |
| 341 | /* We get here if the client took a fault, which caused our |
| 342 | signal handler to longjmp. */ |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 343 | scheduler_jmpbuf_valid = False; |
| 344 | vg_tid_currently_running = VG_INVALID_THREADID; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 345 | vg_assert(trc == 0); |
| 346 | trc = VG_TRC_UNRESUMABLE_SIGNAL; |
| 347 | } |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 348 | |
nethercote | 75d2624 | 2004-08-01 22:59:18 +0000 | [diff] [blame] | 349 | vg_assert(!scheduler_jmpbuf_valid); |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 350 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 351 | done_this_time = (Int)dispatch_ctr_SAVED - (Int)VG_(dispatch_ctr) - 0; |
| 352 | |
| 353 | vg_assert(done_this_time >= 0); |
| 354 | VG_(bbs_done) += (ULong)done_this_time; |
| 355 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 356 | VGP_POPCC(VgpRun); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 357 | return trc; |
| 358 | } |
| 359 | |
| 360 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 361 | static |
| 362 | void mostly_clear_thread_record ( ThreadId tid ) |
| 363 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 364 | vg_assert(tid >= 0 && tid < VG_N_THREADS); |
nethercote | f9b5941 | 2004-09-10 15:33:32 +0000 | [diff] [blame] | 365 | VGA_(clear_thread)(&VG_(threads)[tid].arch); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 366 | VG_(threads)[tid].tid = tid; |
| 367 | VG_(threads)[tid].status = VgTs_Empty; |
| 368 | VG_(threads)[tid].associated_mx = NULL; |
| 369 | VG_(threads)[tid].associated_cv = NULL; |
| 370 | VG_(threads)[tid].awaken_at = 0; |
| 371 | VG_(threads)[tid].joinee_retval = NULL; |
| 372 | VG_(threads)[tid].joiner_thread_return = NULL; |
| 373 | VG_(threads)[tid].joiner_jee_tid = VG_INVALID_THREADID; |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 374 | VG_(threads)[tid].detached = False; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 375 | VG_(threads)[tid].cancel_st = True; /* PTHREAD_CANCEL_ENABLE */ |
| 376 | VG_(threads)[tid].cancel_ty = True; /* PTHREAD_CANCEL_DEFERRED */ |
| 377 | VG_(threads)[tid].cancel_pend = NULL; /* not pending */ |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 378 | VG_(threads)[tid].custack_used = 0; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 379 | VG_(sigemptyset)(&VG_(threads)[tid].sig_mask); |
| 380 | VG_(sigfillset)(&VG_(threads)[tid].eff_sig_mask); |
thughes | 8abf392 | 2004-10-16 10:59:49 +0000 | [diff] [blame] | 381 | VG_(threads)[tid].sigqueue_head = 0; |
| 382 | VG_(threads)[tid].sigqueue_tail = 0; |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 383 | VG_(threads)[tid].specifics_ptr = NULL; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 384 | |
| 385 | VG_(threads)[tid].syscallno = -1; |
thughes | baa46e5 | 2004-07-29 17:44:23 +0000 | [diff] [blame] | 386 | VG_(threads)[tid].sys_flags = 0; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 387 | |
| 388 | VG_(threads)[tid].proxy = NULL; |
fitzhardinge | 2842859 | 2004-03-16 22:07:12 +0000 | [diff] [blame] | 389 | |
| 390 | /* start with no altstack */ |
| 391 | VG_(threads)[tid].altstack.ss_sp = (void *)0xdeadbeef; |
| 392 | VG_(threads)[tid].altstack.ss_size = 0; |
| 393 | VG_(threads)[tid].altstack.ss_flags = VKI_SS_DISABLE; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 397 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 398 | /* Initialise the scheduler. Create a single "main" thread ready to |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 399 | run, with special ThreadId of one. This is called at startup. The |
| 400 | caller subsequently initialises the guest state components of |
| 401 | this main thread, thread 1. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 402 | */ |
| 403 | void VG_(scheduler_init) ( void ) |
| 404 | { |
thughes | c37184f | 2004-09-11 14:16:57 +0000 | [diff] [blame] | 405 | Int i; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 406 | ThreadId tid_main; |
| 407 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 408 | for (i = 0 /* NB; not 1 */; i < VG_N_THREADS; i++) { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 409 | mostly_clear_thread_record(i); |
| 410 | VG_(threads)[i].stack_size = 0; |
| 411 | VG_(threads)[i].stack_base = (Addr)NULL; |
thughes | daa3456 | 2004-06-27 12:48:53 +0000 | [diff] [blame] | 412 | VG_(threads)[i].stack_guard_size = 0; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 413 | VG_(threads)[i].stack_highest_word = (Addr)NULL; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 414 | } |
| 415 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 416 | for (i = 0; i < VG_N_THREAD_KEYS; i++) { |
| 417 | vg_thread_keys[i].inuse = False; |
| 418 | vg_thread_keys[i].destructor = NULL; |
| 419 | } |
| 420 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 421 | vg_fhstack_used = 0; |
| 422 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 423 | /* Assert this is thread one, which has certain magic |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 424 | properties. */ |
| 425 | tid_main = vg_alloc_ThreadState(); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 426 | vg_assert(tid_main == 1); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 427 | VG_(threads)[tid_main].status = VgTs_Runnable; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 428 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 429 | VG_(threads)[tid_main].stack_highest_word = VG_(clstk_end) - 4; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 430 | VG_(threads)[tid_main].stack_base = VG_(clstk_base); |
thughes | c37184f | 2004-09-11 14:16:57 +0000 | [diff] [blame] | 431 | VG_(threads)[tid_main].stack_size = VG_(client_rlimit_stack).rlim_cur; |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 432 | |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 433 | /* Not running client code right now. */ |
nethercote | 75d2624 | 2004-08-01 22:59:18 +0000 | [diff] [blame] | 434 | scheduler_jmpbuf_valid = False; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 435 | |
| 436 | /* Proxy for main thread */ |
| 437 | VG_(proxy_create)(tid_main); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | |
sewardj | 3947e62 | 2002-05-23 16:52:11 +0000 | [diff] [blame] | 441 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 442 | /* vthread tid is returning from a signal handler; modify its |
| 443 | stack/regs accordingly. */ |
| 444 | static |
| 445 | void handle_signal_return ( ThreadId tid ) |
| 446 | { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 447 | Bool restart_blocked_syscalls; |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 448 | struct vki_timespec * rem; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 449 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 450 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 451 | |
| 452 | restart_blocked_syscalls = VG_(signal_returns)(tid); |
| 453 | |
thughes | a3afffc | 2004-08-25 18:58:04 +0000 | [diff] [blame] | 454 | /* If we were interrupted in the middle of a rendezvous |
| 455 | then check the rendezvous hasn't completed while we |
| 456 | were busy handling the signal. */ |
| 457 | if (VG_(threads)[tid].status == VgTs_WaitJoiner || |
| 458 | VG_(threads)[tid].status == VgTs_WaitJoinee ) { |
| 459 | maybe_rendezvous_joiners_and_joinees(); |
| 460 | } |
| 461 | |
thughes | c41c6f4 | 2004-10-16 16:50:14 +0000 | [diff] [blame] | 462 | /* If we were interrupted while waiting on a mutex then check that |
| 463 | it hasn't been unlocked while we were busy handling the signal. */ |
| 464 | if (VG_(threads)[tid].status == VgTs_WaitMX && |
| 465 | VG_(threads)[tid].associated_mx->__vg_m_count == 0) { |
| 466 | vg_pthread_mutex_t* mutex = VG_(threads)[tid].associated_mx; |
| 467 | mutex->__vg_m_count = 1; |
| 468 | mutex->__vg_m_owner = (/*_pthread_descr*/void*)tid; |
| 469 | VG_(threads)[tid].status = VgTs_Runnable; |
| 470 | VG_(threads)[tid].associated_mx = NULL; |
| 471 | /* m_edx already holds pth_mx_lock() success (0) */ |
| 472 | } |
| 473 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 474 | if (restart_blocked_syscalls) |
| 475 | /* Easy; we don't have to do anything. */ |
| 476 | return; |
| 477 | |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 478 | if (VG_(threads)[tid].status == VgTs_Sleeping |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 479 | && SYSCALL_NUM(VG_(threads)[tid].arch) == __NR_nanosleep) { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 480 | /* We interrupted a nanosleep(). The right thing to do is to |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 481 | write the unused time to nanosleep's second param, but that's |
| 482 | too much effort ... we just say that 1 nanosecond was not |
| 483 | used, and return EINTR. */ |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 484 | rem = (struct vki_timespec*)SYSCALL_ARG2(VG_(threads)[tid].arch); |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 485 | if (rem != NULL) { |
| 486 | rem->tv_sec = 0; |
| 487 | rem->tv_nsec = 1; |
| 488 | } |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 489 | SET_SYSCALL_RETVAL(tid, -VKI_EINTR); |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 490 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 491 | return; |
| 492 | } |
| 493 | |
| 494 | /* All other cases? Just return. */ |
| 495 | } |
| 496 | |
| 497 | |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 498 | struct timeout { |
| 499 | UInt time; /* time we should awaken */ |
| 500 | ThreadId tid; /* thread which cares about this timeout */ |
| 501 | struct timeout *next; |
| 502 | }; |
| 503 | |
| 504 | static struct timeout *timeouts; |
| 505 | |
| 506 | static void add_timeout(ThreadId tid, UInt time) |
| 507 | { |
| 508 | struct timeout *t = VG_(arena_malloc)(VG_AR_CORE, sizeof(*t)); |
| 509 | struct timeout **prev, *tp; |
| 510 | |
| 511 | t->time = time; |
| 512 | t->tid = tid; |
| 513 | |
| 514 | if (VG_(clo_trace_sched)) { |
| 515 | Char msg_buf[100]; |
| 516 | VG_(sprintf)(msg_buf, "add_timeout: now=%u adding timeout at %u", |
| 517 | VG_(read_millisecond_timer)(), time); |
| 518 | print_sched_event(tid, msg_buf); |
| 519 | } |
| 520 | |
| 521 | for(tp = timeouts, prev = &timeouts; |
| 522 | tp != NULL && tp->time < time; |
| 523 | prev = &tp->next, tp = tp->next) |
| 524 | ; |
| 525 | t->next = tp; |
| 526 | *prev = t; |
| 527 | } |
| 528 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 529 | static |
| 530 | void sched_do_syscall ( ThreadId tid ) |
| 531 | { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 532 | Int syscall_no; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 533 | Char msg_buf[100]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 534 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 535 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 536 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 537 | |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 538 | syscall_no = SYSCALL_NUM(VG_(threads)[tid].arch); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 539 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 540 | /* Special-case nanosleep because we can. But should we? |
| 541 | |
| 542 | XXX not doing so for now, because it doesn't seem to work |
| 543 | properly, and we can use the syscall nanosleep just as easily. |
| 544 | */ |
| 545 | if (0 && syscall_no == __NR_nanosleep) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 546 | UInt t_now, t_awaken; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 547 | struct vki_timespec* req; |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 548 | req = (struct vki_timespec*)SYSCALL_ARG1(VG_(threads)[tid].arch); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 549 | |
| 550 | if (req->tv_sec < 0 || req->tv_nsec < 0 || req->tv_nsec >= 1000000000) { |
| 551 | SET_SYSCALL_RETVAL(tid, -VKI_EINVAL); |
| 552 | return; |
| 553 | } |
| 554 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 555 | t_now = VG_(read_millisecond_timer)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 556 | t_awaken |
| 557 | = t_now |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 558 | + (UInt)1000ULL * (UInt)(req->tv_sec) |
| 559 | + (UInt)(req->tv_nsec) / 1000000; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 560 | VG_(threads)[tid].status = VgTs_Sleeping; |
| 561 | VG_(threads)[tid].awaken_at = t_awaken; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 562 | if (VG_(clo_trace_sched)) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 563 | VG_(sprintf)(msg_buf, "at %d: nanosleep for %d", |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 564 | t_now, t_awaken-t_now); |
| 565 | print_sched_event(tid, msg_buf); |
| 566 | } |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 567 | add_timeout(tid, t_awaken); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 568 | /* Force the scheduler to run something else for a while. */ |
| 569 | return; |
| 570 | } |
| 571 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 572 | /* If pre_syscall returns true, then we're done immediately */ |
| 573 | if (VG_(pre_syscall)(tid)) { |
fitzhardinge | 31ba905 | 2004-01-16 02:15:23 +0000 | [diff] [blame] | 574 | VG_(post_syscall(tid, True)); |
sewardj | 3947e62 | 2002-05-23 16:52:11 +0000 | [diff] [blame] | 575 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 576 | } else { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 577 | vg_assert(VG_(threads)[tid].status == VgTs_WaitSys); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
| 581 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 582 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 583 | /* Sleep for a while, but be willing to be woken. */ |
| 584 | static |
| 585 | void idle ( void ) |
| 586 | { |
| 587 | struct vki_pollfd pollfd[1]; |
| 588 | Int delta = -1; |
| 589 | Int fd = VG_(proxy_resfd)(); |
| 590 | |
| 591 | pollfd[0].fd = fd; |
| 592 | pollfd[0].events = VKI_POLLIN; |
| 593 | |
| 594 | /* Look though the nearest timeouts, looking for the next future |
| 595 | one (there may be stale past timeouts). They'll all be mopped |
| 596 | below up when the poll() finishes. */ |
| 597 | if (timeouts != NULL) { |
| 598 | struct timeout *tp; |
| 599 | Bool wicked = False; |
| 600 | UInt now = VG_(read_millisecond_timer)(); |
| 601 | |
| 602 | for(tp = timeouts; tp != NULL && tp->time < now; tp = tp->next) { |
| 603 | /* If a thread is still sleeping in the past, make it runnable */ |
| 604 | ThreadState *tst = VG_(get_ThreadState)(tp->tid); |
| 605 | if (tst->status == VgTs_Sleeping) |
| 606 | tst->status = VgTs_Runnable; |
| 607 | wicked = True; /* no sleep for the wicked */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 608 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 609 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 610 | if (tp != NULL) { |
thughes | e761bef | 2004-10-17 15:18:22 +0000 | [diff] [blame] | 611 | vg_assert(tp->time >= now); |
| 612 | /* limit the signed int delta to INT_MAX */ |
| 613 | if ((tp->time - now) <= 0x7FFFFFFFU) { |
| 614 | delta = tp->time - now; |
| 615 | } else { |
| 616 | delta = 0x7FFFFFFF; |
| 617 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 618 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 619 | if (wicked) |
| 620 | delta = 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 621 | } |
| 622 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 623 | /* gotta wake up for something! */ |
| 624 | vg_assert(fd != -1 || delta != -1); |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 625 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 626 | /* If we need to do signal routing, then poll for pending signals |
| 627 | every VG_(clo_signal_polltime) mS */ |
| 628 | if (VG_(do_signal_routing) && (delta > VG_(clo_signal_polltime) || delta == -1)) |
| 629 | delta = VG_(clo_signal_polltime); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 630 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 631 | if (VG_(clo_trace_sched)) { |
| 632 | Char msg_buf[100]; |
| 633 | VG_(sprintf)(msg_buf, "idle: waiting for %dms and fd %d", |
| 634 | delta, fd); |
| 635 | print_sched_event(0, msg_buf); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 636 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 637 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 638 | VG_(poll)(pollfd, fd != -1 ? 1 : 0, delta); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 639 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 640 | /* See if there's anything on the timeout list which needs |
| 641 | waking, and mop up anything in the past. */ |
| 642 | { |
| 643 | UInt now = VG_(read_millisecond_timer)(); |
| 644 | struct timeout *tp; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 645 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 646 | tp = timeouts; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 647 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 648 | while(tp && tp->time <= now) { |
| 649 | struct timeout *dead; |
| 650 | ThreadState *tst; |
| 651 | |
| 652 | tst = VG_(get_ThreadState)(tp->tid); |
| 653 | |
| 654 | if (VG_(clo_trace_sched)) { |
| 655 | Char msg_buf[100]; |
| 656 | VG_(sprintf)(msg_buf, "idle: now=%u removing timeout at %u", |
| 657 | now, tp->time); |
| 658 | print_sched_event(tp->tid, msg_buf); |
| 659 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 660 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 661 | /* If awaken_at != tp->time then it means the timeout is |
| 662 | stale and we should just ignore it. */ |
| 663 | if(tst->awaken_at == tp->time) { |
| 664 | switch(tst->status) { |
| 665 | case VgTs_Sleeping: |
| 666 | tst->awaken_at = 0xFFFFFFFF; |
| 667 | tst->status = VgTs_Runnable; |
| 668 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 669 | |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 670 | case VgTs_WaitMX: |
| 671 | do_pthread_mutex_timedlock_TIMEOUT(tst->tid); |
| 672 | break; |
| 673 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 674 | case VgTs_WaitCV: |
| 675 | do_pthread_cond_timedwait_TIMEOUT(tst->tid); |
| 676 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 677 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 678 | default: |
| 679 | /* This is a bit odd but OK; if a thread had a timeout |
| 680 | but woke for some other reason (signal, condvar |
| 681 | wakeup), then it will still be on the list. */ |
| 682 | if (0) |
| 683 | VG_(printf)("idle(): unexpected status tp->tid=%d tst->status = %d\n", |
| 684 | tp->tid, tst->status); |
| 685 | break; |
| 686 | } |
| 687 | } |
sewardj | bc7d878 | 2002-06-30 12:44:54 +0000 | [diff] [blame] | 688 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 689 | dead = tp; |
| 690 | tp = tp->next; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 691 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 692 | VG_(arena_free)(VG_AR_CORE, dead); |
sewardj | bc7d878 | 2002-06-30 12:44:54 +0000 | [diff] [blame] | 693 | } |
| 694 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 695 | timeouts = tp; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 700 | /* --------------------------------------------------------------------- |
| 701 | The scheduler proper. |
| 702 | ------------------------------------------------------------------ */ |
| 703 | |
nethercote | 238a3c3 | 2004-08-09 13:13:31 +0000 | [diff] [blame] | 704 | // For handling of the default action of a fatal signal. |
| 705 | // jmp_buf for fatal signals; VG_(fatal_signal_jmpbuf_ptr) is NULL until |
| 706 | // the time is right that it can be used. |
| 707 | static jmp_buf fatal_signal_jmpbuf; |
| 708 | static jmp_buf* fatal_signal_jmpbuf_ptr; |
| 709 | static Int fatal_sigNo; // the fatal signal, if it happens |
| 710 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 711 | /* Run user-space threads until either |
| 712 | * Deadlock occurs |
| 713 | * One thread asks to shutdown Valgrind |
| 714 | * The specified number of basic blocks has gone by. |
| 715 | */ |
nethercote | 238a3c3 | 2004-08-09 13:13:31 +0000 | [diff] [blame] | 716 | VgSchedReturnCode do_scheduler ( Int* exitcode, ThreadId* last_run_tid ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 717 | { |
| 718 | ThreadId tid, tid_next; |
| 719 | UInt trc; |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 720 | Int done_this_time, n_in_bounded_wait; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 721 | Int n_exists, n_waiting_for_reaper; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 722 | Addr trans_addr; |
| 723 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 724 | /* Start with the root thread. tid in general indicates the |
| 725 | currently runnable/just-finished-running thread. */ |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 726 | *last_run_tid = tid = 1; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 727 | |
| 728 | /* This is the top level scheduler loop. It falls into three |
| 729 | phases. */ |
| 730 | while (True) { |
| 731 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 732 | /* ======================= Phase 0 of 3 ======================= |
| 733 | Be paranoid. Always a good idea. */ |
sewardj | d7fd4d2 | 2002-04-24 01:57:27 +0000 | [diff] [blame] | 734 | stage1: |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 735 | scheduler_sanity(); |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 736 | VG_(sanity_check_general)( False ); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 737 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 738 | /* ======================= Phase 1 of 3 ======================= |
| 739 | Handle I/O completions and signals. This may change the |
| 740 | status of various threads. Then select a new thread to run, |
| 741 | or declare deadlock, or sleep if there are no runnable |
| 742 | threads but some are blocked on I/O. */ |
| 743 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 744 | /* Do the following loop until a runnable thread is found, or |
| 745 | deadlock is detected. */ |
| 746 | while (True) { |
| 747 | |
| 748 | /* For stats purposes only. */ |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 749 | n_scheduling_events_MAJOR++; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 750 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 751 | /* Route signals to their proper places */ |
| 752 | VG_(route_signals)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 753 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 754 | /* See if any of the proxy LWPs report any activity: either a |
| 755 | syscall completing or a signal arriving. */ |
| 756 | VG_(proxy_results)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 757 | |
| 758 | /* Try and find a thread (tid) to run. */ |
| 759 | tid_next = tid; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 760 | if (prefer_sched != VG_INVALID_THREADID) { |
| 761 | tid_next = prefer_sched-1; |
| 762 | prefer_sched = VG_INVALID_THREADID; |
| 763 | } |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 764 | n_in_bounded_wait = 0; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 765 | n_exists = 0; |
| 766 | n_waiting_for_reaper = 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 767 | while (True) { |
| 768 | tid_next++; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 769 | if (tid_next >= VG_N_THREADS) tid_next = 1; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 770 | if (VG_(threads)[tid_next].status == VgTs_Sleeping |
| 771 | || VG_(threads)[tid_next].status == VgTs_WaitSys |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 772 | || (VG_(threads)[tid_next].status == VgTs_WaitMX |
| 773 | && VG_(threads)[tid_next].awaken_at != 0xFFFFFFFF) |
| 774 | || (VG_(threads)[tid_next].status == VgTs_WaitCV |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 775 | && VG_(threads)[tid_next].awaken_at != 0xFFFFFFFF)) |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 776 | n_in_bounded_wait ++; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 777 | if (VG_(threads)[tid_next].status != VgTs_Empty) |
| 778 | n_exists++; |
| 779 | if (VG_(threads)[tid_next].status == VgTs_WaitJoiner) |
| 780 | n_waiting_for_reaper++; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 781 | if (VG_(threads)[tid_next].status == VgTs_Runnable) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 782 | break; /* We can run this one. */ |
| 783 | if (tid_next == tid) |
| 784 | break; /* been all the way round */ |
| 785 | } |
| 786 | tid = tid_next; |
| 787 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 788 | if (VG_(threads)[tid].status == VgTs_Runnable) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 789 | /* Found a suitable candidate. Fall out of this loop, so |
| 790 | we can advance to stage 2 of the scheduler: actually |
| 791 | running the thread. */ |
| 792 | break; |
| 793 | } |
| 794 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 795 | /* All threads have exited - pretend someone called exit() */ |
| 796 | if (n_waiting_for_reaper == n_exists) { |
nethercote | 47dd12c | 2004-06-22 14:18:42 +0000 | [diff] [blame] | 797 | *exitcode = 0; /* ? */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 798 | return VgSrc_ExitSyscall; |
| 799 | } |
| 800 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 801 | /* We didn't find a runnable thread. Now what? */ |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 802 | if (n_in_bounded_wait == 0) { |
sewardj | 54cacf0 | 2002-04-12 23:24:59 +0000 | [diff] [blame] | 803 | /* No runnable threads and no prospect of any appearing |
| 804 | even if we wait for an arbitrary length of time. In |
| 805 | short, we have a deadlock. */ |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 806 | VG_(pp_sched_status)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 807 | return VgSrc_Deadlock; |
| 808 | } |
| 809 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 810 | /* Nothing needs doing, so sit in idle until either a timeout |
| 811 | happens or a thread's syscall completes. */ |
| 812 | idle(); |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 813 | /* pp_sched_status(); */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 814 | /* VG_(printf)("."); */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | |
| 818 | /* ======================= Phase 2 of 3 ======================= |
| 819 | Wahey! We've finally decided that thread tid is runnable, so |
| 820 | we now do that. Run it for as much of a quanta as possible. |
| 821 | Trivial requests are handled and the thread continues. The |
| 822 | aim is not to do too many of Phase 1 since it is expensive. */ |
| 823 | |
| 824 | if (0) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 825 | VG_(printf)("SCHED: tid %d\n", tid); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 826 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 827 | VG_TRACK( thread_run, tid ); |
| 828 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 829 | /* Figure out how many bbs to ask vg_run_innerloop to do. Note |
| 830 | that it decrements the counter before testing it for zero, so |
| 831 | that if VG_(dispatch_ctr) is set to N you get at most N-1 |
| 832 | iterations. Also this means that VG_(dispatch_ctr) must |
| 833 | exceed zero before entering the innerloop. Also also, the |
| 834 | decrement is done before the bb is actually run, so you |
| 835 | always get at least one decrement even if nothing happens. |
| 836 | */ |
nethercote | 1d44709 | 2004-02-01 17:29:59 +0000 | [diff] [blame] | 837 | VG_(dispatch_ctr) = VG_SCHEDULING_QUANTUM + 1; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 838 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 839 | /* paranoia ... */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 840 | vg_assert(VG_(threads)[tid].tid == tid); |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 841 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 842 | /* Actually run thread tid. */ |
| 843 | while (True) { |
| 844 | |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 845 | *last_run_tid = tid; |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 846 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 847 | /* For stats purposes only. */ |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 848 | n_scheduling_events_MINOR++; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 849 | |
| 850 | if (0) |
| 851 | VG_(message)(Vg_DebugMsg, "thread %d: running for %d bbs", |
| 852 | tid, VG_(dispatch_ctr) - 1 ); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 853 | # if 0 |
| 854 | if (VG_(bbs_done) > 31700000 + 0) { |
| 855 | dispatch_ctr_SAVED = VG_(dispatch_ctr) = 2; |
nethercote | b8ef9d8 | 2004-09-05 22:02:33 +0000 | [diff] [blame] | 856 | VG_(translate)(&VG_(threads)[tid], |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 857 | INSTR_PTR(VG_(threads)[tid].arch), |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 858 | /*debugging*/True); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 859 | } |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 860 | vg_assert(INSTR_PTR(VG_(threads)[tid].arch) != 0); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 861 | # endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 862 | |
| 863 | trc = run_thread_for_a_while ( tid ); |
| 864 | |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 865 | # if 0 |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 866 | if (0 == INSTR_PTR(VG_(threads)[tid].arch)) { |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 867 | VG_(printf)("tid = %d, dc = %llu\n", tid, VG_(bbs_done)); |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 868 | vg_assert(0 != INSTR_PTR(VG_(threads)[tid].arch)); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 869 | } |
| 870 | # endif |
| 871 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 872 | /* Deal quickly with trivial scheduling events, and resume the |
| 873 | thread. */ |
| 874 | |
| 875 | if (trc == VG_TRC_INNER_FASTMISS) { |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 876 | Addr ip = INSTR_PTR(VG_(threads)[tid].arch); |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 877 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 878 | vg_assert(VG_(dispatch_ctr) > 1); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 879 | |
| 880 | /* Trivial event. Miss in the fast-cache. Do a full |
| 881 | lookup for it. */ |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 882 | trans_addr = VG_(search_transtab)( ip ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 883 | if (trans_addr == (Addr)0) { |
| 884 | /* Not found; we need to request a translation. */ |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 885 | if (VG_(translate)( tid, ip, /*debug*/False )) { |
| 886 | trans_addr = VG_(search_transtab)( ip ); |
| 887 | if (trans_addr == (Addr)0) |
| 888 | VG_(core_panic)("VG_TRC_INNER_FASTMISS: missing tt_fast entry"); |
| 889 | } else { |
| 890 | // If VG_(translate)() fails, it's because it had to throw |
| 891 | // a signal because the client jumped to a bad address. |
| 892 | // This means VG_(deliver_signal)() will have been called |
| 893 | // by now, and the program counter will now be pointing to |
| 894 | // the start of the signal handler (if there is no |
| 895 | // handler, things would have been aborted by now), so do |
| 896 | // nothing, and things will work out next time around the |
| 897 | // scheduler loop. |
| 898 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 899 | } |
| 900 | continue; /* with this thread */ |
| 901 | } |
| 902 | |
sewardj | d79ef68 | 2004-11-26 13:25:17 +0000 | [diff] [blame^] | 903 | if (trc == VEX_TRC_JMP_CLIENTREQ) { |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 904 | UWord* args = (UWord*)(CLREQ_ARGS(VG_(threads)[tid].arch)); |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 905 | UWord reqno = args[0]; |
sewardj | 18a62ff | 2002-07-12 22:30:51 +0000 | [diff] [blame] | 906 | /* VG_(printf)("request 0x%x\n", reqno); */ |
sewardj | 1fe7b00 | 2002-07-16 01:43:15 +0000 | [diff] [blame] | 907 | |
| 908 | /* Are we really absolutely totally quitting? */ |
| 909 | if (reqno == VG_USERREQ__LIBC_FREERES_DONE) { |
| 910 | if (0 || VG_(clo_trace_syscalls) || VG_(clo_trace_sched)) { |
| 911 | VG_(message)(Vg_DebugMsg, |
| 912 | "__libc_freeres() done; really quitting!"); |
| 913 | } |
| 914 | return VgSrc_ExitSyscall; |
| 915 | } |
| 916 | |
nethercote | 3e901a2 | 2004-09-11 13:17:02 +0000 | [diff] [blame] | 917 | do_client_request(tid,args); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 918 | /* Following the request, we try and continue with the |
| 919 | same thread if still runnable. If not, go back to |
| 920 | Stage 1 to select a new thread to run. */ |
sewardj | 18a62ff | 2002-07-12 22:30:51 +0000 | [diff] [blame] | 921 | if (VG_(threads)[tid].status == VgTs_Runnable |
| 922 | && reqno != VG_USERREQ__PTHREAD_YIELD) |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 923 | continue; /* with this thread */ |
| 924 | else |
| 925 | goto stage1; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 926 | } |
| 927 | |
sewardj | d79ef68 | 2004-11-26 13:25:17 +0000 | [diff] [blame^] | 928 | if (trc == VEX_TRC_JMP_SYSCALL) { |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 929 | /* Do a syscall for the vthread tid. This could cause it |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 930 | to become non-runnable. One special case: spot the |
| 931 | client doing calls to exit() and take this as the cue |
| 932 | to exit. */ |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 933 | # if 0 |
| 934 | { UInt* esp; Int i; |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 935 | esp=(UInt*)STACK_PTR(VG_(threads)[tid].arch); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 936 | VG_(printf)("\nBEFORE\n"); |
| 937 | for (i = 10; i >= -10; i--) |
| 938 | VG_(printf)("%2d %p = 0x%x\n", i, &esp[i], esp[i]); |
| 939 | } |
| 940 | # endif |
| 941 | |
sewardj | 1fe7b00 | 2002-07-16 01:43:15 +0000 | [diff] [blame] | 942 | /* Deal with calling __libc_freeres() at exit. When the |
| 943 | client does __NR_exit, it's exiting for good. So we |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 944 | then run __libc_freeres_wrapper. That quits by |
sewardj | 1fe7b00 | 2002-07-16 01:43:15 +0000 | [diff] [blame] | 945 | doing VG_USERREQ__LIBC_FREERES_DONE, and at that point |
| 946 | we really exit. To be safe we nuke all other threads |
sewardj | ade9d0d | 2002-07-26 10:52:48 +0000 | [diff] [blame] | 947 | currently running. |
| 948 | |
| 949 | If not valgrinding (cachegrinding, etc) don't do this. |
| 950 | __libc_freeres does some invalid frees which crash |
| 951 | the unprotected malloc/free system. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 952 | |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 953 | if (SYSCALL_NUM(VG_(threads)[tid].arch) == __NR_exit |
| 954 | || SYSCALL_NUM(VG_(threads)[tid].arch) == __NR_exit_group |
sewardj | f3fb92d | 2003-02-23 03:26:08 +0000 | [diff] [blame] | 955 | ) { |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 956 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 957 | /* Remember the supplied argument. */ |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 958 | *exitcode = SYSCALL_ARG1(VG_(threads)[tid].arch); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 959 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 960 | // Inform tool about regs read by syscall |
| 961 | VG_TRACK( pre_reg_read, Vg_CoreSysCall, tid, "(syscallno)", |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 962 | O_SYSCALL_NUM, sizeof(UWord) ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 963 | |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 964 | if (SYSCALL_NUM(VG_(threads)[tid].arch) == __NR_exit) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 965 | VG_TRACK( pre_reg_read, Vg_CoreSysCall, tid, |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 966 | "exit(error_code)", O_SYSCALL_ARG1, sizeof(int) ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 967 | |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 968 | if (SYSCALL_NUM(VG_(threads)[tid].arch) == __NR_exit_group) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 969 | VG_TRACK( pre_reg_read, Vg_CoreSysCall, tid, |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 970 | "exit_group(error_code)", O_SYSCALL_ARG1, |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 971 | sizeof(int) ); |
| 972 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 973 | /* Only run __libc_freeres if the tool says it's ok and |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 974 | it hasn't been overridden with --run-libc-freeres=no |
| 975 | on the command line. */ |
| 976 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 977 | if (VG_(needs).libc_freeres && |
| 978 | VG_(clo_run_libc_freeres) && |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 979 | __libc_freeres_wrapper != 0) { |
sewardj | 0063189 | 2002-10-05 15:34:38 +0000 | [diff] [blame] | 980 | if (VG_(clo_verbosity) > 2 |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 981 | || VG_(clo_trace_syscalls) || VG_(clo_trace_sched)) { |
| 982 | VG_(message)(Vg_DebugMsg, |
| 983 | "Caught __NR_exit; running __libc_freeres()"); |
| 984 | } |
| 985 | VG_(nuke_all_threads_except) ( tid ); |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 986 | INSTR_PTR(VG_(threads)[tid].arch) = |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 987 | __libc_freeres_wrapper; |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 988 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 989 | goto stage1; /* party on, dudes (but not for much longer :) */ |
| 990 | |
| 991 | } else { |
| 992 | /* We won't run __libc_freeres; just exit now. */ |
sewardj | 0063189 | 2002-10-05 15:34:38 +0000 | [diff] [blame] | 993 | if (VG_(clo_verbosity) > 2 |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 994 | || VG_(clo_trace_syscalls) || VG_(clo_trace_sched)) { |
| 995 | VG_(message)(Vg_DebugMsg, |
| 996 | "Caught __NR_exit; quitting"); |
| 997 | } |
| 998 | return VgSrc_ExitSyscall; |
| 999 | } |
| 1000 | |
sewardj | ade9d0d | 2002-07-26 10:52:48 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 1003 | /* We've dealt with __NR_exit at this point. */ |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 1004 | vg_assert(SYSCALL_NUM(VG_(threads)[tid].arch) != __NR_exit && |
| 1005 | SYSCALL_NUM(VG_(threads)[tid].arch) != __NR_exit_group); |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1006 | |
sewardj | 83798bf | 2002-05-24 00:11:16 +0000 | [diff] [blame] | 1007 | /* Trap syscalls to __NR_sched_yield and just have this |
| 1008 | thread yield instead. Not essential, just an |
| 1009 | optimisation. */ |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 1010 | if (SYSCALL_NUM(VG_(threads)[tid].arch) == __NR_sched_yield) { |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1011 | SET_SYSCALL_RETVAL(tid, 0); /* syscall returns with success */ |
sewardj | 83798bf | 2002-05-24 00:11:16 +0000 | [diff] [blame] | 1012 | goto stage1; /* find a new thread to run */ |
| 1013 | } |
| 1014 | |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1015 | sched_do_syscall(tid); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1016 | |
| 1017 | # if 0 |
| 1018 | { UInt* esp; Int i; |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 1019 | esp=(UInt*)STACK_PTR(VG_(threads)[tid].arch); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1020 | VG_(printf)("AFTER\n"); |
| 1021 | for (i = 10; i >= -10; i--) |
| 1022 | VG_(printf)("%2d %p = 0x%x\n", i, &esp[i], esp[i]); |
| 1023 | } |
| 1024 | # endif |
| 1025 | |
sewardj | 77f0fc1 | 2002-07-12 01:23:03 +0000 | [diff] [blame] | 1026 | if (VG_(threads)[tid].status == VgTs_Runnable) { |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1027 | continue; /* with this thread */ |
sewardj | 77f0fc1 | 2002-07-12 01:23:03 +0000 | [diff] [blame] | 1028 | } else { |
| 1029 | goto stage1; |
| 1030 | } |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
sewardj | d7fd4d2 | 2002-04-24 01:57:27 +0000 | [diff] [blame] | 1033 | /* It's an event we can't quickly deal with. Give up running |
| 1034 | this thread and handle things the expensive way. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | } |
| 1037 | |
| 1038 | /* ======================= Phase 3 of 3 ======================= |
| 1039 | Handle non-trivial thread requests, mostly pthread stuff. */ |
| 1040 | |
| 1041 | /* Ok, we've fallen out of the dispatcher for a |
| 1042 | non-completely-trivial reason. First, update basic-block |
| 1043 | counters. */ |
| 1044 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1045 | if (0 && trc != VG_TRC_INNER_FASTMISS) |
| 1046 | VG_(message)(Vg_DebugMsg, "thread %d: completed %d bbs, trc %d", |
| 1047 | tid, done_this_time, (Int)trc ); |
| 1048 | |
| 1049 | if (0 && trc != VG_TRC_INNER_FASTMISS) |
njn | e0205ff | 2003-04-08 00:56:14 +0000 | [diff] [blame] | 1050 | VG_(message)(Vg_DebugMsg, "thread %d: %llu bbs, event %s", |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1051 | tid, VG_(bbs_done), |
| 1052 | name_of_sched_event(trc) ); |
sewardj | 9d1b5d3 | 2002-04-17 19:40:49 +0000 | [diff] [blame] | 1053 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1054 | /* Examine the thread's return code to figure out why it |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1055 | stopped. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1056 | |
| 1057 | switch (trc) { |
| 1058 | |
sewardj | d79ef68 | 2004-11-26 13:25:17 +0000 | [diff] [blame^] | 1059 | case VEX_TRC_JMP_YIELD: |
fitzhardinge | a02f881 | 2003-12-18 09:06:09 +0000 | [diff] [blame] | 1060 | /* Explicit yield. Let a new thread be scheduled, |
| 1061 | simply by doing nothing, causing us to arrive back at |
| 1062 | Phase 1. */ |
fitzhardinge | a02f881 | 2003-12-18 09:06:09 +0000 | [diff] [blame] | 1063 | break; |
| 1064 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1065 | case VG_TRC_INNER_COUNTERZERO: |
| 1066 | /* Timeslice is out. Let a new thread be scheduled, |
| 1067 | simply by doing nothing, causing us to arrive back at |
| 1068 | Phase 1. */ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1069 | vg_assert(VG_(dispatch_ctr) == 1); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1070 | break; |
| 1071 | |
| 1072 | case VG_TRC_UNRESUMABLE_SIGNAL: |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1073 | /* It got a SIGSEGV/SIGBUS/SIGILL/SIGFPE, which we need to |
| 1074 | deliver right away. */ |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 1075 | vg_assert(unresumable_siginfo.si_signo == VKI_SIGSEGV || |
| 1076 | unresumable_siginfo.si_signo == VKI_SIGBUS || |
| 1077 | unresumable_siginfo.si_signo == VKI_SIGILL || |
| 1078 | unresumable_siginfo.si_signo == VKI_SIGFPE); |
| 1079 | vg_assert(longjmpd_on_signal == unresumable_siginfo.si_signo); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1080 | |
| 1081 | /* make sure we've unblocked the signals which the handler blocked */ |
nethercote | 75d2624 | 2004-08-01 22:59:18 +0000 | [diff] [blame] | 1082 | VG_(unblock_host_signal)(longjmpd_on_signal); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1083 | |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 1084 | VG_(deliver_signal)(tid, &unresumable_siginfo, False); |
| 1085 | unresumable_siginfo.si_signo = 0; /* done */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1086 | break; |
| 1087 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1088 | default: |
| 1089 | VG_(printf)("\ntrc = %d\n", trc); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1090 | VG_(core_panic)("VG_(scheduler), phase 3: " |
| 1091 | "unexpected thread return code"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1092 | /* NOTREACHED */ |
| 1093 | break; |
| 1094 | |
| 1095 | } /* switch (trc) */ |
| 1096 | |
| 1097 | /* That completes Phase 3 of 3. Return now to the top of the |
| 1098 | main scheduler loop, to Phase 1 of 3. */ |
| 1099 | |
| 1100 | } /* top-level scheduler loop */ |
| 1101 | |
| 1102 | |
| 1103 | /* NOTREACHED */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1104 | VG_(core_panic)("scheduler: post-main-loop ?!"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1105 | /* NOTREACHED */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
nethercote | 238a3c3 | 2004-08-09 13:13:31 +0000 | [diff] [blame] | 1108 | VgSchedReturnCode VG_(scheduler) ( Int* exitcode, ThreadId* last_run_tid, |
| 1109 | Int* fatal_sigNo_ptr ) |
| 1110 | { |
| 1111 | VgSchedReturnCode src; |
| 1112 | |
| 1113 | fatal_signal_jmpbuf_ptr = &fatal_signal_jmpbuf; |
| 1114 | if (__builtin_setjmp( fatal_signal_jmpbuf_ptr ) == 0) { |
| 1115 | src = do_scheduler( exitcode, last_run_tid ); |
| 1116 | } else { |
| 1117 | src = VgSrc_FatalSig; |
| 1118 | *fatal_sigNo_ptr = fatal_sigNo; |
| 1119 | } |
| 1120 | return src; |
| 1121 | } |
| 1122 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1123 | void VG_(need_resched) ( ThreadId prefer ) |
| 1124 | { |
| 1125 | /* Tell the scheduler now might be a good time to find a new |
| 1126 | runnable thread, because something happened which woke a thread |
| 1127 | up. |
| 1128 | |
| 1129 | NB: This can be called unsynchronized from either a signal |
| 1130 | handler, or from another LWP (ie, real kernel thread). |
| 1131 | |
| 1132 | In principle this could simply be a matter of setting |
| 1133 | VG_(dispatch_ctr) to a small value (say, 2), which would make |
| 1134 | any running code come back to the scheduler fairly quickly. |
| 1135 | |
| 1136 | However, since the scheduler implements a strict round-robin |
| 1137 | policy with only one priority level, there are, by definition, |
| 1138 | no better threads to be running than the current thread anyway, |
| 1139 | so we may as well ignore this hint. For processes with a |
| 1140 | mixture of compute and I/O bound threads, this means the compute |
| 1141 | threads could introduce longish latencies before the I/O threads |
| 1142 | run. For programs with only I/O bound threads, need_resched |
| 1143 | won't have any effect anyway. |
| 1144 | |
| 1145 | OK, so I've added command-line switches to enable low-latency |
| 1146 | syscalls and signals. The prefer_sched variable is in effect |
| 1147 | the ID of a single thread which has higher priority than all the |
| 1148 | others. If set, the scheduler will prefer to schedule that |
| 1149 | thread over all others. Naturally, this could lead to |
| 1150 | starvation or other unfairness. |
| 1151 | */ |
| 1152 | |
| 1153 | if (VG_(dispatch_ctr) > 10) |
| 1154 | VG_(dispatch_ctr) = 2; |
| 1155 | prefer_sched = prefer; |
| 1156 | } |
| 1157 | |
nethercote | 238a3c3 | 2004-08-09 13:13:31 +0000 | [diff] [blame] | 1158 | void VG_(scheduler_handle_fatal_signal) ( Int sigNo ) |
| 1159 | { |
| 1160 | if (NULL != fatal_signal_jmpbuf_ptr) { |
| 1161 | fatal_sigNo = sigNo; |
| 1162 | __builtin_longjmp(*fatal_signal_jmpbuf_ptr, 1); |
| 1163 | } |
| 1164 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1165 | |
| 1166 | /* --------------------------------------------------------------------- |
| 1167 | The pthread implementation. |
| 1168 | ------------------------------------------------------------------ */ |
| 1169 | |
| 1170 | #include <pthread.h> |
| 1171 | #include <errno.h> |
| 1172 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1173 | /* /usr/include/bits/pthreadtypes.h: |
| 1174 | typedef unsigned long int pthread_t; |
| 1175 | */ |
| 1176 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1177 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1178 | /* ----------------------------------------------------------- |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1179 | Thread CREATION, JOINAGE and CANCELLATION: HELPER FNS |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1180 | -------------------------------------------------------- */ |
| 1181 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1182 | /* We've decided to action a cancellation on tid. Make it jump to |
| 1183 | thread_exit_wrapper() in vg_libpthread.c, passing PTHREAD_CANCELED |
| 1184 | as the arg. */ |
| 1185 | static |
| 1186 | void make_thread_jump_to_cancelhdlr ( ThreadId tid ) |
| 1187 | { |
| 1188 | Char msg_buf[100]; |
| 1189 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | dadc8d0 | 2002-12-08 23:24:18 +0000 | [diff] [blame] | 1190 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1191 | /* Push PTHREAD_CANCELED on the stack and jump to the cancellation |
| 1192 | handler -- which is really thread_exit_wrapper() in |
| 1193 | vg_libpthread.c. */ |
| 1194 | vg_assert(VG_(threads)[tid].cancel_pend != NULL); |
sewardj | 4bdd996 | 2002-12-26 11:51:50 +0000 | [diff] [blame] | 1195 | |
nethercote | 6b9c847 | 2004-09-13 13:16:40 +0000 | [diff] [blame] | 1196 | /* Set an argument and bogus return address. The return address will not |
| 1197 | be used, but we still need to have it so that the arg is at the |
| 1198 | correct stack offset. */ |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 1199 | VGA_(set_arg_and_bogus_ret)(tid, (UWord)PTHREAD_CANCELED, 0xBEADDEEF); |
sewardj | 4bdd996 | 2002-12-26 11:51:50 +0000 | [diff] [blame] | 1200 | |
| 1201 | /* .cancel_pend will hold &thread_exit_wrapper */ |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 1202 | INSTR_PTR(VG_(threads)[tid].arch) = (UWord)VG_(threads)[tid].cancel_pend; |
sewardj | dadc8d0 | 2002-12-08 23:24:18 +0000 | [diff] [blame] | 1203 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1204 | VG_(proxy_abort_syscall)(tid); |
sewardj | dadc8d0 | 2002-12-08 23:24:18 +0000 | [diff] [blame] | 1205 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1206 | /* Make sure we aren't cancelled again whilst handling this |
| 1207 | cancellation. */ |
| 1208 | VG_(threads)[tid].cancel_st = False; |
| 1209 | if (VG_(clo_trace_sched)) { |
| 1210 | VG_(sprintf)(msg_buf, |
| 1211 | "jump to cancellation handler (hdlr = %p)", |
| 1212 | VG_(threads)[tid].cancel_pend); |
| 1213 | print_sched_event(tid, msg_buf); |
| 1214 | } |
thughes | 513197c | 2004-06-13 12:07:53 +0000 | [diff] [blame] | 1215 | |
| 1216 | if(VG_(threads)[tid].status == VgTs_WaitCV) { |
| 1217 | /* posix says we must reaquire mutex before handling cancelation */ |
| 1218 | vg_pthread_mutex_t* mx; |
| 1219 | vg_pthread_cond_t* cond; |
| 1220 | |
| 1221 | mx = VG_(threads)[tid].associated_mx; |
| 1222 | cond = VG_(threads)[tid].associated_cv; |
| 1223 | VG_TRACK( pre_mutex_lock, tid, mx ); |
| 1224 | |
| 1225 | if (mx->__vg_m_owner == VG_INVALID_THREADID) { |
| 1226 | /* Currently unheld; hand it out to thread tid. */ |
| 1227 | vg_assert(mx->__vg_m_count == 0); |
| 1228 | VG_(threads)[tid].status = VgTs_Runnable; |
| 1229 | VG_(threads)[tid].associated_cv = NULL; |
| 1230 | VG_(threads)[tid].associated_mx = NULL; |
thughes | 1023647 | 2004-06-13 14:35:43 +0000 | [diff] [blame] | 1231 | mx->__vg_m_owner = (/*_pthread_descr*/void*)tid; |
thughes | 513197c | 2004-06-13 12:07:53 +0000 | [diff] [blame] | 1232 | mx->__vg_m_count = 1; |
| 1233 | /* .m_edx already holds pth_cond_wait success value (0) */ |
| 1234 | |
| 1235 | VG_TRACK( post_mutex_lock, tid, mx ); |
| 1236 | |
| 1237 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 1238 | VG_(sprintf)(msg_buf, "%s cv %p: RESUME with mx %p", |
| 1239 | "pthread_cancel", cond, mx ); |
| 1240 | print_pthread_event(tid, msg_buf); |
| 1241 | } |
| 1242 | |
| 1243 | } else { |
| 1244 | /* Currently held. Make thread tid be blocked on it. */ |
| 1245 | vg_assert(mx->__vg_m_count > 0); |
| 1246 | VG_(threads)[tid].status = VgTs_WaitMX; |
| 1247 | VG_(threads)[tid].associated_cv = NULL; |
| 1248 | VG_(threads)[tid].associated_mx = mx; |
| 1249 | SET_PTHREQ_RETVAL(tid, 0); /* pth_cond_wait success value */ |
| 1250 | |
| 1251 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 1252 | VG_(sprintf)(msg_buf, "%s cv %p: BLOCK for mx %p", |
| 1253 | "pthread_cancel", cond, mx ); |
| 1254 | print_pthread_event(tid, msg_buf); |
| 1255 | } |
| 1256 | } |
| 1257 | } else { |
| 1258 | VG_(threads)[tid].status = VgTs_Runnable; |
| 1259 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | |
| 1263 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1264 | /* Release resources and generally clean up once a thread has finally |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 1265 | disappeared. |
| 1266 | |
| 1267 | BORKAGE/ISSUES as of 29 May 02 (moved from top of file --njn 2004-Aug-02) |
| 1268 | |
| 1269 | TODO sometime: |
| 1270 | - Mutex scrubbing - clearup_after_thread_exit: look for threads |
| 1271 | blocked on mutexes held by the exiting thread, and release them |
| 1272 | appropriately. (??) |
| 1273 | */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1274 | static |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1275 | void cleanup_after_thread_exited ( ThreadId tid, Bool forcekill ) |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1276 | { |
thughes | 3a1b817 | 2004-09-12 22:48:59 +0000 | [diff] [blame] | 1277 | Segment *seg; |
| 1278 | |
nethercote | 36881a2 | 2004-08-04 14:03:16 +0000 | [diff] [blame] | 1279 | vg_assert(is_valid_or_empty_tid(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1280 | vg_assert(VG_(threads)[tid].status == VgTs_Empty); |
thughes | eb9b8fb | 2004-11-12 23:11:21 +0000 | [diff] [blame] | 1281 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1282 | /* Its stack is now off-limits */ |
thughes | eb9b8fb | 2004-11-12 23:11:21 +0000 | [diff] [blame] | 1283 | if (VG_(threads)[tid].stack_base) { |
| 1284 | seg = VG_(find_segment)( VG_(threads)[tid].stack_base ); |
| 1285 | VG_TRACK( die_mem_stack, seg->addr, seg->len ); |
| 1286 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1287 | |
nethercote | f9b5941 | 2004-09-10 15:33:32 +0000 | [diff] [blame] | 1288 | VGA_(cleanup_thread)( &VG_(threads)[tid].arch ); |
fitzhardinge | 47735af | 2004-01-21 01:27:27 +0000 | [diff] [blame] | 1289 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1290 | /* Not interested in the timeout anymore */ |
| 1291 | VG_(threads)[tid].awaken_at = 0xFFFFFFFF; |
| 1292 | |
| 1293 | /* Delete proxy LWP */ |
| 1294 | VG_(proxy_delete)(tid, forcekill); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1298 | /* Look for matching pairs of threads waiting for joiners and threads |
| 1299 | waiting for joinees. For each such pair copy the return value of |
| 1300 | the joinee into the joiner, let the joiner resume and discard the |
| 1301 | joinee. */ |
| 1302 | static |
| 1303 | void maybe_rendezvous_joiners_and_joinees ( void ) |
| 1304 | { |
| 1305 | Char msg_buf[100]; |
| 1306 | void** thread_return; |
| 1307 | ThreadId jnr, jee; |
| 1308 | |
| 1309 | for (jnr = 1; jnr < VG_N_THREADS; jnr++) { |
| 1310 | if (VG_(threads)[jnr].status != VgTs_WaitJoinee) |
| 1311 | continue; |
| 1312 | jee = VG_(threads)[jnr].joiner_jee_tid; |
| 1313 | if (jee == VG_INVALID_THREADID) |
| 1314 | continue; |
| 1315 | vg_assert(VG_(is_valid_tid)(jee)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1316 | if (VG_(threads)[jee].status != VgTs_WaitJoiner) { |
| 1317 | /* if joinee has become detached, then make join fail with |
| 1318 | EINVAL */ |
| 1319 | if (VG_(threads)[jee].detached) { |
| 1320 | VG_(threads)[jnr].status = VgTs_Runnable; |
| 1321 | VG_(threads)[jnr].joiner_jee_tid = VG_INVALID_THREADID; |
| 1322 | SET_PTHREQ_RETVAL(jnr, VKI_EINVAL); |
| 1323 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1324 | continue; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1325 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1326 | /* ok! jnr is waiting to join with jee, and jee is waiting to be |
| 1327 | joined by ... well, any thread. So let's do it! */ |
| 1328 | |
| 1329 | /* Copy return value to where joiner wants it. */ |
| 1330 | thread_return = VG_(threads)[jnr].joiner_thread_return; |
| 1331 | if (thread_return != NULL) { |
| 1332 | /* CHECK thread_return writable */ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1333 | VG_TRACK( pre_mem_write, Vg_CorePThread, jnr, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1334 | "pthread_join: thread_return", |
| 1335 | (Addr)thread_return, sizeof(void*)); |
sewardj | 5a3798b | 2002-06-04 23:24:22 +0000 | [diff] [blame] | 1336 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1337 | *thread_return = VG_(threads)[jee].joinee_retval; |
| 1338 | /* Not really right, since it makes the thread's return value |
| 1339 | appear to be defined even if it isn't. */ |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 1340 | VG_TRACK( post_mem_write, Vg_CorePThread, jnr, |
| 1341 | (Addr)thread_return, sizeof(void*) ); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | /* Joinee is discarded */ |
| 1345 | VG_(threads)[jee].status = VgTs_Empty; /* bye! */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1346 | cleanup_after_thread_exited ( jee, False ); |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 1347 | if (VG_(clo_trace_sched)) { |
| 1348 | VG_(sprintf)(msg_buf, |
| 1349 | "rendezvous with joinee %d. %d resumes, %d exits.", |
| 1350 | jee, jnr, jee ); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1351 | print_sched_event(jnr, msg_buf); |
| 1352 | } |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 1353 | |
| 1354 | VG_TRACK( post_thread_join, jnr, jee ); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1355 | |
| 1356 | /* joiner returns with success */ |
| 1357 | VG_(threads)[jnr].status = VgTs_Runnable; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1358 | SET_PTHREQ_RETVAL(jnr, 0); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1363 | /* Nuke all threads other than tid. POSIX specifies that this should |
| 1364 | happen in __NR_exec, and after a __NR_fork() when I am the child, |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1365 | as POSIX requires. Also used at process exit time with |
| 1366 | me==VG_INVALID_THREADID */ |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1367 | void VG_(nuke_all_threads_except) ( ThreadId me ) |
| 1368 | { |
| 1369 | ThreadId tid; |
| 1370 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
| 1371 | if (tid == me |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1372 | || VG_(threads)[tid].status == VgTs_Empty) |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1373 | continue; |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 1374 | if (0) |
| 1375 | VG_(printf)( |
| 1376 | "VG_(nuke_all_threads_except): nuking tid %d\n", tid); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1377 | VG_(proxy_delete)(tid, True); |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1378 | VG_(threads)[tid].status = VgTs_Empty; |
thughes | 6d41bea | 2004-10-20 12:25:59 +0000 | [diff] [blame] | 1379 | VG_(threads)[tid].associated_mx = NULL; |
| 1380 | VG_(threads)[tid].associated_cv = NULL; |
thughes | 168eb88 | 2004-11-13 00:39:37 +0000 | [diff] [blame] | 1381 | VG_(threads)[tid].stack_base = (Addr)NULL; |
thughes | eb9b8fb | 2004-11-12 23:11:21 +0000 | [diff] [blame] | 1382 | VG_(threads)[tid].stack_size = 0; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1383 | cleanup_after_thread_exited( tid, True ); |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1388 | /* ----------------------------------------------------------- |
| 1389 | Thread CREATION, JOINAGE and CANCELLATION: REQUESTS |
| 1390 | -------------------------------------------------------- */ |
| 1391 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1392 | static |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1393 | void do__cleanup_push ( ThreadId tid, CleanupEntry* cu ) |
| 1394 | { |
| 1395 | Int sp; |
| 1396 | Char msg_buf[100]; |
| 1397 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1398 | sp = VG_(threads)[tid].custack_used; |
| 1399 | if (VG_(clo_trace_sched)) { |
thughes | 11975ff | 2004-06-12 12:58:22 +0000 | [diff] [blame] | 1400 | switch (cu->type) { |
| 1401 | case VgCt_Function: |
| 1402 | VG_(sprintf)(msg_buf, |
| 1403 | "cleanup_push (fn %p, arg %p) -> slot %d", |
| 1404 | cu->data.function.fn, cu->data.function.arg, sp); |
| 1405 | break; |
| 1406 | case VgCt_Longjmp: |
| 1407 | VG_(sprintf)(msg_buf, |
| 1408 | "cleanup_push (ub %p) -> slot %d", |
| 1409 | cu->data.longjmp.ub, sp); |
| 1410 | break; |
| 1411 | default: |
| 1412 | VG_(sprintf)(msg_buf, |
| 1413 | "cleanup_push (unknown type) -> slot %d", |
| 1414 | sp); |
| 1415 | break; |
| 1416 | } |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1417 | print_sched_event(tid, msg_buf); |
| 1418 | } |
| 1419 | vg_assert(sp >= 0 && sp <= VG_N_CLEANUPSTACK); |
| 1420 | if (sp == VG_N_CLEANUPSTACK) |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1421 | VG_(core_panic)("do__cleanup_push: VG_N_CLEANUPSTACK is too small." |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1422 | " Increase and recompile."); |
| 1423 | VG_(threads)[tid].custack[sp] = *cu; |
| 1424 | sp++; |
| 1425 | VG_(threads)[tid].custack_used = sp; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1426 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | |
| 1430 | static |
| 1431 | void do__cleanup_pop ( ThreadId tid, CleanupEntry* cu ) |
| 1432 | { |
| 1433 | Int sp; |
| 1434 | Char msg_buf[100]; |
| 1435 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1436 | sp = VG_(threads)[tid].custack_used; |
| 1437 | if (VG_(clo_trace_sched)) { |
njn | 3665092 | 2002-10-04 09:18:09 +0000 | [diff] [blame] | 1438 | VG_(sprintf)(msg_buf, "cleanup_pop from slot %d", sp-1); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1439 | print_sched_event(tid, msg_buf); |
| 1440 | } |
| 1441 | vg_assert(sp >= 0 && sp <= VG_N_CLEANUPSTACK); |
| 1442 | if (sp == 0) { |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1443 | SET_PTHREQ_RETVAL(tid, -1); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1444 | return; |
| 1445 | } |
| 1446 | sp--; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1447 | VG_TRACK( pre_mem_write, Vg_CorePThread, tid, |
njn | 3665092 | 2002-10-04 09:18:09 +0000 | [diff] [blame] | 1448 | "cleanup pop", (Addr)cu, sizeof(CleanupEntry) ); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1449 | *cu = VG_(threads)[tid].custack[sp]; |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 1450 | VG_TRACK( post_mem_write, Vg_CorePThread, tid, |
| 1451 | (Addr)cu, sizeof(CleanupEntry) ); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1452 | VG_(threads)[tid].custack_used = sp; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1453 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | static |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 1458 | void do_pthread_yield ( ThreadId tid ) |
| 1459 | { |
| 1460 | Char msg_buf[100]; |
| 1461 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 1462 | if (VG_(clo_trace_sched)) { |
| 1463 | VG_(sprintf)(msg_buf, "yield"); |
| 1464 | print_sched_event(tid, msg_buf); |
| 1465 | } |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1466 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | |
| 1470 | static |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1471 | void do__testcancel ( ThreadId tid ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1472 | { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1473 | Char msg_buf[100]; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1474 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1475 | if (VG_(clo_trace_sched)) { |
| 1476 | VG_(sprintf)(msg_buf, "testcancel"); |
| 1477 | print_sched_event(tid, msg_buf); |
| 1478 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1479 | if (/* is there a cancellation pending on this thread? */ |
| 1480 | VG_(threads)[tid].cancel_pend != NULL |
| 1481 | && /* is this thread accepting cancellations? */ |
| 1482 | VG_(threads)[tid].cancel_st) { |
| 1483 | /* Ok, let's do the cancellation. */ |
| 1484 | make_thread_jump_to_cancelhdlr ( tid ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1485 | } else { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1486 | /* No, we keep going. */ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1487 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1488 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | static |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1493 | void do__set_cancelstate ( ThreadId tid, Int state ) |
| 1494 | { |
| 1495 | Bool old_st; |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1496 | Char msg_buf[100]; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1497 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1498 | if (VG_(clo_trace_sched)) { |
| 1499 | VG_(sprintf)(msg_buf, "set_cancelstate to %d (%s)", state, |
| 1500 | state==PTHREAD_CANCEL_ENABLE |
| 1501 | ? "ENABLE" |
| 1502 | : (state==PTHREAD_CANCEL_DISABLE ? "DISABLE" : "???")); |
| 1503 | print_sched_event(tid, msg_buf); |
| 1504 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1505 | old_st = VG_(threads)[tid].cancel_st; |
| 1506 | if (state == PTHREAD_CANCEL_ENABLE) { |
| 1507 | VG_(threads)[tid].cancel_st = True; |
| 1508 | } else |
| 1509 | if (state == PTHREAD_CANCEL_DISABLE) { |
| 1510 | VG_(threads)[tid].cancel_st = False; |
| 1511 | } else { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1512 | VG_(core_panic)("do__set_cancelstate"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1513 | } |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1514 | SET_PTHREQ_RETVAL(tid, old_st ? PTHREAD_CANCEL_ENABLE |
| 1515 | : PTHREAD_CANCEL_DISABLE); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | static |
| 1520 | void do__set_canceltype ( ThreadId tid, Int type ) |
| 1521 | { |
| 1522 | Bool old_ty; |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1523 | Char msg_buf[100]; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1524 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1525 | if (VG_(clo_trace_sched)) { |
| 1526 | VG_(sprintf)(msg_buf, "set_canceltype to %d (%s)", type, |
| 1527 | type==PTHREAD_CANCEL_ASYNCHRONOUS |
| 1528 | ? "ASYNCHRONOUS" |
| 1529 | : (type==PTHREAD_CANCEL_DEFERRED ? "DEFERRED" : "???")); |
| 1530 | print_sched_event(tid, msg_buf); |
| 1531 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1532 | old_ty = VG_(threads)[tid].cancel_ty; |
| 1533 | if (type == PTHREAD_CANCEL_ASYNCHRONOUS) { |
| 1534 | VG_(threads)[tid].cancel_ty = False; |
| 1535 | } else |
| 1536 | if (type == PTHREAD_CANCEL_DEFERRED) { |
sewardj | af00b6d | 2002-05-29 23:30:28 +0000 | [diff] [blame] | 1537 | VG_(threads)[tid].cancel_ty = True; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1538 | } else { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1539 | VG_(core_panic)("do__set_canceltype"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1540 | } |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1541 | SET_PTHREQ_RETVAL(tid, old_ty ? PTHREAD_CANCEL_DEFERRED |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1542 | : PTHREAD_CANCEL_ASYNCHRONOUS); |
| 1543 | } |
| 1544 | |
| 1545 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1546 | /* Set or get the detach state for thread det. */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1547 | static |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1548 | void do__set_or_get_detach ( ThreadId tid, |
| 1549 | Int what, ThreadId det ) |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1550 | { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1551 | Char msg_buf[100]; |
| 1552 | /* VG_(printf)("do__set_or_get_detach tid %d what %d det %d\n", |
| 1553 | tid, what, det); */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1554 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1555 | if (VG_(clo_trace_sched)) { |
| 1556 | VG_(sprintf)(msg_buf, "set_or_get_detach %d (%s) for tid %d", what, |
| 1557 | what==0 ? "not-detached" : ( |
| 1558 | what==1 ? "detached" : ( |
| 1559 | what==2 ? "fetch old value" : "???")), |
| 1560 | det ); |
| 1561 | print_sched_event(tid, msg_buf); |
| 1562 | } |
| 1563 | |
| 1564 | if (!VG_(is_valid_tid)(det)) { |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1565 | SET_PTHREQ_RETVAL(tid, -1); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1566 | return; |
| 1567 | } |
| 1568 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1569 | switch (what) { |
| 1570 | case 2: /* get */ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1571 | SET_PTHREQ_RETVAL(tid, VG_(threads)[det].detached ? 1 : 0); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1572 | return; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1573 | case 1: |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1574 | VG_(threads)[det].detached = True; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1575 | SET_PTHREQ_RETVAL(tid, 0); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1576 | /* wake anyone who was joining on us */ |
| 1577 | maybe_rendezvous_joiners_and_joinees(); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1578 | return; |
| 1579 | case 0: /* set not detached */ |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1580 | VG_(threads)[det].detached = False; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1581 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1582 | return; |
| 1583 | default: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1584 | VG_(core_panic)("do__set_or_get_detach"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | |
| 1589 | static |
| 1590 | void do__set_cancelpend ( ThreadId tid, |
| 1591 | ThreadId cee, |
| 1592 | void (*cancelpend_hdlr)(void*) ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1593 | { |
| 1594 | Char msg_buf[100]; |
| 1595 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1596 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1597 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 1598 | |
thughes | 97e54d2 | 2004-08-15 14:34:02 +0000 | [diff] [blame] | 1599 | if (!VG_(is_valid_tid)(cee) || |
| 1600 | VG_(threads)[cee].status == VgTs_WaitJoiner) { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1601 | if (VG_(clo_trace_sched)) { |
| 1602 | VG_(sprintf)(msg_buf, |
| 1603 | "set_cancelpend for invalid tid %d", cee); |
| 1604 | print_sched_event(tid, msg_buf); |
| 1605 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1606 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1607 | "pthread_cancel: target thread does not exist, or invalid"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1608 | SET_PTHREQ_RETVAL(tid, VKI_ESRCH); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1609 | return; |
| 1610 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1611 | |
| 1612 | VG_(threads)[cee].cancel_pend = cancelpend_hdlr; |
| 1613 | |
thughes | 31c1aae | 2004-10-28 15:56:55 +0000 | [diff] [blame] | 1614 | /* interrupt a pending syscall if asynchronous cancellation |
| 1615 | is enabled for the target thread */ |
| 1616 | if (VG_(threads)[cee].cancel_st && !VG_(threads)[cee].cancel_ty) { |
| 1617 | VG_(proxy_abort_syscall)(cee); |
| 1618 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1619 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1620 | if (VG_(clo_trace_sched)) { |
| 1621 | VG_(sprintf)(msg_buf, |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1622 | "set_cancelpend (hdlr = %p, set by tid %d)", |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1623 | cancelpend_hdlr, tid); |
| 1624 | print_sched_event(cee, msg_buf); |
| 1625 | } |
| 1626 | |
| 1627 | /* Thread doing the cancelling returns with success. */ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1628 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1629 | |
| 1630 | /* Perhaps we can nuke the cancellee right now? */ |
thughes | 513197c | 2004-06-13 12:07:53 +0000 | [diff] [blame] | 1631 | if (!VG_(threads)[cee].cancel_ty || /* if PTHREAD_CANCEL_ASYNCHRONOUS */ |
| 1632 | (VG_(threads)[cee].status != VgTs_Runnable && |
| 1633 | VG_(threads)[cee].status != VgTs_WaitMX)) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1634 | do__testcancel(cee); |
thughes | 513197c | 2004-06-13 12:07:53 +0000 | [diff] [blame] | 1635 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | |
| 1639 | static |
| 1640 | void do_pthread_join ( ThreadId tid, |
| 1641 | ThreadId jee, void** thread_return ) |
| 1642 | { |
| 1643 | Char msg_buf[100]; |
| 1644 | ThreadId i; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1645 | /* jee, the joinee, is the thread specified as an arg in thread |
| 1646 | tid's call to pthread_join. So tid is the join-er. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1647 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1648 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1649 | |
| 1650 | if (jee == tid) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1651 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1652 | "pthread_join: attempt to join to self"); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1653 | SET_PTHREQ_RETVAL(tid, EDEADLK); /* libc constant, not a kernel one */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1654 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1655 | return; |
| 1656 | } |
| 1657 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1658 | /* Flush any completed pairs, so as to make sure what we're looking |
| 1659 | at is up-to-date. */ |
| 1660 | maybe_rendezvous_joiners_and_joinees(); |
| 1661 | |
| 1662 | /* Is this a sane request? */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1663 | if ( ! VG_(is_valid_tid)(jee) || |
| 1664 | VG_(threads)[jee].detached) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1665 | /* Invalid thread to join to. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1666 | VG_(record_pthread_error)( tid, |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1667 | "pthread_join: target thread does not exist, invalid, or detached"); |
| 1668 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1669 | return; |
| 1670 | } |
| 1671 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1672 | /* Is anyone else already in a join-wait for jee? */ |
| 1673 | for (i = 1; i < VG_N_THREADS; i++) { |
| 1674 | if (i == tid) continue; |
| 1675 | if (VG_(threads)[i].status == VgTs_WaitJoinee |
| 1676 | && VG_(threads)[i].joiner_jee_tid == jee) { |
| 1677 | /* Someone already did join on this thread */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1678 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1679 | "pthread_join: another thread already " |
| 1680 | "in join-wait for target thread"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1681 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
| 1682 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1683 | return; |
| 1684 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
thughes | 513197c | 2004-06-13 12:07:53 +0000 | [diff] [blame] | 1687 | if(VG_(threads)[tid].cancel_pend != NULL && |
| 1688 | VG_(threads)[tid].cancel_st) { |
| 1689 | make_thread_jump_to_cancelhdlr ( tid ); |
| 1690 | } else { |
| 1691 | /* Mark this thread as waiting for the joinee. */ |
| 1692 | VG_(threads)[tid].status = VgTs_WaitJoinee; |
| 1693 | VG_(threads)[tid].joiner_thread_return = thread_return; |
| 1694 | VG_(threads)[tid].joiner_jee_tid = jee; |
| 1695 | |
| 1696 | /* Look for matching joiners and joinees and do the right thing. */ |
| 1697 | maybe_rendezvous_joiners_and_joinees(); |
| 1698 | |
| 1699 | /* Return value is irrelevant since this this thread becomes |
| 1700 | non-runnable. maybe_resume_joiner() will cause it to return the |
| 1701 | right value when it resumes. */ |
| 1702 | |
| 1703 | if (VG_(clo_trace_sched)) { |
| 1704 | VG_(sprintf)(msg_buf, |
| 1705 | "wait for joinee %d (may already be ready)", jee); |
| 1706 | print_sched_event(tid, msg_buf); |
| 1707 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1708 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1712 | /* ( void* ): calling thread waits for joiner and returns the void* to |
| 1713 | it. This is one of two ways in which a thread can finally exit -- |
| 1714 | the other is do__quit. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1715 | static |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1716 | void do__wait_joiner ( ThreadId tid, void* retval ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1717 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1718 | Char msg_buf[100]; |
| 1719 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1720 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 1721 | if (VG_(clo_trace_sched)) { |
| 1722 | VG_(sprintf)(msg_buf, |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1723 | "do__wait_joiner(retval = %p) (non-detached thread exit)", retval); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1724 | print_sched_event(tid, msg_buf); |
| 1725 | } |
| 1726 | VG_(threads)[tid].status = VgTs_WaitJoiner; |
| 1727 | VG_(threads)[tid].joinee_retval = retval; |
| 1728 | maybe_rendezvous_joiners_and_joinees(); |
| 1729 | } |
| 1730 | |
| 1731 | |
| 1732 | /* ( no-args ): calling thread disappears from the system forever. |
| 1733 | Reclaim resources. */ |
| 1734 | static |
| 1735 | void do__quit ( ThreadId tid ) |
| 1736 | { |
| 1737 | Char msg_buf[100]; |
| 1738 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1739 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 1740 | VG_(threads)[tid].status = VgTs_Empty; /* bye! */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1741 | cleanup_after_thread_exited ( tid, False ); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1742 | if (VG_(clo_trace_sched)) { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1743 | VG_(sprintf)(msg_buf, "do__quit (detached thread exit)"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1744 | print_sched_event(tid, msg_buf); |
| 1745 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1746 | maybe_rendezvous_joiners_and_joinees(); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1747 | /* Return value is irrelevant; this thread will not get |
| 1748 | rescheduled. */ |
| 1749 | } |
| 1750 | |
| 1751 | |
nethercote | 6b9c847 | 2004-09-13 13:16:40 +0000 | [diff] [blame] | 1752 | /* Should never be entered. If it is, will be on the simulated CPU. */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1753 | static |
| 1754 | void do__apply_in_new_thread_bogusRA ( void ) |
| 1755 | { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1756 | VG_(core_panic)("do__apply_in_new_thread_bogusRA"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | /* (Fn, Arg): Create a new thread and run Fn applied to Arg in it. Fn |
| 1760 | MUST NOT return -- ever. Eventually it will do either __QUIT or |
| 1761 | __WAIT_JOINER. Return the child tid to the parent. */ |
| 1762 | static |
| 1763 | void do__apply_in_new_thread ( ThreadId parent_tid, |
| 1764 | void* (*fn)(void *), |
thughes | daa3456 | 2004-06-27 12:48:53 +0000 | [diff] [blame] | 1765 | void* arg, |
| 1766 | StackInfo *si ) |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1767 | { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1768 | Addr new_stack; |
| 1769 | UInt new_stk_szb; |
| 1770 | ThreadId tid; |
| 1771 | Char msg_buf[100]; |
| 1772 | |
| 1773 | /* Paranoia ... */ |
| 1774 | vg_assert(sizeof(pthread_t) == sizeof(UInt)); |
| 1775 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1776 | vg_assert(VG_(threads)[parent_tid].status != VgTs_Empty); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1777 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1778 | tid = vg_alloc_ThreadState(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1779 | |
| 1780 | /* 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] | 1781 | vg_assert(tid != 1); |
nethercote | 36881a2 | 2004-08-04 14:03:16 +0000 | [diff] [blame] | 1782 | vg_assert(is_valid_or_empty_tid(tid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1783 | |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 1784 | /* do this early, before the child gets any memory writes */ |
| 1785 | VG_TRACK ( post_thread_create, parent_tid, tid ); |
| 1786 | |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 1787 | /* Create new thread with default attrs: |
| 1788 | deferred cancellation, not detached |
| 1789 | */ |
| 1790 | mostly_clear_thread_record(tid); |
| 1791 | VG_(threads)[tid].status = VgTs_Runnable; |
| 1792 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 1793 | /* Copy the parent's CPU state into the child's. */ |
nethercote | f9b5941 | 2004-09-10 15:33:32 +0000 | [diff] [blame] | 1794 | VGA_(setup_child)( &VG_(threads)[tid].arch, |
| 1795 | &VG_(threads)[parent_tid].arch ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1796 | |
| 1797 | /* Consider allocating the child a stack, if the one it already has |
| 1798 | is inadequate. */ |
thughes | 5e5e213 | 2004-11-16 19:40:05 +0000 | [diff] [blame] | 1799 | new_stk_szb = PGROUNDUP(si->size + VG_AR_CLIENT_STACKBASE_REDZONE_SZB + si->guardsize); |
| 1800 | |
thughes | daa3456 | 2004-06-27 12:48:53 +0000 | [diff] [blame] | 1801 | VG_(threads)[tid].stack_guard_size = si->guardsize; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1802 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1803 | if (new_stk_szb > VG_(threads)[tid].stack_size) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1804 | /* Again, for good measure :) We definitely don't want to be |
| 1805 | allocating a stack for the main thread. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1806 | vg_assert(tid != 1); |
thughes | daa3456 | 2004-06-27 12:48:53 +0000 | [diff] [blame] | 1807 | if (VG_(threads)[tid].stack_size > 0) |
| 1808 | VG_(client_free)(VG_(threads)[tid].stack_base); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1809 | new_stack = VG_(client_alloc)(0, new_stk_szb, |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 1810 | VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC, |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1811 | SF_STACK); |
nethercote | 8e9eab0 | 2004-07-11 18:01:06 +0000 | [diff] [blame] | 1812 | // Given the low number of threads Valgrind can handle, stack |
| 1813 | // allocation should pretty much always succeed, so having an |
| 1814 | // assertion here isn't too bad. However, probably better would be |
| 1815 | // this: |
| 1816 | // |
| 1817 | // if (0 == new_stack) |
| 1818 | // SET_PTHREQ_RETVAL(parent_tid, -VKI_EAGAIN); |
| 1819 | // |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 1820 | vg_assert(0 != new_stack); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1821 | VG_(threads)[tid].stack_base = new_stack; |
| 1822 | VG_(threads)[tid].stack_size = new_stk_szb; |
| 1823 | VG_(threads)[tid].stack_highest_word |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1824 | = new_stack + new_stk_szb |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1825 | - VG_AR_CLIENT_STACKBASE_REDZONE_SZB; /* -4 ??? */; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1826 | } |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1827 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1828 | /* Having got memory to hold the thread's stack: |
| 1829 | - set %esp as base + size |
| 1830 | - mark everything below %esp inaccessible |
| 1831 | - mark redzone at stack end inaccessible |
| 1832 | */ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1833 | SET_PTHREQ_ESP(tid, VG_(threads)[tid].stack_base |
| 1834 | + VG_(threads)[tid].stack_size |
| 1835 | - VG_AR_CLIENT_STACKBASE_REDZONE_SZB); |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1836 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1837 | VG_TRACK ( die_mem_stack, VG_(threads)[tid].stack_base, |
thughes | daa3456 | 2004-06-27 12:48:53 +0000 | [diff] [blame] | 1838 | VG_(threads)[tid].stack_size |
| 1839 | - VG_AR_CLIENT_STACKBASE_REDZONE_SZB); |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 1840 | VG_TRACK ( ban_mem_stack, STACK_PTR(VG_(threads)[tid].arch), |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1841 | VG_AR_CLIENT_STACKBASE_REDZONE_SZB ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1842 | |
nethercote | 6b9c847 | 2004-09-13 13:16:40 +0000 | [diff] [blame] | 1843 | VGA_(thread_initial_stack)(tid, (UWord)arg, |
| 1844 | (Addr)&do__apply_in_new_thread_bogusRA); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1845 | |
| 1846 | /* this is where we start */ |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 1847 | INSTR_PTR(VG_(threads)[tid].arch) = (UWord)fn; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1848 | |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 1849 | if (VG_(clo_trace_sched)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1850 | VG_(sprintf)(msg_buf, "new thread, created by %d", parent_tid ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1851 | print_sched_event(tid, msg_buf); |
| 1852 | } |
| 1853 | |
fitzhardinge | f786618 | 2004-03-16 22:09:12 +0000 | [diff] [blame] | 1854 | /* Start the thread with all signals blocked; it's up to the client |
| 1855 | code to set the right signal mask when it's ready. */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1856 | VG_(sigfillset)(&VG_(threads)[tid].sig_mask); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1857 | |
| 1858 | /* Now that the signal mask is set up, create a proxy LWP for this thread */ |
| 1859 | VG_(proxy_create)(tid); |
| 1860 | |
| 1861 | /* Set the proxy's signal mask */ |
| 1862 | VG_(proxy_setsigmask)(tid); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1863 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1864 | /* return child's tid to parent */ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1865 | SET_PTHREQ_RETVAL(parent_tid, tid); /* success */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1869 | /* ----------------------------------------------------------- |
| 1870 | MUTEXes |
| 1871 | -------------------------------------------------------- */ |
| 1872 | |
rjwalsh | 7109a8c | 2004-09-02 00:31:02 +0000 | [diff] [blame] | 1873 | /* vg_pthread_mutex_t is defined in core.h. |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1874 | |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 1875 | The initializers zero everything, except possibly the fourth word, |
| 1876 | which in vg_pthread_mutex_t is the __vg_m_kind field. It gets set to one |
| 1877 | of PTHREAD_MUTEX_{TIMED,RECURSIVE,ERRORCHECK,ADAPTIVE}_NP |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1878 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1879 | How we use it: |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1880 | |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 1881 | __vg_m_kind never changes and indicates whether or not it is recursive. |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1882 | |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 1883 | __vg_m_count indicates the lock count; if 0, the mutex is not owned by |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1884 | anybody. |
| 1885 | |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 1886 | __vg_m_owner has a ThreadId value stuffed into it. We carefully arrange |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1887 | that ThreadId == 0 is invalid (VG_INVALID_THREADID), so that |
| 1888 | statically initialised mutexes correctly appear |
| 1889 | to belong to nobody. |
| 1890 | |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 1891 | In summary, a not-in-use mutex is distinguised by having __vg_m_owner |
| 1892 | == 0 (VG_INVALID_THREADID) and __vg_m_count == 0 too. If one of those |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1893 | conditions holds, the other should too. |
| 1894 | |
| 1895 | There is no linked list of threads waiting for this mutex. Instead |
| 1896 | a thread in WaitMX state points at the mutex with its waited_on_mx |
| 1897 | field. This makes _unlock() inefficient, but simple to implement the |
| 1898 | right semantics viz-a-viz signals. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1899 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1900 | We don't have to deal with mutex initialisation; the client side |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1901 | deals with that for us. |
| 1902 | */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1903 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1904 | /* Helper fns ... */ |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 1905 | static |
| 1906 | void do_pthread_mutex_timedlock_TIMEOUT ( ThreadId tid ) |
| 1907 | { |
| 1908 | Char msg_buf[100]; |
| 1909 | vg_pthread_mutex_t* mx; |
| 1910 | |
| 1911 | vg_assert(VG_(is_valid_tid)(tid) |
| 1912 | && VG_(threads)[tid].status == VgTs_WaitMX |
| 1913 | && VG_(threads)[tid].awaken_at != 0xFFFFFFFF); |
| 1914 | mx = VG_(threads)[tid].associated_mx; |
| 1915 | vg_assert(mx != NULL); |
| 1916 | |
| 1917 | VG_(threads)[tid].status = VgTs_Runnable; |
| 1918 | SET_PTHREQ_RETVAL(tid, ETIMEDOUT); /* pthread_mutex_lock return value */ |
| 1919 | VG_(threads)[tid].associated_mx = NULL; |
| 1920 | |
| 1921 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 1922 | VG_(sprintf)(msg_buf, "pthread_mutex_timedlock mx %p: TIMEOUT", mx); |
| 1923 | print_pthread_event(tid, msg_buf); |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1928 | static |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 1929 | void release_one_thread_waiting_on_mutex ( vg_pthread_mutex_t* mutex, |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1930 | Char* caller ) |
| 1931 | { |
| 1932 | Int i; |
| 1933 | Char msg_buf[100]; |
| 1934 | |
| 1935 | /* Find some arbitrary thread waiting on this mutex, and make it |
| 1936 | runnable. If none are waiting, mark the mutex as not held. */ |
| 1937 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1938 | if (VG_(threads)[i].status == VgTs_Empty) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1939 | continue; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1940 | if (VG_(threads)[i].status == VgTs_WaitMX |
| 1941 | && VG_(threads)[i].associated_mx == mutex) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1942 | break; |
| 1943 | } |
| 1944 | |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 1945 | VG_TRACK( post_mutex_unlock, (ThreadId)mutex->__vg_m_owner, mutex ); |
sewardj | 0af43bc | 2002-10-22 04:30:35 +0000 | [diff] [blame] | 1946 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1947 | vg_assert(i <= VG_N_THREADS); |
| 1948 | if (i == VG_N_THREADS) { |
| 1949 | /* Nobody else is waiting on it. */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 1950 | mutex->__vg_m_count = 0; |
| 1951 | mutex->__vg_m_owner = VG_INVALID_THREADID; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1952 | } else { |
| 1953 | /* Notionally transfer the hold to thread i, whose |
| 1954 | pthread_mutex_lock() call now returns with 0 (success). */ |
| 1955 | /* The .count is already == 1. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1956 | vg_assert(VG_(threads)[i].associated_mx == mutex); |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 1957 | mutex->__vg_m_owner = (/*_pthread_descr*/void*)i; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1958 | VG_(threads)[i].status = VgTs_Runnable; |
| 1959 | VG_(threads)[i].associated_mx = NULL; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1960 | /* m_edx already holds pth_mx_lock() success (0) */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1961 | |
sewardj | 0af43bc | 2002-10-22 04:30:35 +0000 | [diff] [blame] | 1962 | VG_TRACK( post_mutex_lock, (ThreadId)i, mutex); |
| 1963 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1964 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 1965 | VG_(sprintf)(msg_buf, "%s mx %p: RESUME", |
| 1966 | caller, mutex ); |
| 1967 | print_pthread_event(i, msg_buf); |
| 1968 | } |
| 1969 | } |
| 1970 | } |
| 1971 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1972 | |
| 1973 | static |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 1974 | void do_pthread_mutex_lock( ThreadId tid, |
| 1975 | Bool is_trylock, |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 1976 | vg_pthread_mutex_t* mutex, |
| 1977 | UInt ms_end ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1978 | { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 1979 | Char msg_buf[100]; |
| 1980 | Char* caller |
sewardj | 8ccc2be | 2002-05-10 20:26:37 +0000 | [diff] [blame] | 1981 | = is_trylock ? "pthread_mutex_trylock" |
| 1982 | : "pthread_mutex_lock "; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1983 | |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 1984 | /* If ms_end == 0xFFFFFFFF, wait forever (no timeout). Otherwise, |
| 1985 | ms_end is the ending millisecond. */ |
| 1986 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1987 | if (VG_(clo_trace_pthread_level) >= 2) { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 1988 | VG_(sprintf)(msg_buf, "%s mx %p ...", caller, mutex ); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1989 | print_pthread_event(tid, msg_buf); |
| 1990 | } |
| 1991 | |
| 1992 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1993 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1994 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1995 | |
| 1996 | /* POSIX doesn't mandate this, but for sanity ... */ |
| 1997 | if (mutex == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1998 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1999 | "pthread_mutex_lock/trylock: mutex is NULL"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2000 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2001 | return; |
| 2002 | } |
| 2003 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2004 | /* More paranoia ... */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2005 | switch (mutex->__vg_m_kind) { |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2006 | # ifndef GLIBC_2_1 |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2007 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2008 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
| 2009 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2010 | # ifdef GLIBC_2_1 |
sewardj | 8e651d7 | 2002-05-10 21:00:19 +0000 | [diff] [blame] | 2011 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2012 | # endif |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2013 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 2014 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2015 | if (mutex->__vg_m_count >= 0) break; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2016 | /* else fall thru */ |
| 2017 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2018 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2019 | "pthread_mutex_lock/trylock: mutex is invalid"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2020 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2021 | return; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2024 | if (mutex->__vg_m_count > 0) { |
| 2025 | if (!VG_(is_valid_tid)((ThreadId)mutex->__vg_m_owner)) { |
fitzhardinge | 47735af | 2004-01-21 01:27:27 +0000 | [diff] [blame] | 2026 | VG_(record_pthread_error)( tid, |
| 2027 | "pthread_mutex_lock/trylock: mutex has invalid owner"); |
| 2028 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
| 2029 | return; |
| 2030 | } |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2031 | |
| 2032 | /* Someone has it already. */ |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 2033 | if ((ThreadId)mutex->__vg_m_owner == tid && ms_end == 0xFFFFFFFF) { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2034 | /* It's locked -- by me! */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2035 | if (mutex->__vg_m_kind == PTHREAD_MUTEX_RECURSIVE_NP) { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2036 | /* return 0 (success). */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2037 | mutex->__vg_m_count++; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2038 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 2039 | if (0) |
| 2040 | VG_(printf)("!!!!!! tid %d, mx %p -> locked %d\n", |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2041 | tid, mutex, mutex->__vg_m_count); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2042 | return; |
| 2043 | } else { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2044 | if (is_trylock) |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2045 | SET_PTHREQ_RETVAL(tid, EBUSY); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2046 | else |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2047 | SET_PTHREQ_RETVAL(tid, EDEADLK); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2048 | return; |
| 2049 | } |
| 2050 | } else { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2051 | /* Someone else has it; we have to wait. Mark ourselves |
| 2052 | thusly. */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2053 | /* GUARD: __vg_m_count > 0 && __vg_m_owner is valid */ |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2054 | if (is_trylock) { |
| 2055 | /* caller is polling; so return immediately. */ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2056 | SET_PTHREQ_RETVAL(tid, EBUSY); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2057 | } else { |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 2058 | VG_TRACK ( pre_mutex_lock, tid, mutex ); |
| 2059 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2060 | VG_(threads)[tid].status = VgTs_WaitMX; |
| 2061 | VG_(threads)[tid].associated_mx = mutex; |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 2062 | VG_(threads)[tid].awaken_at = ms_end; |
| 2063 | if (ms_end != 0xFFFFFFFF) |
| 2064 | add_timeout(tid, ms_end); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2065 | SET_PTHREQ_RETVAL(tid, 0); /* pth_mx_lock success value */ |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2066 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2067 | VG_(sprintf)(msg_buf, "%s mx %p: BLOCK", |
| 2068 | caller, mutex ); |
| 2069 | print_pthread_event(tid, msg_buf); |
| 2070 | } |
| 2071 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2072 | return; |
| 2073 | } |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2074 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2075 | } else { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2076 | /* Nobody owns it. Sanity check ... */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2077 | vg_assert(mutex->__vg_m_owner == VG_INVALID_THREADID); |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 2078 | |
| 2079 | VG_TRACK ( pre_mutex_lock, tid, mutex ); |
| 2080 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2081 | /* We get it! [for the first time]. */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2082 | mutex->__vg_m_count = 1; |
| 2083 | mutex->__vg_m_owner = (/*_pthread_descr*/void*)tid; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2084 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2085 | /* return 0 (success). */ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2086 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2087 | |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2088 | VG_TRACK( post_mutex_lock, tid, mutex); |
| 2089 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | |
| 2093 | static |
| 2094 | void do_pthread_mutex_unlock ( ThreadId tid, |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2095 | vg_pthread_mutex_t* mutex ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2096 | { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2097 | Char msg_buf[100]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2098 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 2099 | if (VG_(clo_trace_pthread_level) >= 2) { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2100 | VG_(sprintf)(msg_buf, "pthread_mutex_unlock mx %p ...", mutex ); |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 2101 | print_pthread_event(tid, msg_buf); |
| 2102 | } |
| 2103 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2104 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2105 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2106 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2107 | |
| 2108 | if (mutex == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2109 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2110 | "pthread_mutex_unlock: mutex is NULL"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2111 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2112 | return; |
| 2113 | } |
| 2114 | |
| 2115 | /* More paranoia ... */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2116 | switch (mutex->__vg_m_kind) { |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2117 | # ifndef GLIBC_2_1 |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2118 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2119 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
| 2120 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2121 | # ifdef GLIBC_2_1 |
sewardj | 8e651d7 | 2002-05-10 21:00:19 +0000 | [diff] [blame] | 2122 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2123 | # endif |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2124 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 2125 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2126 | if (mutex->__vg_m_count >= 0) break; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2127 | /* else fall thru */ |
| 2128 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2129 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2130 | "pthread_mutex_unlock: mutex is invalid"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2131 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2132 | return; |
| 2133 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2134 | |
| 2135 | /* Barf if we don't currently hold the mutex. */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2136 | if (mutex->__vg_m_count == 0) { |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2137 | /* nobody holds it */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2138 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2139 | "pthread_mutex_unlock: mutex is not locked"); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2140 | SET_PTHREQ_RETVAL(tid, EPERM); |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2141 | return; |
| 2142 | } |
| 2143 | |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2144 | if ((ThreadId)mutex->__vg_m_owner != tid) { |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2145 | /* we don't hold it */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2146 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2147 | "pthread_mutex_unlock: mutex is locked by a different thread"); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2148 | SET_PTHREQ_RETVAL(tid, EPERM); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2149 | return; |
| 2150 | } |
| 2151 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2152 | /* If it's a multiply-locked recursive mutex, just decrement the |
| 2153 | lock count and return. */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2154 | if (mutex->__vg_m_count > 1) { |
| 2155 | vg_assert(mutex->__vg_m_kind == PTHREAD_MUTEX_RECURSIVE_NP); |
| 2156 | mutex->__vg_m_count --; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2157 | SET_PTHREQ_RETVAL(tid, 0); /* success */ |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2158 | return; |
| 2159 | } |
| 2160 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2161 | /* 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] | 2162 | is now doing an unlock on it. */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2163 | vg_assert(mutex->__vg_m_count == 1); |
| 2164 | vg_assert((ThreadId)mutex->__vg_m_owner == tid); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2165 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2166 | /* Release at max one thread waiting on this mutex. */ |
| 2167 | release_one_thread_waiting_on_mutex ( mutex, "pthread_mutex_lock" ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2168 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2169 | /* Our (tid's) pth_unlock() returns with 0 (success). */ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2170 | SET_PTHREQ_RETVAL(tid, 0); /* Success. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2174 | /* ----------------------------------------------------------- |
| 2175 | CONDITION VARIABLES |
| 2176 | -------------------------------------------------------- */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2177 | |
rjwalsh | 7109a8c | 2004-09-02 00:31:02 +0000 | [diff] [blame] | 2178 | /* The relevant type (vg_pthread_cond_t) is in core.h. |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2179 | |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2180 | We don't use any fields of vg_pthread_cond_t for anything at all. |
| 2181 | Only the identity of the CVs is important. (Actually, we initialise |
| 2182 | __vg_c_waiting in pthread_cond_init() to VG_INVALID_THREADID.) |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2183 | |
| 2184 | Linux pthreads supports no attributes on condition variables, so we |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2185 | don't need to think too hard there. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2186 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2187 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2188 | static |
| 2189 | void do_pthread_cond_timedwait_TIMEOUT ( ThreadId tid ) |
| 2190 | { |
| 2191 | Char msg_buf[100]; |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2192 | vg_pthread_mutex_t* mx; |
| 2193 | vg_pthread_cond_t* cv; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2194 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2195 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2196 | && VG_(threads)[tid].status == VgTs_WaitCV |
| 2197 | && VG_(threads)[tid].awaken_at != 0xFFFFFFFF); |
| 2198 | mx = VG_(threads)[tid].associated_mx; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2199 | vg_assert(mx != NULL); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2200 | cv = VG_(threads)[tid].associated_cv; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2201 | vg_assert(cv != NULL); |
| 2202 | |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2203 | if (mx->__vg_m_owner == VG_INVALID_THREADID) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2204 | /* Currently unheld; hand it out to thread tid. */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2205 | vg_assert(mx->__vg_m_count == 0); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2206 | VG_(threads)[tid].status = VgTs_Runnable; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2207 | SET_PTHREQ_RETVAL(tid, ETIMEDOUT); /* pthread_cond_wait return value */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2208 | VG_(threads)[tid].associated_cv = NULL; |
| 2209 | VG_(threads)[tid].associated_mx = NULL; |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2210 | mx->__vg_m_owner = (/*_pthread_descr*/void*)tid; |
| 2211 | mx->__vg_m_count = 1; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2212 | |
sewardj | 0af43bc | 2002-10-22 04:30:35 +0000 | [diff] [blame] | 2213 | VG_TRACK( post_mutex_lock, tid, mx ); |
| 2214 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2215 | if (VG_(clo_trace_pthread_level) >= 1) { |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2216 | VG_(sprintf)(msg_buf, |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2217 | "pthread_cond_timedwait cv %p: TIMEOUT with mx %p", |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2218 | cv, mx ); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2219 | print_pthread_event(tid, msg_buf); |
| 2220 | } |
| 2221 | } else { |
| 2222 | /* Currently held. Make thread tid be blocked on it. */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2223 | vg_assert(mx->__vg_m_count > 0); |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 2224 | VG_TRACK( pre_mutex_lock, tid, mx ); |
| 2225 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2226 | VG_(threads)[tid].status = VgTs_WaitMX; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2227 | SET_PTHREQ_RETVAL(tid, ETIMEDOUT); /* pthread_cond_wait return value */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2228 | VG_(threads)[tid].associated_cv = NULL; |
| 2229 | VG_(threads)[tid].associated_mx = mx; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2230 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2231 | VG_(sprintf)(msg_buf, |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2232 | "pthread_cond_timedwait cv %p: TIMEOUT -> BLOCK for mx %p", |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2233 | cv, mx ); |
| 2234 | print_pthread_event(tid, msg_buf); |
| 2235 | } |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2240 | static |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2241 | void release_N_threads_waiting_on_cond ( vg_pthread_cond_t* cond, |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2242 | Int n_to_release, |
| 2243 | Char* caller ) |
| 2244 | { |
| 2245 | Int i; |
| 2246 | Char msg_buf[100]; |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2247 | vg_pthread_mutex_t* mx; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2248 | |
| 2249 | while (True) { |
| 2250 | if (n_to_release == 0) |
| 2251 | return; |
| 2252 | |
| 2253 | /* Find a thread waiting on this CV. */ |
| 2254 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2255 | if (VG_(threads)[i].status == VgTs_Empty) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2256 | continue; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2257 | if (VG_(threads)[i].status == VgTs_WaitCV |
| 2258 | && VG_(threads)[i].associated_cv == cond) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2259 | break; |
| 2260 | } |
| 2261 | vg_assert(i <= VG_N_THREADS); |
| 2262 | |
| 2263 | if (i == VG_N_THREADS) { |
| 2264 | /* Nobody else is waiting on it. */ |
| 2265 | return; |
| 2266 | } |
| 2267 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2268 | mx = VG_(threads)[i].associated_mx; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2269 | vg_assert(mx != NULL); |
| 2270 | |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 2271 | VG_TRACK( pre_mutex_lock, i, mx ); |
| 2272 | |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2273 | if (mx->__vg_m_owner == VG_INVALID_THREADID) { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2274 | /* Currently unheld; hand it out to thread i. */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2275 | vg_assert(mx->__vg_m_count == 0); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2276 | VG_(threads)[i].status = VgTs_Runnable; |
| 2277 | VG_(threads)[i].associated_cv = NULL; |
| 2278 | VG_(threads)[i].associated_mx = NULL; |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2279 | mx->__vg_m_owner = (/*_pthread_descr*/void*)i; |
| 2280 | mx->__vg_m_count = 1; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2281 | /* .m_edx already holds pth_cond_wait success value (0) */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2282 | |
sewardj | 0af43bc | 2002-10-22 04:30:35 +0000 | [diff] [blame] | 2283 | VG_TRACK( post_mutex_lock, i, mx ); |
| 2284 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2285 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2286 | VG_(sprintf)(msg_buf, "%s cv %p: RESUME with mx %p", |
| 2287 | caller, cond, mx ); |
| 2288 | print_pthread_event(i, msg_buf); |
| 2289 | } |
| 2290 | |
| 2291 | } else { |
| 2292 | /* Currently held. Make thread i be blocked on it. */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2293 | vg_assert(mx->__vg_m_count > 0); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2294 | VG_(threads)[i].status = VgTs_WaitMX; |
| 2295 | VG_(threads)[i].associated_cv = NULL; |
| 2296 | VG_(threads)[i].associated_mx = mx; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2297 | SET_PTHREQ_RETVAL(i, 0); /* pth_cond_wait success value */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2298 | |
| 2299 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2300 | VG_(sprintf)(msg_buf, "%s cv %p: BLOCK for mx %p", |
| 2301 | caller, cond, mx ); |
| 2302 | print_pthread_event(i, msg_buf); |
| 2303 | } |
| 2304 | |
| 2305 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2306 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2307 | n_to_release--; |
| 2308 | } |
| 2309 | } |
| 2310 | |
| 2311 | |
| 2312 | static |
| 2313 | void do_pthread_cond_wait ( ThreadId tid, |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2314 | vg_pthread_cond_t *cond, |
| 2315 | vg_pthread_mutex_t *mutex, |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2316 | UInt ms_end ) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2317 | { |
| 2318 | Char msg_buf[100]; |
| 2319 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2320 | /* If ms_end == 0xFFFFFFFF, wait forever (no timeout). Otherwise, |
| 2321 | ms_end is the ending millisecond. */ |
| 2322 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2323 | /* pre: mutex should be a valid mutex and owned by tid. */ |
| 2324 | if (VG_(clo_trace_pthread_level) >= 2) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2325 | VG_(sprintf)(msg_buf, "pthread_cond_wait cv %p, mx %p, end %d ...", |
| 2326 | cond, mutex, ms_end ); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2327 | print_pthread_event(tid, msg_buf); |
| 2328 | } |
| 2329 | |
| 2330 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2331 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2332 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2333 | |
nethercote | d3693d0 | 2004-04-26 08:05:24 +0000 | [diff] [blame] | 2334 | if (mutex == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2335 | VG_(record_pthread_error)( tid, |
nethercote | d3693d0 | 2004-04-26 08:05:24 +0000 | [diff] [blame] | 2336 | "pthread_cond_wait/timedwait: mutex is NULL"); |
| 2337 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
| 2338 | return; |
| 2339 | } |
| 2340 | |
| 2341 | if (cond == NULL) { |
| 2342 | VG_(record_pthread_error)( tid, |
| 2343 | "pthread_cond_wait/timedwait: cond is NULL"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2344 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2345 | return; |
| 2346 | } |
| 2347 | |
| 2348 | /* More paranoia ... */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2349 | switch (mutex->__vg_m_kind) { |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2350 | # ifndef GLIBC_2_1 |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2351 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2352 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
| 2353 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2354 | # ifdef GLIBC_2_1 |
sewardj | 8e651d7 | 2002-05-10 21:00:19 +0000 | [diff] [blame] | 2355 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2356 | # endif |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2357 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 2358 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2359 | if (mutex->__vg_m_count >= 0) break; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2360 | /* else fall thru */ |
| 2361 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2362 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2363 | "pthread_cond_wait/timedwait: mutex is invalid"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2364 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2365 | return; |
| 2366 | } |
| 2367 | |
| 2368 | /* Barf if we don't currently hold the mutex. */ |
nethercote | d3693d0 | 2004-04-26 08:05:24 +0000 | [diff] [blame] | 2369 | if (mutex->__vg_m_count == 0 /* nobody holds it */) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2370 | VG_(record_pthread_error)( tid, |
nethercote | d3693d0 | 2004-04-26 08:05:24 +0000 | [diff] [blame] | 2371 | "pthread_cond_wait/timedwait: mutex is unlocked"); |
| 2372 | SET_PTHREQ_RETVAL(tid, VKI_EPERM); |
| 2373 | return; |
| 2374 | } |
| 2375 | |
| 2376 | if ((ThreadId)mutex->__vg_m_owner != tid /* we don't hold it */) { |
| 2377 | VG_(record_pthread_error)( tid, |
| 2378 | "pthread_cond_wait/timedwait: mutex is locked by another thread"); |
| 2379 | SET_PTHREQ_RETVAL(tid, VKI_EPERM); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2380 | return; |
| 2381 | } |
| 2382 | |
thughes | 513197c | 2004-06-13 12:07:53 +0000 | [diff] [blame] | 2383 | if(VG_(threads)[tid].cancel_pend != NULL && |
| 2384 | VG_(threads)[tid].cancel_st) { |
| 2385 | make_thread_jump_to_cancelhdlr ( tid ); |
| 2386 | } else { |
| 2387 | /* Queue ourselves on the condition. */ |
| 2388 | VG_(threads)[tid].status = VgTs_WaitCV; |
| 2389 | VG_(threads)[tid].associated_cv = cond; |
| 2390 | VG_(threads)[tid].associated_mx = mutex; |
| 2391 | VG_(threads)[tid].awaken_at = ms_end; |
| 2392 | if (ms_end != 0xFFFFFFFF) |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 2393 | add_timeout(tid, ms_end); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2394 | |
thughes | 513197c | 2004-06-13 12:07:53 +0000 | [diff] [blame] | 2395 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2396 | VG_(sprintf)(msg_buf, |
| 2397 | "pthread_cond_wait cv %p, mx %p: BLOCK", |
| 2398 | cond, mutex ); |
| 2399 | print_pthread_event(tid, msg_buf); |
| 2400 | } |
| 2401 | |
| 2402 | /* Release the mutex. */ |
| 2403 | release_one_thread_waiting_on_mutex ( mutex, "pthread_cond_wait " ); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2404 | } |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | |
| 2408 | static |
| 2409 | void do_pthread_cond_signal_or_broadcast ( ThreadId tid, |
| 2410 | Bool broadcast, |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 2411 | vg_pthread_cond_t *cond ) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2412 | { |
| 2413 | Char msg_buf[100]; |
| 2414 | Char* caller |
| 2415 | = broadcast ? "pthread_cond_broadcast" |
| 2416 | : "pthread_cond_signal "; |
| 2417 | |
| 2418 | if (VG_(clo_trace_pthread_level) >= 2) { |
| 2419 | VG_(sprintf)(msg_buf, "%s cv %p ...", |
| 2420 | caller, cond ); |
| 2421 | print_pthread_event(tid, msg_buf); |
| 2422 | } |
| 2423 | |
| 2424 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2425 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2426 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2427 | |
| 2428 | if (cond == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2429 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2430 | "pthread_cond_signal/broadcast: cond is NULL"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2431 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2432 | return; |
| 2433 | } |
| 2434 | |
| 2435 | release_N_threads_waiting_on_cond ( |
| 2436 | cond, |
| 2437 | broadcast ? VG_N_THREADS : 1, |
| 2438 | caller |
| 2439 | ); |
| 2440 | |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2441 | SET_PTHREQ_RETVAL(tid, 0); /* success */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2444 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2445 | /* ----------------------------------------------------------- |
| 2446 | THREAD SPECIFIC DATA |
| 2447 | -------------------------------------------------------- */ |
| 2448 | |
| 2449 | static __inline__ |
| 2450 | Bool is_valid_key ( ThreadKey k ) |
| 2451 | { |
| 2452 | /* k unsigned; hence no < 0 check */ |
| 2453 | if (k >= VG_N_THREAD_KEYS) return False; |
| 2454 | if (!vg_thread_keys[k].inuse) return False; |
| 2455 | return True; |
| 2456 | } |
| 2457 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2458 | |
| 2459 | /* Return in %EDX a value of 1 if the key is valid, else 0. */ |
| 2460 | static |
| 2461 | void do_pthread_key_validate ( ThreadId tid, |
| 2462 | pthread_key_t key ) |
| 2463 | { |
| 2464 | Char msg_buf[100]; |
| 2465 | |
| 2466 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2467 | VG_(sprintf)(msg_buf, "pthread_key_validate key %p", |
| 2468 | key ); |
| 2469 | print_pthread_event(tid, msg_buf); |
| 2470 | } |
| 2471 | |
| 2472 | vg_assert(sizeof(pthread_key_t) == sizeof(ThreadKey)); |
| 2473 | vg_assert(VG_(is_valid_tid)(tid) |
| 2474 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 2475 | |
| 2476 | if (is_valid_key((ThreadKey)key)) { |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2477 | SET_PTHREQ_RETVAL(tid, 1); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2478 | } else { |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2479 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2484 | static |
| 2485 | void do_pthread_key_create ( ThreadId tid, |
| 2486 | pthread_key_t* key, |
| 2487 | void (*destructor)(void*) ) |
| 2488 | { |
| 2489 | Int i; |
| 2490 | Char msg_buf[100]; |
| 2491 | |
| 2492 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2493 | VG_(sprintf)(msg_buf, "pthread_key_create *key %p, destr %p", |
| 2494 | key, destructor ); |
| 2495 | print_pthread_event(tid, msg_buf); |
| 2496 | } |
| 2497 | |
| 2498 | vg_assert(sizeof(pthread_key_t) == sizeof(ThreadKey)); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2499 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2500 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2501 | |
| 2502 | for (i = 0; i < VG_N_THREAD_KEYS; i++) |
| 2503 | if (!vg_thread_keys[i].inuse) |
| 2504 | break; |
| 2505 | |
| 2506 | if (i == VG_N_THREAD_KEYS) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2507 | VG_(message)(Vg_UserMsg, "pthread_key_create() asked for too many keys (more than %d): increase VG_N_THREAD_KEYS and recompile Valgrind.", |
| 2508 | VG_N_THREAD_KEYS); |
| 2509 | SET_PTHREQ_RETVAL(tid, EAGAIN); |
| 2510 | return; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 2513 | vg_thread_keys[i].inuse = True; |
| 2514 | vg_thread_keys[i].destructor = destructor; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2515 | |
sewardj | 5a3798b | 2002-06-04 23:24:22 +0000 | [diff] [blame] | 2516 | /* check key for addressibility */ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2517 | VG_TRACK( pre_mem_write, Vg_CorePThread, tid, "pthread_key_create: key", |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2518 | (Addr)key, sizeof(pthread_key_t)); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2519 | *key = i; |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2520 | VG_TRACK( post_mem_write, Vg_CorePThread, tid, |
| 2521 | (Addr)key, sizeof(pthread_key_t) ); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2522 | |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2523 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | |
| 2527 | static |
| 2528 | void do_pthread_key_delete ( ThreadId tid, pthread_key_t key ) |
| 2529 | { |
| 2530 | Char msg_buf[100]; |
| 2531 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2532 | VG_(sprintf)(msg_buf, "pthread_key_delete key %d", |
| 2533 | key ); |
| 2534 | print_pthread_event(tid, msg_buf); |
| 2535 | } |
| 2536 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2537 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2538 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2539 | |
| 2540 | if (!is_valid_key(key)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2541 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2542 | "pthread_key_delete: key is invalid"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2543 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2544 | return; |
| 2545 | } |
| 2546 | |
| 2547 | vg_thread_keys[key].inuse = False; |
sewardj | 648b315 | 2002-12-09 19:07:59 +0000 | [diff] [blame] | 2548 | vg_thread_keys[key].destructor = NULL; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2549 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2553 | /* Get the .specific_ptr for a thread. Return 1 if the thread-slot |
| 2554 | isn't in use, so that client-space can scan all thread slots. 1 |
| 2555 | cannot be confused with NULL or a legitimately-aligned specific_ptr |
| 2556 | value. */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2557 | static |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2558 | void do_pthread_getspecific_ptr ( ThreadId tid ) |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2559 | { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2560 | void** specifics_ptr; |
| 2561 | Char msg_buf[100]; |
| 2562 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2563 | if (VG_(clo_trace_pthread_level) >= 2) { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2564 | VG_(sprintf)(msg_buf, "pthread_getspecific_ptr" ); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2565 | print_pthread_event(tid, msg_buf); |
| 2566 | } |
| 2567 | |
nethercote | 36881a2 | 2004-08-04 14:03:16 +0000 | [diff] [blame] | 2568 | vg_assert(is_valid_or_empty_tid(tid)); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2569 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2570 | if (VG_(threads)[tid].status == VgTs_Empty) { |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2571 | SET_PTHREQ_RETVAL(tid, 1); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2572 | return; |
| 2573 | } |
| 2574 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2575 | specifics_ptr = VG_(threads)[tid].specifics_ptr; |
nethercote | 5fd72bb | 2004-11-04 19:28:38 +0000 | [diff] [blame] | 2576 | vg_assert(specifics_ptr == NULL || IS_ALIGNED4_ADDR(specifics_ptr)); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2577 | |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 2578 | SET_PTHREQ_RETVAL(tid, (UWord)specifics_ptr); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | |
| 2582 | static |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2583 | void do_pthread_setspecific_ptr ( ThreadId tid, void** ptr ) |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2584 | { |
| 2585 | Char msg_buf[100]; |
| 2586 | if (VG_(clo_trace_pthread_level) >= 1) { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2587 | VG_(sprintf)(msg_buf, "pthread_setspecific_ptr ptr %p", |
| 2588 | ptr ); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2589 | print_pthread_event(tid, msg_buf); |
| 2590 | } |
| 2591 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2592 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2593 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2594 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2595 | VG_(threads)[tid].specifics_ptr = ptr; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2596 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 2600 | /* Helper for calling destructors at thread exit. If key is valid, |
| 2601 | copy the thread's specific value into cu->arg and put the *key*'s |
| 2602 | destructor fn address in cu->fn. Then return 0 to the caller. |
| 2603 | Otherwise return non-zero to the caller. */ |
| 2604 | static |
| 2605 | void do__get_key_destr_and_spec ( ThreadId tid, |
| 2606 | pthread_key_t key, |
| 2607 | CleanupEntry* cu ) |
| 2608 | { |
| 2609 | Char msg_buf[100]; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2610 | if (VG_(clo_trace_pthread_level) >= 2) { |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 2611 | VG_(sprintf)(msg_buf, |
| 2612 | "get_key_destr_and_arg (key = %d)", key ); |
| 2613 | print_pthread_event(tid, msg_buf); |
| 2614 | } |
| 2615 | vg_assert(VG_(is_valid_tid)(tid)); |
| 2616 | vg_assert(key >= 0 && key < VG_N_THREAD_KEYS); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2617 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 2618 | if (!vg_thread_keys[key].inuse) { |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2619 | SET_PTHREQ_RETVAL(tid, -1); |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 2620 | return; |
| 2621 | } |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2622 | VG_TRACK( pre_mem_write, Vg_CorePThread, tid, "get_key_destr_and_spec: cu", |
| 2623 | (Addr)cu, sizeof(CleanupEntry) ); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2624 | |
thughes | 11975ff | 2004-06-12 12:58:22 +0000 | [diff] [blame] | 2625 | cu->type = VgCt_Function; |
| 2626 | cu->data.function.fn = vg_thread_keys[key].destructor; |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2627 | if (VG_(threads)[tid].specifics_ptr == NULL) { |
thughes | 11975ff | 2004-06-12 12:58:22 +0000 | [diff] [blame] | 2628 | cu->data.function.arg = NULL; |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2629 | } else { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2630 | VG_TRACK( pre_mem_read, Vg_CorePThread, tid, |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2631 | "get_key_destr_and_spec: key", |
| 2632 | (Addr)(&VG_(threads)[tid].specifics_ptr[key]), |
| 2633 | sizeof(void*) ); |
thughes | 11975ff | 2004-06-12 12:58:22 +0000 | [diff] [blame] | 2634 | cu->data.function.arg = VG_(threads)[tid].specifics_ptr[key]; |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2637 | VG_TRACK( post_mem_write, Vg_CorePThread, tid, |
| 2638 | (Addr)cu, sizeof(CleanupEntry) ); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2639 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2643 | /* --------------------------------------------------- |
| 2644 | SIGNALS |
| 2645 | ------------------------------------------------ */ |
| 2646 | |
| 2647 | /* See comment in vg_libthread.c:pthread_sigmask() regarding |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2648 | deliberate confusion of types sigset_t and vki_sigset_t. Return 0 |
| 2649 | for OK and 1 for some kind of addressing error, which the |
| 2650 | vg_libpthread.c routine turns into return values 0 and EFAULT |
| 2651 | respectively. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2652 | static |
| 2653 | void do_pthread_sigmask ( ThreadId tid, |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2654 | Int vki_how, |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 2655 | vki_sigset_t* newmask, |
| 2656 | vki_sigset_t* oldmask ) |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2657 | { |
| 2658 | Char msg_buf[100]; |
| 2659 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2660 | VG_(sprintf)(msg_buf, |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2661 | "pthread_sigmask vki_how %d, newmask %p, oldmask %p", |
| 2662 | vki_how, newmask, oldmask ); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2663 | print_pthread_event(tid, msg_buf); |
| 2664 | } |
| 2665 | |
| 2666 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2667 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2668 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2669 | if (newmask) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2670 | VG_TRACK( pre_mem_read, Vg_CorePThread, tid, "pthread_sigmask: newmask", |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 2671 | (Addr)newmask, sizeof(vki_sigset_t)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2672 | if (oldmask) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2673 | VG_TRACK( pre_mem_write, Vg_CorePThread, tid, "pthread_sigmask: oldmask", |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 2674 | (Addr)oldmask, sizeof(vki_sigset_t)); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2675 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2676 | VG_(do_pthread_sigmask_SCSS_upd) ( tid, vki_how, newmask, oldmask ); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2677 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2678 | if (oldmask) |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2679 | VG_TRACK( post_mem_write, Vg_CorePThread, tid, |
| 2680 | (Addr)oldmask, sizeof(vki_sigset_t) ); |
sewardj | 3a951cf | 2002-05-15 22:25:47 +0000 | [diff] [blame] | 2681 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2682 | /* Success. */ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2683 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | |
| 2687 | static |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2688 | void do_pthread_kill ( ThreadId tid, /* me */ |
| 2689 | ThreadId thread, /* thread to signal */ |
| 2690 | Int sig ) |
| 2691 | { |
nethercote | 97ccd5e | 2004-08-02 12:10:01 +0000 | [diff] [blame] | 2692 | ThreadState* tst; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2693 | Char msg_buf[100]; |
| 2694 | |
| 2695 | if (VG_(clo_trace_signals) || VG_(clo_trace_pthread_level) >= 1) { |
| 2696 | VG_(sprintf)(msg_buf, |
| 2697 | "pthread_kill thread %d, signo %d", |
| 2698 | thread, sig ); |
| 2699 | print_pthread_event(tid, msg_buf); |
| 2700 | } |
| 2701 | |
| 2702 | vg_assert(VG_(is_valid_tid)(tid) |
| 2703 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 2704 | |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2705 | if (!VG_(is_valid_tid)(thread)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2706 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2707 | "pthread_kill: invalid target thread"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2708 | SET_PTHREQ_RETVAL(tid, VKI_ESRCH); |
| 2709 | return; |
| 2710 | } |
| 2711 | |
| 2712 | if (sig == 0) { |
| 2713 | /* OK, signal 0 is just for testing */ |
| 2714 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2715 | return; |
| 2716 | } |
| 2717 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 2718 | if (sig < 1 || sig > _VKI_NSIG) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2719 | SET_PTHREQ_RETVAL(tid, VKI_EINVAL); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2720 | return; |
| 2721 | } |
| 2722 | |
nethercote | 97ccd5e | 2004-08-02 12:10:01 +0000 | [diff] [blame] | 2723 | tst = VG_(get_ThreadState)(thread); |
| 2724 | vg_assert(NULL != tst->proxy); |
sewardj | 0a785fd | 2004-11-24 21:24:24 +0000 | [diff] [blame] | 2725 | VG_(proxy_sendsig)(tid/*from*/, thread/*to*/, sig); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2726 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2730 | /* ----------------------------------------------------------- |
| 2731 | FORK HANDLERS. |
| 2732 | -------------------------------------------------------- */ |
| 2733 | |
| 2734 | static |
| 2735 | void do__set_fhstack_used ( ThreadId tid, Int n ) |
| 2736 | { |
| 2737 | Char msg_buf[100]; |
| 2738 | if (VG_(clo_trace_sched)) { |
| 2739 | VG_(sprintf)(msg_buf, "set_fhstack_used to %d", n ); |
| 2740 | print_pthread_event(tid, msg_buf); |
| 2741 | } |
| 2742 | |
| 2743 | vg_assert(VG_(is_valid_tid)(tid) |
| 2744 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 2745 | |
| 2746 | if (n >= 0 && n < VG_N_FORKHANDLERSTACK) { |
| 2747 | vg_fhstack_used = n; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2748 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2749 | } else { |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2750 | SET_PTHREQ_RETVAL(tid, -1); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | |
| 2755 | static |
| 2756 | void do__get_fhstack_used ( ThreadId tid ) |
| 2757 | { |
| 2758 | Int n; |
| 2759 | Char msg_buf[100]; |
| 2760 | if (VG_(clo_trace_sched)) { |
| 2761 | VG_(sprintf)(msg_buf, "get_fhstack_used" ); |
| 2762 | print_pthread_event(tid, msg_buf); |
| 2763 | } |
| 2764 | |
| 2765 | vg_assert(VG_(is_valid_tid)(tid) |
| 2766 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 2767 | |
| 2768 | n = vg_fhstack_used; |
| 2769 | vg_assert(n >= 0 && n < VG_N_FORKHANDLERSTACK); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2770 | SET_PTHREQ_RETVAL(tid, n); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | static |
| 2774 | void do__set_fhstack_entry ( ThreadId tid, Int n, ForkHandlerEntry* fh ) |
| 2775 | { |
| 2776 | Char msg_buf[100]; |
| 2777 | if (VG_(clo_trace_sched)) { |
| 2778 | VG_(sprintf)(msg_buf, "set_fhstack_entry %d to %p", n, fh ); |
| 2779 | print_pthread_event(tid, msg_buf); |
| 2780 | } |
| 2781 | |
| 2782 | vg_assert(VG_(is_valid_tid)(tid) |
| 2783 | && VG_(threads)[tid].status == VgTs_Runnable); |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2784 | VG_TRACK( pre_mem_read, Vg_CorePThread, tid, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2785 | "pthread_atfork: prepare/parent/child", |
| 2786 | (Addr)fh, sizeof(ForkHandlerEntry)); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2787 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2788 | if (n < 0 || n >= VG_N_FORKHANDLERSTACK) { |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2789 | SET_PTHREQ_RETVAL(tid, -1); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2790 | return; |
| 2791 | } |
| 2792 | |
| 2793 | vg_fhstack[n] = *fh; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2794 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | |
| 2798 | static |
| 2799 | void do__get_fhstack_entry ( ThreadId tid, Int n, /*OUT*/ |
| 2800 | ForkHandlerEntry* fh ) |
| 2801 | { |
| 2802 | Char msg_buf[100]; |
| 2803 | if (VG_(clo_trace_sched)) { |
| 2804 | VG_(sprintf)(msg_buf, "get_fhstack_entry %d", n ); |
| 2805 | print_pthread_event(tid, msg_buf); |
| 2806 | } |
| 2807 | |
| 2808 | vg_assert(VG_(is_valid_tid)(tid) |
| 2809 | && VG_(threads)[tid].status == VgTs_Runnable); |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2810 | VG_TRACK( pre_mem_write, Vg_CorePThread, tid, "fork: prepare/parent/child", |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2811 | (Addr)fh, sizeof(ForkHandlerEntry)); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2812 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2813 | if (n < 0 || n >= VG_N_FORKHANDLERSTACK) { |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2814 | SET_PTHREQ_RETVAL(tid, -1); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2815 | return; |
| 2816 | } |
| 2817 | |
| 2818 | *fh = vg_fhstack[n]; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2819 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2820 | |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2821 | VG_TRACK( post_mem_write, Vg_CorePThread, tid, |
| 2822 | (Addr)fh, sizeof(ForkHandlerEntry) ); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
thughes | daa3456 | 2004-06-27 12:48:53 +0000 | [diff] [blame] | 2825 | |
| 2826 | static |
| 2827 | void do__get_stack_info ( ThreadId tid, ThreadId which, StackInfo* si ) |
| 2828 | { |
| 2829 | Char msg_buf[100]; |
| 2830 | |
| 2831 | vg_assert(VG_(is_valid_tid)(tid) |
| 2832 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 2833 | |
| 2834 | if (VG_(clo_trace_sched)) { |
| 2835 | VG_(sprintf)(msg_buf, "get_stack_info for tid %d", which ); |
| 2836 | print_pthread_event(tid, msg_buf); |
| 2837 | } |
| 2838 | |
| 2839 | if (!VG_(is_valid_tid)(which)) { |
| 2840 | SET_PTHREQ_RETVAL(tid, -1); |
| 2841 | return; |
| 2842 | } |
| 2843 | |
| 2844 | si->base = VG_(threads)[which].stack_base; |
| 2845 | si->size = VG_(threads)[which].stack_size |
| 2846 | - VG_AR_CLIENT_STACKBASE_REDZONE_SZB |
| 2847 | - VG_(threads)[which].stack_guard_size; |
| 2848 | si->guardsize = VG_(threads)[which].stack_guard_size; |
| 2849 | |
| 2850 | SET_PTHREQ_RETVAL(tid, 0); |
| 2851 | } |
| 2852 | |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2853 | /* --------------------------------------------------------------------- |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2854 | Shadow register manipulations |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2855 | ------------------------------------------------------------------ */ |
| 2856 | |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2857 | void VG_(set_shadow_regs_area) ( ThreadId tid, OffT offset, SizeT size, |
| 2858 | const UChar* area ) |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2859 | { |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2860 | ThreadState* tst; |
| 2861 | |
| 2862 | vg_assert(VG_(is_valid_tid)(tid)); |
| 2863 | tst = & VG_(threads)[tid]; |
| 2864 | |
| 2865 | // Bounds check |
| 2866 | vg_assert(0 <= offset && offset < sizeof(VexGuestArchState)); |
| 2867 | vg_assert(offset + size <= sizeof(VexGuestArchState)); |
| 2868 | |
| 2869 | VG_(memcpy)( (void*)(((Addr)(&tst->arch.vex_shadow)) + offset), area, size); |
| 2870 | } |
| 2871 | |
| 2872 | void VG_(get_shadow_regs_area) ( ThreadId tid, OffT offset, SizeT size, |
| 2873 | UChar* area ) |
| 2874 | { |
| 2875 | ThreadState* tst; |
| 2876 | |
| 2877 | vg_assert(VG_(is_valid_tid)(tid)); |
| 2878 | tst = & VG_(threads)[tid]; |
| 2879 | |
| 2880 | // Bounds check |
| 2881 | vg_assert(0 <= offset && offset < sizeof(VexGuestArchState)); |
| 2882 | vg_assert(offset + size <= sizeof(VexGuestArchState)); |
| 2883 | |
| 2884 | VG_(memcpy)( area, (void*)(((Addr)&(tst->arch.vex_shadow)) + offset), size); |
| 2885 | } |
| 2886 | |
| 2887 | |
| 2888 | void VG_(set_return_from_syscall_shadow) ( ThreadId tid, UWord ret_shadow ) |
| 2889 | { |
| 2890 | VG_(set_shadow_regs_area)(tid, O_SYSCALL_RET, sizeof(UWord), |
| 2891 | (UChar*)&ret_shadow); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2894 | UInt VG_(get_exit_status_shadow) ( ThreadId tid ) |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2895 | { |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2896 | UInt ret; |
| 2897 | VG_(get_shadow_regs_area)(tid, O_SYSCALL_ARG1, sizeof(UInt), |
| 2898 | (UChar*)&ret); |
| 2899 | return ret; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 2902 | void VG_(intercept_libc_freeres_wrapper)(Addr addr) |
| 2903 | { |
nethercote | f971ab7 | 2004-08-02 16:27:40 +0000 | [diff] [blame] | 2904 | __libc_freeres_wrapper = addr; |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 2905 | } |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2906 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2907 | /* --------------------------------------------------------------------- |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2908 | Handle client requests. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2909 | ------------------------------------------------------------------ */ |
| 2910 | |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2911 | /* Do a client request for the thread tid. After the request, tid may |
| 2912 | or may not still be runnable; if not, the scheduler will have to |
| 2913 | choose a new thread to run. |
| 2914 | */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2915 | static |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 2916 | void do_client_request ( ThreadId tid, UWord* arg ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2917 | { |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 2918 | UWord req_no = arg[0]; |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2919 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2920 | if (0) |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 2921 | VG_(printf)("req no = 0x%llx, arg = %p\n", (ULong)req_no, arg); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2922 | switch (req_no) { |
| 2923 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2924 | case VG_USERREQ__CLIENT_CALL0: { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2925 | UWord (*f)(ThreadId) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2926 | if (f == NULL) |
nethercote | 66b3af6 | 2004-09-11 13:06:55 +0000 | [diff] [blame] | 2927 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL0: func=%p\n", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2928 | else |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2929 | SET_CLCALL_RETVAL(tid, f ( tid ), (Addr)f); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2930 | break; |
| 2931 | } |
| 2932 | case VG_USERREQ__CLIENT_CALL1: { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2933 | UWord (*f)(ThreadId, UWord) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2934 | if (f == NULL) |
nethercote | 66b3af6 | 2004-09-11 13:06:55 +0000 | [diff] [blame] | 2935 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL1: func=%p\n", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2936 | else |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2937 | SET_CLCALL_RETVAL(tid, f ( tid, arg[2] ), (Addr)f ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2938 | break; |
| 2939 | } |
| 2940 | case VG_USERREQ__CLIENT_CALL2: { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2941 | UWord (*f)(ThreadId, UWord, UWord) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2942 | if (f == NULL) |
nethercote | 66b3af6 | 2004-09-11 13:06:55 +0000 | [diff] [blame] | 2943 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL2: func=%p\n", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2944 | else |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2945 | SET_CLCALL_RETVAL(tid, f ( tid, arg[2], arg[3] ), (Addr)f ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2946 | break; |
| 2947 | } |
| 2948 | case VG_USERREQ__CLIENT_CALL3: { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2949 | UWord (*f)(ThreadId, UWord, UWord, UWord) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2950 | if (f == NULL) |
nethercote | 66b3af6 | 2004-09-11 13:06:55 +0000 | [diff] [blame] | 2951 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL3: func=%p\n", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2952 | else |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2953 | SET_CLCALL_RETVAL(tid, f ( tid, arg[2], arg[3], arg[4] ), (Addr)f ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2954 | break; |
| 2955 | } |
| 2956 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 2957 | /* Note: for tools that replace malloc() et al, we want to call |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2958 | the replacement versions. For those that don't, we want to call |
njn | 26f0251 | 2004-11-22 18:33:15 +0000 | [diff] [blame] | 2959 | VG_(cli_malloc)() et al. We do this by calling TL_(malloc)(), which |
nethercote | 3ced0e3 | 2004-01-26 14:50:45 +0000 | [diff] [blame] | 2960 | malloc-replacing tools must replace, but have the default definition |
njn | 26f0251 | 2004-11-22 18:33:15 +0000 | [diff] [blame] | 2961 | of TL_(malloc)() call VG_(cli_malloc)(). */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2962 | |
| 2963 | /* Note: for MALLOC and FREE, must set the appropriate "lock"... see |
njn | 26f0251 | 2004-11-22 18:33:15 +0000 | [diff] [blame] | 2964 | the comment in vg_defaults.c/TL_(malloc)() for why. */ |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2965 | case VG_USERREQ__MALLOC: |
njn | d225283 | 2004-11-26 10:53:33 +0000 | [diff] [blame] | 2966 | VG_(tl_malloc_called_by_scheduler) = True; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2967 | SET_PTHREQ_RETVAL( |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2968 | tid, (Addr)TL_(malloc) ( tid, arg[1] ) |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2969 | ); |
njn | d225283 | 2004-11-26 10:53:33 +0000 | [diff] [blame] | 2970 | VG_(tl_malloc_called_by_scheduler) = False; |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2971 | break; |
| 2972 | |
| 2973 | case VG_USERREQ__FREE: |
njn | d225283 | 2004-11-26 10:53:33 +0000 | [diff] [blame] | 2974 | VG_(tl_malloc_called_by_scheduler) = True; |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2975 | TL_(free) ( tid, (void*)arg[1] ); |
njn | d225283 | 2004-11-26 10:53:33 +0000 | [diff] [blame] | 2976 | VG_(tl_malloc_called_by_scheduler) = False; |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2977 | SET_PTHREQ_RETVAL(tid, 0); /* irrelevant */ |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2978 | break; |
| 2979 | |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2980 | case VG_USERREQ__PTHREAD_GET_THREADID: |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2981 | SET_PTHREQ_RETVAL(tid, tid); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2982 | break; |
| 2983 | |
| 2984 | case VG_USERREQ__RUNNING_ON_VALGRIND: |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2985 | SET_CLREQ_RETVAL(tid, 1); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2986 | break; |
| 2987 | |
| 2988 | case VG_USERREQ__GET_PTHREAD_TRACE_LEVEL: |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2989 | SET_PTHREQ_RETVAL(tid, VG_(clo_trace_pthread_level)); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2990 | break; |
| 2991 | |
| 2992 | case VG_USERREQ__READ_MILLISECOND_TIMER: |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2993 | SET_PTHREQ_RETVAL(tid, VG_(read_millisecond_timer)()); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2994 | break; |
| 2995 | |
| 2996 | /* Some of these may make thread tid non-runnable, but the |
| 2997 | scheduler checks for that on return from this function. */ |
| 2998 | case VG_USERREQ__PTHREAD_MUTEX_LOCK: |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 2999 | do_pthread_mutex_lock( tid, False, (void *)(arg[1]), 0xFFFFFFFF ); |
| 3000 | break; |
| 3001 | |
| 3002 | case VG_USERREQ__PTHREAD_MUTEX_TIMEDLOCK: |
| 3003 | do_pthread_mutex_lock( tid, False, (void *)(arg[1]), arg[2] ); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3004 | break; |
| 3005 | |
| 3006 | case VG_USERREQ__PTHREAD_MUTEX_TRYLOCK: |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 3007 | do_pthread_mutex_lock( tid, True, (void *)(arg[1]), 0xFFFFFFFF ); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3008 | break; |
| 3009 | |
| 3010 | case VG_USERREQ__PTHREAD_MUTEX_UNLOCK: |
| 3011 | do_pthread_mutex_unlock( tid, (void *)(arg[1]) ); |
| 3012 | break; |
| 3013 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 3014 | case VG_USERREQ__PTHREAD_GETSPECIFIC_PTR: |
| 3015 | do_pthread_getspecific_ptr ( tid ); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3016 | break; |
| 3017 | |
| 3018 | case VG_USERREQ__SET_CANCELTYPE: |
| 3019 | do__set_canceltype ( tid, arg[1] ); |
| 3020 | break; |
| 3021 | |
| 3022 | case VG_USERREQ__CLEANUP_PUSH: |
| 3023 | do__cleanup_push ( tid, (CleanupEntry*)(arg[1]) ); |
| 3024 | break; |
| 3025 | |
| 3026 | case VG_USERREQ__CLEANUP_POP: |
| 3027 | do__cleanup_pop ( tid, (CleanupEntry*)(arg[1]) ); |
| 3028 | break; |
| 3029 | |
| 3030 | case VG_USERREQ__TESTCANCEL: |
| 3031 | do__testcancel ( tid ); |
| 3032 | break; |
| 3033 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3034 | case VG_USERREQ__PTHREAD_JOIN: |
| 3035 | do_pthread_join( tid, arg[1], (void**)(arg[2]) ); |
| 3036 | break; |
| 3037 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3038 | case VG_USERREQ__PTHREAD_COND_WAIT: |
| 3039 | do_pthread_cond_wait( tid, |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 3040 | (vg_pthread_cond_t *)(arg[1]), |
| 3041 | (vg_pthread_mutex_t *)(arg[2]), |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3042 | 0xFFFFFFFF /* no timeout */ ); |
| 3043 | break; |
| 3044 | |
| 3045 | case VG_USERREQ__PTHREAD_COND_TIMEDWAIT: |
| 3046 | do_pthread_cond_wait( tid, |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 3047 | (vg_pthread_cond_t *)(arg[1]), |
| 3048 | (vg_pthread_mutex_t *)(arg[2]), |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3049 | arg[3] /* timeout millisecond point */ ); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3050 | break; |
| 3051 | |
| 3052 | case VG_USERREQ__PTHREAD_COND_SIGNAL: |
| 3053 | do_pthread_cond_signal_or_broadcast( |
| 3054 | tid, |
| 3055 | False, /* signal, not broadcast */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 3056 | (vg_pthread_cond_t *)(arg[1]) ); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3057 | break; |
| 3058 | |
| 3059 | case VG_USERREQ__PTHREAD_COND_BROADCAST: |
| 3060 | do_pthread_cond_signal_or_broadcast( |
| 3061 | tid, |
| 3062 | True, /* broadcast, not signal */ |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 3063 | (vg_pthread_cond_t *)(arg[1]) ); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3064 | break; |
| 3065 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 3066 | case VG_USERREQ__PTHREAD_KEY_VALIDATE: |
| 3067 | do_pthread_key_validate ( tid, |
| 3068 | (pthread_key_t)(arg[1]) ); |
| 3069 | break; |
| 3070 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3071 | case VG_USERREQ__PTHREAD_KEY_CREATE: |
| 3072 | do_pthread_key_create ( tid, |
| 3073 | (pthread_key_t*)(arg[1]), |
| 3074 | (void(*)(void*))(arg[2]) ); |
| 3075 | break; |
| 3076 | |
| 3077 | case VG_USERREQ__PTHREAD_KEY_DELETE: |
| 3078 | do_pthread_key_delete ( tid, |
| 3079 | (pthread_key_t)(arg[1]) ); |
| 3080 | break; |
| 3081 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 3082 | case VG_USERREQ__PTHREAD_SETSPECIFIC_PTR: |
| 3083 | do_pthread_setspecific_ptr ( tid, |
| 3084 | (void**)(arg[1]) ); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3085 | break; |
| 3086 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3087 | case VG_USERREQ__PTHREAD_SIGMASK: |
| 3088 | do_pthread_sigmask ( tid, |
| 3089 | arg[1], |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 3090 | (vki_sigset_t*)(arg[2]), |
| 3091 | (vki_sigset_t*)(arg[3]) ); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3092 | break; |
| 3093 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3094 | case VG_USERREQ__PTHREAD_KILL: |
| 3095 | do_pthread_kill ( tid, arg[1], arg[2] ); |
| 3096 | break; |
| 3097 | |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 3098 | case VG_USERREQ__PTHREAD_YIELD: |
| 3099 | do_pthread_yield ( tid ); |
sewardj | 18a62ff | 2002-07-12 22:30:51 +0000 | [diff] [blame] | 3100 | /* On return from do_client_request(), the scheduler will |
| 3101 | select a new thread to run. */ |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 3102 | break; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3103 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 3104 | case VG_USERREQ__SET_CANCELSTATE: |
| 3105 | do__set_cancelstate ( tid, arg[1] ); |
| 3106 | break; |
| 3107 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 3108 | case VG_USERREQ__SET_OR_GET_DETACH: |
| 3109 | do__set_or_get_detach ( tid, arg[1], arg[2] ); |
| 3110 | break; |
| 3111 | |
| 3112 | case VG_USERREQ__SET_CANCELPEND: |
| 3113 | do__set_cancelpend ( tid, arg[1], (void(*)(void*))arg[2] ); |
| 3114 | break; |
| 3115 | |
| 3116 | case VG_USERREQ__WAIT_JOINER: |
| 3117 | do__wait_joiner ( tid, (void*)arg[1] ); |
| 3118 | break; |
| 3119 | |
| 3120 | case VG_USERREQ__QUIT: |
| 3121 | do__quit ( tid ); |
| 3122 | break; |
| 3123 | |
| 3124 | case VG_USERREQ__APPLY_IN_NEW_THREAD: |
| 3125 | do__apply_in_new_thread ( tid, (void*(*)(void*))arg[1], |
thughes | daa3456 | 2004-06-27 12:48:53 +0000 | [diff] [blame] | 3126 | (void*)arg[2], (StackInfo*)(arg[3]) ); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 3127 | break; |
| 3128 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 3129 | case VG_USERREQ__GET_KEY_D_AND_S: |
| 3130 | do__get_key_destr_and_spec ( tid, |
| 3131 | (pthread_key_t)arg[1], |
| 3132 | (CleanupEntry*)arg[2] ); |
| 3133 | break; |
| 3134 | |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 3135 | case VG_USERREQ__NUKE_OTHER_THREADS: |
| 3136 | VG_(nuke_all_threads_except) ( tid ); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 3137 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 3138 | break; |
| 3139 | |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 3140 | case VG_USERREQ__PTHREAD_ERROR: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3141 | VG_(record_pthread_error)( tid, (Char*)(arg[1]) ); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 3142 | SET_PTHREQ_RETVAL(tid, 0); |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 3143 | break; |
| 3144 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3145 | case VG_USERREQ__SET_FHSTACK_USED: |
| 3146 | do__set_fhstack_used( tid, (Int)(arg[1]) ); |
| 3147 | break; |
| 3148 | |
| 3149 | case VG_USERREQ__GET_FHSTACK_USED: |
| 3150 | do__get_fhstack_used( tid ); |
| 3151 | break; |
| 3152 | |
| 3153 | case VG_USERREQ__SET_FHSTACK_ENTRY: |
| 3154 | do__set_fhstack_entry( tid, (Int)(arg[1]), |
| 3155 | (ForkHandlerEntry*)(arg[2]) ); |
| 3156 | break; |
| 3157 | |
| 3158 | case VG_USERREQ__GET_FHSTACK_ENTRY: |
| 3159 | do__get_fhstack_entry( tid, (Int)(arg[1]), |
| 3160 | (ForkHandlerEntry*)(arg[2]) ); |
| 3161 | break; |
| 3162 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 3163 | case VG_USERREQ__SIGNAL_RETURNS: |
| 3164 | handle_signal_return(tid); |
| 3165 | break; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3166 | |
thughes | daa3456 | 2004-06-27 12:48:53 +0000 | [diff] [blame] | 3167 | case VG_USERREQ__GET_STACK_INFO: |
| 3168 | do__get_stack_info( tid, (Int)(arg[1]), (StackInfo*)(arg[2]) ); |
| 3169 | break; |
| 3170 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3171 | |
| 3172 | case VG_USERREQ__GET_SIGRT_MIN: |
| 3173 | SET_PTHREQ_RETVAL(tid, VG_(sig_rtmin)); |
| 3174 | break; |
| 3175 | |
| 3176 | case VG_USERREQ__GET_SIGRT_MAX: |
| 3177 | SET_PTHREQ_RETVAL(tid, VG_(sig_rtmax)); |
| 3178 | break; |
| 3179 | |
| 3180 | case VG_USERREQ__ALLOC_RTSIG: |
| 3181 | SET_PTHREQ_RETVAL(tid, VG_(sig_alloc_rtsig)((Int)arg[1])); |
| 3182 | break; |
| 3183 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 3184 | case VG_USERREQ__PRINTF: { |
| 3185 | int count = |
nethercote | 3e901a2 | 2004-09-11 13:17:02 +0000 | [diff] [blame] | 3186 | VG_(vmessage)( Vg_ClientMsg, (char *)arg[1], (void*)arg[2] ); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 3187 | SET_CLREQ_RETVAL( tid, count ); |
| 3188 | break; } |
| 3189 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3190 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 3191 | case VG_USERREQ__INTERNAL_PRINTF: { |
| 3192 | int count = |
nethercote | 3e901a2 | 2004-09-11 13:17:02 +0000 | [diff] [blame] | 3193 | VG_(vmessage)( Vg_UserMsg, (char *)arg[1], (void*)arg[2] ); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 3194 | SET_CLREQ_RETVAL( tid, count ); |
| 3195 | break; } |
| 3196 | |
| 3197 | case VG_USERREQ__PRINTF_BACKTRACE: { |
| 3198 | ExeContext *e = VG_(get_ExeContext)( tid ); |
| 3199 | int count = |
nethercote | 3e901a2 | 2004-09-11 13:17:02 +0000 | [diff] [blame] | 3200 | VG_(vmessage)( Vg_ClientMsg, (char *)arg[1], (void*)arg[2] ); |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 3201 | VG_(mini_stack_dump)(e->ips, VG_(clo_backtrace_size)); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 3202 | SET_CLREQ_RETVAL( tid, count ); |
| 3203 | break; } |
| 3204 | |
| 3205 | case VG_USERREQ__INTERNAL_PRINTF_BACKTRACE: { |
| 3206 | ExeContext *e = VG_(get_ExeContext)( tid ); |
| 3207 | int count = |
nethercote | 3e901a2 | 2004-09-11 13:17:02 +0000 | [diff] [blame] | 3208 | VG_(vmessage)( Vg_UserMsg, (char *)arg[1], (void*)arg[2] ); |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 3209 | VG_(mini_stack_dump)(e->ips, VG_(clo_backtrace_size)); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 3210 | SET_CLREQ_RETVAL( tid, count ); |
| 3211 | break; } |
| 3212 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3213 | case VG_USERREQ__GET_MALLOCFUNCS: { |
| 3214 | struct vg_mallocfunc_info *info = (struct vg_mallocfunc_info *)arg[1]; |
| 3215 | |
njn | d225283 | 2004-11-26 10:53:33 +0000 | [diff] [blame] | 3216 | info->tl_malloc = (Addr)TL_(malloc); |
| 3217 | info->tl_calloc = (Addr)TL_(calloc); |
| 3218 | info->tl_realloc = (Addr)TL_(realloc); |
| 3219 | info->tl_memalign = (Addr)TL_(memalign); |
| 3220 | info->tl___builtin_new = (Addr)TL_(__builtin_new); |
| 3221 | info->tl___builtin_vec_new = (Addr)TL_(__builtin_vec_new); |
| 3222 | info->tl_free = (Addr)TL_(free); |
| 3223 | info->tl___builtin_delete = (Addr)TL_(__builtin_delete); |
| 3224 | info->tl___builtin_vec_delete = (Addr)TL_(__builtin_vec_delete); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3225 | |
| 3226 | info->arena_payload_szB = (Addr)VG_(arena_payload_szB); |
| 3227 | |
| 3228 | info->clo_sloppy_malloc = VG_(clo_sloppy_malloc); |
| 3229 | info->clo_trace_malloc = VG_(clo_trace_malloc); |
| 3230 | |
| 3231 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
| 3232 | |
| 3233 | break; |
| 3234 | } |
| 3235 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3236 | /* Requests from the client program */ |
| 3237 | |
| 3238 | case VG_USERREQ__DISCARD_TRANSLATIONS: |
| 3239 | if (VG_(clo_verbosity) > 2) |
| 3240 | VG_(printf)( "client request: DISCARD_TRANSLATIONS," |
| 3241 | " addr %p, len %d\n", |
| 3242 | (void*)arg[1], arg[2] ); |
| 3243 | |
sewardj | 97ad552 | 2003-05-04 12:32:56 +0000 | [diff] [blame] | 3244 | VG_(invalidate_translations)( arg[1], arg[2], True ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3245 | |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 3246 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3247 | break; |
| 3248 | |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 3249 | case VG_USERREQ__COUNT_ERRORS: |
nethercote | f2b1148 | 2004-08-02 12:36:01 +0000 | [diff] [blame] | 3250 | SET_CLREQ_RETVAL( tid, VG_(get_n_errs_found)() ); |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 3251 | break; |
| 3252 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3253 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3254 | if (VG_(needs).client_requests) { |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 3255 | UWord ret; |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 3256 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3257 | if (VG_(clo_verbosity) > 2) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3258 | VG_(printf)("client request: code %x, addr %p, len %d\n", |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3259 | arg[0], (void*)arg[1], arg[2] ); |
| 3260 | |
njn | 26f0251 | 2004-11-22 18:33:15 +0000 | [diff] [blame] | 3261 | if (TL_(handle_client_request) ( tid, arg, &ret )) |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 3262 | SET_CLREQ_RETVAL(tid, ret); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3263 | } else { |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 3264 | static Bool whined = False; |
| 3265 | |
| 3266 | if (!whined) { |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 3267 | // Allow for requests in core, but defined by tools, which |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 3268 | // have 0 and 0 in their two high bytes. |
| 3269 | Char c1 = (arg[0] >> 24) & 0xff; |
| 3270 | Char c2 = (arg[0] >> 16) & 0xff; |
| 3271 | if (c1 == 0) c1 = '_'; |
| 3272 | if (c2 == 0) c2 = '_'; |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 3273 | VG_(message)(Vg_UserMsg, "Warning:\n" |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 3274 | " unhandled client request: 0x%x (%c%c+0x%x). Perhaps\n" |
| 3275 | " VG_(needs).client_requests should be set?\n", |
| 3276 | arg[0], c1, c2, arg[0] & 0xffff); |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 3277 | whined = True; |
| 3278 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3279 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3280 | break; |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3285 | /* --------------------------------------------------------------------- |
| 3286 | Sanity checking. |
| 3287 | ------------------------------------------------------------------ */ |
| 3288 | |
| 3289 | /* Internal consistency checks on the sched/pthread structures. */ |
| 3290 | static |
| 3291 | void scheduler_sanity ( void ) |
| 3292 | { |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 3293 | vg_pthread_mutex_t* mx; |
| 3294 | vg_pthread_cond_t* cv; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3295 | Int i; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3296 | struct timeout* top; |
| 3297 | UInt lasttime = 0; |
| 3298 | |
| 3299 | for(top = timeouts; top != NULL; top = top->next) { |
| 3300 | vg_assert(top->time >= lasttime); |
nethercote | 36881a2 | 2004-08-04 14:03:16 +0000 | [diff] [blame] | 3301 | vg_assert(is_valid_or_empty_tid(top->tid)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3302 | |
| 3303 | #if 0 |
| 3304 | /* assert timeout entry is either stale, or associated with a |
| 3305 | thread in the right state |
| 3306 | |
| 3307 | XXX disable for now - can be stale, but times happen to match |
| 3308 | */ |
| 3309 | vg_assert(VG_(threads)[top->tid].awaken_at != top->time || |
| 3310 | VG_(threads)[top->tid].status == VgTs_Sleeping || |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 3311 | VG_(threads)[top->tid].status == VgTs_WaitMX || |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3312 | VG_(threads)[top->tid].status == VgTs_WaitCV); |
| 3313 | #endif |
| 3314 | |
| 3315 | lasttime = top->time; |
| 3316 | } |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3317 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3318 | /* VG_(printf)("scheduler_sanity\n"); */ |
| 3319 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3320 | mx = VG_(threads)[i].associated_mx; |
| 3321 | cv = VG_(threads)[i].associated_cv; |
| 3322 | if (VG_(threads)[i].status == VgTs_WaitMX) { |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3323 | /* If we're waiting on a MX: (1) the mx is not null, (2, 3) |
| 3324 | it's actually held by someone, since otherwise this thread |
| 3325 | is deadlocked, (4) the mutex's owner is not us, since |
| 3326 | otherwise this thread is also deadlocked. The logic in |
| 3327 | do_pthread_mutex_lock rejects attempts by a thread to lock |
| 3328 | a (non-recursive) mutex which it already owns. |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 3329 | |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3330 | (2) has been seen to fail sometimes. I don't know why. |
| 3331 | Possibly to do with signals. */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3332 | vg_assert(cv == NULL); |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 3333 | /* 1 */ vg_assert(mx != NULL); |
nethercote | 1f0173b | 2004-02-28 15:40:36 +0000 | [diff] [blame] | 3334 | /* 2 */ vg_assert(mx->__vg_m_count > 0); |
| 3335 | /* 3 */ vg_assert(VG_(is_valid_tid)((ThreadId)mx->__vg_m_owner)); |
thughes | e321d49 | 2004-10-17 15:00:20 +0000 | [diff] [blame] | 3336 | /* 4 */ vg_assert((UInt)i != (ThreadId)mx->__vg_m_owner || |
| 3337 | VG_(threads)[i].awaken_at != 0xFFFFFFFF); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3338 | } else |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3339 | if (VG_(threads)[i].status == VgTs_WaitCV) { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3340 | vg_assert(cv != NULL); |
| 3341 | vg_assert(mx != NULL); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3342 | } else { |
thughes | f726923 | 2004-10-16 16:17:06 +0000 | [diff] [blame] | 3343 | vg_assert(cv == NULL); |
| 3344 | vg_assert(mx == NULL); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3345 | } |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3346 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3347 | if (VG_(threads)[i].status != VgTs_Empty) { |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3348 | Int |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3349 | stack_used = (Addr)VG_(threads)[i].stack_highest_word |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 3350 | - (Addr)STACK_PTR(VG_(threads)[i].arch); |
thughes | daa3456 | 2004-06-27 12:48:53 +0000 | [diff] [blame] | 3351 | Int |
| 3352 | stack_avail = VG_(threads)[i].stack_size |
| 3353 | - VG_AR_CLIENT_STACKBASE_REDZONE_SZB |
| 3354 | - VG_(threads)[i].stack_guard_size; |
fitzhardinge | 98c4dc0 | 2004-03-16 08:27:29 +0000 | [diff] [blame] | 3355 | /* This test is a bit bogus - it doesn't take into account |
| 3356 | alternate signal stacks, for a start. Also, if a thread |
| 3357 | has it's stack pointer somewhere strange, killing Valgrind |
| 3358 | isn't the right answer. */ |
| 3359 | if (0 && i > 1 /* not the root thread */ |
thughes | daa3456 | 2004-06-27 12:48:53 +0000 | [diff] [blame] | 3360 | && stack_used >= stack_avail) { |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3361 | VG_(message)(Vg_UserMsg, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3362 | "Error: STACK OVERFLOW: " |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3363 | "thread %d: stack used %d, available %d", |
thughes | daa3456 | 2004-06-27 12:48:53 +0000 | [diff] [blame] | 3364 | i, stack_used, stack_avail ); |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3365 | VG_(message)(Vg_UserMsg, |
| 3366 | "Terminating Valgrind. If thread(s) " |
| 3367 | "really need more stack, increase"); |
| 3368 | VG_(message)(Vg_UserMsg, |
rjwalsh | 7109a8c | 2004-09-02 00:31:02 +0000 | [diff] [blame] | 3369 | "VG_PTHREAD_STACK_SIZE in core.h and recompile."); |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3370 | VG_(exit)(1); |
| 3371 | } |
| 3372 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3373 | } |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3374 | |
| 3375 | for (i = 0; i < VG_N_THREAD_KEYS; i++) { |
| 3376 | if (!vg_thread_keys[i].inuse) |
| 3377 | vg_assert(vg_thread_keys[i].destructor == NULL); |
| 3378 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3382 | /*--------------------------------------------------------------------*/ |
| 3383 | /*--- end vg_scheduler.c ---*/ |
| 3384 | /*--------------------------------------------------------------------*/ |