prio/PrioGDef.thy
changeset 294 bc5bf9e9ada2
parent 291 5ef9f6ebe827
child 295 8e4a5fff2eda
equal deleted inserted replaced
293:cab43c4a96d2 294:bc5bf9e9ada2
   174   {\em schedule state}. It is represented as a record consisting of 
   174   {\em schedule state}. It is represented as a record consisting of 
   175   a function assigning waiting queue to resources and a function assigning precedence to 
   175   a function assigning waiting queue to resources and a function assigning precedence to 
   176   threads:
   176   threads:
   177   *}
   177   *}
   178 record schedule_state = 
   178 record schedule_state = 
   179     waiting_queue :: "cs \<Rightarrow> thread list" -- {* The function assigning waiting queue. *}
   179     wq_fun :: "cs \<Rightarrow> thread list" -- {* The function assigning waiting queue. *}
   180     cur_preced :: "thread \<Rightarrow> precedence" -- {* The function assigning precedence. *}
   180     cprec_fun :: "thread \<Rightarrow> precedence" -- {* The function assigning precedence. *}
   181 
   181 
   182 text {* \noindent 
   182 text {* \noindent 
   183   The following
   183   The following
   184   @{text "cpreced s th"} gives the {\em current precedence} of thread @{text "th"} under
   184   @{text "cpreced s th"} gives the {\em current precedence} of thread @{text "th"} under
   185   state @{text "s"}. The definition of @{text "cpreced"} reflects the basic idea of 
   185   state @{text "s"}. The definition of @{text "cpreced"} reflects the basic idea of 
   218   The following function @{text "schs"} is used to calculate the schedule state @{text "schs s"}.
   218   The following function @{text "schs"} is used to calculate the schedule state @{text "schs s"}.
   219   It is the key function to model Priority Inheritance:
   219   It is the key function to model Priority Inheritance:
   220   *}
   220   *}
   221 fun schs :: "state \<Rightarrow> schedule_state"
   221 fun schs :: "state \<Rightarrow> schedule_state"
   222   where 
   222   where 
   223   "schs [] = (| waiting_queue = \<lambda> cs. [],  cur_preced = (\<lambda>_. Prc 0 0) |)" |
   223   "schs [] = (| wq_fun = \<lambda> cs. [],  cprec_fun = (\<lambda>_. Prc 0 0) |)" |
   224 
   224 
   225   -- {*
   225   -- {*
   226   \begin{minipage}{0.9\textwidth}
   226   \begin{minipage}{0.9\textwidth}
   227   \begin{enumerate}
   227   \begin{enumerate}
   228   \item @{text "ps"} is the schedule state of last moment.
   228   \item @{text "ps"} is the schedule state of last moment.
   246         put them in the same record so that they can evolve together.
   246         put them in the same record so that they can evolve together.
   247   \end{enumerate}
   247   \end{enumerate}
   248   \end{minipage}
   248   \end{minipage}
   249      *}
   249      *}
   250    "schs (Create th prio # s) = 
   250    "schs (Create th prio # s) = 
   251        (let wq = waiting_queue (schs s) in
   251        (let wq = wq_fun (schs s) in
   252           (|waiting_queue = wq, cur_preced = cpreced wq (Create th prio # s)|))"
   252           (|wq_fun = wq, cprec_fun = cpreced wq (Create th prio # s)|))"
   253 |  "schs (Exit th # s) = 
   253 |  "schs (Exit th # s) = 
   254        (let wq = waiting_queue (schs s) in
   254        (let wq = wq_fun (schs s) in
   255           (|waiting_queue = wq, cur_preced = cpreced wq (Exit th # s)|))"
   255           (|wq_fun = wq, cprec_fun = cpreced wq (Exit th # s)|))"
   256 |  "schs (Set th prio # s) = 
   256 |  "schs (Set th prio # s) = 
   257        (let wq = waiting_queue (schs s) in
   257        (let wq = wq_fun (schs s) in
   258           (|waiting_queue = wq, cur_preced = cpreced wq (Set th prio # s)|))"
   258           (|wq_fun = wq, cprec_fun = cpreced wq (Set th prio # s)|))"
   259 |  "schs (P th cs # s) = 
   259 |  "schs (P th cs # s) = 
   260        (let wq = waiting_queue (schs s) in
   260        (let wq = wq_fun (schs s) in
   261         let new_wq = wq(cs := (wq cs @ [th])) in
   261         let new_wq = wq(cs := (wq cs @ [th])) in
   262           (|waiting_queue = new_wq, cur_preced = cpreced new_wq (P th cs # s)|))"
   262           (|wq_fun = new_wq, cprec_fun = cpreced new_wq (P th cs # s)|))"
   263 |  "schs (V th cs # s) = 
   263 |  "schs (V th cs # s) = 
   264        (let wq = waiting_queue (schs s) in
   264        (let wq = wq_fun (schs s) in
   265         let new_wq = wq(cs := release (wq cs)) in
   265         let new_wq = wq(cs := release (wq cs)) in
   266           (|waiting_queue = new_wq, cur_preced = cpreced new_wq (V th cs # s)|))"
   266           (|wq_fun = new_wq, cprec_fun = cpreced new_wq (V th cs # s)|))"
   267 
   267 
   268 lemma cpreced_initial: 
   268 lemma cpreced_initial: 
   269   "cpreced (\<lambda> cs. []) [] = (\<lambda>_. (Prc 0 0))"
   269   "cpreced (\<lambda> cs. []) [] = (\<lambda>_. (Prc 0 0))"
   270 apply(simp add: cpreced_def)
   270 apply(simp add: cpreced_def)
   271 apply(simp add: cs_dependents_def cs_depend_def cs_waiting_def cs_holding_def)
   271 apply(simp add: cs_dependents_def cs_depend_def cs_waiting_def cs_holding_def)
   272 apply(simp add: preced_def)
   272 apply(simp add: preced_def)
   273 done
   273 done
   274 
   274 
   275 lemma sch_old_def:
   275 lemma sch_old_def:
   276   "schs (e#s) = (let ps = schs s in 
   276   "schs (e#s) = (let ps = schs s in 
   277                   let pwq = waiting_queue ps in 
   277                   let pwq = wq_fun ps in 
   278                   let nwq = case e of
   278                   let nwq = case e of
   279                              P th cs \<Rightarrow>  pwq(cs:=(pwq cs @ [th])) |
   279                              P th cs \<Rightarrow>  pwq(cs:=(pwq cs @ [th])) |
   280                              V th cs \<Rightarrow> let nq = case (pwq cs) of
   280                              V th cs \<Rightarrow> let nq = case (pwq cs) of
   281                                                       [] \<Rightarrow> [] | 
   281                                                       [] \<Rightarrow> [] | 
   282                                                       (_#qs) \<Rightarrow> (SOME q. distinct q \<and> set q = set qs)
   282                                                       (_#qs) \<Rightarrow> (SOME q. distinct q \<and> set q = set qs)
   283                                             in pwq(cs:=nq)                 |
   283                                             in pwq(cs:=nq)                 |
   284                               _ \<Rightarrow> pwq
   284                               _ \<Rightarrow> pwq
   285                   in let ncp = cpreced nwq (e#s) in 
   285                   in let ncp = cpreced nwq (e#s) in 
   286                      \<lparr>waiting_queue = nwq, cur_preced = ncp\<rparr>
   286                      \<lparr>wq_fun = nwq, cprec_fun = ncp\<rparr>
   287                  )"
   287                  )"
   288 apply(cases e)
   288 apply(cases e)
   289 apply(simp_all)
   289 apply(simp_all)
   290 done
   290 done
   291 
   291 
   292 
   292 
   293 text {* 
   293 text {* 
   294   \noindent
   294   \noindent
   295   The following @{text "wq"} is a shorthand for @{text "waiting_queue"}. 
   295   The following @{text "wq"} is a shorthand for @{text "wq_fun"}. 
   296   *}
   296   *}
   297 definition wq :: "state \<Rightarrow> cs \<Rightarrow> thread list" 
   297 definition wq :: "state \<Rightarrow> cs \<Rightarrow> thread list" 
   298   where "wq s = waiting_queue (schs s)"
   298   where "wq s = wq_fun (schs s)"
   299 
   299 
   300 text {* \noindent 
   300 text {* \noindent 
   301   The following @{text "cp"} is a shorthand for @{text "cur_preced"}. 
   301   The following @{text "cp"} is a shorthand for @{text "cprec_fun"}. 
   302   *}
   302   *}
   303 definition cp :: "state \<Rightarrow> thread \<Rightarrow> precedence"
   303 definition cp :: "state \<Rightarrow> thread \<Rightarrow> precedence"
   304   where "cp s = cur_preced (schs s)"
   304   where "cp s = cprec_fun (schs s)"
   305 
   305 
   306 text {* \noindent
   306 text {* \noindent
   307   Functions @{text "holding"}, @{text "waiting"}, @{text "depend"} and 
   307   Functions @{text "holding"}, @{text "waiting"}, @{text "depend"} and 
   308   @{text "dependents"} still have the 
   308   @{text "dependents"} still have the 
   309   same meaning, but redefined so that they no longer depend on the 
   309   same meaning, but redefined so that they no longer depend on the