ѧ
JOURNAL OF SOFTWARE
1999 6 10 Vol.10 No.6 1999



˫ṹļ̳쳣*
  ƽ
ժҪ̳쳣ǲеһҪҲǴѵ⣬ʹͬ.Ϊ,һµ˫ṹģ͵ķ,̳쳣,ɶͬĸṩȫλ֧.
ؼʡ̳쳣,,,.
ͼšTP311

A Two-layered-class Method for Solving the Inheritance Anomaly
ZHANG MingLJianYANG Da-junTAO Xian-ping
(State Key Laboratory for Novel Software Technology Nanjing University Nanjing 210093)
(Institute of Computer Software Nanjing University Nanjing 210093)
AbstractIn concurrent object-oriented languages, the inheritance anomaly is an important and difficult problem, which makes synchronization codes difficult to reuse. Based on the two-layered-class model, a new method for solving the inheritance anomaly is proposed. It can provide the flexible and sufficient support to the reuse of synchronization codes.
Key wordsInheritance anomaly, control class, concrete class, connecting mechanism.
ķչ,ơģ黯ԡɸԵȷԱ˺ܴķ1.̳Уinheritance˳ԣsequential object-oriented languagesһص,̳лԵҪ֮һ,ʵãreuseͿЧԻ.
ӦöԲд,ѧϣܽлؽ.ͨ,󻷾Բֿ֧;:һ󻷾Ļ벢йصһЩ,̣procesṣܳmonitor,Smalltalk2вõľַ.ڶ̸Ĳͬ,⽨ģͽж󱣻ʱ.һַǽлͳһɲĸ,Ϣ̼佻Ҳлؽһ,Act-1ABCLõľַ.һ԰ģ,һִеλ.۲ú;,벢ƽ϶һҪ⡪̳,ڲṩʵļ̳л.
ֲ֧ܶOOԶԭԵĻ벢,ڲ󻷾пͬʱжϢﲢҾвȷ,ʹͼ̳лͻ.Ϊֳͻ,еоͲṩԼ̳Ե֧,POOL-T3,COOL,Presto,Emeraldȣ4.ϸ˵,Բ,ֻǻڶ.һ;Matroshkaвõ;,֧־̬̳,һֳͬͻ.ȻPOOL-TԺİ汾POOL/RṩԵļ̳,๤ɳԱ.
Ȼ,;ؽеļ̳쳣.,϶ԲԵļ̳쳣˶෽о,˸ָĽ.ȷ˼̳쳣,Ȼһִ̳쳣·,ͨӼ˵.
1 ̳쳣;
ν̳쳣ָ,Ϊ˱֤,ĳЩ±¶常ԭԼ̳еķ.̳쳣ĺƻ󷽷ŵ:Ϣأװԣʹ.
ݲͬͬƻڽ̳쳣ʱٵ,ף5н绺Ϊ˵˼̳쳣3ֵ:״ֽ̬쳣ʷй״̬쳣״̬޸Ĳ쳣.ͨԿ,̳쳣ԭûܽͬԼ뷽ӸϷֿ,м̳ʱ޷̳бԼ̳еĸзĴ,̳쳣.
̳쳣ΪеһҪ,Ŀǰͼͬ.ӷͬԼ뷽ĽǶȣԽյ,Behavior Abstraction6,Method GuardsԼSynchronizersTransition Specifications5,7ݵȷ;ӷܽյǶȿ,Ƚϵ͵ľIllinoisѧFrfundһֻMethod-GuardsĿܣ8.иԵص,δһֱȽķش̳쳣,ṩͬõȫλ֧.
,Ϊ,Ҫͬĸṩȫλ֧,ӸϽ̳쳣,3:
.ͬķ⡡ͬӷзǽ̳쳣Ҫ,ɱͬ뷽ڼ̳е.
.ͬķֽ⡡Ȼеķ븸еķͬԼкܴ,ͬʱҲڲֲͬ,ˣܽвǲ̳ͬй,ͬṩһܺõֶ.ķֽʵϾǿκʵضͬģ黯,ʹøûб仯ͬ.
.ͬĶ⡡ʵкܶͬģʽȫһ,-дģʽ.ܽģʽдһģʽ,ٰѸͬģʽconnecting,Ϳڸ̶ϴﵽͬЧ.
ӴõĽǶ,3ֱǷáͬáͬ3̶ͬõ.˵,ǰǴӼ̳еĽǶǴ,3ǶֱͬӸ.Ϊֹ,δһַͬʱ3Ҫ.,һ˫˼ͬ.
2 νṹķ
Ϊ˽,ȽͬƲ岿ַֿ,Ӷ˿ĸ.Ϊ˽ֽ,̶ȵ֧ͬ,ǶԲͬƲִֵصԼ౾ӰԼӷͿԷ,ͬʵϿԷֳӲ:һǰж,ڽյϢʱж;һǺ״̬ת,巽ִнԳ״̬Ӱ.,һĿĶӦðֵĿ.,guardƺϢϵķ,ӶԽСȵ.Ϊ˽,˹connecting,ʹÿ಻ͬľ.
ڲ,ǿΪ๹ɣֱǿ;.ṹͼ1ʾ.

ͼ1ṹͼ
ĳ״̬תϵ˵ǲ,ҪϢյͬƣҪгݣ״̬ͳͳ󷽷.ָ״̬תϵĲ,˵ǾʵݼϵĲ.ʵ,һ˳򻷾еȫһ.
״̬ǿĵ,ʾǰĲͬϢ״̬,ϢĽй.Ҳ˵,ڲͬĳ״̬,ܽյϢһ.״̬ɿеĳ.Ŀ,ֱ֮ϵ.һ潫нϾ˵.
;֮ԵĶ,֮ĹϵͨضĹϵ.ڽյϢ,ȸݶǰĳ״̬ԼϵȷϢǷɽ.ɽ,ִӦľеķ,ڽлӦ״̬תbehavior,ִ֮.
ڿҲһ,Ҳм̳ей.벢жͬеĹϵ,ˣĽۿ.
3 Աʾ
3.1 Ķ
״̬ǿĵ.ҪΧƳ״̬й.һ˵,Ķ3:(1) ״̬Ķ;(2) ״̬ɽϢϵĶӦ;(3) ״̬״̬仯һ.
1 ״̬Ķ巽
״̬ñʽ:state-name:=expression,expressionó.ݳܷ,:
(a) ״ֱ̬ӹexpression.,ֱӶӦϵ.Ҳ˵,״̬ı仯ھ״̬ı仯.ʵ,˳״̬״̬֮ӳϵ,Ӷʹ״̬ı仯ž״̬ı仯Զؽ.
(b) ״ֱ̬ӹexpression.״̬Ķ״̬޹.Ҫı.Ϊ˱֤仯֮һ,ҪھķִϺɿ޸Ӧĳ,Ա֤״̬״̬һ.
2 ״̬ɽϢϵĶӦ
÷״̬ĿɽϢ:M(state-name)method-set;
ͨ״̬state-nameϢmethod-setĶӦϵ,ʾڳ״̬state-nameʱ,Խռmethod-setڰϢ.method-setԼϵʽ,˿ӦĽȻ,.Ϣָ󷽷ϣ෽֮Ĺϵɹʵ.
3 ״̬תϵ
Ҫ״̬ת֮һ.
ͨһ״̬תbehavior.
method-setexpressions,ʾmethod-setеķִнɵĶ.òֲǱ.ڳ״̬Ķа޷״ֱ̬ӹĲ,Ϊ˱֤ߵı仯һ,Ҫһ.
3.2 ֮Ĺϵ
ϵͳһOյĳϢm,ϢmöϢȴ,ȻƽϢ:
(1) ɨ豾Ϣȴ,Ϊ,ûдϢ,ڿ״̬;ѡȡеĵ1Ϣm,(2).
(2) Oǰĳ״̬mǷΪɽϢ.ɽ,Ϣmڵȴд;ɽ,mͳȴ,(3).
(3) ִӦľķmķ岿.
(4) mӦbehaviorֵת,ִ경(3)ִ֮иӦĲ.
ʵ,ඨĳ״̬һ״̬仯ѭ.ͬһ״̬ѭڵĸ״̬֮ǻ.Ҳ˵,һһʱ̲ܴһѭеĲͬ״̬ͱ֤ϢͬƵԣ,ԴڲͬѭеĲͬ״̬,ֻҪϢһ״̬ĿɽϢΪɽϢ.ʵ,ͬ״̬ѭֻǴӲͬǶ״̬仯̼ص.
3.3 
Ϊʹͬǰ˵ķԺͶҪ,Կķ,νĹ.
ڿ,ˣΪܽͬʩӵ,ڿ֮佨һĹϵ,Ա֤֮һ.ֹϵӦ:(1) ֮Ĺϵ(2) 󷽷巽֮Ĺϵ.Բʽ:
connecting control-class-name;
by ϵ塵 endݣ
Сϵ塵ϸ̻˳Լ󷽷巽֮Ĺϵ.
ۿζ,֮Ĺϵͨʽʽַ.ʽķҪͬԭ.ʽconnecting֮ȷ󷽷巽֮Ĺϵ.
.ʽ壺ȷ֮䡢󷽷巽֮Ĺϵ.
.ʽ壺һЩ,󷽷巽ͬ,Ϊֱ֮ӵĹϵ.
ûʹóͬƲֿʮڲͬ,ʺЩͨõͬģʽ,ͬó̶.
3.4 ļ̳
ڿһ,ĺܶ.,̳ǺҪҲǶкܴõ֮һ.
̳ͨлƼ̳ие.̳еʱһ˵:һûз״̬仯ļ̳,һ־ǳ״̬仯ļ̳.1ܼ,Ǹıһ³״̬󷽷ĶӦϵ.ﲻϸ.潫˵״̬仯ʱĴ.
һ˵,̳иԸ״̬ı仯: Ը״̬ķֽ, һЩµ״̬.,Ӧdecomposeaddַ.
(1) ״̬̳еdecompose
״ֽ̬쳣,Բdecompose.
decompose state into state-list;
ûҪ״ֽ̬ļ̳쳣.Ҫֽĸеĳ״̬stateͨdecomposeзֽΪ״̬state-list,ı״ֱ̬Ӽ̳йҲͲڳ״̬state.Ӧ״̬ĿɽϢҲӦض壨ܼ̳е̳й.ԸϢϵòsuper.*.
(2) ״̬̳еadd
ûƾµĳ״̬,ԭе״̬Լ̳еķʽ̳й,ȻЧ.
add state-list;
ʾнӡstate-listеµ״̬.е״̬̳йȻЧ.ʱ,ԭйڳ״̬ĿɽϢĶԼ̳й,Ӷ״̬ĿɽϢĶ.
ڲַ,һĶ巽.
4  
,ǾǰܵķȳFIFOн绺ļֵ͵.н绺put()get():put()һԪص,get()ӻɾһȽԪ.sizeʾǰԪصĿ,MAXʾ.Ȼ,ڲ,getputĵҪӦͬԼ.ǰܵķɶ建:
control classbuffer-sync {οඨ
stateempty,partial,full;γ״̬
int size,MAX;γ
methodget,put;γ󷽷
relation
empty:=(size==0);
partial:=(0sizeMAX);
full:=(size==MAX);οɽϢĶ
M(empty){put};
M(partial){get,put};
M(full){get}
}
classbuffer{ξඨ
connectingbuffer-syncʽbuffer-sync
intsize,bufMAX,...;
public
intget() {...ȡһԪ
size--;}
voidput() {...βһԪ
size++;}
}
4.1 get2ļ̳쳣
bufferx-bufferм·get2()(ͬʱȡ߻еԪ)ʱ,״ֽ̬쳣.,decomposeƽ:
control clasx-buffer-sync:buffer-sync{μ̳buffer-syncͬ
stateempty,full ̳и;
decompose partialinto x-one,x-partial;ηֽ״̬partial
methodget2;γ󷽷,get,put̳й
relation
x-one:=(size==1);
x-partial:=(1sizeMAX);
ΡɽϢĶ,ΪϵĲ
M(x-one)super.M(partial);
M(x-partial){get2}super.M(partial);
M(full){get2}super.M(full)
}
classx-buffer:buffer{ξඨ
connectingx-buffer-syncʽx-buffer-sync
publicʵget(),put()̳и;
get2(){...ȡԪ
size:=size-2;}
}
4.2 ggetļ̳쳣
bufferx-buffer2·gget()˲put()úִ,get()ȫͬ,ʷйصļ̳쳣.ӦAddƿɰ¶:
control class x-buffer2-sync:buffer-sync {οඨ
stateempty,partial,full ̳и;
addgget-stat;״̬gget-stat
boolafter-put;γ,̳й
methodgget;γ󷽷,̳й
relation
gget-stat:=(after-put==false) && super.partial;
M(gget-stat){gget};
behaviorγ״̬ת
{put}{after-put==true;}put̳и
default{after-put==false;}ȱʡ״̬仯
}
classx-buffer2:buffer{ξඨ
connectingx-buffer2-syncʽx-buffer2-sync
publicʵget(),put()̳и;
gget(){super.get()}
}
4.3 Ƶ
ԶߺдģʽΪ˵.д֮䡢д֮뻥;֮ͬʱУȻùҲͨʵ,˵Ƶص㣩.
R-opW-opֱдķ.Զ3ֳ״̬:idle-state,read-state,write-state.״̬idle-stateʾǰûκηִ,Խնд;״̬read-stateʾǰжִ,Ϊ֤,ֻܽն,ܽд;״̬write-stateʾǰڽд,ܾκ.üʾǰд.嶨:
control classreader-writer{οĶ
stateidle-state,read-state,write-state;
intr-count,w-count;γ
method
R-op{r-count++;}γ󷽷
W-op{w-count++;}
relation
idle-state:=((r-count==0) && (w-count==0));
read-state:=((w-count==0) && (r-count!=0));
write-state:=(w-count==1);
M(idle-state)R-opW-op;
M(read-state)R-op;
M(write-state){ };
behavior״̬ת
R-op{r-count--;}
W-op{w-count--;}
}
,ϣһ,෽:һǹڲԻ״̬ķ,emptyշ񣩵;һǸı仺ڲԪصķ,get,put.Ҫ෽-дģʽͬ,1෽൱ڶ-дģʽеġ,2൱ڶ-дģʽеġд.Ӧùƶ:
class rw-buffer:buffer{ buffer
connecting reader-writerʽreader-writer
byR-op{empty};
 W-op{get,put};
end;
publicget(),put()̳и;
empty(){β⻺շ
if (size==0) return true;
else return false;}
}
5  
Ϊ,Ҫͬĸṩȫλ֧,ӸϽ̳쳣,ͬķ롢ֽͶ3.Ϊֹ,δһַͬʱ3Ҫ.ˣ˫˼ͬ,Խ̳쳣µĳ.
ǿԿ,Ķ֮ȫ,һʹಿڼ̳еʱܿĸ,һҲʹͬƴ,ܾ.Ӷṩ塢ͬԼͬ3̶ͬȵ֧.
ؿǵǲͬĲԼڲͬĲ⣨ͬͬһͬʱͬķ,ͬһڲĲⲻǱ۵ķΧ.ͬڲмĲ,ԹΪڲĲ.
һĹǽ˼벢ԼԺͲ֮,̽ʵּ.һ,Ŀǰڲеļ̳Ҫڱͬļ̳,Эͬļ̳δ漰,ֵÿչһ.
*оõȻѧ𡢹ҽܳѧ͹ʵǼƻ. 
߼顡,1973,ʿ,ҪоΪ,ʽ.
, 1960,ʿ,,ʿʦ,ҪоΪԶ,гʽ, Ժͻ.
,1972,ʿ,ҪоΪ,ʽ.
ƽ,1970,ʦ,ҪоΪϵṹ,Ӧÿ,Զ.
ͨѶϵ:,Ͼ 210093,Ͼѧо
ߵλϾѧ¼صʵ Ͼ 210093Ͼѧо Ͼ 210093
E-mail: lj@netra.nju.edu.cn
ο
1Ҹ.ʽ.Ͼ:Ͼѧ,1992
(Xu Jia-fu et al. Object-oriented Programming Languages. Nanjing: Nanjing University Press, 1992)
2Yokote Yasuhiko, Tokoro Mario. Concurrent programming in concurrent Smalltalk. Object-oriented Concurrent Programming. Cambridge: MIT Press, 1987
3America Pierre. POOL-T: a parallel object-oriented language. Object-oriented Concurrent Programming. Cambridge, MA: MIT Press, 1987
4Wyatt B et al. Parallelism in object-oriented languages: a survey. IEEE Software, 1992,9(11):5667
5Matsuoka Staoshi, Yonezawa Akinori. Analysis of inheritance anomaly in object-oriented concurrent programming languages. In: Agha G, Wegner G, Yonezawa Akinori eds. Research Directions in Concurrent Object-oriented Programming. Cambridge, MA: MIT Press, 1993
6Kafura D G, Lee K H. Inheritance in actor based concurrent object-oriented languages. In: Cook S ed. Proceedings of the European Conference'89 on Object-oriented Programming. Cambridge: Cambridge University Press, 1989
7Matsuoka Satoshi et al. Highly efficient and encapsulated reuse of synchronization code in concurrent object-oriented languages. ACM Sigplan Notices, 1993,28(10):109126
8Frflund S. Inheritance of synchronization constraints in concurrent object-oriented concurrent languages. In: Madsen O L ed. Proceedings of the European Conference'92 on Object-oriented Programming. Berlin, Heidelberg: Springer-Verlag, 1992. 185196
1998-04-03յԭ,1998-06-22յ޸ĸ
