/[LeafOK_CVS]/lbbs/src/section_list.c
ViewVC logotype

Diff of /lbbs/src/section_list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.62 by sysadm, Thu Nov 20 03:38:49 2025 UTC Revision 1.63 by sysadm, Thu Nov 20 10:20:51 2025 UTC
# Line 441  extern int section_list_init(const char Line 441  extern int section_list_init(const char
441          int fd;          int fd;
442          size_t size;          size_t size;
443          void *p_shm;          void *p_shm;
444          int semid;  #ifdef HAVE_SYSTEM_V
445          int proj_id;          int proj_id;
446          key_t key;          key_t key;
447            int semid;
448          union semun arg;          union semun arg;
449    #endif
450          int i;          int i;
451    
452          if (p_section_list_pool != NULL)          if (p_section_list_pool != NULL)
# Line 496  extern int section_list_init(const char Line 498  extern int section_list_init(const char
498          p_section_list_pool->section_count = 0;          p_section_list_pool->section_count = 0;
499    
500          // Allocate semaphore as section locks          // Allocate semaphore as section locks
501    #ifndef HAVE_SYSTEM_V
502            for (i = 0; i <= BBS_max_section; i++)
503            {
504                    if (sem_init(&(p_section_list_pool->sem[i]), 1, 1) == -1)
505                    {
506                            log_error("sem_init(sem[%d]) error (%d)\n", i, errno);
507                            return -3;
508                    }
509    
510                    p_section_list_pool->read_lock_count[i] = 0;
511                    p_section_list_pool->write_lock_count[i] = 0;
512            }
513    #else
514          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
515          key = ftok(filename, proj_id);          key = ftok(filename, proj_id);
516          if (key == -1)          if (key == -1)
# Line 524  extern int section_list_init(const char Line 539  extern int section_list_init(const char
539          }          }
540    
541          p_section_list_pool->semid = semid;          p_section_list_pool->semid = semid;
542    #endif
543    
544          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();
545          if (p_section_list_pool->p_trie_dict_section_by_name == NULL)          if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
# Line 561  void section_list_cleanup(void) Line 577  void section_list_cleanup(void)
577                  p_section_list_pool->p_trie_dict_section_by_sid = NULL;                  p_section_list_pool->p_trie_dict_section_by_sid = NULL;
578          }          }
579    
580    #ifdef HAVE_SYSTEM_V
581          if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)          if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
582          {          {
583                  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);
584          }          }
585    #else
586            for (int i = 0; i <= BBS_max_section; i++)
587            {
588                    if (sem_destroy(&(p_section_list_pool->sem[i])) == -1)
589                    {
590                            log_error("sem_destroy(sem[%d]) error (%d)\n", i, errno);
591                    }
592            }
593    #endif
594    
595          detach_section_list_shm();          detach_section_list_shm();
596    
# Line 1636  int get_section_info(SECTION_LIST *p_sec Line 1662  int get_section_info(SECTION_LIST *p_sec
1662  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)
1663  {  {
1664          int index;          int index;
1665    #ifdef HAVE_SYSTEM_V
1666          struct sembuf sops[4];          struct sembuf sops[4];
 #ifndef __CYGWIN__  
         struct timespec timeout;  
1667  #endif  #endif
1668          int ret;          struct timespec timeout;
1669            int ret = 0;
1670    
1671          index = get_section_index(p_section);          index = get_section_index(p_section);
1672          if (index < 0)          if (index < 0)
# Line 1648  int section_list_try_rd_lock(SECTION_LIS Line 1674  int section_list_try_rd_lock(SECTION_LIS
1674                  return -2;                  return -2;
1675          }          }
1676    
1677            timeout.tv_sec = wait_sec;
1678            timeout.tv_nsec = 0;
1679    
1680    #ifdef HAVE_SYSTEM_V
1681          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
1682          sops[0].sem_op = 0;                                                                // wait until unlocked          sops[0].sem_op = 0;                                                                // wait until unlocked
1683          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
# Line 1670  int section_list_try_rd_lock(SECTION_LIS Line 1700  int section_list_try_rd_lock(SECTION_LIS
1700                  sops[3].sem_flg = SEM_UNDO;                        // undo on terminate                  sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1701          }          }
1702    
 #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;  
   
1703          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  
1704          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1705          {          {
1706                  log_error("semop(index = %d, lock read) error %d\n", index, errno);                  log_error("semop(index = %d, lock read) error %d\n", index, errno);
1707          }          }
1708    #else
1709            if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1)
1710            {
1711                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1712                    {
1713                            log_error("sem_timedwait(sem[%d]) error %d\n", index, errno);
1714                    }
1715                    return -1;
1716            }
1717    
1718            if (index != BBS_max_section)
1719            {
1720                    if (sem_timedwait(&(p_section_list_pool->sem[BBS_max_section]), &timeout) == -1)
1721                    {
1722                            if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1723                            {
1724                                    log_error("sem_timedwait(sem[%d]) error %d\n", BBS_max_section, errno);
1725                            }
1726                            // release previously acquired lock
1727                            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1728                            {
1729                                    log_error("sem_post(sem[%d]) error %d\n", index, errno);
1730                            }
1731                            return -1;
1732                    }
1733            }
1734    
1735            if (p_section_list_pool->write_lock_count[index] == 0 &&
1736                    (index != BBS_max_section && p_section_list_pool->write_lock_count[BBS_max_section] == 0))
1737            {
1738                    p_section_list_pool->read_lock_count[index]++;
1739                    if (index != BBS_max_section)
1740                    {
1741                            p_section_list_pool->read_lock_count[BBS_max_section]++;
1742                    }
1743            }
1744            else
1745            {
1746                    errno = EAGAIN;
1747                    ret = -1;
1748            }
1749    
1750            if (index != BBS_max_section)
1751            {
1752                    // release lock on "all section"
1753                    if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1)
1754                    {
1755                            log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno);
1756                            ret = -1;
1757                    }
1758            }
1759    
1760            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1761            {
1762                    log_error("sem_post(sem[%d]) error %d\n", index, errno);
1763                    return -1;
1764            }
1765    #endif
1766    
1767          return ret;          return ret;
1768  }  }
# Line 1689  int section_list_try_rd_lock(SECTION_LIS Line 1770  int section_list_try_rd_lock(SECTION_LIS
1770  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)
1771  {  {
1772          int index;          int index;
1773    #ifdef HAVE_SYSTEM_V
1774          struct sembuf sops[3];          struct sembuf sops[3];
 #ifndef __CYGWIN__  
         struct timespec timeout;  
1775  #endif  #endif
1776          int ret;          struct timespec timeout;
1777            int ret = 0;
1778    
1779          index = get_section_index(p_section);          index = get_section_index(p_section);
1780          if (index < 0)          if (index < 0)
# Line 1701  int section_list_try_rw_lock(SECTION_LIS Line 1782  int section_list_try_rw_lock(SECTION_LIS
1782                  return -2;                  return -2;
1783          }          }
1784    
1785            timeout.tv_sec = wait_sec;
1786            timeout.tv_nsec = 0;
1787    
1788    #ifdef HAVE_SYSTEM_V
1789          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
1790          sops[0].sem_op = 0;                                                                // wait until unlocked          sops[0].sem_op = 0;                                                                // wait until unlocked
1791          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
# Line 1713  int section_list_try_rw_lock(SECTION_LIS Line 1798  int section_list_try_rw_lock(SECTION_LIS
1798          sops[2].sem_op = 0;                                                        // wait until unlocked          sops[2].sem_op = 0;                                                        // wait until unlocked
1799          sops[2].sem_flg = 0;          sops[2].sem_flg = 0;
1800    
 #ifdef __CYGWIN__  
         ret = semop(p_section_list_pool->semid, sops, 3);  
 #else  
         timeout.tv_sec = wait_sec;  
         timeout.tv_nsec = 0;  
   
1801          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
 #endif  
1802          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1803          {          {
1804                  log_error("semop(index = %d, lock write) error %d\n", index, errno);                  log_error("semop(index = %d, lock write) error %d\n", index, errno);
1805          }          }
1806    #else
1807            if (sem_timedwait(&(p_section_list_pool->sem[index]), &timeout) == -1)
1808            {
1809                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1810                    {
1811                            log_error("sem_timedwait(sem[%d]) error %d\n", index, errno);
1812                    }
1813                    return -1;
1814            }
1815    
1816            if (p_section_list_pool->read_lock_count[index] == 0 && p_section_list_pool->write_lock_count[index] == 0)
1817            {
1818                    p_section_list_pool->write_lock_count[index]++;
1819            }
1820            else
1821            {
1822                    errno = EAGAIN;
1823                    ret = -1;
1824            }
1825    
1826            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1827            {
1828                    log_error("sem_post(sem[%d]) error %d\n", index, errno);
1829                    return -1;
1830            }
1831    #endif
1832    
1833          return ret;          return ret;
1834  }  }
# Line 1732  int section_list_try_rw_lock(SECTION_LIS Line 1836  int section_list_try_rw_lock(SECTION_LIS
1836  int section_list_rd_unlock(SECTION_LIST *p_section)  int section_list_rd_unlock(SECTION_LIST *p_section)
1837  {  {
1838          int index;          int index;
1839    #ifdef HAVE_SYSTEM_V
1840          struct sembuf sops[2];          struct sembuf sops[2];
1841          int ret;  #endif
1842            int ret = 0;
1843    
1844          index = get_section_index(p_section);          index = get_section_index(p_section);
1845          if (index < 0)          if (index < 0)
# Line 1741  int section_list_rd_unlock(SECTION_LIST Line 1847  int section_list_rd_unlock(SECTION_LIST
1847                  return -2;                  return -2;
1848          }          }
1849    
1850    #ifdef HAVE_SYSTEM_V
1851          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
1852          sops[0].sem_op = -1;                                               // unlock          sops[0].sem_op = -1;                                               // unlock
1853          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
# Line 1757  int section_list_rd_unlock(SECTION_LIST Line 1864  int section_list_rd_unlock(SECTION_LIST
1864          {          {
1865                  log_error("semop(index = %d, unlock read) error %d\n", index, errno);                  log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1866          }          }
1867    #else
1868            if (sem_wait(&(p_section_list_pool->sem[index])) == -1)
1869            {
1870                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1871                    {
1872                            log_error("sem_wait(sem[%d]) error %d\n", index, errno);
1873                    }
1874                    return -1;
1875            }
1876    
1877            if (index != BBS_max_section)
1878            {
1879                    if (sem_wait(&(p_section_list_pool->sem[BBS_max_section])) == -1)
1880                    {
1881                            if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1882                            {
1883                                    log_error("sem_wait(sem[%d]) error %d\n", BBS_max_section, errno);
1884                            }
1885                            // release previously acquired lock
1886                            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1887                            {
1888                                    log_error("sem_post(sem[%d]) error %d\n", index, errno);
1889                            }
1890                            return -1;
1891                    }
1892            }
1893    
1894            if (p_section_list_pool->read_lock_count[index] > 0)
1895            {
1896                    p_section_list_pool->read_lock_count[index]--;
1897            }
1898            else
1899            {
1900                    log_error("read_lock_count[%d] already 0\n", index);
1901            }
1902    
1903            if (index != BBS_max_section && p_section_list_pool->read_lock_count[BBS_max_section] > 0)
1904            {
1905                    p_section_list_pool->read_lock_count[BBS_max_section]--;
1906            }
1907            else
1908            {
1909                    log_error("read_lock_count[%d] already 0\n", BBS_max_section);
1910            }
1911    
1912            if (index != BBS_max_section)
1913            {
1914                    // release lock on "all section"
1915                    if (sem_post(&(p_section_list_pool->sem[BBS_max_section])) == -1)
1916                    {
1917                            log_error("sem_post(sem[%d]) error %d\n", BBS_max_section, errno);
1918                            ret = -1;
1919                    }
1920            }
1921    
1922            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1923            {
1924                    log_error("sem_post(sem[%d]) error %d\n", index, errno);
1925                    return -1;
1926            }
1927    #endif
1928    
1929          return ret;          return ret;
1930  }  }
# Line 1764  int section_list_rd_unlock(SECTION_LIST Line 1932  int section_list_rd_unlock(SECTION_LIST
1932  int section_list_rw_unlock(SECTION_LIST *p_section)  int section_list_rw_unlock(SECTION_LIST *p_section)
1933  {  {
1934          int index;          int index;
1935    #ifdef HAVE_SYSTEM_V
1936          struct sembuf sops[1];          struct sembuf sops[1];
1937          int ret;  #endif
1938            int ret = 0;
1939    
1940          index = get_section_index(p_section);          index = get_section_index(p_section);
1941          if (index < 0)          if (index < 0)
# Line 1773  int section_list_rw_unlock(SECTION_LIST Line 1943  int section_list_rw_unlock(SECTION_LIST
1943                  return -2;                  return -2;
1944          }          }
1945    
1946    #ifdef HAVE_SYSTEM_V
1947          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
1948          sops[0].sem_op = -1;                                                       // unlock          sops[0].sem_op = -1;                                                       // unlock
1949          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
# Line 1782  int section_list_rw_unlock(SECTION_LIST Line 1953  int section_list_rw_unlock(SECTION_LIST
1953          {          {
1954                  log_error("semop(index = %d, unlock write) error %d\n", index, errno);                  log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1955          }          }
1956    #else
1957            if (sem_wait(&(p_section_list_pool->sem[index])) == -1)
1958            {
1959                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
1960                    {
1961                            log_error("sem_wait(sem[%d]) error %d\n", index, errno);
1962                    }
1963                    return -1;
1964            }
1965    
1966            if (p_section_list_pool->write_lock_count[index] > 0)
1967            {
1968                    p_section_list_pool->write_lock_count[index]--;
1969            }
1970            else
1971            {
1972                    log_error("write_lock_count[%d] already 0\n", index);
1973            }
1974    
1975            if (sem_post(&(p_section_list_pool->sem[index])) == -1)
1976            {
1977                    log_error("sem_post(sem[%d]) error %d\n", index, errno);
1978                    return -1;
1979            }
1980    #endif
1981    
1982          return ret;          return ret;
1983  }  }
# Line 1806  int section_list_rd_lock(SECTION_LIST *p Line 2002  int section_list_rd_lock(SECTION_LIST *p
2002                          {                          {
2003                                  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);
2004                          }                          }
2005                            usleep(100 * 1000); // 0.1 second
2006                  }                  }
2007                  else // failed                  else // failed
2008                  {                  {
# Line 1837  int section_list_rw_lock(SECTION_LIST *p Line 2034  int section_list_rw_lock(SECTION_LIST *p
2034                          {                          {
2035                                  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);
2036                          }                          }
2037                            usleep(100 * 1000); // 0.1 second
2038                  }                  }
2039                  else // failed                  else // failed
2040                  {                  {


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1