njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 1 | |
| 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 | |
sewardj | b3a1e4b | 2015-08-21 11:32:26 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2015 Julian Seward |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 11 | 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 | |
florian | 535fb1b | 2013-09-15 13:54:34 +0000 | [diff] [blame] | 34 | #include "pub_core_basics.h" // VG_ macro |
| 35 | #include "pub_core_threadstate.h" // VgSchedReturnCode |
| 36 | |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 37 | //-------------------------------------------------------------------- |
| 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 | |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 43 | /* Allocate a new ThreadState */ |
| 44 | extern ThreadId VG_(alloc_ThreadState)(void); |
| 45 | |
| 46 | /* A thread exits. tid must currently be running. */ |
| 47 | extern void VG_(exit_thread)(ThreadId tid); |
| 48 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 49 | /* 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. */ |
| 52 | extern void VG_(get_thread_out_of_syscall)(ThreadId tid); |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 53 | |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 54 | /* Nuke all threads except tid. */ |
| 55 | extern void VG_(nuke_all_threads_except) ( ThreadId me, |
| 56 | VgSchedReturnCode reason ); |
| 57 | |
| 58 | /* Make a thread the running thread. The thread must previously been |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 59 | sleeping, and not holding the CPU lock. This will set the |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 60 | thread state to VgTs_Runnable, and the thread will attempt to take |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 61 | the CPU lock. By the time it returns, tid will be the running |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 62 | thread. */ |
florian | dbb3584 | 2012-10-27 18:39:11 +0000 | [diff] [blame] | 63 | extern void VG_(acquire_BigLock) ( ThreadId tid, const HChar* who ); |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 64 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 65 | /* Simple version, which simply acquires the lock, but does not mess |
| 66 | with the guest state in the same way as the non _LL version |
| 67 | does. */ |
florian | dbb3584 | 2012-10-27 18:39:11 +0000 | [diff] [blame] | 68 | extern void VG_(acquire_BigLock_LL) ( const HChar* who ); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 69 | |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 70 | /* Set a thread into a sleeping state. Before the call, the thread |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 71 | must be runnable, and holding the CPU lock. When this call |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 72 | returns, the thread will be set to the specified sleeping state, |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 73 | and will not be holding the CPU lock. Note that another |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 74 | thread could be running by the time this call returns, so the |
| 75 | caller must be careful not to touch any shared state. It is also |
| 76 | the caller's responsibility to actually block until the thread is |
| 77 | ready to run again. */ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 78 | extern void VG_(release_BigLock) ( ThreadId tid, |
florian | dbb3584 | 2012-10-27 18:39:11 +0000 | [diff] [blame] | 79 | ThreadStatus state, const HChar* who ); |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 80 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 81 | /* Matching function to acquire_BigLock_LL. */ |
florian | dbb3584 | 2012-10-27 18:39:11 +0000 | [diff] [blame] | 82 | extern void VG_(release_BigLock_LL) ( const HChar* who ); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 83 | |
bart | 9a2b80d | 2012-03-25 17:51:59 +0000 | [diff] [blame] | 84 | /* Whether the specified thread owns the big lock. */ |
| 85 | extern Bool VG_(owns_BigLock_LL) ( ThreadId tid ); |
| 86 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 87 | /* Yield the CPU for a while. Drops/acquires the lock using the |
| 88 | normal (non _LL) functions. */ |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 89 | extern void VG_(vg_yield)(void); |
| 90 | |
| 91 | // The scheduler. |
| 92 | extern VgSchedReturnCode VG_(scheduler) ( ThreadId tid ); |
| 93 | |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 94 | // Initialise, phase 1. Zero out VG_(threads), decide on the root |
| 95 | // ThreadId and initialise the bigLock. |
| 96 | extern ThreadId VG_(scheduler_init_phase1) ( void ); |
| 97 | |
| 98 | // Initialise, phase 2. Is passed the extent of the root thread's |
philippe | 38a74d2 | 2014-08-29 22:53:19 +0000 | [diff] [blame] | 99 | // client stack end (highest addressable byte) and the root ThreadId |
| 100 | // decided on by phase 1. |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 101 | extern void VG_(scheduler_init_phase2) ( ThreadId main_tid, |
| 102 | Addr clstack_end, |
| 103 | SizeT clstack_size ); |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 104 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 105 | // Allows to disable the polling done to detect vgdb input |
| 106 | // or to force a poll at next scheduler call. |
| 107 | extern void VG_(disable_vgdb_poll) (void ); |
| 108 | extern void VG_(force_vgdb_poll) ( void ); |
| 109 | |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 110 | /* Stats ... */ |
| 111 | extern void VG_(print_scheduler_stats) ( void ); |
| 112 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 113 | /* If False, a fault is Valgrind-internal (ie, a bug) */ |
| 114 | extern Bool VG_(in_generated_code); |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 115 | |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 116 | /* Sanity checks which may be done at any time. The scheduler decides when. */ |
| 117 | extern void VG_(sanity_check_general) ( Bool force_expensive ); |
| 118 | |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 119 | #endif // __PUB_CORE_SCHEDULER_H |
| 120 | |
| 121 | /*--------------------------------------------------------------------*/ |
| 122 | /*--- end ---*/ |
| 123 | /*--------------------------------------------------------------------*/ |