summaryrefslogtreecommitdiff
path: root/cont.c
blob: d72d29eacaf7cd86f1a6191fdcc69e94af17774f (plain)
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
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
/**********************************************************************

  cont.c -

  $Author$
  created at: Thu May 23 09:03:43 2007

  Copyright (C) 2007 Koichi Sasada

**********************************************************************/

#include "ruby/internal/config.h"

#ifndef _WIN32
#include <unistd.h>
#include <sys/mman.h>
#endif

#include COROUTINE_H

#include "eval_intern.h"
#include "gc.h"
#include "internal.h"
#include "internal/cont.h"
#include "internal/proc.h"
#include "internal/warnings.h"
#include "ruby/fiber/scheduler.h"
#include "mjit.h"
#include "vm_core.h"
#include "id_table.h"
#include "ractor_core.h"

static const int DEBUG = 0;

#define RB_PAGE_SIZE (pagesize)
#define RB_PAGE_MASK (~(RB_PAGE_SIZE - 1))
static long pagesize;

static const rb_data_type_t cont_data_type, fiber_data_type;
static VALUE rb_cContinuation;
static VALUE rb_cFiber;
static VALUE rb_eFiberError;
#ifdef RB_EXPERIMENTAL_FIBER_POOL
static VALUE rb_cFiberPool;
#endif

#define CAPTURE_JUST_VALID_VM_STACK 1

// Defined in `coroutine/$arch/Context.h`:
#ifdef COROUTINE_LIMITED_ADDRESS_SPACE
#define FIBER_POOL_ALLOCATION_FREE
#define FIBER_POOL_INITIAL_SIZE 8
#define FIBER_POOL_ALLOCATION_MAXIMUM_SIZE 32
#else
#define FIBER_POOL_INITIAL_SIZE 32
#define FIBER_POOL_ALLOCATION_MAXIMUM_SIZE 1024
#endif

enum context_type {
    CONTINUATION_CONTEXT = 0,
    FIBER_CONTEXT = 1
};

struct cont_saved_vm_stack {
    VALUE *ptr;
#ifdef CAPTURE_JUST_VALID_VM_STACK
    size_t slen;  /* length of stack (head of ec->vm_stack) */
    size_t clen;  /* length of control frames (tail of ec->vm_stack) */
#endif
};

struct fiber_pool;

// Represents a single stack.
struct fiber_pool_stack {
    // A pointer to the memory allocation (lowest address) for the stack.
    void * base;

    // The current stack pointer, taking into account the direction of the stack.
    void * current;

    // The size of the stack excluding any guard pages.
    size_t size;

    // The available stack capacity w.r.t. the current stack offset.
    size_t available;

    // The pool this stack should be allocated from.
    struct fiber_pool * pool;

    // If the stack is allocated, the allocation it came from.
    struct fiber_pool_allocation * allocation;
};

// A linked list of vacant (unused) stacks.
// This structure is stored in the first page of a stack if it is not in use.
// @sa fiber_pool_vacancy_pointer
struct fiber_pool_vacancy {
    // Details about the vacant stack:
    struct fiber_pool_stack stack;

    // The vacancy linked list.
#ifdef FIBER_POOL_ALLOCATION_FREE
    struct fiber_pool_vacancy * previous;
#endif
    struct fiber_pool_vacancy * next;
};

// Manages singly linked list of mapped regions of memory which contains 1 more more stack:
//
// base = +-------------------------------+-----------------------+  +
//        |VM Stack       |VM Stack       |                       |  |
//        |               |               |                       |  |
//        |               |               |                       |  |
//        +-------------------------------+                       |  |
//        |Machine Stack  |Machine Stack  |                       |  |
//        |               |               |                       |  |
//        |               |               |                       |  |
//        |               |               | .  .  .  .            |  |  size
//        |               |               |                       |  |
//        |               |               |                       |  |
//        |               |               |                       |  |
//        |               |               |                       |  |
//        |               |               |                       |  |
//        +-------------------------------+                       |  |
//        |Guard Page     |Guard Page     |                       |  |
//        +-------------------------------+-----------------------+  v
//
//        +------------------------------------------------------->
//
//                                  count
//
struct fiber_pool_allocation {
    // A pointer to the memory mapped region.
    void * base;

    // The size of the individual stacks.
    size_t size;

    // The stride of individual stacks (including any guard pages or other accounting details).
    size_t stride;

    // The number of stacks that were allocated.
    size_t count;

#ifdef FIBER_POOL_ALLOCATION_FREE
    // The number of stacks used in this allocation.
    size_t used;
#endif

    struct fiber_pool * pool;

    // The allocation linked list.
#ifdef FIBER_POOL_ALLOCATION_FREE
    struct fiber_pool_allocation * previous;
#endif
    struct fiber_pool_allocation * next;
};

// A fiber pool manages vacant stacks to reduce the overhead of creating fibers.
struct fiber_pool {
    // A singly-linked list of allocations which contain 1 or more stacks each.
    struct fiber_pool_allocation * allocations;

    // Provides O(1) stack "allocation":
    struct fiber_pool_vacancy * vacancies;

    // The size of the stack allocations (excluding any guard page).
    size_t size;

    // The total number of stacks that have been allocated in this pool.
    size_t count;

    // The initial number of stacks to allocate.
    size_t initial_count;

    // Whether to madvise(free) the stack or not:
    int free_stacks;

    // The number of stacks that have been used in this pool.
    size_t used;

    // The amount to allocate for the vm_stack:
    size_t vm_stack_size;
};

typedef struct rb_context_struct {
    enum context_type type;
    int argc;
    int kw_splat;
    VALUE self;
    VALUE value;

    struct cont_saved_vm_stack saved_vm_stack;

    struct {
        VALUE *stack;
        VALUE *stack_src;
        size_t stack_size;
    } machine;
    rb_execution_context_t saved_ec;
    rb_jmpbuf_t jmpbuf;
    rb_ensure_entry_t *ensure_array;
    /* Pointer to MJIT info about the continuation.  */
    struct mjit_cont *mjit_cont;
} rb_context_t;


/*
 * Fiber status:
 *    [Fiber.new] ------> FIBER_CREATED
 *                        | [Fiber#resume]
 *                        v
 *                   +--> FIBER_RESUMED ----+
 *    [Fiber#resume] |    | [Fiber.yield]   |
 *                   |    v                 |
 *                   +-- FIBER_SUSPENDED    | [Terminate]
 *                                          |
 *                       FIBER_TERMINATED <-+
 */
enum fiber_status {
    FIBER_CREATED,
    FIBER_RESUMED,
    FIBER_SUSPENDED,
    FIBER_TERMINATED
};

#define FIBER_CREATED_P(fiber)    ((fiber)->status == FIBER_CREATED)
#define FIBER_RESUMED_P(fiber)    ((fiber)->status == FIBER_RESUMED)
#define FIBER_SUSPENDED_P(fiber)  ((fiber)->status == FIBER_SUSPENDED)
#define FIBER_TERMINATED_P(fiber) ((fiber)->status == FIBER_TERMINATED)
#define FIBER_RUNNABLE_P(fiber)   (FIBER_CREATED_P(fiber) || FIBER_SUSPENDED_P(fiber))

struct rb_fiber_struct {
    rb_context_t cont;
    VALUE first_proc;
    struct rb_fiber_struct *prev;
    VALUE resuming_fiber;

    BITFIELD(enum fiber_status, status, 2);
    /* Whether the fiber is allowed to implicitly yield. */
    unsigned int yielding : 1;
    unsigned int blocking : 1;

    struct coroutine_context context;
    struct fiber_pool_stack stack;
};

static struct fiber_pool shared_fiber_pool = {NULL, NULL, 0, 0, 0, 0};

static ID fiber_initialize_keywords[2] = {0};

/*
 * FreeBSD require a first (i.e. addr) argument of mmap(2) is not NULL
 * if MAP_STACK is passed.
 * http://www.FreeBSD.org/cgi/query-pr.cgi?pr=158755
 */
#if defined(MAP_STACK) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
#define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON | MAP_STACK)
#else
#define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON)
#endif

#define ERRNOMSG strerror(errno)

// Locates the stack vacancy details for the given stack.
// Requires that fiber_pool_vacancy fits within one page.
inline static struct fiber_pool_vacancy *
fiber_pool_vacancy_pointer(void * base, size_t size)
{
    STACK_GROW_DIR_DETECTION;

    return (struct fiber_pool_vacancy *)(
        (char*)base + STACK_DIR_UPPER(0, size - RB_PAGE_SIZE)
    );
}

// Reset the current stack pointer and available size of the given stack.
inline static void
fiber_pool_stack_reset(struct fiber_pool_stack * stack)
{
    STACK_GROW_DIR_DETECTION;

    stack->current = (char*)stack->base + STACK_DIR_UPPER(0, stack->size);
    stack->available = stack->size;
}

// A pointer to the base of the current unused portion of the stack.
inline static void *
fiber_pool_stack_base(struct fiber_pool_stack * stack)
{
    STACK_GROW_DIR_DETECTION;

    VM_ASSERT(stack->current);

    return STACK_DIR_UPPER(stack->current, (char*)stack->current - stack->available);
}

// Allocate some memory from the stack. Used to allocate vm_stack inline with machine stack.
// @sa fiber_initialize_coroutine
inline static void *
fiber_pool_stack_alloca(struct fiber_pool_stack * stack, size_t offset)
{
    STACK_GROW_DIR_DETECTION;

    if (DEBUG) fprintf(stderr, "fiber_pool_stack_alloca(%p): %"PRIuSIZE"/%"PRIuSIZE"\n", (void*)stack, offset, stack->available);
    VM_ASSERT(stack->available >= offset);

    // The pointer to the memory being allocated:
    void * pointer = STACK_DIR_UPPER(stack->current, (char*)stack->current - offset);

    // Move the stack pointer:
    stack->current = STACK_DIR_UPPER((char*)stack->current + offset, (char*)stack->current - offset);
    stack->available -= offset;

    return pointer;
}

// Reset the current stack pointer and available size of the given stack.
inline static void
fiber_pool_vacancy_reset(struct fiber_pool_vacancy * vacancy)
{
    fiber_pool_stack_reset(&vacancy->stack);

    // Consume one page of the stack because it's used for the vacancy list:
    fiber_pool_stack_alloca(&vacancy->stack, RB_PAGE_SIZE);
}

inline static struct fiber_pool_vacancy *
fiber_pool_vacancy_push(struct fiber_pool_vacancy * vacancy, struct fiber_pool_vacancy * head)
{
    vacancy->next = head;

#ifdef FIBER_POOL_ALLOCATION_FREE
    if (head) {
        head->previous = vacancy;
        vacancy->previous = NULL;
    }
#endif

    return vacancy;
}

#ifdef FIBER_POOL_ALLOCATION_FREE
static void
fiber_pool_vacancy_remove(struct fiber_pool_vacancy * vacancy)
{
    if (vacancy->next) {
        vacancy->next->previous = vacancy->previous;
    }

    if (vacancy->previous) {
        vacancy->previous->next = vacancy->next;
    }
    else {
        // It's the head of the list:
        vacancy->stack.pool->vacancies = vacancy->next;
    }
}

inline static struct fiber_pool_vacancy *
fiber_pool_vacancy_pop(struct fiber_pool * pool)
{
    struct fiber_pool_vacancy * vacancy = pool->vacancies;

    if (vacancy) {
        fiber_pool_vacancy_remove(vacancy);
    }

    return vacancy;
}
#else
inline static struct fiber_pool_vacancy *
fiber_pool_vacancy_pop(struct fiber_pool * pool)
{
    struct fiber_pool_vacancy * vacancy = pool->vacancies;

    if (vacancy) {
        pool->vacancies = vacancy->next;
    }

    return vacancy;
}
#endif

// Initialize the vacant stack. The [base, size] allocation should not include the guard page.
// @param base The pointer to the lowest address of the allocated memory.
// @param size The size of the allocated memory.
inline static struct fiber_pool_vacancy *
fiber_pool_vacancy_initialize(struct fiber_pool * fiber_pool, struct fiber_pool_vacancy * vacancies, void * base, size_t size)
{
    struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pointer(base, size);

    vacancy->stack.base = base;
    vacancy->stack.size = size;

    fiber_pool_vacancy_reset(vacancy);

    vacancy->stack.pool = fiber_pool;

    return fiber_pool_vacancy_push(vacancy, vacancies);
}

// Allocate a maximum of count stacks, size given by stride.
// @param count the number of stacks to allocate / were allocated.
// @param stride the size of the individual stacks.
// @return [void *] the allocated memory or NULL if allocation failed.
inline static void *
fiber_pool_allocate_memory(size_t * count, size_t stride)
{
    // We use a divide-by-2 strategy to try and allocate memory. We are trying
    // to allocate `count` stacks. In normal situation, this won't fail. But
    // if we ran out of address space, or we are allocating more memory than
    // the system would allow (e.g. overcommit * physical memory + swap), we
    // divide count by two and try again. This condition should only be
    // encountered in edge cases, but we handle it here gracefully.
    while (*count > 1) {
#if defined(_WIN32)
        void * base = VirtualAlloc(0, (*count)*stride, MEM_COMMIT, PAGE_READWRITE);

        if (!base) {
            *count = (*count) >> 1;
        }
        else {
            return base;
        }
#else
        errno = 0;
        void * base = mmap(NULL, (*count)*stride, PROT_READ | PROT_WRITE, FIBER_STACK_FLAGS, -1, 0);

        if (base == MAP_FAILED) {
            // If the allocation fails, count = count / 2, and try again.
            *count = (*count) >> 1;
        }
        else {
#if defined(MADV_FREE_REUSE)
            // On Mac MADV_FREE_REUSE is necessary for the task_info api
            // to keep the accounting accurate as possible when a page is marked as reusable
            // it can possibly not occurring at first call thus re-iterating if necessary.
            while (madvise(base, (*count)*stride, MADV_FREE_REUSE) == -1 && errno == EAGAIN);
#endif
            return base;
        }
#endif
    }

    return NULL;
}

// Given an existing fiber pool, expand it by the specified number of stacks.
// @param count the maximum number of stacks to allocate.
// @return the allocated fiber pool.
// @sa fiber_pool_allocation_free
static struct fiber_pool_allocation *
fiber_pool_expand(struct fiber_pool * fiber_pool, size_t count)
{
    STACK_GROW_DIR_DETECTION;

    size_t size = fiber_pool->size;
    size_t stride = size + RB_PAGE_SIZE;

    // Allocate the memory required for the stacks:
    void * base = fiber_pool_allocate_memory(&count, stride);

    if (base == NULL) {
        rb_raise(rb_eFiberError, "can't alloc machine stack to fiber (%"PRIuSIZE" x %"PRIuSIZE" bytes): %s", count, size, ERRNOMSG);
    }

    struct fiber_pool_vacancy * vacancies = fiber_pool->vacancies;
    struct fiber_pool_allocation * allocation = RB_ALLOC(struct fiber_pool_allocation);

    // Initialize fiber pool allocation:
    allocation->base = base;
    allocation->size = size;
    allocation->stride = stride;
    allocation->count = count;
#ifdef FIBER_POOL_ALLOCATION_FREE
    allocation->used = 0;
#endif
    allocation->pool = fiber_pool;

    if (DEBUG) {
        fprintf(stderr, "fiber_pool_expand(%"PRIuSIZE"): %p, %"PRIuSIZE"/%"PRIuSIZE" x [%"PRIuSIZE":%"PRIuSIZE"]\n",
                count, (void*)fiber_pool, fiber_pool->used, fiber_pool->count, size, fiber_pool->vm_stack_size);
    }

    // Iterate over all stacks, initializing the vacancy list:
    for (size_t i = 0; i < count; i += 1) {
        void * base = (char*)allocation->base + (stride * i);
        void * page = (char*)base + STACK_DIR_UPPER(size, 0);

#if defined(_WIN32)
        DWORD old_protect;

        if (!VirtualProtect(page, RB_PAGE_SIZE, PAGE_READWRITE | PAGE_GUARD, &old_protect)) {
            VirtualFree(allocation->base, 0, MEM_RELEASE);
            rb_raise(rb_eFiberError, "can't set a guard page: %s", ERRNOMSG);
        }
#else
        if (mprotect(page, RB_PAGE_SIZE, PROT_NONE) < 0) {
            munmap(allocation->base, count*stride);
            rb_raise(rb_eFiberError, "can't set a guard page: %s", ERRNOMSG);
        }
#endif

        vacancies = fiber_pool_vacancy_initialize(
            fiber_pool, vacancies,
            (char*)base + STACK_DIR_UPPER(0, RB_PAGE_SIZE),
            size
        );

#ifdef FIBER_POOL_ALLOCATION_FREE
        vacancies->stack.allocation = allocation;
#endif
    }

    // Insert the allocation into the head of the pool:
    allocation->next = fiber_pool->allocations;

#ifdef FIBER_POOL_ALLOCATION_FREE
    if (allocation->next) {
        allocation->next->previous = allocation;
    }

    allocation->previous = NULL;
#endif

    fiber_pool->allocations = allocation;
    fiber_pool->vacancies = vacancies;
    fiber_pool->count += count;

    return allocation;
}

// Initialize the specified fiber pool with the given number of stacks.
// @param vm_stack_size The size of the vm stack to allocate.
static void
fiber_pool_initialize(struct fiber_pool * fiber_pool, size_t size, size_t count, size_t vm_stack_size)
{
    VM_ASSERT(vm_stack_size < size);

    fiber_pool->allocations = NULL;
    fiber_pool->vacancies = NULL;
    fiber_pool->size = ((size / RB_PAGE_SIZE) + 1) * RB_PAGE_SIZE;
    fiber_pool->count = 0;
    fiber_pool->initial_count = count;
    fiber_pool->free_stacks = 1;
    fiber_pool->used = 0;

    fiber_pool->vm_stack_size = vm_stack_size;

    fiber_pool_expand(fiber_pool, count);
}

#ifdef FIBER_POOL_ALLOCATION_FREE
// Free the list of fiber pool allocations.
static void
fiber_pool_allocation_free(struct fiber_pool_allocation * allocation)
{
    STACK_GROW_DIR_DETECTION;

    VM_ASSERT(allocation->used == 0);

    if (DEBUG) fprintf(stderr, "fiber_pool_allocation_free: %p base=%p count=%"PRIuSIZE"\n", allocation, allocation->base, allocation->count);

    size_t i;
    for (i = 0; i < allocation->count; i += 1) {
        void * base = (char*)allocation->base + (allocation->stride * i) + STACK_DIR_UPPER(0, RB_PAGE_SIZE);

        struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pointer(base, allocation->size);

        // Pop the vacant stack off the free list:
        fiber_pool_vacancy_remove(vacancy);
    }

#ifdef _WIN32
    VirtualFree(allocation->base, 0, MEM_RELEASE);
#else
    munmap(allocation->base, allocation->stride * allocation->count);
#endif

    if (allocation->previous) {
        allocation->previous->next = allocation->next;
    }
    else {
        // We are the head of the list, so update the pool:
        allocation->pool->allocations = allocation->next;
    }

    if (allocation->next) {
        allocation->next->previous = allocation->previous;
    }

    allocation->pool->count -= allocation->count;

    ruby_xfree(allocation);
}
#endif

// Acquire a stack from the given fiber pool. If none are available, allocate more.
static struct fiber_pool_stack
fiber_pool_stack_acquire(struct fiber_pool * fiber_pool)
{
    struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pop(fiber_pool);

    if (DEBUG) fprintf(stderr, "fiber_pool_stack_acquire: %p used=%"PRIuSIZE"\n", (void*)fiber_pool->vacancies, fiber_pool->used);

    if (!vacancy) {
        const size_t maximum = FIBER_POOL_ALLOCATION_MAXIMUM_SIZE;
        const size_t minimum = fiber_pool->initial_count;

        size_t count = fiber_pool->count;
        if (count > maximum) count = maximum;
        if (count < minimum) count = minimum;

        fiber_pool_expand(fiber_pool, count);

        // The free list should now contain some stacks:
        VM_ASSERT(fiber_pool->vacancies);

        vacancy = fiber_pool_vacancy_pop(fiber_pool);
    }

    VM_ASSERT(vacancy);
    VM_ASSERT(vacancy->stack.base);

    // Take the top item from the free list:
    fiber_pool->used += 1;

#ifdef FIBER_POOL_ALLOCATION_FREE
    vacancy->stack.allocation->used += 1;
#endif

    fiber_pool_stack_reset(&vacancy->stack);

    return vacancy->stack;
}

// We advise the operating system that the stack memory pages are no longer being used.
// This introduce some performance overhead but allows system to relaim memory when there is pressure.
static inline void
fiber_pool_stack_free(struct fiber_pool_stack * stack)
{
    void * base = fiber_pool_stack_base(stack);
    size_t size = stack->available;

    // If this is not true, the vacancy information will almost certainly be destroyed:
    VM_ASSERT(size <= (stack->size - RB_PAGE_SIZE));

    if (DEBUG) fprintf(stderr, "fiber_pool_stack_free: %p+%"PRIuSIZE" [base=%p, size=%"PRIuSIZE"]\n", base, size, stack->base, stack->size);

#if VM_CHECK_MODE > 0 && defined(MADV_DONTNEED)
    // This immediately discards the pages and the memory is reset to zero.
    madvise(base, size, MADV_DONTNEED);
#elif defined(POSIX_MADV_DONTNEED)
    posix_madvise(base, size, POSIX_MADV_DONTNEED);
#elif defined(MADV_FREE_REUSABLE)
    // Acknowledge the kernel down to the task info api we make this
    // page reusable for future use.
    // As for MADV_FREE_REUSE below we ensure in the rare occasions the task was not
    // completed at the time of the call to re-iterate.
    while (madvise(base, size, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN);
#elif defined(MADV_FREE)
    madvise(base, size, MADV_FREE);
#elif defined(MADV_DONTNEED)
    madvise(base, size, MADV_DONTNEED);
#elif defined(_WIN32)
    VirtualAlloc(base, size, MEM_RESET, PAGE_READWRITE);
    // Not available in all versions of Windows.
    //DiscardVirtualMemory(base, size);
#endif
}

// Release and return a stack to the vacancy list.
static void
fiber_pool_stack_release(struct fiber_pool_stack * stack)
{
    struct fiber_pool * pool = stack->pool;
    struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pointer(stack->base, stack->size);

    if (DEBUG) fprintf(stderr, "fiber_pool_stack_release: %p used=%"PRIuSIZE"\n", stack->base, stack->pool->used);

    // Copy the stack details into the vacancy area:
    vacancy->stack = *stack;
    // After this point, be careful about updating/using state in stack, since it's copied to the vacancy area.

    // Reset the stack pointers and reserve space for the vacancy data:
    fiber_pool_vacancy_reset(vacancy);

    // Push the vacancy into the vancancies list:
    pool->vacancies = fiber_pool_vacancy_push(vacancy, stack->pool->vacancies);
    pool->used -= 1;

#ifdef FIBER_POOL_ALLOCATION_FREE
    struct fiber_pool_allocation * allocation = stack->allocation;

    allocation->used -= 1;

    // Release address space and/or dirty memory:
    if (allocation->used == 0) {
        fiber_pool_allocation_free(allocation);
    }
    else if (stack->pool->free_stacks) {
        fiber_pool_stack_free(&vacancy->stack);
    }
#else
    // This is entirely optional, but clears the dirty flag from the stack memory, so it won't get swapped to disk when there is memory pressure:
    if (stack->pool->free_stacks) {
        fiber_pool_stack_free(&vacancy->stack);
    }
#endif
}

void rb_fiber_start(rb_fiber_t*);

static inline void
ec_switch(rb_thread_t *th, rb_fiber_t *fiber)
{
    rb_execution_context_t *ec = &fiber->cont.saved_ec;
    rb_ractor_set_current_ec(th->ractor, th->ec = ec);
    // ruby_current_execution_context_ptr = th->ec = ec;

    /*
     * timer-thread may set trap interrupt on previous th->ec at any time;
     * ensure we do not delay (or lose) the trap interrupt handling.
     */
    if (th->vm->ractor.main_thread == th &&
        rb_signal_buff_size() > 0) {
        RUBY_VM_SET_TRAP_INTERRUPT(ec);
    }

    VM_ASSERT(ec->fiber_ptr->cont.self == 0 || ec->vm_stack != NULL);
}

static inline void
fiber_restore_thread(rb_thread_t *th, rb_fiber_t *fiber)
{
    ec_switch(th, fiber);
    VM_ASSERT(th->ec->fiber_ptr == fiber);
}

static COROUTINE
fiber_entry(struct coroutine_context * from, struct coroutine_context * to)
{
    rb_fiber_t *fiber = to->argument;
    rb_thread_t *thread = fiber->cont.saved_ec.thread_ptr;

#ifdef COROUTINE_PTHREAD_CONTEXT
    ruby_thread_set_native(thread);
#endif

    fiber_restore_thread(thread, fiber);

    rb_fiber_start(fiber);

#ifndef COROUTINE_PTHREAD_CONTEXT
    VM_UNREACHABLE(fiber_entry);
#endif
}

// Initialize a fiber's coroutine's machine stack and vm stack.
static VALUE *
fiber_initialize_coroutine(rb_fiber_t *fiber, size_t * vm_stack_size)
{
    struct fiber_pool * fiber_pool = fiber->stack.pool;
    rb_execution_context_t *sec = &fiber->cont.saved_ec;
    void * vm_stack = NULL;

    VM_ASSERT(fiber_pool != NULL);

    fiber->stack = fiber_pool_stack_acquire(fiber_pool);
    vm_stack = fiber_pool_stack_alloca(&fiber->stack, fiber_pool->vm_stack_size);
    *vm_stack_size = fiber_pool->vm_stack_size;

    coroutine_initialize(&fiber->context, fiber_entry, fiber_pool_stack_base(&fiber->stack), fiber->stack.available);

    // The stack for this execution context is the one we allocated:
    sec->machine.stack_start = fiber->stack.current;
    sec->machine.stack_maxsize = fiber->stack.available;

    fiber->context.argument = (void*)fiber;

    return vm_stack;
}

// Release the stack from the fiber, it's execution context, and return it to the fiber pool.
static void
fiber_stack_release(rb_fiber_t * fiber)
{
    rb_execution_context_t *ec = &fiber->cont.saved_ec;

    if (DEBUG) fprintf(stderr, "fiber_stack_release: %p, stack.base=%p\n", (void*)fiber, fiber->stack.base);

    // Return the stack back to the fiber pool if it wasn't already:
    if (fiber->stack.base) {
        fiber_pool_stack_release(&fiber->stack);
        fiber->stack.base = NULL;
    }

    // The stack is no longer associated with this execution context:
    rb_ec_clear_vm_stack(ec);
}

static const char *
fiber_status_name(enum fiber_status s)
{
    switch (s) {
      case FIBER_CREATED: return "created";
      case FIBER_RESUMED: return "resumed";
      case FIBER_SUSPENDED: return "suspended";
      case FIBER_TERMINATED: return "terminated";
    }
    VM_UNREACHABLE(fiber_status_name);
    return NULL;
}

static void
fiber_verify(const rb_fiber_t *fiber)
{
#if VM_CHECK_MODE > 0
    VM_ASSERT(fiber->cont.saved_ec.fiber_ptr == fiber);

    switch (fiber->status) {
      case FIBER_RESUMED:
        VM_ASSERT(fiber->cont.saved_ec.vm_stack != NULL);
        break;
      case FIBER_SUSPENDED:
        VM_ASSERT(fiber->cont.saved_ec.vm_stack != NULL);
        break;
      case FIBER_CREATED:
      case FIBER_TERMINATED:
        /* TODO */
        break;
      default:
        VM_UNREACHABLE(fiber_verify);
    }
#endif
}

inline static void
fiber_status_set(rb_fiber_t *fiber, enum fiber_status s)
{
    // if (DEBUG) fprintf(stderr, "fiber: %p, status: %s -> %s\n", (void *)fiber, fiber_status_name(fiber->status), fiber_status_name(s));
    VM_ASSERT(!FIBER_TERMINATED_P(fiber));
    VM_ASSERT(fiber->status != s);
    fiber_verify(fiber);
    fiber->status = s;
}

static rb_context_t *
cont_ptr(VALUE obj)
{
    rb_context_t *cont;

    TypedData_Get_Struct(obj, rb_context_t, &cont_data_type, cont);

    return cont;
}

static rb_fiber_t *
fiber_ptr(VALUE obj)
{
    rb_fiber_t *fiber;

    TypedData_Get_Struct(obj, rb_fiber_t, &fiber_data_type, fiber);
    if (!fiber) rb_raise(rb_eFiberError, "uninitialized fiber");

    return fiber;
}

NOINLINE(static VALUE cont_capture(volatile int *volatile stat));

#define THREAD_MUST_BE_RUNNING(th) do { \
        if (!(th)->ec->tag) rb_raise(rb_eThreadError, "not running thread"); \
    } while (0)

rb_thread_t*
rb_fiber_threadptr(const rb_fiber_t *fiber)
{
    return fiber->cont.saved_ec.thread_ptr;
}

static VALUE
cont_thread_value(const rb_context_t *cont)
{
    return cont->saved_ec.thread_ptr->self;
}

static void
cont_compact(void *ptr)
{
    rb_context_t *cont = ptr;

    if (cont->self) {
        cont->self = rb_gc_location(cont->self);
    }
    cont->value = rb_gc_location(cont->value);
    rb_execution_context_update(&cont->saved_ec);
}

static void
cont_mark(void *ptr)
{
    rb_context_t *cont = ptr;

    RUBY_MARK_ENTER("cont");
    if (cont->self) {
        rb_gc_mark_movable(cont->self);
    }
    rb_gc_mark_movable(cont->value);

    rb_execution_context_mark(&cont->saved_ec);
    rb_gc_mark(cont_thread_value(cont));

    if (cont->saved_vm_stack.ptr) {
#ifdef CAPTURE_JUST_VALID_VM_STACK
        rb_gc_mark_locations(cont->saved_vm_stack.ptr,
                             cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
#else
        rb_gc_mark_locations(cont->saved_vm_stack.ptr,
                             cont->saved_vm_stack.ptr, cont->saved_ec.stack_size);
#endif
    }

    if (cont->machine.stack) {
        if (cont->type == CONTINUATION_CONTEXT) {
            /* cont */
            rb_gc_mark_locations(cont->machine.stack,
                                 cont->machine.stack + cont->machine.stack_size);
        }
        else {
            /* fiber */
            const rb_fiber_t *fiber = (rb_fiber_t*)cont;

            if (!FIBER_TERMINATED_P(fiber)) {
                rb_gc_mark_locations(cont->machine.stack,
                                     cont->machine.stack + cont->machine.stack_size);
            }
        }
    }

    RUBY_MARK_LEAVE("cont");
}

#if 0
static int
fiber_is_root_p(const rb_fiber_t *fiber)
{
    return fiber == fiber->cont.saved_ec.thread_ptr->root_fiber;
}
#endif

static void
cont_free(void *ptr)
{
    rb_context_t *cont = ptr;

    RUBY_FREE_ENTER("cont");

    if (cont->type == CONTINUATION_CONTEXT) {
        ruby_xfree(cont->saved_ec.vm_stack);
        ruby_xfree(cont->ensure_array);
        RUBY_FREE_UNLESS_NULL(cont->machine.stack);
    }
    else {
        rb_fiber_t *fiber = (rb_fiber_t*)cont;
        coroutine_destroy(&fiber->context);
        fiber_stack_release(fiber);
    }

    RUBY_FREE_UNLESS_NULL(cont->saved_vm_stack.ptr);

    if (mjit_enabled) {
        VM_ASSERT(cont->mjit_cont != NULL);
        mjit_cont_free(cont->mjit_cont);
    }
    /* free rb_cont_t or rb_fiber_t */
    ruby_xfree(ptr);
    RUBY_FREE_LEAVE("cont");
}

static size_t
cont_memsize(const void *ptr)
{
    const rb_context_t *cont = ptr;
    size_t size = 0;

    size = sizeof(*cont);
    if (cont->saved_vm_stack.ptr) {
#ifdef CAPTURE_JUST_VALID_VM_STACK
        size_t n = (cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
#else
        size_t n = cont->saved_ec.vm_stack_size;
#endif
        size += n * sizeof(*cont->saved_vm_stack.ptr);
    }

    if (cont->machine.stack) {
        size += cont->machine.stack_size * sizeof(*cont->machine.stack);
    }

    return size;
}

void
rb_fiber_update_self(rb_fiber_t *fiber)
{
    if (fiber->cont.self) {
        fiber->cont.self = rb_gc_location(fiber->cont.self);
    }
    else {
        rb_execution_context_update(&fiber->cont.saved_ec);
    }
}

void
rb_fiber_mark_self(const rb_fiber_t *fiber)
{
    if (fiber->cont.self) {
        rb_gc_mark_movable(fiber->cont.self);
    }
    else {
        rb_execution_context_mark(&fiber->cont.saved_ec);
    }
}

static void
fiber_compact(void *ptr)
{
    rb_fiber_t *fiber = ptr;
    fiber->first_proc = rb_gc_location(fiber->first_proc);

    if (fiber->prev) rb_fiber_update_self(fiber->prev);

    cont_compact(&fiber->cont);
    fiber_verify(fiber);
}

static void
fiber_mark(void *ptr)
{
    rb_fiber_t *fiber = ptr;
    RUBY_MARK_ENTER("cont");
    fiber_verify(fiber);
    rb_gc_mark_movable(fiber->first_proc);
    if (fiber->prev) rb_fiber_mark_self(fiber->prev);
    cont_mark(&fiber->cont);
    RUBY_MARK_LEAVE("cont");
}

static void
fiber_free(void *ptr)
{
    rb_fiber_t *fiber = ptr;
    RUBY_FREE_ENTER("fiber");

    if (DEBUG) fprintf(stderr, "fiber_free: %p[%p]\n", fiber, fiber->stack.base);

    if (fiber->cont.saved_ec.local_storage) {
        rb_id_table_free(fiber->cont.saved_ec.local_storage);
    }

    cont_free(&fiber->cont);
    RUBY_FREE_LEAVE("fiber");
}

static size_t
fiber_memsize(const void *ptr)
{
    const rb_fiber_t *fiber = ptr;
    size_t size = sizeof(*fiber);
    const rb_execution_context_t *saved_ec = &fiber->cont.saved_ec;
    const rb_thread_t *th = rb_ec_thread_ptr(saved_ec);

    /*
     * vm.c::thread_memsize already counts th->ec->local_storage
     */
    if (saved_ec->local_storage && fiber != th->root_fiber) {
        size += rb_id_table_memsize(saved_ec->local_storage);
    }
    size += cont_memsize(&fiber->cont);
    return size;
}

VALUE
rb_obj_is_fiber(VALUE obj)
{
    if (rb_typeddata_is_kind_of(obj, &fiber_data_type)) {
        return Qtrue;
    }
    else {
        return Qfalse;
    }
}

static void
cont_save_machine_stack(rb_thread_t *th, rb_context_t *cont)
{
    size_t size;

    SET_MACHINE_STACK_END(&th->ec->machine.stack_end);

    if (th->ec->machine.stack_start > th->ec->machine.stack_end) {
        size = cont->machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
        cont->machine.stack_src = th->ec->machine.stack_end;
    }
    else {
        size = cont->machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
        cont->machine.stack_src = th->ec->machine.stack_start;
    }

    if (cont->machine.stack) {
        REALLOC_N(cont->machine.stack, VALUE, size);
    }
    else {
        cont->machine.stack = ALLOC_N(VALUE, size);
    }

    FLUSH_REGISTER_WINDOWS;
    MEMCPY(cont->machine.stack, cont->machine.stack_src, VALUE, size);
}

static const rb_data_type_t cont_data_type = {
    "continuation",
    {cont_mark, cont_free, cont_memsize, cont_compact},
    0, 0, RUBY_TYPED_FREE_IMMEDIATELY
};

static inline void
cont_save_thread(rb_context_t *cont, rb_thread_t *th)
{
    rb_execution_context_t *sec = &cont->saved_ec;

    VM_ASSERT(th->status == THREAD_RUNNABLE);

    /* save thread context */
    *sec = *th->ec;

    /* saved_ec->machine.stack_end should be NULL */
    /* because it may happen GC afterward */
    sec->machine.stack_end = NULL;
}

static void
cont_init_mjit_cont(rb_context_t *cont)
{
    VM_ASSERT(cont->mjit_cont == NULL);
    if (mjit_enabled) {
        cont->mjit_cont = mjit_cont_new(&(cont->saved_ec));
    }
}

static void
cont_init(rb_context_t *cont, rb_thread_t *th)
{
    /* save thread context */
    cont_save_thread(cont, th);
    cont->saved_ec.thread_ptr = th;
    cont->saved_ec.local_storage = NULL;
    cont->saved_ec.local_storage_recursive_hash = Qnil;
    cont->saved_ec.local_storage_recursive_hash_for_trace = Qnil;
    cont_init_mjit_cont(cont);
}

static rb_context_t *
cont_new(VALUE klass)
{
    rb_context_t *cont;
    volatile VALUE contval;
    rb_thread_t *th = GET_THREAD();

    THREAD_MUST_BE_RUNNING(th);
    contval = TypedData_Make_Struct(klass, rb_context_t, &cont_data_type, cont);
    cont->self = contval;
    cont_init(cont, th);
    return cont;
}

VALUE rb_fiberptr_self(struct rb_fiber_struct *fiber)
{
    return fiber->cont.self;
}

unsigned int rb_fiberptr_blocking(struct rb_fiber_struct *fiber)
{
    return fiber->blocking;
}

// This is used for root_fiber because other fibers call cont_init_mjit_cont through cont_new.
void
rb_fiber_init_mjit_cont(struct rb_fiber_struct *fiber)
{
    cont_init_mjit_cont(&fiber->cont);
}

#if 0
void
show_vm_stack(const rb_execution_context_t *ec)
{
    VALUE *p = ec->vm_stack;
    while (p < ec->cfp->sp) {
        fprintf(stderr, "%3d ", (int)(p - ec->vm_stack));
        rb_obj_info_dump(*p);
        p++;
    }
}

void
show_vm_pcs(const rb_control_frame_t *cfp,
            const rb_control_frame_t *end_of_cfp)
{
    int i=0;
    while (cfp != end_of_cfp) {
        int pc = 0;
        if (cfp->iseq) {
            pc = cfp->pc - cfp->iseq->body->iseq_encoded;
        }
        fprintf(stderr, "%2d pc: %d\n", i++, pc);
        cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
    }
}
#endif
COMPILER_WARNING_PUSH
#ifdef __clang__
COMPILER_WARNING_IGNORED(-Wduplicate-decl-specifier)
#endif
static VALUE
cont_capture(volatile int *volatile stat)
{
    rb_context_t *volatile cont;
    rb_thread_t *th = GET_THREAD();
    volatile VALUE contval;
    const rb_execution_context_t *ec = th->ec;

    THREAD_MUST_BE_RUNNING(th);
    rb_vm_stack_to_heap(th->ec);
    cont = cont_new(rb_cContinuation);
    contval = cont->self;

#ifdef CAPTURE_JUST_VALID_VM_STACK
    cont->saved_vm_stack.slen = ec->cfp->sp - ec->vm_stack;
    cont->saved_vm_stack.clen = ec->vm_stack + ec->vm_stack_size - (VALUE*)ec->cfp;
    cont->saved_vm_stack.ptr = ALLOC_N(VALUE, cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
    MEMCPY(cont->saved_vm_stack.ptr,
           ec->vm_stack,
           VALUE, cont->saved_vm_stack.slen);
    MEMCPY(cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
           (VALUE*)ec->cfp,
           VALUE,
           cont->saved_vm_stack.clen);
#else
    cont->saved_vm_stack.ptr = ALLOC_N(VALUE, ec->vm_stack_size);
    MEMCPY(cont->saved_vm_stack.ptr, ec->vm_stack, VALUE, ec->vm_stack_size);
#endif
    // At this point, `cfp` is valid but `vm_stack` should be cleared:
    rb_ec_set_vm_stack(&cont->saved_ec, NULL, 0);
    VM_ASSERT(cont->saved_ec.cfp != NULL);
    cont_save_machine_stack(th, cont);

    /* backup ensure_list to array for search in another context */
    {
        rb_ensure_list_t *p;
        int size = 0;
        rb_ensure_entry_t *entry;
        for (p=th->ec->ensure_list; p; p=p->next)
            size++;
        entry = cont->ensure_array = ALLOC_N(rb_ensure_entry_t,size+1);
        for (p=th->ec->ensure_list; p; p=p->next) {
            if (!p->entry.marker)
                p->entry.marker = rb_ary_tmp_new(0); /* dummy object */
            *entry++ = p->entry;
        }
        entry->marker = 0;
    }

    if (ruby_setjmp(cont->jmpbuf)) {
        VALUE value;

        VAR_INITIALIZED(cont);
        value = cont->value;
        if (cont->argc == -1) rb_exc_raise(value);
        cont->value = Qnil;
        *stat = 1;
        return value;
    }
    else {
        *stat = 0;
        return contval;
    }
}
COMPILER_WARNING_POP

static inline void
cont_restore_thread(rb_context_t *cont)
{
    rb_thread_t *th = GET_THREAD();

    /* restore thread context */
    if (cont->type == CONTINUATION_CONTEXT) {
        /* continuation */
        rb_execution_context_t *sec = &cont->saved_ec;
        rb_fiber_t *fiber = NULL;

        if (sec->fiber_ptr != NULL) {
            fiber = sec->fiber_ptr;
        }
        else if (th->root_fiber) {
            fiber = th->root_fiber;
        }

        if (fiber && th->ec != &fiber->cont.saved_ec) {
            ec_switch(th, fiber);
        }

        if (th->ec->trace_arg != sec->trace_arg) {
            rb_raise(rb_eRuntimeError, "can't call across trace_func");
        }

        /* copy vm stack */
#ifdef CAPTURE_JUST_VALID_VM_STACK
        MEMCPY(th->ec->vm_stack,
               cont->saved_vm_stack.ptr,
               VALUE, cont->saved_vm_stack.slen);
        MEMCPY(th->ec->vm_stack + th->ec->vm_stack_size - cont->saved_vm_stack.clen,
               cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
               VALUE, cont->saved_vm_stack.clen);
#else
        MEMCPY(th->ec->vm_stack, cont->saved_vm_stack.ptr, VALUE, sec->vm_stack_size);
#endif
        /* other members of ec */

        th->ec->cfp = sec->cfp;
        th->ec->raised_flag = sec->raised_flag;
        th->ec->tag = sec->tag;
        th->ec->root_lep = sec->root_lep;
        th->ec->root_svar = sec->root_svar;
        th->ec->ensure_list = sec->ensure_list;
        th->ec->errinfo = sec->errinfo;

        VM_ASSERT(th->ec->vm_stack != NULL);
    }
    else {
        /* fiber */
        fiber_restore_thread(th, (rb_fiber_t*)cont);
    }
}

NOINLINE(static void fiber_setcontext(rb_fiber_t *new_fiber, rb_fiber_t *old_fiber));

static void
fiber_setcontext(rb_fiber_t *new_fiber, rb_fiber_t *old_fiber)
{
    rb_thread_t *th = GET_THREAD();

    /* save old_fiber's machine stack - to ensure efficient garbage collection */
    if (!FIBER_TERMINATED_P(old_fiber)) {
        STACK_GROW_DIR_DETECTION;
        SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
        if (STACK_DIR_UPPER(0, 1)) {
            old_fiber->cont.machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
            old_fiber->cont.machine.stack = th->ec->machine.stack_end;
        }
        else {
            old_fiber->cont.machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
            old_fiber->cont.machine.stack = th->ec->machine.stack_start;
        }
    }

    /* exchange machine_stack_start between old_fiber and new_fiber */
    old_fiber->cont.saved_ec.machine.stack_start = th->ec->machine.stack_start;

    /* old_fiber->machine.stack_end should be NULL */
    old_fiber->cont.saved_ec.machine.stack_end = NULL;

    // if (DEBUG) fprintf(stderr, "fiber_setcontext: %p[%p] -> %p[%p]\n", old_fiber, old_fiber->stack.base, new_fiber, new_fiber->stack.base);

    /* swap machine context */
    struct coroutine_context * from = coroutine_transfer(&old_fiber->context, &new_fiber->context);

    if (from == NULL) {
        rb_syserr_fail(errno, "coroutine_transfer");
    }

    /* restore thread context */
    fiber_restore_thread(th, old_fiber);

    // It's possible to get here, and new_fiber is already freed.
    // if (DEBUG) fprintf(stderr, "fiber_setcontext: %p[%p] <- %p[%p]\n", old_fiber, old_fiber->stack.base, new_fiber, new_fiber->stack.base);
}

NOINLINE(NORETURN(static void cont_restore_1(rb_context_t *)));

static void
cont_restore_1(rb_context_t *cont)
{
    cont_restore_thread(cont);

    /* restore machine stack */
#ifdef _M_AMD64
    {
        /* workaround for x64 SEH */
        jmp_buf buf;
        setjmp(buf);
        _JUMP_BUFFER *bp = (void*)&cont->jmpbuf;
        bp->Frame = ((_JUMP_BUFFER*)((void*)&buf))->Frame;
    }
#endif
    if (cont->machine.stack_src) {
        FLUSH_REGISTER_WINDOWS;
        MEMCPY(cont->machine.stack_src, cont->machine.stack,
               VALUE, cont->machine.stack_size);
    }

    ruby_longjmp(cont->jmpbuf, 1);
}

NORETURN(NOINLINE(static void cont_restore_0(rb_context_t *, VALUE *)));

static void
cont_restore_0(rb_context_t *cont, VALUE *addr_in_prev_frame)
{
    if (cont->machine.stack_src) {
#ifdef HAVE_ALLOCA
#define STACK_PAD_SIZE 1
#else
#define STACK_PAD_SIZE 1024
#endif
        VALUE space[STACK_PAD_SIZE];

#if !STACK_GROW_DIRECTION
        if (addr_in_prev_frame > &space[0]) {
            /* Stack grows downward */
#endif
#if STACK_GROW_DIRECTION <= 0
            volatile VALUE *const end = cont->machine.stack_src;
            if (&space[0] > end) {
# ifdef HAVE_ALLOCA
                volatile VALUE *sp = ALLOCA_N(VALUE, &space[0] - end);
                space[0] = *sp;
# else
                cont_restore_0(cont, &space[0]);
# endif
            }
#endif
#if !STACK_GROW_DIRECTION
        }
        else {
            /* Stack grows upward */
#endif
#if STACK_GROW_DIRECTION >= 0
            volatile VALUE *const end = cont->machine.stack_src + cont->machine.stack_size;
            if (&space[STACK_PAD_SIZE] < end) {
# ifdef HAVE_ALLOCA
                volatile VALUE *sp = ALLOCA_N(VALUE, end - &space[STACK_PAD_SIZE]);
                space[0] = *sp;
# else
                cont_restore_0(cont, &space[STACK_PAD_SIZE-1]);
# endif
            }
#endif
#if !STACK_GROW_DIRECTION
        }
#endif
    }
    cont_restore_1(cont);
}

/*
 *  Document-class: Continuation
 *
 *  Continuation objects are generated by Kernel#callcc,
 *  after having +require+d <i>continuation</i>. They hold
 *  a return address and execution context, allowing a nonlocal return
 *  to the end of the #callcc block from anywhere within a
 *  program. Continuations are somewhat analogous to a structured
 *  version of C's <code>setjmp/longjmp</code> (although they contain
 *  more state, so you might consider them closer to threads).
 *
 *  For instance:
 *
 *     require "continuation"
 *     arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
 *     callcc{|cc| $cc = cc}
 *     puts(message = arr.shift)
 *     $cc.call unless message =~ /Max/
 *
 *  <em>produces:</em>
 *
 *     Freddie
 *     Herbie
 *     Ron
 *     Max
 *
 *  Also you can call callcc in other methods:
 *
 *     require "continuation"
 *
 *     def g
 *       arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
 *       cc = callcc { |cc| cc }
 *       puts arr.shift
 *       return cc, arr.size
 *     end
 *
 *     def f
 *       c, size = g
 *       c.call(c) if size > 1
 *     end
 *
 *     f
 *
 *  This (somewhat contrived) example allows the inner loop to abandon
 *  processing early:
 *
 *     require "continuation"
 *     callcc {|cont|
 *       for i in 0..4
 *         print "#{i}: "
 *         for j in i*5...(i+1)*5
 *           cont.call() if j == 17
 *           printf "%3d", j
 *         end
 *       end
 *     }
 *     puts
 *
 *  <em>produces:</em>
 *
 *     0:   0  1  2  3  4
 *     1:   5  6  7  8  9
 *     2:  10 11 12 13 14
 *     3:  15 16
 */

/*
 *  call-seq:
 *     callcc {|cont| block }   ->  obj
 *
 *  Generates a Continuation object, which it passes to
 *  the associated block. You need to <code>require
 *  'continuation'</code> before using this method. Performing a
 *  <em>cont</em><code>.call</code> will cause the #callcc
 *  to return (as will falling through the end of the block). The
 *  value returned by the #callcc is the value of the
 *  block, or the value passed to <em>cont</em><code>.call</code>. See
 *  class Continuation for more details. Also see
 *  Kernel#throw for an alternative mechanism for
 *  unwinding a call stack.
 */

static VALUE
rb_callcc(VALUE self)
{
    volatile int called;
    volatile VALUE val = cont_capture(&called);

    if (called) {
        return val;
    }
    else {
        return rb_yield(val);
    }
}

static VALUE
make_passing_arg(int argc, const VALUE *argv)
{
    switch (argc) {
      case -1:
        return argv[0];
      case 0:
        return Qnil;
      case 1:
        return argv[0];
      default:
        return rb_ary_new4(argc, argv);
    }
}

typedef VALUE e_proc(VALUE);

/* CAUTION!! : Currently, error in rollback_func is not supported  */
/* same as rb_protect if set rollback_func to NULL */
void
ruby_register_rollback_func_for_ensure(e_proc *ensure_func, e_proc *rollback_func)
{
    st_table **table_p = &GET_VM()->ensure_rollback_table;
    if (UNLIKELY(*table_p == NULL)) {
        *table_p = st_init_numtable();
    }
    st_insert(*table_p, (st_data_t)ensure_func, (st_data_t)rollback_func);
}

static inline e_proc *
lookup_rollback_func(e_proc *ensure_func)
{
    st_table *table = GET_VM()->ensure_rollback_table;
    st_data_t val;
    if (table && st_lookup(table, (st_data_t)ensure_func, &val))
        return (e_proc *) val;
    return (e_proc *) Qundef;
}


static inline void
rollback_ensure_stack(VALUE self,rb_ensure_list_t *current,rb_ensure_entry_t *target)
{
    rb_ensure_list_t *p;
    rb_ensure_entry_t *entry;
    size_t i, j;
    size_t cur_size;
    size_t target_size;
    size_t base_point;
    e_proc *func;

    cur_size = 0;
    for (p=current; p; p=p->next)
        cur_size++;
    target_size = 0;
    for (entry=target; entry->marker; entry++)
        target_size++;

    /* search common stack point */
    p = current;
    base_point = cur_size;
    while (base_point) {
        if (target_size >= base_point &&
            p->entry.marker == target[target_size - base_point].marker)
            break;
        base_point --;
        p = p->next;
    }

    /* rollback function check */
    for (i=0; i < target_size - base_point; i++) {
        if (!lookup_rollback_func(target[i].e_proc)) {
            rb_raise(rb_eRuntimeError, "continuation called from out of critical rb_ensure scope");
        }
    }
    /* pop ensure stack */
    while (cur_size > base_point) {
        /* escape from ensure block */
        (*current->entry.e_proc)(current->entry.data2);
        current = current->next;
        cur_size--;
    }
    /* push ensure stack */
    for (j = 0; j < i; j++) {
        func = lookup_rollback_func(target[i - j - 1].e_proc);
        if ((VALUE)func != Qundef) {
            (*func)(target[i - j - 1].data2);
        }
    }
}

NORETURN(static VALUE rb_cont_call(int argc, VALUE *argv, VALUE contval));

/*
 *  call-seq:
 *     cont.call(args, ...)
 *     cont[args, ...]
 *
 *  Invokes the continuation. The program continues from the end of
 *  the #callcc block. If no arguments are given, the original #callcc
 *  returns +nil+. If one argument is given, #callcc returns
 *  it. Otherwise, an array containing <i>args</i> is returned.
 *
 *     callcc {|cont|  cont.call }           #=> nil
 *     callcc {|cont|  cont.call 1 }         #=> 1
 *     callcc {|cont|  cont.call 1, 2, 3 }   #=> [1, 2, 3]
 */

static VALUE
rb_cont_call(int argc, VALUE *argv, VALUE contval)
{
    rb_context_t *cont = cont_ptr(contval);
    rb_thread_t *th = GET_THREAD();

    if (cont_thread_value(cont) != th->self) {
        rb_raise(rb_eRuntimeError, "continuation called across threads");
    }
    if (cont->saved_ec.fiber_ptr) {
        if (th->ec->fiber_ptr != cont->saved_ec.fiber_ptr) {
            rb_raise(rb_eRuntimeError, "continuation called across fiber");
        }
    }
    rollback_ensure_stack(contval, th->ec->ensure_list, cont->ensure_array);

    cont->argc = argc;
    cont->value = make_passing_arg(argc, argv);

    cont_restore_0(cont, &contval);
    UNREACHABLE_RETURN(Qnil);
}

/*********/
/* fiber */
/*********/

/*
 *  Document-class: Fiber
 *
 *  Fibers are primitives for implementing light weight cooperative
 *  concurrency in Ruby. Basically they are a means of creating code blocks
 *  that can be paused and resumed, much like threads. The main difference
 *  is that they are never preempted and that the scheduling must be done by
 *  the programmer and not the VM.
 *
 *  As opposed to other stackless light weight concurrency models, each fiber
 *  comes with a stack.  This enables the fiber to be paused from deeply
 *  nested function calls within the fiber block.  See the ruby(1)
 *  manpage to configure the size of the fiber stack(s).
 *
 *  When a fiber is created it will not run automatically. Rather it must
 *  be explicitly asked to run using the Fiber#resume method.
 *  The code running inside the fiber can give up control by calling
 *  Fiber.yield in which case it yields control back to caller (the
 *  caller of the Fiber#resume).
 *
 *  Upon yielding or termination the Fiber returns the value of the last
 *  executed expression
 *
 *  For instance:
 *
 *    fiber = Fiber.new do
 *      Fiber.yield 1
 *      2
 *    end
 *
 *    puts fiber.resume
 *    puts fiber.resume
 *    puts fiber.resume
 *
 *  <em>produces</em>
 *
 *    1
 *    2
 *    FiberError: dead fiber called
 *
 *  The Fiber#resume method accepts an arbitrary number of parameters,
 *  if it is the first call to #resume then they will be passed as
 *  block arguments. Otherwise they will be the return value of the
 *  call to Fiber.yield
 *
 *  Example:
 *
 *    fiber = Fiber.new do |first|
 *      second = Fiber.yield first + 2
 *    end
 *
 *    puts fiber.resume 10
 *    puts fiber.resume 1_000_000
 *    puts fiber.resume "The fiber will be dead before I can cause trouble"
 *
 *  <em>produces</em>
 *
 *    12
 *    1000000
 *    FiberError: dead fiber called
 *
 *  == Non-blocking Fibers
 *
 *  The concept of <em>non-blocking fiber</em> was introduced in Ruby 3.0.
 *  A non-blocking fiber, when reaching a operation that would normally block
 *  the fiber (like <code>sleep</code>, or wait for another process or I/O)
 #  will yield control to other fibers and allow the <em>scheduler</em> to
 #  handle blocking and waking up (resuming) this fiber when it can proceed.
 *
 *  For a Fiber to behave as non-blocking, it need to be created in Fiber.new with
 *  <tt>blocking: false</tt> (which is the default), and Fiber.scheduler
 *  should be set with Fiber.set_scheduler. If Fiber.scheduler is not set in
 *  the current thread, blocking and non-blocking fibers' behavior is identical.
 *
 *  Ruby doesn't provide a scheduler class: it is expected to be implemented by
 *  the user and correspond to Fiber::SchedulerInterface.
 *
 *  There is also Fiber.schedule method, which is expected to immediately perform
 *  the given block in a non-blocking manner. Its actual implementation is up to
 *  the scheduler.
 *
 */

static const rb_data_type_t fiber_data_type = {
    "fiber",
    {fiber_mark, fiber_free, fiber_memsize, fiber_compact,},
    0, 0, RUBY_TYPED_FREE_IMMEDIATELY
};

static VALUE
fiber_alloc(VALUE klass)
{
    return TypedData_Wrap_Struct(klass, &fiber_data_type, 0);
}

static rb_fiber_t*
fiber_t_alloc(VALUE fiber_value, unsigned int blocking)
{
    rb_fiber_t *fiber;
    rb_thread_t *th = GET_THREAD();

    if (DATA_PTR(fiber_value) != 0) {
        rb_raise(rb_eRuntimeError, "cannot initialize twice");
    }

    THREAD_MUST_BE_RUNNING(th);
    fiber = ZALLOC(rb_fiber_t);
    fiber->cont.self = fiber_value;
    fiber->cont.type = FIBER_CONTEXT;
    fiber->blocking = blocking;
    cont_init(&fiber->cont, th);

    fiber->cont.saved_ec.fiber_ptr = fiber;
    rb_ec_clear_vm_stack(&fiber->cont.saved_ec);

    fiber->prev = NULL;

    /* fiber->status == 0 == CREATED
     * So that we don't need to set status: fiber_status_set(fiber, FIBER_CREATED); */
    VM_ASSERT(FIBER_CREATED_P(fiber));

    DATA_PTR(fiber_value) = fiber;

    return fiber;
}

static VALUE
fiber_initialize(VALUE self, VALUE proc, struct fiber_pool * fiber_pool, unsigned int blocking)
{
    rb_fiber_t *fiber = fiber_t_alloc(self, blocking);

    fiber->first_proc = proc;
    fiber->stack.base = NULL;
    fiber->stack.pool = fiber_pool;

    return self;
}

static void
fiber_prepare_stack(rb_fiber_t *fiber)
{
    rb_context_t *cont = &fiber->cont;
    rb_execution_context_t *sec = &cont->saved_ec;

    size_t vm_stack_size = 0;
    VALUE *vm_stack = fiber_initialize_coroutine(fiber, &vm_stack_size);

    /* initialize cont */
    cont->saved_vm_stack.ptr = NULL;
    rb_ec_initialize_vm_stack(sec, vm_stack, vm_stack_size / sizeof(VALUE));

    sec->tag = NULL;
    sec->local_storage = NULL;
    sec->local_storage_recursive_hash = Qnil;
    sec->local_storage_recursive_hash_for_trace = Qnil;
}

static struct fiber_pool *
rb_fiber_pool_default(VALUE pool)
{
    return &shared_fiber_pool;
}

/* :nodoc: */
static VALUE
rb_fiber_initialize_kw(int argc, VALUE* argv, VALUE self, int kw_splat)
{
    VALUE pool = Qnil;
    VALUE blocking = Qfalse;

    if (kw_splat != RB_NO_KEYWORDS) {
        VALUE options = Qnil;
        VALUE arguments[2] = {Qundef};

        argc = rb_scan_args_kw(kw_splat, argc, argv, ":", &options);
        rb_get_kwargs(options, fiber_initialize_keywords, 0, 2, arguments);

        if (arguments[0] != Qundef) {
            blocking = arguments[0];
        }

        if (arguments[1] != Qundef) {
            pool = arguments[1];
        }
    }

    return fiber_initialize(self, rb_block_proc(), rb_fiber_pool_default(pool), RTEST(blocking));
}

/*
 *  call-seq:
 *     Fiber.new(blocking: false) { |*args| ... } -> fiber
 *
 *  Creates new Fiber. Initially, the fiber is not running and can be resumed with
 *  #resume. Arguments to the first #resume call will be passed to the block:
 *
 *      f = Fiber.new do |initial|
 *         current = initial
 *         loop do
 *           puts "current: #{current.inspect}"
 *           current = Fiber.yield
 *         end
 *      end
 *      f.resume(100)     # prints: current: 100
 *      f.resume(1, 2, 3) # prints: current: [1, 2, 3]
 *      f.resume          # prints: current: nil
 *      # ... and so on ...
 *
 *  If <tt>blocking: false</tt> is passed to <tt>Fiber.new</tt>, _and_ current thread
 *  has a Fiber.scheduler defined, the Fiber becomes non-blocking (see "Non-blocking
 *  Fibers" section in class docs).
 */
static VALUE
rb_fiber_initialize(int argc, VALUE* argv, VALUE self)
{
    return rb_fiber_initialize_kw(argc, argv, self, rb_keyword_given_p());
}

VALUE
rb_fiber_new(rb_block_call_func_t func, VALUE obj)
{
    return fiber_initialize(fiber_alloc(rb_cFiber), rb_proc_new(func, obj), rb_fiber_pool_default(Qnil), 1);
}

static VALUE
rb_fiber_s_schedule_kw(int argc, VALUE* argv, int kw_splat)
{
    rb_thread_t * th = GET_THREAD();
    VALUE scheduler = th->scheduler;
    VALUE fiber = Qnil;

    if (scheduler != Qnil) {
        fiber = rb_funcall_passing_block_kw(scheduler, rb_intern("fiber"), argc, argv, kw_splat);
    }
    else {
        rb_raise(rb_eRuntimeError, "No scheduler is available!");
    }

    return fiber;
}

/*
 *  call-seq:
 *     Fiber.schedule { |*args| ... } -> fiber
 *
 *  The method is <em>expected</em> to immediately run the provided block of code in a
 *  separate non-blocking fiber.
 *
 *     puts "Go to sleep!"
 *
 *     Fiber.set_scheduler(MyScheduler.new)
 *
 *     Fiber.schedule do
 *       puts "Going to sleep"
 *       sleep(1)
 *       puts "I slept well"
 *     end
 *
 *     puts "Wakey-wakey, sleepyhead"
 *
 *  Assuming MyScheduler is properly implemented, this program will produce:
 *
 *     Go to sleep!
 *     Going to sleep
 *     Wakey-wakey, sleepyhead
 *     ...1 sec pause here...
 *     I slept well
 *
 *  ...e.g. on the first blocking operation inside the Fiber (<tt>sleep(1)</tt>),
 *  the control is yielded to the outside code (main fiber), and <em>at the end
 *  of that execution</em>, the scheduler takes care of properly resuming all the
 *  blocked fibers.
 *
 *  Note that the behavior described above is how the method is <em>expected</em>
 *  to behave, actual behavior is up to the current scheduler's implementation of
 *  Fiber::SchedulerInterface#fiber method. Ruby doesn't enforce this method to
 *  behave in any particular way.
 *
 *  If the scheduler is not set, the method raises
 *  <tt>RuntimeError (No scheduler is available!)</tt>.
 *
 */
static VALUE
rb_fiber_s_schedule(int argc, VALUE *argv, VALUE obj)
{
    return rb_fiber_s_schedule_kw(argc, argv, rb_keyword_given_p());
}

/*
 *  call-seq:
 *     Fiber.scheduler -> obj or nil
 *
 *  Returns the Fiber scheduler, that was last set for the current thread with Fiber.set_scheduler.
 *  Returns +nil+ if no scheduler is set (which is the default), and non-blocking fibers'
 #  behavior is the same as blocking.
 *  (see "Non-blocking fibers" section in class docs for details about the scheduler concept).
 *
 */
static VALUE
rb_fiber_s_scheduler(VALUE klass)
{
    return rb_fiber_scheduler_get();
}

/*
 *  call-seq:
 *     Fiber.current_scheduler -> obj or nil
 *
 *  Returns the Fiber scheduler, that was last set for the current thread with Fiber.set_scheduler
 *  if and only if the current fiber is non-blocking.
 *
 */
static VALUE
rb_fiber_current_scheduler(VALUE klass)
{
    return rb_fiber_scheduler_current();
}

/*
 *  call-seq:
 *     Fiber.set_scheduler(scheduler) -> scheduler
 *
 *  Sets the Fiber scheduler for the current thread. If the scheduler is set, non-blocking
 *  fibers (created by Fiber.new with <tt>blocking: false</tt>, or by Fiber.schedule)
 *  call that scheduler's hook methods on potentially blocking operations, and the current
 *  thread will call scheduler's +close+ method on finalization (allowing the scheduler to
 *  properly manage all non-finished fibers).
 *
 *  +scheduler+ can be an object of any class corresponding to Fiber::SchedulerInterface. Its
 *  implementation is up to the user.
 *
 *  See also the "Non-blocking fibers" section in class docs.
 *
 */
static VALUE
rb_fiber_set_scheduler(VALUE klass, VALUE scheduler)
{
    return rb_fiber_scheduler_set(scheduler);
}

static void rb_fiber_terminate(rb_fiber_t *fiber, int need_interrupt, VALUE err);

void
rb_fiber_start(rb_fiber_t *fiber)
{
    rb_thread_t * volatile th = fiber->cont.saved_ec.thread_ptr;

    rb_proc_t *proc;
    enum ruby_tag_type state;
    int need_interrupt = TRUE;

    VM_ASSERT(th->ec == GET_EC());
    VM_ASSERT(FIBER_RESUMED_P(fiber));

    if (fiber->blocking) {
        th->blocking += 1;
    }

    EC_PUSH_TAG(th->ec);
    if ((state = EC_EXEC_TAG()) == TAG_NONE) {
        rb_context_t *cont = &VAR_FROM_MEMORY(fiber)->cont;
        int argc;
        const VALUE *argv, args = cont->value;
        GetProcPtr(fiber->first_proc, proc);
        argv = (argc = cont->argc) > 1 ? RARRAY_CONST_PTR(args) : &args;
        cont->value = Qnil;
        th->ec->errinfo = Qnil;
        th->ec->root_lep = rb_vm_proc_local_ep(fiber->first_proc);
        th->ec->root_svar = Qfalse;

        EXEC_EVENT_HOOK(th->ec, RUBY_EVENT_FIBER_SWITCH, th->self, 0, 0, 0, Qnil);
        cont->value = rb_vm_invoke_proc(th->ec, proc, argc, argv, cont->kw_splat, VM_BLOCK_HANDLER_NONE);
    }
    EC_POP_TAG();

    VALUE err = Qfalse;
    if (state) {
        err = th->ec->errinfo;
        VM_ASSERT(FIBER_RESUMED_P(fiber));

        if (state == TAG_RAISE) {
            // noop...
        }
        else if (state == TAG_FATAL) {
            rb_threadptr_pending_interrupt_enque(th, err);
        }
        else {
            err = rb_vm_make_jump_tag_but_local_jump(state, err);
        }
        need_interrupt = TRUE;
    }

    rb_fiber_terminate(fiber, need_interrupt, err);
}

static rb_fiber_t *
root_fiber_alloc(rb_thread_t *th)
{
    VALUE fiber_value = fiber_alloc(rb_cFiber);
    rb_fiber_t *fiber = th->ec->fiber_ptr;

    VM_ASSERT(DATA_PTR(fiber_value) == NULL);
    VM_ASSERT(fiber->cont.type == FIBER_CONTEXT);
    VM_ASSERT(fiber->status == FIBER_RESUMED);

    th->root_fiber = fiber;
    DATA_PTR(fiber_value) = fiber;
    fiber->cont.self = fiber_value;

    coroutine_initialize_main(&fiber->context);

    return fiber;
}

void
rb_threadptr_root_fiber_setup(rb_thread_t *th)
{
    rb_fiber_t *fiber = ruby_mimmalloc(sizeof(rb_fiber_t));
    if (!fiber) {
        rb_bug("%s", strerror(errno)); /* ... is it possible to call rb_bug here? */
    }
    MEMZERO(fiber, rb_fiber_t, 1);
    fiber->cont.type = FIBER_CONTEXT;
    fiber->cont.saved_ec.fiber_ptr = fiber;
    fiber->cont.saved_ec.thread_ptr = th;
    fiber->blocking = 1;
    fiber_status_set(fiber, FIBER_RESUMED); /* skip CREATED */
    th->ec = &fiber->cont.saved_ec;
    // This skips mjit_cont_new for the initial thread because mjit_enabled is always false
    // at this point. mjit_init calls rb_fiber_init_mjit_cont again for this root_fiber.
    rb_fiber_init_mjit_cont(fiber);
}

void
rb_threadptr_root_fiber_release(rb_thread_t *th)
{
    if (th->root_fiber) {
        /* ignore. A root fiber object will free th->ec */
    }
    else {
        rb_execution_context_t *ec = GET_EC();

        VM_ASSERT(th->ec->fiber_ptr->cont.type == FIBER_CONTEXT);
        VM_ASSERT(th->ec->fiber_ptr->cont.self == 0);

        if (th->ec == ec) {
            rb_ractor_set_current_ec(th->ractor, NULL);
        }
        fiber_free(th->ec->fiber_ptr);
        th->ec = NULL;
    }
}

void
rb_threadptr_root_fiber_terminate(rb_thread_t *th)
{
    rb_fiber_t *fiber = th->ec->fiber_ptr;

    fiber->status = FIBER_TERMINATED;

    // The vm_stack is `alloca`ed on the thread stack, so it's gone too:
    rb_ec_clear_vm_stack(th->ec);
}

static inline rb_fiber_t*
fiber_current(void)
{
    rb_execution_context_t *ec = GET_EC();
    if (ec->fiber_ptr->cont.self == 0) {
        root_fiber_alloc(rb_ec_thread_ptr(ec));
    }
    return ec->fiber_ptr;
}

static inline rb_fiber_t*
return_fiber(bool terminate)
{
    rb_fiber_t *fiber = fiber_current();
    rb_fiber_t *prev = fiber->prev;

    if (prev) {
        fiber->prev = NULL;
        prev->resuming_fiber = Qnil;
        return prev;
    }
    else {
        if (!terminate) {
            rb_raise(rb_eFiberError, "attempt to yield on a not resumed fiber");
        }

        rb_thread_t *th = GET_THREAD();
        rb_fiber_t *root_fiber = th->root_fiber;

        VM_ASSERT(root_fiber != NULL);

        // search resuming fiber
        for (fiber = root_fiber;
             RTEST(fiber->resuming_fiber);
             fiber = fiber_ptr(fiber->resuming_fiber)) {
        }

        return fiber;
    }
}

VALUE
rb_fiber_current(void)
{
    return fiber_current()->cont.self;
}

// Prepare to execute next_fiber on the given thread.
static inline void
fiber_store(rb_fiber_t *next_fiber, rb_thread_t *th)
{
    rb_fiber_t *fiber;

    if (th->ec->fiber_ptr != NULL) {
        fiber = th->ec->fiber_ptr;
    }
    else {
        /* create root fiber */
        fiber = root_fiber_alloc(th);
    }

    if (FIBER_CREATED_P(next_fiber)) {
        fiber_prepare_stack(next_fiber);
    }

    VM_ASSERT(FIBER_RESUMED_P(fiber) || FIBER_TERMINATED_P(fiber));
    VM_ASSERT(FIBER_RUNNABLE_P(next_fiber));

    if (FIBER_RESUMED_P(fiber)) fiber_status_set(fiber, FIBER_SUSPENDED);

    fiber_status_set(next_fiber, FIBER_RESUMED);
    fiber_setcontext(next_fiber, fiber);
}

static inline VALUE
fiber_switch(rb_fiber_t *fiber, int argc, const VALUE *argv, int kw_splat, VALUE resuming_fiber, bool yielding)
{
    VALUE value;
    rb_context_t *cont = &fiber->cont;
    rb_thread_t *th = GET_THREAD();

    /* make sure the root_fiber object is available */
    if (th->root_fiber == NULL) root_fiber_alloc(th);

    if (th->ec->fiber_ptr == fiber) {
        /* ignore fiber context switch
         * because destination fiber is the same as current fiber
         */
        return make_passing_arg(argc, argv);
    }

    if (cont_thread_value(cont) != th->self) {
        rb_raise(rb_eFiberError, "fiber called across threads");
    }

    if (FIBER_TERMINATED_P(fiber)) {
        value = rb_exc_new2(rb_eFiberError, "dead fiber called");

        if (!FIBER_TERMINATED_P(th->ec->fiber_ptr)) {
            rb_exc_raise(value);
            VM_UNREACHABLE(fiber_switch);
        }
        else {
            /* th->ec->fiber_ptr is also dead => switch to root fiber */
            /* (this means we're being called from rb_fiber_terminate, */
            /* and the terminated fiber's return_fiber() is already dead) */
            VM_ASSERT(FIBER_SUSPENDED_P(th->root_fiber));

            cont = &th->root_fiber->cont;
            cont->argc = -1;
            cont->value = value;

            fiber_setcontext(th->root_fiber, th->ec->fiber_ptr);

            VM_UNREACHABLE(fiber_switch);
        }
    }

    VM_ASSERT(FIBER_RUNNABLE_P(fiber));

    rb_fiber_t *current_fiber = fiber_current();

    VM_ASSERT(!RTEST(current_fiber->resuming_fiber));
    if (RTEST(resuming_fiber)) {
        current_fiber->resuming_fiber = resuming_fiber;
        fiber->prev = fiber_current();
        fiber->yielding = 0;
    }

    VM_ASSERT(!current_fiber->yielding);
    if (yielding) {
        current_fiber->yielding = 1;
    }

    if (current_fiber->blocking) {
        th->blocking -= 1;
    }

    cont->argc = argc;
    cont->kw_splat = kw_splat;
    cont->value = make_passing_arg(argc, argv);

    fiber_store(fiber, th);

    // We cannot free the stack until the pthread is joined:
#ifndef COROUTINE_PTHREAD_CONTEXT
    if (RTEST(resuming_fiber) && FIBER_TERMINATED_P(fiber)) {
        fiber_stack_release(fiber);
    }
#endif

    if (fiber_current()->blocking) {
        th->blocking += 1;
    }

    RUBY_VM_CHECK_INTS(th->ec);

    EXEC_EVENT_HOOK(th->ec, RUBY_EVENT_FIBER_SWITCH, th->self, 0, 0, 0, Qnil);

    current_fiber = th->ec->fiber_ptr;
    value = current_fiber->cont.value;
    if (current_fiber->cont.argc == -1) rb_exc_raise(value);
    return value;
}

VALUE
rb_fiber_transfer(VALUE fiber_value, int argc, const VALUE *argv)
{
    return fiber_switch(fiber_ptr(fiber_value), argc, argv, RB_NO_KEYWORDS, Qfalse, false);
}

/*
 *  call-seq:
 *     fiber.blocking? -> true or false
 *
 *  Returns +true+ if +fiber+ is blocking and +false+ otherwise.
 *  Fiber is non-blocking if it was created via passing <tt>blocking: false</tt>
 *  to Fiber.new, or via Fiber.schedule.
 *
 *  Note that, even if the method returns +false+, the fiber behaves differently
 *  only if Fiber.scheduler is set in the current thread.
 *
 *  See the "Non-blocking fibers" section in class docs for details.
 *
 */
VALUE
rb_fiber_blocking_p(VALUE fiber)
{
    return (fiber_ptr(fiber)->blocking == 0) ? Qfalse : Qtrue;
}

/*
 *  call-seq:
 *     Fiber.blocking? -> false or 1
 *
 *  Returns +false+ if the current fiber is non-blocking.
 *  Fiber is non-blocking if it was created via passing <tt>blocking: false</tt>
 *  to Fiber.new, or via Fiber.schedule.
 *
 *  If the current Fiber is blocking, the method returns 1.
 *  Future developments may allow for situations where larger integers
 *  could be returned.
 *
 *  Note that, even if the method returns +false+, Fiber behaves differently
 *  only if Fiber.scheduler is set in the current thread.
 *
 *  See the "Non-blocking fibers" section in class docs for details.
 *
 */
static VALUE
rb_fiber_s_blocking_p(VALUE klass)
{
    rb_thread_t *thread = GET_THREAD();
    unsigned blocking = thread->blocking;

    if (blocking == 0)
        return Qfalse;

    return INT2NUM(blocking);
}

void
rb_fiber_close(rb_fiber_t *fiber)
{
    fiber_status_set(fiber, FIBER_TERMINATED);
}

static void
rb_fiber_terminate(rb_fiber_t *fiber, int need_interrupt, VALUE error)
{
    VALUE value = fiber->cont.value;

    VM_ASSERT(FIBER_RESUMED_P(fiber));
    rb_fiber_close(fiber);

    fiber->cont.machine.stack = NULL;
    fiber->cont.machine.stack_size = 0;

    rb_fiber_t *next_fiber = return_fiber(true);

    if (need_interrupt) RUBY_VM_SET_INTERRUPT(&next_fiber->cont.saved_ec);

    if (RTEST(error))
        fiber_switch(next_fiber, -1, &error, RB_NO_KEYWORDS, Qfalse, false);
    else
        fiber_switch(next_fiber, 1, &value, RB_NO_KEYWORDS, Qfalse, false);
}

VALUE
rb_fiber_resume_kw(VALUE fiber_value, int argc, const VALUE *argv, int kw_splat)
{
    rb_fiber_t *fiber = fiber_ptr(fiber_value);
    rb_fiber_t *current_fiber = fiber_current();

    if (argc == -1 && FIBER_CREATED_P(fiber)) {
        rb_raise(rb_eFiberError, "cannot raise exception on unborn fiber");
    }
    else if (FIBER_TERMINATED_P(fiber)) {
        rb_raise(rb_eFiberError, "attempt to resume a terminated fiber");
    }
    else if (fiber == current_fiber) {
        rb_raise(rb_eFiberError, "attempt to resume the current fiber");
    }
    else if (fiber->prev != NULL) {
        rb_raise(rb_eFiberError, "attempt to resume a resumed fiber (double resume)");
    }
    else if (RTEST(fiber->resuming_fiber)) {
        rb_raise(rb_eFiberError, "attempt to resume a resuming fiber");
    }
    else if (fiber->prev == NULL &&
             (!fiber->yielding && fiber->status != FIBER_CREATED)) {
        rb_raise(rb_eFiberError, "attempt to resume a transferring fiber");
    }

    VALUE result = fiber_switch(fiber, argc, argv, kw_splat, fiber_value, false);

    return result;
}

VALUE
rb_fiber_resume(VALUE fiber_value, int argc, const VALUE *argv)
{
    return rb_fiber_resume_kw(fiber_value, argc, argv, RB_NO_KEYWORDS);
}

VALUE
rb_fiber_yield_kw(int argc, const VALUE *argv, int kw_splat)
{
    return fiber_switch(return_fiber(false), argc, argv, kw_splat, Qfalse, true);
}

VALUE
rb_fiber_yield(int argc, const VALUE *argv)
{
    return fiber_switch(return_fiber(false), argc, argv, RB_NO_KEYWORDS, Qfalse, true);
}

void
rb_fiber_reset_root_local_storage(rb_thread_t *th)
{
    if (th->root_fiber && th->root_fiber != th->ec->fiber_ptr) {
        th->ec->local_storage = th->root_fiber->cont.saved_ec.local_storage;
    }
}

/*
 *  call-seq:
 *     fiber.alive? -> true or false
 *
 *  Returns true if the fiber can still be resumed (or transferred
 *  to). After finishing execution of the fiber block this method will
 *  always return +false+.
 */
VALUE
rb_fiber_alive_p(VALUE fiber_value)
{
    return FIBER_TERMINATED_P(fiber_ptr(fiber_value)) ? Qfalse : Qtrue;
}

/*
 *  call-seq:
 *     fiber.resume(args, ...) -> obj
 *
 *  Resumes the fiber from the point at which the last Fiber.yield was
 *  called, or starts running it if it is the first call to
 *  #resume. Arguments passed to resume will be the value of the
 *  Fiber.yield expression or will be passed as block parameters to
 *  the fiber's block if this is the first #resume.
 *
 *  Alternatively, when resume is called it evaluates to the arguments passed
 *  to the next Fiber.yield statement inside the fiber's block
 *  or to the block value if it runs to completion without any
 *  Fiber.yield
 */
static VALUE
rb_fiber_m_resume(int argc, VALUE *argv, VALUE fiber)
{
    return rb_fiber_resume_kw(fiber, argc, argv, rb_keyword_given_p());
}

VALUE rb_fiber_transfer_kw(VALUE fiber_value, int argc, const VALUE *argv, int kw_splat);

/*
 *  call-seq:
 *     fiber.raise                                 -> obj
 *     fiber.raise(string)                         -> obj
 *     fiber.raise(exception [, string [, array]]) -> obj
 *
 *  Raises an exception in the fiber at the point at which the last
 *  +Fiber.yield+ was called. If the fiber has not been started or has
 *  already run to completion, raises +FiberError+. If the fiber is
 *  yielding, it is resumed. If it is transferring, it is transferred into.
 *  But if it is resuming, raises +FiberError+.
 *
 *  With no arguments, raises a +RuntimeError+. With a single +String+
 *  argument, raises a +RuntimeError+ with the string as a message.  Otherwise,
 *  the first parameter should be the name of an +Exception+ class (or an
 *  object that returns an +Exception+ object when sent an +exception+
 *  message). The optional second parameter sets the message associated with
 *  the exception, and the third parameter is an array of callback information.
 *  Exceptions are caught by the +rescue+ clause of <code>begin...end</code>
 *  blocks.
 */
static VALUE
rb_fiber_raise(int argc, VALUE *argv, VALUE fiber_value)
{
    rb_fiber_t *fiber = fiber_ptr(fiber_value);
    VALUE exc = rb_make_exception(argc, argv);
    if (RTEST(fiber->resuming_fiber)) {
        rb_raise(rb_eFiberError, "attempt to raise a resuming fiber");
    }
    else if (FIBER_SUSPENDED_P(fiber) && !fiber->yielding) {
        return rb_fiber_transfer_kw(fiber_value, -1, &exc, RB_NO_KEYWORDS);
    }
    else {
        return rb_fiber_resume_kw(fiber_value, -1, &exc, RB_NO_KEYWORDS);
    }
}

/*
 *  call-seq:
 *     fiber.backtrace -> array
 *     fiber.backtrace(start) -> array
 *     fiber.backtrace(start, count) -> array
 *     fiber.backtrace(start..end) -> array
 *
 *  Returns the current execution stack of the fiber. +start+, +count+ and +end+ allow
 *  to select only parts of the backtrace.
 *
 *     def level3
 *       Fiber.yield
 *     end
 *
 *     def level2
 *       level3
 *     end
 *
 *     def level1
 *       level2
 *     end
 *
 *     f = Fiber.new { level1 }
 *
 *     # It is empty before the fiber started
 *     f.backtrace
 *     #=> []
 *
 *     f.resume
 *
 *     f.backtrace
 *     #=> ["test.rb:2:in `yield'", "test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"]
 *     p f.backtrace(1) # start from the item 1
 *     #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"]
 *     p f.backtrace(2, 2) # start from item 2, take 2
 *     #=> ["test.rb:6:in `level2'", "test.rb:10:in `level1'"]
 *     p f.backtrace(1..3) # take items from 1 to 3
 *     #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'"]
 *
 *     f.resume
 *
 *     # It is nil after the fiber is finished
 *     f.backtrace
 *     #=> nil
 *
 */
static VALUE
rb_fiber_backtrace(int argc, VALUE *argv, VALUE fiber)
{
    return rb_vm_backtrace(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
}

/*
 *  call-seq:
 *     fiber.backtrace_locations -> array
 *     fiber.backtrace_locations(start) -> array
 *     fiber.backtrace_locations(start, count) -> array
 *     fiber.backtrace_locations(start..end) -> array
 *
 *  Like #backtrace, but returns each line of the execution stack as a
 *  Thread::Backtrace::Location. Accepts the same arguments as #backtrace.
 *
 *    f = Fiber.new { Fiber.yield }
 *    f.resume
 *    loc = f.backtrace_locations.first
 *    loc.label  #=> "yield"
 *    loc.path   #=> "test.rb"
 *    loc.lineno #=> 1
 *
 *
 */
static VALUE
rb_fiber_backtrace_locations(int argc, VALUE *argv, VALUE fiber)
{
    return rb_vm_backtrace_locations(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
}

/*
 *  call-seq:
 *     fiber.transfer(args, ...) -> obj
 *
 *  Transfer control to another fiber, resuming it from where it last
 *  stopped or starting it if it was not resumed before. The calling
 *  fiber will be suspended much like in a call to
 *  Fiber.yield.
 *
 *  The fiber which receives the transfer call treats it much like
 *  a resume call. Arguments passed to transfer are treated like those
 *  passed to resume.
 *
 *  The two style of control passing to and from fiber (one is #resume and
 *  Fiber::yield, another is #transfer to and from fiber) can't be freely
 *  mixed.
 *
 *  * If the Fiber's lifecycle had started with transfer, it will never
 *    be able to yield or be resumed control passing, only
 *    finish or transfer back. (It still can resume other fibers that
 *    are allowed to be resumed.)
 *  * If the Fiber's lifecycle had started with resume, it can yield
 *    or transfer to another Fiber, but can receive control back only
 *    the way compatible with the way it was given away: if it had
 *    transferred, it only can be transferred back, and if it had
 *    yielded, it only can be resumed back. After that, it again can
 *    transfer or yield.
 *
 *  If those rules are broken FiberError is raised.
 *
 *  For an individual Fiber design, yield/resume is easier to use
 *  (the Fiber just gives away control, it doesn't need to think
 *  about who the control is given to), while transfer is more flexible
 *  for complex cases, allowing to build arbitrary graphs of Fibers
 *  dependent on each other.
 *
 *
 *  Example:
 *
 *     manager = nil # For local var to be visible inside worker block
 *
 *     # This fiber would be started with transfer
 *     # It can't yield, and can't be resumed
 *     worker = Fiber.new { |work|
 *       puts "Worker: starts"
 *       puts "Worker: Performed #{work.inspect}, transferring back"
 *       # Fiber.yield     # this would raise FiberError: attempt to yield on a not resumed fiber
 *       # manager.resume  # this would raise FiberError: attempt to resume a resumed fiber (double resume)
 *       manager.transfer(work.capitalize)
 *     }
 *
 *     # This fiber would be started with resume
 *     # It can yield or transfer, and can be transferred
 *     # back or resumed
 *     manager = Fiber.new {
 *       puts "Manager: starts"
 *       puts "Manager: transferring 'something' to worker"
 *       result = worker.transfer('something')
 *       puts "Manager: worker returned #{result.inspect}"
 *       # worker.resume    # this would raise FiberError: attempt to resume a transferring fiber
 *       Fiber.yield        # this is OK, the fiber transferred from and to, now it can yield
 *       puts "Manager: finished"
 *     }
 *
 *     puts "Starting the manager"
 *     manager.resume
 *     puts "Resuming the manager"
 *     # manager.transfer  # this would raise FiberError: attempt to transfer to a yielding fiber
 *     manager.resume
 *
 *  <em>produces</em>
 *
 *     Starting the manager
 *     Manager: starts
 *     Manager: transferring 'something' to worker
 *     Worker: starts
 *     Worker: Performed "something", transferring back
 *     Manager: worker returned "Something"
 *     Resuming the manager
 *     Manager: finished
 *
 */
static VALUE
rb_fiber_m_transfer(int argc, VALUE *argv, VALUE fiber_value)
{
    return rb_fiber_transfer_kw(fiber_value, argc, argv, rb_keyword_given_p());
}

VALUE
rb_fiber_transfer_kw(VALUE fiber_value, int argc, const VALUE *argv, int kw_splat)
{
    rb_fiber_t *fiber = fiber_ptr(fiber_value);
    if (RTEST(fiber->resuming_fiber)) {
        rb_raise(rb_eFiberError, "attempt to transfer to a resuming fiber");
    }
    if (fiber->yielding) {
        rb_raise(rb_eFiberError, "attempt to transfer to a yielding fiber");
    }
    return fiber_switch(fiber, argc, argv, kw_splat, Qfalse, false);
}

/*
 *  call-seq:
 *     Fiber.yield(args, ...) -> obj
 *
 *  Yields control back to the context that resumed the fiber, passing
 *  along any arguments that were passed to it. The fiber will resume
 *  processing at this point when #resume is called next.
 *  Any arguments passed to the next #resume will be the value that
 *  this Fiber.yield expression evaluates to.
 */
static VALUE
rb_fiber_s_yield(int argc, VALUE *argv, VALUE klass)
{
    return rb_fiber_yield_kw(argc, argv, rb_keyword_given_p());
}

/*
 *  call-seq:
 *     Fiber.current -> fiber
 *
 *  Returns the current fiber. If you are not running in the context of
 *  a fiber this method will return the root fiber.
 */
static VALUE
rb_fiber_s_current(VALUE klass)
{
    return rb_fiber_current();
}

static VALUE
fiber_to_s(VALUE fiber_value)
{
    const rb_fiber_t *fiber = fiber_ptr(fiber_value);
    const rb_proc_t *proc;
    char status_info[0x20];

    if (RTEST(fiber->resuming_fiber)) {
        snprintf(status_info, 0x20, " (%s by resuming)", fiber_status_name(fiber->status));
    }
    else {
        snprintf(status_info, 0x20, " (%s)", fiber_status_name(fiber->status));
    }

    if (!rb_obj_is_proc(fiber->first_proc)) {
        VALUE str = rb_any_to_s(fiber_value);
        strlcat(status_info, ">", sizeof(status_info));
        rb_str_set_len(str, RSTRING_LEN(str)-1);
        rb_str_cat_cstr(str, status_info);
        return str;
    }
    GetProcPtr(fiber->first_proc, proc);
    return rb_block_to_s(fiber_value, &proc->block, status_info);
}

#ifdef HAVE_WORKING_FORK
void
rb_fiber_atfork(rb_thread_t *th)
{
    if (th->root_fiber) {
        if (&th->root_fiber->cont.saved_ec != th->ec) {
            th->root_fiber = th->ec->fiber_ptr;
        }
        th->root_fiber->prev = 0;
    }
}
#endif

#ifdef RB_EXPERIMENTAL_FIBER_POOL
static void
fiber_pool_free(void *ptr)
{
    struct fiber_pool * fiber_pool = ptr;
    RUBY_FREE_ENTER("fiber_pool");

    fiber_pool_free_allocations(fiber_pool->allocations);
    ruby_xfree(fiber_pool);

    RUBY_FREE_LEAVE("fiber_pool");
}

static size_t
fiber_pool_memsize(const void *ptr)
{
    const struct fiber_pool * fiber_pool = ptr;
    size_t size = sizeof(*fiber_pool);

    size += fiber_pool->count * fiber_pool->size;

    return size;
}

static const rb_data_type_t FiberPoolDataType = {
    "fiber_pool",
    {NULL, fiber_pool_free, fiber_pool_memsize,},
    0, 0, RUBY_TYPED_FREE_IMMEDIATELY
};

static VALUE
fiber_pool_alloc(VALUE klass)
{
    struct fiber_pool * fiber_pool = RB_ALLOC(struct fiber_pool);

    return TypedData_Wrap_Struct(klass, &FiberPoolDataType, fiber_pool);
}

static VALUE
rb_fiber_pool_initialize(int argc, VALUE* argv, VALUE self)
{
    rb_thread_t *th = GET_THREAD();
    VALUE size = Qnil, count = Qnil, vm_stack_size = Qnil;
    struct fiber_pool * fiber_pool = NULL;

    // Maybe these should be keyword arguments.
    rb_scan_args(argc, argv, "03", &size, &count, &vm_stack_size);

    if (NIL_P(size)) {
        size = INT2NUM(th->vm->default_params.fiber_machine_stack_size);
    }

    if (NIL_P(count)) {
        count = INT2NUM(128);
    }

    if (NIL_P(vm_stack_size)) {
        vm_stack_size = INT2NUM(th->vm->default_params.fiber_vm_stack_size);
    }

    TypedData_Get_Struct(self, struct fiber_pool, &FiberPoolDataType, fiber_pool);

    fiber_pool_initialize(fiber_pool, NUM2SIZET(size), NUM2SIZET(count), NUM2SIZET(vm_stack_size));

    return self;
}
#endif

/*
 *  Document-class: FiberError
 *
 *  Raised when an invalid operation is attempted on a Fiber, in
 *  particular when attempting to call/resume a dead fiber,
 *  attempting to yield from the root fiber, or calling a fiber across
 *  threads.
 *
 *     fiber = Fiber.new{}
 *     fiber.resume #=> nil
 *     fiber.resume #=> FiberError: dead fiber called
 */

/*
 *  Document-class: Fiber::SchedulerInterface
 *
 *  This is not an existing class, but documentation of the interface that Scheduler
 *  object should comply to in order to be used as argument to Fiber.scheduler and handle non-blocking
 *  fibers. See also the "Non-blocking fibers" section in Fiber class docs for explanations
 *  of some concepts.
 *
 *  Scheduler's behavior and usage are expected to be as follows:
 *
 *  * When the execution in the non-blocking Fiber reaches some blocking operation (like
 *    sleep, wait for a process, or a non-ready I/O), it calls some of the scheduler's
 *    hook methods, listed below.
 *  * Scheduler somehow registers what the current fiber is waiting on, and yields control
 *    to other fibers with Fiber.yield (so the fiber would be suspended while expecting its
 *    wait to end, and other fibers in the same thread can perform)
 *  * At the end of the current thread execution, the scheduler's method #close is called
 *  * The scheduler runs into a wait loop, checking all the blocked fibers (which it has
 *    registered on hook calls) and resuming them when the awaited resource is ready
 *    (e.g. I/O ready or sleep time elapsed).
 *
 *  A typical implementation would probably rely for this closing loop on a gem like
 *  EventMachine[https://github.com/eventmachine/eventmachine] or
 *  Async[https://github.com/socketry/async].
 *
 *  This way concurrent execution will be achieved transparently for every
 *  individual Fiber's code.
 *
 *  Hook methods are:
 *
 *  * #io_wait
 *  * #process_wait
 *  * #kernel_sleep
 *  * #block and #unblock
 *  * (the list is expanded as Ruby developers make more methods having non-blocking calls)
 *
 *  When not specified otherwise, the hook implementations are mandatory: if they are not
 *  implemented, the methods trying to call hook will fail. To provide backward compatibility,
 *  in the future hooks will be optional (if they are not implemented, due to the scheduler
 *  being created for the older Ruby version, the code which needs this hook will not fail,
 *  and will just behave in a blocking fashion).
 *
 *  It is also strongly recommended that the scheduler implements the #fiber method, which is
 *  delegated to by Fiber.schedule.
 *
 *  Sample _toy_ implementation of the scheduler can be found in Ruby's code, in
 *  <tt>test/fiber/scheduler.rb</tt>
 *
 */

#if 0 /* for RDoc */
/*
 *
 *  Document-method: Fiber::SchedulerInterface#close
 *
 *  Called when the current thread exits. The scheduler is expected to implement this
 *  method in order to allow all waiting fibers to finalize their execution.
 *
 *  The suggested pattern is to implement the main event loop in the #close method.
 *
 */
static VALUE
rb_fiber_scheduler_interface_close(VALUE self)
{
}

/*
 *  Document-method: SchedulerInterface#process_wait
 *  call-seq: process_wait(pid, flags)
 *
 *  Invoked by Process::Status.wait in order to wait for a specified process.
 *  See that method description for arguments description.
 *
 *  Suggested minimal implementation:
 *
 *      Thread.new do
 *        Process::Status.wait(pid, flags)
 *      end.value
 *
 *  This hook is optional: if it is not present in the current scheduler,
 *  Process::Status.wait will behave as a blocking method.
 *
 *  Expected to return a Process::Status instance.
 */
static VALUE
rb_fiber_scheduler_interface_process_wait(VALUE self)
{
}

/*
 *  Document-method: SchedulerInterface#io_wait
 *  call-seq: io_wait(io, events, timeout)
 *
 *  Invoked by IO#wait, IO#wait_readable, IO#wait_writable to ask whether the
 *  specified descriptor is ready for specified events within
 *  the specified +timeout+.
 *
 *  +events+ is a bit mask of <tt>IO::READABLE</tt>, <tt>IO::WRITABLE</tt>, and
 *  <tt>IO::PRIORITY</tt>.
 *
 *  Suggested implementation should register which Fiber is waiting for which
 *  resources and immediately calling Fiber.yield to pass control to other
 *  fibers. Then, in the #close method, the scheduler might dispatch all the
 *  I/O resources to fibers waiting for it.
 *
 *  Expected to return the subset of events that are ready immediately.
 *
 */
static VALUE
rb_fiber_scheduler_interface_io_wait(VALUE self)
{
}

/*
 *  Document-method: SchedulerInterface#kernel_sleep
 *  call-seq: kernel_sleep(duration = nil)
 *
 *  Invoked by Kernel#sleep and Mutex#sleep and is expected to provide
 *  an implementation of sleeping in a non-blocking way. Implementation might
 *  register the current fiber in some list of "which fiber wait until what
 *  moment", call Fiber.yield to pass control, and then in #close resume
 *  the fibers whose wait period has elapsed.
 *
 */
static VALUE
rb_fiber_scheduler_interface_kernel_sleep(VALUE self)
{
}

/*
 *  Document-method: SchedulerInterface#block
 *  call-seq: block(blocker, timeout = nil)
 *
 *  Invoked by methods like Thread.join, and by Mutex, to signify that current
 *  Fiber is blocked until further notice (e.g. #unblock) or until +timeout+ has
 *  elapsed.
 *
 *  +blocker+ is what we are waiting on, informational only (for debugging and
 *  logging). There are no guarantee about its value.
 *
 *  Expected to return boolean, specifying whether the blocking operation was
 *  successful or not.
 */
static VALUE
rb_fiber_scheduler_interface_block(VALUE self)
{
}

/*
 *  Document-method: SchedulerInterface#unblock
 *  call-seq: unblock(blocker, fiber)
 *
 *  Invoked to wake up Fiber previously blocked with #block (for example, Mutex#lock
 *  calls #block and Mutex#unlock calls #unblock). The scheduler should use
 *  the +fiber+ parameter to understand which fiber is unblocked.
 *
 *  +blocker+ is what was awaited for, but it is informational only (for debugging
 *  and logging), and it is not guaranteed to be the same value as the +blocker+ for
 *  #block.
 *
 */
static VALUE
rb_fiber_scheduler_interface_unblock(VALUE self)
{
}

/*
 *  Document-method: SchedulerInterface#fiber
 *  call-seq: fiber(&block)
 *
 *  Implementation of the Fiber.schedule. The method is <em>expected</em> to immediately
 *  run the given block of code in a separate non-blocking fiber, and to return that Fiber.
 *
 *  Minimal suggested implementation is:
 *
 *     def fiber(&block)
 *       fiber = Fiber.new(blocking: false, &block)
 *       fiber.resume
 *       fiber
 *     end
 */
static VALUE
rb_fiber_scheduler_interface_fiber(VALUE self)
{
}
#endif

void
Init_Cont(void)
{
    rb_thread_t *th = GET_THREAD();
    size_t vm_stack_size = th->vm->default_params.fiber_vm_stack_size;
    size_t machine_stack_size = th->vm->default_params.fiber_machine_stack_size;
    size_t stack_size = machine_stack_size + vm_stack_size;

#ifdef _WIN32
    SYSTEM_INFO info;
    GetSystemInfo(&info);
    pagesize = info.dwPageSize;
#else /* not WIN32 */
    pagesize = sysconf(_SC_PAGESIZE);
#endif
    SET_MACHINE_STACK_END(&th->ec->machine.stack_end);

    fiber_pool_initialize(&shared_fiber_pool, stack_size, FIBER_POOL_INITIAL_SIZE, vm_stack_size);

    fiber_initialize_keywords[0] = rb_intern_const("blocking");
    fiber_initialize_keywords[1] = rb_intern_const("pool");

    const char *fiber_shared_fiber_pool_free_stacks = getenv("RUBY_SHARED_FIBER_POOL_FREE_STACKS");
    if (fiber_shared_fiber_pool_free_stacks) {
        shared_fiber_pool.free_stacks = atoi(fiber_shared_fiber_pool_free_stacks);
    }

    rb_cFiber = rb_define_class("Fiber", rb_cObject);
    rb_define_alloc_func(rb_cFiber, fiber_alloc);
    rb_eFiberError = rb_define_class("FiberError", rb_eStandardError);
    rb_define_singleton_method(rb_cFiber, "yield", rb_fiber_s_yield, -1);
    rb_define_singleton_method(rb_cFiber, "current", rb_fiber_s_current, 0);
    rb_define_method(rb_cFiber, "initialize", rb_fiber_initialize, -1);
    rb_define_method(rb_cFiber, "blocking?", rb_fiber_blocking_p, 0);
    rb_define_method(rb_cFiber, "resume", rb_fiber_m_resume, -1);
    rb_define_method(rb_cFiber, "raise", rb_fiber_raise, -1);
    rb_define_method(rb_cFiber, "backtrace", rb_fiber_backtrace, -1);
    rb_define_method(rb_cFiber, "backtrace_locations", rb_fiber_backtrace_locations, -1);
    rb_define_method(rb_cFiber, "to_s", fiber_to_s, 0);
    rb_define_alias(rb_cFiber, "inspect", "to_s");
    rb_define_method(rb_cFiber, "transfer", rb_fiber_m_transfer, -1);
    rb_define_method(rb_cFiber, "alive?", rb_fiber_alive_p, 0);

    rb_define_singleton_method(rb_cFiber, "blocking?", rb_fiber_s_blocking_p, 0);
    rb_define_singleton_method(rb_cFiber, "scheduler", rb_fiber_s_scheduler, 0);
    rb_define_singleton_method(rb_cFiber, "set_scheduler", rb_fiber_set_scheduler, 1);
    rb_define_singleton_method(rb_cFiber, "current_scheduler", rb_fiber_current_scheduler, 0);

    rb_define_singleton_method(rb_cFiber, "schedule", rb_fiber_s_schedule, -1);

#if 0 /* for RDoc */
    rb_cFiberScheduler = rb_define_class_under(rb_cFiber, "SchedulerInterface", rb_cObject);
    rb_define_method(rb_cFiberScheduler, "close", rb_fiber_scheduler_interface_close, 0);
    rb_define_method(rb_cFiberScheduler, "process_wait", rb_fiber_scheduler_interface_process_wait, 0);
    rb_define_method(rb_cFiberScheduler, "io_wait", rb_fiber_scheduler_interface_io_wait, 0);
    rb_define_method(rb_cFiberScheduler, "kernel_sleep", rb_fiber_scheduler_interface_kernel_sleep, 0);
    rb_define_method(rb_cFiberScheduler, "block", rb_fiber_scheduler_interface_block, 0);
    rb_define_method(rb_cFiberScheduler, "unblock", rb_fiber_scheduler_interface_unblock, 0);
    rb_define_method(rb_cFiberScheduler, "fiber", rb_fiber_scheduler_interface_fiber, 0);
#endif

#ifdef RB_EXPERIMENTAL_FIBER_POOL
    rb_cFiberPool = rb_define_class("Pool", rb_cFiber);
    rb_define_alloc_func(rb_cFiberPool, fiber_pool_alloc);
    rb_define_method(rb_cFiberPool, "initialize", rb_fiber_pool_initialize, -1);
#endif

    rb_provide("fiber.so");
}

RUBY_SYMBOL_EXPORT_BEGIN

void
ruby_Init_Continuation_body(void)
{
    rb_cContinuation = rb_define_class("Continuation", rb_cObject);
    rb_undef_alloc_func(rb_cContinuation);
    rb_undef_method(CLASS_OF(rb_cContinuation), "new");
    rb_define_method(rb_cContinuation, "call", rb_cont_call, -1);
    rb_define_method(rb_cContinuation, "[]", rb_cont_call, -1);
    rb_define_global_function("callcc", rb_callcc, 0);
}

RUBY_SYMBOL_EXPORT_END