author | urbanc |
Mon, 27 Feb 2012 18:53:53 +0000 | |
changeset 335 | 7fe2a20017c0 |
parent 333 | 813e7257c7c3 |
child 336 | f9e0d3274c14 |
permissions | -rwxr-xr-x |
262 | 1 |
(*<*) |
2 |
theory Paper |
|
301 | 3 |
imports "../CpsG" "../ExtGG" "~~/src/HOL/Library/LaTeXsugar" |
262 | 4 |
begin |
335 | 5 |
|
6 |
(* |
|
7 |
find_unused_assms CpsG |
|
8 |
find_unused_assms ExtGG |
|
9 |
find_unused_assms Moment |
|
10 |
find_unused_assms Precedence_ord |
|
11 |
find_unused_assms PrioG |
|
12 |
find_unused_assms PrioGDef |
|
13 |
*) |
|
266 | 14 |
ML {* |
273 | 15 |
open Printer; |
272 | 16 |
show_question_marks_default := false; |
266 | 17 |
*} |
284 | 18 |
|
19 |
notation (latex output) |
|
20 |
Cons ("_::_" [78,77] 73) and |
|
21 |
vt ("valid'_state") and |
|
22 |
runing ("running") and |
|
286 | 23 |
birthtime ("last'_set") and |
284 | 24 |
If ("(\<^raw:\textrm{>if\<^raw:}> (_)/ \<^raw:\textrm{>then\<^raw:}> (_)/ \<^raw:\textrm{>else\<^raw:}> (_))" 10) and |
286 | 25 |
Prc ("'(_, _')") and |
287 | 26 |
holding ("holds") and |
27 |
waiting ("waits") and |
|
290 | 28 |
Th ("T") and |
29 |
Cs ("C") and |
|
287 | 30 |
readys ("ready") and |
290 | 31 |
depend ("RAG") and |
32 |
preced ("prec") and |
|
33 |
cpreced ("cprec") and |
|
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
34 |
dependents ("dependants") and |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
35 |
cp ("cprec") and |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
36 |
holdents ("resources") and |
299 | 37 |
original_priority ("priority") and |
284 | 38 |
DUMMY ("\<^raw:\mbox{$\_\!\_$}>") |
325 | 39 |
|
40 |
abbreviation |
|
41 |
"detached s th \<equiv> cntP s th = cntV s th" |
|
262 | 42 |
(*>*) |
43 |
||
44 |
section {* Introduction *} |
|
45 |
||
46 |
text {* |
|
284 | 47 |
Many real-time systems need to support threads involving priorities and |
267 | 48 |
locking of resources. Locking of resources ensures mutual exclusion |
275 | 49 |
when accessing shared data or devices that cannot be |
284 | 50 |
preempted. Priorities allow scheduling of threads that need to |
275 | 51 |
finish their work within deadlines. Unfortunately, both features |
52 |
can interact in subtle ways leading to a problem, called |
|
284 | 53 |
\emph{Priority Inversion}. Suppose three threads having priorities |
54 |
$H$(igh), $M$(edium) and $L$(ow). We would expect that the thread |
|
55 |
$H$ blocks any other thread with lower priority and itself cannot |
|
56 |
be blocked by any thread with lower priority. Alas, in a naive |
|
275 | 57 |
implementation of resource looking and priorities this property can |
58 |
be violated. Even worse, $H$ can be delayed indefinitely by |
|
284 | 59 |
threads with lower priorities. For this let $L$ be in the |
275 | 60 |
possession of a lock for a resource that also $H$ needs. $H$ must |
61 |
therefore wait for $L$ to exit the critical section and release this |
|
62 |
lock. The problem is that $L$ might in turn be blocked by any |
|
284 | 63 |
thread with priority $M$, and so $H$ sits there potentially waiting |
64 |
indefinitely. Since $H$ is blocked by threads with lower |
|
275 | 65 |
priorities, the problem is called Priority Inversion. It was first |
277 | 66 |
described in \cite{Lampson80} in the context of the |
275 | 67 |
Mesa programming language designed for concurrent programming. |
265 | 68 |
|
273 | 69 |
If the problem of Priority Inversion is ignored, real-time systems |
267 | 70 |
can become unpredictable and resulting bugs can be hard to diagnose. |
71 |
The classic example where this happened is the software that |
|
284 | 72 |
controlled the Mars Pathfinder mission in 1997 \cite{Reeves98}. |
73 |
Once the spacecraft landed, the software shut down at irregular |
|
74 |
intervals leading to loss of project time as normal operation of the |
|
75 |
craft could only resume the next day (the mission and data already |
|
76 |
collected were fortunately not lost, because of a clever system |
|
77 |
design). The reason for the shutdowns was that the scheduling |
|
78 |
software fell victim of Priority Inversion: a low priority thread |
|
79 |
locking a resource prevented a high priority thread from running in |
|
80 |
time leading to a system reset. Once the problem was found, it was |
|
81 |
rectified by enabling the \emph{Priority Inheritance Protocol} (PIP) |
|
82 |
\cite{Sha90}\footnote{Sha et al.~call it the \emph{Basic Priority |
|
286 | 83 |
Inheritance Protocol} \cite{Sha90} and others sometimes also call it |
84 |
\emph{Priority Boosting}.} in the scheduling software. |
|
262 | 85 |
|
284 | 86 |
The idea behind PIP is to let the thread $L$ temporarily inherit |
286 | 87 |
the high priority from $H$ until $L$ leaves the critical section |
284 | 88 |
unlocking the resource. This solves the problem of $H$ having to |
89 |
wait indefinitely, because $L$ cannot be blocked by threads having |
|
90 |
priority $M$. While a few other solutions exist for the Priority |
|
91 |
Inversion problem, PIP is one that is widely deployed and |
|
92 |
implemented. This includes VxWorks (a proprietary real-time OS used |
|
93 |
in the Mars Pathfinder mission, in Boeing's 787 Dreamliner, Honda's |
|
94 |
ASIMO robot, etc.), but also the POSIX 1003.1c Standard realised for |
|
95 |
example in libraries for FreeBSD, Solaris and Linux. |
|
274 | 96 |
|
284 | 97 |
One advantage of PIP is that increasing the priority of a thread |
275 | 98 |
can be dynamically calculated by the scheduler. This is in contrast |
277 | 99 |
to, for example, \emph{Priority Ceiling} \cite{Sha90}, another |
100 |
solution to the Priority Inversion problem, which requires static |
|
284 | 101 |
analysis of the program in order to prevent Priority |
102 |
Inversion. However, there has also been strong criticism against |
|
103 |
PIP. For instance, PIP cannot prevent deadlocks when lock |
|
104 |
dependencies are circular, and also blocking times can be |
|
105 |
substantial (more than just the duration of a critical section). |
|
106 |
Though, most criticism against PIP centres around unreliable |
|
107 |
implementations and PIP being too complicated and too inefficient. |
|
108 |
For example, Yodaiken writes in \cite{Yodaiken02}: |
|
274 | 109 |
|
110 |
\begin{quote} |
|
111 |
\it{}``Priority inheritance is neither efficient nor reliable. Implementations |
|
112 |
are either incomplete (and unreliable) or surprisingly complex and intrusive.'' |
|
113 |
\end{quote} |
|
273 | 114 |
|
274 | 115 |
\noindent |
275 | 116 |
He suggests to avoid PIP altogether by not allowing critical |
286 | 117 |
sections to be preempted. Unfortunately, this solution does not |
304 | 118 |
help in real-time systems with hard deadlines for high-priority |
119 |
threads. |
|
278 | 120 |
|
286 | 121 |
In our opinion, there is clearly a need for investigating correct |
278 | 122 |
algorithms for PIP. A few specifications for PIP exist (in English) |
123 |
and also a few high-level descriptions of implementations (e.g.~in |
|
124 |
the textbook \cite[Section 5.6.5]{Vahalia96}), but they help little |
|
332 | 125 |
with actual implementations. That this is a problem in practice is |
283 | 126 |
proved by an email from Baker, who wrote on 13 July 2009 on the Linux |
278 | 127 |
Kernel mailing list: |
274 | 128 |
|
129 |
\begin{quote} |
|
275 | 130 |
\it{}``I observed in the kernel code (to my disgust), the Linux PIP |
131 |
implementation is a nightmare: extremely heavy weight, involving |
|
132 |
maintenance of a full wait-for graph, and requiring updates for a |
|
133 |
range of events, including priority changes and interruptions of |
|
134 |
wait operations.'' |
|
274 | 135 |
\end{quote} |
136 |
||
137 |
\noindent |
|
277 | 138 |
The criticism by Yodaiken, Baker and others suggests to us to look |
139 |
again at PIP from a more abstract level (but still concrete enough |
|
286 | 140 |
to inform an implementation), and makes PIP an ideal candidate for a |
277 | 141 |
formal verification. One reason, of course, is that the original |
284 | 142 |
presentation of PIP~\cite{Sha90}, despite being informally |
283 | 143 |
``proved'' correct, is actually \emph{flawed}. |
144 |
||
145 |
Yodaiken \cite{Yodaiken02} points to a subtlety that had been |
|
146 |
overlooked in the informal proof by Sha et al. They specify in |
|
284 | 147 |
\cite{Sha90} that after the thread (whose priority has been raised) |
283 | 148 |
completes its critical section and releases the lock, it ``returns |
149 |
to its original priority level.'' This leads them to believe that an |
|
284 | 150 |
implementation of PIP is ``rather straightforward''~\cite{Sha90}. |
151 |
Unfortunately, as Yodaiken points out, this behaviour is too |
|
152 |
simplistic. Consider the case where the low priority thread $L$ |
|
153 |
locks \emph{two} resources, and two high-priority threads $H$ and |
|
300 | 154 |
$H'$ each wait for one of them. If $L$ releases one resource |
283 | 155 |
so that $H$, say, can proceed, then we still have Priority Inversion |
156 |
with $H'$ (which waits for the other resource). The correct |
|
157 |
behaviour for $L$ is to revert to the highest remaining priority of |
|
284 | 158 |
the threads that it blocks. The advantage of formalising the |
159 |
correctness of a high-level specification of PIP in a theorem prover |
|
160 |
is that such issues clearly show up and cannot be overlooked as in |
|
161 |
informal reasoning (since we have to analyse all possible behaviours |
|
300 | 162 |
of threads, i.e.~\emph{traces}, that could possibly happen).\medskip |
274 | 163 |
|
300 | 164 |
\noindent |
301 | 165 |
{\bf Contributions:} There have been earlier formal investigations |
304 | 166 |
into PIP \cite{Faria08,Jahier09,Wellings07}, but they employ model |
167 |
checking techniques. This paper presents a formalised and |
|
168 |
mechanically checked proof for the correctness of PIP (to our |
|
305 | 169 |
knowledge the first one; the earlier informal proof by Sha et |
304 | 170 |
al.~\cite{Sha90} is flawed). In contrast to model checking, our |
171 |
formalisation provides insight into why PIP is correct and allows us |
|
310 | 172 |
to prove stronger properties that, as we will show, can inform an |
314 | 173 |
efficient implementation. For example, we found by ``playing'' with the formalisation |
304 | 174 |
that the choice of the next thread to take over a lock when a |
305 | 175 |
resource is released is irrelevant for PIP being correct. Something |
176 |
which has not been mentioned in the relevant literature. |
|
280 | 177 |
*} |
278 | 178 |
|
283 | 179 |
section {* Formal Model of the Priority Inheritance Protocol *} |
267 | 180 |
|
280 | 181 |
text {* |
286 | 182 |
The Priority Inheritance Protocol, short PIP, is a scheduling |
183 |
algorithm for a single-processor system.\footnote{We shall come back |
|
184 |
later to the case of PIP on multi-processor systems.} Our model of |
|
185 |
PIP is based on Paulson's inductive approach to protocol |
|
186 |
verification \cite{Paulson98}, where the \emph{state} of a system is |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
187 |
given by a list of events that happened so far. \emph{Events} of PIP fall |
290 | 188 |
into five categories defined as the datatype: |
283 | 189 |
|
190 |
\begin{isabelle}\ \ \ \ \ %%% |
|
284 | 191 |
\mbox{\begin{tabular}{r@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {\hspace{7mm}}l} |
192 |
\isacommand{datatype} event |
|
193 |
& @{text "="} & @{term "Create thread priority"}\\ |
|
194 |
& @{text "|"} & @{term "Exit thread"} \\ |
|
286 | 195 |
& @{text "|"} & @{term "Set thread priority"} & {\rm reset of the priority for} @{text thread}\\ |
284 | 196 |
& @{text "|"} & @{term "P thread cs"} & {\rm request of resource} @{text "cs"} {\rm by} @{text "thread"}\\ |
197 |
& @{text "|"} & @{term "V thread cs"} & {\rm release of resource} @{text "cs"} {\rm by} @{text "thread"} |
|
198 |
\end{tabular}} |
|
199 |
\end{isabelle} |
|
200 |
||
201 |
\noindent |
|
286 | 202 |
whereby threads, priorities and (critical) resources are represented |
203 |
as natural numbers. The event @{term Set} models the situation that |
|
204 |
a thread obtains a new priority given by the programmer or |
|
205 |
user (for example via the {\tt nice} utility under UNIX). As in Paulson's work, we |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
206 |
need to define functions that allow us to make some observations |
297 | 207 |
about states. One, called @{term threads}, calculates the set of |
293 | 208 |
``live'' threads that we have seen so far: |
284 | 209 |
|
210 |
\begin{isabelle}\ \ \ \ \ %%% |
|
211 |
\mbox{\begin{tabular}{lcl} |
|
212 |
@{thm (lhs) threads.simps(1)} & @{text "\<equiv>"} & |
|
213 |
@{thm (rhs) threads.simps(1)}\\ |
|
214 |
@{thm (lhs) threads.simps(2)[where thread="th"]} & @{text "\<equiv>"} & |
|
215 |
@{thm (rhs) threads.simps(2)[where thread="th"]}\\ |
|
216 |
@{thm (lhs) threads.simps(3)[where thread="th"]} & @{text "\<equiv>"} & |
|
217 |
@{thm (rhs) threads.simps(3)[where thread="th"]}\\ |
|
218 |
@{term "threads (DUMMY#s)"} & @{text "\<equiv>"} & @{term "threads s"}\\ |
|
219 |
\end{tabular}} |
|
283 | 220 |
\end{isabelle} |
221 |
||
222 |
\noindent |
|
299 | 223 |
In this definition @{term "DUMMY # DUMMY"} stands for list-cons. |
290 | 224 |
Another function calculates the priority for a thread @{text "th"}, which is |
225 |
defined as |
|
284 | 226 |
|
227 |
\begin{isabelle}\ \ \ \ \ %%% |
|
228 |
\mbox{\begin{tabular}{lcl} |
|
229 |
@{thm (lhs) original_priority.simps(1)[where thread="th"]} & @{text "\<equiv>"} & |
|
230 |
@{thm (rhs) original_priority.simps(1)[where thread="th"]}\\ |
|
231 |
@{thm (lhs) original_priority.simps(2)[where thread="th" and thread'="th'"]} & @{text "\<equiv>"} & |
|
232 |
@{thm (rhs) original_priority.simps(2)[where thread="th" and thread'="th'"]}\\ |
|
233 |
@{thm (lhs) original_priority.simps(3)[where thread="th" and thread'="th'"]} & @{text "\<equiv>"} & |
|
234 |
@{thm (rhs) original_priority.simps(3)[where thread="th" and thread'="th'"]}\\ |
|
235 |
@{term "original_priority th (DUMMY#s)"} & @{text "\<equiv>"} & @{term "original_priority th s"}\\ |
|
236 |
\end{tabular}} |
|
237 |
\end{isabelle} |
|
238 |
||
239 |
\noindent |
|
240 |
In this definition we set @{text 0} as the default priority for |
|
241 |
threads that have not (yet) been created. The last function we need |
|
285 | 242 |
calculates the ``time'', or index, at which time a process had its |
290 | 243 |
priority last set. |
284 | 244 |
|
245 |
\begin{isabelle}\ \ \ \ \ %%% |
|
246 |
\mbox{\begin{tabular}{lcl} |
|
247 |
@{thm (lhs) birthtime.simps(1)[where thread="th"]} & @{text "\<equiv>"} & |
|
248 |
@{thm (rhs) birthtime.simps(1)[where thread="th"]}\\ |
|
249 |
@{thm (lhs) birthtime.simps(2)[where thread="th" and thread'="th'"]} & @{text "\<equiv>"} & |
|
250 |
@{thm (rhs) birthtime.simps(2)[where thread="th" and thread'="th'"]}\\ |
|
251 |
@{thm (lhs) birthtime.simps(3)[where thread="th" and thread'="th'"]} & @{text "\<equiv>"} & |
|
252 |
@{thm (rhs) birthtime.simps(3)[where thread="th" and thread'="th'"]}\\ |
|
253 |
@{term "birthtime th (DUMMY#s)"} & @{text "\<equiv>"} & @{term "birthtime th s"}\\ |
|
254 |
\end{tabular}} |
|
255 |
\end{isabelle} |
|
286 | 256 |
|
257 |
\noindent |
|
287 | 258 |
In this definition @{term "length s"} stands for the length of the list |
259 |
of events @{text s}. Again the default value in this function is @{text 0} |
|
260 |
for threads that have not been created yet. A \emph{precedence} of a thread @{text th} in a |
|
290 | 261 |
state @{text s} is the pair of natural numbers defined as |
284 | 262 |
|
286 | 263 |
\begin{isabelle}\ \ \ \ \ %%% |
290 | 264 |
@{thm preced_def[where thread="th"]} |
286 | 265 |
\end{isabelle} |
266 |
||
267 |
\noindent |
|
287 | 268 |
The point of precedences is to schedule threads not according to priorities (because what should |
286 | 269 |
we do in case two threads have the same priority), but according to precedences. |
290 | 270 |
Precedences allow us to always discriminate between two threads with equal priority by |
296 | 271 |
taking into account the time when the priority was last set. We order precedences so |
286 | 272 |
that threads with the same priority get a higher precedence if their priority has been |
293 | 273 |
set earlier, since for such threads it is more urgent to finish their work. In an implementation |
274 |
this choice would translate to a quite natural FIFO-scheduling of processes with |
|
286 | 275 |
the same priority. |
276 |
||
277 |
Next, we introduce the concept of \emph{waiting queues}. They are |
|
278 |
lists of threads associated with every resource. The first thread in |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
279 |
this list (i.e.~the head, or short @{term hd}) is chosen to be the one |
290 | 280 |
that is in possession of the |
286 | 281 |
``lock'' of the corresponding resource. We model waiting queues as |
293 | 282 |
functions, below abbreviated as @{text wq}. They take a resource as |
283 |
argument and return a list of threads. This allows us to define |
|
290 | 284 |
when a thread \emph{holds}, respectively \emph{waits} for, a |
293 | 285 |
resource @{text cs} given a waiting queue function @{text wq}. |
287 | 286 |
|
287 |
\begin{isabelle}\ \ \ \ \ %%% |
|
288 |
\begin{tabular}{@ {}l} |
|
290 | 289 |
@{thm cs_holding_def[where thread="th"]}\\ |
290 |
@{thm cs_waiting_def[where thread="th"]} |
|
287 | 291 |
\end{tabular} |
292 |
\end{isabelle} |
|
293 |
||
294 |
\noindent |
|
295 |
In this definition we assume @{text "set"} converts a list into a set. |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
296 |
At the beginning, that is in the state where no thread is created yet, |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
297 |
the waiting queue function will be the function that returns the |
293 | 298 |
empty list for every resource. |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
299 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
300 |
\begin{isabelle}\ \ \ \ \ %%% |
301 | 301 |
@{abbrev all_unlocked}\hfill\numbered{allunlocked} |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
302 |
\end{isabelle} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
303 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
304 |
\noindent |
290 | 305 |
Using @{term "holding"} and @{term waiting}, we can introduce \emph{Resource Allocation Graphs} |
306 |
(RAG), which represent the dependencies between threads and resources. |
|
307 |
We represent RAGs as relations using pairs of the form |
|
308 |
||
309 |
\begin{isabelle}\ \ \ \ \ %%% |
|
310 |
@{term "(Th th, Cs cs)"} \hspace{5mm}{\rm and}\hspace{5mm} |
|
311 |
@{term "(Cs cs, Th th)"} |
|
312 |
\end{isabelle} |
|
313 |
||
314 |
\noindent |
|
315 |
where the first stands for a \emph{waiting edge} and the second for a |
|
316 |
\emph{holding edge} (@{term Cs} and @{term Th} are constructors of a |
|
317 |
datatype for vertices). Given a waiting queue function, a RAG is defined |
|
306 | 318 |
as the union of the sets of waiting and holding edges, namely |
290 | 319 |
|
320 |
\begin{isabelle}\ \ \ \ \ %%% |
|
321 |
@{thm cs_depend_def} |
|
322 |
\end{isabelle} |
|
323 |
||
324 |
\noindent |
|
335 | 325 |
Given four threads and three resources, an instance of a RAG can be pictured |
306 | 326 |
as follows: |
290 | 327 |
|
328 |
\begin{center} |
|
297 | 329 |
\newcommand{\fnt}{\fontsize{7}{8}\selectfont} |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
330 |
\begin{tikzpicture}[scale=1] |
297 | 331 |
%%\draw[step=2mm] (-3,2) grid (1,-1); |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
332 |
|
297 | 333 |
\node (A) at (0,0) [draw, rounded corners=1mm, rectangle, very thick] {@{text "th\<^isub>0"}}; |
334 |
\node (B) at (2,0) [draw, circle, very thick, inner sep=0.4mm] {@{text "cs\<^isub>1"}}; |
|
335 |
\node (C) at (4,0.7) [draw, rounded corners=1mm, rectangle, very thick] {@{text "th\<^isub>1"}}; |
|
336 |
\node (D) at (4,-0.7) [draw, rounded corners=1mm, rectangle, very thick] {@{text "th\<^isub>2"}}; |
|
337 |
\node (E) at (6,-0.7) [draw, circle, very thick, inner sep=0.4mm] {@{text "cs\<^isub>2"}}; |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
338 |
\node (E1) at (6, 0.2) [draw, circle, very thick, inner sep=0.4mm] {@{text "cs\<^isub>3"}}; |
297 | 339 |
\node (F) at (8,-0.7) [draw, rounded corners=1mm, rectangle, very thick] {@{text "th\<^isub>3"}}; |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
340 |
|
300 | 341 |
\draw [<-,line width=0.6mm] (A) to node [pos=0.54,sloped,above=-0.5mm] {\fnt{}holding} (B); |
297 | 342 |
\draw [->,line width=0.6mm] (C) to node [pos=0.4,sloped,above=-0.5mm] {\fnt{}waiting} (B); |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
343 |
\draw [->,line width=0.6mm] (D) to node [pos=0.4,sloped,below=-0.5mm] {\fnt{}waiting} (B); |
300 | 344 |
\draw [<-,line width=0.6mm] (D) to node [pos=0.54,sloped,below=-0.5mm] {\fnt{}holding} (E); |
345 |
\draw [<-,line width=0.6mm] (D) to node [pos=0.54,sloped,above=-0.5mm] {\fnt{}holding} (E1); |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
346 |
\draw [->,line width=0.6mm] (F) to node [pos=0.45,sloped,below=-0.5mm] {\fnt{}waiting} (E); |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
347 |
\end{tikzpicture} |
290 | 348 |
\end{center} |
349 |
||
350 |
\noindent |
|
296 | 351 |
The use of relations for representing RAGs allows us to conveniently define |
306 | 352 |
the notion of the \emph{dependants} of a thread using the transitive closure |
353 |
operation for relations. This gives |
|
290 | 354 |
|
355 |
\begin{isabelle}\ \ \ \ \ %%% |
|
356 |
@{thm cs_dependents_def} |
|
357 |
\end{isabelle} |
|
358 |
||
359 |
\noindent |
|
296 | 360 |
This definition needs to account for all threads that wait for a thread to |
290 | 361 |
release a resource. This means we need to include threads that transitively |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
362 |
wait for a resource being released (in the picture above this means the dependants |
306 | 363 |
of @{text "th\<^isub>0"} are @{text "th\<^isub>1"} and @{text "th\<^isub>2"}, which wait for resource @{text "cs\<^isub>1"}, |
364 |
but also @{text "th\<^isub>3"}, |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
365 |
which cannot make any progress unless @{text "th\<^isub>2"} makes progress, which |
332 | 366 |
in turn needs to wait for @{text "th\<^isub>0"} to finish). If there is a circle of dependencies |
367 |
in a RAG, then clearly |
|
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
368 |
we have a deadlock. Therefore when a thread requests a resource, |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
369 |
we must ensure that the resulting RAG is not circular. |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
370 |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
371 |
Next we introduce the notion of the \emph{current precedence} of a thread @{text th} in a |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
372 |
state @{text s}. It is defined as |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
373 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
374 |
\begin{isabelle}\ \ \ \ \ %%% |
299 | 375 |
@{thm cpreced_def2}\hfill\numbered{cpreced} |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
376 |
\end{isabelle} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
377 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
378 |
\noindent |
306 | 379 |
where the dependants of @{text th} are given by the waiting queue function. |
293 | 380 |
While the precedence @{term prec} of a thread is determined by the programmer |
381 |
(for example when the thread is |
|
306 | 382 |
created), the point of the current precedence is to let the scheduler increase this |
383 |
precedence, if needed according to PIP. Therefore the current precedence of @{text th} is |
|
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
384 |
given as the maximum of the precedence @{text th} has in state @{text s} \emph{and} all |
306 | 385 |
threads that are dependants of @{text th}. Since the notion @{term "dependants"} is |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
386 |
defined as the transitive closure of all dependent threads, we deal correctly with the |
306 | 387 |
problem in the informal algorithm by Sha et al.~\cite{Sha90} where a priority of a thread is |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
388 |
lowered prematurely. |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
389 |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
390 |
The next function, called @{term schs}, defines the behaviour of the scheduler. It will be defined |
306 | 391 |
by recursion on the state (a list of events); this function returns a \emph{schedule state}, which |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
392 |
we represent as a record consisting of two |
296 | 393 |
functions: |
293 | 394 |
|
395 |
\begin{isabelle}\ \ \ \ \ %%% |
|
396 |
@{text "\<lparr>wq_fun, cprec_fun\<rparr>"} |
|
397 |
\end{isabelle} |
|
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
398 |
|
294
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
399 |
\noindent |
314 | 400 |
The first function is a waiting queue function (that is, it takes a |
401 |
resource @{text "cs"} and returns the corresponding list of threads |
|
402 |
that lock, respectively wait for, it); the second is a function that |
|
403 |
takes a thread and returns its current precedence (see |
|
332 | 404 |
the definition in \eqref{cpreced}). We assume the usual getter and setter methods for |
314 | 405 |
such records. |
294
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
406 |
|
306 | 407 |
In the initial state, the scheduler starts with all resources unlocked (the corresponding |
408 |
function is defined in \eqref{allunlocked}) and the |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
409 |
current precedence of every thread is initialised with @{term "Prc 0 0"}; that means |
299 | 410 |
\mbox{@{abbrev initial_cprec}}. Therefore |
332 | 411 |
we have for the initial shedule state |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
412 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
413 |
\begin{isabelle}\ \ \ \ \ %%% |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
414 |
\begin{tabular}{@ {}l} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
415 |
@{thm (lhs) schs.simps(1)} @{text "\<equiv>"}\\ |
294
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
416 |
\hspace{5mm}@{term "(|wq_fun = all_unlocked, cprec_fun = (\<lambda>_::thread. Prc 0 0)|)"} |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
417 |
\end{tabular} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
418 |
\end{isabelle} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
419 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
420 |
\noindent |
296 | 421 |
The cases for @{term Create}, @{term Exit} and @{term Set} are also straightforward: |
422 |
we calculate the waiting queue function of the (previous) state @{text s}; |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
423 |
this waiting queue function @{text wq} is unchanged in the next schedule state---because |
306 | 424 |
none of these events lock or release any resource; |
425 |
for calculating the next @{term "cprec_fun"}, we use @{text wq} and |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
426 |
@{term cpreced}. This gives the following three clauses for @{term schs}: |
290 | 427 |
|
428 |
\begin{isabelle}\ \ \ \ \ %%% |
|
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
429 |
\begin{tabular}{@ {}l} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
430 |
@{thm (lhs) schs.simps(2)} @{text "\<equiv>"}\\ |
294
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
431 |
\hspace{5mm}@{text "let"} @{text "wq = wq_fun (schs s)"} @{text "in"}\\ |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
432 |
\hspace{8mm}@{term "(|wq_fun = wq\<iota>, cprec_fun = cpreced wq\<iota> (Create th prio # s)|)"}\smallskip\\ |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
433 |
@{thm (lhs) schs.simps(3)} @{text "\<equiv>"}\\ |
294
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
434 |
\hspace{5mm}@{text "let"} @{text "wq = wq_fun (schs s)"} @{text "in"}\\ |
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
435 |
\hspace{8mm}@{term "(|wq_fun = wq\<iota>, cprec_fun = cpreced wq\<iota> (Exit th # s)|)"}\smallskip\\ |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
436 |
@{thm (lhs) schs.simps(4)} @{text "\<equiv>"}\\ |
294
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
437 |
\hspace{5mm}@{text "let"} @{text "wq = wq_fun (schs s)"} @{text "in"}\\ |
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
438 |
\hspace{8mm}@{term "(|wq_fun = wq\<iota>, cprec_fun = cpreced wq\<iota> (Set th prio # s)|)"} |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
439 |
\end{tabular} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
440 |
\end{isabelle} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
441 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
442 |
\noindent |
306 | 443 |
More interesting are the cases where a resource, say @{text cs}, is locked or released. In these cases |
300 | 444 |
we need to calculate a new waiting queue function. For the event @{term "P th cs"}, we have to update |
306 | 445 |
the function so that the new thread list for @{text cs} is the old thread list plus the thread @{text th} |
314 | 446 |
appended to the end of that list (remember the head of this list is assigned to be in the possession of this |
306 | 447 |
resource). This gives the clause |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
448 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
449 |
\begin{isabelle}\ \ \ \ \ %%% |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
450 |
\begin{tabular}{@ {}l} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
451 |
@{thm (lhs) schs.simps(5)} @{text "\<equiv>"}\\ |
294
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
452 |
\hspace{5mm}@{text "let"} @{text "wq = wq_fun (schs s)"} @{text "in"}\\ |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
453 |
\hspace{5mm}@{text "let"} @{text "new_wq = wq(cs := (wq cs @ [th]))"} @{text "in"}\\ |
294
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
454 |
\hspace{8mm}@{term "(|wq_fun = new_wq, cprec_fun = cpreced new_wq (P th cs # s)|)"} |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
455 |
\end{tabular} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
456 |
\end{isabelle} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
457 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
458 |
\noindent |
300 | 459 |
The clause for event @{term "V th cs"} is similar, except that we need to update the waiting queue function |
301 | 460 |
so that the thread that possessed the lock is deleted from the corresponding thread list. For this |
461 |
list transformation, we use |
|
296 | 462 |
the auxiliary function @{term release}. A simple version of @{term release} would |
306 | 463 |
just delete this thread and return the remaining threads, namely |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
464 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
465 |
\begin{isabelle}\ \ \ \ \ %%% |
296 | 466 |
\begin{tabular}{@ {}lcl} |
467 |
@{term "release []"} & @{text "\<equiv>"} & @{term "[]"}\\ |
|
468 |
@{term "release (DUMMY # qs)"} & @{text "\<equiv>"} & @{term "qs"}\\ |
|
469 |
\end{tabular} |
|
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
470 |
\end{isabelle} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
471 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
472 |
\noindent |
300 | 473 |
In practice, however, often the thread with the highest precedence in the list will get the |
296 | 474 |
lock next. We have implemented this choice, but later found out that the choice |
300 | 475 |
of which thread is chosen next is actually irrelevant for the correctness of PIP. |
296 | 476 |
Therefore we prove the stronger result where @{term release} is defined as |
477 |
||
478 |
\begin{isabelle}\ \ \ \ \ %%% |
|
479 |
\begin{tabular}{@ {}lcl} |
|
480 |
@{term "release []"} & @{text "\<equiv>"} & @{term "[]"}\\ |
|
481 |
@{term "release (DUMMY # qs)"} & @{text "\<equiv>"} & @{term "SOME qs'. distinct qs' \<and> set qs' = set qs"}\\ |
|
482 |
\end{tabular} |
|
483 |
\end{isabelle} |
|
484 |
||
485 |
\noindent |
|
306 | 486 |
where @{text "SOME"} stands for Hilbert's epsilon and implements an arbitrary |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
487 |
choice for the next waiting list. It just has to be a list of distinctive threads and |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
488 |
contain the same elements as @{text "qs"}. This gives for @{term V} the clause: |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
489 |
|
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
490 |
\begin{isabelle}\ \ \ \ \ %%% |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
491 |
\begin{tabular}{@ {}l} |
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
492 |
@{thm (lhs) schs.simps(6)} @{text "\<equiv>"}\\ |
294
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
493 |
\hspace{5mm}@{text "let"} @{text "wq = wq_fun (schs s)"} @{text "in"}\\ |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
494 |
\hspace{5mm}@{text "let"} @{text "new_wq = release (wq cs)"} @{text "in"}\\ |
294
bc5bf9e9ada2
renamed waiting_queue -> wq_fun; cur_preced -> cprec_fun
urbanc
parents:
293
diff
changeset
|
495 |
\hspace{8mm}@{term "(|wq_fun = new_wq, cprec_fun = cpreced new_wq (V th cs # s)|)"} |
291
5ef9f6ebe827
more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents:
290
diff
changeset
|
496 |
\end{tabular} |
290 | 497 |
\end{isabelle} |
498 |
||
300 | 499 |
Having the scheduler function @{term schs} at our disposal, we can ``lift'', or |
500 |
overload, the notions |
|
501 |
@{term waiting}, @{term holding}, @{term depend} and @{term cp} to operate on states only. |
|
286 | 502 |
|
503 |
\begin{isabelle}\ \ \ \ \ %%% |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
504 |
\begin{tabular}{@ {}rcl} |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
505 |
@{thm (lhs) s_holding_abv} & @{text "\<equiv>"} & @{thm (rhs) s_holding_abv}\\ |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
506 |
@{thm (lhs) s_waiting_abv} & @{text "\<equiv>"} & @{thm (rhs) s_waiting_abv}\\ |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
507 |
@{thm (lhs) s_depend_abv} & @{text "\<equiv>"} & @{thm (rhs) s_depend_abv}\\ |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
508 |
@{thm (lhs) cp_def} & @{text "\<equiv>"} & @{thm (rhs) cp_def} |
287 | 509 |
\end{tabular} |
510 |
\end{isabelle} |
|
511 |
||
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
512 |
\noindent |
335 | 513 |
With these abbreviations in place we can introduce |
300 | 514 |
the notion of threads being @{term readys} in a state (i.e.~threads |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
515 |
that do not wait for any resource) and the running thread. |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
516 |
|
287 | 517 |
\begin{isabelle}\ \ \ \ \ %%% |
518 |
\begin{tabular}{@ {}l} |
|
519 |
@{thm readys_def}\\ |
|
520 |
@{thm runing_def}\\ |
|
286 | 521 |
\end{tabular} |
522 |
\end{isabelle} |
|
284 | 523 |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
524 |
\noindent |
332 | 525 |
In the second definition @{term "DUMMY ` DUMMY"} stands for the image of a set under a function. |
306 | 526 |
Note that in the initial state, that is where the list of events is empty, the set |
309 | 527 |
@{term threads} is empty and therefore there is neither a thread ready nor running. |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
528 |
If there is one or more threads ready, then there can only be \emph{one} thread |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
529 |
running, namely the one whose current precedence is equal to the maximum of all ready |
314 | 530 |
threads. We use sets to capture both possibilities. |
306 | 531 |
We can now also conveniently define the set of resources that are locked by a thread in a |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
532 |
given state. |
284 | 533 |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
534 |
\begin{isabelle}\ \ \ \ \ %%% |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
535 |
@{thm holdents_def} |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
536 |
\end{isabelle} |
284 | 537 |
|
306 | 538 |
Finally we can define what a \emph{valid state} is in our model of PIP. For |
304 | 539 |
example we cannot expect to be able to exit a thread, if it was not |
332 | 540 |
created yet. This would cause havoc in any scheduler. |
541 |
These validity constraints on states are characterised by the |
|
306 | 542 |
inductive predicate @{term "step"} and @{term vt}. We first give five inference rules |
543 |
for @{term step} relating a state and an event that can happen next. |
|
284 | 544 |
|
545 |
\begin{center} |
|
546 |
\begin{tabular}{c} |
|
547 |
@{thm[mode=Rule] thread_create[where thread=th]}\hspace{1cm} |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
548 |
@{thm[mode=Rule] thread_exit[where thread=th]} |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
549 |
\end{tabular} |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
550 |
\end{center} |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
551 |
|
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
552 |
\noindent |
333 | 553 |
The first rule states that a thread can only be created, if it is not alive yet. |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
554 |
Similarly, the second rule states that a thread can only be terminated if it was |
306 | 555 |
running and does not lock any resources anymore (this simplifies slightly our model; |
314 | 556 |
in practice we would expect the operating system releases all locks held by a |
306 | 557 |
thread that is about to exit). The event @{text Set} can happen |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
558 |
if the corresponding thread is running. |
284 | 559 |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
560 |
\begin{center} |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
561 |
@{thm[mode=Rule] thread_set[where thread=th]} |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
562 |
\end{center} |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
563 |
|
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
564 |
\noindent |
301 | 565 |
If a thread wants to lock a resource, then the thread needs to be |
566 |
running and also we have to make sure that the resource lock does |
|
567 |
not lead to a cycle in the RAG. In practice, ensuring the latter is |
|
314 | 568 |
the responsibility of the programmer. In our formal |
569 |
model we brush aside these problematic cases in order to be able to make |
|
301 | 570 |
some meaningful statements about PIP.\footnote{This situation is |
333 | 571 |
similar to the infamous \emph{occurs check} in Prolog: In order to say |
306 | 572 |
anything meaningful about unification, one needs to perform an occurs |
331 | 573 |
check. But in practice the occurs check is omitted and the |
306 | 574 |
responsibility for avoiding problems rests with the programmer.} |
575 |
||
576 |
\begin{center} |
|
577 |
@{thm[mode=Rule] thread_P[where thread=th]} |
|
578 |
\end{center} |
|
579 |
||
580 |
\noindent |
|
301 | 581 |
Similarly, if a thread wants to release a lock on a resource, then |
582 |
it must be running and in the possession of that lock. This is |
|
306 | 583 |
formally given by the last inference rule of @{term step}. |
584 |
||
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
585 |
\begin{center} |
306 | 586 |
@{thm[mode=Rule] thread_V[where thread=th]} |
284 | 587 |
\end{center} |
306 | 588 |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
589 |
\noindent |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
590 |
A valid state of PIP can then be conveniently be defined as follows: |
284 | 591 |
|
592 |
\begin{center} |
|
593 |
\begin{tabular}{c} |
|
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
594 |
@{thm[mode=Axiom] vt_nil}\hspace{1cm} |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
595 |
@{thm[mode=Rule] vt_cons} |
284 | 596 |
\end{tabular} |
597 |
\end{center} |
|
598 |
||
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
599 |
\noindent |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
600 |
This completes our formal model of PIP. In the next section we present |
309 | 601 |
properties that show our model of PIP is correct. |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
602 |
*} |
274 | 603 |
|
310 | 604 |
section {* The Correctness Proof *} |
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
605 |
|
301 | 606 |
(*<*) |
607 |
context extend_highest_gen |
|
608 |
begin |
|
307 | 609 |
(*>*) |
301 | 610 |
text {* |
329 | 611 |
Sha et al.~state their first correctness criterion for PIP in terms |
612 |
of the number of low-priority threads \cite[Theorem 3]{Sha90}: if |
|
613 |
there are @{text n} low-priority threads, then a blocked job with |
|
333 | 614 |
high priority can only be blocked a maximum of @{text n} times. |
332 | 615 |
Their second correctness criterion is given |
329 | 616 |
in terms of the number of critical resources \cite[Theorem 6]{Sha90}: if there are |
322 | 617 |
@{text m} critical resources, then a blocked job with high priority |
333 | 618 |
can only be blocked a maximum of @{text m} times. Both results on their own, strictly speaking, do |
324 | 619 |
\emph{not} prevent indefinite, or unbounded, Priority Inversion, |
329 | 620 |
because if a low-priority thread does not give up its critical |
324 | 621 |
resource (the one the high-priority thread is waiting for), then the |
322 | 622 |
high-priority thread can never run. The argument of Sha et al.~is |
623 |
that \emph{if} threads release locked resources in a finite amount |
|
324 | 624 |
of time, then indefinite Priority Inversion cannot occur---the high-priority |
322 | 625 |
thread is guaranteed to run eventually. The assumption is that |
332 | 626 |
programmers must ensure that threads are programmed in this way. However, even |
627 |
taking this assumption into account, the correctness properties of |
|
628 |
Sha et al.~are |
|
629 |
\emph{not} true for their version of PIP---despide being ``proved''. As Yodaiken |
|
324 | 630 |
\cite{Yodaiken02} pointed out: If a low-priority thread possesses |
631 |
locks to two resources for which two high-priority threads are |
|
632 |
waiting for, then lowering the priority prematurely after giving up |
|
633 |
only one lock, can cause indefinite Priority Inversion for one of the |
|
329 | 634 |
high-priority threads, invalidating their two bounds. |
307 | 635 |
|
323 | 636 |
Even when fixed, their proof idea does not seem to go through for |
332 | 637 |
us, because of the way we have set up our formal model of PIP. One |
335 | 638 |
reason is that we allow critical sections to intersect |
333 | 639 |
(something Sha et al.~explicitly exclude). Therefore we have |
640 |
designed a different correctness criterion for PIP. The idea behind |
|
641 |
our criterion is as follows: for all states @{text s}, we know the |
|
642 |
corresponding thread @{text th} with the highest precedence; we show |
|
643 |
that in every future state (denoted by @{text "s' @ s"}) in which |
|
644 |
@{text th} is still alive, either @{text th} is running or it is |
|
335 | 645 |
blocked by a thread that was alive in the state @{text s} and was waiting |
646 |
or in the possession of a lock in @{text s}. Since in @{text s}, as in |
|
333 | 647 |
every state, the set of alive threads is finite, @{text th} can only |
648 |
be blocked a finite number of times. This is independent of how many |
|
649 |
threads of lower priority are created in @{text "s'"}. We will actually prove a |
|
650 |
stronger statement where we also provide the current precedence of |
|
651 |
the blocking thread. However, this correctness criterion hinges upon |
|
652 |
a number of assumptions about the states @{text s} and @{text "s' @ |
|
653 |
s"}, the thread @{text th} and the events happening in @{text |
|
654 |
s'}. We list them next: |
|
307 | 655 |
|
656 |
\begin{quote} |
|
333 | 657 |
{\bf Assumptions on the states {\boldmath@{text s}} and |
658 |
{\boldmath@{text "s' @ s"}:}} In order to make |
|
307 | 659 |
any meaningful statement, we need to require that @{text "s"} and |
660 |
@{text "s' @ s"} are valid states, namely |
|
661 |
\begin{isabelle}\ \ \ \ \ %%% |
|
662 |
\begin{tabular}{l} |
|
663 |
@{term "vt s"}\\ |
|
664 |
@{term "vt (s' @ s)"} |
|
665 |
\end{tabular} |
|
666 |
\end{isabelle} |
|
667 |
\end{quote} |
|
301 | 668 |
|
307 | 669 |
\begin{quote} |
333 | 670 |
{\bf Assumptions on the thread {\boldmath@{text "th"}:}} |
671 |
The thread @{text th} must be alive in @{text s} and |
|
310 | 672 |
has the highest precedence of all alive threads in @{text s}. Furthermore the |
673 |
priority of @{text th} is @{text prio} (we need this in the next assumptions). |
|
307 | 674 |
\begin{isabelle}\ \ \ \ \ %%% |
675 |
\begin{tabular}{l} |
|
676 |
@{term "th \<in> threads s"}\\ |
|
677 |
@{term "prec th s = Max (cprec s ` threads s)"}\\ |
|
678 |
@{term "prec th s = (prio, DUMMY)"} |
|
679 |
\end{tabular} |
|
680 |
\end{isabelle} |
|
681 |
\end{quote} |
|
682 |
||
683 |
\begin{quote} |
|
333 | 684 |
{\bf Assumptions on the events in {\boldmath@{text "s'"}:}} We want to prove that @{text th} cannot |
309 | 685 |
be blocked indefinitely. Of course this can happen if threads with higher priority |
331 | 686 |
than @{text th} are continuously created in @{text s'}. Therefore we have to assume that |
309 | 687 |
events in @{text s'} can only create (respectively set) threads with equal or lower |
310 | 688 |
priority than @{text prio} of @{text th}. We also need to assume that the |
689 |
priority of @{text "th"} does not get reset and also that @{text th} does |
|
690 |
not get ``exited'' in @{text "s'"}. This can be ensured by assuming the following three implications. |
|
307 | 691 |
\begin{isabelle}\ \ \ \ \ %%% |
692 |
\begin{tabular}{l} |
|
310 | 693 |
{If}~~@{text "Create th' prio' \<in> set s'"}~~{then}~~@{text "prio' \<le> prio"}\\ |
307 | 694 |
{If}~~@{text "Set th' prio' \<in> set s'"}~~{then}~~@{text "th' \<noteq> th"}~~{and}~~@{text "prio' \<le> prio"}\\ |
695 |
{If}~~@{text "Exit th' \<in> set s'"}~~{then}~~@{text "th' \<noteq> th"}\\ |
|
696 |
\end{tabular} |
|
697 |
\end{isabelle} |
|
698 |
\end{quote} |
|
301 | 699 |
|
307 | 700 |
\noindent |
332 | 701 |
The locale mechanism of Isabelle helps us to manage conveniently such assumptions~\cite{Haftmann08}. |
333 | 702 |
Under these assumptions we shall prove the following correctness property: |
307 | 703 |
|
308 | 704 |
\begin{theorem}\label{mainthm} |
307 | 705 |
Given the assumptions about states @{text "s"} and @{text "s' @ s"}, |
308 | 706 |
the thread @{text th} and the events in @{text "s'"}, |
707 |
if @{term "th' \<in> running (s' @ s)"} and @{text "th' \<noteq> th"} then |
|
329 | 708 |
@{text "th' \<in> threads s"}, @{text "\<not> detached s th'"} and |
709 |
@{term "cp (s' @ s) th' = prec th s"}. |
|
307 | 710 |
\end{theorem} |
301 | 711 |
|
308 | 712 |
\noindent |
324 | 713 |
This theorem ensures that the thread @{text th}, which has the |
714 |
highest precedence in the state @{text s}, can only be blocked in |
|
715 |
the state @{text "s' @ s"} by a thread @{text th'} that already |
|
329 | 716 |
existed in @{text s} and requested or had a lock on at least |
717 |
one resource---that means the thread was not \emph{detached} in @{text s}. |
|
718 |
As we shall see shortly, that means there are only finitely |
|
332 | 719 |
many threads that can block @{text th} in this way and then they |
720 |
need to run with the same current precedence as @{text th}. |
|
329 | 721 |
|
722 |
Like in the argument by Sha et al.~our |
|
324 | 723 |
finite bound does not guarantee absence of indefinite Priority |
724 |
Inversion. For this we further have to assume that every thread |
|
332 | 725 |
gives up its resources after a finite amount of time. We found that |
324 | 726 |
this assumption is awkward to formalise in our model. Therefore we |
727 |
leave it out and let the programmer assume the responsibility to |
|
331 | 728 |
program threads in such a benign manner (in addition to causing no |
325 | 729 |
circularity in the @{text RAG}). In this detail, we do not |
324 | 730 |
make any progress in comparison with the work by Sha et al. |
329 | 731 |
However, we are able to combine their two separate bounds into a |
332 | 732 |
single theorem improving their bound. |
309 | 733 |
|
734 |
In what follows we will describe properties of PIP that allow us to prove |
|
325 | 735 |
Theorem~\ref{mainthm} and, when instructive, briefly describe our argument. |
736 |
It is relatively easily to see that |
|
309 | 737 |
|
738 |
\begin{isabelle}\ \ \ \ \ %%% |
|
739 |
\begin{tabular}{@ {}l} |
|
740 |
@{text "running s \<subseteq> ready s \<subseteq> threads s"}\\ |
|
741 |
@{thm[mode=IfThen] finite_threads} |
|
742 |
\end{tabular} |
|
743 |
\end{isabelle} |
|
744 |
||
745 |
\noindent |
|
332 | 746 |
The second property is by induction of @{term vt}. The next three |
309 | 747 |
properties are |
308 | 748 |
|
309 | 749 |
\begin{isabelle}\ \ \ \ \ %%% |
750 |
\begin{tabular}{@ {}l} |
|
751 |
@{thm[mode=IfThen] waiting_unique[of _ _ "cs\<^isub>1" "cs\<^isub>2"]}\\ |
|
752 |
@{thm[mode=IfThen] held_unique[of _ "th\<^isub>1" _ "th\<^isub>2"]}\\ |
|
753 |
@{thm[mode=IfThen] runing_unique[of _ "th\<^isub>1" "th\<^isub>2"]} |
|
754 |
\end{tabular} |
|
755 |
\end{isabelle} |
|
308 | 756 |
|
309 | 757 |
\noindent |
325 | 758 |
The first property states that every waiting thread can only wait for a single |
759 |
resource (because it gets suspended after requesting that resource); the second |
|
760 |
that every resource can only be held by a single thread; |
|
310 | 761 |
the third property establishes that in every given valid state, there is |
762 |
at most one running thread. We can also show the following properties |
|
325 | 763 |
about the @{term RAG} in @{text "s"}. |
310 | 764 |
|
765 |
\begin{isabelle}\ \ \ \ \ %%% |
|
766 |
\begin{tabular}{@ {}l} |
|
312 | 767 |
@{text If}~@{thm (prem 1) acyclic_depend}~@{text "then"}:\\ |
768 |
\hspace{5mm}@{thm (concl) acyclic_depend}, |
|
769 |
@{thm (concl) finite_depend} and |
|
770 |
@{thm (concl) wf_dep_converse},\\ |
|
325 | 771 |
\hspace{5mm}@{text "if"}~@{thm (prem 2) dm_depend_threads}~@{text "then"}~@{thm (concl) dm_depend_threads} |
772 |
and\\ |
|
773 |
\hspace{5mm}@{text "if"}~@{thm (prem 2) range_in}~@{text "then"}~@{thm (concl) range_in}. |
|
310 | 774 |
\end{tabular} |
775 |
\end{isabelle} |
|
309 | 776 |
|
325 | 777 |
\noindent |
778 |
The acyclicity property follow from how we restricted the events in |
|
779 |
@{text step}; similarly the finiteness and well-foundedness property. |
|
780 |
The last two properties establish that every thread in a @{text "RAG"} |
|
781 |
(either holding or waiting for a resource) is a live thread. |
|
782 |
||
329 | 783 |
The key lemma in our proof of Theorem~\ref{mainthm} is as follows: |
325 | 784 |
|
785 |
\begin{lemma}\label{mainlem} |
|
786 |
Given the assumptions about states @{text "s"} and @{text "s' @ s"}, |
|
787 |
the thread @{text th} and the events in @{text "s'"}, |
|
788 |
if @{term "th' \<in> treads (s' @ s)"}, @{text "th' \<noteq> th"} and @{text "detached (s' @ s) th'"}\\ |
|
789 |
then @{text "th' \<notin> running (s' @ s)"}. |
|
790 |
\end{lemma} |
|
309 | 791 |
|
792 |
\noindent |
|
325 | 793 |
The point of this lemma is that a thread different from @{text th} (which has the highest |
332 | 794 |
precedence in @{text s}) and not holding any resource, cannot be running |
325 | 795 |
in the state @{text "s' @ s"}. |
301 | 796 |
|
325 | 797 |
\begin{proof} |
798 |
Since thread @{text "th'"} does not hold any resource, no thread can depend on it. |
|
799 |
Therefore its current precedence @{term "cp (s' @ s) th'"} equals its own precedence |
|
800 |
@{term "prec th' (s' @ s)"}. Since @{text "th"} has the highest precedence in the |
|
801 |
state @{text "(s' @ s)"} and precedences are distinct among threads, we have |
|
802 |
@{term "prec th' (s' @s ) < prec th (s' @ s)"}. From this |
|
803 |
we have @{term "cp (s' @ s) th' < prec th (s' @ s)"}. |
|
804 |
Since @{text "prec th (s' @ s)"} is already the highest |
|
805 |
@{term "cp (s' @ s) th"} can not be higher than this and can not be lower either (by |
|
806 |
definition of @{term "cp"}). Consequently, we have @{term "prec th (s' @ s) = cp (s' @ s) th"}. |
|
807 |
Finally we have @{term "cp (s' @ s) th' < cp (s' @ s) th"}. |
|
808 |
By defintion of @{text "running"}, @{text "th'"} can not be running in state |
|
809 |
@{text "s' @ s"}, as we had to show.\qed |
|
810 |
\end{proof} |
|
308 | 811 |
|
325 | 812 |
\noindent |
332 | 813 |
Since @{text "th'"} is not able to run in state @{text "s' @ s"}, it is not able to |
328 | 814 |
issue a @{text "P"} or @{text "V"} event. Therefore if @{text "s' @ s"} is extended |
325 | 815 |
one step further, @{text "th'"} still cannot hold any resource. The situation will |
816 |
not change in further extensions as long as @{text "th"} holds the highest precedence. |
|
817 |
||
329 | 818 |
From this lemma we can deduce Theorem~\ref{mainthm}: that @{text th} can only be |
819 |
blocked by a thread @{text th'} that |
|
326 | 820 |
held some resource in state @{text s} (that is not @{text "detached"}). And furthermore |
821 |
that the current precedence of @{text th'} in state @{text "(s' @ s)"} must be equal to the |
|
822 |
precedence of @{text th} in @{text "s"}. |
|
823 |
We show this theorem by induction on @{text "s'"} using Lemma~\ref{mainlem}. |
|
333 | 824 |
This theorem gives a stricter bound on the threads that can block @{text th} than the |
332 | 825 |
one obtained by Sha et al.~\cite{Sha90}: |
326 | 826 |
only threads that were alive in state @{text s} and moreover held a resource. |
329 | 827 |
This means our bound is in terms of both---alive threads in state @{text s} |
828 |
and number of critical resources. Finally, the theorem establishes that the blocking threads have the |
|
326 | 829 |
current precedence raised to the precedence of @{text th}. |
830 |
||
329 | 831 |
We can furthermore prove that under our assumptions no deadlock exists in the state @{text "s' @ s"} |
328 | 832 |
by showing that @{text "running (s' @ s)"} is not empty. |
833 |
||
834 |
\begin{lemma} |
|
835 |
Given the assumptions about states @{text "s"} and @{text "s' @ s"}, |
|
836 |
the thread @{text th} and the events in @{text "s'"}, |
|
837 |
@{term "running (s' @ s) \<noteq> {}"}. |
|
838 |
\end{lemma} |
|
839 |
||
840 |
\begin{proof} |
|
841 |
If @{text th} is blocked, then by following its dependants graph, we can always |
|
842 |
reach a ready thread @{text th'}, and that thread must have inherited the |
|
843 |
precedence of @{text th}.\qed |
|
844 |
\end{proof} |
|
845 |
||
846 |
||
326 | 847 |
%The following lemmas show how every node in RAG can be chased to ready threads: |
848 |
%\begin{enumerate} |
|
849 |
%\item Every node in RAG can be chased to a ready thread (@{text "chain_building"}): |
|
850 |
% @ {thm [display] chain_building[rule_format]} |
|
851 |
%\item The ready thread chased to is unique (@{text "dchain_unique"}): |
|
852 |
% @ {thm [display] dchain_unique[of _ _ "th\<^isub>1" "th\<^isub>2"]} |
|
853 |
%\end{enumerate} |
|
301 | 854 |
|
326 | 855 |
%Some deeper results about the system: |
856 |
%\begin{enumerate} |
|
857 |
%\item The maximum of @{term "cp"} and @{term "preced"} are equal (@{text "max_cp_eq"}): |
|
858 |
%@ {thm [display] max_cp_eq} |
|
859 |
%\item There must be one ready thread having the max @{term "cp"}-value |
|
860 |
%(@{text "max_cp_readys_threads"}): |
|
861 |
%@ {thm [display] max_cp_readys_threads} |
|
862 |
%\end{enumerate} |
|
325 | 863 |
|
326 | 864 |
%The relationship between the count of @{text "P"} and @{text "V"} and the number of |
865 |
%critical resources held by a thread is given as follows: |
|
866 |
%\begin{enumerate} |
|
867 |
%\item The @{term "V"}-operation decreases the number of critical resources |
|
868 |
% one thread holds (@{text "cntCS_v_dec"}) |
|
869 |
% @ {thm [display] cntCS_v_dec} |
|
870 |
%\item The number of @{text "V"} never exceeds the number of @{text "P"} |
|
871 |
% (@ {text "cnp_cnv_cncs"}): |
|
872 |
% @ {thm [display] cnp_cnv_cncs} |
|
873 |
%\item The number of @{text "V"} equals the number of @{text "P"} when |
|
874 |
% the relevant thread is not living: |
|
875 |
% (@{text "cnp_cnv_eq"}): |
|
876 |
% @ {thm [display] cnp_cnv_eq} |
|
877 |
%\item When a thread is not living, it does not hold any critical resource |
|
878 |
% (@{text "not_thread_holdents"}): |
|
879 |
% @ {thm [display] not_thread_holdents} |
|
880 |
%\item When the number of @{text "P"} equals the number of @{text "V"}, the relevant |
|
881 |
% thread does not hold any critical resource, therefore no thread can depend on it |
|
882 |
% (@{text "count_eq_dependents"}): |
|
883 |
% @ {thm [display] count_eq_dependents} |
|
884 |
%\end{enumerate} |
|
313 | 885 |
|
326 | 886 |
%The reason that only threads which already held some resoures |
887 |
%can be runing and block @{text "th"} is that if , otherwise, one thread |
|
888 |
%does not hold any resource, it may never have its prioirty raised |
|
889 |
%and will not get a chance to run. This fact is supported by |
|
890 |
%lemma @{text "moment_blocked"}: |
|
891 |
%@ {thm [display] moment_blocked} |
|
892 |
%When instantiating @{text "i"} to @{text "0"}, the lemma means threads which did not hold any |
|
893 |
%resource in state @{text "s"} will not have a change to run latter. Rephrased, it means |
|
894 |
%any thread which is running after @{text "th"} became the highest must have already held |
|
895 |
%some resource at state @{text "s"}. |
|
313 | 896 |
|
897 |
||
326 | 898 |
%When instantiating @{text "i"} to a number larger than @{text "0"}, the lemma means |
899 |
%if a thread releases all its resources at some moment in @{text "t"}, after that, |
|
900 |
%it may never get a change to run. If every thread releases its resource in finite duration, |
|
901 |
%then after a while, only thread @{text "th"} is left running. This shows how indefinite |
|
902 |
%priority inversion can be avoided. |
|
313 | 903 |
|
326 | 904 |
%All these assumptions are put into a predicate @{term "extend_highest_gen"}. |
905 |
%It can be proved that @{term "extend_highest_gen"} holds |
|
906 |
%for any moment @{text "i"} in it @{term "t"} (@{text "red_moment"}): |
|
907 |
%@ {thm [display] red_moment} |
|
325 | 908 |
|
326 | 909 |
%From this, an induction principle can be derived for @{text "t"}, so that |
910 |
%properties already derived for @{term "t"} can be applied to any prefix |
|
911 |
%of @{text "t"} in the proof of new properties |
|
912 |
%about @{term "t"} (@{text "ind"}): |
|
913 |
%\begin{center} |
|
914 |
%@ {thm[display] ind} |
|
915 |
%\end{center} |
|
325 | 916 |
|
326 | 917 |
%The following properties can be proved about @{term "th"} in @{term "t"}: |
918 |
%\begin{enumerate} |
|
919 |
%\item In @{term "t"}, thread @{term "th"} is kept live and its |
|
920 |
% precedence is preserved as well |
|
921 |
% (@{text "th_kept"}): |
|
922 |
% @ {thm [display] th_kept} |
|
923 |
%\item In @{term "t"}, thread @{term "th"}'s precedence is always the maximum among |
|
924 |
% all living threads |
|
925 |
% (@{text "max_preced"}): |
|
926 |
% @ {thm [display] max_preced} |
|
927 |
%\item In @{term "t"}, thread @{term "th"}'s current precedence is always the maximum precedence |
|
928 |
% among all living threads |
|
929 |
% (@{text "th_cp_max_preced"}): |
|
930 |
% @ {thm [display] th_cp_max_preced} |
|
931 |
%\item In @{term "t"}, thread @{term "th"}'s current precedence is always the maximum current |
|
932 |
% precedence among all living threads |
|
933 |
% (@{text "th_cp_max"}): |
|
934 |
% @ {thm [display] th_cp_max} |
|
935 |
%\item In @{term "t"}, thread @{term "th"}'s current precedence equals its precedence at moment |
|
936 |
% @{term "s"} |
|
937 |
% (@{text "th_cp_preced"}): |
|
938 |
% @ {thm [display] th_cp_preced} |
|
939 |
%\end{enumerate} |
|
940 |
||
941 |
%The main theorem of this part is to characterizing the running thread during @{term "t"} |
|
942 |
%(@{text "runing_inversion_2"}): |
|
943 |
%@ {thm [display] runing_inversion_2} |
|
944 |
%According to this, if a thread is running, it is either @{term "th"} or was |
|
945 |
%already live and held some resource |
|
946 |
%at moment @{text "s"} (expressed by: @{text "cntV s th' < cntP s th'"}). |
|
947 |
||
948 |
%Since there are only finite many threads live and holding some resource at any moment, |
|
949 |
%if every such thread can release all its resources in finite duration, then after finite |
|
950 |
%duration, none of them may block @{term "th"} anymore. So, no priority inversion may happen |
|
951 |
%then. |
|
325 | 952 |
*} |
313 | 953 |
(*<*) |
954 |
end |
|
955 |
(*>*) |
|
956 |
||
314 | 957 |
section {* Properties for an Implementation\label{implement} *} |
311 | 958 |
|
959 |
text {* |
|
312 | 960 |
While a formal correctness proof for our model of PIP is certainly |
961 |
attractive (especially in light of the flawed proof by Sha et |
|
962 |
al.~\cite{Sha90}), we found that the formalisation can even help us |
|
963 |
with efficiently implementing PIP. |
|
311 | 964 |
|
312 | 965 |
For example Baker complained that calculating the current precedence |
321 | 966 |
in PIP is quite ``heavy weight'' in Linux (see the Introduction). |
332 | 967 |
In our model of PIP the current precedence of a thread in a state @{text s} |
312 | 968 |
depends on all its dependants---a ``global'' transitive notion, |
969 |
which is indeed heavy weight (see Def.~shown in \eqref{cpreced}). |
|
321 | 970 |
We can however improve upon this. For this let us define the notion |
971 |
of @{term children} of a thread @{text th} in a state @{text s} as |
|
312 | 972 |
|
973 |
\begin{isabelle}\ \ \ \ \ %%% |
|
974 |
\begin{tabular}{@ {}l} |
|
975 |
@{thm children_def2} |
|
976 |
\end{tabular} |
|
977 |
\end{isabelle} |
|
978 |
||
979 |
\noindent |
|
332 | 980 |
where a child is a thread that is only one ``hop'' away from the tread |
321 | 981 |
@{text th} in the @{term RAG} (and waiting for @{text th} to release |
332 | 982 |
a resource). We can prove the following lemma. |
311 | 983 |
|
312 | 984 |
\begin{lemma}\label{childrenlem} |
985 |
@{text "If"} @{thm (prem 1) cp_rec} @{text "then"} |
|
986 |
\begin{center} |
|
987 |
@{thm (concl) cp_rec}. |
|
988 |
\end{center} |
|
989 |
\end{lemma} |
|
311 | 990 |
|
312 | 991 |
\noindent |
992 |
That means the current precedence of a thread @{text th} can be |
|
993 |
computed locally by considering only the children of @{text th}. In |
|
994 |
effect, it only needs to be recomputed for @{text th} when one of |
|
321 | 995 |
its children changes its current precedence. Once the current |
312 | 996 |
precedence is computed in this more efficient manner, the selection |
997 |
of the thread with highest precedence from a set of ready threads is |
|
998 |
a standard scheduling operation implemented in most operating |
|
999 |
systems. |
|
311 | 1000 |
|
332 | 1001 |
Of course the main work for implementing PIP involves the |
321 | 1002 |
scheduler and coding how it should react to events. Below we |
1003 |
outline how our formalisation guides this implementation for each |
|
1004 |
kind of event.\smallskip |
|
312 | 1005 |
*} |
311 | 1006 |
|
1007 |
(*<*) |
|
312 | 1008 |
context step_create_cps |
1009 |
begin |
|
1010 |
(*>*) |
|
1011 |
text {* |
|
1012 |
\noindent |
|
321 | 1013 |
\colorbox{mygrey}{@{term "Create th prio"}:} We assume that the current state @{text s'} and |
312 | 1014 |
the next state @{term "s \<equiv> Create th prio#s'"} are both valid (meaning the event |
1015 |
is allowed to occur). In this situation we can show that |
|
1016 |
||
1017 |
\begin{isabelle}\ \ \ \ \ %%% |
|
1018 |
\begin{tabular}{@ {}l} |
|
321 | 1019 |
@{thm eq_dep},\\ |
1020 |
@{thm eq_cp_th}, and\\ |
|
312 | 1021 |
@{thm[mode=IfThen] eq_cp} |
1022 |
\end{tabular} |
|
1023 |
\end{isabelle} |
|
1024 |
||
1025 |
\noindent |
|
332 | 1026 |
This means in an implementation we do not have recalculate the @{text RAG} and also none of the |
312 | 1027 |
current precedences of the other threads. The current precedence of the created |
321 | 1028 |
thread @{text th} is just its precedence, namely the pair @{term "(prio, length (s::event list))"}. |
312 | 1029 |
\smallskip |
1030 |
*} |
|
1031 |
(*<*) |
|
1032 |
end |
|
1033 |
context step_exit_cps |
|
1034 |
begin |
|
1035 |
(*>*) |
|
1036 |
text {* |
|
1037 |
\noindent |
|
321 | 1038 |
\colorbox{mygrey}{@{term "Exit th"}:} We again assume that the current state @{text s'} and |
312 | 1039 |
the next state @{term "s \<equiv> Exit th#s'"} are both valid. We can show that |
1040 |
||
1041 |
\begin{isabelle}\ \ \ \ \ %%% |
|
1042 |
\begin{tabular}{@ {}l} |
|
321 | 1043 |
@{thm eq_dep}, and\\ |
312 | 1044 |
@{thm[mode=IfThen] eq_cp} |
1045 |
\end{tabular} |
|
1046 |
\end{isabelle} |
|
1047 |
||
1048 |
\noindent |
|
321 | 1049 |
This means again we do not have to recalculate the @{text RAG} and |
1050 |
also not the current precedences for the other threads. Since @{term th} is not |
|
312 | 1051 |
alive anymore in state @{term "s"}, there is no need to calculate its |
1052 |
current precedence. |
|
1053 |
\smallskip |
|
1054 |
*} |
|
1055 |
(*<*) |
|
1056 |
end |
|
311 | 1057 |
context step_set_cps |
1058 |
begin |
|
1059 |
(*>*) |
|
312 | 1060 |
text {* |
1061 |
\noindent |
|
321 | 1062 |
\colorbox{mygrey}{@{term "Set th prio"}:} We assume that @{text s'} and |
312 | 1063 |
@{term "s \<equiv> Set th prio#s'"} are both valid. We can show that |
311 | 1064 |
|
312 | 1065 |
\begin{isabelle}\ \ \ \ \ %%% |
1066 |
\begin{tabular}{@ {}l} |
|
321 | 1067 |
@{thm[mode=IfThen] eq_dep}, and\\ |
312 | 1068 |
@{thm[mode=IfThen] eq_cp} |
1069 |
\end{tabular} |
|
1070 |
\end{isabelle} |
|
311 | 1071 |
|
312 | 1072 |
\noindent |
321 | 1073 |
The first property is again telling us we do not need to change the @{text RAG}. The second |
1074 |
however states that only threads that are \emph{not} dependants of @{text th} have their |
|
312 | 1075 |
current precedence unchanged. For the others we have to recalculate the current |
1076 |
precedence. To do this we can start from @{term "th"} |
|
332 | 1077 |
and follow the @{term "depend"}-edges to recompute using Lemma~\ref{childrenlem} |
1078 |
the @{term "cp"} of every |
|
1079 |
thread encountered on the way. Since the @{term "depend"} |
|
335 | 1080 |
is assumed to be loop free, this procedure will always stop. The following two lemmas show, however, |
321 | 1081 |
that this procedure can actually stop often earlier without having to consider all |
1082 |
dependants. |
|
312 | 1083 |
|
1084 |
\begin{isabelle}\ \ \ \ \ %%% |
|
1085 |
\begin{tabular}{@ {}l} |
|
1086 |
@{thm[mode=IfThen] eq_up_self}\\ |
|
1087 |
@{text "If"} @{thm (prem 1) eq_up}, @{thm (prem 2) eq_up} and @{thm (prem 3) eq_up}\\ |
|
1088 |
@{text "then"} @{thm (concl) eq_up}. |
|
1089 |
\end{tabular} |
|
1090 |
\end{isabelle} |
|
1091 |
||
1092 |
\noindent |
|
332 | 1093 |
The first lemma states that if the current precedence of @{text th} is unchanged, |
312 | 1094 |
then the procedure can stop immediately (all dependent threads have their @{term cp}-value unchanged). |
1095 |
The second states that if an intermediate @{term cp}-value does not change, then |
|
1096 |
the procedure can also stop, because none of its dependent threads will |
|
1097 |
have their current precedence changed. |
|
1098 |
\smallskip |
|
311 | 1099 |
*} |
1100 |
(*<*) |
|
1101 |
end |
|
1102 |
context step_v_cps_nt |
|
1103 |
begin |
|
1104 |
(*>*) |
|
1105 |
text {* |
|
312 | 1106 |
\noindent |
321 | 1107 |
\colorbox{mygrey}{@{term "V th cs"}:} We assume that @{text s'} and |
312 | 1108 |
@{term "s \<equiv> V th cs#s'"} are both valid. We have to consider two |
1109 |
subcases: one where there is a thread to ``take over'' the released |
|
321 | 1110 |
resource @{text cs}, and one where there is not. Let us consider them |
312 | 1111 |
in turn. Suppose in state @{text s}, the thread @{text th'} takes over |
332 | 1112 |
resource @{text cs} from thread @{text th}. We can prove |
311 | 1113 |
|
1114 |
||
312 | 1115 |
\begin{isabelle}\ \ \ \ \ %%% |
1116 |
@{thm depend_s} |
|
1117 |
\end{isabelle} |
|
1118 |
||
1119 |
\noindent |
|
332 | 1120 |
which shows how the @{text RAG} needs to be changed. The next lemma suggests |
312 | 1121 |
how the current precedences need to be recalculated. For threads that are |
1122 |
not @{text "th"} and @{text "th'"} nothing needs to be changed, since we |
|
1123 |
can show |
|
1124 |
||
1125 |
\begin{isabelle}\ \ \ \ \ %%% |
|
1126 |
@{thm[mode=IfThen] cp_kept} |
|
1127 |
\end{isabelle} |
|
1128 |
||
1129 |
\noindent |
|
1130 |
For @{text th} and @{text th'} we need to use Lemma~\ref{childrenlem} to |
|
331 | 1131 |
recalculate their current precedence since their children have changed. *}(*<*)end context step_v_cps_nnt begin (*>*)text {* |
312 | 1132 |
\noindent |
1133 |
In the other case where there is no thread that takes over @{text cs}, we can show how |
|
1134 |
to recalculate the @{text RAG} and also show that no current precedence needs |
|
321 | 1135 |
to be recalculated. |
312 | 1136 |
|
1137 |
\begin{isabelle}\ \ \ \ \ %%% |
|
1138 |
\begin{tabular}{@ {}l} |
|
1139 |
@{thm depend_s}\\ |
|
1140 |
@{thm eq_cp} |
|
1141 |
\end{tabular} |
|
1142 |
\end{isabelle} |
|
311 | 1143 |
*} |
1144 |
(*<*) |
|
1145 |
end |
|
1146 |
context step_P_cps_e |
|
1147 |
begin |
|
1148 |
(*>*) |
|
1149 |
text {* |
|
312 | 1150 |
\noindent |
321 | 1151 |
\colorbox{mygrey}{@{term "P th cs"}:} We assume that @{text s'} and |
312 | 1152 |
@{term "s \<equiv> P th cs#s'"} are both valid. We again have to analyse two subcases, namely |
1153 |
the one where @{text cs} is locked, and where it is not. We treat the second case |
|
1154 |
first by showing that |
|
1155 |
||
1156 |
\begin{isabelle}\ \ \ \ \ %%% |
|
1157 |
\begin{tabular}{@ {}l} |
|
1158 |
@{thm depend_s}\\ |
|
1159 |
@{thm eq_cp} |
|
1160 |
\end{tabular} |
|
1161 |
\end{isabelle} |
|
311 | 1162 |
|
312 | 1163 |
\noindent |
1164 |
This means we do not need to add a holding edge to the @{text RAG} and no |
|
321 | 1165 |
current precedence needs to be recalculated.*}(*<*)end context step_P_cps_ne begin(*>*) text {* |
312 | 1166 |
\noindent |
331 | 1167 |
In the second case we know that resource @{text cs} is locked. We can show that |
312 | 1168 |
|
1169 |
\begin{isabelle}\ \ \ \ \ %%% |
|
1170 |
\begin{tabular}{@ {}l} |
|
1171 |
@{thm depend_s}\\ |
|
1172 |
@{thm[mode=IfThen] eq_cp} |
|
1173 |
\end{tabular} |
|
1174 |
\end{isabelle} |
|
311 | 1175 |
|
312 | 1176 |
\noindent |
1177 |
That means we have to add a waiting edge to the @{text RAG}. Furthermore |
|
321 | 1178 |
the current precedence for all threads that are not dependants of @{text th} |
1179 |
are unchanged. For the others we need to follow the edges |
|
312 | 1180 |
in the @{text RAG} and recompute the @{term "cp"}. However, like in the |
332 | 1181 |
case of @{text Set}, this operation can stop often earlier, namely when intermediate |
312 | 1182 |
values do not change. |
311 | 1183 |
*} |
1184 |
(*<*) |
|
1185 |
end |
|
1186 |
(*>*) |
|
1187 |
text {* |
|
312 | 1188 |
\noindent |
332 | 1189 |
As can be seen, a pleasing byproduct of our formalisation is that the properties in |
1190 |
this section closely inform an implementation of PIP, namely whether the |
|
321 | 1191 |
@{text RAG} needs to be reconfigured or current precedences need to |
332 | 1192 |
by recalculated for an event. This information is provided by the lemmas we proved. |
311 | 1193 |
*} |
1194 |
||
298
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
1195 |
section {* Conclusion *} |
f2e0d031a395
completed model section; vt has only state as argument
urbanc
parents:
297
diff
changeset
|
1196 |
|
300 | 1197 |
text {* |
314 | 1198 |
The Priority Inheritance Protocol (PIP) is a classic textbook |
332 | 1199 |
algorithm used in many real-time operating systems in order to avoid the problem of |
315 | 1200 |
Priority Inversion. Although classic and widely used, PIP does have |
317 | 1201 |
its faults: for example it does not prevent deadlocks in cases where threads |
315 | 1202 |
have circular lock dependencies. |
300 | 1203 |
|
317 | 1204 |
We had two goals in mind with our formalisation of PIP: One is to |
315 | 1205 |
make the notions in the correctness proof by Sha et al.~\cite{Sha90} |
317 | 1206 |
precise so that they can be processed by a theorem prover. The reason is |
1207 |
that a mechanically checked proof avoids the flaws that crept into their |
|
1208 |
informal reasoning. We achieved this goal: The correctness of PIP now |
|
315 | 1209 |
only hinges on the assumptions behind our formal model. The reasoning, which is |
314 | 1210 |
sometimes quite intricate and tedious, has been checked beyond any |
315 | 1211 |
reasonable doubt by Isabelle/HOL. We can also confirm that Paulson's |
321 | 1212 |
inductive method for protocol verification~\cite{Paulson98} is quite |
315 | 1213 |
suitable for our formal model and proof. The traditional application |
1214 |
area of this method is security protocols. The only other |
|
1215 |
application of Paulson's method we know of outside this area is |
|
1216 |
\cite{Wang09}. |
|
301 | 1217 |
|
317 | 1218 |
The second goal of our formalisation is to provide a specification for actually |
1219 |
implementing PIP. Textbooks, for example \cite[Section 5.6.5]{Vahalia96}, |
|
315 | 1220 |
explain how to use various implementations of PIP and abstractly |
332 | 1221 |
discuss their properties, but surprisingly lack most details important for a |
1222 |
programmer who wants to implement PIP (similarly Sha et al.~\cite{Sha90}). |
|
1223 |
That this is an issue in practice is illustrated by the |
|
315 | 1224 |
email from Baker we cited in the Introduction. We achieved also this |
317 | 1225 |
goal: The formalisation gives the first author enough data to enable |
1226 |
his undergraduate students to implement PIP (as part of their OS course) |
|
1227 |
on top of PINTOS, a small operating system for teaching |
|
315 | 1228 |
purposes. A byproduct of our formalisation effort is that nearly all |
314 | 1229 |
design choices for the PIP scheduler are backed up with a proved |
317 | 1230 |
lemma. We were also able to establish the property that the choice of |
1231 |
the next thread which takes over a lock is irrelevant for the correctness |
|
332 | 1232 |
of PIP. Earlier model checking approaches which verified particular implementations |
317 | 1233 |
of PIP \cite{Faria08,Jahier09,Wellings07} cannot |
1234 |
provide this kind of ``deep understanding'' about the principles behind |
|
1235 |
PIP and its correctness. |
|
315 | 1236 |
|
1237 |
PIP is a scheduling algorithm for single-processor systems. We are |
|
316 | 1238 |
now living in a multi-processor world. So the question naturally |
318 | 1239 |
arises whether PIP has any relevance in such a world beyond |
1240 |
teaching. Priority Inversion certainly occurs also in |
|
321 | 1241 |
multi-processor systems. However, the surprising answer, according |
1242 |
to \cite{Steinberg10}, is that except for one unsatisfactory |
|
1243 |
proposal nobody has a good idea for how PIP should be modified to |
|
1244 |
work correctly on multi-processor systems. The difficulties become |
|
333 | 1245 |
clear when considering the fact that releasing and re-locking a resource always |
321 | 1246 |
requires a small amount of time. If processes work independently, |
1247 |
then a low priority process can ``steal'' in such an unguarded |
|
332 | 1248 |
moment a lock for a resource that was supposed to allow a high-priority |
321 | 1249 |
process to run next. Thus the problem of Priority Inversion is not |
332 | 1250 |
really prevented by the classic PIP. It seems difficult to design a PIP-algorithm with |
321 | 1251 |
a meaningful correctness property on a multi-processor systems where |
1252 |
processes work independently. We can imagine PIP to be of use in |
|
1253 |
situations where processes are \emph{not} independent, but |
|
1254 |
coordinated via a master process that distributes work over some |
|
332 | 1255 |
slave processes. However, a formal investigation of this idea is beyond |
321 | 1256 |
the scope of this paper. We are not aware of any proofs in this |
332 | 1257 |
area, not even informal or flawed ones. |
265 | 1258 |
|
321 | 1259 |
The most closely related work to ours is the formal verification in |
332 | 1260 |
PVS of the Priority Ceiling Protocol done by Dutertre |
1261 |
\cite{dutertre99b}---another solution to the Priority Inversion |
|
1262 |
problem, which however needs |
|
1263 |
static analysis of programs in order to avoid it. |
|
1264 |
His formalisation consists of 407 lemmas and 2500 lines of (PVS) code. Our formalisation |
|
321 | 1265 |
consists of around 210 lemmas and overall 6950 lines of readable Isabelle/Isar |
1266 |
code with a few apply-scripts interspersed. The formal model of PIP |
|
1267 |
is 385 lines long; the formal correctness proof 3800 lines. Some auxiliary |
|
332 | 1268 |
definitions and proofs span over 770 lines of code. The properties relevant |
1269 |
for an implementation require 2000 lines. The code of our formalisation |
|
1270 |
can be downloaded from |
|
329 | 1271 |
\url{http://www.dcs.kcl.ac.uk/staff/urbanc/pip.html}. |
321 | 1272 |
|
1273 |
\bibliographystyle{plain} |
|
1274 |
\bibliography{root} |
|
262 | 1275 |
*} |
1276 |
||
264 | 1277 |
|
1278 |
(*<*) |
|
1279 |
end |
|
262 | 1280 |
(*>*) |