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