| 41 |
{ |
{ |
| 42 |
SECTION_TRY_LOCK_WAIT_TIME = 1, // second |
SECTION_TRY_LOCK_WAIT_TIME = 1, // second |
| 43 |
SECTION_TRY_LOCK_TIMES = 10, |
SECTION_TRY_LOCK_TIMES = 10, |
| 44 |
|
SECTION_DEAD_LOCK_TIMEOUT = 15, // second |
| 45 |
|
|
| 46 |
ARTICLE_BLOCK_PER_SHM = 1000, // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
ARTICLE_BLOCK_PER_SHM = 1000, // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
| 47 |
ARTICLE_BLOCK_SHM_COUNT_LIMIT = 80, // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
ARTICLE_BLOCK_SHM_COUNT_LIMIT = 80, // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
| 81 |
static char section_list_shm_name[FILE_NAME_LEN]; |
static char section_list_shm_name[FILE_NAME_LEN]; |
| 82 |
SECTION_LIST_POOL *p_section_list_pool = NULL; |
SECTION_LIST_POOL *p_section_list_pool = NULL; |
| 83 |
|
|
| 84 |
|
#ifndef HAVE_SYSTEM_V |
| 85 |
|
static int section_list_reset_lock(SECTION_LIST *p_section); |
| 86 |
|
#endif |
| 87 |
|
|
| 88 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 89 |
{ |
{ |
| 90 |
char filepath[FILE_PATH_LEN]; |
char filepath[FILE_PATH_LEN]; |
| 446 |
int fd; |
int fd; |
| 447 |
size_t size; |
size_t size; |
| 448 |
void *p_shm; |
void *p_shm; |
| 449 |
int semid; |
#ifdef HAVE_SYSTEM_V |
| 450 |
int proj_id; |
int proj_id; |
| 451 |
key_t key; |
key_t key; |
| 452 |
|
int semid; |
| 453 |
union semun arg; |
union semun arg; |
| 454 |
|
#endif |
| 455 |
int i; |
int i; |
| 456 |
|
|
| 457 |
if (p_section_list_pool != NULL) |
if (p_section_list_pool != NULL) |
| 503 |
p_section_list_pool->section_count = 0; |
p_section_list_pool->section_count = 0; |
| 504 |
|
|
| 505 |
// Allocate semaphore as section locks |
// Allocate semaphore as section locks |
| 506 |
|
#ifndef HAVE_SYSTEM_V |
| 507 |
|
for (i = 0; i <= BBS_max_section; i++) |
| 508 |
|
{ |
| 509 |
|
if (sem_init(&(p_section_list_pool->sem[i]), 1, 1) == -1) |
| 510 |
|
{ |
| 511 |
|
log_error("sem_init(sem[%d]) error (%d)\n", i, errno); |
| 512 |
|
return -3; |
| 513 |
|
} |
| 514 |
|
|
| 515 |
|
p_section_list_pool->read_lock_count[i] = 0; |
| 516 |
|
p_section_list_pool->write_lock_count[i] = 0; |
| 517 |
|
} |
| 518 |
|
#else |
| 519 |
proj_id = (int)(time(NULL) % getpid()); |
proj_id = (int)(time(NULL) % getpid()); |
| 520 |
key = ftok(filename, proj_id); |
key = ftok(filename, proj_id); |
| 521 |
if (key == -1) |
if (key == -1) |
| 544 |
} |
} |
| 545 |
|
|
| 546 |
p_section_list_pool->semid = semid; |
p_section_list_pool->semid = semid; |
| 547 |
|
#endif |
| 548 |
|
|
| 549 |
p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create(); |
p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create(); |
| 550 |
if (p_section_list_pool->p_trie_dict_section_by_name == NULL) |
if (p_section_list_pool->p_trie_dict_section_by_name == NULL) |
| 582 |
p_section_list_pool->p_trie_dict_section_by_sid = NULL; |
p_section_list_pool->p_trie_dict_section_by_sid = NULL; |
| 583 |
} |
} |
| 584 |
|
|
| 585 |
|
#ifdef HAVE_SYSTEM_V |
| 586 |
if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1) |
if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1) |
| 587 |
{ |
{ |
| 588 |
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno); |
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno); |
| 589 |
} |
} |
| 590 |
|
#else |
| 591 |
|
for (int i = 0; i <= BBS_max_section; i++) |
| 592 |
|
{ |
| 593 |
|
if (sem_destroy(&(p_section_list_pool->sem[i])) == -1) |
| 594 |
|
{ |
| 595 |
|
log_error("sem_destroy(sem[%d]) error (%d)\n", i, errno); |
| 596 |
|
} |
| 597 |
|
} |
| 598 |
|
#endif |
| 599 |
|
|
| 600 |
detach_section_list_shm(); |
detach_section_list_shm(); |
| 601 |
|
|
| 1667 |
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
| 1668 |
{ |
{ |
| 1669 |
int index; |
int index; |
| 1670 |
|
#ifdef HAVE_SYSTEM_V |
| 1671 |
struct sembuf sops[4]; |
struct sembuf sops[4]; |
|
#ifndef __CYGWIN__ |
|
|
struct timespec timeout; |
|
| 1672 |
#endif |
#endif |
| 1673 |
int ret; |
struct timespec timeout; |
| 1674 |
|
int ret = 0; |
| 1675 |
|
|
| 1676 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1677 |
if (index < 0) |
if (index < 0) |
| 1679 |
return -2; |
return -2; |
| 1680 |
} |
} |
| 1681 |
|
|
| 1682 |
|
timeout.tv_sec = wait_sec; |
| 1683 |
|
timeout.tv_nsec = 0; |
| 1684 |
|
|
| 1685 |
|
#ifdef HAVE_SYSTEM_V |
| 1686 |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1687 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 1688 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 1705 |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
| 1706 |
} |
} |
| 1707 |
|
|
|
#ifdef __CYGWIN__ |
|
|
ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4)); |
|
|
#else |
|
|
timeout.tv_sec = wait_sec; |
|
|
timeout.tv_nsec = 0; |
|
|
|
|
| 1708 |
ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout); |
|
#endif |
|
| 1709 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1710 |
{ |
{ |
| 1711 |
log_error("semop(index = %d, lock read) error %d\n", index, errno); |
log_error("semop(index = %d, lock read) error %d\n", index, errno); |
| 1712 |
} |
} |
| 1713 |
|
#else |
| 1714 |
|
if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1) |
| 1715 |
|
{ |
| 1716 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1717 |
|
{ |
| 1718 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", index, errno); |
| 1719 |
|
} |
| 1720 |
|
return -1; |
| 1721 |
|
} |
| 1722 |
|
|
| 1723 |
|
if (index != BBS_max_section) |
| 1724 |
|
{ |
| 1725 |
|
if (sem_timedwait(&(p_section_list_pool->sem[BBS_max_section]), &timeout) == -1) |
| 1726 |
|
{ |
| 1727 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1728 |
|
{ |
| 1729 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1730 |
|
} |
| 1731 |
|
// release previously acquired lock |
| 1732 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1733 |
|
{ |
| 1734 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1735 |
|
} |
| 1736 |
|
return -1; |
| 1737 |
|
} |
| 1738 |
|
} |
| 1739 |
|
|
| 1740 |
|
if (p_section_list_pool->write_lock_count[index] == 0 && |
| 1741 |
|
(index != BBS_max_section && p_section_list_pool->write_lock_count[BBS_max_section] == 0)) |
| 1742 |
|
{ |
| 1743 |
|
p_section_list_pool->read_lock_count[index]++; |
| 1744 |
|
if (index != BBS_max_section) |
| 1745 |
|
{ |
| 1746 |
|
p_section_list_pool->read_lock_count[BBS_max_section]++; |
| 1747 |
|
} |
| 1748 |
|
} |
| 1749 |
|
else |
| 1750 |
|
{ |
| 1751 |
|
errno = EAGAIN; |
| 1752 |
|
ret = -1; |
| 1753 |
|
} |
| 1754 |
|
|
| 1755 |
|
if (index != BBS_max_section) |
| 1756 |
|
{ |
| 1757 |
|
// release lock on "all section" |
| 1758 |
|
if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1759 |
|
{ |
| 1760 |
|
log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1761 |
|
ret = -1; |
| 1762 |
|
} |
| 1763 |
|
} |
| 1764 |
|
|
| 1765 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1766 |
|
{ |
| 1767 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1768 |
|
return -1; |
| 1769 |
|
} |
| 1770 |
|
#endif |
| 1771 |
|
|
| 1772 |
return ret; |
return ret; |
| 1773 |
} |
} |
| 1775 |
int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec) |
int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec) |
| 1776 |
{ |
{ |
| 1777 |
int index; |
int index; |
| 1778 |
|
#ifdef HAVE_SYSTEM_V |
| 1779 |
struct sembuf sops[3]; |
struct sembuf sops[3]; |
|
#ifndef __CYGWIN__ |
|
|
struct timespec timeout; |
|
| 1780 |
#endif |
#endif |
| 1781 |
int ret; |
struct timespec timeout; |
| 1782 |
|
int ret = 0; |
| 1783 |
|
|
| 1784 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1785 |
if (index < 0) |
if (index < 0) |
| 1787 |
return -2; |
return -2; |
| 1788 |
} |
} |
| 1789 |
|
|
| 1790 |
|
timeout.tv_sec = wait_sec; |
| 1791 |
|
timeout.tv_nsec = 0; |
| 1792 |
|
|
| 1793 |
|
#ifdef HAVE_SYSTEM_V |
| 1794 |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1795 |
sops[0].sem_op = 0; // wait until unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 1796 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 1803 |
sops[2].sem_op = 0; // wait until unlocked |
sops[2].sem_op = 0; // wait until unlocked |
| 1804 |
sops[2].sem_flg = 0; |
sops[2].sem_flg = 0; |
| 1805 |
|
|
|
#ifdef __CYGWIN__ |
|
|
ret = semop(p_section_list_pool->semid, sops, 3); |
|
|
#else |
|
|
timeout.tv_sec = wait_sec; |
|
|
timeout.tv_nsec = 0; |
|
|
|
|
| 1806 |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
|
#endif |
|
| 1807 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1808 |
{ |
{ |
| 1809 |
log_error("semop(index = %d, lock write) error %d\n", index, errno); |
log_error("semop(index = %d, lock write) error %d\n", index, errno); |
| 1810 |
} |
} |
| 1811 |
|
#else |
| 1812 |
|
if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1) |
| 1813 |
|
{ |
| 1814 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1815 |
|
{ |
| 1816 |
|
log_error("sem_timedwait(sem[%d]) error %d\n", index, errno); |
| 1817 |
|
} |
| 1818 |
|
return -1; |
| 1819 |
|
} |
| 1820 |
|
|
| 1821 |
|
if (p_section_list_pool->read_lock_count[index] == 0 && p_section_list_pool->write_lock_count[index] == 0) |
| 1822 |
|
{ |
| 1823 |
|
p_section_list_pool->write_lock_count[index]++; |
| 1824 |
|
} |
| 1825 |
|
else |
| 1826 |
|
{ |
| 1827 |
|
errno = EAGAIN; |
| 1828 |
|
ret = -1; |
| 1829 |
|
} |
| 1830 |
|
|
| 1831 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1832 |
|
{ |
| 1833 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1834 |
|
return -1; |
| 1835 |
|
} |
| 1836 |
|
#endif |
| 1837 |
|
|
| 1838 |
return ret; |
return ret; |
| 1839 |
} |
} |
| 1841 |
int section_list_rd_unlock(SECTION_LIST *p_section) |
int section_list_rd_unlock(SECTION_LIST *p_section) |
| 1842 |
{ |
{ |
| 1843 |
int index; |
int index; |
| 1844 |
|
#ifdef HAVE_SYSTEM_V |
| 1845 |
struct sembuf sops[2]; |
struct sembuf sops[2]; |
| 1846 |
int ret; |
#endif |
| 1847 |
|
int ret = 0; |
| 1848 |
|
|
| 1849 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1850 |
if (index < 0) |
if (index < 0) |
| 1852 |
return -2; |
return -2; |
| 1853 |
} |
} |
| 1854 |
|
|
| 1855 |
|
#ifdef HAVE_SYSTEM_V |
| 1856 |
sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1857 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1858 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1869 |
{ |
{ |
| 1870 |
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
| 1871 |
} |
} |
| 1872 |
|
#else |
| 1873 |
|
if (sem_wait(&(p_section_list_pool->sem[index])) == -1) |
| 1874 |
|
{ |
| 1875 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1876 |
|
{ |
| 1877 |
|
log_error("sem_wait(sem[%d]) error %d\n", index, errno); |
| 1878 |
|
} |
| 1879 |
|
return -1; |
| 1880 |
|
} |
| 1881 |
|
|
| 1882 |
|
if (index != BBS_max_section) |
| 1883 |
|
{ |
| 1884 |
|
if (sem_wait(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1885 |
|
{ |
| 1886 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1887 |
|
{ |
| 1888 |
|
log_error("sem_wait(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1889 |
|
} |
| 1890 |
|
// release previously acquired lock |
| 1891 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1892 |
|
{ |
| 1893 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1894 |
|
} |
| 1895 |
|
return -1; |
| 1896 |
|
} |
| 1897 |
|
} |
| 1898 |
|
|
| 1899 |
|
if (p_section_list_pool->read_lock_count[index] > 0) |
| 1900 |
|
{ |
| 1901 |
|
p_section_list_pool->read_lock_count[index]--; |
| 1902 |
|
} |
| 1903 |
|
else |
| 1904 |
|
{ |
| 1905 |
|
log_error("read_lock_count[%d] already 0\n", index); |
| 1906 |
|
} |
| 1907 |
|
|
| 1908 |
|
if (index != BBS_max_section && p_section_list_pool->read_lock_count[BBS_max_section] > 0) |
| 1909 |
|
{ |
| 1910 |
|
p_section_list_pool->read_lock_count[BBS_max_section]--; |
| 1911 |
|
} |
| 1912 |
|
else |
| 1913 |
|
{ |
| 1914 |
|
log_error("read_lock_count[%d] already 0\n", BBS_max_section); |
| 1915 |
|
} |
| 1916 |
|
|
| 1917 |
|
if (index != BBS_max_section) |
| 1918 |
|
{ |
| 1919 |
|
// release lock on "all section" |
| 1920 |
|
if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 1921 |
|
{ |
| 1922 |
|
log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno); |
| 1923 |
|
ret = -1; |
| 1924 |
|
} |
| 1925 |
|
} |
| 1926 |
|
|
| 1927 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1928 |
|
{ |
| 1929 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1930 |
|
return -1; |
| 1931 |
|
} |
| 1932 |
|
#endif |
| 1933 |
|
|
| 1934 |
return ret; |
return ret; |
| 1935 |
} |
} |
| 1937 |
int section_list_rw_unlock(SECTION_LIST *p_section) |
int section_list_rw_unlock(SECTION_LIST *p_section) |
| 1938 |
{ |
{ |
| 1939 |
int index; |
int index; |
| 1940 |
|
#ifdef HAVE_SYSTEM_V |
| 1941 |
struct sembuf sops[1]; |
struct sembuf sops[1]; |
| 1942 |
int ret; |
#endif |
| 1943 |
|
int ret = 0; |
| 1944 |
|
|
| 1945 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1946 |
if (index < 0) |
if (index < 0) |
| 1948 |
return -2; |
return -2; |
| 1949 |
} |
} |
| 1950 |
|
|
| 1951 |
|
#ifdef HAVE_SYSTEM_V |
| 1952 |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1953 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1954 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1958 |
{ |
{ |
| 1959 |
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
| 1960 |
} |
} |
| 1961 |
|
#else |
| 1962 |
|
if (sem_wait(&(p_section_list_pool->sem[index])) == -1) |
| 1963 |
|
{ |
| 1964 |
|
if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR) |
| 1965 |
|
{ |
| 1966 |
|
log_error("sem_wait(sem[%d]) error %d\n", index, errno); |
| 1967 |
|
} |
| 1968 |
|
return -1; |
| 1969 |
|
} |
| 1970 |
|
|
| 1971 |
|
if (p_section_list_pool->write_lock_count[index] > 0) |
| 1972 |
|
{ |
| 1973 |
|
p_section_list_pool->write_lock_count[index]--; |
| 1974 |
|
} |
| 1975 |
|
else |
| 1976 |
|
{ |
| 1977 |
|
log_error("write_lock_count[%d] already 0\n", index); |
| 1978 |
|
} |
| 1979 |
|
|
| 1980 |
|
if (sem_post(&(p_section_list_pool->sem[index])) == -1) |
| 1981 |
|
{ |
| 1982 |
|
log_error("sem_post(sem[%d]) error %d\n", index, errno); |
| 1983 |
|
return -1; |
| 1984 |
|
} |
| 1985 |
|
#endif |
| 1986 |
|
|
| 1987 |
return ret; |
return ret; |
| 1988 |
} |
} |
| 1992 |
int timer = 0; |
int timer = 0; |
| 1993 |
int sid = (p_section == NULL ? 0 : p_section->sid); |
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1994 |
int ret = -1; |
int ret = -1; |
| 1995 |
|
time_t tm_first_failure = 0; |
| 1996 |
|
|
| 1997 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 1998 |
{ |
{ |
| 2003 |
} |
} |
| 2004 |
else if (errno == EAGAIN || errno == EINTR) // retry |
else if (errno == EAGAIN || errno == EINTR) // retry |
| 2005 |
{ |
{ |
| 2006 |
|
// Dead lock detection |
| 2007 |
|
if (tm_first_failure == 0) |
| 2008 |
|
{ |
| 2009 |
|
time(&tm_first_failure); |
| 2010 |
|
} |
| 2011 |
|
|
| 2012 |
timer++; |
timer++; |
| 2013 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 2014 |
{ |
{ |
| 2015 |
log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid); |
log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid); |
| 2016 |
|
if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT) |
| 2017 |
|
{ |
| 2018 |
|
log_error("Unable to acquire rd_lock for %d seconds\n", time(NULL) - tm_first_failure); |
| 2019 |
|
#ifndef HAVE_SYSTEM_V |
| 2020 |
|
section_list_reset_lock(p_section); |
| 2021 |
|
log_error("Reset POSIX semaphore to resolve dead lock\n"); |
| 2022 |
|
#endif |
| 2023 |
|
break; |
| 2024 |
|
} |
| 2025 |
} |
} |
| 2026 |
|
usleep(100 * 1000); // 0.1 second |
| 2027 |
} |
} |
| 2028 |
else // failed |
else // failed |
| 2029 |
{ |
{ |
| 2040 |
int timer = 0; |
int timer = 0; |
| 2041 |
int sid = (p_section == NULL ? 0 : p_section->sid); |
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 2042 |
int ret = -1; |
int ret = -1; |
| 2043 |
|
time_t tm_first_failure = 0; |
| 2044 |
|
|
| 2045 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 2046 |
{ |
{ |
| 2051 |
} |
} |
| 2052 |
else if (errno == EAGAIN || errno == EINTR) // retry |
else if (errno == EAGAIN || errno == EINTR) // retry |
| 2053 |
{ |
{ |
| 2054 |
|
// Dead lock detection |
| 2055 |
|
if (tm_first_failure == 0) |
| 2056 |
|
{ |
| 2057 |
|
time(&tm_first_failure); |
| 2058 |
|
} |
| 2059 |
|
|
| 2060 |
timer++; |
timer++; |
| 2061 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 2062 |
{ |
{ |
| 2063 |
log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid); |
log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid); |
| 2064 |
|
if (time(NULL) - tm_first_failure >= SECTION_DEAD_LOCK_TIMEOUT) |
| 2065 |
|
{ |
| 2066 |
|
log_error("Unable to acquire rw_lock for %d seconds\n", time(NULL) - tm_first_failure); |
| 2067 |
|
#ifndef HAVE_SYSTEM_V |
| 2068 |
|
section_list_reset_lock(p_section); |
| 2069 |
|
log_error("Reset POSIX semaphore to resolve dead lock\n"); |
| 2070 |
|
#endif |
| 2071 |
|
break; |
| 2072 |
|
} |
| 2073 |
} |
} |
| 2074 |
|
usleep(100 * 1000); // 0.1 second |
| 2075 |
} |
} |
| 2076 |
else // failed |
else // failed |
| 2077 |
{ |
{ |
| 2082 |
|
|
| 2083 |
return ret; |
return ret; |
| 2084 |
} |
} |
| 2085 |
|
|
| 2086 |
|
#ifndef HAVE_SYSTEM_V |
| 2087 |
|
int section_list_reset_lock(SECTION_LIST *p_section) |
| 2088 |
|
{ |
| 2089 |
|
int index; |
| 2090 |
|
|
| 2091 |
|
if (p_section == NULL) |
| 2092 |
|
{ |
| 2093 |
|
log_error("NULL pointer error\n"); |
| 2094 |
|
return -1; |
| 2095 |
|
} |
| 2096 |
|
|
| 2097 |
|
index = get_section_index(p_section); |
| 2098 |
|
if (index < 0) |
| 2099 |
|
{ |
| 2100 |
|
return -2; |
| 2101 |
|
} |
| 2102 |
|
|
| 2103 |
|
if (sem_destroy(&(p_section_list_pool->sem[index])) == -1) |
| 2104 |
|
{ |
| 2105 |
|
log_error("sem_destroy(sem[%d]) error (%d)\n", index, errno); |
| 2106 |
|
} |
| 2107 |
|
|
| 2108 |
|
p_section_list_pool->read_lock_count[index] = 0; |
| 2109 |
|
p_section_list_pool->write_lock_count[index] = 0; |
| 2110 |
|
|
| 2111 |
|
if (sem_init(&(p_section_list_pool->sem[index]), 1, 1) == -1) |
| 2112 |
|
{ |
| 2113 |
|
log_error("sem_init(sem[%d]) error (%d)\n", index, errno); |
| 2114 |
|
} |
| 2115 |
|
|
| 2116 |
|
if (index != BBS_max_section) |
| 2117 |
|
{ |
| 2118 |
|
if (sem_destroy(&(p_section_list_pool->sem[BBS_max_section])) == -1) |
| 2119 |
|
{ |
| 2120 |
|
log_error("sem_destroy(sem[%d]) error (%d)\n", BBS_max_section, errno); |
| 2121 |
|
} |
| 2122 |
|
|
| 2123 |
|
p_section_list_pool->read_lock_count[BBS_max_section] = 0; |
| 2124 |
|
p_section_list_pool->write_lock_count[BBS_max_section] = 0; |
| 2125 |
|
|
| 2126 |
|
if (sem_init(&(p_section_list_pool->sem[BBS_max_section]), 1, 1) == -1) |
| 2127 |
|
{ |
| 2128 |
|
log_error("sem_init(sem[%d]) error (%d)\n", BBS_max_section, errno); |
| 2129 |
|
} |
| 2130 |
|
} |
| 2131 |
|
|
| 2132 |
|
return 0; |
| 2133 |
|
} |
| 2134 |
|
#endif |