blob: 536f721f843da91c7f5a5ec70d21e3c5b6c21c05 [file] [log] [blame]
sewardje663cb92002-04-12 10:26:32 +00001
2/*--------------------------------------------------------------------*/
3/*--- A user-space pthreads implementation. vg_scheduler.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, an x86 protected-mode emulator
8 designed for debugging and profiling binaries on x86-Unixes.
9
10 Copyright (C) 2000-2002 Julian Seward
11 jseward@acm.org
sewardje663cb92002-04-12 10:26:32 +000012
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
28 The GNU General Public License is contained in the file LICENSE.
29*/
30
31#include "vg_include.h"
32#include "vg_constants.h"
33
34#include "valgrind.h" /* for VG_USERREQ__MAKE_NOACCESS and
35 VG_USERREQ__DO_LEAK_CHECK */
36
sewardj77e466c2002-04-14 02:29:29 +000037/* BORKAGE/ISSUES as of 14 Apr 02
sewardje663cb92002-04-12 10:26:32 +000038
sewardj77e466c2002-04-14 02:29:29 +000039Note! This pthreads implementation is so poor as to not be
40suitable for use by anyone at all!
sewardje663cb92002-04-12 10:26:32 +000041
sewardj77e466c2002-04-14 02:29:29 +000042- Currently, when a signal is run, just the ThreadStatus.status fields
43 are saved in the signal frame, along with the CPU state. Question:
44 should I also save and restore:
45 ThreadStatus.joiner
46 ThreadStatus.waited_on_mid
47 ThreadStatus.awaken_at
48 ThreadStatus.retval
49 Currently unsure, and so am not doing so.
sewardje663cb92002-04-12 10:26:32 +000050
sewardj77e466c2002-04-14 02:29:29 +000051- Signals interrupting read/write and nanosleep: SA_RESTART settings.
52 Read/write correctly return with EINTR when SA_RESTART isn't
53 specified and they are interrupted by a signal. nanosleep just
54 pretends signals don't exist -- should be fixed.
sewardje663cb92002-04-12 10:26:32 +000055
sewardj75fe1892002-04-14 02:46:33 +000056- Read/write syscall starts: don't crap out when the initial
57 nonblocking read/write returns an error.
sewardj8937c812002-04-12 20:12:20 +000058
sewardj9a199dc2002-04-14 13:01:38 +000059- Get rid of restrictions re use of sigaltstack; they are no longer
60 needed.
61
sewardj6072c362002-04-19 14:40:57 +000062- Fix signals properly, so that each thread has its own blocking mask.
63 Currently this isn't done, and (worse?) signals are delivered to
64 Thread 1 (the root thread) regardless.
65
66 So, what's the deal with signals and mutexes? If a thread is
67 blocked on a mutex, or for a condition variable for that matter, can
68 signals still be delivered to it? This has serious consequences --
69 deadlocks, etc.
70
sewardje462e202002-04-13 04:09:07 +000071*/
sewardje663cb92002-04-12 10:26:32 +000072
73
74/* ---------------------------------------------------------------------
75 Types and globals for the scheduler.
76 ------------------------------------------------------------------ */
77
78/* type ThreadId is defined in vg_include.h. */
79
80/* struct ThreadState is defined in vg_include.h. */
81
sewardj6072c362002-04-19 14:40:57 +000082/* Private globals. A statically allocated array of threads. NOTE:
83 [0] is never used, to simplify the simulation of initialisers for
84 LinuxThreads. */
sewardje663cb92002-04-12 10:26:32 +000085static ThreadState vg_threads[VG_N_THREADS];
86
sewardj1e8cdc92002-04-18 11:37:52 +000087/* The tid of the thread currently in VG_(baseBlock). */
88static Int vg_tid_currently_in_baseBlock = VG_INVALID_THREADID;
89
sewardje663cb92002-04-12 10:26:32 +000090
91/* vg_oursignalhandler() might longjmp(). Here's the jmp_buf. */
92jmp_buf VG_(scheduler_jmpbuf);
93/* ... and if so, here's the signal which caused it to do so. */
94Int VG_(longjmpd_on_signal);
95
96
97/* Machinery to keep track of which threads are waiting on which
98 fds. */
99typedef
100 struct {
101 /* The thread which made the request. */
102 ThreadId tid;
103
104 /* The next two fields describe the request. */
105 /* File descriptor waited for. -1 means this slot is not in use */
106 Int fd;
107 /* The syscall number the fd is used in. */
108 Int syscall_no;
109
110 /* False => still waiting for select to tell us the fd is ready
111 to go. True => the fd is ready, but the results have not yet
112 been delivered back to the calling thread. Once the latter
113 happens, this entire record is marked as no longer in use, by
114 making the fd field be -1. */
115 Bool ready;
116 }
117 VgWaitedOnFd;
118
119static VgWaitedOnFd vg_waiting_fds[VG_N_WAITING_FDS];
120
121
sewardj5f07b662002-04-23 16:52:51 +0000122/* Keeping track of keys. */
123typedef
124 struct {
125 /* Has this key been allocated ? */
126 Bool inuse;
127 /* If .inuse==True, records the address of the associated
128 destructor, or NULL if none. */
129 void (*destructor)(void*);
130 }
131 ThreadKeyState;
132
133/* And our array of thread keys. */
134static ThreadKeyState vg_thread_keys[VG_N_THREAD_KEYS];
135
136typedef UInt ThreadKey;
137
138
sewardje663cb92002-04-12 10:26:32 +0000139/* Forwards */
sewardj5f07b662002-04-23 16:52:51 +0000140static void do_pthread_cond_timedwait_TIMEOUT ( ThreadId tid );
141
sewardje663cb92002-04-12 10:26:32 +0000142static void do_nontrivial_clientreq ( ThreadId tid );
143
sewardj6072c362002-04-19 14:40:57 +0000144static void scheduler_sanity ( void );
145
sewardjd7fd4d22002-04-24 01:57:27 +0000146static void do_pthread_mutex_unlock ( ThreadId,
147 void* /* pthread_cond_t* */ );
148static void do_pthread_mutex_lock ( ThreadId, Bool,
149 void* /* pthread_cond_t* */ );
150
sewardj51c0aaf2002-04-25 01:32:10 +0000151static void do_pthread_getspecific ( ThreadId,
152 UInt /* pthread_key_t */ );
153
sewardje663cb92002-04-12 10:26:32 +0000154
155/* ---------------------------------------------------------------------
156 Helper functions for the scheduler.
157 ------------------------------------------------------------------ */
158
sewardj604ec3c2002-04-18 22:38:41 +0000159static __inline__
160Bool is_valid_tid ( ThreadId tid )
161{
162 /* tid is unsigned, hence no < 0 test. */
sewardj6072c362002-04-19 14:40:57 +0000163 if (tid == 0) return False;
sewardj604ec3c2002-04-18 22:38:41 +0000164 if (tid >= VG_N_THREADS) return False;
sewardj604ec3c2002-04-18 22:38:41 +0000165 return True;
166}
167
168
sewardj1e8cdc92002-04-18 11:37:52 +0000169/* For constructing error messages only: try and identify a thread
170 whose stack this address currently falls within, or return
171 VG_INVALID_THREADID if it doesn't. A small complication is dealing
172 with any currently VG_(baseBlock)-resident thread.
173*/
174ThreadId VG_(identify_stack_addr)( Addr a )
175{
176 ThreadId tid, tid_to_skip;
177
178 tid_to_skip = VG_INVALID_THREADID;
179
180 /* First check to see if there's a currently-loaded thread in
181 VG_(baseBlock). */
182 if (vg_tid_currently_in_baseBlock != VG_INVALID_THREADID) {
183 tid = vg_tid_currently_in_baseBlock;
184 if (VG_(baseBlock)[VGOFF_(m_esp)] <= a
185 && a <= vg_threads[tid].stack_highest_word)
186 return tid;
187 else
188 tid_to_skip = tid;
189 }
190
sewardj6072c362002-04-19 14:40:57 +0000191 for (tid = 1; tid < VG_N_THREADS; tid++) {
sewardj1e8cdc92002-04-18 11:37:52 +0000192 if (vg_threads[tid].status == VgTs_Empty) continue;
193 if (tid == tid_to_skip) continue;
194 if (vg_threads[tid].m_esp <= a
195 && a <= vg_threads[tid].stack_highest_word)
196 return tid;
197 }
198 return VG_INVALID_THREADID;
199}
200
201
sewardj15a43e12002-04-17 19:35:12 +0000202/* Print the scheduler status. */
203void VG_(pp_sched_status) ( void )
sewardje663cb92002-04-12 10:26:32 +0000204{
205 Int i;
206 VG_(printf)("\nsched status:\n");
sewardj6072c362002-04-19 14:40:57 +0000207 for (i = 1; i < VG_N_THREADS; i++) {
sewardje663cb92002-04-12 10:26:32 +0000208 if (vg_threads[i].status == VgTs_Empty) continue;
sewardj15a43e12002-04-17 19:35:12 +0000209 VG_(printf)("\nThread %d: status = ", i);
sewardje663cb92002-04-12 10:26:32 +0000210 switch (vg_threads[i].status) {
sewardj6072c362002-04-19 14:40:57 +0000211 case VgTs_Runnable: VG_(printf)("Runnable"); break;
212 case VgTs_WaitFD: VG_(printf)("WaitFD"); break;
213 case VgTs_WaitJoiner: VG_(printf)("WaitJoiner(%d)",
sewardje663cb92002-04-12 10:26:32 +0000214 vg_threads[i].joiner); break;
sewardj6072c362002-04-19 14:40:57 +0000215 case VgTs_WaitJoinee: VG_(printf)("WaitJoinee"); break;
216 case VgTs_Sleeping: VG_(printf)("Sleeping"); break;
217 case VgTs_WaitMX: VG_(printf)("WaitMX"); break;
sewardj3b5d8862002-04-20 13:53:23 +0000218 case VgTs_WaitCV: VG_(printf)("WaitCV"); break;
sewardje663cb92002-04-12 10:26:32 +0000219 default: VG_(printf)("???"); break;
220 }
sewardj3b5d8862002-04-20 13:53:23 +0000221 VG_(printf)(", associated_mx = %p, associated_cv = %p\n",
222 vg_threads[i].associated_mx,
223 vg_threads[i].associated_cv );
sewardj15a43e12002-04-17 19:35:12 +0000224 VG_(pp_ExeContext)(
225 VG_(get_ExeContext)( False, vg_threads[i].m_eip,
226 vg_threads[i].m_ebp ));
sewardje663cb92002-04-12 10:26:32 +0000227 }
228 VG_(printf)("\n");
229}
230
231static
232void add_waiting_fd ( ThreadId tid, Int fd, Int syscall_no )
233{
234 Int i;
235
236 vg_assert(fd != -1); /* avoid total chaos */
237
238 for (i = 0; i < VG_N_WAITING_FDS; i++)
239 if (vg_waiting_fds[i].fd == -1)
240 break;
241
242 if (i == VG_N_WAITING_FDS)
243 VG_(panic)("add_waiting_fd: VG_N_WAITING_FDS is too low");
244 /*
245 VG_(printf)("add_waiting_fd: add (tid %d, fd %d) at slot %d\n",
246 tid, fd, i);
247 */
248 vg_waiting_fds[i].fd = fd;
249 vg_waiting_fds[i].tid = tid;
250 vg_waiting_fds[i].ready = False;
251 vg_waiting_fds[i].syscall_no = syscall_no;
252}
253
254
255
256static
257void print_sched_event ( ThreadId tid, Char* what )
258{
sewardj45b4b372002-04-16 22:50:32 +0000259 VG_(message)(Vg_DebugMsg, " SCHED[%d]: %s", tid, what );
sewardj8937c812002-04-12 20:12:20 +0000260}
261
262
263static
264void print_pthread_event ( ThreadId tid, Char* what )
265{
266 VG_(message)(Vg_DebugMsg, "PTHREAD[%d]: %s", tid, what );
sewardje663cb92002-04-12 10:26:32 +0000267}
268
269
270static
271Char* name_of_sched_event ( UInt event )
272{
273 switch (event) {
sewardje663cb92002-04-12 10:26:32 +0000274 case VG_TRC_EBP_JMP_SYSCALL: return "SYSCALL";
275 case VG_TRC_EBP_JMP_CLIENTREQ: return "CLIENTREQ";
276 case VG_TRC_INNER_COUNTERZERO: return "COUNTERZERO";
277 case VG_TRC_INNER_FASTMISS: return "FASTMISS";
278 case VG_TRC_UNRESUMABLE_SIGNAL: return "FATALSIGNAL";
279 default: return "??UNKNOWN??";
280 }
281}
282
283
284/* Create a translation of the client basic block beginning at
285 orig_addr, and add it to the translation cache & translation table.
286 This probably doesn't really belong here, but, hey ...
287*/
sewardj1e8cdc92002-04-18 11:37:52 +0000288static
289void create_translation_for ( ThreadId tid, Addr orig_addr )
sewardje663cb92002-04-12 10:26:32 +0000290{
291 Addr trans_addr;
292 TTEntry tte;
293 Int orig_size, trans_size;
294 /* Ensure there is space to hold a translation. */
295 VG_(maybe_do_lru_pass)();
sewardj1e8cdc92002-04-18 11:37:52 +0000296 VG_(translate)( &vg_threads[tid],
297 orig_addr, &orig_size, &trans_addr, &trans_size );
sewardje663cb92002-04-12 10:26:32 +0000298 /* Copy data at trans_addr into the translation cache.
299 Returned pointer is to the code, not to the 4-byte
300 header. */
301 /* Since the .orig_size and .trans_size fields are
302 UShort, be paranoid. */
303 vg_assert(orig_size > 0 && orig_size < 65536);
304 vg_assert(trans_size > 0 && trans_size < 65536);
305 tte.orig_size = orig_size;
306 tte.orig_addr = orig_addr;
307 tte.trans_size = trans_size;
308 tte.trans_addr = VG_(copy_to_transcache)
309 ( trans_addr, trans_size );
310 tte.mru_epoch = VG_(current_epoch);
311 /* Free the intermediary -- was allocated by VG_(emit_code). */
312 VG_(jitfree)( (void*)trans_addr );
313 /* Add to trans tab and set back pointer. */
314 VG_(add_to_trans_tab) ( &tte );
315 /* Update stats. */
316 VG_(this_epoch_in_count) ++;
317 VG_(this_epoch_in_osize) += orig_size;
318 VG_(this_epoch_in_tsize) += trans_size;
319 VG_(overall_in_count) ++;
320 VG_(overall_in_osize) += orig_size;
321 VG_(overall_in_tsize) += trans_size;
322 /* Record translated area for SMC detection. */
323 VG_(smc_mark_original) ( orig_addr, orig_size );
324}
325
326
327/* Allocate a completely empty ThreadState record. */
328static
329ThreadId vg_alloc_ThreadState ( void )
330{
331 Int i;
sewardj6072c362002-04-19 14:40:57 +0000332 for (i = 1; i < VG_N_THREADS; i++) {
sewardje663cb92002-04-12 10:26:32 +0000333 if (vg_threads[i].status == VgTs_Empty)
334 return i;
335 }
336 VG_(printf)("vg_alloc_ThreadState: no free slots available\n");
337 VG_(printf)("Increase VG_N_THREADS, rebuild and try again.\n");
338 VG_(panic)("VG_N_THREADS is too low");
339 /*NOTREACHED*/
340}
341
342
343ThreadState* VG_(get_thread_state) ( ThreadId tid )
344{
sewardj6072c362002-04-19 14:40:57 +0000345 vg_assert(is_valid_tid(tid));
sewardje663cb92002-04-12 10:26:32 +0000346 vg_assert(vg_threads[tid].status != VgTs_Empty);
347 return & vg_threads[tid];
348}
349
350
sewardj1e8cdc92002-04-18 11:37:52 +0000351ThreadState* VG_(get_current_thread_state) ( void )
352{
353 vg_assert(vg_tid_currently_in_baseBlock != VG_INVALID_THREADID);
sewardj6072c362002-04-19 14:40:57 +0000354 return VG_(get_thread_state) ( vg_tid_currently_in_baseBlock );
sewardj1e8cdc92002-04-18 11:37:52 +0000355}
356
357
358ThreadId VG_(get_current_tid) ( void )
359{
360 vg_assert(vg_tid_currently_in_baseBlock != VG_INVALID_THREADID);
361 return vg_tid_currently_in_baseBlock;
362}
363
364
sewardje663cb92002-04-12 10:26:32 +0000365/* Copy the saved state of a thread into VG_(baseBlock), ready for it
366 to be run. */
367__inline__
368void VG_(load_thread_state) ( ThreadId tid )
369{
370 Int i;
sewardj1e8cdc92002-04-18 11:37:52 +0000371 vg_assert(vg_tid_currently_in_baseBlock == VG_INVALID_THREADID);
372
sewardje663cb92002-04-12 10:26:32 +0000373 VG_(baseBlock)[VGOFF_(m_eax)] = vg_threads[tid].m_eax;
374 VG_(baseBlock)[VGOFF_(m_ebx)] = vg_threads[tid].m_ebx;
375 VG_(baseBlock)[VGOFF_(m_ecx)] = vg_threads[tid].m_ecx;
376 VG_(baseBlock)[VGOFF_(m_edx)] = vg_threads[tid].m_edx;
377 VG_(baseBlock)[VGOFF_(m_esi)] = vg_threads[tid].m_esi;
378 VG_(baseBlock)[VGOFF_(m_edi)] = vg_threads[tid].m_edi;
379 VG_(baseBlock)[VGOFF_(m_ebp)] = vg_threads[tid].m_ebp;
380 VG_(baseBlock)[VGOFF_(m_esp)] = vg_threads[tid].m_esp;
381 VG_(baseBlock)[VGOFF_(m_eflags)] = vg_threads[tid].m_eflags;
382 VG_(baseBlock)[VGOFF_(m_eip)] = vg_threads[tid].m_eip;
383
384 for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++)
385 VG_(baseBlock)[VGOFF_(m_fpustate) + i] = vg_threads[tid].m_fpu[i];
386
387 VG_(baseBlock)[VGOFF_(sh_eax)] = vg_threads[tid].sh_eax;
388 VG_(baseBlock)[VGOFF_(sh_ebx)] = vg_threads[tid].sh_ebx;
389 VG_(baseBlock)[VGOFF_(sh_ecx)] = vg_threads[tid].sh_ecx;
390 VG_(baseBlock)[VGOFF_(sh_edx)] = vg_threads[tid].sh_edx;
391 VG_(baseBlock)[VGOFF_(sh_esi)] = vg_threads[tid].sh_esi;
392 VG_(baseBlock)[VGOFF_(sh_edi)] = vg_threads[tid].sh_edi;
393 VG_(baseBlock)[VGOFF_(sh_ebp)] = vg_threads[tid].sh_ebp;
394 VG_(baseBlock)[VGOFF_(sh_esp)] = vg_threads[tid].sh_esp;
395 VG_(baseBlock)[VGOFF_(sh_eflags)] = vg_threads[tid].sh_eflags;
sewardj1e8cdc92002-04-18 11:37:52 +0000396
397 vg_tid_currently_in_baseBlock = tid;
sewardje663cb92002-04-12 10:26:32 +0000398}
399
400
401/* Copy the state of a thread from VG_(baseBlock), presumably after it
402 has been descheduled. For sanity-check purposes, fill the vacated
403 VG_(baseBlock) with garbage so as to make the system more likely to
404 fail quickly if we erroneously continue to poke around inside
405 VG_(baseBlock) without first doing a load_thread_state().
406*/
407__inline__
408void VG_(save_thread_state) ( ThreadId tid )
409{
410 Int i;
411 const UInt junk = 0xDEADBEEF;
412
sewardj1e8cdc92002-04-18 11:37:52 +0000413 vg_assert(vg_tid_currently_in_baseBlock != VG_INVALID_THREADID);
414
sewardje663cb92002-04-12 10:26:32 +0000415 vg_threads[tid].m_eax = VG_(baseBlock)[VGOFF_(m_eax)];
416 vg_threads[tid].m_ebx = VG_(baseBlock)[VGOFF_(m_ebx)];
417 vg_threads[tid].m_ecx = VG_(baseBlock)[VGOFF_(m_ecx)];
418 vg_threads[tid].m_edx = VG_(baseBlock)[VGOFF_(m_edx)];
419 vg_threads[tid].m_esi = VG_(baseBlock)[VGOFF_(m_esi)];
420 vg_threads[tid].m_edi = VG_(baseBlock)[VGOFF_(m_edi)];
421 vg_threads[tid].m_ebp = VG_(baseBlock)[VGOFF_(m_ebp)];
422 vg_threads[tid].m_esp = VG_(baseBlock)[VGOFF_(m_esp)];
423 vg_threads[tid].m_eflags = VG_(baseBlock)[VGOFF_(m_eflags)];
424 vg_threads[tid].m_eip = VG_(baseBlock)[VGOFF_(m_eip)];
425
426 for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++)
427 vg_threads[tid].m_fpu[i] = VG_(baseBlock)[VGOFF_(m_fpustate) + i];
428
429 vg_threads[tid].sh_eax = VG_(baseBlock)[VGOFF_(sh_eax)];
430 vg_threads[tid].sh_ebx = VG_(baseBlock)[VGOFF_(sh_ebx)];
431 vg_threads[tid].sh_ecx = VG_(baseBlock)[VGOFF_(sh_ecx)];
432 vg_threads[tid].sh_edx = VG_(baseBlock)[VGOFF_(sh_edx)];
433 vg_threads[tid].sh_esi = VG_(baseBlock)[VGOFF_(sh_esi)];
434 vg_threads[tid].sh_edi = VG_(baseBlock)[VGOFF_(sh_edi)];
435 vg_threads[tid].sh_ebp = VG_(baseBlock)[VGOFF_(sh_ebp)];
436 vg_threads[tid].sh_esp = VG_(baseBlock)[VGOFF_(sh_esp)];
437 vg_threads[tid].sh_eflags = VG_(baseBlock)[VGOFF_(sh_eflags)];
438
439 /* Fill it up with junk. */
440 VG_(baseBlock)[VGOFF_(m_eax)] = junk;
441 VG_(baseBlock)[VGOFF_(m_ebx)] = junk;
442 VG_(baseBlock)[VGOFF_(m_ecx)] = junk;
443 VG_(baseBlock)[VGOFF_(m_edx)] = junk;
444 VG_(baseBlock)[VGOFF_(m_esi)] = junk;
445 VG_(baseBlock)[VGOFF_(m_edi)] = junk;
446 VG_(baseBlock)[VGOFF_(m_ebp)] = junk;
447 VG_(baseBlock)[VGOFF_(m_esp)] = junk;
448 VG_(baseBlock)[VGOFF_(m_eflags)] = junk;
449 VG_(baseBlock)[VGOFF_(m_eip)] = junk;
450
451 for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++)
452 VG_(baseBlock)[VGOFF_(m_fpustate) + i] = junk;
sewardj1e8cdc92002-04-18 11:37:52 +0000453
454 vg_tid_currently_in_baseBlock = VG_INVALID_THREADID;
sewardje663cb92002-04-12 10:26:32 +0000455}
456
457
458/* Run the thread tid for a while, and return a VG_TRC_* value to the
459 scheduler indicating what happened. */
sewardj6072c362002-04-19 14:40:57 +0000460static
sewardje663cb92002-04-12 10:26:32 +0000461UInt run_thread_for_a_while ( ThreadId tid )
462{
sewardj7ccc5c22002-04-24 21:39:11 +0000463 volatile UInt trc = 0;
sewardj6072c362002-04-19 14:40:57 +0000464 vg_assert(is_valid_tid(tid));
465 vg_assert(vg_threads[tid].status == VgTs_Runnable);
sewardje663cb92002-04-12 10:26:32 +0000466 vg_assert(VG_(bbs_to_go) > 0);
467
468 VG_(load_thread_state) ( tid );
469 if (__builtin_setjmp(VG_(scheduler_jmpbuf)) == 0) {
470 /* try this ... */
471 trc = VG_(run_innerloop)();
472 /* We get here if the client didn't take a fault. */
473 } else {
474 /* We get here if the client took a fault, which caused our
475 signal handler to longjmp. */
476 vg_assert(trc == 0);
477 trc = VG_TRC_UNRESUMABLE_SIGNAL;
478 }
479 VG_(save_thread_state) ( tid );
480 return trc;
481}
482
483
484/* Increment the LRU epoch counter. */
485static
486void increment_epoch ( void )
487{
488 VG_(current_epoch)++;
489 if (VG_(clo_verbosity) > 2) {
490 UInt tt_used, tc_used;
491 VG_(get_tt_tc_used) ( &tt_used, &tc_used );
492 VG_(message)(Vg_UserMsg,
493 "%lu bbs, in: %d (%d -> %d), out %d (%d -> %d), TT %d, TC %d",
494 VG_(bbs_done),
495 VG_(this_epoch_in_count),
496 VG_(this_epoch_in_osize),
497 VG_(this_epoch_in_tsize),
498 VG_(this_epoch_out_count),
499 VG_(this_epoch_out_osize),
500 VG_(this_epoch_out_tsize),
501 tt_used, tc_used
502 );
503 }
504 VG_(this_epoch_in_count) = 0;
505 VG_(this_epoch_in_osize) = 0;
506 VG_(this_epoch_in_tsize) = 0;
507 VG_(this_epoch_out_count) = 0;
508 VG_(this_epoch_out_osize) = 0;
509 VG_(this_epoch_out_tsize) = 0;
510}
511
512
513/* Initialise the scheduler. Create a single "main" thread ready to
sewardj6072c362002-04-19 14:40:57 +0000514 run, with special ThreadId of one. This is called at startup; the
sewardje663cb92002-04-12 10:26:32 +0000515 caller takes care to park the client's state is parked in
516 VG_(baseBlock).
517*/
518void VG_(scheduler_init) ( void )
519{
520 Int i;
521 Addr startup_esp;
522 ThreadId tid_main;
523
524 startup_esp = VG_(baseBlock)[VGOFF_(m_esp)];
525 if ((startup_esp & VG_STARTUP_STACK_MASK) != VG_STARTUP_STACK_MASK) {
sewardj9a199dc2002-04-14 13:01:38 +0000526 VG_(printf)("%%esp at startup = %p is not near %p; aborting\n",
527 (void*)startup_esp, (void*)VG_STARTUP_STACK_MASK);
sewardje663cb92002-04-12 10:26:32 +0000528 VG_(panic)("unexpected %esp at startup");
529 }
530
sewardj6072c362002-04-19 14:40:57 +0000531 for (i = 0 /* NB; not 1 */; i < VG_N_THREADS; i++) {
532 vg_threads[i].status = VgTs_Empty;
sewardje663cb92002-04-12 10:26:32 +0000533 vg_threads[i].stack_size = 0;
534 vg_threads[i].stack_base = (Addr)NULL;
sewardj1e8cdc92002-04-18 11:37:52 +0000535 vg_threads[i].tid = i;
sewardje663cb92002-04-12 10:26:32 +0000536 }
537
538 for (i = 0; i < VG_N_WAITING_FDS; i++)
539 vg_waiting_fds[i].fd = -1; /* not in use */
540
sewardj5f07b662002-04-23 16:52:51 +0000541 for (i = 0; i < VG_N_THREAD_KEYS; i++) {
542 vg_thread_keys[i].inuse = False;
543 vg_thread_keys[i].destructor = NULL;
544 }
545
sewardje663cb92002-04-12 10:26:32 +0000546 /* Assert this is thread zero, which has certain magic
547 properties. */
548 tid_main = vg_alloc_ThreadState();
sewardj6072c362002-04-19 14:40:57 +0000549 vg_assert(tid_main == 1);
sewardje663cb92002-04-12 10:26:32 +0000550
sewardj3b5d8862002-04-20 13:53:23 +0000551 vg_threads[tid_main].status = VgTs_Runnable;
552 vg_threads[tid_main].joiner = VG_INVALID_THREADID;
553 vg_threads[tid_main].associated_mx = NULL;
554 vg_threads[tid_main].associated_cv = NULL;
555 vg_threads[tid_main].retval = NULL; /* not important */
sewardj1e8cdc92002-04-18 11:37:52 +0000556 vg_threads[tid_main].stack_highest_word
557 = vg_threads[tid_main].m_esp /* -4 ??? */;
sewardj5f07b662002-04-23 16:52:51 +0000558 for (i = 0; i < VG_N_THREAD_KEYS; i++)
559 vg_threads[tid_main].specifics[i] = NULL;
sewardje663cb92002-04-12 10:26:32 +0000560
561 /* Copy VG_(baseBlock) state to tid_main's slot. */
sewardj1e8cdc92002-04-18 11:37:52 +0000562 vg_tid_currently_in_baseBlock = tid_main;
sewardje663cb92002-04-12 10:26:32 +0000563 VG_(save_thread_state) ( tid_main );
sewardj1e8cdc92002-04-18 11:37:52 +0000564
565 /* So now ... */
566 vg_assert(vg_tid_currently_in_baseBlock == VG_INVALID_THREADID);
sewardje663cb92002-04-12 10:26:32 +0000567}
568
569
570/* What if fd isn't a valid fd? */
571static
572void set_fd_nonblocking ( Int fd )
573{
574 Int res = VG_(fcntl)( fd, VKI_F_GETFL, 0 );
575 vg_assert(!VG_(is_kerror)(res));
576 res |= VKI_O_NONBLOCK;
577 res = VG_(fcntl)( fd, VKI_F_SETFL, res );
578 vg_assert(!VG_(is_kerror)(res));
579}
580
581static
582void set_fd_blocking ( Int fd )
583{
584 Int res = VG_(fcntl)( fd, VKI_F_GETFL, 0 );
585 vg_assert(!VG_(is_kerror)(res));
586 res &= ~VKI_O_NONBLOCK;
587 res = VG_(fcntl)( fd, VKI_F_SETFL, res );
588 vg_assert(!VG_(is_kerror)(res));
589}
590
591static
592Bool fd_is_blockful ( Int fd )
593{
594 Int res = VG_(fcntl)( fd, VKI_F_GETFL, 0 );
595 vg_assert(!VG_(is_kerror)(res));
596 return (res & VKI_O_NONBLOCK) ? False : True;
597}
598
599
600
sewardjd7fd4d22002-04-24 01:57:27 +0000601/* Possibly do a for tid. Return values are:
sewardje663cb92002-04-12 10:26:32 +0000602
sewardjd7fd4d22002-04-24 01:57:27 +0000603 True = request done. Thread may or may not be still runnable;
604 caller must check. If it is still runnable, the result will be in
605 the thread's %EDX as expected.
606
607 False = request not done. A more capable but slower mechanism will
608 deal with it.
sewardje663cb92002-04-12 10:26:32 +0000609*/
sewardjd7fd4d22002-04-24 01:57:27 +0000610static
sewardje663cb92002-04-12 10:26:32 +0000611Bool maybe_do_trivial_clientreq ( ThreadId tid )
612{
613# define SIMPLE_RETURN(vvv) \
sewardj8c824512002-04-14 04:16:48 +0000614 { tst->m_edx = (vvv); \
sewardje663cb92002-04-12 10:26:32 +0000615 return True; \
616 }
617
sewardj8c824512002-04-14 04:16:48 +0000618 ThreadState* tst = &vg_threads[tid];
619 UInt* arg = (UInt*)(tst->m_eax);
620 UInt req_no = arg[0];
621
sewardje663cb92002-04-12 10:26:32 +0000622 switch (req_no) {
623 case VG_USERREQ__MALLOC:
624 SIMPLE_RETURN(
sewardj8c824512002-04-14 04:16:48 +0000625 (UInt)VG_(client_malloc) ( tst, arg[1], Vg_AllocMalloc )
sewardje663cb92002-04-12 10:26:32 +0000626 );
627 case VG_USERREQ__BUILTIN_NEW:
628 SIMPLE_RETURN(
sewardj8c824512002-04-14 04:16:48 +0000629 (UInt)VG_(client_malloc) ( tst, arg[1], Vg_AllocNew )
sewardje663cb92002-04-12 10:26:32 +0000630 );
631 case VG_USERREQ__BUILTIN_VEC_NEW:
632 SIMPLE_RETURN(
sewardj8c824512002-04-14 04:16:48 +0000633 (UInt)VG_(client_malloc) ( tst, arg[1], Vg_AllocNewVec )
sewardje663cb92002-04-12 10:26:32 +0000634 );
635 case VG_USERREQ__FREE:
sewardj8c824512002-04-14 04:16:48 +0000636 VG_(client_free) ( tst, (void*)arg[1], Vg_AllocMalloc );
sewardje663cb92002-04-12 10:26:32 +0000637 SIMPLE_RETURN(0); /* irrelevant */
638 case VG_USERREQ__BUILTIN_DELETE:
sewardj8c824512002-04-14 04:16:48 +0000639 VG_(client_free) ( tst, (void*)arg[1], Vg_AllocNew );
sewardje663cb92002-04-12 10:26:32 +0000640 SIMPLE_RETURN(0); /* irrelevant */
641 case VG_USERREQ__BUILTIN_VEC_DELETE:
sewardj8c824512002-04-14 04:16:48 +0000642 VG_(client_free) ( tst, (void*)arg[1], Vg_AllocNewVec );
sewardje663cb92002-04-12 10:26:32 +0000643 SIMPLE_RETURN(0); /* irrelevant */
644 case VG_USERREQ__CALLOC:
645 SIMPLE_RETURN(
sewardj8c824512002-04-14 04:16:48 +0000646 (UInt)VG_(client_calloc) ( tst, arg[1], arg[2] )
sewardje663cb92002-04-12 10:26:32 +0000647 );
648 case VG_USERREQ__REALLOC:
649 SIMPLE_RETURN(
sewardj8c824512002-04-14 04:16:48 +0000650 (UInt)VG_(client_realloc) ( tst, (void*)arg[1], arg[2] )
sewardje663cb92002-04-12 10:26:32 +0000651 );
652 case VG_USERREQ__MEMALIGN:
653 SIMPLE_RETURN(
sewardj8c824512002-04-14 04:16:48 +0000654 (UInt)VG_(client_memalign) ( tst, arg[1], arg[2] )
sewardje663cb92002-04-12 10:26:32 +0000655 );
sewardj9650c992002-04-16 03:44:31 +0000656
sewardj5f07b662002-04-23 16:52:51 +0000657 /* These are heavily used -- or at least we want them to be
658 cheap. */
sewardj9650c992002-04-16 03:44:31 +0000659 case VG_USERREQ__PTHREAD_GET_THREADID:
660 SIMPLE_RETURN(tid);
661 case VG_USERREQ__RUNNING_ON_VALGRIND:
662 SIMPLE_RETURN(1);
sewardj45b4b372002-04-16 22:50:32 +0000663 case VG_USERREQ__GET_PTHREAD_TRACE_LEVEL:
664 SIMPLE_RETURN(VG_(clo_trace_pthread_level));
sewardj5f07b662002-04-23 16:52:51 +0000665 case VG_USERREQ__READ_MILLISECOND_TIMER:
666 SIMPLE_RETURN(VG_(read_millisecond_timer)());
sewardj9650c992002-04-16 03:44:31 +0000667
sewardjd7fd4d22002-04-24 01:57:27 +0000668 case VG_USERREQ__PTHREAD_MUTEX_UNLOCK:
669 do_pthread_mutex_unlock( tid, (void *)(arg[1]) );
670 return True;
671
672 /* This may make thread tid non-runnable, but the scheduler
673 checks for that on return from this function. */
674 case VG_USERREQ__PTHREAD_MUTEX_LOCK:
675 do_pthread_mutex_lock( tid, False, (void *)(arg[1]) );
676 return True;
677
sewardj14e03422002-04-24 19:51:31 +0000678 case VG_USERREQ__PTHREAD_MUTEX_TRYLOCK:
679 do_pthread_mutex_lock( tid, True, (void *)(arg[1]) );
680 return True;
681
sewardj51c0aaf2002-04-25 01:32:10 +0000682 case VG_USERREQ__PTHREAD_GETSPECIFIC:
683 do_pthread_getspecific ( tid, (UInt)(arg[1]) );
684 return True;
685
sewardje663cb92002-04-12 10:26:32 +0000686 default:
687 /* Too hard; wimp out. */
688 return False;
689 }
690# undef SIMPLE_RETURN
691}
692
693
sewardj6072c362002-04-19 14:40:57 +0000694/* vthread tid is returning from a signal handler; modify its
695 stack/regs accordingly. */
sewardj1ffa8da2002-04-26 22:47:57 +0000696
697/* [Helper fn for handle_signal_return] tid, assumed to be in WaitFD
698 for read or write, has been interrupted by a signal. Find and
699 clear the relevant vg_waiting_fd[] entry. Most of the code in this
700 procedure is total paranoia, if you look closely. */
701static
702void cleanup_waiting_fd_table ( ThreadId tid )
703{
704 Int i, waiters;
705
706 vg_assert(is_valid_tid(tid));
707 vg_assert(vg_threads[tid].status == VgTs_WaitFD);
708 vg_assert(vg_threads[tid].m_eax == __NR_read
709 || vg_threads[tid].m_eax == __NR_write);
710
711 /* Excessively paranoidly ... find the fd this op was waiting
712 for, and mark it as not being waited on. */
713 waiters = 0;
714 for (i = 0; i < VG_N_WAITING_FDS; i++) {
715 if (vg_waiting_fds[i].tid == tid) {
716 waiters++;
717 vg_assert(vg_waiting_fds[i].syscall_no == vg_threads[tid].m_eax);
718 }
719 }
720 vg_assert(waiters == 1);
721 for (i = 0; i < VG_N_WAITING_FDS; i++)
722 if (vg_waiting_fds[i].tid == tid)
723 break;
724 vg_assert(i < VG_N_WAITING_FDS);
725 vg_assert(vg_waiting_fds[i].fd != -1);
726 vg_waiting_fds[i].fd = -1; /* not in use */
727}
728
729
sewardj6072c362002-04-19 14:40:57 +0000730static
731void handle_signal_return ( ThreadId tid )
732{
733 Char msg_buf[100];
734 Bool restart_blocked_syscalls;
735
736 vg_assert(is_valid_tid(tid));
737
738 restart_blocked_syscalls = VG_(signal_returns)(tid);
739
740 if (restart_blocked_syscalls)
741 /* Easy; we don't have to do anything. */
742 return;
743
sewardj1ffa8da2002-04-26 22:47:57 +0000744 if (vg_threads[tid].status == VgTs_WaitFD
745 && (vg_threads[tid].m_eax == __NR_read
746 || vg_threads[tid].m_eax == __NR_write)) {
sewardj6072c362002-04-19 14:40:57 +0000747 /* read() or write() interrupted. Force a return with EINTR. */
sewardj1ffa8da2002-04-26 22:47:57 +0000748 cleanup_waiting_fd_table(tid);
sewardj6072c362002-04-19 14:40:57 +0000749 vg_threads[tid].m_eax = -VKI_EINTR;
750 vg_threads[tid].status = VgTs_Runnable;
sewardj1ffa8da2002-04-26 22:47:57 +0000751
sewardj6072c362002-04-19 14:40:57 +0000752 if (VG_(clo_trace_sched)) {
753 VG_(sprintf)(msg_buf,
754 "read() / write() interrupted by signal; return EINTR" );
755 print_sched_event(tid, msg_buf);
756 }
757 return;
758 }
759
sewardj1ffa8da2002-04-26 22:47:57 +0000760 if (vg_threads[tid].status == VgTs_WaitFD
761 && vg_threads[tid].m_eax == __NR_nanosleep) {
sewardj6072c362002-04-19 14:40:57 +0000762 /* We interrupted a nanosleep(). The right thing to do is to
763 write the unused time to nanosleep's second param and return
764 EINTR, but I'm too lazy for that. */
765 return;
766 }
767
sewardj1ffa8da2002-04-26 22:47:57 +0000768 if (vg_threads[tid].status == VgTs_WaitFD) {
769 VG_(panic)("handle_signal_return: unknown interrupted syscall");
770 }
771
sewardj6072c362002-04-19 14:40:57 +0000772 /* All other cases? Just return. */
773}
774
775
sewardje663cb92002-04-12 10:26:32 +0000776static
777void sched_do_syscall ( ThreadId tid )
778{
779 UInt saved_eax;
780 UInt res, syscall_no;
781 UInt fd;
sewardje663cb92002-04-12 10:26:32 +0000782 Bool orig_fd_blockness;
783 Char msg_buf[100];
784
sewardj6072c362002-04-19 14:40:57 +0000785 vg_assert(is_valid_tid(tid));
sewardje663cb92002-04-12 10:26:32 +0000786 vg_assert(vg_threads[tid].status == VgTs_Runnable);
787
788 syscall_no = vg_threads[tid].m_eax; /* syscall number */
789
790 if (syscall_no == __NR_nanosleep) {
sewardj5f07b662002-04-23 16:52:51 +0000791 UInt t_now, t_awaken;
sewardje663cb92002-04-12 10:26:32 +0000792 struct vki_timespec* req;
793 req = (struct vki_timespec*)vg_threads[tid].m_ebx; /* arg1 */
sewardj5f07b662002-04-23 16:52:51 +0000794 t_now = VG_(read_millisecond_timer)();
sewardje663cb92002-04-12 10:26:32 +0000795 t_awaken
796 = t_now
sewardj5f07b662002-04-23 16:52:51 +0000797 + (UInt)1000ULL * (UInt)(req->tv_sec)
798 + (UInt)(req->tv_nsec) / 1000000;
sewardje663cb92002-04-12 10:26:32 +0000799 vg_threads[tid].status = VgTs_Sleeping;
800 vg_threads[tid].awaken_at = t_awaken;
sewardj8937c812002-04-12 20:12:20 +0000801 if (VG_(clo_trace_sched)) {
sewardj5f07b662002-04-23 16:52:51 +0000802 VG_(sprintf)(msg_buf, "at %d: nanosleep for %d",
sewardje663cb92002-04-12 10:26:32 +0000803 t_now, t_awaken-t_now);
804 print_sched_event(tid, msg_buf);
805 }
806 /* Force the scheduler to run something else for a while. */
807 return;
808 }
809
sewardjaec22c02002-04-29 01:58:08 +0000810 if (syscall_no != __NR_read && syscall_no != __NR_write) {
sewardje663cb92002-04-12 10:26:32 +0000811 /* We think it's non-blocking. Just do it in the normal way. */
812 VG_(perform_assumed_nonblocking_syscall)(tid);
813 /* The thread is still runnable. */
814 return;
815 }
816
sewardje663cb92002-04-12 10:26:32 +0000817 /* Set the fd to nonblocking, and do the syscall, which will return
818 immediately, in order to lodge a request with the Linux kernel.
819 We later poll for I/O completion using select(). */
820
sewardjaec22c02002-04-29 01:58:08 +0000821 fd = vg_threads[tid].m_ebx /* arg1 */;
sewardje663cb92002-04-12 10:26:32 +0000822 orig_fd_blockness = fd_is_blockful(fd);
823 set_fd_nonblocking(fd);
824 vg_assert(!fd_is_blockful(fd));
825 VG_(check_known_blocking_syscall)(tid, syscall_no, NULL /* PRE */);
826
827 /* This trashes the thread's %eax; we have to preserve it. */
828 saved_eax = vg_threads[tid].m_eax;
829 KERNEL_DO_SYSCALL(tid,res);
830
831 /* Restore original blockfulness of the fd. */
832 if (orig_fd_blockness)
833 set_fd_blocking(fd);
834 else
835 set_fd_nonblocking(fd);
836
sewardjaec22c02002-04-29 01:58:08 +0000837 if (res != -VKI_EWOULDBLOCK || !orig_fd_blockness) {
838 /* Finish off in the normal way. Don't restore %EAX, since that
839 now (correctly) holds the result of the call. We get here if either:
840 1. The call didn't block, or
841 2. The fd was already in nonblocking mode before we started to
842 mess with it. In this case, we're not expecting to handle
843 the I/O completion -- the client is. So don't file a
844 completion-wait entry.
845 */
sewardje663cb92002-04-12 10:26:32 +0000846 VG_(check_known_blocking_syscall)(tid, syscall_no, &res /* POST */);
847 /* We're still runnable. */
848 vg_assert(vg_threads[tid].status == VgTs_Runnable);
849
850 } else {
851
sewardjaec22c02002-04-29 01:58:08 +0000852 vg_assert(res == -VKI_EWOULDBLOCK && orig_fd_blockness);
853
sewardje663cb92002-04-12 10:26:32 +0000854 /* It would have blocked. First, restore %EAX to what it was
855 before our speculative call. */
856 vg_threads[tid].m_eax = saved_eax;
857 /* Put this fd in a table of fds on which we are waiting for
858 completion. The arguments for select() later are constructed
859 from this table. */
860 add_waiting_fd(tid, fd, saved_eax /* which holds the syscall # */);
861 /* Deschedule thread until an I/O completion happens. */
862 vg_threads[tid].status = VgTs_WaitFD;
sewardj8937c812002-04-12 20:12:20 +0000863 if (VG_(clo_trace_sched)) {
sewardje663cb92002-04-12 10:26:32 +0000864 VG_(sprintf)(msg_buf,"block until I/O ready on fd %d", fd);
865 print_sched_event(tid, msg_buf);
866 }
867
868 }
869}
870
871
872/* Find out which of the fds in vg_waiting_fds are now ready to go, by
873 making enquiries with select(), and mark them as ready. We have to
874 wait for the requesting threads to fall into the the WaitFD state
875 before we can actually finally deliver the results, so this
876 procedure doesn't do that; complete_blocked_syscalls() does it.
877
878 It might seem odd that a thread which has done a blocking syscall
879 is not in WaitFD state; the way this can happen is if it initially
880 becomes WaitFD, but then a signal is delivered to it, so it becomes
881 Runnable for a while. In this case we have to wait for the
882 sighandler to return, whereupon the WaitFD state is resumed, and
883 only at that point can the I/O result be delivered to it. However,
884 this point may be long after the fd is actually ready.
885
886 So, poll_for_ready_fds() merely detects fds which are ready.
887 complete_blocked_syscalls() does the second half of the trick,
888 possibly much later: it delivers the results from ready fds to
889 threads in WaitFD state.
890*/
sewardj9a199dc2002-04-14 13:01:38 +0000891static
sewardje663cb92002-04-12 10:26:32 +0000892void poll_for_ready_fds ( void )
893{
894 vki_ksigset_t saved_procmask;
895 vki_fd_set readfds;
896 vki_fd_set writefds;
897 vki_fd_set exceptfds;
898 struct vki_timeval timeout;
899 Int fd, fd_max, i, n_ready, syscall_no, n_ok;
900 ThreadId tid;
901 Bool rd_ok, wr_ok, ex_ok;
902 Char msg_buf[100];
903
sewardje462e202002-04-13 04:09:07 +0000904 struct vki_timespec* rem;
sewardj5f07b662002-04-23 16:52:51 +0000905 UInt t_now;
sewardje462e202002-04-13 04:09:07 +0000906
sewardje663cb92002-04-12 10:26:32 +0000907 /* Awaken any sleeping threads whose sleep has expired. */
sewardj6072c362002-04-19 14:40:57 +0000908 for (tid = 1; tid < VG_N_THREADS; tid++)
sewardj853f55d2002-04-26 00:27:53 +0000909 if (vg_threads[tid].status == VgTs_Sleeping)
910 break;
sewardj6072c362002-04-19 14:40:57 +0000911
sewardj5f07b662002-04-23 16:52:51 +0000912 /* Avoid pointless calls to VG_(read_millisecond_timer). */
sewardj6072c362002-04-19 14:40:57 +0000913 if (tid < VG_N_THREADS) {
sewardj5f07b662002-04-23 16:52:51 +0000914 t_now = VG_(read_millisecond_timer)();
sewardj6072c362002-04-19 14:40:57 +0000915 for (tid = 1; tid < VG_N_THREADS; tid++) {
916 if (vg_threads[tid].status != VgTs_Sleeping)
917 continue;
918 if (t_now >= vg_threads[tid].awaken_at) {
919 /* Resume this thread. Set to zero the remaining-time
920 (second) arg of nanosleep, since it's used up all its
921 time. */
922 vg_assert(vg_threads[tid].m_eax == __NR_nanosleep);
923 rem = (struct vki_timespec *)vg_threads[tid].m_ecx; /* arg2 */
924 if (rem != NULL) {
925 rem->tv_sec = 0;
926 rem->tv_nsec = 0;
927 }
928 /* Make the syscall return 0 (success). */
929 vg_threads[tid].m_eax = 0;
930 /* Reschedule this thread. */
931 vg_threads[tid].status = VgTs_Runnable;
932 if (VG_(clo_trace_sched)) {
sewardj5f07b662002-04-23 16:52:51 +0000933 VG_(sprintf)(msg_buf, "at %d: nanosleep done",
sewardj6072c362002-04-19 14:40:57 +0000934 t_now);
935 print_sched_event(tid, msg_buf);
936 }
sewardje663cb92002-04-12 10:26:32 +0000937 }
938 }
939 }
sewardje663cb92002-04-12 10:26:32 +0000940
sewardje462e202002-04-13 04:09:07 +0000941 /* And look for threads waiting on file descriptors which are now
942 ready for I/O.*/
sewardje663cb92002-04-12 10:26:32 +0000943 timeout.tv_sec = 0;
944 timeout.tv_usec = 0;
945
946 VKI_FD_ZERO(&readfds);
947 VKI_FD_ZERO(&writefds);
948 VKI_FD_ZERO(&exceptfds);
949 fd_max = -1;
950 for (i = 0; i < VG_N_WAITING_FDS; i++) {
951 if (vg_waiting_fds[i].fd == -1 /* not in use */)
952 continue;
953 if (vg_waiting_fds[i].ready /* already ready? */)
954 continue;
955 fd = vg_waiting_fds[i].fd;
956 /* VG_(printf)("adding QUERY for fd %d\n", fd); */
sewardje462e202002-04-13 04:09:07 +0000957 vg_assert(fd >= 0);
sewardje663cb92002-04-12 10:26:32 +0000958 if (fd > fd_max)
959 fd_max = fd;
960 tid = vg_waiting_fds[i].tid;
sewardj6072c362002-04-19 14:40:57 +0000961 vg_assert(is_valid_tid(tid));
sewardje663cb92002-04-12 10:26:32 +0000962 syscall_no = vg_waiting_fds[i].syscall_no;
963 switch (syscall_no) {
964 case __NR_read:
965 VKI_FD_SET(fd, &readfds); break;
966 case __NR_write:
967 VKI_FD_SET(fd, &writefds); break;
968 default:
969 VG_(panic)("poll_for_ready_fds: unexpected syscall");
970 /*NOTREACHED*/
971 break;
972 }
973 }
974
sewardje462e202002-04-13 04:09:07 +0000975 /* Short cut: if no fds are waiting, give up now. */
976 if (fd_max == -1)
977 return;
978
sewardje663cb92002-04-12 10:26:32 +0000979 /* BLOCK ALL SIGNALS. We don't want the complication of select()
980 getting interrupted. */
981 VG_(block_all_host_signals)( &saved_procmask );
982
983 n_ready = VG_(select)
984 ( fd_max+1, &readfds, &writefds, &exceptfds, &timeout);
985 if (VG_(is_kerror)(n_ready)) {
986 VG_(printf)("poll_for_ready_fds: select returned %d\n", n_ready);
987 VG_(panic)("poll_for_ready_fds: select failed?!");
988 /*NOTREACHED*/
989 }
990
991 /* UNBLOCK ALL SIGNALS */
992 VG_(restore_host_signals)( &saved_procmask );
993
994 /* VG_(printf)("poll_for_io_completions: %d fs ready\n", n_ready); */
995
996 if (n_ready == 0)
997 return;
998
999 /* Inspect all the fds we know about, and handle any completions that
1000 have happened. */
1001 /*
1002 VG_(printf)("\n\n");
1003 for (fd = 0; fd < 100; fd++)
1004 if (VKI_FD_ISSET(fd, &writefds) || VKI_FD_ISSET(fd, &readfds)) {
1005 VG_(printf)("X"); } else { VG_(printf)("."); };
1006 VG_(printf)("\n\nfd_max = %d\n", fd_max);
1007 */
1008
1009 for (fd = 0; fd <= fd_max; fd++) {
1010 rd_ok = VKI_FD_ISSET(fd, &readfds);
1011 wr_ok = VKI_FD_ISSET(fd, &writefds);
1012 ex_ok = VKI_FD_ISSET(fd, &exceptfds);
1013
1014 n_ok = (rd_ok ? 1 : 0) + (wr_ok ? 1 : 0) + (ex_ok ? 1 : 0);
1015 if (n_ok == 0)
1016 continue;
1017 if (n_ok > 1) {
1018 VG_(printf)("offending fd = %d\n", fd);
1019 VG_(panic)("poll_for_ready_fds: multiple events on fd");
1020 }
1021
1022 /* An I/O event completed for fd. Find the thread which
1023 requested this. */
1024 for (i = 0; i < VG_N_WAITING_FDS; i++) {
1025 if (vg_waiting_fds[i].fd == -1 /* not in use */)
1026 continue;
1027 if (vg_waiting_fds[i].fd == fd)
1028 break;
1029 }
1030
1031 /* And a bit more paranoia ... */
1032 vg_assert(i >= 0 && i < VG_N_WAITING_FDS);
1033
1034 /* Mark the fd as ready. */
1035 vg_assert(! vg_waiting_fds[i].ready);
1036 vg_waiting_fds[i].ready = True;
1037 }
1038}
1039
1040
1041/* See comment attached to poll_for_ready_fds() for explaination. */
sewardj9a199dc2002-04-14 13:01:38 +00001042static
sewardje663cb92002-04-12 10:26:32 +00001043void complete_blocked_syscalls ( void )
1044{
1045 Int fd, i, res, syscall_no;
1046 ThreadId tid;
1047 Char msg_buf[100];
1048
1049 /* Inspect all the outstanding fds we know about. */
1050
1051 for (i = 0; i < VG_N_WAITING_FDS; i++) {
1052 if (vg_waiting_fds[i].fd == -1 /* not in use */)
1053 continue;
1054 if (! vg_waiting_fds[i].ready)
1055 continue;
1056
1057 fd = vg_waiting_fds[i].fd;
1058 tid = vg_waiting_fds[i].tid;
sewardj6072c362002-04-19 14:40:57 +00001059 vg_assert(is_valid_tid(tid));
sewardje663cb92002-04-12 10:26:32 +00001060
1061 /* The thread actually has to be waiting for the I/O event it
1062 requested before we can deliver the result! */
1063 if (vg_threads[tid].status != VgTs_WaitFD)
1064 continue;
1065
1066 /* Ok, actually do it! We can safely use %EAX as the syscall
1067 number, because the speculative call made by
1068 sched_do_syscall() doesn't change %EAX in the case where the
1069 call would have blocked. */
1070
1071 syscall_no = vg_waiting_fds[i].syscall_no;
1072 vg_assert(syscall_no == vg_threads[tid].m_eax);
1073 KERNEL_DO_SYSCALL(tid,res);
1074 VG_(check_known_blocking_syscall)(tid, syscall_no, &res /* POST */);
1075
1076 /* Reschedule. */
1077 vg_threads[tid].status = VgTs_Runnable;
1078 /* Mark slot as no longer in use. */
1079 vg_waiting_fds[i].fd = -1;
1080 /* pp_sched_status(); */
sewardj8937c812002-04-12 20:12:20 +00001081 if (VG_(clo_trace_sched)) {
sewardje663cb92002-04-12 10:26:32 +00001082 VG_(sprintf)(msg_buf,"resume due to I/O completion on fd %d", fd);
1083 print_sched_event(tid, msg_buf);
1084 }
1085 }
1086}
1087
1088
1089static
sewardj5f07b662002-04-23 16:52:51 +00001090void check_for_pthread_cond_timedwait ( void )
1091{
sewardj51c0aaf2002-04-25 01:32:10 +00001092 Int i, now;
sewardj5f07b662002-04-23 16:52:51 +00001093 for (i = 1; i < VG_N_THREADS; i++) {
1094 if (vg_threads[i].status != VgTs_WaitCV)
1095 continue;
1096 if (vg_threads[i].awaken_at == 0xFFFFFFFF /* no timeout */)
1097 continue;
sewardj51c0aaf2002-04-25 01:32:10 +00001098 now = VG_(read_millisecond_timer)();
1099 if (now >= vg_threads[i].awaken_at) {
sewardj5f07b662002-04-23 16:52:51 +00001100 do_pthread_cond_timedwait_TIMEOUT(i);
sewardj51c0aaf2002-04-25 01:32:10 +00001101 }
sewardj5f07b662002-04-23 16:52:51 +00001102 }
1103}
1104
1105
1106static
sewardje663cb92002-04-12 10:26:32 +00001107void nanosleep_for_a_while ( void )
1108{
1109 Int res;
1110 struct vki_timespec req;
1111 struct vki_timespec rem;
1112 req.tv_sec = 0;
sewardj51c0aaf2002-04-25 01:32:10 +00001113 req.tv_nsec = 20 * 1000 * 1000;
sewardje663cb92002-04-12 10:26:32 +00001114 res = VG_(nanosleep)( &req, &rem );
sewardj5f07b662002-04-23 16:52:51 +00001115 vg_assert(res == 0 /* ok */ || res == 1 /* interrupted by signal */);
sewardje663cb92002-04-12 10:26:32 +00001116}
1117
1118
1119/* ---------------------------------------------------------------------
1120 The scheduler proper.
1121 ------------------------------------------------------------------ */
1122
1123/* Run user-space threads until either
1124 * Deadlock occurs
1125 * One thread asks to shutdown Valgrind
1126 * The specified number of basic blocks has gone by.
1127*/
1128VgSchedReturnCode VG_(scheduler) ( void )
1129{
1130 ThreadId tid, tid_next;
1131 UInt trc;
1132 UInt dispatch_ctr_SAVED;
sewardj51c0aaf2002-04-25 01:32:10 +00001133 Int request_code, done_this_time, n_in_bounded_wait;
sewardje663cb92002-04-12 10:26:32 +00001134 Char msg_buf[100];
1135 Addr trans_addr;
sewardj14e03422002-04-24 19:51:31 +00001136 Bool sigs_delivered;
sewardje663cb92002-04-12 10:26:32 +00001137
1138 /* For the LRU structures, records when the epoch began. */
1139 ULong lru_epoch_started_at = 0;
1140
1141 /* Start with the root thread. tid in general indicates the
1142 currently runnable/just-finished-running thread. */
sewardj6072c362002-04-19 14:40:57 +00001143 tid = 1;
sewardje663cb92002-04-12 10:26:32 +00001144
1145 /* This is the top level scheduler loop. It falls into three
1146 phases. */
1147 while (True) {
1148
sewardj6072c362002-04-19 14:40:57 +00001149 /* ======================= Phase 0 of 3 =======================
1150 Be paranoid. Always a good idea. */
sewardjd7fd4d22002-04-24 01:57:27 +00001151 stage1:
sewardj6072c362002-04-19 14:40:57 +00001152 scheduler_sanity();
1153
sewardje663cb92002-04-12 10:26:32 +00001154 /* ======================= Phase 1 of 3 =======================
1155 Handle I/O completions and signals. This may change the
1156 status of various threads. Then select a new thread to run,
1157 or declare deadlock, or sleep if there are no runnable
1158 threads but some are blocked on I/O. */
1159
1160 /* Age the LRU structures if an epoch has been completed. */
1161 if (VG_(bbs_done) - lru_epoch_started_at >= VG_BBS_PER_EPOCH) {
1162 lru_epoch_started_at = VG_(bbs_done);
1163 increment_epoch();
1164 }
1165
1166 /* Was a debug-stop requested? */
1167 if (VG_(bbs_to_go) == 0)
1168 goto debug_stop;
1169
1170 /* Do the following loop until a runnable thread is found, or
1171 deadlock is detected. */
1172 while (True) {
1173
1174 /* For stats purposes only. */
1175 VG_(num_scheduling_events_MAJOR) ++;
1176
1177 /* See if any I/O operations which we were waiting for have
1178 completed, and, if so, make runnable the relevant waiting
1179 threads. */
1180 poll_for_ready_fds();
1181 complete_blocked_syscalls();
sewardj5f07b662002-04-23 16:52:51 +00001182 check_for_pthread_cond_timedwait();
sewardje663cb92002-04-12 10:26:32 +00001183
1184 /* See if there are any signals which need to be delivered. If
1185 so, choose thread(s) to deliver them to, and build signal
1186 delivery frames on those thread(s) stacks. */
sewardj6072c362002-04-19 14:40:57 +00001187
1188 /* Be careful about delivering signals to a thread waiting
1189 for a mutex. In particular, when the handler is running,
1190 that thread is temporarily apparently-not-waiting for the
1191 mutex, so if it is unlocked by another thread whilst the
1192 handler is running, this thread is not informed. When the
1193 handler returns, the thread resumes waiting on the mutex,
1194 even if, as a result, it has missed the unlocking of it.
1195 Potential deadlock. This sounds all very strange, but the
1196 POSIX standard appears to require this behaviour. */
sewardj14e03422002-04-24 19:51:31 +00001197 sigs_delivered = VG_(deliver_signals)( 1 /*HACK*/ );
1198 if (sigs_delivered)
1199 VG_(do_sanity_checks)( 1 /*HACK*/, False );
sewardje663cb92002-04-12 10:26:32 +00001200
1201 /* Try and find a thread (tid) to run. */
1202 tid_next = tid;
sewardj51c0aaf2002-04-25 01:32:10 +00001203 n_in_bounded_wait = 0;
sewardje663cb92002-04-12 10:26:32 +00001204 while (True) {
1205 tid_next++;
sewardj6072c362002-04-19 14:40:57 +00001206 if (tid_next >= VG_N_THREADS) tid_next = 1;
sewardj54cacf02002-04-12 23:24:59 +00001207 if (vg_threads[tid_next].status == VgTs_WaitFD
sewardj51c0aaf2002-04-25 01:32:10 +00001208 || vg_threads[tid_next].status == VgTs_Sleeping
1209 || (vg_threads[tid_next].status == VgTs_WaitCV
1210 && vg_threads[tid_next].awaken_at != 0xFFFFFFFF))
1211 n_in_bounded_wait ++;
sewardje663cb92002-04-12 10:26:32 +00001212 if (vg_threads[tid_next].status == VgTs_Runnable)
1213 break; /* We can run this one. */
1214 if (tid_next == tid)
1215 break; /* been all the way round */
1216 }
1217 tid = tid_next;
1218
1219 if (vg_threads[tid].status == VgTs_Runnable) {
1220 /* Found a suitable candidate. Fall out of this loop, so
1221 we can advance to stage 2 of the scheduler: actually
1222 running the thread. */
1223 break;
1224 }
1225
1226 /* We didn't find a runnable thread. Now what? */
sewardj51c0aaf2002-04-25 01:32:10 +00001227 if (n_in_bounded_wait == 0) {
sewardj54cacf02002-04-12 23:24:59 +00001228 /* No runnable threads and no prospect of any appearing
1229 even if we wait for an arbitrary length of time. In
1230 short, we have a deadlock. */
sewardj15a43e12002-04-17 19:35:12 +00001231 VG_(pp_sched_status)();
sewardje663cb92002-04-12 10:26:32 +00001232 return VgSrc_Deadlock;
1233 }
1234
1235 /* At least one thread is in a fd-wait state. Delay for a
1236 while, and go round again, in the hope that eventually a
1237 thread becomes runnable. */
1238 nanosleep_for_a_while();
1239 // pp_sched_status();
1240 // VG_(printf)(".\n");
1241 }
1242
1243
1244 /* ======================= Phase 2 of 3 =======================
1245 Wahey! We've finally decided that thread tid is runnable, so
1246 we now do that. Run it for as much of a quanta as possible.
1247 Trivial requests are handled and the thread continues. The
1248 aim is not to do too many of Phase 1 since it is expensive. */
1249
1250 if (0)
sewardj3b5d8862002-04-20 13:53:23 +00001251 VG_(printf)("SCHED: tid %d\n", tid);
sewardje663cb92002-04-12 10:26:32 +00001252
1253 /* Figure out how many bbs to ask vg_run_innerloop to do. Note
1254 that it decrements the counter before testing it for zero, so
1255 that if VG_(dispatch_ctr) is set to N you get at most N-1
1256 iterations. Also this means that VG_(dispatch_ctr) must
1257 exceed zero before entering the innerloop. Also also, the
1258 decrement is done before the bb is actually run, so you
1259 always get at least one decrement even if nothing happens.
1260 */
1261 if (VG_(bbs_to_go) >= VG_SCHEDULING_QUANTUM)
1262 VG_(dispatch_ctr) = VG_SCHEDULING_QUANTUM + 1;
1263 else
1264 VG_(dispatch_ctr) = (UInt)VG_(bbs_to_go) + 1;
1265
1266 /* ... and remember what we asked for. */
1267 dispatch_ctr_SAVED = VG_(dispatch_ctr);
1268
sewardj1e8cdc92002-04-18 11:37:52 +00001269 /* paranoia ... */
1270 vg_assert(vg_threads[tid].tid == tid);
1271
sewardje663cb92002-04-12 10:26:32 +00001272 /* Actually run thread tid. */
1273 while (True) {
1274
1275 /* For stats purposes only. */
1276 VG_(num_scheduling_events_MINOR) ++;
1277
1278 if (0)
1279 VG_(message)(Vg_DebugMsg, "thread %d: running for %d bbs",
1280 tid, VG_(dispatch_ctr) - 1 );
sewardjb3eef6b2002-05-01 00:05:27 +00001281# if 0
1282 if (VG_(bbs_done) > 31700000 + 0) {
1283 dispatch_ctr_SAVED = VG_(dispatch_ctr) = 2;
1284 VG_(translate)(&vg_threads[tid], vg_threads[tid].m_eip,
1285 NULL,NULL,NULL);
1286 }
1287 vg_assert(vg_threads[tid].m_eip != 0);
1288# endif
sewardje663cb92002-04-12 10:26:32 +00001289
1290 trc = run_thread_for_a_while ( tid );
1291
sewardjb3eef6b2002-05-01 00:05:27 +00001292# if 0
1293 if (0 == vg_threads[tid].m_eip) {
1294 VG_(printf)("tid = %d, dc = %llu\n", tid, VG_(bbs_done));
1295 vg_assert(0 != vg_threads[tid].m_eip);
1296 }
1297# endif
1298
sewardje663cb92002-04-12 10:26:32 +00001299 /* Deal quickly with trivial scheduling events, and resume the
1300 thread. */
1301
1302 if (trc == VG_TRC_INNER_FASTMISS) {
1303 vg_assert(VG_(dispatch_ctr) > 0);
1304
1305 /* Trivial event. Miss in the fast-cache. Do a full
1306 lookup for it. */
1307 trans_addr
1308 = VG_(search_transtab) ( vg_threads[tid].m_eip );
1309 if (trans_addr == (Addr)0) {
1310 /* Not found; we need to request a translation. */
sewardj1e8cdc92002-04-18 11:37:52 +00001311 create_translation_for( tid, vg_threads[tid].m_eip );
sewardje663cb92002-04-12 10:26:32 +00001312 trans_addr = VG_(search_transtab) ( vg_threads[tid].m_eip );
1313 if (trans_addr == (Addr)0)
1314 VG_(panic)("VG_TRC_INNER_FASTMISS: missing tt_fast entry");
1315 }
1316 continue; /* with this thread */
1317 }
1318
1319 if (trc == VG_TRC_EBP_JMP_CLIENTREQ) {
sewardjd7fd4d22002-04-24 01:57:27 +00001320 Bool done = maybe_do_trivial_clientreq(tid);
1321 if (done) {
1322 /* The request is done. We try and continue with the
1323 same thread if still runnable. If not, go back to
1324 Stage 1 to select a new thread to run. */
1325 if (vg_threads[tid].status == VgTs_Runnable)
1326 continue; /* with this thread */
1327 else
1328 goto stage1;
sewardje663cb92002-04-12 10:26:32 +00001329 }
1330 }
1331
sewardj51c0aaf2002-04-25 01:32:10 +00001332 if (trc == VG_TRC_EBP_JMP_SYSCALL) {
1333 /* Do a syscall for the vthread tid. This could cause it
1334 to become non-runnable. */
sewardjb3eef6b2002-05-01 00:05:27 +00001335# if 0
1336 { UInt* esp; Int i;
1337 esp=(UInt*)vg_threads[tid].m_esp;
1338 VG_(printf)("\nBEFORE\n");
1339 for (i = 10; i >= -10; i--)
1340 VG_(printf)("%2d %p = 0x%x\n", i, &esp[i], esp[i]);
1341 }
1342# endif
1343
sewardj51c0aaf2002-04-25 01:32:10 +00001344 sched_do_syscall(tid);
sewardjb3eef6b2002-05-01 00:05:27 +00001345
1346# if 0
1347 { UInt* esp; Int i;
1348 esp=(UInt*)vg_threads[tid].m_esp;
1349 VG_(printf)("AFTER\n");
1350 for (i = 10; i >= -10; i--)
1351 VG_(printf)("%2d %p = 0x%x\n", i, &esp[i], esp[i]);
1352 }
1353# endif
1354
sewardj51c0aaf2002-04-25 01:32:10 +00001355 if (vg_threads[tid].status == VgTs_Runnable)
1356 continue; /* with this thread */
1357 else
1358 goto stage1;
1359 }
1360
sewardjd7fd4d22002-04-24 01:57:27 +00001361 /* It's an event we can't quickly deal with. Give up running
1362 this thread and handle things the expensive way. */
sewardje663cb92002-04-12 10:26:32 +00001363 break;
1364 }
1365
1366 /* ======================= Phase 3 of 3 =======================
1367 Handle non-trivial thread requests, mostly pthread stuff. */
1368
1369 /* Ok, we've fallen out of the dispatcher for a
1370 non-completely-trivial reason. First, update basic-block
1371 counters. */
1372
1373 done_this_time = (Int)dispatch_ctr_SAVED - (Int)VG_(dispatch_ctr) - 1;
1374 vg_assert(done_this_time >= 0);
1375 VG_(bbs_to_go) -= (ULong)done_this_time;
1376 VG_(bbs_done) += (ULong)done_this_time;
1377
1378 if (0 && trc != VG_TRC_INNER_FASTMISS)
1379 VG_(message)(Vg_DebugMsg, "thread %d: completed %d bbs, trc %d",
1380 tid, done_this_time, (Int)trc );
1381
1382 if (0 && trc != VG_TRC_INNER_FASTMISS)
1383 VG_(message)(Vg_DebugMsg, "thread %d: %ld bbs, event %s",
1384 tid, VG_(bbs_done),
1385 name_of_sched_event(trc) );
sewardj9d1b5d32002-04-17 19:40:49 +00001386
sewardje663cb92002-04-12 10:26:32 +00001387 /* Examine the thread's return code to figure out why it
1388 stopped, and handle requests. */
1389
1390 switch (trc) {
1391
1392 case VG_TRC_INNER_FASTMISS:
1393 VG_(panic)("VG_(scheduler): VG_TRC_INNER_FASTMISS");
1394 /*NOTREACHED*/
1395 break;
1396
1397 case VG_TRC_INNER_COUNTERZERO:
1398 /* Timeslice is out. Let a new thread be scheduled,
1399 simply by doing nothing, causing us to arrive back at
1400 Phase 1. */
1401 if (VG_(bbs_to_go) == 0) {
1402 goto debug_stop;
1403 }
1404 vg_assert(VG_(dispatch_ctr) == 0);
1405 break;
1406
1407 case VG_TRC_UNRESUMABLE_SIGNAL:
1408 /* It got a SIGSEGV/SIGBUS, which we need to deliver right
1409 away. Again, do nothing, so we wind up back at Phase
1410 1, whereupon the signal will be "delivered". */
1411 break;
1412
sewardj51c0aaf2002-04-25 01:32:10 +00001413#if 0
sewardje663cb92002-04-12 10:26:32 +00001414 case VG_TRC_EBP_JMP_SYSCALL:
1415 /* Do a syscall for the vthread tid. This could cause it
1416 to become non-runnable. */
1417 sched_do_syscall(tid);
1418 break;
sewardj51c0aaf2002-04-25 01:32:10 +00001419#endif
sewardje663cb92002-04-12 10:26:32 +00001420
1421 case VG_TRC_EBP_JMP_CLIENTREQ:
1422 /* Do a client request for the vthread tid. Note that
1423 some requests will have been handled by
1424 maybe_do_trivial_clientreq(), so we don't expect to see
1425 those here.
1426 */
sewardj54cacf02002-04-12 23:24:59 +00001427 /* The thread's %EAX points at an arg block, the first
1428 word of which is the request code. */
1429 request_code = ((UInt*)(vg_threads[tid].m_eax))[0];
sewardje663cb92002-04-12 10:26:32 +00001430 if (0) {
sewardj54cacf02002-04-12 23:24:59 +00001431 VG_(sprintf)(msg_buf, "request 0x%x", request_code );
sewardje663cb92002-04-12 10:26:32 +00001432 print_sched_event(tid, msg_buf);
1433 }
1434 /* Do a non-trivial client request for thread tid. tid's
1435 %EAX points to a short vector of argument words, the
1436 first of which is the request code. The result of the
1437 request is put in tid's %EDX. Alternatively, perhaps
1438 the request causes tid to become non-runnable and/or
1439 other blocked threads become runnable. In general we
1440 can and often do mess with the state of arbitrary
1441 threads at this point. */
sewardj54cacf02002-04-12 23:24:59 +00001442 if (request_code == VG_USERREQ__SHUTDOWN_VALGRIND) {
1443 return VgSrc_Shutdown;
1444 } else {
1445 do_nontrivial_clientreq(tid);
1446 }
sewardje663cb92002-04-12 10:26:32 +00001447 break;
1448
1449 default:
1450 VG_(printf)("\ntrc = %d\n", trc);
1451 VG_(panic)("VG_(scheduler), phase 3: "
1452 "unexpected thread return code");
1453 /* NOTREACHED */
1454 break;
1455
1456 } /* switch (trc) */
1457
1458 /* That completes Phase 3 of 3. Return now to the top of the
1459 main scheduler loop, to Phase 1 of 3. */
1460
1461 } /* top-level scheduler loop */
1462
1463
1464 /* NOTREACHED */
1465 VG_(panic)("scheduler: post-main-loop ?!");
1466 /* NOTREACHED */
1467
1468 debug_stop:
1469 /* If we exited because of a debug stop, print the translation
1470 of the last block executed -- by translating it again, and
1471 throwing away the result. */
1472 VG_(printf)(
1473 "======vvvvvvvv====== LAST TRANSLATION ======vvvvvvvv======\n");
sewardj1e8cdc92002-04-18 11:37:52 +00001474 VG_(translate)( &vg_threads[tid], vg_threads[tid].m_eip, NULL, NULL, NULL );
sewardje663cb92002-04-12 10:26:32 +00001475 VG_(printf)("\n");
1476 VG_(printf)(
1477 "======^^^^^^^^====== LAST TRANSLATION ======^^^^^^^^======\n");
1478
1479 return VgSrc_BbsDone;
1480}
1481
1482
1483/* ---------------------------------------------------------------------
1484 The pthread implementation.
1485 ------------------------------------------------------------------ */
1486
1487#include <pthread.h>
1488#include <errno.h>
1489
1490#if !defined(PTHREAD_STACK_MIN)
1491# define PTHREAD_STACK_MIN (16384 - VG_AR_CLIENT_STACKBASE_REDZONE_SZB)
1492#endif
1493
1494/* /usr/include/bits/pthreadtypes.h:
1495 typedef unsigned long int pthread_t;
1496*/
1497
sewardje663cb92002-04-12 10:26:32 +00001498
sewardj604ec3c2002-04-18 22:38:41 +00001499/* -----------------------------------------------------------
1500 Thread CREATION, JOINAGE and CANCELLATION.
1501 -------------------------------------------------------- */
1502
sewardje663cb92002-04-12 10:26:32 +00001503static
sewardj853f55d2002-04-26 00:27:53 +00001504void do_pthread_cancel ( ThreadId tid,
sewardje663cb92002-04-12 10:26:32 +00001505 pthread_t tid_cancellee )
1506{
1507 Char msg_buf[100];
sewardj853f55d2002-04-26 00:27:53 +00001508
1509 vg_assert(is_valid_tid(tid));
1510 vg_assert(vg_threads[tid].status != VgTs_Empty);
1511
1512 if (!is_valid_tid(tid_cancellee)
1513 || vg_threads[tid_cancellee].status == VgTs_Empty) {
1514 vg_threads[tid].m_edx = ESRCH;
1515 return;
1516 }
1517
sewardje663cb92002-04-12 10:26:32 +00001518 /* We want make is appear that this thread has returned to
1519 do_pthread_create_bogusRA with PTHREAD_CANCELED as the
1520 return value. So: simple: put PTHREAD_CANCELED into %EAX
1521 and &do_pthread_create_bogusRA into %EIP and keep going! */
sewardj8937c812002-04-12 20:12:20 +00001522 if (VG_(clo_trace_sched)) {
sewardj853f55d2002-04-26 00:27:53 +00001523 VG_(sprintf)(msg_buf, "cancelled by %d", tid);
sewardje663cb92002-04-12 10:26:32 +00001524 print_sched_event(tid_cancellee, msg_buf);
1525 }
1526 vg_threads[tid_cancellee].m_eax = (UInt)PTHREAD_CANCELED;
sewardjbc5b99f2002-04-13 00:08:51 +00001527 vg_threads[tid_cancellee].m_eip = (UInt)&VG_(pthreadreturn_bogusRA);
sewardje663cb92002-04-12 10:26:32 +00001528 vg_threads[tid_cancellee].status = VgTs_Runnable;
sewardj853f55d2002-04-26 00:27:53 +00001529
1530 /* We return with success (0). */
1531 vg_threads[tid].m_edx = 0;
sewardje663cb92002-04-12 10:26:32 +00001532}
1533
1534
sewardj3b5d8862002-04-20 13:53:23 +00001535static
1536void do_pthread_exit ( ThreadId tid, void* retval )
1537{
1538 Char msg_buf[100];
1539 /* We want make is appear that this thread has returned to
1540 do_pthread_create_bogusRA with retval as the
1541 return value. So: simple: put retval into %EAX
1542 and &do_pthread_create_bogusRA into %EIP and keep going! */
1543 if (VG_(clo_trace_sched)) {
1544 VG_(sprintf)(msg_buf, "exiting with %p", retval);
1545 print_sched_event(tid, msg_buf);
1546 }
1547 vg_threads[tid].m_eax = (UInt)retval;
1548 vg_threads[tid].m_eip = (UInt)&VG_(pthreadreturn_bogusRA);
1549 vg_threads[tid].status = VgTs_Runnable;
1550}
1551
sewardje663cb92002-04-12 10:26:32 +00001552
1553/* Thread tid is exiting, by returning from the function it was
sewardjbc5b99f2002-04-13 00:08:51 +00001554 created with. Or possibly due to pthread_exit or cancellation.
1555 The main complication here is to resume any thread waiting to join
1556 with this one. */
sewardje663cb92002-04-12 10:26:32 +00001557static
sewardjbc5b99f2002-04-13 00:08:51 +00001558void handle_pthread_return ( ThreadId tid, void* retval )
sewardje663cb92002-04-12 10:26:32 +00001559{
1560 ThreadId jnr; /* joiner, the thread calling pthread_join. */
1561 UInt* jnr_args;
1562 void** jnr_thread_return;
1563 Char msg_buf[100];
1564
1565 /* Mark it as not in use. Leave the stack in place so the next
1566 user of this slot doesn't reallocate it. */
sewardj6072c362002-04-19 14:40:57 +00001567 vg_assert(is_valid_tid(tid));
sewardje663cb92002-04-12 10:26:32 +00001568 vg_assert(vg_threads[tid].status != VgTs_Empty);
1569
sewardjbc5b99f2002-04-13 00:08:51 +00001570 vg_threads[tid].retval = retval;
sewardje663cb92002-04-12 10:26:32 +00001571
1572 if (vg_threads[tid].joiner == VG_INVALID_THREADID) {
1573 /* No one has yet done a join on me */
1574 vg_threads[tid].status = VgTs_WaitJoiner;
sewardj8937c812002-04-12 20:12:20 +00001575 if (VG_(clo_trace_sched)) {
sewardje663cb92002-04-12 10:26:32 +00001576 VG_(sprintf)(msg_buf,
1577 "root fn returns, waiting for a call pthread_join(%d)",
1578 tid);
1579 print_sched_event(tid, msg_buf);
1580 }
1581 } else {
1582 /* Some is waiting; make their join call return with success,
1583 putting my exit code in the place specified by the caller's
1584 thread_return param. This is all very horrible, since we
1585 need to consult the joiner's arg block -- pointed to by its
1586 %EAX -- in order to extract the 2nd param of its pthread_join
1587 call. TODO: free properly the slot (also below).
1588 */
1589 jnr = vg_threads[tid].joiner;
sewardj6072c362002-04-19 14:40:57 +00001590 vg_assert(is_valid_tid(jnr));
sewardje663cb92002-04-12 10:26:32 +00001591 vg_assert(vg_threads[jnr].status == VgTs_WaitJoinee);
1592 jnr_args = (UInt*)vg_threads[jnr].m_eax;
1593 jnr_thread_return = (void**)(jnr_args[2]);
1594 if (jnr_thread_return != NULL)
1595 *jnr_thread_return = vg_threads[tid].retval;
1596 vg_threads[jnr].m_edx = 0; /* success */
1597 vg_threads[jnr].status = VgTs_Runnable;
1598 vg_threads[tid].status = VgTs_Empty; /* bye! */
sewardj75fe1892002-04-14 02:46:33 +00001599 if (VG_(clo_instrument) && tid != 0)
1600 VGM_(make_noaccess)( vg_threads[tid].stack_base,
1601 vg_threads[tid].stack_size );
sewardj8937c812002-04-12 20:12:20 +00001602 if (VG_(clo_trace_sched)) {
sewardje663cb92002-04-12 10:26:32 +00001603 VG_(sprintf)(msg_buf,
1604 "root fn returns, to find a waiting pthread_join(%d)", tid);
1605 print_sched_event(tid, msg_buf);
1606 VG_(sprintf)(msg_buf,
1607 "my pthread_join(%d) returned; resuming", tid);
1608 print_sched_event(jnr, msg_buf);
1609 }
1610 }
1611
1612 /* Return value is irrelevant; this thread will not get
1613 rescheduled. */
1614}
1615
1616
1617static
1618void do_pthread_join ( ThreadId tid, ThreadId jee, void** thread_return )
1619{
1620 Char msg_buf[100];
1621
1622 /* jee, the joinee, is the thread specified as an arg in thread
1623 tid's call to pthread_join. So tid is the join-er. */
sewardj6072c362002-04-19 14:40:57 +00001624 vg_assert(is_valid_tid(tid));
sewardje663cb92002-04-12 10:26:32 +00001625 vg_assert(vg_threads[tid].status == VgTs_Runnable);
1626
1627 if (jee == tid) {
1628 vg_threads[tid].m_edx = EDEADLK; /* libc constant, not a kernel one */
1629 vg_threads[tid].status = VgTs_Runnable;
1630 return;
1631 }
1632
1633 if (jee < 0
1634 || jee >= VG_N_THREADS
1635 || vg_threads[jee].status == VgTs_Empty) {
1636 /* Invalid thread to join to. */
1637 vg_threads[tid].m_edx = EINVAL;
1638 vg_threads[tid].status = VgTs_Runnable;
1639 return;
1640 }
1641
1642 if (vg_threads[jee].joiner != VG_INVALID_THREADID) {
1643 /* Someone already did join on this thread */
1644 vg_threads[tid].m_edx = EINVAL;
1645 vg_threads[tid].status = VgTs_Runnable;
1646 return;
1647 }
1648
1649 /* if (vg_threads[jee].detached) ... */
1650
1651 /* Perhaps the joinee has already finished? If so return
1652 immediately with its return code, and free up the slot. TODO:
1653 free it properly (also above). */
1654 if (vg_threads[jee].status == VgTs_WaitJoiner) {
1655 vg_assert(vg_threads[jee].joiner == VG_INVALID_THREADID);
1656 vg_threads[tid].m_edx = 0; /* success */
1657 if (thread_return != NULL)
1658 *thread_return = vg_threads[jee].retval;
1659 vg_threads[tid].status = VgTs_Runnable;
1660 vg_threads[jee].status = VgTs_Empty; /* bye! */
sewardj75fe1892002-04-14 02:46:33 +00001661 if (VG_(clo_instrument) && jee != 0)
1662 VGM_(make_noaccess)( vg_threads[jee].stack_base,
1663 vg_threads[jee].stack_size );
sewardj8937c812002-04-12 20:12:20 +00001664 if (VG_(clo_trace_sched)) {
sewardje663cb92002-04-12 10:26:32 +00001665 VG_(sprintf)(msg_buf,
1666 "someone called pthread_join() on me; bye!");
1667 print_sched_event(jee, msg_buf);
1668 VG_(sprintf)(msg_buf,
1669 "my pthread_join(%d) returned immediately",
1670 jee );
1671 print_sched_event(tid, msg_buf);
1672 }
1673 return;
1674 }
1675
1676 /* Ok, so we'll have to wait on jee. */
1677 vg_threads[jee].joiner = tid;
1678 vg_threads[tid].status = VgTs_WaitJoinee;
sewardj8937c812002-04-12 20:12:20 +00001679 if (VG_(clo_trace_sched)) {
sewardje663cb92002-04-12 10:26:32 +00001680 VG_(sprintf)(msg_buf,
1681 "blocking on call of pthread_join(%d)", jee );
1682 print_sched_event(tid, msg_buf);
1683 }
1684 /* So tid's join call does not return just now. */
1685}
1686
1687
1688static
1689void do_pthread_create ( ThreadId parent_tid,
1690 pthread_t* thread,
1691 pthread_attr_t* attr,
1692 void* (*start_routine)(void *),
1693 void* arg )
1694{
sewardj5f07b662002-04-23 16:52:51 +00001695 Int i;
sewardje663cb92002-04-12 10:26:32 +00001696 Addr new_stack;
1697 UInt new_stk_szb;
1698 ThreadId tid;
1699 Char msg_buf[100];
1700
1701 /* Paranoia ... */
1702 vg_assert(sizeof(pthread_t) == sizeof(UInt));
1703
1704 vg_assert(vg_threads[parent_tid].status != VgTs_Empty);
1705
sewardj1e8cdc92002-04-18 11:37:52 +00001706 tid = vg_alloc_ThreadState();
sewardje663cb92002-04-12 10:26:32 +00001707
1708 /* If we've created the main thread's tid, we're in deep trouble :) */
sewardj6072c362002-04-19 14:40:57 +00001709 vg_assert(tid != 1);
1710 vg_assert(is_valid_tid(tid));
sewardje663cb92002-04-12 10:26:32 +00001711
1712 /* Copy the parent's CPU state into the child's, in a roundabout
1713 way (via baseBlock). */
1714 VG_(load_thread_state)(parent_tid);
1715 VG_(save_thread_state)(tid);
1716
1717 /* Consider allocating the child a stack, if the one it already has
1718 is inadequate. */
1719 new_stk_szb = PTHREAD_STACK_MIN;
1720
1721 if (new_stk_szb > vg_threads[tid].stack_size) {
1722 /* Again, for good measure :) We definitely don't want to be
1723 allocating a stack for the main thread. */
sewardj6072c362002-04-19 14:40:57 +00001724 vg_assert(tid != 1);
sewardje663cb92002-04-12 10:26:32 +00001725 /* for now, we don't handle the case of anything other than
1726 assigning it for the first time. */
1727 vg_assert(vg_threads[tid].stack_size == 0);
1728 vg_assert(vg_threads[tid].stack_base == (Addr)NULL);
1729 new_stack = (Addr)VG_(get_memory_from_mmap)( new_stk_szb );
1730 vg_threads[tid].stack_base = new_stack;
1731 vg_threads[tid].stack_size = new_stk_szb;
sewardj1e8cdc92002-04-18 11:37:52 +00001732 vg_threads[tid].stack_highest_word
sewardje663cb92002-04-12 10:26:32 +00001733 = new_stack + new_stk_szb
sewardj1e8cdc92002-04-18 11:37:52 +00001734 - VG_AR_CLIENT_STACKBASE_REDZONE_SZB; /* -4 ??? */;
sewardje663cb92002-04-12 10:26:32 +00001735 }
sewardj1e8cdc92002-04-18 11:37:52 +00001736
1737 vg_threads[tid].m_esp
1738 = vg_threads[tid].stack_base
1739 + vg_threads[tid].stack_size
1740 - VG_AR_CLIENT_STACKBASE_REDZONE_SZB;
1741
sewardje663cb92002-04-12 10:26:32 +00001742 if (VG_(clo_instrument))
1743 VGM_(make_noaccess)( vg_threads[tid].m_esp,
1744 VG_AR_CLIENT_STACKBASE_REDZONE_SZB );
1745
1746 /* push arg */
1747 vg_threads[tid].m_esp -= 4;
1748 * (UInt*)(vg_threads[tid].m_esp) = (UInt)arg;
1749
1750 /* push (magical) return address */
1751 vg_threads[tid].m_esp -= 4;
sewardjbc5b99f2002-04-13 00:08:51 +00001752 * (UInt*)(vg_threads[tid].m_esp) = (UInt)VG_(pthreadreturn_bogusRA);
sewardje663cb92002-04-12 10:26:32 +00001753
1754 if (VG_(clo_instrument))
1755 VGM_(make_readable)( vg_threads[tid].m_esp, 2 * 4 );
1756
1757 /* this is where we start */
1758 vg_threads[tid].m_eip = (UInt)start_routine;
1759
sewardj8937c812002-04-12 20:12:20 +00001760 if (VG_(clo_trace_sched)) {
sewardje663cb92002-04-12 10:26:32 +00001761 VG_(sprintf)(msg_buf,
1762 "new thread, created by %d", parent_tid );
1763 print_sched_event(tid, msg_buf);
1764 }
1765
1766 /* store the thread id in *thread. */
1767 // if (VG_(clo_instrument))
1768 // ***** CHECK *thread is writable
1769 *thread = (pthread_t)tid;
1770
sewardj3b5d8862002-04-20 13:53:23 +00001771 vg_threads[tid].associated_mx = NULL;
1772 vg_threads[tid].associated_cv = NULL;
1773 vg_threads[tid].joiner = VG_INVALID_THREADID;
1774 vg_threads[tid].status = VgTs_Runnable;
sewardj604ec3c2002-04-18 22:38:41 +00001775
sewardj5f07b662002-04-23 16:52:51 +00001776 for (i = 0; i < VG_N_THREAD_KEYS; i++)
1777 vg_threads[tid].specifics[i] = NULL;
1778
sewardj604ec3c2002-04-18 22:38:41 +00001779 /* return zero */
sewardje663cb92002-04-12 10:26:32 +00001780 vg_threads[tid].m_edx = 0; /* success */
1781}
1782
1783
sewardj604ec3c2002-04-18 22:38:41 +00001784/* -----------------------------------------------------------
1785 MUTEXes
1786 -------------------------------------------------------- */
1787
sewardj604ec3c2002-04-18 22:38:41 +00001788/* pthread_mutex_t is a struct with at 5 words:
sewardje663cb92002-04-12 10:26:32 +00001789 typedef struct
1790 {
1791 int __m_reserved; -- Reserved for future use
1792 int __m_count; -- Depth of recursive locking
1793 _pthread_descr __m_owner; -- Owner thread (if recursive or errcheck)
1794 int __m_kind; -- Mutex kind: fast, recursive or errcheck
1795 struct _pthread_fastlock __m_lock; -- Underlying fast lock
1796 } pthread_mutex_t;
sewardj604ec3c2002-04-18 22:38:41 +00001797
sewardj6072c362002-04-19 14:40:57 +00001798 #define PTHREAD_MUTEX_INITIALIZER \
1799 {0, 0, 0, PTHREAD_MUTEX_TIMED_NP, __LOCK_INITIALIZER}
1800 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
1801 {0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, __LOCK_INITIALIZER}
1802 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
1803 {0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, __LOCK_INITIALIZER}
1804 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
1805 {0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, __LOCK_INITIALIZER}
sewardj604ec3c2002-04-18 22:38:41 +00001806
sewardj6072c362002-04-19 14:40:57 +00001807 How we use it:
sewardj604ec3c2002-04-18 22:38:41 +00001808
sewardj6072c362002-04-19 14:40:57 +00001809 __m_kind never changes and indicates whether or not it is recursive.
1810
1811 __m_count indicates the lock count; if 0, the mutex is not owned by
1812 anybody.
1813
1814 __m_owner has a ThreadId value stuffed into it. We carefully arrange
1815 that ThreadId == 0 is invalid (VG_INVALID_THREADID), so that
1816 statically initialised mutexes correctly appear
1817 to belong to nobody.
1818
1819 In summary, a not-in-use mutex is distinguised by having __m_owner
1820 == 0 (VG_INVALID_THREADID) and __m_count == 0 too. If one of those
1821 conditions holds, the other should too.
1822
1823 There is no linked list of threads waiting for this mutex. Instead
1824 a thread in WaitMX state points at the mutex with its waited_on_mx
1825 field. This makes _unlock() inefficient, but simple to implement the
1826 right semantics viz-a-viz signals.
sewardje663cb92002-04-12 10:26:32 +00001827
sewardj604ec3c2002-04-18 22:38:41 +00001828 We don't have to deal with mutex initialisation; the client side
sewardj6072c362002-04-19 14:40:57 +00001829 deals with that for us.
1830*/
sewardje663cb92002-04-12 10:26:32 +00001831
sewardj3b5d8862002-04-20 13:53:23 +00001832/* Helper fns ... */
1833static
1834void release_one_thread_waiting_on_mutex ( pthread_mutex_t* mutex,
1835 Char* caller )
1836{
1837 Int i;
1838 Char msg_buf[100];
1839
1840 /* Find some arbitrary thread waiting on this mutex, and make it
1841 runnable. If none are waiting, mark the mutex as not held. */
1842 for (i = 1; i < VG_N_THREADS; i++) {
1843 if (vg_threads[i].status == VgTs_Empty)
1844 continue;
1845 if (vg_threads[i].status == VgTs_WaitMX
1846 && vg_threads[i].associated_mx == mutex)
1847 break;
1848 }
1849
1850 vg_assert(i <= VG_N_THREADS);
1851 if (i == VG_N_THREADS) {
1852 /* Nobody else is waiting on it. */
1853 mutex->__m_count = 0;
1854 mutex->__m_owner = VG_INVALID_THREADID;
1855 } else {
1856 /* Notionally transfer the hold to thread i, whose
1857 pthread_mutex_lock() call now returns with 0 (success). */
1858 /* The .count is already == 1. */
1859 vg_assert(vg_threads[i].associated_mx == mutex);
1860 mutex->__m_owner = (_pthread_descr)i;
1861 vg_threads[i].status = VgTs_Runnable;
1862 vg_threads[i].associated_mx = NULL;
sewardj5f07b662002-04-23 16:52:51 +00001863 /* m_edx already holds pth_mx_lock() success (0) */
sewardj3b5d8862002-04-20 13:53:23 +00001864
1865 if (VG_(clo_trace_pthread_level) >= 1) {
1866 VG_(sprintf)(msg_buf, "%s mx %p: RESUME",
1867 caller, mutex );
1868 print_pthread_event(i, msg_buf);
1869 }
1870 }
1871}
1872
sewardje663cb92002-04-12 10:26:32 +00001873
1874static
sewardj30671ff2002-04-21 00:13:57 +00001875void do_pthread_mutex_lock( ThreadId tid,
1876 Bool is_trylock,
sewardjd7fd4d22002-04-24 01:57:27 +00001877 void* /* pthread_mutex_t* */ mutexV )
sewardje663cb92002-04-12 10:26:32 +00001878{
sewardj30671ff2002-04-21 00:13:57 +00001879 Char msg_buf[100];
1880 Char* caller
1881 = is_trylock ? "pthread_mutex_lock "
1882 : "pthread_mutex_trylock";
sewardje663cb92002-04-12 10:26:32 +00001883
sewardjd7fd4d22002-04-24 01:57:27 +00001884 pthread_mutex_t* mutex = (pthread_mutex_t*)mutexV;
1885
sewardj604ec3c2002-04-18 22:38:41 +00001886 if (VG_(clo_trace_pthread_level) >= 2) {
sewardj30671ff2002-04-21 00:13:57 +00001887 VG_(sprintf)(msg_buf, "%s mx %p ...", caller, mutex );
sewardj604ec3c2002-04-18 22:38:41 +00001888 print_pthread_event(tid, msg_buf);
1889 }
1890
1891 /* Paranoia ... */
1892 vg_assert(is_valid_tid(tid)
1893 && vg_threads[tid].status == VgTs_Runnable);
sewardje663cb92002-04-12 10:26:32 +00001894
1895 /* POSIX doesn't mandate this, but for sanity ... */
1896 if (mutex == NULL) {
1897 vg_threads[tid].m_edx = EINVAL;
1898 return;
1899 }
1900
sewardj604ec3c2002-04-18 22:38:41 +00001901 /* More paranoia ... */
1902 switch (mutex->__m_kind) {
sewardj2a1dcce2002-04-22 12:45:25 +00001903# ifndef GLIBC_2_1
sewardj604ec3c2002-04-18 22:38:41 +00001904 case PTHREAD_MUTEX_TIMED_NP:
sewardj2a1dcce2002-04-22 12:45:25 +00001905 case PTHREAD_MUTEX_ADAPTIVE_NP:
1906# endif
sewardj604ec3c2002-04-18 22:38:41 +00001907 case PTHREAD_MUTEX_RECURSIVE_NP:
1908 case PTHREAD_MUTEX_ERRORCHECK_NP:
sewardj604ec3c2002-04-18 22:38:41 +00001909 if (mutex->__m_count >= 0) break;
1910 /* else fall thru */
1911 default:
1912 vg_threads[tid].m_edx = EINVAL;
1913 return;
sewardje663cb92002-04-12 10:26:32 +00001914 }
1915
sewardj604ec3c2002-04-18 22:38:41 +00001916 if (mutex->__m_count > 0) {
sewardje663cb92002-04-12 10:26:32 +00001917
sewardj604ec3c2002-04-18 22:38:41 +00001918 vg_assert(is_valid_tid((ThreadId)mutex->__m_owner));
sewardjf8f819e2002-04-17 23:21:37 +00001919
1920 /* Someone has it already. */
sewardj604ec3c2002-04-18 22:38:41 +00001921 if ((ThreadId)mutex->__m_owner == tid) {
sewardjf8f819e2002-04-17 23:21:37 +00001922 /* It's locked -- by me! */
sewardj604ec3c2002-04-18 22:38:41 +00001923 if (mutex->__m_kind == PTHREAD_MUTEX_RECURSIVE_NP) {
sewardjf8f819e2002-04-17 23:21:37 +00001924 /* return 0 (success). */
sewardj604ec3c2002-04-18 22:38:41 +00001925 mutex->__m_count++;
sewardjf8f819e2002-04-17 23:21:37 +00001926 vg_threads[tid].m_edx = 0;
sewardj853f55d2002-04-26 00:27:53 +00001927 if (0)
1928 VG_(printf)("!!!!!! tid %d, mx %p -> locked %d\n",
1929 tid, mutex, mutex->__m_count);
sewardjf8f819e2002-04-17 23:21:37 +00001930 return;
1931 } else {
sewardj30671ff2002-04-21 00:13:57 +00001932 if (is_trylock)
1933 vg_threads[tid].m_edx = EBUSY;
1934 else
1935 vg_threads[tid].m_edx = EDEADLK;
sewardjf8f819e2002-04-17 23:21:37 +00001936 return;
1937 }
1938 } else {
sewardj6072c362002-04-19 14:40:57 +00001939 /* Someone else has it; we have to wait. Mark ourselves
1940 thusly. */
sewardj05553872002-04-20 20:53:17 +00001941 /* GUARD: __m_count > 0 && __m_owner is valid */
sewardj30671ff2002-04-21 00:13:57 +00001942 if (is_trylock) {
1943 /* caller is polling; so return immediately. */
1944 vg_threads[tid].m_edx = EBUSY;
1945 } else {
1946 vg_threads[tid].status = VgTs_WaitMX;
1947 vg_threads[tid].associated_mx = mutex;
sewardj5f07b662002-04-23 16:52:51 +00001948 vg_threads[tid].m_edx = 0; /* pth_mx_lock success value */
sewardj30671ff2002-04-21 00:13:57 +00001949 if (VG_(clo_trace_pthread_level) >= 1) {
1950 VG_(sprintf)(msg_buf, "%s mx %p: BLOCK",
1951 caller, mutex );
1952 print_pthread_event(tid, msg_buf);
1953 }
1954 }
sewardje663cb92002-04-12 10:26:32 +00001955 return;
1956 }
sewardjf8f819e2002-04-17 23:21:37 +00001957
sewardje663cb92002-04-12 10:26:32 +00001958 } else {
sewardj6072c362002-04-19 14:40:57 +00001959 /* Nobody owns it. Sanity check ... */
1960 vg_assert(mutex->__m_owner == VG_INVALID_THREADID);
sewardjf8f819e2002-04-17 23:21:37 +00001961 /* We get it! [for the first time]. */
sewardj604ec3c2002-04-18 22:38:41 +00001962 mutex->__m_count = 1;
1963 mutex->__m_owner = (_pthread_descr)tid;
sewardj3b5d8862002-04-20 13:53:23 +00001964 vg_assert(vg_threads[tid].associated_mx == NULL);
sewardje663cb92002-04-12 10:26:32 +00001965 /* return 0 (success). */
1966 vg_threads[tid].m_edx = 0;
1967 }
sewardjf8f819e2002-04-17 23:21:37 +00001968
sewardje663cb92002-04-12 10:26:32 +00001969}
1970
1971
1972static
1973void do_pthread_mutex_unlock ( ThreadId tid,
sewardjd7fd4d22002-04-24 01:57:27 +00001974 void* /* pthread_mutex_t* */ mutexV )
sewardje663cb92002-04-12 10:26:32 +00001975{
sewardj3b5d8862002-04-20 13:53:23 +00001976 Char msg_buf[100];
sewardjd7fd4d22002-04-24 01:57:27 +00001977 pthread_mutex_t* mutex = (pthread_mutex_t*)mutexV;
sewardje663cb92002-04-12 10:26:32 +00001978
sewardj45b4b372002-04-16 22:50:32 +00001979 if (VG_(clo_trace_pthread_level) >= 2) {
sewardj3b5d8862002-04-20 13:53:23 +00001980 VG_(sprintf)(msg_buf, "pthread_mutex_unlock mx %p ...", mutex );
sewardj8937c812002-04-12 20:12:20 +00001981 print_pthread_event(tid, msg_buf);
1982 }
1983
sewardj604ec3c2002-04-18 22:38:41 +00001984 /* Paranoia ... */
1985 vg_assert(is_valid_tid(tid)
1986 && vg_threads[tid].status == VgTs_Runnable);
1987
1988 if (mutex == NULL) {
1989 vg_threads[tid].m_edx = EINVAL;
1990 return;
1991 }
1992
1993 /* More paranoia ... */
1994 switch (mutex->__m_kind) {
sewardj2a1dcce2002-04-22 12:45:25 +00001995# ifndef GLIBC_2_1
sewardj604ec3c2002-04-18 22:38:41 +00001996 case PTHREAD_MUTEX_TIMED_NP:
sewardj2a1dcce2002-04-22 12:45:25 +00001997 case PTHREAD_MUTEX_ADAPTIVE_NP:
1998# endif
sewardj604ec3c2002-04-18 22:38:41 +00001999 case PTHREAD_MUTEX_RECURSIVE_NP:
2000 case PTHREAD_MUTEX_ERRORCHECK_NP:
sewardj604ec3c2002-04-18 22:38:41 +00002001 if (mutex->__m_count >= 0) break;
2002 /* else fall thru */
2003 default:
2004 vg_threads[tid].m_edx = EINVAL;
2005 return;
2006 }
sewardje663cb92002-04-12 10:26:32 +00002007
2008 /* Barf if we don't currently hold the mutex. */
sewardj604ec3c2002-04-18 22:38:41 +00002009 if (mutex->__m_count == 0 /* nobody holds it */
2010 || (ThreadId)mutex->__m_owner != tid /* we don't hold it */) {
sewardje663cb92002-04-12 10:26:32 +00002011 vg_threads[tid].m_edx = EPERM;
2012 return;
2013 }
2014
sewardjf8f819e2002-04-17 23:21:37 +00002015 /* If it's a multiply-locked recursive mutex, just decrement the
2016 lock count and return. */
sewardj604ec3c2002-04-18 22:38:41 +00002017 if (mutex->__m_count > 1) {
2018 vg_assert(mutex->__m_kind == PTHREAD_MUTEX_RECURSIVE_NP);
2019 mutex->__m_count --;
sewardjf8f819e2002-04-17 23:21:37 +00002020 vg_threads[tid].m_edx = 0; /* success */
2021 return;
2022 }
2023
sewardj604ec3c2002-04-18 22:38:41 +00002024 /* Now we're sure it is locked exactly once, and by the thread who
sewardjf8f819e2002-04-17 23:21:37 +00002025 is now doing an unlock on it. */
sewardj604ec3c2002-04-18 22:38:41 +00002026 vg_assert(mutex->__m_count == 1);
sewardj6072c362002-04-19 14:40:57 +00002027 vg_assert((ThreadId)mutex->__m_owner == tid);
sewardjf8f819e2002-04-17 23:21:37 +00002028
sewardj3b5d8862002-04-20 13:53:23 +00002029 /* Release at max one thread waiting on this mutex. */
2030 release_one_thread_waiting_on_mutex ( mutex, "pthread_mutex_lock" );
sewardje663cb92002-04-12 10:26:32 +00002031
sewardj3b5d8862002-04-20 13:53:23 +00002032 /* Our (tid's) pth_unlock() returns with 0 (success). */
sewardje663cb92002-04-12 10:26:32 +00002033 vg_threads[tid].m_edx = 0; /* Success. */
2034}
2035
2036
sewardj6072c362002-04-19 14:40:57 +00002037/* -----------------------------------------------------------
2038 CONDITION VARIABLES
2039 -------------------------------------------------------- */
sewardje663cb92002-04-12 10:26:32 +00002040
sewardj6072c362002-04-19 14:40:57 +00002041/* The relevant native types are as follows:
2042 (copied from /usr/include/bits/pthreadtypes.h)
sewardj77e466c2002-04-14 02:29:29 +00002043
sewardj6072c362002-04-19 14:40:57 +00002044 -- Conditions (not abstract because of PTHREAD_COND_INITIALIZER
2045 typedef struct
2046 {
2047 struct _pthread_fastlock __c_lock; -- Protect against concurrent access
2048 _pthread_descr __c_waiting; -- Threads waiting on this condition
2049 } pthread_cond_t;
sewardj77e466c2002-04-14 02:29:29 +00002050
sewardj6072c362002-04-19 14:40:57 +00002051 -- Attribute for conditionally variables.
2052 typedef struct
2053 {
2054 int __dummy;
2055 } pthread_condattr_t;
sewardj77e466c2002-04-14 02:29:29 +00002056
sewardj6072c362002-04-19 14:40:57 +00002057 #define PTHREAD_COND_INITIALIZER {__LOCK_INITIALIZER, 0}
sewardj77e466c2002-04-14 02:29:29 +00002058
sewardj3b5d8862002-04-20 13:53:23 +00002059 We don't use any fields of pthread_cond_t for anything at all.
2060 Only the identity of the CVs is important.
sewardj6072c362002-04-19 14:40:57 +00002061
2062 Linux pthreads supports no attributes on condition variables, so we
sewardj3b5d8862002-04-20 13:53:23 +00002063 don't need to think too hard there. */
sewardj6072c362002-04-19 14:40:57 +00002064
sewardj77e466c2002-04-14 02:29:29 +00002065
sewardj5f07b662002-04-23 16:52:51 +00002066static
2067void do_pthread_cond_timedwait_TIMEOUT ( ThreadId tid )
2068{
2069 Char msg_buf[100];
2070 pthread_mutex_t* mx;
2071 pthread_cond_t* cv;
2072
2073 vg_assert(is_valid_tid(tid)
2074 && vg_threads[tid].status == VgTs_WaitCV
2075 && vg_threads[tid].awaken_at != 0xFFFFFFFF);
2076 mx = vg_threads[tid].associated_mx;
2077 vg_assert(mx != NULL);
2078 cv = vg_threads[tid].associated_cv;
2079 vg_assert(cv != NULL);
2080
2081 if (mx->__m_owner == VG_INVALID_THREADID) {
2082 /* Currently unheld; hand it out to thread tid. */
2083 vg_assert(mx->__m_count == 0);
2084 vg_threads[tid].status = VgTs_Runnable;
2085 vg_threads[tid].m_edx = ETIMEDOUT;
2086 /* pthread_cond_wait return value */
2087 vg_threads[tid].associated_cv = NULL;
2088 vg_threads[tid].associated_mx = NULL;
2089 mx->__m_owner = (_pthread_descr)tid;
2090 mx->__m_count = 1;
2091
2092 if (VG_(clo_trace_pthread_level) >= 1) {
2093 VG_(sprintf)(msg_buf, "pthread_cond_timedwai cv %p: TIMEOUT with mx %p",
2094 cv, mx );
2095 print_pthread_event(tid, msg_buf);
2096 }
2097 } else {
2098 /* Currently held. Make thread tid be blocked on it. */
2099 vg_assert(mx->__m_count > 0);
2100 vg_threads[tid].status = VgTs_WaitMX;
2101 vg_threads[tid].m_edx = ETIMEDOUT;
2102 /* pthread_cond_wait return value */
2103 vg_threads[tid].associated_cv = NULL;
2104 vg_threads[tid].associated_mx = mx;
2105 if (VG_(clo_trace_pthread_level) >= 1) {
2106 VG_(sprintf)(msg_buf,
2107 "pthread_cond_timedwai cv %p: TIMEOUT -> BLOCK for mx %p",
2108 cv, mx );
2109 print_pthread_event(tid, msg_buf);
2110 }
2111
2112 }
2113}
2114
2115
sewardj3b5d8862002-04-20 13:53:23 +00002116static
2117void release_N_threads_waiting_on_cond ( pthread_cond_t* cond,
2118 Int n_to_release,
2119 Char* caller )
2120{
2121 Int i;
2122 Char msg_buf[100];
2123 pthread_mutex_t* mx;
2124
2125 while (True) {
2126 if (n_to_release == 0)
2127 return;
2128
2129 /* Find a thread waiting on this CV. */
2130 for (i = 1; i < VG_N_THREADS; i++) {
2131 if (vg_threads[i].status == VgTs_Empty)
2132 continue;
2133 if (vg_threads[i].status == VgTs_WaitCV
2134 && vg_threads[i].associated_cv == cond)
2135 break;
2136 }
2137 vg_assert(i <= VG_N_THREADS);
2138
2139 if (i == VG_N_THREADS) {
2140 /* Nobody else is waiting on it. */
2141 return;
2142 }
2143
2144 mx = vg_threads[i].associated_mx;
2145 vg_assert(mx != NULL);
2146
2147 if (mx->__m_owner == VG_INVALID_THREADID) {
2148 /* Currently unheld; hand it out to thread i. */
2149 vg_assert(mx->__m_count == 0);
2150 vg_threads[i].status = VgTs_Runnable;
2151 vg_threads[i].associated_cv = NULL;
2152 vg_threads[i].associated_mx = NULL;
2153 mx->__m_owner = (_pthread_descr)i;
2154 mx->__m_count = 1;
sewardj5f07b662002-04-23 16:52:51 +00002155 /* .m_edx already holds pth_cond_wait success value (0) */
sewardj3b5d8862002-04-20 13:53:23 +00002156
2157 if (VG_(clo_trace_pthread_level) >= 1) {
2158 VG_(sprintf)(msg_buf, "%s cv %p: RESUME with mx %p",
2159 caller, cond, mx );
2160 print_pthread_event(i, msg_buf);
2161 }
2162
2163 } else {
2164 /* Currently held. Make thread i be blocked on it. */
sewardj5f07b662002-04-23 16:52:51 +00002165 vg_assert(mx->__m_count > 0);
sewardj3b5d8862002-04-20 13:53:23 +00002166 vg_threads[i].status = VgTs_WaitMX;
2167 vg_threads[i].associated_cv = NULL;
2168 vg_threads[i].associated_mx = mx;
sewardj5f07b662002-04-23 16:52:51 +00002169 vg_threads[i].m_edx = 0; /* pth_cond_wait success value */
sewardj3b5d8862002-04-20 13:53:23 +00002170
2171 if (VG_(clo_trace_pthread_level) >= 1) {
2172 VG_(sprintf)(msg_buf, "%s cv %p: BLOCK for mx %p",
2173 caller, cond, mx );
2174 print_pthread_event(i, msg_buf);
2175 }
2176
2177 }
2178
2179 n_to_release--;
2180 }
2181}
2182
2183
2184static
2185void do_pthread_cond_wait ( ThreadId tid,
2186 pthread_cond_t *cond,
sewardj5f07b662002-04-23 16:52:51 +00002187 pthread_mutex_t *mutex,
2188 UInt ms_end )
sewardj3b5d8862002-04-20 13:53:23 +00002189{
2190 Char msg_buf[100];
2191
sewardj5f07b662002-04-23 16:52:51 +00002192 /* If ms_end == 0xFFFFFFFF, wait forever (no timeout). Otherwise,
2193 ms_end is the ending millisecond. */
2194
sewardj3b5d8862002-04-20 13:53:23 +00002195 /* pre: mutex should be a valid mutex and owned by tid. */
2196 if (VG_(clo_trace_pthread_level) >= 2) {
sewardj5f07b662002-04-23 16:52:51 +00002197 VG_(sprintf)(msg_buf, "pthread_cond_wait cv %p, mx %p, end %d ...",
2198 cond, mutex, ms_end );
sewardj3b5d8862002-04-20 13:53:23 +00002199 print_pthread_event(tid, msg_buf);
2200 }
2201
2202 /* Paranoia ... */
2203 vg_assert(is_valid_tid(tid)
2204 && vg_threads[tid].status == VgTs_Runnable);
2205
2206 if (mutex == NULL || cond == NULL) {
2207 vg_threads[tid].m_edx = EINVAL;
2208 return;
2209 }
2210
2211 /* More paranoia ... */
2212 switch (mutex->__m_kind) {
sewardj2a1dcce2002-04-22 12:45:25 +00002213# ifndef GLIBC_2_1
sewardj3b5d8862002-04-20 13:53:23 +00002214 case PTHREAD_MUTEX_TIMED_NP:
sewardj2a1dcce2002-04-22 12:45:25 +00002215 case PTHREAD_MUTEX_ADAPTIVE_NP:
2216# endif
sewardj3b5d8862002-04-20 13:53:23 +00002217 case PTHREAD_MUTEX_RECURSIVE_NP:
2218 case PTHREAD_MUTEX_ERRORCHECK_NP:
sewardj3b5d8862002-04-20 13:53:23 +00002219 if (mutex->__m_count >= 0) break;
2220 /* else fall thru */
2221 default:
2222 vg_threads[tid].m_edx = EINVAL;
2223 return;
2224 }
2225
2226 /* Barf if we don't currently hold the mutex. */
2227 if (mutex->__m_count == 0 /* nobody holds it */
2228 || (ThreadId)mutex->__m_owner != tid /* we don't hold it */) {
2229 vg_threads[tid].m_edx = EINVAL;
2230 return;
2231 }
2232
2233 /* Queue ourselves on the condition. */
2234 vg_threads[tid].status = VgTs_WaitCV;
2235 vg_threads[tid].associated_cv = cond;
2236 vg_threads[tid].associated_mx = mutex;
sewardj5f07b662002-04-23 16:52:51 +00002237 vg_threads[tid].awaken_at = ms_end;
sewardj3b5d8862002-04-20 13:53:23 +00002238
2239 if (VG_(clo_trace_pthread_level) >= 1) {
2240 VG_(sprintf)(msg_buf,
2241 "pthread_cond_wait cv %p, mx %p: BLOCK",
2242 cond, mutex );
2243 print_pthread_event(tid, msg_buf);
2244 }
2245
2246 /* Release the mutex. */
2247 release_one_thread_waiting_on_mutex ( mutex, "pthread_cond_wait " );
2248}
2249
2250
2251static
2252void do_pthread_cond_signal_or_broadcast ( ThreadId tid,
2253 Bool broadcast,
2254 pthread_cond_t *cond )
2255{
2256 Char msg_buf[100];
2257 Char* caller
2258 = broadcast ? "pthread_cond_broadcast"
2259 : "pthread_cond_signal ";
2260
2261 if (VG_(clo_trace_pthread_level) >= 2) {
2262 VG_(sprintf)(msg_buf, "%s cv %p ...",
2263 caller, cond );
2264 print_pthread_event(tid, msg_buf);
2265 }
2266
2267 /* Paranoia ... */
2268 vg_assert(is_valid_tid(tid)
2269 && vg_threads[tid].status == VgTs_Runnable);
2270
2271 if (cond == NULL) {
2272 vg_threads[tid].m_edx = EINVAL;
2273 return;
2274 }
2275
2276 release_N_threads_waiting_on_cond (
2277 cond,
2278 broadcast ? VG_N_THREADS : 1,
2279 caller
2280 );
2281
2282 vg_threads[tid].m_edx = 0; /* success */
2283}
2284
sewardj77e466c2002-04-14 02:29:29 +00002285
sewardj5f07b662002-04-23 16:52:51 +00002286/* -----------------------------------------------------------
2287 THREAD SPECIFIC DATA
2288 -------------------------------------------------------- */
2289
2290static __inline__
2291Bool is_valid_key ( ThreadKey k )
2292{
2293 /* k unsigned; hence no < 0 check */
2294 if (k >= VG_N_THREAD_KEYS) return False;
2295 if (!vg_thread_keys[k].inuse) return False;
2296 return True;
2297}
2298
2299static
2300void do_pthread_key_create ( ThreadId tid,
2301 pthread_key_t* key,
2302 void (*destructor)(void*) )
2303{
2304 Int i;
2305 Char msg_buf[100];
2306
2307 if (VG_(clo_trace_pthread_level) >= 1) {
2308 VG_(sprintf)(msg_buf, "pthread_key_create *key %p, destr %p",
2309 key, destructor );
2310 print_pthread_event(tid, msg_buf);
2311 }
2312
2313 vg_assert(sizeof(pthread_key_t) == sizeof(ThreadKey));
2314 vg_assert(is_valid_tid(tid)
2315 && vg_threads[tid].status == VgTs_Runnable);
2316
2317 for (i = 0; i < VG_N_THREAD_KEYS; i++)
2318 if (!vg_thread_keys[i].inuse)
2319 break;
2320
2321 if (i == VG_N_THREAD_KEYS) {
2322 /* vg_threads[tid].m_edx = EAGAIN;
2323 return;
2324 */
2325 VG_(panic)("pthread_key_create: VG_N_THREAD_KEYS is too low;"
2326 " increase and recompile");
2327 }
2328
2329 vg_thread_keys[i].inuse = True;
2330 /* TODO: check key for addressibility */
2331 *key = i;
2332 vg_threads[tid].m_edx = 0;
2333}
2334
2335
2336static
2337void do_pthread_key_delete ( ThreadId tid, pthread_key_t key )
2338{
2339 Char msg_buf[100];
2340 if (VG_(clo_trace_pthread_level) >= 1) {
2341 VG_(sprintf)(msg_buf, "pthread_key_delete key %d",
2342 key );
2343 print_pthread_event(tid, msg_buf);
2344 }
2345
2346 vg_assert(is_valid_tid(tid)
2347 && vg_threads[tid].status == VgTs_Runnable);
2348
2349 if (!is_valid_key(key)) {
2350 vg_threads[tid].m_edx = EINVAL;
2351 return;
2352 }
2353
2354 vg_thread_keys[key].inuse = False;
2355
2356 /* Optional. We're not required to do this, although it shouldn't
2357 make any difference to programs which use the key/specifics
2358 functions correctly. */
sewardj3b13f0e2002-04-25 20:17:29 +00002359# if 1
sewardj5f07b662002-04-23 16:52:51 +00002360 for (tid = 1; tid < VG_N_THREADS; tid++) {
2361 if (vg_threads[tid].status != VgTs_Empty)
2362 vg_threads[tid].specifics[key] = NULL;
2363 }
sewardj3b13f0e2002-04-25 20:17:29 +00002364# endif
sewardj5f07b662002-04-23 16:52:51 +00002365}
2366
2367
2368static
2369void do_pthread_getspecific ( ThreadId tid, pthread_key_t key )
2370{
2371 Char msg_buf[100];
2372 if (VG_(clo_trace_pthread_level) >= 1) {
2373 VG_(sprintf)(msg_buf, "pthread_getspecific key %d",
2374 key );
2375 print_pthread_event(tid, msg_buf);
2376 }
2377
2378 vg_assert(is_valid_tid(tid)
2379 && vg_threads[tid].status == VgTs_Runnable);
2380
2381 if (!is_valid_key(key)) {
2382 vg_threads[tid].m_edx = (UInt)NULL;
2383 return;
2384 }
2385
2386 vg_threads[tid].m_edx = (UInt)vg_threads[tid].specifics[key];
2387}
2388
2389
2390static
2391void do_pthread_setspecific ( ThreadId tid,
2392 pthread_key_t key,
2393 void *pointer )
2394{
2395 Char msg_buf[100];
2396 if (VG_(clo_trace_pthread_level) >= 1) {
2397 VG_(sprintf)(msg_buf, "pthread_setspecific key %d, ptr %p",
2398 key, pointer );
2399 print_pthread_event(tid, msg_buf);
2400 }
2401
2402 vg_assert(is_valid_tid(tid)
2403 && vg_threads[tid].status == VgTs_Runnable);
2404
2405 if (!is_valid_key(key)) {
2406 vg_threads[tid].m_edx = EINVAL;
2407 return;
2408 }
2409
2410 vg_threads[tid].specifics[key] = pointer;
2411 vg_threads[tid].m_edx = 0;
2412}
2413
2414
sewardje663cb92002-04-12 10:26:32 +00002415/* ---------------------------------------------------------------------
2416 Handle non-trivial client requests.
2417 ------------------------------------------------------------------ */
2418
2419static
2420void do_nontrivial_clientreq ( ThreadId tid )
2421{
2422 UInt* arg = (UInt*)(vg_threads[tid].m_eax);
2423 UInt req_no = arg[0];
2424 switch (req_no) {
2425
2426 case VG_USERREQ__PTHREAD_CREATE:
2427 do_pthread_create( tid,
2428 (pthread_t*)arg[1],
2429 (pthread_attr_t*)arg[2],
2430 (void*(*)(void*))arg[3],
2431 (void*)arg[4] );
2432 break;
2433
sewardjbc5b99f2002-04-13 00:08:51 +00002434 case VG_USERREQ__PTHREAD_RETURNS:
2435 handle_pthread_return( tid, (void*)arg[1] );
sewardje663cb92002-04-12 10:26:32 +00002436 break;
2437
2438 case VG_USERREQ__PTHREAD_JOIN:
2439 do_pthread_join( tid, arg[1], (void**)(arg[2]) );
2440 break;
2441
sewardje663cb92002-04-12 10:26:32 +00002442 case VG_USERREQ__PTHREAD_CANCEL:
2443 do_pthread_cancel( tid, (pthread_t)(arg[1]) );
2444 break;
2445
sewardj3b5d8862002-04-20 13:53:23 +00002446 case VG_USERREQ__PTHREAD_EXIT:
2447 do_pthread_exit( tid, (void*)(arg[1]) );
2448 break;
2449
2450 case VG_USERREQ__PTHREAD_COND_WAIT:
2451 do_pthread_cond_wait( tid,
2452 (pthread_cond_t *)(arg[1]),
sewardj5f07b662002-04-23 16:52:51 +00002453 (pthread_mutex_t *)(arg[2]),
2454 0xFFFFFFFF /* no timeout */ );
2455 break;
2456
2457 case VG_USERREQ__PTHREAD_COND_TIMEDWAIT:
2458 do_pthread_cond_wait( tid,
2459 (pthread_cond_t *)(arg[1]),
2460 (pthread_mutex_t *)(arg[2]),
2461 arg[3] /* timeout millisecond point */ );
sewardj3b5d8862002-04-20 13:53:23 +00002462 break;
2463
2464 case VG_USERREQ__PTHREAD_COND_SIGNAL:
2465 do_pthread_cond_signal_or_broadcast(
2466 tid,
2467 False, /* signal, not broadcast */
2468 (pthread_cond_t *)(arg[1]) );
2469 break;
2470
2471 case VG_USERREQ__PTHREAD_COND_BROADCAST:
2472 do_pthread_cond_signal_or_broadcast(
2473 tid,
2474 True, /* broadcast, not signal */
2475 (pthread_cond_t *)(arg[1]) );
2476 break;
2477
sewardj5f07b662002-04-23 16:52:51 +00002478 case VG_USERREQ__PTHREAD_KEY_CREATE:
2479 do_pthread_key_create ( tid,
2480 (pthread_key_t*)(arg[1]),
2481 (void(*)(void*))(arg[2]) );
2482 break;
2483
2484 case VG_USERREQ__PTHREAD_KEY_DELETE:
2485 do_pthread_key_delete ( tid,
2486 (pthread_key_t)(arg[1]) );
2487 break;
2488
sewardj5f07b662002-04-23 16:52:51 +00002489 case VG_USERREQ__PTHREAD_SETSPECIFIC:
2490 do_pthread_setspecific ( tid,
2491 (pthread_key_t)(arg[1]),
2492 (void*)(arg[2]) );
2493 break;
2494
sewardje663cb92002-04-12 10:26:32 +00002495 case VG_USERREQ__MAKE_NOACCESS:
2496 case VG_USERREQ__MAKE_WRITABLE:
2497 case VG_USERREQ__MAKE_READABLE:
2498 case VG_USERREQ__DISCARD:
2499 case VG_USERREQ__CHECK_WRITABLE:
2500 case VG_USERREQ__CHECK_READABLE:
2501 case VG_USERREQ__MAKE_NOACCESS_STACK:
2502 case VG_USERREQ__RUNNING_ON_VALGRIND:
2503 case VG_USERREQ__DO_LEAK_CHECK:
sewardj8c824512002-04-14 04:16:48 +00002504 vg_threads[tid].m_edx
2505 = VG_(handle_client_request) ( &vg_threads[tid], arg );
sewardje663cb92002-04-12 10:26:32 +00002506 break;
2507
sewardj77e466c2002-04-14 02:29:29 +00002508 case VG_USERREQ__SIGNAL_RETURNS:
2509 handle_signal_return(tid);
2510 break;
sewardj54cacf02002-04-12 23:24:59 +00002511
sewardje663cb92002-04-12 10:26:32 +00002512 default:
2513 VG_(printf)("panic'd on private request = 0x%x\n", arg[0] );
2514 VG_(panic)("handle_private_client_pthread_request: "
2515 "unknown request");
2516 /*NOTREACHED*/
2517 break;
2518 }
2519}
2520
2521
sewardj6072c362002-04-19 14:40:57 +00002522/* ---------------------------------------------------------------------
2523 Sanity checking.
2524 ------------------------------------------------------------------ */
2525
2526/* Internal consistency checks on the sched/pthread structures. */
2527static
2528void scheduler_sanity ( void )
2529{
sewardj3b5d8862002-04-20 13:53:23 +00002530 pthread_mutex_t* mx;
2531 pthread_cond_t* cv;
sewardj6072c362002-04-19 14:40:57 +00002532 Int i;
sewardj5f07b662002-04-23 16:52:51 +00002533
sewardj6072c362002-04-19 14:40:57 +00002534 /* VG_(printf)("scheduler_sanity\n"); */
2535 for (i = 1; i < VG_N_THREADS; i++) {
sewardj3b5d8862002-04-20 13:53:23 +00002536 mx = vg_threads[i].associated_mx;
2537 cv = vg_threads[i].associated_cv;
sewardj6072c362002-04-19 14:40:57 +00002538 if (vg_threads[i].status == VgTs_WaitMX) {
sewardj05553872002-04-20 20:53:17 +00002539 /* If we're waiting on a MX: (1) the mx is not null, (2, 3)
2540 it's actually held by someone, since otherwise this thread
2541 is deadlocked, (4) the mutex's owner is not us, since
2542 otherwise this thread is also deadlocked. The logic in
2543 do_pthread_mutex_lock rejects attempts by a thread to lock
2544 a (non-recursive) mutex which it already owns.
2545
2546 (2) has been seen to fail sometimes. I don't know why.
2547 Possibly to do with signals. */
sewardj3b5d8862002-04-20 13:53:23 +00002548 vg_assert(cv == NULL);
sewardj05553872002-04-20 20:53:17 +00002549 /* 1 */ vg_assert(mx != NULL);
2550 /* 2 */ vg_assert(mx->__m_count > 0);
2551 /* 3 */ vg_assert(is_valid_tid((ThreadId)mx->__m_owner));
2552 /* 4 */ vg_assert(i != (ThreadId)mx->__m_owner);
sewardj3b5d8862002-04-20 13:53:23 +00002553 } else
2554 if (vg_threads[i].status == VgTs_WaitCV) {
2555 vg_assert(cv != NULL);
2556 vg_assert(mx != NULL);
sewardj6072c362002-04-19 14:40:57 +00002557 } else {
sewardj05553872002-04-20 20:53:17 +00002558 /* Unfortunately these don't hold true when a sighandler is
2559 running. To be fixed. */
2560 /* vg_assert(cv == NULL); */
2561 /* vg_assert(mx == NULL); */
sewardj6072c362002-04-19 14:40:57 +00002562 }
2563 }
sewardj5f07b662002-04-23 16:52:51 +00002564
2565 for (i = 0; i < VG_N_THREAD_KEYS; i++) {
2566 if (!vg_thread_keys[i].inuse)
2567 vg_assert(vg_thread_keys[i].destructor == NULL);
2568 }
sewardj6072c362002-04-19 14:40:57 +00002569}
2570
2571
sewardje663cb92002-04-12 10:26:32 +00002572/*--------------------------------------------------------------------*/
2573/*--- end vg_scheduler.c ---*/
2574/*--------------------------------------------------------------------*/