/[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.22 by sysadm, Sun May 25 23:47:36 2025 UTC Revision 1.29 by sysadm, Wed May 28 07:30:23 2025 UTC
# Line 41  union semun Line 41  union semun
41  };  };
42  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #ifdef _SEM_SEMUN_UNDEFINED
43    
44    #define SECTION_TRY_LOCK_WAIT_TIME 1 // second
45    #define SECTION_TRY_LOCK_TIMES 10
46    
47  #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
48  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // 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)
49  #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)
# Line 105  int article_block_init(const char *filen Line 108  int article_block_init(const char *filen
108                  return -1;                  return -1;
109          }          }
110    
111          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
112          {          {
113                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);
114                  return -2;                  return -2;
# Line 233  void article_block_cleanup(void) Line 236  void article_block_cleanup(void)
236          p_article_block_pool = NULL;          p_article_block_pool = NULL;
237  }  }
238    
239    int set_article_block_shm_readonly(void)
240    {
241            int shmid;
242            void *p_shm;
243            int i;
244    
245            if (p_article_block_pool == NULL)
246            {
247                    log_error("article_block_pool not initialized\n");
248                    return -1;
249            }
250    
251            for (i = 0; i < p_article_block_pool->shm_count; i++)
252            {
253                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
254    
255                    // Remap shared memory in read-only mode
256                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
257                    if (p_shm == (void *)-1)
258                    {
259                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
260                            return -2;
261                    }
262            }
263    
264            return 0;
265    }
266    
267    int detach_article_block_shm(void)
268    {
269            int shmid;
270    
271            if (p_article_block_pool == NULL)
272            {
273                    return -1;
274            }
275    
276            for (int i = 0; i < p_article_block_pool->shm_count; i++)
277            {
278                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
279                    {
280                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
281                            return -2;
282                    }
283            }
284    
285            shmid = p_article_block_pool->shmid;
286    
287            if (shmdt(p_article_block_pool) == -1)
288            {
289                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
290                    return -3;
291            }
292    
293            p_article_block_pool = NULL;
294    
295            return 0;
296    }
297    
298  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
299  {  {
300          ARTICLE_BLOCK *p_block = NULL;          ARTICLE_BLOCK *p_block = NULL;
# Line 337  ARTICLE *article_block_find_by_aid(int32 Line 399  ARTICLE *article_block_find_by_aid(int32
399                  }                  }
400          }          }
401    
402          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
403            {
404                    return NULL;
405            }
406    
407            return (p_block->articles + left); // found
408  }  }
409    
410  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 449  extern int section_list_init(const char Line 516  extern int section_list_init(const char
516          return 0;          return 0;
517  }  }
518    
519    void section_list_cleanup(void)
520    {
521            int shmid;
522    
523            if (p_section_list_pool == NULL)
524            {
525                    return;
526            }
527    
528            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
529            {
530                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
531                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
532            }
533    
534            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
535            {
536                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
537                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
538            }
539    
540            shmid = p_section_list_pool->shmid;
541    
542            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
543            {
544                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
545            }
546    
547            if (shmdt(p_section_list_pool) == -1)
548            {
549                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
550            }
551    
552            if (shmctl(shmid, IPC_RMID, NULL) == -1)
553            {
554                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
555            }
556    
557            p_section_list_pool = NULL;
558    }
559    
560    int set_section_list_shm_readonly(void)
561    {
562            int shmid;
563            void *p_shm;
564    
565            if (p_section_list_pool == NULL)
566            {
567                    log_error("p_section_list_pool not initialized\n");
568                    return -1;
569            }
570    
571            shmid = p_section_list_pool->shmid;
572    
573            // Remap shared memory in read-only mode
574            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
575            if (p_shm == (void *)-1)
576            {
577                    log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
578                    return -3;
579            }
580    
581            p_section_list_pool = p_shm;
582    
583            return 0;
584    }
585    
586    int detach_section_list_shm(void)
587    {
588            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
589            {
590                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
591                    return -1;
592            }
593    
594            p_section_list_pool = NULL;
595    
596            return 0;
597    }
598    
599  inline static void sid_to_str(int32_t sid, char *p_sid_str)  inline static void sid_to_str(int32_t sid, char *p_sid_str)
600  {  {
601          uint32_t u_sid;          uint32_t u_sid;
# Line 486  SECTION_LIST *section_list_create(int32_ Line 633  SECTION_LIST *section_list_create(int32_
633    
634          p_section->sid = sid;          p_section->sid = sid;
635    
636          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
637          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
638    
639          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
640          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
641    
642          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));          strncpy(p_section->master_list, master_name, sizeof(p_section->master_list) - 1);
643          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
644    
645          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
646          {          {
# Line 527  void section_list_reset_articles(SECTION Line 674  void section_list_reset_articles(SECTION
674          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
675  }  }
676    
 void section_list_cleanup(void)  
 {  
         int shmid;  
   
         if (p_section_list_pool == NULL)  
         {  
                 return;  
         }  
   
         if (p_section_list_pool->p_trie_dict_section_by_name != NULL)  
         {  
                 trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);  
                 p_section_list_pool->p_trie_dict_section_by_name = NULL;  
         }  
   
         if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)  
         {  
                 trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);  
                 p_section_list_pool->p_trie_dict_section_by_sid = NULL;  
         }  
   
         if (p_section_list_pool != NULL)  
         {  
                 if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)  
                 {  
                         log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);  
                 }  
   
                 shmid = p_section_list_pool->shmid;  
   
                 if (shmdt(p_section_list_pool) == -1)  
                 {  
                         log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);  
                 }  
   
                 if (shmctl(shmid, IPC_RMID, NULL) == -1)  
                 {  
                         log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);  
                 }  
   
                 p_section_list_pool = NULL;  
         }  
 }  
   
677  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
678  {  {
679          int64_t index;          int64_t index;
680            int ret;
681    
682          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
683          {          {
# Line 581  SECTION_LIST *section_list_find_by_name( Line 685  SECTION_LIST *section_list_find_by_name(
685                  return NULL;                  return NULL;
686          }          }
687    
688          if (trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index) != 1)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
689            if (ret < 0)
690          {          {
691                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
692                  return NULL;                  return NULL;
693          }          }
694            else if (ret == 0)
695            {
696                    return NULL;
697            }
698    
699          return (p_section_list_pool->sections + index);          return (p_section_list_pool->sections + index);
700  }  }
# Line 593  SECTION_LIST *section_list_find_by_name( Line 702  SECTION_LIST *section_list_find_by_name(
702  SECTION_LIST *section_list_find_by_sid(int32_t sid)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
703  {  {
704          int64_t index;          int64_t index;
705            int ret;
706          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
707    
708          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
# Line 603  SECTION_LIST *section_list_find_by_sid(i Line 713  SECTION_LIST *section_list_find_by_sid(i
713    
714          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
715    
716          if (trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index) != 1)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
717            if (ret < 0)
718          {          {
719                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error\n", sid);
720                  return NULL;                  return NULL;
721          }          }
722            else if (ret == 0)
723            {
724                    return NULL;
725            }
726    
727          return (p_section_list_pool->sections + index);          return (p_section_list_pool->sections + index);
728  }  }
# Line 1020  int section_list_calculate_page(SECTION_ Line 1135  int section_list_calculate_page(SECTION_
1135          return 0;          return 0;
1136  }  }
1137    
1138    int32_t article_block_last_aid(void)
1139    {
1140            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1141            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1142    
1143            return last_aid;
1144    }
1145    
1146    int article_block_article_count(void)
1147    {
1148            int ret;
1149    
1150            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1151            {
1152                    return -1;
1153            }
1154    
1155            ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +
1156                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1157    
1158            return ret;
1159    }
1160    
1161  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1162  {  {
1163          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1060  int section_list_move_topic(SECTION_LIST Line 1198  int section_list_move_topic(SECTION_LIST
1198          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1199          int move_counter;          int move_counter;
1200    
1201          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1202          {          {
1203                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("section_list_move_topic() NULL pointer error\n");
1204                  return -1;                  return -1;
1205          }          }
1206    
1207            if (p_section_src->sid == p_section_dest->sid)
1208            {
1209                    log_error("section_list_move_topic() src and dest section are the same\n");
1210                    return -1;
1211            }
1212    
1213          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1214          {          {
1215                  log_error("section_list_move_topic() error: article %d not found in block\n", aid);                  log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);
1216                  return -2;                  return -2;
1217          }          }
1218    
# Line 1109  int section_list_move_topic(SECTION_LIST Line 1253  int section_list_move_topic(SECTION_LIST
1253          {          {
1254                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1255                  {                  {
1256                          log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",                          log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n",
1257                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1258                          return -2;                          p_article = p_article->p_topic_next;
1259                            continue;
1260                  }                  }
1261    
1262                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1137  int section_list_move_topic(SECTION_LIST Line 1282  int section_list_move_topic(SECTION_LIST
1282    
1283                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1284                  {                  {
1285                          log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);                          log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1286                          return -4;                          p_article = p_article->p_topic_next;
1287                            continue;
1288                  }                  }
1289    
1290                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1216  int section_list_move_topic(SECTION_LIST Line 1362  int section_list_move_topic(SECTION_LIST
1362    
1363          if (p_section_dest->article_count - dest_article_count_old != move_article_count)          if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1364          {          {
1365                  log_error("section_list_move_topic() error: count of moved articles %d != %d\n",                  log_error("section_list_move_topic() warning: count of moved articles %d != %d\n",
1366                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1367          }          }
1368    
# Line 1406  int section_list_rw_unlock(SECTION_LIST Line 1552  int section_list_rw_unlock(SECTION_LIST
1552          }          }
1553    
1554          return ret;          return ret;
1555    }
1556    
1557    int section_list_rd_lock(SECTION_LIST *p_section)
1558    {
1559            int timer = 0;
1560            int sid = (p_section == NULL ? 0 : p_section->sid);
1561            int ret = -1;
1562    
1563            while (!SYS_server_exit)
1564            {
1565                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1566                    if (ret == 0) // success
1567                    {
1568                            break;
1569                    }
1570                    else if (errno == EAGAIN || errno == EINTR) // retry
1571                    {
1572                            timer++;
1573                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1574                            {
1575                                    log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer);
1576                            }
1577                    }
1578                    else // failed
1579                    {
1580                            log_error("section_list_rd_lock() failed on section %d\n", sid);
1581                            break;
1582                    }
1583            }
1584    
1585            return ret;
1586    }
1587    
1588    int section_list_rw_lock(SECTION_LIST *p_section)
1589    {
1590            int timer = 0;
1591            int sid = (p_section == NULL ? 0 : p_section->sid);
1592            int ret = -1;
1593    
1594            while (!SYS_server_exit)
1595            {
1596                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1597                    if (ret == 0) // success
1598                    {
1599                            break;
1600                    }
1601                    else if (errno == EAGAIN || errno == EINTR) // retry
1602                    {
1603                            timer++;
1604                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1605                            {
1606                                    log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer);
1607                            }
1608                    }
1609                    else // failed
1610                    {
1611                            log_error("acquire_section_rw_lock() failed on section %d\n", sid);
1612                            break;
1613                    }
1614            }
1615    
1616            return ret;
1617  }  }


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

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