mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-15 14:19:28 +02:00
Fix for trwsync.pp sporadic failure that appeared in trunk.
The problem was in the source of random related functions, which are used in this test by several thread in parallel, while this code is known and advertised as non-thread safe. The access by multiple threads led to multiple increments of the mt_index static variable, which was then never reset to zero and led to all successive calls to random function to return zero. This fix simply insures that the mt_state array is never accessed past its end, and that mt_index is reset correctly even if multiple increments do happen. It does not make the random function thread-safe. * Use local variable l_index to avoid accessing mt_state array past last element. * Change mt_index=MTWIST_N+1 into l_index>=MTWIST_N+1 to insure that mt_init will be called again if index is past end. git-svn-id: trunk@34320 -
This commit is contained in:
parent
cdda4da2d3
commit
b9e336dcbe
@ -606,9 +606,13 @@ end;
|
||||
|
||||
|
||||
function mtwist_u32rand: cardinal;
|
||||
var
|
||||
l_index :cardinal;
|
||||
begin
|
||||
l_index:=mt_index;
|
||||
inc(mt_index);
|
||||
if (RandSeed<>OldRandSeed) or
|
||||
(mt_index=MTWIST_N+1) then
|
||||
(l_index>=MTWIST_N+1) then
|
||||
begin
|
||||
mtwist_init(RandSeed);
|
||||
{ Detect resets of randseed
|
||||
@ -619,11 +623,14 @@ begin
|
||||
}
|
||||
RandSeed:=not(RandSeed);
|
||||
OldRandSeed:=RandSeed;
|
||||
l_index:=MTWIST_N;
|
||||
end;
|
||||
if mt_index=MTWIST_N then
|
||||
mtwist_update_state;
|
||||
result:=mt_state[mt_index];
|
||||
inc(mt_index);
|
||||
begin
|
||||
mtwist_update_state;
|
||||
l_index:=0;
|
||||
end;
|
||||
result:=mt_state[l_index];
|
||||
result:=result xor (result shr 11);
|
||||
result:=result xor ((result shl 7) and cardinal($9D2C5680));
|
||||
result:=result xor ((result shl 15) and cardinal($EFC60000));
|
||||
|
Loading…
Reference in New Issue
Block a user