Merge pull request #5720 from libgit2/ethomson/tlsdata Thread-local storage: a generic internal library (with no allocations)
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
diff --git a/src/attr_file.c b/src/attr_file.c
index 0781c1d..adc56d5 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -960,7 +960,7 @@ int git_attr_session__init(git_attr_session *session, git_repository *repo)
GIT_ASSERT_ARG(repo);
memset(session, 0, sizeof(*session));
- session->key = git_atomic_inc(&repo->attr_session_key);
+ session->key = git_atomic32_inc(&repo->attr_session_key);
return 0;
}
diff --git a/src/attrcache.c b/src/attrcache.c
index 47fb675..2485b05 100644
--- a/src/attrcache.c
+++ b/src/attrcache.c
@@ -108,7 +108,7 @@ static int attr_cache_upsert(git_attr_cache *cache, git_attr_file *file)
* Replace the existing value if another thread has
* created it in the meantime.
*/
- old = git__swap(entry->file[file->source], file);
+ old = git_atomic_swap(entry->file[file->source], file);
if (old) {
GIT_REFCOUNT_OWN(old, NULL);
@@ -132,7 +132,7 @@ static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file)
return error;
if ((entry = attr_cache_lookup_entry(cache, file->entry->path)) != NULL)
- old = git__compare_and_swap(&entry->file[file->source], file, NULL);
+ old = git_atomic_compare_and_swap(&entry->file[file->source], file, NULL);
attr_cache_unlock(cache);
@@ -321,7 +321,7 @@ static void attr_cache__free(git_attr_cache *cache)
git_strmap_foreach_value(cache->files, entry, {
for (i = 0; i < GIT_ATTR_FILE_NUM_SOURCES; ++i) {
- if ((file = git__swap(entry->file[i], NULL)) != NULL) {
+ if ((file = git_atomic_swap(entry->file[i], NULL)) != NULL) {
GIT_REFCOUNT_OWN(file, NULL);
git_attr_file__free(file);
}
@@ -395,7 +395,7 @@ int git_attr_cache__init(git_repository *repo)
(ret = git_pool_init(&cache->pool, 1)) < 0)
goto cancel;
- cache = git__compare_and_swap(&repo->attrcache, NULL, cache);
+ cache = git_atomic_compare_and_swap(&repo->attrcache, NULL, cache);
if (cache)
goto cancel; /* raced with another thread, free this but no error */
@@ -417,7 +417,7 @@ int git_attr_cache_flush(git_repository *repo)
/* this could be done less expensively, but for now, we'll just free
* the entire attrcache and let the next use reinitialize it...
*/
- if (repo && (cache = git__swap(repo->attrcache, NULL)) != NULL)
+ if (repo && (cache = git_atomic_swap(repo->attrcache, NULL)) != NULL)
attr_cache__free(cache);
return 0;
diff --git a/src/cache.c b/src/cache.c
index a76da50..2f68e35 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -9,7 +9,7 @@
#include "repository.h"
#include "commit.h"
-#include "thread-utils.h"
+#include "thread.h"
#include "util.h"
#include "odb.h"
#include "object.h"
@@ -235,7 +235,7 @@ void git_cached_obj_decref(void *_obj)
{
git_cached_obj *obj = _obj;
- if (git_atomic_dec(&obj->refcount) == 0) {
+ if (git_atomic32_dec(&obj->refcount) == 0) {
switch (obj->flags) {
case GIT_CACHE_STORE_RAW:
git_odb_object__free(_obj);
diff --git a/src/cache.h b/src/cache.h
index 1e61792..42c4fa8 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -13,7 +13,7 @@
#include "git2/oid.h"
#include "git2/odb.h"
-#include "thread-utils.h"
+#include "thread.h"
#include "oidmap.h"
enum {
@@ -23,11 +23,11 @@ enum {
};
typedef struct {
- git_oid oid;
- int16_t type; /* git_object_t value */
- uint16_t flags; /* GIT_CACHE_STORE value */
- size_t size;
- git_atomic refcount;
+ git_oid oid;
+ int16_t type; /* git_object_t value */
+ uint16_t flags; /* GIT_CACHE_STORE value */
+ size_t size;
+ git_atomic32 refcount;
} git_cached_obj;
typedef struct {
@@ -61,7 +61,7 @@ GIT_INLINE(size_t) git_cache_size(git_cache *cache)
GIT_INLINE(void) git_cached_obj_incref(void *_obj)
{
git_cached_obj *obj = _obj;
- git_atomic_inc(&obj->refcount);
+ git_atomic32_inc(&obj->refcount);
}
void git_cached_obj_decref(void *_obj);
diff --git a/src/common.h b/src/common.h
index 2b1a4a4..bda3edb 100644
--- a/src/common.h
+++ b/src/common.h
@@ -63,7 +63,9 @@
# include <pthread.h>
# include <sched.h>
# endif
-#define GIT_STDLIB_CALL
+
+#define GIT_LIBGIT2_CALL
+#define GIT_SYSTEM_CALL
#ifdef GIT_USE_STAT_ATIMESPEC
# define st_atim st_atimespec
@@ -78,7 +80,7 @@
#include "git2/types.h"
#include "git2/errors.h"
#include "errors.h"
-#include "thread-utils.h"
+#include "thread.h"
#include "integer.h"
#include "assert_safe.h"
diff --git a/src/config_cache.c b/src/config_cache.c
index 1a28ba9..2f0455a 100644
--- a/src/config_cache.c
+++ b/src/config_cache.c
@@ -111,7 +111,7 @@ int git_config__configmap_lookup(int *out, git_config *config, git_configmap_ite
int git_repository__configmap_lookup(int *out, git_repository *repo, git_configmap_item item)
{
- *out = (int)(intptr_t)git__load(repo->configmap_cache[(int)item]);
+ *out = (int)(intptr_t)git_atomic_load(repo->configmap_cache[(int)item]);
if (*out == GIT_CONFIGMAP_NOT_CACHED) {
int error;
@@ -122,7 +122,7 @@ int git_repository__configmap_lookup(int *out, git_repository *repo, git_configm
(error = git_config__configmap_lookup(out, config, item)) < 0)
return error;
- git__compare_and_swap(&repo->configmap_cache[(int)item], &oldval, out);
+ git_atomic_compare_and_swap(&repo->configmap_cache[(int)item], &oldval, out);
}
return 0;
diff --git a/src/diff_driver.c b/src/diff_driver.c
index 606c391..6b19012 100644
--- a/src/diff_driver.c
+++ b/src/diff_driver.c
@@ -144,7 +144,7 @@ static git_diff_driver_registry *git_repository_driver_registry(
{
if (!repo->diff_drivers) {
git_diff_driver_registry *reg = git_diff_driver_registry_new();
- reg = git__compare_and_swap(&repo->diff_drivers, NULL, reg);
+ reg = git_atomic_compare_and_swap(&repo->diff_drivers, NULL, reg);
if (reg != NULL) /* if we race, free losing allocation */
git_diff_driver_registry_free(reg);
diff --git a/src/index.c b/src/index.c
index d9116c4..7ebe075 100644
--- a/src/index.c
+++ b/src/index.c
@@ -461,7 +461,7 @@ static void index_free(git_index *index)
/* index iterators increment the refcount of the index, so if we
* get here then there should be no outstanding iterators.
*/
- if (git_atomic_get(&index->readers))
+ if (git_atomic32_get(&index->readers))
return;
git_index_clear(index);
@@ -488,14 +488,14 @@ void git_index_free(git_index *index)
/* call with locked index */
static void index_free_deleted(git_index *index)
{
- int readers = (int)git_atomic_get(&index->readers);
+ int readers = (int)git_atomic32_get(&index->readers);
size_t i;
if (readers > 0 || !index->deleted.length)
return;
for (i = 0; i < index->deleted.length; ++i) {
- git_index_entry *ie = git__swap(index->deleted.contents[i], NULL);
+ git_index_entry *ie = git_atomic_swap(index->deleted.contents[i], NULL);
index_entry_free(ie);
}
@@ -516,7 +516,7 @@ static int index_remove_entry(git_index *index, size_t pos)
error = git_vector_remove(&index->entries, pos);
if (!error) {
- if (git_atomic_get(&index->readers) > 0) {
+ if (git_atomic32_get(&index->readers) > 0) {
error = git_vector_insert(&index->deleted, entry);
} else {
index_entry_free(entry);
@@ -2295,7 +2295,7 @@ int git_index_reuc_clear(git_index *index)
GIT_ASSERT_ARG(index);
for (i = 0; i < index->reuc.length; ++i)
- index_entry_reuc_free(git__swap(index->reuc.contents[i], NULL));
+ index_entry_reuc_free(git_atomic_swap(index->reuc.contents[i], NULL));
git_vector_clear(&index->reuc);
@@ -3197,7 +3197,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
/* well, this isn't good */;
} else {
git_vector_swap(&entries, &index->entries);
- entries_map = git__swap(index->entries_map, entries_map);
+ entries_map = git_atomic_swap(index->entries_map, entries_map);
}
index->dirty = 1;
@@ -3331,7 +3331,7 @@ static int git_index_read_iterator(
goto done;
git_vector_swap(&new_entries, &index->entries);
- new_entries_map = git__swap(index->entries_map, new_entries_map);
+ new_entries_map = git_atomic_swap(index->entries_map, new_entries_map);
git_vector_foreach(&remove_entries, i, entry) {
if (index->tree)
@@ -3637,7 +3637,7 @@ int git_index_snapshot_new(git_vector *snap, git_index *index)
GIT_REFCOUNT_INC(index);
- git_atomic_inc(&index->readers);
+ git_atomic32_inc(&index->readers);
git_vector_sort(&index->entries);
error = git_vector_dup(snap, &index->entries, index->entries._cmp);
@@ -3652,7 +3652,7 @@ void git_index_snapshot_release(git_vector *snap, git_index *index)
{
git_vector_free(snap);
- git_atomic_dec(&index->readers);
+ git_atomic32_dec(&index->readers);
git_index_free(index);
}
diff --git a/src/index.h b/src/index.h
index 54402f5..a365867 100644
--- a/src/index.h
+++ b/src/index.h
@@ -33,7 +33,7 @@ struct git_index {
git_idxmap *entries_map;
git_vector deleted; /* deleted entries if readers > 0 */
- git_atomic readers; /* number of active iterators */
+ git_atomic32 readers; /* number of active iterators */
unsigned int on_disk:1;
unsigned int ignore_case:1;
diff --git a/src/libgit2.c b/src/libgit2.c
index 9e5112d..e41988c 100644
--- a/src/libgit2.c
+++ b/src/libgit2.c
@@ -22,7 +22,7 @@
#include "refs.h"
#include "runtime.h"
#include "sysdir.h"
-#include "thread-utils.h"
+#include "thread.h"
#include "threadstate.h"
#include "git2/global.h"
#include "streams/registry.h"
diff --git a/src/mwindow.c b/src/mwindow.c
index 4f105e6..7832d9c 100644
--- a/src/mwindow.c
+++ b/src/mwindow.c
@@ -79,7 +79,7 @@ int git_mwindow_get_pack(struct git_pack_file **out, const char *path)
git__free(packname);
if (pack != NULL) {
- git_atomic_inc(&pack->refcount);
+ git_atomic32_inc(&pack->refcount);
git_mutex_unlock(&git__mwindow_mutex);
*out = pack;
return 0;
@@ -91,7 +91,7 @@ int git_mwindow_get_pack(struct git_pack_file **out, const char *path)
return error;
}
- git_atomic_inc(&pack->refcount);
+ git_atomic32_inc(&pack->refcount);
error = git_strmap_set(git__pack_cache, pack->pack_name, pack);
git_mutex_unlock(&git__mwindow_mutex);
@@ -118,7 +118,7 @@ int git_mwindow_put_pack(struct git_pack_file *pack)
/* if we cannot find it, the state is corrupted */
GIT_ASSERT(git_strmap_exists(git__pack_cache, pack->pack_name));
- count = git_atomic_dec(&pack->refcount);
+ count = git_atomic32_dec(&pack->refcount);
if (count == 0) {
git_strmap_delete(git__pack_cache, pack->pack_name);
pack_to_delete = pack;
diff --git a/src/pack-objects.c b/src/pack-objects.c
index 03d05b7..f9d8bfd 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -12,7 +12,7 @@
#include "iterator.h"
#include "netops.h"
#include "pack.h"
-#include "thread-utils.h"
+#include "thread.h"
#include "tree.h"
#include "util.h"
#include "revwalk.h"
@@ -1158,7 +1158,7 @@ static int ll_find_deltas(git_packbuilder *pb, git_pobject **list,
int ret, active_threads = 0;
if (!pb->nr_threads)
- pb->nr_threads = git_online_cpus();
+ pb->nr_threads = git__online_cpus();
if (pb->nr_threads <= 1) {
find_deltas(pb, list, &list_size, window, depth);
diff --git a/src/pack.c b/src/pack.c
index b88c52a..a9140c6 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -57,7 +57,7 @@ static git_pack_cache_entry *new_cache_object(git_rawobj *source)
if (!e)
return NULL;
- git_atomic_inc(&e->refcount);
+ git_atomic32_inc(&e->refcount);
memcpy(&e->raw, source, sizeof(git_rawobj));
return e;
@@ -114,7 +114,7 @@ static git_pack_cache_entry *cache_get(git_pack_cache *cache, off64_t offset)
return NULL;
if ((entry = git_offmap_get(cache->entries, offset)) != NULL) {
- git_atomic_inc(&entry->refcount);
+ git_atomic32_inc(&entry->refcount);
entry->last_usage = cache->use_ctr++;
}
git_mutex_unlock(&cache->lock);
@@ -129,7 +129,7 @@ static void free_lowest_entry(git_pack_cache *cache)
git_pack_cache_entry *entry;
git_offmap_foreach(cache->entries, offset, entry, {
- if (entry && git_atomic_get(&entry->refcount) == 0) {
+ if (entry && git_atomic32_get(&entry->refcount) == 0) {
cache->memory_used -= entry->raw.len;
git_offmap_delete(cache->entries, offset);
free_cache_object(entry);
@@ -759,7 +759,7 @@ int git_packfile_unpack(
GIT_ERROR_CHECK_ALLOC(obj->data);
memcpy(obj->data, data, obj->len + 1);
- git_atomic_dec(&cached->refcount);
+ git_atomic32_dec(&cached->refcount);
goto cleanup;
}
@@ -807,7 +807,7 @@ int git_packfile_unpack(
}
if (cached) {
- git_atomic_dec(&cached->refcount);
+ git_atomic32_dec(&cached->refcount);
cached = NULL;
}
@@ -821,7 +821,7 @@ cleanup:
if (error < 0) {
git__free(obj->data);
if (cached)
- git_atomic_dec(&cached->refcount);
+ git_atomic32_dec(&cached->refcount);
}
if (elem)
diff --git a/src/pack.h b/src/pack.h
index 7c3be07..1d07724 100644
--- a/src/pack.h
+++ b/src/pack.h
@@ -58,7 +58,7 @@ struct git_pack_idx_header {
typedef struct git_pack_cache_entry {
size_t last_usage; /* enough? */
- git_atomic refcount;
+ git_atomic32 refcount;
git_rawobj raw;
} git_pack_cache_entry;
@@ -86,7 +86,7 @@ struct git_pack_file {
git_mwindow_file mwf;
git_map index_map;
git_mutex lock; /* protect updates to index_map */
- git_atomic refcount;
+ git_atomic32 refcount;
uint32_t num_objects;
uint32_t num_bad_objects;
diff --git a/src/repository.c b/src/repository.c
index bef84ca..3b6bcbe 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -93,7 +93,7 @@ static void set_odb(git_repository *repo, git_odb *odb)
GIT_REFCOUNT_INC(odb);
}
- if ((odb = git__swap(repo->_odb, odb)) != NULL) {
+ if ((odb = git_atomic_swap(repo->_odb, odb)) != NULL) {
GIT_REFCOUNT_OWN(odb, NULL);
git_odb_free(odb);
}
@@ -106,7 +106,7 @@ static void set_refdb(git_repository *repo, git_refdb *refdb)
GIT_REFCOUNT_INC(refdb);
}
- if ((refdb = git__swap(repo->_refdb, refdb)) != NULL) {
+ if ((refdb = git_atomic_swap(repo->_refdb, refdb)) != NULL) {
GIT_REFCOUNT_OWN(refdb, NULL);
git_refdb_free(refdb);
}
@@ -119,7 +119,7 @@ static void set_config(git_repository *repo, git_config *config)
GIT_REFCOUNT_INC(config);
}
- if ((config = git__swap(repo->_config, config)) != NULL) {
+ if ((config = git_atomic_swap(repo->_config, config)) != NULL) {
GIT_REFCOUNT_OWN(config, NULL);
git_config_free(config);
}
@@ -134,7 +134,7 @@ static void set_index(git_repository *repo, git_index *index)
GIT_REFCOUNT_INC(index);
}
- if ((index = git__swap(repo->_index, index)) != NULL) {
+ if ((index = git_atomic_swap(repo->_index, index)) != NULL) {
GIT_REFCOUNT_OWN(index, NULL);
git_index_free(index);
}
@@ -1054,7 +1054,7 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo)
if (!error) {
GIT_REFCOUNT_OWN(config, repo);
- config = git__compare_and_swap(&repo->_config, NULL, config);
+ config = git_atomic_compare_and_swap(&repo->_config, NULL, config);
if (config != NULL) {
GIT_REFCOUNT_OWN(config, NULL);
git_config_free(config);
@@ -1107,7 +1107,7 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
GIT_ASSERT_ARG(repo);
GIT_ASSERT_ARG(out);
- *out = git__load(repo->_odb);
+ *out = git_atomic_load(repo->_odb);
if (*out == NULL) {
git_buf odb_path = GIT_BUF_INIT;
git_odb *odb;
@@ -1125,14 +1125,14 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
return error;
}
- odb = git__compare_and_swap(&repo->_odb, NULL, odb);
+ odb = git_atomic_compare_and_swap(&repo->_odb, NULL, odb);
if (odb != NULL) {
GIT_REFCOUNT_OWN(odb, NULL);
git_odb_free(odb);
}
git_buf_dispose(&odb_path);
- *out = git__load(repo->_odb);
+ *out = git_atomic_load(repo->_odb);
}
return error;
@@ -1170,7 +1170,7 @@ int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo)
if (!error) {
GIT_REFCOUNT_OWN(refdb, repo);
- refdb = git__compare_and_swap(&repo->_refdb, NULL, refdb);
+ refdb = git_atomic_compare_and_swap(&repo->_refdb, NULL, refdb);
if (refdb != NULL) {
GIT_REFCOUNT_OWN(refdb, NULL);
git_refdb_free(refdb);
@@ -1218,7 +1218,7 @@ int git_repository_index__weakptr(git_index **out, git_repository *repo)
if (!error) {
GIT_REFCOUNT_OWN(index, repo);
- index = git__compare_and_swap(&repo->_index, NULL, index);
+ index = git_atomic_compare_and_swap(&repo->_index, NULL, index);
if (index != NULL) {
GIT_REFCOUNT_OWN(index, NULL);
git_index_free(index);
@@ -3044,8 +3044,8 @@ int git_repository_set_ident(git_repository *repo, const char *name, const char
GIT_ERROR_CHECK_ALLOC(tmp_email);
}
- tmp_name = git__swap(repo->ident_name, tmp_name);
- tmp_email = git__swap(repo->ident_email, tmp_email);
+ tmp_name = git_atomic_swap(repo->ident_name, tmp_name);
+ tmp_email = git_atomic_swap(repo->ident_email, tmp_email);
git__free(tmp_name);
git__free(tmp_email);
diff --git a/src/repository.h b/src/repository.h
index de009ba..4b1222e 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -152,7 +152,7 @@ struct git_repository {
unsigned int lru_counter;
- git_atomic attr_session_key;
+ git_atomic32 attr_session_key;
git_configmap_value configmap_cache[GIT_CONFIGMAP_CACHE_MAX];
git_strmap *submodule_cache;
diff --git a/src/runtime.c b/src/runtime.c
index a214b43..b61c3c4 100644
--- a/src/runtime.c
+++ b/src/runtime.c
@@ -9,9 +9,9 @@
#include "runtime.h"
static git_runtime_shutdown_fn shutdown_callback[32];
-static git_atomic shutdown_callback_count;
+static git_atomic32 shutdown_callback_count;
-static git_atomic init_count;
+static git_atomic32 init_count;
static int init_common(git_runtime_init_fn init_fns[], size_t cnt)
{
@@ -34,10 +34,10 @@ static void shutdown_common(void)
git_runtime_shutdown_fn cb;
int pos;
- for (pos = git_atomic_get(&shutdown_callback_count);
+ for (pos = git_atomic32_get(&shutdown_callback_count);
pos > 0;
- pos = git_atomic_dec(&shutdown_callback_count)) {
- cb = git__swap(shutdown_callback[pos - 1], NULL);
+ pos = git_atomic32_dec(&shutdown_callback_count)) {
+ cb = git_atomic_swap(shutdown_callback[pos - 1], NULL);
if (cb != NULL)
cb();
@@ -46,12 +46,12 @@ static void shutdown_common(void)
int git_runtime_shutdown_register(git_runtime_shutdown_fn callback)
{
- int count = git_atomic_inc(&shutdown_callback_count);
+ int count = git_atomic32_inc(&shutdown_callback_count);
if (count > (int)ARRAY_SIZE(shutdown_callback) || count == 0) {
git_error_set(GIT_ERROR_INVALID,
"too many shutdown callbacks registered");
- git_atomic_dec(&shutdown_callback_count);
+ git_atomic32_dec(&shutdown_callback_count);
return -1;
}
@@ -116,7 +116,7 @@ int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt)
return -1;
/* Only do work on a 0 -> 1 transition of the refcount */
- if ((ret = git_atomic_inc(&init_count)) == 1) {
+ if ((ret = git_atomic32_inc(&init_count)) == 1) {
if (init_common(init_fns, cnt) < 0)
ret = -1;
}
@@ -136,7 +136,7 @@ int git_runtime_shutdown(void)
return -1;
/* Only do work on a 1 -> 0 transition of the refcount */
- if ((ret = git_atomic_dec(&init_count)) == 0)
+ if ((ret = git_atomic32_dec(&init_count)) == 0)
shutdown_common();
/* Exit the lock */
diff --git a/src/sortedcache.h b/src/sortedcache.h
index e553d01..ca8b106 100644
--- a/src/sortedcache.h
+++ b/src/sortedcache.h
@@ -12,7 +12,7 @@
#include "util.h"
#include "futils.h"
#include "vector.h"
-#include "thread-utils.h"
+#include "thread.h"
#include "pool.h"
#include "strmap.h"
diff --git a/src/thread-utils.c b/src/thread-utils.c
deleted file mode 100644
index e5ec6a8..0000000
--- a/src/thread-utils.c
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) the libgit2 contributors. All rights reserved.
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-
-#include "common.h"
-#include "thread-utils.h"
-
-#ifdef _WIN32
-#ifndef WIN32_LEAN_AND_MEAN
-# define WIN32_LEAN_AND_MEAN
-#endif
-# include <windows.h>
-#elif defined(hpux) || defined(__hpux) || defined(_hpux)
-# include <sys/pstat.h>
-#endif
-
-/*
- * By doing this in two steps we can at least get
- * the function to be somewhat coherent, even
- * with this disgusting nest of #ifdefs.
- */
-#ifndef _SC_NPROCESSORS_ONLN
-# ifdef _SC_NPROC_ONLN
-# define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
-# elif defined _SC_CRAY_NCPU
-# define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU
-# endif
-#endif
-
-int git_online_cpus(void)
-{
-#ifdef _SC_NPROCESSORS_ONLN
- long ncpus;
-#endif
-
-#ifdef _WIN32
- SYSTEM_INFO info;
- GetSystemInfo(&info);
-
- if ((int)info.dwNumberOfProcessors > 0)
- return (int)info.dwNumberOfProcessors;
-#elif defined(hpux) || defined(__hpux) || defined(_hpux)
- struct pst_dynamic psd;
-
- if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0))
- return (int)psd.psd_proc_cnt;
-#endif
-
-#ifdef _SC_NPROCESSORS_ONLN
- if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)
- return (int)ncpus;
-#endif
-
- return 1;
-}
diff --git a/src/thread-utils.h b/src/thread-utils.h
deleted file mode 100644
index d71fabe..0000000
--- a/src/thread-utils.h
+++ /dev/null
@@ -1,370 +0,0 @@
-/*
- * Copyright (C) the libgit2 contributors. All rights reserved.
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-#ifndef INCLUDE_thread_utils_h__
-#define INCLUDE_thread_utils_h__
-
-#if defined(GIT_THREADS)
-
-#if defined(__clang__)
-
-# if (__clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 1))
-# error Atomic primitives do not exist on this version of clang; configure libgit2 with -DTHREADSAFE=OFF
-# else
-# define GIT_BUILTIN_ATOMIC
-# endif
-
-#elif defined(__GNUC__)
-
-# if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1))
-# error Atomic primitives do not exist on this version of gcc; configure libgit2 with -DTHREADSAFE=OFF
-# elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
-# define GIT_BUILTIN_ATOMIC
-# else
-# define GIT_BUILTIN_SYNC
-# endif
-
-#endif
-
-#endif /* GIT_THREADS */
-
-/* Common operations even if threading has been disabled */
-typedef struct {
-#if defined(GIT_WIN32)
- volatile long val;
-#else
- volatile int val;
-#endif
-} git_atomic;
-
-#ifdef GIT_ARCH_64
-
-typedef struct {
-#if defined(GIT_WIN32)
- volatile __int64 val;
-#else
- volatile int64_t val;
-#endif
-} git_atomic64;
-
-typedef git_atomic64 git_atomic_ssize;
-
-#define git_atomic_ssize_set git_atomic64_set
-#define git_atomic_ssize_add git_atomic64_add
-#define git_atomic_ssize_get git_atomic64_get
-
-#else
-
-typedef git_atomic git_atomic_ssize;
-
-#define git_atomic_ssize_set git_atomic_set
-#define git_atomic_ssize_add git_atomic_add
-#define git_atomic_ssize_get git_atomic_get
-
-#endif
-
-#ifdef GIT_THREADS
-
-#ifdef GIT_WIN32
-# include "win32/thread.h"
-#else
-# include "unix/pthread.h"
-#endif
-
-GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
-{
-#if defined(GIT_WIN32)
- InterlockedExchange(&a->val, (LONG)val);
-#elif defined(GIT_BUILTIN_ATOMIC)
- __atomic_store_n(&a->val, val, __ATOMIC_SEQ_CST);
-#elif defined(GIT_BUILTIN_SYNC)
- __sync_lock_test_and_set(&a->val, val);
-#else
-# error "Unsupported architecture for atomic operations"
-#endif
-}
-
-GIT_INLINE(int) git_atomic_inc(git_atomic *a)
-{
-#if defined(GIT_WIN32)
- return InterlockedIncrement(&a->val);
-#elif defined(GIT_BUILTIN_ATOMIC)
- return __atomic_add_fetch(&a->val, 1, __ATOMIC_SEQ_CST);
-#elif defined(GIT_BUILTIN_SYNC)
- return __sync_add_and_fetch(&a->val, 1);
-#else
-# error "Unsupported architecture for atomic operations"
-#endif
-}
-
-GIT_INLINE(int) git_atomic_add(git_atomic *a, int32_t addend)
-{
-#if defined(GIT_WIN32)
- return InterlockedExchangeAdd(&a->val, addend);
-#elif defined(GIT_BUILTIN_ATOMIC)
- return __atomic_add_fetch(&a->val, addend, __ATOMIC_SEQ_CST);
-#elif defined(GIT_BUILTIN_SYNC)
- return __sync_add_and_fetch(&a->val, addend);
-#else
-# error "Unsupported architecture for atomic operations"
-#endif
-}
-
-GIT_INLINE(int) git_atomic_dec(git_atomic *a)
-{
-#if defined(GIT_WIN32)
- return InterlockedDecrement(&a->val);
-#elif defined(GIT_BUILTIN_ATOMIC)
- return __atomic_sub_fetch(&a->val, 1, __ATOMIC_SEQ_CST);
-#elif defined(GIT_BUILTIN_SYNC)
- return __sync_sub_and_fetch(&a->val, 1);
-#else
-# error "Unsupported architecture for atomic operations"
-#endif
-}
-
-GIT_INLINE(int) git_atomic_get(git_atomic *a)
-{
-#if defined(GIT_WIN32)
- return (int)InterlockedCompareExchange(&a->val, 0, 0);
-#elif defined(GIT_BUILTIN_ATOMIC)
- return __atomic_load_n(&a->val, __ATOMIC_SEQ_CST);
-#elif defined(GIT_BUILTIN_SYNC)
- return __sync_val_compare_and_swap(&a->val, 0, 0);
-#else
-# error "Unsupported architecture for atomic operations"
-#endif
-}
-
-GIT_INLINE(void *) git___compare_and_swap(
- void * volatile *ptr, void *oldval, void *newval)
-{
-#if defined(GIT_WIN32)
- volatile void *foundval;
- foundval = InterlockedCompareExchangePointer((volatile PVOID *)ptr, newval, oldval);
- return (foundval == oldval) ? oldval : newval;
-#elif defined(GIT_BUILTIN_ATOMIC)
- bool success = __atomic_compare_exchange(ptr, &oldval, &newval, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
- return success ? oldval : newval;
-#elif defined(GIT_BUILTIN_SYNC)
- volatile void *foundval;
- foundval = __sync_val_compare_and_swap(ptr, oldval, newval);
- return (foundval == oldval) ? oldval : newval;
-#else
-# error "Unsupported architecture for atomic operations"
-#endif
-}
-
-GIT_INLINE(volatile void *) git___swap(
- void * volatile *ptr, void *newval)
-{
-#if defined(GIT_WIN32)
- return InterlockedExchangePointer(ptr, newval);
-#elif defined(GIT_BUILTIN_ATOMIC)
- void * volatile foundval;
- __atomic_exchange(ptr, &newval, &foundval, __ATOMIC_SEQ_CST);
- return foundval;
-#elif defined(GIT_BUILTIN_SYNC)
- return __sync_lock_test_and_set(ptr, newval);
-#else
-# error "Unsupported architecture for atomic operations"
-#endif
-}
-
-GIT_INLINE(volatile void *) git___load(void * volatile *ptr)
-{
-#if defined(GIT_WIN32)
- void *newval = NULL, *oldval = NULL;
- volatile void *foundval = NULL;
- foundval = InterlockedCompareExchangePointer((volatile PVOID *)ptr, newval, oldval);
- return foundval;
-#elif defined(GIT_BUILTIN_ATOMIC)
- return (volatile void *)__atomic_load_n(ptr, __ATOMIC_SEQ_CST);
-#elif defined(GIT_BUILTIN_SYNC)
- return (volatile void *)__sync_val_compare_and_swap(ptr, 0, 0);
-#else
-# error "Unsupported architecture for atomic operations"
-#endif
-}
-
-#ifdef GIT_ARCH_64
-
-GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
-{
-#if defined(GIT_WIN32)
- return InterlockedExchangeAdd64(&a->val, addend);
-#elif defined(GIT_BUILTIN_ATOMIC)
- return __atomic_add_fetch(&a->val, addend, __ATOMIC_SEQ_CST);
-#elif defined(GIT_BUILTIN_SYNC)
- return __sync_add_and_fetch(&a->val, addend);
-#else
-# error "Unsupported architecture for atomic operations"
-#endif
-}
-
-GIT_INLINE(void) git_atomic64_set(git_atomic64 *a, int64_t val)
-{
-#if defined(GIT_WIN32)
- InterlockedExchange64(&a->val, val);
-#elif defined(GIT_BUILTIN_ATOMIC)
- __atomic_store_n(&a->val, val, __ATOMIC_SEQ_CST);
-#elif defined(GIT_BUILTIN_SYNC)
- __sync_lock_test_and_set(&a->val, val);
-#else
-# error "Unsupported architecture for atomic operations"
-#endif
-}
-
-GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a)
-{
-#if defined(GIT_WIN32)
- return (int64_t)InterlockedCompareExchange64(&a->val, 0, 0);
-#elif defined(GIT_BUILTIN_ATOMIC)
- return __atomic_load_n(&a->val, __ATOMIC_SEQ_CST);
-#elif defined(GIT_BUILTIN_SYNC)
- return __sync_val_compare_and_swap(&a->val, 0, 0);
-#else
-# error "Unsupported architecture for atomic operations"
-#endif
-}
-
-#endif
-
-#else
-
-#define git_threads_global_init git__noop
-
-#define git_thread unsigned int
-#define git_thread_create(thread, start_routine, arg) git__noop()
-#define git_thread_join(id, status) git__noop()
-
-/* Pthreads Mutex */
-#define git_mutex unsigned int
-#define git_mutex_init(a) git__noop()
-#define git_mutex_init(a) git__noop()
-#define git_mutex_lock(a) git__noop()
-#define git_mutex_unlock(a) git__noop()
-#define git_mutex_free(a) git__noop()
-
-/* Pthreads condition vars */
-#define git_cond unsigned int
-#define git_cond_init(c) git__noop()
-#define git_cond_free(c) git__noop()
-#define git_cond_wait(c, l) git__noop()
-#define git_cond_signal(c) git__noop()
-#define git_cond_broadcast(c) git__noop()
-
-/* Pthreads rwlock */
-#define git_rwlock unsigned int
-#define git_rwlock_init(a) git__noop()
-#define git_rwlock_rdlock(a) git__noop()
-#define git_rwlock_rdunlock(a) git__noop()
-#define git_rwlock_wrlock(a) git__noop()
-#define git_rwlock_wrunlock(a) git__noop()
-#define git_rwlock_free(a) git__noop()
-#define GIT_RWLOCK_STATIC_INIT 0
-
-
-GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
-{
- a->val = val;
-}
-
-GIT_INLINE(int) git_atomic_inc(git_atomic *a)
-{
- return ++a->val;
-}
-
-GIT_INLINE(int) git_atomic_add(git_atomic *a, int32_t addend)
-{
- a->val += addend;
- return a->val;
-}
-
-GIT_INLINE(int) git_atomic_dec(git_atomic *a)
-{
- return --a->val;
-}
-
-GIT_INLINE(int) git_atomic_get(git_atomic *a)
-{
- return (int)a->val;
-}
-
-GIT_INLINE(void *) git___compare_and_swap(
- void * volatile *ptr, void *oldval, void *newval)
-{
- if (*ptr == oldval)
- *ptr = newval;
- else
- oldval = newval;
- return oldval;
-}
-
-GIT_INLINE(volatile void *) git___swap(
- void * volatile *ptr, void *newval)
-{
- volatile void *old = *ptr;
- *ptr = newval;
- return old;
-}
-
-GIT_INLINE(volatile void *) git___load(void * volatile *ptr)
-{
- return *ptr;
-}
-
-#ifdef GIT_ARCH_64
-
-GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
-{
- a->val += addend;
- return a->val;
-}
-
-GIT_INLINE(void) git_atomic64_set(git_atomic64 *a, int64_t val)
-{
- a->val = val;
-}
-
-GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a)
-{
- return (int64_t)a->val;
-}
-
-#endif
-
-#endif
-
-/* Atomically replace oldval with newval
- * @return oldval if it was replaced or newval if it was not
- */
-#define git__compare_and_swap(P,O,N) \
- git___compare_and_swap((void * volatile *)P, O, N)
-
-#define git__swap(ptr, val) (void *)git___swap((void * volatile *)&ptr, val)
-
-#define git__load(ptr) (void *)git___load((void * volatile *)&ptr)
-
-extern int git_online_cpus(void);
-
-#if defined(GIT_THREADS)
-
-# if defined(GIT_WIN32)
-# define GIT_MEMORY_BARRIER MemoryBarrier()
-# elif defined(GIT_BUILTIN_ATOMIC)
-# define GIT_MEMORY_BARRIER __atomic_thread_fence(__ATOMIC_SEQ_CST)
-# elif defined(GIT_BUILTIN_SYNC)
-# define GIT_MEMORY_BARRIER __sync_synchronize()
-# endif
-
-#else
-
-# define GIT_MEMORY_BARRIER /* noop */
-
-#endif
-
-#endif
diff --git a/src/thread.c b/src/thread.c
new file mode 100644
index 0000000..3171771
--- /dev/null
+++ b/src/thread.c
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "common.h"
+
+#if !defined(GIT_THREADS)
+
+#define TLSDATA_MAX 16
+
+typedef struct {
+ void *value;
+ void (GIT_SYSTEM_CALL *destroy_fn)(void *);
+} tlsdata_value;
+
+static tlsdata_value tlsdata_values[TLSDATA_MAX];
+static int tlsdata_cnt = 0;
+
+int git_tlsdata_init(git_tlsdata_key *key, void (GIT_SYSTEM_CALL *destroy_fn)(void *))
+{
+ if (tlsdata_cnt >= TLSDATA_MAX)
+ return -1;
+
+ tlsdata_values[tlsdata_cnt].value = NULL;
+ tlsdata_values[tlsdata_cnt].destroy_fn = destroy_fn;
+
+ *key = tlsdata_cnt;
+ tlsdata_cnt++;
+
+ return 0;
+}
+
+int git_tlsdata_set(git_tlsdata_key key, void *value)
+{
+ if (key < 0 || key > tlsdata_cnt)
+ return -1;
+
+ tlsdata_values[key].value = value;
+ return 0;
+}
+
+void *git_tlsdata_get(git_tlsdata_key key)
+{
+ if (key < 0 || key > tlsdata_cnt)
+ return NULL;
+
+ return tlsdata_values[key].value;
+}
+
+int git_tlsdata_dispose(git_tlsdata_key key)
+{
+ void *value;
+ void (*destroy_fn)(void *) = NULL;
+
+ if (key < 0 || key > tlsdata_cnt)
+ return -1;
+
+ value = tlsdata_values[key].value;
+ destroy_fn = tlsdata_values[key].destroy_fn;
+
+ tlsdata_values[key].value = NULL;
+ tlsdata_values[key].destroy_fn = NULL;
+
+ if (value && destroy_fn)
+ destroy_fn(value);
+
+ return 0;
+}
+
+#elif defined(GIT_WIN32)
+
+int git_tlsdata_init(git_tlsdata_key *key, void (GIT_SYSTEM_CALL *destroy_fn)(void *))
+{
+ DWORD fls_index = FlsAlloc(destroy_fn);
+
+ if (fls_index == FLS_OUT_OF_INDEXES)
+ return -1;
+
+ *key = fls_index;
+ return 0;
+}
+
+int git_tlsdata_set(git_tlsdata_key key, void *value)
+{
+ if (!FlsSetValue(key, value))
+ return -1;
+
+ return 0;
+}
+
+void *git_tlsdata_get(git_tlsdata_key key)
+{
+ return FlsGetValue(key);
+}
+
+int git_tlsdata_dispose(git_tlsdata_key key)
+{
+ if (!FlsFree(key))
+ return -1;
+
+ return 0;
+}
+
+#elif defined(_POSIX_THREADS)
+
+int git_tlsdata_init(git_tlsdata_key *key, void (GIT_SYSTEM_CALL *destroy_fn)(void *))
+{
+ if (pthread_key_create(key, destroy_fn) != 0)
+ return -1;
+
+ return 0;
+}
+
+int git_tlsdata_set(git_tlsdata_key key, void *value)
+{
+ if (pthread_setspecific(key, value) != 0)
+ return -1;
+
+ return 0;
+}
+
+void *git_tlsdata_get(git_tlsdata_key key)
+{
+ return pthread_getspecific(key);
+}
+
+int git_tlsdata_dispose(git_tlsdata_key key)
+{
+ if (pthread_key_delete(key) != 0)
+ return -1;
+
+ return 0;
+}
+
+#else
+# error unknown threading model
+#endif
diff --git a/src/thread.h b/src/thread.h
new file mode 100644
index 0000000..b4f8692
--- /dev/null
+++ b/src/thread.h
@@ -0,0 +1,422 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_thread_h__
+#define INCLUDE_thread_h__
+
+#if defined(GIT_THREADS)
+
+#if defined(__clang__)
+
+# if (__clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 1))
+# error Atomic primitives do not exist on this version of clang; configure libgit2 with -DTHREADSAFE=OFF
+# else
+# define GIT_BUILTIN_ATOMIC
+# endif
+
+#elif defined(__GNUC__)
+
+# if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1))
+# error Atomic primitives do not exist on this version of gcc; configure libgit2 with -DTHREADSAFE=OFF
+# elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
+# define GIT_BUILTIN_ATOMIC
+# else
+# define GIT_BUILTIN_SYNC
+# endif
+
+#endif
+
+#endif /* GIT_THREADS */
+
+/* Common operations even if threading has been disabled */
+typedef struct {
+#if defined(GIT_WIN32)
+ volatile long val;
+#else
+ volatile int val;
+#endif
+} git_atomic32;
+
+#ifdef GIT_ARCH_64
+
+typedef struct {
+#if defined(GIT_WIN32)
+ volatile __int64 val;
+#else
+ volatile int64_t val;
+#endif
+} git_atomic64;
+
+typedef git_atomic64 git_atomic_ssize;
+
+#define git_atomic_ssize_set git_atomic64_set
+#define git_atomic_ssize_add git_atomic64_add
+#define git_atomic_ssize_get git_atomic64_get
+
+#else
+
+typedef git_atomic32 git_atomic_ssize;
+
+#define git_atomic_ssize_set git_atomic32_set
+#define git_atomic_ssize_add git_atomic32_add
+#define git_atomic_ssize_get git_atomic32_get
+
+#endif
+
+#ifdef GIT_THREADS
+
+#ifdef GIT_WIN32
+# include "win32/thread.h"
+#else
+# include "unix/pthread.h"
+#endif
+
+GIT_INLINE(void) git_atomic32_set(git_atomic32 *a, int val)
+{
+#if defined(GIT_WIN32)
+ InterlockedExchange(&a->val, (LONG)val);
+#elif defined(GIT_BUILTIN_ATOMIC)
+ __atomic_store_n(&a->val, val, __ATOMIC_SEQ_CST);
+#elif defined(GIT_BUILTIN_SYNC)
+ __sync_lock_test_and_set(&a->val, val);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+GIT_INLINE(int) git_atomic32_inc(git_atomic32 *a)
+{
+#if defined(GIT_WIN32)
+ return InterlockedIncrement(&a->val);
+#elif defined(GIT_BUILTIN_ATOMIC)
+ return __atomic_add_fetch(&a->val, 1, __ATOMIC_SEQ_CST);
+#elif defined(GIT_BUILTIN_SYNC)
+ return __sync_add_and_fetch(&a->val, 1);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+GIT_INLINE(int) git_atomic32_add(git_atomic32 *a, int32_t addend)
+{
+#if defined(GIT_WIN32)
+ return InterlockedExchangeAdd(&a->val, addend);
+#elif defined(GIT_BUILTIN_ATOMIC)
+ return __atomic_add_fetch(&a->val, addend, __ATOMIC_SEQ_CST);
+#elif defined(GIT_BUILTIN_SYNC)
+ return __sync_add_and_fetch(&a->val, addend);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+GIT_INLINE(int) git_atomic32_dec(git_atomic32 *a)
+{
+#if defined(GIT_WIN32)
+ return InterlockedDecrement(&a->val);
+#elif defined(GIT_BUILTIN_ATOMIC)
+ return __atomic_sub_fetch(&a->val, 1, __ATOMIC_SEQ_CST);
+#elif defined(GIT_BUILTIN_SYNC)
+ return __sync_sub_and_fetch(&a->val, 1);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+GIT_INLINE(int) git_atomic32_get(git_atomic32 *a)
+{
+#if defined(GIT_WIN32)
+ return (int)InterlockedCompareExchange(&a->val, 0, 0);
+#elif defined(GIT_BUILTIN_ATOMIC)
+ return __atomic_load_n(&a->val, __ATOMIC_SEQ_CST);
+#elif defined(GIT_BUILTIN_SYNC)
+ return __sync_val_compare_and_swap(&a->val, 0, 0);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+GIT_INLINE(void *) git_atomic__compare_and_swap(
+ void * volatile *ptr, void *oldval, void *newval)
+{
+#if defined(GIT_WIN32)
+ volatile void *foundval;
+ foundval = InterlockedCompareExchangePointer((volatile PVOID *)ptr, newval, oldval);
+ return (foundval == oldval) ? oldval : newval;
+#elif defined(GIT_BUILTIN_ATOMIC)
+ bool success = __atomic_compare_exchange(ptr, &oldval, &newval, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
+ return success ? oldval : newval;
+#elif defined(GIT_BUILTIN_SYNC)
+ volatile void *foundval;
+ foundval = __sync_val_compare_and_swap(ptr, oldval, newval);
+ return (foundval == oldval) ? oldval : newval;
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+GIT_INLINE(volatile void *) git_atomic__swap(
+ void * volatile *ptr, void *newval)
+{
+#if defined(GIT_WIN32)
+ return InterlockedExchangePointer(ptr, newval);
+#elif defined(GIT_BUILTIN_ATOMIC)
+ void * volatile foundval;
+ __atomic_exchange(ptr, &newval, &foundval, __ATOMIC_SEQ_CST);
+ return foundval;
+#elif defined(GIT_BUILTIN_SYNC)
+ return __sync_lock_test_and_set(ptr, newval);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+GIT_INLINE(volatile void *) git_atomic__load(void * volatile *ptr)
+{
+#if defined(GIT_WIN32)
+ void *newval = NULL, *oldval = NULL;
+ volatile void *foundval = NULL;
+ foundval = InterlockedCompareExchangePointer((volatile PVOID *)ptr, newval, oldval);
+ return foundval;
+#elif defined(GIT_BUILTIN_ATOMIC)
+ return (volatile void *)__atomic_load_n(ptr, __ATOMIC_SEQ_CST);
+#elif defined(GIT_BUILTIN_SYNC)
+ return (volatile void *)__sync_val_compare_and_swap(ptr, 0, 0);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+#ifdef GIT_ARCH_64
+
+GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
+{
+#if defined(GIT_WIN32)
+ return InterlockedExchangeAdd64(&a->val, addend);
+#elif defined(GIT_BUILTIN_ATOMIC)
+ return __atomic_add_fetch(&a->val, addend, __ATOMIC_SEQ_CST);
+#elif defined(GIT_BUILTIN_SYNC)
+ return __sync_add_and_fetch(&a->val, addend);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+GIT_INLINE(void) git_atomic64_set(git_atomic64 *a, int64_t val)
+{
+#if defined(GIT_WIN32)
+ InterlockedExchange64(&a->val, val);
+#elif defined(GIT_BUILTIN_ATOMIC)
+ __atomic_store_n(&a->val, val, __ATOMIC_SEQ_CST);
+#elif defined(GIT_BUILTIN_SYNC)
+ __sync_lock_test_and_set(&a->val, val);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a)
+{
+#if defined(GIT_WIN32)
+ return (int64_t)InterlockedCompareExchange64(&a->val, 0, 0);
+#elif defined(GIT_BUILTIN_ATOMIC)
+ return __atomic_load_n(&a->val, __ATOMIC_SEQ_CST);
+#elif defined(GIT_BUILTIN_SYNC)
+ return __sync_val_compare_and_swap(&a->val, 0, 0);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+#endif
+
+#else
+
+#define git_threads_global_init git__noop
+
+#define git_thread unsigned int
+#define git_thread_create(thread, start_routine, arg) git__noop()
+#define git_thread_join(id, status) git__noop()
+
+/* Pthreads Mutex */
+#define git_mutex unsigned int
+#define git_mutex_init(a) git__noop()
+#define git_mutex_init(a) git__noop()
+#define git_mutex_lock(a) git__noop()
+#define git_mutex_unlock(a) git__noop()
+#define git_mutex_free(a) git__noop()
+
+/* Pthreads condition vars */
+#define git_cond unsigned int
+#define git_cond_init(c) git__noop()
+#define git_cond_free(c) git__noop()
+#define git_cond_wait(c, l) git__noop()
+#define git_cond_signal(c) git__noop()
+#define git_cond_broadcast(c) git__noop()
+
+/* Pthreads rwlock */
+#define git_rwlock unsigned int
+#define git_rwlock_init(a) git__noop()
+#define git_rwlock_rdlock(a) git__noop()
+#define git_rwlock_rdunlock(a) git__noop()
+#define git_rwlock_wrlock(a) git__noop()
+#define git_rwlock_wrunlock(a) git__noop()
+#define git_rwlock_free(a) git__noop()
+#define GIT_RWLOCK_STATIC_INIT 0
+
+
+GIT_INLINE(void) git_atomic32_set(git_atomic32 *a, int val)
+{
+ a->val = val;
+}
+
+GIT_INLINE(int) git_atomic32_inc(git_atomic32 *a)
+{
+ return ++a->val;
+}
+
+GIT_INLINE(int) git_atomic32_add(git_atomic32 *a, int32_t addend)
+{
+ a->val += addend;
+ return a->val;
+}
+
+GIT_INLINE(int) git_atomic32_dec(git_atomic32 *a)
+{
+ return --a->val;
+}
+
+GIT_INLINE(int) git_atomic32_get(git_atomic32 *a)
+{
+ return (int)a->val;
+}
+
+GIT_INLINE(void *) git_atomic__compare_and_swap(
+ void * volatile *ptr, void *oldval, void *newval)
+{
+ if (*ptr == oldval)
+ *ptr = newval;
+ else
+ oldval = newval;
+ return oldval;
+}
+
+GIT_INLINE(volatile void *) git_atomic__swap(
+ void * volatile *ptr, void *newval)
+{
+ volatile void *old = *ptr;
+ *ptr = newval;
+ return old;
+}
+
+GIT_INLINE(volatile void *) git_atomic__load(void * volatile *ptr)
+{
+ return *ptr;
+}
+
+#ifdef GIT_ARCH_64
+
+GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
+{
+ a->val += addend;
+ return a->val;
+}
+
+GIT_INLINE(void) git_atomic64_set(git_atomic64 *a, int64_t val)
+{
+ a->val = val;
+}
+
+GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a)
+{
+ return (int64_t)a->val;
+}
+
+#endif
+
+#endif
+
+/* Atomically replace oldval with newval
+ * @return oldval if it was replaced or newval if it was not
+ */
+#define git_atomic_compare_and_swap(P,O,N) \
+ git_atomic__compare_and_swap((void * volatile *)P, O, N)
+
+#define git_atomic_swap(ptr, val) \
+ (void *)git_atomic__swap((void * volatile *)&ptr, val)
+
+#define git_atomic_load(ptr) \
+ (void *)git_atomic__load((void * volatile *)&ptr)
+
+#if defined(GIT_THREADS)
+
+# if defined(GIT_WIN32)
+# define GIT_MEMORY_BARRIER MemoryBarrier()
+# elif defined(GIT_BUILTIN_ATOMIC)
+# define GIT_MEMORY_BARRIER __atomic_thread_fence(__ATOMIC_SEQ_CST)
+# elif defined(GIT_BUILTIN_SYNC)
+# define GIT_MEMORY_BARRIER __sync_synchronize()
+# endif
+
+#else
+
+# define GIT_MEMORY_BARRIER /* noop */
+
+#endif
+
+/* Thread-local data */
+
+#if !defined(GIT_THREADS)
+# define git_tlsdata_key int
+#elif defined(GIT_WIN32)
+# define git_tlsdata_key DWORD
+#elif defined(_POSIX_THREADS)
+# define git_tlsdata_key pthread_key_t
+#else
+# error unknown threading model
+#endif
+
+/**
+ * Create a thread-local data key. The destroy function will be
+ * called upon thread exit. On some platforms, it may be called
+ * when all threads have deleted their keys.
+ *
+ * Note that the tlsdata functions do not set an error message on
+ * failure; this is because the error handling in libgit2 is itself
+ * handled by thread-local data storage.
+ *
+ * @param key the tlsdata key
+ * @param destroy_fn function pointer called upon thread exit
+ * @return 0 on success, non-zero on failure
+ */
+int git_tlsdata_init(git_tlsdata_key *key, void (GIT_SYSTEM_CALL *destroy_fn)(void *));
+
+/**
+ * Set a the thread-local value for the given key.
+ *
+ * @param key the tlsdata key to store data on
+ * @param value the pointer to store
+ * @return 0 on success, non-zero on failure
+ */
+int git_tlsdata_set(git_tlsdata_key key, void *value);
+
+/**
+ * Get the thread-local value for the given key.
+ *
+ * @param key the tlsdata key to retrieve the value of
+ * @return the pointer stored with git_tlsdata_set
+ */
+void *git_tlsdata_get(git_tlsdata_key key);
+
+/**
+ * Delete the given thread-local key.
+ *
+ * @param key the tlsdata key to dispose
+ * @return 0 on success, non-zero on failure
+ */
+int git_tlsdata_dispose(git_tlsdata_key key);
+
+#endif
diff --git a/src/threadstate.c b/src/threadstate.c
index 1c5ac60..6031e82 100644
--- a/src/threadstate.c
+++ b/src/threadstate.c
@@ -8,8 +8,6 @@
#include "threadstate.h"
#include "runtime.h"
-static void threadstate_dispose(git_threadstate *threadstate);
-
/**
* Handle the thread-local state
*
@@ -17,8 +15,7 @@ static void threadstate_dispose(git_threadstate *threadstate);
* of `git_libgit2_init` (which itself must be called
* before calling any other function in the library).
*
- * This function allocates a TLS index (using pthreads
- * or fiber-local storage in Win32) to store the per-
+ * This function allocates a TLS index to store the per-
* thread state.
*
* Any internal method that requires thread-local state
@@ -30,77 +27,41 @@ static void threadstate_dispose(git_threadstate *threadstate);
* (`git_threadstate_global_shutdown`) which will free the
* TLS index. This shutdown handler will be called by
* `git_libgit2_shutdown`.
- *
- * If libgit2 is built without threading support, the
- * `git_threadstate_get()` call returns a pointer to a single,
- * statically allocated global state. The `git_thread_`
- * functions are not available in that case.
*/
-#if defined(GIT_THREADS) && defined(GIT_WIN32)
+static git_tlsdata_key tls_key;
-static DWORD fls_index;
-
-static void git_threadstate_global_shutdown(void)
+static void threadstate_dispose(git_threadstate *threadstate)
{
- FlsFree(fls_index);
+ if (!threadstate)
+ return;
+
+ git__free(threadstate->error_t.message);
+ threadstate->error_t.message = NULL;
}
-static void WINAPI fls_free(void *threadstate)
+static void GIT_SYSTEM_CALL threadstate_free(void *threadstate)
{
threadstate_dispose(threadstate);
git__free(threadstate);
}
-int git_threadstate_global_init(void)
-{
- if ((fls_index = FlsAlloc(fls_free)) == FLS_OUT_OF_INDEXES)
- return -1;
-
- return git_runtime_shutdown_register(git_threadstate_global_shutdown);
-}
-
-git_threadstate *git_threadstate_get(void)
-{
- git_threadstate *threadstate;
-
- if ((threadstate = FlsGetValue(fls_index)) != NULL)
- return threadstate;
-
- if ((threadstate = git__calloc(1, sizeof(git_threadstate))) == NULL ||
- git_buf_init(&threadstate->error_buf, 0) < 0)
- return NULL;
-
- FlsSetValue(fls_index, threadstate);
- return threadstate;
-}
-
-#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
-
-static pthread_key_t tls_key;
-
static void git_threadstate_global_shutdown(void)
{
git_threadstate *threadstate;
- threadstate = pthread_getspecific(tls_key);
- pthread_setspecific(tls_key, NULL);
+ threadstate = git_tlsdata_get(tls_key);
+ git_tlsdata_set(tls_key, NULL);
threadstate_dispose(threadstate);
git__free(threadstate);
- pthread_key_delete(tls_key);
-}
-
-static void tls_free(void *threadstate)
-{
- threadstate_dispose(threadstate);
- git__free(threadstate);
+ git_tlsdata_dispose(tls_key);
}
int git_threadstate_global_init(void)
{
- if (pthread_key_create(&tls_key, &tls_free) != 0)
+ if (git_tlsdata_init(&tls_key, &threadstate_free) != 0)
return -1;
return git_runtime_shutdown_register(git_threadstate_global_shutdown);
@@ -110,46 +71,13 @@ git_threadstate *git_threadstate_get(void)
{
git_threadstate *threadstate;
- if ((threadstate = pthread_getspecific(tls_key)) != NULL)
+ if ((threadstate = git_tlsdata_get(tls_key)) != NULL)
return threadstate;
if ((threadstate = git__calloc(1, sizeof(git_threadstate))) == NULL ||
git_buf_init(&threadstate->error_buf, 0) < 0)
return NULL;
- pthread_setspecific(tls_key, threadstate);
+ git_tlsdata_set(tls_key, threadstate);
return threadstate;
}
-
-#elif defined(GIT_THREADS)
-# error unknown threading model
-#else
-
-static git_threadstate threadstate;
-
-static void git_threadstate_global_shutdown(void)
-{
- threadstate_dispose(&threadstate);
- memset(&threadstate, 0, sizeof(git_threadstate));
-}
-
-int git_threadstate_global_init(void)
-{
- return git_runtime_shutdown_register(git_threadstate_global_shutdown);
-}
-
-git_threadstate *git_threadstate_get(void)
-{
- return &threadstate;
-}
-
-#endif
-
-static void threadstate_dispose(git_threadstate *threadstate)
-{
- if (!threadstate)
- return;
-
- git__free(threadstate->error_t.message);
- threadstate->error_t.message = NULL;
-}
diff --git a/src/transports/local.c b/src/transports/local.c
index 4af85db..bb31b13 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -36,7 +36,7 @@ typedef struct {
char *url;
int direction;
int flags;
- git_atomic cancelled;
+ git_atomic32 cancelled;
git_repository *repo;
git_transport_message_cb progress_cb;
git_transport_message_cb error_cb;
@@ -671,7 +671,7 @@ static void local_cancel(git_transport *transport)
{
transport_local *t = (transport_local *)transport;
- git_atomic_set(&t->cancelled, 1);
+ git_atomic32_set(&t->cancelled, 1);
}
static int local_close(git_transport *transport)
diff --git a/src/transports/smart.c b/src/transports/smart.c
index 3b8a14a..da8fe8e 100644
--- a/src/transports/smart.c
+++ b/src/transports/smart.c
@@ -30,7 +30,7 @@ static int git_smart__recv_cb(gitno_buffer *buf)
if (t->packetsize_cb && !t->cancelled.val) {
error = t->packetsize_cb(bytes_read, t->packetsize_payload);
if (error) {
- git_atomic_set(&t->cancelled, 1);
+ git_atomic32_set(&t->cancelled, 1);
return GIT_EUSER;
}
}
@@ -389,7 +389,7 @@ static void git_smart__cancel(git_transport *transport)
{
transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
- git_atomic_set(&t->cancelled, 1);
+ git_atomic32_set(&t->cancelled, 1);
}
static int git_smart__is_connected(git_transport *transport)
diff --git a/src/transports/smart.h b/src/transports/smart.h
index 18e0b7e..a05d4c9 100644
--- a/src/transports/smart.h
+++ b/src/transports/smart.h
@@ -153,7 +153,7 @@ typedef struct {
git_vector refs;
git_vector heads;
git_vector common;
- git_atomic cancelled;
+ git_atomic32 cancelled;
packetsize_cb packetsize_cb;
void *packetsize_payload;
unsigned rpc : 1,
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index 18dcaa8..9482915 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -535,7 +535,7 @@ int git_smart__download_pack(
/* We might have something in the buffer already from negotiate_fetch */
if (t->buffer.offset > 0 && !t->cancelled.val)
if (t->packetsize_cb(t->buffer.offset, t->packetsize_payload))
- git_atomic_set(&t->cancelled, 1);
+ git_atomic32_set(&t->cancelled, 1);
}
if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
diff --git a/src/util.c b/src/util.c
index 2efb212..87ccf32 100644
--- a/src/util.c
+++ b/src/util.c
@@ -13,6 +13,11 @@
# include "win32/utf-conv.h"
# include "win32/w32_buffer.h"
+# ifndef WIN32_LEAN_AND_MEAN
+# define WIN32_LEAN_AND_MEAN
+# endif
+# include <windows.h>
+
# ifdef HAVE_QSORT_S
# include <search.h>
# endif
@@ -22,6 +27,10 @@
# include <Shlwapi.h>
#endif
+#if defined(hpux) || defined(__hpux) || defined(_hpux)
+# include <sys/pstat.h>
+#endif
+
int git__strntol64(int64_t *result, const char *nptr, size_t nptr_len, const char **endptr, int base)
{
const char *p;
@@ -678,7 +687,7 @@ typedef struct {
void *payload;
} git__qsort_r_glue;
-static int GIT_STDLIB_CALL git__qsort_r_glue_cmp(
+static int GIT_LIBGIT2_CALL git__qsort_r_glue_cmp(
void *payload, const void *a, const void *b)
{
git__qsort_r_glue *glue = payload;
@@ -893,3 +902,43 @@ int git__getenv(git_buf *out, const char *name)
return git_buf_puts(out, val);
}
#endif
+
+/*
+ * By doing this in two steps we can at least get
+ * the function to be somewhat coherent, even
+ * with this disgusting nest of #ifdefs.
+ */
+#ifndef _SC_NPROCESSORS_ONLN
+# ifdef _SC_NPROC_ONLN
+# define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
+# elif defined _SC_CRAY_NCPU
+# define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU
+# endif
+#endif
+
+int git__online_cpus(void)
+{
+#ifdef _SC_NPROCESSORS_ONLN
+ long ncpus;
+#endif
+
+#ifdef _WIN32
+ SYSTEM_INFO info;
+ GetSystemInfo(&info);
+
+ if ((int)info.dwNumberOfProcessors > 0)
+ return (int)info.dwNumberOfProcessors;
+#elif defined(hpux) || defined(__hpux) || defined(_hpux)
+ struct pst_dynamic psd;
+
+ if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0))
+ return (int)psd.psd_proc_cnt;
+#endif
+
+#ifdef _SC_NPROCESSORS_ONLN
+ if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)
+ return (int)ncpus;
+#endif
+
+ return 1;
+}
diff --git a/src/util.h b/src/util.h
index 185a1b1..5076df2 100644
--- a/src/util.h
+++ b/src/util.h
@@ -18,7 +18,7 @@
#include "buffer.h"
#include "common.h"
#include "strnlen.h"
-#include "thread-utils.h"
+#include "thread.h"
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define bitsizeof(x) (CHAR_BIT * sizeof(x))
@@ -169,29 +169,29 @@ extern int git__strncasecmp(const char *a, const char *b, size_t sz);
extern int git__strcasesort_cmp(const char *a, const char *b);
typedef struct {
- git_atomic refcount;
+ git_atomic32 refcount;
void *owner;
} git_refcount;
typedef void (*git_refcount_freeptr)(void *r);
#define GIT_REFCOUNT_INC(r) { \
- git_atomic_inc(&(r)->rc.refcount); \
+ git_atomic32_inc(&(r)->rc.refcount); \
}
#define GIT_REFCOUNT_DEC(_r, do_free) { \
git_refcount *r = &(_r)->rc; \
- int val = git_atomic_dec(&r->refcount); \
+ int val = git_atomic32_dec(&r->refcount); \
if (val <= 0 && r->owner == NULL) { do_free(_r); } \
}
#define GIT_REFCOUNT_OWN(r, o) { \
- (void)git__swap((r)->rc.owner, o); \
+ (void)git_atomic_swap((r)->rc.owner, o); \
}
-#define GIT_REFCOUNT_OWNER(r) git__load((r)->rc.owner)
+#define GIT_REFCOUNT_OWNER(r) git_atomic_load((r)->rc.owner)
-#define GIT_REFCOUNT_VAL(r) git_atomic_get((r)->rc.refcount)
+#define GIT_REFCOUNT_VAL(r) git_atomic32_get((r)->rc.refcount)
static signed char from_hex[] = {
@@ -414,6 +414,8 @@ GIT_INLINE(double) git__timer(void)
extern int git__getenv(git_buf *out, const char *name);
+extern int git__online_cpus(void);
+
GIT_INLINE(int) git__noop(void) { return 0; }
#include "alloc.h"
diff --git a/src/win32/msvc-compat.h b/src/win32/msvc-compat.h
index 4cf471f..03f9f36 100644
--- a/src/win32/msvc-compat.h
+++ b/src/win32/msvc-compat.h
@@ -23,6 +23,14 @@ typedef SSIZE_T ssize_t;
#endif
-#define GIT_STDLIB_CALL __cdecl
+/*
+ * Offer GIT_LIBGIT2_CALL for our calling conventions (__cdecl, always).
+ * This is useful for providing callbacks to userspace code.
+ *
+ * Offer GIT_SYSTEM_CALL for the system calling conventions (__stdcall on
+ * Win32). Useful for providing callbacks to system libraries.
+ */
+#define GIT_LIBGIT2_CALL __cdecl
+#define GIT_SYSTEM_CALL NTAPI
#endif
diff --git a/tests/threads/diff.c b/tests/threads/diff.c
index 6996427..04c8cb9 100644
--- a/tests/threads/diff.c
+++ b/tests/threads/diff.c
@@ -17,7 +17,7 @@
static git_repository *_repo;
static git_tree *_a, *_b;
-static git_atomic _counts[4];
+static git_atomic32 _counts[4];
static int _check_counts;
#ifdef GIT_WIN32
static int _retries;
@@ -66,10 +66,10 @@ static void free_trees(void)
git_tree_free(_b); _b = NULL;
if (_check_counts) {
- cl_assert_equal_i(288, git_atomic_get(&_counts[0]));
- cl_assert_equal_i(112, git_atomic_get(&_counts[1]));
- cl_assert_equal_i( 80, git_atomic_get(&_counts[2]));
- cl_assert_equal_i( 96, git_atomic_get(&_counts[3]));
+ cl_assert_equal_i(288, git_atomic32_get(&_counts[0]));
+ cl_assert_equal_i(112, git_atomic32_get(&_counts[1]));
+ cl_assert_equal_i( 80, git_atomic32_get(&_counts[2]));
+ cl_assert_equal_i( 96, git_atomic32_get(&_counts[3]));
}
}
@@ -107,14 +107,14 @@ static void *run_index_diffs(void *arg)
/* keep some diff stats to make sure results are as expected */
i = git_diff_num_deltas(diff);
- git_atomic_add(&_counts[0], (int32_t)i);
+ git_atomic32_add(&_counts[0], (int32_t)i);
exp[0] = (int)i;
while (i > 0) {
switch (git_diff_get_delta(diff, --i)->status) {
- case GIT_DELTA_MODIFIED: exp[1]++; git_atomic_inc(&_counts[1]); break;
- case GIT_DELTA_ADDED: exp[2]++; git_atomic_inc(&_counts[2]); break;
- case GIT_DELTA_DELETED: exp[3]++; git_atomic_inc(&_counts[3]); break;
+ case GIT_DELTA_MODIFIED: exp[1]++; git_atomic32_inc(&_counts[1]); break;
+ case GIT_DELTA_ADDED: exp[2]++; git_atomic32_inc(&_counts[2]); break;
+ case GIT_DELTA_DELETED: exp[3]++; git_atomic32_inc(&_counts[3]); break;
default: break;
}
}
diff --git a/tests/threads/thread_helpers.h b/tests/threads/thread_helpers.h
index 3c13cfb..0f23a4c 100644
--- a/tests/threads/thread_helpers.h
+++ b/tests/threads/thread_helpers.h
@@ -1,4 +1,4 @@
-#include "thread-utils.h"
+#include "thread.h"
void run_in_parallel(
int repeats,
diff --git a/tests/threads/tlsdata.c b/tests/threads/tlsdata.c
new file mode 100644
index 0000000..7c69b44
--- /dev/null
+++ b/tests/threads/tlsdata.c
@@ -0,0 +1,65 @@
+#include "clar_libgit2.h"
+
+#include "thread_helpers.h"
+
+void test_threads_tlsdata__can_set_and_get(void)
+{
+ git_tlsdata_key key_one, key_two, key_three;
+
+ cl_git_pass(git_tlsdata_init(&key_one, NULL));
+ cl_git_pass(git_tlsdata_init(&key_two, NULL));
+ cl_git_pass(git_tlsdata_init(&key_three, NULL));
+
+ cl_git_pass(git_tlsdata_set(key_one, (void *)(size_t)42424242));
+ cl_git_pass(git_tlsdata_set(key_two, (void *)(size_t)0xdeadbeef));
+ cl_git_pass(git_tlsdata_set(key_three, (void *)(size_t)98761234));
+
+ cl_assert_equal_sz((size_t)42424242, git_tlsdata_get(key_one));
+ cl_assert_equal_sz((size_t)0xdeadbeef, git_tlsdata_get(key_two));
+ cl_assert_equal_sz((size_t)98761234, git_tlsdata_get(key_three));
+
+ cl_git_pass(git_tlsdata_dispose(key_one));
+ cl_git_pass(git_tlsdata_dispose(key_two));
+ cl_git_pass(git_tlsdata_dispose(key_three));
+}
+
+#ifdef GIT_THREADS
+
+static void *set_and_get(void *param)
+{
+ git_tlsdata_key *tlsdata_key = (git_tlsdata_key *)param;
+ int val;
+
+ if (git_tlsdata_set(*tlsdata_key, &val) != 0 ||
+ git_tlsdata_get(*tlsdata_key) != &val)
+ return (void *)0;
+
+ return (void *)1;
+}
+
+#endif
+
+#define THREAD_COUNT 10
+
+void test_threads_tlsdata__threads(void)
+{
+#ifdef GIT_THREADS
+ git_thread thread[THREAD_COUNT];
+ git_tlsdata_key tlsdata;
+ int i;
+
+ cl_git_pass(git_tlsdata_init(&tlsdata, NULL));
+
+ for (i = 0; i < THREAD_COUNT; i++)
+ cl_git_pass(git_thread_create(&thread[i], set_and_get, &tlsdata));
+
+ for (i = 0; i < THREAD_COUNT; i++) {
+ void *result;
+
+ cl_git_pass(git_thread_join(&thread[i], &result));
+ cl_assert_equal_sz(1, (size_t)result);
+ }
+
+ cl_git_pass(git_tlsdata_dispose(tlsdata));
+#endif
+}