blob: aef7ad8840369972140d5fcf777b871dfca9b3bb [file] [log] [blame]
njn278b3d62005-05-30 23:20:51 +00001
2/*--------------------------------------------------------------------*/
3/*--- The scheduler. pub_core_scheduler.h ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
Elliott Hughesed398002017-06-21 14:41:24 -070010 Copyright (C) 2000-2017 Julian Seward
njn278b3d62005-05-30 23:20:51 +000011 jseward@acm.org
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
31#ifndef __PUB_CORE_SCHEDULER_H
32#define __PUB_CORE_SCHEDULER_H
33
florian535fb1b2013-09-15 13:54:34 +000034#include "pub_core_basics.h" // VG_ macro
35#include "pub_core_threadstate.h" // VgSchedReturnCode
36
njn278b3d62005-05-30 23:20:51 +000037//--------------------------------------------------------------------
38// PURPOSE: This module is the scheduler, which is the main loop
39// controlling the running of all the program's threads.
40// It's at the centre of everything.
41//--------------------------------------------------------------------
42
njn278b3d62005-05-30 23:20:51 +000043/* Allocate a new ThreadState */
44extern ThreadId VG_(alloc_ThreadState)(void);
45
46/* A thread exits. tid must currently be running. */
47extern void VG_(exit_thread)(ThreadId tid);
48
sewardjf54342a2006-10-17 01:51:24 +000049/* If 'tid' is blocked in a syscall, send it SIGVGKILL so as to get it
50 out of the syscall and onto doing the next thing, whatever that is.
51 If it isn't blocked in a syscall, has no effect on the thread. */
52extern void VG_(get_thread_out_of_syscall)(ThreadId tid);
njn278b3d62005-05-30 23:20:51 +000053
Elliott Hughesa0664b92017-04-18 17:46:52 -070054/* This causes all threads except tid to forceably exit. They aren't actually
55 dead by the time this returns; you need to call
56 VG_(reap_threads)() to wait for them. */
njn278b3d62005-05-30 23:20:51 +000057extern void VG_(nuke_all_threads_except) ( ThreadId me,
58 VgSchedReturnCode reason );
59
60/* Make a thread the running thread. The thread must previously been
sewardjf54342a2006-10-17 01:51:24 +000061 sleeping, and not holding the CPU lock. This will set the
njn278b3d62005-05-30 23:20:51 +000062 thread state to VgTs_Runnable, and the thread will attempt to take
sewardjf54342a2006-10-17 01:51:24 +000063 the CPU lock. By the time it returns, tid will be the running
njn278b3d62005-05-30 23:20:51 +000064 thread. */
floriandbb35842012-10-27 18:39:11 +000065extern void VG_(acquire_BigLock) ( ThreadId tid, const HChar* who );
njn278b3d62005-05-30 23:20:51 +000066
njnf76d27a2009-05-28 01:53:07 +000067/* Simple version, which simply acquires the lock, but does not mess
68 with the guest state in the same way as the non _LL version
69 does. */
floriandbb35842012-10-27 18:39:11 +000070extern void VG_(acquire_BigLock_LL) ( const HChar* who );
njnf76d27a2009-05-28 01:53:07 +000071
njn278b3d62005-05-30 23:20:51 +000072/* Set a thread into a sleeping state. Before the call, the thread
sewardjf54342a2006-10-17 01:51:24 +000073 must be runnable, and holding the CPU lock. When this call
njn278b3d62005-05-30 23:20:51 +000074 returns, the thread will be set to the specified sleeping state,
sewardjf54342a2006-10-17 01:51:24 +000075 and will not be holding the CPU lock. Note that another
njn278b3d62005-05-30 23:20:51 +000076 thread could be running by the time this call returns, so the
77 caller must be careful not to touch any shared state. It is also
78 the caller's responsibility to actually block until the thread is
79 ready to run again. */
njnf76d27a2009-05-28 01:53:07 +000080extern void VG_(release_BigLock) ( ThreadId tid,
floriandbb35842012-10-27 18:39:11 +000081 ThreadStatus state, const HChar* who );
njn278b3d62005-05-30 23:20:51 +000082
njnf76d27a2009-05-28 01:53:07 +000083/* Matching function to acquire_BigLock_LL. */
floriandbb35842012-10-27 18:39:11 +000084extern void VG_(release_BigLock_LL) ( const HChar* who );
njnf76d27a2009-05-28 01:53:07 +000085
bart9a2b80d2012-03-25 17:51:59 +000086/* Whether the specified thread owns the big lock. */
87extern Bool VG_(owns_BigLock_LL) ( ThreadId tid );
88
njnf76d27a2009-05-28 01:53:07 +000089/* Yield the CPU for a while. Drops/acquires the lock using the
90 normal (non _LL) functions. */
njn278b3d62005-05-30 23:20:51 +000091extern void VG_(vg_yield)(void);
92
93// The scheduler.
94extern VgSchedReturnCode VG_(scheduler) ( ThreadId tid );
95
sewardjde764e82007-11-09 23:13:22 +000096// Initialise, phase 1. Zero out VG_(threads), decide on the root
97// ThreadId and initialise the bigLock.
98extern ThreadId VG_(scheduler_init_phase1) ( void );
99
100// Initialise, phase 2. Is passed the extent of the root thread's
philippe38a74d22014-08-29 22:53:19 +0000101// client stack end (highest addressable byte) and the root ThreadId
102// decided on by phase 1.
sewardjde764e82007-11-09 23:13:22 +0000103extern void VG_(scheduler_init_phase2) ( ThreadId main_tid,
104 Addr clstack_end,
105 SizeT clstack_size );
njn278b3d62005-05-30 23:20:51 +0000106
sewardj3b290482011-05-06 21:02:55 +0000107// Allows to disable the polling done to detect vgdb input
108// or to force a poll at next scheduler call.
109extern void VG_(disable_vgdb_poll) (void );
110extern void VG_(force_vgdb_poll) ( void );
111
njn04e16982005-05-31 00:23:43 +0000112/* Stats ... */
113extern void VG_(print_scheduler_stats) ( void );
114
sewardj0ec07f32006-01-12 12:32:32 +0000115/* If False, a fault is Valgrind-internal (ie, a bug) */
116extern Bool VG_(in_generated_code);
njn278b3d62005-05-30 23:20:51 +0000117
njn6676d5b2005-06-19 18:49:19 +0000118/* Sanity checks which may be done at any time. The scheduler decides when. */
119extern void VG_(sanity_check_general) ( Bool force_expensive );
120
njn278b3d62005-05-30 23:20:51 +0000121#endif // __PUB_CORE_SCHEDULER_H
122
123/*--------------------------------------------------------------------*/
124/*--- end ---*/
125/*--------------------------------------------------------------------*/