1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
|
/** Control of executable units within a shared virtual memory space
Copyright (C) 1996-2018 Free Software Foundation, Inc.
Original Author: Scott Christley <scottc@net-community.com>
Rewritten by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
Created: 1996
Rewritten by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
to add optimisations features for faster thread access.
Modified by: Nicola Pero <n.pero@mi.flashnet.it>
to add GNUstep extensions allowing to interact with threads created
by external libraries/code (eg, a Java Virtual Machine).
This file is part of the GNUstep Objective-C Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
<title>NSThread class reference</title>
*/
#import "common.h"
#import "GSPThread.h"
#ifdef _WIN32
#import <processthreadsapi.h>
#endif
// Dummy implementatation
// cleaner than IFDEF'ing the code everywhere
#ifndef HAVE_PTHREAD_SPIN_LOCK
typedef volatile int pthread_spinlock_t;
int pthread_spin_init(pthread_spinlock_t *lock, int pshared)
{
#if DEBUG && !__has_builtin(__sync_bool_compare_and_swap)
fprintf(stderr,"NSThread.m: Warning this platform does not support spin locks - init.\n");
#endif
return 0;
}
int pthread_spin_lock(pthread_spinlock_t *lock)
{
#if __has_builtin(__sync_bool_compare_and_swap)
int count = 0;
// Set the spin lock value to 1 if it is 0.
while(!__sync_bool_compare_and_swap(lock, 0, 1))
{
count++;
if (0 == count % 10)
{
// If it is already 1, let another thread play with the CPU for a
// bit then try again.
#if defined(_WIN32)
Sleep(0);
#else
sleep(0);
#endif
}
}
#else
#warning no spin_locks, using dummy versions
#endif
return 0;
}
int pthread_spin_unlock(pthread_spinlock_t *lock)
{
#if __has_builtin(__sync_bool_compare_and_swap)
__sync_synchronize();
*lock = 0;
#endif
return 0;
}
int pthread_spin_destroy(pthread_spinlock_t *lock)
{
return 0;
}
#endif /* HAVE_PTHREAD_SPIN_LOCK */
/** Structure for holding lock information for a thread.
*/
typedef struct {
pthread_spinlock_t spin; /* protect access to struct members */
NSHashTable *held; /* all locks/conditions held by thread */
id wait; /* the lock/condition we are waiting for */
} GSLockInfo;
#define EXPOSE_NSThread_IVARS 1
#define GS_NSThread_IVARS \
id _stringCollatorCache; \
BOOL _targetIsBlock; \
gs_thread_id_t _pthreadID; \
NSUInteger _threadID; \
GSLockInfo _lockInfo
#ifdef HAVE_NANOSLEEP
# include <time.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
#if defined(HAVE_SYS_FILE_H)
# include <sys/file.h>
#endif
#if defined(__ANDROID__)
# include <string.h> // For strerror
# include <sys/resource.h> // For getpriority and setpriority
#endif
#if defined(HAVE_SYS_FCNTL_H)
# include <sys/fcntl.h>
#elif defined(HAVE_FCNTL_H)
# include <fcntl.h>
#endif
#if defined(__POSIX_SOURCE)\
|| defined(__EXT_POSIX1_198808)\
|| defined(O_NONBLOCK)
#define NBLK_OPT O_NONBLOCK
#else
#define NBLK_OPT FNDELAY
#endif
#import "Foundation/NSException.h"
#import "Foundation/NSHashTable.h"
#import "Foundation/NSThread.h"
#import "Foundation/NSLock.h"
#import "Foundation/NSMapTable.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSNotificationQueue.h"
#import "Foundation/NSRunLoop.h"
#import "Foundation/NSConnection.h"
#import "Foundation/NSInvocation.h"
#import "Foundation/NSUserDefaults.h"
#import "Foundation/NSValue.h"
#import "GSPrivate.h"
#import "GSRunLoopCtxt.h"
#if defined(HAVE_PTHREAD_NP_H)
# include <pthread_np.h>
#endif
#if defined(HAVE_GETTID)
# include <unistd.h>
# include <sys/syscall.h>
# include <sys/types.h>
#endif
#define GSInternal NSThreadInternal
#include "GSInternal.h"
GS_PRIVATE_INTERNAL(NSThread)
#define pthreadID (internal->_pthreadID)
#define threadID (internal->_threadID)
#define lockInfo (internal->_lockInfo)
#define targetIsBlock (internal->_targetIsBlock)
#define stringCollatorCache (internal->_stringCollatorCache)
#if defined(HAVE_PTHREAD_MAIN_NP)
# define IS_MAIN_PTHREAD (pthread_main_np() == 1)
#elif defined(HAVE_GETTID)
# define IS_MAIN_PTHREAD (getpid() == (pid_t)syscall(SYS_gettid))
#else
# define IS_MAIN_PTHREAD (1)
#endif
/* Return the current thread ID as an unsigned long.
* Ideally, we use the operating-system's notion of a thread ID so
* that external process monitoring software will be using the same
* value that we log. If we don't know the system's mechanism, we
* use the address of the current NSThread object so that, even if
* it makes no sense externally, it can still be used to show that
* different threads generated different logs.
*/
NSUInteger
GSPrivateThreadID()
{
#if defined(_WIN32)
return (NSUInteger)GetCurrentThreadId();
#elif defined(HAVE_GETTID)
return (NSUInteger)syscall(SYS_gettid);
#elif defined(HAVE_PTHREAD_GETTHREADID_NP)
return (NSUInteger)pthread_getthreadid_np();
#else
return (NSUInteger)GSCurrentThread();
#endif
}
#ifndef PTHREAD_SETNAME
#define PTHREAD_SETNAME(a) -1
#endif
#ifndef PTHREAD_GETNAME
#define PTHREAD_GETNAME(a, b) -1
#endif
@interface NSThread (Activation)
- (void) _makeThreadCurrent;
@end
@interface NSAutoreleasePool (NSThread)
+ (void) _endThread: (NSThread*)thread;
@end
static Class threadClass = Nil;
static NSNotificationCenter *nc = nil;
static BOOL disableTraceLocks = NO;
/**
* This class performs a dual function ...
* <p>
* As a class, it is responsible for handling incoming events from
* the main runloop on a special inputFd. This consumes any bytes
* written to wake the main runloop.<br />
* During initialisation, the default runloop is set up to watch
* for data arriving on inputFd.
* </p>
* <p>
* As instances, each instance retains perform receiver and argument
* values as long as they are needed, and handles locking to support
* methods which want to block until an action has been performed.
* </p>
* <p>
* The initialize method of this class is called before any new threads
* run.
* </p>
*/
@interface GSPerformHolder : NSObject
{
id receiver;
id argument;
SEL selector;
NSConditionLock *lock; // Not retained.
NSArray *modes;
BOOL invalidated;
@public
NSException *exception;
}
+ (GSPerformHolder*) newForReceiver: (id)r
argument: (id)a
selector: (SEL)s
modes: (NSArray*)m
lock: (NSConditionLock*)l;
- (void) fire;
- (void) invalidate;
- (BOOL) isInvalidated;
- (NSArray*) modes;
@end
/**
* Sleep until the current date/time is the specified time interval
* past the reference date/time.<br />
* Implemented as a function taking an NSTimeInterval argument in order
* to avoid objc messaging and object allocation/deallocation (NSDate)
* overheads.<br />
* Used to implement [NSThread+sleepUntilDate:]
* If the date is in the past, this function simply allows other threads
* (if any) to run.
*/
void
GSSleepUntilIntervalSinceReferenceDate(NSTimeInterval when)
{
NSTimeInterval delay;
// delay is always the number of seconds we still need to wait
delay = when - GSPrivateTimeNow();
if (delay <= 0.0)
{
/* We don't need to wait, but since we are willing to wait at this
* point, we should let other threads have preference over this one.
*/
GS_YIELD();
return;
}
#if defined(_WIN32)
/*
* Avoid integer overflow by breaking up long sleeps.
*/
while (delay > 30.0*60.0)
{
// sleep 30 minutes
Sleep (30*60*1000);
delay = when - GSPrivateTimeNow();
}
/* Don't use nanosleep (even if available) on mingw ... it's reported no
* to work with pthreads.
* Sleeping may return early because of signals, so we need to re-calculate
* the required delay and check to see if we need to sleep again.
*/
while (delay > 0)
{
#if defined(HAVE_USLEEP)
/* On windows usleep() seems to perform a busy wait ... so we only
* use it for short delays ... otherwise use the less accurate Sleep()
*/
if (delay > 0.1)
{
Sleep ((NSInteger)(delay*1000));
}
else
{
usleep ((NSInteger)(delay*1000000));
}
#else
Sleep ((NSInteger)(delay*1000));
#endif /* HAVE_USLEEP */
delay = when - GSPrivateTimeNow();
}
#else /* _WIN32 */
/*
* Avoid integer overflow by breaking up long sleeps.
*/
while (delay > 30.0*60.0)
{
// sleep 30 minutes
sleep(30*60);
delay = when - GSPrivateTimeNow();
}
#ifdef HAVE_NANOSLEEP
if (delay > 0)
{
struct timespec request;
struct timespec remainder;
request.tv_sec = (time_t)delay;
request.tv_nsec = (long)((delay - request.tv_sec) * 1000000000);
remainder.tv_sec = 0;
remainder.tv_nsec = 0;
/*
* With nanosleep, we can restart the sleep after a signal by using
* the remainder information ... so we can be sure to sleep to the
* desired limit without having to re-generate the delay needed.
*/
while (nanosleep(&request, &remainder) < 0
&& (remainder.tv_sec > 0 || remainder.tv_nsec > 0))
{
request.tv_sec = remainder.tv_sec;
request.tv_nsec = remainder.tv_nsec;
remainder.tv_sec = 0;
remainder.tv_nsec = 0;
}
}
#else /* HAVE_NANOSLEEP */
/*
* sleeping may return early because of signals, so we need to re-calculate
* the required delay and check to see if we need to sleep again.
*/
while (delay > 0)
{
#if defined(HAVE_USLEEP)
usleep((NSInteger)(delay*1000000));
#else /* HAVE_USLEEP */
sleep((NSInteger)delay);
#endif /* !HAVE_USLEEP */
delay = when - GSPrivateTimeNow();
}
#endif /* !HAVE_NANOSLEEP */
#endif /* !_WIN32 */
}
static NSArray *
commonModes(void)
{
static gs_mutex_t modesLock = GS_MUTEX_INIT_STATIC;
static NSArray *modes = nil;
if (modes == nil)
{
GS_MUTEX_LOCK(modesLock);
if (modes == nil)
{
Class c = NSClassFromString(@"NSApplication");
SEL s = @selector(allRunLoopModes);
if (c != 0 && [c respondsToSelector: s])
{
modes = RETAIN([c performSelector: s]);
}
else
{
modes = [[NSArray alloc] initWithObjects:
NSDefaultRunLoopMode, NSConnectionReplyMode, nil];
}
}
GS_MUTEX_UNLOCK(modesLock);
}
return modes;
}
/*
* Flag indicating whether the objc runtime ever went multi-threaded.
*/
static BOOL entered_multi_threaded_state = NO;
static NSThread *defaultThread;
static BOOL keyInitialized = NO;
static gs_thread_key_t thread_object_key;
static NSHashTable *_activeBlocked = nil;
static NSHashTable *_activeThreads = nil;
static gs_mutex_t _activeLock = GS_MUTEX_INIT_STATIC;
/**
* pthread_t is an opaque type. It might be a scalar type or
* some kind of struct depending on the implementation, so we
* need to wrap it up in an NSValue object if we want to pass
* it around.
* This follows the CoreFoundation 'create rule' and returns an object with
* a reference count of 1.
*/
static inline NSValue* NSValueCreateFromPthread(gs_thread_id_t thread)
{
return [[NSValue alloc] initWithBytes: &thread
objCType: @encode(gs_thread_id_t)];
}
/**
* Conversely, we need to be able to retrieve the pthread_t
* from an NSValue.
*/
static inline void
_getPthreadFromNSValue(const void *value, gs_thread_id_t *thread_ptr)
{
const char *enc;
NSCAssert(thread_ptr, @"No storage for thread reference");
# ifndef NS_BLOCK_ASSERTIONS
enc = [(NSValue*)value objCType];
NSCAssert(enc != NULL && (0 == strcmp(@encode(gs_thread_id_t),enc)),
@"Invalid NSValue container for thread reference");
# endif
[(NSValue*)value getValue: (void*)thread_ptr];
}
/**
* This is the comparison function for boxed pthreads, as used by the
* NSMapTable containing them.
*/
static BOOL
_boxedPthreadIsEqual(NSMapTable *t,
const void *boxed,
const void *boxedOther)
{
gs_thread_id_t thread;
gs_thread_id_t otherThread;
_getPthreadFromNSValue(boxed, &thread);
_getPthreadFromNSValue(boxedOther, &otherThread);
#if GS_USE_WIN32_THREADS_AND_LOCKS
return thread == otherThread;
#else
return pthread_equal(thread, otherThread);
#endif
}
/**
* Since pthread_t is opaque, we cannot make any assumption about how
* to hash it. There are a few problems here:
* 1. Functions to obtain the thread ID of an arbitrary thread
* exist in the in the Win32 and some pthread APIs (GetThreadId() and
* pthread_getunique_np(), respectively), but there is no protable solution
* for this problem.
* 2. Even where pthread_getunique_np() is available, it might have different
* definitions, so it's not really robust to use it.
*
* For these reasons, we always return the same hash. That fulfills the API
* contract for NSMapTable (key-hash equality as a necessary condition for key
* equality), but makes things quite inefficient (linear search over all
* elements), so we need to keep the table small.
*/
static NSUInteger _boxedPthreadHash(NSMapTable *t, const void *value)
{
return 0;
}
/**
* Retain callback for boxed thread references.
*/
static void _boxedPthreadRetain(NSMapTable *t, const void *value)
{
RETAIN((NSValue*)value);
}
/**
* Release callback for boxed thread references.
*/
static void _boxedPthreadRelease(NSMapTable *t, void *value)
{
RELEASE((NSValue*)value);
}
/**
* Description callback for boxed thread references.
*/
static NSString *_boxedPthreadDescribe(NSMapTable *t, const void *value)
{
return [(NSValue*)value description];
}
static const NSMapTableKeyCallBacks _boxedPthreadKeyCallBacks =
{
_boxedPthreadHash,
_boxedPthreadIsEqual,
_boxedPthreadRetain,
_boxedPthreadRelease,
_boxedPthreadDescribe,
NULL
};
/**
* This map table maintains a list of all threads currently undergoing
* cleanup. This is a required so that +currentThread can still find the
* thred if called from within the late-cleanup function.
*/
static NSMapTable *_exitingThreads = nil;
static gs_mutex_t _exitingThreadsLock = GS_MUTEX_INIT_STATIC;
/**
* Called before late cleanup is run and inserts the NSThread object into the
* table that is used by GSCurrentThread to find the thread if it is called
* during cleanup. The boxedThread variable contains a boxed reference to
* the result of calling pthread_self().
*/
static inline void _willLateUnregisterThread(NSValue *boxedThread,
NSThread *specific)
{
GS_MUTEX_LOCK(_exitingThreadsLock);
/* The map table is created lazily/late so that the NSThread
* +initialize method can be called without causing other
* classes to be initialized.
* NB this locked section cannot be protected by an exception handler
* because the exception handler stores information in the current
* thread variables ... which causes recursion.
*/
if (nil == _exitingThreads)
{
_exitingThreads = NSCreateMapTable(_boxedPthreadKeyCallBacks,
NSObjectMapValueCallBacks, 10);
}
NSMapInsert(_exitingThreads, (const void*)boxedThread,
(const void*)specific);
GS_MUTEX_UNLOCK(_exitingThreadsLock);
}
/**
* Called after late cleanup has run. Will remove the current thread from
* the lookup table again. The boxedThread variable contains a boxed reference
* to the result of calling pthread_self().
*/
static inline void _didLateUnregisterCurrentThread(NSValue *boxedThread)
{
/* NB this locked section cannot be protected by an exception handler
* because the exception handler stores information in the current
* thread variables ... which causes recursion.
*/
GS_MUTEX_LOCK(_exitingThreadsLock);
if (nil != _exitingThreads)
{
NSMapRemove(_exitingThreads, (const void*)boxedThread);
}
GS_MUTEX_UNLOCK(_exitingThreadsLock);
}
/*
* Forward declaration of the thread unregistration function
*/
static void
unregisterActiveThread(NSThread *thread);
/**
* Pthread cleanup call.
*
* We should normally not get here ... because threads should exit properly
* and clean up, so that this function doesn't get called. However if a
* thread terminates for some reason without calling the exit method, we
* we add it to a special lookup table that is used by GSCurrentThread() to
* obtain the NSThread object.
* We need to be a bit careful about this regarding object allocation because
* we must not call into NSAutoreleasePool unless the NSThread object can still
* be found using GSCurrentThread()
*/
static void GS_WINAPI
exitedThread(void *thread)
{
if (thread != defaultThread)
{
NSValue *ref;
if (0 == thread)
{
/* On some systems this is called with a null thread pointer,
* so try to get the NSThread object for the current thread.
*/
thread = GS_THREAD_KEY_GET(thread_object_key);
if (0 == thread)
{
return; // no thread info
}
}
RETAIN((NSThread*)thread);
ref = NSValueCreateFromPthread(GS_THREAD_ID_SELF());
_willLateUnregisterThread(ref, (NSThread*)thread);
{
CREATE_AUTORELEASE_POOL(arp);
NS_DURING
{
unregisterActiveThread((NSThread*)thread);
}
NS_HANDLER
{
DESTROY(arp);
_didLateUnregisterCurrentThread(ref);
DESTROY(ref);
RELEASE((NSThread*)thread);
}
NS_ENDHANDLER
DESTROY(arp);
}
/* At this point threre shouldn't be any autoreleased objects lingering
* around anymore. So we may remove the thread from the lookup table.
*/
_didLateUnregisterCurrentThread(ref);
DESTROY(ref);
RELEASE((NSThread*)thread);
}
}
/**
* These functions needed because sending messages to classes is a seriously
* slow process with gcc and the gnu runtime.
*/
inline NSThread*
GSCurrentThread(void)
{
NSThread *thr;
if (NO == keyInitialized)
{
if (!GS_THREAD_KEY_INIT(thread_object_key, exitedThread))
{
[NSException raise: NSInternalInconsistencyException
format: @"Unable to create thread key!"];
}
keyInitialized = YES;
}
thr = GS_THREAD_KEY_GET(thread_object_key);
if (nil == thr)
{
NSValue *selfThread = NSValueCreateFromPthread(GS_THREAD_ID_SELF());
/* NB this locked section cannot be protected by an exception handler
* because the exception handler stores information in the current
* thread variables ... which causes recursion.
*/
if (nil != _exitingThreads)
{
GS_MUTEX_LOCK(_exitingThreadsLock);
thr = NSMapGet(_exitingThreads, (const void*)selfThread);
GS_MUTEX_UNLOCK(_exitingThreadsLock);
}
DESTROY(selfThread);
if (nil == thr)
{
GSRegisterCurrentThread();
thr = GS_THREAD_KEY_GET(thread_object_key);
if ((nil == defaultThread) && IS_MAIN_PTHREAD)
{
defaultThread = RETAIN(thr);
}
}
assert(nil != thr && "No main thread");
}
return thr;
}
NSMutableDictionary*
GSDictionaryForThread(NSThread *t)
{
if (nil == t)
{
t = GSCurrentThread();
}
return [t threadDictionary];
}
/**
* Fast access function for thread dictionary of current thread.
*/
NSMutableDictionary*
GSCurrentThreadDictionary(void)
{
return GSDictionaryForThread(nil);
}
/*
* Callback function to send notifications on becoming multi-threaded.
*/
static void
gnustep_base_thread_callback(void)
{
static gs_mutex_t threadLock = GS_MUTEX_INIT_STATIC;
/*
* Protect this function with locking ... to avoid any possibility
* of multiple threads registering with the system simultaneously,
* and so that all NSWillBecomeMultiThreadedNotifications are sent
* out before any second thread can interfere with anything.
*/
if (entered_multi_threaded_state == NO)
{
GS_MUTEX_LOCK(threadLock);
if (entered_multi_threaded_state == NO)
{
/*
* For apple compatibility ... and to make things easier for
* code called indirectly within a will-become-multi-threaded
* notification handler, we set the flag to say we are multi
* threaded BEFORE sending the notifications.
*/
entered_multi_threaded_state = YES;
/*
* Enter pool after setting flag, because -[NSAutoreleasePool
* allocWithZone:] calls GSCurrentThread(), which may end up
* calling this function, which would cause a deadlock.
*/
ENTER_POOL
NS_DURING
{
[GSPerformHolder class]; // Force initialization
/*
* Post a notification if this is the first new thread
* to be created.
* Won't work properly if threads are not all created
* by this class, but it's better than nothing.
*/
if (nc == nil)
{
nc = RETAIN([NSNotificationCenter defaultCenter]);
}
#if !defined(HAVE_INITIALIZE)
if (NO == [[NSUserDefaults standardUserDefaults]
boolForKey: @"GSSilenceInitializeWarning"])
{
NSLog(@"WARNING your program is becoming multi-threaded, but you are using an ObjectiveC runtime library which does not have a thread-safe implementation of the +initialize method. Please see README.initialize for more information.");
}
#endif
[nc postNotificationName: NSWillBecomeMultiThreadedNotification
object: nil
userInfo: nil];
}
NS_HANDLER
{
fprintf(stderr,
"ALERT ... exception while becoming multi-threaded ... system may not be\n"
"properly initialised.\n");
fflush(stderr);
}
NS_ENDHANDLER
LEAVE_POOL
}
GS_MUTEX_UNLOCK(threadLock);
}
}
@implementation NSThread (Activation)
- (void) _makeThreadCurrent
{
/* NB. We must set up the pointer to the new NSThread instance from
* pthread specific memory before we do anything which might need to
* check what the current thread is (like getting the ID)!
*/
GS_THREAD_KEY_SET(thread_object_key, self);
threadID = GSPrivateThreadID();
GS_MUTEX_LOCK(_activeLock);
/* The hash table is created lazily/late so that the NSThread
* +initialize method can be called without causing other
* classes to be initialized.
* NB this locked section cannot be protected by an exception handler
* because the exception handler stores information in the current
* thread variables ... which causes recursion.
*/
if (nil == _activeThreads)
{
_activeThreads = NSCreateHashTable(
NSNonRetainedObjectHashCallBacks, 100);
}
NSHashInsert(_activeThreads, (const void*)self);
GS_MUTEX_UNLOCK(_activeLock);
}
@end
@implementation NSThread
static void
setThreadForCurrentThread(NSThread *t)
{
[t _makeThreadCurrent];
gnustep_base_thread_callback();
}
static void
unregisterActiveThread(NSThread *thread)
{
if (thread->_active == YES)
{
/* Let observers know this thread is exiting.
*/
ENTER_POOL
if (nc == nil)
{
nc = RETAIN([NSNotificationCenter defaultCenter]);
}
[nc postNotificationName: NSThreadWillExitNotification
object: thread
userInfo: nil];
/* Set the thread to be finished *after* notification it will exit.
* This is the order OSX 10.15.4 does it (May 2020).
*/
thread->_active = NO;
thread->_finished = YES;
[(GSRunLoopThreadInfo*)thread->_runLoopInfo invalidate];
LEAVE_POOL
RELEASE(thread);
GS_THREAD_KEY_SET(thread_object_key, nil);
}
}
+ (NSArray*) callStackReturnAddresses
{
GSStackTrace *stack;
NSArray *addrs;
stack = [GSStackTrace new];
[stack trace];
addrs = RETAIN([stack addresses]);
RELEASE(stack);
return AUTORELEASE(addrs);
}
+ (BOOL) _createThreadForCurrentPthread
{
NSThread *t = GS_THREAD_KEY_GET(thread_object_key);
if (t == nil)
{
t = [self new];
t->_active = YES;
[t _makeThreadCurrent];
GS_CONSUMED(t);
if (defaultThread != nil && t != defaultThread)
{
gnustep_base_thread_callback();
}
return YES;
}
return NO;
}
+ (NSThread*) currentThread
{
return GSCurrentThread();
}
+ (void) detachNewThreadSelector: (SEL)aSelector
toTarget: (id)aTarget
withObject: (id)anArgument
{
NSThread *thread;
/*
* Create the new thread.
*/
thread = [[NSThread alloc] initWithTarget: aTarget
selector: aSelector
object: anArgument];
[thread start];
RELEASE(thread);
}
+ (void) exit
{
NSThread *t;
t = GSCurrentThread();
if (t->_active == YES)
{
unregisterActiveThread(t);
if (t == defaultThread || defaultThread == nil)
{
/* For the default thread, we exit the process.
*/
exit(0);
}
else
{
#if GS_USE_WIN32_THREADS_AND_LOCKS
_endthread();
#else
pthread_exit(NULL);
#endif
}
}
}
/*
* Class initialization
*/
+ (void) initialize
{
if (self == [NSThread class])
{
if (NO == keyInitialized)
{
if (!GS_THREAD_KEY_INIT(thread_object_key, exitedThread))
{
[NSException raise: NSInternalInconsistencyException
format: @"Unable to create thread key!"];
}
keyInitialized = YES;
}
/* Ensure that the default thread exists.
* It's safe to create a lock here (since [NSObject+initialize]
* creates locks, and locks don't depend on any other class),
* but we want to avoid initialising other classes while we are
* initialising NSThread.
*/
threadClass = self;
GSCurrentThread();
}
}
+ (BOOL) isMainThread
{
return (GSCurrentThread() == defaultThread ? YES : NO);
}
+ (BOOL) isMultiThreaded
{
return entered_multi_threaded_state;
}
+ (NSThread*) mainThread
{
return defaultThread;
}
/**
* Set the priority of the current thread. This is a value in the
* range 0.0 (lowest) to 1.0 (highest) which is mapped to the underlying
* system priorities.
*/
+ (BOOL) setThreadPriority: (double)pri
{
#if GS_USE_WIN32_THREADS_AND_LOCKS
// convert [0.0,1.0] priority to Windows defines
int winPri;
if (pri <= 0.0) {
winPri = THREAD_PRIORITY_IDLE;
} else if (pri <= 0.25) {
winPri = THREAD_PRIORITY_LOWEST;
} else if (pri < 0.5) {
winPri = THREAD_PRIORITY_BELOW_NORMAL;
} else if (pri >= 1.0) {
winPri = THREAD_PRIORITY_TIME_CRITICAL;
} else if (pri >= 0.75) {
winPri = THREAD_PRIORITY_HIGHEST;
} else if (pri > 0.5) {
winPri = THREAD_PRIORITY_ABOVE_NORMAL;
} else {
winPri = THREAD_PRIORITY_NORMAL;
}
if (!SetThreadPriority(GetCurrentThread(), winPri)) {
NSLog(@"Failed to set thread priority %d: %@", winPri, [NSError _last]);
return NO;
}
return YES;
#elif defined(__ANDROID__)
/* Android's pthread_setschedparam is currently broken, as it checks
* if the priority is in the range of the system's min and max
* priorities. The interval bounds are queried with `sched_get_priority_min`,
* and `sched_get_priority_max` which just return 0, regardless of the
* specified scheduling policy.
*
* The solution is to use `setpriority` to set the thread
* priority. This is possible because on Linux, it is not a per-process setting
* as specified by POSIX but a per-thread setting (See the `Bugs` section in `setpriority`).
*
* Android's internal implementation also relies on this behavior, so it
* is safe to use it here.
*/
// Clamp pri into the required range.
if (pri > 1) { pri = 1; }
if (pri < 0) { pri = 0; }
// Convert [0.0, 1.0] to [-20, 19] range where -20 is the highest
// and 19 the lowest priority.
int priority = (int)(-20 + (1-pri) * 39);
if (setpriority(PRIO_PROCESS, 0, priority) == -1)
{
NSLog(@"Failed to set thread priority %d: %s", priority, strerror(errno));
return NO;
}
return YES;
#elif defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING > 0)
int res;
int policy;
struct sched_param param;
// Clamp pri into the required range.
if (pri > 1) { pri = 1; }
if (pri < 0) { pri = 0; }
res = pthread_getschedparam(pthread_self(), &policy, ¶m);
if (res == 0)
{
int min = sched_get_priority_min(policy);
int max = sched_get_priority_max(policy);
if (min == max) {
// priority not settable => fail silently
return NO;
}
// Scale pri based on the range of the host system.
pri = min + pri * (max - min);
param.sched_priority = pri;
res = pthread_setschedparam(pthread_self(), policy, ¶m);
}
if (res != 0) {
NSLog(@"Failed to set thread priority %f: %d", pri, res);
return NO;
}
return YES;
#else
return NO;
#endif
}
+ (void) sleepForTimeInterval: (NSTimeInterval)ti
{
GSSleepUntilIntervalSinceReferenceDate(GSPrivateTimeNow() + ti);
}
/**
* Delaying a thread ... pause until the specified date.
*/
+ (void) sleepUntilDate: (NSDate*)date
{
GSSleepUntilIntervalSinceReferenceDate([date timeIntervalSinceReferenceDate]);
}
/**
* Return the priority of the current thread.
*/
+ (double) threadPriority
{
double pri = 0;
#if GS_USE_WIN32_THREADS_AND_LOCKS
// convert [0.0,1.0] priority to Windows defines
int winPri = GetThreadPriority(GetCurrentThread());
if (winPri == THREAD_PRIORITY_ERROR_RETURN) {
NSLog(@"Failed to get thread priority: %@", [NSError _last]);
return pri;
}
switch (winPri)
{
case THREAD_PRIORITY_IDLE:
pri = 0.0;
break;
case THREAD_PRIORITY_LOWEST:
pri = 0.2;
break;
case THREAD_PRIORITY_BELOW_NORMAL:
pri = 0.4;
break;
case THREAD_PRIORITY_TIME_CRITICAL:
pri = 1.0;
break;
case THREAD_PRIORITY_HIGHEST:
pri = 0.8;
break;
case THREAD_PRIORITY_ABOVE_NORMAL:
pri = 0.6;
break;
case THREAD_PRIORITY_NORMAL:
pri = 0.5;
break;
default:
NSLog(@"Unknown thread priority: %d", winPri);
break;
}
#elif defined(__ANDROID__)
/* See notes in setThreadPriority
*/
int priority = getpriority(PRIO_PROCESS, 0);
if (priority == -1)
{
NSLog(@"Failed to get thread priority: %s", strerror(errno));
return pri;
}
// Convert [-20, 19] to [0.0, 1.0] range
pri = 1 - (priority + 20) / 39.0;
#elif defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING > 0)
int res;
int policy;
struct sched_param param;
res = pthread_getschedparam(pthread_self(), &policy, ¶m);
if (res == 0)
{
int min = sched_get_priority_min(policy);
int max = sched_get_priority_max(policy);
if (min != max) /* avoid division by zero */
{
pri = param.sched_priority;
// Scale pri based on the range of the host system.
pri = (pri - min) / (max - min);
}
}
else
{
NSLog(@"Failed to get thread priority: %d", res);
}
#else
#warning Your pthread implementation does not support thread priorities
#endif
return pri;
}
/*
* Thread instance methods.
*/
- (void) cancel
{
_cancelled = YES;
}
- (void) dealloc
{
int retries = 0;
if (_active == YES)
{
[NSException raise: NSInternalInconsistencyException
format: @"Deallocating an active thread without [+exit]!"];
}
DESTROY(_runLoopInfo);
DESTROY(_thread_dictionary);
DESTROY(_target);
DESTROY(_arg);
DESTROY(_name);
DESTROY(stringCollatorCache);
if (_autorelease_vars.pool_cache != 0)
{
[NSAutoreleasePool _endThread: self];
}
while ((_thread_dictionary != nil || _runLoopInfo != nil) && retries++ < 10)
{
/* Try again.
*/
DESTROY(_runLoopInfo);
DESTROY(_thread_dictionary);
if (_autorelease_vars.pool_cache != 0)
{
[NSAutoreleasePool _endThread: self];
}
}
if (_runLoopInfo != nil)
{
NSLog(@"Oops - leak - run loop is %@", _runLoopInfo);
if (_autorelease_vars.pool_cache != 0)
{
[NSAutoreleasePool _endThread: self];
}
}
if (_thread_dictionary != nil)
{
NSLog(@"Oops - leak - thread dictionary is %@", _thread_dictionary);
if (_autorelease_vars.pool_cache != 0)
{
[NSAutoreleasePool _endThread: self];
}
}
DESTROY(_gcontext);
if (_activeThreads)
{
GS_MUTEX_LOCK(_activeLock);
NSHashRemove(_activeThreads, self);
GS_MUTEX_UNLOCK(_activeLock);
}
if (GS_EXISTS_INTERNAL)
{
pthread_spin_lock(&lockInfo.spin);
DESTROY(lockInfo.held);
lockInfo.wait = nil;
pthread_spin_unlock(&lockInfo.spin);
pthread_spin_destroy(&lockInfo.spin);
if (internal != nil)
{
GS_DESTROY_INTERNAL(NSThread);
}
}
[super dealloc];
}
- (NSString*) description
{
return [NSString stringWithFormat: @"%@{name = %@, num = %"PRIuPTR"}",
[super description], _name, threadID];
}
- (id) init
{
// SetThreadDescription() was added in Windows 10 1607 (Redstone 1)
#if defined(_WIN32) && (NTDDI_VERSION >= NTDDI_WIN10_RS1)
HANDLE current;
HRESULT hr;
PWSTR name;
NSString *threadName;
current = GetCurrentThread();
hr = GetThreadDescription(current, &name);
if (SUCCEEDED(hr))
{
threadName = [NSString stringWithCharacters: (const void *) name length: wcslen(name)];
ASSIGN(_name, threadName);
LocalFree(name);
}
#elif defined(PTHREAD_GETNAME)
NSString *threadName;
char name[16];
int status;
status = PTHREAD_GETNAME(name, 16);
if (status == 0)
{
threadName = [NSString stringWithCString: name encoding: NSUTF8StringEncoding];
ASSIGN(_name, threadName);
}
#endif
GS_CREATE_INTERNAL(NSThread);
pthread_spin_init(&lockInfo.spin, 0);
lockInfo.held = NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 10);
init_autorelease_thread_vars(&_autorelease_vars);
return self;
}
- (id) initWithTarget: (id)aTarget
selector: (SEL)aSelector
object: (id)anArgument
{
if (nil != (self = [self init]))
{
/* initialize our ivars. */
_selector = aSelector;
_target = RETAIN(aTarget);
_arg = RETAIN(anArgument);
}
return self;
}
- (BOOL) isCancelled
{
return _cancelled;
}
- (BOOL) isExecuting
{
return _active;
}
- (BOOL) isFinished
{
return _finished;
}
- (BOOL) isMainThread
{
return (self == defaultThread ? YES : NO);
}
- (void) main
{
if (_active == NO)
{
[NSException raise: NSInternalInconsistencyException
format: @"[%@-%@] called on inactive thread",
NSStringFromClass([self class]),
NSStringFromSelector(_cmd)];
}
if (targetIsBlock)
{
GSThreadBlock block = (GSThreadBlock)_target;
CALL_BLOCK_NO_ARGS(block);
}
else
{
[_target performSelector: _selector withObject: _arg];
}
}
- (NSString*) name
{
return _name;
}
- (void) _setName: (NSString *)aName
{
if ([aName isKindOfClass: [NSString class]])
{
// SetThreadDescription() was added in Windows 10 1607 (Redstone 1)
#if defined(_WIN32) && (NTDDI_VERSION >= NTDDI_WIN10_RS1)
HANDLE current;
const void *utf16String;
current = GetCurrentThread();
utf16String = [aName cStringUsingEncoding: NSUnicodeStringEncoding];
SetThreadDescription(current, utf16String);
#elif defined(PTHREAD_SETNAME)
int i;
char buf[200];
if (YES == [aName getCString: buf
maxLength: sizeof(buf)
encoding: NSUTF8StringEncoding])
{
i = strlen(buf);
}
else
{
/* Too much for buffer ... truncate on a character boundary.
*/
i = sizeof(buf) - 1;
if (buf[i] & 0x80)
{
while (i > 0 && (buf[i] & 0x80))
{
buf[i--] = '\0';
}
}
else
{
buf[i--] = '\0';
}
}
while (i > 0)
{
if (PTHREAD_SETNAME(buf) == ERANGE)
{
/* Name must be too long ... gnu/linux uses 15 characters
*/
if (i > 15)
{
NSWarnLog(@"Truncating thread name '%s' to 15 characters"
@" due to platform limitations", buf);
i = 15;
}
else
{
i--;
}
/* too long a name ... truncate on a character boundary.
*/
if (buf[i] & 0x80)
{
while (i > 0 && (buf[i] & 0x80))
{
buf[i--] = '\0';
}
}
else
{
buf[i--] = '\0';
}
}
else
{
break; // Success or some other error
}
}
#endif
}
}
- (void) setName: (NSString*)aName
{
ASSIGN(_name, aName);
if (YES == _active)
{
[self performSelector: @selector(_setName:)
onThread: self
withObject: aName
waitUntilDone: NO];
}
}
- (void) setStackSize: (NSUInteger)stackSize
{
_stackSize = stackSize;
}
- (NSUInteger) stackSize
{
return _stackSize;
}
/**
* Trampoline function called to launch the thread
*/
static
#if GS_USE_WIN32_THREADS_AND_LOCKS
void __cdecl
#else
void *
#endif
nsthreadLauncher(void *thread)
{
NSThread *t = (NSThread*)thread;
setThreadForCurrentThread(t);
ENTER_POOL
/*
* Let observers know a new thread is starting.
*/
if (nc == nil)
{
nc = RETAIN([NSNotificationCenter defaultCenter]);
}
[nc postNotificationName: NSThreadDidStartNotification
object: t
userInfo: nil];
[t _setName: [t name]];
LEAVE_POOL
[t main];
[NSThread exit];
// Not reached
#if !GS_USE_WIN32_THREADS_AND_LOCKS
return NULL;
#endif
}
- (void) start
{
#if !GS_USE_WIN32_THREADS_AND_LOCKS
pthread_attr_t attr;
#endif
if (_active == YES)
{
[NSException raise: NSInternalInconsistencyException
format: @"[%@-%@] called on active thread",
NSStringFromClass([self class]),
NSStringFromSelector(_cmd)];
}
if (_cancelled == YES)
{
[NSException raise: NSInternalInconsistencyException
format: @"[%@-%@] called on cancelled thread",
NSStringFromClass([self class]),
NSStringFromSelector(_cmd)];
}
if (_finished == YES)
{
[NSException raise: NSInternalInconsistencyException
format: @"[%@-%@] called on finished thread",
NSStringFromClass([self class]),
NSStringFromSelector(_cmd)];
}
/* Make sure the notification is posted BEFORE the new thread starts.
*/
gnustep_base_thread_callback();
/* The thread must persist until it finishes executing.
*/
RETAIN(self);
/* Mark the thread as active while it's running.
*/
_active = YES;
errno = 0;
#if GS_USE_WIN32_THREADS_AND_LOCKS
if (_beginthread(nsthreadLauncher, _stackSize, self) == -1)
{
DESTROY(self);
[NSException raise: NSInternalInconsistencyException
format: @"Unable to detach thread (last error %d)",
errno];
}
#else
pthread_attr_init(&attr);
/* Create this thread detached, because we never use the return state from
* threads.
*/
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
/* Set the stack size when the thread is created. Unlike the old setrlimit
* code, this actually works.
*/
if (_stackSize > 0)
{
pthread_attr_setstacksize(&attr, _stackSize);
}
if (pthread_create(&pthreadID, &attr, nsthreadLauncher, self))
{
DESTROY(self);
[NSException raise: NSInternalInconsistencyException
format: @"Unable to detach thread (last error %@)",
[NSError _last]];
}
#endif
}
/**
* Return the thread dictionary. This dictionary can be used to store
* arbitrary thread specific data.<br />
* NB. This cannot be autoreleased, since we cannot be sure that the
* autorelease pool for the thread will continue to exist for the entire
* life of the thread!
*/
- (NSMutableDictionary*) threadDictionary
{
if (_thread_dictionary == nil)
{
_thread_dictionary = [NSMutableDictionary new];
}
return _thread_dictionary;
}
- (id) _stringCollatorCache
{
return (id)stringCollatorCache;
}
- (void) _setStringCollatorCache: (id) cache
{
ASSIGN(stringCollatorCache, cache);
}
@end
@implementation NSThread (GSLockInfo)
static NSString *
lockInfoErr(NSString *str)
{
if (disableTraceLocks)
{
return nil;
}
return str;
}
- (NSString *) mutexDrop: (id)mutex
{
if (GS_EXISTS_INTERNAL)
{
GSLockInfo *li = &lockInfo;
int err;
if (YES == disableTraceLocks) return nil;
err = pthread_spin_lock(&li->spin);
if (EDEADLK == err) return lockInfoErr(@"thread spin lock deadlocked");
if (EINVAL == err) return lockInfoErr(@"thread spin lock invalid");
if (mutex == li->wait)
{
/* The mutex was being waited for ... simply remove it.
*/
li->wait = nil;
}
else if (NSHashGet(li->held, (void*)mutex) == (void*)mutex)
{
GSStackTrace *stck = [mutex stack];
/* The mutex was being held ... if the recursion count was zero
* we remove it (otherwise the count is decreased).
*/
if (stck->recursion-- == 0)
{
NSHashRemove(li->held, (void*)mutex);
// fprintf(stderr, "%lu: Drop %p (final) %lu\n", (unsigned long)_threadID, mutex, [li->held count]);
}
else
{
// fprintf(stderr, "%lu: Drop %p (%lu) %lu\n", (unsigned long)threadID, mutex, (unsigned long)stck->recursion, [li->held count]);
}
}
else
{
// fprintf(stderr, "%lu: Drop %p (bad) %lu\n", (unsigned long)threadID, mutex, [li->held count]);
pthread_spin_unlock(&li->spin);
return lockInfoErr(
@"attempt to unlock mutex not locked by this thread");
}
pthread_spin_unlock(&li->spin);
return nil;
}
return lockInfoErr(@"thread not active");
}
- (NSString *) mutexHold: (id)mutex
{
if (GS_EXISTS_INTERNAL)
{
GSLockInfo *li = &lockInfo;
int err;
if (YES == disableTraceLocks) return nil;
err = pthread_spin_lock(&li->spin);
if (EDEADLK == err) return lockInfoErr(@"thread spin lock deadlocked");
if (EINVAL == err) return lockInfoErr(@"thread spin lock invalid");
if (nil == mutex)
{
mutex = li->wait;
if (nil == mutex)
{
pthread_spin_unlock(&li->spin);
return lockInfoErr(@"attempt to hold nil mutex");
}
}
else if (nil != li->wait && mutex != li->wait)
{
pthread_spin_unlock(&li->spin);
return lockInfoErr(@"attempt to hold mutex without waiting for it");
}
if (NSHashGet(li->held, (void*)mutex) == NULL)
{
[[mutex stack] trace]; // Get current strack trace
NSHashInsert(li->held, (void*)mutex);
// fprintf(stderr, "%lu: Hold %p (initial) %lu\n", (unsigned long)threadID, mutex, [li->held count]);
}
else
{
GSStackTrace *stck = [mutex stack];
stck->recursion++;
// fprintf(stderr, "%lu: Hold %p (%lu) %lu\n", (unsigned long)threadID, mutex, (unsigned long)stck->recursion, [li->held count]);
}
li->wait = nil;
pthread_spin_unlock(&li->spin);
return nil;
}
return lockInfoErr(@"thread not active");
}
- (NSString *) mutexWait: (id)mutex
{
if (GS_EXISTS_INTERNAL)
{
NSMutableArray *dependencies;
id want;
BOOL done;
GSLockInfo *li = &lockInfo;
BOOL owned = NO;
int err;
if (YES == disableTraceLocks) return nil;
err = pthread_spin_lock(&li->spin);
if (EDEADLK == err) return lockInfoErr(@"thread spin lock deadlocked");
if (EINVAL == err) return lockInfoErr(@"thread spin lock invalid");
if (nil != li->wait)
{
NSString *msg = [NSString stringWithFormat:
@ "trying to lock %@ when already trying to lock %@",
mutex, li->wait];
pthread_spin_unlock(&li->spin);
return lockInfoErr(msg);
}
li->wait = mutex;
if (nil != NSHashGet(li->held, (const void*)mutex))
{
owned = YES;
}
pthread_spin_unlock(&li->spin);
// fprintf(stderr, "%lu: Wait %p\n", (unsigned long)_threadID, mutex);
if (YES == owned && [mutex isKindOfClass: [NSRecursiveLock class]])
{
return nil; // We can't deadlock on a recursive lock we own
}
/* While checking for deadlocks we don't want threads created/destroyed
* So we hold the lock to prevent thread activity changes.
* This also ensures that no more than one thread can be checking for
* deadlocks at a time (no interference between checks).
*/
GS_MUTEX_LOCK(_activeLock);
/* As we isolate dependencies (a thread holding the lock another thread
* is waiting for) we disable locking in each thread and record the
* thread in a hash table. Once we have determined all the dependencies
* we can re-enable locking in each of the threads.
*/
if (nil == _activeBlocked)
{
_activeBlocked = NSCreateHashTable(
NSNonRetainedObjectHashCallBacks, 100);
}
dependencies = nil;
want = mutex;
done = NO;
while (NO == done)
{
NSHashEnumerator enumerator;
NSThread *found = nil;
BOOL foundWasLocked = NO;
NSThread *th;
/* Look for a thread which is holding the mutex we are currently
* interested in. We are only interested in thread which are
* themselves waiting for a lock (if they aren't waiting then
* they can't be part of a deadlock dependency list).
*/
enumerator = NSEnumerateHashTable(_activeThreads);
while ((th = NSNextHashEnumeratorItem(&enumerator)) != nil)
{
GSLockInfo *info = &GSIVar(th, _lockInfo);
if (YES == th->_active && nil != info->wait)
{
BOOL wasLocked;
if (th == self
|| NULL != NSHashGet(_activeBlocked, (const void*)th))
{
/* Don't lock ... this is the current thread or is
* already in the set of blocked threads.
*/
wasLocked = YES;
}
else
{
pthread_spin_lock(&info->spin);
wasLocked = NO;
}
if (nil != info->wait
&& nil != (id)NSHashGet(info->held, (const void*)want))
{
/* This thread holds the lock we are interested in and
* is waiting for another lock.
* We therefore record the details in the dependency list
* and will go on to look for the thread this found one
* depends on.
*/
found = th;
foundWasLocked = wasLocked;
want = info->wait;
if (nil == dependencies)
{
dependencies = [NSMutableArray new];
}
[dependencies addObject: found]; // thread
[dependencies addObject: want]; // mutex
/* NB. breaking out here holds the spin lock so that
* the lock state of each dependency thread is
* preserved (if we don't have a deadlock, we get a
* consistent snapshot of the threads and their locks).
* We therefore have to unlock the threads when done.
*/
break;
}
/* This thread did not hold the lock we are interested in,
* so we can unlock it (if necessary) and check another.
*/
if (NO == wasLocked)
{
pthread_spin_unlock(&info->spin);
}
}
}
NSEndHashTableEnumeration(&enumerator);
if (nil == found)
{
/* There is no thread blocked on the mutex we are checking,
* so we can't have a deadlock.
*/
DESTROY(dependencies);
done = YES;
}
else if (foundWasLocked)
{
/* The found thread is the current one or in the blocked set
* so we have a deadlock.
*/
done = YES;
}
else
{
/* Record the found (and locked) thread and continue
* to find the next dependency.
*/
NSHashInsert(_activeBlocked, (const void*)found);
}
}
/* Ensure any locked threads are unlocked again.
*/
if (NSCountHashTable(_activeBlocked) > 0)
{
NSHashEnumerator enumerator;
NSThread *th;
enumerator = NSEnumerateHashTable(_activeThreads);
while ((th = NSNextHashEnumeratorItem(&enumerator)) != nil)
{
GSLockInfo *info = &GSIVar(th, _lockInfo);
pthread_spin_unlock(&info->spin);
}
NSEndHashTableEnumeration(&enumerator);
NSResetHashTable(_activeBlocked);
}
/* Finished check ... re-enable thread activity changes.
*/
GS_MUTEX_UNLOCK(_activeLock);
if (nil != dependencies)
{
GSStackTrace *stack;
NSUInteger count;
NSUInteger index = 0;
NSMutableString *m;
disableTraceLocks = YES;
m = [NSMutableString stringWithCapacity: 1000];
stack = [GSStackTrace new];
[stack trace];
[m appendFormat: @"Deadlock on %@ at\n %@\n",
mutex, [stack symbols]];
RELEASE(stack);
count = [dependencies count];
while (index < count)
{
NSArray *symbols;
NSThread *thread;
NSUInteger frameCount;
thread = [dependencies objectAtIndex: index++];
mutex = [dependencies objectAtIndex: index++];
symbols = [[mutex stack] symbols];
frameCount = [symbols count];
if (frameCount > 0)
{
NSUInteger i;
[m appendFormat: @" depends on %@\n blocked by %@\n at\n",
mutex, thread];
for (i = 0; i < frameCount; i++)
{
[m appendFormat: @" %@\n", [symbols objectAtIndex: i]];
}
}
else
{
[m appendFormat: @" depends on %@\n blocked by %@\n",
mutex, thread];
}
}
DESTROY(dependencies);
/* NB. Return m directly because we have turned off tracing to
* avoid recursion, and don't want lockInfoErr() to stop the
* error being ruturned.
*/
return m;
}
return nil;
}
return lockInfoErr(@"thread not active");
}
@end
@implementation GSRunLoopThreadInfo
- (void) addPerformer: (id)performer
{
BOOL signalled = NO;
[lock lock];
#if defined(_WIN32)
if (INVALID_HANDLE_VALUE != event)
{
if (SetEvent(event) == 0)
{
NSLog(@"Set event failed - %@", [NSError _last]);
}
else
{
signalled = YES;
}
}
#else
{
NSTimeInterval start = 0.0;
/* The write could concievably fail if the pipe is full.
* In that case we need to release the lock temporarily to allow the other
* thread to consume data from the pipe. It's possible that the thread
* and its runloop might stop during that ... so we need to check that
* outputFd is still valid.
*/
while (outputFd >= 0
&& NO == (signalled = (write(outputFd, "0", 1) == 1) ? YES : NO))
{
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
if (0.0 == start)
{
start = now;
}
else if (now - start >= 1.0)
{
NSLog(@"Unable to signal %@ within a second; blocked?", self);
break;
}
[lock unlock];
[lock lock];
}
}
#endif
if (YES == signalled)
{
[performers addObject: performer];
}
[lock unlock];
if (NO == signalled)
{
/* We failed to add the performer ... so we must invalidate it in
* case there is code waiting for it to complete.
*/
[performer invalidate];
}
}
- (void) dealloc
{
[self invalidate];
DESTROY(lock);
DESTROY(loop);
[super dealloc];
}
- (id) init
{
#ifdef _WIN32
if ((event = CreateEvent(NULL, TRUE, FALSE, NULL)) == INVALID_HANDLE_VALUE)
{
DESTROY(self);
[NSException raise: NSInternalInconsistencyException
format: @"Failed to create event to handle perform in thread"];
}
#else
int fd[2];
if (pipe(fd) == 0)
{
int e;
inputFd = fd[0];
outputFd = fd[1];
if ((e = fcntl(inputFd, F_GETFL, 0)) >= 0)
{
e |= NBLK_OPT;
if (fcntl(inputFd, F_SETFL, e) < 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"Failed to set non block flag for perform in thread"];
}
}
else
{
[NSException raise: NSInternalInconsistencyException
format: @"Failed to get non block flag for perform in thread"];
}
if ((e = fcntl(outputFd, F_GETFL, 0)) >= 0)
{
e |= NBLK_OPT;
if (fcntl(outputFd, F_SETFL, e) < 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"Failed to set non block flag for perform in thread"];
}
}
else
{
[NSException raise: NSInternalInconsistencyException
format: @"Failed to get non block flag for perform in thread"];
}
}
else
{
DESTROY(self);
[NSException raise: NSInternalInconsistencyException
format: @"Failed to create pipe to handle perform in thread"];
}
#endif
lock = [NSLock new];
performers = [NSMutableArray new];
return self;
}
- (void) invalidate
{
NSArray *p;
[lock lock];
p = AUTORELEASE(performers);
performers = nil;
#ifdef _WIN32
if (event != INVALID_HANDLE_VALUE)
{
CloseHandle(event);
event = INVALID_HANDLE_VALUE;
}
#else
if (inputFd >= 0)
{
close(inputFd);
inputFd = -1;
}
if (outputFd >= 0)
{
close(outputFd);
outputFd = -1;
}
#endif
[lock unlock];
[p makeObjectsPerformSelector: @selector(invalidate)];
}
- (void) fire
{
NSArray *toDo;
unsigned int i;
unsigned int c;
[lock lock];
#if defined(_WIN32)
if (event != INVALID_HANDLE_VALUE)
{
if (ResetEvent(event) == 0)
{
NSLog(@"Reset event failed - %@", [NSError _last]);
}
}
#else
if (inputFd >= 0)
{
char buf[BUFSIZ];
/* We don't care how much we read. If there have been multiple
* performers queued then there will be multiple bytes available,
* but we always handle all available performers, so we can also
* read all available bytes.
* The descriptor is non-blocking ... so it's safe to ask for more
* bytes than are available.
*/
while (read(inputFd, buf, sizeof(buf)) > 0)
;
}
#endif
c = [performers count];
if (0 == c)
{
/* We deal with all available performers each time we fire, so
* it's likely that we will fire when we have no performers left.
* In that case we can skip the copying and emptying of the array.
*/
[lock unlock];
return;
}
toDo = [NSArray arrayWithArray: performers];
[performers removeAllObjects];
[lock unlock];
for (i = 0; i < c; i++)
{
GSPerformHolder *h = [toDo objectAtIndex: i];
[loop performSelector: @selector(fire)
target: h
argument: nil
order: 0
modes: [h modes]];
}
}
@end
GSRunLoopThreadInfo *
GSRunLoopInfoForThread(NSThread *aThread)
{
GSRunLoopThreadInfo *info;
if (aThread == nil)
{
aThread = GSCurrentThread();
}
if (aThread->_runLoopInfo == nil)
{
static gs_mutex_t infoLock = GS_MUTEX_INIT_STATIC;
GS_MUTEX_LOCK(infoLock);
if (aThread->_runLoopInfo == nil)
{
aThread->_runLoopInfo = [GSRunLoopThreadInfo new];
}
GS_MUTEX_UNLOCK(infoLock);
}
info = aThread->_runLoopInfo;
return info;
}
@implementation GSPerformHolder
+ (GSPerformHolder*) newForReceiver: (id)r
argument: (id)a
selector: (SEL)s
modes: (NSArray*)m
lock: (NSConditionLock*)l
{
GSPerformHolder *h;
h = (GSPerformHolder*)NSAllocateObject(self, 0, NSDefaultMallocZone());
h->receiver = RETAIN(r);
h->argument = RETAIN(a);
h->selector = s;
h->modes = RETAIN(m);
h->lock = l;
return h;
}
- (void) dealloc
{
DESTROY(exception);
DESTROY(receiver);
DESTROY(argument);
DESTROY(modes);
if (lock != nil)
{
[lock lock];
[lock unlockWithCondition: 1];
lock = nil;
}
NSDeallocateObject(self);
GSNOSUPERDEALLOC;
}
- (NSString*) description
{
return [[super description] stringByAppendingFormat: @" [%s %s]",
class_getName(object_getClass(receiver)), sel_getName(selector)];
}
- (void) fire
{
GSRunLoopThreadInfo *threadInfo;
if (receiver == nil)
{
return; // Already fired!
}
threadInfo = GSRunLoopInfoForThread(GSCurrentThread());
[threadInfo->loop cancelPerformSelectorsWithTarget: self];
NS_DURING
{
[receiver performSelector: selector withObject: argument];
}
NS_HANDLER
{
ASSIGN(exception, localException);
if (nil == lock)
{
NSLog(@"*** NSRunLoop ignoring exception '%@' (reason '%@') "
@"raised during perform in other thread... with receiver %p (%s) "
@"and selector '%s'",
[localException name], [localException reason], receiver,
class_getName(object_getClass(receiver)),
sel_getName(selector));
}
}
NS_ENDHANDLER
DESTROY(receiver);
DESTROY(argument);
DESTROY(modes);
if (lock != nil)
{
NSConditionLock *l = lock;
[lock lock];
lock = nil;
[l unlockWithCondition: 1];
}
}
- (void) invalidate
{
if (invalidated == NO)
{
invalidated = YES;
DESTROY(receiver);
if (lock != nil)
{
NSConditionLock *l = lock;
[lock lock];
lock = nil;
[l unlockWithCondition: 1];
}
}
}
- (BOOL) isInvalidated
{
return invalidated;
}
- (NSArray*) modes
{
return modes;
}
@end
@implementation NSObject (NSThreadPerformAdditions)
- (void) performSelectorOnMainThread: (SEL)aSelector
withObject: (id)anObject
waitUntilDone: (BOOL)aFlag
modes: (NSArray*)anArray
{
/* It's possible that this method could be called before the NSThread
* class is initialised, so we check and make sure it's initiailised
* if necessary.
*/
if (defaultThread == nil)
{
[NSThread currentThread];
}
[self performSelector: aSelector
onThread: defaultThread
withObject: anObject
waitUntilDone: aFlag
modes: anArray];
}
- (void) performSelectorOnMainThread: (SEL)aSelector
withObject: (id)anObject
waitUntilDone: (BOOL)aFlag
{
[self performSelectorOnMainThread: aSelector
withObject: anObject
waitUntilDone: aFlag
modes: commonModes()];
}
- (void) performSelector: (SEL)aSelector
onThread: (NSThread*)aThread
withObject: (id)anObject
waitUntilDone: (BOOL)aFlag
modes: (NSArray*)anArray
{
GSRunLoopThreadInfo *info;
NSThread *t;
if ([anArray count] == 0)
{
return;
}
t = GSCurrentThread();
if (aThread == nil)
{
aThread = t;
}
info = GSRunLoopInfoForThread(aThread);
if (t == aThread)
{
/* Perform in current thread.
*/
if (aFlag == YES || info->loop == nil)
{
/* Wait until done or no run loop.
*/
[self performSelector: aSelector withObject: anObject];
}
else
{
/* Don't wait ... schedule operation in run loop.
*/
[info->loop performSelector: aSelector
target: self
argument: anObject
order: 0
modes: anArray];
}
}
else
{
GSPerformHolder *h;
NSConditionLock *l = nil;
if ([aThread isFinished] == YES)
{
[NSException raise: NSInternalInconsistencyException
format: @"perform [%@-%@] attempted on finished thread (%@)",
NSStringFromClass([self class]),
NSStringFromSelector(aSelector),
aThread];
}
if (aFlag == YES)
{
l = [[NSConditionLock alloc] init];
}
h = [GSPerformHolder newForReceiver: self
argument: anObject
selector: aSelector
modes: anArray
lock: l];
[info addPerformer: h];
if (l != nil)
{
[l lockWhenCondition: 1];
[l unlock];
RELEASE(l);
if ([h isInvalidated] == NO)
{
/* If we have an exception passed back from the remote thread,
* re-raise it.
*/
if (nil != h->exception)
{
NSException *e = AUTORELEASE(RETAIN(h->exception));
RELEASE(h);
[e raise];
}
}
}
RELEASE(h);
}
}
- (void) performSelector: (SEL)aSelector
onThread: (NSThread*)aThread
withObject: (id)anObject
waitUntilDone: (BOOL)aFlag
{
[self performSelector: aSelector
onThread: aThread
withObject: anObject
waitUntilDone: aFlag
modes: commonModes()];
}
- (void) performSelectorInBackground: (SEL)aSelector
withObject: (id)anObject
{
[NSThread detachNewThreadSelector: aSelector
toTarget: self
withObject: anObject];
}
@end
@implementation NSThread (BlockAdditions)
+ (void) detachNewThreadWithBlock: (GSThreadBlock)block
{
NSThread *thread;
thread = [[NSThread alloc] initWithBlock: block];
[thread start];
RELEASE(thread);
}
- (instancetype) initWithBlock: (GSThreadBlock)block
{
if (nil != (self = [self init]))
{
targetIsBlock = YES;
/* Copy block to heap */
_target = _Block_copy(block);
}
return self;
}
@end
/**
* <p>
* This function is provided to let threads started by some other
* software library register themselves to be used with the
* GNUstep system. All such threads should call this function
* before attempting to use any GNUstep objects.
* </p>
* <p>
* Returns <code>YES</code> if the thread can be registered,
* <code>NO</code> if it is already registered.
* </p>
* <p>
* Sends out a <code>NSWillBecomeMultiThreadedNotification</code>
* if the process was not already multithreaded.
* </p>
*/
BOOL
GSRegisterCurrentThread(void)
{
return [NSThread _createThreadForCurrentPthread];
}
/**
* <p>
* This function is provided to let threads started by some other
* software library unregister themselves from the GNUstep threading
* system.
* </p>
* <p>
* Calling this function causes a
* <code>NSThreadWillExitNotification</code>
* to be sent out, and destroys the GNUstep NSThread object
* associated with the thread (like [NSThread+exit]) but does
* not exit the underlying thread.
* </p>
*/
void
GSUnregisterCurrentThread(void)
{
unregisterActiveThread(GSCurrentThread());
}
|