text stringlengths 0 234 |
|---|
function Unreliable_Slow_Add (x : Integer) return Integer is |
Error_Rate : Constant := 0.125; -- (between 0 and 1) |
Random_Num : Float := 0.0; |
begin |
Random_Num := Random(Gen); |
delay Duration(Random_Num * 4.0); |
if Random_Num > Error_Rate then |
return x + 10; |
else |
raise Count_Failed; |
end if; |
end Unreliable_Slow_Add; |
task type Transaction_Worker (Initial : Integer; Manager : access Transaction_Manager); |
task body Transaction_Worker is |
Num : Integer := Initial; |
Prev : Integer := Num; |
Round_Num : Integer := 0; |
begin |
Put_Line ("Worker" & Integer'Image(Initial) & " started"); |
loop |
Put_Line ("Worker" & Integer'Image(Initial) & " started round" & Integer'Image(Round_Num)); |
Round_Num := Round_Num + 1; |
-- PART 1 -- |
select |
Manager.Wait_Until_Aborted; |
Num := Prev + 5; |
Put_Line( "Forward error recovery: Worker" |
& Integer'Image( Initial ) & " commiting" & Integer'Image( Num ) ); |
then abort |
begin |
Num := Unreliable_Slow_Add( Num ); |
Manager.Finished; |
Put_Line (" Worker" & Integer'Image(Initial) & " comitting" & Integer'Image(Num)); |
exception |
when Count_Failed => |
begin |
Put_Line( " Worker" & Integer'Image( Initial ) & " aborting" ); |
Manager.Signal_Abort; |
Manager.Finished; |
Function Definition: procedure exercise7 is |
Function Body: Count_Failed : exception; -- Exception to be raised when counting fails |
Gen : Generator; -- Random number generator |
protected type Transaction_Manager (N : Positive) is |
entry Finished; |
function Commit return Boolean; |
procedure Signal_Abort; |
private |
Finished_Gate_Open : Boolean := False; |
Aborted : Boolean := False; |
Should_Commit : Boolean := True; |
end Transaction_Manager; |
protected body Transaction_Manager is |
entry Finished when Finished_Gate_Open or Finished'Count = N is |
begin |
------------------------------------------ |
-- PART 3: Complete the exit protocol here |
------------------------------------------ |
if Finished'Count = N - 1 then |
Finished_Gate_Open := True; |
Should_Commit := True; |
end if; |
if Aborted then |
Should_Commit := False; |
end if; |
if Finished'Count = 0 then |
Finished_Gate_Open := False; |
Aborted := False; |
end if; |
end Finished; |
procedure Signal_Abort is |
begin |
Aborted := True; |
end Signal_Abort; |
function Commit return Boolean is |
begin |
return Should_Commit; |
end Commit; |
end Transaction_Manager; |
function Unreliable_Slow_Add (x : Integer) return Integer is |
Error_Rate : Constant := 0.15; -- (between 0 and 1) |
begin |
------------------------------------------- |
-- PART 1: Create the transaction work here |
------------------------------------------- |
Random_Num := Random(Gen); |
delay Duration(Random_Num * 4.0); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.