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
|
/*
* This file implements a family of commands for sharing variables
* between threads.
*
* Initial code is taken from nsd/tclvar.c found in AOLserver 3.+
* distribution and modified to support Tcl 8.0+ command object interface
* and internal storage in private shared Tcl objects.
*
* Copyright (c) 2002 by Zoran Vasiljevic.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
* ----------------------------------------------------------------------------
*/
#include "tclThreadInt.h"
#include "threadSvCmd.h"
#include "threadSvListCmd.h" /* Shared variants of list commands */
#include "threadSvKeylistCmd.h" /* Shared variants of list commands */
#include "psGdbm.h" /* The gdbm persistent store implementation */
#include "psLmdb.h" /* The lmdb persistent store implementation */
#define SV_FINALIZE
/*
* Number of buckets to spread shared arrays into. Each bucket is
* associated with one mutex so locking a bucket locks all arrays
* in that bucket as well. The number of buckets should be a prime.
*/
#define NUMBUCKETS 31
/*
* Number of object containers
* to allocate in one shot.
*/
#define OBJS_TO_ALLOC_EACH_TIME 100
/*
* Reference to Tcl object types used in object-copy code.
* Those are referenced read-only, thus no mutex protection.
*/
static const Tcl_ObjType* booleanObjTypePtr = 0;
static const Tcl_ObjType* byteArrayObjTypePtr = 0;
static const Tcl_ObjType* doubleObjTypePtr = 0;
static const Tcl_ObjType* intObjTypePtr = 0;
static const Tcl_ObjType* wideIntObjTypePtr = 0;
static const Tcl_ObjType* stringObjTypePtr = 0;
/*
* In order to be fully stub enabled, a small
* hack is needed to query the tclEmptyStringRep
* global symbol defined by Tcl. See SvInit.
*/
static char *Sv_tclEmptyStringRep = NULL;
/*
* Global variables used within this file.
*/
#ifdef SV_FINALIZE
static size_t nofThreads; /* Number of initialized threads */
static Tcl_Mutex nofThreadsMutex; /* Protects the nofThreads variable */
#endif /* SV_FINALIZE */
static Bucket* buckets; /* Array of buckets. */
static Tcl_Mutex bucketsMutex; /* Protects the array of buckets */
static SvCmdInfo* svCmdInfo; /* Linked list of registered commands */
static RegType* regType; /* Linked list of registered obj types */
static PsStore* psStore; /* Linked list of registered pers. stores */
static Tcl_Mutex svMutex; /* Protects inserts into above lists */
static Tcl_Mutex initMutex; /* Serializes initialization issues */
/*
* The standard commands found in NaviServer/AOLserver nsv_* interface.
* For sharp-eye readers: the implementation of the "lappend" command
* is moved to new list-command package, since it really belongs there.
*/
static Tcl_ObjCmdProc2 SvObjObjCmd;
static Tcl_ObjCmdProc2 SvAppendObjCmd;
static Tcl_ObjCmdProc2 SvIncrObjCmd;
static Tcl_ObjCmdProc2 SvSetObjCmd;
static Tcl_ObjCmdProc2 SvExistsObjCmd;
static Tcl_ObjCmdProc2 SvGetObjCmd;
static Tcl_ObjCmdProc2 SvArrayObjCmd;
static Tcl_ObjCmdProc2 SvUnsetObjCmd;
static Tcl_ObjCmdProc2 SvNamesObjCmd;
static Tcl_ObjCmdProc2 SvHandlersObjCmd;
/*
* New commands added to
* standard set of nsv_*
*/
static Tcl_ObjCmdProc2 SvPopObjCmd;
static Tcl_ObjCmdProc2 SvMoveObjCmd;
static Tcl_ObjCmdProc2 SvLockObjCmd;
/*
* Forward declarations for functions to
* manage buckets, arrays and shared objects.
*/
static Container* CreateContainer(Array*, Tcl_HashEntry*, Tcl_Obj*);
static Container* AcquireContainer(Array*, const char*, int);
static Array* CreateArray(Bucket*, const char*);
static Array* LockArray(Tcl_Interp*, const char*, int);
static int ReleaseContainer(Tcl_Interp*, Container*, int);
static int DeleteContainer(Container*);
static int FlushArray(Array*);
static int DeleteArray(Tcl_Interp *, Array*);
static void SvAllocateContainers(Bucket*);
static void SvRegisterStdCommands(void);
#ifdef SV_FINALIZE
static void SvFinalizeContainers(Bucket*);
static void SvFinalize(void *);
#endif /* SV_FINALIZE */
static PsStore* GetPsStore(const char *handle);
static int SvObjDispatchObjCmd(void *arg,
Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]);
/*
*-----------------------------------------------------------------------------
*
* Sv_RegisterCommand --
*
* Utility to register commands to be loaded at module start.
*
* Results:
* None.
*
* Side effects;
* New command will be added to a linked list of registered commands.
*
*-----------------------------------------------------------------------------
*/
void
Sv_RegisterCommand(
const char *cmdName, /* Name of command to register */
Tcl_ObjCmdProc2 *objProc, /* Object-based command procedure */
Tcl_CmdDeleteProc *delProc, /* Command delete procedure */
int aolSpecial)
{
size_t len = strlen(cmdName) + strlen(TSV_CMD_PREFIX) + 1;
size_t len2 = strlen(cmdName) + strlen(TSV_CMD2_PREFIX) + 1;
SvCmdInfo *newCmd = (SvCmdInfo *)Tcl_Alloc(sizeof(SvCmdInfo) + len + len2);
/*
* Setup new command structure
*/
newCmd->cmdName = (char*)((char*)newCmd + sizeof(SvCmdInfo));
newCmd->cmdName2 = newCmd->cmdName + len;
newCmd->aolSpecial = aolSpecial;
newCmd->objProcPtr = objProc;
newCmd->delProcPtr = delProc;
/*
* Rewrite command name. This is needed so we can
* easily turn-on the compatiblity with NaviServer/AOLserver
* command names.
*/
strcpy(newCmd->cmdName, TSV_CMD_PREFIX);
strcat(newCmd->cmdName, cmdName);
newCmd->name = newCmd->cmdName + strlen(TSV_CMD_PREFIX);
strcpy(newCmd->cmdName2, TSV_CMD2_PREFIX);
strcat(newCmd->cmdName2, cmdName);
/*
* Plug-in in shared list of commands.
*/
Tcl_MutexLock(&svMutex);
if (svCmdInfo == NULL) {
svCmdInfo = newCmd;
newCmd->nextPtr = NULL;
} else {
newCmd->nextPtr = svCmdInfo;
svCmdInfo = newCmd;
}
Tcl_MutexUnlock(&svMutex);
return;
}
/*
*-----------------------------------------------------------------------------
*
* Sv_RegisterObjType --
*
* Registers custom object duplicator function for a specific
* object type. Registered function will be called by the
* private object creation routine every time an object is
* plugged out or in the shared array. This way we assure that
* Tcl objects do not get shared per-reference between threads.
*
* Results:
* None.
*
* Side effects;
* Memory gets allocated.
*
*-----------------------------------------------------------------------------
*/
void
Sv_RegisterObjType(
const Tcl_ObjType *typePtr, /* Type of object to register */
Tcl_DupInternalRepProc *dupProc) /* Custom object duplicator */
{
RegType *newType = (RegType *)Tcl_Alloc(sizeof(RegType));
/*
* Setup new type structure
*/
newType->typePtr = typePtr;
newType->dupIntRepProc = dupProc;
/*
* Plug-in in shared list
*/
Tcl_MutexLock(&svMutex);
newType->nextPtr = regType;
regType = newType;
Tcl_MutexUnlock(&svMutex);
}
/*
*-----------------------------------------------------------------------------
*
* Sv_RegisterPsStore --
*
* Registers a handler to the persistent storage.
*
* Results:
* None.
*
* Side effects;
* Memory gets allocated.
*
*-----------------------------------------------------------------------------
*/
void
Sv_RegisterPsStore(const PsStore *psStorePtr)
{
PsStore *psPtr = (PsStore *)Tcl_Alloc(sizeof(PsStore));
*psPtr = *psStorePtr;
/*
* Plug-in in shared list
*/
Tcl_MutexLock(&svMutex);
if (psStore == NULL) {
psStore = psPtr;
psStore->nextPtr = NULL;
} else {
psPtr->nextPtr = psStore;
psStore = psPtr;
}
Tcl_MutexUnlock(&svMutex);
}
/*
*-----------------------------------------------------------------------------
*
* Sv_GetContainer --
*
* This is the workhorse of the module. It returns the container
* with the shared Tcl object. It also locks the container, so
* when finished with operation on the Tcl object, one has to
* unlock the container by calling the Sv_PutContainer().
* If instructed, this command might also create new container
* with empty Tcl object.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* New container might be created.
*
*-----------------------------------------------------------------------------
*/
int
Sv_GetContainer(
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments */
Tcl_Obj *const objv[], /* Argument objects. */
Container **retObj, /* OUT: shared object container */
Tcl_Size *offset, /* Shift in argument list */
int flags) /* Options for locking shared array */
{
const char *array, *key;
if (*retObj == NULL) {
Array *arrayPtr = NULL;
/*
* Parse mandatory arguments: <cmd> array key
*/
if (objc < 3) {
Tcl_WrongNumArgs(interp, 1, objv, "array key ?args?");
return TCL_ERROR;
}
array = Tcl_GetString(objv[1]);
key = Tcl_GetString(objv[2]);
*offset = 3; /* Consumed three arguments: cmd, array, key */
/*
* Lock the shared array and locate the shared object
*/
arrayPtr = LockArray(interp, array, flags);
if (arrayPtr == NULL) {
return TCL_BREAK;
}
*retObj = AcquireContainer(arrayPtr, Tcl_GetString(objv[2]), flags);
if (*retObj == NULL) {
UnlockArray(arrayPtr);
Tcl_AppendResult(interp, "no key ", array, "(", key, ")", (void *)NULL);
return TCL_BREAK;
}
} else {
Tcl_HashTable *handles = &((*retObj)->bucketPtr->handles);
LOCK_CONTAINER(*retObj);
if (Tcl_FindHashEntry(handles, (char*)(*retObj)) == NULL) {
UNLOCK_CONTAINER(*retObj);
Tcl_SetObjResult(interp, Tcl_NewStringObj("key has been deleted", TCL_INDEX_NONE));
return TCL_BREAK;
}
*offset = 2; /* Consumed two arguments: object, cmd */
}
return TCL_OK;
}
/*
*-----------------------------------------------------------------------------
*
* Sv_PutContainer --
*
* Releases the container obtained by the Sv_GetContainer.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* For bound arrays, update the underlying persistent storage.
*
*-----------------------------------------------------------------------------
*/
int
Sv_PutContainer(
Tcl_Interp *interp, /* For error reporting; might be NULL */
Container *svObj, /* Shared object container */
int mode) /* One of SV_XXX modes */
{
int ret;
ret = ReleaseContainer(interp, svObj, mode);
UnlockArray(svObj->arrayPtr);
return ret;
}
/*
*-----------------------------------------------------------------------------
*
* GetPsStore --
*
* Performs a lookup in the list of registered persistent storage
* handlers. If the match is found, duplicates the persistent
* storage record and passes the copy to the caller.
*
* Results:
* Pointer to the newly allocated persistent storage handler. Caller
* must free this block when done with it. If none found, returns NULL,
*
* Side effects;
* Memory gets allocated. Caller should free the return value of this
* function using Tcl_Free().
*
*-----------------------------------------------------------------------------
*/
static PsStore*
GetPsStore(const char *handle)
{
int i;
const char *type = handle;
char *addr, *delimiter = (char *)strchr(handle, ':');
PsStore *tmpPtr, *psPtr = NULL;
/*
* Expect the handle in the following format: <type>:<address>
* where "type" must match one of the registered presistent store
* types (gdbm, tcl, whatever) and <address> is what is passed to
* the open procedure of the registered store.
*
* Example: gdbm:/path/to/gdbm/file
*/
/*
* Try to see wether some array is already bound to the
* same persistent storage address.
*/
for (i = 0; i < NUMBUCKETS; i++) {
Tcl_HashSearch search;
Tcl_HashEntry *hPtr;
Bucket *bucketPtr = &buckets[i];
LOCK_BUCKET(bucketPtr);
hPtr = Tcl_FirstHashEntry(&bucketPtr->arrays, &search);
while (hPtr) {
Array *arrayPtr = (Array*)Tcl_GetHashValue(hPtr);
if (arrayPtr->bindAddr && arrayPtr->psPtr) {
if (strcmp(arrayPtr->bindAddr, handle) == 0) {
UNLOCK_BUCKET(bucketPtr);
return NULL; /* Array already bound */
}
}
hPtr = Tcl_NextHashEntry(&search);
}
UNLOCK_BUCKET(bucketPtr);
}
/*
* Split the address and storage handler
*/
if (delimiter == NULL) {
addr = NULL;
} else {
*delimiter = 0;
addr = delimiter + 1;
}
/*
* No array was bound to the same persistent storage.
* Lookup the persistent storage to bind to.
*/
Tcl_MutexLock(&svMutex);
for (tmpPtr = psStore; tmpPtr; tmpPtr = tmpPtr->nextPtr) {
if (strcmp(tmpPtr->type, type) == 0) {
tmpPtr->psHandle = tmpPtr->psOpen(addr);
if (tmpPtr->psHandle) {
psPtr = (PsStore *)Tcl_Alloc(sizeof(PsStore));
*psPtr = *tmpPtr;
psPtr->nextPtr = NULL;
}
break;
}
}
Tcl_MutexUnlock(&svMutex);
if (delimiter) {
*delimiter = ':';
}
return psPtr;
}
/*
*-----------------------------------------------------------------------------
*
* AcquireContainer --
*
* Finds a variable within an array and returns it's container.
*
* Results:
* Pointer to variable object.
*
* Side effects;
* New variable may be created. For bound arrays, try to locate
* the key in the persistent storage as well.
*
*-----------------------------------------------------------------------------
*/
static Container *
AcquireContainer(
Array *arrayPtr,
const char *key,
int flags)
{
int isNew;
Tcl_Obj *tclObj = NULL;
Tcl_HashEntry *hPtr = Tcl_FindHashEntry(&arrayPtr->vars, key);
if (hPtr == NULL) {
PsStore *psPtr = arrayPtr->psPtr;
if (psPtr) {
char *val = NULL;
Tcl_Size len = 0;
if (psPtr->psGet(psPtr->psHandle, key, &val, &len) == 0) {
tclObj = Tcl_NewStringObj(val, len);
psPtr->psFree(psPtr->psHandle, val);
}
}
if (!(flags & FLAGS_CREATEVAR) && tclObj == NULL) {
return NULL;
}
if (tclObj == NULL) {
tclObj = Tcl_NewObj();
}
hPtr = Tcl_CreateHashEntry(&arrayPtr->vars, key, &isNew);
Tcl_SetHashValue(hPtr, CreateContainer(arrayPtr, hPtr, tclObj));
}
return (Container*)Tcl_GetHashValue(hPtr);
}
/*
*-----------------------------------------------------------------------------
*
* ReleaseContainer --
*
* Does some post-processing on the used container. This is mostly
* needed when the container has been modified and needs to be
* saved in the bound persistent storage.
*
* Results:
* A standard Tcl result
*
* Side effects:
* Persistent storage, if bound, might be modified.
*
*-----------------------------------------------------------------------------
*/
static int
ReleaseContainer(
Tcl_Interp *interp,
Container *svObj,
int mode)
{
const PsStore *psPtr = svObj->arrayPtr->psPtr;
Tcl_Size len;
char *key, *val;
switch (mode) {
case SV_UNCHANGED: return TCL_OK;
case SV_ERROR: return TCL_ERROR;
case SV_CHANGED:
if (psPtr) {
key = (char *)Tcl_GetHashKey(&svObj->arrayPtr->vars, svObj->entryPtr);
val = Tcl_GetStringFromObj(svObj->tclObj, &len);
if (psPtr->psPut(psPtr->psHandle, key, val, len) == -1) {
const char *err = psPtr->psError(psPtr->psHandle);
Tcl_SetObjResult(interp, Tcl_NewStringObj(err, TCL_INDEX_NONE));
return TCL_ERROR;
}
}
return TCL_OK;
}
return TCL_ERROR; /* Should never be reached */
}
/*
*-----------------------------------------------------------------------------
*
* CreateContainer --
*
* Creates new shared container holding Tcl object to be stored
* in the shared array
*
* Results:
* The container pointer.
*
* Side effects:
* Memory gets allocated.
*
*-----------------------------------------------------------------------------
*/
static Container *
CreateContainer(
Array *arrayPtr,
Tcl_HashEntry *entryPtr,
Tcl_Obj *tclObj)
{
Container *svObj;
if (arrayPtr->bucketPtr->freeCt == NULL) {
SvAllocateContainers(arrayPtr->bucketPtr);
}
svObj = arrayPtr->bucketPtr->freeCt;
arrayPtr->bucketPtr->freeCt = svObj->nextPtr;
svObj->arrayPtr = arrayPtr;
svObj->bucketPtr = arrayPtr->bucketPtr;
svObj->tclObj = tclObj;
svObj->entryPtr = entryPtr;
svObj->handlePtr = NULL;
if (svObj->tclObj) {
Tcl_IncrRefCount(svObj->tclObj);
}
return svObj;
}
/*
*-----------------------------------------------------------------------------
*
* DeleteContainer --
*
* Destroys the container and the Tcl object within it. For bound
* shared arrays, the underlying persistent store is updated as well.
*
* Results:
* None.
*
* Side effects:
* Memory gets reclaimed. If the shared array was bound to persistent
* storage, it removes the corresponding record.
*
*-----------------------------------------------------------------------------
*/
static int
DeleteContainer(
Container *svObj)
{
if (svObj->tclObj) {
Tcl_DecrRefCount(svObj->tclObj);
}
if (svObj->handlePtr) {
Tcl_DeleteHashEntry(svObj->handlePtr);
}
if (svObj->entryPtr) {
PsStore *psPtr = svObj->arrayPtr->psPtr;
if (psPtr) {
char *key = (char *)Tcl_GetHashKey(&svObj->arrayPtr->vars,svObj->entryPtr);
if (psPtr->psDelete(psPtr->psHandle, key) == -1) {
return TCL_ERROR;
}
}
Tcl_DeleteHashEntry(svObj->entryPtr);
}
svObj->arrayPtr = NULL;
svObj->entryPtr = NULL;
svObj->handlePtr = NULL;
svObj->tclObj = NULL;
svObj->nextPtr = svObj->bucketPtr->freeCt;
svObj->bucketPtr->freeCt = svObj;
return TCL_OK;
}
/*
*-----------------------------------------------------------------------------
*
* LockArray --
*
* Find (or create) the array structure for shared array and lock it.
* Array structure must be later unlocked with UnlockArray.
*
* Results:
* TCL_OK or TCL_ERROR if no such array.
*
* Side effects:
* Sets *arrayPtrPtr with Array pointer or leave error in given interp.
*
*-----------------------------------------------------------------------------
*/
static Array *
LockArray(
Tcl_Interp *interp, /* Interpreter to leave result. */
const char *array, /* Name of array to lock */
int flags) /* FLAGS_CREATEARRAY/FLAGS_NOERRMSG*/
{
const char *p;
size_t result, i;
Bucket *bucketPtr;
Array *arrayPtr;
/*
* Compute a hash to map an array to a bucket.
*/
p = array;
result = 0;
while (*p++) {
i = (unsigned char)*p;
result += (result << 3) + i;
}
i = (result % NUMBUCKETS);
bucketPtr = &buckets[i];
/*
* Lock the bucket and find the array, or create a new one.
* The bucket will be left locked on success.
*/
LOCK_BUCKET(bucketPtr); /* Note: no matching unlock below ! */
if (flags & FLAGS_CREATEARRAY) {
arrayPtr = CreateArray(bucketPtr, array);
} else {
Tcl_HashEntry *hPtr = Tcl_FindHashEntry(&bucketPtr->arrays, array);
if (hPtr == NULL) {
UNLOCK_BUCKET(bucketPtr);
if (!(flags & FLAGS_NOERRMSG)) {
Tcl_AppendResult(interp, "\"", array,
"\" is not a thread shared array", (void *)NULL);
}
return NULL;
}
arrayPtr = (Array*)Tcl_GetHashValue(hPtr);
}
return arrayPtr;
}
/*
*-----------------------------------------------------------------------------
*
* FlushArray --
*
* Unset all keys in an array.
*
* Results:
* None.
*
* Side effects:
* Array is cleaned but it's variable hash-hable still lives.
* For bound arrays, the persistent store is updated accordingly.
*
*-----------------------------------------------------------------------------
*/
static int
FlushArray(Array *arrayPtr) /* Name of array to flush */
{
Tcl_HashEntry *hPtr;
Tcl_HashSearch search;
for (hPtr = Tcl_FirstHashEntry(&arrayPtr->vars, &search); hPtr;
hPtr = Tcl_NextHashEntry(&search)) {
if (DeleteContainer((Container*)Tcl_GetHashValue(hPtr)) != TCL_OK) {
return TCL_ERROR;
}
}
return TCL_OK;
}
/*
*-----------------------------------------------------------------------------
*
* CreateArray --
*
* Creates new shared array instance.
*
* Results:
* Pointer to the newly created array
*
* Side effects:
* Memory gets allocated
*
*-----------------------------------------------------------------------------
*/
static Array *
CreateArray(
Bucket *bucketPtr,
const char *arrayName)
{
int isNew;
Array *arrayPtr;
Tcl_HashEntry *hPtr;
hPtr = Tcl_CreateHashEntry(&bucketPtr->arrays, arrayName, &isNew);
if (!isNew) {
return (Array*)Tcl_GetHashValue(hPtr);
}
arrayPtr = (Array *)Tcl_Alloc(sizeof(Array));
arrayPtr->bucketPtr = bucketPtr;
arrayPtr->entryPtr = hPtr;
arrayPtr->psPtr = NULL;
arrayPtr->bindAddr = NULL;
Tcl_InitHashTable(&arrayPtr->vars, TCL_STRING_KEYS);
Tcl_SetHashValue(hPtr, arrayPtr);
return arrayPtr;
}
/*
*-----------------------------------------------------------------------------
*
* DeleteArray --
*
* Deletes the shared array.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* Memory gets reclaimed.
*
*-----------------------------------------------------------------------------
*/
static int
UnbindArray(Tcl_Interp *interp, Array *arrayPtr)
{
PsStore *psPtr = arrayPtr->psPtr;
if (arrayPtr->bindAddr) {
Tcl_Free(arrayPtr->bindAddr);
arrayPtr->bindAddr = NULL;
}
if (psPtr) {
if (psPtr->psClose(psPtr->psHandle) == -1) {
if (interp) {
const char *err = psPtr->psError(psPtr->psHandle);
Tcl_SetObjResult(interp, Tcl_NewStringObj(err, TCL_INDEX_NONE));
}
return TCL_ERROR;
}
Tcl_Free(arrayPtr->psPtr), arrayPtr->psPtr = NULL;
arrayPtr->psPtr = NULL;
}
return TCL_OK;
}
static int
DeleteArray(Tcl_Interp *interp, Array *arrayPtr)
{
if (FlushArray(arrayPtr) == -1) {
return TCL_ERROR;
}
if (arrayPtr->psPtr) {
if (UnbindArray(interp, arrayPtr) != TCL_OK) {
return TCL_ERROR;
};
}
if (arrayPtr->entryPtr) {
Tcl_DeleteHashEntry(arrayPtr->entryPtr);
}
Tcl_DeleteHashTable(&arrayPtr->vars);
Tcl_Free(arrayPtr);
return TCL_OK;
}
/*
*-----------------------------------------------------------------------------
*
* SvAllocateContainers --
*
* Any similarity with the Tcl AllocateFreeObj function is purely
* coincidental... Just joking; this is (almost) 100% copy of it! :-)
*
* Results:
* None.
*
* Side effects:
* Allocates memory for many containers at the same time
*
*-----------------------------------------------------------------------------
*/
static void
SvAllocateContainers(Bucket *bucketPtr)
{
Container tmp[2];
size_t objSizePlusPadding = (size_t)(((char*)(tmp+1))-(char*)tmp);
size_t bytesToAlloc = (OBJS_TO_ALLOC_EACH_TIME * objSizePlusPadding);
char *basePtr;
Container *prevPtr = NULL, *objPtr = NULL;
int i;
basePtr = (char *)Tcl_Alloc(bytesToAlloc);
memset(basePtr, 0, bytesToAlloc);
objPtr = (Container*)basePtr;
objPtr->chunkAddr = basePtr; /* Mark chunk address for reclaim */
for (i = 0; i < OBJS_TO_ALLOC_EACH_TIME; i++) {
objPtr->nextPtr = prevPtr;
prevPtr = objPtr;
objPtr = (Container*)(((char*)objPtr) + objSizePlusPadding);
}
bucketPtr->freeCt = prevPtr;
}
#ifdef SV_FINALIZE
/*
*-----------------------------------------------------------------------------
*
* SvFinalizeContainers --
*
* Reclaim memory for free object containers per bucket.
*
* Results:
* None.
*
* Side effects:
* Memory gets reclaimed
*
*-----------------------------------------------------------------------------
*/
static void
SvFinalizeContainers(Bucket *bucketPtr)
{
Container *tmpPtr, *objPtr = bucketPtr->freeCt;
while (objPtr) {
if (objPtr->chunkAddr == (char*)objPtr) {
tmpPtr = objPtr->nextPtr;
Tcl_Free(objPtr);
objPtr = tmpPtr;
} else {
objPtr = objPtr->nextPtr;
}
}
}
#endif /* SV_FINALIZE */
/*
*-----------------------------------------------------------------------------
*
* Sv_DuplicateObj --
*
* Create and return a new object that is (mostly) a duplicate of the
* argument object. We take care that the duplicate object is either
* a proper object copy, i.e. w/o hidden references to original object
* elements or a plain string object, i.e one w/o internal representation.
*
* Decision about whether to produce a real duplicate or a string object
* is done as follows:
*
* 1) Scalar Tcl object types are properly copied by default;
* these include: boolean, int double, string and byteArray types.
* 2) Object registered with Sv_RegisterObjType are duplicated
* using custom duplicator function which is guaranteed to
* produce a proper deep copy of the object in question.
* 3) All other object types are stringified; these include
* miscelaneous Tcl objects (cmdName, nsName, bytecode, etc, etc)
* and all user-defined objects.
*
* Results:
* The return value is a pointer to a newly created Tcl_Obj. This
* object has reference count 0 and the same type, if any, as the
* source object objPtr. Also:
*
* 1) If the source object has a valid string rep, we copy it;
* otherwise, the new string rep is marked invalid.
* 2) If the source object has an internal representation (i.e. its
* typePtr is non-NULL), the new object's internal rep is set to
* a copy; otherwise the new internal rep is marked invalid.
*
* Side effects:
* Some object may, when copied, loose their type, i.e. will become
* just plain string objects.
*
*-----------------------------------------------------------------------------
*/
Tcl_Obj *
Sv_DuplicateObj(
Tcl_Obj *objPtr /* The object to duplicate. */
) {
Tcl_Obj *dupPtr = Tcl_NewObj();
/*
* Handle the internal rep
*/
if (objPtr->typePtr != NULL) {
if (objPtr->typePtr->dupIntRepProc == NULL) {
dupPtr->internalRep = objPtr->internalRep;
dupPtr->typePtr = objPtr->typePtr;
Tcl_InvalidateStringRep(dupPtr);
} else {
if ( objPtr->typePtr == booleanObjTypePtr \
|| objPtr->typePtr == byteArrayObjTypePtr \
|| objPtr->typePtr == doubleObjTypePtr \
|| objPtr->typePtr == intObjTypePtr \
|| objPtr->typePtr == wideIntObjTypePtr \
|| objPtr->typePtr == stringObjTypePtr) {
/*
* Cover all "safe" obj types (see header comment)
*/
(*objPtr->typePtr->dupIntRepProc)(objPtr, dupPtr);
if (dupPtr->typePtr != NULL) {
Tcl_InvalidateStringRep(dupPtr);
}
} else {
int found = 0;
RegType *regPtr;
/*
* Cover special registered types. Assume not
* very many of those, so this sequential walk
* should be fast enough.
*/
for (regPtr = regType; regPtr; regPtr = regPtr->nextPtr) {
if (objPtr->typePtr == regPtr->typePtr) {
(*regPtr->dupIntRepProc)(objPtr, dupPtr);
if (dupPtr->typePtr != NULL) {
Tcl_InvalidateStringRep(dupPtr);
}
found = 1;
break;
}
}
/*
* Assure at least string rep of the source
* is present, which will be copied below.
*/
if (found == 0 && objPtr->bytes == NULL
&& objPtr->typePtr->updateStringProc != NULL) {
(*objPtr->typePtr->updateStringProc)(objPtr);
}
}
}
}
/*
* Handle the string rep
*/
if (objPtr->bytes == NULL) {
if (dupPtr->bytes != Sv_tclEmptyStringRep) {
dupPtr->bytes = NULL;
}
} else if (objPtr->bytes != Sv_tclEmptyStringRep) {
/* A copy of TclInitStringRep macro */
dupPtr->bytes = (char *)Tcl_Alloc(objPtr->length + 1);
if (objPtr->length > 0) {
memcpy(dupPtr->bytes, objPtr->bytes,
objPtr->length);
}
dupPtr->length = objPtr->length;
dupPtr->bytes[objPtr->length] = '\0';
}
return dupPtr;
}
/*
*-----------------------------------------------------------------------------
*
* SvObjDispatchObjCmd --
*
* The method command for dispatching sub-commands of the shared
* object.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* Depends on the dispatched command
*
*-----------------------------------------------------------------------------
*/
static int
SvObjDispatchObjCmd(
void *arg, /* Pointer to object container. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
const char *cmdName;
SvCmdInfo *cmdPtr;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "args");
return TCL_ERROR;
}
cmdName = Tcl_GetString(objv[1]);
/*
* Do simple linear search. We may later replace this list
* with the hash table to gain speed. Currently, the list
* of registered commands is so small, so this will work
* fast enough.
*/
for (cmdPtr = svCmdInfo; cmdPtr; cmdPtr = cmdPtr->nextPtr) {
if (!strcmp(cmdPtr->name, cmdName)) {
return (*cmdPtr->objProcPtr)(arg, interp, objc, objv);
}
}
Tcl_AppendResult(interp, "invalid command name \"", cmdName, "\"", (void *)NULL);
return TCL_ERROR;
}
/*
*-----------------------------------------------------------------------------
*
* SvObjObjCmd --
*
* Creates the object command for a shared array.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* New Tcl command gets created.
*
*-----------------------------------------------------------------------------
*/
static int
SvObjObjCmd(
void *arg, /* != NULL if aolSpecial */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
int isNew, ret, flg;
Tcl_Size off;
char buf[128];
Tcl_Obj *val = NULL;
Container *svObj = NULL;
/*
* Syntax: sv::object array key ?var?
*/
ret = Sv_GetContainer(interp, objc, objv, &svObj, &off, 0);
switch (ret) {
case TCL_BREAK: /* Shared array was not found */
if (objc != off) {
val = objv[off];
}
Tcl_ResetResult(interp);
flg = FLAGS_CREATEARRAY | FLAGS_CREATEVAR;
ret = Sv_GetContainer(interp, objc, objv, &svObj, &off, flg);
if (ret != TCL_OK) {
return TCL_ERROR;
}
Tcl_DecrRefCount(svObj->tclObj);
svObj->tclObj = Sv_DuplicateObj(val ? val : Tcl_NewObj());
Tcl_IncrRefCount(svObj->tclObj);
break;
case TCL_ERROR:
return TCL_ERROR;
}
if (svObj->handlePtr == NULL) {
Tcl_HashTable *handles = &svObj->arrayPtr->bucketPtr->handles;
svObj->handlePtr = Tcl_CreateHashEntry(handles, (char*)svObj, &isNew);
}
/*
* Format the command name
*/
snprintf(buf, sizeof(buf), "::%p", svObj);
svObj->aolSpecial = (arg != NULL);
Tcl_CreateObjCommand2(interp, buf, SvObjDispatchObjCmd, svObj, NULL);
Tcl_ResetResult(interp);
Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, TCL_INDEX_NONE));
return Sv_PutContainer(interp, svObj, SV_UNCHANGED);
}
/*
*-----------------------------------------------------------------------------
*
* SvArrayObjCmd --
*
* This procedure is invoked to process the "tsv::array" command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*-----------------------------------------------------------------------------
*/
static int
SvArrayObjCmd(
void *arg, /* Pointer to object container. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
int ret = TCL_OK;
Tcl_Size i, argx = 0, lobjc = 0;
const char *arrayName = NULL;
Array *arrayPtr = NULL;
Tcl_Obj **lobjv = NULL;
Container *svObj, *elObj = NULL;
static const char *const opts[] = {
"set", "reset", "get", "names", "size", "exists", "isbound",
"bind", "unbind", NULL
};
enum options {
ASET, ARESET, AGET, ANAMES, ASIZE, AEXISTS, AISBOUND,
ABIND, AUNBIND
} index;
svObj = (Container*)arg;
if (objc < 3) {
Tcl_WrongNumArgs(interp, 1, objv, "option array");
return TCL_ERROR;
}
arrayName = Tcl_GetString(objv[2]);
arrayPtr = LockArray(interp, arrayName, FLAGS_NOERRMSG);
if (objc > 3) {
argx = 3;
}
Tcl_ResetResult(interp);
if (Tcl_GetIndexFromObjStruct(interp,objv[1],opts, sizeof(char *),"option",0,&index) != TCL_OK) {
ret = TCL_ERROR;
} else if (index == AEXISTS) {
Tcl_SetIntObj(Tcl_GetObjResult(interp), arrayPtr!=0);
} else if (index == AISBOUND) {
if (arrayPtr == NULL) {
Tcl_SetIntObj(Tcl_GetObjResult(interp), 0);
} else {
Tcl_SetIntObj(Tcl_GetObjResult(interp), arrayPtr->psPtr!=0);
}
} else if (index == ASIZE) {
if (arrayPtr == NULL) {
Tcl_SetIntObj(Tcl_GetObjResult(interp), 0);
} else {
Tcl_SetWideIntObj(Tcl_GetObjResult(interp), (Tcl_WideInt)arrayPtr->vars.numEntries);
}
} else if (index == ASET || index == ARESET) {
if (argx == (objc - 1)) {
if (argx && Tcl_ListObjGetElements(interp, objv[argx], &lobjc,
&lobjv) != TCL_OK) {
ret = TCL_ERROR;
goto cmdExit;
}
} else {
lobjc = objc - 3;
lobjv = (Tcl_Obj**)objv + 3;
}
if (lobjc & 1) {
Tcl_AppendResult(interp, "list must have an even number"
" of elements", (void *)NULL);
ret = TCL_ERROR;
goto cmdExit;
}
if (arrayPtr == NULL) {
arrayPtr = LockArray(interp, arrayName, FLAGS_CREATEARRAY);
}
if (index == ARESET) {
ret = FlushArray(arrayPtr);
if (ret != TCL_OK) {
if (arrayPtr->psPtr) {
PsStore *psPtr = arrayPtr->psPtr;
const char *err = psPtr->psError(psPtr->psHandle);
Tcl_SetObjResult(interp, Tcl_NewStringObj(err, TCL_INDEX_NONE));
}
goto cmdExit;
}
}
for (i = 0; i < lobjc; i += 2) {
const char *key = Tcl_GetString(lobjv[i]);
elObj = AcquireContainer(arrayPtr, key, FLAGS_CREATEVAR);
Tcl_DecrRefCount(elObj->tclObj);
elObj->tclObj = Sv_DuplicateObj(lobjv[i+1]);
Tcl_IncrRefCount(elObj->tclObj);
if (ReleaseContainer(interp, elObj, SV_CHANGED) != TCL_OK) {
ret = TCL_ERROR;
goto cmdExit;
}
}
} else if (index == AGET || index == ANAMES) {
if (arrayPtr) {
Tcl_HashSearch search;
Tcl_Obj *resObj = Tcl_NewListObj(0, NULL);
const char *pattern = (argx == 0) ? NULL : Tcl_GetString(objv[argx]);
Tcl_HashEntry *hPtr = Tcl_FirstHashEntry(&arrayPtr->vars,&search);
while (hPtr) {
char *key = (char *)Tcl_GetHashKey(&arrayPtr->vars, hPtr);
if (pattern == NULL || Tcl_StringCaseMatch(key, pattern, 0)) {
Tcl_ListObjAppendElement(interp, resObj,
Tcl_NewStringObj(key, TCL_INDEX_NONE));
if (index == AGET) {
elObj = (Container*)Tcl_GetHashValue(hPtr);
Tcl_ListObjAppendElement(interp, resObj,
Sv_DuplicateObj(elObj->tclObj));
}
}
hPtr = Tcl_NextHashEntry(&search);
}
Tcl_SetObjResult(interp, resObj);
}
} else if (index == ABIND) {
/*
* This is more complex operation, requiring some clarification.
*
* When binding an already existing array, we walk the array
* first and store all key/value pairs found there in the
* persistent storage. Then we proceed with the below.
*
* When binding an non-existent array, we open the persistent
* storage and cache all key/value pairs found there into tne
* newly created shared array.
*/
PsStore *psPtr;
Tcl_HashEntry *hPtr;
Tcl_Size len;
int isNew;
char *psurl, *key = NULL, *val = NULL;
if (objc < 4) {
Tcl_WrongNumArgs(interp, 2, objv, "array handle");
ret = TCL_ERROR;
goto cmdExit;
}
if (arrayPtr && arrayPtr->psPtr) {
Tcl_AppendResult(interp, "array is already bound", (void *)NULL);
ret = TCL_ERROR;
goto cmdExit;
}
psurl = Tcl_GetStringFromObj(objv[3], &len);
psPtr = GetPsStore(psurl);
if (psPtr == NULL) {
Tcl_AppendResult(interp, "can't open persistent storage on \"",
psurl, "\"", (void *)NULL);
ret = TCL_ERROR;
goto cmdExit;
}
if (arrayPtr) {
Tcl_HashSearch search;
hPtr = Tcl_FirstHashEntry(&arrayPtr->vars,&search);
arrayPtr->psPtr = psPtr;
arrayPtr->bindAddr = strcpy((char *)Tcl_Alloc(len+1), psurl);
while (hPtr) {
svObj = (Container *)Tcl_GetHashValue(hPtr);
if (ReleaseContainer(interp, svObj, SV_CHANGED) != TCL_OK) {
ret = TCL_ERROR;
goto cmdExit;
}
hPtr = Tcl_NextHashEntry(&search);
}
} else {
arrayPtr = LockArray(interp, arrayName, FLAGS_CREATEARRAY);
arrayPtr->psPtr = psPtr;
arrayPtr->bindAddr = strcpy((char *)Tcl_Alloc(len+1), psurl);
}
if (!psPtr->psFirst(psPtr->psHandle, &key, &val, &len)) {
do {
Tcl_Obj * tclObj = Tcl_NewStringObj(val, len);
hPtr = Tcl_CreateHashEntry(&arrayPtr->vars, key, &isNew);
Tcl_SetHashValue(hPtr, CreateContainer(arrayPtr, hPtr, tclObj));
psPtr->psFree(psPtr->psHandle, val);
} while (!psPtr->psNext(psPtr->psHandle, &key, &val, &len));
}
} else if (index == AUNBIND) {
if (!arrayPtr || !arrayPtr->psPtr) {
Tcl_AppendResult(interp, "shared variable is not bound", (void *)NULL);
ret = TCL_ERROR;
goto cmdExit;
}
if (UnbindArray(interp, arrayPtr) != TCL_OK) {
ret = TCL_ERROR;
goto cmdExit;
}
}
cmdExit:
if (arrayPtr) {
UnlockArray(arrayPtr);
}
return ret;
}
/*
*-----------------------------------------------------------------------------
*
* SvUnsetObjCmd --
*
* This procedure is invoked to process the "tsv::unset" command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*-----------------------------------------------------------------------------
*/
static int
SvUnsetObjCmd(
TCL_UNUSED(void *), /* Not used. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tcl_Size ii;
const char *arrayName;
Array *arrayPtr;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "array ?key ...?");
return TCL_ERROR;
}
arrayName = Tcl_GetString(objv[1]);
arrayPtr = LockArray(interp, arrayName, 0);
if (arrayPtr == NULL) {
return TCL_ERROR;
}
if (objc == 2) {
UnlockArray(arrayPtr);
if (DeleteArray(interp, arrayPtr) != TCL_OK) {
return TCL_ERROR;
}
} else {
for (ii = 2; ii < objc; ii++) {
const char *key = Tcl_GetString(objv[ii]);
Tcl_HashEntry *hPtr = Tcl_FindHashEntry(&arrayPtr->vars, key);
if (hPtr) {
if (DeleteContainer((Container*)Tcl_GetHashValue(hPtr))
!= TCL_OK) {
UnlockArray(arrayPtr);
return TCL_ERROR;
}
} else {
UnlockArray(arrayPtr);
Tcl_AppendResult(interp,"no key ",arrayName,"(",key,")",(void *)NULL);
return TCL_ERROR;
}
}
UnlockArray(arrayPtr);
}
return TCL_OK;
}
/*
*-----------------------------------------------------------------------------
*
* SvNamesObjCmd --
*
* This procedure is invoked to process the "tsv::names" command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*-----------------------------------------------------------------------------
*/
static int
SvNamesObjCmd(
void *arg, /* != NULL if aolSpecial */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
int i;
const char *pattern = NULL;
Tcl_HashEntry *hPtr;
Tcl_HashSearch search;
Tcl_Obj *resObj;
if (objc > 2) {
Tcl_WrongNumArgs(interp, 1, objv, "?pattern?");
return TCL_ERROR;
}
if (objc == 2) {
pattern = Tcl_GetString(objv[1]);
}
resObj = Tcl_NewListObj(0, NULL);
for (i = 0; i < NUMBUCKETS; i++) {
Bucket *bucketPtr = &buckets[i];
LOCK_BUCKET(bucketPtr);
hPtr = Tcl_FirstHashEntry(&bucketPtr->arrays, &search);
while (hPtr) {
char *key = (char *)Tcl_GetHashKey(&bucketPtr->arrays, hPtr);
if ((arg==NULL || (*key != '.')) /* Hide .<name> arrays for AOL*/ &&
(pattern == NULL || Tcl_StringCaseMatch(key, pattern, 0))) {
Tcl_ListObjAppendElement(interp, resObj,
Tcl_NewStringObj(key, TCL_INDEX_NONE));
}
hPtr = Tcl_NextHashEntry(&search);
}
UNLOCK_BUCKET(bucketPtr);
}
Tcl_SetObjResult(interp, resObj);
return TCL_OK;
}
/*
*-----------------------------------------------------------------------------
*
* SvGetObjCmd --
*
* This procedure is invoked to process "tsv::get" command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*-----------------------------------------------------------------------------
*/
static int
SvGetObjCmd(
void *arg, /* Pointer to object container. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
int ret;
Tcl_Size off;
Tcl_Obj *res;
Container *svObj = (Container*)arg;
/*
* Syntax:
* tsv::get array key ?var?
* $object get ?var?
*/
ret = Sv_GetContainer(interp, objc, objv, &svObj, &off, 0);
switch (ret) {
case TCL_BREAK:
if (objc == off) {
return TCL_ERROR;
} else {
Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
return TCL_OK;
}
case TCL_ERROR:
return TCL_ERROR;
}
res = Sv_DuplicateObj(svObj->tclObj);
if (objc == off) {
Tcl_SetObjResult(interp, res);
} else {
if (Tcl_ObjSetVar2(interp, objv[off], NULL, res, 0) == NULL) {
Tcl_DecrRefCount(res);
goto cmd_err;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(1));
}
return Sv_PutContainer(interp, svObj, SV_UNCHANGED);
cmd_err:
return Sv_PutContainer(interp, svObj, SV_ERROR);
}
/*
*-----------------------------------------------------------------------------
*
* SvExistsObjCmd --
*
* This procedure is invoked to process "tsv::exists" command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*-----------------------------------------------------------------------------
*/
static int
SvExistsObjCmd(
void *arg, /* Pointer to object container. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tcl_Size off;
int ret;
Container *svObj = (Container*)arg;
/*
* Syntax:
* tsv::exists array key
* $object exists
*/
ret = Sv_GetContainer(interp, objc, objv, &svObj, &off, 0);
switch (ret) {
case TCL_BREAK: /* Array/key not found */
Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
return TCL_OK;
case TCL_ERROR:
return TCL_ERROR;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(1));
return Sv_PutContainer(interp, svObj, SV_UNCHANGED);
}
/*
*-----------------------------------------------------------------------------
*
* SvSetObjCmd --
*
* This procedure is invoked to process the "tsv::set" command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*-----------------------------------------------------------------------------
*/
static int
SvSetObjCmd(
void *arg, /* Pointer to object container */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
int ret, flg, mode;
Tcl_Size off;
Tcl_Obj *val;
Container *svObj = (Container*)arg;
/*
* Syntax:
* tsv::set array key ?value?
* $object set ?value?
*/
ret = Sv_GetContainer(interp, objc, objv, &svObj, &off, 0);
switch (ret) {
case TCL_BREAK:
if (objc == off) {
return TCL_ERROR;
} else {
Tcl_ResetResult(interp);
flg = FLAGS_CREATEARRAY | FLAGS_CREATEVAR;
ret = Sv_GetContainer(interp, objc, objv, &svObj, &off, flg);
if (ret != TCL_OK) {
return TCL_ERROR;
}
}
break;
case TCL_ERROR:
return TCL_ERROR;
}
if (objc != off) {
val = objv[off];
Tcl_DecrRefCount(svObj->tclObj);
svObj->tclObj = Sv_DuplicateObj(val);
Tcl_IncrRefCount(svObj->tclObj);
mode = SV_CHANGED;
} else {
val = Sv_DuplicateObj(svObj->tclObj);
mode = SV_UNCHANGED;
}
Tcl_SetObjResult(interp, val);
return Sv_PutContainer(interp, svObj, mode);
}
/*
*-----------------------------------------------------------------------------
*
* SvIncrObjCmd --
*
* This procedure is invoked to process the "tsv::incr" command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*-----------------------------------------------------------------------------
*/
static int
SvIncrObjCmd(
void *arg, /* Pointer to object container */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
int ret, flg, isNew = 0;
Tcl_Size off;
Tcl_WideInt incrValue = 1, currValue = 0;
Container *svObj = (Container*)arg;
/*
* Syntax:
* tsv::incr array key ?increment?
* $object incr ?increment?
*/
ret = Sv_GetContainer(interp, objc, objv, &svObj, &off, 0);
if (ret != TCL_OK) {
if (ret != TCL_BREAK) {
return TCL_ERROR;
}
flg = FLAGS_CREATEARRAY | FLAGS_CREATEVAR;
Tcl_ResetResult(interp);
ret = Sv_GetContainer(interp, objc, objv, &svObj, &off, flg);
if (ret != TCL_OK) {
return TCL_ERROR;
}
isNew = 1;
}
if ((objc != off)) {
ret = Tcl_GetWideIntFromObj(interp, objv[off], &incrValue);
if (ret != TCL_OK) {
goto cmd_err;
}
}
if (isNew) {
currValue = 0;
} else {
ret = Tcl_GetWideIntFromObj(interp, svObj->tclObj, &currValue);
if (ret != TCL_OK) {
goto cmd_err;
}
}
incrValue += currValue;
Tcl_SetWideIntObj(svObj->tclObj, incrValue);
Tcl_ResetResult(interp);
Tcl_SetWideIntObj(Tcl_GetObjResult(interp), incrValue);
return Sv_PutContainer(interp, svObj, SV_CHANGED);
cmd_err:
return Sv_PutContainer(interp, svObj, SV_ERROR);
}
/*
*-----------------------------------------------------------------------------
*
* SvAppendObjCmd --
*
* This procedure is invoked to process the "tsv::append" command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*-----------------------------------------------------------------------------
*/
static int
SvAppendObjCmd(
void *arg, /* Pointer to object container */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
int flg, ret;
Tcl_Size i, off;
Container *svObj = (Container*)arg;
/*
* Syntax:
* tsv::append array key value ?value ...?
* $object append value ?value ...?
*/
flg = FLAGS_CREATEARRAY | FLAGS_CREATEVAR;
ret = Sv_GetContainer(interp, objc, objv, &svObj, &off, flg);
if (ret != TCL_OK) {
return TCL_ERROR;
}
if (objc < 1 + off) {
Tcl_WrongNumArgs(interp, off, objv, "value ?value ...?");
goto cmd_err;
}
for (i = off; i < objc; ++i) {
Tcl_AppendObjToObj(svObj->tclObj, Sv_DuplicateObj(objv[i]));
}
Tcl_SetObjResult(interp, Sv_DuplicateObj(svObj->tclObj));
return Sv_PutContainer(interp, svObj, SV_CHANGED);
cmd_err:
return Sv_PutContainer(interp, svObj, SV_ERROR);
}
/*
*-----------------------------------------------------------------------------
*
* SvPopObjCmd --
*
* This procedure is invoked to process "tsv::pop" command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*-----------------------------------------------------------------------------
*/
static int
SvPopObjCmd(
void *arg, /* Pointer to object container */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
int ret;
Tcl_Size off;
Tcl_Obj *retObj;
Array *arrayPtr = NULL;
Container *svObj = (Container*)arg;
/*
* Syntax:
* tsv::pop array key ?var?
* $object pop ?var?
*
* Note: the object command will run into error next time !
*/
ret = Sv_GetContainer(interp, objc, objv, &svObj, &off, 0);
switch (ret) {
case TCL_BREAK:
if (objc == off) {
return TCL_ERROR;
} else {
Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
return TCL_OK;
}
case TCL_ERROR:
return TCL_ERROR;
}
arrayPtr = svObj->arrayPtr;
retObj = svObj->tclObj;
svObj->tclObj = NULL;
if (DeleteContainer(svObj) != TCL_OK) {
if (svObj->arrayPtr->psPtr) {
PsStore *psPtr = svObj->arrayPtr->psPtr;
const char *err = psPtr->psError(psPtr->psHandle);
Tcl_SetObjResult(interp, Tcl_NewStringObj(err, TCL_INDEX_NONE));
}
ret = TCL_ERROR;
goto cmd_exit;
}
if (objc == off) {
Tcl_SetObjResult(interp, retObj);
} else {
if (Tcl_ObjSetVar2(interp, objv[off], NULL, retObj, 0) == NULL) {
ret = TCL_ERROR;
goto cmd_exit;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(1));
}
cmd_exit:
Tcl_DecrRefCount(retObj);
UnlockArray(arrayPtr);
return ret;
}
/*
*-----------------------------------------------------------------------------
*
* SvMoveObjCmd --
*
* This procedure is invoked to process the "tsv::move" command.
* See the user documentation for details on what it does.
*
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*-----------------------------------------------------------------------------
*/
static int
SvMoveObjCmd(
void *arg, /* Pointer to object container. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
int ret, isNew;
Tcl_Size off;
const char *toKey;
Tcl_HashEntry *hPtr;
Container *svObj = (Container*)arg;
/*
* Syntax:
* tsv::move array key to
* $object move to
*/
ret = Sv_GetContainer(interp, objc, objv, &svObj, &off, 0);
if (ret != TCL_OK) {
return TCL_ERROR;
}
toKey = Tcl_GetString(objv[off]);
hPtr = Tcl_CreateHashEntry(&svObj->arrayPtr->vars, toKey, &isNew);
if (!isNew) {
Tcl_AppendResult(interp, "key \"", toKey, "\" exists", (void *)NULL);
goto cmd_err;
}
if (svObj->entryPtr) {
char *key = (char *)Tcl_GetHashKey(&svObj->arrayPtr->vars, svObj->entryPtr);
if (svObj->arrayPtr->psPtr) {
PsStore *psPtr = svObj->arrayPtr->psPtr;
if (psPtr->psDelete(psPtr->psHandle, key) == -1) {
const char *err = psPtr->psError(psPtr->psHandle);
Tcl_SetObjResult(interp, Tcl_NewStringObj(err, TCL_INDEX_NONE));
return TCL_ERROR;
}
}
Tcl_DeleteHashEntry(svObj->entryPtr);
}
svObj->entryPtr = hPtr;
Tcl_SetHashValue(hPtr, svObj);
return Sv_PutContainer(interp, svObj, SV_CHANGED);
cmd_err:
return Sv_PutContainer(interp, svObj, SV_ERROR);
}
/*
*----------------------------------------------------------------------
*
* SvLockObjCmd --
*
* This procedure is invoked to process "tsv::lock" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
static int
SvLockObjCmd(
TCL_UNUSED(void *), /* Not used. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
int ret;
Tcl_Obj *scriptObj;
Bucket *bucketPtr;
Array *arrayPtr = NULL;
/*
* Syntax:
*
* tsv::lock array arg ?arg ...?
*/
if (objc < 3) {
Tcl_WrongNumArgs(interp, 1, objv, "array arg ?arg...?");
return TCL_ERROR;
}
arrayPtr = LockArray(interp, Tcl_GetString(objv[1]), FLAGS_CREATEARRAY);
bucketPtr = arrayPtr->bucketPtr;
/*
* Evaluate passed arguments as Tcl script. Note that
* Tcl_EvalObjEx throws away the passed object by
* doing an decrement reference count on it. This also
* means we need not build object bytecode rep.
*/
if (objc == 3) {
scriptObj = Tcl_DuplicateObj(objv[2]);
} else {
scriptObj = Tcl_ConcatObj(objc-2, objv + 2);
}
Tcl_AllowExceptions(interp);
ret = Tcl_EvalObjEx(interp, scriptObj, TCL_EVAL_DIRECT);
if (ret == TCL_ERROR) {
char msg[32 + TCL_INTEGER_SPACE];
snprintf(msg, sizeof(msg), "\n (\"eval\" body line %d)", Tcl_GetErrorLine(interp));
Tcl_AppendObjToErrorInfo(interp, Tcl_NewStringObj(msg, TCL_INDEX_NONE));
}
/*
* We unlock the bucket directly, w/o going to Sv_Unlock()
* since it needs the array which may be unset by the script.
*/
UNLOCK_BUCKET(bucketPtr);
return ret;
}
/*
*-----------------------------------------------------------------------------
*
* SvHandlersObjCmd --
*
* This procedure is invoked to process "tsv::handlers" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static int
SvHandlersObjCmd(
TCL_UNUSED(void *), /* Not used. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
PsStore *tmpPtr = NULL;
/*
* Syntax:
*
* tsv::handlers
*/
if (objc != 1) {
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return TCL_ERROR;
}
Tcl_ResetResult(interp);
Tcl_MutexLock(&svMutex);
for (tmpPtr = psStore; tmpPtr; tmpPtr = tmpPtr->nextPtr) {
Tcl_AppendElement(interp, tmpPtr->type);
}
Tcl_MutexUnlock(&svMutex);
return TCL_OK;
}
/*
*-----------------------------------------------------------------------------
*
* Sv_RegisterStdCommands --
*
* Register standard shared variable commands
*
* Results:
* A standard Tcl result.
*
* Side effects:
* Memory gets allocated
*
*-----------------------------------------------------------------------------
*/
static void
SvRegisterStdCommands(void)
{
static int initialized = 0;
if (initialized == 0) {
Tcl_MutexLock(&initMutex);
if (initialized == 0) {
Sv_RegisterCommand("var", SvObjObjCmd, NULL, 1);
Sv_RegisterCommand("object", SvObjObjCmd, NULL, 1);
Sv_RegisterCommand("set", SvSetObjCmd, NULL, 0);
Sv_RegisterCommand("unset", SvUnsetObjCmd, NULL, 0);
Sv_RegisterCommand("get", SvGetObjCmd, NULL, 0);
Sv_RegisterCommand("incr", SvIncrObjCmd, NULL, 0);
Sv_RegisterCommand("exists", SvExistsObjCmd, NULL, 0);
Sv_RegisterCommand("append", SvAppendObjCmd, NULL, 0);
Sv_RegisterCommand("array", SvArrayObjCmd, NULL, 0);
Sv_RegisterCommand("names", SvNamesObjCmd, NULL, 0);
Sv_RegisterCommand("pop", SvPopObjCmd, NULL, 0);
Sv_RegisterCommand("move", SvMoveObjCmd, NULL, 0);
Sv_RegisterCommand("lock", SvLockObjCmd, NULL, 0);
Sv_RegisterCommand("handlers", SvHandlersObjCmd, NULL, 0);
initialized = 1;
}
Tcl_MutexUnlock(&initMutex);
}
}
/*
*-----------------------------------------------------------------------------
*
* SvInit --
*
* Creates commands in current interpreter.
*
* Results:
* NULL
*
* Side effects
* Many new command created in current interpreter. Global data
* structures used by them initialized as well.
*
*-----------------------------------------------------------------------------
*/
const char *
SvInit (
Tcl_Interp *interp
) {
Tcl_Size i;
int b;
Bucket *bucketPtr;
SvCmdInfo *cmdPtr;
Tcl_Obj *obj;
#ifdef SV_FINALIZE
/*
* Create exit handler for this thread
*/
Tcl_CreateThreadExitHandler(SvFinalize, NULL);
/*
* Increment number of threads
*/
Tcl_MutexLock(&nofThreadsMutex);
++nofThreads;
Tcl_MutexUnlock(&nofThreadsMutex);
#endif /* SV_FINALIZE */
/*
* Add keyed-list datatype
*/
TclX_KeyedListInit(interp);
Sv_RegisterKeylistCommands();
/*
* Register standard (nsv_* compatible) and our
* own extensive set of list manipulating commands
*/
SvRegisterStdCommands();
Sv_RegisterListCommands();
/*
* Get Tcl object types. These are used
* in custom object duplicator function.
*/
obj = Tcl_NewStringObj("no", TCL_INDEX_NONE);
Tcl_GetBooleanFromObj(NULL, obj, &b);
booleanObjTypePtr = obj->typePtr;
#ifdef USE_TCL_STUBS
if (tclStubsPtr->tcl_GetUnicodeFromObj)
#endif
{
Tcl_GetUnicodeFromObj(obj, &i);
stringObjTypePtr = obj->typePtr;
}
Tcl_GetByteArrayFromObj(obj, &i);
byteArrayObjTypePtr = obj->typePtr;
Tcl_DecrRefCount(obj);
obj = Tcl_NewDoubleObj(0.0);
doubleObjTypePtr = obj->typePtr;
Tcl_DecrRefCount(obj);
obj = Tcl_NewIntObj(0);
intObjTypePtr = obj->typePtr;
Tcl_DecrRefCount(obj);
obj = Tcl_NewWideIntObj(((Tcl_WideInt)1)<<35);
wideIntObjTypePtr = obj->typePtr;
Tcl_DecrRefCount(obj);
/*
* Plug-in registered commands in current interpreter
*/
for (cmdPtr = svCmdInfo; cmdPtr; cmdPtr = cmdPtr->nextPtr) {
Tcl_CreateObjCommand2(interp, cmdPtr->cmdName, cmdPtr->objProcPtr,
NULL, NULL);
#ifdef NS_AOLSERVER
Tcl_CreateObjCommand2(interp, cmdPtr->cmdName2, cmdPtr->objProcPtr,
(void *)(size_t)cmdPtr->aolSpecial, NULL);
#endif
}
/*
* Create array of buckets and initialize each bucket
*/
if (buckets == NULL) {
Tcl_MutexLock(&bucketsMutex);
if (buckets == NULL) {
buckets = (Bucket *)Tcl_Alloc(sizeof(Bucket) * NUMBUCKETS);
for (i = 0; i < NUMBUCKETS; ++i) {
bucketPtr = &buckets[i];
memset(bucketPtr, 0, sizeof(Bucket));
Tcl_InitHashTable(&bucketPtr->arrays, TCL_STRING_KEYS);
Tcl_InitHashTable(&bucketPtr->handles, TCL_ONE_WORD_KEYS);
}
/*
* There is no other way to get Sv_tclEmptyStringRep
* pointer value w/o this trick.
*/
{
Tcl_Obj *dummy = Tcl_NewObj();
Sv_tclEmptyStringRep = dummy->bytes;
Tcl_DecrRefCount(dummy);
}
/*
* Register persistent store handlers
*/
#ifdef HAVE_GDBM
Sv_RegisterGdbmStore();
#endif
#ifdef HAVE_LMDB
Sv_RegisterLmdbStore();
#endif
}
Tcl_MutexUnlock(&bucketsMutex);
}
return NULL;
}
#ifdef SV_FINALIZE
/*
* Left for reference, but unused since multithreaded finalization is
* unsolvable in the general case. Brave souls can revive this by
* installing a late exit handler on Thread's behalf, bringing the
* function back onto the Tcl_Finalize (but not Tcl_Exit) path.
*/
/*
*-----------------------------------------------------------------------------
*
* SvFinalize --
*
* Unset all arrays and reclaim all buckets.
*
* Results:
* None.
*
* Side effects
* Memory gets reclaimed.
*
*-----------------------------------------------------------------------------
*/
static void
SvFinalize (
TCL_UNUSED(void *))
{
int i;
SvCmdInfo *cmdPtr;
RegType *regPtr;
Tcl_HashEntry *hashPtr;
Tcl_HashSearch search;
/*
* Decrement number of threads. Proceed only if I was the last one. The
* mutex is unlocked at the end of this function, so new threads that might
* want to register in the meanwhile will find a clean environment when
* they eventually succeed acquiring nofThreadsMutex.
*/
Tcl_MutexLock(&nofThreadsMutex);
if (nofThreads > 1)
{
goto done;
}
/*
* Reclaim memory for shared arrays
*/
if (buckets != NULL) {
Tcl_MutexLock(&bucketsMutex);
if (buckets != NULL) {
for (i = 0; i < NUMBUCKETS; ++i) {
Bucket *bucketPtr = &buckets[i];
hashPtr = Tcl_FirstHashEntry(&bucketPtr->arrays, &search);
while (hashPtr != NULL) {
Array *arrayPtr = (Array*)Tcl_GetHashValue(hashPtr);
UnlockArray(arrayPtr);
/* unbind array before delete (avoid flush of persistent storage) */
UnbindArray(NULL, arrayPtr);
/* flush, delete etc. */
DeleteArray(NULL, arrayPtr);
hashPtr = Tcl_NextHashEntry(&search);
}
if (bucketPtr->lock) {
Sp_RecursiveMutexFinalize(&bucketPtr->lock);
}
SvFinalizeContainers(bucketPtr);
Tcl_DeleteHashTable(&bucketPtr->handles);
Tcl_DeleteHashTable(&bucketPtr->arrays);
}
Tcl_Free(buckets), buckets = NULL;
}
buckets = NULL;
Tcl_MutexUnlock(&bucketsMutex);
}
Tcl_MutexLock(&svMutex);
/*
* Reclaim memory for registered commands
*/
if (svCmdInfo != NULL) {
cmdPtr = svCmdInfo;
while (cmdPtr) {
SvCmdInfo *tmpPtr = cmdPtr->nextPtr;
Tcl_Free(cmdPtr);
cmdPtr = tmpPtr;
}
svCmdInfo = NULL;
}
/*
* Reclaim memory for registered object types
*/
if (regType != NULL) {
regPtr = regType;
while (regPtr) {
RegType *tmpPtr = regPtr->nextPtr;
Tcl_Free(regPtr);
regPtr = tmpPtr;
}
regType = NULL;
}
Tcl_MutexUnlock(&svMutex);
done:
--nofThreads;
Tcl_MutexUnlock(&nofThreadsMutex);
}
#endif /* SV_FINALIZE */
/* EOF $RCSfile: threadSvCmd.c,v $ */
/* Emacs Setup Variables */
/* Local Variables: */
/* mode: C */
/* indent-tabs-mode: nil */
/* c-basic-offset: 4 */
/* End: */
|