File: threads.c

package info (click to toggle)
form 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 8,312 kB
  • sloc: ansic: 110,546; cpp: 20,395; sh: 5,874; makefile: 545
file content (5171 lines) | stat: -rw-r--r-- 145,713 bytes parent folder | download
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
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
/** @file threads.c
 * 
 *  Routines for the interface of FORM with the pthreads library
 *
 *	This is the main part of the parallelization of TFORM.
 *	It is important to also look in the files structs.h and variable.h
 *	because the treatment of the A and B structs is essential (these
 *	structs are used by means of the macros AM, AP, AC, AS, AR, AT, AN,
 *	AO and AX). Also the definitions and use of the macros BHEAD and PHEAD
 *	should be looked up.
 *
 *	The sources are set up in such a way that if WITHPTHREADS isn't defined
 *	there is no trace of pthread parallelization.
 *	The reason is that TFORM is far more memory hungry than sequential FORM.
 *
 *	Special attention should also go to the locks. The proper use of the
 *	locks is essential and determines whether TFORM can work at all.
 *	We use the LOCK/UNLOCK macros which are empty in the case of sequential FORM
 *	These locks are at many places in the source files when workers can
 *	interfere with each others data or with the data of the master.
 */
/* #[ License : */
/*
 *   Copyright (C) 1984-2026 J.A.M. Vermaseren
 *   When using this file you are requested to refer to the publication
 *   J.A.M.Vermaseren "New features of FORM" math-ph/0010025
 *   This is considered a matter of courtesy as the development was paid
 *   for by FOM the Dutch physics granting agency and we would like to
 *   be able to track its scientific use to convince FOM of its value
 *   for the community.
 *
 *   This file is part of FORM.
 *
 *   FORM is free software: you can redistribute it and/or modify it under the
 *   terms of the GNU General Public License as published by the Free Software
 *   Foundation, either version 3 of the License, or (at your option) any later
 *   version.
 *
 *   FORM is distributed in the hope that it will be useful, but WITHOUT ANY
 *   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 *   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 *   details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with FORM.  If not, see <http://www.gnu.org/licenses/>.
 */
/* #] License : */ 
 
#ifdef WITHPTHREADS

#define WHOLEBRACKETS
/*
  	#[ Variables :

	The sortbot additions are from 17-may-2007 and after. They constitute
	an attempt to make the final merge sorting faster for the master.
	This way the master has only one compare per term.
	It does add some complexity, but the final merge routine (MasterMerge)
	is much simpler for the sortbots. On the other hand the original merging is
	for a large part a copy of the MergePatches routine in sort.c and hence
	even though complex the bad part has been thoroughly debugged.
*/

#include "form3.h"

#ifdef WITH_ALARM
// This is only required if we are blocking SIG_ALRM in the worker threads.
#include <signal.h>
#endif

#ifdef WITHFLOAT
#include <gmp.h>

int PackFloat(WORD *,mpf_t);
int UnpackFloat(mpf_t, WORD *);
void RatToFloat(mpf_t result, UWORD *formrat, int ratsize);
#endif
 
static int numberofthreads;
static int numberofworkers;
static int identityofthreads = 0;
static int *listofavailables;
static int topofavailables = 0;
static pthread_key_t identitykey;
static INILOCK(numberofthreadslock)
static INILOCK(availabilitylock)
static pthread_t *threadpointers = 0;
static pthread_mutex_t *wakeuplocks;
static pthread_mutex_t *wakeupmasterthreadlocks;
static pthread_cond_t *wakeupconditions;
static pthread_condattr_t *wakeupconditionattributes;
static int *wakeup;
static int *wakeupmasterthread;
static INILOCK(wakeupmasterlock)
static pthread_cond_t wakeupmasterconditions = PTHREAD_COND_INITIALIZER;
static pthread_cond_t *wakeupmasterthreadconditions;
static int wakeupmaster = 0;
static int identityretval;
/* static INILOCK(clearclocklock) */
static LONG *timerinfo;
static LONG *sumtimerinfo;
static int numberclaimed;

static THREADBUCKET **threadbuckets, **freebuckets;
static int numthreadbuckets;
static int numberoffullbuckets;

/* static int numberbusy = 0; */

INILOCK(dummylock)
INIRWLOCK(dummyrwlock)
static pthread_cond_t dummywakeupcondition = PTHREAD_COND_INITIALIZER;

#ifdef WITHSORTBOTS
static POSITION SortBotPosition;
static int numberofsortbots;
static INILOCK(wakeupsortbotlock)
static pthread_cond_t wakeupsortbotconditions = PTHREAD_COND_INITIALIZER;
static int topsortbotavailables = 0;
static LONG numberofterms;
#endif

/*
  	#] Variables : 
  	#[ Identity :
 		#[ StartIdentity :
*/
/**
 *	To be called once when we start up the threads.
 *	Starts our identity administration.
 */

void StartIdentity(void)
{
	pthread_key_create(&identitykey,FinishIdentity);
}

/*
 		#] StartIdentity : 
 		#[ FinishIdentity :
*/
/**
 *	The library needs a finishing routine
 */

void FinishIdentity(void *keyp)
{
	DUMMYUSE(keyp);
/*	free(keyp); */
}

/*
 		#] FinishIdentity : 
 		#[ SetIdentity :
*/
/**
 *	Assigns an integer value to a thread, starting at zero.
 */

int SetIdentity(int *identityretval)
{
/*
#ifdef _MSC_VER
	printf("addr %d\n",&numberofthreadslock);
	printf("size %d\n",sizeof(numberofthreadslock));
#endif
*/
	LOCK(numberofthreadslock);
	*identityretval = identityofthreads++;
	UNLOCK(numberofthreadslock);
	pthread_setspecific(identitykey,(void *)identityretval);
	return(*identityretval);
}

/*
 		#] SetIdentity : 
 		#[ WhoAmI :
*/

/**
 *	Returns the number of the current thread in our administration
 *
 *	This routine is to be called in routines that need access to the thread
 *	specific data and that don't get their B-struct passed as an argument.
 *	Routines that get called frequently need their B-struct passed.
 *	This is done with BHEAD and the argumentfield gets declared with
 *	one of the BARG macros rather than the ARG macros.
 */

int WhoAmI(void)
{
	int *identity;
/*
	First a fast exit for when there is at most one thread
*/
	if ( identityofthreads <= 1 ) return(0);
/*
	Now the reading of the key.
	According to the book the statement should read:

	pthread_getspecific(identitykey,(void **)(&identity));

	but according to the information in pthread.h it is:
*/
	identity = (int *)pthread_getspecific(identitykey);
	return(*identity);
}

/*
 		#] WhoAmI : 
 		#[ BeginIdentities :
*/
/**
 *	Starts up the identity registration. This is the routine to be called
 *	at the startup of TFORM.
 */

void BeginIdentities(void)
{
	StartIdentity();
	SetIdentity(&identityretval);
}

/*
 		#] BeginIdentities : 
  	#] Identity : 
  	#[ StartHandleLock :
*/
/**
 *	Routine to be called at the startup of TFORM.
 *	We have this routine because we would like to keep all access to TFORM
 *	specific data in this file.
 */

void StartHandleLock(void)
{
	AM.handlelock = dummyrwlock;
}

/*
  	#] StartHandleLock : 
  	#[ StartAllThreads :
*/
/**
 *	In this routine we start 'number' threads.
 *	The routine that runs the show for each worker is called RunThread.
 *	It will call the allocations and all the worker specific action.
 *	Then the master has to wait till all workers are asleep before continuing.
 *	If we use SortBots (special threads to help the master during the
 *	final stages of a big sort) they are started and their routine is
 *	called RunSortBot.
 *	The master then waits till all sortbots are asleep before continuing.
 *	Finally the sort buffers of the master are parcelled up for the final
 *	merge in big sorts in which the workers have to feed the master.
 *
 *	@param number The number of main threads (including the master)
 *	              The number of workers is number-1.
 *	@return  Standard return conventions (OK -> 0)
 */

int StartAllThreads(int number)
{
	int identity, j, dummy, mul;
	ALLPRIVATES *B;
	pthread_t thethread;
	identity = WhoAmI();

#ifdef WITHSORTBOTS
	timerinfo = (LONG *)Malloc1(sizeof(LONG)*number*2,"timerinfo");
	sumtimerinfo = (LONG *)Malloc1(sizeof(LONG)*number*2,"sumtimerinfo");
	for ( j = 0; j < number*2; j++ ) { timerinfo[j] = 0; sumtimerinfo[j] = 0; }
	mul = 2;
#else
	timerinfo = (LONG *)Malloc1(sizeof(LONG)*number,"timerinfo");
	sumtimerinfo = (LONG *)Malloc1(sizeof(LONG)*number,"sumtimerinfo");
	for ( j = 0; j < number; j++ ) { timerinfo[j] = 0; sumtimerinfo[j] = 0; }
	mul = 1;
#endif
 
	listofavailables = (int *)Malloc1(sizeof(int)*(number+1),"listofavailables");
	threadpointers = (pthread_t *)Malloc1(sizeof(pthread_t)*number*mul,"threadpointers");
	AB = (ALLPRIVATES **)Malloc1(sizeof(ALLPRIVATES *)*number*mul,"Private structs");

	wakeup = (int *)Malloc1(sizeof(int)*number*mul,"wakeup");
	wakeuplocks = (pthread_mutex_t *)Malloc1(sizeof(pthread_mutex_t)*number*mul,"wakeuplocks");
	wakeupconditions = (pthread_cond_t *)Malloc1(sizeof(pthread_cond_t)*number*mul,"wakeupconditions");
	wakeupconditionattributes = (pthread_condattr_t *)
			Malloc1(sizeof(pthread_condattr_t)*number*mul,"wakeupconditionattributes");

	wakeupmasterthread = (int *)Malloc1(sizeof(int)*number*mul,"wakeupmasterthread");
	wakeupmasterthreadlocks = (pthread_mutex_t *)Malloc1(sizeof(pthread_mutex_t)*number*mul,"wakeupmasterthreadlocks");
	wakeupmasterthreadconditions = (pthread_cond_t *)Malloc1(sizeof(pthread_cond_t)*number*mul,"wakeupmasterthread");

	numberofthreads = number;
	numberofworkers = number - 1;
	threadpointers[identity] = pthread_self();
	topofavailables = 0;

#ifdef WITH_ALARM
	/* During thread creation, we block SIGALRM on the main thread. The created
	   threads will inherit this. This is required for #timeout to work properly
	   in TFORM: only the main thread should recieve SIGALRM. */
	sigset_t sig_set;
	sigemptyset(&sig_set);
	sigaddset(&sig_set, SIGALRM);
	pthread_sigmask(SIG_BLOCK, &sig_set, NULL);
#endif

	for ( j = 1; j < number; j++ ) {
		if ( pthread_create(&thethread,NULL,RunThread,(void *)(&dummy)) )
			goto failure;
	}
/*
	Now we initialize the master at the same time that the workers are doing so.
*/
	B = InitializeOneThread(identity);
	AR.infile = &(AR.Fscr[0]);
	AR.outfile = &(AR.Fscr[1]);
	AR.hidefile = &(AR.Fscr[2]);
	AM.sbuflock = dummylock;
	AS.inputslock = dummylock;
	AS.outputslock = dummylock;
	AS.MaxExprSizeLock = dummylock;
	AP.PreVarLock = dummylock;
	AC.halfmodlock = dummylock;
	MakeThreadBuckets(number,0);
/*
	Now we wait for the workers to finish their startup.
	We don't want to initialize the sortbots yet and run the risk that 
	some of them may end up with a lower number than one of the workers.
*/
	MasterWaitAll();
#ifdef WITHSORTBOTS
	if ( numberofworkers > 2 ) {
		numberofsortbots = numberofworkers-2;
		for ( j = numberofworkers+1; j < 2*numberofworkers-1; j++ ) {
			if ( pthread_create(&thethread,NULL,RunSortBot,(void *)(&dummy)) )
				goto failure;
		}
	}
	else {
		numberofsortbots = 0;
	}
	MasterWaitAllSortBots();
	DefineSortBotTree();
#endif
	IniSortBlocks(number-1);
	AS.MasterSort = 0;
	AM.storefilelock = dummylock;

#ifdef WITH_ALARM
	/* Now we allow the main thread to recieve SIGALRM again. */
	pthread_sigmask(SIG_UNBLOCK, &sig_set, NULL);
#endif

/*
MesPrint("AB = %x %x %x  %d",AB[0],AB[1],AB[2], identityofthreads);
*/
	return(0);
failure:
	MesPrint("Cannot start %d threads",number);
	Terminate(-1);
	return(-1);
}

/*
  	#] StartAllThreads : 
  	#[ InitializeOneThread :
*/
/**
 *	Array for putting a label on memory allocations and error messages.
 */
UBYTE *scratchname[] = { (UBYTE *)"scratchsize",
                         (UBYTE *)"scratchsize",
                         (UBYTE *)"hidesize" };
/**
 *	Initializes one thread. This includes the allocation of its private
 *	space and all its buffers. Also the initialization of variables.
 *
 *	@param identity The (TFORM defined) integer identifier of the thread.
 *	@return A pointer to the struct with all private data of the thread.
 *	We call this struct B and we have a system of macros
 *	(defined in variable.h) that allows us to access its substructs in
 *	the same way as the corresponding substructs in sequential FORM are
 *	accessed. Example:
 *		In TFORM  AR is defined as B->R
 *		In FORM   AR is defined as A.R  (here it is part of the A struct)
 *
 *	One complication:
 *		AM.ScratSize can be rather big. We don't want all the workers
 *		to have an allocation of that size. Some computers may run out
 *		of allocations.
 *		We need on the workers:
 *			AR.Fscr[0] : input for keep brackets and expressions in rhs
 *			AR.Fscr[1] : output of the sorting to be fed to the master
 *			AR.Fscr[2] : input for keep brackets and expressions in rhs
 *		Hence the 0 and 2 channels can use a rather small buffer like
 *			10*AM.MaxTer.
 *		The 1 channel needs a buffer roughly AM.ScratSize/#ofworkers.
 */

ALLPRIVATES *InitializeOneThread(int identity)
{
	WORD *t, *ScratchBuf;
	int i, j, bsize, *bp;
	LONG ScratchSize[3], IOsize;
	ALLPRIVATES *B;
	UBYTE *s;

	wakeup[identity] = 0;
	wakeuplocks[identity] = dummylock;
	pthread_condattr_init(&(wakeupconditionattributes[identity]));
	pthread_condattr_setpshared(&(wakeupconditionattributes[identity]),PTHREAD_PROCESS_PRIVATE);
	wakeupconditions[identity] = dummywakeupcondition;
	pthread_cond_init(&(wakeupconditions[identity]),&(wakeupconditionattributes[identity]));
	wakeupmasterthread[identity] = 0;
	wakeupmasterthreadlocks[identity] = dummylock;
	wakeupmasterthreadconditions[identity] = dummywakeupcondition;

	bsize = sizeof(ALLPRIVATES);
	bsize = (bsize+sizeof(int)-1)/sizeof(int);
	B = (ALLPRIVATES *)Malloc1(sizeof(int)*bsize,"B struct");
	for ( bp = (int *)B, j = 0; j < bsize; j++ ) *bp++ = 0;

	AB[identity] = B;
/*
			12-jun-2007 JV:
	For the timing one has to know a few things:
	The POSIX standard is that there is only a single process ID and that
	getrusage returns the time of all the threads together.
	Under Linux there are two methods though: The older LinuxThreads and NPTL.
	LinuxThreads gives each thread its own process id. This makes that we
	can time the threads with getrusage, and hence this was done. Under NPTL
	this has been 'corrected' and suddenly getruage doesn't work anymore the
	way it used to. Now we need
		clock_gettime(CLOCK_THREAD_CPUTIME_ID,&timing)
	which is declared in <time.h> and we need -lrt extra in the link statement.
	(this is at least the case on blade02 at DESY-Zeuthen).
	See also the code in tools.c at the routine Timer.
	We may still have to include more stuff there.
*/
	if ( identity > 0 ) TimeCPU(0);

#ifdef WITHSORTBOTS

	if ( identity > numberofworkers ) {
/*
		Some workspace is needed when we have a PolyFun and we have to add
		two terms and the new result is going to be longer than the old result.
*/
		LONG length = AM.WorkSize*sizeof(WORD)/8+AM.MaxTer*2;
		AT.WorkSpace = (WORD *)Malloc1(length,"WorkSpace");
		AT.WorkTop = AT.WorkSpace + length/sizeof(WORD);
		AT.WorkPointer = AT.WorkSpace;
		AT.identity = identity;
/*
		The SB struct gets treated in IniSortBlocks.
		The SortBotIn variables will be defined DefineSortBotTree.
*/
		if ( AN.SoScratC == 0 ) {
			AN.SoScratC = (UWORD *)Malloc1(2*(AM.MaxTal+2)*sizeof(UWORD),"Scratch in SortBot");
		}
		AT.SS = (SORTING *)Malloc1(sizeof(SORTING),"dummy sort buffer");
		AT.SS->PolyFlag = 0;

		AT.comsym[0] = 8;
		AT.comsym[1] = SYMBOL;
		AT.comsym[2] = 4;
		AT.comsym[3] = 0;
		AT.comsym[4] = 1;
		AT.comsym[5] = 1;
		AT.comsym[6] = 1;
		AT.comsym[7] = 3;
		AT.comnum[0] = 4;
		AT.comnum[1] = 1;
		AT.comnum[2] = 1;
		AT.comnum[3] = 3;
		AT.comfun[0] = FUNHEAD+4;
		AT.comfun[1] = FUNCTION;
		AT.comfun[2] = FUNHEAD;
		AT.comfun[3] = 0;
#if FUNHEAD > 3
		for ( i = 4; i <= FUNHEAD; i++ )
			AT.comfun[i] = 0;
#endif
		AT.comfun[FUNHEAD+1] = 1;
		AT.comfun[FUNHEAD+2] = 1;
		AT.comfun[FUNHEAD+3] = 3;
		AT.comind[0] = 7;
		AT.comind[1] = INDEX;
		AT.comind[2] = 3;
		AT.comind[3] = 0;
		AT.comind[4] = 1;
		AT.comind[5] = 1;
		AT.comind[6] = 3;

		AT.inprimelist = -1;
		AT.sizeprimelist = 0;
		AT.primelist = 0;
		AT.bracketinfo = 0;
		
		AR.CompareRoutine = (COMPAREDUMMY)(&Compare1);

		AR.sLevel = 0;
		AR.wranfia = 0;
		AR.wranfcall = 0;
		AR.wranfnpair1 = NPAIR1;
		AR.wranfnpair2 = NPAIR2;
		AN.NumFunSorts = 5;
		AN.MaxFunSorts = 5;
		AN.SplitScratch = 0;
		AN.SplitScratchSize = AN.InScratch = 0;
		AN.SplitScratch1 = 0;
		AN.SplitScratchSize1 = AN.InScratch1 = 0;

		AN.FunSorts = (SORTING **)Malloc1((AN.NumFunSorts+1)*sizeof(SORTING *),"FunSort pointers");
		for ( i = 0; i <= AN.NumFunSorts; i++ ) AN.FunSorts[i] = 0;
		AN.FunSorts[0] = AT.S0 = AT.SS;
		AN.idfunctionflag = 0;
		AN.tryterm = 0;

		return(B);
	}
	if ( identity == 0 && AN.SoScratC == 0 ) {
		AN.SoScratC = (UWORD *)Malloc1(2*(AM.MaxTal+2)*sizeof(UWORD),"Scratch in SortBot");
	}
#endif
	AR.CurDum = AM.IndDum;
	for ( j = 0; j < 3; j++ ) {
		if ( identity == 0 ) {
			if ( j == 2 ) {
				ScratchSize[j] = AM.HideSize;
			}
			else {
				ScratchSize[j] = AM.ScratSize;
			}
			if ( ScratchSize[j] < 10*AM.MaxTer ) ScratchSize[j] = 10 * AM.MaxTer;
		}
		else {
/*
			ScratchSize[j] = AM.ScratSize / (numberofthreads-1);
			ScratchSize[j] = ScratchSize[j] / 20;
			if ( ScratchSize[j] < 10*AM.MaxTer ) ScratchSize[j] = 10 * AM.MaxTer;
*/
			if ( j == 1 ) ScratchSize[j] = AM.ThreadScratOutSize;
			else          ScratchSize[j] = AM.ThreadScratSize;
			if ( ScratchSize[j] < 4*AM.MaxTer ) ScratchSize[j] = 4 * AM.MaxTer;
			AR.Fscr[j].name = 0;
		}
		ScratchSize[j] = ( ScratchSize[j] + 255 ) / 256;
		ScratchSize[j] = ScratchSize[j] * 256;
		ScratchBuf = (WORD *)Malloc1(ScratchSize[j]*sizeof(WORD),(char *)(scratchname[j]));
		AR.Fscr[j].POsize = ScratchSize[j] * sizeof(WORD);
		AR.Fscr[j].POfull = AR.Fscr[j].POfill = AR.Fscr[j].PObuffer = ScratchBuf;
		AR.Fscr[j].POstop = AR.Fscr[j].PObuffer + ScratchSize[j];
		PUTZERO(AR.Fscr[j].POposition);
		AR.Fscr[j].pthreadslock = dummylock;
		AR.Fscr[j].wPOsize = AR.Fscr[j].POsize;
		AR.Fscr[j].wPObuffer = AR.Fscr[j].PObuffer;
		AR.Fscr[j].wPOfill = AR.Fscr[j].POfill;
		AR.Fscr[j].wPOfull = AR.Fscr[j].POfull;
		AR.Fscr[j].wPOstop = AR.Fscr[j].POstop;
	}
	AR.InInBuf = 0;
	AR.InHiBuf = 0;
	AR.Fscr[0].handle = -1;
	AR.Fscr[1].handle = -1;
	AR.Fscr[2].handle = -1;
	AR.FoStage4[0].handle = -1;
	AR.FoStage4[1].handle = -1;
	IOsize = AM.S0->file.POsize;
#ifdef WITHZLIB
	AR.FoStage4[0].ziosize = IOsize;
	AR.FoStage4[1].ziosize = IOsize;
	AR.FoStage4[0].ziobuffer = 0;
	AR.FoStage4[1].ziobuffer = 0;
#endif	
	AR.FoStage4[0].POsize  = ((IOsize+sizeof(WORD)-1)/sizeof(WORD))*sizeof(WORD);
	AR.FoStage4[1].POsize  = ((IOsize+sizeof(WORD)-1)/sizeof(WORD))*sizeof(WORD);

	AR.hidefile = &(AR.Fscr[2]);
	AR.StoreData.Handle = -1;
	AR.SortType = AC.SortType;

	AN.IndDum = AM.IndDum;

	if ( identity > 0 ) {
		s = (UBYTE *)(FG.fname); i = 0;
		while ( *s ) { s++; i++; }
		s = (UBYTE *)Malloc1(sizeof(char)*(i+12),"name for Fscr[0] file");
		snprintf((char *)s,i+12,"%s.%d",FG.fname,identity);
		s[i-3] = 's'; s[i-2] = 'c'; s[i-1] = '0';
		AR.Fscr[0].name = (char *)s;
		s = (UBYTE *)(FG.fname); i = 0;
		while ( *s ) { s++; i++; }
		s = (UBYTE *)Malloc1(sizeof(char)*(i+12),"name for Fscr[1] file");
		snprintf((char *)s,i+12,"%s.%d",FG.fname,identity);
		s[i-3] = 's'; s[i-2] = 'c'; s[i-1] = '1';
		AR.Fscr[1].name = (char *)s;
	}

	AR.CompressBuffer = (WORD *)Malloc1((AM.CompressSize+10)*sizeof(WORD),"compresssize");
	AR.ComprTop = AR.CompressBuffer + AM.CompressSize;
	AR.CompareRoutine = (COMPAREDUMMY)(&Compare1);
/*
	Here we make all allocations for the struct AT
	(which is AB[identity].T or B->T with B = AB+identity).
*/
	AT.WorkSpace = (WORD *)Malloc1(AM.WorkSize*sizeof(WORD),"WorkSpace");
	AT.WorkTop = AT.WorkSpace + AM.WorkSize;
	AT.WorkPointer = AT.WorkSpace;

	AT.Nest = (NESTING)Malloc1((LONG)sizeof(struct NeStInG)*AM.maxFlevels,"functionlevels");
	AT.NestStop = AT.Nest + AM.maxFlevels;
	AT.NestPoin = AT.Nest;

	AT.WildMask = (WORD *)Malloc1((LONG)AM.MaxWildcards*sizeof(WORD),"maxwildcards");

	LOCK(availabilitylock);
	AT.ebufnum = inicbufs();		/* Buffer for extras during execution */
	AT.fbufnum = inicbufs();		/* Buffer for caching in factorization */
	AT.allbufnum = inicbufs();		/* Buffer for id,all */
	AT.aebufnum = inicbufs();		/* Buffer for id,all */
	UNLOCK(availabilitylock);

	AT.RepCount = (int *)Malloc1((LONG)((AM.RepMax+3)*sizeof(int)),"repeat buffers");
	AN.RepPoint = AT.RepCount;
	AN.polysortflag = 0;
	AN.subsubveto = 0;
	AN.tryterm = 0;
	AT.RepTop = AT.RepCount + AM.RepMax;

	AT.WildArgTaken = (WORD *)Malloc1((LONG)AC.WildcardBufferSize*sizeof(WORD)/2
				,"argument list names");
	AT.WildcardBufferSize = AC.WildcardBufferSize;
	AT.previousEfactor = 0;

	AT.identity = identity;
	AT.LoadBalancing = 0;
/*
	Still to do: the SS stuff.
	             the Fscr[3]
	             the FoStage4[2]
*/
	if ( AT.WorkSpace == 0 ||
	     AT.Nest == 0 ||
	     AT.WildMask == 0 ||
	     AT.RepCount == 0 ||
	     AT.WildArgTaken == 0 ) goto OnError;
/*
	And initializations
*/
	AT.comsym[0] = 8;
	AT.comsym[1] = SYMBOL;
	AT.comsym[2] = 4;
	AT.comsym[3] = 0;
	AT.comsym[4] = 1;
	AT.comsym[5] = 1;
	AT.comsym[6] = 1;
	AT.comsym[7] = 3;
	AT.comnum[0] = 4;
	AT.comnum[1] = 1;
	AT.comnum[2] = 1;
	AT.comnum[3] = 3;
	AT.comfun[0] = FUNHEAD+4;
	AT.comfun[1] = FUNCTION;
	AT.comfun[2] = FUNHEAD;
	AT.comfun[3] = 0;
#if FUNHEAD > 3
	for ( i = 4; i <= FUNHEAD; i++ )
		AT.comfun[i] = 0;
#endif
	AT.comfun[FUNHEAD+1] = 1;
	AT.comfun[FUNHEAD+2] = 1;
	AT.comfun[FUNHEAD+3] = 3;
	AT.comind[0] = 7;
	AT.comind[1] = INDEX;
	AT.comind[2] = 3;
	AT.comind[3] = 0;
	AT.comind[4] = 1;
	AT.comind[5] = 1;
	AT.comind[6] = 3;
	AT.locwildvalue[0] = SUBEXPRESSION;
	AT.locwildvalue[1] = SUBEXPSIZE;
	for ( i = 2; i < SUBEXPSIZE; i++ ) AT.locwildvalue[i] = 0;
	AT.mulpat[0] = TYPEMULT;
	AT.mulpat[1] = SUBEXPSIZE+3;
	AT.mulpat[2] = 0;
	AT.mulpat[3] = SUBEXPRESSION;
	AT.mulpat[4] = SUBEXPSIZE;
	AT.mulpat[5] = 0;
	AT.mulpat[6] = 1;
	for ( i = 7; i < SUBEXPSIZE+5; i++ ) AT.mulpat[i] = 0;
	AT.proexp[0] = SUBEXPSIZE+4;
	AT.proexp[1] = EXPRESSION;
	AT.proexp[2] = SUBEXPSIZE;
	AT.proexp[3] = -1;
	AT.proexp[4] = 1;
	for ( i = 5; i < SUBEXPSIZE+1; i++ ) AT.proexp[i] = 0;
	AT.proexp[SUBEXPSIZE+1] = 1;
	AT.proexp[SUBEXPSIZE+2] = 1;
	AT.proexp[SUBEXPSIZE+3] = 3;
	AT.proexp[SUBEXPSIZE+4] = 0;
	AT.dummysubexp[0] = SUBEXPRESSION;
	AT.dummysubexp[1] = SUBEXPSIZE+4;
	for ( i = 2; i < SUBEXPSIZE; i++ ) AT.dummysubexp[i] = 0;
	AT.dummysubexp[SUBEXPSIZE] = WILDDUMMY;
	AT.dummysubexp[SUBEXPSIZE+1] = 4;
	AT.dummysubexp[SUBEXPSIZE+2] = 0;
	AT.dummysubexp[SUBEXPSIZE+3] = 0;

	AT.MinVecArg[0] = 7+ARGHEAD;
	AT.MinVecArg[ARGHEAD] = 7;
	AT.MinVecArg[1+ARGHEAD] = INDEX;
	AT.MinVecArg[2+ARGHEAD] = 3;
	AT.MinVecArg[3+ARGHEAD] = 0;
	AT.MinVecArg[4+ARGHEAD] = 1;
	AT.MinVecArg[5+ARGHEAD] = 1;
	AT.MinVecArg[6+ARGHEAD] = -3;
	t = AT.FunArg;
	*t++ = 4+ARGHEAD+FUNHEAD;
	for ( i = 1; i < ARGHEAD; i++ ) *t++ = 0;
	*t++ = 4+FUNHEAD;
	*t++ = 0;
	*t++ = FUNHEAD;
	for ( i = 2; i < FUNHEAD; i++ ) *t++ = 0;
	*t++ = 1; *t++ = 1; *t++ = 3;

	AT.inprimelist = -1;
	AT.sizeprimelist = 0;
	AT.primelist = 0;
	AT.nfac = AT.nBer = 0;
	AT.factorials = 0;
	AT.bernoullis = 0;
	AR.wranfia = 0;
	AR.wranfcall = 0;
	AR.wranfnpair1 = NPAIR1;
	AR.wranfnpair2 = NPAIR2;
	AR.wranfseed = 0;
	AN.SplitScratch = 0;
	AN.SplitScratchSize = AN.InScratch = 0;
	AN.SplitScratch1 = 0;
	AN.SplitScratchSize1 = AN.InScratch1 = 0;
/*
	Now the sort buffers. They depend on which thread. The master
	inherits the sortbuffer from AM.S0
*/
	if ( identity == 0 ) {
		AT.S0 = AM.S0;
	}
	else {
/*
		For the moment we don't have special settings.
		They may become costly in virtual memory.
*/
		AT.S0 = AllocSort(AM.S0->LargeSize*sizeof(WORD)/numberofworkers
						 ,AM.S0->SmallSize*sizeof(WORD)/numberofworkers
						 ,AM.S0->SmallEsize*sizeof(WORD)/numberofworkers
						 ,AM.S0->TermsInSmall
						 ,AM.S0->MaxPatches
/*						 ,AM.S0->MaxPatches/numberofworkers  */
						 ,AM.S0->MaxFpatches/numberofworkers
						 ,AM.S0->file.POsize
						 ,0);
	}
	AR.CompressPointer = AR.CompressBuffer;
/*
	Install the store caches (15-aug-2006 JV)
*/
	AT.StoreCache = AT.StoreCacheAlloc = 0;
	if ( AM.NumStoreCaches > 0 ) {
		STORECACHE sa, sb;
		LONG size;
		size = sizeof(struct StOrEcAcHe)+AM.SizeStoreCache;
		size = ((size-1)/sizeof(size_t)+1)*sizeof(size_t);
		AT.StoreCacheAlloc = (STORECACHE)Malloc1(size*AM.NumStoreCaches,"StoreCaches");
		sa = AT.StoreCache = AT.StoreCacheAlloc;
		for ( i = 0; i < AM.NumStoreCaches; i++ ) {
			sb = (STORECACHE)(void *)((UBYTE *)sa+size);
			if ( i == AM.NumStoreCaches-1 ) {
				sa->next = 0;
			}
			else {
				sa->next = sb;
			}
			SETBASEPOSITION(sa->position,-1);
			SETBASEPOSITION(sa->toppos,-1);
			sa = sb;
		}		
	}

	ReserveTempFiles(2);
	return(B);
OnError:;
	MLOCK(ErrorMessageLock);
	MesPrint("Error initializing thread %d",identity);
	MUNLOCK(ErrorMessageLock);
	Terminate(-1);
	return(B);
}

/*
  	#] InitializeOneThread : 
  	#[ FinalizeOneThread :
*/
/**
 *	To be called at the end of the run to give the final time statistics for
 *	this thread.
 *
 *	@param identity The TFORM defined integer identity of the thread.
 *	                In principle we could find it out from here with a call
 *	                to WhoAmI but because this is to be called at a very
 *	                late stage during clean up, we don't want to run any risks.
 */

void FinalizeOneThread(int identity)
{
	timerinfo[identity] = TimeCPU(1);
}

/*
  	#] FinalizeOneThread : 
  	#[ ClearAllThreads :
*/
/**
 *	To be called at the end of running TFORM.
 *	Theoretically the system can clean up after up, but it may be better
 *	to do it ourselves.
 */

void ClearAllThreads(void)
{
	int i;
	MasterWaitAll();
	for ( i = 1; i <= numberofworkers; i++ ) {
		WakeupThread(i,CLEARCLOCK);
	}
#ifdef WITHSORTBOTS
	for ( i = numberofworkers+1; i <= numberofworkers+numberofsortbots; i++ ) {
		WakeupThread(i,CLEARCLOCK);
	}
#endif
}

/*
  	#] ClearAllThreads : 
  	#[ TerminateAllThreads :
*/
/**
 *	To be called at the end of running TFORM.
 *	Theoretically the system can clean up after up, but it may be better
 *	to do it ourselves.
 */

void TerminateAllThreads(void)
{
	int i;
	for ( i = 1; i <= numberofworkers; i++ ) {
		GetThread(i);
		WakeupThread(i,TERMINATETHREAD);
	}
#ifdef WITHSORTBOTS
	for ( i = numberofworkers+1; i <= numberofworkers+numberofsortbots; i++ ) {
		WakeupThread(i,TERMINATETHREAD);
	}
#endif
	for ( i = 1; i <= numberofworkers; i++ ) {
		pthread_join(threadpointers[i],NULL);
	}
#ifdef WITHSORTBOTS
	for ( i = numberofworkers+1; i <= numberofworkers+numberofsortbots; i++ ) {
		pthread_join(threadpointers[i],NULL);
	}
#endif
}

/*
  	#] TerminateAllThreads : 
  	#[ MakeThreadBuckets :
*/
/**
 *	Creates 2*number thread buckets. We want double the number because
 *	we want to prepare number of them while another number are occupied.
 *
 *	Each bucket should have about AC.ThreadBucketSize*AM.MaxTerm words.
 *
 *	When loading a thread we only have to pass the address of a full bucket.
 *	This gives more overlap between the master and the workers and hence
 *	less waiting.
 *
 *	The buckets are used because sending terms one by one to the workers
 *	costs too much overhead. Hence we put a number of terms in each bucket
 *	and then pass the whole bucket. In the ideal case the master loads the
 *	buckets while the workers are processing the contents of the buckets
 *	they have been assigned. In practise often the processing can go faster
 *	than that the master can fill the buckets for all workers.
 *	It should be possible to improve this bucket system, but the trivial
 *	idea 
 *
 *	@param number The number of workers
 *	@param par    par = 0: First allocation
 *	              par = 1: Reallocation when we change the bucket size with the 
 *	                       threadbucketsize statement.
 */

int MakeThreadBuckets(int number, int par)
{
	int i;
	LONG sizethreadbuckets;
	THREADBUCKET *thr;
/*
	First we need a decent estimate. Not all terms should be maximal.
	Note that AM.MaxTer is in bytes!!!
	Maybe we should try to limit the size here a bit more effectively.
	This is a great consumer of memory.
*/
	sizethreadbuckets = ( AC.ThreadBucketSize + 1 ) * AM.MaxTer + 2*sizeof(WORD);
	if ( AC.ThreadBucketSize >= 250 )      sizethreadbuckets /= 4;
	else if ( AC.ThreadBucketSize >= 90 )  sizethreadbuckets /= 3;
	else if ( AC.ThreadBucketSize >= 40 )  sizethreadbuckets /= 2;
	sizethreadbuckets /= sizeof(WORD);
	
	if ( par == 0 ) {
		numthreadbuckets = 2*(number-1);
		threadbuckets = (THREADBUCKET **)Malloc1(numthreadbuckets*sizeof(THREADBUCKET *),"threadbuckets");
		freebuckets = (THREADBUCKET **)Malloc1(numthreadbuckets*sizeof(THREADBUCKET *),"threadbuckets");
	}
	if ( par > 0 ) {
		if ( sizethreadbuckets <= threadbuckets[0]->threadbuffersize ) return(0);
		for ( i = 0; i < numthreadbuckets; i++ ) {
			thr = threadbuckets[i];
			M_free(thr->deferbuffer,"deferbuffer");
		}
	}
	else {
		for ( i = 0; i < numthreadbuckets; i++ ) {
			threadbuckets[i] = (THREADBUCKET *)Malloc1(sizeof(THREADBUCKET),"threadbuckets");
			threadbuckets[i]->lock = dummylock;
		}
	}
	for ( i = 0; i < numthreadbuckets; i++ ) {
		thr = threadbuckets[i];
		thr->threadbuffersize = sizethreadbuckets;
		thr->free = BUCKETFREE;
		thr->deferbuffer = (POSITION *)Malloc1(2*sizethreadbuckets*sizeof(WORD)
					+(AC.ThreadBucketSize+1)*sizeof(POSITION),"deferbuffer");
		thr->threadbuffer = (WORD *)(thr->deferbuffer+AC.ThreadBucketSize+1);
		thr->compressbuffer = (WORD *)(thr->threadbuffer+sizethreadbuckets);
		thr->busy = BUCKETPREPARINGTERM;
		thr->usenum = thr->totnum = 0;
		thr->type = BUCKETDOINGTERMS;
	}
	return(0);
}

/*
  	#] MakeThreadBuckets : 
  	#[ GetTimerInfo :
*/

/**
 *  Returns a pointer to the static timerinfo together with information about
 *  its size. This is used by the checkpoint code to save this information in
 *  the recovery file.
 */
int GetTimerInfo(LONG** ti,LONG** sti)
{
	*ti = timerinfo;
	*sti = sumtimerinfo;
#ifdef WITHSORTBOTS
	return AM.totalnumberofthreads*2;
#else
	return AM.totalnumberofthreads;
#endif
}

/*
  	#] GetTimerInfo : 
  	#[ WriteTimerInfo :
*/

/**
 *  Writes data into the static timerinfo variable. This is used by the
 *  checkpoint code to restore the correct timings for the individual threads.
 */
void WriteTimerInfo(LONG* ti,LONG* sti)
{
	int i;
#ifdef WITHSORTBOTS
	int max = AM.totalnumberofthreads*2;
#else
	int max = AM.totalnumberofthreads;
#endif
	for ( i=0; i<max; ++i ) {
		timerinfo[i] = ti[i];
		sumtimerinfo[i] = sti[i];
	}
}

/*
  	#] WriteTimerInfo : 
  	#[ GetWorkerTimes :
*/
/**
 *	Gets the total CPU time of all workers together.
 *	To be called at the end of the TFORM run.
 */

LONG GetWorkerTimes(void)
{
	LONG retval = 0;
	int i;
	for ( i = 1; i <= numberofworkers; i++ ) retval += timerinfo[i] + sumtimerinfo[i];
#ifdef WITHSORTBOTS
	for ( i = numberofworkers+1; i <= numberofworkers+numberofsortbots; i++ )
		retval += timerinfo[i] + sumtimerinfo[i];
#endif
	return(retval);
}

/*
  	#] GetWorkerTimes : 
  	#[ UpdateOneThread :
*/
/**
 *	Fix up some of the things that happened at compiler time.
 *
 *	@param identity The TFORM defined integer thread identifier.
 */

int UpdateOneThread(int identity)
{
	ALLPRIVATES *B = AB[identity], *B0 = AB[0];
	AR.GetFile = AR0.GetFile;
	AR.KeptInHold = AR0.KeptInHold;
	AR.CurExpr = AR0.CurExpr;
	AR.SortType = AC.SortType;
	if ( AT.WildcardBufferSize < AC.WildcardBufferSize ) {
		M_free(AT.WildArgTaken,"argument list names");
		AT.WildcardBufferSize = AC.WildcardBufferSize;
		AT.WildArgTaken = (WORD *)Malloc1((LONG)AC.WildcardBufferSize*sizeof(WORD)/2
				,"argument list names");
		if ( AT.WildArgTaken == 0 ) return(-1);
	}
	return(0);
}

/*
  	#] UpdateOneThread : 
  	#[ LoadOneThread :
*/
/**
 *	Loads all relevant variables from thread 'from' into thread 'identity'
 *	This is to be done just prior to waking up the thread.
 *	It is important to keep the number of variables to be copied to a minimum
 *	because this is part of the 'overhead'.
 *
 *	@param from     the source thread which has all the variables already
 *	@param identity the TFORM defined integer thread identifier of the thread that needs the copy
 *	@param thr      the bucket that contains the terms to be processed by 'identity'
 *	@param par		if 1 copies the already active pieces in the (de)compress buffer
 *	@return Standard return convention (OK -> 0)
 */

int LoadOneThread(int from, int identity, THREADBUCKET *thr, int par)
{
	WORD *t1, *t2;
	ALLPRIVATES *B = AB[identity], *B0 = AB[from];

	AR.DefPosition = AR0.DefPosition;
	AR.NoCompress = AR0.NoCompress;
	AR.gzipCompress = AR0.gzipCompress;
	AR.BracketOn = AR0.BracketOn;
	AR.CurDum = AR0.CurDum;
	AR.DeferFlag = AR0.DeferFlag;
	AR.TePos = 0;
	AR.sLevel = AR0.sLevel;
	AR.Stage4Name = AR0.Stage4Name;
	AR.GetOneFile = AR0.GetOneFile;
	AR.PolyFun = AR0.PolyFun;
	AR.PolyFunInv = AR0.PolyFunInv;
	AR.PolyFunType = AR0.PolyFunType;
	AR.PolyFunExp = AR0.PolyFunExp;
	AR.PolyFunVar = AR0.PolyFunVar;
	AR.PolyFunPow = AR0.PolyFunPow;
	AR.Eside = AR0.Eside;
	AR.Cnumlhs = AR0.Cnumlhs;
/*
	AR.MaxBracket = AR0.MaxBracket;

	The compressbuffer contents are mainly relevant for keep brackets
	We should do this only if there is a keep brackets statement
	We may however still need the compressbuffer for expressions in the rhs.
*/
	if ( par >= 1 ) {
/*
		We may not need this %%%%% 7-apr-2006
*/
		t1 = AR.CompressBuffer; t2 = AR0.CompressBuffer;
		while ( t2 < AR0.CompressPointer ) *t1++ = *t2++;
		AR.CompressPointer = t1;

	}
	else {
		AR.CompressPointer = AR.CompressBuffer;
	}
	if ( AR.DeferFlag ) {
		if ( AR.infile->handle < 0 ) {
			AR.infile->POfill = AR0.infile->POfill;
		}
		else {
/*
			We have to set the value of POposition to something that will
			force a read in the first try.
*/
			AR.infile->POfull = AR.infile->POfill = AR.infile->PObuffer;
		}
	}
	if ( par == 0 ) {
		AN.threadbuck = thr;
		AN.ninterms = thr->firstterm;
	}
	else if ( par == 1 ) {
		WORD *tstop;
		t1 = thr->threadbuffer; tstop = t1 + *t1;
		t2 = AT.WorkPointer;
		while ( t1 < tstop ) *t2++ = *t1++;
		AN.ninterms = thr->firstterm;
	}
	AN.TeInFun = 0;
	AN.ncmod = AC.ncmod;
	AT.BrackBuf = AT0.BrackBuf;
	AT.bracketindexflag = AT0.bracketindexflag;
	AN.PolyFunTodo = 0;
/*
	The relevant variables and the term are in their place.
	There is nothing more to do.
*/
	return(0);
}

/*
  	#] LoadOneThread : 
  	#[ BalanceRunThread :
*/
/**
 *	To start a thread from the Generator routine we need to pass a number
 *	of variables.
 *	This is part of the second stage load balancing. The second stage is
 *	when we interfere with the expansion tree in Generator and let branches
 *	of the tree be treated by other workers.
 *	Early experiments show disappointing results and hence the system is
 *	currently disabled.
 *
 *	@param identity  The identity of the thread that will receive the term.
 *	@param term      The term to be passed to thread 'identity'
 *	@param level     The level at which we are in the tree. Defines the statement.
 *	@return Standard return convention (OK -> 0)
 */

int BalanceRunThread(PHEAD int identity, WORD *term, WORD level)
{
	GETBIDENTITY
	ALLPRIVATES *BB;
	WORD *t, *tt;
	int i, *ti, *tti;

	LoadOneThread(AT.identity,identity,0,2);
/*
	Extra loading if needed. Quantities changed in Generator.
	Like the level that has to be passed.
*/
	BB = AB[identity];
	BB->R.level = level;
	BB->T.TMbuff = AT.TMbuff;
	ti = AT.RepCount; tti = BB->T.RepCount;
	i = AN.RepPoint - AT.RepCount;
	BB->N.RepPoint = BB->T.RepCount + i;
	for ( ; i >= 0; i-- ) tti[i] = ti[i];

	t = term; i = *term;
	tt = BB->T.WorkSpace;
	NCOPY(tt,t,i);
	BB->T.WorkPointer = tt;

	WakeupThread(identity,HIGHERLEVELGENERATION);

	return(0);
}

/*
  	#] BalanceRunThread : 
  	#[ SetWorkerFiles :
*/
/**
 *	Initializes the scratch files at the start of the execution of a module.
 */

void SetWorkerFiles(void)
{
	int id;
	ALLPRIVATES *B, *B0 = AB[0];
	for ( id = 1; id < AM.totalnumberofthreads; id++ ) {
		B = AB[id];
		AR.infile = &(AR.Fscr[0]);
		AR.outfile = &(AR.Fscr[1]);
		AR.hidefile = &(AR.Fscr[2]);
		AR.infile->handle = AR0.infile->handle;
		AR.hidefile->handle = AR0.hidefile->handle;
		if ( AR.infile->handle < 0 ) {
			AR.infile->PObuffer = AR0.infile->PObuffer;
			AR.infile->POstop = AR0.infile->POstop;
			AR.infile->POfill = AR0.infile->POfill;
			AR.infile->POfull = AR0.infile->POfull;
			AR.infile->POsize = AR0.infile->POsize;
			AR.InInBuf = AR0.InInBuf;
			AR.infile->POposition = AR0.infile->POposition;
			AR.infile->filesize = AR0.infile->filesize;
		}
		else {
			AR.infile->PObuffer = AR.infile->wPObuffer;
			AR.infile->POstop = AR.infile->wPOstop;
			AR.infile->POfill = AR.infile->wPOfill;
			AR.infile->POfull = AR.infile->wPOfull;
			AR.infile->POsize = AR.infile->wPOsize;
			AR.InInBuf = 0;
			PUTZERO(AR.infile->POposition);
		}
/*
		If there is some writing, it betters happens to ones own outfile.
		Currently this is to be done only for InParallel.
		Merging of the outputs is then done by the CopyExpression routine.
*/
		{
			AR.outfile->PObuffer = AR.outfile->wPObuffer;
			AR.outfile->POstop = AR.outfile->wPOstop;
			AR.outfile->POfill = AR.outfile->wPOfill;
			AR.outfile->POfull = AR.outfile->wPOfull;
			AR.outfile->POsize = AR.outfile->wPOsize;
			PUTZERO(AR.outfile->POposition);
		}
		if ( AR.hidefile->handle < 0 ) {
			AR.hidefile->PObuffer = AR0.hidefile->PObuffer;
			AR.hidefile->POstop = AR0.hidefile->POstop;
			AR.hidefile->POfill = AR0.hidefile->POfill;
			AR.hidefile->POfull = AR0.hidefile->POfull;
			AR.hidefile->POsize = AR0.hidefile->POsize;
			AR.InHiBuf = AR0.InHiBuf;
			AR.hidefile->POposition = AR0.hidefile->POposition;
			AR.hidefile->filesize = AR0.hidefile->filesize;
		}
		else {
			AR.hidefile->PObuffer = AR.hidefile->wPObuffer;
			AR.hidefile->POstop = AR.hidefile->wPOstop;
			AR.hidefile->POfill = AR.hidefile->wPOfill;
			AR.hidefile->POfull = AR.hidefile->wPOfull;
			AR.hidefile->POsize = AR.hidefile->wPOsize;
			AR.InHiBuf = 0;
			PUTZERO(AR.hidefile->POposition);
		}
	}
	if ( AR0.StoreData.dirtyflag ) {
		for ( id = 1; id < AM.totalnumberofthreads; id++ ) {
			B = AB[id];
			AR.StoreData = AR0.StoreData;
		}
	}
}

/*
  	#] SetWorkerFiles : 
  	#[ RunThread :
*/
/**
 *	This is the routine that represents each worker.
 *	The model is that the worker waits for a 'signal'.
 *	If there is a signal it wakes up, looks at what signal and then takes
 *	the corresponding action. After this it goes back to sleep.
 */

void *RunThread(void *dummy)
{
	WORD *term, *ttin, *tt, *ttco, *oldwork;
	int identity, wakeupsignal, identityretv, i, tobereleased, errorcode;
	ALLPRIVATES *B;
	THREADBUCKET *thr;
	POSITION *ppdef;
	EXPRESSIONS e;
	DUMMYUSE(dummy);
	identity = SetIdentity(&identityretv);
	threadpointers[identity] = pthread_self();
	B = InitializeOneThread(identity);
	while ( ( wakeupsignal = ThreadWait(identity) ) > 0 ) {
		switch ( wakeupsignal ) {
/*
			#[ STARTNEWEXPRESSION :
*/
			case STARTNEWEXPRESSION:
/*
				Set up the sort routines etc.
				Start with getting some buffers synchronized with the compiler
*/
				if ( UpdateOneThread(identity) ) {
					MLOCK(ErrorMessageLock);
					MesPrint("Update error in starting expression in thread %d in module %d",identity,AC.CModule);
					MUNLOCK(ErrorMessageLock);
					Terminate(-1);
				}
				AR.DeferFlag = AC.ComDefer;
				AR.sLevel = AS.sLevel;
				AR.MaxDum = AM.IndDum;
				AR.expchanged = AB[0]->R.expchanged;
				AR.expflags = AB[0]->R.expflags;
				AR.PolyFun = AB[0]->R.PolyFun;
				AR.PolyFunInv = AB[0]->R.PolyFunInv;
				AR.PolyFunType = AB[0]->R.PolyFunType;
				AR.PolyFunExp = AB[0]->R.PolyFunExp;
				AR.PolyFunVar = AB[0]->R.PolyFunVar;
				AR.PolyFunPow = AB[0]->R.PolyFunPow;
/*
				Now fire up the sort buffer.
*/
				NewSort(BHEAD0);
				break;
/*
			#] STARTNEWEXPRESSION : 
			#[ LOWESTLEVELGENERATION :
*/
			case LOWESTLEVELGENERATION:
#ifdef INNERTEST
				if ( AC.InnerTest ) {
					if ( StrCmp(AC.TestValue,(UBYTE *)INNERTEST) == 0 ) {
						MesPrint("Testing(Worker%d): value = %s",AT.identity,AC.TestValue);
					}
				}
#endif
				e = Expressions + AR.CurExpr;
				thr = AN.threadbuck;
				ppdef = thr->deferbuffer;
				ttin = thr->threadbuffer;
				ttco = thr->compressbuffer;
				term = AT.WorkPointer;
				thr->usenum = 0;
				tobereleased = 0;
				AN.inputnumber = thr->firstterm;
				AN.ninterms = thr->firstterm;
				do {
				  thr->usenum++;	/* For if the master wants to steal the bucket */
				  tt = term; i = *ttin;
				  NCOPY(tt,ttin,i);
				  AT.WorkPointer = tt;
				  if ( AR.DeferFlag ) {
					tt = AR.CompressBuffer; i = *ttco;
					NCOPY(tt,ttco,i);
					AR.CompressPointer = tt;
					AR.DefPosition = ppdef[0]; ppdef++;
				  }
				  if ( thr->free == BUCKETTERMINATED ) {
/*
				    The next statement allows the master to steal the bucket
				    for load balancing purposes. We do still execute the current
					term, but afterwards we drop out.
					Once we have written the release code, we cannot use this
					bucket anymore. Hence the exit to the label bucketstolen.
*/
					if ( thr->usenum == thr->totnum ) {
						thr->free = BUCKETCOMINGFREE;
					}
					else {
						thr->free = BUCKETRELEASED;
						tobereleased = 1;
					}
				  }
/*
					What if we want to steal and we set thr->free while
					the thread is inside the next code for a long time?
				  if ( AT.LoadBalancing ) {
*/
					LOCK(thr->lock);
					thr->busy = BUCKETDOINGTERM;
					UNLOCK(thr->lock);
/*
				  }
				  else {
					thr->busy = BUCKETDOINGTERM;
				  }
*/
				  AN.RepPoint = AT.RepCount + 1;

				  if ( ( e->vflags & ISFACTORIZED ) != 0 && term[1] == HAAKJE ) {
				    StoreTerm(BHEAD term);
				  }
				  else {
				  if ( AR.DeferFlag ) {
					AR.CurDum = AN.IndDum = Expressions[AR.CurExpr].numdummies + AM.IndDum;
				  }
				  else {
					AN.IndDum = AM.IndDum;
					AR.CurDum = ReNumber(BHEAD term);
				  }
				  if ( AC.SymChangeFlag ) MarkDirty(term,DIRTYSYMFLAG);
				  if ( AN.ncmod ) {
					if ( ( AC.modmode & ALSOFUNARGS ) != 0 ) MarkDirty(term,DIRTYFLAG);
					else if ( AR.PolyFun ) PolyFunDirty(BHEAD term);
				  }
				  else if ( AC.PolyRatFunChanged ) PolyFunDirty(BHEAD term);
				  if ( ( AP.PreDebug & THREADSDEBUG ) != 0 ) {
					MLOCK(ErrorMessageLock);
					MesPrint("Thread %w executing term:");
					PrintTerm(term,"LLG");
					MUNLOCK(ErrorMessageLock);
				  }
				  if ( ( AR.PolyFunType == 2 ) && ( AC.PolyRatFunChanged == 0 )
						&& ( e->status == LOCALEXPRESSION || e->status == GLOBALEXPRESSION ) ) {
						PolyFunClean(BHEAD term);
				  }
				  if ( Generator(BHEAD term,0) ) {
					LowerSortLevel();
					MLOCK(ErrorMessageLock);
					MesPrint("Error in processing one term in thread %d in module %d",identity,AC.CModule);
					MUNLOCK(ErrorMessageLock);
					Terminate(-1);
				  }
				  AN.ninterms++;
				  }
/*				  if ( AT.LoadBalancing ) { */
					LOCK(thr->lock);
					thr->busy = BUCKETPREPARINGTERM;
					UNLOCK(thr->lock);
/*
				  }
				  else {
					thr->busy = BUCKETPREPARINGTERM;
				  }
*/
				  if ( thr->free == BUCKETTERMINATED ) {
					if ( thr->usenum == thr->totnum ) {
						thr->free = BUCKETCOMINGFREE;
					}
					else {
						thr->free = BUCKETRELEASED;
						tobereleased = 1;
					}
				  }
				  if ( tobereleased ) goto bucketstolen;
				} while ( *ttin );
				thr->free = BUCKETCOMINGFREE;
bucketstolen:;
/*				if ( AT.LoadBalancing ) { */
					LOCK(thr->lock);
					thr->busy = BUCKETTOBERELEASED;
					UNLOCK(thr->lock);
/*				}
				else {
					thr->busy = BUCKETTOBERELEASED;
				}
*/
				AT.WorkPointer = term;
				break;
/*
			#] LOWESTLEVELGENERATION : 
			#[ FINISHEXPRESSION :
*/
#ifdef WITHSORTBOTS
			case CLAIMOUTPUT:
				LOCK(AT.SB.MasterBlockLock[1]);
				break;
#endif
			case FINISHEXPRESSION:
/*
				Finish the sort

				Start with claiming the first block
				Once we have claimed it we can let the master know that
				everything is all right.
*/
				LOCK(AT.SB.MasterBlockLock[1]);
				ThreadClaimedBlock(identity);
/*
				Entry for when we work with sortbots
*/
#ifdef WITHSORTBOTS
				/* fall through */
			case FINISHEXPRESSION2:
#endif
/*
				Now we may need here an fsync on the sort file
*/
				if ( AC.ThreadSortFileSynch ) {
				  if ( AT.S0->file.handle >= 0 ) {
					SynchFile(AT.S0->file.handle);
				  }
				}
				AT.SB.FillBlock = 1;
				AT.SB.MasterFill[1] = AT.SB.MasterStart[1];
				errorcode = EndSort(BHEAD AT.S0->sBuffer,0);
				UNLOCK(AT.SB.MasterBlockLock[AT.SB.FillBlock]);
				UpdateMaxSize();
				if ( errorcode ) {
					MLOCK(ErrorMessageLock);
					MesPrint("Error terminating sort in thread %d in module %d",identity,AC.CModule);
					MUNLOCK(ErrorMessageLock);
					Terminate(-1);
				}
				break;
/*
			#] FINISHEXPRESSION : 
			#[ CLEANUPEXPRESSION :
*/
			case CLEANUPEXPRESSION:
/*
				Cleanup everything and wait for the next expression
*/
				if ( AR.outfile->handle >= 0 ) {
					CloseFile(AR.outfile->handle);
					AR.outfile->handle = -1;
					remove(AR.outfile->name);
					AR.outfile->POfill = AR.outfile->POfull = AR.outfile->PObuffer;
					PUTZERO(AR.outfile->POposition);
					PUTZERO(AR.outfile->filesize);
				}
				else {
					AR.outfile->POfill = AR.outfile->POfull = AR.outfile->PObuffer;
					PUTZERO(AR.outfile->POposition);
					PUTZERO(AR.outfile->filesize);
				}
				{
					CBUF *C = cbuf+AT.ebufnum;
					WORD **w, ii;
					if ( C->numrhs > 0 || C->numlhs > 0 ) {
						if ( C->rhs ) {
							w = C->rhs; ii = C->numrhs;
							do { *w++ = 0; } while ( --ii > 0 );
						}
						if ( C->lhs ) {
							w = C->lhs; ii = C->numlhs;
							do { *w++ = 0; } while ( --ii > 0 );
						}
						C->numlhs = C->numrhs = 0;
						ClearTree(AT.ebufnum);
						C->Pointer = C->Buffer;
					}
				}
				break;
/*
			#] CLEANUPEXPRESSION : 
			#[ HIGHERLEVELGENERATION :
*/
			case HIGHERLEVELGENERATION:
/*
				When foliating halfway the tree.
				This should only be needed in a second level load balancing
*/
				term = AT.WorkSpace; AT.WorkPointer = term + *term;
				if ( Generator(BHEAD term,AR.level) ) {
					LowerSortLevel();
					MLOCK(ErrorMessageLock);
					MesPrint("Error in load balancing one term at level %d in thread %d in module %d",AR.level,AT.identity,AC.CModule);
					MUNLOCK(ErrorMessageLock);
					Terminate(-1);
				}
				AT.WorkPointer = term;
				break;
/*
			#] HIGHERLEVELGENERATION : 
			#[ STARTNEWMODULE :
*/
			case STARTNEWMODULE:
/*
				For resetting variables.
*/
				SpecialCleanup(B);
				break;
/*
			#] STARTNEWMODULE : 
			#[ TERMINATETHREAD :
*/
			case TERMINATETHREAD:
				goto EndOfThread;
/*
			#] TERMINATETHREAD : 
			#[ DOONEEXPRESSION :

				When a thread has to do a complete (not too big) expression.
				The number of the expression to be done is in AR.exprtodo.
				The code is mostly taken from Processor. The only difference
				is with what to do with the output.
				The output should go to the scratch buffer of the worker
				(which is free at the right moment). If this buffer is too
				small we have a problem. We could write to file or give the
				master what we have and from now on the master has to collect
				pieces until things are complete.
				Note: this assumes that the expressions don't keep their order.
				If they have to keep their order, don't use this feature.
*/
			case DOONEEXPRESSION: {

				POSITION position, outposition;
				FILEHANDLE *fi, *fout, *oldoutfile;
				LONG dd = 0;
				WORD oldBracketOn = AR.BracketOn;
				WORD *oldBrackBuf = AT.BrackBuf;
				WORD oldbracketindexflag = AT.bracketindexflag;
				WORD fromspectator = 0;
				e = Expressions + AR.exprtodo;
				i = AR.exprtodo;
				AR.CurExpr = i;
				AR.SortType = AC.SortType;
				AR.expchanged = 0;
				if ( ( e->vflags & ISFACTORIZED ) != 0 ) {
					AR.BracketOn = 1;
					AT.BrackBuf = AM.BracketFactors;
					AT.bracketindexflag = 1;
				}

				position = AS.OldOnFile[i];
				if ( e->status == HIDDENLEXPRESSION || e->status == HIDDENGEXPRESSION
					|| e->status == UNHIDELEXPRESSION || e->status == UNHIDEGEXPRESSION ) {
					AR.GetFile = 2; fi = AR.hidefile;
				}
				else {
					AR.GetFile = 0; fi = AR.infile;
				}
/*
				PUTZERO(fi->POposition);
				if ( fi->handle >= 0 ) {
					fi->POfill = fi->POfull = fi->PObuffer;
				}
*/
				SetScratch(fi,&position);
				term = oldwork = AT.WorkPointer;
				AR.CompressPointer = AR.CompressBuffer;
				AR.CompressPointer[0] = 0;
				AR.KeptInHold = 0;
				if ( GetTerm(BHEAD term) <= 0 ) {
					MLOCK(ErrorMessageLock);
					MesPrint("Expression %d has problems in scratchfile (t)",i);
					MUNLOCK(ErrorMessageLock);
					Terminate(-1);
				}
				if ( AT.bracketindexflag > 0 ) OpenBracketIndex(i);
				term[3] = i;
				if ( term[5] < 0 ) {
					fromspectator = -term[5];
					PUTZERO(AM.SpectatorFiles[fromspectator-1].readpos);
					term[5] = AC.cbufnum;
				}
				PUTZERO(outposition);
				fout = AR.outfile;
				fout->POfill = fout->POfull = fout->PObuffer;
				fout->POposition = outposition;
				if ( fout->handle >= 0 ) {
					fout->POposition = outposition;
				}
/*
				The next statement is needed because we need the system
				to believe that the expression is at position zero for
				the moment. In this worker, with no memory of other expressions,
				it is. This is needed for when a bracket index is made
				because there e->onfile is an offset. Afterwards, when the
				expression is written to its final location in the masters
				output e->onfile will get its real value.
*/
				PUTZERO(e->onfile);
				if ( PutOut(BHEAD term,&outposition,fout,0) < 0 ) goto ProcErr;

				AR.DeferFlag = AC.ComDefer;

				AR.sLevel = AB[0]->R.sLevel;
				term = AT.WorkPointer;
				NewSort(BHEAD0);
				AR.MaxDum = AM.IndDum;
				AN.ninterms = 0;
				if ( fromspectator ) {
				 while ( GetFromSpectator(term,fromspectator-1) ) {
				  AT.WorkPointer = term + *term;
				  AN.RepPoint = AT.RepCount + 1;
				  AN.IndDum = AM.IndDum;
				  AR.CurDum = ReNumber(BHEAD term);
				  if ( AC.SymChangeFlag ) MarkDirty(term,DIRTYSYMFLAG);
				  if ( AN.ncmod ) {
					if ( ( AC.modmode & ALSOFUNARGS ) != 0 ) MarkDirty(term,DIRTYFLAG);
					else if ( AR.PolyFun ) PolyFunDirty(BHEAD term);
				  }
				  else if ( AC.PolyRatFunChanged ) PolyFunDirty(BHEAD term);
				  if ( ( AR.PolyFunType == 2 ) && ( AC.PolyRatFunChanged == 0 )
						&& ( e->status == LOCALEXPRESSION || e->status == GLOBALEXPRESSION ) ) {
						PolyFunClean(BHEAD term);
				  }
				  if ( Generator(BHEAD term,0) ) {
					LowerSortLevel(); goto ProcErr;
				  }
				 }
				}
				else {
				 while ( GetTerm(BHEAD term) ) {
				  SeekScratch(fi,&position);
				  AN.ninterms++; dd = AN.deferskipped;
				  if ( ( e->vflags & ISFACTORIZED ) != 0 && term[1] == HAAKJE ) {
					  StoreTerm(BHEAD term);
				  }
				  else {
				  if ( AC.CollectFun && *term <= (AM.MaxTer/(2*(LONG)sizeof(WORD))) ) {
					if ( GetMoreTerms(term) < 0 ) {
					  LowerSortLevel(); goto ProcErr;
					}
				    SeekScratch(fi,&position);
				  }
				  AT.WorkPointer = term + *term;
				  AN.RepPoint = AT.RepCount + 1;
				  if ( AR.DeferFlag ) {
					AR.CurDum = AN.IndDum = Expressions[AR.exprtodo].numdummies;
				  }
				  else {
					AN.IndDum = AM.IndDum;
					AR.CurDum = ReNumber(BHEAD term);
				  }
				  if ( AC.SymChangeFlag ) MarkDirty(term,DIRTYSYMFLAG);
				  if ( AN.ncmod ) {
					if ( ( AC.modmode & ALSOFUNARGS ) != 0 ) MarkDirty(term,DIRTYFLAG);
					else if ( AR.PolyFun ) PolyFunDirty(BHEAD term);
				  }
				  else if ( AC.PolyRatFunChanged ) PolyFunDirty(BHEAD term);
				  if ( ( AR.PolyFunType == 2 ) && ( AC.PolyRatFunChanged == 0 )
						&& ( e->status == LOCALEXPRESSION || e->status == GLOBALEXPRESSION ) ) {
						PolyFunClean(BHEAD term);
				  }
				  if ( Generator(BHEAD term,0) ) {
					LowerSortLevel(); goto ProcErr;
				  }
				  AN.ninterms += dd;
				  }
				  SetScratch(fi,&position);
				  if ( fi == AR.hidefile ) {
					AR.InHiBuf = (fi->POfull-fi->PObuffer)
						-DIFBASE(position,fi->POposition)/sizeof(WORD);
				  }
				  else {
					AR.InInBuf = (fi->POfull-fi->PObuffer)
						-DIFBASE(position,fi->POposition)/sizeof(WORD);
				  }
				 }
				}
				AN.ninterms += dd;
				if ( EndSort(BHEAD AT.S0->sBuffer,0) < 0 ) goto ProcErr;
				e->numdummies = AR.MaxDum - AM.IndDum;
				AR.BracketOn = oldBracketOn;
				AT.BrackBuf = oldBrackBuf;
				if ( ( e->vflags & TOBEFACTORED ) != 0 )
						poly_factorize_expression(e);
				else if ( ( ( e->vflags & TOBEUNFACTORED ) != 0 )
				 && ( ( e->vflags & ISFACTORIZED ) != 0 ) )
						poly_unfactorize_expression(e);
				if ( AT.S0->TermsLeft )   e->vflags &= ~ISZERO;
				else                      e->vflags |= ISZERO;
				if ( AR.expchanged == 0 ) e->vflags |= ISUNMODIFIED;
				if ( AT.S0->TermsLeft ) AR.expflags |= ISZERO;
				if ( AR.expchanged )    AR.expflags |= ISUNMODIFIED;
				AR.GetFile = 0;
				AT.bracketindexflag = oldbracketindexflag;
/*
				Now copy the whole thing from fout to AR0.outfile
				Do this in one go to keep the lock occupied as short as possible
*/
				SeekScratch(fout,&outposition);
				LOCK(AS.outputslock);
				oldoutfile = AB[0]->R.outfile;
				if ( e->status == INTOHIDELEXPRESSION || e->status == INTOHIDEGEXPRESSION ) {
					AB[0]->R.outfile = AB[0]->R.hidefile;
				}
				SeekScratch(AB[0]->R.outfile,&position);
				e->onfile = position;
				if ( CopyExpression(fout,AB[0]->R.outfile) < 0 ) {
					AB[0]->R.outfile = oldoutfile;
					UNLOCK(AS.outputslock);
					MLOCK(ErrorMessageLock);
					MesPrint("Error copying output of 'InParallel' expression to master. Thread: %d",identity);
					MUNLOCK(ErrorMessageLock);
					goto ProcErr;
				}
				AB[0]->R.outfile = oldoutfile;
				AB[0]->R.hidefile->POfull = AB[0]->R.hidefile->POfill;
				AB[0]->R.expflags = AR.expflags;
				UNLOCK(AS.outputslock);

				if ( fout->handle >= 0 ) {	/* Now get rid of the file */
					CloseFile(fout->handle);
					fout->handle = -1;
					remove(fout->name);
					PUTZERO(fout->POposition);
					PUTZERO(fout->filesize);
					fout->POfill = fout->POfull = fout->PObuffer;
				}
				UpdateMaxSize();

				AT.WorkPointer = oldwork;

				} break;
/*
			#] DOONEEXPRESSION : 
			#[ DOBRACKETS :

				In case we have a bracket index we can have the worker treat
				one or more of the entries in the bracket index.
				The advantage is that identical terms will meet each other
				sooner in the sorting and hence fewer compares will be needed.
				Also this way the master doesn't need to fill the buckets.
				The main problem is the load balancing which can become very
				bad when there is a long tail without things outside the bracket.
				
				We get sent:
				1: The number of the first bracket to be done
				2: The number of the last bracket to be done
*/
			case DOBRACKETS: {
				BRACKETINFO *binfo;
				BRACKETINDEX *bi;
				FILEHANDLE *fi;
				POSITION stoppos,where;
				e = Expressions + AR.CurExpr;
				binfo = e->bracketinfo;
				thr = AN.threadbuck;
				bi = &(binfo->indexbuffer[thr->firstbracket]);
				if ( AR.GetFile == 2 ) fi = AR.hidefile;
				else                   fi = AR.infile;
				where = bi->start;
				ADD2POS(where,AS.OldOnFile[AR.CurExpr]);
				SetScratch(fi,&(where));
				stoppos = binfo->indexbuffer[thr->lastbracket].next;
				ADD2POS(stoppos,AS.OldOnFile[AR.CurExpr]);
				AN.ninterms = thr->firstterm;
/*
				Now we have to put the 'value' of the bracket in the
				Compress buffer.
*/
				ttco = AR.CompressBuffer;
				tt = binfo->bracketbuffer + bi->bracket;
				i = *tt;
				NCOPY(ttco,tt,i)
				AR.CompressPointer = ttco;
				term = AT.WorkPointer;
				while ( GetTerm(BHEAD term) ) {
					SeekScratch(fi,&where);
					AT.WorkPointer = term + *term;
					AN.IndDum = AM.IndDum;
					AR.CurDum = ReNumber(BHEAD term);
					if ( AC.SymChangeFlag ) MarkDirty(term,DIRTYSYMFLAG);
					if ( AN.ncmod ) {
						if ( ( AC.modmode & ALSOFUNARGS ) != 0 ) MarkDirty(term,DIRTYFLAG);
						else if ( AR.PolyFun ) PolyFunDirty(BHEAD term);
					}
					else if ( AC.PolyRatFunChanged ) PolyFunDirty(BHEAD term);
					if ( ( AR.PolyFunType == 2 ) && ( AC.PolyRatFunChanged == 0 )
						&& ( e->status == LOCALEXPRESSION || e->status == GLOBALEXPRESSION ) ) {
						PolyFunClean(BHEAD term);
					}
					if ( ( AP.PreDebug & THREADSDEBUG ) != 0 ) {
						MLOCK(ErrorMessageLock);
						MesPrint("Thread %w executing term:");
						PrintTerm(term,"DoBrackets");
						MUNLOCK(ErrorMessageLock);
					}
					AT.WorkPointer = term + *term;
					if ( Generator(BHEAD term,0) ) {
						LowerSortLevel();
						MLOCK(ErrorMessageLock);
						MesPrint("Error in processing one term in thread %d in module %d",identity,AC.CModule);
						MUNLOCK(ErrorMessageLock);
						Terminate(-1);
					}
					AN.ninterms++;
					SetScratch(fi,&(where));
					if ( ISGEPOS(where,stoppos) ) break;
				}
				AT.WorkPointer = term;
				thr->free = BUCKETCOMINGFREE;
				break;
			}
/*
			#] DOBRACKETS : 
			#[ CLEARCLOCK :

			The program only comes here after a .clear
*/
			case CLEARCLOCK:
/*				LOCK(clearclocklock); */
				sumtimerinfo[identity] += TimeCPU(1);
				timerinfo[identity] = TimeCPU(0);
/*				UNLOCK(clearclocklock); */
				break;
/*
			#] CLEARCLOCK : 
			#[ MCTSEXPANDTREE :
*/
			case MCTSEXPANDTREE:
				AT.optimtimes = AB[0]->T.optimtimes;
				find_Horner_MCTS_expand_tree();
				break;
/*
			#] MCTSEXPANDTREE : 
			#[ OPTIMIZEEXPRESSION :
*/
			case OPTIMIZEEXPRESSION:
				optimize_expression_given_Horner();
				break;
/*
			#] OPTIMIZEEXPRESSION : 
*/
			default:
				MLOCK(ErrorMessageLock);
				MesPrint("Illegal wakeup signal %d for thread %d",wakeupsignal,identity);
				MUNLOCK(ErrorMessageLock);
				Terminate(-1);
				break;
		}
		/* we need the following update in case we are using checkpoints. then we
		   need to readjust the clocks when recovering using this information */
		timerinfo[identity] = TimeCPU(1);
	}
EndOfThread:;
/*
	This is the end of the thread. We cleanup and exit.
	If we are using flint, call the per-thread cleanup function. This keep valgrind happy.
*/
#ifdef WITHFLINT
	flint_final_cleanup_thread();
#endif
	FinalizeOneThread(identity);
	return(0);
ProcErr:
	Terminate(-1);
	return(0);
}

/*
  	#] RunThread : 
  	#[ RunSortBot :
*/
/**
 *	This is the routine that represents each sortbot.
 *	The model is that the sortbot waits for a 'signal'.
 *	If there is a signal it wakes up, looks at what signal and then takes
 *	the corresponding action. After this it goes back to sleep.
 */

#ifdef WITHSORTBOTS

void *RunSortBot(void *dummy)
{
	int identity, wakeupsignal, identityretv;
	ALLPRIVATES *B, *BB;
	DUMMYUSE(dummy);
	identity = SetIdentity(&identityretv);
	threadpointers[identity] = pthread_self();
	B = InitializeOneThread(identity);
	while ( ( wakeupsignal = SortBotWait(identity) ) > 0 ) {
		switch ( wakeupsignal ) {
/*
			#[ INISORTBOT :
*/
			case INISORTBOT:
				AR.CompressBuffer = AB[0]->R.CompressBuffer;
				AR.ComprTop = AB[0]->R.ComprTop;
				AR.CompressPointer = AB[0]->R.CompressPointer;
				AR.CurExpr = AB[0]->R.CurExpr;
				AR.PolyFun = AB[0]->R.PolyFun;
				AR.PolyFunInv = AB[0]->R.PolyFunInv;
				AR.PolyFunType = AB[0]->R.PolyFunType;
				AR.PolyFunExp = AB[0]->R.PolyFunExp;
				AR.PolyFunVar = AB[0]->R.PolyFunVar;
				AR.PolyFunPow = AB[0]->R.PolyFunPow;
				AR.SortType = AC.SortType;
				if ( AR.PolyFun == 0 ) { AT.SS->PolyFlag = 0; }
				else if ( AR.PolyFunType == 1 ) { AT.SS->PolyFlag = 1; }
				else if ( AR.PolyFunType == 2 ) {
					if ( AR.PolyFunExp == 2
					  || AR.PolyFunExp == 3 ) AT.SS->PolyFlag = 1;
					else                      AT.SS->PolyFlag = 2;
				}
				AT.SS->PolyWise = 0;
				AN.ncmod = AC.ncmod;
				LOCK(AT.SB.MasterBlockLock[1]);
				BB = AB[AT.SortBotIn1];
				LOCK(BB->T.SB.MasterBlockLock[BB->T.SB.MasterNumBlocks]);
				BB = AB[AT.SortBotIn2];
				LOCK(BB->T.SB.MasterBlockLock[BB->T.SB.MasterNumBlocks]);
				AT.SB.FillBlock = 1;
				AT.SB.MasterFill[1] = AT.SB.MasterStart[1];
				SETBASEPOSITION(AN.theposition,0);
				break;
/*
			#] INISORTBOT : 
			#[ RUNSORTBOT :
*/
			case RUNSORTBOT:
				SortBotMerge(B);
				break;
/*
			#] RUNSORTBOT : 
			#[ TERMINATETHREAD :
*/
			case TERMINATETHREAD:
				goto EndOfThread;
/*
			#] TERMINATETHREAD : 
			#[ CLEARCLOCK :

			The program only comes here after a .clear
*/
			case CLEARCLOCK:
/*				LOCK(clearclocklock); */
				sumtimerinfo[identity] += TimeCPU(1);
				timerinfo[identity] = TimeCPU(0);
/*				UNLOCK(clearclocklock); */
				break;
/*
			#] CLEARCLOCK : 
*/
			default:
				MLOCK(ErrorMessageLock);
				MesPrint("Illegal wakeup signal %d for thread %d",wakeupsignal,identity);
				MUNLOCK(ErrorMessageLock);
				Terminate(-1);
				break;
		}
	}
EndOfThread:;
/*
	This is the end of the thread. We cleanup and exit.
	If we are using flint, call the per-thread cleanup function. This keep valgrind happy.
*/
#ifdef WITHFLINT
	flint_final_cleanup_thread();
#endif
	FinalizeOneThread(identity);
	return(0);
}

#endif

/*
  	#] RunSortBot : 
  	#[ IAmAvailable :
*/
/**
 *	To be called by a thread when it becomes available.
 *	Puts it on a stack.
 *	We use a stack model. It is also possible to define a circular queue.
 *	This will be tried out at a later stage.
 *	One advantage of a stack could be that if we cannot feed all threads
 *	more sorting is done at the threads and the master has to do less.
 *
 *	@param identity The identity thread that signals its availability.
 */

void IAmAvailable(int identity)
{
	int top;
	LOCK(availabilitylock);
	top = topofavailables;
	listofavailables[topofavailables++] = identity;
	if ( top == 0 ) {
		UNLOCK(availabilitylock);
		LOCK(wakeupmasterlock);
		wakeupmaster = identity;
		pthread_cond_signal(&wakeupmasterconditions);
		UNLOCK(wakeupmasterlock);
	}
	else {
		UNLOCK(availabilitylock);
	}
}

/*
  	#] IAmAvailable : 
  	#[ GetAvailableThread :
*/
/**
 *	Gets an available thread from the top of the stack.
 *	Maybe a circular buffer model would work better. This would mean that
 *	we take the lowest available worker, rather than the highest.
 *	We then have to work with high water marks and low water marks.
 *	(writing point and reading point). Still to be investigated.
 */

int GetAvailableThread(void)
{
	int retval = -1;
	LOCK(availabilitylock);
	if ( topofavailables > 0 ) retval = listofavailables[--topofavailables];
	UNLOCK(availabilitylock);
	if ( retval >= 0 ) {
/*
		Make sure the thread is indeed waiting and not between
		saying that it is available and starting to wait.
*/
		LOCK(wakeuplocks[retval]);
		UNLOCK(wakeuplocks[retval]);
	}
	return(retval);
}

/*
  	#] GetAvailableThread : 
  	#[ ConditionalGetAvailableThread :
*/
/**
 *	Looks whether a thread is available.
 *	If a thread is available it is taken from the stack of available threads.
 *
 *	@return the identity of an available thread or -1 if none is available.
 */

int ConditionalGetAvailableThread(void)
{
	int retval = -1;
	if ( topofavailables > 0 ) {
		LOCK(availabilitylock);
		if ( topofavailables > 0 ) {
			retval = listofavailables[--topofavailables];
		}
		UNLOCK(availabilitylock);
		if ( retval >= 0 ) {
/*
			Make sure the thread is indeed waiting and not between
			saying that it is available and starting to wait.
*/
			LOCK(wakeuplocks[retval]);
			UNLOCK(wakeuplocks[retval]);
		}
	}
	return(retval);
}

/*
  	#] ConditionalGetAvailableThread : 
  	#[ GetThread :
*/
/**
 *	Gets a given thread from the list of available threads, even if
 *	it isn't on the top of the stack.
 *
 *	@param identity The number of the thread that we want to remove from the
 *	                list of available threads.
 *	@return The number of the thread if it was available. -1 otherwise.
 */

int GetThread(int identity)
{
	int retval = -1, j;
	LOCK(availabilitylock);
	for ( j = 0; j < topofavailables; j++ ) {
		if ( identity == listofavailables[j] ) break;
	}
	if ( j < topofavailables ) {
		--topofavailables;
		for ( ; j < topofavailables; j++ ) {
			listofavailables[j] = listofavailables[j+1];
		}
		retval = identity;
	}
	UNLOCK(availabilitylock);
	return(retval);
}

/*
  	#] GetThread : 
  	#[ ThreadWait :
*/
/**
 *	To be called by a thread when it has nothing to do.
 *	It goes to sleep and waits for a wakeup call.
 *	The return value is the number of the wakeup signal.
 *
 *	@param identity The number of the thread.
 *	@return The number of the wake-up signal.
 */

int ThreadWait(int identity)
{
	int retval, top, j;
	LOCK(wakeuplocks[identity]);
	LOCK(availabilitylock);
	top = topofavailables;
	for ( j = topofavailables; j > 0; j-- )
		listofavailables[j] = listofavailables[j-1];
	listofavailables[0] = identity;
	topofavailables++;
	if ( top == 0 || topofavailables == numberofworkers ) {
		UNLOCK(availabilitylock);
		LOCK(wakeupmasterlock);
		wakeupmaster = identity;
		pthread_cond_signal(&wakeupmasterconditions);
		UNLOCK(wakeupmasterlock);
	}
	else {
		UNLOCK(availabilitylock);
	}
	while ( wakeup[identity] == 0 ) {
		pthread_cond_wait(&(wakeupconditions[identity]),&(wakeuplocks[identity]));
	}
	retval = wakeup[identity];
	wakeup[identity] = 0;
	UNLOCK(wakeuplocks[identity]);
	return(retval);
}

/*
  	#] ThreadWait : 
  	#[ SortBotWait :
*/
 
#ifdef WITHSORTBOTS
/**
 *	To be called by a sortbot thread when it has nothing to do.
 *	It goes to sleep and waits for a wakeup call.
 *	The return value is the number of the wakeup signal.
 *
 *	@param identity The number of the sortbot thread.
 *	@return The number of the wake-up signal.
 */

int SortBotWait(int identity)
{
	int retval;
	LOCK(wakeuplocks[identity]);
	LOCK(availabilitylock);
	topsortbotavailables++;
	if ( topsortbotavailables >= numberofsortbots ) {
		UNLOCK(availabilitylock);
		LOCK(wakeupsortbotlock);
		wakeupmaster = identity;
		pthread_cond_signal(&wakeupsortbotconditions);
		UNLOCK(wakeupsortbotlock);
	}
	else {
		UNLOCK(availabilitylock);
	}
	while ( wakeup[identity] == 0 ) {
		pthread_cond_wait(&(wakeupconditions[identity]),&(wakeuplocks[identity]));
	}
	retval = wakeup[identity];
	wakeup[identity] = 0;
	UNLOCK(wakeuplocks[identity]);
	return(retval);
}

#endif

/*
  	#] SortBotWait : 
  	#[ ThreadClaimedBlock :
*/
/**
 *	When the final sort of an expression starts the workers have to claim
 *	the first block in the buffers of the master for their output.
 *	The master may only continue after all workers have claimed their block
 *	because otherwise it is possible that the master may claim this block for
 *	reading before it has been written in.
 *	Hence the master must wait till all blocks have been claimed. Then the
 *	master will get signalled that it can continue.
 */

int ThreadClaimedBlock(int identity)
{
	LOCK(availabilitylock);
	numberclaimed++;	
	if ( numberclaimed >= numberofworkers ) {
		UNLOCK(availabilitylock);
		LOCK(wakeupmasterlock);
		wakeupmaster = identity;
		pthread_cond_signal(&wakeupmasterconditions);
		UNLOCK(wakeupmasterlock);
	}
	else {
		UNLOCK(availabilitylock);
	}
	return(0);
}

/*
  	#] ThreadClaimedBlock : 
  	#[ MasterWait :
*/
/**
 *	To be called by the master when it has to wait for one of the
 *	workers to become available.
 *	It goes to sleep and waits for a wakeupmaster call.
 *	The return value is the identity of the process that wakes up the master.
 */

int MasterWait(void)
{
	int retval;
	LOCK(wakeupmasterlock);
	while ( wakeupmaster == 0 ) {
		pthread_cond_wait(&wakeupmasterconditions,&wakeupmasterlock);
	}
	retval = wakeupmaster;
	wakeupmaster = 0;
	UNLOCK(wakeupmasterlock);
	return(retval);
}

/*
  	#] MasterWait : 
  	#[ MasterWaitThread :
*/
/**
 *	To be called by the master when it has to wait for a specific one of the
 *	workers to become available.
 *	The return value is the value of the signal.
 */

int MasterWaitThread(int identity)
{
	int retval;
	LOCK(wakeupmasterthreadlocks[identity]);
	while ( wakeupmasterthread[identity] == 0 ) {
		pthread_cond_wait(&(wakeupmasterthreadconditions[identity])
				,&(wakeupmasterthreadlocks[identity]));
	}
	retval = wakeupmasterthread[identity];
	wakeupmasterthread[identity] = 0;
	UNLOCK(wakeupmasterthreadlocks[identity]);
	return(retval);
}

/*
  	#] MasterWaitThread : 
  	#[ MasterWaitAll :
*/
/**
 *	To be called by the master when it has to wait for all of the
 *	workers to finish a given task.
 *	It goes to sleep and waits for a wakeup call in ThreadWait
 */

void MasterWaitAll(void)
{
	LOCK(wakeupmasterlock);
	while ( topofavailables < numberofworkers ) {
		pthread_cond_wait(&wakeupmasterconditions,&wakeupmasterlock);
	}
	UNLOCK(wakeupmasterlock);
	return;
}

/*
  	#] MasterWaitAll : 
  	#[ MasterWaitAllSortBots :
*/
 
#ifdef WITHSORTBOTS

/**
 *	To be called by the master when it has to wait for all of the
 *	sortbots to start their task.
 */

void MasterWaitAllSortBots(void)
{
	LOCK(wakeupsortbotlock);
	while ( topsortbotavailables < numberofsortbots ) {
		pthread_cond_wait(&wakeupsortbotconditions,&wakeupsortbotlock);
	}
	UNLOCK(wakeupsortbotlock);
	return;
}

#endif

/*
  	#] MasterWaitAllSortBots : 
  	#[ MasterWaitAllBlocks :
*/
/**
 *	To be called by the master when it has to wait for all of the
 *	workers to claim their first block in the sort buffers of the master.
 *	It goes to sleep and waits for a wakeup call.
 */

void MasterWaitAllBlocks(void)
{
	LOCK(wakeupmasterlock);
	while ( numberclaimed < numberofworkers ) {
		pthread_cond_wait(&wakeupmasterconditions,&wakeupmasterlock);
	}
	UNLOCK(wakeupmasterlock);
	return;
}

/*
  	#] MasterWaitAllBlocks : 
  	#[ WakeupThread :
*/
/**
 *	To be called when the indicated thread needs waking up.
 *	The signal number should be nonzero!
 *
 *	@param identity     The number of the worker to be woken up
 *	@param signalnumber The signal with which it should be woken up.
 */

void WakeupThread(int identity, int signalnumber)
{
	if ( signalnumber == 0 ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Illegal wakeup signal for thread %d",identity);
		MUNLOCK(ErrorMessageLock);
		Terminate(-1);
	}
	LOCK(wakeuplocks[identity]);
	wakeup[identity] = signalnumber;
	pthread_cond_signal(&(wakeupconditions[identity]));
	UNLOCK(wakeuplocks[identity]);
}

/*
  	#] WakeupThread : 
  	#[ WakeupMasterFromThread :
*/
/**
 *	To be called when the indicated thread needs to wake up the master.
 *	The signal number should be nonzero!
 *
 *	@param identity     The number of the worker who wakes up the master.
 *	@param signalnumber The signal with which the master should be woken up.
 */

void WakeupMasterFromThread(int identity, int signalnumber)
{
	if ( signalnumber == 0 ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Illegal wakeup signal for master %d",identity);
		MUNLOCK(ErrorMessageLock);
		Terminate(-1);
	}
	LOCK(wakeupmasterthreadlocks[identity]);
	wakeupmasterthread[identity] = signalnumber;
	pthread_cond_signal(&(wakeupmasterthreadconditions[identity]));
	UNLOCK(wakeupmasterthreadlocks[identity]);
}

/*
  	#] WakeupMasterFromThread : 
  	#[ SendOneBucket :
*/
/**
 *	To be called when there is a full bucket and an available thread
 *	It prepares the thread and then wakes it up.
 */

int SendOneBucket(int type)
{
	ALLPRIVATES *B0 = AB[0];
	THREADBUCKET *thr = 0;
	int j, k, id;
	for ( j = 0; j < numthreadbuckets; j++ ) {
		if ( threadbuckets[j]->free == BUCKETFILLED ) {
			thr = threadbuckets[j];
			for ( k = j+1; k < numthreadbuckets; k++ )
				threadbuckets[k-1] = threadbuckets[k];
			threadbuckets[numthreadbuckets-1] = thr;
			break;
		}
	}
	AN0.ninterms++;
	while ( ( id = GetAvailableThread() ) < 0 ) { MasterWait(); }
/*
	Prepare the thread. Give it the term and variables.
*/
	LoadOneThread(0,id,thr,0);
	thr->busy = BUCKETASSIGNED;
	thr->free = BUCKETINUSE;
	numberoffullbuckets--;
/*
	And signal the thread to run.
	Form now on we may only interfere with this bucket
	1: after it has been marked BUCKETCOMINGFREE
	2: when thr->busy == BUCKETDOINGTERM and then only when protected by
	   thr->lock. This would be for load balancing.
*/
	WakeupThread(id,type);
/*	AN0.ninterms += thr->ddterms; */
	return(0);
}

/*
  	#] SendOneBucket : 
  	#[ InParallelProcessor :
*/
/**
 *	We divide the expressions marked by partodo over the workers.
 *	The workers are responsible for writing their results into the buffers
 *	of the master (output). This is to be controlled by locks.
 *	The order of the expressions may get changed this way.
 *
 *	The InParallel statement allows the execution of complete expressions
 *	in a single worker simultaneously. This is useful for when there are
 *	many short expressions. This way we don't need the bottleneck of the
 *	merging by the master. The complete sort for each expression is done
 *	inside its own single worker. The bottleneck here is the writing of the
 *	result into the scratch system. This is now done by the workers themselves.
 *	Because each expression must be contiguous, the writing should be done
 *	as quickly as possible and be protected by locks.
 *
 *	The implementation of this statement gave a significant increase in
 *	efficiency in the running of the Multiple Zeta Values program.
 */

int InParallelProcessor(void)
{
	GETIDENTITY
	int i, id, retval = 0, num = 0;
	EXPRESSIONS e;
	if ( numberofworkers >= 2 ) {
		SetWorkerFiles();
		for ( i = 0; i < NumExpressions; i++ ) {
			e = Expressions+i;
			if ( e->partodo <= 0 ) continue;
			if ( e->status == LOCALEXPRESSION || e->status == GLOBALEXPRESSION
			|| e->status == UNHIDELEXPRESSION || e->status == UNHIDEGEXPRESSION
			|| e->status == INTOHIDELEXPRESSION || e->status == INTOHIDEGEXPRESSION ) {
			}
			else {
				e->partodo = 0;
				continue;
			}
			if ( e->counter == 0 ) { /* Expression with zero terms */
				e->partodo = 0;
				continue;
			}
/*
			This expression should go to an idle worker
*/
			while ( ( id = GetAvailableThread() ) < 0 ) { MasterWait(); }
			LoadOneThread(0,id,0,-1);
			AB[id]->R.exprtodo = i;
			WakeupThread(id,DOONEEXPRESSION);
			num++;
		}
/*
		Now we have to wait for all workers to finish
*/
		if ( num > 0 ) MasterWaitAll();

		if ( AC.CollectFun ) AR.DeferFlag = 0;
	}
	else {
		for ( i = 0; i < NumExpressions; i++ ) {
			Expressions[i].partodo = 0;
		}
	}
	return(retval);
}

/*
  	#] InParallelProcessor : 
  	#[ ThreadsProcessor :
*/
/**
 *	This routine takes the role of the central part of the Processor routine
 *	in the file proces.c when multiple threads are available.
 *	It deals with the expressions that are not marked in the InParallel
 *	statement. These are usually the large expressions. It will divide
 *	the terms of these expressions over the workers, using a bucket system
 *	to reduce overhead (buckets are collections of a number of terms that
 *	are transferred together).
 *	At the end of the expression when all terms have been assigned and 
 *	workers become available again, there is a load balancing system to
 *	take terms from the buckets of workers that still have to do many terms
 *	and give them to idle workers. This is called first level load balancing.
 *
 *	A new feature is that for expressions with a bracket index the terms
 *	can be distributed in collections of complete brackets (12-nov-2009).
 *
 *	The routine is called for each expression separately by Processor.
 *
 *	@param e              The expression to be executed
 *	@param LastExpression Indicates whether it is the last expression in which case
 *	                      in the end the input scratch file can be deleted before
 *	                      the output is written. This saves diskspace.
 */

int ThreadsProcessor(EXPRESSIONS e, WORD LastExpression, WORD fromspectator)
{
	ALLPRIVATES *B0 = AB[0], *B = B0;
	int id, oldgzipCompress, endofinput = 0, j, still, k, defcount = 0, bra = 0, first = 1;
	LONG dd = 0, ddd, thrbufsiz, thrbufsiz0, thrbufsiz2, numbucket = 0, numpasses;
	LONG num, i;
	WORD *oldworkpointer = AT0.WorkPointer, *tt, *ttco = 0, *t1 = 0, ter, *tstop = 0, *t2;
	THREADBUCKET *thr = 0;
	FILEHANDLE *oldoutfile = AR0.outfile;
	GETTERM GetTermP = &GetTerm;
	POSITION eonfile = AS.OldOnFile[e-Expressions];
	numberoffullbuckets = 0;
/*
	Start up all threads. The lock needs to be around the whole loop
	to keep processes from terminating quickly and putting themselves
	in the list of available threads again.
*/
	AM.tracebackflag = 1;

	AS.sLevel = AR0.sLevel;
	LOCK(availabilitylock);
	topofavailables = 0;
	for ( id = 1; id <= numberofworkers; id++ ) {
		WakeupThread(id,STARTNEWEXPRESSION);
	}
	UNLOCK(availabilitylock);
	NewSort(BHEAD0);
	AN0.ninterms = 1;
/*
	Now for redefine
*/
	if ( AC.numpfirstnum > 0 ) {
		for ( j = 0; j < AC.numpfirstnum; j++ ) {
			AC.inputnumbers[j] = -1;
		}
	}
	MasterWaitAll();
/*
	Determine a reasonable bucketsize.
	This is based on the value of AC.ThreadBucketSize and the number
	of terms. We want at least 5 buckets per worker at the moment.
	Some research should show whether this is reasonable.

	The number of terms in the expression is in e->counter
*/
	thrbufsiz2 = thrbufsiz = AC.ThreadBucketSize-1;
	if ( ( e->counter / ( numberofworkers * 5 ) ) < thrbufsiz ) {
		thrbufsiz = e->counter / ( numberofworkers * 5 ) - 1;
		if ( thrbufsiz < 0 ) thrbufsiz = 0;
	}
	thrbufsiz0 = thrbufsiz;
	numpasses = 5; /* this is just for trying */
	thrbufsiz = thrbufsiz0 / (2 << numpasses);
/*
	Mark all buckets as free and take the first.
*/
	for ( j = 0; j < numthreadbuckets; j++ )
		threadbuckets[j]->free = BUCKETFREE;
	thr = threadbuckets[0];
/*
  	#[ Whole brackets :

	First we look whether we have to work with entire brackets
	This is the case when there is a non-NULL address in e->bracketinfo.
	Of course we shouldn't have interference from a collect or keep statement.
*/
#ifdef WHOLEBRACKETS
	if ( e->bracketinfo && AC.CollectFun == 0 && AR0.DeferFlag == 0 ) {
		FILEHANDLE *curfile;
		int didone = 0;
		LONG num, n;
		AN0.expr = e;
		for ( n = 0; n < e->bracketinfo->indexfill; n++ ) {
			num = TreatIndexEntry(B0,n);
			if ( num > 0 ) {
				didone = 1;
/*
				This bracket can be sent off.
				1: Look for an empty bucket
*/
ReTry:;
				for ( j = 0; j < numthreadbuckets; j++ ) {
					switch ( threadbuckets[j]->free ) {
						case BUCKETFREE:
							thr = threadbuckets[j];
							goto Found1;
						case BUCKETCOMINGFREE:
							thr = threadbuckets[j];
							thr->free = BUCKETFREE;
							for ( k = j+1; k < numthreadbuckets; k++ )
								threadbuckets[k-1] = threadbuckets[k];
							threadbuckets[numthreadbuckets-1] = thr;
							j--;
							break;
						default:
							break;
					}
				}
Found1:;
				if ( j < numthreadbuckets ) {
/*
					Found an empty bucket. Fill it.
*/
					thr->firstbracket = n;
					thr->lastbracket = n + num - 1;
					thr->type = BUCKETDOINGBRACKET;
					thr->free = BUCKETFILLED;
					thr->firstterm = AN0.ninterms;
					for ( j = n; j < n+num; j++ ) {
						AN0.ninterms += e->bracketinfo->indexbuffer[j].termsinbracket;
					}
					n += num-1;
					numberoffullbuckets++;
					if ( topofavailables > 0 ) {
						SendOneBucket(DOBRACKETS);
					}
				}
/*
					All buckets are in use.
					Look/wait for an idle worker. Give it a bucket.
					After that, retry for a bucket
*/
				else {
					while ( topofavailables <= 0 ) {
						MasterWait();
					}
					SendOneBucket(DOBRACKETS);
					goto ReTry;
				}
			}
		}
		if ( didone ) {
/*
			And now put the input back in the original position.
*/
			switch ( e->status ) {
				case UNHIDELEXPRESSION:
				case UNHIDEGEXPRESSION:
				case DROPHLEXPRESSION:
				case DROPHGEXPRESSION:
				case HIDDENLEXPRESSION:
				case HIDDENGEXPRESSION:
					curfile = AR0.hidefile;
					break;
				default:
					curfile = AR0.infile;
					break;
			}
			SetScratch(curfile,&eonfile);
			GetTerm(B0,AT0.WorkPointer);
/*
			Now we point the GetTerm that is used to the one that is selective
*/
			GetTermP = &GetTerm2;
/*
			Next wait till there is a bucket available and initialize thr to it.
*/
			for(;;) {
				for ( j = 0; j < numthreadbuckets; j++ ) {
					switch ( threadbuckets[j]->free ) {
						case BUCKETFREE:
							thr = threadbuckets[j];
							goto Found2;
						case BUCKETCOMINGFREE:
							thr = threadbuckets[j];
							thr->free = BUCKETFREE;
							for ( k = j+1; k < numthreadbuckets; k++ )
								threadbuckets[k-1] = threadbuckets[k];
							threadbuckets[numthreadbuckets-1] = thr;
							j--;
							break;
						default:
							break;
					}
				}
				while ( topofavailables <= 0 ) {
					MasterWait();
				}
				while ( topofavailables > 0 && numberoffullbuckets > 0 ) {
					SendOneBucket(DOBRACKETS);
				}
			}
Found2:;
			while ( numberoffullbuckets > 0 ) {
				while ( topofavailables <= 0 ) {
					MasterWait();
				}
				while ( topofavailables > 0 && numberoffullbuckets > 0 ) {
					SendOneBucket(DOBRACKETS);
				}
			}
/*
			Disable the 'warming up' with smaller buckets.

			numpasses = 0;
			thrbufsiz = thrbufsiz0;
*/
			AN0.lastinindex = -1;
		}
		MasterWaitAll();
	}
#endif
/*
  	#] Whole brackets : 

	Now the loop to start a bucket
*/
	for(;;) {
		if ( fromspectator ) {
			ter = GetFromSpectator(thr->threadbuffer,fromspectator-1);
			if ( ter == 0 ) fromspectator = 0;
		}
		else {
			ter = GetTermP(B0,thr->threadbuffer);
/*
			At this point we could check whether the input term is
			just an expression that resides in a scratch file.
			If this is the case we should store the current input info
			(file and buffer content) and redirect the input.
			At the end we can go back to where we were.
			There are two possibilities: we are in the same scratchfile
			as the main input and the other expression is still in the
			input buffer, or we have to do some reading. The reading is
			of course done in GetTerm.

			How to set this up can also be studied in TestSub (in file proces.c)
			where it checks for EXPRESSION.
*/
		}
		if ( ter < 0 ) break;
		if ( ter == 0 ) { endofinput = 1; goto Finalize; }
		dd = AN0.deferskipped;
		if ( AR0.DeferFlag ) {
			defcount = 0;
			thr->deferbuffer[defcount++] = AR0.DefPosition;
			ttco = thr->compressbuffer; t1 = AR0.CompressBuffer; j = *t1;
			NCOPY(ttco,t1,j);
		}
		else if ( first && ( AC.CollectFun == 0 ) ) { /* Brackets ? */
			first = 0;
			t1 = tstop = thr->threadbuffer;
			tstop += *tstop; tstop -= ABS(tstop[-1]);
			t1++;
			while ( t1 < tstop ) {
				if ( t1[0] == HAAKJE ) { bra = 1; break; }
				t1 += t1[1];
			}
			t1 = thr->threadbuffer;
		}
/*
		Check whether we have a collect,function going. If so execute it.
*/
		if ( AC.CollectFun && *(thr->threadbuffer) < (AM.MaxTer/((LONG)sizeof(WORD))-10) ) {
			if ( ( dd = GetMoreTerms(thr->threadbuffer) ) < 0 ) {
				LowerSortLevel(); goto ProcErr;
			}
		}
/*
		Check whether we have a priority task:
*/
		if ( topofavailables > 0 && numberoffullbuckets > 0 ) SendOneBucket(LOWESTLEVELGENERATION);
/*
		Now put more terms in the bucket. Position tt after the first term
*/
		tt = thr->threadbuffer; tt += *tt;
		thr->totnum = 1;
		thr->usenum = 0;
/*
		Next we worry about the 'slow startup' in which we make the initial
		buckets smaller, so that we get all threads busy as soon as possible.
*/
		if ( numpasses > 0 ) {
			numbucket++;
			if ( numbucket >= numberofworkers ) {
				numbucket = 0;
				numpasses--;
				if ( numpasses == 0 ) thrbufsiz = thrbufsiz0;
				else                  thrbufsiz = thrbufsiz0 / (2 << numpasses);
			}
			thrbufsiz2 = thrbufsiz + thrbufsiz/5; /* for completing brackets */
		}
/*
		we have already 1+dd terms
*/
		while ( ( dd < thrbufsiz ) &&
			( tt - thr->threadbuffer ) < ( thr->threadbuffersize - AM.MaxTer/((LONG)sizeof(WORD)) - 2 ) ) {
/*
			First check:
*/
			if ( topofavailables > 0 && numberoffullbuckets > 0 ) SendOneBucket(LOWESTLEVELGENERATION);
/*
			There is room in the bucket. Fill yet another term.
*/
			if ( GetTermP(B0,tt) == 0 ) { endofinput = 1; break; }
			dd++;
			thr->totnum++;
			dd += AN0.deferskipped;
			if ( AR0.DeferFlag ) {
				thr->deferbuffer[defcount++] = AR0.DefPosition;
				t1 = AR0.CompressBuffer; j = *t1;
				NCOPY(ttco,t1,j);
			}
			if ( AC.CollectFun && *tt < (AM.MaxTer/((LONG)sizeof(WORD))-10) ) {
				if ( ( ddd = GetMoreTerms(tt) ) < 0 ) {
					LowerSortLevel(); goto ProcErr;
				}
				dd += ddd;
			}
			t1 = tt;
			tt += *tt;
		}
/*
		Check whether there are regular brackets and if we have no DeferFlag
		and no collect, we try to add more terms till we finish the current
		bracket. We should however not overdo it. Let us say: up to 20%
		more terms are allowed.
*/
		if ( bra ) {
			tstop = t1 + *t1; tstop -= ABS(tstop[-1]);
			t2 = t1+1;
			while ( t2 < tstop ) {
				if ( t2[0] == HAAKJE ) { break; }
				t2 += t2[1];
			}
			if ( t2[0] == HAAKJE ) {
			  t2 += t2[1]; num = t2 - t1;
			  while ( ( dd < thrbufsiz2 ) &&
				( tt - thr->threadbuffer ) < ( thr->threadbuffersize - AM.MaxTer - 2 ) ) {
/*
				First check:
*/
				if ( topofavailables > 0 && numberoffullbuckets > 0 ) SendOneBucket(LOWESTLEVELGENERATION);
/*
				There is room in the bucket. Fill yet another term.
*/
				if ( GetTermP(B0,tt) == 0 ) { endofinput = 1; break; }
/*
				Same bracket?
*/
				tstop = tt + *tt; tstop -= ABS(tstop[-1]);
				if ( tstop-tt < num ) { /* Different: abort */
					AR0.KeptInHold = 1;
					break;
				}
				for ( i = 1; i < num; i++ ) {
					if ( t1[i] != tt[i] ) break;
				}
				if ( i < num ) { /* Different: abort */
					AR0.KeptInHold = 1;
					break;
				}
/*
				Same bracket. We need this term.
*/
				dd++;
				thr->totnum++;
				tt += *tt;
			  }
			}
		}
		thr->ddterms = dd; /* total number of terms including keep brackets */
		thr->firstterm = AN0.ninterms;
		AN0.ninterms += dd;
		*tt = 0;           /* mark end of bucket */
		thr->free = BUCKETFILLED;
		thr->type = BUCKETDOINGTERMS;
		numberoffullbuckets++;
		if ( topofavailables <= 0 && endofinput == 0 ) {
/*
			Problem: topofavailables may already be > 0, but the
			thread has not yet gone into waiting. Can the signal get lost?
			How can we tell that a thread is waiting for a signal?

			All threads are busy. Try to load up another bucket.
			In the future we could be more sophisticated.
			At the moment we load a complete bucket which could be
			1000 terms or even more.
			In principle it is better to keep a full bucket ready
			and check after each term we put in the next bucket. That
			way we don't waste time of the workers.
*/
			for ( j = 0; j < numthreadbuckets; j++ ) {
				switch ( threadbuckets[j]->free ) {
					case BUCKETFREE:
						thr = threadbuckets[j];
						if ( !endofinput ) goto NextBucket;
/*
						If we are at the end of the input we mark
						the free buckets in a special way. That way
						we don't keep running into them.
*/
						thr->free = BUCKETATEND;
						break;
					case BUCKETCOMINGFREE:
						thr = threadbuckets[j];
						thr->free = BUCKETFREE;
/*
						Bucket has just been finished.
						Put at the end of the list. We don't want
						an early bucket to wait to be treated last.
*/
						for ( k = j+1; k < numthreadbuckets; k++ )
							threadbuckets[k-1] = threadbuckets[k];
						threadbuckets[numthreadbuckets-1] = thr;
						j--;   /* we have to redo the same number j. */
						break;
					default:
						break;
				}
			}
/*
			We have no free bucket or we are at the end.
			The only thing we can do now is wait for a worker to come free,
			provided there are still buckets to send.
*/
		}
/*
		Look for the next bucket to send. There is at least one full bucket!
*/
		for ( j = 0; j < numthreadbuckets; j++ ) {
			if ( threadbuckets[j]->free == BUCKETFILLED ) {
				thr = threadbuckets[j];
				for ( k = j+1; k < numthreadbuckets; k++ )
					threadbuckets[k-1] = threadbuckets[k];
				threadbuckets[numthreadbuckets-1] = thr;
				break;
			}
		}
/*
		Wait for a thread to become available
		The bucket we are going to use is in thr.
*/
DoBucket:;
		AN0.ninterms++;
		while ( ( id = GetAvailableThread() ) < 0 ) { MasterWait(); }
/*
		Prepare the thread. Give it the term and variables.
*/
		LoadOneThread(0,id,thr,0);
		LOCK(thr->lock);
		thr->busy = BUCKETASSIGNED;
		UNLOCK(thr->lock);
		thr->free = BUCKETINUSE;
		numberoffullbuckets--;
/*
		And signal the thread to run.
		Form now on we may only interfere with this bucket
		1: after it has been marked BUCKETCOMINGFREE
		2: when thr->busy == BUCKETDOINGTERM and then only when protected by
		   thr->lock. This would be for load balancing.
*/
		WakeupThread(id,LOWESTLEVELGENERATION);
/*		AN0.ninterms += thr->ddterms; */
/*
		Now look whether there is another bucket filled and a worker available
*/
		if ( topofavailables > 0 ) {  /* there is a worker */
			for ( j = 0; j < numthreadbuckets; j++ ) {
				if ( threadbuckets[j]->free == BUCKETFILLED ) {
					thr = threadbuckets[j];
					for ( k = j+1; k < numthreadbuckets; k++ )
						threadbuckets[k-1] = threadbuckets[k];
					threadbuckets[numthreadbuckets-1] = thr;
					goto DoBucket; /* and we found a bucket */
				}
			}
/*
			no bucket is loaded but there is a thread available
			find a bucket to load. If there is none (all are USED or ATEND)
			we jump out of the loop.
*/
			for ( j = 0; j < numthreadbuckets; j++ ) {
				switch ( threadbuckets[j]->free ) {
					case BUCKETFREE:
						thr = threadbuckets[j];
						if ( !endofinput ) goto NextBucket;
						thr->free = BUCKETATEND;
						break;
					case BUCKETCOMINGFREE:
						thr = threadbuckets[j];
						if ( endofinput ) {
							thr->free = BUCKETATEND;
						}
						else {
							thr->free = BUCKETFREE;
							for ( k = j+1; k < numthreadbuckets; k++ )
								threadbuckets[k-1] = threadbuckets[k];
							threadbuckets[numthreadbuckets-1] = thr;
							j--;
						}
						break;
					default:
						break;
				}
			}
			if ( j >= numthreadbuckets ) break;
		}
		else {
/*
			No worker available.
			Look for a bucket to load.
			Its number will be in "still"
*/
Finalize:;
			still = -1;
			for ( j = 0; j < numthreadbuckets; j++ ) {
				switch ( threadbuckets[j]->free ) {
					case BUCKETFREE:
						thr = threadbuckets[j];
						if ( !endofinput ) goto NextBucket;
						thr->free = BUCKETATEND;
						break;
					case BUCKETCOMINGFREE:
						thr = threadbuckets[j];
						if ( endofinput ) thr->free = BUCKETATEND;
						else {
							thr->free = BUCKETFREE;
							for ( k = j+1; k < numthreadbuckets; k++ )
								threadbuckets[k-1] = threadbuckets[k];
							threadbuckets[numthreadbuckets-1] = thr;
							j--;
						}
						break;
					case BUCKETFILLED:
						if ( still < 0 ) still = j;
						break;
					default:
						break;
				}
			}
			if ( still < 0 ) {
/*
				No buckets to be executed and no buckets FREE.
				We must be at the end. Break out of the loop.
*/
				break;
			}
			thr = threadbuckets[still];
			for ( k = still+1; k < numthreadbuckets; k++ )
				threadbuckets[k-1] = threadbuckets[k];
			threadbuckets[numthreadbuckets-1] = thr;
			goto DoBucket;
		}
NextBucket:;
	}
/*
	Now the stage one load balancing.
	If the load has been readjusted we have again filled buckets.
	In that case we jump back in the loop.

	Tricky point: when do the workers see the new value of AT.LoadBalancing?
	It should activate the locks on thr->busy
*/
	if ( AC.ThreadBalancing ) {
		for ( id = 1; id <= numberofworkers; id++ ) {
			AB[id]->T.LoadBalancing = 1;
		}
		if ( LoadReadjusted() ) goto Finalize;
		for ( id = 1; id <= numberofworkers; id++ ) {
			AB[id]->T.LoadBalancing = 0;
		}
	}
	if ( AC.ThreadBalancing ) {
/*
		The AS.Balancing flag should have Generator look for
		free workers and apply the "buro" method.

		There is still a serious problem.
		When for instance a sum_, there may be space created in a local
		compiler buffer for a wildcard substitution or whatever.
		Compiler buffer execution scribble space.....
		This isn't copied along?
		Look up ebufnum. There are 12 places with AddRHS!
		Problem: one process allocates in ebuf. Then term is given to
		other process. It would like to use from this ebuf, but the sender
		finishes first and removes the ebuf (and/or overwrites it).

		Other problem: local $ variables aren't copied along.
*/
		AS.Balancing = 0;
	}
	MasterWaitAll();
	AS.Balancing = 0;
/*
	When we deal with the last expression we can now remove the input
	scratch file. This saves potentially much disk space (up to 1/3)
*/
	if ( LastExpression ) {
		UpdateMaxSize();
		if ( AR0.infile->handle >= 0 ) {
			CloseFile(AR0.infile->handle);
			AR0.infile->handle = -1;
			remove(AR0.infile->name);
			PUTZERO(AR0.infile->POposition);
			AR0.infile->POfill = AR0.infile->POfull = AR0.infile->PObuffer;
		}
	}
/*
	We order the threads to finish in the MasterMerge routine
	It will start with waiting for all threads to finish.
	One could make an administration in which threads that have
	finished can start already with the final sort but
	1: The load balancing should not make this super urgent
	2: It would definitely not be very compatible with the second
	   stage load balancing.
*/
	oldgzipCompress = AR0.gzipCompress;
	AR0.gzipCompress = 0;
	if ( AR0.outtohide ) AR0.outfile = AR0.hidefile;
	if ( MasterMerge() < 0 ) {
		if ( AR0.outtohide ) AR0.outfile = oldoutfile;
		AR0.gzipCompress = oldgzipCompress;
		goto ProcErr;
	}
	if ( AR0.outtohide ) AR0.outfile = oldoutfile;
	AR0.gzipCompress = oldgzipCompress;
/*
	Now wait for all threads to be ready to give them the cleaning up signal.
	With the new MasterMerge routine we can do the cleanup already automatically
	avoiding having to send these signals.
*/
	MasterWaitAll();
	AR0.sLevel--;
	for ( id = 1; id < AM.totalnumberofthreads; id++ ) {
		if ( GetThread(id) > 0 ) WakeupThread(id,CLEANUPEXPRESSION);
	}
	e->numdummies = 0;
	for ( id = 1; id < AM.totalnumberofthreads; id++ ) {
		if ( AB[id]->R.MaxDum - AM.IndDum > e->numdummies )
			e->numdummies = AB[id]->R.MaxDum - AM.IndDum;
		AR0.expchanged |= AB[id]->R.expchanged;
	}
/*
	And wait for all to be clean.
*/
	MasterWaitAll();
	AT0.WorkPointer = oldworkpointer;
	return(0);
ProcErr:;
	return(-1);
}

/*
  	#] ThreadsProcessor : 
  	#[ LoadReadjusted :
*/
/**
 *	This routine does the load readjustment at the end of a module.
 *	It may be that there are still some threads that have a bucket full of
 *	difficult terms. In that case we steal the bucket from such a thread
 *	and redistribute the terms over the available buckets to be sent to
 *	the free threads. As we steal all remaining terms from the bucket
 *	it can happen that eventually the same worker gets some of the terms
 *	back at a later stage.
 *
 *	The only tricky point is the stealing process. We have to do this
 *	without having to send signals or testing locks for each term processed.
 *	The lock is set around thr->busy when AT.LoadBalancing == 1 but
 *	when does the worker see this? (caching?)
 *
 *	Remark: the thr->busy == BUCKETASSIGNED flag is to prevent stealing
 *	from a thread that has not done anything yet.
 */

int LoadReadjusted(void)
{
	ALLPRIVATES *B0 = AB[0];
	THREADBUCKET *thr = 0, *thrtogo = 0;
	int numtogo, numfree, numbusy, n, nperbucket, extra, i, j, u, bus;
	LONG numinput;
	WORD *t1, *c1, *t2, *c2, *t3;
/*
	Start with waiting for at least one free processor.
	We don't want the master competing for time when all are busy.
*/
	while ( topofavailables <= 0 ) MasterWait();
/*
	Now look for the fullest bucket and make a list of free buckets
	The bad part is that most numbers can change at any moment.
*/
restart:;
	numtogo = 0;
	numfree = 0;
	numbusy = 0;
	for ( j = 0; j < numthreadbuckets; j++ ) {
		thr = threadbuckets[j];
		if ( thr->free == BUCKETFREE || thr->free == BUCKETATEND
		|| thr->free == BUCKETCOMINGFREE ) {
			freebuckets[numfree++] = thr;
		}
		else if ( thr->type != BUCKETDOINGTERMS ) {}
		else if ( thr->totnum > 1 ) { /* never steal from a bucket with one term */
			LOCK(thr->lock);
			bus = thr->busy;
			UNLOCK(thr->lock);
			if ( thr->free == BUCKETINUSE ) {
				n = thr->totnum-thr->usenum;
				if ( bus == BUCKETASSIGNED ) numbusy++;
				else if ( ( bus != BUCKETASSIGNED )
					   && ( n > numtogo ) ) {
					numtogo = n;
					thrtogo = thr;
				}
			}
			else if ( bus == BUCKETTOBERELEASED
			 && thr->free == BUCKETRELEASED ) {
				freebuckets[numfree++] = thr;
				thr->free = BUCKETATEND;
				LOCK(thr->lock);
				thr->busy = BUCKETPREPARINGTERM;
				UNLOCK(thr->lock);
			}
		}
	}
	if ( numfree == 0 ) return(0); /* serious problem */
	if ( numtogo > 0 ) {   /* provisionally there is something to be stolen */
		thr = thrtogo;
/*
		If the number has changed there is good progress.
		Maybe there is another thread that needs assistance.
		We start all over.
*/
		if ( thr->totnum-thr->usenum < numtogo ) goto restart;
/*
		If the thread is in the term loading phase
		(thr->busy == BUCKETPREPARINGTERM) we better stay away from it.
		We wait now for the thread to be busy, and don't allow it
		now to drop out of this state till we are done here.
		This all depends on whether AT.LoadBalancing == 1 is seen by
		the thread.
*/
		LOCK(thr->lock);
		if ( thr->busy != BUCKETDOINGTERM ) {
			UNLOCK(thr->lock);
			goto restart;
		}
		if ( thr->totnum-thr->usenum < numtogo ) {
			UNLOCK(thr->lock);
			goto restart;
		}
		thr->free = BUCKETTERMINATED;
/*
		The above will signal the thread we want to terminate.
		Next all effort goes into making sure the landing is soft.
		Unfortunately we don't want to wait for a signal, because the thread
		may be working for a long time on a single term.
*/
		if ( thr->usenum == thr->totnum ) {
/*
			Terminated in the mean time or by now working on the
			last term. Try again.
*/
			thr->free = BUCKETATEND;
			UNLOCK(thr->lock);
			goto restart;
		}
		goto intercepted;
	}
/* This has always been commented. Indeed no lock is held here. */
/*	UNLOCK(thr->lock); */
	if ( numbusy > 0 ) {
		/* JD: this avoids large runtimes for tform tests under valgrind.
		   What seems to happen is we return from here, goto Finalize, and
		   end up in LoadReadjusted again without the threads having a
		   chance to update their busy status. Then we end up here again.
		   Sleep the thread for, say, 1us to allow threads to aquire the lock. */
		struct timespec sleeptime;
		sleeptime.tv_sec = 0;
		sleeptime.tv_nsec = 1000L;
		nanosleep(&sleeptime, NULL);
		return(1); /* Wait a bit.... */
	}
	return(0);
intercepted:;
/*
	We intercepted one successfully. Now it becomes interesting. Action:
	1: determine how many terms per free bucket.
	2: find the first untreated term.
	3: put the terms in the free buckets.

	Remember: we still have the lock to avoid interference from the thread
	that is being robbed. We were holding it and then jumped here with
	goto intercepted.
*/
	numinput = thr->firstterm + thr->usenum;
	nperbucket = numtogo / numfree;
	extra = numtogo - nperbucket*numfree;
	if ( AR0.DeferFlag ) {
		t1 = thr->threadbuffer; c1 = thr->compressbuffer; u = thr->usenum;
		for ( n = 0; n < thr->usenum; n++ ) { t1 += *t1; c1 += *c1; }
		t3 = t1;
		if ( extra > 0 ) {
		  for ( i = 0; i < extra; i++ ) {
			thrtogo = freebuckets[i];
			t2 = thrtogo->threadbuffer;
			c2 = thrtogo->compressbuffer;
			thrtogo->free = BUCKETFILLED;
			thrtogo->type = BUCKETDOINGTERMS;
			thrtogo->totnum = nperbucket+1;
			thrtogo->ddterms = 0;
			thrtogo->usenum = 0;
			thrtogo->busy = BUCKETASSIGNED;
			thrtogo->firstterm = numinput;
			numinput += nperbucket+1;
			for ( n = 0; n <= nperbucket; n++ ) {
				j = *t1; NCOPY(t2,t1,j);
				j = *c1; NCOPY(c2,c1,j);
				thrtogo->deferbuffer[n] = thr->deferbuffer[u++];
			}
			*t2 = *c2 = 0;
		  }
		}
		if ( nperbucket > 0 ) {
		  for ( i = extra; i < numfree; i++ ) {
			thrtogo = freebuckets[i];
			t2 = thrtogo->threadbuffer;
			c2 = thrtogo->compressbuffer;
			thrtogo->free = BUCKETFILLED;
			thrtogo->type = BUCKETDOINGTERMS;
			thrtogo->totnum = nperbucket;
			thrtogo->ddterms = 0;
			thrtogo->usenum = 0;
			thrtogo->busy = BUCKETASSIGNED;
			thrtogo->firstterm = numinput;
			numinput += nperbucket;
			for ( n = 0; n < nperbucket; n++ ) {
				j = *t1; NCOPY(t2,t1,j);
				j = *c1; NCOPY(c2,c1,j);
				thrtogo->deferbuffer[n] = thr->deferbuffer[u++];
			}
			*t2 = *c2 = 0;
		  }
		}
	}
	else {
		t1 = thr->threadbuffer;
		for ( n = 0; n < thr->usenum; n++ ) { t1 += *t1; }
		t3 = t1;
		if ( extra > 0 ) {
		  for ( i = 0; i < extra; i++ ) {
			thrtogo = freebuckets[i];
			t2 = thrtogo->threadbuffer;
			thrtogo->free = BUCKETFILLED;
			thrtogo->type = BUCKETDOINGTERMS;
			thrtogo->totnum = nperbucket+1;
			thrtogo->ddterms = 0;
			thrtogo->usenum = 0;
			thrtogo->busy = BUCKETASSIGNED;
			thrtogo->firstterm = numinput;
			numinput += nperbucket+1;
			for ( n = 0; n <= nperbucket; n++ ) {
				j = *t1; NCOPY(t2,t1,j);
			}
			*t2 = 0;
		  }
		}
		if ( nperbucket > 0 ) {
		  for ( i = extra; i < numfree; i++ ) {
			thrtogo = freebuckets[i];
			t2 = thrtogo->threadbuffer;
			thrtogo->free = BUCKETFILLED;
			thrtogo->type = BUCKETDOINGTERMS;
			thrtogo->totnum = nperbucket;
			thrtogo->ddterms = 0;
			thrtogo->usenum = 0;
			thrtogo->busy = BUCKETASSIGNED;
			thrtogo->firstterm = numinput;
			numinput += nperbucket;
			for ( n = 0; n < nperbucket; n++ ) {
				j = *t1; NCOPY(t2,t1,j);
			}
			*t2 = 0;
		  }
		}
	}
	*t3 = 0;   /* This is some form of extra insurance */
	if ( thr->free == BUCKETRELEASED && thr->busy == BUCKETTOBERELEASED ) {
		thr->free = BUCKETATEND; thr->busy = BUCKETPREPARINGTERM;
	}
	UNLOCK(thr->lock);
	return(1);
}

/*
  	#] LoadReadjusted : 
  	#[ SortStrategy :
*/
/**
 *	When the final sort to the scratch file should take place
 *	in a thread we should redirect to a different PutOut say PutToMaster.
 *	The buffer in the Master should be an integer number times the size
 *	of the buffer for PutToMaster (the PObuffersize in the 'scratchfile').
 *	The action should be (assume the multiple is 3):
 *		Once the worker has its buffer full it fills block 1. Next 2. etc.
 *		After filling block 3 the next fill will be at 1 when it is available
 *		again. Becarefull to have a locked variable that indicates whether the
 *		Master has started to claim all blocks 1.
 *		The Master starts working once all blocks 1 are full.
 *		Each Worker has an array for the blocks that tells their status. ???
 *		(Maybe better the lock on the whole block).
 *		There should be a lock on them. The locks will make the threads
 *		wait properly. When the Master finished a block, it marks it as
 *		empty. When the master reaches the end of the last block it moves
 *		the remainder to the piece before block 1. Etc.
 *		Once terminated the worker can do the same as currently after
 *		the call to EndSort (leave control to the master).
 *		The master starts after there is a signal that all blocks 1 have
 *		been filled. The tricky point is this signal without having
 *		threads spend time in a waiting loop.
 *	Don't compress the terms. It costs more time and serves here no real
 *	purpose. It only makes things slower for the master.
 *
 *	At the moment the scratch buffer of the workers is 1/N times the scratch
 *	buffer of the master which is usually about the size of the Large buffer
 *	of the master. This way we can save a factor on the scratch buffer size
 *	of the workers. Alternative: let PutToMaster write directly into the
 *	buffer/block of the master and leave out the scratch of the worker
 *	completely.
*/
/*
  	#] SortStrategy : 
  	#[ PutToMaster :
*/
/**
 *		Writes the term (uncompressed) to the masters buffers.
 *		We put it inside a block. The blocks have locks. This makes
 *		that we have to wait automatically when all blocks are full.
 *		This routine takes the place of PutOut when making the final
 *		sort in a thread.
 *		It takes the place of FlushOut when the argument is NULL.
 *
 *		We need an initialization first in which the first MasterBlockLock
 *		is set and MasterBlock is set to 1.
 *		At the end we need to unlock the last block. Both actions can
 *		be done in the routine that calls EndSort for the thread.
 *
 *		The initialization of the variables in SB is done in
 *		IniSortBlocks. This is done only once but it has to wait till
 *		all threads exist and the masters sort buffers have been allocated.
 *
 *		Note: the zero block is reserved for leftovers at the end of the
 *		last block that get moved back to the front to keep the terms
 *		contiguous (done in MasterMerge).
 */

int PutToMaster(PHEAD WORD *term)
{
	int i,j,nexti,ret = 0;
	WORD *t, *fill, *top, zero = 0;
	if ( term == 0 ) { /* Mark the end of the expression */
		t = &zero; j = 1;
	}
	else {
		t = term; ret = j = *term;
		if ( j == 0 ) { j = 1; } /* Just in case there is a spurious end */
	}
	i = AT.SB.FillBlock;          /* The block we are working at */
	fill = AT.SB.MasterFill[i];     /* Where we are filling */
	top = AT.SB.MasterStop[i];      /* End of the block */
	while ( j > 0 ) {
		while ( j > 0 && fill < top ) {
			*fill++ = *t++; j--;
		}
		if ( j > 0 ) {
/*
			We reached the end of the block.
			Get the next block and release this block.
			The order is important. This is why there should be at least
			4 blocks or deadlocks can occur.
*/
			nexti = i+1;
			if ( nexti > AT.SB.MasterNumBlocks ) {
				nexti = 1;
			}
			LOCK(AT.SB.MasterBlockLock[nexti]);
			UNLOCK(AT.SB.MasterBlockLock[i]);
			AT.SB.MasterFill[i] = AT.SB.MasterStart[i];
			AT.SB.FillBlock = i = nexti;
			fill = AT.SB.MasterStart[i];
			top = AT.SB.MasterStop[i];
		}
	}
	AT.SB.MasterFill[i] = fill;
	return(ret);
}

/*
  	#] PutToMaster : 
  	#[ SortBotOut :
*/
 
#ifdef WITHSORTBOTS

/**
 *		This is the output routine of the SortBots.
 *		It can run PutToMaster, except for the final merge.
 *		In that case we need to do special things like calling PutOut.
 *		Hence the first thing we have to do is to figure out where our
 *		output should be going.
 */

int
SortBotOut(PHEAD WORD *term)
{
	WORD im;

	if ( AT.identity != 0 ) return(PutToMaster(BHEAD term));

	if ( term == 0 ) {
		if ( FlushOut(&SortBotPosition,AR.outfile,1) ) return(-1);
		ADDPOS(AT.SS->SizeInFile[0],1);
		return(0);
	}
	else {
		numberofterms++;
		if ( ( im = PutOut(BHEAD term,&SortBotPosition,AR.outfile,1) ) < 0 ) {
			MLOCK(ErrorMessageLock);
			MesPrint("Called from MasterMerge/SortBotOut");
			MUNLOCK(ErrorMessageLock);
			return(-1);
		}
		ADDPOS(AT.SS->SizeInFile[0],im);
		return(im);
	}
}

#endif

/*
  	#] SortBotOut : 
  	#[ MasterMerge :
*/
/**
 *	This is the routine in which the master merges the sorted output that
 *	comes from the workers. It is similar to MergePatches in sort.c from which
 *	it takes much code.
 *	The important concept here is that we want the master to be working as
 *	little as possible because it constitutes the bottleneck.
 *	The workers fill the buffers of the master. These buffers are divided
 *	into parts for each worker as is done with the file patches in MergePatches
 *	but now also each worker part is divided into blocks. This allows the
 *	worker to fill blocks while the master is already working on blocks that
 *	were filled before. The blocks are arranged in a circular fashion.
 *	The whole is controlled by locks which seems faster than setting it up
 *	with signals.
 *
 *	This routine is run by the master when we don't use the sortbots.
 */

int MasterMerge(void)
{
	ALLPRIVATES *B0 = AB[0], *B = 0;
	SORTING *S = AT0.SS;
	WORD **poin, **poin2, ul, k, i, im, *m1, j;
	WORD lpat, mpat, level, l1, l2, r1, r2, r3, c;
	WORD *m2, *m3, r31, r33, ki, *rr;
	UWORD *coef;
	POSITION position;
	FILEHANDLE *fin, *fout;
#ifdef WITHSORTBOTS
	if ( numberofworkers > 2 ) return(SortBotMasterMerge());
#endif
	fin = &S->file;
	if ( AR0.PolyFun == 0 ) { S->PolyFlag = 0; }
	else if ( AR0.PolyFunType == 1 ) { S->PolyFlag = 1; }
	else if ( AR0.PolyFunType == 2 ) {
		if ( AR0.PolyFunExp == 2
		  || AR0.PolyFunExp == 3 ) S->PolyFlag = 1;
		else                       S->PolyFlag = 2;
	}
	S->TermsLeft = 0;
	coef = AN0.SoScratC;
	poin = S->poina; poin2 = S->poin2a;
	rr = AR0.CompressPointer;
	*rr = 0;
/*
 		#[ Setup :
*/
	S->inNum = numberofthreads;
	fout = AR0.outfile;
/*
	Load the patches. The threads have to finish their sort first.
*/
	S->lPatch = S->inNum - 1;
/*
	Claim all zero blocks. We need them anyway.
	In principle the workers should never get into these.
	We also claim all last blocks. This is a safety procedure that
	should prevent the workers from working their way around the clock
	before the master gets started again.
*/
	AS.MasterSort = 1;
	numberclaimed = 0;
	for ( i = 1; i <= S->lPatch; i++ ) {
		B = AB[i];
		LOCK(AT.SB.MasterBlockLock[0]);
		LOCK(AT.SB.MasterBlockLock[AT.SB.MasterNumBlocks]);
	}
/*
	Now wake up the threads and have them start their final sorting.
	They should start with claiming their block and the master is
	not allowed to continue until that has been done.
	This waiting of the master will be done below in MasterWaitAllBlocks
*/
	for ( i = 0; i < S->lPatch; i++ ) {
		GetThread(i+1);
		WakeupThread(i+1,FINISHEXPRESSION);
	}
/*
	Prepare the output file.
*/
	if ( fout->handle >= 0 ) {
		PUTZERO(position);
		SeekFile(fout->handle,&position,SEEK_END);
		ADDPOS(position,((fout->POfill-fout->PObuffer)*sizeof(WORD)));
	}
	else {
		SETBASEPOSITION(position,(fout->POfill-fout->PObuffer)*sizeof(WORD));
	}
/*
	Wait for all threads to finish loading their first block.
*/
	MasterWaitAllBlocks();
/*
	Claim all first blocks.
	We don't release the last blocks.
	The strategy is that we always keep the previous block.
	In principle it looks like it isn't needed for the last block but
	actually it is to keep the front from overrunning the tail when writing.
*/
	for ( i = 1; i <= S->lPatch; i++ ) {
		B = AB[i];
		LOCK(AT.SB.MasterBlockLock[1]);
		AT.SB.MasterBlock = 1;
	}
/*
 		#] Setup : 

	Now construct the tree:
*/
	lpat = 1;
	do { lpat <<= 1; } while ( lpat < S->lPatch );
	mpat = ( lpat >> 1 ) - 1;
	k = lpat - S->lPatch;
/*
	k is the number of empty places in the tree. they will
	be at the even positions from 2 to 2*k
*/
	for ( i = 1; i < lpat; i++ ) { S->tree[i] = -1; }
	for ( i = 1; i <= k; i++ ) {
		im = ( i * 2 ) - 1;
		poin[im] = AB[i]->T.SB.MasterStart[AB[i]->T.SB.MasterBlock];
		poin2[im] = poin[im] + *(poin[im]);
		S->used[i] = im;
		S->ktoi[im] = i-1;
		S->tree[mpat+i] = 0;
		poin[im-1] = poin2[im-1] = 0;
	}
	for ( i = (k*2)+1; i <= lpat; i++ ) {
		S->used[i-k] = i;
		S->ktoi[i] = i-k-1;
		poin[i] = AB[i-k]->T.SB.MasterStart[AB[i-k]->T.SB.MasterBlock];
		poin2[i] = poin[i] + *(poin[i]);
	}
/*
	the array poin tells the position of the i-th element of the S->tree
	'S->used' is a stack with the S->tree elements that need to be entered
	into the S->tree. at the beginning this is S->lPatch. during the
	sort there will be only very few elements.
	poin2 is the next value of poin. it has to be determined
	before the comparisons as the position or the size of the
	term indicated by poin may change.
	S->ktoi translates a S->tree element back to its stream number.

	start the sort
*/
	level = S->lPatch;
/*
	introduce one term
*/
OneTerm:
	k = S->used[level];
	i = k + lpat - 1;
	if ( !*(poin[k]) ) {
		do { if ( !( i >>= 1 ) ) goto EndOfMerge; } while ( !S->tree[i] );
		if ( S->tree[i] == -1 ) {
			S->tree[i] = 0;
			level--;
			goto OneTerm;
		}
		k = S->tree[i];
		S->used[level] = k;
		S->tree[i] = 0;
	}
/*
	move terms down the tree
*/
	while ( i >>= 1 ) {
		if ( S->tree[i] > 0 ) {
			if ( ( c = CompareTerms(B0, poin[S->tree[i]],poin[k],(WORD)0) ) > 0 ) {
/*
				S->tree[i] is the smaller. Exchange and go on.
*/
				S->used[level] = S->tree[i];
				S->tree[i] = k;
				k = S->used[level];
			}
			else if ( !c ) {	/* Terms are equal */
/*
				S->TermsLeft--;
					Here the terms are equal and their coefficients
					have to be added.
*/
				l1 = *( m1 = poin[S->tree[i]] );
				l2 = *( m2 = poin[k] );
				if ( S->PolyWise ) {  /* Here we work with PolyFun */
					WORD *tt1, *w;
					tt1 = m1;
					m1 += S->PolyWise;
					m2 += S->PolyWise;
					if ( S->PolyFlag == 2 ) {
						w = poly_ratfun_add(B0,m1,m2);
						if ( *tt1 + w[1] - m1[1] > AM.MaxTer/((LONG)sizeof(WORD)) ) {
							MLOCK(ErrorMessageLock);
							MesPrint("Term too complex in PolyRatFun addition. MaxTermSize of %10l is too small",AM.MaxTer);
							MUNLOCK(ErrorMessageLock);
							Terminate(-1);
						}
						AT0.WorkPointer = w;
						if ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 && w[1] > FUNHEAD ) {
							goto cancelled;
						}
					}
					else {
						w = AT0.WorkPointer;
						if ( w + m1[1] + m2[1] > AT0.WorkTop ) {
							MLOCK(ErrorMessageLock);
							MesPrint("MasterMerge: A WorkSpace of %10l is too small",AM.WorkSize);
							MUNLOCK(ErrorMessageLock);
							Terminate(-1);
						}
						AddArgs(B0,m1,m2,w);
					}
					r1 = w[1];
					if ( r1 <= FUNHEAD
						|| ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) )
							 { goto cancelled; }
					if ( r1 == m1[1] ) {
						NCOPY(m1,w,r1);
					}
					else if ( r1 < m1[1] ) {
						r2 = m1[1] - r1;
						m2 = w + r1;
						m1 += m1[1];
						while ( --r1 >= 0 ) *--m1 = *--m2;
						m2 = m1 - r2;
						r1 = S->PolyWise;
						while ( --r1 >= 0 ) *--m1 = *--m2;
						*m1 -= r2;
						poin[S->tree[i]] = m1;
					}
					else {
						r2 = r1 - m1[1];
						m2 = tt1 - r2;
						r1 = S->PolyWise;
						m1 = tt1;
						*m1 += r2;
						poin[S->tree[i]] = m2;
						NCOPY(m2,m1,r1);
						r1 = w[1];
						NCOPY(m2,w,r1);
					}
				}
#ifdef WITHFLOAT
				else if ( AT.SortFloatMode ) {
					WORD *term1, *term2;
					term1 = poin[S->tree[i]];
					term2 = poin[k];
					if ( MergeWithFloat(B0, &term1,&term2) == 0 )
						goto cancelled;
					poin[S->tree[i]] = term1;
				}
#endif
				else {
					r1 = *( m1 += l1 - 1 );
					m1 -= ABS(r1) - 1;
					r1 = ( ( r1 > 0 ) ? (r1-1) : (r1+1) ) >> 1;
					r2 = *( m2 += l2 - 1 );
					m2 -= ABS(r2) - 1;
					r2 = ( ( r2 > 0 ) ? (r2-1) : (r2+1) ) >> 1;

					if ( AddRat(B0,(UWORD *)m1,r1,(UWORD *)m2,r2,coef,&r3) ) {
						MLOCK(ErrorMessageLock);
						MesCall("MasterMerge");
						MUNLOCK(ErrorMessageLock);
						SETERROR(-1)
					}

					if ( AN.ncmod != 0 ) {
						if ( ( AC.modmode & POSNEG ) != 0 ) {
							NormalModulus(coef,&r3);
						}
						else if ( BigLong(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
							WORD ii;
							SubPLon(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod),coef,&r3);
							coef[r3] = 1;
							for ( ii = 1; ii < r3; ii++ ) coef[r3+ii] = 0;
						}
					}
					r3 *= 2;
					r33 = ( r3 > 0 ) ? ( r3 + 1 ) : ( r3 - 1 );
					if ( r3 < 0 ) r3 = -r3;
					if ( r1 < 0 ) r1 = -r1;
					r1 *= 2;
					r31 = r3 - r1;
					if ( !r3 ) {		/* Terms cancel */
cancelled:
						ul = S->used[level] = S->tree[i];
						S->tree[i] = -1;
/*
						We skip to the next term in stream ul
*/
						im = *poin2[ul];
						poin[ul] = poin2[ul];
						ki = S->ktoi[ul];
						if ( (poin[ul] + im + COMPINC) >=
						AB[ki+1]->T.SB.MasterStop[AB[ki+1]->T.SB.MasterBlock]
						&& im > 0 ) {
/*
							We made it to the end of the block. We have to
							release the previous block and claim the next.
*/
							B = AB[ki+1];
							i = AT.SB.MasterBlock;
							if ( i == 1 ) {
								UNLOCK(AT.SB.MasterBlockLock[AT.SB.MasterNumBlocks]);
							}
							else {
								UNLOCK(AT.SB.MasterBlockLock[i-1]);
							}
							if ( i == AT.SB.MasterNumBlocks ) {
/*
								Move the remainder down into block 0
*/
								WORD *from, *to;
								to = AT.SB.MasterStart[1];
								from = AT.SB.MasterStop[i];
								while ( from > poin[ul] ) *--to = *--from;
								poin[ul] = to;
								i = 1;
							}
							else { i++; }
							LOCK(AT.SB.MasterBlockLock[i]);
							AT.SB.MasterBlock = i;
							poin2[ul] = poin[ul] + im;
						}
						else {
							poin2[ul] += im;
						}
						S->used[++level] = k;
					}
					else if ( !r31 ) {		/* copy coef into term1 */
						goto CopCof2;
					}
					else if ( r31 < 0 ) {		/* copy coef into term1
											and adjust the length of term1 */
						goto CopCoef;
					}
					else {
/*
							this is the dreaded calamity.
							is there enough space?
*/
						if( (poin[S->tree[i]]+l1+r31) >= poin2[S->tree[i]] ) {
/*
								no space! now the special trick for which
								we left 2*maxlng spaces open at the beginning
								of each patch.
*/
							if ( (l1 + r31)*((LONG)sizeof(WORD)) >= AM.MaxTer ) {
								MLOCK(ErrorMessageLock);
								MesPrint("MasterMerge: Coefficient overflow during sort");
								MUNLOCK(ErrorMessageLock);
								goto ReturnError;
							}
							m2 = poin[S->tree[i]];
							m3 = ( poin[S->tree[i]] -= r31 );
							do { *m3++ = *m2++; } while ( m2 < m1 );
							m1 = m3;
						}
CopCoef:
						*(poin[S->tree[i]]) += r31;
CopCof2:
						m2 = (WORD *)coef; im = r3;
						NCOPY(m1,m2,im);
						*m1 = r33;
					}
				}
/*
				Now skip to the next term in stream k.
*/
NextTerm:
				im = poin2[k][0];
				poin[k] = poin2[k];
				ki = S->ktoi[k];
				if ( (poin[k] + im + COMPINC) >=
				AB[ki+1]->T.SB.MasterStop[AB[ki+1]->T.SB.MasterBlock]
				&& im > 0 ) {
/*
				We made it to the end of the block. We have to
				release the previous block and claim the next.
*/
					B = AB[ki+1];
					i = AT.SB.MasterBlock;
					if ( i == 1 ) {
						UNLOCK(AT.SB.MasterBlockLock[AT.SB.MasterNumBlocks]);
					}
					else {
						UNLOCK(AT.SB.MasterBlockLock[i-1]);
					}
					if ( i == AT.SB.MasterNumBlocks ) {
/*
						Move the remainder down into block 0
*/
						WORD *from, *to;
						to = AT.SB.MasterStart[1];
						from = AT.SB.MasterStop[i];
						while ( from > poin[k] ) *--to = *--from;
						poin[k] = to;
						i = 1;
					}
					else { i++; }
					LOCK(AT.SB.MasterBlockLock[i]);
					AT.SB.MasterBlock = i;
					poin2[k] = poin[k] + im;
				}
				else {
					poin2[k] += im;
				}
				goto OneTerm;
			}
		}
		else if ( S->tree[i] < 0 ) {
			S->tree[i] = k;
			level--;
			goto OneTerm;
		}
	}
/*
	found the smallest in the set. indicated by k.
	write to its destination.
*/
	S->TermsLeft++;
	if ( ( im = PutOut(B0,poin[k],&position,fout,1) ) < 0 ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Called from MasterMerge with k = %d (stream %d)",k,S->ktoi[k]);
		MUNLOCK(ErrorMessageLock);
		goto ReturnError;
	}
	ADDPOS(S->SizeInFile[0],im);
	goto NextTerm;
EndOfMerge:
	if ( FlushOut(&position,fout,1) ) goto ReturnError;
	ADDPOS(S->SizeInFile[0],1);
	CloseFile(fin->handle);
	remove(fin->name);
	fin->handle = -1;
	position = S->SizeInFile[0];
	MULPOS(position,sizeof(WORD));
	S->GenTerms = 0;
	for ( j = 1; j <= numberofworkers; j++ ) {
		S->GenTerms += AB[j]->T.SS->GenTerms;
	}
	WriteStats(&position,STATSPOSTSORT,NOCHECKLOGTYPE);
	Expressions[AR0.CurExpr].counter = S->TermsLeft;
	Expressions[AR0.CurExpr].size = position;
/*
	Release all locks
*/
	for ( i = 1; i <= S->lPatch; i++ ) {
		B = AB[i];
		UNLOCK(AT.SB.MasterBlockLock[0]);
		if ( AT.SB.MasterBlock == 1 ) {
			UNLOCK(AT.SB.MasterBlockLock[AT.SB.MasterNumBlocks]);
		}
		else {
			UNLOCK(AT.SB.MasterBlockLock[AT.SB.MasterBlock-1]);
		}
		UNLOCK(AT.SB.MasterBlockLock[AT.SB.MasterBlock]);
	}
	AS.MasterSort = 0;
	return(0);
ReturnError:
	for ( i = 1; i <= S->lPatch; i++ ) {
		B = AB[i];
		UNLOCK(AT.SB.MasterBlockLock[0]);
		if ( AT.SB.MasterBlock == 1 ) {
			UNLOCK(AT.SB.MasterBlockLock[AT.SB.MasterNumBlocks]);
		}
		else {
			UNLOCK(AT.SB.MasterBlockLock[AT.SB.MasterBlock-1]);
		}
		UNLOCK(AT.SB.MasterBlockLock[AT.SB.MasterBlock]);
	}
	AS.MasterSort = 0;
	return(-1);
}

/*
  	#] MasterMerge : 
  	#[ SortBotMasterMerge :
*/
 
#ifdef WITHSORTBOTS

/**
 *	This is the master routine for the final stage in a sortbot merge.
 *	A sortbot merge is a merge in which the output of two workers is
 *	merged into a single output which then can be given as one of two
 *	streams to another sortbot. The idea is that each sortbot is responsible
 *	for one one compare per term. In the end the master does the last
 *	merge of only two streams and writes the result to the output.
 *	There doesn't seem to be an advantage to splitting this last task.
 *
 *	The use of the sortbots gives a measurable improvement but it isn't
 *	optimal yet.
 *
 *	This routine is run as master. Hence B = B0. Etc.
 */

int SortBotMasterMerge(void)
{
	FILEHANDLE *fin, *fout;
	ALLPRIVATES *B = AB[0], *BB;
	POSITION position;
	SORTING *S = AT.SS;
	int i, j;
/*
	Get the sortbots get to claim their writing blocks.
	We have to wait till all have been claimed because they also have to
	claim the last writing blocks of the workers to prevent the head of
	the circular buffer to overrun the tail.

	Before waiting we can do some needed initializations.
	Also the master has to claim the last writing blocks of its input.
*/
	topsortbotavailables = 0;
	for ( i = numberofworkers+1; i <= numberofworkers+numberofsortbots; i++ ) {
		WakeupThread(i,INISORTBOT);
	}

	AS.MasterSort = 1;
	fout = AR.outfile;
	numberofterms = 0;
	AR.CompressPointer[0] = 0;
	numberclaimed = 0;
	BB = AB[AT.SortBotIn1];
	LOCK(BB->T.SB.MasterBlockLock[BB->T.SB.MasterNumBlocks]);
	BB = AB[AT.SortBotIn2];
	LOCK(BB->T.SB.MasterBlockLock[BB->T.SB.MasterNumBlocks]);

	MasterWaitAllSortBots();
/*
	Now we can start up the workers. They will claim their writing blocks.
	Here the master will wait till all writing blocks have been claimed.
*/
	for ( i = 1; i <= numberofworkers; i++ ) {
		j = GetThread(i);
		WakeupThread(i,FINISHEXPRESSION);
	}
/*
	Prepare the output file in the mean time.
*/
	if ( fout->handle >= 0 ) {
		PUTZERO(SortBotPosition);
		SeekFile(fout->handle,&SortBotPosition,SEEK_END);
		ADDPOS(SortBotPosition,((fout->POfill-fout->PObuffer)*sizeof(WORD)));
	}
	else {
		SETBASEPOSITION(SortBotPosition,(fout->POfill-fout->PObuffer)*sizeof(WORD));
	}
	MasterWaitAllBlocks();
/*
	Now we can start the sortbots after which the master goes in
	sortbot mode to do its part of the job (the very final merge and
	the writing to output file).
*/
	topsortbotavailables = 0;
	for ( i = numberofworkers+1; i <= numberofworkers+numberofsortbots; i++ ) {
		WakeupThread(i,RUNSORTBOT);
	}
	if ( SortBotMerge(BHEAD0) ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Called from SortBotMasterMerge");
		MUNLOCK(ErrorMessageLock);
		AS.MasterSort = 0;
		return(-1);
	}
/*
	And next the cleanup
*/
	if ( S->file.handle >= 0 )
	{
		fin = &S->file;
		CloseFile(fin->handle);
		remove(fin->name);
		fin->handle = -1;
	}
	position = S->SizeInFile[0];
	MULPOS(position,sizeof(WORD));
	S->GenTerms = 0;
	for ( j = 1; j <= numberofworkers; j++ ) {
		S->GenTerms += AB[j]->T.SS->GenTerms;
	}
	S->TermsLeft = numberofterms;
	WriteStats(&position,STATSPOSTSORT,NOCHECKLOGTYPE);
	Expressions[AR.CurExpr].counter = S->TermsLeft;
	Expressions[AR.CurExpr].size = position;
	AS.MasterSort = 0;
/*
	The next statement is to prevent one of the sortbots not having
	completely cleaned up before the next module starts.
	If this statement is omitted every once in a while one of the sortbots
	is still running when the next expression starts and misses its
	initialization. The result is usually disastrous.
*/
	MasterWaitAllSortBots();

	return(0);
}

#endif

/*
  	#] SortBotMasterMerge : 
  	#[ SortBotMerge :
*/
 
#ifdef WITHSORTBOTS

/**
 *	This routine is run by a sortbot and merges two sorted output streams into
 *	a single sorted stream.
 */

int SortBotMerge(PHEAD0)
{
	GETBIDENTITY
	ALLPRIVATES *Bin1 = AB[AT.SortBotIn1],*Bin2 = AB[AT.SortBotIn2];
	WORD *term1, *term2, *next, *wp;
	int blin1, blin2;	/* Current block numbers */
	int error = 0;
	WORD l1, l2, *m1, *m2, *w, r1, r2, r3, r33, r31, *tt1, ii;
	WORD *to, *from, im, c;
	UWORD *coef;
	SORTING *S = AT.SS;
#ifdef WITHFLOAT
	WORD *fun1, *fun2, *fun3, *tstop1, *tstop2, size1, size2, size3, l3, jj, *m3;
#endif
/*
	Set the pointers to the input terms and the output space
*/
	coef = AN.SoScratC;
	blin1 = 1;
	blin2 = 1;
	if ( AT.identity == 0 ) {
		wp = AT.WorkPointer;
	}
	else {
		wp = AT.WorkPointer = AT.WorkSpace;
	}
/*
	Get the locks for reading the input
	This means that we can start once these locks have been cleared
	which means that there will be input.
*/
	LOCK(Bin1->T.SB.MasterBlockLock[blin1]);
	LOCK(Bin2->T.SB.MasterBlockLock[blin2]);

	term1 = Bin1->T.SB.MasterStart[blin1];
	term2 = Bin2->T.SB.MasterStart[blin2];
	AT.SB.FillBlock = 1;
/*
	Now the main loop. Keep going until one of the two hits the end.
*/
	while ( *term1 && *term2 ) {
		if ( ( c = CompareTerms(BHEAD term1,term2,(WORD)0) ) > 0 ) {
/*
			#[ One is smallest :
*/
			if ( SortBotOut(BHEAD term1) < 0 ) {
				MLOCK(ErrorMessageLock);
				MesPrint("Called from SortBotMerge with thread = %d",AT.identity);
				MUNLOCK(ErrorMessageLock);
				error = -1;
				goto ReturnError;
			}
			im = *term1;
			next = term1 + im;
			if ( next >= Bin1->T.SB.MasterStop[blin1] || ( *next &&
			next+*next+COMPINC > Bin1->T.SB.MasterStop[blin1] ) ) {
				if ( blin1 == 1 ) {
					UNLOCK(Bin1->T.SB.MasterBlockLock[Bin1->T.SB.MasterNumBlocks]);
				}
				else {
					UNLOCK(Bin1->T.SB.MasterBlockLock[blin1-1]);
				}
				if ( blin1 == Bin1->T.SB.MasterNumBlocks ) {
/*
					Move the remainder down into block 0
*/
					to = Bin1->T.SB.MasterStart[1];
					from = Bin1->T.SB.MasterStop[Bin1->T.SB.MasterNumBlocks];
					while ( from > next ) *--to = *--from;
					next = to;
					blin1 = 1;
				}
				else {
					blin1++;
				}
				LOCK(Bin1->T.SB.MasterBlockLock[blin1]);
				Bin1->T.SB.MasterBlock = blin1;
			}
			term1 = next;
/*
			#] One is smallest : 
*/
		}
		else if ( c < 0 ) {
/*
			#[ Two is smallest :
*/
			if ( SortBotOut(BHEAD term2) < 0 ) {
				MLOCK(ErrorMessageLock);
				MesPrint("Called from SortBotMerge with thread = %d",AT.identity);
				MUNLOCK(ErrorMessageLock);
				error = -1;
				goto ReturnError;
			}
next2:		im = *term2;
			next = term2 + im;
			if ( next >= Bin2->T.SB.MasterStop[blin2] || ( *next
			&& next+*next+COMPINC > Bin2->T.SB.MasterStop[blin2] ) ) {
				if ( blin2 == 1 ) {
					UNLOCK(Bin2->T.SB.MasterBlockLock[Bin2->T.SB.MasterNumBlocks]);
				}
				else {
					UNLOCK(Bin2->T.SB.MasterBlockLock[blin2-1]);
				}
				if ( blin2 == Bin2->T.SB.MasterNumBlocks ) {
/*
					Move the remainder down into block 0
*/
					to = Bin2->T.SB.MasterStart[1];
					from = Bin2->T.SB.MasterStop[Bin2->T.SB.MasterNumBlocks];
					while ( from > next ) *--to = *--from;
					next = to;
					blin2 = 1;
				}
				else {
					blin2++;
				}
				LOCK(Bin2->T.SB.MasterBlockLock[blin2]);
				Bin2->T.SB.MasterBlock = blin2;
			}
			term2 = next;
/*
			#] Two is smallest : 
*/
		}
		else {
/*
			#[ Equal :
*/
			l1 = *( m1 = term1 );
			l2 = *( m2 = term2 );
			if ( S->PolyWise ) {  /* Here we work with PolyFun */
				tt1 = m1;
				m1 += S->PolyWise;
				m2 += S->PolyWise;
				if ( S->PolyFlag == 2 ) {
					AT.WorkPointer = wp;
					w = poly_ratfun_add(BHEAD m1,m2);
					if ( *tt1 + w[1] - m1[1] > AM.MaxTer/((LONG)sizeof(WORD)) ) {
						MLOCK(ErrorMessageLock);
						MesPrint("Term too complex in PolyRatFun addition. MaxTermSize of %10l is too small",AM.MaxTer);
						MUNLOCK(ErrorMessageLock);
						Terminate(-1);
					}
					AT.WorkPointer = wp;
					if ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 && w[1] > FUNHEAD ) {
						goto cancelled;
					}
				}
				else {
					w = wp;
					if ( w + m1[1] + m2[1] > AT.WorkTop ) {
						MLOCK(ErrorMessageLock);
						MesPrint("SortBotMerge(%d): A Maxtermsize of %10l is too small",
								AT.identity,AM.MaxTer/sizeof(WORD));
						MesPrint("m1[1] = %d, m2[1] = %d, Space = %l",m1[1],m2[1],(LONG)(AT.WorkTop-wp));
						PrintTerm(term1,"term1");
						PrintTerm(term2,"term2");
						MesPrint("PolyWise = %d",S->PolyWise);
						MUNLOCK(ErrorMessageLock);
						Terminate(-1);
					}
					AddArgs(BHEAD m1,m2,w);
				}
				r1 = w[1];
				if ( r1 <= FUNHEAD
					|| ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) )
						 { goto cancelled; }
				if ( r1 == m1[1] ) {
					NCOPY(m1,w,r1);
				}
				else if ( r1 < m1[1] ) {
					r2 = m1[1] - r1;
					m2 = w + r1;
					m1 += m1[1];
					while ( --r1 >= 0 ) *--m1 = *--m2;
					m2 = m1 - r2;
					r1 = S->PolyWise;
					while ( --r1 >= 0 ) *--m1 = *--m2;
					*m1 -= r2;
					term1 = m1;
				}
				else {
					r2 = r1 - m1[1];
					m2 = tt1 - r2;
					r1 = S->PolyWise;
					m1 = tt1;
					*m1 += r2;
					term1 = m2;
					NCOPY(m2,m1,r1);
					r1 = w[1];
					NCOPY(m2,w,r1);
				}
			}
#ifdef WITHFLOAT
			else if ( AT.SortFloatMode ) {
/*
				The terms are in m1/term1 and m2/term2 and their length in l1 and l2.
				We have to locate the floats which have already been
				verified in the compare routine. Once we have the new
				term we can jump to the code that writes away the
				result after adding the rationals.
*/
				tstop1 = m1+l1; size1 = tstop1[-1]; tstop1 -= ABS(size1);
				tstop2 = m2+l2; size2 = tstop2[-1]; tstop2 -= ABS(size2);
				if ( AT.SortFloatMode == 3 ) {
					fun1 = m1+1;
					while ( fun1[0] != FLOATFUN && fun1+fun1[1] < tstop1 ) {
						fun1 += fun1[1];
					}
					if ( size1 < 0 ) fun1[FUNHEAD+3] = -fun1[FUNHEAD+3];
					UnpackFloat(aux1,fun1);
					fun2 = m2+1;
					while ( fun2[0] != FLOATFUN && fun2+fun2[1] < tstop2 ) {
						fun2 += fun2[1];
					}
					if ( size2 < 0 ) fun2[FUNHEAD+3] = -fun2[FUNHEAD+3];
					UnpackFloat(aux2,fun2);
				}
				else if ( AT.SortFloatMode == 1 ) {
					fun1 = m1+1;
					while ( fun1[0] != FLOATFUN && fun1+fun1[1] < tstop1 ) {
						fun1 += fun1[1];
					}
					if ( size1 < 0 ) fun1[FUNHEAD+3] = -fun1[FUNHEAD+3];
					UnpackFloat(aux1,fun1);
					fun2 = tstop2;
					RatToFloat(aux2,(UWORD *)fun2,size2);
				}
				else if ( AT.SortFloatMode == 2 ) {
					fun1 = tstop1;
					RatToFloat(aux1,(UWORD *)fun1,size1);
					fun2 = m2+1;
					while ( fun2[0] != FLOATFUN && fun2+fun2[1] < tstop2 ) {
						fun2 += fun2[1];
					}
					if ( size2 < 0 ) fun2[FUNHEAD+3] = -fun2[FUNHEAD+3];
					UnpackFloat(aux2,fun2);
				}
				else {
					MLOCK(ErrorMessageLock);
					MesPrint("Illegal value %d for AT.SortFloatMode in SortBotMerge.",AT.SortFloatMode);
					MUNLOCK(ErrorMessageLock);
					Terminate(-1);
					return(0);
				}
				mpf_add(aux3,aux1,aux2);
				size3 = mpf_sgn(aux3);
				if ( size3 == 0 ) { /* Cancelling! Rare! */
					goto cancelled;
				}
				else if ( size3 < 0 ) mpf_neg(aux3,aux3);
				fun3 = TermMalloc("MasterMerge");
				PackFloat(fun3,aux3);
				l3 = fun3[1]+(fun1-m1)+3; /* new size */

				if ( l3 != l1 ) {
/*
					Copy it all to wp
*/
					if ( (l3)*((LONG)sizeof(WORD)) >= AM.MaxTer ) {
						MLOCK(ErrorMessageLock);
						MesPrint("MasterMerge: Coefficient overflow during sort");
						MUNLOCK(ErrorMessageLock);
						goto ReturnError;
					}
                    m3 = wp; m2 = term1;
					while ( m2 < fun1 ) *m3++ = *m2++;
					for ( jj = 0; jj < fun3[1]; jj++ ) *m3++ = fun3[jj];
					*m3++ = 1; *m3++ = 1;
					*m3++ = size3 < 0 ? -3: 3;
					*wp = m3-wp;
					TermFree(fun3,"MasterMerge");
					goto PutOutwp;
				}
				for ( jj = 0; jj < fun3[1]; jj++ ) fun1[jj] = fun3[jj];
				fun1 += fun3[1];
				*fun1++ = 1; *fun1++ = 1;
				*fun1++ = size3 < 0 ? -3: 3;
				*term1 = fun1-term1;
				TermFree(fun3,"MasterMerge");
			}
#endif
			else {
				r1 = *( m1 += l1 - 1 );
				m1 -= ABS(r1) - 1;
				r1 = ( ( r1 > 0 ) ? (r1-1) : (r1+1) ) >> 1;
				r2 = *( m2 += l2 - 1 );
				m2 -= ABS(r2) - 1;
				r2 = ( ( r2 > 0 ) ? (r2-1) : (r2+1) ) >> 1;

				if ( AddRat(BHEAD (UWORD *)m1,r1,(UWORD *)m2,r2,coef,&r3) ) {
					MLOCK(ErrorMessageLock);
					MesCall("SortBotMerge");
					MUNLOCK(ErrorMessageLock);
					SETERROR(-1)
				}

				if ( AN.ncmod != 0 ) {
					if ( ( AC.modmode & POSNEG ) != 0 ) {
						NormalModulus(coef,&r3);
					}
					else if ( BigLong(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
						SubPLon(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod),coef,&r3);
						coef[r3] = 1;
						for ( ii = 1; ii < r3; ii++ ) coef[r3+ii] = 0;
					}
				}
				if ( !r3 ) { goto cancelled; }
				r3 *= 2;
				r33 = ( r3 > 0 ) ? ( r3 + 1 ) : ( r3 - 1 );
				if ( r3 < 0 ) r3 = -r3;
				if ( r1 < 0 ) r1 = -r1;
				r1 *= 2;
				r31 = r3 - r1;
				if ( !r31 ) {		/* copy coef into term1 */
					m2 = (WORD *)coef; im = r3;
					NCOPY(m1,m2,im);
					*m1 = r33;
				}
				else {
					to = wp; from = term1;
					while ( from < m1 ) *to++ = *from++;
					from = (WORD *)coef; im = r3;
					NCOPY(to,from,im);
					*to++ = r33;
					wp[0] = to - wp;
PutOutwp:
					if ( SortBotOut(BHEAD wp) < 0 ) {
						MLOCK(ErrorMessageLock);
						MesPrint("Called from SortBotMerge with thread = %d",AT.identity);
						MUNLOCK(ErrorMessageLock);
						error = -1;
						goto ReturnError;
					}
					goto cancelled;
				}
			}
			if ( SortBotOut(BHEAD term1) < 0 ) {
				MLOCK(ErrorMessageLock);
				MesPrint("Called from SortBotMerge with thread = %d",AT.identity);
				MUNLOCK(ErrorMessageLock);
				error = -1;
				goto ReturnError;
			}
cancelled:;		/* Now we need two new terms */
			im = *term1;
			next = term1 + im;
			if ( next >= Bin1->T.SB.MasterStop[blin1] || ( *next &&
			next+*next+COMPINC > Bin1->T.SB.MasterStop[blin1] ) ) {
				if ( blin1 == 1 ) {
					UNLOCK(Bin1->T.SB.MasterBlockLock[Bin1->T.SB.MasterNumBlocks]);
				}
				else {
					UNLOCK(Bin1->T.SB.MasterBlockLock[blin1-1]);
				}
				if ( blin1 == Bin1->T.SB.MasterNumBlocks ) {
/*
					Move the remainder down into block 0
*/
					to = Bin1->T.SB.MasterStart[1];
					from = Bin1->T.SB.MasterStop[Bin1->T.SB.MasterNumBlocks];
					while ( from > next ) *--to = *--from;
					next = to;
					blin1 = 1;
				}
				else {
					blin1++;
				}
				LOCK(Bin1->T.SB.MasterBlockLock[blin1]);
				Bin1->T.SB.MasterBlock = blin1;
			}
			term1 = next;
			goto next2;
/*
			#] Equal : 
*/
		}
	}
/*
	Copy the tail
*/
	if ( *term1 ) {
/*
			#[ Tail in one :
*/
		while ( *term1 ) {
			if ( SortBotOut(BHEAD term1) < 0 ) {
				MLOCK(ErrorMessageLock);
				MesPrint("Called from SortBotMerge with thread = %d",AT.identity);
				MUNLOCK(ErrorMessageLock);
				error = -1;
				goto ReturnError;
			}
			im = *term1;
			next = term1 + im;
			if ( next >= Bin1->T.SB.MasterStop[blin1] || ( *next &&
			next+*next+COMPINC > Bin1->T.SB.MasterStop[blin1] ) ) {
				if ( blin1 == 1 ) {
					UNLOCK(Bin1->T.SB.MasterBlockLock[Bin1->T.SB.MasterNumBlocks]);
				}
				else {
					UNLOCK(Bin1->T.SB.MasterBlockLock[blin1-1]);
				}
				if ( blin1 == Bin1->T.SB.MasterNumBlocks ) {
/*
					Move the remainder down into block 0
*/
					to = Bin1->T.SB.MasterStart[1];
					from = Bin1->T.SB.MasterStop[Bin1->T.SB.MasterNumBlocks];
					while ( from > next ) *--to = *--from;
					next = to;
					blin1 = 1;
				}
				else {
					blin1++;
				}
				LOCK(Bin1->T.SB.MasterBlockLock[blin1]);
				Bin1->T.SB.MasterBlock = blin1;
			}
			term1 = next;
		}
/*
			#] Tail in one : 
*/
	}
	else if ( *term2 ) {
/*
			#[ Tail in two :
*/
		while ( *term2 ) {
			if ( SortBotOut(BHEAD term2) < 0 ) {
				MLOCK(ErrorMessageLock);
				MesPrint("Called from SortBotMerge with thread = %d",AT.identity);
				MUNLOCK(ErrorMessageLock);
				error = -1;
				goto ReturnError;
			}
			im = *term2;
			next = term2 + im;
			if ( next >= Bin2->T.SB.MasterStop[blin2] || ( *next
			&& next+*next+COMPINC > Bin2->T.SB.MasterStop[blin2] ) ) {
				if ( blin2 == 1 ) {
					UNLOCK(Bin2->T.SB.MasterBlockLock[Bin2->T.SB.MasterNumBlocks]);
				}
				else {
					UNLOCK(Bin2->T.SB.MasterBlockLock[blin2-1]);
				}
				if ( blin2 == Bin2->T.SB.MasterNumBlocks ) {
/*
					Move the remainder down into block 0
*/
					to = Bin2->T.SB.MasterStart[1];
					from = Bin2->T.SB.MasterStop[Bin2->T.SB.MasterNumBlocks];
					while ( from > next ) *--to = *--from;
					next = to;
					blin2 = 1;
				}
				else {
					blin2++;
				}
				LOCK(Bin2->T.SB.MasterBlockLock[blin2]);
				Bin2->T.SB.MasterBlock = blin2;
			}
			term2 = next;
		}
/*
			#] Tail in two : 
*/
	}
	SortBotOut(BHEAD 0);
ReturnError:;
/*
	Release all locks
*/
	UNLOCK(Bin1->T.SB.MasterBlockLock[blin1]);
	if ( blin1 > 1 ) {
		UNLOCK(Bin1->T.SB.MasterBlockLock[blin1-1]);
	}
	else {
		UNLOCK(Bin1->T.SB.MasterBlockLock[Bin1->T.SB.MasterNumBlocks]);
	}
	UNLOCK(Bin2->T.SB.MasterBlockLock[blin2]);
	if ( blin2 > 1 ) {
		UNLOCK(Bin2->T.SB.MasterBlockLock[blin2-1]);
	}
	else {
		UNLOCK(Bin2->T.SB.MasterBlockLock[Bin2->T.SB.MasterNumBlocks]);
	}
	if ( AT.identity > 0 ) {
		UNLOCK(AT.SB.MasterBlockLock[AT.SB.FillBlock]);
	}
/*
	And that was all folks
*/
	return(error);
}

#endif

/*
  	#] SortBotMerge : 
  	#[ IniSortBlocks :
*/
 
static int SortBlocksInitialized = 0;

/**
 *	Initializes the blocks in the sort buffers of the master.
 *	These blocks are needed to keep both the workers and the master working
 *	simultaneously. See also the commentary at the routine MasterMerge.
 */

int IniSortBlocks(int numworkers)
{
	ALLPRIVATES *B;
	SORTING *S;
	LONG totalsize, workersize, blocksize, numberofterms;
	int maxter, id, j;
	int numberofblocks = NUMBEROFBLOCKSINSORT, numparts;
	WORD *w;

	if ( SortBlocksInitialized ) return(0);
	SortBlocksInitialized = 1;
	if ( numworkers == 0 ) return(0);

#ifdef WITHSORTBOTS
	if ( numworkers > 2 ) {
		numparts = 2*numworkers - 2;
		numberofblocks = numberofblocks/2;
	}
	else {
		numparts = numworkers;
	}
#else
	numparts = numworkers;
#endif
	S = AM.S0;
	totalsize = S->LargeSize + S->SmallEsize;
	workersize = totalsize / numparts;
	maxter = AM.MaxTer/sizeof(WORD);
	blocksize = ( workersize - maxter )/numberofblocks;
	numberofterms = blocksize / maxter;
	if ( numberofterms < MINIMUMNUMBEROFTERMS ) {
/*
		This should have been taken care of in RecalcSetups.
*/
		MesPrint("We have a problem with the size of the blocks in IniSortBlocks");
		Terminate(-1);
	}
/*
	Layout:  For each worker
				block 0: size is maxter WORDS
				numberofblocks blocks of size blocksize WORDS
*/
	w = S->lBuffer;
	if ( w == 0 ) w = S->sBuffer;
	for ( id = 1; id <= numparts; id++ ) {
		B = AB[id];
		AT.SB.MasterBlockLock = (pthread_mutex_t *)Malloc1(
			sizeof(pthread_mutex_t)*(numberofblocks+1),"MasterBlockLock");
		AT.SB.MasterStart = (WORD **)Malloc1(sizeof(WORD *)*(numberofblocks+1)*3,"MasterBlock");
		AT.SB.MasterFill = AT.SB.MasterStart + (numberofblocks+1);
		AT.SB.MasterStop = AT.SB.MasterFill  + (numberofblocks+1);
		AT.SB.MasterNumBlocks = numberofblocks;
		AT.SB.MasterBlock = 0;
		AT.SB.FillBlock = 0;
		AT.SB.MasterFill[0] = AT.SB.MasterStart[0] = w;
		w += maxter;
		AT.SB.MasterStop[0] = w;
		AT.SB.MasterBlockLock[0] = dummylock;
		for ( j = 1; j <= numberofblocks; j++ ) {
			AT.SB.MasterFill[j] = AT.SB.MasterStart[j] = w;
			w += blocksize;
			AT.SB.MasterStop[j] = w;
			AT.SB.MasterBlockLock[j] = dummylock;
		}
	}
	if ( w > S->sTop2 ) {
		MesPrint("Counting problem in IniSortBlocks");
		Terminate(-1);
	}
	return(0);
}

/*
  	#] IniSortBlocks : 
  	#[ UpdateSortBlocks :
*/
 
/**
 *	A version of IniSortBlocks which only updates the pointers in the master's
 *	buffer, to be used after reallocation of that buffer.
 */
int UpdateSortBlocks(int numworkers)
{
	ALLPRIVATES *B;
	SORTING *S;
	LONG totalsize, workersize, blocksize, numberofterms;
	int maxter, id, j;
	int numberofblocks = NUMBEROFBLOCKSINSORT, numparts;
	WORD *w;

	if ( numworkers == 0 ) return(0);

#ifdef WITHSORTBOTS
	if ( numworkers > 2 ) {
		numparts = 2*numworkers - 2;
		numberofblocks = numberofblocks/2;
	}
	else {
		numparts = numworkers;
	}
#else
	numparts = numworkers;
#endif
	S = AM.S0;
	totalsize = S->LargeSize + S->SmallEsize;
	workersize = totalsize / numparts;
	maxter = AM.MaxTer/sizeof(WORD);
	blocksize = ( workersize - maxter )/numberofblocks;
	numberofterms = blocksize / maxter;
	if ( numberofterms < MINIMUMNUMBEROFTERMS ) {
/*
		This should have been taken care of in RecalcSetups.
*/
		MesPrint("We have a problem with the size of the blocks in UpdateSortBlocks");
		Terminate(-1);
	}
/*
	Layout:  For each worker
				block 0: size is maxter WORDS
				numberofblocks blocks of size blocksize WORDS
*/
	w = S->lBuffer;
	if ( w == 0 ) w = S->sBuffer;
	for ( id = 1; id <= numparts; id++ ) {
		B = AB[id];
		AT.SB.MasterFill[0] = AT.SB.MasterStart[0] = w;
		w += maxter;
		AT.SB.MasterStop[0] = w;
		for ( j = 1; j <= numberofblocks; j++ ) {
			AT.SB.MasterFill[j] = AT.SB.MasterStart[j] = w;
			w += blocksize;
			AT.SB.MasterStop[j] = w;
		}
	}
	if ( w > S->sTop2 ) {
		MesPrint("Counting problem in UpdateSortBlocks");
		Terminate(-1);
	}
	return(0);
}

/*
  	#] UpdateSortBlocks : 
  	#[ DefineSortBotTree :
*/
 
#ifdef WITHSORTBOTS

/**
 *	To be used in a sortbot merge. It initializes the whole sortbot
 *	system by telling the sortbot which threads provide their input.
 */

void DefineSortBotTree(void)
{
	ALLPRIVATES *B;
	int n, i, from;
	if ( numberofworkers <= 2 ) return;
	n = numberofworkers*2-2;
	for ( i = numberofworkers+1, from = 1; i <= n; i++ ) {
		B = AB[i];
		AT.SortBotIn1 = from++;
		AT.SortBotIn2 = from++;
	}
	B = AB[0];
	AT.SortBotIn1 = from++;
	AT.SortBotIn2 = from++;
}

#endif

/*
  	#] DefineSortBotTree : 
  	#[ GetTerm2 :

	Routine does a GetTerm but only when a bracket index is involved and
	only from brackets that have been judged not suitable for treatment
	as complete brackets by a single worker. Whether or not a bracket should
	be treated by a single worker is decided by TreatIndexEntry
*/

WORD GetTerm2(PHEAD WORD *term)
{
	WORD *ttco, *tt, retval;
	LONG n,i;
	FILEHANDLE *fi;
	EXPRESSIONS e = AN.expr;
	BRACKETINFO *b  = e->bracketinfo;
	BRACKETINDEX *bi = b->indexbuffer;
	POSITION where, eonfile = AS.OldOnFile[e-Expressions], bstart, bnext;
/*
	1: Get the current position.
*/
	switch ( e->status ) {
		case UNHIDELEXPRESSION:
		case UNHIDEGEXPRESSION:
		case DROPHLEXPRESSION:
		case DROPHGEXPRESSION:
		case HIDDENLEXPRESSION:
		case HIDDENGEXPRESSION:
			fi = AR.hidefile;
			break;
		default:
			fi = AR.infile;
			break;
	}
	if ( AR.KeptInHold ) {
		retval = GetTerm(BHEAD term);
		return(retval);
	}
	SeekScratch(fi,&where);
	if ( AN.lastinindex < 0 ) {
/*
		We have to test whether we have to do the first bracket
*/
		if ( ( n = TreatIndexEntry(BHEAD 0) ) <= 0 ) {
			AN.lastinindex = n;
			where = bi[n].start;
			ADD2POS(where,eonfile);
			SetScratch(fi,&where);
/*
			Put the bracket in the Compress buffer.
*/
			ttco = AR.CompressBuffer;
			tt = b->bracketbuffer + bi[0].bracket;
			i = *tt;
			NCOPY(ttco,tt,i)
			AR.CompressPointer = ttco;
			retval = GetTerm(BHEAD term);
			return(retval);
		}
		else AN.lastinindex = n-1;
	}
/*
	2: Find the corresponding index number
	   a: test whether it is in the current bracket
*/
	n = AN.lastinindex;
	bstart = bi[n].start;
	ADD2POS(bstart,eonfile);
	bnext = bi[n].next;
	ADD2POS(bnext,eonfile);
	if ( ISLESSPOS(bstart,where) && ISLESSPOS(where,bnext) ) {
		retval = GetTerm(BHEAD term);
		return(retval);
	}
	for ( n++ ; n < b->indexfill; n++ ) {
		i = TreatIndexEntry(BHEAD n);
		if ( i <= 0 ) {
/*
			Put the bracket in the Compress buffer.
*/
			ttco = AR.CompressBuffer;
			tt = b->bracketbuffer + bi[n].bracket;
			i = *tt;
			NCOPY(ttco,tt,i)
			AR.CompressPointer = ttco;
			AN.lastinindex = n;
			where = bi[n].start;
			ADD2POS(where,eonfile);
			SetScratch(fi,&(where));
			retval = GetTerm(BHEAD term);
			return(retval);
		}
		else n += i - 1;
	}
	return(0);
}

/*
  	#] GetTerm2 : 
  	#[ TreatIndexEntry :
*/
/**
 *	Routine has to decide whether a bracket has to be sent as a complete
 *	bracket to a worker or whether it has to be treated by the bucket system.
 *	Return value is positive when we should send it as a complete bracket and
 *	0 when it should be done via the buckets.
 *	The positive return value indicates how many brackets should be treated.
 */
 
int TreatIndexEntry(PHEAD LONG n)
{
	BRACKETINFO *b  = AN.expr->bracketinfo;
	LONG numbra = b->indexfill - 1, i;
	LONG totterms;
	BRACKETINDEX *bi;
	POSITION pos1, average;
/*
	1: number of the bracket should be such that there is one bucket
	   for each worker remaining.
*/
	if ( ( numbra - n ) <= numberofworkers ) return(0);
/*
	2: size of the bracket contents should be less than what remains in
	   the expression divided by the number of workers.
*/
	bi = b->indexbuffer;
	DIFPOS(pos1,bi[numbra].next,bi[n].next);  /* Size of what remains */
	BASEPOSITION(average) = DIVPOS(pos1,(3*numberofworkers));
	DIFPOS(pos1,bi[n].next,bi[n].start);      /* Size of the current bracket */
	if ( ISLESSPOS(average,pos1) ) return(0);
/*
	It passes.
	Now check whether we can do more brackets
*/
	totterms = bi->termsinbracket;
	if ( totterms > 2*AC.ThreadBucketSize ) return(1);
	for ( i = 1; i < numbra-n; i++ ) {
		DIFPOS(pos1,bi[n+i].next,bi[n].start); /* Size of the combined brackets */
		if ( ISLESSPOS(average,pos1) ) return(i);
		totterms += bi->termsinbracket;
		if ( totterms > 2*AC.ThreadBucketSize ) return(i+1);
	}
/*
	We have a problem at the end of the system. Just do one.
*/
	return(1);
}

/*
  	#] TreatIndexEntry : 
  	#[ SetHideFiles :
*/

void SetHideFiles(void) {
	int i;
	ALLPRIVATES *B, *B0 = AB[0];
	for ( i = 1; i <= numberofworkers; i++ ) {
		B = AB[i];
		AR.hidefile->handle = AR0.hidefile->handle;
		if ( AR.hidefile->handle < 0 ) {
			AR.hidefile->PObuffer = AR0.hidefile->PObuffer;
			AR.hidefile->POstop = AR0.hidefile->POstop;
			AR.hidefile->POfill = AR0.hidefile->POfill;
			AR.hidefile->POfull = AR0.hidefile->POfull;
			AR.hidefile->POsize = AR0.hidefile->POsize;
			AR.hidefile->POposition = AR0.hidefile->POposition;
			AR.hidefile->filesize = AR0.hidefile->filesize;
		}
		else {
			AR.hidefile->PObuffer = AR.hidefile->wPObuffer;
			AR.hidefile->POstop = AR.hidefile->wPOstop;
			AR.hidefile->POfill = AR.hidefile->wPOfill;
			AR.hidefile->POfull = AR.hidefile->wPOfull;
			AR.hidefile->POsize = AR.hidefile->wPOsize;
			PUTZERO(AR.hidefile->POposition);
		}
	}
}

/*
  	#] SetHideFiles : 
  	#[ IniFbufs :
*/

void IniFbufs(void)
{
	int i;
	for ( i = 0; i < AM.totalnumberofthreads; i++ ) {
		IniFbuffer(AB[i]->T.fbufnum);
	}
}

/*
  	#] IniFbufs : 
  	#[ SetMods :
*/

void SetMods(void)
{
	ALLPRIVATES *B;
	int i, n, j;
	for ( j = 0; j < AM.totalnumberofthreads; j++ ) {
		B = AB[j];
		AN.ncmod = AC.ncmod;
		if ( AN.cmod != 0 ) M_free(AN.cmod,"AN.cmod");
		n = ABS(AN.ncmod);
		AN.cmod = (UWORD *)Malloc1(sizeof(WORD)*n,"AN.cmod");
		for ( i = 0; i < n; i++ ) AN.cmod[i] = AC.cmod[i];
	}
}

/*
  	#] SetMods : 
  	#[ UnSetMods :
*/

void UnSetMods(void)
{
	ALLPRIVATES *B;
	int j;
	for ( j = 0; j < AM.totalnumberofthreads; j++ ) {
		B = AB[j];
		if ( AN.cmod != 0 ) M_free(AN.cmod,"AN.cmod");
		AN.cmod = 0;
	}
}

/*
  	#] UnSetMods : 
  	#[ find_Horner_MCTS_expand_tree_threaded :
*/
 
void find_Horner_MCTS_expand_tree_threaded(void) {
	int id;
	while (( id = GetAvailableThread() ) < 0)
		MasterWait();	
	WakeupThread(id,MCTSEXPANDTREE);
}

/*
  	#] find_Horner_MCTS_expand_tree_threaded : 
  	#[ optimize_expression_given_Horner_threaded :
*/
 
extern void optimize_expression_given_Horner_threaded(void) {
	int id;
	while (( id = GetAvailableThread() ) < 0)
		MasterWait();	
	WakeupThread(id,OPTIMIZEEXPRESSION);
}

/*
  	#] optimize_expression_given_Horner_threaded : 
*/

#endif