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
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
|
/*!
* Task management.
*
* An executing Rust program consists of a tree of tasks, each with their own
* stack, and sole ownership of their allocated heap data. Tasks communicate
* with each other using ports and channels.
*
* When a task fails, that failure will propagate to its parent (the task
* that spawned it) and the parent will fail as well. The reverse is not
* true: when a parent task fails its children will continue executing. When
* the root (main) task fails, all tasks fail, and then so does the entire
* process.
*
* Tasks may execute in parallel and are scheduled automatically by the
* runtime.
*
* # Example
*
* ~~~
* do spawn {
* log(error, "Hello, World!");
* }
* ~~~
*/
import result::result;
import dvec::extensions;
import dvec_iter::extensions;
import arc::methods;
export task;
export task_result;
export notification;
export sched_mode;
export sched_opts;
export task_opts;
export task_builder;
export default_task_opts;
export get_opts;
export set_opts;
export set_sched_mode;
export add_wrapper;
export run;
export future_result;
export run_listener;
export run_with;
export spawn;
export spawn_unlinked;
export spawn_supervised;
export spawn_with;
export spawn_listener;
export spawn_sched;
export try;
export yield;
export failing;
export get_task;
export unkillable;
export atomically;
export local_data_key;
export local_data_pop;
export local_data_get;
export local_data_set;
export local_data_modify;
export single_threaded;
export thread_per_core;
export thread_per_task;
export manual_threads;
export osmain;
/* Data types */
/// A handle to a task
enum task { task_handle(task_id) }
/**
* Indicates the manner in which a task exited.
*
* A task that completes without failing and whose supervised children
* complete without failing is considered to exit successfully.
*
* FIXME (See #1868): This description does not indicate the current behavior
* for linked failure.
*/
enum task_result {
success,
failure,
}
/// A message type for notifying of task lifecycle events
enum notification {
/// Sent when a task exits with the task handle and result
exit(task, task_result)
}
/// Scheduler modes
enum sched_mode {
/// All tasks run in the same OS thread
single_threaded,
/// Tasks are distributed among available CPUs
thread_per_core,
/// Each task runs in its own OS thread
thread_per_task,
/// Tasks are distributed among a fixed number of OS threads
manual_threads(uint),
/**
* Tasks are scheduled on the main OS thread
*
* The main OS thread is the thread used to launch the runtime which,
* in most cases, is the process's initial thread as created by the OS.
*/
osmain
}
/**
* Scheduler configuration options
*
* # Fields
*
* * sched_mode - The operating mode of the scheduler
*
* * foreign_stack_size - The size of the foreign stack, in bytes
*
* Rust code runs on Rust-specific stacks. When Rust code calls foreign
* code (via functions in foreign modules) it switches to a typical, large
* stack appropriate for running code written in languages like C. By
* default these foreign stacks have unspecified size, but with this
* option their size can be precisely specified.
*/
type sched_opts = {
mode: sched_mode,
foreign_stack_size: option<uint>
};
/**
* Task configuration options
*
* # Fields
*
* * linked - Do not propagate failure to the parent task
*
* All tasks are linked together via a tree, from parents to children. By
* default children are 'supervised' by their parent and when they fail
* so too will their parents. Settings this flag to false disables that
* behavior.
*
* * notify_chan - Enable lifecycle notifications on the given channel
*
* * sched - Specify the configuration of a new scheduler to create the task
* in
*
* By default, every task is created in the same scheduler as its
* parent, where it is scheduled cooperatively with all other tasks
* in that scheduler. Some specialized applications may want more
* control over their scheduling, in which case they can be spawned
* into a new scheduler with the specific properties required.
*
* This is of particular importance for libraries which want to call
* into foreign code that blocks. Without doing so in a different
* scheduler other tasks will be impeded or even blocked indefinitely.
*/
type task_opts = {
linked: bool,
parented: bool,
notify_chan: option<comm::chan<notification>>,
sched: option<sched_opts>,
};
/**
* The task builder type.
*
* Provides detailed control over the properties and behavior of new tasks.
*/
// NB: Builders are designed to be single-use because they do stateful
// things that get weird when reusing - e.g. if you create a result future
// it only applies to a single task, so then you have to maintain some
// potentially tricky state to ensure that everything behaves correctly
// when you try to reuse the builder to spawn a new task. We'll just
// sidestep that whole issue by making builders uncopyable and making
// the run function move them in.
class dummy { let x: (); new() { self.x = (); } drop { } }
// FIXME (#2585): Replace the 'consumed' bit with move mode on self
enum task_builder = {
opts: task_opts,
gen_body: fn@(+fn~()) -> fn~(),
can_not_copy: option<dummy>,
mut consumed: bool,
};
/**
* Generate the base configuration for spawning a task, off of which more
* configuration methods can be chained.
* For example, task().unlinked().spawn is equivalent to spawn_unlinked.
*/
fn task() -> task_builder {
task_builder({
opts: default_task_opts(),
gen_body: |body| body, // Identity function
can_not_copy: none,
mut consumed: false,
})
}
impl private_methods for task_builder {
fn consume() -> task_builder {
if self.consumed {
fail ~"Cannot copy a task_builder"; // Fake move mode on self
}
self.consumed = true;
task_builder({ can_not_copy: none, mut consumed: false, with *self })
}
}
impl task_builder for task_builder {
/**
* Decouple the child task's failure from the parent's. If either fails,
* the other will not be killed.
*/
fn unlinked() -> task_builder {
task_builder({
opts: { linked: false with self.opts },
can_not_copy: none,
with *self.consume()
})
}
/**
* Unidirectionally link the child task's failure with the parent's. The
* child's failure will not kill the parent, but the parent's will kill
* the child.
*/
fn supervised() -> task_builder {
task_builder({
opts: { linked: false, parented: true with self.opts },
can_not_copy: none,
with *self.consume()
})
}
/**
* Link the child task's and parent task's failures. If either fails, the
* other will be killed.
*/
fn linked() -> task_builder {
task_builder({
opts: { linked: true, parented: false with self.opts },
can_not_copy: none,
with *self.consume()
})
}
/**
* Get a future representing the exit status of the task.
*
* Taking the value of the future will block until the child task
* terminates. The future-receiving callback specified will be called
* *before* the task is spawned; as such, do not invoke .get() within the
* closure; rather, store it in an outer variable/list for later use.
*
* Note that the future returning by this function is only useful for
* obtaining the value of the next task to be spawning with the
* builder. If additional tasks are spawned with the same builder
* then a new result future must be obtained prior to spawning each
* task.
*/
fn future_result(blk: fn(-future::future<task_result>)) -> task_builder {
// FIXME (#1087, #1857): Once linked failure and notification are
// handled in the library, I can imagine implementing this by just
// registering an arbitrary number of task::on_exit handlers and
// sending out messages.
// Construct the future and give it to the caller.
let po = comm::port::<notification>();
let ch = comm::chan(po);
blk(do future::from_fn {
alt comm::recv(po) {
exit(_, result) { result }
}
});
// Reconfigure self to use a notify channel.
task_builder({
opts: { notify_chan: some(ch) with self.opts },
can_not_copy: none,
with *self.consume()
})
}
/// Configure a custom scheduler mode for the task.
fn sched_mode(mode: sched_mode) -> task_builder {
task_builder({
opts: { sched: some({ mode: mode, foreign_stack_size: none})
with self.opts },
can_not_copy: none,
with *self.consume()
})
}
/**
* Add a wrapper to the body of the spawned task.
*
* Before the task is spawned it is passed through a 'body generator'
* function that may perform local setup operations as well as wrap
* the task body in remote setup operations. With this the behavior
* of tasks can be extended in simple ways.
*
* This function augments the current body generator with a new body
* generator by applying the task body which results from the
* existing body generator to the new body generator.
*/
fn add_wrapper(wrapper: fn@(+fn~()) -> fn~()) -> task_builder {
let prev_gen_body = self.gen_body;
task_builder({
gen_body: |body| { wrapper(prev_gen_body(body)) },
can_not_copy: none,
with *self.consume()
})
}
/**
* Creates and exucutes a new child task
*
* Sets up a new task with its own call stack and schedules it to run
* the provided unique closure. The task has the properties and behavior
* specified by the task_builder.
*
* # Failure
*
* When spawning into a new scheduler, the number of threads requested
* must be greater than zero.
*/
fn spawn(+f: fn~()) {
let x = self.consume();
spawn_raw(x.opts, x.gen_body(f));
}
/// Runs a task, while transfering ownership of one argument to the child.
fn spawn_with<A: send>(+arg: A, +f: fn~(+A)) {
let arg = ~mut some(arg);
do self.spawn {
let mut my_arg = none;
my_arg <-> *arg;
f(option::unwrap(my_arg))
}
}
/**
* Runs a new task while providing a channel from the parent to the child
*
* Sets up a communication channel from the current task to the new
* child task, passes the port to child's body, and returns a channel
* linked to the port to the parent.
*
* This encapsulates some boilerplate handshaking logic that would
* otherwise be required to establish communication from the parent
* to the child.
*/
fn spawn_listener<A: send>(+f: fn~(comm::port<A>)) -> comm::chan<A> {
let setup_po = comm::port();
let setup_ch = comm::chan(setup_po);
do self.spawn {
let po = comm::port();
let ch = comm::chan(po);
comm::send(setup_ch, ch);
f(po);
}
comm::recv(setup_po)
}
}
/* Task construction */
fn default_task_opts() -> task_opts {
/*!
* The default task options
*
* By default all tasks are supervised by their parent, are spawned
* into the same scheduler, and do not post lifecycle notifications.
*/
{
linked: true,
parented: false,
notify_chan: none,
sched: none
}
}
/* Spawn convenience functions */
fn spawn(+f: fn~()) {
/*!
* Creates and executes a new child task
*
* Sets up a new task with its own call stack and schedules it to run
* the provided unique closure.
*
* This function is equivalent to `task().spawn(f)`.
*/
task().spawn(f)
}
fn spawn_unlinked(+f: fn~()) {
/*!
* Creates a child task unlinked from the current one. If either this
* task or the child task fails, the other will not be killed.
*/
task().unlinked().spawn(f)
}
fn spawn_supervised(+f: fn~()) {
/*!
* Creates a child task unlinked from the current one. If either this
* task or the child task fails, the other will not be killed.
*/
task().supervised().spawn(f)
}
fn spawn_with<A:send>(+arg: A, +f: fn~(+A)) {
/*!
* Runs a task, while transfering ownership of one argument to the
* child.
*
* This is useful for transfering ownership of noncopyables to
* another task.
*
* This function is equivalent to `task().spawn_with(arg, f)`.
*/
task().spawn_with(arg, f)
}
fn spawn_listener<A:send>(+f: fn~(comm::port<A>)) -> comm::chan<A> {
/*!
* Runs a new task while providing a channel from the parent to the child
*
* Sets up a communication channel from the current task to the new
* child task, passes the port to child's body, and returns a channel
* linked to the port to the parent.
*
* This encapsulates some boilerplate handshaking logic that would
* otherwise be required to establish communication from the parent
* to the child.
*
* The simplest way to establish bidirectional communication between
* a parent in child is as follows:
*
* let po = comm::port();
* let ch = comm::chan(po);
* let ch = do spawn_listener |po| {
* // Now the child has a port called 'po' to read from and
* // an environment-captured channel called 'ch'.
* };
* // Likewise, the parent has both a 'po' and 'ch'
*
* This function is equivalent to `task().spawn_listener(f)`.
*/
task().spawn_listener(f)
}
fn spawn_sched(mode: sched_mode, +f: fn~()) {
/*!
* Creates a new scheduler and executes a task on it
*
* Tasks subsequently spawned by that task will also execute on
* the new scheduler. When there are no more tasks to execute the
* scheduler terminates.
*
* # Failure
*
* In manual threads mode the number of threads requested must be
* greater than zero.
*/
task().sched_mode(mode).spawn(f)
}
fn try<T:send>(+f: fn~() -> T) -> result<T,()> {
/*!
* Execute a function in another task and return either the return value
* of the function or result::err.
*
* # Return value
*
* If the function executed successfully then try returns result::ok
* containing the value returned by the function. If the function fails
* then try returns result::err containing nil.
*/
let po = comm::port();
let ch = comm::chan(po);
let mut result = none;
do task().unlinked().future_result(|-r| { result = some(r); }).spawn {
comm::send(ch, f());
}
alt future::get(option::unwrap(result)) {
success { result::ok(comm::recv(po)) }
failure { result::err(()) }
}
}
/* Lifecycle functions */
fn yield() {
//! Yield control to the task scheduler
let task_ = rustrt::rust_get_task();
let killed = rustrt::rust_task_yield(task_);
if killed && !failing() {
fail ~"killed";
}
}
fn failing() -> bool {
//! True if the running task has failed
rustrt::rust_task_is_unwinding(rustrt::rust_get_task())
}
fn get_task() -> task {
//! Get a handle to the running task
task_handle(rustrt::get_task_id())
}
/**
* Temporarily make the task unkillable
*
* # Example
*
* ~~~
* do task::unkillable {
* // detach / yield / destroy must all be called together
* rustrt::rust_port_detach(po);
* // This must not result in the current task being killed
* task::yield();
* rustrt::rust_port_destroy(po);
* }
* ~~~
*/
unsafe fn unkillable(f: fn()) {
class allow_failure {
let t: *rust_task;
new(t: *rust_task) { self.t = t; }
drop { rustrt::rust_task_allow_kill(self.t); }
}
let t = rustrt::rust_get_task();
let _allow_failure = allow_failure(t);
rustrt::rust_task_inhibit_kill(t);
f();
}
/**
* A stronger version of unkillable that also inhibits scheduling operations.
* For use with exclusive ARCs, which use pthread mutexes directly.
*/
unsafe fn atomically<U>(f: fn() -> U) -> U {
class defer_interrupts {
let t: *rust_task;
new(t: *rust_task) { self.t = t; }
drop {
rustrt::rust_task_allow_yield(self.t);
rustrt::rust_task_allow_kill(self.t);
}
}
let t = rustrt::rust_get_task();
let _interrupts = defer_interrupts(t);
rustrt::rust_task_inhibit_kill(t);
rustrt::rust_task_inhibit_yield(t);
f()
}
/****************************************************************************
* Internal
****************************************************************************/
/* spawning */
type sched_id = int;
type task_id = int;
// These are both opaque runtime/compiler types that we don't know the
// structure of and should only deal with via unsafe pointer
type rust_task = libc::c_void;
type rust_closure = libc::c_void;
/* linked failure */
type taskgroup_arc =
arc::exclusive<option<(dvec::dvec<option<*rust_task>>,dvec::dvec<uint>)>>;
class taskgroup {
// FIXME (#2816): Change dvec to an O(1) data structure (and change 'me'
// to a node-handle or somesuch when so done (or remove the field entirely
// if keyed by *rust_task)).
let me: *rust_task;
// List of tasks with whose fates this one's is intertwined.
let tasks: taskgroup_arc; // 'none' means the group already failed.
let my_pos: uint; // Index into above for this task's slot.
// Lists of tasks who will kill us if they fail, but whom we won't kill.
let parents: option<(taskgroup_arc,uint)>;
let is_main: bool;
let notifier: option<auto_notify>;
new(me: *rust_task, -tasks: taskgroup_arc, my_pos: uint,
-parents: option<(taskgroup_arc,uint)>, is_main: bool,
-notifier: option<auto_notify>) {
self.me = me;
self.tasks = tasks;
self.my_pos = my_pos;
self.parents = parents;
self.is_main = is_main;
self.notifier = notifier;
self.notifier.iter(|x| { x.failed = false; });
}
// Runs on task exit.
drop {
// If we are failing, the whole taskgroup needs to die.
if rustrt::rust_task_is_unwinding(self.me) {
self.notifier.iter(|x| { x.failed = true; });
// Take everybody down with us.
kill_taskgroup(self.tasks, self.me, self.my_pos, self.is_main);
} else {
// Remove ourselves from the group(s).
leave_taskgroup(self.tasks, self.me, self.my_pos);
}
// It doesn't matter whether this happens before or after dealing with
// our own taskgroup, so long as both happen before we die.
alt self.parents {
some((parent_group,pos_in_group)) {
leave_taskgroup(parent_group, self.me, pos_in_group);
}
none { }
}
}
}
class auto_notify {
let notify_chan: comm::chan<notification>;
let mut failed: bool;
new(chan: comm::chan<notification>) {
self.notify_chan = chan;
self.failed = true; // Un-set above when taskgroup successfully made.
}
drop {
let result = if self.failed { failure } else { success };
comm::send(self.notify_chan, exit(get_task(), result));
}
}
fn enlist_in_taskgroup(group_arc: taskgroup_arc,
me: *rust_task) -> option<uint> {
do group_arc.with |_c, state| {
// If 'none', the group was failing. Can't enlist.
let mut newstate = none;
*state <-> newstate;
if newstate.is_some() {
let (tasks,empty_slots) = option::unwrap(newstate);
// Try to find an empty slot.
let slotno = if empty_slots.len() > 0 {
let empty_index = empty_slots.pop();
assert tasks[empty_index] == none;
tasks.set_elt(empty_index, some(me));
empty_index
} else {
tasks.push(some(me));
tasks.len() - 1
};
*state = some((tasks,empty_slots));
some(slotno)
} else {
none
}
}
}
// NB: Runs in destructor/post-exit context. Can't 'fail'.
fn leave_taskgroup(group_arc: taskgroup_arc, me: *rust_task, index: uint) {
do group_arc.with |_c, state| {
let mut newstate = none;
*state <-> newstate;
// If 'none', already failing and we've already gotten a kill signal.
if newstate.is_some() {
let (tasks,empty_slots) = option::unwrap(newstate);
assert tasks[index] == some(me);
tasks.set_elt(index, none);
empty_slots.push(index);
*state = some((tasks,empty_slots));
};
};
}
// NB: Runs in destructor/post-exit context. Can't 'fail'.
fn kill_taskgroup(group_arc: taskgroup_arc, me: *rust_task, index: uint,
is_main: bool) {
// NB: We could do the killing iteration outside of the group arc, by
// having "let mut newstate" here, swapping inside, and iterating after.
// But that would let other exiting tasks fall-through and exit while we
// were trying to kill them, causing potential use-after-free. A task's
// presence in the arc guarantees it's alive only while we hold the lock,
// so if we're failing, all concurrently exiting tasks must wait for us.
// To do it differently, we'd have to use the runtime's task refcounting.
do group_arc.with |_c, state| {
let mut newstate = none;
*state <-> newstate;
// Might already be none, if somebody is failing simultaneously.
// That's ok; only one task needs to do the dirty work. (Might also
// see 'none' if somebody already failed and we got a kill signal.)
if newstate.is_some() {
let (tasks,_empty_slots) = option::unwrap(newstate);
// First remove ourself (killing ourself won't do much good). This
// is duplicated here to avoid having to lock twice.
assert tasks[index] == some(me);
tasks.set_elt(index, none);
// Now send takedown signal.
for tasks.each |entry| {
do entry.map |task| {
rustrt::rust_task_kill_other(task);
};
}
// Only one task should ever do this.
if is_main {
rustrt::rust_task_kill_all(me);
}
// Do NOT restore state to some(..)! It stays none to indicate
// that the whole taskgroup is failing, to forbid new spawns.
}
// (note: multiple tasks may reach this point)
};
}
// FIXME (#2912): Work around core-vs-coretest function duplication. Can't use
// a proper closure because the #[test]s won't understand. Have to fake it.
unsafe fn taskgroup_key() -> local_data_key<taskgroup> {
// Use a "code pointer" value that will never be a real code pointer.
unsafe::transmute((-2 as uint, 0u))
}
// The 'linked' arg tells whether or not to also ref the unidirectionally-
// linked supervisors' group. False when the spawn is supervised, not linked.
fn share_spawner_taskgroup(linked: bool)
-> (taskgroup_arc, option<taskgroup_arc>, bool) {
let me = rustrt::rust_get_task();
alt unsafe { local_get(me, taskgroup_key()) } {
some(group) {
// If they are linked to us, they share our parent group.
let parent_arc_opt = if linked {
group.parents.map(|x| alt x { (pg,_) { pg.clone() } })
} else {
none
};
// Clone the shared state for the child; propagate main-ness.
(group.tasks.clone(), parent_arc_opt, group.is_main)
}
none {
// Main task, doing first spawn ever.
let tasks = arc::exclusive(some((dvec::from_elem(some(me)),
dvec::dvec())));
// Main group has no parent group.
let group = @taskgroup(me, tasks.clone(), 0, none, true, none);
unsafe { local_set(me, taskgroup_key(), group); }
// Tell child task it's also in the main group.
// Whether or not it wanted our parent group, we haven't got one.
(tasks, none, true)
}
}
}
fn spawn_raw(opts: task_opts, +f: fn~()) {
// Decide whether the child needs to be in a new linked failure group.
// This whole conditional should be consolidated with share_spawner above.
let (child_tg, parent_tg, is_main) = if opts.linked {
// It doesn't mean anything for a linked-spawned-task to have a parent
// group. The spawning task is already bidirectionally linked to it.
share_spawner_taskgroup(true)
} else {
// Detached from the parent group; create a new (non-main) one.
(arc::exclusive(some((dvec::dvec(),dvec::dvec()))),
// Allow the parent to unidirectionally fail the child?
if opts.parented {
// Use the spawner's own group as the child's parent group.
let (pg,_,_) = share_spawner_taskgroup(false); some(pg)
} else {
none
},
false)
};
unsafe {
let child_data_ptr = ~mut some((child_tg, parent_tg, f));
// Being killed with the unsafe task/closure pointers would leak them.
do unkillable {
// Agh. Get move-mode items into the closure. FIXME (#2829)
let mut child_data = none;
*child_data_ptr <-> child_data;
let (child_tg, parent_tg, f) = option::unwrap(child_data);
// Create child task.
let new_task = alt opts.sched {
none { rustrt::new_task() }
some(sched_opts) { new_task_in_new_sched(sched_opts) }
};
assert !new_task.is_null();
// Getting killed after here would leak the task.
let child_wrapper =
make_child_wrapper(new_task, child_tg, parent_tg, is_main,
opts.notify_chan, f);
let fptr = ptr::addr_of(child_wrapper);
let closure: *rust_closure = unsafe::reinterpret_cast(fptr);
// Getting killed between these two calls would free the child's
// closure. (Reordering them wouldn't help - then getting killed
// between them would leak.)
rustrt::start_task(new_task, closure);
unsafe::forget(child_wrapper);
}
}
// This function returns a closure-wrapper that we pass to the child task.
// In brief, it does the following:
// if enlist_in_group(child_group) {
// if parent_group {
// if !enlist_in_group(parent_group) {
// leave_group(child_group); // Roll back
// ret; // Parent group failed. Don't run child's f().
// }
// }
// stash_taskgroup_data_in_TLS(child_group, parent_group);
// f();
// } else {
// // My group failed. Don't run chid's f().
// }
fn make_child_wrapper(child: *rust_task, -child_tg: taskgroup_arc,
-parent_tg: option<taskgroup_arc>, is_main: bool,
notify_chan: option<comm::chan<notification>>,
-f: fn~()) -> fn~() {
let child_tg_ptr = ~mut some((child_tg, parent_tg));
fn~() {
// Agh. Get move-mode items into the closure. FIXME (#2829)
let mut tg_data_opt = none;
*child_tg_ptr <-> tg_data_opt;
let (child_tg, parent_tg) = option::unwrap(tg_data_opt);
// Child task runs this code.
// Even if the below code fails to kick the child off, we must
// send something on the notify channel.
let notifier = notify_chan.map(|c| auto_notify(c));
// Set up membership in taskgroup. If this returns none, some
// task was already failing, so don't bother doing anything.
alt enlist_in_taskgroup(child_tg, child) {
some(my_pos) {
// Enlist in parent group too. If enlist returns none, a
// parent was failing: don't spawn; leave this group too.
let (pg, enlist_ok) = if parent_tg.is_some() {
let parent_group = option::unwrap(parent_tg);
alt enlist_in_taskgroup(parent_group, child) {
some(my_p_index) {
// Successful enlist.
(some((parent_group, my_p_index)), true)
}
none {
// Couldn't enlist. Have to quit here too.
leave_taskgroup(child_tg, child, my_pos);
(none, false)
}
}
} else {
// No parent group to enlist in. No worry.
(none, true)
};
if enlist_ok {
let group = @taskgroup(child, child_tg, my_pos,
pg, is_main, notifier);
unsafe { local_set(child, taskgroup_key(), group); }
// Run the child's body.
f();
// TLS cleanup code will exit the taskgroup.
}
}
none { }
}
}
}
fn new_task_in_new_sched(opts: sched_opts) -> *rust_task {
if opts.foreign_stack_size != none {
fail ~"foreign_stack_size scheduler option unimplemented";
}
let num_threads = alt opts.mode {
single_threaded { 1u }
thread_per_core {
fail ~"thread_per_core scheduling mode unimplemented"
}
thread_per_task {
fail ~"thread_per_task scheduling mode unimplemented"
}
manual_threads(threads) {
if threads == 0u {
fail ~"can not create a scheduler with no threads";
}
threads
}
osmain { 0u /* Won't be used */ }
};
let sched_id = if opts.mode != osmain {
rustrt::rust_new_sched(num_threads)
} else {
rustrt::rust_osmain_sched_id()
};
rustrt::rust_new_task_in_sched(sched_id)
}
}
/****************************************************************************
* Task local data management
*
* Allows storing boxes with arbitrary types inside, to be accessed anywhere
* within a task, keyed by a pointer to a global finaliser function. Useful
* for task-spawning metadata (tracking linked failure state), dynamic
* variables, and interfacing with foreign code with bad callback interfaces.
*
* To use, declare a monomorphic global function at the type to store, and use
* it as the 'key' when accessing. See the 'tls' tests below for examples.
*
* Casting 'Arcane Sight' reveals an overwhelming aura of Transmutation magic.
****************************************************************************/
/**
* Indexes a task-local data slot. The function's code pointer is used for
* comparison. Recommended use is to write an empty function for each desired
* task-local data slot (and use class destructors, not code inside the
* function, if specific teardown is needed). DO NOT use multiple
* instantiations of a single polymorphic function to index data of different
* types; arbitrary type coercion is possible this way.
*
* One other exception is that this global state can be used in a destructor
* context to create a circular @-box reference, which will crash during task
* failure (see issue #3039).
*
* These two cases aside, the interface is safe.
*/
type local_data_key<T: owned> = fn@(+@T);
iface local_data { }
impl<T: owned> of local_data for @T { }
// We use dvec because it's the best data structure in core. If TLS is used
// heavily in future, this could be made more efficient with a proper map.
type task_local_element = (*libc::c_void, *libc::c_void, local_data);
// Has to be a pointer at outermost layer; the foreign call returns void *.
type task_local_map = @dvec::dvec<option<task_local_element>>;
extern fn cleanup_task_local_map(map_ptr: *libc::c_void) unsafe {
assert !map_ptr.is_null();
// Get and keep the single reference that was created at the beginning.
let _map: task_local_map = unsafe::reinterpret_cast(map_ptr);
// All local_data will be destroyed along with the map.
}
// Gets the map from the runtime. Lazily initialises if not done so already.
unsafe fn get_task_local_map(task: *rust_task) -> task_local_map {
// Relies on the runtime initialising the pointer to null.
// NOTE: The map's box lives in TLS invisibly referenced once. Each time
// we retrieve it for get/set, we make another reference, which get/set
// drop when they finish. No "re-storing after modifying" is needed.
let map_ptr = rustrt::rust_get_task_local_data(task);
if map_ptr.is_null() {
let map: task_local_map = @dvec::dvec();
// Use reinterpret_cast -- transmute would take map away from us also.
rustrt::rust_set_task_local_data(task, unsafe::reinterpret_cast(map));
rustrt::rust_task_local_data_atexit(task, cleanup_task_local_map);
// Also need to reference it an extra time to keep it for now.
unsafe::bump_box_refcount(map);
map
} else {
let map = unsafe::transmute(map_ptr);
unsafe::bump_box_refcount(map);
map
}
}
unsafe fn key_to_key_value<T: owned>(
key: local_data_key<T>) -> *libc::c_void {
// Keys are closures, which are (fnptr,envptr) pairs. Use fnptr.
// Use reintepret_cast -- transmute would leak (forget) the closure.
let pair: (*libc::c_void, *libc::c_void) = unsafe::reinterpret_cast(key);
pair.first()
}
// If returning some(..), returns with @T with the map's reference. Careful!
unsafe fn local_data_lookup<T: owned>(
map: task_local_map, key: local_data_key<T>)
-> option<(uint, *libc::c_void)> {
let key_value = key_to_key_value(key);
let map_pos = (*map).position(|entry|
alt entry { some((k,_,_)) { k == key_value } none { false } }
);
do map_pos.map |index| {
// .get() is guaranteed because of "none { false }" above.
let (_, data_ptr, _) = (*map)[index].get();
(index, data_ptr)
}
}
unsafe fn local_get_helper<T: owned>(
task: *rust_task, key: local_data_key<T>,
do_pop: bool) -> option<@T> {
let map = get_task_local_map(task);
// Interpret our findings from the map
do local_data_lookup(map, key).map |result| {
// A reference count magically appears on 'data' out of thin air. It
// was referenced in the local_data box, though, not here, so before
// overwriting the local_data_box we need to give an extra reference.
// We must also give an extra reference when not removing.
let (index, data_ptr) = result;
let data: @T = unsafe::transmute(data_ptr);
unsafe::bump_box_refcount(data);
if do_pop {
(*map).set_elt(index, none);
}
data
}
}
unsafe fn local_pop<T: owned>(
task: *rust_task,
key: local_data_key<T>) -> option<@T> {
local_get_helper(task, key, true)
}
unsafe fn local_get<T: owned>(
task: *rust_task,
key: local_data_key<T>) -> option<@T> {
local_get_helper(task, key, false)
}
unsafe fn local_set<T: owned>(
task: *rust_task, key: local_data_key<T>, +data: @T) {
let map = get_task_local_map(task);
// Store key+data as *voids. Data is invisibly referenced once; key isn't.
let keyval = key_to_key_value(key);
// We keep the data in two forms: one as an unsafe pointer, so we can get
// it back by casting; another in an existential box, so the reference we
// own on it can be dropped when the box is destroyed. The unsafe pointer
// does not have a reference associated with it, so it may become invalid
// when the box is destroyed.
let data_ptr = unsafe::reinterpret_cast(data);
let data_box = data as local_data;
// Construct new entry to store in the map.
let new_entry = some((keyval, data_ptr, data_box));
// Find a place to put it.
alt local_data_lookup(map, key) {
some((index, _old_data_ptr)) {
// Key already had a value set, _old_data_ptr, whose reference
// will get dropped when the local_data box is overwritten.
(*map).set_elt(index, new_entry);
}
none {
// Find an empty slot. If not, grow the vector.
alt (*map).position(|x| x == none) {
some(empty_index) {
(*map).set_elt(empty_index, new_entry);
}
none {
(*map).push(new_entry);
}
}
}
}
}
unsafe fn local_modify<T: owned>(
task: *rust_task, key: local_data_key<T>,
modify_fn: fn(option<@T>) -> option<@T>) {
// Could be more efficient by doing the lookup work, but this is easy.
let newdata = modify_fn(local_pop(task, key));
if newdata.is_some() {
local_set(task, key, option::unwrap(newdata));
}
}
/* Exported interface for task-local data (plus local_data_key above). */
/**
* Remove a task-local data value from the table, returning the
* reference that was originally created to insert it.
*/
unsafe fn local_data_pop<T: owned>(
key: local_data_key<T>) -> option<@T> {
local_pop(rustrt::rust_get_task(), key)
}
/**
* Retrieve a task-local data value. It will also be kept alive in the
* table until explicitly removed.
*/
unsafe fn local_data_get<T: owned>(
key: local_data_key<T>) -> option<@T> {
local_get(rustrt::rust_get_task(), key)
}
/**
* Store a value in task-local data. If this key already has a value,
* that value is overwritten (and its destructor is run).
*/
unsafe fn local_data_set<T: owned>(
key: local_data_key<T>, +data: @T) {
local_set(rustrt::rust_get_task(), key, data)
}
/**
* Modify a task-local data value. If the function returns 'none', the
* data is removed (and its reference dropped).
*/
unsafe fn local_data_modify<T: owned>(
key: local_data_key<T>,
modify_fn: fn(option<@T>) -> option<@T>) {
local_modify(rustrt::rust_get_task(), key, modify_fn)
}
extern mod rustrt {
#[rust_stack]
fn rust_task_yield(task: *rust_task) -> bool;
fn rust_get_sched_id() -> sched_id;
fn rust_new_sched(num_threads: libc::uintptr_t) -> sched_id;
fn get_task_id() -> task_id;
#[rust_stack]
fn rust_get_task() -> *rust_task;
fn new_task() -> *rust_task;
fn rust_new_task_in_sched(id: sched_id) -> *rust_task;
fn start_task(task: *rust_task, closure: *rust_closure);
fn rust_task_is_unwinding(task: *rust_task) -> bool;
fn rust_osmain_sched_id() -> sched_id;
fn rust_task_inhibit_kill(t: *rust_task);
fn rust_task_allow_kill(t: *rust_task);
fn rust_task_inhibit_yield(t: *rust_task);
fn rust_task_allow_yield(t: *rust_task);
fn rust_task_kill_other(task: *rust_task);
fn rust_task_kill_all(task: *rust_task);
#[rust_stack]
fn rust_get_task_local_data(task: *rust_task) -> *libc::c_void;
#[rust_stack]
fn rust_set_task_local_data(task: *rust_task, map: *libc::c_void);
#[rust_stack]
fn rust_task_local_data_atexit(task: *rust_task, cleanup_fn: *u8);
}
#[test]
fn test_spawn_raw_simple() {
let po = comm::port();
let ch = comm::chan(po);
do spawn_raw(default_task_opts()) {
comm::send(ch, ());
}
comm::recv(po);
}
#[test]
#[ignore(cfg(windows))]
fn test_spawn_raw_unsupervise() {
let opts = {
linked: false
with default_task_opts()
};
do spawn_raw(opts) {
fail;
}
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_cant_dup_task_builder() {
let b = task().unlinked();
do b.spawn { }
// FIXME(#2585): For now, this is a -runtime- failure, because we haven't
// got modes on self. When 2585 is fixed, this test should fail to compile
// instead, and should go in tests/compile-fail.
do b.spawn { } // b should have been consumed by the previous call
}
// The following 8 tests test the following 2^3 combinations:
// {un,}linked {un,}supervised failure propagation {up,down}wards.
// !!! These tests are dangerous. If something is buggy, they will hang, !!!
// !!! instead of exiting cleanly. This might wedge the buildbots. !!!
#[test] #[ignore(cfg(windows))]
fn test_spawn_unlinked_unsup_no_fail_down() { // grandchild sends on a port
let po = comm::port();
let ch = comm::chan(po);
do spawn_unlinked {
do spawn_unlinked {
// Give middle task a chance to fail-but-not-kill-us.
for iter::repeat(128) { task::yield(); }
comm::send(ch, ()); // If killed first, grandparent hangs.
}
fail; // Shouldn't kill either (grand)parent or (grand)child.
}
comm::recv(po);
}
#[test] #[ignore(cfg(windows))]
fn test_spawn_unlinked_unsup_no_fail_up() { // child unlinked fails
do spawn_unlinked { fail; }
}
#[test] #[ignore(cfg(windows))]
fn test_spawn_unlinked_sup_no_fail_up() { // child unlinked fails
do spawn_supervised { fail; }
// Give child a chance to fail-but-not-kill-us.
for iter::repeat(128) { task::yield(); }
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_spawn_unlinked_sup_fail_down() {
do spawn_supervised { loop { task::yield(); } }
fail; // Shouldn't leave a child hanging around.
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_spawn_linked_sup_fail_up() { // child fails; parent fails
let po = comm::port::<()>();
let _ch = comm::chan(po);
// Unidirectional "parenting" shouldn't override bidirectional linked.
// We have to cheat with opts - the interface doesn't support them because
// they don't make sense (redundant with task().supervised()).
let b0 = task();
let b1 = task_builder({
opts: { linked: true, parented: true with b0.opts },
can_not_copy: none,
with *b0
});
do b1.spawn { fail; }
comm::recv(po); // We should get punted awake
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_spawn_linked_sup_fail_down() { // parent fails; child fails
// We have to cheat with opts - the interface doesn't support them because
// they don't make sense (redundant with task().supervised()).
let b0 = task();
let b1 = task_builder({
opts: { linked: true, parented: true with b0.opts },
can_not_copy: none,
with *b0
});
do b1.spawn { loop { task::yield(); } }
fail; // *both* mechanisms would be wrong if this didn't kill the child...
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_spawn_linked_unsup_fail_up() { // child fails; parent fails
let po = comm::port::<()>();
let _ch = comm::chan(po);
// Default options are to spawn linked & unsupervised.
do spawn { fail; }
comm::recv(po); // We should get punted awake
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_spawn_linked_unsup_fail_down() { // parent fails; child fails
// Default options are to spawn linked & unsupervised.
do spawn { loop { task::yield(); } }
fail;
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_spawn_linked_unsup_default_opts() { // parent fails; child fails
// Make sure the above test is the same as this one.
do task().linked().spawn { loop { task::yield(); } }
fail;
}
// A couple bonus linked failure tests - testing for failure propagation even
// when the middle task exits successfully early before kill signals are sent.
#[test] #[should_fail] // #[ignore(cfg(windows))]
#[ignore] // FIXME (#1868) (bblum) make this work
fn test_spawn_failure_propagate_grandchild() {
// Middle task exits; does grandparent's failure propagate across the gap?
do spawn_supervised {
do spawn_supervised {
loop { task::yield(); }
}
}
for iter::repeat(128) { task::yield(); }
fail;
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_spawn_failure_propagate_secondborn() {
// First-born child exits; does parent's failure propagate to sibling?
do spawn_supervised {
do spawn { // linked
loop { task::yield(); }
}
}
for iter::repeat(128) { task::yield(); }
fail;
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_spawn_failure_propagate_nephew_or_niece() {
// Our sibling exits; does our failure propagate to sibling's child?
do spawn { // linked
do spawn_supervised {
loop { task::yield(); }
}
}
for iter::repeat(128) { task::yield(); }
fail;
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_spawn_linked_sup_propagate_sibling() {
// Middle sibling exits - does eldest's failure propagate to youngest?
do spawn { // linked
do spawn { // linked
loop { task::yield(); }
}
}
for iter::repeat(128) { task::yield(); }
fail;
}
#[test]
#[ignore(cfg(windows))]
fn test_spawn_raw_notify() {
let task_po = comm::port();
let task_ch = comm::chan(task_po);
let notify_po = comm::port();
let notify_ch = comm::chan(notify_po);
let opts = {
notify_chan: some(notify_ch)
with default_task_opts()
};
do spawn_raw(opts) {
comm::send(task_ch, get_task());
}
let task_ = comm::recv(task_po);
assert comm::recv(notify_po) == exit(task_, success);
let opts = {
linked: false,
notify_chan: some(notify_ch)
with default_task_opts()
};
do spawn_raw(opts) {
comm::send(task_ch, get_task());
fail;
}
let task_ = comm::recv(task_po);
assert comm::recv(notify_po) == exit(task_, failure);
}
#[test]
fn test_run_basic() {
let po = comm::port();
let ch = comm::chan(po);
do task().spawn {
comm::send(ch, ());
}
comm::recv(po);
}
#[test]
fn test_add_wrapper() {
let po = comm::port();
let ch = comm::chan(po);
let b0 = task();
let b1 = do b0.add_wrapper |body| {
fn~() {
body();
comm::send(ch, ());
}
};
do b1.spawn { }
comm::recv(po);
}
#[test]
#[ignore(cfg(windows))]
fn test_future_result() {
let mut result = none;
do task().future_result(|-r| { result = some(r); }).spawn { }
assert future::get(option::unwrap(result)) == success;
result = none;
do task().future_result(|-r| { result = some(r); }).unlinked().spawn {
fail;
}
assert future::get(option::unwrap(result)) == failure;
}
#[test]
fn test_spawn_listiner_bidi() {
let po = comm::port();
let ch = comm::chan(po);
let ch = do spawn_listener |po| {
// Now the child has a port called 'po' to read from and
// an environment-captured channel called 'ch'.
let res = comm::recv(po);
assert res == ~"ping";
comm::send(ch, ~"pong");
};
// Likewise, the parent has both a 'po' and 'ch'
comm::send(ch, ~"ping");
let res = comm::recv(po);
assert res == ~"pong";
}
#[test]
fn test_try_success() {
alt do try {
~"Success!"
} {
result::ok(~"Success!") { }
_ { fail; }
}
}
#[test]
#[ignore(cfg(windows))]
fn test_try_fail() {
alt do try {
fail
} {
result::err(()) { }
result::ok(()) { fail; }
}
}
#[test]
#[should_fail]
#[ignore(cfg(windows))]
fn test_spawn_sched_no_threads() {
do spawn_sched(manual_threads(0u)) { }
}
#[test]
fn test_spawn_sched() {
let po = comm::port();
let ch = comm::chan(po);
fn f(i: int, ch: comm::chan<()>) {
let parent_sched_id = rustrt::rust_get_sched_id();
do spawn_sched(single_threaded) {
let child_sched_id = rustrt::rust_get_sched_id();
assert parent_sched_id != child_sched_id;
if (i == 0) {
comm::send(ch, ());
} else {
f(i - 1, ch);
}
};
}
f(10, ch);
comm::recv(po);
}
#[test]
fn test_spawn_sched_childs_on_same_sched() {
let po = comm::port();
let ch = comm::chan(po);
do spawn_sched(single_threaded) {
let parent_sched_id = rustrt::rust_get_sched_id();
do spawn {
let child_sched_id = rustrt::rust_get_sched_id();
// This should be on the same scheduler
assert parent_sched_id == child_sched_id;
comm::send(ch, ());
};
};
comm::recv(po);
}
#[nolink]
#[cfg(test)]
extern mod testrt {
fn rust_dbg_lock_create() -> *libc::c_void;
fn rust_dbg_lock_destroy(lock: *libc::c_void);
fn rust_dbg_lock_lock(lock: *libc::c_void);
fn rust_dbg_lock_unlock(lock: *libc::c_void);
fn rust_dbg_lock_wait(lock: *libc::c_void);
fn rust_dbg_lock_signal(lock: *libc::c_void);
}
#[test]
fn test_spawn_sched_blocking() {
// Testing that a task in one scheduler can block in foreign code
// without affecting other schedulers
for iter::repeat(20u) {
let start_po = comm::port();
let start_ch = comm::chan(start_po);
let fin_po = comm::port();
let fin_ch = comm::chan(fin_po);
let lock = testrt::rust_dbg_lock_create();
do spawn_sched(single_threaded) {
testrt::rust_dbg_lock_lock(lock);
comm::send(start_ch, ());
// Block the scheduler thread
testrt::rust_dbg_lock_wait(lock);
testrt::rust_dbg_lock_unlock(lock);
comm::send(fin_ch, ());
};
// Wait until the other task has its lock
comm::recv(start_po);
fn pingpong(po: comm::port<int>, ch: comm::chan<int>) {
let mut val = 20;
while val > 0 {
val = comm::recv(po);
comm::send(ch, val - 1);
}
}
let setup_po = comm::port();
let setup_ch = comm::chan(setup_po);
let parent_po = comm::port();
let parent_ch = comm::chan(parent_po);
do spawn {
let child_po = comm::port();
comm::send(setup_ch, comm::chan(child_po));
pingpong(child_po, parent_ch);
};
let child_ch = comm::recv(setup_po);
comm::send(child_ch, 20);
pingpong(parent_po, child_ch);
testrt::rust_dbg_lock_lock(lock);
testrt::rust_dbg_lock_signal(lock);
testrt::rust_dbg_lock_unlock(lock);
comm::recv(fin_po);
testrt::rust_dbg_lock_destroy(lock);
}
}
#[cfg(test)]
fn avoid_copying_the_body(spawnfn: fn(+fn~())) {
let p = comm::port::<uint>();
let ch = comm::chan(p);
let x = ~1;
let x_in_parent = ptr::addr_of(*x) as uint;
do spawnfn {
let x_in_child = ptr::addr_of(*x) as uint;
comm::send(ch, x_in_child);
}
let x_in_child = comm::recv(p);
assert x_in_parent == x_in_child;
}
#[test]
fn test_avoid_copying_the_body_spawn() {
avoid_copying_the_body(spawn);
}
#[test]
fn test_avoid_copying_the_body_spawn_listener() {
do avoid_copying_the_body |f| {
spawn_listener(fn~(move f, _po: comm::port<int>) {
f();
});
}
}
#[test]
fn test_avoid_copying_the_body_task_spawn() {
do avoid_copying_the_body |f| {
do task().spawn {
f();
}
}
}
#[test]
fn test_avoid_copying_the_body_spawn_listener() {
do avoid_copying_the_body |f| {
task().spawn_listener(fn~(move f, _po: comm::port<int>) {
f();
});
}
}
#[test]
fn test_avoid_copying_the_body_try() {
do avoid_copying_the_body |f| {
do try {
f()
};
}
}
#[test]
fn test_avoid_copying_the_body_unlinked() {
do avoid_copying_the_body |f| {
do spawn_unlinked {
f();
}
}
}
#[test]
fn test_osmain() {
let po = comm::port();
let ch = comm::chan(po);
do task().sched_mode(osmain).spawn {
comm::send(ch, ());
}
comm::recv(po);
}
#[test]
#[ignore(cfg(windows))]
#[should_fail]
fn test_unkillable() {
import comm::methods;
let po = comm::port();
let ch = po.chan();
// We want to do this after failing
do spawn_raw({ linked: false with default_task_opts() }) {
for iter::repeat(10u) { yield() }
ch.send(());
}
do spawn {
yield();
// We want to fail after the unkillable task
// blocks on recv
fail;
}
unsafe {
do unkillable {
let p = ~0;
let pp: *uint = unsafe::transmute(p);
// If we are killed here then the box will leak
po.recv();
let _p: ~int = unsafe::transmute(pp);
}
}
// Now we can be killed
po.recv();
}
#[test]
#[ignore(cfg(windows))]
#[should_fail]
fn test_unkillable_nested() {
import comm::methods;
let po = comm::port();
let ch = po.chan();
// We want to do this after failing
do spawn_raw({ linked: false with default_task_opts() }) {
for iter::repeat(10u) { yield() }
ch.send(());
}
do spawn {
yield();
// We want to fail after the unkillable task
// blocks on recv
fail;
}
unsafe {
do unkillable {
do unkillable {} // Here's the difference from the previous test.
let p = ~0;
let pp: *uint = unsafe::transmute(p);
// If we are killed here then the box will leak
po.recv();
let _p: ~int = unsafe::transmute(pp);
}
}
// Now we can be killed
po.recv();
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_atomically() {
unsafe { do atomically { yield(); } }
}
#[test]
fn test_atomically2() {
unsafe { do atomically { } } yield(); // shouldn't fail
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_atomically_nested() {
unsafe { do atomically { do atomically { } yield(); } }
}
#[test]
fn test_child_doesnt_ref_parent() {
// If the child refcounts the parent task, this will stack overflow when
// climbing the task tree to dereference each ancestor. (See #1789)
const generations: uint = 128;
fn child_no(x: uint) -> fn~() {
ret || {
if x < generations {
task::spawn(child_no(x+1));
}
}
}
task::spawn(child_no(0));
}
#[test]
fn test_tls_multitask() unsafe {
fn my_key(+_x: @~str) { }
local_data_set(my_key, @~"parent data");
do task::spawn {
assert local_data_get(my_key) == none; // TLS shouldn't carry over.
local_data_set(my_key, @~"child data");
assert *(local_data_get(my_key).get()) == ~"child data";
// should be cleaned up for us
}
// Must work multiple times
assert *(local_data_get(my_key).get()) == ~"parent data";
assert *(local_data_get(my_key).get()) == ~"parent data";
assert *(local_data_get(my_key).get()) == ~"parent data";
}
#[test]
fn test_tls_overwrite() unsafe {
fn my_key(+_x: @~str) { }
local_data_set(my_key, @~"first data");
local_data_set(my_key, @~"next data"); // Shouldn't leak.
assert *(local_data_get(my_key).get()) == ~"next data";
}
#[test]
fn test_tls_pop() unsafe {
fn my_key(+_x: @~str) { }
local_data_set(my_key, @~"weasel");
assert *(local_data_pop(my_key).get()) == ~"weasel";
// Pop must remove the data from the map.
assert local_data_pop(my_key) == none;
}
#[test]
fn test_tls_modify() unsafe {
fn my_key(+_x: @~str) { }
local_data_modify(my_key, |data| {
alt data {
some(@val) { fail ~"unwelcome value: " + val }
none { some(@~"first data") }
}
});
local_data_modify(my_key, |data| {
alt data {
some(@~"first data") { some(@~"next data") }
some(@val) { fail ~"wrong value: " + val }
none { fail ~"missing value" }
}
});
assert *(local_data_pop(my_key).get()) == ~"next data";
}
#[test]
fn test_tls_crust_automorestack_memorial_bug() unsafe {
// This might result in a stack-canary clobber if the runtime fails to set
// sp_limit to 0 when calling the cleanup extern - it might automatically
// jump over to the rust stack, which causes next_c_sp to get recorded as
// something within a rust stack segment. Then a subsequent upcall (esp.
// for logging, think vsnprintf) would run on a stack smaller than 1 MB.
fn my_key(+_x: @~str) { }
do task::spawn {
unsafe { local_data_set(my_key, @~"hax"); }
}
}
#[test]
fn test_tls_multiple_types() unsafe {
fn str_key(+_x: @~str) { }
fn box_key(+_x: @@()) { }
fn int_key(+_x: @int) { }
do task::spawn {
local_data_set(str_key, @~"string data");
local_data_set(box_key, @@());
local_data_set(int_key, @42);
}
}
#[test]
fn test_tls_overwrite_multiple_types() unsafe {
fn str_key(+_x: @~str) { }
fn box_key(+_x: @@()) { }
fn int_key(+_x: @int) { }
do task::spawn {
local_data_set(str_key, @~"string data");
local_data_set(int_key, @42);
// This could cause a segfault if overwriting-destruction is done with
// the crazy polymorphic transmute rather than the provided finaliser.
local_data_set(int_key, @31337);
}
}
#[test]
#[should_fail]
#[ignore(cfg(windows))]
fn test_tls_cleanup_on_failure() unsafe {
fn str_key(+_x: @~str) { }
fn box_key(+_x: @@()) { }
fn int_key(+_x: @int) { }
local_data_set(str_key, @~"parent data");
local_data_set(box_key, @@());
do task::spawn { // spawn_linked
local_data_set(str_key, @~"string data");
local_data_set(box_key, @@());
local_data_set(int_key, @42);
fail;
}
// Not quite nondeterministic.
local_data_set(int_key, @31337);
fail;
}
|