prio/PrioGDef.thy
author urbanc
Sat, 11 Feb 2012 08:16:11 +0000
changeset 292 1f16ff7fea94
parent 291 5ef9f6ebe827
child 294 bc5bf9e9ada2
permissions -rw-r--r--
fixed problem with Latexsugar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
     1
(*<*)
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
     2
theory PrioGDef
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
     3
imports Precedence_ord Moment
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
     4
begin
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
     5
(*>*)
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
     6
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
     7
text {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
     8
  In this section, the formal model of Priority Inheritance is presented. 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
     9
  The model is based on Paulson's inductive protocol verification method, where 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    10
  the state of the system is modelled as a list of events happened so far with the latest 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    11
  event put at the head. 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    12
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    13
  To define events, the identifiers of {\em threads},
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    14
  {\em priority} and {\em critical resources } (abbreviated as @{text "cs"}) 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    15
  need to be represented. All three are represetned using standard 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    16
  Isabelle/HOL type @{typ "nat"}:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    17
*}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    18
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    19
type_synonym thread = nat -- {* Type for thread identifiers. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    20
type_synonym priority = nat  -- {* Type for priorities. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    21
type_synonym cs = nat -- {* Type for critical sections (or critical resources). *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    22
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    23
text {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    24
  \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    25
  Every event in the system corresponds to a system call, the formats of which are
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    26
  defined as follows:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    27
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    28
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    29
datatype event = 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    30
  Create thread priority | -- {* Thread @{text "thread"} is created with priority @{text "priority"}. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    31
  Exit thread | -- {* Thread @{text "thread"} finishing its execution. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    32
  P thread cs | -- {* Thread @{text "thread"} requesting critical resource @{text "cs"}. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    33
  V thread cs | -- {* Thread @{text "thread"}  releasing critical resource @{text "cs"}. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    34
  Set thread priority -- {* Thread @{text "thread"} resets its priority to @{text "priority"}. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    35
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    36
text {* 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    37
\noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    38
  Resource Allocation Graph (RAG for short) is used extensively in our formal analysis. 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    39
  The following type @{text "node"} is used to represent nodes in RAG.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    40
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    41
datatype node = 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    42
   Th "thread" | -- {* Node for thread. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    43
   Cs "cs" -- {* Node for critical resource. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    44
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    45
text {* 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    46
  In Paulson's inductive method, the states of system are represented as lists of events,
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    47
  which is defined by the following type @{text "state"}:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    48
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    49
type_synonym state = "event list"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    50
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    51
text {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    52
  \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    53
  The following function
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    54
  @{text "threads"} is used to calculate the set of live threads (@{text "threads s"})
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    55
  in state @{text "s"}.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    56
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    57
fun threads :: "state \<Rightarrow> thread set"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    58
  where 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    59
  -- {* At the start of the system, the set of threads is empty: *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    60
  "threads [] = {}" | 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    61
  -- {* New thread is added to the @{text "threads"}: *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    62
  "threads (Create thread prio#s) = {thread} \<union> threads s" | 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    63
  -- {* Finished thread is removed: *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    64
  "threads (Exit thread # s) = (threads s) - {thread}" | 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    65
  -- {* Other kind of events does not affect the value of @{text "threads"}: *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    66
  "threads (e#s) = threads s" 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    67
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    68
  Functions such as @{text "threads"}, which extract information out of system states, are called
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    69
  {\em observing functions}. A series of observing functions will be defined in the sequel in order to 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    70
  model the protocol. 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    71
  Observing function @{text "original_priority"} calculates 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    72
  the {\em original priority} of thread @{text "th"} in state @{text "s"}, expressed as
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    73
  : @{text "original_priority th s" }. The {\em original priority} is the priority 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    74
  assigned to a thread when it is created or when it is reset by system call 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    75
  @{text "Set thread priority"}.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    76
*}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    77
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    78
fun original_priority :: "thread \<Rightarrow> state \<Rightarrow> priority"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    79
  where
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    80
  -- {* @{text "0"} is assigned to threads which have never been created: *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    81
  "original_priority thread [] = 0" |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    82
  "original_priority thread (Create thread' prio#s) = 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    83
     (if thread' = thread then prio else original_priority thread s)" |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    84
  "original_priority thread (Set thread' prio#s) = 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    85
     (if thread' = thread then prio else original_priority thread s)" |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    86
  "original_priority thread (e#s) = original_priority thread s"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    87
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    88
text {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    89
  \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    90
  In the following,
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    91
  @{text "birthtime th s"} is the time when thread @{text "th"} is created, 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    92
  observed from state @{text "s"}.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    93
  The time in the system is measured by the number of events happened so far since the very beginning.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    94
*}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    95
fun birthtime :: "thread \<Rightarrow> state \<Rightarrow> nat"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    96
  where
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    97
  "birthtime thread [] = 0" |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    98
  "birthtime thread ((Create thread' prio)#s) = 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
    99
       (if (thread = thread') then length s else birthtime thread s)" |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   100
  "birthtime thread ((Set thread' prio)#s) = 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   101
       (if (thread = thread') then length s else birthtime thread s)" |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   102
  "birthtime thread (e#s) = birthtime thread s"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   103
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   104
text {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   105
  \noindent 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   106
  The {\em precedence} is a notion derived from {\em priority}, where the {\em precedence} of 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   107
  a thread is the combination of its {\em original priority} and {\em birth time}. The intention is
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   108
  to discriminate threads with the same priority by giving threads whose priority
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   109
  is assigned earlier higher precedences, becasue such threads are more urgent to finish. 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   110
  This explains the following definition:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   111
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   112
definition preced :: "thread \<Rightarrow> state \<Rightarrow> precedence"
290
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   113
  where "preced thread s \<equiv> Prc (original_priority thread s) (birthtime thread s)"
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   114
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   115
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   116
text {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   117
  \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   118
  A number of important notions are defined here:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   119
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   120
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   121
consts 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   122
  holding :: "'b \<Rightarrow> thread \<Rightarrow> cs \<Rightarrow> bool" 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   123
  waiting :: "'b \<Rightarrow> thread \<Rightarrow> cs \<Rightarrow> bool"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   124
  depend :: "'b \<Rightarrow> (node \<times> node) set"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   125
  dependents :: "'b \<Rightarrow> thread \<Rightarrow> thread set"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   126
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   127
text {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   128
  \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   129
  In the definition of the following several functions, it is supposed that
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   130
  the waiting queue of every critical resource is given by a waiting queue 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   131
  function @{text "wq"}, which servers as arguments of these functions.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   132
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   133
defs (overloaded) 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   134
  -- {* 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   135
  \begin{minipage}{0.9\textwidth}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   136
  We define that the thread which is at the head of waiting queue of resource @{text "cs"}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   137
  is holding the resource. This definition is slightly different from tradition where
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   138
  all threads in the waiting queue are considered as waiting for the resource.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   139
  This notion is reflected in the definition of @{text "holding wq th cs"} as follows:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   140
  \end{minipage}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   141
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   142
  cs_holding_def: 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   143
  "holding wq thread cs \<equiv> (thread \<in> set (wq cs) \<and> thread = hd (wq cs))"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   144
  -- {* 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   145
  \begin{minipage}{0.9\textwidth}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   146
  In accordance with the definition of @{text "holding wq th cs"}, 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   147
  a thread @{text "th"} is considered waiting for @{text "cs"} if 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   148
  it is in the {\em waiting queue} of critical resource @{text "cs"}, but not at the head.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   149
  This is reflected in the definition of @{text "waiting wq th cs"} as follows:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   150
  \end{minipage}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   151
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   152
  cs_waiting_def: 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   153
  "waiting wq thread cs \<equiv> (thread \<in> set (wq cs) \<and> thread \<noteq> hd (wq cs))"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   154
  -- {* 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   155
  \begin{minipage}{0.9\textwidth}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   156
  @{text "depend wq"} represents the Resource Allocation Graph of the system under the waiting 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   157
  queue function @{text "wq"}.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   158
  \end{minipage}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   159
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   160
  cs_depend_def: 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   161
  "depend (wq::cs \<Rightarrow> thread list) \<equiv>
287
440382eb6427 more on the specification section
urbanc
parents: 262
diff changeset
   162
      {(Th th, Cs cs) | th cs. waiting wq th cs} \<union> {(Cs cs, Th th) | cs th. holding wq th cs}"
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   163
  -- {* 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   164
  \begin{minipage}{0.9\textwidth}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   165
  The following @{text "dependents wq th"} represents the set of threads which are depending on
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   166
  thread @{text "th"} in Resource Allocation Graph @{text "depend wq"}:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   167
  \end{minipage}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   168
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   169
  cs_dependents_def: 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   170
  "dependents (wq::cs \<Rightarrow> thread list) th \<equiv> {th' . (Th th', Th th) \<in> (depend wq)^+}"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   171
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   172
text {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   173
  The data structure used by the operating system for scheduling is referred to as 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   174
  {\em schedule state}. It is represented as a record consisting of 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   175
  a function assigning waiting queue to resources and a function assigning precedence to 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   176
  threads:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   177
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   178
record schedule_state = 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   179
    waiting_queue :: "cs \<Rightarrow> thread list" -- {* The function assigning waiting queue. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   180
    cur_preced :: "thread \<Rightarrow> precedence" -- {* The function assigning precedence. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   181
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   182
text {* \noindent 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   183
  The following
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   184
  @{text "cpreced s th"} gives the {\em current precedence} of thread @{text "th"} under
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   185
  state @{text "s"}. The definition of @{text "cpreced"} reflects the basic idea of 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   186
  Priority Inheritance that the {\em current precedence} of a thread is the precedence 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   187
  inherited from the maximum of all its dependents, i.e. the threads which are waiting 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   188
  directly or indirectly waiting for some resources from it. If no such thread exits, 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   189
  @{text "th"}'s {\em current precedence} equals its original precedence, i.e. 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   190
  @{text "preced th s"}.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   191
  *}
290
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   192
definition cpreced :: "(cs \<Rightarrow> thread list) \<Rightarrow> state \<Rightarrow> thread \<Rightarrow> precedence"
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   193
  where "cpreced wq s = (\<lambda> th. Max ((\<lambda> th. preced th s) ` ({th} \<union> dependents wq th)))"
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   194
291
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   195
(*<*)
290
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   196
lemma 
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   197
  cpreced_def2:
291
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   198
  "cpreced wq s th \<equiv> Max ({preced th s} \<union> {preced th' s | th'. th' \<in> dependents wq th})"
290
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   199
  unfolding cpreced_def image_def
291
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   200
  apply(rule eq_reflection)
290
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   201
  apply(rule arg_cong)
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   202
  back
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   203
  by (auto)
291
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   204
(*>*)
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   205
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   206
abbreviation
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   207
  "all_unlocked \<equiv> \<lambda>_::cs. ([]::thread list)"
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   208
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   209
abbreviation 
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   210
  "initial_cprec \<equiv> \<lambda>_::thread. Prc 0 0"
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   211
 
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   212
abbreviation
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   213
  "release qs \<equiv> case qs of
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   214
             [] => [] 
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   215
          |  (_#qs) => (SOME q. distinct q \<and> set q = set qs)"
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   216
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   217
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   218
  The following function @{text "schs"} is used to calculate the schedule state @{text "schs s"}.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   219
  It is the key function to model Priority Inheritance:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   220
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   221
fun schs :: "state \<Rightarrow> schedule_state"
290
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   222
  where 
291
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   223
  "schs [] = (| waiting_queue = \<lambda> cs. [],  cur_preced = (\<lambda>_. Prc 0 0) |)" |
290
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   224
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   225
  -- {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   226
  \begin{minipage}{0.9\textwidth}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   227
  \begin{enumerate}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   228
  \item @{text "ps"} is the schedule state of last moment.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   229
  \item @{text "pwq"} is the waiting queue function of last moment.
291
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   230
  \item @{text "pcp"} is the precedence function of last moment (NOT USED). 
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   231
  \item @{text "nwq"} is the new waiting queue function. It is calculated using a @{text "case"} statement:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   232
  \begin{enumerate}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   233
      \item If the happening event is @{text "P thread cs"}, @{text "thread"} is added to 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   234
            the end of @{text "cs"}'s waiting queue.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   235
      \item If the happening event is @{text "V thread cs"} and @{text "s"} is a legal state,
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   236
            @{text "th'"} must equal to @{text "thread"}, 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   237
            because @{text "thread"} is the one currently holding @{text "cs"}. 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   238
            The case @{text "[] \<Longrightarrow> []"} may never be executed in a legal state.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   239
            the @{text "(SOME q. distinct q \<and> set q = set qs)"} is used to choose arbitrarily one 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   240
            thread in waiting to take over the released resource @{text "cs"}. In our representation,
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   241
            this amounts to rearrange elements in waiting queue, so that one of them is put at the head.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   242
      \item For other happening event, the schedule state just does not change.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   243
  \end{enumerate}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   244
  \item @{text "ncp"} is new precedence function, it is calculated from the newly updated waiting queue 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   245
        function. The dependency of precedence function on waiting queue function is the reason to 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   246
        put them in the same record so that they can evolve together.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   247
  \end{enumerate}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   248
  \end{minipage}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   249
     *}
291
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   250
   "schs (Create th prio # s) = 
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   251
       (let wq = waiting_queue (schs s) in
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   252
          (|waiting_queue = wq, cur_preced = cpreced wq (Create th prio # s)|))"
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   253
|  "schs (Exit th # s) = 
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   254
       (let wq = waiting_queue (schs s) in
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   255
          (|waiting_queue = wq, cur_preced = cpreced wq (Exit th # s)|))"
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   256
|  "schs (Set th prio # s) = 
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   257
       (let wq = waiting_queue (schs s) in
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   258
          (|waiting_queue = wq, cur_preced = cpreced wq (Set th prio # s)|))"
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   259
|  "schs (P th cs # s) = 
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   260
       (let wq = waiting_queue (schs s) in
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   261
        let new_wq = wq(cs := (wq cs @ [th])) in
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   262
          (|waiting_queue = new_wq, cur_preced = cpreced new_wq (P th cs # s)|))"
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   263
|  "schs (V th cs # s) = 
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   264
       (let wq = waiting_queue (schs s) in
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   265
        let new_wq = wq(cs := release (wq cs)) in
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   266
          (|waiting_queue = new_wq, cur_preced = cpreced new_wq (V th cs # s)|))"
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   267
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   268
lemma cpreced_initial: 
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   269
  "cpreced (\<lambda> cs. []) [] = (\<lambda>_. (Prc 0 0))"
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   270
apply(simp add: cpreced_def)
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   271
apply(simp add: cs_dependents_def cs_depend_def cs_waiting_def cs_holding_def)
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   272
apply(simp add: preced_def)
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   273
done
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   274
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   275
lemma sch_old_def:
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   276
  "schs (e#s) = (let ps = schs s in 
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   277
                  let pwq = waiting_queue ps in 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   278
                  let nwq = case e of
290
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   279
                             P th cs \<Rightarrow>  pwq(cs:=(pwq cs @ [th])) |
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   280
                             V th cs \<Rightarrow> let nq = case (pwq cs) of
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   281
                                                      [] \<Rightarrow> [] | 
290
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   282
                                                      (_#qs) \<Rightarrow> (SOME q. distinct q \<and> set q = set qs)
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   283
                                            in pwq(cs:=nq)                 |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   284
                              _ \<Rightarrow> pwq
290
6a6d0bd16035 more on paper
urbanc
parents: 288
diff changeset
   285
                  in let ncp = cpreced nwq (e#s) in 
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   286
                     \<lparr>waiting_queue = nwq, cur_preced = ncp\<rparr>
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   287
                 )"
291
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   288
apply(cases e)
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   289
apply(simp_all)
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   290
done
5ef9f6ebe827 more on paper; modified schs functions; it is still compatible with the old definition
urbanc
parents: 290
diff changeset
   291
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   292
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   293
text {* 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   294
  \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   295
  The following @{text "wq"} is a shorthand for @{text "waiting_queue"}. 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   296
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   297
definition wq :: "state \<Rightarrow> cs \<Rightarrow> thread list" 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   298
  where "wq s = waiting_queue (schs s)"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   299
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   300
text {* \noindent 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   301
  The following @{text "cp"} is a shorthand for @{text "cur_preced"}. 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   302
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   303
definition cp :: "state \<Rightarrow> thread \<Rightarrow> precedence"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   304
  where "cp s = cur_preced (schs s)"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   305
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   306
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   307
  Functions @{text "holding"}, @{text "waiting"}, @{text "depend"} and 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   308
  @{text "dependents"} still have the 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   309
  same meaning, but redefined so that they no longer depend on the 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   310
  fictitious {\em waiting queue function}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   311
  @{text "wq"}, but on system state @{text "s"}.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   312
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   313
defs (overloaded) 
288
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   314
  s_holding_abv: 
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   315
  "holding (s::state) \<equiv> holding (wq s)"
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   316
  s_waiting_abv: 
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   317
  "waiting (s::state) \<equiv> waiting (wq s)"
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   318
  s_depend_abv: 
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   319
  "depend (s::state) \<equiv> depend (wq s)"
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   320
  s_dependents_abv: 
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   321
  "dependents (s::state) th \<equiv> dependents (wq s) th"
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   322
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   323
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   324
text {* 
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   325
  The following lemma can be proved easily:
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   326
  *}
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   327
lemma
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   328
  s_holding_def: 
288
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   329
  "holding (s::state) thread cs = (thread \<in> set (wq s cs) \<and> thread = hd (wq s cs))"
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   330
  by (auto simp:s_holding_abv wq_def cs_holding_def)
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   331
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   332
lemma s_waiting_def: 
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   333
  "waiting (s::state) thread cs = (thread \<in> set (wq s cs) \<and> thread \<noteq> hd (wq s cs))"
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   334
  by (auto simp:s_waiting_abv wq_def cs_waiting_def)
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   335
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   336
lemma s_depend_def: 
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   337
  "depend (s::state) =
287
440382eb6427 more on the specification section
urbanc
parents: 262
diff changeset
   338
    {(Th th, Cs cs) | th cs. waiting (wq s) th cs} \<union> {(Cs cs, Th th) | cs th. holding (wq s) th cs}"
288
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   339
  by (auto simp:s_depend_abv wq_def cs_depend_def)
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   340
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   341
lemma
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   342
  s_dependents_def: 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   343
  "dependents (s::state) th \<equiv> {th' . (Th th', Th th) \<in> (depend (wq s))^+}"
288
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   344
  by (auto simp:s_dependents_abv wq_def cs_dependents_def)
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   345
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   346
text {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   347
  The following function @{text "readys"} calculates the set of ready threads. A thread is {\em ready} 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   348
  for running if it is a live thread and it is not waiting for any critical resource.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   349
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   350
definition readys :: "state \<Rightarrow> thread set"
287
440382eb6427 more on the specification section
urbanc
parents: 262
diff changeset
   351
  where "readys s \<equiv> {th . th \<in> threads s \<and> (\<forall> cs. \<not> waiting s th cs)}"
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   352
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   353
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   354
  The following function @{text "runing"} calculates the set of running thread, which is the ready 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   355
  thread with the highest precedence. 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   356
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   357
definition runing :: "state \<Rightarrow> thread set"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   358
  where "runing s = {th . th \<in> readys s \<and> cp s th = Max ((cp s) ` (readys s))}"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   359
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   360
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   361
  The following function @{text "holdents s th"} returns the set of resources held by thread 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   362
  @{text "th"} in state @{text "s"}.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   363
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   364
definition holdents :: "state \<Rightarrow> thread \<Rightarrow> cs set"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   365
  where "holdents s th = {cs . (Cs cs, Th th) \<in> depend s}"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   366
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   367
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   368
  @{text "cntCS s th"} returns the number of resources held by thread @{text "th"} in
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   369
  state @{text "s"}:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   370
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   371
definition cntCS :: "state \<Rightarrow> thread \<Rightarrow> nat"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   372
  where "cntCS s th = card (holdents s th)"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   373
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   374
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   375
  The fact that event @{text "e"} is eligible to happen next in state @{text "s"} 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   376
  is expressed as @{text "step s e"}. The predicate @{text "step"} is inductively defined as 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   377
  follows:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   378
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   379
inductive step :: "state \<Rightarrow> event \<Rightarrow> bool"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   380
  where
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   381
  -- {* 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   382
  A thread can be created if it is not a live thread:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   383
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   384
  thread_create: "\<lbrakk>thread \<notin> threads s\<rbrakk> \<Longrightarrow> step s (Create thread prio)" |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   385
  -- {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   386
  A thread can exit if it no longer hold any resource:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   387
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   388
  thread_exit: "\<lbrakk>thread \<in> runing s; holdents s thread = {}\<rbrakk> \<Longrightarrow> step s (Exit thread)" |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   389
  -- {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   390
  \begin{minipage}{0.9\textwidth}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   391
  A thread can request for an critical resource @{text "cs"}, if it is running and 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   392
  the request does not form a loop in the current RAG. The latter condition 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   393
  is set up to avoid deadlock. The condition also reflects our assumption all threads are 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   394
  carefully programmed so that deadlock can not happen:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   395
  \end{minipage}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   396
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   397
  thread_P: "\<lbrakk>thread \<in> runing s;  (Cs cs, Th thread)  \<notin> (depend s)^+\<rbrakk> \<Longrightarrow> 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   398
                                                                step s (P thread cs)" |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   399
  -- {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   400
  \begin{minipage}{0.9\textwidth}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   401
  A thread can release a critical resource @{text "cs"} 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   402
  if it is running and holding that resource:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   403
  \end{minipage}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   404
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   405
  thread_V: "\<lbrakk>thread \<in> runing s;  holding s thread cs\<rbrakk> \<Longrightarrow> step s (V thread cs)" |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   406
  -- {*
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   407
  A thread can adjust its own priority as long as it is current running:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   408
  *}  
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   409
  thread_set: "\<lbrakk>thread \<in> runing s\<rbrakk> \<Longrightarrow> step s (Set thread prio)"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   410
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   411
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   412
  With predicate @{text "step"}, the fact that @{text "s"} is a legal state in 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   413
  Priority Inheritance protocol can be expressed as: @{text "vt step s"}, where
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   414
  the predicate @{text "vt"} can be defined as the following:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   415
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   416
inductive vt :: "(state \<Rightarrow> event \<Rightarrow> bool) \<Rightarrow> state \<Rightarrow> bool"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   417
 for cs -- {* @{text "cs"} is an argument representing any step predicate. *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   418
  where
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   419
  -- {* Empty list @{text "[]"} is a legal state in any protocol:*}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   420
  vt_nil[intro]: "vt cs []" |
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   421
  -- {* 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   422
  \begin{minipage}{0.9\textwidth}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   423
  If @{text "s"} a legal state, and event @{text "e"} is eligible to happen
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   424
  in state @{text "s"}, then @{text "e#s"} is a legal state as well:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   425
  \end{minipage}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   426
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   427
  vt_cons[intro]: "\<lbrakk>vt cs s; cs s e\<rbrakk> \<Longrightarrow> vt cs (e#s)"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   428
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   429
text {*  \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   430
  It is easy to see that the definition of @{text "vt"} is generic. It can be applied to 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   431
  any step predicate to get the set of legal states.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   432
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   433
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   434
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   435
  The following two functions @{text "the_cs"} and @{text "the_th"} are used to extract
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   436
  critical resource and thread respectively out of RAG nodes.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   437
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   438
fun the_cs :: "node \<Rightarrow> cs"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   439
  where "the_cs (Cs cs) = cs"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   440
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   441
fun the_th :: "node \<Rightarrow> thread"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   442
  where "the_th (Th th) = th"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   443
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   444
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   445
  The following predicate @{text "next_th"} describe the next thread to 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   446
  take over when a critical resource is released. In @{text "next_th s th cs t"}, 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   447
  @{text "th"} is the thread to release, @{text "t"} is the one to take over.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   448
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   449
definition next_th:: "state \<Rightarrow> thread \<Rightarrow> cs \<Rightarrow> thread \<Rightarrow> bool"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   450
  where "next_th s th cs t = (\<exists> rest. wq s cs = th#rest \<and> rest \<noteq> [] \<and> 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   451
                                                t = hd (SOME q. distinct q \<and> set q = set rest))"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   452
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   453
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   454
  The function @{text "count Q l"} is used to count the occurrence of situation @{text "Q"}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   455
  in list @{text "l"}:
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   456
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   457
definition count :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> nat"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   458
  where "count Q l = length (filter Q l)"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   459
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   460
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   461
  The following @{text "cntP s"} returns the number of operation @{text "P"} happened 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   462
  before reaching state @{text "s"}.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   463
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   464
definition cntP :: "state \<Rightarrow> thread \<Rightarrow> nat"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   465
  where "cntP s th = count (\<lambda> e. \<exists> cs. e = P th cs) s"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   466
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   467
text {* \noindent
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   468
  The following @{text "cntV s"} returns the number of operation @{text "V"} happened 
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   469
  before reaching state @{text "s"}.
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   470
  *}
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   471
definition cntV :: "state \<Rightarrow> thread \<Rightarrow> nat"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   472
  where "cntV s th = count (\<lambda> e. \<exists> cs. e = V th cs) s"
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   473
(*<*)
288
64c9f151acf5 changes by Xingyuan
urbanc
parents: 287
diff changeset
   474
262
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   475
end
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   476
(*>*)
4190df6f4488 initial version of the PIP formalisation
urbanc
parents:
diff changeset
   477