/[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.16 by sysadm, Sat May 24 07:32:46 2025 UTC Revision 1.19 by sysadm, Sun May 25 06:56:48 2025 UTC
# Line 30  Line 30 
30  #include <sys/shm.h>  #include <sys/shm.h>
31  #include <sys/ipc.h>  #include <sys/ipc.h>
32    
33    #ifdef _SEM_SEMUN_UNDEFINED
34    union semun
35    {
36            int val;                           /* Value for SETVAL */
37            struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
38            unsigned short *array; /* Array for GETALL, SETALL */
39            struct seminfo *__buf; /* Buffer for IPC_INFO
40                                                              (Linux-specific) */
41    };
42    #endif // #ifdef _SEM_SEMUN_UNDEFINED
43    
44  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
45  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id)
46  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)
47    
48  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic
# Line 63  struct article_block_pool_t Line 74  struct article_block_pool_t
74  };  };
75  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
76    
77    static int article_block_pool_shmid;
78  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
79    
 static int section_list_pool_shmid;  
80  static int section_list_pool_semid;  static int section_list_pool_semid;
81    static int section_list_pool_shmid;
82  static SECTION_LIST *p_section_list_pool = NULL;  static SECTION_LIST *p_section_list_pool = NULL;
83    
84  static int section_list_count = 0;  static int section_list_count = 0;
85  static TRIE_NODE *p_trie_dict_section_by_name = NULL;  static TRIE_NODE *p_trie_dict_section_by_name = NULL;
86  static TRIE_NODE *p_trie_dict_section_by_sid = NULL;  static TRIE_NODE *p_trie_dict_section_by_sid = NULL;
# Line 96  int article_block_init(const char *filen Line 109  int article_block_init(const char *filen
109                  return -2;                  return -2;
110          }          }
111    
112          p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL));          // Allocate shared memory
113          if (p_article_block_pool == NULL)          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm
114            key = ftok(filename, proj_id);
115            if (key == -1)
116          {          {
117                  log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n");                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
118                  return -2;                  return -3;
119          }          }
120    
121          // Allocate shared memory          size = sizeof(ARTICLE_BLOCK_POOL);
122            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
123            if (shmid == -1)
124            {
125                    log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);
126                    return -3;
127            }
128            p_shm = shmat(shmid, NULL, 0);
129            if (p_shm == (void *)-1)
130            {
131                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
132                    return -3;
133            }
134    
135            article_block_pool_shmid = shmid;
136            p_article_block_pool = p_shm;
137    
138          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
139          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
140    
# Line 112  int article_block_init(const char *filen Line 143  int article_block_init(const char *filen
143                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
144                  block_count -= block_count_in_shm;                  block_count -= block_count_in_shm;
145    
146                  proj_id = getpid() + p_article_block_pool->shm_count;                  proj_id = p_article_block_pool->shm_count;
147                  key = ftok(filename, proj_id);                  key = ftok(filename, proj_id);
148                  if (key == -1)                  if (key == -1)
149                  {                  {
# Line 121  int article_block_init(const char *filen Line 152  int article_block_init(const char *filen
152                          return -3;                          return -3;
153                  }                  }
154    
155                  size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
156                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
157                  if (shmid == -1)                  if (shmid == -1)
158                  {                  {
# Line 165  int article_block_init(const char *filen Line 196  int article_block_init(const char *filen
196    
197  void article_block_cleanup(void)  void article_block_cleanup(void)
198  {  {
199          if (p_article_block_pool != NULL)          if (p_article_block_pool == NULL)
200            {
201                    return;
202            }
203    
204            for (int i = 0; i < p_article_block_pool->shm_count; i++)
205          {          {
206                  for (int i = 0; i < p_article_block_pool->shm_count; i++)                  if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
207                  {                  {
208                          if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)                          log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
209                          {                  }
                                 log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);  
                         }  
210    
211                          if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)                  if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)
212                          {                  {
213                                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);                          log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
                         }  
214                  }                  }
215            }
216    
217                  free(p_article_block_pool);          if (shmdt(p_article_block_pool) == -1)
218                  p_article_block_pool = NULL;          {
219                    log_error("shmdt(shmid = %d) error (%d)\n", article_block_pool_shmid, errno);
220          }          }
221    
222            if (shmctl(article_block_pool_shmid, IPC_RMID, NULL) == -1)
223            {
224                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", article_block_pool_shmid, errno);
225            }
226    
227            p_article_block_pool = NULL;
228  }  }
229    
230  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 319  ARTICLE *article_block_find_by_index(int Line 361  ARTICLE *article_block_find_by_index(int
361          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % ARTICLE_PER_BLOCK));
362  }  }
363    
364  extern int section_list_pool_init(const char *filename)  extern int section_list_init(const char *filename)
365  {  {
366          int semid;          int semid;
367          int shmid;          int shmid;
# Line 327  extern int section_list_pool_init(const Line 369  extern int section_list_pool_init(const
369          key_t key;          key_t key;
370          size_t size;          size_t size;
371          void *p_shm;          void *p_shm;
372            union semun arg;
373            int i;
374    
375          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL)
376          {          {
377                  section_list_pool_cleanup();                  section_list_cleanup();
         }  
   
         p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST));  
         if (p_section_list_pool == NULL)  
         {  
                 log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);  
                 return -1;  
378          }          }
379    
380          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
# Line 352  extern int section_list_pool_init(const Line 389  extern int section_list_pool_init(const
389          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
390          if (semid == -1)          if (semid == -1)
391          {          {
392                  log_error("semget(section_list_pool, size = %d) error (%d)\n", size, errno);                  log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
393                  return -3;                  return -3;
394          }          }
395    
396            // Initialize sem value to 0
397            arg.val = 0;
398            for (i = 0; i < size; i++)
399            {
400                    if (semctl(semid, i, SETVAL, arg) == -1)
401                    {
402                            log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
403                            return -3;
404                    }
405            }
406    
407          section_list_pool_semid = semid;          section_list_pool_semid = semid;
408    
409          size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section;          size = sizeof(SECTION_LIST) * BBS_max_section;
410          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
411          if (shmid == -1)          if (shmid == -1)
412          {          {
413                  log_error("shmget(section_list_pool, size = %d) error (%d)\n", size, errno);                  log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
414                  return -3;                  return -3;
415          }          }
416          p_shm = shmat(shmid, NULL, 0);          p_shm = shmat(shmid, NULL, 0);
# Line 472  void section_list_reset_articles(SECTION Line 520  void section_list_reset_articles(SECTION
520          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
521  }  }
522    
523  void section_list_pool_cleanup(void)  void section_list_cleanup(void)
524  {  {
525          if (p_trie_dict_section_by_name != NULL)          if (p_trie_dict_section_by_name != NULL)
526          {          {
# Line 1207  int get_section_index(SECTION_LIST *p_se Line 1255  int get_section_index(SECTION_LIST *p_se
1255  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)
1256  {  {
1257          int index;          int index;
1258          struct sembuf sops[2];          struct sembuf sops[4];
1259          struct timespec timeout;          struct timespec timeout;
1260          int ret;          int ret;
1261    
# Line 1217  int section_list_try_rd_lock(SECTION_LIS Line 1265  int section_list_try_rd_lock(SECTION_LIS
1265                  return -2;                  return -2;
1266          }          }
1267    
1268          sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index          sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1269          sops[0].sem_op = 0;                                                      // check if unlocked          sops[0].sem_op = 0;                                                                // wait until unlocked
1270          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
1271    
1272          sops[1].sem_num = (unsigned short)index; // r_sem of section index          sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1273          sops[1].sem_op = 1;                                              // lock          sops[1].sem_op = 1;                                                        // lock
1274          sops[1].sem_flg = SEM_UNDO;                              // undo on terminate          sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1275    
1276            // Read lock on any specific section will also acquire single read lock on "all section"
1277            // so that write lock on all section only need to acquire single write on on "all section"
1278            // rather than to acquire multiple write locks on all the available sections.
1279            if (index == BBS_max_section)
1280            {
1281                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1282                    sops[2].sem_op = 0;                                                // wait until unlocked
1283                    sops[2].sem_flg = 0;
1284    
1285                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1286                    sops[3].sem_op = 1;                                        // lock
1287                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1288            }
1289    
1290          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1291          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1292    
1293          ret = semtimedop(section_list_pool_semid, sops, 2, &timeout);          ret = semtimedop(section_list_pool_semid, sops, (index == BBS_max_section ? 4 : 2), &timeout);
1294            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1295            {
1296                    log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1297            }
1298    
1299          return ret;          return ret;
1300  }  }
# Line 1246  int section_list_try_rw_lock(SECTION_LIS Line 1312  int section_list_try_rw_lock(SECTION_LIS
1312                  return -2;                  return -2;
1313          }          }
1314    
1315          sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index          sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1316          sops[0].sem_op = 0;                                                      // check if unlocked          sops[0].sem_op = 0;                                                                // wait until unlocked
1317          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
1318    
1319          sops[1].sem_num = (unsigned short)index + 1; // w_sem of section index          sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1320          sops[1].sem_op = 1;                                                      // lock          sops[1].sem_op = 1;                                                                // lock
1321          sops[1].sem_flg = SEM_UNDO;                                      // undo on terminate          sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1322    
1323          sops[2].sem_num = (unsigned short)index; // r_sem of section index          sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1324          sops[1].sem_op = 0;                                              // wait until unlocked          sops[2].sem_op = 0;                                                        // wait until unlocked
1325          sops[1].sem_flg = 0;          sops[2].sem_flg = 0;
1326    
1327          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1328          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1329    
1330          ret = semtimedop(section_list_pool_semid, sops, 3, &timeout);          ret = semtimedop(section_list_pool_semid, sops, 3, &timeout);
1331            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1332            {
1333                    log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1334            }
1335    
1336          return ret;          return ret;
1337  }  }
# Line 1269  int section_list_try_rw_lock(SECTION_LIS Line 1339  int section_list_try_rw_lock(SECTION_LIS
1339  int section_list_rd_unlock(SECTION_LIST *p_section)  int section_list_rd_unlock(SECTION_LIST *p_section)
1340  {  {
1341          int index;          int index;
1342          struct sembuf sops[1];          struct sembuf sops[2];
1343          int ret;          int ret;
1344    
1345          index = get_section_index(p_section);          index = get_section_index(p_section);
# Line 1278  int section_list_rd_unlock(SECTION_LIST Line 1348  int section_list_rd_unlock(SECTION_LIST
1348                  return -2;                  return -2;
1349          }          }
1350    
1351          sops[0].sem_num = (unsigned short)index; // r_sem of section index          sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1352          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                               // unlock
1353          sops[0].sem_flg = IPC_NOWAIT;                    // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1354    
1355          ret = semop(section_list_pool_semid, sops, 1);          // The same reason as section_list_try_rd_lock()
1356            if (index == BBS_max_section)
1357            {
1358                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1359                    sops[1].sem_op = -1;                                     // unlock
1360                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1361            }
1362    
1363            ret = semop(section_list_pool_semid, sops, (index == BBS_max_section ? 2 : 1));
1364            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1365            {
1366                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1367            }
1368    
1369          return ret;          return ret;
1370  }  }
# Line 1299  int section_list_rw_unlock(SECTION_LIST Line 1381  int section_list_rw_unlock(SECTION_LIST
1381                  return -2;                  return -2;
1382          }          }
1383    
1384          sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index          sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1385          sops[0].sem_op = -1;                                             // unlock          sops[0].sem_op = -1;                                                       // unlock
1386          sops[0].sem_flg = IPC_NOWAIT;                            // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1387    
1388          ret = semop(section_list_pool_semid, sops, 1);          ret = semop(section_list_pool_semid, sops, 1);
1389            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1390            {
1391                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1392            }
1393    
1394          return ret;          return ret;
1395  }  }


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

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