/[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.12 by sysadm, Fri May 23 10:45:54 2025 UTC Revision 1.31 by sysadm, Thu Jun 5 11:12:11 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 moving topic between sections
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 SECTION_LIST *p_section_list_pool = NULL;  struct section_list_pool_t
81  static int section_list_count = 0;  {
82  static TRIE_NODE *p_trie_dict_section_list = NULL;          int shmid;
83            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 82  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;
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 104  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 113  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 157  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                    return;
209            }
210    
211            for (int i = 0; i < p_article_block_pool->shm_count; i++)
212          {          {
213                  for (int i = 0; i < p_article_block_pool->shm_count; i++)                  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                  free(p_article_block_pool);          if (shmctl(shmid, IPC_RMID, NULL) == -1)
232                  p_article_block_pool = NULL;          {
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            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)
# Line 281  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 296  ARTICLE *article_block_find_by_index(int Line 419  ARTICLE *article_block_find_by_index(int
419    
420          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)
421          {          {
422                  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);
423                  return NULL;                  return NULL;
424          }          }
425    
# Line 304  ARTICLE *article_block_find_by_index(int Line 427  ARTICLE *article_block_find_by_index(int
427    
428          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)
429          {          {
430                  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);
431                  return NULL;                  return NULL;
432          }          }
433    
434          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % ARTICLE_PER_BLOCK));
435  }  }
436    
437  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name)  extern int section_list_init(const char *filename)
438  {  {
439          SECTION_LIST *p_section;          int semid;
440            int shmid;
441            int proj_id;
442            key_t key;
443            size_t size;
444            void *p_shm;
445            union semun arg;
446            int i;
447    
448          if (p_section_list_pool == NULL)          if (p_section_list_pool != NULL)
449          {          {
450                  p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST));                  section_list_cleanup();
451                  if (p_section_list_pool == NULL)          }
452                  {  
453                          log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);          proj_id = (int)(time(NULL) % getpid());
454                          return NULL;          key = ftok(filename, proj_id);
455                  }          if (key == -1)
456            {
457                    log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
458                    return -3;
459            }
460    
461                  section_list_count = 0;          // Allocate shared memory
462            size = sizeof(SECTION_LIST_POOL);
463            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
464            if (shmid == -1)
465            {
466                    log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
467                    return -3;
468            }
469            p_shm = shmat(shmid, NULL, 0);
470            if (p_shm == (void *)-1)
471            {
472                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
473                    return -3;
474          }          }
475    
476          if (p_trie_dict_section_list == NULL)          p_section_list_pool = p_shm;
477            p_section_list_pool->shmid = shmid;
478            p_section_list_pool->section_count = 0;
479    
480            // Allocate semaphore as section locks
481            size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
482            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
483            if (semid == -1)
484          {          {
485                  p_trie_dict_section_list = trie_dict_create();                  log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
486                  if (p_trie_dict_section_list == NULL)                  return -3;
487            }
488    
489            // Initialize sem value to 0
490            arg.val = 0;
491            for (i = 0; i < size; i++)
492            {
493                    if (semctl(semid, i, SETVAL, arg) == -1)
494                  {                  {
495                          log_error("trie_dict_create() OOM\n", BBS_max_section);                          log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
496                          return NULL;                          return -3;
497                  }                  }
498          }          }
499    
500          if (section_list_count >= BBS_max_section)          p_section_list_pool->semid = semid;
501    
502            p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
503            if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
504            {
505                    log_error("trie_dict_create() OOM\n", BBS_max_section);
506                    return -2;
507            }
508    
509            p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
510            if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
511            {
512                    log_error("trie_dict_create() OOM\n", BBS_max_section);
513                    return -2;
514            }
515    
516            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)
600    {
601            uint32_t u_sid;
602            int i;
603    
604            u_sid = (uint32_t)sid;
605            for (i = 0; i < SID_STR_LEN - 1; i++)
606          {          {
607                  log_error("section_list_count exceed limit %d\n", BBS_max_section);                  p_sid_str[i] = (char)(u_sid % 255 + 1);
608                    u_sid /= 255;
609            }
610            p_sid_str[i] = '\0';
611    }
612    
613    SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_list)
614    {
615            SECTION_LIST *p_section;
616            char sid_str[SID_STR_LEN];
617    
618            if (p_section_list_pool == NULL)
619            {
620                    log_error("session_list_pool not initialized\n");
621                    return NULL;
622            }
623    
624            if (p_section_list_pool->section_count >= BBS_max_section)
625            {
626                    log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section);
627                  return NULL;                  return NULL;
628          }          }
629    
630          p_section = p_section_list_pool + section_list_count;          sid_to_str(sid, sid_str);
631    
632            p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
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_list, 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_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)
646          {          {
647                  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);
648                    return NULL;
649            }
650    
651            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)
652            {
653                    log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count);
654                  return NULL;                  return NULL;
655          }          }
656    
657          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
658    
659          section_list_count++;          p_section_list_pool->section_count++;
660    
661          return p_section;          return p_section;
662  }  }
# Line 382  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    
677  void section_list_cleanup(void)  SECTION_LIST *section_list_find_by_name(const char *sname)
678  {  {
679          if (p_trie_dict_section_list != NULL)          int64_t index;
680            int ret;
681    
682            if (p_section_list_pool == NULL)
683          {          {
684                  trie_dict_destroy(p_trie_dict_section_list);                  log_error("section_list not initialized\n");
685                  p_trie_dict_section_list = NULL;                  return NULL;
686          }          }
687    
688          if (p_section_list_pool != NULL)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
689            if (ret < 0)
690          {          {
691                  free(p_section_list_pool);                  log_error("trie_dict_get(section, %s) error\n", sname);
692                  p_section_list_pool = NULL;                  return NULL;
693            }
694            else if (ret == 0)
695            {
696                    return NULL;
697          }          }
698    
699          section_list_count = 0;          return (p_section_list_pool->sections + index);
700  }  }
701    
702  SECTION_LIST *section_list_find_by_name(const char *sname)  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];
707    
708          if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL)          if (p_section_list_pool == NULL)
709          {          {
710                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
711                  return NULL;                  return NULL;
712          }          }
713    
714          if (trie_dict_get(p_trie_dict_section_list, sname, &index) != 1)          sid_to_str(sid, sid_str);
715    
716            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);
720                    return NULL;
721            }
722            else if (ret == 0)
723          {          {
                 log_error("trie_dict_get(section_data, %s) error\n", sname);  
724                  return NULL;                  return NULL;
725          }          }
726    
727          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
728  }  }
729    
730  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 438  int section_list_append_article(SECTION_ Line 747  int section_list_append_article(SECTION_
747                  return -1;                  return -1;
748          }          }
749    
750            if (p_section->sid != p_article_src->sid)
751            {
752                    log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid);
753                    return -2;
754            }
755    
756          if (p_section->article_count >= BBS_article_limit_per_section)          if (p_section->article_count >= BBS_article_limit_per_section)
757          {          {
758                  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 470  int section_list_append_article(SECTION_ Line 785  int section_list_append_article(SECTION_
785          // AID of articles should be strictly ascending          // AID of articles should be strictly ascending
786          if (p_article_src->aid <= last_aid)          if (p_article_src->aid <= last_aid)
787          {          {
788                  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);
789                  return -3;                  return -3;
790          }          }
791    
# Line 568  int section_list_set_article_visible(SEC Line 883  int section_list_set_article_visible(SEC
883                  return -1; // Not found                  return -1; // Not found
884          }          }
885    
886            if (p_section->sid != p_article->sid)
887            {
888                    log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid);
889                    return -2;
890            }
891    
892          if (p_article->visible == visible)          if (p_article->visible == visible)
893          {          {
894                  return 0; // Already set                  return 0; // Already set
# Line 734  int section_list_calculate_page(SECTION_ Line 1055  int section_list_calculate_page(SECTION_
1055    
1056          if (start_aid > 0)          if (start_aid > 0)
1057          {          {
1058                    p_article = article_block_find_by_aid(start_aid);
1059                    if (p_article == NULL)
1060                    {
1061                            return -1; // Not found
1062                    }
1063    
1064                    if (p_section->sid != p_article->sid)
1065                    {
1066                            log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid);
1067                            return -2;
1068                    }
1069    
1070                  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);
1071                  if (p_article == NULL)                  if (p_article == NULL)
1072                  {                  {
# Line 802  int section_list_calculate_page(SECTION_ Line 1135  int section_list_calculate_page(SECTION_
1135          return 0;          return 0;
1136  }  }
1137    
1138  int section_list_count_of_topic_articles(int32_t aid)  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)
1162  {  {
1163          ARTICLE *p_article;          ARTICLE *p_article;
1164          int article_count;          int article_count;
# Line 817  int section_list_count_of_topic_articles Line 1173  int section_list_count_of_topic_articles
1173    
1174          do          do
1175          {          {
1176                    if (p_article->tid != 0 && p_article->tid != aid)
1177                    {
1178                            log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid);
1179                            break;
1180                    }
1181    
1182                  article_count++;                  article_count++;
1183                  p_article = p_article->p_topic_next;                  p_article = p_article->p_topic_next;
1184          } while (p_article->aid != aid);          } while (p_article->aid != aid);
# Line 836  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_article = section_list_find_article_with_offset(p_section_src, aid, &page, &offset, &p_next)) == NULL)          if (p_section_src->sid == p_section_dest->sid)
1208          {          {
1209                  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() src and dest section are the same\n");
1210                    return -1;
1211            }
1212    
1213            if ((p_article = article_block_find_by_aid(aid)) == NULL)
1214            {
1215                    log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);
1216                    return -2;
1217            }
1218    
1219            if (p_section_src->sid != p_article->sid)
1220            {
1221                    log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1222                                      p_section_src->sid, p_article->aid, p_article->sid);
1223                  return -2;                  return -2;
1224          }          }
1225    
# Line 856  int section_list_move_topic(SECTION_LIST Line 1231  int section_list_move_topic(SECTION_LIST
1231    
1232          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);
1233    
1234          move_article_count = section_list_count_of_topic_articles(aid);          move_article_count = article_count_of_topic(aid);
1235          if (move_article_count <= 0)          if (move_article_count <= 0)
1236          {          {
1237                  log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1238                  return -2;                  return -2;
1239          }          }
1240    
# Line 876  int section_list_move_topic(SECTION_LIST Line 1251  int section_list_move_topic(SECTION_LIST
1251    
1252          do          do
1253          {          {
1254                  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)
1255                  {                  {
1256                          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: src section sid %d != article %d sid %d\n",
1257                          return -4;                                            p_section_src->sid, p_article->aid, p_article->sid);
1258                            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 900  int section_list_move_topic(SECTION_LIST Line 1277  int section_list_move_topic(SECTION_LIST
1277                  p_article->p_prior->p_next = p_article->p_next;                  p_article->p_prior->p_next = p_article->p_next;
1278                  p_article->p_next->p_prior = p_article->p_prior;                  p_article->p_next->p_prior = p_article->p_prior;
1279    
1280                    // Update sid of article
1281                    p_article->sid = p_section_dest->sid;
1282    
1283                    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() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1286                            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
1291                  if (p_next == NULL) // empty section                  if (p_next == NULL) // empty section
1292                  {                  {
# Line 965  int section_list_move_topic(SECTION_LIST Line 1352  int section_list_move_topic(SECTION_LIST
1352                          // Re-calculate pages of desc section                          // Re-calculate pages of desc section
1353                          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)
1354                          {                          {
1355                                  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",
1356                                                    p_section_dest->sid, first_inserted_aid_dest);                                                    p_section_dest->sid, first_inserted_aid_dest);
1357                          }                          }
1358    
# Line 975  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    
1369          // Re-calculate pages of src section          // Re-calculate pages of src section
1370          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)
1371          {          {
1372                  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",
1373                                    p_section_src->sid, last_unaffected_aid_src, aid);                                    p_section_src->sid, last_unaffected_aid_src, aid);
1374          }          }
1375    
# Line 991  int section_list_move_topic(SECTION_LIST Line 1378  int section_list_move_topic(SECTION_LIST
1378                  // Re-calculate pages of desc section                  // Re-calculate pages of desc section
1379                  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)
1380                  {                  {
1381                          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",
1382                                            p_section_dest->sid, first_inserted_aid_dest);                                            p_section_dest->sid, first_inserted_aid_dest);
1383                  }                  }
1384          }          }
1385    
1386          return move_article_count;          return move_article_count;
1387  }  }
1388    
1389    int get_section_index(SECTION_LIST *p_section)
1390    {
1391            int index;
1392    
1393            if (p_section_list_pool == NULL)
1394            {
1395                    log_error("get_section_index() error: uninitialized\n");
1396                    return -1;
1397            }
1398    
1399            if (p_section == NULL)
1400            {
1401                    index = BBS_max_section;
1402            }
1403            else
1404            {
1405                    index = (int)(p_section - p_section_list_pool->sections);
1406                    if (index < 0 || index >= BBS_max_section)
1407                    {
1408                            log_error("get_section_index(%d) error: index out of range\n", index);
1409                            return -2;
1410                    }
1411            }
1412    
1413            return index;
1414    }
1415    
1416    int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1417    {
1418            int index;
1419            struct sembuf sops[4];
1420            struct timespec timeout;
1421            int ret;
1422    
1423            index = get_section_index(p_section);
1424            if (index < 0)
1425            {
1426                    return -2;
1427            }
1428    
1429            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1430            sops[0].sem_op = 0;                                                                // wait until unlocked
1431            sops[0].sem_flg = 0;
1432    
1433            sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1434            sops[1].sem_op = 1;                                                        // lock
1435            sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1436    
1437            // Read lock on any specific section will also acquire single read lock on "all section"
1438            // so that write lock on all section only need to acquire single write on on "all section"
1439            // rather than to acquire multiple write locks on all the available sections.
1440            if (index != BBS_max_section)
1441            {
1442                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1443                    sops[2].sem_op = 0;                                                // wait until unlocked
1444                    sops[2].sem_flg = 0;
1445    
1446                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1447                    sops[3].sem_op = 1;                                        // lock
1448                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1449            }
1450    
1451            timeout.tv_sec = wait_sec;
1452            timeout.tv_nsec = 0;
1453    
1454            ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1455            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1456            {
1457                    log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1458            }
1459    
1460            return ret;
1461    }
1462    
1463    int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1464    {
1465            int index;
1466            struct sembuf sops[3];
1467            struct timespec timeout;
1468            int ret;
1469    
1470            index = get_section_index(p_section);
1471            if (index < 0)
1472            {
1473                    return -2;
1474            }
1475    
1476            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1477            sops[0].sem_op = 0;                                                                // wait until unlocked
1478            sops[0].sem_flg = 0;
1479    
1480            sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1481            sops[1].sem_op = 1;                                                                // lock
1482            sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1483    
1484            sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1485            sops[2].sem_op = 0;                                                        // wait until unlocked
1486            sops[2].sem_flg = 0;
1487    
1488            timeout.tv_sec = wait_sec;
1489            timeout.tv_nsec = 0;
1490    
1491            ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1492            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1493            {
1494                    log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1495            }
1496    
1497            return ret;
1498    }
1499    
1500    int section_list_rd_unlock(SECTION_LIST *p_section)
1501    {
1502            int index;
1503            struct sembuf sops[2];
1504            int ret;
1505    
1506            index = get_section_index(p_section);
1507            if (index < 0)
1508            {
1509                    return -2;
1510            }
1511    
1512            sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1513            sops[0].sem_op = -1;                                               // unlock
1514            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1515    
1516            if (index != BBS_max_section)
1517            {
1518                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1519                    sops[1].sem_op = -1;                                     // unlock
1520                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1521            }
1522    
1523            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1524            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1525            {
1526                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1527            }
1528    
1529            return ret;
1530    }
1531    
1532    int section_list_rw_unlock(SECTION_LIST *p_section)
1533    {
1534            int index;
1535            struct sembuf sops[1];
1536            int ret;
1537    
1538            index = get_section_index(p_section);
1539            if (index < 0)
1540            {
1541                    return -2;
1542            }
1543    
1544            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1545            sops[0].sem_op = -1;                                                       // unlock
1546            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1547    
1548            ret = semop(p_section_list_pool->semid, sops, 1);
1549            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1550            {
1551                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1552            }
1553    
1554            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