Journal/Paper.thy
changeset 127 38c6acf03f68
parent 126 a88af0e4731f
child 130 0f124691c191
equal deleted inserted replaced
126:a88af0e4731f 127:38c6acf03f68
    29 
    29 
    30 notation (latex output)
    30 notation (latex output)
    31   Cons ("_::_" [78,77] 73) and
    31   Cons ("_::_" [78,77] 73) and
    32   If  ("(\<^raw:\textrm{>if\<^raw:}> (_)/ \<^raw:\textrm{>then\<^raw:}> (_)/ \<^raw:\textrm{>else\<^raw:}> (_))" 10) and
    32   If  ("(\<^raw:\textrm{>if\<^raw:}> (_)/ \<^raw:\textrm{>then\<^raw:}> (_)/ \<^raw:\textrm{>else\<^raw:}> (_))" 10) and
    33   vt ("valid'_state") and
    33   vt ("valid'_state") and
    34   runing ("running") and
       
    35   Prc ("'(_, _')") and
    34   Prc ("'(_, _')") and
    36   holding ("holds") and
    35   holding_raw ("holds") and
    37   waiting ("waits") and
    36   holding ("Holds") and
       
    37   waiting_raw ("waits") and
       
    38   waiting ("Waits") and
       
    39   dependants_raw ("dependants") and
       
    40   dependants ("Dependants") and
    38   Th ("T") and
    41   Th ("T") and
    39   Cs ("C") and
    42   Cs ("C") and
    40   readys ("ready") and
    43   readys ("ready") and
    41   preced ("prec") and
    44   preced ("prec") and
    42 (*  preceds ("precs") and*)
    45   preceds ("precs") and
    43   cpreced ("cprec") and
    46   cpreced ("cprec") and
    44   cp ("cprec") and
    47   cp ("cprec") and
    45   holdents ("resources") and
    48   holdents ("resources") and
    46   DUMMY  ("\<^raw:\mbox{$\_\!\_$}>") and
    49   DUMMY  ("\<^raw:\mbox{$\_\!\_$}>") and
    47   cntP ("c\<^bsub>P\<^esub>") and
    50   cntP ("c\<^bsub>P\<^esub>") and
   328 
   331 
   329   \begin{isabelle}\ \ \ \ \ %%%
   332   \begin{isabelle}\ \ \ \ \ %%%
   330   \mbox{\begin{tabular}{lcl}
   333   \mbox{\begin{tabular}{lcl}
   331   @{thm (lhs) threads.simps(1)} & @{text "\<equiv>"} & 
   334   @{thm (lhs) threads.simps(1)} & @{text "\<equiv>"} & 
   332     @{thm (rhs) threads.simps(1)}\\
   335     @{thm (rhs) threads.simps(1)}\\
   333   @{thm (lhs) threads.simps(2)[where thread="th"]} & @{text "\<equiv>"} & 
   336   @{thm (lhs) threads.simps(2)} & @{text "\<equiv>"} & 
   334     @{thm (rhs) threads.simps(2)[where thread="th"]}\\
   337     @{thm (rhs) threads.simps(2)}\\
   335   @{thm (lhs) threads.simps(3)[where thread="th"]} & @{text "\<equiv>"} & 
   338   @{thm (lhs) threads.simps(3)} & @{text "\<equiv>"} & 
   336     @{thm (rhs) threads.simps(3)[where thread="th"]}\\
   339     @{thm (rhs) threads.simps(3)}\\
   337   @{term "threads (DUMMY#s)"} & @{text "\<equiv>"} & @{term "threads s"}\\
   340   @{term "threads (DUMMY#s)"} & @{text "\<equiv>"} & @{term "threads s"}\\
   338   \end{tabular}}
   341   \end{tabular}}
   339   \end{isabelle}
   342   \end{isabelle}
   340 
   343 
   341   \noindent
   344   \noindent
   343   Another function calculates the priority for a thread @{text "th"}, which is 
   346   Another function calculates the priority for a thread @{text "th"}, which is 
   344   defined as
   347   defined as
   345 
   348 
   346   \begin{isabelle}\ \ \ \ \ %%%
   349   \begin{isabelle}\ \ \ \ \ %%%
   347   \mbox{\begin{tabular}{lcl}
   350   \mbox{\begin{tabular}{lcl}
   348   @{thm (lhs) priority.simps(1)[where thread="th"]} & @{text "\<equiv>"} & 
   351   @{thm (lhs) priority.simps(1)} & @{text "\<equiv>"} & 
   349     @{thm (rhs) priority.simps(1)[where thread="th"]}\\
   352     @{thm (rhs) priority.simps(1)}\\
   350   @{thm (lhs) priority.simps(2)[where thread="th" and thread'="th'"]} & @{text "\<equiv>"} & 
   353   @{thm (lhs) priority.simps(2)} & @{text "\<equiv>"} & 
   351     @{thm (rhs) priority.simps(2)[where thread="th" and thread'="th'"]}\\
   354     @{thm (rhs) priority.simps(2)}\\
   352   @{thm (lhs) priority.simps(3)[where thread="th" and thread'="th'"]} & @{text "\<equiv>"} & 
   355   @{thm (lhs) priority.simps(3)} & @{text "\<equiv>"} & 
   353     @{thm (rhs) priority.simps(3)[where thread="th" and thread'="th'"]}\\
   356     @{thm (rhs) priority.simps(3)}\\
   354   @{term "priority th (DUMMY#s)"} & @{text "\<equiv>"} & @{term "priority th s"}\\
   357   @{term "priority th (DUMMY#s)"} & @{text "\<equiv>"} & @{term "priority th s"}\\
   355   \end{tabular}}
   358   \end{tabular}}
   356   \end{isabelle}
   359   \end{isabelle}
   357 
   360 
   358   \noindent
   361   \noindent
   361   calculates the ``time'', or index, at which time a thread had its 
   364   calculates the ``time'', or index, at which time a thread had its 
   362   priority last set.
   365   priority last set.
   363 
   366 
   364   \begin{isabelle}\ \ \ \ \ %%%
   367   \begin{isabelle}\ \ \ \ \ %%%
   365   \mbox{\begin{tabular}{lcl}
   368   \mbox{\begin{tabular}{lcl}
   366   @{thm (lhs) last_set.simps(1)[where thread="th"]} & @{text "\<equiv>"} & 
   369   @{thm (lhs) last_set.simps(1)} & @{text "\<equiv>"} & 
   367     @{thm (rhs) last_set.simps(1)[where thread="th"]}\\
   370     @{thm (rhs) last_set.simps(1)}\\
   368   @{thm (lhs) last_set.simps(2)[where thread="th" and thread'="th'"]} & @{text "\<equiv>"} & 
   371   @{thm (lhs) last_set.simps(2)} & @{text "\<equiv>"} & 
   369     @{thm (rhs) last_set.simps(2)[where thread="th" and thread'="th'"]}\\
   372     @{thm (rhs) last_set.simps(2)}\\
   370   @{thm (lhs) last_set.simps(3)[where thread="th" and thread'="th'"]} & @{text "\<equiv>"} & 
   373   @{thm (lhs) last_set.simps(3)} & @{text "\<equiv>"} & 
   371     @{thm (rhs) last_set.simps(3)[where thread="th" and thread'="th'"]}\\
   374     @{thm (rhs) last_set.simps(3)}\\
   372   @{term "last_set th (DUMMY#s)"} & @{text "\<equiv>"} & @{term "last_set th s"}\\
   375   @{term "last_set th (DUMMY#s)"} & @{text "\<equiv>"} & @{term "last_set th s"}\\
   373   \end{tabular}}
   376   \end{tabular}}
   374   \end{isabelle}
   377   \end{isabelle}
   375 
   378 
   376   \noindent
   379   \noindent
   378   of events @{text s}. Again the default value in this function is @{text 0}
   381   of events @{text s}. Again the default value in this function is @{text 0}
   379   for threads that have not been created yet. A \emph{precedence} of a thread @{text th} in a 
   382   for threads that have not been created yet. A \emph{precedence} of a thread @{text th} in a 
   380   state @{text s} is the pair of natural numbers defined as
   383   state @{text s} is the pair of natural numbers defined as
   381   
   384   
   382   \begin{isabelle}\ \ \ \ \ %%%
   385   \begin{isabelle}\ \ \ \ \ %%%
   383   @{thm preced_def[where thread="th"]}
   386   @{thm preced_def}
   384   \end{isabelle}
   387   \end{isabelle}
   385 
   388 
   386   \noindent
   389   \noindent
   387   We also use the abbreviation 
   390   We also use the abbreviation 
   388 
   391 
   389   \begin{isabelle}\ \ \ \ \ %%%
   392   \begin{isabelle}\ \ \ \ \ %%%
   390   ???preceds s ths
   393   @{thm preceds_def}
   391   \end{isabelle}
   394   \end{isabelle}
   392 
   395 
   393   \noindent
   396   \noindent
   394   for the set of precedences of threads @{text ths} in state @{text s}.
   397   for the set of precedences of threads @{text ths} in state @{text s}.
   395   The point of precedences is to schedule threads not according to priorities (because what should
   398   The point of precedences is to schedule threads not according to priorities (because what should
   430   when a thread \emph{holds}, respectively \emph{waits} for, a
   433   when a thread \emph{holds}, respectively \emph{waits} for, a
   431   resource @{text cs} given a waiting queue function @{text wq}.
   434   resource @{text cs} given a waiting queue function @{text wq}.
   432 
   435 
   433   \begin{isabelle}\ \ \ \ \ %%%
   436   \begin{isabelle}\ \ \ \ \ %%%
   434   \begin{tabular}{@ {}l}
   437   \begin{tabular}{@ {}l}
   435   @{thm cs_holding_raw[where thread="th"]}\\
   438   @{thm holding_raw_def[where thread="th"]}\\
   436   @{thm cs_waiting_raw[where thread="th"]}
   439   @{thm waiting_raw_def[where thread="th"]}
   437   \end{tabular}
   440   \end{tabular}
   438   \end{isabelle}
   441   \end{isabelle}
   439 
   442 
   440   \noindent
   443   \noindent
   441   In this definition we assume @{text "set"} converts a list into a set.
   444   In this definition we assume @{text "set"} converts a list into a set.
   448   \begin{isabelle}\ \ \ \ \ %%%
   451   \begin{isabelle}\ \ \ \ \ %%%
   449   @{abbrev all_unlocked}\hfill\numbered{allunlocked}
   452   @{abbrev all_unlocked}\hfill\numbered{allunlocked}
   450   \end{isabelle}
   453   \end{isabelle}
   451 
   454 
   452   \noindent
   455   \noindent
   453   Using @{term "holding"} and @{term waiting}, we can introduce \emph{Resource Allocation Graphs} 
   456   Using @{term "holding_raw"} and @{term waiting_raw}, we can introduce \emph{Resource Allocation Graphs} 
   454   (RAG), which represent the dependencies between threads and resources.
   457   (RAG), which represent the dependencies between threads and resources.
   455   We represent RAGs as relations using pairs of the form
   458   We represent RAGs as relations using pairs of the form
   456 
   459 
   457   \begin{isabelle}\ \ \ \ \ %%%
   460   \begin{isabelle}\ \ \ \ \ %%%
   458   @{term "(Th th, Cs cs)"} \hspace{5mm}{\rm and}\hspace{5mm}
   461   @{term "(Th th, Cs cs)"} \hspace{5mm}{\rm and}\hspace{5mm}
   464   \emph{holding edge} (@{term Cs} and @{term Th} are constructors of a 
   467   \emph{holding edge} (@{term Cs} and @{term Th} are constructors of a 
   465   datatype for vertices). Given a waiting queue function, a RAG is defined 
   468   datatype for vertices). Given a waiting queue function, a RAG is defined 
   466   as the union of the sets of waiting and holding edges, namely
   469   as the union of the sets of waiting and holding edges, namely
   467 
   470 
   468   \begin{isabelle}\ \ \ \ \ %%%
   471   \begin{isabelle}\ \ \ \ \ %%%
   469   @{thm cs_RAG_raw}
   472   @{thm RAG_raw_def}
   470   \end{isabelle}
   473   \end{isabelle}
   471 
   474 
   472 
   475 
   473   \begin{figure}[t]
   476   \begin{figure}[t]
   474   \begin{center}
   477   \begin{center}
   526   The use of relations for representing RAGs allows us to conveniently define
   529   The use of relations for representing RAGs allows us to conveniently define
   527   the notion of the \emph{dependants} of a thread using the transitive closure
   530   the notion of the \emph{dependants} of a thread using the transitive closure
   528   operation for relations, written ~@{term "trancl DUMMY"}. This gives
   531   operation for relations, written ~@{term "trancl DUMMY"}. This gives
   529 
   532 
   530   \begin{isabelle}\ \ \ \ \ %%%
   533   \begin{isabelle}\ \ \ \ \ %%%
   531   @{thm cs_dependants_def}
   534   @{thm dependants_raw_def}
   532   \end{isabelle}
   535   \end{isabelle}
   533 
   536 
   534   \noindent
   537   \noindent This definition needs to account for all threads that wait
   535   This definition needs to account for all threads that wait for a thread to
   538   for a thread to release a resource. This means we need to include
   536   release a resource. This means we need to include threads that transitively
   539   threads that transitively wait for a resource to be released (in the
   537   wait for a resource to be released (in the picture above this means the dependants
   540   picture above this means the dependants of @{text "th\<^sub>0"} are
   538   of @{text "th\<^sub>0"} are @{text "th\<^sub>1"} and @{text "th\<^sub>2"}, which wait for resource @{text "cs\<^sub>1"}, 
   541   @{text "th\<^sub>1"} and @{text "th\<^sub>2"}, which wait for
   539   but also @{text "th\<^sub>3"}, 
   542   resource @{text "cs\<^sub>1"}, but also @{text "th\<^sub>3"}, which
   540   which cannot make any progress unless @{text "th\<^sub>2"} makes progress, which
   543   cannot make any progress unless @{text "th\<^sub>2"} makes progress,
   541   in turn needs to wait for @{text "th\<^sub>0"} to finish). If there is a circle of dependencies 
   544   which in turn needs to wait for @{text "th\<^sub>0"} to finish). If
   542   in a RAG, then clearly
   545   there is a circle of dependencies in a RAG, then clearly we have a
   543   we have a deadlock. Therefore when a thread requests a resource,
   546   deadlock. Therefore when a thread requests a resource, we must
   544   we must ensure that the resulting RAG is not circular. In practice, the 
   547   ensure that the resulting RAG is not circular. In practice, the
   545   programmer has to ensure this.
   548   programmer has to ensure this.
   546 
   549 
   547 
   550 
   548   Next we introduce the notion of the \emph{current precedence} of a thread @{text th} in a 
   551   Next we introduce the notion of the \emph{current precedence} of a thread @{text th} in a 
   549   state @{text s}. It is defined as
   552   state @{text s}. It is defined as
   697   is then the thread with the highest current precedence of all ready threads.
   700   is then the thread with the highest current precedence of all ready threads.
   698 
   701 
   699   \begin{isabelle}\ \ \ \ \ %%%
   702   \begin{isabelle}\ \ \ \ \ %%%
   700   \begin{tabular}{@ {}l}
   703   \begin{tabular}{@ {}l}
   701   @{thm readys_def}\\
   704   @{thm readys_def}\\
   702   @{thm runing_def}
   705   @{thm running_def}
   703   \end{tabular}
   706   \end{tabular}
   704   \end{isabelle}
   707   \end{isabelle}
   705 
   708 
   706   \noindent
   709   \noindent
   707   %%In the second definition @{term "DUMMY ` DUMMY"} stands for the image of a set under a function.
   710   %%In the second definition @{term "DUMMY ` DUMMY"} stands for the image of a set under a function.
   999   @{thm [source] th_cp_max th_cp_preced th_kept}
  1002   @{thm [source] th_cp_max th_cp_preced th_kept}
  1000   @{thm th_cp_max th_cp_preced th_kept}
  1003   @{thm th_cp_max th_cp_preced th_kept}
  1001 
  1004 
  1002   THENTHEN
  1005   THENTHEN
  1003 
  1006 
  1004   (here) %@ {thm [source] runing_inversion_4} @  {thm runing_inversion_4}
  1007   (here) %@ {thm [source] running_inversion_4} @  {thm running_inversion_4}
  1005 
  1008 
  1006   which explains what the @{term "th'"} looks like. Now, we have found the 
  1009   which explains what the @{term "th'"} looks like. Now, we have found the 
  1007   @{term "th'"} which blocks @{term th}, we need to know more about it.
  1010   @{term "th'"} which blocks @{term th}, we need to know more about it.
  1008   To see what kind of thread can block @{term th}.
  1011   To see what kind of thread can block @{term th}.
  1009 
  1012 
  1013   Lemmas we want to describe:
  1016   Lemmas we want to describe:
  1014 
  1017 
  1015   
  1018   
  1016 
  1019 
  1017   \begin{lemma}
  1020   \begin{lemma}
  1018   @{thm runing_cntP_cntV_inv}
  1021   @{thm running_cntP_cntV_inv}
  1019   \end{lemma}
  1022   \end{lemma}
  1020 
  1023 
  1021   \noindent
  1024   \noindent
  1022   Remember we do not have the well-nestedness restriction in our
  1025   Remember we do not have the well-nestedness restriction in our
  1023   proof, which means the difference between the counters @{const cntV}
  1026   proof, which means the difference between the counters @{const cntV}
  1024   and @{const cntP} can be larger than @{term 1}.
  1027   and @{const cntP} can be larger than @{term 1}.
  1025 
  1028 
  1026   \begin{lemma}\label{runninginversion}
  1029   \begin{lemma}\label{runninginversion}
  1027   @{thm runing_inversion}
  1030   @{thm running_inversion}
  1028   \end{lemma}
  1031   \end{lemma}
  1029   
  1032   
  1030   explain tRAG
  1033   explain tRAG
  1031   \bigskip
  1034   \bigskip
  1032 
  1035 
  1105   \begin{isabelle}\ \ \ \ \ %%%
  1108   \begin{isabelle}\ \ \ \ \ %%%
  1106   \begin{tabular}{@ {}l}
  1109   \begin{tabular}{@ {}l}
  1107   HERE??
  1110   HERE??
  1108   %@ {thm[mode=IfThen] waiting_unique[of _ _ "cs1" "cs2"]}\\
  1111   %@ {thm[mode=IfThen] waiting_unique[of _ _ "cs1" "cs2"]}\\
  1109   %@ {thm[mode=IfThen] held_unique[of _ "th1" _ "th2"]}\\
  1112   %@ {thm[mode=IfThen] held_unique[of _ "th1" _ "th2"]}\\
  1110   %@ {thm[mode=IfThen] runing_unique[of _ "th1" "th2"]}
  1113   %@ {thm[mode=IfThen] running_unique[of _ "th1" "th2"]}
  1111   \end{tabular}
  1114   \end{tabular}
  1112   \end{isabelle}
  1115   \end{isabelle}
  1113 
  1116 
  1114   \noindent
  1117   \noindent
  1115   The first property states that every waiting thread can only wait for a single
  1118   The first property states that every waiting thread can only wait for a single
  1239   %  (@{text "count_eq_dependants"}):
  1242   %  (@{text "count_eq_dependants"}):
  1240   %  @  {thm [display] count_eq_dependants}
  1243   %  @  {thm [display] count_eq_dependants}
  1241   %\end{enumerate}
  1244   %\end{enumerate}
  1242 
  1245 
  1243   %The reason that only threads which already held some resoures
  1246   %The reason that only threads which already held some resoures
  1244   %can be runing and block @{text "th"} is that if , otherwise, one thread 
  1247   %can be running and block @{text "th"} is that if , otherwise, one thread 
  1245   %does not hold any resource, it may never have its prioirty raised
  1248   %does not hold any resource, it may never have its prioirty raised
  1246   %and will not get a chance to run. This fact is supported by 
  1249   %and will not get a chance to run. This fact is supported by 
  1247   %lemma @{text "moment_blocked"}:
  1250   %lemma @{text "moment_blocked"}:
  1248   %@   {thm [display] moment_blocked}
  1251   %@   {thm [display] moment_blocked}
  1249   %When instantiating  @{text "i"} to @{text "0"}, the lemma means threads which did not hold any
  1252   %When instantiating  @{text "i"} to @{text "0"}, the lemma means threads which did not hold any
  1294   %  (@{text "th_cp_preced"}): 
  1297   %  (@{text "th_cp_preced"}): 
  1295   %  @   {thm [display] th_cp_preced}
  1298   %  @   {thm [display] th_cp_preced}
  1296   %\end{enumerate}
  1299   %\end{enumerate}
  1297 
  1300 
  1298   %The main theorem of this part is to characterizing the running thread during @{term "t"} 
  1301   %The main theorem of this part is to characterizing the running thread during @{term "t"} 
  1299   %(@{text "runing_inversion_2"}):
  1302   %(@{text "running_inversion_2"}):
  1300   %@   {thm [display] runing_inversion_2}
  1303   %@   {thm [display] running_inversion_2}
  1301   %According to this, if a thread is running, it is either @{term "th"} or was
  1304   %According to this, if a thread is running, it is either @{term "th"} or was
  1302   %already live and held some resource 
  1305   %already live and held some resource 
  1303   %at moment @{text "s"} (expressed by: @{text "cntV s th' < cntP s th'"}).
  1306   %at moment @{text "s"} (expressed by: @{text "cntV s th' < cntP s th'"}).
  1304 
  1307 
  1305   %Since there are only finite many threads live and holding some resource at any moment,
  1308   %Since there are only finite many threads live and holding some resource at any moment,