3 imports Precedence_ord Moment |
3 imports Precedence_ord Moment |
4 begin |
4 begin |
5 (*>*) |
5 (*>*) |
6 |
6 |
7 text {* |
7 text {* |
8 In this section, the formal model of Priority Inheritance is presented. |
8 In this section, the formal model of Priority Inheritance Protocol (PIP) is presented. |
9 The model is based on Paulson's inductive protocol verification method, where |
9 The model is based on Paulson's inductive protocol verification method, where |
10 the state of the system is modelled as a list of events happened so far with the latest |
10 the state of the system is modelled as a list of events happened so far with the latest |
11 event put at the head. |
11 event put at the head. |
12 |
12 *} |
|
13 |
|
14 text {* |
13 To define events, the identifiers of {\em threads}, |
15 To define events, the identifiers of {\em threads}, |
14 {\em priority} and {\em critical resources } (abbreviated as @{text "cs"}) |
16 {\em priority} and {\em critical resources } (abbreviated as @{text "cs"}) |
15 need to be represented. All three are represetned using standard |
17 need to be represented. All three are represetned using standard |
16 Isabelle/HOL type @{typ "nat"}: |
18 Isabelle/HOL type @{typ "nat"}: |
17 *} |
19 *} |
20 type_synonym priority = nat -- {* Type for priorities. *} |
22 type_synonym priority = nat -- {* Type for priorities. *} |
21 type_synonym cs = nat -- {* Type for critical sections (or critical resources). *} |
23 type_synonym cs = nat -- {* Type for critical sections (or critical resources). *} |
22 |
24 |
23 text {* |
25 text {* |
24 \noindent |
26 \noindent |
25 Every event in the system corresponds to a system call, the formats of which are |
27 The abstraction of Priority Inheritance Protocol (PIP) is set at the system call level. |
|
28 Every system call is represented as an event. The format of events is defined |
26 defined as follows: |
29 defined as follows: |
27 *} |
30 *} |
28 |
31 |
29 datatype event = |
32 datatype event = |
30 Create thread priority | -- {* Thread @{text "thread"} is created with priority @{text "priority"}. *} |
33 Create thread priority | -- {* Thread @{text "thread"} is created with priority @{text "priority"}. *} |
31 Exit thread | -- {* Thread @{text "thread"} finishing its execution. *} |
34 Exit thread | -- {* Thread @{text "thread"} finishing its execution. *} |
32 P thread cs | -- {* Thread @{text "thread"} requesting critical resource @{text "cs"}. *} |
35 P thread cs | -- {* Thread @{text "thread"} requesting critical resource @{text "cs"}. *} |
33 V thread cs | -- {* Thread @{text "thread"} releasing critical resource @{text "cs"}. *} |
36 V thread cs | -- {* Thread @{text "thread"} releasing critical resource @{text "cs"}. *} |
34 Set thread priority -- {* Thread @{text "thread"} resets its priority to @{text "priority"}. *} |
37 Set thread priority -- {* Thread @{text "thread"} resets its priority to @{text "priority"}. *} |
35 |
38 |
|
39 |
|
40 text {* |
|
41 As mentioned earlier, in Paulson's inductive method, the states of system are represented as lists of events, |
|
42 which is defined by the following type @{text "state"}: |
|
43 *} |
|
44 type_synonym state = "event list" |
|
45 |
|
46 |
36 text {* |
47 text {* |
37 \noindent |
48 \noindent |
38 Resource Allocation Graph (RAG for short) is used extensively in our formal analysis. |
49 Resource Allocation Graph (RAG for short) is used extensively in our formal analysis. |
39 The following type @{text "node"} is used to represent nodes in RAG. |
50 The following type @{text "node"} is used to represent nodes in RAG. |
40 *} |
51 *} |
41 datatype node = |
52 datatype node = |
42 Th "thread" | -- {* Node for thread. *} |
53 Th "thread" | -- {* Node for thread. *} |
43 Cs "cs" -- {* Node for critical resource. *} |
54 Cs "cs" -- {* Node for critical resource. *} |
44 |
|
45 text {* |
|
46 In Paulson's inductive method, the states of system are represented as lists of events, |
|
47 which is defined by the following type @{text "state"}: |
|
48 *} |
|
49 type_synonym state = "event list" |
|
50 |
55 |
51 text {* |
56 text {* |
52 \noindent |
57 \noindent |
53 The following function |
58 The following function |
54 @{text "threads"} is used to calculate the set of live threads (@{text "threads s"}) |
59 @{text "threads"} is used to calculate the set of live threads (@{text "threads s"}) |
62 "threads (Create thread prio#s) = {thread} \<union> threads s" | |
67 "threads (Create thread prio#s) = {thread} \<union> threads s" | |
63 -- {* Finished thread is removed: *} |
68 -- {* Finished thread is removed: *} |
64 "threads (Exit thread # s) = (threads s) - {thread}" | |
69 "threads (Exit thread # s) = (threads s) - {thread}" | |
65 -- {* Other kind of events does not affect the value of @{text "threads"}: *} |
70 -- {* Other kind of events does not affect the value of @{text "threads"}: *} |
66 "threads (e#s) = threads s" |
71 "threads (e#s) = threads s" |
|
72 |
67 text {* \noindent |
73 text {* \noindent |
68 Functions such as @{text "threads"}, which extract information out of system states, are called |
74 Functions such as @{text "threads"}, which extract information out of system states, are called |
69 {\em observing functions}. A series of observing functions will be defined in the sequel in order to |
75 {\em observing functions}. A series of observing functions will be defined in the sequel in order to |
70 model the protocol. |
76 model the protocol. |
71 Observing function @{text "priority"} calculates |
77 Observing function @{text "priority"} calculates |
102 "last_set thread (_#s) = last_set thread s" |
108 "last_set thread (_#s) = last_set thread s" |
103 |
109 |
104 text {* |
110 text {* |
105 \noindent |
111 \noindent |
106 The {\em precedence} is a notion derived from {\em priority}, where the {\em precedence} of |
112 The {\em precedence} is a notion derived from {\em priority}, where the {\em precedence} of |
107 a thread is the combination of its {\em original priority} and {\em birth time}. The intention is |
113 a thread is the combination of its {\em original priority} and {\em time} the priority is set. |
108 to discriminate threads with the same priority by giving threads whose priority |
114 The intention is to discriminate threads with the same priority by giving threads whose priority |
109 is assigned earlier higher precedences, becasue such threads are more urgent to finish. |
115 is assigned earlier higher precedences, becasue such threads are more urgent to finish. |
110 This explains the following definition: |
116 This explains the following definition: |
111 *} |
117 *} |
112 definition preced :: "thread \<Rightarrow> state \<Rightarrow> precedence" |
118 definition preced :: "thread \<Rightarrow> state \<Rightarrow> precedence" |
113 where "preced thread s \<equiv> Prc (priority thread s) (last_set thread s)" |
119 where "preced thread s \<equiv> Prc (priority thread s) (last_set thread s)" |
114 |
120 |
115 |
121 |
116 text {* |
122 text {* |
117 \noindent |
123 \noindent |
118 A number of important notions are defined here: |
124 A number of important notions in PIP are represented as functions defined |
|
125 in terms of the waiting queues of the system, where the waiting queues |
|
126 , as a whole, is represented by the @{text "wq"} argument of every notion function. |
|
127 The @{text "wq"} argument is itself a functions which |
|
128 maps every critical resource @{text "cs"} to the list of threads which are holding or waiting for it. |
|
129 The thread at the head of this list is designated as the thread which is current |
|
130 holding the resrouce, which is slightly different from tradition where |
|
131 all threads in the waiting queue are considered as waiting for the resource. |
119 *} |
132 *} |
120 |
133 |
121 consts |
134 consts |
122 holding :: "'b \<Rightarrow> thread \<Rightarrow> cs \<Rightarrow> bool" |
135 holding :: "'b \<Rightarrow> thread \<Rightarrow> cs \<Rightarrow> bool" |
123 waiting :: "'b \<Rightarrow> thread \<Rightarrow> cs \<Rightarrow> bool" |
136 waiting :: "'b \<Rightarrow> thread \<Rightarrow> cs \<Rightarrow> bool" |
124 RAG :: "'b \<Rightarrow> (node \<times> node) set" |
137 RAG :: "'b \<Rightarrow> (node \<times> node) set" |
125 dependants :: "'b \<Rightarrow> thread \<Rightarrow> thread set" |
138 dependants :: "'b \<Rightarrow> thread \<Rightarrow> thread set" |
126 |
139 |
127 text {* |
|
128 \noindent |
|
129 In the definition of the following several functions, it is supposed that |
|
130 the waiting queue of every critical resource is given by a waiting queue |
|
131 function @{text "wq"}, which servers as arguments of these functions. |
|
132 *} |
|
133 defs (overloaded) |
140 defs (overloaded) |
134 -- {* |
141 -- {* |
135 \begin{minipage}{0.9\textwidth} |
142 \begin{minipage}{0.9\textwidth} |
136 We define that the thread which is at the head of waiting queue of resource @{text "cs"} |
143 This meaning of @{text "wq"} is reflected in the following definition of @{text "holding wq th cs"}, |
137 is holding the resource. This definition is slightly different from tradition where |
144 where @{text "holding wq th cs"} means thread @{text "th"} is holding the critical |
138 all threads in the waiting queue are considered as waiting for the resource. |
145 resource @{text "cs"}. This decision is based on @{text "wq"}. |
139 This notion is reflected in the definition of @{text "holding wq th cs"} as follows: |
146 \end{minipage} |
140 \end{minipage} |
147 *} |
141 *} |
148 |
142 cs_holding_def: |
149 cs_holding_def: |
143 "holding wq thread cs \<equiv> (thread \<in> set (wq cs) \<and> thread = hd (wq cs))" |
150 "holding wq thread cs \<equiv> (thread \<in> set (wq cs) \<and> thread = hd (wq cs))" |
144 -- {* |
151 -- {* |
145 \begin{minipage}{0.9\textwidth} |
152 \begin{minipage}{0.9\textwidth} |
146 In accordance with the definition of @{text "holding wq th cs"}, |
153 In accordance with the definition of @{text "holding wq th cs"}, |
151 *} |
158 *} |
152 cs_waiting_def: |
159 cs_waiting_def: |
153 "waiting wq thread cs \<equiv> (thread \<in> set (wq cs) \<and> thread \<noteq> hd (wq cs))" |
160 "waiting wq thread cs \<equiv> (thread \<in> set (wq cs) \<and> thread \<noteq> hd (wq cs))" |
154 -- {* |
161 -- {* |
155 \begin{minipage}{0.9\textwidth} |
162 \begin{minipage}{0.9\textwidth} |
156 @{text "RAG wq"} represents the Resource Allocation Graph of the system under the waiting |
163 @{text "RAG wq"} generates RAG (a binary relations on @{text "node"}) |
157 queue function @{text "wq"}. |
164 out of waiting queues of the system (represented by the @{text "wq"} argument): |
158 \end{minipage} |
165 \end{minipage} |
159 *} |
166 *} |
160 cs_RAG_def: |
167 cs_RAG_def: |
161 "RAG (wq::cs \<Rightarrow> thread list) \<equiv> |
168 "RAG (wq::cs \<Rightarrow> thread list) \<equiv> |
162 {(Th th, Cs cs) | th cs. waiting wq th cs} \<union> {(Cs cs, Th th) | cs th. holding wq th cs}" |
169 {(Th th, Cs cs) | th cs. waiting wq th cs} \<union> {(Cs cs, Th th) | cs th. holding wq th cs}" |
163 -- {* |
170 -- {* |
164 \begin{minipage}{0.9\textwidth} |
171 \begin{minipage}{0.9\textwidth} |
165 The following @{text "dependants wq th"} represents the set of threads which are RAGing on |
172 The following @{text "dependants wq th"} represents the set of threads which are RAGing on |
166 thread @{text "th"} in Resource Allocation Graph @{text "RAG wq"}: |
173 thread @{text "th"} in Resource Allocation Graph @{text "RAG wq"}. |
|
174 Here, "RAGing" means waiting directly or indirectly on the critical resource. |
167 \end{minipage} |
175 \end{minipage} |
168 *} |
176 *} |
169 cs_dependants_def: |
177 cs_dependants_def: |
170 "dependants (wq::cs \<Rightarrow> thread list) th \<equiv> {th' . (Th th', Th th) \<in> (RAG wq)^+}" |
178 "dependants (wq::cs \<Rightarrow> thread list) th \<equiv> {th' . (Th th', Th th) \<in> (RAG wq)^+}" |
171 |
179 |
|
180 (* I stop here for the moment ccc *) |
172 |
181 |
173 text {* |
182 text {* |
174 The data structure used by the operating system for scheduling is referred to as |
183 The data structure used by the operating system for scheduling is referred to as |
175 {\em schedule state}. It is represented as a record consisting of |
184 {\em schedule state}. It is represented as a record consisting of |
176 a function assigning waiting queue to resources and a function assigning precedence to |
185 a function assigning waiting queue to resources and a function assigning precedence to |