PIPDefs.thy
changeset 130 0f124691c191
parent 129 e3cf792db636
child 136 fb3f52fe99d1
equal deleted inserted replaced
129:e3cf792db636 130:0f124691c191
     1 (*<*)
     1 (*<*)
     2 theory PIPDefs
     2 theory PIPDefs
     3 imports Precedence_ord RTree Max
     3 imports Precedence_ord Max
     4 begin
     4 begin
     5 (*>*)
     5 (*>*)
     6 
     6 
     7 chapter {* Definitions *}
     7 chapter {* Definitions *}
     8 
     8 
     9 text {*
     9 text {*
    10 
    10 
    11   In this section, the formal model of Priority Inheritance Protocol (PIP)
    11   In this chapter, the formal model of the Priority Inheritance Protocol
    12   is presented. The model is based on Paulson's inductive protocol
    12   (PIP) is presented. The model is based on Paulson's inductive protocol
    13   verification method, where the state of the system is modelled as a list
    13   verification method, where the state of the system is modelled as a list
    14   of events (trace) happened so far with the latest event put at the head.
    14   of events (trace) happened so far with the latest event put at the head.
    15   *}
    15   *}
    16 
    16 
    17 text {*
    17 text {*
    18 
    18 
    19   To define events, the identifiers of {\em threads}, {\em priority} and
    19   To define events, the identifiers of {\em threads}, {\em priority} and
    20   {\em critical resources } (abbreviated as @{text "cs"}) need to be
    20   {\em critical resources } (abbreviated as @{text "cs"}) need to be
    21   represented. All three are represetned using standard Isabelle/HOL type
    21   represented. All three are represented using standard Isabelle/HOL type
    22   @{typ "nat"}: *}
    22   @{typ "nat"}: *}
    23 
    23 
    24 type_synonym thread = nat -- {* Type for thread identifiers. *}
    24 type_synonym thread = nat    -- {* Type for thread identifiers. *}
    25 type_synonym priority = nat  -- {* Type for priorities. *}
    25 type_synonym priority = nat  -- {* Type for priorities. *}
    26 type_synonym cs = nat -- {* Type for critical sections (or critical resources). *}
    26 type_synonym cs = nat        -- {* Type for critical sections (or critical resources). *}
    27 
    27 
    28 text {*
    28 text {*
    29 
    29 
    30   \noindent The abstraction of Priority Inheritance Protocol (PIP) is set at
    30   \noindent The abstraction of Priority Inheritance Protocol (PIP) is set at
    31   the system call level. Every system call is represented as an event. The
    31   the system call level. Every system call is represented as an event. The
    36 | Exit thread              -- {* Thread @{text "thread"} finishing its execution. *}
    36 | Exit thread              -- {* Thread @{text "thread"} finishing its execution. *}
    37 | P thread cs              -- {* Thread @{text "thread"} requesting critical resource @{text "cs"}. *}
    37 | P thread cs              -- {* Thread @{text "thread"} requesting critical resource @{text "cs"}. *}
    38 | V thread cs              -- {* Thread @{text "thread"} releasing critical resource @{text "cs"}. *}
    38 | V thread cs              -- {* Thread @{text "thread"} releasing critical resource @{text "cs"}. *}
    39 | Set thread priority      -- {* Thread @{text "thread"} resets its priority to @{text "priority"}. *}
    39 | Set thread priority      -- {* Thread @{text "thread"} resets its priority to @{text "priority"}. *}
    40 
    40 
    41 fun actor  where
    41 
    42   "actor (Exit th) = th" |
    42 
    43   "actor (P th cs) = th" |
    43 text {* 
    44   "actor (V th cs) = th" |
    44   
    45   "actor (Set th pty) = th" |
    45   As mentioned earlier, in Paulson's inductive method, the states of the
    46   "actor (Create th prio) = th"
    46   system are represented as lists of events, which is defined by the
    47 
    47   following type @{text "state"}: *}
    48 -- {* The actions of a set of threads *}
       
    49 definition "actions_of ths s = filter (\<lambda> e. actor e \<in> ths) s"
       
    50 
       
    51 fun isCreate :: "event \<Rightarrow> bool" where
       
    52   "isCreate (Create th pty) = True" |
       
    53   "isCreate _ = False"
       
    54 
       
    55 fun isP :: "event \<Rightarrow> bool" where
       
    56   "isP (P th cs) = True" |
       
    57   "isP _ = False"
       
    58 
       
    59 fun isV :: "event \<Rightarrow> bool" where
       
    60   "isV (V th cs) = True" |
       
    61   "isV _ = False"
       
    62 
       
    63 text {* 
       
    64   
       
    65   As mentioned earlier, in Paulson's inductive method, the states of system
       
    66   are represented as lists of events, which is defined by the following type
       
    67   @{text "state"}: *}
       
    68 
    48 
    69 type_synonym state = "event list"
    49 type_synonym state = "event list"
    70 
    50 
    71 
    51 
    72 text {*
    52 text {*
    89 
    69 
    90   \noindent The function @{text "threads"} is one of the {\em observation
    70   \noindent The function @{text "threads"} is one of the {\em observation
    91   function}s which forms the very basis of Paulson's inductive protocol
    71   function}s which forms the very basis of Paulson's inductive protocol
    92   verification method. Each observation function {\em observes} one
    72   verification method. Each observation function {\em observes} one
    93   particular aspect (or attribute) of the system. For example, the attribute
    73   particular aspect (or attribute) of the system. For example, the attribute
    94   observed by @{text "threads s"} is the set of threads living in state
    74   observed by @{text "threads s"} is the set of threads being live in state
    95   @{text "s"}. The protocol being modelled The decision made the protocol
    75   @{text "s"}. The protocol being modelled The decision made the protocol
    96   being modelled is based on the {\em observation}s returned by {\em
    76   being modelled is based on the {\em observation}s returned by {\em
    97   observation function}s. Since {\observation function}s forms the very
    77   observation function}s. Since {\observation function}s forms the very
    98   basis on which Paulson's inductive method is based, there will be a lot of
    78   basis on which Paulson's inductive method is based, there will be a lot of
    99   such observation functions introduced in the following. In fact, any
    79   such observation functions introduced in the following. In fact, any
   100   function which takes event list as argument is a {\em observation
    80   function which takes event list as argument is a {\em observation
   101   function}. *}
    81   function}. *}
   102 
    82 
   103 text {* 
    83 text {* 
   104 
    84 
   105   \noindent Observation @{text "priority th s"} is the {\em original
    85   Observation @{text "priority th s"} is the {\em original priority} of
   106   priority} of thread @{text "th"} in state @{text "s"}. The {\em original
    86   thread @{text "th"} in state @{text "s"}. The {\em original priority} is
   107   priority} is the priority assigned to a thread when it is created or when
    87   the priority assigned to a thread when it is created or when it is reset
   108   it is reset by system call (represented by event @{text "Set thread
    88   by system call (represented by event @{text "Set thread priority"}). *}
   109   priority"}). *}
       
   110 
    89 
   111 fun priority :: "thread \<Rightarrow> state \<Rightarrow> priority"
    90 fun priority :: "thread \<Rightarrow> state \<Rightarrow> priority"
   112   where
    91   where
   113   -- {* @{text "0"} is assigned to threads which have never been created: *}
    92   -- {* @{text "0"} is assigned to threads which have never been created: *}
   114   "priority th [] = 0" |
    93   "priority th [] = 0" |
   207       {(Th th, Cs cs) | th cs. waiting_raw wq th cs} \<union> 
   186       {(Th th, Cs cs) | th cs. waiting_raw wq th cs} \<union> 
   208       {(Cs cs, Th th) | cs th. holding_raw wq th cs}"
   187       {(Cs cs, Th th) | cs th. holding_raw wq th cs}"
   209 
   188 
   210 text {*
   189 text {*
   211  
   190  
   212   \begin{minipage}{0.9\textwidth} The following @{text "dependants wq th"}
   191   \noindent The following @{text "dependants wq th"} represents the set of
   213   represents the set of threads which are RAGing on thread @{text "th"} in
   192   threads which are waiting on thread @{text "th"} in Resource Allocation
   214   Resource Allocation Graph @{text "RAG wq"}. Here, "RAGing" means waiting
   193   Graph @{text "RAG wq"}. Here, "waiting" means waiting directly or
   215   directly or indirectly on the critical resource. \end{minipage} *}
   194   indirectly on the critical resource. *}
   216 
   195 
   217 definition
   196 definition
   218   dependants_raw :: "(cs \<Rightarrow> thread list) \<Rightarrow> thread \<Rightarrow> thread set"
   197   dependants_raw :: "(cs \<Rightarrow> thread list) \<Rightarrow> thread \<Rightarrow> thread set"
   219 where
   198 where
   220   "dependants_raw wq th \<equiv> {th' . (Th th', Th th) \<in> (RAG_raw wq)^+}"
   199   "dependants_raw wq th \<equiv> {th' . (Th th', Th th) \<in> (RAG_raw wq)^+}"
   231   precedence, i.e. @{text "preced th s"}. *}
   210   precedence, i.e. @{text "preced th s"}. *}
   232 
   211 
   233 definition 
   212 definition 
   234   cpreced :: "(cs \<Rightarrow> thread list) \<Rightarrow> state \<Rightarrow> thread \<Rightarrow> precedence"
   213   cpreced :: "(cs \<Rightarrow> thread list) \<Rightarrow> state \<Rightarrow> thread \<Rightarrow> precedence"
   235   where 
   214   where 
   236   "cpreced wq s th = Max ((\<lambda>th'. preced th' s) ` ({th} \<union> dependants_raw wq th))"
   215   "cpreced wq s th \<equiv> Max ({preced th s} \<union> preceds (dependants_raw wq th) s)"
       
   216 
       
   217 
   237 
   218 
   238 text {*
   219 text {*
   239 
   220 
   240   Notice that the current precedence (@{text "cpreced"}) of one thread
   221   Notice that the current precedence (@{text "cpreced"}) of one thread
   241   @{text "th"} can be boosted (increased) by those threads in the @{text
   222   @{text "th"} can be boosted (increased) by those threads in the @{text
   243   the priority (or, more precisely, the precedence) of its dependants. This
   224   the priority (or, more precisely, the precedence) of its dependants. This
   244   is whrere the word "Inheritance" in Priority Inheritance Protocol comes
   225   is whrere the word "Inheritance" in Priority Inheritance Protocol comes
   245   from. *}
   226   from. *}
   246 
   227 
   247 lemma cpreced_def2:
   228 lemma cpreced_def2:
   248   "cpreced wq s th \<equiv> Max ({preced th s} \<union> preceds (dependants_raw wq th) s)"
   229   "cpreced wq s th \<equiv> Max ((\<lambda>th'. preced th' s) ` ({th} \<union> dependants_raw wq th))"
   249   unfolding cpreced_def image_def preceds_def
   230   unfolding cpreced_def image_def preceds_def
   250   apply(rule eq_reflection)
   231   apply(rule eq_reflection)
   251   apply(rule_tac f="Max" in arg_cong)
   232   apply(rule_tac f="Max" in arg_cong)
   252   by (auto)
   233   by (auto)
       
   234 
       
   235 definition 
       
   236   cpreceds :: "(cs \<Rightarrow> thread list) \<Rightarrow> state \<Rightarrow> thread set \<Rightarrow> precedence set"
       
   237   where 
       
   238   "cpreceds wq s ths \<equiv> {cpreced wq s th | th. th \<in> ths}"
       
   239 
   253 
   240 
   254 text {* 
   241 text {* 
   255 
   242 
   256   \noindent Assuming @{text "qs"} be the waiting queue of a critical
   243   \noindent Assuming @{text "qs"} be the waiting queue of a critical
   257   resource, the following abbreviation "release qs" is the waiting queue
   244   resource, the following abbreviation "release qs" is the waiting queue
   390 lemma cpreced_initial: 
   377 lemma cpreced_initial: 
   391   "cpreced (\<lambda> cs. []) [] = (\<lambda>_. (Prc 0 0))"
   378   "cpreced (\<lambda> cs. []) [] = (\<lambda>_. (Prc 0 0))"
   392 apply(rule ext)
   379 apply(rule ext)
   393 apply(simp add: cpreced_def)
   380 apply(simp add: cpreced_def)
   394 apply(simp add: dependants_raw_def RAG_raw_def waiting_raw_def holding_raw_def)
   381 apply(simp add: dependants_raw_def RAG_raw_def waiting_raw_def holding_raw_def)
   395 apply(simp add: preced_def)
   382 apply(simp add: preced_def preceds_def)
   396 done
   383 done
   397 
   384 
   398 text {* 
   385 text {* 
   399 
   386 
   400   \noindent The following @{text "wq"} is a shorthand for @{text "wq_fun"}.
   387   \noindent The following @{text "wq"} is a shorthand for @{text "wq_fun"}.
   418   that they no longer RAG on the fictitious {\em waiting queue function}
   405   that they no longer RAG on the fictitious {\em waiting queue function}
   419   @{text "wq"}, but on system state @{text "s"}. *}
   406   @{text "wq"}, but on system state @{text "s"}. *}
   420 
   407 
   421 definition 
   408 definition 
   422   s_holding_abv: 
   409   s_holding_abv: 
   423   "holding (s::state) \<equiv> holding_raw (wq_fun (schs s))"
   410   "holding (s::state) \<equiv> holding_raw (wq s)"
   424 
   411 
   425 definition
   412 definition
   426   s_waiting_abv: 
   413   s_waiting_abv: 
   427   "waiting (s::state) \<equiv> waiting_raw (wq_fun (schs s))"
   414   "waiting (s::state) \<equiv> waiting_raw (wq s)"
   428 
   415 
   429 definition
   416 definition
   430   s_RAG_abv: 
   417   s_RAG_abv: 
   431   "RAG (s::state) \<equiv> RAG_raw (wq_fun (schs s))"
   418   "RAG (s::state) \<equiv> RAG_raw (wq s)"
   432   
   419   
   433 definition
   420 definition
   434   s_dependants_abv: 
   421   s_dependants_abv: 
   435   "dependants (s::state) \<equiv> dependants_raw (wq_fun (schs s))"
   422   "dependants (s::state) \<equiv> dependants_raw (wq s)"
   436 
       
   437 text {* 
       
   438 
       
   439   The following four lemmas relate the @{term wq} and non-@{term wq}
       
   440   versions of @{term waiting}, @{term holding}, @{term dependants} and
       
   441   @{term cp}. *}
       
   442 
       
   443 lemma waiting_eq: 
       
   444   shows "waiting s th cs = waiting_raw (wq s) th cs"
       
   445   by (simp add: s_waiting_abv wq_def)
       
   446 
       
   447 lemma holding_eq: 
       
   448   shows "holding s th cs = holding_raw (wq s) th cs"
       
   449   by (simp add: s_holding_abv wq_def)
       
   450 
       
   451 lemma eq_dependants: 
       
   452   shows "dependants_raw (wq s) = dependants s"
       
   453   by (simp add: s_dependants_abv wq_def)
       
   454 
   423 
   455 lemma cp_eq: 
   424 lemma cp_eq: 
   456   shows "cp s th = cpreced (wq s) s th"
   425   shows "cp s th = cpreced (wq s) s th"
   457 unfolding cp_def wq_def
   426 unfolding cp_def wq_def
   458 apply(induct s rule: schs.induct)
   427 apply(induct s rule: schs.induct)
   470 
   439 
   471   The following lemma can be proved easily, and the meaning is obvious. *}
   440   The following lemma can be proved easily, and the meaning is obvious. *}
   472 
   441 
   473 lemma
   442 lemma
   474   s_holding_def: 
   443   s_holding_def: 
   475   "holding (s::state) th cs \<equiv> (th \<in> set (wq_fun (schs s) cs) \<and> th = hd (wq_fun (schs s) cs))"
   444   "holding (s::state) th cs \<equiv> (th \<in> set (wq s cs) \<and> th = hd (wq s cs))"
   476   by (auto simp:s_holding_abv wq_def holding_raw_def)
   445   by(simp add: s_holding_abv holding_raw_def)
   477 
   446 
   478 lemma s_waiting_def: 
   447 lemma s_waiting_def: 
   479   "waiting (s::state) th cs \<equiv> (th \<in> set (wq_fun (schs s) cs) \<and> th \<noteq> hd (wq_fun (schs s) cs))"
   448   "waiting (s::state) th cs \<equiv> (th \<in> set (wq_fun (schs s) cs) \<and> th \<noteq> hd (wq_fun (schs s) cs))"
   480   by (auto simp:s_waiting_abv wq_def waiting_raw_def)
   449   by (auto simp:s_waiting_abv wq_def waiting_raw_def)
   481 
   450 
   719 definition "tRAG s = wRAG s O hRAG s"
   688 definition "tRAG s = wRAG s O hRAG s"
   720 
   689 
   721 text {* The following lemma splits @{term "RAG"} graph into the above two sub-graphs. *}
   690 text {* The following lemma splits @{term "RAG"} graph into the above two sub-graphs. *}
   722 
   691 
   723 lemma RAG_split: "RAG s = (wRAG s \<union> hRAG s)"
   692 lemma RAG_split: "RAG s = (wRAG s \<union> hRAG s)"
   724   by (unfold s_RAG_abv wRAG_def hRAG_def s_waiting_abv 
   693 using hRAG_def s_RAG_def s_holding_abv s_waiting_abv wRAG_def wq_def by auto
   725              s_holding_abv RAG_raw_def, auto)
       
   726 
   694 
   727 lemma tRAG_alt_def: 
   695 lemma tRAG_alt_def: 
   728   "tRAG s = {(Th th1, Th th2) | th1 th2. 
   696   "tRAG s = {(Th th1, Th th2) | th1 th2. 
   729                   \<exists> cs. (Th th1, Cs cs) \<in> RAG s \<and> (Cs cs, Th th2) \<in> RAG s}"
   697                   \<exists> cs. (Th th1, Cs cs) \<in> RAG s \<and> (Cs cs, Th th2) \<in> RAG s}"
   730  by (auto simp:tRAG_def RAG_split wRAG_def hRAG_def)
   698  by (auto simp:tRAG_def RAG_split wRAG_def hRAG_def)
   731 
   699 
       
   700 
       
   701 fun actor  where
       
   702   "actor (Exit th) = th" |
       
   703   "actor (P th cs) = th" |
       
   704   "actor (V th cs) = th" |
       
   705   "actor (Set th pty) = th" |
       
   706   "actor (Create th prio) = th"
       
   707 
       
   708 -- {* The actions of a set of threads *}
       
   709 definition "actions_of ths s = filter (\<lambda> e. actor e \<in> ths) s"
       
   710 
       
   711 fun isCreate :: "event \<Rightarrow> bool" where
       
   712   "isCreate (Create th pty) = True" |
       
   713   "isCreate _ = False"
       
   714 
       
   715 fun isP :: "event \<Rightarrow> bool" where
       
   716   "isP (P th cs) = True" |
       
   717   "isP _ = False"
       
   718 
       
   719 fun isV :: "event \<Rightarrow> bool" where
       
   720   "isV (V th cs) = True" |
       
   721   "isV _ = False"
       
   722 
       
   723 
   732 (*<*)
   724 (*<*)
   733 
   725 
   734 end
   726 end
   735 (*>*)
   727 (*>*)
   736 
   728