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