File: Cpool.c

package info (click to toggle)
lcgdm 1.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 14,044 kB
  • sloc: ansic: 149,126; sh: 13,441; perl: 11,498; python: 5,778; cpp: 5,113; sql: 1,805; makefile: 1,388; fortran: 113
file content (2908 lines) | stat: -rw-r--r-- 91,339 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) 1999-2010 by CERN/IT/PDP/DM
 * All rights reserved
 */

/* ------------------------------------ */
/* For the what command                 */
/* ------------------------------------ */
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: Cpool.c,v $ $Revision: 3476 $ $Date: 2010-03-10 13:40:05 +0100 (Wed, 10 Mar 2010) $ CERN IT-ADC-CA/HSM Jean-Damien Durand";
#endif /* not lint */

#include <Cpool_api.h>
/* We do these #undef because we want to create entry points */
/* for old application not recompiled using shared library */
/* and the #define's would cause is some problem... */
#undef Cpool_create
#undef Cpool_assign
#undef Cpool_next_index
#undef Cpool_next_index_timeout
#include <serrno.h>
#ifndef _WIN32
/* All that stuff is for CTHREAD_MULTI_PROCESS support on */
/* Unix-like systems.                                     */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#endif /* _WIN32 */
#ifdef _AIX
/* Otherwise cc will not know about fd_set on */
/* old aix versions.                          */
#include <sys/select.h>
#endif
#ifdef DEBUG
#ifndef CPOOL_DEBUG
#define CPOOL_DEBUG
#endif
#endif /* DEBUG */

#ifdef CPOOL_DEBUG
#include <log.h>
#endif

#include <osdep.h>

int Cpool_debug = 0;

#ifndef _WIN32
#ifndef _CTHREAD
/* ------------------------------------ */
/* Undefinition of memory wrappers      */
/* ------------------------------------ */
/* We don't want to recursively call   */
/* ourselves...                        */
#undef calloc
#undef malloc
#undef realloc
#undef free
#endif 
#endif /* _WIN32 */

/* ------------------------------------ */
/* Linked list of pools                 */
/* ------------------------------------ */
struct Cpool_t {
	int                     lock_parent;
	int                     poolnb;   /* Pool number                        */
	int                     nbelem;   /* Nb of elems                        */
	int                    *cid;      /* Elements Cthread ID                */
	int                     forceid;  /* Index forcing the assignment to    */
#ifndef _WIN32
	/* If CTHREAD_MULTI_PROCESS */
	int                    *writefd;  /* Parent->Child (only on unix)       */
	int                    *readfd;   /* Child->Parent (only on unix)       */
#endif
	/* If CTHREAD_TRUE_THREAD */
	int                    *state;    /* Elements current status (0=READY)  */
	/*                        (1=RUNNING) */
	/*                       (-1=STARTED) */
	
	int                     flag;     /* Parent flag (-1=WAITING)           */
	void                  *(**start) _PROTO((void *)); /* Start routines             */
	void                  **arg;
	
	void                   *lock_parent_cthread_structure;
	void                   **state_cthread_structure;

	struct Cpool_t         *next;     /* Next pool                          */
};

#ifndef _WIN32
static struct Cpool_t Cpool = { -1, 0, 0, NULL, -1, NULL, NULL, NULL, 0, NULL, NULL, NULL};
#else
static struct Cpool_t Cpool = { -1, 0, 0, NULL, -1, NULL, 0, NULL, NULL, NULL};
#endif

#ifndef _WIN32
/* ------------------------------------ */
/* Linked list of memory allocation     */
/* ------------------------------------ */
struct Cmalloc_t {
	void *start;
	void *end;
	struct Cmalloc_t *next;
};
static struct Cmalloc_t Cmalloc = { NULL, NULL, NULL};
#endif /* _WIN32 */

#ifndef _WIN32
/* ------------------------------------ */
/* Non-thread environment pipe protocol */
/* ------------------------------------ */
static int  tubes[5];
#endif /* _WIN32 */

#ifndef _WIN32
/* ------------------------------------ */
/* Typedefs                             */
/* ------------------------------------ */
typedef void    Sigfunc _PROTO((int));
#endif /* _WIN32 */

#ifdef hpux
/* hpux wants int instead of fd_set */
typedef int _cpool_fd_set;
/* typedef fd_set _cpool_fd_set; */
#else
typedef fd_set _cpool_fd_set;
#endif /* hpux */

/* ------------------------------------ */
/* Prototypes                           */
/* ------------------------------------ */
void   DLL_DECL  *_Cpool_starter _PROTO((void *));
#ifndef _WIN32
size_t   _Cpool_writen _PROTO((int, void *, size_t));
size_t   _Cpool_readn _PROTO((int, void *, size_t));
#ifdef CPOOL_DEBUG
size_t   _Cpool_writen_timeout _PROTO((char *, int, int, void *, size_t, int));
size_t   _Cpool_readn_timeout _PROTO((char *, int, int, void *, size_t, int));
#else
size_t   _Cpool_writen_timeout _PROTO((int, void *, size_t, int));
size_t   _Cpool_readn_timeout _PROTO((int, void *, size_t, int));
#endif
void     _Cpool_alarm _PROTO((int));
Sigfunc *_Cpool_signal _PROTO((int, Sigfunc *));
#endif /* _WIN32 */
int    DLL_DECL  _Cpool_self();

#ifndef _WIN32
/* ------------------------------------ */
/* Constants used in the fork() model   */
/* ------------------------------------ */
#ifndef _CPOOL_STARTER_TIMEOUT
#define _CPOOL_STARTER_TIMEOUT 10
#endif
#ifndef _CPOOL_SLEEP_FLAG
#define _CPOOL_SLEEP_FLAG       -1
#endif
static void *_cpool_sleep_flag = (void *) _CPOOL_SLEEP_FLAG;
#endif /* _WIN32 */

/* ============================================ */
/* Routine  : Cpool_create                      */
/* Arguments: Number of requested processes     */
/*            &Number of created processses     */
/* -------------------------------------------- */
/* Output   : >= 0 (PoolNumber) -1 (ERROR)      */
/* -------------------------------------------- */
/* History:                                     */
/* 17-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
int DLL_DECL Cpool_create(nbreq,nbget)
	int nbreq;
	int *nbget;
{
	return(Cpool_create_ext(nbreq,nbget,NULL));
}

/* ============================================ */
/* Routine  : Cpool_create_ext                  */
/* Arguments: Number of requested processes     */
/*            &Number of created processses     */
/*            &Cpool_structure                  */
/* -------------------------------------------- */
/* Output   : >= 0 (PoolNumber) -1 (ERROR)      */
/* -------------------------------------------- */
/* History:                                     */
/* 17-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* 04-SEP-2003       Added &Cpool_structure     */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
int DLL_DECL Cpool_create_ext(nbreq,nbget,pooladdr)
	int nbreq;
	int *nbget;
	void **pooladdr;
{
	struct Cpool_t         *current          = &Cpool;
	struct Cpool_t         *previous         = &Cpool;
	int                     i;
	int                     j;
	int                     k;
	int                     poolnb, nbcreated;
#ifndef _WIN32
	/* If CTHREAD_MULTI_PROCESS */
	int                     p_to_c[2];
	int                     c_to_p[2];
	int                    *to_close = NULL;
	int                     pid = 0;
#endif
	/* If CTHREAD_TRUE_THREAD */
	void                   *cpool_arg = NULL;

	/* We makes sure that Cthread pakage is initalized */
	Cthread_init();
  
#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create(%d,0x%lx)\n",
			_Cpool_self(),_Cthread_self(),nbreq,(unsigned long) nbget);
#endif

	if (nbreq <= 0) {
		serrno = EINVAL;
		return(-1);
	}

#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : lock on &Cpool\n",
			_Cpool_self(),_Cthread_self());
#endif

	/* We search the last available pool */
	if (Cthread_mutex_lock(&Cpool) != 0) {
		return(-1);
	}
	previous = &Cpool;
	if (current->next != NULL) {
		while (current->next != NULL) {
			previous = current;
			current  = current->next;
		}
		previous = current;
		poolnb = current->poolnb;
	} else {
		/* Make sure that ++poolnb will return a number >= 0 */
		poolnb = -1;
	}

#ifndef _WIN32
	if (Cthread_environment() == CTHREAD_MULTI_PROCESS) {
		/* To let know the child which parent is the creator */
		/* and to die in case of non-parent existence        */
		/* (on unix only, this is to prevent zombies)        */
		pid = getpid();
    
		/* We further more don't want to die in case of SIGCHLD */
		/* or SIGALRM (which is our private signal for timeout) */
		/* 
		   _Cpool_signal(SIGCHLD, SIG_IGN);
		   _Cpool_signal(SIGPIPE, SIG_IGN);
		   _Cpool_signal(SIGALRM, SIG_IGN);
		*/

		/* We prepare the list of fd's to close */
		if ((to_close = malloc(2 * nbreq * sizeof(int))) == NULL) {
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
					_Cpool_self(),_Cthread_self());
#endif
			Cthread_mutex_unlock(&Cpool);
			serrno = SEINTERNAL;
			return(-1);
		}
	}
#endif
  
	/* We create a new pool element */
	if ((current = malloc(sizeof(struct Cpool_t))) == NULL) {
#ifdef CPOOL_DEBUG
		if (Cpool_debug != 0)
			log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
				_Cpool_self(),_Cthread_self());
#endif
		Cthread_mutex_unlock(&Cpool);
		serrno = SEINTERNAL;
		return(-1);
	}

	/* Allocation for Cthread ID's */
	if ((current->cid = malloc(nbreq * sizeof(int))) == NULL) {
#ifndef _WIN32
		if (Cthread_environment() == CTHREAD_MULTI_PROCESS) {
			free(to_close);
		}
#endif
		free(current);
#ifdef CPOOL_DEBUG
		if (Cpool_debug != 0)
			log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
				_Cpool_self(),_Cthread_self());
#endif
		Cthread_mutex_unlock(&Cpool);
		serrno = SEINTERNAL;
		return(-1);
	}
#ifndef _WIN32
	if (Cthread_environment() == CTHREAD_MULTI_PROCESS) {
		/* Allocation for writing pipes (unix only) */
		if ((current->writefd = malloc(nbreq * sizeof(int))) == NULL) {
			free(to_close);
			free(current->cid);
			free(current);
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
					_Cpool_self(),_Cthread_self());
#endif
			Cthread_mutex_unlock(&Cpool);
			serrno = SEINTERNAL;
			return(-1);
		}
		/* Allocation for reading pipes (unix only) */
		if ((current->readfd = malloc(nbreq * sizeof(int))) == NULL) {
			free(to_close);
			free(current->writefd);
			free(current->cid);
			free(current);
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
					_Cpool_self(),_Cthread_self());
#endif
			Cthread_mutex_unlock(&Cpool);
			serrno = SEINTERNAL;
			return(-1);
		}
	} else {
#endif /* _WIN32 */
#ifdef CPOOL_DEBUG
		if (Cpool_debug != 0)
			log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : lock on &(current->lock_parent)\n",
				_Cpool_self(),_Cthread_self());
#endif
		if (Cthread_mutex_lock(&(current->lock_parent)) != 0) {
			free(current->cid);
			free(current);
			Cthread_mutex_unlock(&Cpool);
			serrno = SEINTERNAL;
			return(-1);
		}
		if ((current->lock_parent_cthread_structure = Cthread_mutex_lock_addr(&(current->lock_parent))) == NULL) {
			free(current->cid);
			free(current);
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
					_Cpool_self(),_Cthread_self());
#endif
			Cthread_mutex_unlock(&Cpool);
			serrno = SEINTERNAL;
			return(-1);
		}
#ifdef CPOOL_DEBUG
		if (Cpool_debug != 0)
			log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on current->lock_parent_cthread_structure\n",
				_Cpool_self(),_Cthread_self());
#endif
		Cthread_mutex_unlock_ext(current->lock_parent_cthread_structure);
		/* Allocation for threads status */
		if ((current->state = malloc(nbreq * sizeof(int))) == NULL) {
			free(current->cid);
			free(current);
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
					_Cpool_self(),_Cthread_self());
#endif
			Cthread_mutex_unlock(&Cpool);
			serrno = SEINTERNAL;
			return(-1);
		}
		if ((current->state_cthread_structure = (void **) malloc(nbreq * sizeof(void *))) == NULL) {
			free(current->state);
			free(current->cid);
			free(current);
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
					_Cpool_self(),_Cthread_self());
#endif
			Cthread_mutex_unlock(&Cpool);
			serrno = SEINTERNAL;
			return(-1);
		}
		{
			int icthread;
			for (icthread = 0; icthread < nbreq; icthread++) {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : lock on &(current->state[%d]\n",
						_Cpool_self(),_Cthread_self(), icthread);
#endif
				if (Cthread_mutex_lock(&(current->state[icthread])) != 0) {
					free(current->state_cthread_structure);
					free(current->state);
					free(current->cid);
					free(current);
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0)
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
							_Cpool_self(),_Cthread_self());
#endif
					Cthread_mutex_unlock(&Cpool);
					serrno = SEINTERNAL;
					return(-1);
				}
				if ((current->state_cthread_structure[icthread] = Cthread_mutex_lock_addr(&(current->state[icthread]))) == NULL) {
					free(current->state_cthread_structure);
					free(current->state);
					free(current->cid);
					free(current);
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0)
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
							_Cpool_self(),_Cthread_self());
#endif
					Cthread_mutex_unlock(&Cpool);
					serrno = SEINTERNAL;
					return(-1);
				}
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on current->state_cthread_structure[%d]\n",
						_Cpool_self(),_Cthread_self(), icthread);
#endif
				Cthread_mutex_unlock_ext(current->state_cthread_structure[icthread]);
			}
		}
		/* Allocation for threads starting routines */
		if ((current->start = malloc(nbreq * sizeof(void *))) == NULL) {
			free(current->state_cthread_structure);
			free(current->state);
			free(current->cid);
			free(current);
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
					_Cpool_self(),_Cthread_self());
#endif
			Cthread_mutex_unlock(&Cpool);
			serrno = SEINTERNAL;
			return(-1);
		}
		/* Allocation for threads arguments addresses */
		if ((current->arg = malloc(nbreq * sizeof(void *))) == NULL) {
			free(current->state_cthread_structure);
			free(current->start);
			free(current->state);
			free(current->cid);
			free(current);
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
					_Cpool_self(),_Cthread_self());
#endif
			Cthread_mutex_unlock(&Cpool);
			serrno = SEINTERNAL;
			return(-1);
		}
#ifndef _WIN32
	}
#endif
	current->next = NULL;
	if (Cthread_environment() != CTHREAD_MULTI_PROCESS) {
		/* We tell that there is no dispatch at this time */
		current->flag = -2;
	}
    
	nbcreated = j = k = 0;
	/* We create the pools */
	for (i = 0; i < nbreq; i++) {
#ifndef _WIN32
		if (Cthread_environment() == CTHREAD_MULTI_PROCESS) {
			/* The pipes */
			if (pipe(p_to_c))
				continue;
			if (pipe(c_to_p)) {
				close(p_to_c[0]);
				close(p_to_c[1]);
				continue;
			}
			/* We prepare the argument */
			tubes[0] = p_to_c[0]; /* For reading */
			tubes[1] = p_to_c[1]; /* For writing */
			tubes[2] = c_to_p[0]; /* For reading */
			tubes[3] = c_to_p[1]; /* For writing */
			tubes[4] = pid;
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : pipes give [%d,%d,%d,%d]\n",
					_Cpool_self(),_Cthread_self(),tubes[0],tubes[1],tubes[2],tubes[3]);
#endif
			/* Non thread environment : everything will be done with pipes */
			current->cid[i] = Cthread_create(_Cpool_starter,NULL);
		} else {
#endif /* _WIN32 */
			/* Thread environment : everything will be done with shared mem. and cond. vars. */
			/* We send as argument the address of the current pool structure as well as the  */
			/* index of this thread in this structure                                        */
			if ((cpool_arg = malloc(sizeof(struct Cpool_t *) + sizeof(int))) != NULL) {
				char *dummy = cpool_arg;
				memcpy(dummy,&current,sizeof(struct Cpool_t *));
				dummy += sizeof(struct Cpool_t *);
				memcpy(dummy,&i,sizeof(int));
				/* And we don't forget to initialize the state of this thread to be STARTED... */
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : lock on current->state_cthread_structure[%d]\n",
						_Cpool_self(),_Cthread_self(), i);
#endif
				Cthread_mutex_lock_ext(current->state_cthread_structure[i]);
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : Setting current->state_cthread_structure[%d] to 1\n",
						_Cpool_self(),_Cthread_self(), i);
#endif
				current->state[i] = 1;
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : Cthread_create\n",
						_Cpool_self(),_Cthread_self());
#endif
				if ((current->cid[i] = Cthread_create(_Cpool_starter,cpool_arg)) < 0) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0)
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on current->state_cthread_structure[%d]\n",
							_Cpool_self(),_Cthread_self(), i);
#endif
					Cthread_mutex_unlock_ext(current->state_cthread_structure[i]);
					free(cpool_arg);
				} else {
					/* And we wait for this thread to have really started */
					while (current->state[i] != 0) {
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0)
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : cond-wait on current->state_cthread_structure[%d] until it is == 0\n",
								_Cpool_self(),_Cthread_self(), i);
#endif
						Cthread_cond_wait_ext(current->state_cthread_structure[i]);
					}
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0)
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on current->state_cthread_structure[%d]\n",
							_Cpool_self(),_Cthread_self(), i);
#endif
					Cthread_mutex_unlock_ext(current->state_cthread_structure[i]);
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0)
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : Thread No %d started\n",
							_Cpool_self(),_Cthread_self(),i);
#endif
				}
			} else {
				serrno = SEINTERNAL;
				current->cid[i] = -1;
			}
#ifndef _WIN32
		}
#endif /* _WIN32 */
		if (current->cid[i] < 0) {
			/* Error at cthread_create : we clean fd created an try the */
			/* next iteration                                           */
#ifndef _WIN32
			if (Cthread_environment() == CTHREAD_MULTI_PROCESS) {
				for (j=0; j <= 3; j++) {
					close(tubes[j]);
				}
			}
#endif /* _WIN32 */
		} else {
#ifndef _WIN32
			if (Cthread_environment() == CTHREAD_MULTI_PROCESS) {
				current->writefd[i] = p_to_c[1];
				current->readfd[i] = c_to_p[0];
				to_close[k++] = p_to_c[0];
				to_close[k++] = c_to_p[1];
			}
#endif /* _WIN32 */
			/* We count number of created processes */
			++nbcreated;
		}
	}

#ifndef _WIN32
	if (Cthread_environment() == CTHREAD_MULTI_PROCESS) {
		for (j=0; j < k; j++) {
			close(to_close[j]);
		}
		free(to_close);
	}
#endif /* _WIN32 */

	/* We update the return value */
	if (nbget != NULL)
		*nbget = nbcreated;

	/* We initialize the element structure */
	current->poolnb = ++poolnb;                  /* Pool number */
	current->nbelem = nbcreated;                 /* Number of threads in it */
	current->forceid = -1;                       /* Force thread index assignment */
	previous->next = current;                    /* Next pool */
	if (pooladdr != NULL) {
		*pooladdr = current;
	}
#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_create : un-lock on &Cpool\n",
			_Cpool_self(),_Cthread_self(), i);
#endif
	Cthread_mutex_unlock(&Cpool);
  
	return(current->poolnb);
}

/* ============================================ */
/* Routine  : _Cpool_starter                    */
/* Arguments: address of arguments              */
/*            (used only within threads)        */
/* -------------------------------------------- */
/* Output   : dummy (not used)                  */
/* -------------------------------------------- */
/* History:                                     */
/* 14-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
/* Nota: the prototype of this function is like */
/* this just to agree with the prototype of     */
/* Cthread_create and al.                       */
/* ============================================ */
void *_Cpool_starter(arg)
	void *arg;
{
#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter(0x%lx)\n",
			_Cpool_self(),_Cthread_self(),(unsigned long) arg);
#endif

#ifndef _WIN32
	if (Cthread_environment() == CTHREAD_MULTI_PROCESS) {
		/*----------------------- */
		/* Non-Thread only        */
		/*----------------------- */
		int         p_to_c[2];
		int         c_to_p[2];
		int         ppid;
		void       *thisarg;
		int         ready = 1;
		size_t      thislength;
		void     *(*routine) _PROTO((void *));
		int         sleep_flag;
    
		/* We get the argument */
		p_to_c[0] = tubes[0];
		p_to_c[1] = tubes[1];
		c_to_p[0] = tubes[2];
		c_to_p[1] = tubes[3];
		ppid      = tubes[4];
    
		/* We close unnecessary fd's */
    
		close(p_to_c[1]);
		close(c_to_p[0]);
    
		while (1) {
			/* We send a flag in c_to_p[1] to say that we are ready */
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : Write ready flag with timeout\n",
					_Cpool_self(),_Cthread_self());
#endif
		  _cpool_multi_process_again:
			sleep_flag = 0;
			while (1) {
				if (_Cpool_writen_timeout(
#ifdef CPOOL_DEBUG
						__FILE__,__LINE__,
#endif
						c_to_p[1],&ready,sizeof(int),_CPOOL_STARTER_TIMEOUT) != 
					sizeof(int)) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : errno No %d (%s)\n",
							_Cpool_self(),_Cthread_self(),errno,strerror(errno));
						log(LOG_INFO,"[Cpool  [%2d][%2d]] . bis repetita .  : serrno No %d (%s)\n",
							_Cpool_self(),_Cthread_self(),errno,strerror(errno));
					}
#endif
					if (serrno == SETIMEDOUT) {
						/* Timeout */
						/* We check that the parent is still there */
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0)
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : kill(%d,0)\n",
								_Cpool_self(),_Cthread_self(),ppid);
#endif
						if (kill(ppid,0) != 0) {
							/* Nope... */
							return(NULL);
						}
						continue;
					} else {
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0)
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : serrno (%d) != SETIMEDOUT (%d). End of this process.\n",
								_Cpool_self(),_Cthread_self(),serrno,SETIMEDOUT);
#endif
						/* Error */
						return(NULL);
					}
				} else {
					break;
				}
			}
			/* And now we wait for something in the read tube */
			/* We read the address of the start routine, unless it is _CPOOL_SLEEP_FLAG */
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : Read startroutine address with timeout on fd = %d\n",
					_Cpool_self(),_Cthread_self(),p_to_c[0]);
#endif
			while (1) {
				if (_Cpool_readn_timeout(
#ifdef CPOOL_DEBUG
						__FILE__,__LINE__,
#endif
						p_to_c[0],&routine,sizeof(void *),_CPOOL_STARTER_TIMEOUT) !=
					sizeof(void *)) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : errno No %d (%s)\n",
							_Cpool_self(),_Cthread_self(),errno,strerror(errno));
						log(LOG_INFO,"[Cpool  [%2d][%2d]] . bis repetita .  : serrno No %d (%s)\n",
							_Cpool_self(),_Cthread_self(),errno,strerror(errno));
					}
#endif
					if (serrno == SETIMEDOUT) {
						/* Timeout */
						/* We check that the parent is still there */
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0)
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : kill(%d,0)\n",_Cpool_self(),_Cthread_self(),ppid);
#endif
						if (kill(ppid,0) != 0) {
							/* Nope... */
							return(NULL);
						}
						continue;
					} else {
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0)
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : serrno (%d) != SETIMEDOUT (%d). End of this process.\n",
								_Cpool_self(),_Cthread_self(),serrno,SETIMEDOUT);
#endif
						/* Error */
						return(NULL);
					}
				} else {
					if (routine == (void *(*) _PROTO((void *))) _CPOOL_SLEEP_FLAG) {
						/* We just had a hit from Cpool_next_index, that sent us the sleep flag */
						sleep_flag = 1;
					}
					break;
				}
			}

			if (sleep_flag == 1) {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] -> Sleep flag received. Looping again\n",_Cpool_self(),_Cthread_self());
#endif
				goto _cpool_multi_process_again;
			}

#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] -> startroutine is at 0x%lx\n",_Cpool_self(),_Cthread_self(),(unsigned long) routine);
#endif
			/* In a non-thread environment we read the argument length and content */
			if (_Cpool_readn_timeout(
#ifdef CPOOL_DEBUG
					__FILE__,__LINE__,
#endif
					p_to_c[0],&thislength,sizeof(size_t),_CPOOL_STARTER_TIMEOUT) != 
				sizeof(size_t)) 
				return(NULL);
			if (thislength > 0) {
				if ((thisarg = malloc(thislength)) == NULL)
					return(NULL);
				if (_Cpool_readn_timeout(
#ifdef CPOOL_DEBUG
						__FILE__,__LINE__,
#endif
						p_to_c[0],thisarg,thislength,_CPOOL_STARTER_TIMEOUT) != 
					thislength)
					return(NULL);
			} else {
				thisarg = NULL;
			}
			/* We execute the routine */
			routine(thisarg);
			/* We free memory if needed */
			if (thisarg != NULL)
				free(thisarg);
		}
	} else {
#endif /* _WIN32 */
		{
			/*----------------------- */
			/* Thread only			  */
			/*----------------------- */
			struct Cpool_t *current;
			int 			index;
			char		   *dummy;
			void		  *(*start) _PROTO((void *));
			void		   *startarg;
			void *lock_parent_cthread_structure;
			/* We receive in the argument the address of the pool structure */
			/* And the index of our thread in this structure                */
			current = (struct Cpool_t *) * (struct Cpool_t **) arg;
			dummy = arg;
			dummy  += sizeof(struct Cpool_t *);
			index   = (int) * (int *) dummy;
			lock_parent_cthread_structure = current->lock_parent_cthread_structure;
		  
			/* And we free this memory */
			free(arg);

#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : lock on current->state_cthread_structure[%d]\n",_Cpool_self(),_Cthread_self(), index);
			}
#endif
			  
			/* We put a lock on the state index */
			if (Cthread_mutex_lock_ext(current->state_cthread_structure[index]) != 0) {
				return(NULL);
			}
			
			/* This is happening at startup only : parent is waiting on us */
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : set current->state[%d] to 0\n",
					_Cpool_self(),_Cthread_self(),index);
			}
#endif
			current->state[index] = 0;
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : cond-signal on current->state_cthread_structure[%d]\n",
					_Cpool_self(),_Cthread_self(),index);
			}
#endif
			Cthread_cond_signal_ext(current->state_cthread_structure[index]);
					
			/* We loop indefinitely */
			while (1) {
			  
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : lock on lock_parent_cthread_structure\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
			  
				/* We check current->flag */
				if (Cthread_mutex_lock_ext(lock_parent_cthread_structure) != 0) {
					/* Error */
					return(NULL);
				}
			  
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : current->flag is %d\n",
						_Cpool_self(),_Cthread_self(),current->flag);
				}
#endif
			  
				if (current->flag == -1) {
					/* Either the parent is waiting for any of us, or for us only */
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : current->forceid is %d\n",
							_Cpool_self(),_Cthread_self(),current->forceid);
					}
#endif
				  
					if (current->forceid == -1 || current->forceid == index) {
						/* The parent is waiting for: */
						/* -1   : any of us           */
						/* index: us, exactly         */
						/* We tell what is our index  */
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : Parent waiting on %s (current->forceid=%d), setting current->flag to my index %d\n",
								_Cpool_self(),_Cthread_self(),current->forceid == -1 ? "any of us" : "me!", current->forceid, index);
						}
#endif
						current->flag = index;
						/* And we signal              */
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : cond-signal on lock_parent_cthread_structure\n",
								_Cpool_self(),_Cthread_self());
						}
#endif
						if (Cthread_cond_signal_ext(lock_parent_cthread_structure)) {
							/* Error */
#ifdef CPOOL_DEBUG
							if (Cpool_debug != 0) {
								log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : Signal on lock_parent_cthread_structure error: %s\n",
									_Cpool_self(),_Cthread_self(), strerror(errno));
							}
#endif
#ifdef CPOOL_DEBUG
							if (Cpool_debug != 0) {
								log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : un-lock on lock_parent_cthread_structure error\n",
									_Cpool_self(),_Cthread_self());
							}
#endif
							Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
							return(NULL);
						}
					}
				}
			  
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : un-lock on lock_parent_cthread_structure\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
			  
				Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
			  
				/* We wait until there is something new      */
				/* The lock on "current->state[index]" is    */
				/* then released                             */
				
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : current->state[%d] is %d\n",
						_Cpool_self(),_Cthread_self(),index,current->state[index]);
				}
#endif
				while ( current->state[index] == 0 ) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : cond-wait on current->state_cthread_structure[%d] until it is != 0\n",
							_Cpool_self(),_Cthread_self(),index);
					}
#endif
					if (Cthread_cond_wait_ext(current->state_cthread_structure[index]) != 0) {
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : wait on current->state_cthread_structure[%d] error: %s\n",
								_Cpool_self(),_Cthread_self(),index, strerror(errno));
						}
#endif
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : un-lock on current->state_cthread_structure[%d]\n",
								_Cpool_self(),_Cthread_self(),index);
						}
#endif
						Cthread_mutex_unlock_ext(current->state_cthread_structure[index]);
						return(NULL);
#ifdef CPOOL_DEBUG
					} else {
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : waked up on on current->state_cthread_structure[%d] with current->state[%d] == %d\n",
								_Cpool_self(),_Cthread_self(),index, index, current->state[index]);
						}
#endif
					}
				}
			  
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : out of cond-wait loop, current->state[%d] is %d\n",
						_Cpool_self(),_Cthread_self(),index,current->state[index]);
				}
#endif
			  
				/* We are waked up: the routine and its arguments */
				/* address are put in current->start[index] and   */
				/* current->arg[index]                            */
				start    = (void *(*) _PROTO((void *))) current->start[index];
				startarg = (void *)            current->arg[index];
			  
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : Execute 0x%lx(0x%lx)\n",
						_Cpool_self(),_Cthread_self(),(unsigned long) start,(unsigned long) (startarg != NULL ? startarg : 0));
				}
#endif
			  
				/* We execute the routine                         */
				(*start)(startarg);
			  
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : Finished 0x%lx(0x%lx)\n",
						_Cpool_self(),_Cthread_self(),(unsigned long) start,(unsigned long) (startarg != NULL ? startarg : 0));
				}
#endif
			  
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : Setting current->state[%d] to 0\n",
						_Cpool_self(),_Cthread_self(),index);
				}
#endif
				
				/* We flag us in the non-running state */
				current->state[index] = 0;

			}

#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_starter : un-lock on current->state_cthread_structure[%d]\n",
					_Cpool_self(),_Cthread_self(),index);
			}
#endif
			Cthread_mutex_unlock_ext(current->state_cthread_structure[index]);
			  
		}
#ifndef _WIN32
	}
#endif
}

#ifndef _WIN32
/* ============================================ */
/* Routine  : _Cpool_writen_timeout             */
/* Arguments: file des., pointer, size, timeout */
/* -------------------------------------------- */
/* Output   : Number of bytes writen            */
/* -------------------------------------------- */
/* History:                                     */
/* 17-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
size_t _Cpool_writen_timeout(
#ifdef CPOOL_DEBUG
	file,line,
#endif
	fd,vptr,n,timeout)
#ifdef CPOOL_DEBUG
	char *file;
	int line;
#endif
	int fd;
	void *vptr;
	size_t n;
	int timeout;
{
	size_t		nleft;
	ssize_t       nwriten;
	char         *ptr;
	int           save_serrno;

	/* We use the signal alarm */
	Sigfunc  *sigfunc;
  
#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_writen_timeout(%d,0x%lx,0x%x,%d) called at %s:%d\n",
			_Cpool_self(),_Cthread_self(),fd,(unsigned long) vptr, (unsigned int) n, timeout,
			file,line);
#endif

	/* Get previous handler */
	if ((sigfunc = _Cpool_signal(SIGALRM, _Cpool_alarm)) == SIG_ERR) {
		serrno = SEINTERNAL;
		return(0);
	}
  
	/* In any case we catch trap SIGPIPE */
	_Cpool_signal(SIGPIPE, SIG_IGN);
  
	ptr = vptr;
	nleft = n;
	while (nleft > 0) {
		alarm(timeout);
		if ( (nwriten = write(fd, ptr, nleft)) <= 0) {
			if (errno == EINTR) {
				errno = ETIMEDOUT;
				serrno = SETIMEDOUT;
			}
			goto doreturn;
		}    
		nleft -= nwriten;
		ptr   += nwriten;
	}
  doreturn:
	save_serrno = serrno;
	/* Disable alarm            */
	alarm(0);
	/* Restore previous handler */
	_Cpool_signal(SIGALRM, sigfunc);
	serrno = save_serrno;
	return(n - nleft);
}

/* ============================================ */
/* Routine  : _Cpool_writen                     */
/* Arguments: file des., pointer, size          */
/* -------------------------------------------- */
/* Output   : Number of bytes writen            */
/* -------------------------------------------- */
/* History:                                     */
/* 14-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
size_t _Cpool_writen(fd,vptr,n)
	int fd;
	void *vptr;
	size_t n;
{
	size_t		nleft;
	ssize_t       nwriten;
	char         *ptr;
	Sigfunc          *handler;

#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_writen(%d,0x%lx,0x%x)\n",
			_Cpool_self(),_Cthread_self(),fd,(unsigned long) vptr, (unsigned int) n);
#endif

	/* In any case we catch trap SIGPIPE */
	handler = _Cpool_signal(SIGPIPE, SIG_IGN);

	ptr = vptr;
	nleft = n;
	while (nleft > 0) {
		if ( (nwriten = write(fd, ptr, nleft)) <= 0) {
			if (errno == EINTR) {
				nwriten = 0;		/* and call write() again */
			} else {
				_Cpool_signal(SIGPIPE, handler);
				return(n - nleft);
			}
		}
		nleft -= nwriten;
		ptr   += nwriten;
	}
	_Cpool_signal(SIGPIPE, handler);
	return(n - nleft);
}

/* ============================================ */
/* Routine  : _Cpool_readn                      */
/* Arguments: file des., pointer, size          */
/* -------------------------------------------- */
/* Output   : Number of bytes read              */
/* -------------------------------------------- */
/* History:                                     */
/* 14-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
size_t _Cpool_readn(fd,vptr,n)
	int fd;
	void *vptr;
	size_t n;
{
	size_t	nleft;
	ssize_t   nread;
	char	*ptr;
	Sigfunc          *handler;
  
#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_readn(%d,0x%lx,0x%x)\n",
			_Cpool_self(),_Cthread_self(),fd,(unsigned long) vptr, (unsigned int) n);
#endif

	/* In any case we catch trap SIGPIPE */
	if ((handler = _Cpool_signal(SIGPIPE, SIG_IGN)) == SIG_ERR) {
		return(0);
	}
	_Cpool_signal(SIGPIPE, SIG_IGN);

	ptr = vptr;
	nleft = n;
	nread = 0;
	while (nleft > 0) {
		if ( (nread = read(fd, ptr, nleft)) < 0) {
			if (errno == EINTR) {
				nread = 0;		/* and call read() again */
			} else {
				_Cpool_signal(SIGPIPE, handler);
				return(n - nleft);
			}
		} else if (nread == 0) {
			break;				/* EOF */
		} else {
			nleft -= nread;
			ptr   += nread;
		}
	}
	_Cpool_signal(SIGPIPE, handler);
	return(n - nleft);		/* return >= 0 */
}

/* ============================================ */
/* Routine  : _Cpool_readn_timeout              */
/* Arguments: file des., pointer, size, timeout */
/* -------------------------------------------- */
/* Output   : Number of bytes read              */
/* -------------------------------------------- */
/* History:                                     */
/* 14-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
size_t _Cpool_readn_timeout(
#ifdef CPOOL_DEBUG
	file,line,
#endif
	fd,vptr,n,timeout)
#ifdef CPOOL_DEBUG
	char *file;
	int line;
#endif
	int fd;
	void *vptr;
	size_t n;
	int timeout;
{
	size_t	nleft;
	ssize_t   nread;
	char	*ptr;
	int       save_serrno;

	/* We use the signal alarm */
	Sigfunc  *sigfunc;
  
#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_readn_timeout(%d,0x%lx,0x%x,%d) called at %s:%d\n",
			_Cpool_self(),_Cthread_self(),fd,(unsigned long) vptr, (unsigned int) n, timeout,
			file,line);
#endif

	/* Get previous handler */
	if ((sigfunc = _Cpool_signal(SIGALRM, _Cpool_alarm)) == SIG_ERR) {
		serrno = SEINTERNAL;
		return(0);
	}

	/* In any case we trap SIGPIPE */
	_Cpool_signal(SIGPIPE, SIG_IGN);
  
	ptr = vptr;
	nleft = n;
	nread = 0;
	while (nleft > 0) {
		alarm(timeout);
#ifdef CPOOL_DEBUG
		if (Cpool_debug != 0)
			log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_readn_timeout : read(%d,0x%lx,%d)\n",
				_Cpool_self(),_Cthread_self(),fd,(unsigned long) ptr, (int) nleft);
#endif
		if ( (nread = read(fd, ptr, nleft)) < 0) {
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_readn_timeout : errno = %d (%s) [EINTR=%d]\n",
					_Cpool_self(),_Cthread_self(),errno,strerror(errno),EINTR);
#endif
			if (errno == EINTR) {
				errno = ETIMEDOUT;
				serrno = SETIMEDOUT;
			}
			goto doreturn;
		} else if (nread == 0) {
			break;				/* EOF */
		} else {
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_readn_timeout : nleft (%d) -= nread (%d)\n",
					_Cpool_self(),_Cthread_self(),(int) nleft,(int) nread);
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In _Cpool_readn_timeout : ptr (0x%lx) += nread (%d)\n",
					_Cpool_self(),_Cthread_self(),(unsigned long) ptr,(int) nread);
			}
#endif
			nleft -= nread;
			ptr   += nread;
		}
	}
  doreturn:
	save_serrno = serrno;
	/* Disable alarm            */
	alarm(0);
	/* Restore previous handler */
	_Cpool_signal(SIGALRM, sigfunc);
	serrno = save_serrno;
	return(n - nleft);		/* return >= 0 */
}

/* ============================================ */
/* Routine  : _Cpool_alarm                      */
/* Arguments: signal to trap                    */
/* -------------------------------------------- */
/* Output   :                                   */
/* -------------------------------------------- */
/* History:                                     */
/* 17-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
void _Cpool_alarm(signo)
	int signo;
{
#ifdef CPOOL_DEBUG
	/* Better not to do any mutex/cond etc... in a signal handler... */
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool[...[...]]] ### SIGALRM catched\n");
#endif
	/* We just trap the signal and return */
	return;
}

/* ============================================ */
/* Routine  : _Cpool_signal                     */
/* Arguments: signal to trap, signal routine    */
/* -------------------------------------------- */
/* Output   : status (SIG_ERR if error)         */
/* -------------------------------------------- */
/* History:                                     */
/* 11-MAY-1999       First implementation       */
/*                   [with R.W.Stevens example] */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
Sigfunc *_Cpool_signal(signo,func)
	int signo;
	Sigfunc *func;
{
	struct sigaction	act, oact;
	int n = 0;

	act.sa_handler = func;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	if (signo == SIGALRM) {
#ifdef	SA_INTERRUPT
		act.sa_flags |= SA_INTERRUPT;	/* SunOS 4.x */
#endif
	} else {
#ifdef	SA_RESTART
		act.sa_flags |= SA_RESTART;		/* SVR4, 44BSD */
#endif
	}
	n = sigaction(signo, &act, &oact);
	if (n < 0) {
		return(SIG_ERR);
	}

	return(oact.sa_handler);
}

/* ============================================ */
/* Set of wrappers around memory allocation     */
/* to allow passing of arguments between        */
/* forked child in case of pools processes      */
/* assignment.                                  */
/* This has also the advantage of beeing a      */
/* run-time checker for memory allocation       */
/* and use.                                     */
/* ============================================ */

/* ============================================ */
/* Routine  : Cpool_calloc                      */
/* Arguments: nb of members, size of each       */
/* -------------------------------------------- */
/* Output   : address allocated                 */
/* -------------------------------------------- */
/* History:                                     */
/* 11-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
/* Notes:                                       */
/* This routine is a wrapper around calloc      */
/* ============================================ */
void DLL_DECL *Cpool_calloc(file,line,nmemb,size)
	char *file;
	int line;
	size_t nmemb;
	size_t size;
{
	struct Cmalloc_t *current  = &Cmalloc;
	struct Cmalloc_t *previous = &Cmalloc;
	char             *dummy;

	if (Cthread_environment() != CTHREAD_MULTI_PROCESS) {
		/* We are in multi-threaded mode : memory is shared    */
		/* and there is no need to try to know every allocated */
		/* memory in the Cmalloc linked list.                  */
		return(calloc(nmemb,size));
	}

#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_calloc(0x%x,0x%x) called at %s:%d\n",
			_Cpool_self(),_Cthread_self(),(unsigned int) nmemb, (unsigned int) size, file, line);
#endif

	/* We search the last element */
	while (current->next != NULL) {
		previous = current;
		current = current->next;
	}

	/* If there is no entry yet, then previous will be equal to &Cmalloc */

	/* We create an element */
	if ((previous->next = malloc(sizeof(struct Cmalloc_t))) == NULL) {
		return(NULL);
	}
	/* We create the requested memory */
	if ((previous->next->start = calloc(nmemb,size)) == NULL) {
		free(previous->next);
		previous->next = NULL;
		return(NULL);
	}
	/* Unfortunately "current->end += (nmemb * size)" is not ANSI-C and */
	/* will be rejected by a lot of compilers (exception I know is gcc) */
	dummy  = (char *) previous->next->start;
	dummy += ((nmemb * size) - 1);
	previous->next->end   = dummy;
	previous->next->next  = NULL;

	/* We return the result of _calloc */
	return(previous->next->start);
}

/* ============================================ */
/* Routine  : Cpool_malloc                      */
/* Arguments: size to allocate                  */
/* -------------------------------------------- */
/* Output   : address allocated                 */
/* -------------------------------------------- */
/* History:                                     */
/* 11-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
/* Notes:                                       */
/* This routine is a wrapper around malloc      */
/* ============================================ */
void DLL_DECL *Cpool_malloc(file,line,size)
	char *file;
	int line;
	size_t size;
{
	struct Cmalloc_t *current  = &Cmalloc;
	struct Cmalloc_t *previous = &Cmalloc;
	char             *dummy;

	if (Cthread_environment() != CTHREAD_MULTI_PROCESS) {
		/* We are in multi-threaded mode : memory is shared    */
		/* and there is no need to try to know every allocated */
		/* memory in the Cmalloc linked list.                  */
		return(malloc(size));
	}

#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_malloc(%d) called at %s:%d\n",
			_Cpool_self(),_Cthread_self(),(int) size, file, line);
#endif

	/* We search the last element */
	while (current->next != NULL) {
		previous = current;
		current = current->next;
	}

	/* If there is no entry yet, then previous will be equal to &Cmalloc */

	/* We create an element */
	if ((previous->next = malloc(sizeof(struct Cmalloc_t))) == NULL) {
		return(NULL);
	}
	/* We create the requested memory */
	if ((previous->next->start = malloc(size)) == NULL) {
		free(previous->next);
		previous->next = NULL;
		return(NULL);
	}
	/* Unfortunately "current->end += (nmemb * size)" is not ANSI-C and */
	/* will be rejected by a lot of compilers (exception I know is gcc) */
	dummy  = (char *) previous->next->start;
	dummy += (size - 1);
	previous->next->end  = (void *) dummy;
	previous->next->next = NULL;

	/* We return the result of _malloc */
	return(previous->next->start);
}

/* ============================================ */
/* Routine  : Cpool_free                        */
/* Arguments: address to free                   */
/* -------------------------------------------- */
/* Output   :                                   */
/* -------------------------------------------- */
/* History:                                     */
/* 11-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
/* Notes:                                       */
/* This routine is a wrapper around free        */
/* ============================================ */
void DLL_DECL Cpool_free(file,line,ptr)
	char *file;
	int line;
	void *ptr;
{
	struct Cmalloc_t *current  = &Cmalloc;
	struct Cmalloc_t *previous = NULL;
	int               n = 1;
	/* We test to see if the user wants to */
	/* to free something really allocated  */

	if (Cthread_environment() != CTHREAD_MULTI_PROCESS) {
		free(ptr);
		return;
	}

#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_free(0x%lx) called at %s:%d\n",
			_Cpool_self(),_Cthread_self(),(unsigned long) ptr, file, line);
#endif

	while (current->next != NULL) {
		previous = current;
		current = current->next;

		if (current->start == ptr) {
			n = 0;
			break;
		}
	}

	if (n) {
		/* ERROR */
		errno = EINVAL;
		return;
	}

	free(ptr);

	/* We update pointers */
	if (previous != NULL) {
		previous->next = current->next;
	} else {
		/* No more entry... */
		Cmalloc.next = NULL;
	}
	free(current);
	return;
}

/* ============================================ */
/* Routine  : Cpool_realloc                     */
/* Arguments: address to realloc, its size      */
/* -------------------------------------------- */
/* Output   :                                   */
/* -------------------------------------------- */
/* History:                                     */
/* 11-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
/* Notes:                                       */
/* This routine is a wrapper around realloc     */
/* ============================================ */
void DLL_DECL *Cpool_realloc(file,line,ptr,size)
	char *file;
	int line;
	void *ptr;
	size_t size;
{
	struct Cmalloc_t *current  = &Cmalloc;
	void             *result;
	int               n = 1;
	char             *dummy;

	if (Cthread_environment() != CTHREAD_MULTI_PROCESS) {
		return(realloc(ptr,size));
	}

#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_realloc(0x%lx,%d) called at %s:%d\n",
			_Cpool_self(),_Cthread_self(),(unsigned long) ptr, (int) size, file, line);
#endif

	/* We search the correct element */
	while (current->next != NULL) {
		current = current->next;
		if (current->start == ptr) {
			n = 0;
			break;
		}
	}

	if (n) {
		/* Error */
		errno = EINVAL;
		return(NULL);
	}

	if ((result = realloc(ptr,size)) == NULL) {
		return(NULL);
	}

	/* We update the pointer */
	current->start = result;
	/* Unfortunately "current->end += (nmemb * size)" is not ANSI-C and */
	/* will be rejected by a lot of compilers (exception I know is gcc) */
	dummy          = result;
	dummy         += (size - 1);
	current->end   = dummy;

	/* We return the result */
	return(result);
}
#endif /* _WIN32 */

/* ============================================ */
/* Routine  : Cpool_assign                      */
/* Arguments: pool number                       */
/*            starting routine                  */
/*            arguments address                 */
/*            timeout                           */
/* -------------------------------------------- */
/* Output   : 0 (OK) -1 (ERROR)                 */
/* -------------------------------------------- */
/* History:                                     */
/* 17-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
int DLL_DECL Cpool_assign(poolnb,startroutine,arg,timeout)
	int poolnb;
	void *(*startroutine) _PROTO((void *));
	void *arg;
	int timeout;
{
	return(Cpool_assign_ext(poolnb,NULL,startroutine,arg,timeout));
}

/* ============================================ */
/* Routine  : Cpool_assign_ext                  */
/* Arguments: pool number                       */
/*            pool address                      */
/*            starting routine                  */
/*            arguments address                 */
/*            timeout                           */
/* -------------------------------------------- */
/* Output   : 0 (OK) -1 (ERROR)                 */
/* -------------------------------------------- */
/* History:                                     */
/* 17-MAY-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* 04-SEP-2003       Added pooladdr             */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
int DLL_DECL Cpool_assign_ext(poolnb,pooladdr,startroutine,arg,timeout)
	int poolnb;
	void *pooladdr;
	void *(*startroutine) _PROTO((void *));
	void *arg;
	int timeout;
{

	/* We makes sure that Cthread pakage is initalized */
	Cthread_init();
  
#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign(%d,0x%lx,0x%lx,%d)\n",
			_Cpool_self(),_Cthread_self(),poolnb, (unsigned long) startroutine, (unsigned long) arg, timeout);
#endif
    
#ifndef _WIN32
	/* THIS ROUTINE IS EXPLICITELY SPLITTED IN TWO PARTS    */
	/* - The _NOCTHREAD one (using pipes)                   */
	/* - The _CTHREAD one (using shared mem. and cond. var. */
	if (Cthread_environment() == CTHREAD_MULTI_PROCESS) {
		/*----------------------- */
		/* Non-Thread only        */
		/*----------------------- */
		struct Cpool_t *current = &Cpool;
		int    found;
		size_t length_vars;
		struct timeval          tv;
		int                     maxfd;
		int                     i;
		fd_set                  readlist;
		int                     ready;
    
		if (poolnb < 0) {
			serrno = EINVAL;
			return(-1);
		}
    
		/* In case of a non-thread environment we check that */
		/* the argument is in a windows allocated memory or  */
		/* not.                                              */
		found = 0;
		if (arg != NULL) {
			struct Cmalloc_t *current  = &Cmalloc;
			struct Cmalloc_t *previous = NULL;
      
			while (current->next != NULL) {
				previous = current;
				current = current->next;
        
				if (current->start <= arg && arg <= current->end) {
					found = 1;
					length_vars  = (size_t) current->end;
					length_vars -= (size_t) arg;
					length_vars++;
					break;
				}
			}
			if (! found) {
				/* Not in a known window : we assume it is not longer   */
				/* than the maximum size of any typedef, itself assumed */
				/* to be 8 bytes.                                       */
				length_vars = 8;
			}
		} else {
			length_vars = 0;
		}
#ifdef CPOOL_DEBUG
		if (Cpool_debug != 0)
			log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : length_vars set to %d\n",
				_Cpool_self(),_Cthread_self(),(int) length_vars);
#endif
    
		found = 0;
		if (pooladdr != NULL) {
			found = 1;
			current = pooladdr;
		} else {
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : lock on &Cpool\n",
					_Cpool_self(),_Cthread_self(),(int) length_vars);
#endif
			if (Cthread_mutex_lock(&Cpool) != 0) {
				return(-1);
			}
			/* We search the corresponding pool */
			current = &Cpool;
			while ((current = current->next) != NULL) {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Wanted pool is %d, Got pool No %d\n",
						_Cpool_self(),_Cthread_self(),poolnb,current->poolnb);
#endif
				if (current->poolnb == poolnb) {
					found = 1;
					break;
				}
			}
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on &Cpool\n",
					_Cpool_self(),_Cthread_self(),(int) length_vars);
#endif
			Cthread_mutex_unlock(&Cpool);
		}
    
		if (found == 0) {
			serrno = EINVAL;
			return(-1);
		}
    
		/* We collect all the fd's to read from */
#ifdef CPOOL_DEBUG
		if (Cpool_debug != 0)
			log(LOG_INFO,"[Cpool  [%2d][%2d]] Wait flag on ",
				_Cpool_self(),_Cthread_self());
#endif
		maxfd = 0;
		FD_ZERO(&readlist);
		for (i = 0; i < current->nbelem; i++) {
			/* We take care of any previous call to Cpool_next_index */
			if (current->forceid != -1) {
				if (i != current->forceid) {
					continue;
				}
			}
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"%d ",current->readfd[i]);
#endif
			if (current->readfd[i] > maxfd)
				maxfd = current->readfd[i];
			FD_SET(current->readfd[i],&readlist);
		}
#ifdef CPOOL_DEBUG
		if (Cpool_debug != 0)
			log(LOG_INFO,"\n");
#endif
    
		/* We wait for a flag of any of the child */
		if (timeout > 0) {
			tv.tv_sec  = timeout;
			tv.tv_usec = 0;
			/* Possible warning on hpux 10 because cma_select expects "int" */
			/* instead of "fd_set"                                          */
			if (select(maxfd+1, (_cpool_fd_set *) &readlist, NULL, NULL, &tv) <= 0) {
				/* Error or timeout */
				/* ... We reset any previous call to Cpool_next_index */
				current->forceid = -1;
				serrno = SETIMEDOUT;
				return(-1);
			}
		} else {
			/* Possible warning on hpux 10 because cma_select expects "int" */
			/* instead of "fd_set"                                          */
			if (select(maxfd+1, (_cpool_fd_set *) &readlist, NULL, NULL, NULL) < 0) {
				/* Error */
				/* ... We reset any previous call to Cpool_next_index */
				current->forceid = -1;
				serrno = SEINTERNAL;
				return(-1);
			}
		}
    
		/* We got one, let's know which one it is */
		i = 0;
		for (i = 0; i <= current->nbelem; i++) {
			if (FD_ISSET(current->readfd[i],&readlist)) {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] Got flag on %d\n",
						_Cpool_self(),_Cthread_self(),current->readfd[i]);
#endif
				/* We read the flag */
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] Read the flag on %d\n",
						_Cpool_self(),_Cthread_self(),current->readfd[i]);
#endif
				_Cpool_readn(current->readfd[i], (void *) &ready, sizeof(int));
				/* And we send the arguments */
				/* ... address of the start routine */
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] Write routine address = 0x%lx on %d\n",
						_Cpool_self(),_Cthread_self(),(unsigned long) startroutine,current->writefd[i]);
#endif
				_Cpool_writen(current->writefd[i],(void *) &startroutine,sizeof(void *));
				/* In a non-thread environment we write the argument length and content */
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] Write argument length = %d on %d\n",
						_Cpool_self(),_Cthread_self(),(int) length_vars,current->writefd[i]);
#endif
				_Cpool_writen(current->writefd[i],(void *) &length_vars,sizeof(size_t));
				/* And the arguments themselves */
				if (length_vars > 0) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0)
						log(LOG_INFO,"[Cpool  [%2d][%2d]] Write argument content of size %d on %d\n",
							_Cpool_self(),_Cthread_self(),(int) length_vars,current->writefd[i]);
#endif
					_Cpool_writen(current->writefd[i],arg,length_vars);
				}
				break;
			}
		}
    
		/* ... We reset any previous call to Cpool_next_index */
		current->forceid = -1;

		/* Return OK */
		return(0);

	} else {
#endif /* _WIN32 */
		{
			/*----------------------- */
			/* Thread only            */
			/*----------------------- */
			struct Cpool_t *current = &Cpool;
			int    found;
			int    i;
			void *lock_parent_cthread_structure;
		  
			if (poolnb < 0) {
				errno = EINVAL;
				return(-1);
			}
		  
			found = 0;
			if (pooladdr != NULL) {
				current = pooladdr;
				found = 1;
				lock_parent_cthread_structure = current->lock_parent_cthread_structure;
			} else {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : lock on &Cpool\n",
						_Cpool_self(),_Cthread_self());
#endif
				if (Cthread_mutex_lock(&Cpool) != 0) {
					return(-1);
				}
				/* We search the corresponding pool */
				current = &Cpool;
				while ((current = current->next) != NULL) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Wanted pool is %d, Got pool No %d\n",
							_Cpool_self(),_Cthread_self(),poolnb,current->poolnb);
					}
#endif
					if (current->poolnb == poolnb) {
						found = 1;
						lock_parent_cthread_structure = current->lock_parent_cthread_structure;
						break;
					}
				}
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on &Cpool\n",
						_Cpool_self(),_Cthread_self());
#endif
				Cthread_mutex_unlock(&Cpool);
			}
		  
			if (found == 0) {
				serrno = EINVAL;
				return(-1);
			}
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : lock on lock_parent_cthread_structure\n",
					_Cpool_self(),_Cthread_self());
			}
#endif
		  
			/* We assume that we will not have to wait   */
			if (Cthread_mutex_lock_ext(lock_parent_cthread_structure) != 0) {
				return(-1);
			}
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Setting current->flag to -2 (parent not waiting)\n",
					_Cpool_self(),_Cthread_self());
			}
#endif

			current->flag = -2;
		  
			/* We check if there is one thread available */
			found = 0;
		  
			for (i = 0; i < current->nbelem; i++) {

				if (current->forceid != -1) {
					if (i != current->forceid) {
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : current->forceid=%d, skipped index %d\n",
								_Cpool_self(),_Cthread_self(), current->forceid, i);
						}
#endif
						continue;
					}
				}
#ifdef CPOOL_DEBUG
				if (current->forceid == -1) {
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : try-lock on current->state_cthread_structure[%d]\n",
							_Cpool_self(),_Cthread_self(), i);
					}
				} else {
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : lock on current->state_cthread_structure[%d], timeout=%d\n",
							_Cpool_self(),_Cthread_self(), i, timeout);
					}
				}
#endif
				if (((current->forceid == -1) ? Cthread_mutex_trylock_ext(current->state_cthread_structure[i]) : Cthread_mutex_timedlock_ext(current->state_cthread_structure[i], timeout)) == 0) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : current->state[%d]=%d, current->forceid=%d\n",
							_Cpool_self(),_Cthread_self(), i, current->state[i], current->forceid);
					}
#endif
					if (current->state[i] == 0) {
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Found thread at index %d\n",
								_Cpool_self(),_Cthread_self(),i);
						}
#endif
						
						/* Okay: We change the predicate */
						/* value on which the thread is  */
						/* waiting for to be changed     */
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Setting current->state[%d] to 1\n",
								_Cpool_self(),_Cthread_self(),i);
						}
#endif
						current->state[i] = 1;
						/* We put the routine and its    */
						/* arguments                     */
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Setting current->start = 0x%lx, i = %d, stored at 0x%lx\n",
								_Cpool_self(),_Cthread_self(),
								(unsigned long) current->start,i,
								(unsigned long) (current->start + (i * sizeof(void *))));
						}
#endif
						current->start[i] = (void *(*) _PROTO((void *))) startroutine;
						current->arg[i] = (void *) arg;
						/* We signal the thread          */
						
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Set current->forceid to -1\n",
								_Cpool_self(),_Cthread_self());
						}
#endif
						/* ... We reset any call to Cpool_next_index */
						current->forceid = -1;
						
						/* Note: Only one thread can wait on that condition variable */
						/* It means that we are sure that Cthread will internal use */
						/* a pure cond_signal instead of a cond_broadcast - this is */
						/* better */
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : cond-signal on current->state_cthread_structure[%d]\n",
								_Cpool_self(),_Cthread_self(), i);
						}
#endif
						if (Cthread_cond_signal_ext(current->state_cthread_structure[i]) != 0) {
#ifdef CPOOL_DEBUG
							if (Cpool_debug != 0) {
								log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Oooups... Cthread_cond_signal_ext(current->state_cthread_structure[%d]) error (%s)\n",
									_Cpool_self(),_Cthread_self(),i,strerror(errno));
							}
#endif
#ifdef CPOOL_DEBUG
							if (Cpool_debug != 0) {
								log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on current->state_cthread_structure[%d]\n",
									_Cpool_self(),_Cthread_self(),i);
							}
#endif
							Cthread_mutex_unlock_ext(current->state_cthread_structure[i]);
#ifdef CPOOL_DEBUG
							if (Cpool_debug != 0) {
								log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on lock_parent_cthread_structure\n",
									_Cpool_self(),_Cthread_self());
							}
#endif
							Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
							return(-1);
						}
						
						/* We remember for the immediate out of this loop... */
						found = 1;
				  
						/* And we exit of the loop       */
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on current->state_cthread_structure[%d]\n",
								_Cpool_self(),_Cthread_self(), i);
						}
#endif
						Cthread_mutex_unlock_ext(current->state_cthread_structure[i]);
						break;
					} else {
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on current->state_cthread_structure[%d]\n",
								_Cpool_self(),_Cthread_self(), i);
						}
#endif
						Cthread_mutex_unlock_ext(current->state_cthread_structure[i]);
					}
#ifdef CPOOL_DEBUG
				} else {
					if (current->forceid == -1) {
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : try-lock on current->state_cthread_structure[%d] failed: %s\n",
								_Cpool_self(),_Cthread_self(), i, strerror(errno));
						}
					} else {
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : lock on current->state_cthread_structure[%d] failed: %s\n",
								_Cpool_self(),_Cthread_self(), i, strerror(errno));
						}
					}
#endif
				}
			}
		  
			if (found != 0) {
				/* We found at least one thread available: */
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on lock_parent_cthread_structure\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
		  
				Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
				return(0);
			} else {
				/* This is an error if a there was a previous call to Cpool_next_index(): then Cpool_assign() did try to */
				/* assign the current->forceid and only to it. Suppose that user gave a timeout of zero: it is possible */
				/* that Cpool_assign() tried to get lock on current->state_cthread_structure[] before the child putted */
				/* itself in condition wait. Then error is EBUSY. A safe way to work is to call: */
				/* Cpool_next_index() with a timeout */
				/* Cpool_assign() with no timeout (or a timeout large enough (?)) */
				if (current->forceid != -1) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Set current->forceid to -1\n",
							_Cpool_self(),_Cthread_self());
					}
#endif
					current->forceid = -1;
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on lock_parent_cthread_structure\n",
							_Cpool_self(),_Cthread_self());
					}
#endif
		  
					Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
					return(-1);
				}
			}
		  
			/* We did not found one thread available */
			/* We say that we are waiting            */
		  
			if (timeout == 0) {
				/* No thread immediately available, and timeout == 0 */
				/* So we exit immediately                            */
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Set current->forceid to -1\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
				current->forceid = -1;
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on lock_parent_cthread_structure and setting SEWOULDBLOCK\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
				
				Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
				serrno = SEWOULDBLOCK;
				return(-1);
			}
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Setting current->flag to -1 (parent waiting)\n",
					_Cpool_self(),_Cthread_self());
			}
#endif
		  
			current->flag = -1;
		  
			/* And we wait on our predicate          */
			while (current->flag == -1) {
				if (timeout > 0) {
				  
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : cond-timedwait on lock_parent_cthread_structure with timeout = %d seconds until current->flag != -1\n",
							_Cpool_self(),_Cthread_self(),timeout);
					}
#endif
				  
					if (Cthread_cond_timedwait_ext(lock_parent_cthread_structure,timeout) != 0) {
					  
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Waiting condition failed, reset current->flag to -2 (parent not waiting)\n",
								_Cpool_self(),_Cthread_self());
						}
#endif
					  
						current->flag = -2;
					  
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on lock_parent_cthread_structure\n",
								_Cpool_self(),_Cthread_self());
						}
#endif
					  
						Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
					  
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Set current->forceid to -1\n",
								_Cpool_self(),_Cthread_self());
						}
#endif
						/* ... We reset any call to Cpool_next_index */
						current->forceid = -1;
					  
						return(-1);
					}
				} else {
					/* timeout < 0 : we wait, wait, wait... */
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : cond-wait on lock_parent_cthread_structure until current->flag != -1\n",
							_Cpool_self(),_Cthread_self());
					}
#endif
				  
					if (Cthread_cond_wait_ext(lock_parent_cthread_structure) != 0) {
					  
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Waiting condition failed, reset current->flag to -2 (parent not waiting)\n",
								_Cpool_self(),_Cthread_self());
						}
#endif
					  
						current->flag = -2;
					  
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Set current->forceid to -1\n",
								_Cpool_self(),_Cthread_self());
						}
#endif
						/* ... We reset any call to Cpool_next_index */
						current->forceid = -1;
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on lock_parent_cthread_structure\n",
								_Cpool_self(),_Cthread_self());
						}
#endif
					  
						Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
					  					  
						return(-1);
					}
				}
			}
		  
			/* Yes... */
			/* current->flag contains the index */
			/* of the thread that tell us it is */
			/* available                        */
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Waiting condition okay, current->flag=%d\n",
					_Cpool_self(),_Cthread_self(),current->flag);
			}
#endif
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : lock on current->state_cthread_structure[%d]\n",
					_Cpool_self(),_Cthread_self(), current->flag);
			}
#endif
		  
			/* Shall we use timeout again here !? The problem is that timeout (user parameter) has already */
			/* been used in the condition wait. In theory, now, the procedure must not fail, but it is possible */
			/* that we take some real time do do the following, time for the child to put itself on cond-wait */
			if (Cthread_mutex_lock_ext(current->state_cthread_structure[current->flag]) != 0) {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : lock on current->state_cthread_structure[%d] error: %s\n",
						_Cpool_self(),_Cthread_self(), current->flag, strerror(errno));
				}
#endif
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Reset current->flag to -2 (parent not waiting)\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
				current->flag = -2;
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Set current->forceid to -1\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
				current->forceid = -1;
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on lock_parent_cthread_structure\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
				Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
				return(-1);
			}
			/* We change child predicate        */
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Thread that answered has index %d, Setting predicate current->state[%d] to 1\n",
					_Cpool_self(),_Cthread_self(),current->flag,current->flag);
			}
#endif
		  
			current->state[current->flag] = 1;
			/* We put arguments                 */
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Setting current->start[%d] to 0x%lx and current->arg[%d] to 0x%lx\n",
					_Cpool_self(),_Cthread_self(),current->flag,(unsigned long) startroutine,current->flag,
					arg != NULL ? (unsigned long) arg : 0);
			}
#endif
		  
			/* We put the routine and its    */
			/* arguments                     */
			current->start[current->flag] = (void *(*) _PROTO((void *))) startroutine;
			current->arg[current->flag] = (void *) arg;
		  
			/* We signal the child       */
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : cond-signal on current->state_cthread_structure[%d]\n",
					_Cpool_self(),_Cthread_self(), current->flag);
			}
#endif
		  
			if (Cthread_cond_signal_ext(current->state_cthread_structure[current->flag]) != 0) {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Signalling on current->state_cthread_structure[%d] error: %s\n",
						_Cpool_self(),_Cthread_self(), current->flag, strerror(errno));
				}
#endif
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on current->state_cthread_structure[%d]\n",
						_Cpool_self(),_Cthread_self(), current->flag);
				}
#endif
				Cthread_mutex_unlock_ext(current->state_cthread_structure[current->flag]);
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Reset current->flag to -2 (parent not waiting)\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
				current->flag = -2;
				  
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Set current->forceid to -1\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
				current->forceid = -1;

#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on lock_parent_cthread_structure\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
				Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
				return(-1);
			}
			/* We release our lock on his predicate */
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on current->state_cthread_structure[%d]\n",
					_Cpool_self(),_Cthread_self(), current->flag);
			}
#endif
		  
			Cthread_mutex_unlock_ext(current->state_cthread_structure[current->flag]);
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Set current->flag to -2 (parent not waiting)\n",
					_Cpool_self(),_Cthread_self());
			}
#endif
			current->flag = -2;
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : Set current->forceid to -1\n",
					_Cpool_self(),_Cthread_self());
			}
#endif
			current->forceid = -1;
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_assign : un-lock on lock_parent_cthread_structure\n",
					_Cpool_self(),_Cthread_self());
			}
#endif
			Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
			return(0);
		}
#ifndef _WIN32
	}
#endif
}

/* ============================================ */
/* Routine  : Cpool_next_index_timeout          */
/* Arguments: pool number                       */
/*            timeout (<= 0 means no timeout)   */
/* -------------------------------------------- */
/* Output   : index >= 0 (OK) -1 (ERROR)        */
/* -------------------------------------------- */
/* History:                                     */
/* 08-JUN-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
int DLL_DECL Cpool_next_index_timeout(poolnb,timeout)
	int poolnb;
	int timeout;
{
	return(Cpool_next_index_timeout_ext(poolnb,NULL,timeout));
}
/* ============================================ */
/* Routine  : Cpool_next_index_timeout_ext      */
/* Arguments: pool number                       */
/*            pool address                      */
/*            timeout (<= 0 means no timeout)   */
/* -------------------------------------------- */
/* Output   : index >= 0 (OK) -1 (ERROR)        */
/* -------------------------------------------- */
/* History:                                     */
/* 08-JUN-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* 04-SEP-2003       Added pooladdr             */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
int DLL_DECL Cpool_next_index_timeout_ext(poolnb,pooladdr,timeout)
	int poolnb;
	void *pooladdr;
	int timeout;
{

	/* We makes sure that Cthread pakage is initalized */
	Cthread_init();
  
#ifdef CPOOL_DEBUG
	if (Cpool_debug != 0)
		log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext(%d,0x%lx,%d)\n",
			_Cpool_self(),_Cthread_self(),poolnb,(unsigned long) pooladdr, timeout);
#endif
    
#ifndef _WIN32
	/* THIS ROUTINE IS EXPLICITELY SPLITTED IN TWO PARTS    */
	/* - The _NOCTHREAD one (using pipes)                   */
	/* - The _CTHREAD one (using shared mem. and cond. var. */
	if (Cthread_environment() == CTHREAD_MULTI_PROCESS) {
		/*----------------------- */
		/* Non-Thread only        */
		/*----------------------- */
		struct Cpool_t *current = &Cpool;
		int    found;
		int                     maxfd;
		int                     i;
		fd_set                  readlist;
		int                     ready;
		struct timeval timeval;               /* For timeout */
		int select_rc;

		if (poolnb < 0) {
			serrno = EINVAL;
			return(-1);
		}
    
		found = 0;
		if (pooladdr != NULL) {
			found = 1;
			current = pooladdr;
		} else {
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : lock on &Cpool\n",
					_Cpool_self(),_Cthread_self());
#endif
			if (Cthread_mutex_lock(&Cpool) != 0) {
				return(-1);
			}
			/* We search the corresponding pool */
			current = &Cpool;
			while ((current = current->next) != NULL) {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Wanted pool is %d, Got pool No %d\n",
						_Cpool_self(),_Cthread_self(),poolnb,current->poolnb);
#endif
				if (current->poolnb == poolnb) {
					found = 1;
					break;
				}
			}
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : un-lock on &Cpool\n",
					_Cpool_self(),_Cthread_self());
#endif
			Cthread_mutex_unlock(&Cpool);
		}
    
		if (found == 0) {
			errno = EINVAL;
			return(-1);
		}
    
		/* We collect all the fd's to read from */
#ifdef CPOOL_DEBUG
		if (Cpool_debug != 0)
			log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Wait flag on ",
				_Cpool_self(),_Cthread_self());
#endif
		/* We build the timeout if needed */
		if (timeout > 0) {
			timeval.tv_sec = timeout;
			timeval.tv_usec = 0;
		}
		maxfd = 0;
		FD_ZERO(&readlist);
		for (i = 0; i < current->nbelem; i++) {
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0)
				log(LOG_INFO,"%d ",current->readfd[i]);
#endif
			if (current->readfd[i] > maxfd)
				maxfd = current->readfd[i];
			FD_SET(current->readfd[i],&readlist);
		}
#ifdef CPOOL_DEBUG
		if (Cpool_debug != 0)
			log(LOG_INFO,"\n");
#endif
    
		/* We wait for a flag of any of the child */
		if ((select_rc = select(maxfd+1, (_cpool_fd_set *) &readlist, NULL, NULL, (timeout > 0 ? &timeval : NULL))) <= 0) {
			/* Error */
			serrno = (select_rc == 0 ? SETIMEDOUT : SEINTERNAL);
			return(-1);
		}
    
		/* We got one, let's know which one it is */
		i = 0;
		for (i = 0; i <= current->nbelem; i++) {
			if (FD_ISSET(current->readfd[i],&readlist)) {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Got flag on %d\n",
						_Cpool_self(),_Cthread_self(),current->readfd[i]);
#endif
				/* We read the flag */
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Read the flag on %d\n",
						_Cpool_self(),_Cthread_self(),current->readfd[i]);
#endif
				_Cpool_readn(current->readfd[i], (void *) &ready, sizeof(int));
				/* And we send a wait flag */
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Write sleep flag on %d\n",
						_Cpool_self(),_Cthread_self(),current->writefd[i]);
#endif
				_Cpool_writen(current->writefd[i],(void *) &_cpool_sleep_flag,sizeof(void *));
				/* We remember it for the next assignment */
				current->forceid = i;
				return(i);
			}
		}
    
		/* We should not be there */
		errno = SEINTERNAL;
		return(-1);
    
	} else {
#endif /* _WIN32 */
		{
			/*----------------------- */
			/* Thread only            */
			/*----------------------- */
			struct Cpool_t *current = &Cpool;
			int    found;
			int    i;
			void *lock_parent_cthread_structure;
		  
			if (poolnb < 0) {
				errno = EINVAL;
				return(-1);
			}
		  
			found = 0;
			if (pooladdr != NULL) {
				current = pooladdr;
				found = 1;
				lock_parent_cthread_structure = current->lock_parent_cthread_structure;
			} else {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : lock on &Cpool\n",
						_Cpool_self(),_Cthread_self());
#endif
				if (Cthread_mutex_lock(&Cpool) != 0) {
					return(-1);
				}
				/* We search the corresponding pool */
				current = &Cpool;
				while ((current = current->next) != NULL) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Got pool No %d\n",
							_Cpool_self(),_Cthread_self(),current->poolnb);
					}
#endif
					if (current->poolnb == poolnb) {
						found = 1;
						lock_parent_cthread_structure = current->lock_parent_cthread_structure;
						break;
					}
				}
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0)
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : un-lock on &Cpool\n",
						_Cpool_self(),_Cthread_self());
#endif
				Cthread_mutex_unlock(&Cpool);
			}
		  
			if (found == 0) {
				errno = EINVAL;
				return(-1);
			}
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : lock on lock_parent_cthread_structure\n",
					_Cpool_self(),_Cthread_self());
			}
#endif
		  
			/* We assume that we will not have to wait   */
			if (Cthread_mutex_lock_ext(lock_parent_cthread_structure) != 0) {
				return(-1);
			}
		  
			for (i = 0; i < current->nbelem; i++) {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : try-lock on current->state_cthread_structure[%d]\n",
						_Cpool_self(),_Cthread_self(), i);
				}
#endif
				if (Cthread_mutex_trylock_ext(current->state_cthread_structure[i]) == 0) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : current->state[%d] is %d\n",
							_Cpool_self(),_Cthread_self(), poolnb, current->state[i]);
					}
#endif
					if (current->state[i] == 0) {
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Found thread at index %d\n",
								_Cpool_self(),_Cthread_self(),i);
						}
#endif
						
						/* Okay: We will return this index */
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : un-lock on current->state_cthread_structure[%d], set current->forceid to %d\n",
								_Cpool_self(),_Cthread_self(), i, i);
						}
#endif
						Cthread_mutex_unlock_ext(current->state_cthread_structure[i]);
						current->forceid = i;
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : un-lock on lock_parent_cthread_structure\n",
								_Cpool_self(),_Cthread_self());
						}
#endif
						
						Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Return %d\n",
								_Cpool_self(),_Cthread_self(),i);
						}
#endif
						return(i);
					} else {
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : un-lock on current->state_cthread_structure[%d]\n",
								_Cpool_self(),_Cthread_self(), i);
						}
#endif
						Cthread_mutex_unlock_ext(current->state_cthread_structure[i]);
					}
#ifdef CPOOL_DEBUG
				} else {
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : try-lock on current->state_cthread_structure[%d] error: %s\n",
							_Cpool_self(),_Cthread_self(), i, strerror(errno));
					}
#endif
				}
			}
		  
			/* We did not found one thread available */
			/* We say that we are waiting            */
		  
			while (1) {
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : set current->flag to -1\n",
						_Cpool_self(),_Cthread_self());
				}
#endif
				current->flag = -1;
			  
				/* And we wait on our predicate          */
				while (current->flag == -1) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : cond-timedwait on lock_parent_cthread_structure\n",
							_Cpool_self(),_Cthread_self());
					}
#endif
				  
					if (Cthread_cond_timedwait_ext(lock_parent_cthread_structure,timeout) != 0) {
					  
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Waiting condition failed, reset current->flag to zero\n",
								_Cpool_self(),_Cthread_self());
						}
#endif
					  
						current->flag = -2;
					  
#ifdef CPOOL_DEBUG
						if (Cpool_debug != 0) {
							log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : un-lock on lock_parent_cthread_structure\n",
								_Cpool_self(),_Cthread_self());
						}
#endif
					  
					  
						Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
						return(-1);
					}
				}
			  
				/* Yes... */
				/* current->flag contains the index */
				/* of the thread that tell us it is */
				/* available                        */
			  
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Waiting condition okay, returns current->flag=%d\n",
						_Cpool_self(),_Cthread_self(),current->flag);
				}
#endif
#ifdef CPOOL_DEBUG
				if (Cpool_debug != 0) {
					log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : current->state[current->flag=%d] is %d\n",
						_Cpool_self(),_Cthread_self(), current->flag, current->state[current->flag]);
				}
#endif
			  
				/* We check that this thread is REALLY in a READY status */
				if (current->state[current->flag] != 0) {
#ifdef CPOOL_DEBUG
					if (Cpool_debug != 0) {
						log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Thread No %d's state is not ready for assignment. Continue.\n",
							_Cpool_self(),_Cthread_self(),current->flag);
					}
#endif
					continue;
				}
				break;
			}
		  
		  
			/* ... And remember it to force assignment */
			i = current->forceid = current->flag;
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : un-lock on lock_parent_cthread_structure\n",
					_Cpool_self(),_Cthread_self());
			}
#endif
			Cthread_mutex_unlock_ext(lock_parent_cthread_structure);
		  
#ifdef CPOOL_DEBUG
			if (Cpool_debug != 0) {
				log(LOG_INFO,"[Cpool  [%2d][%2d]] In Cpool_next_index_timeout_ext : Return %d\n",
					_Cpool_self(),_Cthread_self(),i);
			}
#endif
		  
			return(i);
		}
#ifndef _WIN32
	}
#endif
}

/* ============================================ */
/* Routine  : _Cpool_self                       */
/* Arguments:                                   */
/* -------------------------------------------- */
/* Output   : pool number >= 0 (OK) -1 (ERROR)  */
/* -------------------------------------------- */
/* History:                                     */
/* 12-JUL-1999       First implementation       */
/*                   Jean-Damien.Durand@cern.ch */
/* ============================================ */
int DLL_DECL _Cpool_self() {
	struct Cpool_t *current = NULL;
	int             i;
	int             cid;

	/* Nota: There is no call to Cthread_mutex_lock
	   nor Cthread_mutex_unlock on &Cpool because
	   this would block Cpool execution.
	*/

	/* We makes sure that Cthread pakage is initalized */
	Cthread_init();
  
	/* We get current Cthread ID */
	if ((cid = _Cthread_self()) < 0) {
		return(-1);
	}
  
	/* We search the corresponding pool */
	current = &Cpool;
	while ((current = current->next) != NULL) {
		for (i = 0; i < current->nbelem; i++) {
			if (current->cid[i] == cid) {
				return(current->poolnb);
			}
		}
	}
  
	/* We do not set serrno for _Cpool_self() */
	/* serrno = EINVAL; */
	return(-1);
}