[de19b7]: tests / threads.impure.lisp Maximize Restore History

Download this file

threads.impure.lisp    1406 lines (1256 with data), 55.3 kB

   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
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
;;;; miscellaneous tests of thread stuff
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; While most of SBCL is derived from the CMU CL system, the test
;;;; files (like this one) were written from scratch after the fork
;;;; from CMU CL.
;;;
;;;; This software is in the public domain and is provided with
;;;; absoluely no warranty. See the COPYING and CREDITS files for
;;;; more information.
; WHITE-BOX TESTS
(in-package "SB-THREAD")
(use-package :test-util)
(use-package "ASSERTOID")
(setf sb-unix::*on-dangerous-wait* :error)
(defun wait-for-threads (threads)
(mapc (lambda (thread) (sb-thread:join-thread thread :default nil)) threads)
(assert (not (some #'sb-thread:thread-alive-p threads))))
(with-test (:name (:threads :trivia))
(assert (eql 1 (length (list-all-threads))))
(assert (eq *current-thread*
(find (thread-name *current-thread*) (list-all-threads)
:key #'thread-name :test #'equal)))
(assert (thread-alive-p *current-thread*)))
(with-test (:name (:with-mutex :basics))
(let ((mutex (make-mutex)))
(with-mutex (mutex)
mutex)))
(sb-alien:define-alien-routine "check_deferrables_blocked_or_lose"
void
(where sb-alien:unsigned-long))
(sb-alien:define-alien-routine "check_deferrables_unblocked_or_lose"
void
(where sb-alien:unsigned-long))
(with-test (:name (:interrupt-thread :basics :no-unwinding))
(let ((a 0))
(interrupt-thread *current-thread* (lambda () (setq a 1)))
(assert (eql a 1))))
(with-test (:name (:interrupt-thread :deferrables-blocked))
(sb-thread:interrupt-thread sb-thread:*current-thread*
(lambda ()
(check-deferrables-blocked-or-lose 0))))
(with-test (:name (:interrupt-thread :deferrables-unblocked))
(sb-thread:interrupt-thread sb-thread:*current-thread*
(lambda ()
(with-interrupts
(check-deferrables-unblocked-or-lose 0)))))
(with-test (:name (:interrupt-thread :nlx))
(catch 'xxx
(sb-thread:interrupt-thread sb-thread:*current-thread*
(lambda ()
(check-deferrables-blocked-or-lose 0)
(throw 'xxx nil))))
(check-deferrables-unblocked-or-lose 0))
#-sb-thread (sb-ext:quit :unix-status 104)
;;;; Now the real tests...
(with-test (:name (:interrupt-thread :deferrables-unblocked-by-lock))
(let ((lock (sb-thread::make-mutex))
(thread (sb-thread:make-thread (lambda ()
(loop (sleep 1))))))
(sb-thread::grab-mutex lock)
(sb-thread:interrupt-thread thread
(lambda ()
(check-deferrables-blocked-or-lose 0)
(sb-thread::grab-mutex lock)
(check-deferrables-unblocked-or-lose 0)
(sb-ext:quit)))
(sleep 1)
(sb-thread::release-mutex lock)))
;;; compare-and-swap
(defmacro defincf (name accessor &rest args)
`(defun ,name (x)
(let* ((old (,accessor x ,@args))
(new (1+ old)))
(loop until (eq old (sb-ext:compare-and-swap (,accessor x ,@args) old new))
do (setf old (,accessor x ,@args)
new (1+ old)))
new)))
(defstruct cas-struct (slot 0))
(defincf incf-car car)
(defincf incf-cdr cdr)
(defincf incf-slot cas-struct-slot)
(defincf incf-symbol-value symbol-value)
(defincf incf-svref/1 svref 1)
(defincf incf-svref/0 svref 0)
(defmacro def-test-cas (name init incf op)
`(with-test (:name ,name)
(flet ((,name (n)
(declare (fixnum n))
(let* ((x ,init)
(run nil)
(threads
(loop repeat 10
collect (sb-thread:make-thread
(lambda ()
(loop until run
do (sb-thread:thread-yield))
(loop repeat n do (,incf x)))))))
(setf run t)
(dolist (th threads)
(sb-thread:join-thread th))
(assert (= (,op x) (* 10 n))))))
(,name 200000))))
(def-test-cas test-cas-car (cons 0 nil) incf-car car)
(def-test-cas test-cas-cdr (cons nil 0) incf-cdr cdr)
(def-test-cas test-cas-slot (make-cas-struct) incf-slot cas-struct-slot)
(def-test-cas test-cas-value (let ((x '.x.))
(set x 0)
x)
incf-symbol-value symbol-value)
(def-test-cas test-cas-svref/0 (vector 0 nil) incf-svref/0 (lambda (x)
(svref x 0)))
(def-test-cas test-cas-svref/1 (vector nil 0) incf-svref/1 (lambda (x)
(svref x 1)))
(format t "~&compare-and-swap tests done~%")
(with-test (:name (:threads :more-trivia)))
(let ((old-threads (list-all-threads))
(thread (make-thread (lambda ()
(assert (find *current-thread* *all-threads*))
(sleep 2))))
(new-threads (list-all-threads)))
(assert (thread-alive-p thread))
(assert (eq thread (first new-threads)))
(assert (= (1+ (length old-threads)) (length new-threads)))
(sleep 3)
(assert (not (thread-alive-p thread))))
(with-test (:name (:join-thread :nlx :default))
(let ((sym (gensym)))
(assert (eq sym (join-thread (make-thread (lambda () (sb-ext:quit)))
:default sym)))))
(with-test (:name (:join-thread :nlx :error))
(raises-error? (join-thread (make-thread (lambda () (sb-ext:quit))))
join-thread-error))
(with-test (:name (:join-thread :multiple-values))
(assert (equal '(1 2 3)
(multiple-value-list
(join-thread (make-thread (lambda () (values 1 2 3))))))))
;;; We had appalling scaling properties for a while. Make sure they
;;; don't reappear.
(defun scaling-test (function &optional (nthreads 5))
"Execute FUNCTION with NTHREADS lurking to slow it down."
(let ((queue (sb-thread:make-waitqueue))
(mutex (sb-thread:make-mutex)))
;; Start NTHREADS idle threads.
(dotimes (i nthreads)
(sb-thread:make-thread (lambda ()
(with-mutex (mutex)
(sb-thread:condition-wait queue mutex))
(sb-ext:quit))))
(let ((start-time (get-internal-run-time)))
(funcall function)
(prog1 (- (get-internal-run-time) start-time)
(sb-thread:condition-broadcast queue)))))
(defun fact (n)
"A function that does work with the CPU."
(if (zerop n) 1 (* n (fact (1- n)))))
(let ((work (lambda () (fact 15000))))
(let ((zero (scaling-test work 0))
(four (scaling-test work 4)))
;; a slightly weak assertion, but good enough for starters.
(assert (< four (* 1.5 zero)))))
;;; For one of the interupt-thread tests, we want a foreign function
;;; that does not make syscalls
(with-open-file (o "threads-foreign.c" :direction :output :if-exists :supersede)
(format o "void loop_forever() { while(1) ; }~%"))
(sb-ext:run-program "/bin/sh"
'("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
"-o" "threads-foreign.so" "threads-foreign.c")
:environment (test-util::test-env))
(sb-alien:load-shared-object (truename "threads-foreign.so"))
(sb-alien:define-alien-routine loop-forever sb-alien:void)
(delete-file "threads-foreign.c")
;;; elementary "can we get a lock and release it again"
(with-test (:name (:mutex :basics))
(let ((l (make-mutex :name "foo"))
(p *current-thread*))
(assert (eql (mutex-value l) nil) nil "1")
(sb-thread:get-mutex l)
(assert (eql (mutex-value l) p) nil "3")
(sb-thread:release-mutex l)
(assert (eql (mutex-value l) nil) nil "5")))
(with-test (:name (:with-recursive-lock :basics))
(labels ((ours-p (value)
(eq *current-thread* value)))
(let ((l (make-mutex :name "rec")))
(assert (eql (mutex-value l) nil) nil "1")
(sb-thread:with-recursive-lock (l)
(assert (ours-p (mutex-value l)) nil "3")
(sb-thread:with-recursive-lock (l)
(assert (ours-p (mutex-value l)) nil "4"))
(assert (ours-p (mutex-value l)) nil "5"))
(assert (eql (mutex-value l) nil) nil "6"))))
(with-test (:name (:mutex :nesting-mutex-and-recursive-lock))
(let ((l (make-mutex :name "a mutex")))
(with-mutex (l)
(with-recursive-lock (l)))))
;; test that SLEEP actually sleeps for at least the given time, even
;; if interrupted by another thread exiting/a gc/anything
(with-test (:name (:sleep :continue-sleeping-after-interrupt))
(let ((start-time (get-universal-time)))
(make-thread (lambda () (sleep 1) (sb-ext:gc :full t)))
(sleep 5)
(assert (>= (get-universal-time) (+ 5 start-time)))))
(with-test (:name (:condition-wait :basics-1))
(let ((queue (make-waitqueue :name "queue"))
(lock (make-mutex :name "lock"))
(n 0))
(labels ((in-new-thread ()
(with-mutex (lock)
(assert (eql (mutex-value lock) *current-thread*))
(format t "~A got mutex~%" *current-thread*)
;; now drop it and sleep
(condition-wait queue lock)
;; after waking we should have the lock again
(assert (eql (mutex-value lock) *current-thread*))
(assert (eql n 1))
(decf n))))
(make-thread #'in-new-thread)
(sleep 2) ; give it a chance to start
;; check the lock is free while it's asleep
(format t "parent thread ~A~%" *current-thread*)
(assert (eql (mutex-value lock) nil))
(with-mutex (lock)
(incf n)
(condition-notify queue))
(sleep 1))))
(with-test (:name (:condition-wait :basics-2))
(let ((queue (make-waitqueue :name "queue"))
(lock (make-mutex :name "lock")))
(labels ((ours-p (value)
(eq *current-thread* value))
(in-new-thread ()
(with-recursive-lock (lock)
(assert (ours-p (mutex-value lock)))
(format t "~A got mutex~%" (mutex-value lock))
;; now drop it and sleep
(condition-wait queue lock)
;; after waking we should have the lock again
(format t "woken, ~A got mutex~%" (mutex-value lock))
(assert (ours-p (mutex-value lock))))))
(make-thread #'in-new-thread)
(sleep 2) ; give it a chance to start
;; check the lock is free while it's asleep
(format t "parent thread ~A~%" *current-thread*)
(assert (eql (mutex-value lock) nil))
(with-recursive-lock (lock)
(condition-notify queue))
(sleep 1))))
(with-test (:name (:mutex :contention))
(let ((mutex (make-mutex :name "contended")))
(labels ((run ()
(let ((me *current-thread*))
(dotimes (i 100)
(with-mutex (mutex)
(sleep .03)
(assert (eql (mutex-value mutex) me)))
(assert (not (eql (mutex-value mutex) me))))
(format t "done ~A~%" *current-thread*))))
(let ((kid1 (make-thread #'run))
(kid2 (make-thread #'run)))
(format t "contention ~A ~A~%" kid1 kid2)
(wait-for-threads (list kid1 kid2))))))
;;; GRAB-MUTEX
(with-test (:name (:grab-mutex :waitp nil))
(let ((m (make-mutex)))
(with-mutex (m)
(assert (null (join-thread (make-thread
#'(lambda ()
(grab-mutex m :waitp nil)))))))))
(with-test (:name (:grab-mutex :timeout :acquisition-fail))
(let ((m (make-mutex))
(w (make-semaphore)))
(with-mutex (m)
(let ((th (make-thread
#'(lambda ()
(prog1
(grab-mutex m :timeout 0.1)
(signal-semaphore w))))))
;; Wait for it to -- otherwise the detect the deadlock chain
;; from JOIN-THREAD.
(wait-on-semaphore w)
(assert (null (join-thread th)))))))
(with-test (:name (:grab-mutex :timeout :acquisition-success))
(let ((m (make-mutex))
(child))
(with-mutex (m)
(setq child (make-thread #'(lambda () (grab-mutex m :timeout 1.0))))
(sleep 0.2))
(assert (eq (join-thread child) 't))))
(with-test (:name (:grab-mutex :timeout+deadline))
(let ((m (make-mutex))
(w (make-semaphore)))
(with-mutex (m)
(let ((th (make-thread #'(lambda ()
(sb-sys:with-deadline (:seconds 0.0)
(handler-case
(grab-mutex m :timeout 0.0)
(sb-sys:deadline-timeout ()
(signal-semaphore w)
:deadline)))))))
(wait-on-semaphore w)
(assert (eq (join-thread th) :deadline))))))
(with-test (:name (:grab-mutex :waitp+deadline))
(let ((m (make-mutex)))
(with-mutex (m)
(assert (eq (join-thread
(make-thread #'(lambda ()
(sb-sys:with-deadline (:seconds 0.0)
(handler-case
(grab-mutex m :waitp nil)
(sb-sys:deadline-timeout ()
:deadline))))))
'nil)))))
;;; semaphores
(defmacro raises-timeout-p (&body body)
`(handler-case (progn (progn ,@body) nil)
(sb-ext:timeout () t)))
(with-test (:name (:semaphore :wait-forever))
(let ((sem (make-semaphore :count 0)))
(assert (raises-timeout-p
(sb-ext:with-timeout 0.1
(wait-on-semaphore sem))))))
(with-test (:name (:semaphore :initial-count))
(let ((sem (make-semaphore :count 1)))
(sb-ext:with-timeout 0.1
(wait-on-semaphore sem))))
(with-test (:name (:semaphore :wait-then-signal))
(let ((sem (make-semaphore))
(signalled-p nil))
(make-thread (lambda ()
(sleep 0.1)
(setq signalled-p t)
(signal-semaphore sem)))
(wait-on-semaphore sem)
(assert signalled-p)))
(with-test (:name (:semaphore :signal-then-wait))
(let ((sem (make-semaphore))
(signalled-p nil))
(make-thread (lambda ()
(signal-semaphore sem)
(setq signalled-p t)))
(loop until signalled-p)
(wait-on-semaphore sem)
(assert signalled-p)))
(defun test-semaphore-multiple-signals (wait-on-semaphore)
(let* ((sem (make-semaphore :count 5))
(threads (loop repeat 20 collecting
(make-thread (lambda ()
(funcall wait-on-semaphore sem))))))
(flet ((count-live-threads ()
(count-if #'thread-alive-p threads)))
(sleep 0.5)
(assert (= 15 (count-live-threads)))
(signal-semaphore sem 10)
(sleep 0.5)
(assert (= 5 (count-live-threads)))
(signal-semaphore sem 3)
(sleep 0.5)
(assert (= 2 (count-live-threads)))
(signal-semaphore sem 4)
(sleep 0.5)
(assert (= 0 (count-live-threads))))))
(with-test (:name (:semaphore :multiple-signals))
(test-semaphore-multiple-signals #'wait-on-semaphore))
(with-test (:name (:try-semaphore :trivial-fail))
(assert (eq (try-semaphore (make-semaphore :count 0)) 'nil)))
(with-test (:name (:try-semaphore :trivial-success))
(let ((sem (make-semaphore :count 1)))
(assert (try-semaphore sem))
(assert (zerop (semaphore-count sem)))))
(with-test (:name (:try-semaphore :trivial-fail :n>1))
(assert (eq (try-semaphore (make-semaphore :count 1) 2) 'nil)))
(with-test (:name (:try-semaphore :trivial-success :n>1))
(let ((sem (make-semaphore :count 10)))
(assert (try-semaphore sem 5))
(assert (try-semaphore sem 5))
(assert (zerop (semaphore-count sem)))))
(with-test (:name (:try-semaphore :emulate-wait-on-semaphore))
(flet ((busy-wait-on-semaphore (sem)
(loop until (try-semaphore sem) do (sleep 0.001))))
(test-semaphore-multiple-signals #'busy-wait-on-semaphore)))
;;; Here we test that interrupting TRY-SEMAPHORE does not leave a
;;; semaphore in a bad state.
(with-test (:name (:try-semaphore :interrupt-safe))
(flet ((make-threads (count fn)
(loop repeat count collect (make-thread fn)))
(kill-thread (thread)
(when (thread-alive-p thread)
(ignore-errors (terminate-thread thread))))
(count-live-threads (threads)
(count-if #'thread-alive-p threads)))
;; WAITERS will already be waiting on the semaphore while
;; threads-being-interrupted will perform TRY-SEMAPHORE on that
;; semaphore, and MORE-WAITERS are new threads trying to wait on
;; the semaphore during the interruption-fire.
(let* ((sem (make-semaphore :count 100))
(waiters (make-threads 20 #'(lambda ()
(wait-on-semaphore sem))))
(triers (make-threads 40 #'(lambda ()
(sleep (random 0.01))
(try-semaphore sem (1+ (random 5))))))
(more-waiters
(loop repeat 10
do (kill-thread (nth (random 40) triers))
collect (make-thread #'(lambda () (wait-on-semaphore sem)))
do (kill-thread (nth (random 40) triers)))))
(sleep 0.5)
;; Now ensure that the waiting threads will all be waked up,
;; i.e. that the semaphore is still working.
(loop repeat (+ (count-live-threads waiters)
(count-live-threads more-waiters))
do (signal-semaphore sem))
(sleep 0.5)
(assert (zerop (count-live-threads triers)))
(assert (zerop (count-live-threads waiters)))
(assert (zerop (count-live-threads more-waiters))))))
(format t "~&semaphore tests done~%")
(defun test-interrupt (function-to-interrupt &optional quit-p)
(let ((child (make-thread function-to-interrupt)))
;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
(sleep 2)
(format t "interrupting child ~A~%" child)
(interrupt-thread child
(lambda ()
(format t "child pid ~A~%" *current-thread*)
(when quit-p (sb-ext:quit))))
(sleep 1)
child))
;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
;; (d) waiting on a lock, (e) some code which we hope is likely to be
;; in pseudo-atomic
(with-test (:name (:interrupt-thread :more-basics))
(let ((child (test-interrupt (lambda () (loop)))))
(terminate-thread child)))
(with-test (:name (:interrupt-thread :interrupt-foreign-loop))
(test-interrupt #'loop-forever :quit))
(with-test (:name (:interrupt-thread :interrupt-sleep))
(let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
(terminate-thread child)
(wait-for-threads (list child))))
(with-test (:name (:interrupt-thread :interrupt-mutex-acquisition))
(let ((lock (make-mutex :name "loctite"))
child)
(with-mutex (lock)
(setf child (test-interrupt
(lambda ()
(with-mutex (lock)
(assert (eql (mutex-value lock) *current-thread*)))
(assert (not (eql (mutex-value lock) *current-thread*)))
(sleep 10))))
;;hold onto lock for long enough that child can't get it immediately
(sleep 5)
(interrupt-thread child (lambda () (format t "l ~A~%" (mutex-value lock))))
(format t "parent releasing lock~%"))
(terminate-thread child)
(wait-for-threads (list child))))
(format t "~&locking test done~%")
(defun alloc-stuff () (copy-list '(1 2 3 4 5)))
(with-test (:name (:interrupt-thread :interrupt-consing-child))
(let ((thread (sb-thread:make-thread (lambda () (loop (alloc-stuff))))))
(let ((killers
(loop repeat 4 collect
(sb-thread:make-thread
(lambda ()
(loop repeat 25 do
(sleep (random 0.1d0))
(princ ".")
(force-output)
(sb-thread:interrupt-thread thread (lambda ()))))))))
(wait-for-threads killers)
(sb-thread:terminate-thread thread)
(wait-for-threads (list thread))))
(sb-ext:gc :full t))
(format t "~&multi interrupt test done~%")
#+(or x86 x86-64) ;; x86oid-only, see internal commentary.
(with-test (:name (:interrupt-thread :interrupt-consing-child :again))
(let ((c (make-thread (lambda () (loop (alloc-stuff))))))
;; NB this only works on x86: other ports don't have a symbol for
;; pseudo-atomic atomicity
(dotimes (i 100)
(sleep (random 0.1d0))
(interrupt-thread c
(lambda ()
(princ ".") (force-output)
(assert (thread-alive-p *current-thread*))
(assert
(not (logbitp 0 SB-KERNEL:*PSEUDO-ATOMIC-BITS*))))))
(terminate-thread c)
(wait-for-threads (list c))))
(format t "~&interrupt test done~%")
(defstruct counter (n 0 :type sb-vm:word))
(defvar *interrupt-counter* (make-counter))
(declaim (notinline check-interrupt-count))
(defun check-interrupt-count (i)
(declare (optimize (debug 1) (speed 1)))
;; This used to lose if eflags were not restored after an interrupt.
(unless (typep i 'fixnum)
(error "!!!!!!!!!!!")))
(with-test (:name (:interrupt-thread :interrupt-ATOMIC-INCF))
(let ((c (make-thread
(lambda ()
(handler-bind ((error #'(lambda (cond)
(princ cond)
(sb-debug:backtrace
most-positive-fixnum))))
(loop (check-interrupt-count
(counter-n *interrupt-counter*))))))))
(let ((func (lambda ()
(princ ".")
(force-output)
(sb-ext:atomic-incf (counter-n *interrupt-counter*)))))
(setf (counter-n *interrupt-counter*) 0)
(dotimes (i 100)
(sleep (random 0.1d0))
(interrupt-thread c func))
(loop until (= (counter-n *interrupt-counter*) 100) do (sleep 0.1))
(terminate-thread c)
(wait-for-threads (list c)))))
(format t "~&interrupt count test done~%")
(defvar *runningp* nil)
(with-test (:name (:interrupt-thread :no-nesting))
(let ((thread (sb-thread:make-thread
(lambda ()
(catch 'xxx
(loop))))))
(declare (special runningp))
(sleep 0.2)
(sb-thread:interrupt-thread thread
(lambda ()
(let ((*runningp* t))
(sleep 1))))
(sleep 0.2)
(sb-thread:interrupt-thread thread
(lambda ()
(throw 'xxx *runningp*)))
(assert (not (sb-thread:join-thread thread)))))
(with-test (:name (:interrupt-thread :nesting))
(let ((thread (sb-thread:make-thread
(lambda ()
(catch 'xxx
(loop))))))
(declare (special runningp))
(sleep 0.2)
(sb-thread:interrupt-thread thread
(lambda ()
(let ((*runningp* t))
(sb-sys:with-interrupts
(sleep 1)))))
(sleep 0.2)
(sb-thread:interrupt-thread thread
(lambda ()
(throw 'xxx *runningp*)))
(assert (sb-thread:join-thread thread))))
(with-test (:name (:two-threads-running-gc))
(let (a-done b-done)
(make-thread (lambda ()
(dotimes (i 100)
(sb-ext:gc) (princ "\\") (force-output))
(setf a-done t)))
(make-thread (lambda ()
(dotimes (i 25)
(sb-ext:gc :full t)
(princ "/") (force-output))
(setf b-done t)))
(loop
(when (and a-done b-done) (return))
(sleep 1))))
(terpri)
(defun waste (&optional (n 100000))
(loop repeat n do (make-string 16384)))
(with-test (:name (:one-thread-runs-gc-while-other-conses))
(loop for i below 100 do
(princ "!")
(force-output)
(sb-thread:make-thread
#'(lambda ()
(waste)))
(waste)
(sb-ext:gc)))
(terpri)
(defparameter *aaa* nil)
(with-test (:name (:one-thread-runs-gc-while-other-conses :again))
(loop for i below 100 do
(princ "!")
(force-output)
(sb-thread:make-thread
#'(lambda ()
(let ((*aaa* (waste)))
(waste))))
(let ((*aaa* (waste)))
(waste))
(sb-ext:gc)))
(format t "~&gc test done~%")
;; this used to deadlock on session-lock
(with-test (:name (:no-session-deadlock))
(sb-thread:make-thread (lambda () (sb-ext:gc))))
(defun exercise-syscall (fn reference-errno)
(sb-thread:make-thread
(lambda ()
(loop do
(funcall fn)
(let ((errno (sb-unix::get-errno)))
(sleep (random 0.1d0))
(unless (eql errno reference-errno)
(format t "Got errno: ~A (~A) instead of ~A~%"
errno
(sb-unix::strerror)
reference-errno)
(force-output)
(sb-ext:quit :unix-status 1)))))))
;; (nanosleep -1 0) does not fail on FreeBSD
(with-test (:name (:exercising-concurrent-syscalls))
(let* (#-freebsd
(nanosleep-errno (progn
(sb-unix:nanosleep -1 0)
(sb-unix::get-errno)))
(open-errno (progn
(open "no-such-file"
:if-does-not-exist nil)
(sb-unix::get-errno)))
(threads
(list
#-freebsd
(exercise-syscall (lambda () (sb-unix:nanosleep -1 0)) nanosleep-errno)
(exercise-syscall (lambda () (open "no-such-file"
:if-does-not-exist nil))
open-errno)
(sb-thread:make-thread (lambda () (loop (sb-ext:gc) (sleep 1)))))))
(sleep 10)
(princ "terminating threads")
(dolist (thread threads)
(sb-thread:terminate-thread thread))))
(format t "~&errno test done~%")
(with-test (:name (:terminate-thread-restart))
(loop repeat 100 do
(let ((thread (sb-thread:make-thread (lambda () (sleep 0.1)))))
(sb-thread:interrupt-thread
thread
(lambda ()
(assert (find-restart 'sb-thread:terminate-thread)))))))
(sb-ext:gc :full t)
(format t "~&thread startup sigmask test done~%")
(with-test (:name (:debugger-no-hang-on-session-lock-if-interrupted))
(sb-debug::enable-debugger)
(let* ((main-thread *current-thread*)
(interruptor-thread
(make-thread (lambda ()
(sleep 2)
(interrupt-thread main-thread
(lambda ()
(with-interrupts
(break))))
(sleep 2)
(interrupt-thread main-thread #'continue))
:name "interruptor")))
(with-session-lock (*session*)
(sleep 3))
(loop while (thread-alive-p interruptor-thread))))
(format t "~&session lock test done~%")
;; expose thread creation races by exiting quickly
(with-test (:name (:no-thread-creation-race :light))
(sb-thread:make-thread (lambda ())))
(with-test (:name (:no-thread-creation-race :heavy))
(loop repeat 20 do
(wait-for-threads
(loop for i below 100 collect
(sb-thread:make-thread (lambda ()))))))
(format t "~&creation test done~%")
;; interrupt handlers are per-thread with pthreads, make sure the
;; handler installed in one thread is global
(with-test (:name (:global-interrupt-handler))
(sb-thread:make-thread
(lambda ()
(sb-ext:run-program "sleep" '("1") :search t :wait nil))))
;;;; Binding stack safety
(defparameter *x* nil)
(defparameter *n-gcs-requested* 0)
(defparameter *n-gcs-done* 0)
(let ((counter 0))
(defun make-something-big ()
(let ((x (make-string 32000)))
(incf counter)
(let ((counter counter))
(sb-ext:finalize x (lambda () (format t " ~S" counter)
(force-output)))))))
(defmacro wait-for-gc ()
`(progn
(incf *n-gcs-requested*)
(loop while (< *n-gcs-done* *n-gcs-requested*))))
(defun send-gc ()
(loop until (< *n-gcs-done* *n-gcs-requested*))
(format t "G")
(force-output)
(sb-ext:gc)
(incf *n-gcs-done*))
(defun exercise-binding ()
(loop
(let ((*x* (make-something-big)))
(let ((*x* 42))
;; at this point the binding stack looks like this:
;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
t))
(wait-for-gc)
;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
;; unbinding but values are not).
(let ((*x* nil)
(binding-pointer-delta (ash 2 (- sb-vm:word-shift sb-vm:n-fixnum-tag-bits))))
;; bump bsp as if a BIND had just started
(incf sb-vm::*binding-stack-pointer* binding-pointer-delta)
(wait-for-gc)
(decf sb-vm::*binding-stack-pointer* binding-pointer-delta))))
(with-test (:name (:binding-stack-gc-safety))
(let (threads)
(unwind-protect
(progn
(push (sb-thread:make-thread #'exercise-binding) threads)
(push (sb-thread:make-thread (lambda ()
(loop
(sleep 0.1)
(send-gc))))
threads)
(sleep 4))
(mapc #'sb-thread:terminate-thread threads))))
(format t "~&binding test done~%")
;;; HASH TABLES
(defvar *errors* nil)
(defun oops (e)
(setf *errors* e)
(format t "~&oops: ~A in ~S~%" e *current-thread*)
(sb-debug:backtrace)
(catch 'done))
(with-test (:name (:unsynchronized-hash-table)
;; FIXME: This test occasionally eats out craploads
;; of heap instead of expected error early. Not 100%
;; sure if it would finish as expected, but since it
;; hits swap on my system I'm not likely to find out
;; soon. Disabling for now. -- nikodemus
:skipped-on :sbcl)
;; We expect a (probable) error here: parellel readers and writers
;; on a hash-table are not expected to work -- but we also don't
;; expect this to corrupt the image.
(let* ((hash (make-hash-table))
(*errors* nil)
(threads (list (sb-thread:make-thread
(lambda ()
(catch 'done
(handler-bind ((serious-condition 'oops))
(loop
;;(princ "1") (force-output)
(setf (gethash (random 100) hash) 'h)))))
:name "writer")
(sb-thread:make-thread
(lambda ()
(catch 'done
(handler-bind ((serious-condition 'oops))
(loop
;;(princ "2") (force-output)
(remhash (random 100) hash)))))
:name "reader")
(sb-thread:make-thread
(lambda ()
(catch 'done
(handler-bind ((serious-condition 'oops))
(loop
(sleep (random 1.0))
(sb-ext:gc :full t)))))
:name "collector"))))
(unwind-protect
(sleep 10)
(mapc #'sb-thread:terminate-thread threads))))
(format t "~&unsynchronized hash table test done~%")
(with-test (:name (:synchronized-hash-table))
(let* ((hash (make-hash-table :synchronized t))
(*errors* nil)
(threads (list (sb-thread:make-thread
(lambda ()
(catch 'done
(handler-bind ((serious-condition 'oops))
(loop
;;(princ "1") (force-output)
(setf (gethash (random 100) hash) 'h)))))
:name "writer")
(sb-thread:make-thread
(lambda ()
(catch 'done
(handler-bind ((serious-condition 'oops))
(loop
;;(princ "2") (force-output)
(remhash (random 100) hash)))))
:name "reader")
(sb-thread:make-thread
(lambda ()
(catch 'done
(handler-bind ((serious-condition 'oops))
(loop
(sleep (random 1.0))
(sb-ext:gc :full t)))))
:name "collector"))))
(unwind-protect
(sleep 10)
(mapc #'sb-thread:terminate-thread threads))
(assert (not *errors*))))
(format t "~&synchronized hash table test done~%")
(with-test (:name (:hash-table-parallel-readers))
(let ((hash (make-hash-table))
(*errors* nil))
(loop repeat 50
do (setf (gethash (random 100) hash) 'xxx))
(let ((threads (list (sb-thread:make-thread
(lambda ()
(catch 'done
(handler-bind ((serious-condition 'oops))
(loop
until (eq t (gethash (random 100) hash))))))
:name "reader 1")
(sb-thread:make-thread
(lambda ()
(catch 'done
(handler-bind ((serious-condition 'oops))
(loop
until (eq t (gethash (random 100) hash))))))
:name "reader 2")
(sb-thread:make-thread
(lambda ()
(catch 'done
(handler-bind ((serious-condition 'oops))
(loop
until (eq t (gethash (random 100) hash))))))
:name "reader 3")
(sb-thread:make-thread
(lambda ()
(catch 'done
(handler-bind ((serious-condition 'oops))
(loop
(sleep (random 1.0))
(sb-ext:gc :full t)))))
:name "collector"))))
(unwind-protect
(sleep 10)
(mapc #'sb-thread:terminate-thread threads))
(assert (not *errors*)))))
(format t "~&multiple reader hash table test done~%")
(with-test (:name (:hash-table-single-accessor-parallel-gc))
(let ((hash (make-hash-table))
(*errors* nil))
(let ((threads (list (sb-thread:make-thread
(lambda ()
(handler-bind ((serious-condition 'oops))
(loop
(let ((n (random 100)))
(if (gethash n hash)
(remhash n hash)
(setf (gethash n hash) 'h))))))
:name "accessor")
(sb-thread:make-thread
(lambda ()
(handler-bind ((serious-condition 'oops))
(loop
(sleep (random 1.0))
(sb-ext:gc :full t))))
:name "collector"))))
(unwind-protect
(sleep 10)
(mapc #'sb-thread:terminate-thread threads))
(assert (not *errors*)))))
(format t "~&single accessor hash table test~%")
#| ;; a cll post from eric marsden
| (defun crash ()
| (setq *debugger-hook*
| (lambda (condition old-debugger-hook)
| (debug:backtrace 10)
| (unix:unix-exit 2)))
| #+live-dangerously
| (mp::start-sigalrm-yield)
| (flet ((roomy () (loop (with-output-to-string (*standard-output*) (room)))))
| (mp:make-process #'roomy)
| (mp:make-process #'roomy)))
|#
(with-test (:name (:condition-variable :notify-multiple))
(flet ((tester (notify-fun)
(let ((queue (make-waitqueue :name "queue"))
(lock (make-mutex :name "lock"))
(data nil))
(labels ((test (x)
(loop
(with-mutex (lock)
(format t "condition-wait ~a~%" x)
(force-output)
(condition-wait queue lock)
(format t "woke up ~a~%" x)
(force-output)
(push x data)))))
(let ((threads (loop for x from 1 to 10
collect
(let ((x x))
(sb-thread:make-thread (lambda ()
(test x)))))))
(sleep 5)
(with-mutex (lock)
(funcall notify-fun queue))
(sleep 5)
(mapcar #'terminate-thread threads)
;; Check that all threads woke up at least once
(assert (= (length (remove-duplicates data)) 10)))))))
(tester (lambda (queue)
(format t "~&(condition-notify queue 10)~%")
(force-output)
(condition-notify queue 10)))
(tester (lambda (queue)
(format t "~&(condition-broadcast queue)~%")
(force-output)
(condition-broadcast queue)))))
(format t "waitqueue wakeup tests done~%")
;;; Make sure that a deadline handler is not invoked twice in a row in
;;; CONDITION-WAIT. See LP #512914 for a detailed explanation.
;;;
(with-test (:name (:condition-wait :deadlines :LP-512914))
(let ((n 2) ; was empirically enough to trigger the bug
(mutex (sb-thread:make-mutex))
(waitq (sb-thread:make-waitqueue))
(threads nil)
(deadline-handler-run-twice? nil))
(dotimes (i n)
(let ((child
(sb-thread:make-thread
#'(lambda ()
(handler-bind
((sb-sys:deadline-timeout
(let ((already? nil))
#'(lambda (c)
(when already?
(setq deadline-handler-run-twice? t))
(setq already? t)
(sleep 0.2)
(sb-thread:condition-broadcast waitq)
(sb-sys:defer-deadline 10.0 c)))))
(sb-sys:with-deadline (:seconds 0.1)
(sb-thread:with-mutex (mutex)
(sb-thread:condition-wait waitq mutex))))))))
(push child threads)))
(mapc #'sb-thread:join-thread threads)
(assert (not deadline-handler-run-twice?))))
(with-test (:name (:condition-wait :signal-deadline-with-interrupts-enabled))
(let ((mutex (sb-thread:make-mutex))
(waitq (sb-thread:make-waitqueue))
(A-holds? :unknown)
(B-holds? :unknown)
(A-interrupts-enabled? :unknown)
(B-interrupts-enabled? :unknown)
(A)
(B))
;; W.L.O.G., we assume that A is executed first...
(setq A (sb-thread:make-thread
#'(lambda ()
(handler-bind
((sb-sys:deadline-timeout
#'(lambda (c)
;; We came here through the call to DECODE-TIMEOUT
;; in CONDITION-WAIT; hence both here are supposed
;; to evaluate to T.
(setq A-holds? (sb-thread:holding-mutex-p mutex))
(setq A-interrupts-enabled?
sb-sys:*interrupts-enabled*)
(sleep 0.2)
(sb-thread:condition-broadcast waitq)
(sb-sys:defer-deadline 10.0 c))))
(sb-sys:with-deadline (:seconds 0.1)
(sb-thread:with-mutex (mutex)
(sb-thread:condition-wait waitq mutex)))))
:name "A"))
(setq B (sb-thread:make-thread
#'(lambda ()
(thread-yield)
(handler-bind
((sb-sys:deadline-timeout
#'(lambda (c)
;; We came here through the call to GET-MUTEX
;; in CONDITION-WAIT (contended case of
;; reaquiring the mutex) - so the former will
;; be NIL, but interrupts should still be enabled.
(setq B-holds? (sb-thread:holding-mutex-p mutex))
(setq B-interrupts-enabled?
sb-sys:*interrupts-enabled*)
(sleep 0.2)
(sb-thread:condition-broadcast waitq)
(sb-sys:defer-deadline 10.0 c))))
(sb-sys:with-deadline (:seconds 0.1)
(sb-thread:with-mutex (mutex)
(sb-thread:condition-wait waitq mutex)))))
:name "B"))
(sb-thread:join-thread A)
(sb-thread:join-thread B)
(let ((A-result (list A-holds? A-interrupts-enabled?))
(B-result (list B-holds? B-interrupts-enabled?)))
;; We also check some subtle behaviour w.r.t. whether a deadline
;; handler in CONDITION-WAIT got the mutex, or not. This is most
;; probably very internal behaviour (so user should not depend
;; on it) -- I added the testing here just to manifest current
;; behaviour.
(cond ((equal A-result '(t t)) (assert (equal B-result '(nil t))))
((equal B-result '(t t)) (assert (equal A-result '(nil t))))
(t
(error "Failure: fell through wit A: ~S, B: ~S"
A-result
B-result))))))
(with-test (:name (:mutex :finalization))
(let ((a nil))
(dotimes (i 500000)
(setf a (make-mutex)))))
(format t "mutex finalization test done~%")
;;; Check that INFO is thread-safe, at least when we're just doing reads.
(let* ((symbols (loop repeat 10000 collect (gensym)))
(functions (loop for (symbol . rest) on symbols
for next = (car rest)
for fun = (let ((next next))
(lambda (n)
(if next
(funcall next (1- n))
n)))
do (setf (symbol-function symbol) fun)
collect fun)))
(defun infodb-test ()
(funcall (car functions) 9999)))
(with-test (:name (:infodb :read))
(let* ((ok t)
(threads (loop for i from 0 to 10
collect (sb-thread:make-thread
(lambda ()
(dotimes (j 100)
(write-char #\-)
(finish-output)
(let ((n (infodb-test)))
(unless (zerop n)
(setf ok nil)
(format t "N != 0 (~A)~%" n)
(sb-ext:quit)))))))))
(wait-for-threads threads)
(assert ok)))
(format t "infodb test done~%")
(with-test (:name :backtrace)
;; Printing backtraces from several threads at once used to hang the
;; whole SBCL process (discovered by accident due to a timer.impure
;; test misbehaving). The cause was that packages weren't even
;; thread-safe for only doing FIND-SYMBOL, and while printing
;; backtraces a loot of symbol lookups need to be done due to
;; *PRINT-ESCAPE*.
(let* ((threads (loop repeat 10
collect (sb-thread:make-thread
(lambda ()
(dotimes (i 1000)
(with-output-to-string (*debug-io*)
(sb-debug::backtrace 10))))))))
(wait-for-threads threads)))
(format t "backtrace test done~%")
(format t "~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
(with-test (:name :gc-deadlock)
;; Prior to 0.9.16.46 thread exit potentially deadlocked the
;; GC due to *all-threads-lock* and session lock. On earlier
;; versions and at least on one specific box this test is good enough
;; to catch that typically well before the 1500th iteration.
(loop
with i = 0
with n = 3000
while (< i n)
do
(incf i)
(when (zerop (mod i 100))
(write-char #\.)
(force-output))
(handler-case
(if (oddp i)
(sb-thread:make-thread
(lambda ()
(sleep (random 0.001)))
:name (format nil "SLEEP-~D" i))
(sb-thread:make-thread
(lambda ()
;; KLUDGE: what we are doing here is explicit,
;; but the same can happen because of a regular
;; MAKE-THREAD or LIST-ALL-THREADS, and various
;; session functions.
(sb-thread::with-all-threads-lock
(sb-thread::with-session-lock (sb-thread::*session*)
(sb-ext:gc))))
:name (format nil "GC-~D" i)))
(error (e)
(format t "~%error creating thread ~D: ~A -- backing off for retry~%" i e)
(sleep 0.1)
(incf i)))))
(format t "~&gc deadlock test done~%")
(let ((count (make-array 8 :initial-element 0)))
(defun closure-one ()
(declare (optimize safety))
(values (incf (aref count 0)) (incf (aref count 1))
(incf (aref count 2)) (incf (aref count 3))
(incf (aref count 4)) (incf (aref count 5))
(incf (aref count 6)) (incf (aref count 7))))
(defun no-optimizing-away-closure-one ()
(setf count (make-array 8 :initial-element 0))))
(defstruct box
(count 0))
(let ((one (make-box))
(two (make-box))
(three (make-box)))
(defun closure-two ()
(declare (optimize safety))
(values (incf (box-count one)) (incf (box-count two)) (incf (box-count three))))
(defun no-optimizing-away-closure-two ()
(setf one (make-box)
two (make-box)
three (make-box))))
(with-test (:name (:funcallable-instances))
;; the funcallable-instance implementation used not to be threadsafe
;; against setting the funcallable-instance function to a closure
;; (because the code and lexenv were set separately).
(let ((fun (sb-kernel:%make-funcallable-instance 0))
(condition nil))
(setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
(flet ((changer ()
(loop (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
(setf (sb-kernel:funcallable-instance-fun fun) #'closure-two)))
(test ()
(handler-case (loop (funcall fun))
(serious-condition (c) (setf condition c)))))
(let ((changer (make-thread #'changer))
(test (make-thread #'test)))
(handler-case
(progn
;; The two closures above are fairly carefully crafted
;; so that if given the wrong lexenv they will tend to
;; do some serious damage, but it is of course difficult
;; to predict where the various bits and pieces will be
;; allocated. Five seconds failed fairly reliably on
;; both my x86 and x86-64 systems. -- CSR, 2006-09-27.
(sb-ext:with-timeout 5
(wait-for-threads (list test)))
(error "~@<test thread got condition:~2I~_~A~@:>" condition))
(sb-ext:timeout ()
(terminate-thread changer)
(terminate-thread test)
(wait-for-threads (list changer test))))))))
(format t "~&funcallable-instance test done~%")
(defun random-type (n)
`(integer ,(random n) ,(+ n (random n))))
(defun subtypep-hash-cache-test ()
(dotimes (i 10000)
(let ((type1 (random-type 500))
(type2 (random-type 500)))
(let ((a (subtypep type1 type2)))
(dotimes (i 100)
(assert (eq (subtypep type1 type2) a))))))
(format t "ok~%")
(force-output))
(with-test (:name (:hash-cache :subtypep))
(dotimes (i 10)
(sb-thread:make-thread #'subtypep-hash-cache-test)))
(format t "hash-cache tests done~%")
;;;; BLACK BOX TESTS
(in-package :cl-user)
(use-package :test-util)
(use-package "ASSERTOID")
(format t "parallel defclass test -- WARNING, WILL HANG ON FAILURE!~%")
(with-test (:name :parallel-defclass)
(defclass test-1 () ((a :initform :orig-a)))
(defclass test-2 () ((b :initform :orig-b)))
(defclass test-3 (test-1 test-2) ((c :initform :orig-c)))
(let* ((run t)
(d1 (sb-thread:make-thread (lambda ()
(loop while run
do (defclass test-1 () ((a :initform :new-a)))
(write-char #\1)
(force-output)))
:name "d1"))
(d2 (sb-thread:make-thread (lambda ()
(loop while run
do (defclass test-2 () ((b :initform :new-b)))
(write-char #\2)
(force-output)))
:name "d2"))
(d3 (sb-thread:make-thread (lambda ()
(loop while run
do (defclass test-3 (test-1 test-2) ((c :initform :new-c)))
(write-char #\3)
(force-output)))
:name "d3"))
(i (sb-thread:make-thread (lambda ()
(loop while run
do (let ((i (make-instance 'test-3)))
(assert (member (slot-value i 'a) '(:orig-a :new-a)))
(assert (member (slot-value i 'b) '(:orig-b :new-b)))
(assert (member (slot-value i 'c) '(:orig-c :new-c))))
(write-char #\i)
(force-output)))
:name "i")))
(format t "~%sleeping!~%")
(sleep 2.0)
(format t "~%stopping!~%")
(setf run nil)
(mapc (lambda (th)
(sb-thread:join-thread th)
(format t "~%joined ~S~%" (sb-thread:thread-name th)))
(list d1 d2 d3 i))))
(format t "parallel defclass test done~%")
(with-test (:name (:deadlock-detection :interrupts))
(let* ((m1 (sb-thread:make-mutex :name "M1"))
(m2 (sb-thread:make-mutex :name "M2"))
(t1 (sb-thread:make-thread
(lambda ()
(sb-thread:with-mutex (m1)
(sleep 0.3)
:ok))
:name "T1"))
(t2 (sb-thread:make-thread
(lambda ()
(sleep 0.1)
(sb-thread:with-mutex (m1 :wait-p t)
(sleep 0.2)
:ok))
:name "T2")))
(sleep 0.2)
(sb-thread:interrupt-thread t2 (lambda ()
(sb-thread:with-mutex (m2 :wait-p t)
(sleep 0.3))))
(sleep 0.05)
(sb-thread:interrupt-thread t1 (lambda ()
(sb-thread:with-mutex (m2 :wait-p t)
(sleep 0.3))))
;; both threads should finish without a deadlock or deadlock
;; detection error
(let ((res (list (sb-thread:join-thread t1)
(sb-thread:join-thread t2))))
(assert (equal '(:ok :ok) res)))))
(with-test (:name (:deadlock-detection :gc))
;; To semi-reliably trigger the error (in SBCL's where)
;; it was present you had to run this for > 30 seconds,
;; but that's a bit long for a single test.
(let* ((stop (+ 5 (get-universal-time)))
(m1 (sb-thread:make-mutex :name "m1"))
(t1 (sb-thread:make-thread
(lambda ()
(loop until (> (get-universal-time) stop)
do (sb-thread:with-mutex (m1)
(eval `(make-array 24))))
:ok)))
(t2 (sb-thread:make-thread
(lambda ()
(loop until (> (get-universal-time) stop)
do (sb-thread:with-mutex (m1)
(eval `(make-array 24))))
:ok))))
(let ((res (list (sb-thread:join-thread t1)
(sb-thread:join-thread t2))))
(assert (equal '(:ok :ok) res)))))