File: globus_thread.c

package info (click to toggle)
globus-common 14.7-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,308 kB
  • sloc: ansic: 33,976; sh: 9,902; makefile: 532; perl: 342; xml: 322
file content (1789 lines) | stat: -rw-r--r-- 53,869 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
/*
 * Copyright 1999-2010 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * @file globus_thread.c Globus Threading Abstraction
 *
 * @details
 *
 * Stubs for the Globus threads package, to be used when Globus has been
 * configured not to use threads.
 *
 * CVS Information:
 *
 * $Source: /home/globdev/CVS/globus-packages/common/source/library/globus_thread.c,v $
 * $Date: 2011/10/25 13:27:34 $
 * $Revision: 1.7 $
 * $State: Exp $
 * $Author: bester $
 */

#include "config.h"
#include "globus_thread.h"
extern globus_result_t globus_eval_path(const char *, char **);

#include "ltdl.h"
extern globus_module_descriptor_t globus_i_thread_none_module;

const globus_thread_once_t GLOBUS_THREAD_ONCE_INIT_VALUE = GLOBUS_THREAD_ONCE_INIT;

static
int
globus_l_thread_activate(void);

static
int
globus_l_thread_deactivate(void);

extern globus_mutex_t                   globus_libc_mutex;
static globus_thread_impl_t *           globus_l_thread_impl;
static globus_thread_impl_t *           globus_l_activated_thread_impl;
static globus_module_descriptor_t *     globus_l_thread_impl_module;

static char                             globus_l_thread_model[16] = "";

globus_module_descriptor_t              globus_i_thread_module =
{
    "globus_thread",
    globus_l_thread_activate,
    globus_l_thread_deactivate
};

/**
 * @defgroup globus_thread Threading
 *
 * The Globus runtime includes support for portably creating threads on POSIX
 * and Windows systems. It also provides a callback-driven system for
 * applications that may use threads but don't require them. The Globus
 * Thread API is modeled closely after the POSIX threads API.
 *
 * Applications can choose whether to run as threaded or non-threaded at
 * runtime by either setting the GLOBUS_THREAD_MODEL environment variable
 * or calling the globus_thread_set_model() function prior to activating
 * any Globus modules.
 *
 * The Globus thread system provides primitives for mutual exclusion
 * (globus_mutex_t, globus_rmutex_t, globus_rw_mutex_t), event
 * synchronization (globus_cond_t), one-time execution (globus_once_t), and
 * threading (globus_thread_t). 
 *
 * In non-threaded operation, globus_cond_wait() and its variants will poll
 * the callback queue and I/O system to allow event-driven programs to 
 * run in the absence of threads. The globus_thread_create() function will
 * fail in that model. Other primitive operations will return success but
 * not provide any thread exclusion as there is only one thread.
 */

/**
 * @brief Select threading model for an application
 * @ingroup globus_thread
 * @details
 *     The globus_thread_set_model() function selects which runtime model
 *     the current application will use. By default, the Globus runtime
 *     uses a non-threaded model. Additional models may be available based
 *     on system support: pthread, or windows. This function must be called
 *     prior to activating any globus module, as it changes how certain
 *     functions (like globus_mutex_lock() and globus_cond_wait()) behave.
 *     This function overrides the value set by the
 *     GLOBUS_THREAD_MODEL environment variable.
 *
 *     The globus_thread_set_model() function will fail if a Globus module
 *     has been activated already.
 *
 * @param model
 *     The name of the thread model to use. Depending on operating system
 *     capabilities, this may be "none", "pthread", "windows", or some other
 *     custom thread implementation. The corresponding libtool module
 *     "libglobus_thread_pthread.la" or "libglobus_thread_windows.la" must
 *     be installed on the system for it to be used. 
 * 
 * @return
 *     On success, globus_thread_set_model() sets the name of the thread
 *     model to use and returns GLOBUS_SUCCESS. If an error occurs, then
 *     globus_thread_set_model() returns GLOBUS_FAILURE.
 */
extern
int
globus_thread_set_model(
    const char *                        model)
{
    if (model == NULL)
    {
        return GLOBUS_FAILURE;
    }

    if (globus_l_thread_impl != NULL &&
        strcmp(model, globus_l_thread_model) != 0)
    {
        return GLOBUS_FAILURE;
    }

    strncpy(globus_l_thread_model, model, sizeof(globus_l_thread_model));
    globus_l_thread_model[sizeof(globus_l_thread_model)-1] = 0;

    return GLOBUS_SUCCESS;
}
/* globus_thread_set_model() */

static
int
globus_l_thread_activate(void)
{
    return globus_module_activate(globus_l_thread_impl_module);
}

static
int
globus_l_thread_deactivate(void)
{
    return globus_module_deactivate(globus_l_thread_impl_module);
}

extern
int
globus_i_thread_pre_activate(void)
{
    char *                              impl_name;
    char *                              libdir;
    const char                          format[] = "libglobus_thread_%s";
    lt_dlhandle                         impl_lib;
    globus_thread_impl_t *              impl;
    globus_result_t                     result;

    result = globus_eval_path("${libdir}", &libdir);
    if (result != GLOBUS_SUCCESS || libdir == NULL)
    {
        return GLOBUS_FAILURE;
    }

    lt_dlinit();
    lt_dladdsearchdir(libdir);

    if (globus_l_thread_model[0] == 0)
    {
        char *                          model;

        model = getenv("GLOBUS_THREAD_MODEL");

        if (model)
        {
            strncpy(globus_l_thread_model, model, sizeof(globus_l_thread_model));
            globus_l_thread_model[sizeof(globus_l_thread_model)-1] = 0;
        }
    }
    if (globus_l_thread_model[0] == 0)
    {
        strncpy(globus_l_thread_model, "none", sizeof(globus_l_thread_model));
    }
    if (strcmp(globus_l_thread_model, "none") != 0)
    {
        impl_name = malloc(sizeof(format) + strlen(globus_l_thread_model) + 1);
        sprintf(impl_name, format, globus_l_thread_model);

        impl_lib = lt_dlopenext(impl_name);
        if (impl_lib == NULL)
        {
            printf("dlopen %s: %s\n", impl_name, lt_dlerror());
            exit(1);
        }
        globus_assert(impl_lib != NULL);

        globus_l_thread_impl_module = lt_dlsym(impl_lib, "globus_extension_module");
        if (globus_l_thread_impl_module == NULL)
        {
            printf("dlsym: %s\n", lt_dlerror());
            exit(1);
        }
        globus_assert(globus_l_thread_impl_module != NULL);
    }
    else
    {
        globus_l_thread_impl_module = &globus_i_thread_none_module;
    }

    impl = globus_l_thread_impl_module->get_pointer_func();
    globus_assert(impl != NULL);

    globus_l_thread_impl = impl;

    globus_l_activated_thread_impl = globus_l_thread_impl;

    globus_l_thread_impl->thread_pre_activate();

    globus_mutex_init(&globus_libc_mutex, NULL);

    return 0;
}

/**
 * @defgroup globus_mutex Mutual Exclusion
 * @ingroup globus_thread
 * @details
 *     The Globus runtime includes three portable, related mutual exclusion
 *     primitives that can be used in applications and libraries. These are
 *     - globus_mutex_t: a non-recursive, non-shared lock
 *     - globus_rmutex_t: a recursive non-shared lock
 *     - globus_rw_mutex_t: a reader-writer lock
 */

/**
 * @brief Initialize a mutex
 * @ingroup globus_mutex
 * @details
 * The globus_mutex_init() function creates a mutex
 * variable that can be used for synchronization.  Currently, the 
 * @a attr parameter is ignored.
 *
 * @param mutex
 *     Pointer to the mutex to initialize.
 * @param attr
 *     Ignored.
 * 
 * @return
 *     On success, globus_mutex_init() initializes the mutex and
 *     returns GLOBUS_SUCCESS. Otherwise, a non-0 value is returned.
 */
extern
int
globus_mutex_init(
    globus_mutex_t *                    mutex,
    globus_mutexattr_t *                attr)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }
    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->mutex_init)
    {
        rc = globus_l_thread_impl->mutex_init(mutex, attr);
    }

    return rc;
}
/* globus_mutex_init() */

/**
 * @brief Destroy a mutex
 * @ingroup globus_mutex
 * @details
 *     The globus_mutex_destroy() function destroys the mutex pointed to by its
 *     @a mutex parameter. After a mutex is destroyed it may no longer be used
 *     unless it is again initialized by globus_mutex_init(). Behavior is
 *     undefined if globus_mutex_destroy() is called with a pointer to a locked
 *     mutex.
 *
 * @param mutex
 *     The mutex to destroy
 * @return
 *     On success, globus_mutex_destroy() returns GLOBUS_SUCCESS. Otherwise,
 *     a non-zero implementation-specific error value is returned. 
 */
extern
int
globus_mutex_destroy(
    globus_mutex_t *                    mutex)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->mutex_destroy)
    {
        rc = globus_l_thread_impl->mutex_destroy(mutex);
    }

    return rc;
}
/* globus_mutex_destroy() */

/**
 * @brief Lock a mutex
 * @ingroup globus_mutex
 * @details
 *     The globus_mutex_lock() function locks the mutex pointed to by its
 *     @a mutex parameter.
 *     Upon successful
 *     return, the thread calling globus_mutex_lock() has an exclusive 
 *     lock on the resources protected by @a mutex. Other threads calling
 *     globus_mutex_lock() will wait until that thread later calls
 *     globus_mutex_unlock() or globus_cond_wait() with that mutex. Depending
 *     on the thread model, calling globus_mutex_lock on a mutex locked
 *     by the current thread will either return an error or result in deadlock.
 *
 * @param mutex
 *     The mutex to lock.
 * @return
 *     On success, globus_mutex_lock() returns GLOBUS_SUCCESS. Otherwise,
 *     a non-zero implementation-specific error value is returned. 
 */
extern
int
globus_mutex_lock(
    globus_mutex_t *                    mutex)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->mutex_lock)
    {
        rc = globus_l_thread_impl->mutex_lock(mutex);
    }

    return rc;
}
/* globus_mutex_lock() */


/**
 * @brief Unlock a mutex
 * @ingroup globus_mutex
 * @details
 *     The globus_mutex_unlock() function unlocks the mutex pointed to by its
 *     @a mutex parameter.  Upon successful
 *     return, the thread calling globus_mutex_unlock() no longer has an
 *     exclusive lock on the resources protected by @a mutex. Another thread
 *     calling globus_mutex_lock() may be unblocked so that it may acquire
 *     the mutex. Behavior is undefined if globus_mutex_unlock is called with
 *     an unlocked mutex.
 *
 * @param mutex
 *     The mutex to unlock.
 * @return
 *     On success, globus_mutex_unlock() returns GLOBUS_SUCCESS. Otherwise,
 *     a non-zero implementation-specific error value is returned. 
 */
extern
int
globus_mutex_unlock(
    globus_mutex_t *                    mutex)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->mutex_unlock)
    {
        rc = globus_l_thread_impl->mutex_unlock(mutex);
    }

    return rc;
}
/* globus_mutex_unlock() */

/**
 * @brief Lock a mutex if it is not locked
 * @ingroup globus_mutex
 * @details
 *     The globus_mutex_trylock() function locks the mutex pointed to by its
 *     @a mutex parameter if no thread has already locked the mutex. If 
 *     @a mutex is locked, then globus_mutex_trylock() returns EBUSY and does
 *     not block the current thread or lock the mutex.  Upon successful
 *     return, the thread calling globus_mutex_trylock() has an exclusive 
 *     lock on the resources protected by @a mutex. Other threads calling
 *     globus_mutex_lock() will wait until that thread later calls
 *     globus_mutex_unlock() or globus_cond_wait() with that mutex. 
 *
 * @param mutex
 *     The mutex to lock.
 * @return
 *     On success, globus_mutex_trylock() returns GLOBUS_SUCCESS and locks the
 *     mutex. If another thread holds the lock, globus_mutex_trylock()
 *     returns EBUSY. Otherwise, a non-zero implementation-specific error value
 *     is returned. 
 */
extern
int
globus_mutex_trylock(
    globus_mutex_t *                    mutex)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->mutex_trylock)
    {
        rc = globus_l_thread_impl->mutex_trylock(mutex);
    }

    return rc;
}
/* globus_mutex_trylock() */

/**
 * @defgroup globus_cond Condition Variables
 * @ingroup globus_thread
 * @details
 *     The globus_cond_t provides condition variables for signalling events
 *     between threads interested in particular state. One or many threads
 *     may wait on a condition variable until it is signalled, at which point
 *     they can attempt to lock a mutex related to that condition's state
 *     and process the event.
 *
 *     In a non-threaded model, the condition variable wait operations are
 *     used to poll the event driver to handle any operations that have been
 *     scheduled for execution by the globus_callback system or I/O system.
 *     In this way, applications written to  use those systems to handle
 *     nonblocking operations will work with either a threaded or nonthreaded
 *     runtime choice.
 */

/** @brief Initialize a condition variable
 * @ingroup globus_cond
 * The globus_cond_init() function creates a condition 
 * variable that can be used for event signalling between threads.
 *
 * @param cond
 *     Pointer to the condition variable to initialize.
 * @param attr
 *     Condition variable attributes.
 * 
 * @return
 *     On success, globus_cond_init() initializes the condition variable and
 *     returns GLOBUS_SUCCESS. Otherwise, a non-0 value is returned.
 */
extern
int
globus_cond_init(
    globus_cond_t *                     cond,
    globus_condattr_t *                 attr)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->cond_init)
    {
        rc = globus_l_thread_impl->cond_init(cond, attr);
    }

    return rc;

}
/* globus_cond_init() */

/**
 * @brief Destroy a condition variable
 * @ingroup globus_cond
 * @details
 *     The globus_cond_destroy() function destroys the condition variable
 *     pointed to by its @a cond parameter. After a condition variable is
 *     destroyed it may no longer be used
 *     unless it is again initialized by globus_cond_init(). 
 *
 * @param cond
 *     The condition variable to destroy.
 * @return
 *     On success, globus_cond_destroy() returns GLOBUS_SUCCESS. Otherwise,
 *     a non-zero implementation-specific error value is returned. 
 */
extern
int
globus_cond_destroy(
    globus_cond_t *                     cond)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->cond_destroy)
    {
        rc = globus_l_thread_impl->cond_destroy(cond);
    }

    return rc;

}
/* globus_cond_destroy() */

/**
 * @brief Wait for a condition to be signalled
 * @ingroup globus_cond
 * @details
 *     The globus_cond_wait() function atomically unlocks the mutex pointed to
 *     by the @a mutex parameter and blocks the current thread until the
 *     condition variable pointed to by @a cond is signalled by either
 *     globus_cond_signal() or globus_cond_broadcast(). Behavior is undefined
 *     if globus_cond_wait() is called with the mutex pointed to by the @a
 *     mutex variable unlocked.
 *
 * @param cond
 *     The condition variable to wait for.
 * @param mutex
 *     The mutex associated with the condition state.
 *
 * @return
 *     On success, globus_cond_wait() unlocks the mutex and blocks the current
 *     thread until it has been signalled, returning GLOBUS_SUCCES. Otherwise,
 *     globus_cond_wait() returns an implementation-specific non-zero error
 *     value.
 */
extern
int
globus_cond_wait(
    globus_cond_t *                     cond,
    globus_mutex_t *                    mutex)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->cond_wait)
    {
        rc = globus_l_thread_impl->cond_wait(cond, mutex);
    }

    return rc;

}
/* globus_cond_wait() */

/**
 * @brief Wait for a condition to be signalled
 * @ingroup globus_cond
 * @details
 *     The globus_cond_timedwait() function atomically unlocks the mutex
 *     pointed to by the @a mutex parameter and blocks the current thread until
 *     either the condition variable pointed to by @a cond is signalled by
 *     another thread or the current time exceeds the value pointed to by the
 *     @a abstime parameter. If the timeout occurs before the condition is
 *     signalled, globus_cond_timedwait() returns ETIMEDOUT. Behavior is
 *     undefined if globus_cond_timedwait() is called with the mutex pointed to
 *     by the @a mutex variable unlocked.
 *
 * @param cond
 *     The condition variable to wait for.
 * @param mutex
 *     The mutex associated with the condition state.
 * @param abstime
 *     The absolute time to wait until.
 *
 * @return
 *     On success, globus_cond_timedwait() unlocks the mutex and blocks the
 *     current thread until it has been signalled, returning GLOBUS_SUCCES.
 *     If a timeout occurs before signal, globus_cond_timedwait() unlocks
 *     the mutex and returns ETIMEDOUT. Otherwise, 
 *     globus_cond_timedwait() returns an implementation-specific non-zero
 *     error value.
 */
extern
int
globus_cond_timedwait(
    globus_cond_t *                     cond,
    globus_mutex_t *                    mutex,
    globus_abstime_t *                  abstime)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->cond_timedwait)
    {
        rc = globus_l_thread_impl->cond_timedwait(cond, mutex, abstime);
    }

    return rc;
}
/* globus_cond_timedwait() */

/**
 * @brief Signal a condition to a thread
 * @ingroup globus_cond
 * @details
 *     The globus_cond_signal() function signals a condition as occurring.
 *     This will unblock at least one thread waiting for that condition.
 *
 * @param cond
 *     A pointer to the condition variable to signal.
 * @return
 *     Upon success, globus_cond_signal() returns GLOBUS_SUCCESS. If
 *     an error occurs, globus_cond_signal() returns an implementation-specific
 *     non-zero error code.
 */
extern int
globus_cond_signal(
    globus_cond_t *                     cond)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->cond_signal)
    {
        rc = globus_l_thread_impl->cond_signal(cond);
    }

    return rc;
}
/* globus_cond_signal() */

/**
 * @brief Signal a condition to multiple threads
 * @ingroup globus_cond
 * @details
 *     The globus_cond_signal() function signals a condition as occurring.
 *     This will unblock all threads waiting for that condition.
 *
 * @param cond
 *     A pointer to the condition variable to signal.
 * @return
 *     Upon success, globus_cond_broadcast() returns GLOBUS_SUCCESS. If
 *     an error occurs, globus_cond_broadcast() returns an
 *     implementation-specific non-zero error code.
 */
extern
int
globus_cond_broadcast(
    globus_cond_t *                     cond)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->cond_broadcast)
    {
        rc = globus_l_thread_impl->cond_broadcast(cond);
    }

    return rc;
}
/* globus_cond_broadcast() */

/**
 * @brief Initialize a mutex attribute
 * @ingroup globus_mutex
 * @details
 *     The globus_mutexattr_init() function initializes the mutex attribute
 *     structure pointed to by its @a attr parameter. Currently there are
 *     no attribute values that can be set via this API, so there's no real
 *     use to calling this function.
 *
 * @param attr
 *     Attribute structure to initialize.
 *
 * @return
 *     Upon success, globus_mutexattr_init() returns GLOBUS_SUCCESS and
 *     modifies the attribute pointed to by @a attr. If an error occurs,
 *     globus_mutexattr_init() returns an implementation-specific non-zero
 *     error code.
 */
extern
int
globus_mutexattr_init(
    globus_mutexattr_t *                attr)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->mutexattr_init)
    {
        rc = globus_l_thread_impl->mutexattr_init(attr);
    }

    return rc;
}
/* globus_mutexattr_init() */

/**
 * @brief Destroy a mutex attribute
 * @ingroup globus_mutex
 * @details
 *     The globus_mutexattr_destroy() function destroys the mutex attribute
 *     structure pointed to by its @a attr parameter. 
 *
 * @param attr
 *     Attribute structure to destroy.
 *
 * @return
 *     Upon success, globus_mutexattr_destroy() returns GLOBUS_SUCCESS and
 *     modifies the attribute pointed to by @a attr. If an error occurs,
 *     globus_mutexattr_destroy() returns an implementation-specific non-zero
 *     error code.
 */
extern
int
globus_mutexattr_destroy(
    globus_mutexattr_t *                attr)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->mutexattr_destroy)
    {
        rc = globus_l_thread_impl->mutexattr_destroy(attr);
    }

    return rc;
}
/* globus_mutexattr_destroy() */

/**
 * @brief Initialize a condition variable attribute
 * @ingroup globus_cond
 * @details
 *     The globus_condattr_init() function initializes the condition variable
 *     attribute structure pointed to by its @a cond_attr parameter to the
 *     system default values.
 *
 * @param cond_attr
 *     Attribute structure to initialize.
 *
 * @return
 *     Upon success, globus_condattr_init() returns GLOBUS_SUCCESS and
 *     modifies the attribute pointed to by @a cond_attr. If an error occurs,
 *     globus_condattr_init() returns an implementation-specific non-zero
 *     error code.
 */
extern int
globus_condattr_init(
    globus_condattr_t *                 cond_attr)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->condattr_init)
    {
        rc = globus_l_thread_impl->condattr_init(cond_attr);
    }

    return rc;
}
/* globus_condattr_init() */
    
/**
 * @brief Destroy a condition attribute
 * @ingroup globus_cond
 * @details
 *     The globus_condattr_destroy() function destroys the condition variable
 *     attribute structure pointed to by its @a cond_attr parameter. 
 *
 * @param cond_attr
 *     Attribute structure to destroy.
 *
 * @return
 *     Upon success, globus_condattr_destroy() returns GLOBUS_SUCCESS and
 *     modifies the attribute pointed to by @a cond_attr. If an error occurs,
 *     globus_condattr_destroy() returns an implementation-specific non-zero
 *     error code.
 */
extern int
globus_condattr_destroy(
    globus_condattr_t *                 cond_attr)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->condattr_destroy)
    {
        rc = globus_l_thread_impl->condattr_destroy(cond_attr);
    }

    return rc;
}
/* globus_condattr_destroy() */

/**
 * @brief Set callback space associated with a condition variable attribute
 * @ingroup globus_cond
 * The globus_condattr_setspace() function sets the callback space to use
 * with condition variables created with this attribute. Callback spaces
 * are used to control how callbacks are issued to different threads. See
 * @link globus_callback_spaces Callback Spaces @endlink for more information
 * on callback spaces.
 *
 * @param cond_attr
 *     Condition variable attribute to modify.
 * @param space
 *     Callback space to associate with the attribute.
 *  
 * @return
 *     On success, globus_condattr_setspace() returns GLOBUS_SUCCESS and
 *     adds a reference to the callback space to the condition variable
 *     attribute. If an error occurs, globus_condattr_setspace() returns
 *     an implementation-specific non-zero error code.
 */
extern int
globus_condattr_setspace(
    globus_condattr_t *                 cond_attr,
    int                                 space)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->condattr_setspace)
    {
        rc = globus_l_thread_impl->condattr_setspace(cond_attr, space);
    }

    return rc;
}
/* globus_condattr_setspace() */

/**
 * @brief Get callback space associated with a condition variable attribute
 * @ingroup globus_cond
 * The globus_condattr_getspace() function copies the value of the callback
 * space associated with a condition variable attribute to the integer 
 * pointed to by the @a space parameter. 
 *
 * @param cond_attr
 *     Condition variable attribute to modify.
 * @param space
 *     Pointer to an integer to be set to point to the callback space
 *     associated with cond_attr.
 *  
 * @return
 *     On success, globus_condattr_getspace() returns GLOBUS_SUCCESS and
 *     modifies the value pointed to by @a space to refer to the callback
 *     space associated with @a cond_attr.
 *     If an error occurs, globus_condattr_getspace() returns
 *     an implementation-specific non-zero error code.
 */
extern int
globus_condattr_getspace(
    globus_condattr_t *                 cond_attr,
    int *                               space)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->condattr_getspace)
    {
        rc = globus_l_thread_impl->condattr_getspace(cond_attr, space);
    }
    else
    {
        rc = 0;
    }

    return rc;
}
/* globus_condattr_getspace() */

/**
 * @brief Create a new thread
 * @ingroup globus_thread
 * @details
 *     The globus_thread_create() function creates a new thread of execution
 *     in the current process to run the function pointed to by the @a func
 *     parameter passed the @a user_arg value as its only parameter. This
 *     new thread will be detached, so that storage associated with the thread
 *     will be automatically reclaimed by the operating system. A thread
 *     identifier will be copied to the value pointed by the @a thread
 *     parameter if it is non-NULL. The caller may use this thread identifier
 *     to signal or cancel this thread. The @a attr paramter is ignored by
 *     this function. If the "none" threading model is used by an application,
 *     then this function will always fail. One alternative that will work both
 *     with and without threads is to use the functions in the
 *     @link globus_callback Globus Callback API @endlink.
 *
 * @param thread
 *     Pointer to a variable to contain the new thread's identifier.
 * @param attr
 *     Ignored
 * @param func
 *     Pointer to a function to start in the new thread.
 * @param user_arg
 *     Argument to the new thread's function.
 *
 * @return
 *     On success, globus_thread_create() will start a new thread, invoking
 *     (*func)(user_arg), modify the value pointed to by the @a thread
 *     parameter to contain the new thread's identifier and return
 *     GLOBUS_SUCCESS. If an error occurs, then the value of @a thread is
 *     undefined and globus_thread_create() returns an implementation-specific
 *     non-zero error value.
 */
extern
int
globus_thread_create(
    globus_thread_t *                   thread,
    globus_threadattr_t *               attr,
    globus_thread_func_t                func,
    void *                              user_arg)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_create)
    {
        rc = globus_l_thread_impl->thread_create(thread, attr, func, user_arg);
    }
    else
    {
        rc = EINVAL;
    }

    return rc;
}
/* globus_thread_create() */

#if USE_SYMBOL_LABELS
__asm__(".symver globus_thread_key_create_compat,"
        "globus_thread_key_create@GLOBUS_COMMON_11");
__asm__(".symver globus_thread_key_create_new,"
        "globus_thread_key_create@@GLOBUS_COMMON_14");

#define globus_thread_key_create globus_thread_key_create_new
int
globus_thread_key_create_compat(
    pthread_key_t *                     key,
    globus_thread_key_destructor_func_t destructor)
{
    return pthread_key_create(key, destructor);
}
/* globus_thread_getspecific_compat() */
#endif
/**
 * @defgroup globus_thread_key Thread-Specific Storage
 * @ingroup globus_thread
 * @details
 *     The globus_thread_key_t data type acts as a key to thread-specific
 *     storage. For each key created by globus_thread_key_create(), each 
 *     thread may store and retrieve its own value. 
 */
/**
 * @brief Create a key for thread-specific storage
 * @ingroup globus_thread_key
 * @details
 *     The globus_thread_key_create() function creates a new key for
 *     thread-specific data. The new key will be available for all threads
 *     to store a distinct value. If the function pointer @a destructor
 *     is non-NULL, then that function will be invoked when a thread exits
 *     that has a non-NULL value associated with the key.
 *
 * @param key
 *     Pointer to be set to the new key.
 * @param destructor
 *     Pointer to a function to call when a thread exits to free the key's
 *     value.
 *
 * @return
 *     On success, globus_thread_create_key() will create a new key to
 *     thread-local storage and return GLOBUS_SUCCESS. If an error occurs, then
 *     the value of @a key is undefined and globus_thread_create_key() returns
 *     an implementation-specific non-zero error value.
 */
extern
int
globus_thread_key_create(
    globus_thread_key_t *               key,
    globus_thread_key_destructor_func_t destructor)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_key_create)
    {
        rc = globus_l_thread_impl->thread_key_create(key, destructor);
    }

    return rc;
}
/* globus_thread_key_create() */

/**
 * @brief Delete a thread-local storage key
 * @ingroup globus_thread_key
 * @details
 *     The globus_thread_key_delete() function deletes the key used for a 
 *     thread-local storage association. The destructor function for this
 *     key will no longer be called after this function returns. The behavior
 *     of subsequent calls to globus_thread_getspecific() or
 *     globus_thread_setspecific() with this key will be undefined.
 *
 * @param key
 *     Key to destroy.
 *
 * @return
 *     On success, globus_thread_key_delete() will delete a thread-local
 *     storage key and return GLOBUS_SUCCESS.  If an error occurs, then
 *     the value of @a key is undefined and globus_thread_create_key() returns
 *     an implementation-specific non-zero error value.
 */
extern
int
globus_thread_key_delete(
    globus_thread_key_t                 key)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_key_delete)
    {
        rc = globus_l_thread_impl->thread_key_delete(key);
    }

    return rc;
}
/* globus_thread_key_delete() */

/**
 * @defgroup globus_thread_once One-time execution
 * @ingroup globus_thread
 * @details
 *     The globus_thread_once_t provides a way for applications and libraries
 *     to execute some code exactly one time, independent of the number of 
 *     threads which attempt to execute it. To use this, statically initialize
 *     a globus_thread_once_t control with the value GLOBUS_THREAD_ONCE_INIT,
 *     and pass a pointer to a function to execute once, along with the control,
 *     to globus_thread_once().
 */

#if USE_SYMBOL_LABELS
__asm__(".symver globus_thread_once_compat,"
        "globus_thread_once@GLOBUS_COMMON_11");
__asm__(".symver globus_thread_once_new,"
        "globus_thread_once@@GLOBUS_COMMON_14");

#define globus_thread_once globus_thread_once_new

int
globus_thread_once_compat(
    pthread_once_t *                    once, 
    void                                (*init_routine)(void))
{
    return pthread_once(once, init_routine);
}
/* globus_thread_once_compat() */
#endif

/**
 * @brief Execute a function one time
 * @ingroup globus_thread_once
 * @details
 *     The globus_thread_once() function will execute the function pointed to
 *     by its @a init_routine parameter one time for each unique
 *     globus_thread_once_t object passed to it, independent of the number
 *     of threads calling it. The @a once value must be a static value
 *     initialized to GLOBUS_THREAD_ONCE_INIT.
 *
 * @param once
 *     A pointer to the value used to govern whether the function passed via
 *     the @a init_routine parameter has executed.
 * @param init_routine
 *     Function to execute one time. It is called with no parameters.
 * 
 * @return
 *     On success, globus_thread_once() guarantees that the function 
 *     pointed to by @a init_routine has run, and that subsequent calls to
 *     globus_thread_once() with the same value of @a once will not execute
 *     that function, and returns GLOBUS_SUCCESS. If an error occurs, 
 *     globus_thread_once() returns an implementation-specific non-zero error
 *     value.
 */
extern
int
globus_thread_once(
    globus_thread_once_t *              once,
    void                                (*init_routine)(void))
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_once)
    {
        rc = globus_l_thread_impl->thread_once(once, init_routine);
    }
    else
    {
        rc = EINVAL;
    }

    return rc;
}
/* globus_thread_once() */

#if USE_SYMBOL_LABELS
__asm__(".symver globus_thread_getspecific_compat,"
        "globus_thread_getspecific@GLOBUS_COMMON_11");
__asm__(".symver globus_thread_getspecific_new,"
        "globus_thread_getspecific@@GLOBUS_COMMON_14");

#define globus_thread_getspecific globus_thread_getspecific_new
void *
globus_thread_getspecific_compat(
    pthread_key_t                       key)
{
    return pthread_getspecific(key);
}
/* globus_thread_getspecific_compat() */
#endif

/**
 * @brief Get a thread-specific data value
 * @ingroup globus_thread_key
 * @details
 *     The globus_thread_getspecific() function returns the value associated
 *     with the thread-specific data key passed as its first parameter. This
 *     function returns NULL if the value has not been set by the current
 *     thread. The return value is undefined if the key is not valid.
 * @param key
 *     Thread-specific data key to look up.
 * @return
 *     The value passed to a previous call to globus_thread_setspecific() in
 *     the current thread for this key.
 */
extern
void *
globus_thread_getspecific(
    globus_thread_key_t                 key)
{
    void *                              val = NULL;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }
    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_getspecific)
    {
        val = globus_l_thread_impl->thread_getspecific(key);
    }

    return val;
}
/* globus_thread_getspecific() */

#if USE_SYMBOL_LABELS
__asm__(".symver globus_thread_setspecific_compat,"
        "globus_thread_setspecific@GLOBUS_COMMON_11");
__asm__(".symver globus_thread_setspecific_new,"
        "globus_thread_setspecific@@GLOBUS_COMMON_14");

#define globus_thread_setspecific globus_thread_setspecific_new
int
globus_thread_setspecific_compat(
    pthread_key_t                       key,
    void *                              value)
{
    return pthread_setspecific(key, value);
}
/* globus_thread_getspecific_compat() */
#endif
/**
 * @brief Set a thread-specific data value
 * @ingroup globus_thread_key
 * @details
 *     The globus_thread_setspecific() function associates a thread-specific
 *     value with a data key. If the key had a previous value set in the
 *     current thread, it is replaced, but the destructor function is not
 *     called for the old value.
 * @param key
 *     Thread-specific data key to store.
 * @param value
 *     A pointer to data to store as the thread-specific data for this thread.
 * @return
 *     On success, globus_thread_setspecific() stores value in the
 *     thread-specific data for the specified key and returns GLOBUS_SUCCESS.
 *     If an error occurs, globus_thread_setspecific() returns an
 *     implementation-specific non-zero error code and does not modify the
 *     key's value for this thread.
 */
extern
int
globus_thread_setspecific(
    globus_thread_key_t                 key,
    void *                              value)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_setspecific)
    {
        rc = globus_l_thread_impl->thread_setspecific(key, value);
    }

    return rc;
}
/* globus_thread_setspecific() */

/**
 * @brief Yield execution to another thread
 * @ingroup globus_thread
 * @details
 *     The globus_thread_yield() function yields execution to other threads
 *     which are ready for execution. The current thread may continue to
 *     execute if there are no other threads in the system's ready queue.
 */
extern
void
globus_thread_yield(void)
{

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }
    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_yield)
    {
        globus_l_thread_impl->thread_yield();
    }
}
/* globus_thread_yield() */

/**
 * @brief Terminate the current thread
 * @ingroup globus_thread
 *     The globus_thread_exit() terminates the current thread with the value
 *     passed to it. This function does not return.
 */
extern
void
globus_thread_exit(
    void *                              value)
{

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }
    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_exit)
    {
        globus_l_thread_impl->thread_exit(value);
    }
    exit((int)value);
}
/* globus_thread_exit() */

/**
 * @brief Modify the current thread's signal mask
 * @ingroup globus_thread
 * @details
 *     The globus_thread_sigmask() function modifies the current thread's
 *     signal mask and returns the old value of the signal mask in the value
 *     pointed to by the @a old_mask parameter. The @a how parameter can be
 *     one of SIG_BLOCK, SIG_UNBLOCK, or SIG_SETMASK to control how the
 *     signal mask is modified.
 * 
 * @param how
 *     Flag indicating how to interpret @a new_mask if it is non-NULL. If 
 *     @a how is SIG_BLOCK, then all signals in @a new_mask are blocked, as
 *     well as any which were previously blocked. If
 *     @a how is SIG_UNBLOCK, then all signals in which were previously blocked
 *     in @a new_mask are unblocked. If @a how is SIG_SETMASK, then the old
 *     signal mask is replaced with the value of @a new_mask.
 * @param new_mask
 *     Set of signals to block or unblock, based on the @a how parameter.
 * @param old_mask
 *     A pointer to be set to the old signal mask associated with the current
 *     thread.
 *
 * @return
 *     On success, globus_thread_sigmask() modifies the signal mask,
 *     modifies the value pointed to by @a old_mask with the signal mask
 *     prior to this function's execution and returns GLOBUS_SUCCESS. If an
 *     error occurs, globus_thread_sigmask() returns an implementation-specific
 *     non-zero error value.
 */
extern
int
globus_thread_sigmask(
    int                                 how,
    const sigset_t *                    new_mask,
    sigset_t *                          old_mask)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_sigmask)
    {
        rc = globus_l_thread_impl->thread_sigmask(how, new_mask, old_mask);
    }

    return rc;
}
/* globus_thread_sigmask() */

/**
 * @brief Send a signal to a thread
 * @ingroup globus_thread
 * @details
 *     The globus_thread_kill() function sends the signal specified by the
 *     @a sig number to the thread whose ID matches the @a thread parameter.
 *     Depending on the signal mask of that thread, this may result in
 *     a signal being delivered or not, and depending on the process's
 *     signal actions, a signal handler, termination, or no operation will
 *     occur in that thread.
 *
 * @param thread
 *     The thread identifier of the thread to signal.
 * @param sig
 *     The signal to send to the thread.
 * @return
 *     On success, globus_thread_kill() queues the signal for delivery to the
 *     specified thread and returns GLOBUS_SUCCESS. If an error occurs, 
 *     globus_thread_kill() returns an implementation-specific non-zero error
 *     value.
 */
extern
int
globus_thread_kill(
    globus_thread_t                     thread,
    int                                 sig)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_kill)
    {
        rc = globus_l_thread_impl->thread_kill(thread, sig);
    }

    return rc;
}
/* globus_thread_kill() */

/**
 * @brief Determine the current thread's ID
 * @ingroup globus_thread
 * @details
 *     The globus_thread_self() function returns the thread identifier of the
 *     current thread. This value is unique among all threads which are running
 *     at any given time.
 */
extern
globus_thread_t
globus_thread_self(void)
{
    globus_thread_t                     result;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    memset(&result, 0, sizeof(globus_thread_t));

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_self)
    {
        result = globus_l_thread_impl->thread_self();
    }

    return result;
}
/* globus_thread_self() */

/**
 * @brief Check whether thread identifiers match
 * @ingroup globus_thread
 * @details
 *     The globus_thread_equal() function checks whether the thread identifiers
 *     passed as the @a thread1 and @a thread2 parameters refer to the same
 *     thread. If so, globus_thread_equal() returns GLOBUS_TRUE; otherwise
 *     GLOBUS_FALSE.
 *
 * @param thread1
 *     Thread identifier to compare.
 * @param thread2
 *     Thread identifier to compare.
 */
extern
globus_bool_t
globus_thread_equal(
    globus_thread_t                     thread1,
    globus_thread_t                     thread2)
{
    globus_bool_t                       result = GLOBUS_TRUE;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_equal)
    {
        result = globus_l_thread_impl->thread_equal(thread1, thread2);
    }

    return result;
}
/* globus_thread_equal() */

/**
 * @brief Indicate whether the active thread model supports preemption
 * @ingroup globus_thread
 * @details
 *     The globus_thread_preemptive_threads() function returns GLOBUS_TRUE
 *     if the current thread model supports thread preemption; othwerise
 *     it returns GLOBUS_FALSE.
 */
extern
globus_bool_t
globus_thread_preemptive_threads(void)
{
    globus_bool_t                       result = GLOBUS_TRUE;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->preemptive_threads)
    {
        result = globus_l_thread_impl->preemptive_threads();
    }

    return result;
}
/* globus_thread_preemptive_threads() */

/**
 * @brief Determine if threads are supported
 * @ingroup globus_thread
 * @details
 *     The globus_i_am_only_thread() function returns GLOBUS_TRUE if the
 *     current thread model is the "none" thread model; GLOBUS_FALSE otherwise.
 *     If running with the "none" thread model, there will only
 *     be one Globus thread available and the globus_thread_create() function
 *     will always fail.
 */
extern
globus_bool_t
globus_i_am_only_thread(void)
{
    globus_bool_t                       result = GLOBUS_TRUE;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->i_am_only_thread)
    {
        result = globus_l_thread_impl->i_am_only_thread();
    }

    return result;
}
/* globus_i_am_only_thread() */

/**
 * @brief Execute a function with thread cleanup in case of cancellation
 * @ingroup globus_thread
 * @details
 *     The globus_thread_cancellable_func() function provides an interface to
 *     POSIX thread cancellation points that does not rely on preprocessor
 *     macros. It is roughly equivalent to 
 * @code
 * pthread_cleanup_push(cleanup_func, cleanup_arg);
 * (*func)(arg);
 * pthread_cleanup_pop(execute_cleanup)
 * @endcode
 *
 * @param func
 *     Pointer to a function which may be cancelled.
 * @param arg
 *     Parameter to the @a func function.
 * @param cleanup_func
 *     Pointer to a function to execute if thread cancellation occurs during
 *     @a func.
 * @param cleanup_arg
 *     Parameter to the @a cleanup_func function.
 * @param execute_cleanup
 *     Flag indicating whether the function pointed to by @a cleanup_func
 *     should be executed after @a func completes even if it is not cancelled.
 *
 * @return
 *     globus_thread_cancellable_func() returns the value returned by @a func.
 */
extern
void *
globus_thread_cancellable_func(
    void *                              (*func)(void *),
    void *                              arg,
    void                                (*cleanup_func)(void *),
    void *                              cleanup_arg,
    globus_bool_t                       execute_cleanup)
{
    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }
    if (globus_l_thread_impl->thread_cancellable_func)
    {
        return globus_l_thread_impl->thread_cancellable_func(
                func, arg, cleanup_func, cleanup_arg, execute_cleanup);
    }
    else
    {
        return NULL;
    }
}
/* globus_thread_cancellable_func() */

/**
 * @brief Cancel a thread
 * @ingroup globus_thread
 * @details
 *     The globus_thread_cancel() function cancels the thread with the
 *     identifier @a thr if it is still executing. If it is running with a 
 *     cancellation cleanup stack, the functions in that stack are executed.
 *     The target thread's cancel state determines when the cancellation is
 *     delivered.
 *
 * @param thr
 *     The id of the thread to cancel
 *
 * @return
 *     On success, the globus_thread_cancel() function delivers the
 *     cancellation to the target thread and returns GLOBUS_SUCCESS. If an
 *     error occurs, globus_thread_cancel() returns an implementation-specific
 *     non-zero error value.
 */
extern
int
globus_thread_cancel(globus_thread_t thr)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_cancel)
    {
        rc = globus_l_thread_impl->thread_cancel(thr);
    }

    return rc;
}
/* globus_thread_cancel() */

/**
 * @brief Thread cancellation point
 * @ingroup globus_thread
 * @details
 *     The globus_thread_testcancel() function acts as a cancellation point
 *     for the current thread. If a thread has called globus_thread_cancel()
 *     and cancellation is enabled, this will cause the thread to be cancelled
 *     and any functions on the thread's cleanup stack to be executed. This
 *     function will not return if the thread is cancelled.
 */
extern
void
globus_thread_testcancel(void)
{

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }
    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_testcancel)
    {
        globus_l_thread_impl->thread_testcancel();
    }
}

/**
 * @brief Set the thread's cancellable state
 * @ingroup globus_thread
 * @details
 *     The globus_thread_setcancelstate() function sets the current
 *     cancellation state to either GLOBUS_THREAD_CANCEL_DISABLE or
 *     GLOBUS_THREAD_CANCEL_ENABLE, do control whether globus_thread_cancel()
 *     is able to cancel this thread.
 *
 * @param state
 *     The desired cancellation state. If the value is
 *     GLOBUS_THREAD_CANCEL_DISABLE, then cancellation will be disabled for
 *     this thread. If the value is GLOBUS_THREAD_CANCEL_ENABLE, then
 *     cancellation will be enabled for this thread.
 * @param oldstate
 *     A pointer to a value which will be set to the value of the thread's 
 *     cancellation state when this function call began. This may be NULL if
 *     the caller is not interested in the previous value.
 * @return
 *     On success, the globus_thread_setcancelstate() function modifies the
 *     thread cancellation state, modifies oldstate (if non-NULL) to the value
 *     of its previous state, and returns GLOBUS_SUCCESS. If an error occurs,
 *     globus_thread_setcancelstate() returns an implementation-specific
 *     non-zero error value.
 */
extern
int
globus_thread_setcancelstate(
    int                                 state,
    int *                               oldstate)
{
    int                                 rc = 0;

    if (globus_l_thread_impl == NULL)
    {
        globus_i_thread_pre_activate();
    }

    globus_assert(globus_l_thread_impl == globus_l_activated_thread_impl);

    if (globus_l_thread_impl->thread_setcancelstate)
    {
        rc = globus_l_thread_impl->thread_setcancelstate(state, oldstate);
    }

    return rc;
}