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