1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
|
{%skiptarget=$nothread }
{$ifdef fpc}
{$mode objfpc}
{$h+}
{$endif}
uses
{$ifdef unix}
cthreads,
{$endif}
SysUtils, Classes;
var
lock: TMultiReadExclusiveWriteSynchronizer;
event1, event2: prtlevent;
gcount: longint;
gotdeadlockexception,
waiting: boolean;
type
terrorcheck = class(tthread)
procedure execute; override;
end;
tcounter = class(tthread)
private
flock: TMultiReadExclusiveWriteSynchronizer;
flocalcount: longint;
public
constructor create;
property localcount: longint read flocalcount;
end;
treadcounter = class(tcounter)
procedure execute; override;
end;
twritecounter = class(tcounter)
procedure execute; override;
end;
treadwritecounter = class(tcounter)
private
ftrywriteupgrade: boolean;
public
constructor create(trywriteupgrade: boolean);
procedure execute; override;
end;
tdeadlock1 = class(tthread)
procedure execute; override;
end;
tdeadlock2 = class(tthread)
procedure execute; override;
end;
tdoublereadonewrite1 = class(tthread)
procedure execute; override;
end;
tdoublereadonewrite2 = class(tthread)
procedure execute; override;
end;
twrongthreadendacquire = class(tthread)
ftestwrongreadrelease: boolean;
constructor create(testwrongreadrelease: boolean);
procedure execute; override;
end;
twrongthreadendrelease = class(tthread)
ftestwrongreadrelease: boolean;
constructor create(testwrongreadrelease: boolean);
procedure execute; override;
end;
tdoublewrite = class(tthread)
fsecondwritethread: boolean;
constructor create(secondwritethread: boolean);
procedure execute; override;
end;
constructor tcounter.create;
begin
{ create suspended }
inherited create(true);
freeonterminate:=false;
flock:=lock;
flocalcount:=0;
end;
procedure treadcounter.execute;
var
i: longint;
l: longint;
r: longint;
begin
for i:=1 to 100000 do
begin
lock.beginread;
inc(flocalcount);
l:=gcount;
{ guarantee at least one sleep }
if i=50000 then
sleep(20+random(30))
else if (random(10000)=0) then
sleep(20);
{ this must cause data races/loss at some point }
gcount:=l+1;
lock.endread;
r:=random(30000);
if (r=0) then
sleep(30);
end;
end;
procedure twritecounter.execute;
var
i: longint;
l: longint;
r: longint;
begin
for i:=1 to 500 do
begin
lock.beginwrite;
inc(flocalcount);
l:=gcount;
{ guarantee at least one sleep }
if i=250 then
sleep(20+random(30))
else if (random(100)=0) then
sleep(20);
{ we must be exclusive }
if gcount<>l then
begin
writeln('error 1');
halt(1);
end;
gcount:=l+1;
lock.endwrite;
r:=random(30);
if (r>28) then
sleep(r);
end;
end;
constructor treadwritecounter.create(trywriteupgrade: boolean);
begin
ftrywriteupgrade:=trywriteupgrade;
inherited create;
end;
procedure treadwritecounter.execute;
var
i: longint;
l: longint;
r: longint;
begin
for i:=1 to 100000 do
begin
lock.beginread;
if ftrywriteupgrade and
((i=50000) or
(random(10000)=0)) then
begin
inc(flocalcount);
lock.beginwrite;
l:=gcount;
{ guarantee at least one sleep }
if i=50000 then
sleep(20+random(30))
else if (random(5)=0) then
sleep(20);
lock.beginwrite;
gcount:=l+1;
lock.endwrite;
lock.endwrite;
end;
lock.endread;
r:=random(30000);
if (r=0) then
sleep(30);
end;
end;
procedure tdeadlock1.execute;
var
localgotdeadlockexception: boolean;
begin
localgotdeadlockexception:=false;
lock.beginread;
RTLEventSetEvent(event2);
RTLEventWaitFor(event1);
try
lock.beginwrite;
except
localgotdeadlockexception:=true;
gotdeadlockexception:=true;
end;
if not localgotdeadlockexception then
lock.endwrite;
lock.endread;
end;
procedure tdeadlock2.execute;
var
localgotdeadlockexception: boolean;
begin
localgotdeadlockexception:=false;
lock.beginread;
RTLEventSetEvent(event1);
RTLEventWaitFor(event2);
try
lock.beginwrite;
except
localgotdeadlockexception:=true;
gotdeadlockexception:=true;
end;
if not localgotdeadlockexception then
lock.endwrite;
lock.endread;
end;
procedure tdoublereadonewrite1.execute;
begin
// 1)
lock.beginread;
// 2)
RTLEventSetEvent(event2);
// 5)
RTLEventWaitFor(event1);
{ ensure tdoublereadonewrite2 has time to get stuck in beginwrite }
sleep(500);
// 6)
lock.beginread;
// 7)
lock.endread;
// 8)
lock.endread;
end;
procedure tdoublereadonewrite2.execute;
begin
// 3)
RTLEventWaitFor(event2);
// 4)
RTLEventSetEvent(event1);
// 4a -- block until after 8)
lock.beginwrite;
// 9)
lock.endwrite;
end;
constructor twrongthreadendacquire.create(testwrongreadrelease: boolean);
begin
ftestwrongreadrelease:=testwrongreadrelease;
inherited create(false);
end;
procedure twrongthreadendacquire.execute;
begin
if ftestwrongreadrelease then
lock.beginread
else
lock.beginwrite;
RTLEventSetEvent(event1);
RTLEventWaitFor(event2);
try
if ftestwrongreadrelease then
lock.endread
else
lock.endwrite;
except
halt(30);
end;
end;
constructor twrongthreadendrelease.create(testwrongreadrelease: boolean);
begin
ftestwrongreadrelease:=testwrongreadrelease;
inherited create(false);
end;
procedure twrongthreadendrelease.execute;
var
caught: boolean;
begin
RTLEventWaitFor(event1);
caught:=false;
try
if ftestwrongreadrelease then
lock.endread
else
lock.endwrite;
except
caught:=true;
end;
RTLEventSetEvent(event2);
if not caught then
halt(40);
end;
constructor tdoublewrite.create(secondwritethread: boolean);
begin
fsecondwritethread:=secondwritethread;
inherited create(false);
end;
procedure tdoublewrite.execute;
begin
if fsecondwritethread then
begin
RTLEventWaitFor(event1);
if lock.beginwrite then
halt(50);
end
else
begin
if not lock.beginwrite then
halt(51);
RTLEventSetEvent(event1);
// give the other thread the time to get to its beginwrite call
Sleep(500);
end;
lock.endwrite;
end;
procedure terrorcheck.execute;
begin
{ make sure we don't exit before this thread has initialised, since }
{ it can allocate memory in its initialisation, which would cause }
{ problems for heaptrc as it goes over the memory map in its exit code }
waiting:=true;
{ avoid deadlocks/bugs from causing this test to never quit }
sleep(1000*60);
writeln('error 4');
halt(4);
end;
var
r1,r2,r3,r4,r5,r6: treadcounter;
w1,w2,w3,w4: twritecounter;
rw1,rw2,rw3: treadwritecounter;
d1: tdeadlock1;
d2: tdeadlock2;
dr1: tdoublereadonewrite1;
dr2: tdoublereadonewrite2;
wr1: twrongthreadendacquire;
wr2: twrongthreadendrelease;
dw1, dw2: tdoublewrite;
caught: boolean;
begin
waiting:=false;
terrorcheck.create(false);
randomize;
lock:=TMultiReadExclusiveWriteSynchronizer.create;
event1:=RTLEventCreate;
event2:=RTLEventCreate;
{ verify that the lock is recursive }
if not lock.beginwrite then
halt(10);
if not lock.beginwrite then
halt(11);
lock.endwrite;
lock.endwrite;
{ verify that we can upgrade a read lock to a write lock }
lock.beginread;
if not lock.beginwrite then
halt(12);
lock.endwrite;
lock.endread;
{ verify that owning a write lock does not prevent getting a read lock }
if not lock.beginwrite then
halt(13);
lock.beginread;
lock.endread;
lock.endwrite;
{ verify that calling endread without beginread throws an exception }
caught:=false;
try
lock.endread;
except
caught:=true;
end;
if not caught then
halt(14);
{ verify that calling endwrite without beginwrite throws an exception }
caught:=false;
try
lock.endwrite;
except
caught:=true;
end;
if not caught then
halt(15);
{ first try some writers }
w1:=twritecounter.create;
w2:=twritecounter.create;
w3:=twritecounter.create;
w4:=twritecounter.create;
w1.resume;
w2.resume;
w3.resume;
w4.resume;
w1.waitfor;
w2.waitfor;
w3.waitfor;
w4.waitfor;
{ must not have caused any data races }
if (gcount<>w1.localcount+w2.localcount+w3.localcount+w4.localcount) then
begin
writeln('error 2');
halt(2);
end;
w1.free;
w2.free;
w3.free;
w4.free;
{ mixed readers and writers with proper synchronisation }
gcount:=0;
rw1:=treadwritecounter.create(true);
rw2:=treadwritecounter.create(false);
rw3:=treadwritecounter.create(false);
rw1.resume;
rw2.resume;
rw3.resume;
rw1.waitfor;
rw2.waitfor;
rw3.waitfor;
{ must not have caused any data races }
if (gcount<>rw1.localcount+rw2.localcount+rw3.localcount) then
begin
writeln('error 5');
halt(5);
end;
RTLEventResetEvent(event1);
RTLEventResetEvent(event2);
{ check deadlock detection }
d1:=tdeadlock1.create(false);
d2:=tdeadlock2.create(false);
d1.waitfor;
d2.waitfor;
if not gotdeadlockexception then
halt(6);
d1.free;
d2.free;
{ check that a waiting writer does not block a reader trying to get
a recursive read lock it already holds }
dr1:=tdoublereadonewrite1.create(false);
dr2:=tdoublereadonewrite2.create(false);
dr1.waitfor;
dr2.waitfor;
dr1.free;
dr2.free;
{ check that releasing a lock in another thread compared to where it
was acquired causes an exception }
wr1:=twrongthreadendacquire.create(true);
wr2:=twrongthreadendrelease.create(true);
wr1.waitfor;
wr2.waitfor;
wr1.free;
wr2.free;
wr1:=twrongthreadendacquire.create(false);
wr2:=twrongthreadendrelease.create(false);
wr1.waitfor;
wr2.waitfor;
wr1.free;
wr2.free;
dw1:=tdoublewrite.create(false);
dw2:=tdoublewrite.create(true);
dw1.waitfor;
dw2.waitfor;
dw1.free;
dw2.free;
RTLEventDestroy(event1);
RTLEventDestroy(event2);
lock.free;
while not waiting do
sleep(20);
end.
|