/[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.13 by sysadm, Fri May 23 14:04:05 2025 UTC Revision 1.26 by sysadm, Tue May 27 00:54:01 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _GNU_SOURCE
18    
19  #include "section_list.h"  #include "section_list.h"
20  #include "log.h"  #include "log.h"
21  #include "trie_dict.h"  #include "trie_dict.h"
# Line 24  Line 26 
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <errno.h>  #include <errno.h>
28  #include <sys/param.h>  #include <sys/param.h>
29    #include <sys/sem.h>
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 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 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)
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)
50    
51  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic
52    
53    #define SID_STR_LEN 5 // 32-bit + NULL
54    
55  struct article_block_t  struct article_block_t
56  {  {
57          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[ARTICLE_PER_BLOCK];
58          int32_t article_count;          int article_count;
59          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
60  };  };
61  typedef struct article_block_t ARTICLE_BLOCK;  typedef struct article_block_t ARTICLE_BLOCK;
62    
 struct article_block_shm_t  
 {  
         int shmid;  
         void *p_shm;  
 };  
 typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  
   
63  struct article_block_pool_t  struct article_block_pool_t
64  {  {
65          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];          int shmid;
66            struct
67            {
68                    int shmid;
69                    void *p_shm;
70            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
71          int shm_count;          int shm_count;
72          ARTICLE_BLOCK *p_block_free_list;          ARTICLE_BLOCK *p_block_free_list;
73          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
74          int32_t block_count;          int block_count;
75  };  };
76  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
77    
78  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
79    
80  static int section_list_pool_shmid;  struct section_list_pool_t
81  static SECTION_LIST *p_section_list_pool = NULL;  {
82  static int section_list_count = 0;          int shmid;
83  static TRIE_NODE *p_trie_dict_section_list = NULL;          SECTION_LIST sections[BBS_max_section];
84            int section_count;
85            int semid;
86            TRIE_NODE *p_trie_dict_section_by_name;
87            TRIE_NODE *p_trie_dict_section_by_sid;
88    };
89    typedef struct section_list_pool_t SECTION_LIST_POOL;
90    
91    static SECTION_LIST_POOL *p_section_list_pool = NULL;
92    
93  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
94  {  {
# Line 89  int article_block_init(const char *filen Line 114  int article_block_init(const char *filen
114                  return -2;                  return -2;
115          }          }
116    
117          p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL));          // Allocate shared memory
118          if (p_article_block_pool == NULL)          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm
119            key = ftok(filename, proj_id);
120            if (key == -1)
121          {          {
122                  log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n");                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
123                  return -2;                  return -3;
124          }          }
125    
126          // Allocate shared memory          size = sizeof(ARTICLE_BLOCK_POOL);
127            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
128            if (shmid == -1)
129            {
130                    log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);
131                    return -3;
132            }
133            p_shm = shmat(shmid, NULL, 0);
134            if (p_shm == (void *)-1)
135            {
136                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
137                    return -3;
138            }
139    
140            p_article_block_pool = p_shm;
141            p_article_block_pool->shmid = shmid;
142    
143          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
144          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
145    
# Line 105  int article_block_init(const char *filen Line 148  int article_block_init(const char *filen
148                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
149                  block_count -= block_count_in_shm;                  block_count -= block_count_in_shm;
150    
151                  proj_id = getpid() + p_article_block_pool->shm_count;                  proj_id = p_article_block_pool->shm_count;
152                  key = ftok(filename, proj_id);                  key = ftok(filename, proj_id);
153                  if (key == -1)                  if (key == -1)
154                  {                  {
# Line 114  int article_block_init(const char *filen Line 157  int article_block_init(const char *filen
157                          return -3;                          return -3;
158                  }                  }
159    
160                  size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
161                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
162                  if (shmid == -1)                  if (shmid == -1)
163                  {                  {
# Line 158  int article_block_init(const char *filen Line 201  int article_block_init(const char *filen
201    
202  void article_block_cleanup(void)  void article_block_cleanup(void)
203  {  {
204          if (p_article_block_pool != NULL)          int shmid;
205    
206            if (p_article_block_pool == NULL)
207          {          {
208                  for (int i = 0; i < p_article_block_pool->shm_count; i++)                  return;
209            }
210    
211            for (int i = 0; i < p_article_block_pool->shm_count; i++)
212            {
213                    if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
214                  {                  {
215                          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);
216                          {                  }
                                 log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);  
                         }  
217    
218                          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)
219                          {                  {
220                                  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);
                         }  
221                  }                  }
222            }
223    
224            shmid = p_article_block_pool->shmid;
225    
226            if (shmdt(p_article_block_pool) == -1)
227            {
228                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
229            }
230    
231            if (shmctl(shmid, IPC_RMID, NULL) == -1)
232            {
233                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
234            }
235    
236            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                  free(p_article_block_pool);          for (i = 0; i < p_article_block_pool->shm_count; i++)
252                  p_article_block_pool = NULL;          {
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)
# Line 297  ARTICLE *article_block_find_by_index(int Line 414  ARTICLE *article_block_find_by_index(int
414    
415          if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)          if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)
416          {          {
417                  log_error("section_data_find_article_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);                  log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);
418                  return NULL;                  return NULL;
419          }          }
420    
# Line 305  ARTICLE *article_block_find_by_index(int Line 422  ARTICLE *article_block_find_by_index(int
422    
423          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)
424          {          {
425                  log_error("section_data_find_article_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);                  log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);
426                  return NULL;                  return NULL;
427          }          }
428    
429          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % ARTICLE_PER_BLOCK));
430  }  }
431    
432  extern int section_list_pool_init(const char *filename)  extern int section_list_init(const char *filename)
433  {  {
434            int semid;
435          int shmid;          int shmid;
436          int proj_id;          int proj_id;
437          key_t key;          key_t key;
438          size_t size;          size_t size;
439          void *p_shm;          void *p_shm;
440            union semun arg;
441            int i;
442    
443          if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL)          if (p_section_list_pool != NULL)
         {  
                 section_list_pool_cleanup();  
         }  
   
         p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST));  
         if (p_section_list_pool == NULL)  
444          {          {
445                  log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);                  section_list_cleanup();
                 return -1;  
446          }          }
447    
448          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
# Line 340  extern int section_list_pool_init(const Line 453  extern int section_list_pool_init(const
453                  return -3;                  return -3;
454          }          }
455    
456          size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section;          // Allocate shared memory
457            size = sizeof(SECTION_LIST_POOL);
458          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
459          if (shmid == -1)          if (shmid == -1)
460          {          {
461                  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);
462                  return -3;                  return -3;
463          }          }
464          p_shm = shmat(shmid, NULL, 0);          p_shm = shmat(shmid, NULL, 0);
# Line 354  extern int section_list_pool_init(const Line 468  extern int section_list_pool_init(const
468                  return -3;                  return -3;
469          }          }
470    
         section_list_pool_shmid = shmid;  
471          p_section_list_pool = p_shm;          p_section_list_pool = p_shm;
472          section_list_count = 0;          p_section_list_pool->shmid = shmid;
473            p_section_list_pool->section_count = 0;
474    
475            // Allocate semaphore as section locks
476            size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
477            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
478            if (semid == -1)
479            {
480                    log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
481                    return -3;
482            }
483    
484          p_trie_dict_section_list = trie_dict_create();          // Initialize sem value to 0
485          if (p_trie_dict_section_list == NULL)          arg.val = 0;
486            for (i = 0; i < size; i++)
487            {
488                    if (semctl(semid, i, SETVAL, arg) == -1)
489                    {
490                            log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
491                            return -3;
492                    }
493            }
494    
495            p_section_list_pool->semid = semid;
496    
497            p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
498            if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
499            {
500                    log_error("trie_dict_create() OOM\n", BBS_max_section);
501                    return -2;
502            }
503    
504            p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
505            if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
506          {          {
507                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
508                  return -2;                  return -2;
# Line 368  extern int section_list_pool_init(const Line 511  extern int section_list_pool_init(const
511          return 0;          return 0;
512  }  }
513    
514    void section_list_cleanup(void)
515    {
516            int shmid;
517    
518            if (p_section_list_pool == NULL)
519            {
520                    return;
521            }
522    
523            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
524            {
525                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
526                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
527            }
528    
529            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
530            {
531                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
532                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
533            }
534    
535            shmid = p_section_list_pool->shmid;
536    
537            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
538            {
539                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
540            }
541    
542            if (shmdt(p_section_list_pool) == -1)
543            {
544                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
545            }
546    
547            if (shmctl(shmid, IPC_RMID, NULL) == -1)
548            {
549                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
550            }
551    
552            p_section_list_pool = NULL;
553    }
554    
555    int set_section_list_shm_readonly(void)
556    {
557            int shmid;
558            void *p_shm;
559    
560            if (p_section_list_pool == NULL)
561            {
562                    log_error("p_section_list_pool not initialized\n");
563                    return -1;
564            }
565    
566            shmid = p_section_list_pool->shmid;
567    
568            // Remap shared memory in read-only mode
569            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
570            if (p_shm == (void *)-1)
571            {
572                    log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
573                    return -3;
574            }
575    
576            p_section_list_pool = p_shm;
577    
578            return 0;
579    }
580    
581    int detach_section_list_shm(void)
582    {
583            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
584            {
585                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
586                    return -1;
587            }
588    
589            p_section_list_pool = NULL;
590    
591            return 0;
592    }
593    
594    inline static void sid_to_str(int32_t sid, char *p_sid_str)
595    {
596            uint32_t u_sid;
597            int i;
598    
599            u_sid = (uint32_t)sid;
600            for (i = 0; i < SID_STR_LEN - 1; i++)
601            {
602                    p_sid_str[i] = (char)(u_sid % 255 + 1);
603                    u_sid /= 255;
604            }
605            p_sid_str[i] = '\0';
606    }
607    
608  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name)  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name)
609  {  {
610          SECTION_LIST *p_section;          SECTION_LIST *p_section;
611            char sid_str[SID_STR_LEN];
612    
613          if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL)          if (p_section_list_pool == NULL)
614          {          {
615                  log_error("session_list_pool not initialized\n");                  log_error("session_list_pool not initialized\n");
616                  return NULL;                  return NULL;
617          }          }
618    
619          if (section_list_count >= BBS_max_section)          if (p_section_list_pool->section_count >= BBS_max_section)
620          {          {
621                  log_error("section_list_count exceed limit %d >= %d\n", section_list_count, BBS_max_section);                  log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section);
622                  return NULL;                  return NULL;
623          }          }
624    
625          p_section = p_section_list_pool + section_list_count;          sid_to_str(sid, sid_str);
626    
627            p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
628    
629          p_section->sid = sid;          p_section->sid = sid;
630    
# Line 397  SECTION_LIST *section_list_create(int32_ Line 637  SECTION_LIST *section_list_create(int32_
637          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));
638          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';
639    
640          if (trie_dict_set(p_trie_dict_section_list, sname, section_list_count) != 1)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
641          {          {
642                  log_error("trie_dict_set(section_data, %s, %d) error\n", sname, section_list_count);                  log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count);
643                    return NULL;
644            }
645    
646            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)
647            {
648                    log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count);
649                  return NULL;                  return NULL;
650          }          }
651    
652          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
653    
654          section_list_count++;          p_section_list_pool->section_count++;
655    
656          return p_section;          return p_section;
657  }  }
# Line 423  void section_list_reset_articles(SECTION Line 669  void section_list_reset_articles(SECTION
669          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
670  }  }
671    
672  void section_list_pool_cleanup(void)  SECTION_LIST *section_list_find_by_name(const char *sname)
673  {  {
674          if (p_trie_dict_section_list != NULL)          int64_t index;
675            int ret;
676    
677            if (p_section_list_pool == NULL)
678          {          {
679                  trie_dict_destroy(p_trie_dict_section_list);                  log_error("section_list not initialized\n");
680                  p_trie_dict_section_list = NULL;                  return NULL;
681          }          }
682    
683          if (p_section_list_pool != NULL)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
684            if (ret < 0)
685          {          {
686                  if (shmdt(p_section_list_pool) == -1)                  log_error("trie_dict_get(section, %s) error\n", sname);
687                  {                  return NULL;
688                          log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno);          }
689                  }          else if (ret == 0)
690                  p_section_list_pool = NULL;          {
691                    return NULL;
                 if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1)  
                 {  
                         log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno);  
                 }  
692          }          }
693    
694          section_list_count = 0;          return (p_section_list_pool->sections + index);
695  }  }
696    
697  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
698  {  {
699          int64_t index;          int64_t index;
700            int ret;
701            char sid_str[SID_STR_LEN];
702    
703          if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL)          if (p_section_list_pool == NULL)
704          {          {
705                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
706                  return NULL;                  return NULL;
707          }          }
708    
709          if (trie_dict_get(p_trie_dict_section_list, sname, &index) != 1)          sid_to_str(sid, sid_str);
710    
711            ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
712            if (ret < 0)
713            {
714                    log_error("trie_dict_get(section, %d) error\n", sid);
715                    return NULL;
716            }
717            else if (ret == 0)
718          {          {
                 log_error("trie_dict_get(section_data, %s) error\n", sname);  
719                  return NULL;                  return NULL;
720          }          }
721    
722          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
723  }  }
724    
725  int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src)  int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src)
# Line 487  int section_list_append_article(SECTION_ Line 742  int section_list_append_article(SECTION_
742                  return -1;                  return -1;
743          }          }
744    
745            if (p_section->sid != p_article_src->sid)
746            {
747                    log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid);
748                    return -2;
749            }
750    
751          if (p_section->article_count >= BBS_article_limit_per_section)          if (p_section->article_count >= BBS_article_limit_per_section)
752          {          {
753                  log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);                  log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);
# Line 519  int section_list_append_article(SECTION_ Line 780  int section_list_append_article(SECTION_
780          // AID of articles should be strictly ascending          // AID of articles should be strictly ascending
781          if (p_article_src->aid <= last_aid)          if (p_article_src->aid <= last_aid)
782          {          {
783                  log_error("section_data_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid);                  log_error("section_list_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid);
784                  return -3;                  return -3;
785          }          }
786    
# Line 617  int section_list_set_article_visible(SEC Line 878  int section_list_set_article_visible(SEC
878                  return -1; // Not found                  return -1; // Not found
879          }          }
880    
881            if (p_section->sid != p_article->sid)
882            {
883                    log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid);
884                    return -2;
885            }
886    
887          if (p_article->visible == visible)          if (p_article->visible == visible)
888          {          {
889                  return 0; // Already set                  return 0; // Already set
# Line 783  int section_list_calculate_page(SECTION_ Line 1050  int section_list_calculate_page(SECTION_
1050    
1051          if (start_aid > 0)          if (start_aid > 0)
1052          {          {
1053                    p_article = article_block_find_by_aid(start_aid);
1054                    if (p_article == NULL)
1055                    {
1056                            return -1; // Not found
1057                    }
1058    
1059                    if (p_section->sid != p_article->sid)
1060                    {
1061                            log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid);
1062                            return -2;
1063                    }
1064    
1065                  p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);                  p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);
1066                  if (p_article == NULL)                  if (p_article == NULL)
1067                  {                  {
# Line 851  int section_list_calculate_page(SECTION_ Line 1130  int section_list_calculate_page(SECTION_
1130          return 0;          return 0;
1131  }  }
1132    
1133  int section_list_count_of_topic_articles(int32_t aid)  int32_t article_block_last_aid(void)
1134    {
1135            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1136            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1137    
1138            return last_aid;
1139    }
1140    
1141    int article_count_of_topic(int32_t aid)
1142  {  {
1143          ARTICLE *p_article;          ARTICLE *p_article;
1144          int article_count;          int article_count;
# Line 866  int section_list_count_of_topic_articles Line 1153  int section_list_count_of_topic_articles
1153    
1154          do          do
1155          {          {
1156                    if (p_article->tid != 0 && p_article->tid != aid)
1157                    {
1158                            log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid);
1159                            break;
1160                    }
1161    
1162                  article_count++;                  article_count++;
1163                  p_article = p_article->p_topic_next;                  p_article = p_article->p_topic_next;
1164          } while (p_article->aid != aid);          } while (p_article->aid != aid);
# Line 891  int section_list_move_topic(SECTION_LIST Line 1184  int section_list_move_topic(SECTION_LIST
1184                  return -1;                  return -1;
1185          }          }
1186    
1187          if ((p_article = section_list_find_article_with_offset(p_section_src, aid, &page, &offset, &p_next)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1188          {          {
1189                  log_error("section_list_move_topic() error: article %d not found in section %d\n", aid, p_section_src->sid);                  log_error("section_list_move_topic() error: article %d not found in block\n", aid);
1190                    return -2;
1191            }
1192    
1193            if (p_section_src->sid != p_article->sid)
1194            {
1195                    log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1196                                      p_section_src->sid, p_article->aid, p_article->sid);
1197                  return -2;                  return -2;
1198          }          }
1199    
# Line 905  int section_list_move_topic(SECTION_LIST Line 1205  int section_list_move_topic(SECTION_LIST
1205    
1206          last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);          last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);
1207    
1208          move_article_count = section_list_count_of_topic_articles(aid);          move_article_count = article_count_of_topic(aid);
1209          if (move_article_count <= 0)          if (move_article_count <= 0)
1210          {          {
1211                  log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1212                  return -2;                  return -2;
1213          }          }
1214    
# Line 925  int section_list_move_topic(SECTION_LIST Line 1225  int section_list_move_topic(SECTION_LIST
1225    
1226          do          do
1227          {          {
1228                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)                  if (p_section_src->sid != p_article->sid)
1229                  {                  {
1230                          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() error: src section sid %d != article %d sid %d\n",
1231                          return -4;                                            p_section_src->sid, p_article->aid, p_article->sid);
1232                            return -2;
1233                  }                  }
1234    
1235                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 949  int section_list_move_topic(SECTION_LIST Line 1250  int section_list_move_topic(SECTION_LIST
1250                  p_article->p_prior->p_next = p_article->p_next;                  p_article->p_prior->p_next = p_article->p_next;
1251                  p_article->p_next->p_prior = p_article->p_prior;                  p_article->p_next->p_prior = p_article->p_prior;
1252    
1253                    // Update sid of article
1254                    p_article->sid = p_section_dest->sid;
1255    
1256                    if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1257                    {
1258                            log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1259                            return -4;
1260                    }
1261    
1262                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
1263                  if (p_next == NULL) // empty section                  if (p_next == NULL) // empty section
1264                  {                  {
# Line 1014  int section_list_move_topic(SECTION_LIST Line 1324  int section_list_move_topic(SECTION_LIST
1324                          // Re-calculate pages of desc section                          // Re-calculate pages of desc section
1325                          if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)                          if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1326                          {                          {
1327                                  log_error("section_list_calculate_page(section = %d, aid = %d) error\n",                                  log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1328                                                    p_section_dest->sid, first_inserted_aid_dest);                                                    p_section_dest->sid, first_inserted_aid_dest);
1329                          }                          }
1330    
# Line 1031  int section_list_move_topic(SECTION_LIST Line 1341  int section_list_move_topic(SECTION_LIST
1341          // Re-calculate pages of src section          // Re-calculate pages of src section
1342          if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)          if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)
1343          {          {
1344                  log_error("section_list_calculate_page(section = %d, aid = %d) error at aid = %d\n",                  log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n",
1345                                    p_section_src->sid, last_unaffected_aid_src, aid);                                    p_section_src->sid, last_unaffected_aid_src, aid);
1346          }          }
1347    
# Line 1040  int section_list_move_topic(SECTION_LIST Line 1350  int section_list_move_topic(SECTION_LIST
1350                  // Re-calculate pages of desc section                  // Re-calculate pages of desc section
1351                  if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)                  if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1352                  {                  {
1353                          log_error("section_list_calculate_page(section = %d, aid = %d) error\n",                          log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1354                                            p_section_dest->sid, first_inserted_aid_dest);                                            p_section_dest->sid, first_inserted_aid_dest);
1355                  }                  }
1356          }          }
1357    
1358          return move_article_count;          return move_article_count;
1359  }  }
1360    
1361    int get_section_index(SECTION_LIST *p_section)
1362    {
1363            int index;
1364    
1365            if (p_section_list_pool == NULL)
1366            {
1367                    log_error("get_section_index() error: uninitialized\n");
1368                    return -1;
1369            }
1370    
1371            if (p_section == NULL)
1372            {
1373                    index = BBS_max_section;
1374            }
1375            else
1376            {
1377                    index = (int)(p_section - p_section_list_pool->sections);
1378                    if (index < 0 || index >= BBS_max_section)
1379                    {
1380                            log_error("get_section_index(%d) error: index out of range\n", index);
1381                            return -2;
1382                    }
1383            }
1384    
1385            return index;
1386    }
1387    
1388    int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1389    {
1390            int index;
1391            struct sembuf sops[4];
1392            struct timespec timeout;
1393            int ret;
1394    
1395            index = get_section_index(p_section);
1396            if (index < 0)
1397            {
1398                    return -2;
1399            }
1400    
1401            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1402            sops[0].sem_op = 0;                                                                // wait until unlocked
1403            sops[0].sem_flg = 0;
1404    
1405            sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1406            sops[1].sem_op = 1;                                                        // lock
1407            sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1408    
1409            // Read lock on any specific section will also acquire single read lock on "all section"
1410            // so that write lock on all section only need to acquire single write on on "all section"
1411            // rather than to acquire multiple write locks on all the available sections.
1412            if (index != BBS_max_section)
1413            {
1414                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1415                    sops[2].sem_op = 0;                                                // wait until unlocked
1416                    sops[2].sem_flg = 0;
1417    
1418                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1419                    sops[3].sem_op = 1;                                        // lock
1420                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1421            }
1422    
1423            timeout.tv_sec = wait_sec;
1424            timeout.tv_nsec = 0;
1425    
1426            ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1427            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1428            {
1429                    log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1430            }
1431    
1432            return ret;
1433    }
1434    
1435    int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1436    {
1437            int index;
1438            struct sembuf sops[3];
1439            struct timespec timeout;
1440            int ret;
1441    
1442            index = get_section_index(p_section);
1443            if (index < 0)
1444            {
1445                    return -2;
1446            }
1447    
1448            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1449            sops[0].sem_op = 0;                                                                // wait until unlocked
1450            sops[0].sem_flg = 0;
1451    
1452            sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1453            sops[1].sem_op = 1;                                                                // lock
1454            sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1455    
1456            sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1457            sops[2].sem_op = 0;                                                        // wait until unlocked
1458            sops[2].sem_flg = 0;
1459    
1460            timeout.tv_sec = wait_sec;
1461            timeout.tv_nsec = 0;
1462    
1463            ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1464            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1465            {
1466                    log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1467            }
1468    
1469            return ret;
1470    }
1471    
1472    int section_list_rd_unlock(SECTION_LIST *p_section)
1473    {
1474            int index;
1475            struct sembuf sops[2];
1476            int ret;
1477    
1478            index = get_section_index(p_section);
1479            if (index < 0)
1480            {
1481                    return -2;
1482            }
1483    
1484            sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1485            sops[0].sem_op = -1;                                               // unlock
1486            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1487    
1488            if (index != BBS_max_section)
1489            {
1490                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1491                    sops[1].sem_op = -1;                                     // unlock
1492                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1493            }
1494    
1495            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1496            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1497            {
1498                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1499            }
1500    
1501            return ret;
1502    }
1503    
1504    int section_list_rw_unlock(SECTION_LIST *p_section)
1505    {
1506            int index;
1507            struct sembuf sops[1];
1508            int ret;
1509    
1510            index = get_section_index(p_section);
1511            if (index < 0)
1512            {
1513                    return -2;
1514            }
1515    
1516            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1517            sops[0].sem_op = -1;                                                       // unlock
1518            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1519    
1520            ret = semop(p_section_list_pool->semid, sops, 1);
1521            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1522            {
1523                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1524            }
1525    
1526            return ret;
1527    }
1528    
1529    int section_list_rd_lock(SECTION_LIST *p_section)
1530    {
1531            int timer = 0;
1532            int sid = (p_section == NULL ? 0 : p_section->sid);
1533            int ret = -1;
1534    
1535            while (!SYS_server_exit)
1536            {
1537                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1538                    if (ret == 0) // success
1539                    {
1540                            break;
1541                    }
1542                    else if (errno == EAGAIN || errno == EINTR) // retry
1543                    {
1544                            timer++;
1545                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1546                            {
1547                                    log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer);
1548                            }
1549                    }
1550                    else // failed
1551                    {
1552                            log_error("section_list_rd_lock() failed on section %d\n", sid);
1553                            break;
1554                    }
1555            }
1556    
1557            return ret;
1558    }
1559    
1560    int section_list_rw_lock(SECTION_LIST *p_section)
1561    {
1562            int timer = 0;
1563            int sid = (p_section == NULL ? 0 : p_section->sid);
1564            int ret = -1;
1565    
1566            while (!SYS_server_exit)
1567            {
1568                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1569                    if (ret == 0) // success
1570                    {
1571                            break;
1572                    }
1573                    else if (errno == EAGAIN || errno == EINTR) // retry
1574                    {
1575                            timer++;
1576                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1577                            {
1578                                    log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer);
1579                            }
1580                    }
1581                    else // failed
1582                    {
1583                            log_error("acquire_section_rw_lock() failed on section %d\n", sid);
1584                            break;
1585                    }
1586            }
1587    
1588            return ret;
1589    }


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

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