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